|
|
@ -3,6 +3,7 @@ import sinon from 'sinon' |
|
|
|
import proxyquire from 'proxyquire' |
|
|
|
import proxyquire from 'proxyquire' |
|
|
|
import { |
|
|
|
import { |
|
|
|
ONE_GWEI_IN_WEI_HEX, |
|
|
|
ONE_GWEI_IN_WEI_HEX, |
|
|
|
|
|
|
|
SIMPLE_GAS_COST, |
|
|
|
} from '../send.constants' |
|
|
|
} from '../send.constants' |
|
|
|
const { |
|
|
|
const { |
|
|
|
addCurrencies, |
|
|
|
addCurrencies, |
|
|
@ -18,11 +19,19 @@ const stubs = { |
|
|
|
addCurrencies: sinon.stub().callsFake((a, b, obj) => a + b), |
|
|
|
addCurrencies: sinon.stub().callsFake((a, b, obj) => a + b), |
|
|
|
conversionUtil: sinon.stub().callsFake((val, obj) => parseInt(val, 16)), |
|
|
|
conversionUtil: sinon.stub().callsFake((val, obj) => parseInt(val, 16)), |
|
|
|
conversionGTE: sinon.stub().callsFake((obj1, obj2) => obj1.value > obj2.value), |
|
|
|
conversionGTE: sinon.stub().callsFake((obj1, obj2) => obj1.value > obj2.value), |
|
|
|
multiplyCurrencies: sinon.stub().callsFake((a, b) => a * b), |
|
|
|
multiplyCurrencies: sinon.stub().callsFake((a, b) => `${a}x${b}`), |
|
|
|
calcTokenAmount: sinon.stub().callsFake((a, d) => 'calc:' + a + d), |
|
|
|
calcTokenAmount: sinon.stub().callsFake((a, d) => 'calc:' + a + d), |
|
|
|
rawEncode: sinon.stub().returns([16, 1100]), |
|
|
|
rawEncode: sinon.stub().returns([16, 1100]), |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const EthQuery = function () {} |
|
|
|
|
|
|
|
EthQuery.prototype.estimateGas = sinon.stub().callsFake( |
|
|
|
|
|
|
|
(data) => Promise.resolve({ toString: (n) => `mockToString:${n}` }) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
EthQuery.prototype.getCode = sinon.stub().callsFake( |
|
|
|
|
|
|
|
(address) => Promise.resolve(address.match(/isContract/) ? 'not-0x' : '0x')
|
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
|
|
|
|
const sendUtils = proxyquire('../send.utils.js', { |
|
|
|
const sendUtils = proxyquire('../send.utils.js', { |
|
|
|
'../../conversion-util': { |
|
|
|
'../../conversion-util': { |
|
|
|
addCurrencies: stubs.addCurrencies, |
|
|
|
addCurrencies: stubs.addCurrencies, |
|
|
@ -34,6 +43,7 @@ const sendUtils = proxyquire('../send.utils.js', { |
|
|
|
'ethereumjs-abi': { |
|
|
|
'ethereumjs-abi': { |
|
|
|
rawEncode: stubs.rawEncode, |
|
|
|
rawEncode: stubs.rawEncode, |
|
|
|
}, |
|
|
|
}, |
|
|
|
|
|
|
|
'ethjs-query': EthQuery, |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
const { |
|
|
|
const { |
|
|
@ -43,7 +53,6 @@ const { |
|
|
|
estimateGasPriceFromRecentBlocks, |
|
|
|
estimateGasPriceFromRecentBlocks, |
|
|
|
generateTokenTransferData, |
|
|
|
generateTokenTransferData, |
|
|
|
getAmountErrorObject, |
|
|
|
getAmountErrorObject, |
|
|
|
getParamsForGasEstimate, |
|
|
|
|
|
|
|
calcTokenBalance, |
|
|
|
calcTokenBalance, |
|
|
|
isBalanceSufficient, |
|
|
|
isBalanceSufficient, |
|
|
|
isTokenBalanceSufficient, |
|
|
|
isTokenBalanceSufficient, |
|
|
@ -54,7 +63,7 @@ describe('send utils', () => { |
|
|
|
describe('calcGasTotal()', () => { |
|
|
|
describe('calcGasTotal()', () => { |
|
|
|
it('should call multiplyCurrencies with the correct params and return the multiplyCurrencies return', () => { |
|
|
|
it('should call multiplyCurrencies with the correct params and return the multiplyCurrencies return', () => { |
|
|
|
const result = calcGasTotal(12, 15) |
|
|
|
const result = calcGasTotal(12, 15) |
|
|
|
assert.equal(result, 180) |
|
|
|
assert.equal(result, '12x15') |
|
|
|
const call_ = stubs.multiplyCurrencies.getCall(0).args |
|
|
|
const call_ = stubs.multiplyCurrencies.getCall(0).args |
|
|
|
assert.deepEqual( |
|
|
|
assert.deepEqual( |
|
|
|
call_, |
|
|
|
call_, |
|
|
@ -145,41 +154,6 @@ describe('send utils', () => { |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
describe('getParamsForGasEstimate()', () => { |
|
|
|
|
|
|
|
it('should return from and gas properties if no symbol or data', () => { |
|
|
|
|
|
|
|
assert.deepEqual( |
|
|
|
|
|
|
|
getParamsForGasEstimate('mockAddress'), |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
from: 'mockAddress', |
|
|
|
|
|
|
|
gas: '746a528800', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('should return value property if selected token provided', () => { |
|
|
|
|
|
|
|
assert.deepEqual( |
|
|
|
|
|
|
|
getParamsForGasEstimate('mockAddress', { symbol: 'ABC' }), |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
from: 'mockAddress', |
|
|
|
|
|
|
|
gas: '746a528800', |
|
|
|
|
|
|
|
value: '0x0', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('should return data property if data provided', () => { |
|
|
|
|
|
|
|
assert.deepEqual( |
|
|
|
|
|
|
|
getParamsForGasEstimate('mockAddress', { symbol: 'ABC' }, 'somedata'), |
|
|
|
|
|
|
|
{ |
|
|
|
|
|
|
|
from: 'mockAddress', |
|
|
|
|
|
|
|
gas: '746a528800', |
|
|
|
|
|
|
|
value: '0x0', |
|
|
|
|
|
|
|
data: 'somedata', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
describe('calcTokenBalance()', () => { |
|
|
|
describe('calcTokenBalance()', () => { |
|
|
|
it('should return the calculated token blance', () => { |
|
|
|
it('should return the calculated token blance', () => { |
|
|
|
assert.equal(calcTokenBalance({ |
|
|
|
assert.equal(calcTokenBalance({ |
|
|
@ -271,38 +245,66 @@ describe('send utils', () => { |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
describe('estimateGas', () => { |
|
|
|
describe('estimateGas', () => { |
|
|
|
let tempEthQuery |
|
|
|
const baseMockParams = { |
|
|
|
beforeEach(() => { |
|
|
|
blockGasLimit: '0x64', |
|
|
|
tempEthQuery = global.ethQuery |
|
|
|
selectedAddress: 'mockAddress', |
|
|
|
global.ethQuery = { |
|
|
|
to: '0xisContract', |
|
|
|
estimateGas: sinon.stub().callsFake((data, cb) => { |
|
|
|
} |
|
|
|
return cb( |
|
|
|
const baseExpectedCall = { |
|
|
|
data.isMockErr ? 'mockErr' : null, |
|
|
|
from: 'mockAddress', |
|
|
|
Object.assign(data, { estimateGasCalled: true }) |
|
|
|
gas: '0x64x0.95', |
|
|
|
|
|
|
|
to: '0xisContract', |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
afterEach(() => { |
|
|
|
|
|
|
|
EthQuery.prototype.estimateGas.resetHistory() |
|
|
|
|
|
|
|
EthQuery.prototype.getCode.resetHistory() |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
it('should call ethQuery.estimateGas with the expected params', async () => { |
|
|
|
|
|
|
|
const result = await estimateGas(baseMockParams) |
|
|
|
|
|
|
|
assert.equal(EthQuery.prototype.estimateGas.callCount, 1) |
|
|
|
|
|
|
|
assert.deepEqual( |
|
|
|
|
|
|
|
EthQuery.prototype.estimateGas.getCall(0).args[0], |
|
|
|
|
|
|
|
baseExpectedCall |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
assert.equal(result, 'mockToString:16') |
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
it('should call ethQuery.estimateGas with a value of 0x0 if the passed selectedToken has a symbol', async () => { |
|
|
|
|
|
|
|
const result = await estimateGas(Object.assign({ selectedToken: { symbol: true } }, baseMockParams)) |
|
|
|
|
|
|
|
assert.equal(EthQuery.prototype.estimateGas.callCount, 1) |
|
|
|
|
|
|
|
assert.deepEqual( |
|
|
|
|
|
|
|
EthQuery.prototype.estimateGas.getCall(0).args[0], |
|
|
|
|
|
|
|
Object.assign({ value: '0x0' }, baseExpectedCall) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
assert.equal(result, 'mockToString:16') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
afterEach(() => { |
|
|
|
it('should call ethQuery.estimateGas with data if data is passed', async () => { |
|
|
|
global.ethQuery = tempEthQuery |
|
|
|
const result = await estimateGas(Object.assign({ data: 'mockData' }, baseMockParams)) |
|
|
|
|
|
|
|
assert.equal(EthQuery.prototype.estimateGas.callCount, 1) |
|
|
|
|
|
|
|
assert.deepEqual( |
|
|
|
|
|
|
|
EthQuery.prototype.estimateGas.getCall(0).args[0], |
|
|
|
|
|
|
|
Object.assign({ data: 'mockData' }, baseExpectedCall) |
|
|
|
|
|
|
|
) |
|
|
|
|
|
|
|
assert.equal(result, 'mockToString:16') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should call ethQuery.estimateGas and resolve that call\'s data', async () => { |
|
|
|
it('should call ethQuery.estimateGas with data if data is passed', async () => { |
|
|
|
const result = await estimateGas({ mockParam: 'someData' }) |
|
|
|
const result = await estimateGas(Object.assign({ data: 'mockData' }, baseMockParams)) |
|
|
|
assert.equal(global.ethQuery.estimateGas.callCount, 1) |
|
|
|
assert.equal(EthQuery.prototype.estimateGas.callCount, 1) |
|
|
|
assert.deepEqual( |
|
|
|
assert.deepEqual( |
|
|
|
result, |
|
|
|
EthQuery.prototype.estimateGas.getCall(0).args[0], |
|
|
|
{ mockParam: 'someData', estimateGasCalled: true } |
|
|
|
Object.assign({ data: 'mockData' }, baseExpectedCall) |
|
|
|
) |
|
|
|
) |
|
|
|
|
|
|
|
assert.equal(result, 'mockToString:16') |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|
it('should reject with ethQuery.estimateGas error', async () => { |
|
|
|
it(`should return ${SIMPLE_GAS_COST} if ethQuery.getCode does not return '0x'`, async () => { |
|
|
|
try { |
|
|
|
assert.equal(EthQuery.prototype.estimateGas.callCount, 0) |
|
|
|
await estimateGas({ mockParam: 'someData', isMockErr: true }) |
|
|
|
const result = await estimateGas(Object.assign({}, baseMockParams, { to: '0x123' })) |
|
|
|
} catch (err) { |
|
|
|
assert.equal(result, SIMPLE_GAS_COST) |
|
|
|
assert.equal(err, 'mockErr') |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
}) |
|
|
|
|
|
|
|
|
|
|
|