2017-03-07 20:34:11 +01:00
|
|
|
const assert = require('assert')
|
|
|
|
const ethUtil = require('ethereumjs-util')
|
|
|
|
const BN = ethUtil.BN
|
|
|
|
|
|
|
|
const TxUtils = require('../../app/scripts/lib/tx-utils')
|
|
|
|
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('txUtils', function () {
|
2017-03-07 20:34:11 +01:00
|
|
|
let txUtils
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
before(function () {
|
2017-05-23 08:12:28 +02:00
|
|
|
txUtils = new TxUtils(new Proxy({}, {
|
|
|
|
get: (obj, name) => {
|
|
|
|
return () => {}
|
|
|
|
},
|
|
|
|
}))
|
2017-03-07 20:34:11 +01:00
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('chain Id', function () {
|
|
|
|
it('prepares a transaction with the provided chainId', function () {
|
2017-03-30 23:23:23 +02:00
|
|
|
const txParams = {
|
|
|
|
to: '0x70ad465e0bab6504002ad58c744ed89c7da38524',
|
|
|
|
from: '0x69ad465e0bab6504002ad58c744ed89c7da38525',
|
|
|
|
value: '0x0',
|
|
|
|
gas: '0x7b0c',
|
|
|
|
gasPrice: '0x199c82cc00',
|
|
|
|
data: '0x',
|
|
|
|
nonce: '0x3',
|
|
|
|
chainId: 42,
|
|
|
|
}
|
|
|
|
const ethTx = txUtils.buildEthTxFromParams(txParams)
|
|
|
|
assert.equal(ethTx.getChainId(), 42, 'chainId is set from tx params')
|
|
|
|
})
|
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
describe('addGasBuffer', function () {
|
|
|
|
it('multiplies by 1.5, when within block gas limit', function () {
|
2017-03-30 08:59:42 +02:00
|
|
|
// naive estimatedGas: 0x16e360 (1.5 mil)
|
|
|
|
const inputHex = '0x16e360'
|
2017-03-08 07:28:02 +01:00
|
|
|
// dummy gas limit: 0x3d4c52 (4 mil)
|
2017-03-08 07:47:35 +01:00
|
|
|
const blockGasLimitHex = '0x3d4c52'
|
|
|
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
2017-03-08 07:51:39 +01:00
|
|
|
const inputBn = hexToBn(inputHex)
|
|
|
|
const outputBn = hexToBn(output)
|
2017-03-08 04:42:16 +01:00
|
|
|
const expectedBn = inputBn.muln(1.5)
|
|
|
|
assert(outputBn.eq(expectedBn), 'returns 1.5 the input value')
|
2017-03-07 20:34:11 +01:00
|
|
|
})
|
2017-05-04 23:35:10 +02:00
|
|
|
|
|
|
|
it('uses original estimatedGas, when above block gas limit', function () {
|
2017-03-30 08:59:42 +02:00
|
|
|
// naive estimatedGas: 0x16e360 (1.5 mil)
|
|
|
|
const inputHex = '0x16e360'
|
2017-03-08 07:28:02 +01:00
|
|
|
// dummy gas limit: 0x0f4240 (1 mil)
|
2017-03-08 07:47:35 +01:00
|
|
|
const blockGasLimitHex = '0x0f4240'
|
|
|
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
2017-05-04 23:35:10 +02:00
|
|
|
// const inputBn = hexToBn(inputHex)
|
2017-03-08 07:51:39 +01:00
|
|
|
const outputBn = hexToBn(output)
|
|
|
|
const expectedBn = hexToBn(inputHex)
|
2017-03-08 07:28:02 +01:00
|
|
|
assert(outputBn.eq(expectedBn), 'returns the original estimatedGas value')
|
|
|
|
})
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
it('buffers up to reccomend gas limit reccomended ceiling', function () {
|
2017-03-30 08:59:42 +02:00
|
|
|
// naive estimatedGas: 0x16e360 (1.5 mil)
|
|
|
|
const inputHex = '0x16e360'
|
2017-03-08 07:28:02 +01:00
|
|
|
// dummy gas limit: 0x1e8480 (2 mil)
|
2017-03-08 07:47:35 +01:00
|
|
|
const blockGasLimitHex = '0x1e8480'
|
2017-03-30 08:59:42 +02:00
|
|
|
const blockGasLimitBn = hexToBn(blockGasLimitHex)
|
|
|
|
const ceilGasLimitBn = blockGasLimitBn.muln(0.9)
|
2017-03-08 07:47:35 +01:00
|
|
|
const output = txUtils.addGasBuffer(inputHex, blockGasLimitHex)
|
2017-03-30 08:59:42 +02:00
|
|
|
// const inputBn = hexToBn(inputHex)
|
|
|
|
// const outputBn = hexToBn(output)
|
|
|
|
const expectedHex = bnToHex(ceilGasLimitBn)
|
|
|
|
assert.equal(output, expectedHex, 'returns the gas limit reccomended ceiling value')
|
2017-03-08 07:28:02 +01:00
|
|
|
})
|
2017-03-07 20:34:11 +01:00
|
|
|
})
|
2017-03-08 02:49:40 +01:00
|
|
|
})
|
2017-03-08 07:51:39 +01:00
|
|
|
|
|
|
|
// util
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
function hexToBn (inputHex) {
|
2017-03-08 07:51:39 +01:00
|
|
|
return new BN(ethUtil.stripHexPrefix(inputHex), 16)
|
2017-03-30 08:59:42 +02:00
|
|
|
}
|
|
|
|
|
2017-05-04 23:35:10 +02:00
|
|
|
function bnToHex (inputBn) {
|
2017-03-30 08:59:42 +02:00
|
|
|
return ethUtil.addHexPrefix(inputBn.toString(16))
|
2017-05-04 23:35:10 +02:00
|
|
|
}
|