Skip to content

Commit

Permalink
add tests for calculate base fee
Browse files Browse the repository at this point in the history
  • Loading branch information
0xstepit committed Dec 23, 2024
1 parent c080628 commit 016175e
Showing 1 changed file with 91 additions and 8 deletions.
99 changes: 91 additions & 8 deletions x/evm/wrappers/feemarket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,12 @@ import (
func TestGetBaseFee(t *testing.T) {
testCases := []struct {
name string
evmDenom string
evmDecimals uint8
expResult *big.Int
mockSetup func(*testutil.MockFeeMarketKeeper)
}{
{
name: "success - does not convert 18 decimals",
evmDenom: TokenDenom,
evmDecimals: 18,
expResult: big.NewInt(1e18), // 1 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
Expand All @@ -34,7 +32,6 @@ func TestGetBaseFee(t *testing.T) {
},
{
name: "success - convert 6 decimals to 18 decimals",
evmDenom: TokenDenom,
evmDecimals: 6,
expResult: big.NewInt(1e18), // 1 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
Expand All @@ -45,7 +42,6 @@ func TestGetBaseFee(t *testing.T) {
},
{
name: "success - nil base fee",
evmDenom: TokenDenom,
evmDecimals: 6,
expResult: nil,
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
Expand All @@ -56,7 +52,6 @@ func TestGetBaseFee(t *testing.T) {
},
{
name: "success - small amount 18 decimals",
evmDenom: TokenDenom,
evmDecimals: 6,
expResult: big.NewInt(1e12), // 0.000001 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
Expand All @@ -67,7 +62,6 @@ func TestGetBaseFee(t *testing.T) {
},
{
name: "success - base fee is zero",
evmDenom: TokenDenom,
evmDecimals: 6,
expResult: big.NewInt(0),
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
Expand All @@ -78,7 +72,6 @@ func TestGetBaseFee(t *testing.T) {
},
{
name: "success - truncate decimals with number less than 1",
evmDenom: TokenDenom,
evmDecimals: 6,
expResult: big.NewInt(0), // 0.000001 token in 18 decimals
mockSetup: func(mfk *testutil.MockFeeMarketKeeper) {
Expand All @@ -94,7 +87,7 @@ func TestGetBaseFee(t *testing.T) {
// Setup EVM configurator to have access to the EVM coin info.
configurator := evmtypes.NewEVMConfigurator()
configurator.ResetTestConfig()
err := configurator.WithEVMCoinInfo(tc.evmDenom, tc.evmDecimals).Configure()
err := configurator.WithEVMCoinInfo("token", tc.evmDecimals).Configure()
require.NoError(t, err, "failed to configure EVMConfigurator")

ctrl := gomock.NewController(t)
Expand All @@ -108,3 +101,93 @@ func TestGetBaseFee(t *testing.T) {
})
}
}

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)
})
}
}

0 comments on commit 016175e

Please sign in to comment.