Skip to content

Commit

Permalink
feat: add pool reward accounting
Browse files Browse the repository at this point in the history
  • Loading branch information
snobbee committed Nov 26, 2023
1 parent 22a4c41 commit 2ebd543
Show file tree
Hide file tree
Showing 16 changed files with 227 additions and 88 deletions.
3 changes: 2 additions & 1 deletion integrationtest/output/results.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
"unsettled_external_liabilities": "0",
"unsettled_native_liabilities": "0",
"block_interest_native": "0",
"block_interest_external": "13699020"
"block_interest_external": "13699020",
"reward_amount_external": "0"
}
},
"LPs": {
Expand Down
3 changes: 2 additions & 1 deletion integrationtest/output/tc1.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@
"unsettled_external_liabilities": "0",
"unsettled_native_liabilities": "2023304519940209643",
"block_interest_native": "0",
"block_interest_external": "4390203"
"block_interest_external": "4390203",
"reward_amount_external": "0"
}
},
"LPs": {
Expand Down
3 changes: 2 additions & 1 deletion integrationtest/output/tc2.json
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,8 @@
"unsettled_external_liabilities": "0",
"unsettled_native_liabilities": "0",
"block_interest_native": "89729273457704882289",
"block_interest_external": "0"
"block_interest_external": "0",
"reward_amount_external": "0"
}
},
"LPs": {
Expand Down
6 changes: 4 additions & 2 deletions integrationtest/output/tc3.json
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@
"unsettled_external_liabilities": "0",
"unsettled_native_liabilities": "0",
"block_interest_native": "0",
"block_interest_external": "223827155962"
"block_interest_external": "223827155962",
"reward_amount_external": "0"
},
"cusdc": {
"external_asset": {
Expand All @@ -86,7 +87,8 @@
"unsettled_external_liabilities": "0",
"unsettled_native_liabilities": "0",
"block_interest_native": "2153417486774893264327",
"block_interest_external": "0"
"block_interest_external": "0",
"reward_amount_external": "0"
}
},
"LPs": {
Expand Down
6 changes: 6 additions & 0 deletions proto/sifnode/clp/v1/types.proto
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ message Pool {
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false
];
// the amount of external asset rewards distributed to liquidity providers from reward buckets
string reward_amount_external = 19 [
(gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Uint",
(gogoproto.nullable) = false,
(gogoproto.moretags) = "yaml:\"reward_amount_external\""
];
}

message LiquidityProvider {
Expand Down
5 changes: 5 additions & 0 deletions x/clp/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,7 @@ func TestBeginBlocker_Incremental(t *testing.T) {
Health: sdk.ZeroDec(),
InterestRate: sdk.NewDecWithPrec(1, 1),
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
SwapPriceNative: sdk.MustNewDecFromStr("1.100000000000000089"),
SwapPriceExternal: sdk.MustNewDecFromStr("0.909090909090909017"),
Expand Down Expand Up @@ -449,6 +450,7 @@ func TestBeginBlocker_Incremental(t *testing.T) {
Health: sdk.ZeroDec(),
InterestRate: sdk.NewDecWithPrec(1, 1),
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
SwapPriceNative: sdk.MustNewDecFromStr("1.210000000000000196"),
SwapPriceExternal: sdk.MustNewDecFromStr("0.826446280991735403"),
Expand Down Expand Up @@ -476,6 +478,7 @@ func TestBeginBlocker_Incremental(t *testing.T) {
Health: sdk.ZeroDec(),
InterestRate: sdk.NewDecWithPrec(1, 1),
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
SwapPriceNative: sdk.MustNewDecFromStr("1.331000000000000323"),
SwapPriceExternal: sdk.MustNewDecFromStr("0.751314800901577578"),
Expand Down Expand Up @@ -503,6 +506,7 @@ func TestBeginBlocker_Incremental(t *testing.T) {
Health: sdk.ZeroDec(),
InterestRate: sdk.NewDecWithPrec(1, 1),
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
SwapPriceNative: sdk.MustNewDecFromStr("1.464100000000000474"),
SwapPriceExternal: sdk.MustNewDecFromStr("0.683013455365070470"),
Expand Down Expand Up @@ -565,6 +569,7 @@ func TestBeginBlocker_Incremental(t *testing.T) {
BlockInterestNative: sdk.ZeroUint(),
Health: sdk.ZeroDec(),
InterestRate: sdk.NewDecWithPrec(1, 1),
RewardAmountExternal: sdk.ZeroUint(),
},
}
clpGs := types.DefaultGenesisState()
Expand Down
25 changes: 25 additions & 0 deletions x/clp/keeper/calculations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1456,6 +1456,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1489,6 +1490,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
errString: errors.New("not enough received asset tokens to swap"),
},
Expand Down Expand Up @@ -1524,6 +1526,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1556,6 +1559,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1588,6 +1592,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1620,6 +1625,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1652,6 +1658,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1684,6 +1691,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1716,6 +1724,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1748,6 +1757,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1780,6 +1790,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1812,6 +1823,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1844,6 +1856,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1876,6 +1889,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1908,6 +1922,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1940,6 +1955,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -1972,6 +1988,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -2004,6 +2021,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -2036,6 +2054,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -2068,6 +2087,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -2100,6 +2120,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -2132,6 +2153,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -2164,6 +2186,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
},
{
Expand Down Expand Up @@ -2242,6 +2265,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
},
}
lps := []*types.LiquidityProvider{
Expand Down Expand Up @@ -2288,6 +2312,7 @@ func TestKeeper_SwapOneFromGenesis(t *testing.T) {
SwapPriceNative: &SwapPriceNative,
SwapPriceExternal: &SwapPriceExternal,
RewardPeriodNativeDistributed: sdk.ZeroUint(),
RewardAmountExternal: sdk.ZeroUint(),
})

var swapAmount sdk.Uint
Expand Down
22 changes: 18 additions & 4 deletions x/clp/keeper/epoch_hooks.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
"github.com/Sifchain/sifnode/x/clp/types"
epochstypes "github.com/Sifchain/sifnode/x/epochs/types"
sdk "github.com/cosmos/cosmos-sdk/types"
)
Expand All @@ -16,15 +17,15 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ int64)

