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

feat: add support for spendable balances gRPC query #11417

Merged
merged 13 commits into from Mar 25, 2022

Conversation

alexanderbez
Copy link
Contributor

@alexanderbez alexanderbez commented Mar 19, 2022

Description

It's often useful for users to know their current spendable balance. Currently there is only an endpoint that gives you your total balance and an endpoint that gives you full account details. In the context of vesting accounts, it is not possible to know what part of the balance is "spendable" without doing manual arithmetic. This PR adds a query to provide that data directly.

It operates the same way as all balance query does, but filters the balance value by what's spendable.


Author Checklist

All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.

I have...

  • included the correct type prefix in the PR title
  • added ! to the type prefix if API or client breaking change
  • targeted the correct branch (see PR Targeting)
  • provided a link to the relevant issue or specification
  • followed the guidelines for building modules
  • included the necessary unit and integration tests
  • added a changelog entry to CHANGELOG.md
  • included comments for documenting Go code
  • updated the relevant documentation or specification
  • reviewed "Files changed" and left comments if necessary
  • confirmed all CI checks have passed

Reviewers Checklist

All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.

I have...

  • confirmed the correct type prefix in the PR title
  • confirmed ! in the type prefix if API or client breaking change
  • confirmed all author checklist items have been addressed
  • reviewed state machine logic
  • reviewed API design and naming
  • reviewed documentation is accurate
  • reviewed tests and test coverage
  • manually tested (if applicable)

@alexanderbez alexanderbez added C:x/bank C: gRPC Issues and PRs related to the gRPC service and HTTP gateway. backport/0.45.x labels Mar 19, 2022
@alexanderbez alexanderbez changed the title feat: feat: add support for spendable balances gRPC query Mar 19, 2022
Copy link
Member

@tac0turtle tac0turtle left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

could we get a test for this please 😄

@tac0turtle
Copy link
Member

tac0turtle commented Mar 21, 2022

I was thinking why not have spendable coins, vested coins, staked coins and unbonding coins under one response, https://github.com/cosmos/cosmos-sdk/blob/master/x/bank/types/query.pb.go#L78.

Something like:

type QueryBalanceResponse struct {
	// balance is the balance of the coin.
	totalBalance *types.Coin `protobuf:"bytes,1,opt,name=balance,proto3" json:"balance,omitempty"`
	spendableBalance *types.Coin `protobuf:"bytes,1,opt,name=balance,proto3" json:"balance,omitempty"`
	vestingBalance *types.Coin `protobuf:"bytes,1,opt,name=balance,proto3" json:"balance,omitempty"`
}

we could also add bonded and unbending balance here too.

seems some what simpler than introducing a new endpoint for this.

@alexanderbez
Copy link
Contributor Author

alexanderbez commented Mar 21, 2022

I was thinking why not have spendable coins, vested coins, staked coins and unbonding coins under one response, https://github.com/cosmos/cosmos-sdk/blob/master/x/bank/types/query.pb.go#L78.

Something like:

type QueryBalanceResponse struct {
	// balance is the balance of the coin.
	totalBalance *types.Coin `protobuf:"bytes,1,opt,name=balance,proto3" json:"balance,omitempty"`
	spendableBalance *types.Coin `protobuf:"bytes,1,opt,name=balance,proto3" json:"balance,omitempty"`
	vestingBalance *types.Coin `protobuf:"bytes,1,opt,name=balance,proto3" json:"balance,omitempty"`
}

we could also add bonded and unbending balance here too.

seems some what simpler than introducing a new endpoint for this.

That would require more work and proto types. Also, that info is already available via other endpoints. That's also not backwards compatible.

@tac0turtle
Copy link
Member

That would require more work and proto types. Also, that info is already available via other endpoints. That's also not backwards compatible.

the spendable would be this pr, not in a new sturct?

@alexanderbez
Copy link
Contributor Author

That would require more work and proto types. Also, that info is already available via other endpoints. That's also not backwards compatible.

the spendable would be this pr, not in a new sturct?

It depends, are you proposing to introduce a new type or modify the existing balances gRPC query?

@amaury1093 amaury1093 self-assigned this Mar 21, 2022
@tac0turtle
Copy link
Member

It depends, are you proposing to introduce a new type or modify the existing balances gRPC query?

modify existing

@alexanderbez
Copy link
Contributor Author

