-
Notifications
You must be signed in to change notification settings - Fork 3.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add convenience method for constructing key to access account's…
… balance for a given denom (#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) # Conflicts: # CHANGELOG.md # x/bank/legacy/v043/store.go # x/bank/types/keys.go
- Loading branch information
1 parent
eb032e3
commit 606a04c
Showing
4 changed files
with
133 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
package types | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
"github.com/cosmos/cosmos-sdk/types/address" | ||
"github.com/cosmos/cosmos-sdk/types/kv" | ||
) | ||
|
||
const ( | ||
// ModuleName defines the module name | ||
ModuleName = "bank" | ||
|
||
// StoreKey defines the primary module store key | ||
StoreKey = ModuleName | ||
|
||
// RouterKey defines the module's message routing key | ||
RouterKey = ModuleName | ||
|
||
// QuerierRoute defines the module's query routing key | ||
QuerierRoute = ModuleName | ||
|
||
// ModuleQueryPath defines the ABCI query path of the module | ||
ModuleQueryPath = "store/bank/key" | ||
) | ||
|
||
// KVStore keys | ||
var ( | ||
SupplyKey = []byte{0x00} | ||
DenomMetadataPrefix = []byte{0x1} | ||
DenomAddressPrefix = []byte{0x03} | ||
|
||
// BalancesPrefix is the prefix for the account balances store. We use a byte | ||
// (instead of `[]byte("balances")` to save some disk space). | ||
BalancesPrefix = []byte{0x02} | ||
|
||
// SendEnabledPrefix is the prefix for the SendDisabled flags for a Denom. | ||
SendEnabledPrefix = []byte{0x04} | ||
|
||
// ParamsKey is the prefix for x/bank parameters | ||
ParamsKey = []byte{0x05} | ||
) | ||
|
||
const ( | ||
// TrueB is a byte with value 1 that represents true. | ||
TrueB = byte(0x01) | ||
// FalseB is a byte with value 0 that represents false. | ||
FalseB = byte(0x00) | ||
) | ||
|
||
// AddressAndDenomFromBalancesStore returns an account address and denom from a balances prefix | ||
// store. The key must not contain the prefix BalancesPrefix as the prefix store | ||
// iterator discards the actual prefix. | ||
// | ||
// If invalid key is passed, AddressAndDenomFromBalancesStore returns ErrInvalidKey. | ||
func AddressAndDenomFromBalancesStore(key []byte) (sdk.AccAddress, string, error) { | ||
if len(key) == 0 { | ||
return nil, "", ErrInvalidKey | ||
} | ||
|
||
kv.AssertKeyAtLeastLength(key, 1) | ||
|
||
addrBound := int(key[0]) | ||
|
||
if len(key)-1 < addrBound { | ||
return nil, "", ErrInvalidKey | ||
} | ||
|
||
return key[1 : addrBound+1], string(key[addrBound+1:]), nil | ||
} | ||
|
||
// 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...) | ||
} | ||
|
||
// CreateAccountBalancesPrefix creates the prefix for an account's balances. | ||
func CreateAccountBalancesPrefix(addr []byte) []byte { | ||
return append(BalancesPrefix, address.MustLengthPrefix(addr)...) | ||
} | ||
|
||
// CreateDenomAddressPrefix creates a prefix for a reverse index of denomination | ||
// to account balance for that denomination. | ||
func CreateDenomAddressPrefix(denom string) []byte { | ||
// we add a "zero" byte at the end - null byte terminator, to allow prefix denom prefix | ||
// scan. Setting it is not needed (key[last] = 0) - because this is the default. | ||
key := make([]byte, len(DenomAddressPrefix)+len(denom)+1) | ||
copy(key, DenomAddressPrefix) | ||
copy(key[len(DenomAddressPrefix):], denom) | ||
return key | ||
} | ||
|
||
// CreateSendEnabledKey creates the key of the SendDisabled flag for a denom. | ||
func CreateSendEnabledKey(denom string) []byte { | ||
key := make([]byte, len(SendEnabledPrefix)+len(denom)) | ||
copy(key, SendEnabledPrefix) | ||
copy(key[len(SendEnabledPrefix):], denom) | ||
return key | ||
} | ||
|
||
// IsTrueB returns true if the provided byte slice has exactly one byte, and it is equal to TrueB. | ||
func IsTrueB(bz []byte) bool { | ||
return len(bz) == 1 && bz[0] == TrueB | ||
} | ||
|
||
// ToBoolB returns TrueB if v is true, and FalseB if it's false. | ||
func ToBoolB(v bool) byte { | ||
if v { | ||
return TrueB | ||
} | ||
return FalseB | ||
} |