rewardsEligibleLps, err := k.GetRewardsEligibleLiquidityProviders(ctx)
if err != nil {
ctx.Logger().Error("unable to get rewards eligible liquidity providers", "error", err)
ctx.Logger().Error(types.ErrUnableToGetRewardsEligibleLiquidityProviders.Error(), "error", err)
return
}

for asset, assetLps := range rewardsEligibleLps {
// get reward bucket for given asset
rewardsBucket, found := k.GetRewardsBucket(ctx, asset.Symbol)
if !found {
ctx.Logger().Error("unable to get rewards bucket", "asset", asset.Symbol)
ctx.Logger().Error(types.ErrRewardsBucketNotFound.Error(), "asset", asset.Symbol)
continue
}

Expand All @@ -35,12 +36,12 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ int64)
if k.ShouldDistributeRewardsToLPWallet(ctx) {
err := k.DistributeLiquidityProviderRewards(ctx, lp, asset.Symbol, rewardAmounts[i])
if err != nil {
ctx.Logger().Error("unable to distribute liquidity provider rewards", "error", err)
ctx.Logger().Error(types.ErrUnableToDistributeLPRewards.Error(), "error", err)
}
} else {
err := k.AddRewardAmountToLiquidityPool(ctx, lp, asset, rewardAmounts[i])
if err != nil {
ctx.Logger().Error("unable to add reward amount to liquidity pool", "error", err)
ctx.Logger().Error(types.ErrUnableToAddRewardAmountToLiquidityPool.Error(), "error", err)
}
}

Expand All @@ -50,6 +51,19 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ int64)
// update the liquidity provider
k.SetLiquidityProvider(ctx, lp)
}

// increment pool reward amount
pool, err := k.GetPool(ctx, asset.Symbol)
if err != nil {
ctx.Logger().Error(types.ErrPoolDoesNotExist.Error(), "error", err)
continue
}
pool.RewardAmountExternal = pool.RewardAmountExternal.Add(sdk.NewUintFromBigInt(rewardsBucket.Amount.BigInt()))
err = k.SetPool(ctx, &pool)
if err != nil {
ctx.Logger().Error(types.ErrUnableToSetPool.Error(), "error", err)
continue
}
}
}

Expand Down
Loading

0 comments on commit 2ebd543

Please sign in to comment.