aboutsummaryrefslogtreecommitdiffstats
path: root/test/unit/tx-utils-test.js
diff options
context:
space:
mode:
Diffstat (limited to 'test/unit/tx-utils-test.js')
-rw-r--r--test/unit/tx-utils-test.js145
1 files changed, 83 insertions, 62 deletions
diff --git a/test/unit/tx-utils-test.js b/test/unit/tx-utils-test.js
index 8ca13412e..be16225ba 100644
--- a/test/unit/tx-utils-test.js
+++ b/test/unit/tx-utils-test.js
@@ -1,77 +1,98 @@
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/lib/tx-gas-utils')
+const txUtils = require('../../app/scripts/controllers/transactions/lib/util')
describe('txUtils', function () {
- let txUtils
-
- before(function () {
- txUtils = new TxUtils(new Proxy({}, {
- get: (obj, name) => {
- return () => {}
- },
- }))
- })
+ describe('#validateTxParams', function () {
+ it('does not throw for positive values', function () {
+ var sample = {
+ from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
+ value: '0x01',
+ }
+ txUtils.validateTxParams(sample)
+ })
- 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,
+ it('returns error for negative values', function () {
+ var sample = {
+ from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
+ value: '-0x01',
+ }
+ try {
+ txUtils.validateTxParams(sample)
+ } catch (err) {
+ assert.ok(err, 'error')
}
- 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')
+ describe('#normalizeTxParams', () => {
+ it('should normalize txParams', () => {
+ let txParams = {
+ chainId: '0x1',
+ from: 'a7df1beDBF813f57096dF77FCd515f0B3900e402',
+ to: null,
+ data: '68656c6c6f20776f726c64',
+ random: 'hello world',
+ }
+
+ let normalizedTxParams = txUtils.normalizeTxParams(txParams)
+
+ assert(!normalizedTxParams.chainId, 'their should be no chainId')
+ assert(!normalizedTxParams.to, 'their should be no to address if null')
+ assert.equal(normalizedTxParams.from.slice(0, 2), '0x', 'from should be hexPrefixd')
+ assert.equal(normalizedTxParams.data.slice(0, 2), '0x', 'data should be hexPrefixd')
+ assert(!('random' in normalizedTxParams), 'their should be no random key in normalizedTxParams')
+
+ txParams.to = 'a7df1beDBF813f57096dF77FCd515f0B3900e402'
+ normalizedTxParams = txUtils.normalizeTxParams(txParams)
+ assert.equal(normalizedTxParams.to.slice(0, 2), '0x', 'to should be hexPrefixd')
+
})
+ })
- 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')
+ describe('#validateRecipient', () => {
+ it('removes recipient for txParams with 0x when contract data is provided', function () {
+ const zeroRecipientandDataTxParams = {
+ from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
+ to: '0x',
+ data: 'bytecode',
+ }
+ const sanitizedTxParams = txUtils.validateRecipient(zeroRecipientandDataTxParams)
+ assert.deepEqual(sanitizedTxParams, { from: '0x1678a085c290ebd122dc42cba69373b5953b831d', data: 'bytecode' }, 'no recipient with 0x')
})
- 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')
+ it('should error when recipient is 0x', function () {
+ const zeroRecipientTxParams = {
+ from: '0x1678a085c290ebd122dc42cba69373b5953b831d',
+ to: '0x',
+ }
+ assert.throws(() => { txUtils.validateRecipient(zeroRecipientTxParams) }, Error, 'Invalid recipient address')
})
})
-}) \ No newline at end of file
+
+
+ describe('#validateFrom', () => {
+ it('should error when from is not a hex string', function () {
+
+ // where from is undefined
+ const txParams = {}
+ assert.throws(() => { txUtils.validateFrom(txParams) }, Error, `Invalid from address ${txParams.from} not a string`)
+
+ // where from is array
+ txParams.from = []
+ assert.throws(() => { txUtils.validateFrom(txParams) }, Error, `Invalid from address ${txParams.from} not a string`)
+
+ // where from is a object
+ txParams.from = {}
+ assert.throws(() => { txUtils.validateFrom(txParams) }, Error, `Invalid from address ${txParams.from} not a string`)
+
+ // where from is a invalid address
+ txParams.from = 'im going to fail'
+ assert.throws(() => { txUtils.validateFrom(txParams) }, Error, `Invalid from address`)
+
+ // should run
+ txParams.from ='0x1678a085c290ebd122dc42cba69373b5953b831d'
+ txUtils.validateFrom(txParams)
+ })
+ })
+})