Skip to content

Commit

Permalink
Shares update (#1086)
Browse files Browse the repository at this point in the history
* shares

* test:

* update

* fix

* migrating using iterator

---------

Co-authored-by: avkr003 <[email protected]>
Co-authored-by: Abhinav Kumar <[email protected]>
  • Loading branch information
3 people authored Dec 31, 2024
1 parent 427ea25 commit edfda6c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 16 deletions.
2 changes: 1 addition & 1 deletion x/commitment/keeper/msg_server_uncommit_tokens.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (k Keeper) UncommitTokens(ctx sdk.Context, addr sdk.AccAddress, denom strin

// Update total commitment
params := k.GetParams(ctx)
params.TotalCommitted = params.TotalCommitted.Add(liquidCoins...)
params.TotalCommitted = params.TotalCommitted.Sub(liquidCoins...)
k.SetParams(ctx, params)

// Emit Hook if Eden is uncommitted
Expand Down
4 changes: 4 additions & 0 deletions x/commitment/keeper/msg_server_uncommit_tokens_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ func TestUncommitTokens(t *testing.T) {
Amount: initialCommitted,
}

params := app.CommitmentKeeper.GetParams(ctx)
params.TotalCommitted = sdk.NewCoins(sdk.NewCoin(denom, initialCommitted))
app.CommitmentKeeper.SetParams(ctx, params)

initialCommitments := types.Commitments{
Creator: creator,
CommittedTokens: []*types.CommittedTokens{&committedTokens},
Expand Down
24 changes: 24 additions & 0 deletions x/commitment/keeper/params.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package keeper

import (
storetypes "cosmossdk.io/store/types"
"github.com/cosmos/cosmos-sdk/runtime"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/elys-network/elys/x/commitment/types"
Expand Down Expand Up @@ -51,3 +52,26 @@ func (k Keeper) GetVestingInfo(ctx sdk.Context, baseDenom string) (*types.Vestin

return nil, 0
}

func (k Keeper) V8_ParamsMigration(ctx sdk.Context) {
store := runtime.KVStoreAdapter(k.storeService.OpenKVStore(ctx))
iterator := storetypes.KVStorePrefixIterator(store, types.CommitmentsKeyPrefix)

defer iterator.Close()

var totalCommited sdk.Coins

for ; iterator.Valid(); iterator.Next() {
var val types.Commitments
k.cdc.MustUnmarshal(iterator.Value(), &val)
for _, token := range val.CommittedTokens {
totalCommited = totalCommited.Add(sdk.NewCoin(token.Denom, token.Amount))
}
}

params := k.GetParams(ctx)
params.TotalCommitted = totalCommited
k.SetParams(ctx, params)

return
}
10 changes: 10 additions & 0 deletions x/commitment/migrations/v8_migration.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package migrations

import (
sdk "github.com/cosmos/cosmos-sdk/types"
)

func (m Migrator) V8Migration(ctx sdk.Context) error {
m.keeper.V8_ParamsMigration(ctx)
return nil
}
4 changes: 2 additions & 2 deletions x/commitment/module.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ func (am AppModule) RegisterServices(cfg module.Configurator) {
types.RegisterMsgServer(cfg.MsgServer(), keeper.NewMsgServerImpl(am.keeper))
types.RegisterQueryServer(cfg.QueryServer(), am.keeper)
m := migrations.NewMigrator(am.keeper)
err := cfg.RegisterMigration(types.ModuleName, 6, m.V7Migration)
err := cfg.RegisterMigration(types.ModuleName, 7, m.V8Migration)
if err != nil {
panic(err)
}
Expand All @@ -155,7 +155,7 @@ func (am AppModule) ExportGenesis(ctx sdk.Context, cdc codec.JSONCodec) json.Raw
}

// ConsensusVersion is a sequence number for state-breaking change of the module. It should be incremented on each consensus-breaking change introduced by the module. To avoid wrong/empty versions, the initial version should be set to 1
func (AppModule) ConsensusVersion() uint64 { return 7 }
func (AppModule) ConsensusVersion() uint64 { return 8 }

// BeginBlock contains the logic that is automatically triggered at the beginning of each block
func (am AppModule) BeginBlock(_ context.Context) error {
Expand Down
10 changes: 5 additions & 5 deletions x/masterchef/keeper/external_incentive_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,11 +146,11 @@ func (suite *MasterchefKeeperTestSuite) TestUSDCExternalIncentive() {
// new user join pool with same shares
share := ammtypes.InitPoolSharesSupply.Mul(math.NewIntWithDecimal(1, 5))
suite.T().Log(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId))
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId).String(), "10002000000000000000000000")
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId).String(), "10000000000000000000000000")
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolBalance(suite.ctx, pools[0].PoolId, addr[0]).String(), "10000000000000000000000000")
_, _, err = suite.app.AmmKeeper.JoinPoolNoSwap(suite.ctx, addr[1], pools[0].PoolId, share, sdk.NewCoins(sdk.NewCoin(ptypes.Elys, math.NewInt(10000000)), sdk.NewCoin(ptypes.BaseCurrency, math.NewInt(10000000))))
suite.Require().NoError(err)
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId).String(), "20002000000000000000000000")
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId).String(), "20000000000000000000000000")
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolBalance(suite.ctx, pools[0].PoolId, addr[1]), share)