It depends, are you proposing to introduce a new type or modify the existing balances gRPC query?

modify existing

I suppose I would also be in favor of that, but:

  1. It's not backwards-compatible
  2. Would require an x/auth dependency (to get vesting fields)

This is why I propose we just keep it simple and have a spendable query :)

@alexanderbez
Copy link
Contributor Author

@AmauryM or @robert-zaremba thoughts or any opposition to this new handler? If not, I'll merge.

@atheeshp
Copy link
Contributor

lgtm

@AmauryM or @robert-zaremba thoughts or any opposition to this new handler? If not, I'll merge.

but it's better to wait for the response.

@atheeshp
Copy link
Contributor

atheeshp commented Mar 22, 2022

I think the failing tests are from this PR #10988

@alexanderbez
Copy link
Contributor Author

I think the failing tests are from this PR #10988

@marbar3778 are you gonna fix that? How did that PR land in master with a failing test?

Copy link
Collaborator

@robert-zaremba robert-zaremba left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

useful query. One nit to see if we can optimize the store flow.

x/bank/keeper/grpc_query.go Show resolved Hide resolved
x/bank/keeper/grpc_query.go Show resolved Hide resolved
x/bank/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/bank/keeper/grpc_query.go Outdated Show resolved Hide resolved
x/bank/keeper/grpc_query.go Show resolved Hide resolved
@tac0turtle
Copy link
Member

I think the failing tests are from this PR #10988

@marbar3778 are you gonna fix that? How did that PR land in master with a failing test?

it looks to be non deterministic. I was having issues replicating it locally, seems GitHub actions had it pass once and it worked

spendable := k.SpendableCoins(sdkCtx, addr)

for _, c := range balances {
result = append(result, sdk.NewCoin(c.Denom, spendable.AmountOf(c.Denom)))
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This function can be called by other transaction. So, let's charge extra gas here. Eg 20gas per iteration.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Charge gas? This is a query, not a transaction.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I know. But it can be called from a transaction, in other words, a RPC Msg method, when running can do a query call.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't follow?

These methods are for queries only. In fact, in a subsequent PR, I'll be removing these from the Keeper entirely, in favor of a dedicated Querier (Example). All modules should follow this approach.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Look at this example: https://github.com/regen-network/regen-ledger/blob/master/x/ecocredit/server/msg_server.go#L718 - The rpc Buy will call bank.SpendableCoins.

Technically we can create a QueryClient of other module, and use it when processing a transaction.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Technically we can create a QueryClient of other module, and use it when processing a transaction.

That doesn't make any sense. Gas and txs are not involved here.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably I didn't express myself well enough. Here is the snippet (didn't test it but I believe it's possible):

/// in authz Grant Msg function
// let's imagine a scenario where we want to check spendable coins
func (k Keeper) Grant(goCtx context.Context, msg *authz.MsgGrant) (*authz.MsgGrantResponse, error) {
   ...  
   bc := banktypes.NewQueryClient(ctx)
   resp, err := bc.SpendableBalances(ctx, types.QuerySpendableBalancesRequest{...})
   ....

BTW - I think this is general discussion if we should check if we should consider checking if context provides a gas meter in queries and charge gas (so feel free to merge)

@tac0turtle tac0turtle merged commit 9e1ec7b into master Mar 25, 2022
@tac0turtle tac0turtle deleted the bez/spendable-balance-grpc-query branch March 25, 2022 10:25
mergify bot pushed a commit that referenced this pull request Mar 25, 2022
* updates

* cl++

* tests++

* updates

* updates

(cherry picked from commit 9e1ec7b)

# Conflicts:
#	CHANGELOG.md
#	x/bank/keeper/grpc_query_test.go
#	x/bank/keeper/keeper_test.go
#	x/bank/types/query.pb.go
@amaury1093 amaury1093 mentioned this pull request May 20, 2022
72 tasks
JimLarson pushed a commit to agoric-labs/cosmos-sdk that referenced this pull request Jul 7, 2022
randy75828 pushed a commit to Switcheo/cosmos-sdk that referenced this pull request Aug 10, 2022
randy75828 pushed a commit to Switcheo/cosmos-sdk that referenced this pull request Aug 10, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C: gRPC Issues and PRs related to the gRPC service and HTTP gateway. C:x/bank
Projects
No open projects
Archived in project
Development

Successfully merging this pull request may close these issues.

None yet

6 participants