Skip to content

Commit

Permalink
feat: keep track of reward distribution
Browse files Browse the repository at this point in the history
  • Loading branch information
snobbee committed Nov 25, 2023
1 parent 5e8e6e6 commit c267fcb
Show file tree
Hide file tree
Showing 11 changed files with 172 additions and 77 deletions.
3 changes: 2 additions & 1 deletion integrationtest/output/results.json
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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
Expand Up @@ -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";

Expand Down Expand Up @@ -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 {
Expand Down
6 changes: 6 additions & 0 deletions x/clp/keeper/epoch_hooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
}
}
Expand Down
5 changes: 5 additions & 0 deletions x/clp/keeper/epoch_hooks_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -233,6 +235,7 @@ func TestAfterEpochEnd_AddToLiquidityPoolWithMultipleLiquidityProviders(t *testi
LiquidityProviderAddress: signer1.String(),
Unlocks: nil,
LastUpdatedBlock: ctx.BlockHeight(),
RewardAmount: nil,
}, lp1)

// create liquidity provider 2
Expand Down Expand Up @@ -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)
Expand Down
2 changes: 2 additions & 0 deletions x/clp/keeper/executors.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions x/clp/keeper/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

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

Expand Down
Loading

0 comments on commit c267fcb

Please sign in to comment.