1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-10-22 11:22:43 +02:00
metamask-extension/shared/modules/gas.utils.test.js

135 lines
5.1 KiB
JavaScript
Raw Normal View History

2021-07-07 18:13:40 +02:00
const { addHexPrefix } = require('ethereumjs-util');
const {
getMaximumGasTotalInHexWei,
getMinimumGasTotalInHexWei,
} = require('./gas.utils');
const { Numeric } = require('./Numeric');
2021-07-07 18:13:40 +02:00
const feesToTest = [10, 24, 90];
const tipsToTest = [2, 10, 50];
const baseFeesToTest = [8, 12, 24];
const gasLimitsToTest = [21000, 100000];
describe('gas utils', () => {
describe('when using EIP 1559 fields', () => {
describe('getMaximumGasTotalInHexWei', () => {
feesToTest.forEach((maxFeePerGas) => {
describe(`when maxFeePerGas is ${maxFeePerGas}`, () => {
gasLimitsToTest.forEach((gasLimit) => {
const expectedResult = (gasLimit * maxFeePerGas).toString();
const gasLimitHex = addHexPrefix(gasLimit.toString(16));
const result = new Numeric(
2021-07-07 18:13:40 +02:00
getMaximumGasTotalInHexWei({
gasLimit: gasLimitHex,
maxFeePerGas: addHexPrefix(maxFeePerGas.toString(16)),
}),
16,
2021-07-07 18:13:40 +02:00
);
it(`returns ${expectedResult} when provided gasLimit: ${gasLimit}`, () => {
expect(result.toBase(10).toString()).toStrictEqual(
expectedResult,
);
2021-07-07 18:13:40 +02:00
});
});
});
});
});
describe('getMinimumGasTotalInHexWei', () => {
feesToTest.forEach((maxFeePerGas) => {
tipsToTest.forEach((maxPriorityFeePerGas) => {
baseFeesToTest.forEach((baseFeePerGas) => {
describe(`when baseFee is ${baseFeePerGas}, maxFeePerGas is ${maxFeePerGas} and tip is ${maxPriorityFeePerGas}`, () => {
const maximum = maxFeePerGas;
const minimum = baseFeePerGas + maxPriorityFeePerGas;
const expectedEffectiveGasPrice =
minimum < maximum ? minimum : maximum;
const results = gasLimitsToTest.map((gasLimit) => {
const gasLimitHex = addHexPrefix(gasLimit.toString(16));
const result = new Numeric(
2021-07-07 18:13:40 +02:00
getMinimumGasTotalInHexWei({
gasLimit: gasLimitHex,
maxFeePerGas: addHexPrefix(maxFeePerGas.toString(16)),
maxPriorityFeePerGas: addHexPrefix(
maxPriorityFeePerGas.toString(16),
),
baseFeePerGas: addHexPrefix(baseFeePerGas.toString(16)),
}),
16,
2021-07-07 18:13:40 +02:00
);
return { result: result.toBase(10).toString(), gasLimit };
2021-07-07 18:13:40 +02:00
});
it(`should use an effective gasPrice of ${expectedEffectiveGasPrice}`, () => {
expect(
results.every(({ result, gasLimit }) => {
const effectiveGasPrice = Number(result) / gasLimit;
return effectiveGasPrice === expectedEffectiveGasPrice;
}),
).toBe(true);
});
results.forEach(({ result, gasLimit }) => {
const expectedResult = (
expectedEffectiveGasPrice * gasLimit
).toString();
it(`returns ${expectedResult} when provided gasLimit: ${gasLimit}`, () => {
expect(result).toStrictEqual(expectedResult);
});
});
});
});
});
});
});
});
describe('when using legacy fields', () => {
describe('getMaximumGasTotalInHexWei', () => {
feesToTest.forEach((gasPrice) => {
describe(`when gasPrice is ${gasPrice}`, () => {
gasLimitsToTest.forEach((gasLimit) => {
const expectedResult = (gasLimit * gasPrice).toString();
const gasLimitHex = addHexPrefix(gasLimit.toString(16));
it(`returns ${expectedResult} when provided gasLimit of ${gasLimit}`, () => {
expect(
new Numeric(
2021-07-07 18:13:40 +02:00
getMaximumGasTotalInHexWei({
gasLimit: gasLimitHex,
gasPrice: addHexPrefix(gasPrice.toString(16)),
}),
16,
)
.toBase(10)
.toString(),
2021-07-07 18:13:40 +02:00
).toStrictEqual(expectedResult);
});
});
});
});
});
describe('getMinimumGasTotalInHexWei', () => {
feesToTest.forEach((gasPrice) => {
describe(`when gasPrice is ${gasPrice}`, () => {
gasLimitsToTest.forEach((gasLimit) => {
const expectedResult = (gasLimit * gasPrice).toString();
const gasLimitHex = addHexPrefix(gasLimit.toString(16));
it(`returns ${expectedResult} when provided gasLimit of ${gasLimit}`, () => {
expect(
new Numeric(
2021-07-07 18:13:40 +02:00
getMinimumGasTotalInHexWei({
gasLimit: gasLimitHex,
gasPrice: addHexPrefix(gasPrice.toString(16)),
}),
16,
)
.toBase(10)
.toString(),
2021-07-07 18:13:40 +02:00
).toStrictEqual(expectedResult);
});
});
});
});
});
});
});