diff --git a/util/keys/keys.go b/util/keys/keys.go index 64ce7a2fda..8a0a2861ec 100644 --- a/util/keys/keys.go +++ b/util/keys/keys.go @@ -57,3 +57,14 @@ func ExtractAddressAndString(startIndex int, key []byte) (addr sdk.AccAddress, s s, nextIndex, err = ExtractString(nextIndex, key) return addr, s, nextIndex, err } + +// ToStr takes the full key and converts it to a string +func ToStr(key []byte) string { + return string(key) +} + +// NoLastByte returns sub-slice of the key without the last byte. +// Panics if length of key is zero. +func NoLastByte(key []byte) string { + return string(key[:len(key)-1]) +} diff --git a/util/store/iter.go b/util/store/iter.go index ba6e2072e2..441f3951d3 100644 --- a/util/store/iter.go +++ b/util/store/iter.go @@ -80,9 +80,3 @@ func SumCoins(s storetypes.KVStore, f StrExtractor) sdk.Coins { // StrExtractor is a function type which will take a bytes string value and extracts // string out of it. type StrExtractor func([]byte) string - -// NoLastByte returns sub-slice of the key without the last byte. -// Panics if length of key is zero. -func NoLastByte(key []byte) string { - return string(key[:len(key)-1]) -} diff --git a/util/store/iter_test.go b/util/store/iter_test.go index 7322d41956..b61ecdd7d3 100644 --- a/util/store/iter_test.go +++ b/util/store/iter_test.go @@ -8,6 +8,7 @@ import ( "gotest.tools/v3/assert" "github.com/umee-network/umee/v5/tests/tsdk" + "github.com/umee-network/umee/v5/util/keys" ) func TestIterate(t *testing.T) { @@ -74,7 +75,7 @@ func TestSumCoins(t *testing.T) { } pdb := prefixstore.NewStore(db, []byte(prefix)) - sum := SumCoins(pdb, NoLastByte) + sum := SumCoins(pdb, keys.NoLastByte) sum.Sort() assert.DeepEqual(t, expected, sum) } diff --git a/x/incentive/keeper/iter.go b/x/incentive/keeper/iter.go index 62c875a686..27f3e0825d 100644 --- a/x/incentive/keeper/iter.go +++ b/x/incentive/keeper/iter.go @@ -87,10 +87,10 @@ func (k Keeper) getAllAccountUnbondings(ctx sdk.Context) ([]incentive.AccountUnb // getAllTotalUnbonding gets total unbonding for all uTokens (used for a query) func (k Keeper) getAllTotalUnbonding(ctx sdk.Context) sdk.Coins { - return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalUnbonding), store.NoLastByte) + return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalUnbonding), keys.ToStr) } // getAllTotalBonded gets total bonded for all uTokens (used for a query) func (k Keeper) getAllTotalBonded(ctx sdk.Context) sdk.Coins { - return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalBonded), store.NoLastByte) + return store.SumCoins(k.prefixStore(ctx, keyPrefixTotalBonded), keys.ToStr) } diff --git a/x/incentive/keeper/keys.go b/x/incentive/keeper/keys.go index c740fc3197..3a0d7bd724 100644 --- a/x/incentive/keeper/keys.go +++ b/x/incentive/keeper/keys.go @@ -48,14 +48,14 @@ func keyIncentiveProgram(id uint32, status incentive.ProgramStatus) []byte { // keyTotalBonded returns a KVStore key for total bonded uTokens of a given denom. func keyTotalBonded(denom string) []byte { - // totalBondedPrefix | denom | 0x00 - return util.ConcatBytes(1, keyPrefixTotalBonded, []byte(denom)) + // totalBondedPrefix | denom + return util.ConcatBytes(0, keyPrefixTotalBonded, []byte(denom)) } // keyTotalUnbonding returns a KVStore key for total unbonding uTokens of a given denom. func keyTotalUnbonding(denom string) []byte { - // totalUnbondingPrefix | denom | 0x00 - return util.ConcatBytes(1, keyPrefixTotalUnbonding, []byte(denom)) + // totalUnbondingPrefix | denom + return util.ConcatBytes(0, keyPrefixTotalUnbonding, []byte(denom)) } // keyBondAmount returns a KVStore key for bonded amounts for a uToken denom and account. diff --git a/x/leverage/keeper/iter.go b/x/leverage/keeper/iter.go index 97337592cc..0795a3b0d1 100644 --- a/x/leverage/keeper/iter.go +++ b/x/leverage/keeper/iter.go @@ -7,6 +7,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/umee-network/umee/v5/util" + "github.com/umee-network/umee/v5/util/keys" "github.com/umee-network/umee/v5/util/store" "github.com/umee-network/umee/v5/x/leverage/types" ) @@ -177,12 +178,12 @@ func (k Keeper) SweepBadDebts(ctx sdk.Context) error { // GetAllUTokenSupply returns total supply of all uToken denoms. func (k Keeper) GetAllUTokenSupply(ctx sdk.Context) sdk.Coins { - return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixUtokenSupply), store.NoLastByte) + return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixUtokenSupply), keys.NoLastByte) } // GetAllReserves returns all reserves. func (k Keeper) GetAllReserves(ctx sdk.Context) sdk.Coins { - return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixReserveAmount), store.NoLastByte) + return store.SumCoins(k.prefixStore(ctx, types.KeyPrefixReserveAmount), keys.NoLastByte) } func (k Keeper) prefixStore(ctx sdk.Context, p []byte) storetypes.KVStore {