Skip to content

Commit

Permalink
Add forward calc field for eden rewards query (#751)
Browse files Browse the repository at this point in the history
* lifetime must be > 0

* add forward calc

* add test

* more tests

* cover

* accum

* comment
  • Loading branch information
amityadav0 authored Aug 29, 2024
1 parent 59a34a6 commit 050c973
Show file tree
Hide file tree
Showing 7 changed files with 282 additions and 51 deletions.
3 changes: 3 additions & 0 deletions proto/elys/incentive/query.proto
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,9 @@ message PoolRewards {
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
cosmos.base.v1beta1.Coin eden_forward = 4 [
(gogoproto.nullable) = false
];
}

message QueryPoolRewardsResponse {
Expand Down
2 changes: 2 additions & 0 deletions x/incentive/keeper/queries.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,10 +162,12 @@ func (k Keeper) PoolRewards(goCtx context.Context, req *types.QueryPoolRewardsRe
func (k *Keeper) generatePoolRewards(ctx sdk.Context, ammPool *ammtypes.Pool) types.PoolRewards {
// Get rewards amount
rewardsUsd, rewardCoins := k.GetDailyRewardsAmountForPool(ctx, ammPool.PoolId)
edenForward := sdk.NewCoin(ptypes.Eden, k.masterchef.ForwardEdenCalc(ctx, ammPool.PoolId).RoundInt())

return types.PoolRewards{
PoolId: ammPool.PoolId,
RewardsUsd: rewardsUsd,
RewardCoins: rewardCoins,
EdenForward: edenForward,
}
}
156 changes: 105 additions & 51 deletions x/incentive/types/query.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 26 additions & 0 deletions x/masterchef/keeper/pool_rewards_accum.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,32 @@ func (k Keeper) LastPoolRewardsAccum(ctx sdk.Context, poolId uint64) types.PoolR
}
}

// Returns eden rewards using forward calc for 24 hours
func (k Keeper) ForwardEdenCalc(ctx sdk.Context, poolId uint64) sdk.Dec {
store := ctx.KVStore(k.storeKey)
iter := sdk.KVStoreReversePrefixIterator(store, types.GetPoolRewardsAccumPrefix(poolId))
defer iter.Close()

var lastTwo []types.PoolRewardsAccum
for ; iter.Valid() && len(lastTwo) < 2; iter.Next() {
accum := types.PoolRewardsAccum{}
k.cdc.MustUnmarshal(iter.Value(), &accum)
lastTwo = append([]types.PoolRewardsAccum{accum}, lastTwo...)
}

if len(lastTwo) == 2 {
diff := lastTwo[1].EdenReward.Sub(lastTwo[0].EdenReward)
// Here we are assuming average block time of 4s
// 1 DAY = 86400
// Note: This calculation maybe used in FE, the idea is to
// give estimated numbers of rewards that a user will get
return diff.MulInt64(21600)
}

// Return zero if there are not enough entries
return sdk.ZeroDec()
}

func (k Keeper) AddPoolRewardsAccum(ctx sdk.Context, poolId, timestamp uint64, height int64, dexReward, gasReward, edenReward math.LegacyDec) {
lastAccum := k.LastPoolRewardsAccum(ctx, poolId)
lastAccum.Timestamp = timestamp
Expand Down
68 changes: 68 additions & 0 deletions x/masterchef/keeper/pool_rewards_accum_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (

"cosmossdk.io/math"
tmproto "github.com/cometbft/cometbft/proto/tendermint/types"
sdk "github.com/cosmos/cosmos-sdk/types"
simapp "github.com/elys-network/elys/app"
"github.com/elys-network/elys/x/masterchef/types"
"github.com/stretchr/testify/require"
Expand Down Expand Up @@ -71,3 +72,70 @@ func TestPoolRewardsAccum(t *testing.T) {
accum = app.MasterchefKeeper.LastPoolRewardsAccum(ctx, 1)
require.Equal(t, accum, accums[1])
}

func TestAddPoolRewardsAccum(t *testing.T) {
app := simapp.InitElysTestApp(true)
ctx := app.BaseApp.NewContext(true, tmproto.Header{})
k := app.MasterchefKeeper

tests := []struct {
name string
poolId uint64
timestamp uint64
height int64
dexReward math.LegacyDec
gasReward math.LegacyDec
edenReward math.LegacyDec
}{
{
name: "Add rewards to new pool",
poolId: 1,
timestamp: uint64(time.Now().Unix()),
height: 100,
dexReward: math.LegacyNewDec(10),
gasReward: math.LegacyNewDec(5),
edenReward: math.LegacyNewDec(3),
},
{
name: "Add rewards to existing pool",
poolId: 1,
timestamp: uint64(time.Now().Unix()) + 3600, // 1 hour later
height: 200,
dexReward: math.LegacyNewDec(20),
gasReward: math.LegacyNewDec(10),
edenReward: math.LegacyNewDec(6),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
k.AddPoolRewardsAccum(ctx, tt.poolId, tt.timestamp, tt.height, tt.dexReward, tt.gasReward, tt.edenReward)

accum, err := k.GetPoolRewardsAccum(ctx, tt.poolId, tt.timestamp)
require.NoError(t, err)

require.Equal(t, tt.poolId, accum.PoolId)
require.Equal(t, tt.timestamp, accum.Timestamp)
require.Equal(t, tt.height, accum.BlockHeight)

if tt.name == "Add rewards to new pool" {
require.Equal(t, tt.dexReward, accum.DexReward)
require.Equal(t, tt.gasReward, accum.GasReward)
require.Equal(t, tt.edenReward, accum.EdenReward)

// Check forward
forwardEden := k.ForwardEdenCalc(ctx, tt.poolId)
require.Equal(t, sdk.ZeroDec(), forwardEden)
} else {
// For existing pool, rewards should be cumulative
require.Equal(t, math.LegacyNewDec(30), accum.DexReward)
require.Equal(t, math.LegacyNewDec(15), accum.GasReward)
require.Equal(t, math.LegacyNewDec(9), accum.EdenReward)

// Check forward
forwardEden := k.ForwardEdenCalc(ctx, tt.poolId)
require.Equal(t, sdk.MustNewDecFromStr("21600").Mul(tt.edenReward), forwardEden)
}
})
}
}
4 changes: 4 additions & 0 deletions x/parameter/keeper/msg_server_update_rewards_data_lifetime.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ func (k msgServer) UpdateRewardsDataLifetime(goCtx context.Context, msg *types.M
return nil, errorsmod.Wrapf(types.ErrInvalidRewardsDataLifecycle, "invalid data in rewards_data_lifecycle")
}

if !rewardsDataLifetime.IsPositive() {
return nil, errorsmod.Wrapf(types.ErrInvalidRewardsDataLifecycle, "rewards_data_lifecycle must be positive")
}

params := k.GetParams(ctx)
params.RewardsDataLifetime = rewardsDataLifetime.Int64()
k.SetParams(ctx, params)
Expand Down
Loading

0 comments on commit 050c973

Please sign in to comment.