diff options
author | kumavis <aaron@kumavis.me> | 2018-05-23 04:20:04 +0800 |
---|---|---|
committer | kumavis <aaron@kumavis.me> | 2018-05-23 04:20:04 +0800 |
commit | 3a80f3dd836889b7e4f4d4db8eaf42883623d9bf (patch) | |
tree | bcde7f3a6acbdf311bf66b70be6bfac2b3ed088a /test/unit/app/controllers/transactions/tx-gas-util-test.js | |
parent | 3084dc47d10e3e455c924e5aad0b0961c500ec8d (diff) | |
parent | cee55b47d03006630b1dbe038c6008654ca8f674 (diff) | |
download | tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.tar.gz tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.tar.zst tangerine-wallet-browser-3a80f3dd836889b7e4f4d4db8eaf42883623d9bf.zip |
Merge branch 'develop' of github.com:MetaMask/metamask-extension into network-remove-provider-engine
Diffstat (limited to 'test/unit/app/controllers/transactions/tx-gas-util-test.js')
-rw-r--r-- | test/unit/app/controllers/transactions/tx-gas-util-test.js | 77 |
1 files changed, 77 insertions, 0 deletions
diff --git a/test/unit/app/controllers/transactions/tx-gas-util-test.js b/test/unit/app/controllers/transactions/tx-gas-util-test.js new file mode 100644 index 000000000..d1ee86033 --- /dev/null +++ b/test/unit/app/controllers/transactions/tx-gas-util-test.js @@ -0,0 +1,77 @@ +const assert = require('assert') +const Transaction = require('ethereumjs-tx') +const BN = require('bn.js') + + +const { hexToBn, bnToHex } = require('../../../../../app/scripts/lib/util') +const TxUtils = require('../../../../../app/scripts/controllers/transactions/tx-gas-utils') + + +describe('txUtils', function () { + let txUtils + + before(function () { + txUtils = new TxUtils(new Proxy({}, { + get: (obj, name) => { + return () => {} + }, + })) + }) + + 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', + chainId: 42, + } + const ethTx = new Transaction(txParams) + assert.equal(ethTx.getChainId(), 42, 'chainId is set from tx params') + }) + }) + + describe('addGasBuffer', function () { + it('multiplies by 1.5, when within block gas limit', function () { + // naive estimatedGas: 0x16e360 (1.5 mil) + const inputHex = '0x16e360' + // dummy gas limit: 0x3d4c52 (4 mil) + const blockGasLimitHex = '0x3d4c52' + const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex) + const inputBn = hexToBn(inputHex) + const outputBn = hexToBn(output) + const expectedBn = inputBn.muln(1.5) + assert(outputBn.eq(expectedBn), 'returns 1.5 the input value') + }) + + it('uses original estimatedGas, when above block gas limit', function () { + // naive estimatedGas: 0x16e360 (1.5 mil) + const inputHex = '0x16e360' + // dummy gas limit: 0x0f4240 (1 mil) + const blockGasLimitHex = '0x0f4240' + const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex) + // const inputBn = hexToBn(inputHex) + const outputBn = hexToBn(output) + const expectedBn = hexToBn(inputHex) + assert(outputBn.eq(expectedBn), 'returns the original estimatedGas value') + }) + + it('buffers up to recommend gas limit recommended ceiling', function () { + // naive estimatedGas: 0x16e360 (1.5 mil) + const inputHex = '0x16e360' + // dummy gas limit: 0x1e8480 (2 mil) + const blockGasLimitHex = '0x1e8480' + const blockGasLimitBn = hexToBn(blockGasLimitHex) + const ceilGasLimitBn = blockGasLimitBn.muln(0.9) + const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex) + // const inputBn = hexToBn(inputHex) + // const outputBn = hexToBn(output) + const expectedHex = bnToHex(ceilGasLimitBn) + assert.equal(output, expectedHex, 'returns the gas limit recommended ceiling value') + }) + }) +}) |