Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getter to get staked balance #1116

Closed
webmaster128 opened this issue Apr 11, 2022 · 0 comments · Fixed by #1124
Closed

Add getter to get staked balance #1116

webmaster128 opened this issue Apr 11, 2022 · 0 comments · Fixed by #1124
Assignees

Comments

@webmaster128
Copy link
Member

webmaster128 commented Apr 11, 2022

When you call StargateClient.getBalance/CosmWasmClient.getBalance, the response does not contain staked amounts. It would be good to have a simple method that does this.

An implementation I tested looks like this:

export function addCoins(lhs: Coin, rhs: Coin): Coin {
  if (lhs.denom !== rhs.denom) throw new Error("Trying to add two coins with different demoms");
  return {
    amount: Decimal.fromAtomics(lhs.amount, 0).plus(Decimal.fromAtomics(rhs.amount, 0)).atomics,
    denom: lhs.denom,
  };
}


  const { delegationResponses, pagination } = await queryClient.staking.delegatorDelegations(
    address,
  );
  assert(pagination?.nextKey?.length === 0); // TODO: support multiple pages
  const staked = delegationResponses.reduce((acc: Coin | null, resp) => {
    // console.log(resp.balance);
    assert(resp.balance);
    if (acc) {
      return addCoins(acc, resp.balance);
    } else {
      return resp.balance;
    }
  }, null);
  return staked;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants