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/rewards abci #3430

Merged
merged 15 commits into from
Dec 2, 2023
Prev Previous commit
Next Next commit
feat: keep track of reward distribution
snobbee committed Nov 25, 2023
commit c267fcbf519714ac362a26e505c1afc797388168
3 changes: 2 additions & 1 deletion integrationtest/output/results.json
Original file line number Diff line number Diff line change
@@ -32,7 +32,8 @@
"asset": { "symbol": "cusdc" },
"liquidity_provider_units": "1000000000000000000000",
"liquidity_provider_address": "sif1syavy2npfyt9tcncdtsdzf7kny9lh777yqc2nd",
"last_updated_block": 0
"last_updated_block": 0,
"reward_amount": null
}
}
}
3 changes: 2 additions & 1 deletion integrationtest/output/tc1.json
Original file line number Diff line number Diff line change
@@ -42,7 +42,8 @@
},
"liquidity_provider_units": "1000000000000000000000",
"liquidity_provider_address": "sif1syavy2npfyt9tcncdtsdzf7kny9lh777yqc2nd",
"last_updated_block": 1
"last_updated_block": 1,
"reward_amount": null
}
}
}
3 changes: 2 additions & 1 deletion integrationtest/output/tc2.json
Original file line number Diff line number Diff line change
@@ -60,7 +60,8 @@
},
"liquidity_provider_units": "1000000000000000000000000000",
"liquidity_provider_address": "sif1syavy2npfyt9tcncdtsdzf7kny9lh777yqc2nd",
"last_updated_block": 0
"last_updated_block": 0,
"reward_amount": null
}
}
}
6 changes: 4 additions & 2 deletions integrationtest/output/tc3.json
Original file line number Diff line number Diff line change
@@ -96,15 +96,17 @@
},
"liquidity_provider_units": "1000000000000000000000000000",
"liquidity_provider_address": "sif1syavy2npfyt9tcncdtsdzf7kny9lh777yqc2nd",
"last_updated_block": 0
"last_updated_block": 0,
"reward_amount": null
},
"\u0001cusdc_sif1syavy2npfyt9tcncdtsdzf7kny9lh777yqc2nd": {
"asset": {
"symbol": "cusdc"
},
"liquidity_provider_units": "1000000000000000000000000000",
"liquidity_provider_address": "sif1syavy2npfyt9tcncdtsdzf7kny9lh777yqc2nd",
"last_updated_block": 1
"last_updated_block": 1,
"reward_amount": null
}
}
}
6 changes: 6 additions & 0 deletions proto/sifnode/clp/v1/types.proto
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ syntax = "proto3";
package sifnode.clp.v1;

import "gogoproto/gogo.proto";
import "cosmos/base/coin.proto";

option go_package = "github.com/Sifchain/sifnode/x/clp/types";

@@ -91,6 +92,11 @@ message LiquidityProvider {
string liquidity_provider_address = 3;
repeated LiquidityUnlock unlocks = 4;
int64 last_updated_block = 5; // contains the block height of the last update
// distributed or added to liquidity provider shares rewards
repeated cosmos.base.v1beta1.Coin reward_amount = 6 [
(gogoproto.nullable) = false,
(gogoproto.castrepeated) = "github.com/cosmos/cosmos-sdk/types.Coins"
];
}

message LiquidityUnlock {
6 changes: 6 additions & 0 deletions x/clp/keeper/epoch_hooks.go
Original file line number Diff line number Diff line change
@@ -43,6 +43,12 @@ func (k Keeper) AfterEpochEnd(ctx sdk.Context, epochIdentifier string, _ int64)
ctx.Logger().Error("unable to add reward amount to liquidity pool", "error", err)
}
}

// increment lp reward amount
lp.RewardAmount = lp.RewardAmount.Add(sdk.NewCoin(asset.Symbol, rewardAmounts[i]))

// update the liquidity provider
k.SetLiquidityProvider(ctx, lp)
}
}
}
5 changes: 5 additions & 0 deletions x/clp/keeper/epoch_hooks_test.go
Original file line number Diff line number Diff line change
@@ -117,6 +117,7 @@ func TestAfterEpochEnd_AddToLiquidityPool(t *testing.T) {
LiquidityProviderAddress: signer.String(),
Unlocks: nil,
LastUpdatedBlock: ctx.BlockHeight(),
RewardAmount: nil,
}, lp)

// set last updated block to be before the rewards lock period
@@ -169,6 +170,7 @@ func TestAfterEpochEnd_AddToLiquidityPool(t *testing.T) {
lp, err = clpKeeper.GetLiquidityProvider(ctx, asset.Symbol, signer.String())
require.NoError(t, err)
require.Equal(t, sdk.NewUint(1), lp.LiquidityProviderUnits)
require.Equal(t, sdk.NewCoins(sdk.NewCoin(asset.Symbol, initialAmount)), lp.RewardAmount)
}

func TestAfterEpochEnd_AddToLiquidityPoolWithMultipleLiquidityProviders(t *testing.T) {
@@ -233,6 +235,7 @@ func TestAfterEpochEnd_AddToLiquidityPoolWithMultipleLiquidityProviders(t *testi
LiquidityProviderAddress: signer1.String(),
Unlocks: nil,
LastUpdatedBlock: ctx.BlockHeight(),
RewardAmount: nil,
}, lp1)

// create liquidity provider 2
@@ -313,11 +316,13 @@ func TestAfterEpochEnd_AddToLiquidityPoolWithMultipleLiquidityProviders(t *testi
lp1, err = clpKeeper.GetLiquidityProvider(ctx, asset.Symbol, signer1.String())
require.NoError(t, err)
require.Equal(t, sdk.NewUint(100), lp1.LiquidityProviderUnits)
require.Nil(t, lp1.RewardAmount)

// check liquidity provider units 2
lp2, err = clpKeeper.GetLiquidityProvider(ctx, asset.Symbol, signer2.String())
require.NoError(t, err)
require.Equal(t, sdk.NewUint(128), lp2.LiquidityProviderUnits)
require.Equal(t, sdk.NewCoins(sdk.NewCoin(asset.Symbol, initialAmount)), lp2.RewardAmount)

// check if pool units changed
updatedPool, err := clpKeeper.GetPool(ctx, asset.Symbol)
2 changes: 2 additions & 0 deletions x/clp/keeper/executors.go
Original file line number Diff line number Diff line change
@@ -121,6 +121,8 @@ func (k Keeper) AddLiquidity(ctx sdk.Context, msg *types.MsgAddLiquidity, pool t
if err != nil {
return nil, sdkerrors.Wrap(types.ErrUnableToSetPool, err.Error())
}
// Set LP’s LastUpdatedBlock to current block height
lp.LastUpdatedBlock = ctx.BlockHeight()
// Save LP
k.SetLiquidityProvider(ctx, &lp)
return &lp, err
2 changes: 2 additions & 0 deletions x/clp/keeper/migrations.go
Original file line number Diff line number Diff line change
@@ -96,12 +96,14 @@ func (m Migrator) MigrateToVer5(ctx sdk.Context) error {

// loop over all the LPs and set their last_updated_block to the current block
// this will ensure that they get their rewards
// and initialize the reward amount array
all, err := m.keeper.GetAllLiquidityProviders(ctx)
if err != nil {
return err
}
for _, lp := range all {
lp.LastUpdatedBlock = ctx.BlockHeight()
lp.RewardAmount = sdk.NewCoins()
m.keeper.SetLiquidityProvider(ctx, lp)
}

1 change: 1 addition & 0 deletions x/clp/types/types.go
Original file line number Diff line number Diff line change
@@ -66,6 +66,7 @@ func NewLiquidityProvider(asset *Asset, liquidityProviderUnits sdk.Uint, liquidi
LiquidityProviderUnits: liquidityProviderUnits,
LiquidityProviderAddress: liquidityProviderAddress.String(),
LastUpdatedBlock: lastUpdatedBlock,
RewardAmount: nil,
}
}

Loading