2021-02-04 19:15:23 +01:00
|
|
|
import { strict as assert } from 'assert';
|
2021-06-16 22:40:17 +02:00
|
|
|
import { TransactionFactory } from '@ethereumjs/tx';
|
2023-05-05 17:55:41 +02:00
|
|
|
import { Common } from '@ethereumjs/common';
|
2023-01-20 18:04:37 +01:00
|
|
|
import { hexToBn } from '../../lib/util';
|
|
|
|
import { bnToHex } from '../../../../shared/modules/conversion.utils';
|
2021-03-16 22:00:08 +01:00
|
|
|
import TxUtils from './tx-gas-utils';
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
describe('txUtils', function () {
|
2021-02-04 19:15:23 +01:00
|
|
|
let txUtils;
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
before(function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
txUtils = new TxUtils(
|
|
|
|
new Proxy(
|
|
|
|
{},
|
|
|
|
{
|
|
|
|
get: () => {
|
2021-02-04 19:15:23 +01:00
|
|
|
return () => undefined;
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
},
|
|
|
|
),
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
describe('chain Id', function () {
|
|
|
|
it('prepares a transaction with the provided chainId', function () {
|
|
|
|
const txParams = {
|
|
|
|
to: '0x70ad465e0bab6504002ad58c744ed89c7da38524',
|
|
|
|
from: '0x69ad465e0bab6504002ad58c744ed89c7da38525',
|
|
|
|
value: '0x0',
|
|
|
|
gas: '0x7b0c',
|
|
|
|
gasPrice: '0x199c82cc00',
|
|
|
|
data: '0x',
|
|
|
|
nonce: '0x3',
|
2022-09-29 05:26:01 +02:00
|
|
|
chainId: 5,
|
2021-02-04 19:15:23 +01:00
|
|
|
};
|
2021-06-16 22:40:17 +02:00
|
|
|
const ethTx = TransactionFactory.fromTxData(txParams, {
|
2022-09-29 05:26:01 +02:00
|
|
|
common: new Common({ chain: 'goerli' }),
|
2021-06-16 22:40:17 +02:00
|
|
|
});
|
|
|
|
assert.equal(
|
2023-05-05 17:55:41 +02:00
|
|
|
Number(ethTx.common.chainId()),
|
2022-09-29 05:26:01 +02:00
|
|
|
5,
|
2021-06-16 22:40:17 +02:00
|
|
|
'chainId is set from tx params',
|
|
|
|
);
|
2021-02-04 19:15:23 +01:00
|
|
|
});
|
|
|
|
});
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
describe('addGasBuffer', function () {
|
|
|
|
it('multiplies by 1.5, when within block gas limit', function () {
|
|
|
|
// naive estimatedGas: 0x16e360 (1.5 mil)
|
2021-02-04 19:15:23 +01:00
|
|
|
const inputHex = '0x16e360';
|
2018-04-06 20:07:20 +02:00
|
|
|
// dummy gas limit: 0x3d4c52 (4 mil)
|
2021-02-04 19:15:23 +01:00
|
|
|
const blockGasLimitHex = '0x3d4c52';
|
|
|
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex);
|
|
|
|
const inputBn = hexToBn(inputHex);
|
|
|
|
const outputBn = hexToBn(output);
|
|
|
|
const expectedBn = inputBn.muln(1.5);
|
|
|
|
assert.ok(outputBn.eq(expectedBn), 'returns 1.5 the input value');
|
|
|
|
});
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
it('uses original estimatedGas, when above block gas limit', function () {
|
|
|
|
// naive estimatedGas: 0x16e360 (1.5 mil)
|
2021-02-04 19:15:23 +01:00
|
|
|
const inputHex = '0x16e360';
|
2018-04-06 20:07:20 +02:00
|
|
|
// dummy gas limit: 0x0f4240 (1 mil)
|
2021-02-04 19:15:23 +01:00
|
|
|
const blockGasLimitHex = '0x0f4240';
|
|
|
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex);
|
2018-04-06 20:07:20 +02:00
|
|
|
// const inputBn = hexToBn(inputHex)
|
2021-02-04 19:15:23 +01:00
|
|
|
const outputBn = hexToBn(output);
|
|
|
|
const expectedBn = hexToBn(inputHex);
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.ok(
|
|
|
|
outputBn.eq(expectedBn),
|
|
|
|
'returns the original estimatedGas value',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
2018-04-06 20:07:20 +02:00
|
|
|
|
|
|
|
it('buffers up to recommend gas limit recommended ceiling', function () {
|
|
|
|
// naive estimatedGas: 0x16e360 (1.5 mil)
|
2021-02-04 19:15:23 +01:00
|
|
|
const inputHex = '0x16e360';
|
2018-04-06 20:07:20 +02:00
|
|
|
// dummy gas limit: 0x1e8480 (2 mil)
|
2021-02-04 19:15:23 +01:00
|
|
|
const blockGasLimitHex = '0x1e8480';
|
|
|
|
const blockGasLimitBn = hexToBn(blockGasLimitHex);
|
|
|
|
const ceilGasLimitBn = blockGasLimitBn.muln(0.9);
|
|
|
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex);
|
2018-04-06 20:07:20 +02:00
|
|
|
// const inputBn = hexToBn(inputHex)
|
|
|
|
// const outputBn = hexToBn(output)
|
2021-02-04 19:15:23 +01:00
|
|
|
const expectedHex = bnToHex(ceilGasLimitBn);
|
2020-11-03 00:41:28 +01:00
|
|
|
assert.equal(
|
|
|
|
output,
|
|
|
|
expectedHex,
|
|
|
|
'returns the gas limit recommended ceiling value',
|
2021-02-04 19:15:23 +01:00
|
|
|
);
|
|
|
|
});
|
|
|
|
});
|
|
|
|
});
|