|
| 1 | +package wrappers_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "math/big" |
| 5 | + "testing" |
| 6 | + |
| 7 | + sdkmath "cosmossdk.io/math" |
| 8 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 9 | + evmtypes "github.com/evmos/os/x/evm/types" |
| 10 | + "github.com/evmos/os/x/evm/wrappers" |
| 11 | + "github.com/evmos/os/x/evm/wrappers/testutil" |
| 12 | + feemarkettypes "github.com/evmos/os/x/feemarket/types" |
| 13 | + "github.com/stretchr/testify/require" |
| 14 | + "go.uber.org/mock/gomock" |
| 15 | +) |
| 16 | + |
| 17 | +func TestGetBaseFee(t *testing.T) { |
| 18 | + testCases := []struct { |
| 19 | + name string |
| 20 | + evmDecimals uint8 |
| 21 | + expResult *big.Int |
| 22 | + mockSetup func(*testutil.MockFeeMarketKeeper) |
| 23 | + }{ |
| 24 | + { |
| 25 | + name: "success - does not convert 18 decimals", |
| 26 | + evmDecimals: 18, |
| 27 | + expResult: big.NewInt(1e18), // 1 token in 18 decimals |
| 28 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 29 | + mfk.EXPECT(). |
| 30 | + GetBaseFee(gomock.Any()). |
| 31 | + Return(sdkmath.LegacyNewDec(1e18)) |
| 32 | + }, |
| 33 | + }, |
| 34 | + { |
| 35 | + name: "success - convert 6 decimals to 18 decimals", |
| 36 | + evmDecimals: 6, |
| 37 | + expResult: big.NewInt(1e18), // 1 token in 18 decimals |
| 38 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 39 | + mfk.EXPECT(). |
| 40 | + GetBaseFee(gomock.Any()). |
| 41 | + Return(sdkmath.LegacyNewDec(1_000_000)) |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + name: "success - nil base fee", |
| 46 | + evmDecimals: 6, |
| 47 | + expResult: nil, |
| 48 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 49 | + mfk.EXPECT(). |
| 50 | + GetBaseFee(gomock.Any()). |
| 51 | + Return(sdkmath.LegacyDec{}) |
| 52 | + }, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "success - small amount 18 decimals", |
| 56 | + evmDecimals: 6, |
| 57 | + expResult: big.NewInt(1e12), // 0.000001 token in 18 decimals |
| 58 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 59 | + mfk.EXPECT(). |
| 60 | + GetBaseFee(gomock.Any()). |
| 61 | + Return(sdkmath.LegacyNewDec(1)) |
| 62 | + }, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "success - base fee is zero", |
| 66 | + evmDecimals: 6, |
| 67 | + expResult: big.NewInt(0), |
| 68 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 69 | + mfk.EXPECT(). |
| 70 | + GetBaseFee(gomock.Any()). |
| 71 | + Return(sdkmath.LegacyNewDec(0)) |
| 72 | + }, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "success - truncate decimals with number less than 1", |
| 76 | + evmDecimals: 6, |
| 77 | + expResult: big.NewInt(0), // 0.000001 token in 18 decimals |
| 78 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 79 | + mfk.EXPECT(). |
| 80 | + GetBaseFee(gomock.Any()). |
| 81 | + Return(sdkmath.LegacyNewDecWithPrec(1, 13)) // multiplied by 1e12 is still less than 1 |
| 82 | + }, |
| 83 | + }, |
| 84 | + } |
| 85 | + |
| 86 | + for _, tc := range testCases { |
| 87 | + t.Run(tc.name, func(t *testing.T) { |
| 88 | + // Setup EVM configurator to have access to the EVM coin info. |
| 89 | + configurator := evmtypes.NewEVMConfigurator() |
| 90 | + configurator.ResetTestConfig() |
| 91 | + err := configurator.WithEVMCoinInfo("token", tc.evmDecimals).Configure() |
| 92 | + require.NoError(t, err, "failed to configure EVMConfigurator") |
| 93 | + |
| 94 | + ctrl := gomock.NewController(t) |
| 95 | + mockFeeMarketKeeper := testutil.NewMockFeeMarketKeeper(ctrl) |
| 96 | + tc.mockSetup(mockFeeMarketKeeper) |
| 97 | + |
| 98 | + feeMarketWrapper := wrappers.NewFeeMarketWrapper(mockFeeMarketKeeper) |
| 99 | + result := feeMarketWrapper.GetBaseFee(sdk.Context{}) |
| 100 | + |
| 101 | + require.Equal(t, tc.expResult, result) |
| 102 | + }) |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +func TestCalculateBaseFee(t *testing.T) { |
| 107 | + testCases := []struct { |
| 108 | + name string |
| 109 | + evmDecimals uint8 |
| 110 | + baseFee sdkmath.LegacyDec |
| 111 | + expResult *big.Int |
| 112 | + mockSetup func(*testutil.MockFeeMarketKeeper) |
| 113 | + }{ |
| 114 | + { |
| 115 | + name: "success - does not convert 18 decimals", |
| 116 | + evmDecimals: 18, |
| 117 | + expResult: big.NewInt(1e18), // 1 token in 18 decimals |
| 118 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 119 | + mfk.EXPECT(). |
| 120 | + CalculateBaseFee(gomock.Any()). |
| 121 | + Return(sdkmath.LegacyNewDec(1e18)) |
| 122 | + }, |
| 123 | + }, |
| 124 | + { |
| 125 | + name: "success - convert 6 decimals to 18 decimals", |
| 126 | + evmDecimals: 6, |
| 127 | + expResult: big.NewInt(1e18), // 1 token in 18 decimals |
| 128 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 129 | + mfk.EXPECT(). |
| 130 | + CalculateBaseFee(gomock.Any()). |
| 131 | + Return(sdkmath.LegacyNewDec(1_000_000)) |
| 132 | + }, |
| 133 | + }, |
| 134 | + { |
| 135 | + name: "success - nil base fee", |
| 136 | + evmDecimals: 6, |
| 137 | + expResult: nil, |
| 138 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 139 | + mfk.EXPECT(). |
| 140 | + CalculateBaseFee(gomock.Any()). |
| 141 | + Return(sdkmath.LegacyDec{}) |
| 142 | + }, |
| 143 | + }, |
| 144 | + { |
| 145 | + name: "success - small amount 18 decimals", |
| 146 | + evmDecimals: 6, |
| 147 | + expResult: big.NewInt(1e12), // 0.000001 token in 18 decimals |
| 148 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 149 | + mfk.EXPECT(). |
| 150 | + CalculateBaseFee(gomock.Any()). |
| 151 | + Return(sdkmath.LegacyNewDec(1)) |
| 152 | + }, |
| 153 | + }, |
| 154 | + { |
| 155 | + name: "success - base fee is zero", |
| 156 | + evmDecimals: 6, |
| 157 | + expResult: big.NewInt(0), |
| 158 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 159 | + mfk.EXPECT(). |
| 160 | + CalculateBaseFee(gomock.Any()). |
| 161 | + Return(sdkmath.LegacyNewDec(0)) |
| 162 | + }, |
| 163 | + }, |
| 164 | + { |
| 165 | + name: "success - truncate decimals with number less than 1", |
| 166 | + evmDecimals: 6, |
| 167 | + expResult: big.NewInt(0), // 0.000001 token in 18 decimals |
| 168 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 169 | + mfk.EXPECT(). |
| 170 | + CalculateBaseFee(gomock.Any()). |
| 171 | + Return(sdkmath.LegacyNewDecWithPrec(1, 13)) // multiplied by 1e12 is still less than 1 |
| 172 | + }, |
| 173 | + }, |
| 174 | + } |
| 175 | + |
| 176 | + for _, tc := range testCases { |
| 177 | + t.Run(tc.name, func(t *testing.T) { |
| 178 | + // Setup EVM configurator to have access to the EVM coin info. |
| 179 | + configurator := evmtypes.NewEVMConfigurator() |
| 180 | + configurator.ResetTestConfig() |
| 181 | + err := configurator.WithEVMCoinInfo("token", tc.evmDecimals).Configure() |
| 182 | + require.NoError(t, err, "failed to configure EVMConfigurator") |
| 183 | + |
| 184 | + ctrl := gomock.NewController(t) |
| 185 | + mockFeeMarketKeeper := testutil.NewMockFeeMarketKeeper(ctrl) |
| 186 | + tc.mockSetup(mockFeeMarketKeeper) |
| 187 | + |
| 188 | + feeMarketWrapper := wrappers.NewFeeMarketWrapper(mockFeeMarketKeeper) |
| 189 | + result := feeMarketWrapper.CalculateBaseFee(sdk.Context{}) |
| 190 | + |
| 191 | + require.Equal(t, tc.expResult, result) |
| 192 | + }) |
| 193 | + } |
| 194 | +} |
| 195 | + |
| 196 | +func TestGetParams(t *testing.T) { |
| 197 | + testCases := []struct { |
| 198 | + name string |
| 199 | + evmDecimals uint8 |
| 200 | + expParams feemarkettypes.Params |
| 201 | + mockSetup func(*testutil.MockFeeMarketKeeper) |
| 202 | + }{ |
| 203 | + { |
| 204 | + name: "success - convert 6 decimals to 18 decimals", |
| 205 | + evmDecimals: 6, |
| 206 | + expParams: feemarkettypes.Params{ |
| 207 | + BaseFee: sdkmath.LegacyNewDec(1e18), |
| 208 | + MinGasPrice: sdkmath.LegacyNewDec(1e18), |
| 209 | + }, |
| 210 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 211 | + mfk.EXPECT(). |
| 212 | + GetParams(gomock.Any()). |
| 213 | + Return(feemarkettypes.Params{ |
| 214 | + BaseFee: sdkmath.LegacyNewDec(1_000_000), |
| 215 | + MinGasPrice: sdkmath.LegacyNewDec(1_000_000), |
| 216 | + }) |
| 217 | + }, |
| 218 | + }, |
| 219 | + { |
| 220 | + name: "success - does not convert 18 decimals", |
| 221 | + evmDecimals: 18, |
| 222 | + expParams: feemarkettypes.Params{ |
| 223 | + BaseFee: sdkmath.LegacyNewDec(1e18), |
| 224 | + MinGasPrice: sdkmath.LegacyNewDec(1e18), |
| 225 | + }, |
| 226 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 227 | + mfk.EXPECT(). |
| 228 | + GetParams(gomock.Any()). |
| 229 | + Return(feemarkettypes.Params{ |
| 230 | + BaseFee: sdkmath.LegacyNewDec(1e18), |
| 231 | + MinGasPrice: sdkmath.LegacyNewDec(1e18), |
| 232 | + }) |
| 233 | + }, |
| 234 | + }, |
| 235 | + { |
| 236 | + name: "success - nil base fee", |
| 237 | + evmDecimals: 18, |
| 238 | + expParams: feemarkettypes.Params{ |
| 239 | + MinGasPrice: sdkmath.LegacyNewDec(1e18), |
| 240 | + }, |
| 241 | + mockSetup: func(mfk *testutil.MockFeeMarketKeeper) { |
| 242 | + mfk.EXPECT(). |
| 243 | + GetParams(gomock.Any()). |
| 244 | + Return(feemarkettypes.Params{ |
| 245 | + MinGasPrice: sdkmath.LegacyNewDec(1e18), |
| 246 | + }) |
| 247 | + }, |
| 248 | + }, |
| 249 | + } |
| 250 | + |
| 251 | + for _, tc := range testCases { |
| 252 | + t.Run(tc.name, func(t *testing.T) { |
| 253 | + // Setup EVM configurator to have access to the EVM coin info. |
| 254 | + configurator := evmtypes.NewEVMConfigurator() |
| 255 | + configurator.ResetTestConfig() |
| 256 | + err := configurator.WithEVMCoinInfo("token", tc.evmDecimals).Configure() |
| 257 | + require.NoError(t, err, "failed to configure EVMConfigurator") |
| 258 | + |
| 259 | + ctrl := gomock.NewController(t) |
| 260 | + mockFeeMarketKeeper := testutil.NewMockFeeMarketKeeper(ctrl) |
| 261 | + tc.mockSetup(mockFeeMarketKeeper) |
| 262 | + |
| 263 | + feeMarketWrapper := wrappers.NewFeeMarketWrapper(mockFeeMarketKeeper) |
| 264 | + result := feeMarketWrapper.GetParams(sdk.Context{}) |
| 265 | + |
| 266 | + require.Equal(t, tc.expParams, result) |
| 267 | + }) |
| 268 | + } |
| 269 | +} |
0 commit comments