suite.MintTokenToAddress(addr[0], math.NewIntWithDecimal(100000000, 6), ptypes.ATOM)
Expand Down Expand Up @@ -195,13 +195,13 @@ func (suite *MasterchefKeeperTestSuite) TestUSDCExternalIncentive() {
})
suite.Require().NoError(err)
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "49")
suite.Require().Equal(res.TotalRewards[1].Amount.String(), "4949535046")
suite.Require().Equal(res.TotalRewards[1].Amount.String(), "4950030000")
res, err = suite.app.MasterchefKeeper.UserPendingReward(suite.ctx, &types.QueryUserPendingRewardRequest{
User: addr[1].String(),
})
suite.Require().NoError(err)
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "49")
suite.Require().Equal(res.TotalRewards[1].Amount.String(), "4949535046")
suite.Require().Equal(res.TotalRewards[1].Amount.String(), "4950030000")

prevUSDCBal := suite.app.BankKeeper.GetBalance(suite.ctx, addr[1], ptypes.BaseCurrency)

Expand All @@ -218,7 +218,7 @@ func (suite *MasterchefKeeperTestSuite) TestUSDCExternalIncentive() {
suite.Require().NoError(err)

curUSDCBal := suite.app.BankKeeper.GetBalance(suite.ctx, addr[1], ptypes.BaseCurrency)
amount, _ := math.NewIntFromString("4949535046")
amount, _ := math.NewIntFromString("4950030000")
suite.Require().Equal(curUSDCBal.Amount.String(), prevUSDCBal.Amount.Add(amount).String())

// no pending rewards
Expand Down
16 changes: 8 additions & 8 deletions x/masterchef/keeper/hooks_masterchef_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,11 +97,11 @@ func (suite *MasterchefKeeperTestSuite) TestHookMasterchef() {
// new user join pool with same shares
share := ammtypes.InitPoolSharesSupply.Mul(math.NewIntWithDecimal(1, 5))
suite.T().Log(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId))
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId).String(), "10002000000000000000000000")
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId).String(), "10000000000000000000000000")
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolBalance(suite.ctx, pools[0].PoolId, addr[0]).String(), "10000000000000000000000000")
_, _, err = suite.app.AmmKeeper.JoinPoolNoSwap(suite.ctx, addr[1], pools[0].PoolId, share, sdk.NewCoins(sdk.NewCoin(ptypes.Elys, math.NewInt(10000000)), sdk.NewCoin(ptypes.BaseCurrency, math.NewInt(10000000))))
suite.Require().NoError(err)
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId).String(), "20002000000000000000000000")
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolTotalCommit(suite.ctx, pools[0].PoolId).String(), "20000000000000000000000000")
suite.Require().Equal(suite.app.MasterchefKeeper.GetPoolBalance(suite.ctx, pools[0].PoolId, addr[1]), share)

// Mint uatom
Expand Down Expand Up @@ -140,12 +140,12 @@ func (suite *MasterchefKeeperTestSuite) TestHookMasterchef() {
User: addr[0].String(),
})
suite.Require().NoError(err)
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "4949505049")
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "4950000000")
res, err = suite.app.MasterchefKeeper.UserPendingReward(ctx, &types.QueryUserPendingRewardRequest{
User: addr[1].String(),
})
suite.Require().NoError(err)
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "4949505049")
suite.Require().Equal(res.TotalRewards[0].Amount.String(), "4950000000")

// check rewards claimed
_, err = suite.msgServer.ClaimRewards(ctx, &types.MsgClaimRewards{
Expand All @@ -160,7 +160,7 @@ func (suite *MasterchefKeeperTestSuite) TestHookMasterchef() {
suite.Require().NoError(err)

atomAmount := suite.app.BankKeeper.GetBalance(ctx, addr[1], "uatom")
suite.Require().Equal(atomAmount.Amount.String(), "4949505049")
suite.Require().Equal(atomAmount.Amount.String(), "4950000000")

// no pending rewards
res, err = suite.app.MasterchefKeeper.UserPendingReward(ctx, &types.QueryUserPendingRewardRequest{
Expand Down Expand Up @@ -192,12 +192,12 @@ func (suite *MasterchefKeeperTestSuite) TestHookMasterchef() {
User: addr[0].String(),
})
suite.Require().NoError(err)
suite.Require().Equal(res.TotalRewards[0].String(), "3999680025uatom")
suite.Require().Equal(res.TotalRewards[0].String(), "6666666666uatom")
res, err = suite.app.MasterchefKeeper.UserPendingReward(ctx, &types.QueryUserPendingRewardRequest{
User: addr[1].String(),
})
suite.Require().NoError(err)
suite.Require().Equal(res.TotalRewards[0].String(), "1999840012uatom")
suite.Require().Equal(res.TotalRewards[0].String(), "3333333333uatom")

// check rewards claimed
_, err = suite.msgServer.ClaimRewards(ctx, &types.MsgClaimRewards{
Expand All @@ -212,7 +212,7 @@ func (suite *MasterchefKeeperTestSuite) TestHookMasterchef() {
suite.Require().NoError(err)

atomAmount = suite.app.BankKeeper.GetBalance(ctx, addr[1], "uatom")
suite.Require().Equal(atomAmount.String(), "6949345061uatom")
suite.Require().Equal(atomAmount.String(), "8283333333uatom")

// no pending rewards
res, err = suite.app.MasterchefKeeper.UserPendingReward(ctx, &types.QueryUserPendingRewardRequest{
Expand Down

0 comments on commit edfda6c

Please sign in to comment.