Skip to content

Commit

Permalink
fixed UT
Browse files Browse the repository at this point in the history
  • Loading branch information
mtsitrin committed Sep 17, 2024
1 parent ea9acc8 commit 4eb4fb6
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 16 deletions.
3 changes: 2 additions & 1 deletion x/incentives/client/cli/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,10 @@ import (

"github.com/cosmos/cosmos-sdk/types/query"

"github.com/osmosis-labs/osmosis/v15/osmoutils/osmocli"

"github.com/dymensionxyz/dymension/v3/x/incentives/client/cli"
"github.com/dymensionxyz/dymension/v3/x/incentives/types"
"github.com/osmosis-labs/osmosis/v15/osmoutils/osmocli"
)

func TestGetCmdGauges(t *testing.T) {
Expand Down
3 changes: 2 additions & 1 deletion x/incentives/client/cli/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ package cli
import (
"github.com/spf13/cobra"

"github.com/dymensionxyz/dymension/v3/x/incentives/types"
"github.com/osmosis-labs/osmosis/v15/osmoutils/osmocli"

"github.com/dymensionxyz/dymension/v3/x/incentives/types"
)

// GetQueryCmd returns the query commands for this module.
Expand Down
5 changes: 2 additions & 3 deletions x/incentives/keeper/distribute.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,10 @@ import (
"fmt"

errorsmod "cosmossdk.io/errors"

sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/dymensionxyz/dymension/v3/x/incentives/types"

sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/dymensionxyz/dymension/v3/x/incentives/types"
)

// Distribute distributes coins from an array of gauges.
Expand Down
8 changes: 4 additions & 4 deletions x/incentives/keeper/msg_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ func (server msgServer) CreateGauge(goCtx context.Context, msg *types.MsgCreateG
// Fee = CreateGaugeBaseFee + AddDenomFee * NumDenoms
params := server.keeper.GetParams(ctx)
fee := params.CreateGaugeBaseFee.Add(params.AddDenomFee.MulRaw(int64(len(msg.Coins))))
if err = server.keeper.chargeGaugesFee(ctx, owner, fee, msg.Coins); err != nil {
if err = server.keeper.ChargeGaugesFee(ctx, owner, fee, msg.Coins); err != nil {
return nil, err
}

Expand Down Expand Up @@ -77,7 +77,7 @@ func (server msgServer) AddToGauge(goCtx context.Context, msg *types.MsgAddToGau
// 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.chargeGaugesFee(ctx, owner, fee, msg.Rewards); err != nil {
if err = server.keeper.ChargeGaugesFee(ctx, owner, fee, msg.Rewards); err != nil {
return nil, err
}

Expand All @@ -96,11 +96,11 @@ func (server msgServer) AddToGauge(goCtx context.Context, msg *types.MsgAddToGau
return &types.MsgAddToGaugeResponse{}, nil
}

// chargeGaugesFee charges fee in the base denom on the address if the address has
// ChargeGaugesFee charges fee in the base denom on the address if the address has
// balance that is less than fee + amount of the coin from gaugeCoins that is of base denom.
// gaugeCoins might not have a coin of tx base denom. In that case, fee is only compared to balance.
// The fee is sent to the txfees module, to be burned.
func (k Keeper) chargeGaugesFee(ctx sdk.Context, address sdk.AccAddress, fee sdk.Int, gaugeCoins sdk.Coins) (err error) {
func (k Keeper) ChargeGaugesFee(ctx sdk.Context, address sdk.AccAddress, fee sdk.Int, gaugeCoins sdk.Coins) (err error) {
var feeDenom string
if k.tk == nil {
feeDenom, err = sdk.GetBaseDenom()
Expand Down
114 changes: 107 additions & 7 deletions x/incentives/keeper/msg_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,12 @@ import (
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
"github.com/stretchr/testify/suite"

"github.com/osmosis-labs/osmosis/v15/x/txfees"

"github.com/dymensionxyz/dymension/v3/app/apptesting"
"github.com/dymensionxyz/dymension/v3/x/incentives/keeper"
"github.com/dymensionxyz/dymension/v3/x/incentives/types"
lockuptypes "github.com/dymensionxyz/dymension/v3/x/lockup/types"
"github.com/osmosis-labs/osmosis/v15/x/txfees"
)

var _ = suite.TestingSuite(nil)
Expand Down Expand Up @@ -165,7 +167,7 @@ func (suite *KeeperTestSuite) TestCreateGauge() {
StartTime: time.Now(),
NumEpochsPaidOver: 1,
}
txfeesBalanceBefore := bankKeeper.GetBalance(ctx, accountKeeper.GetModuleAddress(txfees.ModuleName), "adym")
txfeesBalanceBefore := bankKeeper.GetBalance(ctx, accountKeeper.GetModuleAddress(txfees.ModuleName), "stake")

// System under test.
_, err := msgServer.CreateGauge(sdk.WrapSDKContext(ctx), msg)
Expand All @@ -191,7 +193,7 @@ func (suite *KeeperTestSuite) TestCreateGauge() {
suite.Require().Equal(finalAccountBalance.String(), balanceAmount.String(), "test: %v", tc.name)

// test fee charged to txfees module account
txfeesBalanceAfter := bankKeeper.GetBalance(ctx, accountKeeper.GetModuleAddress(txfees.ModuleName), "adym")
txfeesBalanceAfter := bankKeeper.GetBalance(ctx, accountKeeper.GetModuleAddress(txfees.ModuleName), "stake")
suite.Require().Equal(txfeesBalanceBefore.Amount.Add(feeRaw), txfeesBalanceAfter.Amount, "test: %v", tc.name)
}
})
Expand All @@ -204,7 +206,6 @@ func (suite *KeeperTestSuite) TestAddToGauge() {
accountBalanceToFund sdk.Coins
gaugeAddition sdk.Coins
isPerpetual bool
nonexistentGauge bool
isModuleAccount bool
expectErr bool
}{
Expand Down Expand Up @@ -334,9 +335,6 @@ func (suite *KeeperTestSuite) TestAddToGauge() {
// System under test.
coins := sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(500000000)))
gaugeID, gauge, _, _ := suite.SetupNewGauge(true, coins)
if tc.nonexistentGauge {
gaugeID = incentivesKeeper.GetLastGaugeID(ctx) + 1
}
msg := &types.MsgAddToGauge{
Owner: testAccountAddress.String(),
GaugeId: gaugeID,
Expand All @@ -347,6 +345,8 @@ func (suite *KeeperTestSuite) TestAddToGauge() {
feeRaw := params.AddToGaugeBaseFee.Add(params.AddDenomFee.MulRaw(int64(len(tc.gaugeAddition) + len(gauge.Coins))))
suite.T().Log(feeRaw, params.AddToGaugeBaseFee, params.AddDenomFee)

txfeesBalanceBefore := bankKeeper.GetBalance(ctx, accountKeeper.GetModuleAddress(txfees.ModuleName), "stake")

_, err = msgServer.AddToGauge(sdk.WrapSDKContext(ctx), msg)

if tc.expectErr {
Expand All @@ -368,6 +368,106 @@ func (suite *KeeperTestSuite) TestAddToGauge() {
accountBalance := tc.accountBalanceToFund.Sub(tc.gaugeAddition...)
finalAccountBalance := accountBalance.Sub(fee...)
suite.Require().Equal(finalAccountBalance.String(), bal.String(), "test: %v", tc.name)

// test fee charged to txfees module account
txfeesBalanceAfter := bankKeeper.GetBalance(ctx, accountKeeper.GetModuleAddress(txfees.ModuleName), "stake")
suite.Require().Equal(txfeesBalanceBefore.Amount.Add(feeRaw), txfeesBalanceAfter.Amount, "test: %v", tc.name)
}
})
}
}

func (suite *KeeperTestSuite) TestChargeFeeIfSufficientFeeDenomBalance() {
const baseFee = int64(100)

testcases := map[string]struct {
accountBalanceToFund sdk.Coin
feeToCharge int64
gaugeCoins sdk.Coins

expectError bool
}{
"fee + base denom gauge coin == acount balance, success": {
accountBalanceToFund: sdk.NewCoin("adym", sdk.NewInt(baseFee)),
feeToCharge: baseFee / 2,
gaugeCoins: sdk.NewCoins(sdk.NewCoin("adym", sdk.NewInt(baseFee/2))),
},
"fee + base denom gauge coin < acount balance, success": {
accountBalanceToFund: sdk.NewCoin("adym", sdk.NewInt(baseFee)),
feeToCharge: baseFee/2 - 1,
gaugeCoins: sdk.NewCoins(sdk.NewCoin("adym", sdk.NewInt(baseFee/2))),
},
"fee + base denom gauge coin > acount balance, error": {
accountBalanceToFund: sdk.NewCoin("adym", sdk.NewInt(baseFee)),
feeToCharge: baseFee/2 + 1,
gaugeCoins: sdk.NewCoins(sdk.NewCoin("adym", sdk.NewInt(baseFee/2))),
expectError: true,
},
"fee + base denom gauge coin < acount balance, custom values, success": {
accountBalanceToFund: sdk.NewCoin("adym", sdk.NewInt(11793193112)),
feeToCharge: 55,
gaugeCoins: sdk.NewCoins(sdk.NewCoin("adym", sdk.NewInt(328812))),
},
"account funded with coins other than base denom, error": {
accountBalanceToFund: sdk.NewCoin("usdc", sdk.NewInt(baseFee)),
feeToCharge: baseFee,
gaugeCoins: sdk.NewCoins(sdk.NewCoin("adym", sdk.NewInt(baseFee/2))),
expectError: true,
},
"fee == account balance, no gauge coins, success": {
accountBalanceToFund: sdk.NewCoin("adym", sdk.NewInt(baseFee)),
feeToCharge: baseFee,
},
"gauge coins == account balance, no fee, success": {
accountBalanceToFund: sdk.NewCoin("adym", sdk.NewInt(baseFee)),
gaugeCoins: sdk.NewCoins(sdk.NewCoin("adym", sdk.NewInt(baseFee))),
},
"fee == account balance, gauge coins in denom other than base, success": {
accountBalanceToFund: sdk.NewCoin("adym", sdk.NewInt(baseFee)),
feeToCharge: baseFee,
gaugeCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(baseFee*2))),
},
"fee + gauge coins == account balance, multiple gauge coins, one in denom other than base, success": {
accountBalanceToFund: sdk.NewCoin("adym", sdk.NewInt(baseFee)),
feeToCharge: baseFee / 2,
gaugeCoins: sdk.NewCoins(sdk.NewCoin("usdc", sdk.NewInt(baseFee*2)), sdk.NewCoin("adym", sdk.NewInt(baseFee/2))),
},
}

for name, tc := range testcases {
suite.Run(name, func() {
suite.SetupTest()

err := suite.App.TxFeesKeeper.SetBaseDenom(suite.Ctx, "adym")
suite.Require().NoError(err)

testAccount := apptesting.CreateRandomAccounts(1)[0]
ctx := suite.Ctx
incentivesKeepers := suite.App.IncentivesKeeper
bankKeeper := suite.App.BankKeeper

// Pre-fund account.
// suite.FundAcc(testAccount, testutil.DefaultAcctFunds)
suite.FundAcc(testAccount, sdk.NewCoins(tc.accountBalanceToFund))

oldBalanceAmount := bankKeeper.GetBalance(ctx, testAccount, "adym").Amount

// System under test.
err = incentivesKeepers.ChargeGaugesFee(ctx, testAccount, sdk.NewInt(tc.feeToCharge), tc.gaugeCoins)

// Assertions.
newBalanceAmount := bankKeeper.GetBalance(ctx, testAccount, "adym").Amount
if tc.expectError {
suite.Require().Error(err)

// check account balance unchanged
suite.Require().Equal(oldBalanceAmount, newBalanceAmount)
} else {
suite.Require().NoError(err)

// check account balance changed.
expectedNewBalanceAmount := oldBalanceAmount.Sub(sdk.NewInt(tc.feeToCharge))
suite.Require().Equal(expectedNewBalanceAmount.String(), newBalanceAmount.String())
}
})
}
Expand Down

0 comments on commit 4eb4fb6

Please sign in to comment.