Skip to content

Commit

Permalink
feeburn test
Browse files Browse the repository at this point in the history
  • Loading branch information
trung2891 committed Nov 21, 2023
1 parent e4dcff4 commit 8c5d0a1
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 8 deletions.
44 changes: 44 additions & 0 deletions x/feeburn/ante/antetest/fee_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package antetest

import (
"testing"

feeburnante "github.com/White-Whale-Defi-Platform/migaloo-chain/v3/x/feeburn/ante"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/bank/testutil"
"github.com/stretchr/testify/suite"
)

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}

func (s *IntegrationTestSuite) TestFeeBurnAnteHandler() {
// setup test
s.SetupTest()
s.txBuilder = s.clientCtx.TxConfig.NewTxBuilder()

// keys and addresses
priv1, _, addr1 := testdata.KeyTestPubAddr()

// msg and signatures
msg := testdata.NewTestMsg(addr1)
feeAmount := testdata.NewTestFeeAmount()
gasLimit := testdata.NewTestGasLimit()
s.Require().NoError(s.txBuilder.SetMsgs(msg))
s.txBuilder.SetFeeAmount(feeAmount)
s.txBuilder.SetGasLimit(gasLimit)

privs, accNums, accSeqs := []cryptotypes.PrivKey{priv1}, []uint64{0}, []uint64{0}
tx, err := s.CreateTestTx(privs, accNums, accSeqs, s.ctx.ChainID())
s.Require().NoError(err)

mfd := feeburnante.NewDeductFeeDecorator(s.app.AccountKeeper, s.app.BankKeeper, s.app.FeeGrantKeeper, nil, s.app.FeeburnKeeper)
antehandler := sdk.ChainAnteDecorators(mfd)
err = testutil.FundAccount(s.app.BankKeeper, s.ctx, addr1, sdk.NewCoins(sdk.NewCoin("atom", sdk.NewInt(200))))
s.Require().NoError(err)
_, err = antehandler(s.ctx, tx, false)
s.Require().Error(err)
}
9 changes: 1 addition & 8 deletions x/feeburn/ante/antetest/fee_test_setup.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/types/tx/signing"
xauthsigning "github.com/cosmos/cosmos-sdk/x/auth/signing"
"github.com/cosmos/cosmos-sdk/x/params/types"
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types"
"github.com/stretchr/testify/suite"
tmrand "github.com/tendermint/tendermint/libs/rand"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
Expand Down Expand Up @@ -46,19 +45,13 @@ func (s *IntegrationTestSuite) SetupTest() {
s.clientCtx = client.Context{}.WithTxConfig(encodingConfig.TxConfig)
}

func (s *IntegrationTestSuite) SetupTestBurnFeeStore(minGasPrice []sdk.DecCoin, feeBurnParams *feeburnmoduletypes.Params) types.Subspace {
func (s *IntegrationTestSuite) SetupTestBurnFeeStore(feeBurnParams *feeburnmoduletypes.Params) types.Subspace {
subspace := s.app.GetSubspace(feeburn.ModuleName)
subspace.SetParamSet(s.ctx, feeBurnParams)

return subspace
}

// SetupTestStakingSubspace sets uatom as bond denom for the fee tests.
func (s *IntegrationTestSuite) SetupTestStakingSubspace(params stakingtypes.Params) types.Subspace {
s.app.GetSubspace(stakingtypes.ModuleName).SetParamSet(s.ctx, &params)
return s.app.GetSubspace(stakingtypes.ModuleName)
}

func (s *IntegrationTestSuite) CreateTestTx(privs []cryptotypes.PrivKey, accNums []uint64, accSeqs []uint64, chainID string) (xauthsigning.Tx, error) {
var sigsV2 []signing.SignatureV2
for i, priv := range privs {
Expand Down
2 changes: 2 additions & 0 deletions x/feeburn/ante/fee.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ func NewDeductFeeDecorator(ak ante.AccountKeeper, bk BankKeeper, fk ante.Feegran

func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bool, next sdk.AnteHandler) (sdk.Context, error) {
feeTx, ok := tx.(sdk.FeeTx)

if !ok {
return ctx, errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}
Expand Down Expand Up @@ -72,6 +73,7 @@ func (dfd DeductFeeDecorator) AnteHandle(ctx sdk.Context, tx sdk.Tx, simulate bo

func (dfd DeductFeeDecorator) checkDeductFee(ctx sdk.Context, sdkTx sdk.Tx, fee sdk.Coins) error {
feeTx, ok := sdkTx.(sdk.FeeTx)

if !ok {
return errorsmod.Wrap(sdkerrors.ErrTxDecode, "Tx must be a FeeTx")
}
Expand Down
48 changes: 48 additions & 0 deletions x/feeburn/types/params_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package types

import (
"testing"

"github.com/stretchr/testify/require"
)

func TestDefaultParams(t *testing.T) {
p := DefaultParams()
require.EqualValues(t, p.TxFeeBurnPercent, "0")
}

func TestValidateParams(t *testing.T) {
tests := map[string]struct {
percent interface{}
expectErr bool
}{
"DafaultParams, pass": {
DefaultParams().TxFeeBurnPercent,
false,
},
"lower boundary testing, pass": {
"0",
false,
},
"upper boundary testing, pass": {
"100",
false,
},
"greater 100%, fail": {
"101",
true,
},
}

for name, test := range tests {
t.Run(name, func(t *testing.T) {
err := validateTxFeeBurnPercent(test.percent)
if test.expectErr {
require.Error(t, err)
return
}
require.NoError(t, err)
})
}

}

0 comments on commit 8c5d0a1

Please sign in to comment.