Skip to content

Commit

Permalink
test(evm): Add tests for feemarket wrapper (#66)
Browse files Browse the repository at this point in the history
  • Loading branch information
0xstepit authored Jan 6, 2025
1 parent a5e96b2 commit cd67b16
Show file tree
Hide file tree
Showing 3 changed files with 273 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,4 @@ This changelog was created using the `clu` binary
- (eip-712) [#13](https://github.com/evmos/os/pull/13) Add EIP-712 package.
- (ci) [#12](https://github.com/evmos/os/pull/12) Add CI workflows, configurations, Makefile, License, etc.
- (tests) [#65](https://github.com/evmos/os/pull/65) Add tests for bank wrapper in EVM module with mock.
- (tests) [#66](https://github.com/evmos/os/pull/66) Add tests for feemarket wrapper in EVM module with mock.
4 changes: 3 additions & 1 deletion x/evm/wrappers/feemarket.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,9 @@ func (w FeeMarketWrapper) CalculateBaseFee(ctx sdk.Context) *big.Int {
// GetParams returns the params with associated fees values converted to 18 decimals.
func (w FeeMarketWrapper) GetParams(ctx sdk.Context) feemarkettypes.Params {
params := w.FeeMarketKeeper.GetParams(ctx)
params.BaseFee = types.ConvertAmountTo18DecimalsLegacy(params.BaseFee)
if !params.BaseFee.IsNil() {
params.BaseFee = types.ConvertAmountTo18DecimalsLegacy(params.BaseFee)
}
params.MinGasPrice = types.ConvertAmountTo18DecimalsLegacy(params.MinGasPrice)
return params
}
269 changes: 269 additions & 0 deletions x/evm/wrappers/feemarket_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,269 @@
package wrappers_test

import (
"math/big"
"testing"

sdkmath "cosmossdk.io/math"
sdk "github.com/cosmos/cosmos-sdk/types"
evmtypes "github.com/evmos/os/x/evm/types"
"github.com/evmos/os/x/evm/wrappers"
"github.com/evmos/os/x/evm/wrappers/testutil"
feemarkettypes "github.com/evmos/os/x/feemarket/types"
"github.com/stretchr/testify/require"
"go.uber.org/mock/gomock"
)

func TestGetBaseFee(t *testing.T) {
testCases := []struct {
name string
evmDecimals uint8
expResult *big.Int
mockSetup func(*testutil.MockFeeMarketKeeper)
}{
{
name: "success - does not convert 18 decimals",
evmDecimals: 18,
expResult: big.NewInt(1e18), // 1 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDec(1e18))
},
},
{
name: "success - convert 6 decimals to 18 decimals",
evmDecimals: 6,
expResult: big.NewInt(1e18), // 1 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDec(1_000_000))
},
},
{
name: "success - nil base fee",
evmDecimals: 6,
expResult: nil,
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetBaseFee(gomock.Any()).
Return(sdkmath.LegacyDec{})
},
},
{
name: "success - small amount 18 decimals",
evmDecimals: 6,
expResult: big.NewInt(1e12), // 0.000001 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDec(1))
},
},
{
name: "success - base fee is zero",
evmDecimals: 6,
expResult: big.NewInt(0),
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDec(0))
},
},
{
name: "success - truncate decimals with number less than 1",
evmDecimals: 6,
expResult: big.NewInt(0), // 0.000001 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDecWithPrec(1, 13)) // multiplied by 1e12 is still less than 1
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup EVM configurator to have access to the EVM coin info.
configurator := evmtypes.NewEVMConfigurator()
configurator.ResetTestConfig()
err := configurator.WithEVMCoinInfo("token", tc.evmDecimals).Configure()
require.NoError(t, err, "failed to configure EVMConfigurator")

ctrl := gomock.NewController(t)
mockFeeMarketKeeper := testutil.NewMockFeeMarketKeeper(ctrl)
tc.mockSetup(mockFeeMarketKeeper)

feeMarketWrapper := wrappers.NewFeeMarketWrapper(mockFeeMarketKeeper)
result := feeMarketWrapper.GetBaseFee(sdk.Context{})

require.Equal(t, tc.expResult, result)
})
}
}

