def test_from_str_parse_dec_coin(): coin1 = Coin("uluna", 1001.5) coin2 = Coin.from_str("1001.500000000000000000uluna") assert coin1 == coin2 coin3 = Coin("uluna", "-1.0") coin2 = Coin.from_str("-1.000000000000000000uluna")
def test_eq(): coin1 = Coin("uluna", 1000) coin2 = Coin("uluna", 1000) coin3 = Coin("uluna", 1001) assert coin1 == coin2 assert coin1 != coin3
def test_from_str_parse_int_coin(): coin1 = Coin("uluna", 1001) coin2 = Coin.from_str("1001uluna") assert coin1 == coin2 coin3 = Coin("uluna", -1) coin4 = Coin.from_str("-1uluna") assert coin3 == coin4
def test_clobbers_similar_denom(): coins1 = Coins([Coin("ukrw", 1000), Coin("uluna", 1000), Coin("uluna", 1000)]) coinKRW = coins1["ukrw"] coinLUNA = coins1["uluna"] assert coinKRW.amount == 1000 assert coinLUNA.amount == 2000
async def pool(self) -> StakingPool: """Fetch current staking pool information. Returns: StakingPool: information about current staking pool """ res = await self._c._get("/staking/pool") return StakingPool( bonded_tokens=Coin("uluna", res["bonded_tokens"]), not_bonded_tokens=Coin("uluna", res["not_bonded_tokens"]), )
def from_data(cls, data: dict) -> PolicyConstraints: return cls( rate_min=Dec(data["rate_min"]), rate_max=Dec(data["rate_max"]), cap=Coin.from_data(data["cap"]), change_max=Dec(data["change_max"]), )
async def create( self, source_address: str, msgs: List[Msg], fee: Optional[StdFee] = None, memo: str = "", gas_prices: Optional[Coins.Input] = None, gas_adjustment: Optional[Numeric.Input] = None, denoms: Optional[List[str]] = None, account_number: Optional[int] = None, sequence: Optional[int] = None, ) -> StdSignMsg: if fee is None: # create the fake fee balance = await self._c.bank.balance(source_address) balance_one = [Coin(c.denom, 1) for c in balance] # estimate the fee tx = StdTx(msgs, StdFee(0, balance_one), [], memo) fee = await self.estimate_fee(tx, gas_prices, gas_adjustment, denoms) if account_number is None or sequence is None: account = await self._c.auth.account_info(source_address) if account_number is None: account_number = account.account_number if sequence is None: sequence = account.sequence return StdSignMsg(self._c.chain_id, account_number or 0, sequence or 0, fee, msgs, memo)
def from_data(cls, data: dict) -> Delegation: return cls( delegator_address=data["delegator_address"], validator_address=data["validator_address"], shares=data["shares"], balance=Coin.from_data(data["balance"]), )
def from_data(cls, data) -> AggregateExchangeRateVote: return cls( exchange_rate_tuples=Coins( [Coin(d.denom, d.exchange_rate) for d in data["exchange_rate_tuples"]], ), voter=data["voter"], )
def from_data(cls, data: dict) -> MsgSwap: data = data["value"] return cls( trader=data["trader"], offer_coin=Coin.from_data(data["offer_coin"]), ask_denom=data["ask_denom"], )
def from_data(cls, data: dict) -> MsgUndelegate: data = data["value"] return cls( delegator_address=data["delegator_address"], validator_address=data["validator_address"], amount=Coin.from_data(data["amount"]), )
def from_data(cls, data: dict) -> MsgSwapSend: data = data["value"] return cls( from_address=data["from_address"], to_address=data["to_address"], offer_coin=Coin.from_data(data["offer_coin"]), ask_denom=data["ask_denom"], )
async def seigniorage_proceeds(self) -> Coin: """Fetches the current seigniorage proceeds. Returns: Coin: seigniorage proceeds """ res = await self._c._get("/treasury/seigniorage_proceeds") return Coin("uluna", res)
async def create( self, source_address: AccAddress, msgs: List[Msg], fee: Optional[StdFee] = None, memo: str = "", gas_prices: Optional[Coins.Input] = None, gas_adjustment: Optional[Numeric.Input] = None, denoms: Optional[List[str]] = None, account_number: Optional[int] = None, sequence: Optional[int] = None, ) -> StdSignMsg: """Create a new unsigned transaction, with helpful utilities such as lookup of chain ID, account number, sequence and fee estimation. Args: source_address (AccAddress): transaction sender's account address msgs (List[Msg]): list of messages to include fee (Optional[StdFee], optional): fee to use (estimates if empty). memo (str, optional): memo to use. Defaults to "". gas_prices (Optional[Coins.Input], optional): gas prices for fee estimation. gas_adjustment (Optional[Numeric.Input], optional): gas adjustment for fee estimation. denoms (Optional[List[str]], optional): list of denoms to use for gas fee when estimating. account_number (Optional[int], optional): account number to use. sequence (Optional[int], optional): sequence number to use. Returns: StdSignMsg: unsigned tx """ if fee is None: # create the fake fee balance = await BaseAsyncAPI._try_await( self._c.bank.balance(source_address)) balance_one = [Coin(c.denom, 1) for c in balance] # estimate the fee tx = StdTx(msgs, StdFee(0, balance_one), [], memo) fee = await BaseAsyncAPI._try_await( self.estimate_fee(tx, gas_prices, gas_adjustment, denoms)) if account_number is None or sequence is None: account = await BaseAsyncAPI._try_await( self._c.auth.account_info(source_address)) if account_number is None: account_number = account.account_number if sequence is None: sequence = account.sequence return StdSignMsg( self._c.chain_id, account_number or 0, sequence or 0, fee, msgs, memo # type: ignore )
def from_data(cls, data: dict) -> MsgCreateValidator: data = data["value"] return cls( description=Description.from_data(data["description"]), commission=CommissionRates.from_data(data["commission"]), min_self_delegation=int(data["min_self_delegation"]), delegator_address=data["delegator_address"], validator_address=data["validator_address"], pubkey=data["pubkey"], value=Coin.from_data(data["value"]), )
async def tax_cap(self, denom: str) -> Coin: """Fetches the tax cap for a denom. Args: denom (str): denom Returns: Coin: tax cap """ res = await self._c._get(f"/treasury/tax_cap/{denom}") return Coin(denom, res)
def test_to_str(): coin1 = Coin("uluna", 123456) coin2 = Coin("uluna", 123456.789) assert str(coin1) == "123456uluna" assert str(coin1.to_dec_coin()) == "123456.0uluna" assert str(coin2.to_dec_coin()) == "123456.789uluna"
def swap_rate(self, offer_coin: Coin, ask_denom: str) -> Coin: """Simulates a swap given an amount offered and a target denom. Args: offer_coin (Coin): amount offered (swap from) ask_denom (str): target denom (swap to) Returns: Coin: simulated amount received """ params = {"offer_coin": str(offer_coin), "ask_denom": ask_denom} res = self._c._get("/market/swap", params) return Coin.from_data(res)
def test_deserializes_coin(): coin = Coin.from_data({"denom": "uluna", "amount": "1000"}) assert coin.denom == "uluna" assert coin.amount == 1000
async def pool(self) -> dict: res = await self._c._get(f"/staking/pool") return { "bonded_tokens": Coin("uluna", res["bonded_tokens"]), "not_bonded_tokens": Coin("uluna", res["not_bonded_tokens"]), }
def from_data(cls, data) -> ExchangeRateVote: xr = Coin(data["denom"], data["exchange_rate"]) return cls(exchange_rate=xr, denom=xr.denom, voter=data["voter"])
def test_arithmetic(): zero = Coin("uluna", 0) coin = Coin("uluna", 1000) coin2 = Coin("uluna", 2000) coin3 = Coin("ukrw", 2000) # addition sum = coin.add(coin2) dec_sum = coin.add(0.1) sum2 = coin + coin2 assert coin.add(zero).amount == coin.amount assert sum.amount == 3000 assert sum.denom == "uluna" assert sum2.amount == 3000 assert coin.add(1500) == Coin("uluna", 2500) assert dec_sum.is_dec_coin() assert not dec_sum.is_int_coin() assert dec_sum.amount == Dec(1000.1) with pytest.raises(ArithmeticError) as e: coin.add(coin3) # subtraction diff = coin2.sub(coin) diff2 = coin2 - coin.amount diff3 = coin2 - coin assert diff.denom == "uluna" assert diff.amount == 1000 assert diff2.amount == 1000 assert diff3.amount == 1000 with pytest.raises(ArithmeticError) as e: coin2.sub(coin3) # multiplication product = coin.mul(3.1233) product2 = coin * 3.1233 assert product.denom == "uluna" assert product.amount == 3123.3 assert product2.amount == 3123.3 # division quotient = coin.div(5) quotient2 = coin / 5 quotient3 = coin / 5.0 quotient4 = coin // 3 assert quotient.denom == "uluna" assert quotient.amount == 200 assert quotient2.amount == 200 and quotient2.is_int_coin() assert quotient3.amount == 200 and quotient3.is_dec_coin() assert quotient4.amount == 333 and quotient4.is_int_coin() # modulo rem = coin.mod(43) assert rem.denom == "uluna" assert rem.amount == coin.amount % 43
def test_multiple_amount_types(): ref = Coin("uluna", 1000) assert ref == Coin("uluna", 1000) assert ref == Coin("uluna", "1000")
async def seigniorage_proceeds(self) -> Coin: res = await self._c._get(f"/treasury/seigniorage_proceeds") return Coin("uluna", res)
async def tax_cap(self, denom: str) -> Coin: res = await self._c._get(f"/treasury/tax_cap/{denom}") return Coin(denom, res)
async def swap_rate(self, offer_coin: Coin, ask_denom: str) -> Coin: params = {"offer_coin": str(offer_coin), "ask_denom": ask_denom} res = await self._c._get(f"/market/swap", params) return Coin.from_data(res.get("result"))