From dabd7e8312a462e70e3495a00000dc25b10eef98 Mon Sep 17 00:00:00 2001 From: kenta-elys <130330089+kenta-elys@users.noreply.github.com> Date: Thu, 21 Sep 2023 18:51:25 +0900 Subject: [PATCH] Incentive module Eden rewards calculation algorithm update. (#198) * chore: manage eden boost burning from elys unstake and eden uncommitted * chore: add abci test * refactor: margin functions (#194) * refactor: margin functions * fix: test cases * fix: mocks * test: fix test cases * fix: issue with staking hook handling and burning amount calculation * chore: track Elys staked, Eden committed and EdenB committed separately in Eden rewards calculation * fix: update comments for elys staked --------- Co-authored-by: Cosmic Vagabond <121588426+cosmic-vagabond@users.noreply.github.com> --- config.yml | 1 + docs/static/openapi.yml | 9 + proto/elys/incentive/elys_staked.proto | 17 + proto/elys/incentive/params.proto | 2 + proto/elys/incentive/query.proto | 1 - x/commitment/keeper/hooks.go | 15 + .../keeper/msg_server_uncommit_tokens.go | 6 + x/commitment/types/interfaces.go | 3 + x/incentive/keeper/abci.go | 63 +++ x/incentive/keeper/abci_test.go | 50 +++ x/incentive/keeper/elys_staked.go | 63 +++ x/incentive/keeper/hooks_commitment.go | 10 + x/incentive/keeper/hooks_staking.go | 2 +- x/incentive/keeper/keeper.go | 16 +- x/incentive/keeper/keeper_burn_edenB.go | 92 ++++ x/incentive/keeper/keeper_burn_edenB_test.go | 158 +++++++ x/incentive/keeper/keeper_stakers.go | 37 +- x/incentive/keeper/keeper_stakers_test.go | 24 +- x/incentive/module.go | 4 +- x/incentive/types/elys_staked.pb.go | 396 ++++++++++++++++++ x/incentive/types/expected_keepers.go | 2 + x/incentive/types/genesis.go | 2 +- x/incentive/types/keys.go | 15 + x/incentive/types/params.go | 44 +- x/incentive/types/params.pb.go | 92 ++-- 25 files changed, 1067 insertions(+), 57 deletions(-) create mode 100644 proto/elys/incentive/elys_staked.proto create mode 100644 x/incentive/keeper/abci.go create mode 100644 x/incentive/keeper/abci_test.go create mode 100644 x/incentive/keeper/elys_staked.go create mode 100644 x/incentive/keeper/keeper_burn_edenB.go create mode 100644 x/incentive/keeper/keeper_burn_edenB_test.go create mode 100644 x/incentive/types/elys_staked.pb.go diff --git a/config.yml b/config.yml index 770e65a91..a8c80126b 100644 --- a/config.yml +++ b/config.yml @@ -238,6 +238,7 @@ genesis: withdraw_addr_enabled: true reward_portion_for_lps: "0.65" pool_infos: [] + elys_stake_tracking_rate: "10" fee_pool: community_pool: - amount: "0" diff --git a/docs/static/openapi.yml b/docs/static/openapi.yml index 1717056b1..ae44dc8e2 100644 --- a/docs/static/openapi.yml +++ b/docs/static/openapi.yml @@ -38979,6 +38979,9 @@ paths: title: |- Pool information poolId, reward wallet, mulitplier + elys_stake_tracking_rate: + type: string + format: int64 description: >- QueryParamsResponse is response type for the Query/Params RPC method. @@ -78818,6 +78821,9 @@ definitions: title: |- Pool information poolId, reward wallet, mulitplier + elys_stake_tracking_rate: + type: string + format: int64 description: Params defines the parameters for the module. elys.incentive.PoolInfo: type: object @@ -78938,6 +78944,9 @@ definitions: title: |- Pool information poolId, reward wallet, mulitplier + elys_stake_tracking_rate: + type: string + format: int64 description: QueryParamsResponse is response type for the Query/Params RPC method. elys.liquidityprovider.Params: type: object diff --git a/proto/elys/incentive/elys_staked.proto b/proto/elys/incentive/elys_staked.proto new file mode 100644 index 000000000..d48647966 --- /dev/null +++ b/proto/elys/incentive/elys_staked.proto @@ -0,0 +1,17 @@ +syntax = "proto3"; +package elys.incentive; + +option go_package = "github.com/elys-network/elys/x/incentive/types"; +option (gogoproto.equal_all) = true; + +import "gogoproto/gogo.proto"; + +// Elys staked +message ElysStaked { + string address = 1; + string amount = 2 [ + (gogoproto.customtype) = "github.com/cosmos/cosmos-sdk/types.Int", + (gogoproto.nullable) = false + ]; + } + \ No newline at end of file diff --git a/proto/elys/incentive/params.proto b/proto/elys/incentive/params.proto index 6e8f0ff80..05f445d09 100644 --- a/proto/elys/incentive/params.proto +++ b/proto/elys/incentive/params.proto @@ -28,4 +28,6 @@ message Params { // Pool information // poolId, reward wallet, mulitplier repeated PoolInfo pool_infos = 6 [(gogoproto.nullable) = false]; + + int64 elys_stake_tracking_rate = 7; } diff --git a/proto/elys/incentive/query.proto b/proto/elys/incentive/query.proto index 33217a4bf..191b8fc20 100644 --- a/proto/elys/incentive/query.proto +++ b/proto/elys/incentive/query.proto @@ -25,7 +25,6 @@ service Query { option (google.api.http).get = "/elys-network/elys/incentive/community_pool"; } - } // QueryParamsRequest is request type for the Query/Params RPC method. message QueryParamsRequest {} diff --git a/x/commitment/keeper/hooks.go b/x/commitment/keeper/hooks.go index 86b8ae763..55b216868 100644 --- a/x/commitment/keeper/hooks.go +++ b/x/commitment/keeper/hooks.go @@ -21,6 +21,13 @@ func (mh MultiCommitmentHooks) CommitmentChanged(ctx sdk.Context, creator string } } +// Committed is called when staker committed his token +func (mh MultiCommitmentHooks) EdenUncommitted(ctx sdk.Context, creator string, amount sdk.Coin) { + for i := range mh { + mh[i].EdenUncommitted(ctx, creator, amount) + } +} + // Committed executes the indicated for committed hook func (k Keeper) AfterCommitmentChange(ctx sdk.Context, creator string, amount sdk.Coin) { if k.hooks == nil { @@ -28,3 +35,11 @@ func (k Keeper) AfterCommitmentChange(ctx sdk.Context, creator string, amount sd } k.hooks.CommitmentChanged(ctx, creator, amount) } + +// Committed executes the indicated for committed hook +func (k Keeper) EdenUncommitted(ctx sdk.Context, creator string, amount sdk.Coin) { + if k.hooks == nil { + return + } + k.hooks.EdenUncommitted(ctx, creator, amount) +} diff --git a/x/commitment/keeper/msg_server_uncommit_tokens.go b/x/commitment/keeper/msg_server_uncommit_tokens.go index 5c4e00e74..53023c630 100644 --- a/x/commitment/keeper/msg_server_uncommit_tokens.go +++ b/x/commitment/keeper/msg_server_uncommit_tokens.go @@ -7,6 +7,7 @@ import ( sdkerrors "github.com/cosmos/cosmos-sdk/types/errors" aptypes "github.com/elys-network/elys/x/assetprofile/types" "github.com/elys-network/elys/x/commitment/types" + ptypes "github.com/elys-network/elys/x/parameter/types" ) func (k msgServer) UncommitTokens(goCtx context.Context, msg *types.MsgUncommitTokens) (*types.MsgUncommitTokensResponse, error) { @@ -58,6 +59,11 @@ func (k msgServer) UncommitTokens(goCtx context.Context, msg *types.MsgUncommitT // Emit Hook commitment changed k.AfterCommitmentChange(ctx, msg.Creator, sdk.NewCoin(msg.Denom, msg.Amount)) + // Emit Hook if Eden is uncommitted + if msg.Denom == ptypes.Eden { + k.EdenUncommitted(ctx, msg.Creator, sdk.NewCoin(msg.Denom, msg.Amount)) + } + // Emit blockchain event ctx.EventManager().EmitEvent( sdk.NewEvent( diff --git a/x/commitment/types/interfaces.go b/x/commitment/types/interfaces.go index f0ef60a5d..9247e744c 100644 --- a/x/commitment/types/interfaces.go +++ b/x/commitment/types/interfaces.go @@ -6,4 +6,7 @@ import sdk "github.com/cosmos/cosmos-sdk/types" type CommitmentHooks interface { // Token commitment changed CommitmentChanged(ctx sdk.Context, creator string, amount sdk.Coin) + + // Eden uncommitted + EdenUncommitted(ctx sdk.Context, creator string, amount sdk.Coin) } diff --git a/x/incentive/keeper/abci.go b/x/incentive/keeper/abci.go new file mode 100644 index 000000000..150059152 --- /dev/null +++ b/x/incentive/keeper/abci.go @@ -0,0 +1,63 @@ +// Copyright 2022 Evmos Foundation +// This file is part of the Evmos Network packages. +// +// Evmos is free software: you can redistribute it and/or modify +// it under the terms of the GNU Lesser General Public License as published by +// the Free Software Foundation, either version 3 of the License, or +// (at your option) any later version. +// +// The Evmos packages are distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU Lesser General Public License for more details. +// +// You should have received a copy of the GNU Lesser General Public License +// along with the Evmos packages. If not, see https://github.com/evmos/evmos/blob/main/LICENSE + +package keeper + +import ( + "time" + + "github.com/cosmos/cosmos-sdk/telemetry" + sdk "github.com/cosmos/cosmos-sdk/types" + + ctypes "github.com/elys-network/elys/x/commitment/types" + "github.com/elys-network/elys/x/incentive/types" +) + +// EndBlocker of incentive module +func (k Keeper) EndBlocker(ctx sdk.Context) { + defer telemetry.ModuleMeasureSince(types.ModuleName, time.Now(), telemetry.MetricKeyEndBlocker) + params := k.GetParams(ctx) + // Update Elys staked amount every n blocks + if params.ElysStakeTrackingRate == 0 || ctx.BlockHeight()%params.ElysStakeTrackingRate != 0 { + return + } + + // Track the amount of Elys staked + k.cmk.IterateCommitments( + ctx, func(commitments ctypes.Commitments) bool { + // Commitment owner + creator := commitments.Creator + _, err := sdk.AccAddressFromBech32(creator) + if err != nil { + // This could be validator address + return false + } + + // Calculate delegated amount per delegator + delegatedAmt := k.CalculateDelegatedAmount(ctx, creator) + + elysStaked := types.ElysStaked{ + Address: creator, + Amount: delegatedAmt, + } + + // Set Elys staked amount + k.SetElysStaked(ctx, elysStaked) + + return false + }, + ) +} diff --git a/x/incentive/keeper/abci_test.go b/x/incentive/keeper/abci_test.go new file mode 100644 index 000000000..d808bf78a --- /dev/null +++ b/x/incentive/keeper/abci_test.go @@ -0,0 +1,50 @@ +package keeper_test + +import ( + "testing" + + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + sdk "github.com/cosmos/cosmos-sdk/types" + simapp "github.com/elys-network/elys/app" + ctypes "github.com/elys-network/elys/x/commitment/types" + ptypes "github.com/elys-network/elys/x/parameter/types" + "github.com/stretchr/testify/require" +) + +func TestABCI_EndBlocker(t *testing.T) { + app, genAccount, _ := simapp.InitElysTestAppWithGenAccount() + ctx := app.BaseApp.NewContext(initChain, tmproto.Header{}) + + ik := app.IncentiveKeeper + + var committed []sdk.Coins + var uncommitted []sdk.Coins + + // Prepare uncommitted tokens + uedenToken := sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdk.NewInt(2000))) + uedenBToken := sdk.NewCoins(sdk.NewCoin(ptypes.EdenB, sdk.NewInt(2000))) + uncommitted = append(uncommitted, uedenToken) + uncommitted = append(uncommitted, uedenBToken) + + // Eden + err := app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenToken) + require.NoError(t, err) + + // EdenB + err = app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenBToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenBToken) + require.NoError(t, err) + + // Add testing commitment + simapp.AddTestCommitment(app, ctx, genAccount, committed, uncommitted) + // Update Elys staked amount + ik.EndBlocker(ctx) + + // Get elys staked + elysStaked, found := ik.GetElysStaked(ctx, genAccount.String()) + require.Equal(t, found, true) + require.Equal(t, elysStaked.Amount, sdk.DefaultPowerReduction) +} diff --git a/x/incentive/keeper/elys_staked.go b/x/incentive/keeper/elys_staked.go new file mode 100644 index 000000000..0da23b731 --- /dev/null +++ b/x/incentive/keeper/elys_staked.go @@ -0,0 +1,63 @@ +package keeper + +import ( + "github.com/cosmos/cosmos-sdk/store/prefix" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/elys-network/elys/x/incentive/types" +) + +// SetElysStaked set a specific elysStaked in the store from its index +func (k Keeper) SetElysStaked(ctx sdk.Context, elysStaked types.ElysStaked) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ElysStakedKeyPrefix)) + b := k.cdc.MustMarshal(&elysStaked) + store.Set(types.ElysStakedKey( + elysStaked.Address, + ), b) +} + +// GetElysStaked returns a elysStaked from its index +func (k Keeper) GetElysStaked( + ctx sdk.Context, + address string, + +) (val types.ElysStaked, found bool) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ElysStakedKeyPrefix)) + + b := store.Get(types.ElysStakedKey( + address, + )) + if b == nil { + return val, false + } + + k.cdc.MustUnmarshal(b, &val) + return val, true +} + +// RemoveElysStaked removes a elysStaked from the store +func (k Keeper) RemoveElysStaked( + ctx sdk.Context, + address string, + +) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ElysStakedKeyPrefix)) + store.Delete(types.ElysStakedKey( + address, + )) +} + +// GetAllElysStaked returns all elysStaked +func (k Keeper) GetAllElysStaked(ctx sdk.Context) (list []types.ElysStaked) { + store := prefix.NewStore(ctx.KVStore(k.storeKey), types.KeyPrefix(types.ElysStakedKeyPrefix)) + iterator := sdk.KVStorePrefixIterator(store, []byte{}) + + defer iterator.Close() + + for ; iterator.Valid(); iterator.Next() { + var val types.ElysStaked + k.cdc.MustUnmarshal(iterator.Value(), &val) + list = append(list, val) + } + + return +} diff --git a/x/incentive/keeper/hooks_commitment.go b/x/incentive/keeper/hooks_commitment.go index 278b34afd..638b18500 100644 --- a/x/incentive/keeper/hooks_commitment.go +++ b/x/incentive/keeper/hooks_commitment.go @@ -9,6 +9,11 @@ import ( func (k Keeper) CommitmentChanged(ctx sdk.Context, creator string, amount sdk.Coin) { } +// Process eden uncommitted hook +func (k Keeper) EdenUncommitted(ctx sdk.Context, creator string, amount sdk.Coin) { + k.BurnEdenBFromEdenUncommitted(ctx, creator, amount.Amount) +} + // ___________________________________________________________________________________________________ // Hooks wrapper struct for incentive keeper @@ -27,3 +32,8 @@ func (k Keeper) CommitmentHooks() CommitmentHooks { func (h CommitmentHooks) CommitmentChanged(ctx sdk.Context, creator string, amount sdk.Coin) { h.k.CommitmentChanged(ctx, creator, amount) } + +// EdenUncommitted implements EdenUncommitted +func (h CommitmentHooks) EdenUncommitted(ctx sdk.Context, creator string, amount sdk.Coin) { + h.k.EdenUncommitted(ctx, creator, amount) +} diff --git a/x/incentive/keeper/hooks_staking.go b/x/incentive/keeper/hooks_staking.go index 3099077ce..ef76f14ba 100644 --- a/x/incentive/keeper/hooks_staking.go +++ b/x/incentive/keeper/hooks_staking.go @@ -22,7 +22,7 @@ func (k Keeper) BeforeDelegationCreated(ctx sdk.Context, delAddr sdk.AccAddress, // Updating commitments on delegation changes func (k Keeper) AfterDelegationModified(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error { - return nil + return k.BurnEdenBFromElysUnstaking(ctx, delAddr) } func (k Keeper) BeforeDelegationRemoved(ctx sdk.Context, delAddr sdk.AccAddress, valAddr sdk.ValAddress) error { diff --git a/x/incentive/keeper/keeper.go b/x/incentive/keeper/keeper.go index 37c60e6e6..44bdd2a53 100644 --- a/x/incentive/keeper/keeper.go +++ b/x/incentive/keeper/keeper.go @@ -140,7 +140,21 @@ func (k Keeper) UpdateUncommittedTokens(ctx sdk.Context, epochIdentifier string, // Calculate new uncommitted Eden tokens from Eden & Eden boost committed, Dex rewards distribution // Distribute gas fees to stakers - newUncommittedEdenTokens, dexRewards, dexRewardsByStakers := k.CalculateRewardsForStakers(ctx, delegatedAmt, commitments, edenAmountPerEpochStakers, dexRevenueStakersAmt) + + // Calculate new uncommitted Eden tokens from Elys staked + newUncommittedEdenTokens, dexRewards, dexRewardsByStakers := k.CalculateRewardsForStakersByElysStaked(ctx, delegatedAmt, edenAmountPerEpochStakers, dexRevenueStakersAmt) + totalEdenGiven = totalEdenGiven.Add(newUncommittedEdenTokens) + totalRewardsGiven = totalRewardsGiven.Add(dexRewards) + + // Calculate new uncommitted Eden tokens from Eden committed + edenCommitted := commitments.GetCommittedAmountForDenom(ptypes.Eden) + newUncommittedEdenTokens, dexRewards = k.CalculateRewardsForStakersByCommitted(ctx, edenCommitted, edenAmountPerEpochStakers, dexRevenueStakersAmt) + totalEdenGiven = totalEdenGiven.Add(newUncommittedEdenTokens) + totalRewardsGiven = totalRewardsGiven.Add(dexRewards) + + // Calculate new uncommitted Eden tokens from Eden Boost committed + edenBoostCommitted := commitments.GetCommittedAmountForDenom(ptypes.EdenB) + newUncommittedEdenTokens, dexRewards = k.CalculateRewardsForStakersByCommitted(ctx, edenBoostCommitted, edenAmountPerEpochStakers, dexRevenueStakersAmt) totalEdenGiven = totalEdenGiven.Add(newUncommittedEdenTokens) totalRewardsGiven = totalRewardsGiven.Add(dexRewards) diff --git a/x/incentive/keeper/keeper_burn_edenB.go b/x/incentive/keeper/keeper_burn_edenB.go new file mode 100644 index 000000000..b3d06fb29 --- /dev/null +++ b/x/incentive/keeper/keeper_burn_edenB.go @@ -0,0 +1,92 @@ +package keeper + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" + ptypes "github.com/elys-network/elys/x/parameter/types" +) + +// Burn EdenBoost from Elys unstaked +func (k Keeper) BurnEdenBFromElysUnstaking(ctx sdk.Context, delegator sdk.AccAddress) error { + delAddr := delegator.String() + // Get commitments + commitments, found := k.cmk.GetCommitments(ctx, delAddr) + // should return nil otherwise it will break staking module + if !found { + return nil + } + + // Get previous amount + prevElysStaked, found := k.GetElysStaked(ctx, delAddr) + // should return nil otherwise it will break staking module + if !found { + return nil + } + + // Calculate current delegated amount of delegator + delegatedAmt := k.CalculateDelegatedAmount(ctx, delAddr) + + // If not unstaked, + // should return nil otherwise it will break staking module + if delegatedAmt.GTE(prevElysStaked.Amount) { + return nil + } + + edenCommitted := commitments.GetCommittedAmountForDenom(ptypes.Eden) + + //Total EdenB amount + edenBCommitted := commitments.GetCommittedAmountForDenom(ptypes.EdenB) + edenBUncommitted := commitments.GetUncommittedAmountForDenom(ptypes.EdenB) + + // Total EdenB amount + totalEdenB := edenBCommitted.Add(edenBUncommitted) + + // Unstaked + unstakedElys := prevElysStaked.Amount.Sub(delegatedAmt) + + unstakedElysDec := sdk.NewDecFromInt(unstakedElys) + edenCommittedAndElysStakedDec := sdk.NewDecFromInt(edenCommitted.Add(prevElysStaked.Amount)) + totalEdenBDec := sdk.NewDecFromInt(totalEdenB) + edenBToBurn := unstakedElysDec.Quo(edenCommittedAndElysStakedDec).Mul(totalEdenBDec) + + // Burn EdenB ( Deduction EdenB in commitment module) + commitment, err := k.cmk.DeductCommitments(ctx, delAddr, ptypes.EdenB, edenBToBurn.TruncateInt()) + k.cmk.SetCommitments(ctx, commitment) + + return err +} + +// Burn EdenBoost from Eden uncommitted +func (k Keeper) BurnEdenBFromEdenUncommitted(ctx sdk.Context, delegator string, uncommittedAmt sdk.Int) error { + // Get elys staked amount + elysStaked, found := k.GetElysStaked(ctx, delegator) + if !found { + return nil + } + + commitments, found := k.cmk.GetCommitments(ctx, delegator) + // should return nil otherwise it will break commitment module + if !found { + return nil + } + + edenCommitted := commitments.GetCommittedAmountForDenom(ptypes.Eden) + + //Total EdenB amount + edenBCommitted := commitments.GetCommittedAmountForDenom(ptypes.EdenB) + edenBUncommitted := commitments.GetUncommittedAmountForDenom(ptypes.EdenB) + + // Total EdenB amount + totalEdenB := edenBCommitted.Add(edenBUncommitted) + + uncommittedAmtDec := sdk.NewDecFromInt(uncommittedAmt) + edenCommittedAndElysStakedDec := sdk.NewDecFromInt(edenCommitted.Add(elysStaked.Amount)) + totalEdenBDec := sdk.NewDecFromInt(totalEdenB) + + edenBToBurn := uncommittedAmtDec.Quo(edenCommittedAndElysStakedDec).Mul(totalEdenBDec) + + // Burn EdenB ( Deduction EdenB in commitment module) + commitment, err := k.cmk.DeductCommitments(ctx, delegator, ptypes.EdenB, edenBToBurn.TruncateInt()) + k.cmk.SetCommitments(ctx, commitment) + + return err +} diff --git a/x/incentive/keeper/keeper_burn_edenB_test.go b/x/incentive/keeper/keeper_burn_edenB_test.go new file mode 100644 index 000000000..7b37fb811 --- /dev/null +++ b/x/incentive/keeper/keeper_burn_edenB_test.go @@ -0,0 +1,158 @@ +package keeper_test + +import ( + "testing" + + tmproto "github.com/cometbft/cometbft/proto/tendermint/types" + sdk "github.com/cosmos/cosmos-sdk/types" + simapp "github.com/elys-network/elys/app" + aptypes "github.com/elys-network/elys/x/assetprofile/types" + commkeeper "github.com/elys-network/elys/x/commitment/keeper" + ctypes "github.com/elys-network/elys/x/commitment/types" + ptypes "github.com/elys-network/elys/x/parameter/types" + "github.com/stretchr/testify/require" +) + +func TestBurnEdenBFromElysUnstaked(t *testing.T) { + app, genAccount, valAddr := simapp.InitElysTestAppWithGenAccount() + ctx := app.BaseApp.NewContext(initChain, tmproto.Header{}) + + ik, sk := app.IncentiveKeeper, app.StakingKeeper + + var committed []sdk.Coins + var uncommitted []sdk.Coins + + // Prepare uncommitted tokens + uedenToken := sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdk.NewInt(2000))) + uedenBToken := sdk.NewCoins(sdk.NewCoin(ptypes.EdenB, sdk.NewInt(20000))) + uncommitted = append(uncommitted, uedenToken) + uncommitted = append(uncommitted, uedenBToken) + + // Eden + err := app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenToken) + require.NoError(t, err) + + // EdenB + err = app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenBToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenBToken) + require.NoError(t, err) + + // Prepare committed tokens + uedenToken = sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdk.NewInt(10000))) + uedenBToken = sdk.NewCoins(sdk.NewCoin(ptypes.EdenB, sdk.NewInt(5000))) + committed = append(committed, uedenToken) + committed = append(committed, uedenBToken) + + // Eden + err = app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenToken) + require.NoError(t, err) + + // EdenB + err = app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenBToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenBToken) + require.NoError(t, err) + + // Add testing commitment + simapp.AddTestCommitment(app, ctx, genAccount, committed, uncommitted) + + commitment, found := app.CommitmentKeeper.GetCommitments(ctx, genAccount.String()) + require.True(t, found) + require.Equal(t, commitment.UncommittedTokens[1].Denom, ptypes.EdenB) + require.Equal(t, commitment.UncommittedTokens[1].Amount, sdk.NewInt(20000)) + + // Track Elys staked amount + ik.EndBlocker(ctx) + + // burn amount = 100000 (unbonded amt) / (1000000 (elys staked) + 10000 (Eden committed)) * (20000 EdenB + 5000 EdenB committed) + unbondAmt, err := sk.Unbond(ctx, genAccount, valAddr, sdk.NewDecWithPrec(10, 2)) + require.Equal(t, unbondAmt, sdk.NewInt(100000)) + + commitment, found = app.CommitmentKeeper.GetCommitments(ctx, genAccount.String()) + require.True(t, found) + + require.Equal(t, commitment.UncommittedTokens[1].Denom, ptypes.EdenB) + require.Equal(t, commitment.UncommittedTokens[1].Amount, sdk.NewInt(17525)) +} + +func TestBurnEdenBFromEdenUncommitted(t *testing.T) { + app, genAccount, _ := simapp.InitElysTestAppWithGenAccount() + ctx := app.BaseApp.NewContext(initChain, tmproto.Header{}) + + ik, cmk := app.IncentiveKeeper, app.CommitmentKeeper + + var committed []sdk.Coins + var uncommitted []sdk.Coins + + // Prepare uncommitted tokens + uedenToken := sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdk.NewInt(2000))) + uedenBToken := sdk.NewCoins(sdk.NewCoin(ptypes.EdenB, sdk.NewInt(20000))) + uncommitted = append(uncommitted, uedenToken) + uncommitted = append(uncommitted, uedenBToken) + + // Eden + err := app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenToken) + require.NoError(t, err) + + // EdenB + err = app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenBToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenBToken) + require.NoError(t, err) + + // Prepare committed tokens + uedenToken = sdk.NewCoins(sdk.NewCoin(ptypes.Eden, sdk.NewInt(10000))) + uedenBToken = sdk.NewCoins(sdk.NewCoin(ptypes.EdenB, sdk.NewInt(5000))) + committed = append(committed, uedenToken) + committed = append(committed, uedenBToken) + + // Eden + err = app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenToken) + require.NoError(t, err) + + // EdenB + err = app.BankKeeper.MintCoins(ctx, ctypes.ModuleName, uedenBToken) + require.NoError(t, err) + err = app.BankKeeper.SendCoinsFromModuleToAccount(ctx, ctypes.ModuleName, genAccount, uedenBToken) + require.NoError(t, err) + + // Add testing commitment + simapp.AddTestCommitment(app, ctx, genAccount, committed, uncommitted) + + commitment, found := app.CommitmentKeeper.GetCommitments(ctx, genAccount.String()) + require.True(t, found) + require.Equal(t, commitment.UncommittedTokens[1].Denom, ptypes.EdenB) + require.Equal(t, commitment.UncommittedTokens[1].Amount, sdk.NewInt(20000)) + + // Track Elys staked amount + ik.EndBlocker(ctx) + + // Set assetprofile entry for denom + app.AssetprofileKeeper.SetEntry(ctx, aptypes.Entry{BaseDenom: ptypes.Eden, CommitEnabled: true}) + + msg := &ctypes.MsgUncommitTokens{ + Creator: genAccount.String(), + Amount: sdk.NewInt(1000), + Denom: ptypes.Eden, + } + + msgServer := commkeeper.NewMsgServerImpl(cmk) + _, err = msgServer.UncommitTokens(sdk.WrapSDKContext(ctx), msg) + require.NoError(t, err) + + // burn amount = 1000 (uncommitted amt) / (1000000 (elys staked) + 10000 (Eden committed)) * (20000 EdenB + 5000 EdenB committed) + commitment, found = app.CommitmentKeeper.GetCommitments(ctx, genAccount.String()) + require.True(t, found) + + require.Equal(t, commitment.UncommittedTokens[1].Denom, ptypes.EdenB) + require.Equal(t, commitment.UncommittedTokens[1].Amount, sdk.NewInt(19976)) +} diff --git a/x/incentive/keeper/keeper_stakers.go b/x/incentive/keeper/keeper_stakers.go index e8e694eb8..455c99d9d 100644 --- a/x/incentive/keeper/keeper_stakers.go +++ b/x/incentive/keeper/keeper_stakers.go @@ -7,16 +7,10 @@ import ( ) // Calculate new Eden token amounts based on the given conditions and user's current uncommitted token balance -func (k Keeper) CalculateRewardsForStakers(ctx sdk.Context, delegatedAmt sdk.Int, commitments ctypes.Commitments, edenAmountPerEpoch sdk.Int, dexRevenueAmtForStakers sdk.Dec) (sdk.Int, sdk.Int, sdk.Dec) { - // -----------Eden & Eden boost calculation --------------------- +func (k Keeper) CalculateRewardsForStakersByElysStaked(ctx sdk.Context, delegatedAmt sdk.Int, edenAmountPerEpoch sdk.Int, dexRevenueAmtForStakers sdk.Dec) (sdk.Int, sdk.Int, sdk.Dec) { + // -----------Eden calculation --------------------- // -------------------------------------------------------------- - // Get eden commitments and eden boost commitments - edenCommitted := commitments.GetCommittedAmountForDenom(ptypes.Eden) - edenBoostCommitted := commitments.GetCommittedAmountForDenom(ptypes.EdenB) - - // compute eden reward based on above and param factors for each - totalEdenCommittedByStake := delegatedAmt.Add(edenCommitted).Add(edenBoostCommitted) - stakeShare := k.CalculateTotalShareOfStaking(totalEdenCommittedByStake) + stakeShare := k.CalculateTotalShareOfStaking(delegatedAmt) // Calculate newly creating eden amount by its share newEdenAllocated := stakeShare.MulInt(edenAmountPerEpoch) @@ -41,6 +35,31 @@ func (k Keeper) CalculateRewardsForStakers(ctx sdk.Context, delegatedAmt sdk.Int return newEdenAllocated.TruncateInt(), dexRewards, dexRewardsByStakeOnly } +// Calculate new Eden token amounts based on the given conditions and user's current uncommitted token balance +func (k Keeper) CalculateRewardsForStakersByCommitted(ctx sdk.Context, amt sdk.Int, edenAmountPerEpoch sdk.Int, dexRevenueAmtForStakers sdk.Dec) (sdk.Int, sdk.Int) { + // -----------Eden calculation --------------------- + // -------------------------------------------------------------- + stakeShare := k.CalculateTotalShareOfStaking(amt) + + // Calculate newly creating eden amount by its share + newEdenAllocated := stakeShare.MulInt(edenAmountPerEpoch) + + // -----------------Fund community Eden token---------------------- + // ---------------------------------------------------------------- + edenCoin := sdk.NewDecCoinFromDec(ptypes.Eden, newEdenAllocated) + newEdenCoinRemained := k.UpdateCommunityPool(ctx, sdk.DecCoins{edenCoin}) + + // Get remained Eden amount + newEdenAllocated = newEdenCoinRemained.AmountOf(ptypes.Eden) + + // --------------------DEX rewards calculation -------------------- + // ---------------------------------------------------------------- + // Calculate dex rewards + dexRewards := stakeShare.Mul(dexRevenueAmtForStakers).TruncateInt() + + return newEdenAllocated.TruncateInt(), dexRewards +} + // Calculate new Eden-Boost token amounts based on the given conditions and user's current uncommitted token balance func (k Keeper) CalculateEdenBoostRewards(ctx sdk.Context, delegatedAmt sdk.Int, commitments ctypes.Commitments, epochIdentifier string, edenBoostAPR int64) sdk.Int { // Get eden commitments diff --git a/x/incentive/keeper/keeper_stakers_test.go b/x/incentive/keeper/keeper_stakers_test.go index e86f23260..c4a03e5ed 100644 --- a/x/incentive/keeper/keeper_stakers_test.go +++ b/x/incentive/keeper/keeper_stakers_test.go @@ -101,12 +101,28 @@ func TestCalculateRewardsForStakers(t *testing.T) { // Recalculate total committed info ik.UpdateTotalCommitmentInfo(ctx) + totalEdenGiven := sdk.ZeroInt() + totalRewardsGiven := sdk.ZeroInt() + dexRevenueStakersAmt := sdk.NewDec(100000) edenAmountPerEpochStakers := sdk.NewInt(100000) // Calculate delegated amount per delegator delegatedAmt := sdk.NewInt(1000) - // Calculate new uncommitted Eden tokens from Eden & Eden boost committed, Dex rewards distribution - newUncommittedEdenTokens, dexRewards, _ := ik.CalculateRewardsForStakers(ctx, delegatedAmt, commitment, edenAmountPerEpochStakers, dexRevenueStakersAmt) - require.Equal(t, newUncommittedEdenTokens, sdk.NewInt(292)) - require.Equal(t, dexRewards, sdk.NewInt(298)) + // Calculate new uncommitted Eden tokens from Elys staked Eden & Eden boost committed, Dex rewards distribution + newUncommittedEdenTokens, dexRewards, _ := ik.CalculateRewardsForStakersByElysStaked(ctx, delegatedAmt, edenAmountPerEpochStakers, dexRevenueStakersAmt) + totalEdenGiven = totalEdenGiven.Add(newUncommittedEdenTokens) + totalRewardsGiven = totalRewardsGiven.Add(dexRewards) + + // Calculate new uncommitted Eden tokens from Eden committed, Dex rewards distribution + newUncommittedEdenTokens, dexRewards = ik.CalculateRewardsForStakersByCommitted(ctx, delegatedAmt, edenAmountPerEpochStakers, dexRevenueStakersAmt) + totalEdenGiven = totalEdenGiven.Add(newUncommittedEdenTokens) + totalRewardsGiven = totalRewardsGiven.Add(dexRewards) + + // Calculate new uncommitted Eden tokens from Eden boost committed, Dex rewards distribution + newUncommittedEdenTokens, dexRewards = ik.CalculateRewardsForStakersByCommitted(ctx, delegatedAmt, edenAmountPerEpochStakers, dexRevenueStakersAmt) + totalEdenGiven = totalEdenGiven.Add(newUncommittedEdenTokens) + totalRewardsGiven = totalRewardsGiven.Add(dexRewards) + + require.Equal(t, totalEdenGiven, sdk.NewInt(291)) + require.Equal(t, totalRewardsGiven, sdk.NewInt(297)) } diff --git a/x/incentive/module.go b/x/incentive/module.go index a38da13e8..d8103b813 100644 --- a/x/incentive/module.go +++ b/x/incentive/module.go @@ -143,6 +143,8 @@ func (AppModule) ConsensusVersion() uint64 { return 3 } func (am AppModule) BeginBlock(_ sdk.Context, _ abci.RequestBeginBlock) {} // EndBlock contains the logic that is automatically triggered at the end of each block -func (am AppModule) EndBlock(_ sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { +func (am AppModule) EndBlock(ctx sdk.Context, _ abci.RequestEndBlock) []abci.ValidatorUpdate { + am.keeper.EndBlocker(ctx) + return []abci.ValidatorUpdate{} } diff --git a/x/incentive/types/elys_staked.pb.go b/x/incentive/types/elys_staked.pb.go new file mode 100644 index 000000000..cbaa038dd --- /dev/null +++ b/x/incentive/types/elys_staked.pb.go @@ -0,0 +1,396 @@ +// Code generated by protoc-gen-gogo. DO NOT EDIT. +// source: elys/incentive/elys_staked.proto + +package types + +import ( + fmt "fmt" + github_com_cosmos_cosmos_sdk_types "github.com/cosmos/cosmos-sdk/types" + _ "github.com/cosmos/gogoproto/gogoproto" + proto "github.com/cosmos/gogoproto/proto" + io "io" + math "math" + math_bits "math/bits" +) + +// Reference imports to suppress errors if they are not otherwise used. +var _ = proto.Marshal +var _ = fmt.Errorf +var _ = math.Inf + +// This is a compile-time assertion to ensure that this generated file +// is compatible with the proto package it is being compiled against. +// A compilation error at this line likely means your copy of the +// proto package needs to be updated. +const _ = proto.GoGoProtoPackageIsVersion3 // please upgrade the proto package + +// Elys staked +type ElysStaked struct { + Address string `protobuf:"bytes,1,opt,name=address,proto3" json:"address,omitempty"` + Amount github_com_cosmos_cosmos_sdk_types.Int `protobuf:"bytes,2,opt,name=amount,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Int" json:"amount"` +} + +func (m *ElysStaked) Reset() { *m = ElysStaked{} } +func (m *ElysStaked) String() string { return proto.CompactTextString(m) } +func (*ElysStaked) ProtoMessage() {} +func (*ElysStaked) Descriptor() ([]byte, []int) { + return fileDescriptor_ebe5e1add8f72d5c, []int{0} +} +func (m *ElysStaked) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *ElysStaked) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_ElysStaked.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *ElysStaked) XXX_Merge(src proto.Message) { + xxx_messageInfo_ElysStaked.Merge(m, src) +} +func (m *ElysStaked) XXX_Size() int { + return m.Size() +} +func (m *ElysStaked) XXX_DiscardUnknown() { + xxx_messageInfo_ElysStaked.DiscardUnknown(m) +} + +var xxx_messageInfo_ElysStaked proto.InternalMessageInfo + +func (m *ElysStaked) GetAddress() string { + if m != nil { + return m.Address + } + return "" +} + +func init() { + proto.RegisterType((*ElysStaked)(nil), "elys.incentive.ElysStaked") +} + +func init() { proto.RegisterFile("elys/incentive/elys_staked.proto", fileDescriptor_ebe5e1add8f72d5c) } + +var fileDescriptor_ebe5e1add8f72d5c = []byte{ + // 226 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x52, 0x48, 0xcd, 0xa9, 0x2c, + 0xd6, 0xcf, 0xcc, 0x4b, 0x4e, 0x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xf2, 0x2f, 0xd8, 0xe4, 0x24, + 0x36, 0xb0, 0x27, 0x8c, 0x01, 0x01, 0x00, 0x00, 0xff, 0xff, 0xb5, 0xe0, 0x1a, 0xce, 0x0e, 0x01, + 0x00, 0x00, +} + +func (this *ElysStaked) Equal(that interface{}) bool { + if that == nil { + return this == nil + } + + that1, ok := that.(*ElysStaked) + if !ok { + that2, ok := that.(ElysStaked) + if ok { + that1 = &that2 + } else { + return false + } + } + if that1 == nil { + return this == nil + } else if this == nil { + return false + } + if this.Address != that1.Address { + return false + } + if !this.Amount.Equal(that1.Amount) { + return false + } + return true +} +func (m *ElysStaked) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *ElysStaked) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *ElysStaked) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + { + size := m.Amount.Size() + i -= size + if _, err := m.Amount.MarshalTo(dAtA[i:]); err != nil { + return 0, err + } + i = encodeVarintElysStaked(dAtA, i, uint64(size)) + } + i-- + dAtA[i] = 0x12 + if len(m.Address) > 0 { + i -= len(m.Address) + copy(dAtA[i:], m.Address) + i = encodeVarintElysStaked(dAtA, i, uint64(len(m.Address))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + +func encodeVarintElysStaked(dAtA []byte, offset int, v uint64) int { + offset -= sovElysStaked(v) + base := offset + for v >= 1<<7 { + dAtA[offset] = uint8(v&0x7f | 0x80) + v >>= 7 + offset++ + } + dAtA[offset] = uint8(v) + return base +} +func (m *ElysStaked) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Address) + if l > 0 { + n += 1 + l + sovElysStaked(uint64(l)) + } + l = m.Amount.Size() + n += 1 + l + sovElysStaked(uint64(l)) + return n +} + +func sovElysStaked(x uint64) (n int) { + return (math_bits.Len64(x|1) + 6) / 7 +} +func sozElysStaked(x uint64) (n int) { + return sovElysStaked(uint64((x << 1) ^ uint64((int64(x) >> 63)))) +} +func (m *ElysStaked) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowElysStaked + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: ElysStaked: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: ElysStaked: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Address", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowElysStaked + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthElysStaked + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthElysStaked + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Address = string(dAtA[iNdEx:postIndex]) + iNdEx = postIndex + case 2: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Amount", wireType) + } + var stringLen uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowElysStaked + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + stringLen |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + intStringLen := int(stringLen) + if intStringLen < 0 { + return ErrInvalidLengthElysStaked + } + postIndex := iNdEx + intStringLen + if postIndex < 0 { + return ErrInvalidLengthElysStaked + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + if err := m.Amount.Unmarshal(dAtA[iNdEx:postIndex]); err != nil { + return err + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipElysStaked(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthElysStaked + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} +func skipElysStaked(dAtA []byte) (n int, err error) { + l := len(dAtA) + iNdEx := 0 + depth := 0 + for iNdEx < l { + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowElysStaked + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= (uint64(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + wireType := int(wire & 0x7) + switch wireType { + case 0: + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowElysStaked + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + iNdEx++ + if dAtA[iNdEx-1] < 0x80 { + break + } + } + case 1: + iNdEx += 8 + case 2: + var length int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return 0, ErrIntOverflowElysStaked + } + if iNdEx >= l { + return 0, io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + length |= (int(b) & 0x7F) << shift + if b < 0x80 { + break + } + } + if length < 0 { + return 0, ErrInvalidLengthElysStaked + } + iNdEx += length + case 3: + depth++ + case 4: + if depth == 0 { + return 0, ErrUnexpectedEndOfGroupElysStaked + } + depth-- + case 5: + iNdEx += 4 + default: + return 0, fmt.Errorf("proto: illegal wireType %d", wireType) + } + if iNdEx < 0 { + return 0, ErrInvalidLengthElysStaked + } + if depth == 0 { + return iNdEx, nil + } + } + return 0, io.ErrUnexpectedEOF +} + +var ( + ErrInvalidLengthElysStaked = fmt.Errorf("proto: negative length found during unmarshaling") + ErrIntOverflowElysStaked = fmt.Errorf("proto: integer overflow") + ErrUnexpectedEndOfGroupElysStaked = fmt.Errorf("proto: unexpected end of group") +) diff --git a/x/incentive/types/expected_keepers.go b/x/incentive/types/expected_keepers.go index cf4ebe9c4..7aa398c80 100644 --- a/x/incentive/types/expected_keepers.go +++ b/x/incentive/types/expected_keepers.go @@ -25,6 +25,8 @@ type CommitmentKeeper interface { ProcessWithdrawValidatorCommission(sdk.Context, string, string, string, sdk.Int) error // Withdraw tokens - only USDC ProcessWithdrawUSDC(ctx sdk.Context, creator string, denom string, amount sdk.Int) error + // Deduct commitments + DeductCommitments(ctx sdk.Context, creator string, denom string, amount sdk.Int) (ctypes.Commitments, error) } // Staking keeper diff --git a/x/incentive/types/genesis.go b/x/incentive/types/genesis.go index 1e2878c81..62f1d161e 100644 --- a/x/incentive/types/genesis.go +++ b/x/incentive/types/genesis.go @@ -26,7 +26,7 @@ func DefaultGenesis() *GenesisState { // failure. func (gs GenesisState) Validate() error { // this line is used by starport scaffolding # genesis/types/validate - if err := gs.Params.ValidateBasic(); err != nil { + if err := gs.Params.Validate(); err != nil { return err } diff --git a/x/incentive/types/keys.go b/x/incentive/types/keys.go index e33d4f316..73b72db6a 100644 --- a/x/incentive/types/keys.go +++ b/x/incentive/types/keys.go @@ -12,6 +12,8 @@ const ( // MemStoreKey defines the in-memory store key MemStoreKey = "mem_incentive" + + ElysStakedKeyPrefix = "ElysStaked/value/" ) func KeyPrefix(p string) []byte { @@ -21,3 +23,16 @@ func KeyPrefix(p string) []byte { var ( FeePoolKey = []byte{0x00} // key for global distribution state ) + +// ElysStakedKey returns the store key to retrieve a ElysStaked from the address fields +func ElysStakedKey( + address string, +) []byte { + var key []byte + + addressBytes := []byte(address) + key = append(key, addressBytes...) + key = append(key, []byte("/")...) + + return key +} diff --git a/x/incentive/types/params.go b/x/incentive/types/params.go index bcf33541b..e190299d2 100644 --- a/x/incentive/types/params.go +++ b/x/incentive/types/params.go @@ -13,12 +13,13 @@ var _ paramtypes.ParamSet = (*Params)(nil) // Parameter keys var ( - ParamStoreKeyCommunityTax = []byte("communitytax") - ParamStoreKeyWithdrawAddrEnabled = []byte("withdrawaddrenabled") - ParamStoreKeyRewardPortionForLps = []byte("rewardportionforlps") - ParamStoreKeyLPIncentives = []byte("lpincentives") - ParamStoreKeyStkIncentives = []byte("stkincentives") - ParamStoreKeyPoolInfos = []byte("poolinfos") + ParamStoreKeyCommunityTax = []byte("communitytax") + ParamStoreKeyWithdrawAddrEnabled = []byte("withdrawaddrenabled") + ParamStoreKeyRewardPortionForLps = []byte("rewardportionforlps") + ParamStoreKeyLPIncentives = []byte("lpincentives") + ParamStoreKeyStkIncentives = []byte("stkincentives") + ParamStoreKeyPoolInfos = []byte("poolinfos") + ParamStoreKeyElysStakeTrackingRate = []byte("elysstaketrackingrate") ) // ParamKeyTable the param key table for launch module @@ -29,12 +30,13 @@ func ParamKeyTable() paramtypes.KeyTable { // NewParams creates a new Params instance func NewParams() Params { return Params{ - LpIncentives: []IncentiveInfo(nil), - StakeIncentives: []IncentiveInfo(nil), - CommunityTax: sdk.NewDecWithPrec(2, 2), // 2% - WithdrawAddrEnabled: true, - RewardPortionForLps: sdk.NewDecWithPrec(65, 2), - PoolInfos: []PoolInfo(nil), + LpIncentives: []IncentiveInfo(nil), + StakeIncentives: []IncentiveInfo(nil), + CommunityTax: sdk.NewDecWithPrec(2, 2), // 2% + WithdrawAddrEnabled: true, + RewardPortionForLps: sdk.NewDecWithPrec(65, 2), + PoolInfos: []PoolInfo(nil), + ElysStakeTrackingRate: 10, } } @@ -52,11 +54,16 @@ func (p *Params) ParamSetPairs() paramtypes.ParamSetPairs { paramtypes.NewParamSetPair(ParamStoreKeyLPIncentives, &p.LpIncentives, validateLPIncentives), paramtypes.NewParamSetPair(ParamStoreKeyStkIncentives, &p.StakeIncentives, validateStakeIncentives), paramtypes.NewParamSetPair(ParamStoreKeyPoolInfos, &p.PoolInfos, validatePoolInfos), + paramtypes.NewParamSetPair(ParamStoreKeyElysStakeTrackingRate, &p.ElysStakeTrackingRate, validateElysStakeTrakcingRate), } } // Validate validates the set of params func (p Params) Validate() error { + if err := p.ValidateBasic(); err != nil { + return err + } + if err := validateCommunityTax(p.CommunityTax); err != nil { return err } @@ -81,6 +88,10 @@ func (p Params) Validate() error { return err } + if err := validateElysStakeTrakcingRate(p.ElysStakeTrackingRate); err != nil { + return err + } + return nil } @@ -228,3 +239,12 @@ func validatePoolInfos(i interface{}) error { return nil } + +func validateElysStakeTrakcingRate(i interface{}) error { + _, ok := i.(int64) + if !ok { + return fmt.Errorf("invalid parameter type: %T", i) + } + + return nil +} diff --git a/x/incentive/types/params.pb.go b/x/incentive/types/params.pb.go index ade36d298..874d06fae 100644 --- a/x/incentive/types/params.pb.go +++ b/x/incentive/types/params.pb.go @@ -34,7 +34,8 @@ type Params struct { RewardPortionForLps github_com_cosmos_cosmos_sdk_types.Dec `protobuf:"bytes,5,opt,name=reward_portion_for_lps,json=rewardPortionForLps,proto3,customtype=github.com/cosmos/cosmos-sdk/types.Dec" json:"reward_portion_for_lps"` // Pool information // poolId, reward wallet, mulitplier - PoolInfos []PoolInfo `protobuf:"bytes,6,rep,name=pool_infos,json=poolInfos,proto3" json:"pool_infos"` + PoolInfos []PoolInfo `protobuf:"bytes,6,rep,name=pool_infos,json=poolInfos,proto3" json:"pool_infos"` + ElysStakeTrackingRate int64 `protobuf:"varint,7,opt,name=elys_stake_tracking_rate,json=elysStakeTrackingRate,proto3" json:"elys_stake_tracking_rate,omitempty"` } func (m *Params) Reset() { *m = Params{} } @@ -97,6 +98,13 @@ func (m *Params) GetPoolInfos() []PoolInfo { return nil } +func (m *Params) GetElysStakeTrackingRate() int64 { + if m != nil { + return m.ElysStakeTrackingRate + } + return 0 +} + func init() { proto.RegisterType((*Params)(nil), "elys.incentive.Params") } @@ -104,32 +112,35 @@ func init() { func init() { proto.RegisterFile("elys/incentive/params.proto", fileDescriptor_3bca0267cb466fec) } var fileDescriptor_3bca0267cb466fec = []byte{ - // 398 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0xc1, 0xef, 0xd2, 0x30, - 0x14, 0xc7, 0x37, 0x41, 0x22, 0x15, 0xd4, 0x0c, 0x35, 0x13, 0xe3, 0x20, 0x1e, 0x0c, 0x17, 0xba, - 0x04, 0x6f, 0x26, 0x1e, 0x24, 0x6a, 0x20, 0x31, 0x86, 0xa0, 0x27, 0x2f, 0x4d, 0x59, 0x0b, 0x2c, - 0x6c, 0x7d, 0x4d, 0x5b, 0x1c, 0xfc, 0x17, 0x1e, 0x3d, 0xfa, 0xe7, 0x70, 0xe4, 0x62, 0x62, 0x3c, - 0x10, 0x03, 0xff, 0x88, 0xd9, 0x06, 0xcb, 0xe4, 0x64, 0x7e, 0xa7, 0xbe, 0xd7, 0xef, 0xeb, 0xe7, - 0x7d, 0xf3, 0xfa, 0xd0, 0x53, 0x1e, 0x6d, 0xb5, 0x1f, 0x8a, 0x80, 0x0b, 0x13, 0x7e, 0xe5, 0xbe, - 0xa4, 0x8a, 0xc6, 0x1a, 0x4b, 0x05, 0x06, 0x9c, 0x7b, 0xa9, 0x88, 0x0b, 0xb1, 0xfd, 0x70, 0x01, - 0x0b, 0xc8, 0x24, 0x3f, 0x8d, 0xf2, 0xaa, 0xb6, 0x77, 0x85, 0x28, 0xa2, 0xb3, 0xfe, 0xe4, 0xba, - 0x05, 0x40, 0x94, 0x4b, 0xcf, 0x7f, 0x56, 0x50, 0x6d, 0x92, 0x75, 0x74, 0x46, 0xa8, 0x19, 0x49, - 0x52, 0x54, 0x69, 0xd7, 0xee, 0x56, 0x7a, 0x77, 0x07, 0xcf, 0xf0, 0xbf, 0x1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, 0x35, 0x1c, 0xed, 0x8e, 0x9e, 0xbd, 0x3f, 0x7a, 0xf6, 0x9f, 0xa3, 0x67, 0x7f, 0x3b, - 0x79, 0xd6, 0xfe, 0xe4, 0x59, 0xbf, 0x4e, 0x9e, 0xf5, 0x05, 0x97, 0xbc, 0xa5, 0xd0, 0xbe, 0xe0, - 0x26, 0x01, 0xb5, 0xca, 0x12, 0x7f, 0x53, 0x5a, 0x93, 0xcc, 0xe7, 0xac, 0x96, 0x2d, 0xca, 0xcb, - 0xbf, 0x01, 0x00, 0x00, 0xff, 0xff, 0x0b, 0x38, 0x21, 0x0a, 0xa8, 0x02, 0x00, 0x00, + // 435 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x9c, 0x92, 0xc1, 0x6e, 0xd3, 0x30, + 0x18, 0xc7, 0x13, 0x5a, 0x0a, 0x33, 0x1b, 0xa0, 0x8c, 0x21, 0x33, 0x44, 0x5a, 0x71, 0x40, 0xbd, + 0xcc, 0x91, 0xc6, 0x01, 0x09, 0x89, 0x03, 0x15, 0xa0, 0x4d, 0x42, 0xa8, 0xca, 0x76, 0xe2, 0x62, + 0xb9, 0xb1, 0xdb, 0x59, 0x4d, 0xfc, 0x59, 0xb6, 0x47, 0xdb, 0x97, 0x40, 0x1c, 0x39, 0xf2, 0x38, + 0x3b, 0xee, 0x88, 0x38, 0x4c, 0xa8, 0x7d, 0x11, 0xe4, 0xa4, 0x8d, 0x42, 0x4f, 0x88, 0x53, 0x3e, + 0xe7, 0xf7, 0xf7, 0xcf, 0xf6, 0x67, 0xa3, 0xa7, 0x22, 0x5f, 0xd8, 0x44, 0xaa, 0x4c, 0x28, 0x27, + 0xbf, 0x88, 0x44, 0x33, 0xc3, 0x0a, 0x4b, 0xb4, 0x01, 0x07, 0xd1, 0x7d, 0x0f, 0x49, 0x0d, 0x0f, + 0x1f, 0x4d, 0x60, 0x02, 0x25, 0x4a, 0x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 0xda, 0xd1, 0x7e, 0x65, + 0x1b, 0x56, 0xb2, 0x0f, 0x60, 0x3e, 0x6a, 0x1b, 0xbd, 0x41, 0xc8, 0x5f, 0x10, 0x95, 0x6a, 0x0c, + 0x16, 0x77, 0xca, 0xbe, 0xe1, 0xed, 0xbe, 0x0d, 0x01, 0xf2, 0x46, 0xcb, 0x76, 0xf4, 0x7a, 0x6c, + 0xa3, 0x57, 0x08, 0xfb, 0x2c, 0xad, 0x6e, 0xc0, 0x19, 0x96, 0x4d, 0xa5, 0x9a, 0x50, 0xc3, 0x9c, + 0xc0, 0x77, 0x7a, 0x61, 0xbf, 0x95, 0x1e, 0x78, 0x7e, 0xe6, 0xf1, 0xf9, 0x9a, 0xa6, 0xcc, 0x89, + 0xd7, 0xed, 0xef, 0x3f, 0xba, 0xc1, 0xe0, 0xe4, 0x6a, 0x19, 0x87, 0xd7, 0xcb, 0x38, 0xfc, 0xbd, + 0x8c, 0xc3, 0x6f, 0xab, 0x38, 0xb8, 0x5e, 0xc5, 0xc1, 0xcf, 0x55, 0x1c, 0x7c, 0x26, 0x8d, 0x43, + 0x79, 0xc3, 0x91, 0x12, 0x6e, 0x06, 0x66, 0x5a, 0x0e, 0x92, 0x79, 0xe3, 0x7d, 0x95, 0x07, 0x1c, + 0x75, 0xca, 0x17, 0xf6, 0xf2, 0x4f, 0x00, 0x00, 0x00, 0xff, 0xff, 0xcb, 0xb6, 0x05, 0x1f, 0xe1, + 0x02, 0x00, 0x00, } func (m *Params) Marshal() (dAtA []byte, err error) { @@ -152,6 +163,11 @@ func (m *Params) MarshalToSizedBuffer(dAtA []byte) (int, error) { _ = i var l int _ = l + if m.ElysStakeTrackingRate != 0 { + i = encodeVarintParams(dAtA, i, uint64(m.ElysStakeTrackingRate)) + i-- + dAtA[i] = 0x38 + } if len(m.PoolInfos) > 0 { for iNdEx := len(m.PoolInfos) - 1; iNdEx >= 0; iNdEx-- { { @@ -269,6 +285,9 @@ func (m *Params) Size() (n int) { n += 1 + l + sovParams(uint64(l)) } } + if m.ElysStakeTrackingRate != 0 { + n += 1 + sovParams(uint64(m.ElysStakeTrackingRate)) + } return n } @@ -497,6 +516,25 @@ func (m *Params) Unmarshal(dAtA []byte) error { return err } iNdEx = postIndex + case 7: + if wireType != 0 { + return fmt.Errorf("proto: wrong wireType = %d for field ElysStakeTrackingRate", wireType) + } + m.ElysStakeTrackingRate = 0 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowParams + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + m.ElysStakeTrackingRate |= int64(b&0x7F) << shift + if b < 0x80 { + break + } + } default: iNdEx = preIndex skippy, err := skipParams(dAtA[iNdEx:])