func TestCalculateBaseFee(t *testing.T) {
testCases := []struct {
name string
evmDecimals uint8
baseFee sdkmath.LegacyDec
expResult *big.Int
mockSetup func(*testutil.MockFeeMarketKeeper)
}{
{
name: "success - does not convert 18 decimals",
evmDecimals: 18,
expResult: big.NewInt(1e18), // 1 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
CalculateBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDec(1e18))
},
},
{
name: "success - convert 6 decimals to 18 decimals",
evmDecimals: 6,
expResult: big.NewInt(1e18), // 1 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
CalculateBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDec(1_000_000))
},
},
{
name: "success - nil base fee",
evmDecimals: 6,
expResult: nil,
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
CalculateBaseFee(gomock.Any()).
Return(sdkmath.LegacyDec{})
},
},
{
name: "success - small amount 18 decimals",
evmDecimals: 6,
expResult: big.NewInt(1e12), // 0.000001 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
CalculateBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDec(1))
},
},
{
name: "success - base fee is zero",
evmDecimals: 6,
expResult: big.NewInt(0),
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
CalculateBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDec(0))
},
},
{
name: "success - truncate decimals with number less than 1",
evmDecimals: 6,
expResult: big.NewInt(0), // 0.000001 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
CalculateBaseFee(gomock.Any()).
Return(sdkmath.LegacyNewDecWithPrec(1, 13)) // multiplied by 1e12 is still less than 1
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup EVM configurator to have access to the EVM coin info.
configurator := evmtypes.NewEVMConfigurator()
configurator.ResetTestConfig()
err := configurator.WithEVMCoinInfo("token", tc.evmDecimals).Configure()
require.NoError(t, err, "failed to configure EVMConfigurator")

ctrl := gomock.NewController(t)
mockFeeMarketKeeper := testutil.NewMockFeeMarketKeeper(ctrl)
tc.mockSetup(mockFeeMarketKeeper)

feeMarketWrapper := wrappers.NewFeeMarketWrapper(mockFeeMarketKeeper)
result := feeMarketWrapper.CalculateBaseFee(sdk.Context{})

require.Equal(t, tc.expResult, result)
})
}
}

func TestGetParams(t *testing.T) {
testCases := []struct {
name string
evmDecimals uint8
expParams feemarkettypes.Params
mockSetup func(*testutil.MockFeeMarketKeeper)
}{
{
name: "success - convert 6 decimals to 18 decimals",
evmDecimals: 6,
expParams: feemarkettypes.Params{
BaseFee: sdkmath.LegacyNewDec(1e18),
MinGasPrice: sdkmath.LegacyNewDec(1e18),
},
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetParams(gomock.Any()).
Return(feemarkettypes.Params{
BaseFee: sdkmath.LegacyNewDec(1_000_000),
MinGasPrice: sdkmath.LegacyNewDec(1_000_000),
})
},
},
{
name: "success - does not convert 18 decimals",
evmDecimals: 18,
expParams: feemarkettypes.Params{
BaseFee: sdkmath.LegacyNewDec(1e18),
MinGasPrice: sdkmath.LegacyNewDec(1e18),
},
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetParams(gomock.Any()).
Return(feemarkettypes.Params{
BaseFee: sdkmath.LegacyNewDec(1e18),
MinGasPrice: sdkmath.LegacyNewDec(1e18),
})
},
},
{
name: "success - nil base fee",
evmDecimals: 18,
expParams: feemarkettypes.Params{
MinGasPrice: sdkmath.LegacyNewDec(1e18),
},
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
mfk.EXPECT().
GetParams(gomock.Any()).
Return(feemarkettypes.Params{
MinGasPrice: sdkmath.LegacyNewDec(1e18),
})
},
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
// Setup EVM configurator to have access to the EVM coin info.
configurator := evmtypes.NewEVMConfigurator()
configurator.ResetTestConfig()
err := configurator.WithEVMCoinInfo("token", tc.evmDecimals).Configure()
require.NoError(t, err, "failed to configure EVMConfigurator")

ctrl := gomock.NewController(t)
mockFeeMarketKeeper := testutil.NewMockFeeMarketKeeper(ctrl)
tc.mockSetup(mockFeeMarketKeeper)

feeMarketWrapper := wrappers.NewFeeMarketWrapper(mockFeeMarketKeeper)
result := feeMarketWrapper.GetParams(sdk.Context{})

require.Equal(t, tc.expParams, result)
})
}
}

0 comments on commit cd67b16

Please sign in to comment.