From 0ac2b4ca0c97998644d2f4e2581b7506b5a03a9a Mon Sep 17 00:00:00 2001 From: keruch Date: Thu, 12 Sep 2024 18:49:50 +0200 Subject: [PATCH] tests --- x/incentives/keeper/gauge.go | 38 ++++++++++------------------ x/incentives/keeper/gauge_test.go | 4 --- x/incentives/keeper/genesis_test.go | 4 +-- x/incentives/keeper/msg_server.go | 23 ++++++++++++++++- x/incentives/keeper/suite_test.go | 10 ++++---- x/incentives/types/constants.go | 15 ++++------- x/streamer/keeper/distribute.go | 2 +- x/streamer/types/expected_keepers.go | 2 +- 8 files changed, 49 insertions(+), 49 deletions(-) diff --git a/x/incentives/keeper/gauge.go b/x/incentives/keeper/gauge.go index 144de30d0..eb9b61e95 100644 --- a/x/incentives/keeper/gauge.go +++ b/x/incentives/keeper/gauge.go @@ -110,14 +110,6 @@ func (k Keeper) CreateGauge(ctx sdk.Context, isPerpetual bool, owner sdk.AccAddr return 0, fmt.Errorf("denom does not exist: %s", distrTo.Denom) } - // Charge fess based on the number of coins to add - // Fee = CreateGaugeBaseFee + AddDenomFee * NumDenoms - params := k.GetParams(ctx) - fee := params.CreateGaugeBaseFee.Add(params.AddDenomFee.MulRaw(int64(len(coins)))) - if err := k.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, fee, coins); err != nil { - return 0, err - } - gauge := types.Gauge{ Id: k.GetLastGaugeID(ctx) + 1, IsPerpetual: isPerpetual, @@ -149,29 +141,17 @@ func (k Keeper) CreateGauge(ctx sdk.Context, isPerpetual bool, owner sdk.AccAddr } // AddToGaugeRewards adds coins to gauge. -func (k Keeper) AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins sdk.Coins, gaugeID uint64) error { - gauge, err := k.GetGaugeByID(ctx, gaugeID) - if err != nil { - return err - } +func (k Keeper) AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins sdk.Coins, gauge *types.Gauge) error { if gauge.IsFinishedGauge(ctx.BlockTime()) { - return types.UnexpectedFinishedGaugeError{GaugeId: gaugeID} + return types.UnexpectedFinishedGaugeError{GaugeId: gauge.Id} } - // Charge fess based on the number of coins to add - // Fee = AddToGaugeBaseFee + AddDenomFee * (NumAddedDenoms + NumGaugeDenoms) - params := k.GetParams(ctx) - fee := params.AddToGaugeBaseFee.Add(params.AddDenomFee.MulRaw(int64(len(coins) + len(gauge.Coins)))) - if err = k.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, fee, coins); err != nil { - return err - } - - if err = k.bk.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, coins); err != nil { + if err := k.bk.SendCoinsFromAccountToModule(ctx, owner, types.ModuleName, coins); err != nil { return err } gauge.Coins = gauge.Coins.Add(coins...) - err = k.setGauge(ctx, gauge) + err := k.setGauge(ctx, gauge) if err != nil { return err } @@ -179,6 +159,16 @@ func (k Keeper) AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins s return nil } +// AddToGaugeRewardsByID adds coins to gauge. +// TODO: Used only in x/streamer. Delete after https://github.com/dymensionxyz/dymension/pull/1173 is merged! +func (k Keeper) AddToGaugeRewardsByID(ctx sdk.Context, owner sdk.AccAddress, coins sdk.Coins, gaugeID uint64) error { + gauge, err := k.GetGaugeByID(ctx, gaugeID) + if err != nil { + return err + } + return k.AddToGaugeRewards(ctx, owner, coins, gauge) +} + // GetGaugeByID returns gauge from gauge ID. func (k Keeper) GetGaugeByID(ctx sdk.Context, gaugeID uint64) (*types.Gauge, error) { gauge := types.Gauge{} diff --git a/x/incentives/keeper/gauge_test.go b/x/incentives/keeper/gauge_test.go index fad58f47d..4a13041af 100644 --- a/x/incentives/keeper/gauge_test.go +++ b/x/incentives/keeper/gauge_test.go @@ -23,8 +23,6 @@ func (suite *KeeperTestSuite) TestInvalidDurationGaugeCreationValidation() { Denom: defaultLPDenom, Duration: defaultLockDuration / 2, // 0.5 second, invalid duration } - // add tokens for fees - suite.FundAcc(addrs[0], sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, types.DefaultCreateGaugeFee.MulRaw(2)))) _, err := suite.App.IncentivesKeeper.CreateGauge(suite.Ctx, false, addrs[0], defaultLiquidTokens, distrTo, time.Time{}, 1) suite.Require().Error(err) @@ -44,8 +42,6 @@ func (suite *KeeperTestSuite) TestNonExistentDenomGaugeCreation() { Denom: defaultLPDenom, Duration: defaultLockDuration, } - // add tokens for fees - suite.FundAcc(addrs[0], sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, types.DefaultCreateGaugeFee.MulRaw(2)))) _, err := suite.App.IncentivesKeeper.CreateGauge(suite.Ctx, false, addrNoSupply, defaultLiquidTokens, distrTo, time.Time{}, 1) suite.Require().Error(err) diff --git a/x/incentives/keeper/genesis_test.go b/x/incentives/keeper/genesis_test.go index 06626193e..7bd05cab6 100644 --- a/x/incentives/keeper/genesis_test.go +++ b/x/incentives/keeper/genesis_test.go @@ -28,9 +28,7 @@ func TestIncentivesExportGenesis(t *testing.T) { // create an address and fund with coins addr := sdk.AccAddress([]byte("addr1---------------")) coins := sdk.Coins{sdk.NewInt64Coin("stake", 10000)} - // balance including fees - addrCoins := coins.Add(sdk.NewCoin("stake", types.DYM.MulRaw(1000))) - err := bankutil.FundAccount(app.BankKeeper, ctx, addr, addrCoins) + err := bankutil.FundAccount(app.BankKeeper, ctx, addr, coins) require.NoError(t, err) // mints LP tokens and send to address created earlier diff --git a/x/incentives/keeper/msg_server.go b/x/incentives/keeper/msg_server.go index 4373156b3..f603425d9 100644 --- a/x/incentives/keeper/msg_server.go +++ b/x/incentives/keeper/msg_server.go @@ -34,6 +34,14 @@ func (server msgServer) CreateGauge(goCtx context.Context, msg *types.MsgCreateG return nil, err } + // Charge fess based on the number of coins to add + // Fee = CreateGaugeBaseFee + AddDenomFee * NumDenoms + params := server.keeper.GetParams(ctx) + fee := params.CreateGaugeBaseFee.Add(params.AddDenomFee.MulRaw(int64(len(msg.Coins)))) + if err = server.keeper.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, fee, msg.Coins); err != nil { + return nil, err + } + gaugeID, err := server.keeper.CreateGauge(ctx, msg.IsPerpetual, owner, msg.Coins, msg.DistributeTo, msg.StartTime, msg.NumEpochsPaidOver) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error()) @@ -58,7 +66,20 @@ func (server msgServer) AddToGauge(goCtx context.Context, msg *types.MsgAddToGau return nil, err } - err = server.keeper.AddToGaugeRewards(ctx, owner, msg.Rewards, msg.GaugeId) + gauge, err := server.keeper.GetGaugeByID(ctx, msg.GaugeId) + if err != nil { + return nil, err + } + + // Charge fess based on the number of coins to add + // Fee = AddToGaugeBaseFee + AddDenomFee * (NumAddedDenoms + NumGaugeDenoms) + params := server.keeper.GetParams(ctx) + fee := params.AddToGaugeBaseFee.Add(params.AddDenomFee.MulRaw(int64(len(msg.Rewards) + len(gauge.Coins)))) + if err = server.keeper.chargeFeeIfSufficientFeeDenomBalance(ctx, owner, fee, msg.Rewards); err != nil { + return nil, err + } + + err = server.keeper.AddToGaugeRewards(ctx, owner, msg.Rewards, gauge) if err != nil { return nil, sdkerrors.Wrap(sdkerrors.ErrInvalidRequest, err.Error()) } diff --git a/x/incentives/keeper/suite_test.go b/x/incentives/keeper/suite_test.go index b32aec893..eccb86390 100644 --- a/x/incentives/keeper/suite_test.go +++ b/x/incentives/keeper/suite_test.go @@ -92,8 +92,7 @@ func (suite *KeeperTestSuite) SetupGauges(gaugeDescriptors []perpGaugeDesc, deno // CreateGauge creates a gauge struct given the required params. func (suite *KeeperTestSuite) CreateGauge(isPerpetual bool, addr sdk.AccAddress, coins sdk.Coins, distrTo lockuptypes.QueryCondition, startTime time.Time, numEpoch uint64) (uint64, *types.Gauge) { - addrCoins := coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, types.DYM.MulRaw(10_000))) // create gauge fees - suite.FundAcc(addr, addrCoins) + suite.FundAcc(addr, coins) gaugeID, err := suite.App.IncentivesKeeper.CreateGauge(suite.Ctx, isPerpetual, addr, coins, distrTo, startTime, numEpoch) suite.Require().NoError(err) gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID) @@ -103,10 +102,11 @@ func (suite *KeeperTestSuite) CreateGauge(isPerpetual bool, addr sdk.AccAddress, // AddToGauge adds coins to the specified gauge. func (suite *KeeperTestSuite) AddToGauge(coins sdk.Coins, gaugeID uint64) uint64 { + gauge, err := suite.App.IncentivesKeeper.GetGaugeByID(suite.Ctx, gaugeID) + suite.Require().NoError(err) addr := sdk.AccAddress([]byte("addrx---------------")) - addrCoins := coins.Add(sdk.NewCoin(sdk.DefaultBondDenom, types.DYM.MulRaw(10_000))) // add to gauge fees - suite.FundAcc(addr, addrCoins) - err := suite.App.IncentivesKeeper.AddToGaugeRewards(suite.Ctx, addr, coins, gaugeID) + suite.FundAcc(addr, coins) + err = suite.App.IncentivesKeeper.AddToGaugeRewards(suite.Ctx, addr, coins, gauge) suite.Require().NoError(err) return gaugeID } diff --git a/x/incentives/types/constants.go b/x/incentives/types/constants.go index 87bad0eb1..35a5f974c 100644 --- a/x/incentives/types/constants.go +++ b/x/incentives/types/constants.go @@ -1,19 +1,14 @@ package types -import ( - "math/big" - - "cosmossdk.io/math" - sdk "github.com/cosmos/cosmos-sdk/types" -) +import "cosmossdk.io/math" var ( // DYM represents 1 DYM - DYM = math.NewIntFromBigInt(new(big.Int).Exp(big.NewInt(10), big.NewInt(18), nil)) + DYM = math.NewIntWithDecimal(1, 18) - DefaultCreateGaugeFee = DYM.Mul(sdk.NewInt(10)) - DefaultAddToGaugeFee = math.ZeroInt() - DefaultAddDenomFee = DYM + DefaultCreateGaugeFee = DYM.MulRaw(10) // 10 DYM + DefaultAddToGaugeFee = math.ZeroInt() // 0 DYM + DefaultAddDenomFee = DYM // 1 DYM ) const DefaultDistrEpochIdentifier = "week" diff --git a/x/streamer/keeper/distribute.go b/x/streamer/keeper/distribute.go index 4adb1352c..bf17a3557 100644 --- a/x/streamer/keeper/distribute.go +++ b/x/streamer/keeper/distribute.go @@ -43,7 +43,7 @@ func (k Keeper) DistributeToGauge(ctx sdk.Context, coins sdk.Coins, record types } allocatedCoin := sdk.Coin{Denom: coin.Denom, Amount: allocatingAmount} - err = k.ik.AddToGaugeRewards(ctx, k.ak.GetModuleAddress(types.ModuleName), sdk.NewCoins(allocatedCoin), record.GaugeId) + err = k.ik.AddToGaugeRewardsByID(ctx, k.ak.GetModuleAddress(types.ModuleName), sdk.NewCoins(allocatedCoin), record.GaugeId) if err != nil { return sdk.Coins{}, fmt.Errorf("add rewards to gauge %d: %w", record.GaugeId, err) } diff --git a/x/streamer/types/expected_keepers.go b/x/streamer/types/expected_keepers.go index 51572aead..39ac4c604 100644 --- a/x/streamer/types/expected_keepers.go +++ b/x/streamer/types/expected_keepers.go @@ -37,7 +37,7 @@ type IncentivesKeeper interface { GetLockableDurations(ctx sdk.Context) []time.Duration GetGaugeByID(ctx sdk.Context, gaugeID uint64) (*incentivestypes.Gauge, error) - AddToGaugeRewards(ctx sdk.Context, owner sdk.AccAddress, coins sdk.Coins, gaugeID uint64) error + AddToGaugeRewardsByID(ctx sdk.Context, owner sdk.AccAddress, coins sdk.Coins, gaugeID uint64) error } type SponsorshipKeeper interface {