def from_data(cls, data: dict) -> LazyGradedVestingAccount: data = data["value"] return cls( address=data["address"], coins=Coins.from_data(data["coins"]), public_key=PublicKey.from_data(data["public_key"]), account_number=data["account_number"], sequence=data["sequence"], original_vesting=Coins.from_data(data["original_vesting"]), delegated_free=Coins.from_data(data["delegated_free"]), delegated_vesting=Coins.from_data(data["delegated_vesting"]), end_time=data["end_time"], vesting_schedules=data["vesting_schedules"], )
async def validator_rewards(self, validator: ValAddress) -> ValidatorRewards: """Fetches the commission reward data for a validator. Args: validator (ValAddress): validator operator address Returns: ValidatorRewards: validator rewards """ res = await self._c._get(f"/distribution/validators/{validator}") return ValidatorRewards( self_bond_rewards=Coins.from_data(res["self_bond_rewards"]), val_commission=Coins.from_data(res["val_commission"]), )
def from_data(cls, data: dict) -> MsgSend: data = data["value"] return cls( from_address=data["from_address"], to_address=data["to_address"], amount=Coins.from_data(data["amount"]), )
async def estimate_fee( self, tx: Union[StdSignMsg, StdTx], gas_prices: Optional[Coins.Input] = None, gas_adjustment: Optional[Numeric.Input] = None, denoms: Optional[List[str]] = None, ) -> StdFee: gas_prices = gas_prices or self._c.gas_prices gas_adjustment = gas_adjustment or self._c.gas_adjustment if isinstance(tx, StdSignMsg): tx_value = tx.to_stdtx().to_data()["value"] else: tx_value = tx.to_data()["value"] tx_value["fee"]["gas"] = "0" data = { "tx": tx_value, "gas_prices": gas_prices and Coins(gas_prices).to_data(), "gas_adjustment": gas_adjustment and str(gas_adjustment), } res = await self._c._post("/txs/estimate_fee", data) fees = Coins.from_data(res["fees"]) # only pick the denoms we are interested in? if denoms: fees = fees.filter(lambda c: c.denom in denoms) # type: ignore return StdFee(int(res["gas"]), fees)
def from_data(cls, data: dict) -> MsgDeposit: data = data["value"] return cls( proposal_id=data["proposal_id"], depositor=data["depositor"], amount=Coins.from_data(data["amount"]), )
async def community_pool(self) -> Coins: """Fetches the community pool. Returns: Coins: community pool """ res = await self._c._get("/distribution/community_pool") return Coins.from_data(res)
def from_data(cls, data: dict) -> CommunityPoolSpendProposal: data = data["value"] return cls( title=data["title"], description=data["description"], recipient=data["recipient"], amount=Coins.from_data(data["amount"]), )
async def tax_proceeds(self) -> Coins: """Fetches the current tax proceeds. Returns: Coins: tax proceeds """ res = await self._c._get("/treasury/tax_proceeds") return Coins.from_data(res)
def from_data(cls, data: dict) -> MsgExecuteContract: data = data["value"] return cls( sender=data["sender"], contract=data["contract"], execute_msg=b64_to_dict(data["execute_msg"]), coins=Coins.from_data(data["coins"]), )
def from_data(cls, data: dict) -> Account: data = data["value"] return cls( address=data["address"], coins=Coins.from_data(data["coins"]), public_key=PublicKey.from_data(data["public_key"]), account_number=data["account_number"], sequence=data["sequence"], )
def from_data(cls, data: dict) -> MsgInstantiateContract: data = data["value"] return cls( owner=data["owner"], code_id=data["code_id"], init_msg=b64_to_dict(data["init_msg"]), init_coins=Coins.from_data(data["init_coins"]), migratable=data["migratable"], )
def from_data(cls, data: dict) -> MsgSubmitProposal: from terra_sdk.util.parse_content import parse_content data = data["value"] content = parse_content(data["content"]) return cls( content=content, initial_deposit=Coins.from_data(data["initial_deposit"]), proposer=data["proposer"], )
async def rewards(self, delegator: AccAddress) -> Rewards: """Fetches the staking reward data for a delegator. Args: delegator (AccAddress): delegator account address Returns: Rewards: delegator rewards """ res = await self._c._get( f"/distribution/delegators/{delegator}/rewards") return Rewards( rewards={ item["validator_address"]: Coins.from_data(item["reward"] or []) for item in res["rewards"] }, total=Coins.from_data(res["total"]), )
async def exchange_rates(self) -> Coins: """Fetches registered exchange rates of Luna in all available denoms. Returns: Coins: exchange rates of Luna """ res = await self._c._get("/oracle/denoms/exchange_rates") if res: return Coins.from_data(res) else: return Coins({})
async def balance(self, address: AccAddress) -> Coins: """Fetches an account's current balance. Args: address (AccAddress): account address Returns: Coins: balance """ res = await self._c._get(f"/bank/balances/{address}") return Coins.from_data(res)
def from_data(cls, data: dict) -> Proposal: return cls( id=data["id"], content=Content.from_data(data["content"]), proposal_status=data["proposal_status"], final_tally_result=data["final_tally_result"], submit_time=data["submit_time"], deposit_end_time=data["deposit_end_time"], total_deposit=Coins.from_data(data["total_deposit"]), voting_start_time=data["voting_start_time"], voting_end_time=data["voting_end_time"], )
async def estimate_fee( self, tx: Union[StdSignMsg, StdTx], gas_prices: Optional[Coins.Input] = None, gas_adjustment: Optional[Numeric.Input] = None, denoms: Optional[List[str]] = None, ) -> StdFee: """Estimates the proper fee to apply by simulating it within the node. Args: tx (Union[StdSignMsg, StdTx]): transaction to estimate fee for gas_prices (Optional[Coins.Input], optional): gas prices to use. gas_adjustment (Optional[Numeric.Input], optional): gas adjustment to use. denoms (Optional[List[str]], optional): list of denoms to use to pay for gas. Returns: StdFee: estimated fee """ gas_prices = gas_prices or self._c.gas_prices gas_adjustment = gas_adjustment or self._c.gas_adjustment if isinstance(tx, StdSignMsg): tx_value = tx.to_stdtx().to_data()["value"] else: tx_value = tx.to_data()["value"] tx_value["fee"]["gas"] = "0" data = { "tx": tx_value, "gas_prices": gas_prices and Coins(gas_prices).to_data(), "gas_adjustment": gas_adjustment and str(gas_adjustment), } res = await self._c._post("/txs/estimate_fee", data) fees = Coins.from_data(res["fees"]) # only pick the denoms we are interested in? if denoms: fees = fees.filter(lambda c: c.denom in denoms) # type: ignore return StdFee(int(res["gas"]), fees)
async def balance(self, address: AccAddress) -> Coins: res = await self._c._get(f"/bank/balances/{address}") return Coins.from_data(res)
def from_data(cls, data: dict) -> MsgFundCommunityPool: data = data["value"] return cls(depositor=data["depositor"], amount=Coins.from_data(data["amount"]))
async def tax_proceeds(self) -> Coins: res = await self._c._get(f"/treasury/tax_proceeds") return Coins.from_data(res)
def from_data(cls, data: dict) -> SendAuthorization: data = data["value"] return cls(spend_limit=Coins.from_data(data["spend_limit"]))
async def exchange_rates(self) -> Coins: res = self._c._get(f"/oracle/denoms/exchange_rates", raw=True) if res.get("result"): return Coins.from_data(res) else: return Coins({})