Skip to content

Commit

Permalink
cleanup trading reward candidates after expiration
Browse files Browse the repository at this point in the history
  • Loading branch information
faneaatiku committed Apr 8, 2024
1 parent 2511be4 commit 749f148
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
8 changes: 8 additions & 0 deletions x/rewards/keeper/trading_reward_distribution.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ func (k Keeper) distributeTradingRewards(ctx sdk.Context, epochNumber int64) {
k.RemoveActiveTradingRewardExpiration(ctx, exp.ExpireAt, exp.RewardId)
k.RemoveActiveTradingReward(ctx, exp.RewardId)
k.RemoveTradingRewardLeaderboard(ctx, exp.RewardId)
k.cleanupTradingRewardCandidates(ctx, exp.RewardId)

continue
}
Expand Down Expand Up @@ -57,3 +58,10 @@ func (k Keeper) distributeTradingRewards(ctx sdk.Context, epochNumber int64) {
k.SetActiveTradingRewardExpiration(ctx, exp)
}
}

func (k Keeper) cleanupTradingRewardCandidates(ctx sdk.Context, rewardId string) {
toRemove := k.GetTradingRewardCandidateByRewardId(ctx, rewardId)
for _, trc := range toRemove {
k.RemoveTradingRewardCandidate(ctx, trc.RewardId, trc.Address)
}
}
21 changes: 21 additions & 0 deletions x/rewards/keeper/trading_reward_leaderboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,11 @@ func (k Keeper) GetTradingRewardCandidate(ctx sdk.Context, rewardId, address str
return val, true
}

func (k Keeper) RemoveTradingRewardCandidate(ctx sdk.Context, rewardId, address string) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.RewardCandidateKeyPrefix))
store.Delete(types.TradingRewardCandidateKey(rewardId, address))
}

// GetAllTradingRewardCandidate returns all []types.TradingRewardCandidate
func (k Keeper) GetAllTradingRewardCandidate(ctx sdk.Context) (list []types.TradingRewardCandidate) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.RewardCandidateKeyPrefix))
Expand All @@ -81,3 +86,19 @@ func (k Keeper) GetAllTradingRewardCandidate(ctx sdk.Context) (list []types.Trad

return
}

// GetTradingRewardCandidateByRewardId returns all []types.TradingRewardCandidate for a rewardId
func (k Keeper) GetTradingRewardCandidateByRewardId(ctx sdk.Context, rewardId string) (list []types.TradingRewardCandidate) {
store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.RewardCandidateKeyPrefix))
iterator := sdk.KVStorePrefixIterator(store, []byte(rewardId+"/"))

defer iterator.Close()

for ; iterator.Valid(); iterator.Next() {
var val types.TradingRewardCandidate
k.cdc.MustUnmarshal(iterator.Value(), &val)
list = append(list, val)
}

return
}

0 comments on commit 749f148

Please sign in to comment.