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: use more store functions in incentive #2101

Merged
merged 10 commits into from
Jun 22, 2023
81 changes: 13 additions & 68 deletions x/incentive/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@ import (
// The status of an incentive program is either Upcoming, Ongoing, or Completed.
func (k Keeper) getAllIncentivePrograms(ctx sdk.Context, status incentive.ProgramStatus,
) ([]incentive.IncentiveProgram, error) {
programs := []incentive.IncentiveProgram{}

var prefix []byte
switch status {
case incentive.ProgramStatusUpcoming:
Expand All @@ -27,16 +25,7 @@ func (k Keeper) getAllIncentivePrograms(ctx sdk.Context, status incentive.Progra
return []incentive.IncentiveProgram{}, incentive.ErrInvalidProgramStatus
}

iterator := func(_, val []byte) error {
var p incentive.IncentiveProgram
k.cdc.MustUnmarshal(val, &p)

programs = append(programs, p)
return nil
}

err := store.Iterate(k.KVStore(ctx), prefix, iterator)
return programs, err
return store.LoadAll[*incentive.IncentiveProgram](k.KVStore(ctx), prefix)
}

// getAllBondDenoms gets all uToken denoms for which an account has nonzero bonded amounts.
Expand All @@ -45,7 +34,7 @@ func (k Keeper) getAllBondDenoms(ctx sdk.Context, addr sdk.AccAddress) ([]string
prefix := keyBondAmountNoDenom(addr)
bonds := []string{}

iterator := func(key, val []byte) error {
iterator := func(key, _ []byte) error {
_, denom, _, err := keys.ExtractAddressAndString(len(keyPrefixBondAmount), key)
if err != nil {
return err
Expand Down Expand Up @@ -83,82 +72,38 @@ func (k Keeper) getAllBonds(ctx sdk.Context) ([]incentive.Bond, error) {

// getAllTotalBonded gets total bonded for all uTokens (used for a query)
func (k Keeper) getAllTotalBonded(ctx sdk.Context) (sdk.Coins, error) {
prefix := keyPrefixTotalBonded
total := sdk.NewCoins()

iterator := func(key, val []byte) error {
denom, _, err := keys.ExtractString(len(keyPrefixTotalBonded), key)
if err != nil {
return err
}
amount := store.Int(val, "total bonded")
total = total.Add(sdk.NewCoin(denom, amount))
return nil
}

err := store.Iterate(k.KVStore(ctx), prefix, iterator)
return total, err
return k.sumCoins(ctx, keyPrefixTotalBonded)
}

// getAllRewardTrackers gets all reward trackers for all accounts (used during export genesis)
func (k Keeper) getAllRewardTrackers(ctx sdk.Context) ([]incentive.RewardTracker, error) {
prefix := keyPrefixRewardTracker
rewardTrackers := []incentive.RewardTracker{}

iterator := func(_, val []byte) error {
tracker := incentive.RewardTracker{}
k.cdc.MustUnmarshal(val, &tracker)
rewardTrackers = append(rewardTrackers, tracker)
return nil
}

err := store.Iterate(k.KVStore(ctx), prefix, iterator)
return rewardTrackers, err
return store.LoadAll[*incentive.RewardTracker](k.KVStore(ctx), keyPrefixRewardTracker)
}

// getAllRewardAccumulators gets all reward accumulators for all uTokens (used during export genesis)
func (k Keeper) getAllRewardAccumulators(ctx sdk.Context) ([]incentive.RewardAccumulator, error) {
prefix := keyPrefixRewardAccumulator
rewardAccumulators := []incentive.RewardAccumulator{}

iterator := func(_, val []byte) error {
accumulator := incentive.RewardAccumulator{}
k.cdc.MustUnmarshal(val, &accumulator)
rewardAccumulators = append(rewardAccumulators, accumulator)
return nil
}

err := store.Iterate(k.KVStore(ctx), prefix, iterator)
return rewardAccumulators, err
return store.LoadAll[*incentive.RewardAccumulator](k.KVStore(ctx), keyPrefixRewardAccumulator)
}

// getAllAccountUnbondings gets all account unbondings for all accounts (used during export genesis)
func (k Keeper) getAllAccountUnbondings(ctx sdk.Context) ([]incentive.AccountUnbondings, error) {
prefix := keyPrefixUnbondings
unbondings := []incentive.AccountUnbondings{}

iterator := func(key, val []byte) error {
au := incentive.AccountUnbondings{}
k.cdc.MustUnmarshal(val, &au)
unbondings = append(unbondings, au)
return nil
}

err := store.Iterate(k.KVStore(ctx), prefix, iterator)
return unbondings, err
return store.LoadAll[*incentive.AccountUnbondings](k.KVStore(ctx), keyPrefixUnbondings)
}

// getAllTotalUnbonding gets total unbonding for all uTokens (used for a query)
func (k Keeper) getAllTotalUnbonding(ctx sdk.Context) (sdk.Coins, error) {
prefix := keyPrefixTotalUnbonding
total := sdk.NewCoins()
return k.sumCoins(ctx, keyPrefixTotalUnbonding)
}

// getAllTotalUnbonding gets total unbonding for all uTokens (used for a query)
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
func (k Keeper) sumCoins(ctx sdk.Context, prefix []byte) (sdk.Coins, error) {
total := sdk.NewCoins()
iterator := func(key, val []byte) error {
denom, _, err := keys.ExtractString(len(keyPrefixTotalUnbonding), key)
denom, _, err := keys.ExtractString(len(prefix), key)
if err != nil {
return err
}
amount := store.Int(val, "total unbonding")
amount := store.Int(val, "amount")
total = total.Add(sdk.NewCoin(denom, amount))
return nil
}
Expand Down
40 changes: 10 additions & 30 deletions x/leverage/keeper/iter.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,7 @@ func (k Keeper) GetAllRegisteredTokens(ctx sdk.Context) []types.Token {

// GetAllReserves returns all reserves.
func (k Keeper) GetAllReserves(ctx sdk.Context) sdk.Coins {
prefix := types.KeyPrefixReserveAmount
reserves := sdk.NewCoins()

iterator := func(key, val []byte) error {
denom := types.DenomFromKey(key, prefix)
var amount sdkmath.Int
if err := amount.Unmarshal(val); err != nil {
// improperly marshaled reserve amount should never happen
return err
}

reserves = reserves.Add(sdk.NewCoin(denom, amount))
return nil
}

util.Panic(k.iterate(ctx, prefix, iterator))

return reserves
return k.sumCoins(ctx, types.KeyPrefixReserveAmount)
}

// GetBorrowerBorrows returns an sdk.Coins object containing all open borrows
Expand Down Expand Up @@ -197,22 +180,19 @@ 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 {
prefix := types.KeyPrefixUtokenSupply
supplies := sdk.NewCoins()
return k.sumCoins(ctx, types.KeyPrefixUtokenSupply)
}

// getAllTotalUnbonding gets total unbonding for all uTokens (used for a query)
func (k Keeper) sumCoins(ctx sdk.Context, prefix []byte) sdk.Coins {
robert-zaremba marked this conversation as resolved.
Show resolved Hide resolved
total := sdk.NewCoins()
iterator := func(key, val []byte) error {
denom := types.DenomFromKey(key, prefix)
var amount sdkmath.Int
if err := amount.Unmarshal(val); err != nil {
// improperly marshaled utoken supply should never happen
return err
}

supplies = supplies.Add(sdk.NewCoin(denom, amount))
amount := store.Int(val, "amount")
total = total.Add(sdk.NewCoin(denom, amount))
return nil
}

util.Panic(k.iterate(ctx, prefix, iterator))

return supplies
util.Panic(store.Iterate(ctx.KVStore(k.storeKey), prefix, iterator))
return total
}