Skip to content

Commit

Permalink
feat: Add convenience method for constructing key to access account's…
Browse files Browse the repository at this point in the history
… balance for a given denom (backport cosmos#12674) (cosmos#12745) (#352)

* feat: Add convenience method for constructing key to access account's balance for a given denom (cosmos#12674)

This PR adds a convenience method for constructing the key necessary to query for the account's balance of a given denom.

I ran into this issue since we are using ABCI query now to perform balance requests because we are also requesting merkle proofs for the returned balance [here](https://github.com/celestiaorg/celestia-node/pull/911/files#diff-0ee31f5a7bd88e9f758e6bebdf3ee36365519e55a451098d9638c39afe5eac42R144).

It would be nice to have a definitive convenience method for constructing the key.

[Ref.](github.com/celestiaorg/celestia-node/pull/911)

(cherry picked from commit a1777a8)

* Update CHANGELOG.md

* fix conflict

Co-authored-by: rene <41963722+renaynay@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>

Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
Co-authored-by: rene <41963722+renaynay@users.noreply.github.com>
Co-authored-by: Marko <marbar3778@yahoo.com>
Co-authored-by: Roman <roman@osmosis.team>
  • Loading branch information
5 people committed Oct 13, 2022
1 parent a9dfefa commit c6b1e6a
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
4 changes: 2 additions & 2 deletions x/bank/legacy/v043/store_test.go
Expand Up @@ -90,13 +90,13 @@ func TestBalanceKeysMigration(t *testing.T) {
err = v043bank.MigrateStore(ctx, bankKey, encCfg.Marshaler)
require.NoError(t, err)

newKey := append(types.CreateAccountBalancesPrefix(addr), []byte(fooCoin.Denom)...)
newKey := types.CreatePrefixedAccountStoreKey(addr, []byte(fooCoin.Denom))
// -7 because we replaced "balances" with 0x02,
// +1 because we added length-prefix to address.
require.Equal(t, len(oldFooKey)-7+1, len(newKey))
require.Nil(t, store.Get(oldFooKey))
require.Equal(t, fooBz, store.Get(newKey))

newKeyFooBar := append(types.CreateAccountBalancesPrefix(addr), []byte(fooBarCoin.Denom)...)
newKeyFooBar := types.CreatePrefixedAccountStoreKey(addr, []byte(fooBarCoin.Denom))
require.Nil(t, store.Get(newKeyFooBar)) // after migration zero balances pruned from store.
}
6 changes: 6 additions & 0 deletions x/bank/types/key.go
Expand Up @@ -57,3 +57,9 @@ func AddressFromBalancesStore(key []byte) (sdk.AccAddress, error) {
func CreateAccountBalancesPrefix(addr []byte) []byte {
return append(BalancesPrefix, address.MustLengthPrefix(addr)...)
}

// CreatePrefixedAccountStoreKey returns the key for the given account and denomination.
// This method can be used when performing an ABCI query for the balance of an account.
func CreatePrefixedAccountStoreKey(addr []byte, denom []byte) []byte {
return append(CreateAccountBalancesPrefix(addr), denom...)
}

0 comments on commit c6b1e6a

Please sign in to comment.