2019-06-05 16:26:17 -07:00
|
|
|
package types
|
|
|
|
|
2020-01-30 13:31:16 -08:00
|
|
|
import (
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-02-01 05:17:44 -08:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/address"
|
2021-07-08 04:29:12 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/kv"
|
2020-01-30 13:31:16 -08:00
|
|
|
)
|
|
|
|
|
2019-06-05 16:26:17 -07:00
|
|
|
const (
|
2020-01-30 13:31:16 -08:00
|
|
|
// 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
|
2019-06-18 06:44:09 -07:00
|
|
|
QuerierRoute = ModuleName
|
2019-06-05 16:26:17 -07:00
|
|
|
)
|
2020-01-30 13:31:16 -08:00
|
|
|
|
2020-04-20 08:22:12 -07:00
|
|
|
// KVStore keys
|
2020-01-30 13:31:16 -08:00
|
|
|
var (
|
2020-07-28 00:49:56 -07:00
|
|
|
SupplyKey = []byte{0x00}
|
|
|
|
DenomMetadataPrefix = []byte{0x1}
|
2021-07-26 10:51:04 -07:00
|
|
|
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}
|
2020-01-30 13:31:16 -08:00
|
|
|
)
|
|
|
|
|
2021-08-05 10:00:28 -07:00
|
|
|
// AddressAndDenomFromBalancesStore returns an account address and denom from a balances prefix
|
2021-05-10 09:17:55 -07:00
|
|
|
// store. The key must not contain the prefix BalancesPrefix as the prefix store
|
2020-01-30 13:31:16 -08:00
|
|
|
// iterator discards the actual prefix.
|
2021-04-07 09:20:38 -07:00
|
|
|
//
|
2021-08-05 10:00:28 -07:00
|
|
|
// If invalid key is passed, AddressAndDenomFromBalancesStore returns ErrInvalidKey.
|
|
|
|
func AddressAndDenomFromBalancesStore(key []byte) (sdk.AccAddress, string, error) {
|
2021-04-07 09:20:38 -07:00
|
|
|
if len(key) == 0 {
|
2021-08-05 10:00:28 -07:00
|
|
|
return nil, "", ErrInvalidKey
|
2021-04-07 09:20:38 -07:00
|
|
|
}
|
2021-07-26 10:51:04 -07:00
|
|
|
|
2021-07-08 04:29:12 -07:00
|
|
|
kv.AssertKeyAtLeastLength(key, 1)
|
2021-07-26 10:51:04 -07:00
|
|
|
|
2021-08-05 10:00:28 -07:00
|
|
|
addrBound := int(key[0])
|
2021-07-26 10:51:04 -07:00
|
|
|
|
2021-08-05 10:00:28 -07:00
|
|
|
if len(key)-1 < addrBound {
|
|
|
|
return nil, "", ErrInvalidKey
|
2021-04-07 09:20:38 -07:00
|
|
|
}
|
2021-07-26 10:51:04 -07:00
|
|
|
|
2021-08-05 10:00:28 -07:00
|
|
|
return key[1 : addrBound+1], string(key[addrBound+1:]), nil
|
2020-01-30 13:31:16 -08:00
|
|
|
}
|
2021-02-01 05:17:44 -08:00
|
|
|
|
|
|
|
// CreateAccountBalancesPrefix creates the prefix for an account's balances.
|
|
|
|
func CreateAccountBalancesPrefix(addr []byte) []byte {
|
|
|
|
return append(BalancesPrefix, address.MustLengthPrefix(addr)...)
|
|
|
|
}
|
2021-07-26 10:51:04 -07:00
|
|
|
|
|
|
|
// CreateDenomAddressPrefix creates a prefix for a reverse index of denomination
|
|
|
|
// to account balance for that denomination.
|
|
|
|
func CreateDenomAddressPrefix(denom string) []byte {
|
2021-09-28 16:40:18 -07:00
|
|
|
// 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
|
2021-07-26 10:51:04 -07:00
|
|
|
}
|