|
|
|
@ -1,6 +1,13 @@ |
|
|
|
|
import assert from 'assert' |
|
|
|
|
import sinon from 'sinon' |
|
|
|
|
import proxyquire from 'proxyquire' |
|
|
|
|
import { |
|
|
|
|
ONE_GWEI_IN_WEI_HEX, |
|
|
|
|
} from '../send.constants' |
|
|
|
|
const { |
|
|
|
|
addCurrencies, |
|
|
|
|
subtractCurrencies, |
|
|
|
|
} = require('../../../conversion-util') |
|
|
|
|
|
|
|
|
|
const { |
|
|
|
|
INSUFFICIENT_FUNDS_ERROR, |
|
|
|
@ -31,7 +38,9 @@ const sendUtils = proxyquire('../send.utils.js', { |
|
|
|
|
|
|
|
|
|
const { |
|
|
|
|
calcGasTotal, |
|
|
|
|
estimateGas, |
|
|
|
|
doesAmountErrorRequireUpdate, |
|
|
|
|
estimateGasPriceFromRecentBlocks, |
|
|
|
|
generateTokenTransferData, |
|
|
|
|
getAmountErrorObject, |
|
|
|
|
getParamsForGasEstimate, |
|
|
|
@ -261,4 +270,101 @@ describe('send utils', () => { |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
describe('estimateGas', () => { |
|
|
|
|
let tempEthQuery |
|
|
|
|
beforeEach(() => { |
|
|
|
|
tempEthQuery = global.ethQuery |
|
|
|
|
global.ethQuery = { |
|
|
|
|
estimateGas: sinon.stub().callsFake((data, cb) => { |
|
|
|
|
return cb( |
|
|
|
|
data.isMockErr ? 'mockErr' : null, |
|
|
|
|
Object.assign(data, { estimateGasCalled: true }) |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
afterEach(() => { |
|
|
|
|
global.ethQuery = tempEthQuery |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
it('should call ethQuery.estimateGas and resolve that call\'s data', async () => { |
|
|
|
|
const result = await estimateGas({ mockParam: 'someData' }) |
|
|
|
|
assert.equal(global.ethQuery.estimateGas.callCount, 1) |
|
|
|
|
assert.deepEqual( |
|
|
|
|
result, |
|
|
|
|
{ mockParam: 'someData', estimateGasCalled: true } |
|
|
|
|
) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
it('should reject with ethQuery.estimateGas error', async () => { |
|
|
|
|
try { |
|
|
|
|
await estimateGas({ mockParam: 'someData', isMockErr: true }) |
|
|
|
|
} catch (err) { |
|
|
|
|
assert.equal(err, 'mockErr') |
|
|
|
|
} |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
describe('estimateGasPriceFromRecentBlocks', () => { |
|
|
|
|
const ONE_GWEI_IN_WEI_HEX_PLUS_ONE = addCurrencies(ONE_GWEI_IN_WEI_HEX, '0x1', { |
|
|
|
|
aBase: 16, |
|
|
|
|
bBase: 16, |
|
|
|
|
toNumericBase: 'hex', |
|
|
|
|
}) |
|
|
|
|
const ONE_GWEI_IN_WEI_HEX_PLUS_TWO = addCurrencies(ONE_GWEI_IN_WEI_HEX, '0x2', { |
|
|
|
|
aBase: 16, |
|
|
|
|
bBase: 16, |
|
|
|
|
toNumericBase: 'hex', |
|
|
|
|
}) |
|
|
|
|
const ONE_GWEI_IN_WEI_HEX_MINUS_ONE = subtractCurrencies(ONE_GWEI_IN_WEI_HEX, '0x1', { |
|
|
|
|
aBase: 16, |
|
|
|
|
bBase: 16, |
|
|
|
|
toNumericBase: 'hex', |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
it(`should return ${ONE_GWEI_IN_WEI_HEX} if recentBlocks is falsy`, () => { |
|
|
|
|
assert.equal(estimateGasPriceFromRecentBlocks(), ONE_GWEI_IN_WEI_HEX) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
it(`should return ${ONE_GWEI_IN_WEI_HEX} if recentBlocks is empty`, () => { |
|
|
|
|
assert.equal(estimateGasPriceFromRecentBlocks([]), ONE_GWEI_IN_WEI_HEX) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
it(`should estimate a block's gasPrice as ${ONE_GWEI_IN_WEI_HEX} if it has no gas prices`, () => { |
|
|
|
|
const mockRecentBlocks = [ |
|
|
|
|
{ gasPrices: null }, |
|
|
|
|
{ gasPrices: [ ONE_GWEI_IN_WEI_HEX_PLUS_ONE ] }, |
|
|
|
|
{ gasPrices: [ ONE_GWEI_IN_WEI_HEX_MINUS_ONE ] }, |
|
|
|
|
] |
|
|
|
|
assert.equal(estimateGasPriceFromRecentBlocks(mockRecentBlocks), ONE_GWEI_IN_WEI_HEX) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
it(`should estimate a block's gasPrice as ${ONE_GWEI_IN_WEI_HEX} if it has empty gas prices`, () => { |
|
|
|
|
const mockRecentBlocks = [ |
|
|
|
|
{ gasPrices: [] }, |
|
|
|
|
{ gasPrices: [ ONE_GWEI_IN_WEI_HEX_PLUS_ONE ] }, |
|
|
|
|
{ gasPrices: [ ONE_GWEI_IN_WEI_HEX_MINUS_ONE ] }, |
|
|
|
|
] |
|
|
|
|
assert.equal(estimateGasPriceFromRecentBlocks(mockRecentBlocks), ONE_GWEI_IN_WEI_HEX) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
it(`should return the middle value of all blocks lowest prices`, () => { |
|
|
|
|
const mockRecentBlocks = [ |
|
|
|
|
{ gasPrices: [ ONE_GWEI_IN_WEI_HEX_PLUS_TWO ] }, |
|
|
|
|
{ gasPrices: [ ONE_GWEI_IN_WEI_HEX_MINUS_ONE ] }, |
|
|
|
|
{ gasPrices: [ ONE_GWEI_IN_WEI_HEX_PLUS_ONE ] }, |
|
|
|
|
] |
|
|
|
|
assert.equal(estimateGasPriceFromRecentBlocks(mockRecentBlocks), ONE_GWEI_IN_WEI_HEX_PLUS_ONE) |
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
it(`should work if a block has multiple gas prices`, () => { |
|
|
|
|
const mockRecentBlocks = [ |
|
|
|
|
{ gasPrices: [ '0x1', '0x2', '0x3', '0x4', '0x5' ] }, |
|
|
|
|
{ gasPrices: [ '0x101', '0x100', '0x103', '0x104', '0x102' ] }, |
|
|
|
|
{ gasPrices: [ '0x150', '0x50', '0x100', '0x200', '0x5' ] }, |
|
|
|
|
] |
|
|
|
|
assert.equal(estimateGasPriceFromRecentBlocks(mockRecentBlocks), '0x5') |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|
}) |
|
|
|
|