-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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>
- Loading branch information
1 parent
511b0eb
commit dabd7e8
Showing
25 changed files
with
1,067 additions
and
57 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
]; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
}, | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.