Skip to content

Commit

Permalink
tests
Browse files Browse the repository at this point in the history
  • Loading branch information
keruch committed Sep 12, 2024
1 parent 380559f commit 0ac2b4c
Show file tree
Hide file tree
Showing 8 changed files with 49 additions and 49 deletions.
38 changes: 14 additions & 24 deletions x/incentives/keeper/gauge.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -149,36 +141,34 @@ 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
}
k.hooks.AfterAddToGauge(ctx, gauge.Id)
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{}
Expand Down
4 changes: 0 additions & 4 deletions x/incentives/keeper/gauge_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand Down
4 changes: 1 addition & 3 deletions x/incentives/keeper/genesis_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
23 changes: 22 additions & 1 deletion x/incentives/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand All @@ -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())
}
Expand Down
10 changes: 5 additions & 5 deletions x/incentives/keeper/suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Expand Down
15 changes: 5 additions & 10 deletions x/incentives/types/constants.go
Original file line number Diff line number Diff line change
@@ -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"
2 changes: 1 addition & 1 deletion x/streamer/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down
2 changes: 1 addition & 1 deletion x/streamer/types/expected_keepers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 0ac2b4c

Please sign in to comment.