2018-05-05 17:11:53 +02:00
|
|
|
import assert from 'assert'
|
|
|
|
import sinon from 'sinon'
|
|
|
|
import proxyquire from 'proxyquire'
|
2018-05-22 10:10:06 +02:00
|
|
|
import {
|
2018-06-29 01:33:09 +02:00
|
|
|
BASE_TOKEN_GAS_COST,
|
2018-05-22 17:16:53 +02:00
|
|
|
SIMPLE_GAS_COST,
|
2018-05-05 17:11:53 +02:00
|
|
|
INSUFFICIENT_FUNDS_ERROR,
|
|
|
|
INSUFFICIENT_TOKENS_ERROR,
|
2020-01-09 04:34:58 +01:00
|
|
|
} from '../send.constants'
|
2018-05-05 17:11:53 +02:00
|
|
|
|
|
|
|
const stubs = {
|
2019-05-08 21:51:33 +02:00
|
|
|
addCurrencies: sinon.stub().callsFake((a, b) => {
|
2020-08-15 13:58:11 +02:00
|
|
|
let [a1, b1] = [a, b]
|
2020-08-14 13:48:42 +02:00
|
|
|
if (String(a).match(/^0x.+/u)) {
|
2020-08-15 13:58:11 +02:00
|
|
|
a1 = Number(String(a).slice(2))
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-08-14 13:48:42 +02:00
|
|
|
if (String(b).match(/^0x.+/u)) {
|
2020-08-15 13:58:11 +02:00
|
|
|
b1 = Number(String(b).slice(2))
|
2019-11-20 01:03:20 +01:00
|
|
|
}
|
2020-08-15 13:58:11 +02:00
|
|
|
return a1 + b1
|
2018-06-29 19:19:40 +02:00
|
|
|
}),
|
2019-05-08 21:51:33 +02:00
|
|
|
conversionUtil: sinon.stub().callsFake((val) => parseInt(val, 16)),
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionGTE: sinon
|
|
|
|
.stub()
|
|
|
|
.callsFake((obj1, obj2) => obj1.value >= obj2.value),
|
2018-05-22 17:16:53 +02:00
|
|
|
multiplyCurrencies: sinon.stub().callsFake((a, b) => `${a}x${b}`),
|
2020-08-19 18:27:05 +02:00
|
|
|
calcTokenAmount: sinon.stub().callsFake((a, d) => `calc:${a}${d}`),
|
2018-05-05 17:11:53 +02:00
|
|
|
rawEncode: sinon.stub().returns([16, 1100]),
|
2020-11-03 00:41:28 +01:00
|
|
|
conversionGreaterThan: sinon
|
|
|
|
.stub()
|
|
|
|
.callsFake((obj1, obj2) => obj1.value > obj2.value),
|
|
|
|
conversionLessThan: sinon
|
|
|
|
.stub()
|
|
|
|
.callsFake((obj1, obj2) => obj1.value < obj2.value),
|
2018-05-05 17:11:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const sendUtils = proxyquire('../send.utils.js', {
|
2019-04-17 21:15:13 +02:00
|
|
|
'../../helpers/utils/conversion-util': {
|
2018-05-05 17:11:53 +02:00
|
|
|
addCurrencies: stubs.addCurrencies,
|
|
|
|
conversionUtil: stubs.conversionUtil,
|
|
|
|
conversionGTE: stubs.conversionGTE,
|
|
|
|
multiplyCurrencies: stubs.multiplyCurrencies,
|
2018-06-15 04:24:48 +02:00
|
|
|
conversionGreaterThan: stubs.conversionGreaterThan,
|
|
|
|
conversionLessThan: stubs.conversionLessThan,
|
2018-05-05 17:11:53 +02:00
|
|
|
},
|
2019-04-17 21:15:13 +02:00
|
|
|
'../../helpers/utils/token-util': { calcTokenAmount: stubs.calcTokenAmount },
|
2018-05-05 17:11:53 +02:00
|
|
|
'ethereumjs-abi': {
|
|
|
|
rawEncode: stubs.rawEncode,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
|
|
|
|
const {
|
|
|
|
calcGasTotal,
|
2020-10-13 23:13:54 +02:00
|
|
|
estimateGasForSend,
|
2018-05-05 17:11:53 +02:00
|
|
|
doesAmountErrorRequireUpdate,
|
|
|
|
generateTokenTransferData,
|
|
|
|
getAmountErrorObject,
|
2018-06-29 19:19:40 +02:00
|
|
|
getGasFeeErrorObject,
|
2018-06-18 18:37:01 +02:00
|
|
|
getToAddressForGasUpdate,
|
2018-05-05 17:11:53 +02:00
|
|
|
calcTokenBalance,
|
|
|
|
isBalanceSufficient,
|
|
|
|
isTokenBalanceSufficient,
|
2018-07-03 02:31:57 +02:00
|
|
|
removeLeadingZeroes,
|
2018-05-05 17:11:53 +02:00
|
|
|
} = sendUtils
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('send utils', function () {
|
|
|
|
describe('calcGasTotal()', function () {
|
|
|
|
it('should call multiplyCurrencies with the correct params and return the multiplyCurrencies return', function () {
|
2018-05-05 17:11:53 +02:00
|
|
|
const result = calcGasTotal(12, 15)
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(result, '12x15')
|
2018-05-05 17:11:53 +02:00
|
|
|
const call_ = stubs.multiplyCurrencies.getCall(0).args
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.deepStrictEqual(call_, [
|
2020-11-03 00:41:28 +01:00
|
|
|
12,
|
|
|
|
15,
|
|
|
|
{
|
2018-05-05 17:11:53 +02:00
|
|
|
toNumericBase: 'hex',
|
2019-07-31 22:17:11 +02:00
|
|
|
multiplicandBase: 16,
|
|
|
|
multiplierBase: 16,
|
2020-11-03 00:41:28 +01:00
|
|
|
},
|
|
|
|
])
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('doesAmountErrorRequireUpdate()', function () {
|
2018-05-05 17:11:53 +02:00
|
|
|
const config = {
|
|
|
|
'should return true if balances are different': {
|
|
|
|
balance: 0,
|
|
|
|
prevBalance: 1,
|
|
|
|
expectedResult: true,
|
|
|
|
},
|
|
|
|
'should return true if gasTotals are different': {
|
|
|
|
gasTotal: 0,
|
|
|
|
prevGasTotal: 1,
|
|
|
|
expectedResult: true,
|
|
|
|
},
|
|
|
|
'should return true if token balances are different': {
|
|
|
|
tokenBalance: 0,
|
|
|
|
prevTokenBalance: 1,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: { address: '0x0' },
|
2018-05-05 17:11:53 +02:00
|
|
|
expectedResult: true,
|
|
|
|
},
|
|
|
|
'should return false if they are all the same': {
|
|
|
|
balance: 1,
|
|
|
|
prevBalance: 1,
|
|
|
|
gasTotal: 1,
|
|
|
|
prevGasTotal: 1,
|
|
|
|
tokenBalance: 1,
|
|
|
|
prevTokenBalance: 1,
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: { address: '0x0' },
|
2018-05-05 17:11:53 +02:00
|
|
|
expectedResult: false,
|
|
|
|
},
|
|
|
|
}
|
2020-07-15 16:36:52 +02:00
|
|
|
Object.entries(config).forEach(([description, obj]) => {
|
2020-02-11 17:51:13 +01:00
|
|
|
it(description, function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(
|
|
|
|
doesAmountErrorRequireUpdate(obj),
|
|
|
|
obj.expectedResult,
|
|
|
|
)
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('generateTokenTransferData()', function () {
|
2020-05-29 19:46:10 +02:00
|
|
|
it('should return undefined if not passed a send token', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
generateTokenTransferData({
|
|
|
|
toAddress: 'mockAddress',
|
|
|
|
amount: '0xa',
|
|
|
|
sendToken: undefined,
|
|
|
|
}),
|
|
|
|
undefined,
|
|
|
|
)
|
2018-06-02 06:23:01 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should call abi.rawEncode with the correct params', function () {
|
2018-06-02 06:23:01 +02:00
|
|
|
stubs.rawEncode.resetHistory()
|
2020-11-03 00:41:28 +01:00
|
|
|
generateTokenTransferData({
|
|
|
|
toAddress: 'mockAddress',
|
|
|
|
amount: 'ab',
|
|
|
|
sendToken: { address: '0x0' },
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.deepStrictEqual(stubs.rawEncode.getCall(0).args, [
|
2020-11-03 00:41:28 +01:00
|
|
|
['address', 'uint256'],
|
|
|
|
['mockAddress', '0xab'],
|
|
|
|
])
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return encoded token transfer data', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
generateTokenTransferData({
|
|
|
|
toAddress: 'mockAddress',
|
|
|
|
amount: '0xa',
|
|
|
|
sendToken: { address: '0x0' },
|
|
|
|
}),
|
2020-07-14 17:20:41 +02:00
|
|
|
'0xa9059cbb104c',
|
2018-06-02 06:23:01 +02:00
|
|
|
)
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('getAmountErrorObject()', function () {
|
2018-05-05 17:11:53 +02:00
|
|
|
const config = {
|
|
|
|
'should return insufficientFunds error if isBalanceSufficient returns false': {
|
|
|
|
amount: 15,
|
|
|
|
balance: 1,
|
|
|
|
conversionRate: 3,
|
|
|
|
gasTotal: 17,
|
|
|
|
primaryCurrency: 'ABC',
|
|
|
|
expectedResult: { amount: INSUFFICIENT_FUNDS_ERROR },
|
|
|
|
},
|
2020-05-29 19:46:10 +02:00
|
|
|
'should not return insufficientFunds error if sendToken is truthy': {
|
2018-06-29 19:19:40 +02:00
|
|
|
amount: '0x0',
|
|
|
|
balance: 1,
|
|
|
|
conversionRate: 3,
|
|
|
|
gasTotal: 17,
|
|
|
|
primaryCurrency: 'ABC',
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: { address: '0x0', symbol: 'DEF', decimals: 0 },
|
2018-06-29 19:19:40 +02:00
|
|
|
decimals: 0,
|
|
|
|
tokenBalance: 'sometokenbalance',
|
|
|
|
expectedResult: { amount: null },
|
|
|
|
},
|
2018-05-05 17:11:53 +02:00
|
|
|
'should return insufficientTokens error if token is selected and isTokenBalanceSufficient returns false': {
|
|
|
|
amount: '0x10',
|
|
|
|
balance: 100,
|
|
|
|
conversionRate: 3,
|
|
|
|
decimals: 10,
|
|
|
|
gasTotal: 17,
|
|
|
|
primaryCurrency: 'ABC',
|
2020-05-29 19:46:10 +02:00
|
|
|
sendToken: { address: '0x0' },
|
2018-05-05 17:11:53 +02:00
|
|
|
tokenBalance: 123,
|
|
|
|
expectedResult: { amount: INSUFFICIENT_TOKENS_ERROR },
|
|
|
|
},
|
|
|
|
}
|
2020-07-15 16:36:52 +02:00
|
|
|
Object.entries(config).forEach(([description, obj]) => {
|
2020-02-11 17:51:13 +01:00
|
|
|
it(description, function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.deepStrictEqual(getAmountErrorObject(obj), obj.expectedResult)
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('getGasFeeErrorObject()', function () {
|
2018-06-29 19:19:40 +02:00
|
|
|
const config = {
|
|
|
|
'should return insufficientFunds error if isBalanceSufficient returns false': {
|
|
|
|
balance: 16,
|
|
|
|
conversionRate: 3,
|
|
|
|
gasTotal: 17,
|
|
|
|
primaryCurrency: 'ABC',
|
|
|
|
expectedResult: { gasFee: INSUFFICIENT_FUNDS_ERROR },
|
|
|
|
},
|
|
|
|
'should return null error if isBalanceSufficient returns true': {
|
|
|
|
balance: 16,
|
|
|
|
conversionRate: 3,
|
|
|
|
gasTotal: 15,
|
|
|
|
primaryCurrency: 'ABC',
|
|
|
|
expectedResult: { gasFee: null },
|
|
|
|
},
|
|
|
|
}
|
2020-07-15 16:36:52 +02:00
|
|
|
Object.entries(config).forEach(([description, obj]) => {
|
2020-02-11 17:51:13 +01:00
|
|
|
it(description, function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.deepStrictEqual(getGasFeeErrorObject(obj), obj.expectedResult)
|
2018-06-29 19:19:40 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('calcTokenBalance()', function () {
|
2020-07-20 19:02:49 +02:00
|
|
|
it('should return the calculated token balance', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(
|
2020-11-03 00:41:28 +01:00
|
|
|
calcTokenBalance({
|
|
|
|
sendToken: {
|
|
|
|
address: '0x0',
|
|
|
|
decimals: 11,
|
|
|
|
},
|
|
|
|
usersToken: {
|
|
|
|
balance: 20,
|
|
|
|
},
|
|
|
|
}),
|
|
|
|
'calc:2011',
|
|
|
|
)
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('isBalanceSufficient()', function () {
|
|
|
|
it('should correctly call addCurrencies and return the result of calling conversionGTE', function () {
|
2018-05-05 17:11:53 +02:00
|
|
|
stubs.conversionGTE.resetHistory()
|
|
|
|
const result = isBalanceSufficient({
|
|
|
|
amount: 15,
|
|
|
|
balance: 100,
|
|
|
|
conversionRate: 3,
|
|
|
|
gasTotal: 17,
|
|
|
|
primaryCurrency: 'ABC',
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.deepStrictEqual(stubs.addCurrencies.getCall(0).args, [
|
2020-11-03 00:41:28 +01:00
|
|
|
15,
|
|
|
|
17,
|
|
|
|
{
|
|
|
|
aBase: 16,
|
|
|
|
bBase: 16,
|
|
|
|
toNumericBase: 'hex',
|
|
|
|
},
|
|
|
|
])
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.deepStrictEqual(stubs.conversionGTE.getCall(0).args, [
|
2020-11-03 00:41:28 +01:00
|
|
|
{
|
|
|
|
value: 100,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
fromCurrency: 'ABC',
|
|
|
|
conversionRate: 3,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 32,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
conversionRate: 3,
|
|
|
|
fromCurrency: 'ABC',
|
|
|
|
},
|
|
|
|
])
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(result, true)
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('isTokenBalanceSufficient()', function () {
|
|
|
|
it('should correctly call conversionUtil and return the result of calling conversionGTE', function () {
|
2018-05-05 17:11:53 +02:00
|
|
|
stubs.conversionGTE.resetHistory()
|
2018-06-29 19:19:40 +02:00
|
|
|
stubs.conversionUtil.resetHistory()
|
2018-05-05 17:11:53 +02:00
|
|
|
const result = isTokenBalanceSufficient({
|
|
|
|
amount: '0x10',
|
|
|
|
tokenBalance: 123,
|
|
|
|
decimals: 10,
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.deepStrictEqual(stubs.conversionUtil.getCall(0).args, [
|
2020-11-03 00:41:28 +01:00
|
|
|
'0x10',
|
|
|
|
{
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
},
|
|
|
|
])
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.deepStrictEqual(stubs.conversionGTE.getCall(0).args, [
|
2020-11-03 00:41:28 +01:00
|
|
|
{
|
|
|
|
value: 123,
|
|
|
|
fromNumericBase: 'hex',
|
|
|
|
},
|
|
|
|
{
|
|
|
|
value: 'calc:1610',
|
|
|
|
},
|
|
|
|
])
|
2018-05-05 17:11:53 +02:00
|
|
|
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(result, false)
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2020-10-13 23:13:54 +02:00
|
|
|
describe('estimateGasForSend', function () {
|
2018-05-22 17:16:53 +02:00
|
|
|
const baseMockParams = {
|
|
|
|
blockGasLimit: '0x64',
|
|
|
|
selectedAddress: 'mockAddress',
|
|
|
|
to: '0xisContract',
|
2020-11-03 00:41:28 +01:00
|
|
|
estimateGasMethod: sinon.stub().callsFake(({ to }) => {
|
|
|
|
if (typeof to === 'string' && to.match(/willFailBecauseOf:/u)) {
|
|
|
|
throw new Error(to.match(/:(.+)$/u)[1])
|
|
|
|
}
|
|
|
|
return { toString: (n) => `0xabc${n}` }
|
|
|
|
}),
|
2018-05-22 17:16:53 +02:00
|
|
|
}
|
|
|
|
const baseExpectedCall = {
|
|
|
|
from: 'mockAddress',
|
|
|
|
gas: '0x64x0.95',
|
|
|
|
to: '0xisContract',
|
2019-02-25 19:16:23 +01:00
|
|
|
value: '0xff',
|
2018-05-22 17:16:53 +02:00
|
|
|
}
|
2018-05-22 10:10:06 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
beforeEach(function () {
|
2018-05-23 18:43:25 +02:00
|
|
|
global.eth = {
|
2020-11-03 00:41:28 +01:00
|
|
|
getCode: sinon
|
|
|
|
.stub()
|
|
|
|
.callsFake((address) =>
|
|
|
|
Promise.resolve(address.match(/isContract/u) ? 'not-0x' : '0x'),
|
|
|
|
),
|
2018-05-23 18:43:25 +02:00
|
|
|
}
|
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
afterEach(function () {
|
2018-05-23 18:43:25 +02:00
|
|
|
baseMockParams.estimateGasMethod.resetHistory()
|
|
|
|
global.eth.getCode.resetHistory()
|
2018-05-22 17:16:53 +02:00
|
|
|
})
|
|
|
|
|
2020-10-13 23:13:54 +02:00
|
|
|
it('should call ethQuery.estimateGasForSend with the expected params', async function () {
|
|
|
|
const result = await estimateGasForSend(baseMockParams)
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(baseMockParams.estimateGasMethod.callCount, 1)
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
baseMockParams.estimateGasMethod.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
gasPrice: undefined,
|
|
|
|
value: undefined,
|
|
|
|
...baseExpectedCall,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.strictEqual(result, '0xabc16')
|
2018-06-15 04:24:48 +02:00
|
|
|
})
|
|
|
|
|
2020-10-13 23:13:54 +02:00
|
|
|
it('should call ethQuery.estimateGasForSend with the expected params when initialGasLimitHex is lower than the upperGasLimit', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = await estimateGasForSend({
|
|
|
|
...baseMockParams,
|
|
|
|
blockGasLimit: '0xbcd',
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(baseMockParams.estimateGasMethod.callCount, 1)
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
baseMockParams.estimateGasMethod.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
gasPrice: undefined,
|
|
|
|
value: undefined,
|
|
|
|
...baseExpectedCall,
|
|
|
|
gas: '0xbcdx0.95',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.strictEqual(result, '0xabc16x1.5')
|
2018-05-22 17:16:53 +02:00
|
|
|
})
|
|
|
|
|
2020-10-13 23:13:54 +02:00
|
|
|
it('should call ethQuery.estimateGasForSend with a value of 0x0 and the expected data and to if passed a sendToken', async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = await estimateGasForSend({
|
|
|
|
data: 'mockData',
|
|
|
|
sendToken: { address: 'mockAddress' },
|
|
|
|
...baseMockParams,
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(baseMockParams.estimateGasMethod.callCount, 1)
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
baseMockParams.estimateGasMethod.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
...baseExpectedCall,
|
|
|
|
gasPrice: undefined,
|
|
|
|
value: '0x0',
|
|
|
|
data: '0xa9059cbb104c',
|
|
|
|
to: 'mockAddress',
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.strictEqual(result, '0xabc16')
|
2018-05-22 10:10:06 +02:00
|
|
|
})
|
|
|
|
|
2020-10-13 23:13:54 +02:00
|
|
|
it('should call ethQuery.estimateGasForSend without a recipient if the recipient is empty and data passed', async function () {
|
2018-09-27 15:46:24 +02:00
|
|
|
const data = 'mockData'
|
|
|
|
const to = ''
|
2020-10-13 23:13:54 +02:00
|
|
|
const result = await estimateGasForSend({ ...baseMockParams, data, to })
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(baseMockParams.estimateGasMethod.callCount, 1)
|
|
|
|
assert.deepStrictEqual(
|
|
|
|
baseMockParams.estimateGasMethod.getCall(0).args[0],
|
|
|
|
{
|
|
|
|
gasPrice: undefined,
|
|
|
|
value: '0xff',
|
|
|
|
data,
|
|
|
|
from: baseExpectedCall.from,
|
|
|
|
gas: baseExpectedCall.gas,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
assert.strictEqual(result, '0xabc16')
|
2018-09-27 15:46:24 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it(`should return ${SIMPLE_GAS_COST} if ethQuery.getCode does not return '0x'`, async function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(baseMockParams.estimateGasMethod.callCount, 0)
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = await estimateGasForSend({
|
|
|
|
...baseMockParams,
|
|
|
|
to: '0x123',
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(result, SIMPLE_GAS_COST)
|
2018-05-22 10:10:06 +02:00
|
|
|
})
|
2018-05-25 15:37:16 +02:00
|
|
|
|
2020-05-29 19:46:10 +02:00
|
|
|
it(`should return ${SIMPLE_GAS_COST} if not passed a sendToken or truthy to address`, async function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(baseMockParams.estimateGasMethod.callCount, 0)
|
2020-10-13 23:13:54 +02:00
|
|
|
const result = await estimateGasForSend({ ...baseMockParams, to: null })
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(result, SIMPLE_GAS_COST)
|
2018-06-18 18:25:20 +02:00
|
|
|
})
|
|
|
|
|
2020-05-29 19:46:10 +02:00
|
|
|
it(`should not return ${SIMPLE_GAS_COST} if passed a sendToken`, async function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(baseMockParams.estimateGasMethod.callCount, 0)
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = await estimateGasForSend({
|
|
|
|
...baseMockParams,
|
|
|
|
to: '0x123',
|
|
|
|
sendToken: { address: '0x0' },
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.notStrictEqual(result, SIMPLE_GAS_COST)
|
2018-06-02 06:23:01 +02:00
|
|
|
})
|
|
|
|
|
2020-05-29 19:46:10 +02:00
|
|
|
it(`should return ${BASE_TOKEN_GAS_COST} if passed a sendToken but no to address`, async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = await estimateGasForSend({
|
|
|
|
...baseMockParams,
|
|
|
|
to: null,
|
|
|
|
sendToken: { address: '0x0' },
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(result, BASE_TOKEN_GAS_COST)
|
2018-06-29 01:33:09 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it(`should return the adjusted blockGasLimit if it fails with a 'Transaction execution error.'`, async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = await estimateGasForSend({
|
|
|
|
...baseMockParams,
|
|
|
|
to: 'isContract willFailBecauseOf:Transaction execution error.',
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(result, '0x64x0.95')
|
2018-05-25 15:37:16 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it(`should return the adjusted blockGasLimit if it fails with a 'gas required exceeds allowance or always failing transaction.'`, async function () {
|
2020-11-03 00:41:28 +01:00
|
|
|
const result = await estimateGasForSend({
|
|
|
|
...baseMockParams,
|
|
|
|
to:
|
|
|
|
'isContract willFailBecauseOf:gas required exceeds allowance or always failing transaction.',
|
|
|
|
})
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(result, '0x64x0.95')
|
2018-05-25 15:37:16 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it(`should reject other errors`, async function () {
|
2018-05-25 15:37:16 +02:00
|
|
|
try {
|
2020-11-03 00:41:28 +01:00
|
|
|
await estimateGasForSend({
|
|
|
|
...baseMockParams,
|
|
|
|
to: 'isContract willFailBecauseOf:some other error',
|
|
|
|
})
|
2018-05-25 15:37:16 +02:00
|
|
|
} catch (err) {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(err.message, 'some other error')
|
2018-05-25 15:37:16 +02:00
|
|
|
}
|
|
|
|
})
|
2018-05-22 10:10:06 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('getToAddressForGasUpdate()', function () {
|
|
|
|
it('should return empty string if all params are undefined or null', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(getToAddressForGasUpdate(undefined, null), '')
|
2018-06-18 18:37:01 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should return the first string that is not defined or null in lower case', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(getToAddressForGasUpdate('A', null), 'a')
|
|
|
|
assert.strictEqual(getToAddressForGasUpdate(undefined, 'B'), 'b')
|
2018-06-18 18:37:01 +02:00
|
|
|
})
|
|
|
|
})
|
2018-07-03 02:31:57 +02:00
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
describe('removeLeadingZeroes()', function () {
|
|
|
|
it('should remove leading zeroes from int when user types', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(removeLeadingZeroes('0'), '0')
|
|
|
|
assert.strictEqual(removeLeadingZeroes('1'), '1')
|
|
|
|
assert.strictEqual(removeLeadingZeroes('00'), '0')
|
|
|
|
assert.strictEqual(removeLeadingZeroes('01'), '1')
|
2018-07-03 02:31:57 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should remove leading zeroes from int when user copy/paste', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(removeLeadingZeroes('001'), '1')
|
2018-07-03 02:31:57 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should remove leading zeroes from float when user types', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(removeLeadingZeroes('0.'), '0.')
|
|
|
|
assert.strictEqual(removeLeadingZeroes('0.0'), '0.0')
|
|
|
|
assert.strictEqual(removeLeadingZeroes('0.00'), '0.00')
|
|
|
|
assert.strictEqual(removeLeadingZeroes('0.001'), '0.001')
|
|
|
|
assert.strictEqual(removeLeadingZeroes('0.10'), '0.10')
|
2018-07-03 02:31:57 +02:00
|
|
|
})
|
|
|
|
|
2020-02-11 17:51:13 +01:00
|
|
|
it('should remove leading zeroes from float when user copy/paste', function () {
|
2020-12-03 16:46:22 +01:00
|
|
|
assert.strictEqual(removeLeadingZeroes('00.1'), '0.1')
|
2018-07-03 02:31:57 +02:00
|
|
|
})
|
|
|
|
})
|
2018-05-05 17:11:53 +02:00
|
|
|
})
|