aboutsummaryrefslogtreecommitdiffstats
path: root/test
diff options
context:
space:
mode:
authorkumavis <kumavis@users.noreply.github.com>2017-09-28 07:04:51 +0800
committerGitHub <noreply@github.com>2017-09-28 07:04:51 +0800
commit15b2823e546cfefd7e867f078b4385ddc6be3a0f (patch)
tree448a321264f3d648d6edee7e90c6f487bc31084b /test
parent734490c58c25587a247e48eea086880bcb6a14fe (diff)
parentecf909e140b2fc99afbd15f6f0882dd17e3ecb88 (diff)
downloadtangerine-wallet-browser-15b2823e546cfefd7e867f078b4385ddc6be3a0f.tar.gz
tangerine-wallet-browser-15b2823e546cfefd7e867f078b4385ddc6be3a0f.tar.zst
tangerine-wallet-browser-15b2823e546cfefd7e867f078b4385ddc6be3a0f.zip
Merge branch 'master' into new-currency-test
Diffstat (limited to 'test')
-rw-r--r--test/lib/mock-encryptor.js4
-rw-r--r--test/unit/components/pending-tx-test.js8
-rw-r--r--test/unit/keyring-controller-test.js164
-rw-r--r--test/unit/network-contoller-test.js7
-rw-r--r--test/unit/pending-balance-test.js93
-rw-r--r--test/unit/pending-tx-test.js24
-rw-r--r--test/unit/tx-controller-test.js438
-rw-r--r--test/unit/tx-state-manager-test.js241
-rw-r--r--test/unit/tx-utils-test.js6
9 files changed, 581 insertions, 404 deletions
diff --git a/test/lib/mock-encryptor.js b/test/lib/mock-encryptor.js
index cdf13c507..ef229a82f 100644
--- a/test/lib/mock-encryptor.js
+++ b/test/lib/mock-encryptor.js
@@ -29,4 +29,8 @@ module.exports = {
return 'WHADDASALT!'
},
+ getRandomValues () {
+ return 'SOO RANDO!!!1'
+ }
+
}
diff --git a/test/unit/components/pending-tx-test.js b/test/unit/components/pending-tx-test.js
index 22a98bc93..20feba2a3 100644
--- a/test/unit/components/pending-tx-test.js
+++ b/test/unit/components/pending-tx-test.js
@@ -24,7 +24,8 @@ describe('PendingTx', function () {
'to': '0xc5b8dbac4c1d3f152cdeb400e2313f309c410acb',
'value': '0xde0b6b3a7640000',
gasPrice,
- 'gas': '0x7b0c'},
+ 'gas': '0x7b0c',
+ },
'gasLimitSpecified': false,
'estimatedGas': '0x5208',
}
@@ -34,10 +35,15 @@ describe('PendingTx', function () {
const renderer = ReactTestUtils.createRenderer()
const newGasPrice = '0x77359400'
+ const computedBalances = {}
+ computedBalances[Object.keys(identities)[0]] = {
+ ethBalance: '0x00000000000000056bc75e2d63100000',
+ }
const props = {
identities,
accounts: identities,
txData,
+ computedBalances,
sendTransaction: (txMeta, event) => {
// Assert changes:
const result = ethUtil.addHexPrefix(txMeta.txParams.gasPrice)
diff --git a/test/unit/keyring-controller-test.js b/test/unit/keyring-controller-test.js
deleted file mode 100644
index 8d0d75f12..000000000
--- a/test/unit/keyring-controller-test.js
+++ /dev/null
@@ -1,164 +0,0 @@
-const assert = require('assert')
-const KeyringController = require('../../app/scripts/keyring-controller')
-const configManagerGen = require('../lib/mock-config-manager')
-const ethUtil = require('ethereumjs-util')
-const BN = ethUtil.BN
-const mockEncryptor = require('../lib/mock-encryptor')
-const sinon = require('sinon')
-
-describe('KeyringController', function () {
- let keyringController
- const password = 'password123'
- const seedWords = 'puzzle seed penalty soldier say clay field arctic metal hen cage runway'
- const addresses = ['eF35cA8EbB9669A35c31b5F6f249A9941a812AC1'.toLowerCase()]
- const accounts = []
- // let originalKeystore
-
- beforeEach(function (done) {
- this.sinon = sinon.sandbox.create()
- window.localStorage = {} // Hacking localStorage support into JSDom
-
- keyringController = new KeyringController({
- configManager: configManagerGen(),
- txManager: {
- getTxList: () => [],
- getUnapprovedTxList: () => [],
- },
- ethStore: {
- addAccount (acct) { accounts.push(ethUtil.addHexPrefix(acct)) },
- },
- encryptor: mockEncryptor,
- })
-
- keyringController.createNewVaultAndKeychain(password)
- .then(function (newState) {
- newState
- done()
- })
- .catch((err) => {
- done(err)
- })
- })
-
- afterEach(function () {
- // Cleanup mocks
- this.sinon.restore()
- })
-
- describe('#createNewVaultAndKeychain', function () {
- this.timeout(10000)
-
- it('should set a vault on the configManager', function (done) {
- keyringController.store.updateState({ vault: null })
- assert(!keyringController.store.getState().vault, 'no previous vault')
- keyringController.createNewVaultAndKeychain(password)
- .then(() => {
- const vault = keyringController.store.getState().vault
- assert(vault, 'vault created')
- done()
- })
- .catch((reason) => {
- done(reason)
- })
- })
- })
-
- describe('#restoreKeyring', function () {
- it(`should pass a keyring's serialized data back to the correct type.`, function (done) {
- const mockSerialized = {
- type: 'HD Key Tree',
- data: {
- mnemonic: seedWords,
- numberOfAccounts: 1,
- },
- }
- const mock = this.sinon.mock(keyringController)
-
- mock.expects('getBalanceAndNickname')
- .exactly(1)
-
- keyringController.restoreKeyring(mockSerialized)
- .then((keyring) => {
- assert.equal(keyring.wallets.length, 1, 'one wallet restored')
- return keyring.getAccounts()
- })
- .then((accounts) => {
- assert.equal(accounts[0], addresses[0])
- mock.verify()
- done()
- })
- .catch((reason) => {
- done(reason)
- })
- })
- })
-
- describe('#createNickname', function () {
- it('should add the address to the identities hash', function () {
- const fakeAddress = '0x12345678'
- keyringController.createNickname(fakeAddress)
- const identities = keyringController.memStore.getState().identities
- const identity = identities[fakeAddress]
- assert.equal(identity.address, fakeAddress)
- })
- })
-
- describe('#saveAccountLabel', function () {
- it('sets the nickname', function (done) {
- const account = addresses[0]
- var nick = 'Test nickname'
- const identities = keyringController.memStore.getState().identities
- identities[ethUtil.addHexPrefix(account)] = {}
- keyringController.memStore.updateState({ identities })
- keyringController.saveAccountLabel(account, nick)
- .then((label) => {
- try {
- assert.equal(label, nick)
- const persisted = keyringController.store.getState().walletNicknames[account]
- assert.equal(persisted, nick)
- done()
- } catch (err) {
- done()
- }
- })
- .catch((reason) => {
- done(reason)
- })
- })
- })
-
- describe('#getAccounts', function () {
- it('returns the result of getAccounts for each keyring', function (done) {
- keyringController.keyrings = [
- { getAccounts () { return Promise.resolve([1, 2, 3]) } },
- { getAccounts () { return Promise.resolve([4, 5, 6]) } },
- ]
-
- keyringController.getAccounts()
- .then((result) => {
- assert.deepEqual(result, [1, 2, 3, 4, 5, 6])
- done()
- })
- })
- })
-
- describe('#addGasBuffer', function () {
- it('adds 100k gas buffer to estimates', function () {
- const gas = '0x04ee59' // Actual estimated gas example
- const tooBigOutput = '0x80674f9' // Actual bad output
- const bnGas = new BN(ethUtil.stripHexPrefix(gas), 16)
- const correctBuffer = new BN('100000', 10)
- const correct = bnGas.add(correctBuffer)
-
- // const tooBig = new BN(tooBigOutput, 16)
- const result = keyringController.addGasBuffer(gas)
- const bnResult = new BN(ethUtil.stripHexPrefix(result), 16)
-
- assert.equal(result.indexOf('0x'), 0, 'included hex prefix')
- assert(bnResult.gt(bnGas), 'Estimate increased in value.')
- assert.equal(bnResult.sub(bnGas).toString(10), '100000', 'added 100k gas')
- assert.equal(result, '0x' + correct.toString(16), 'Added the right amount')
- assert.notEqual(result, tooBigOutput, 'not that bad estimate')
- })
- })
-})
diff --git a/test/unit/network-contoller-test.js b/test/unit/network-contoller-test.js
index 87c2ee7a3..0b3b5adeb 100644
--- a/test/unit/network-contoller-test.js
+++ b/test/unit/network-contoller-test.js
@@ -20,9 +20,9 @@ describe('# Network Controller', function () {
describe('#provider', function () {
it('provider should be updatable without reassignment', function () {
networkController.initializeProvider(networkControllerProviderInit, dummyProviderConstructor)
- const provider = networkController.provider
- networkController._provider = {test: true}
- assert.ok(provider.test)
+ const proxy = networkController._proxy
+ proxy.setTarget({ test: true, on: () => {} })
+ assert.ok(proxy.test)
})
})
describe('#getNetworkState', function () {
@@ -71,6 +71,7 @@ function dummyProviderConstructor() {
// provider
sendAsync: noop,
// block tracker
+ _blockTracker: {},
start: noop,
stop: noop,
on: noop,
diff --git a/test/unit/pending-balance-test.js b/test/unit/pending-balance-test.js
new file mode 100644
index 000000000..5048d487b
--- /dev/null
+++ b/test/unit/pending-balance-test.js
@@ -0,0 +1,93 @@
+const assert = require('assert')
+const PendingBalanceCalculator = require('../../app/scripts/lib/pending-balance-calculator')
+const MockTxGen = require('../lib/mock-tx-gen')
+const BN = require('ethereumjs-util').BN
+let providerResultStub = {}
+
+const zeroBn = new BN(0)
+const etherBn = new BN(String(1e18))
+const ether = '0x' + etherBn.toString(16)
+
+describe('PendingBalanceCalculator', function () {
+ let balanceCalculator
+
+ describe('#calculateMaxCost(tx)', function () {
+ it('returns a BN for a given tx value', function () {
+ const txGen = new MockTxGen()
+ pendingTxs = txGen.generate({
+ status: 'submitted',
+ txParams: {
+ value: ether,
+ gasPrice: '0x0',
+ gas: '0x0',
+ }
+ }, { count: 1 })
+
+ const balanceCalculator = generateBalanceCalcWith([], zeroBn)
+ const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
+ assert.equal(result.toString(), etherBn.toString(), 'computes one ether')
+ })
+
+ it('calculates gas costs as well', function () {
+ const txGen = new MockTxGen()
+ pendingTxs = txGen.generate({
+ status: 'submitted',
+ txParams: {
+ value: '0x0',
+ gasPrice: '0x2',
+ gas: '0x3',
+ }
+ }, { count: 1 })
+
+ const balanceCalculator = generateBalanceCalcWith([], zeroBn)
+ const result = balanceCalculator.calculateMaxCost(pendingTxs[0])
+ assert.equal(result.toString(), '6', 'computes 6 wei of gas')
+ })
+ })
+
+ describe('if you have no pending txs and one ether', function () {
+
+ beforeEach(function () {
+ balanceCalculator = generateBalanceCalcWith([], etherBn)
+ })
+
+ it('returns the network balance', async function () {
+ const result = await balanceCalculator.getBalance()
+ assert.equal(result, ether, `gave ${result} needed ${ether}`)
+ })
+ })
+
+ describe('if you have a one ether pending tx and one ether', function () {
+ beforeEach(function () {
+ const txGen = new MockTxGen()
+ pendingTxs = txGen.generate({
+ status: 'submitted',
+ txParams: {
+ value: ether,
+ gasPrice: '0x0',
+ gas: '0x0',
+ }
+ }, { count: 1 })
+
+ balanceCalculator = generateBalanceCalcWith(pendingTxs, etherBn)
+ })
+
+ it('returns the subtracted result', async function () {
+ const result = await balanceCalculator.getBalance()
+ assert.equal(result, '0x0', `gave ${result} needed '0x0'`)
+ return true
+ })
+
+ })
+})
+
+function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
+ const getPendingTransactions = async () => transactions
+ const getBalance = async () => providerStub
+
+ return new PendingBalanceCalculator({
+ getBalance,
+ getPendingTransactions,
+ })
+}
+
diff --git a/test/unit/pending-tx-test.js b/test/unit/pending-tx-test.js
index 8c6d287f8..1af464656 100644
--- a/test/unit/pending-tx-test.js
+++ b/test/unit/pending-tx-test.js
@@ -62,7 +62,7 @@ describe('PendingTransactionTracker', function () {
it('should emit \'txFailed\' if the txMeta does not have a hash', function (done) {
const block = Proxy.revocable({}, {}).revoke()
pendingTxTracker.getPendingTransactions = () => [txMetaNoHash]
- pendingTxTracker.once('txFailed', (txId, err) => {
+ pendingTxTracker.once('tx:failed', (txId, err) => {
assert(txId, txMetaNoHash.id, 'should pass txId')
done()
})
@@ -71,11 +71,11 @@ describe('PendingTransactionTracker', function () {
it('should emit \'txConfirmed\' if the tx is in the block', function (done) {
const block = { transactions: [txMeta]}
pendingTxTracker.getPendingTransactions = () => [txMeta]
- pendingTxTracker.once('txConfirmed', (txId) => {
+ pendingTxTracker.once('tx:confirmed', (txId) => {
assert(txId, txMeta.id, 'should pass txId')
done()
})
- pendingTxTracker.once('txFailed', (_, err) => { done(err) })
+ pendingTxTracker.once('tx:failed', (_, err) => { done(err) })
pendingTxTracker.checkForTxInBlock(block)
})
})
@@ -84,14 +84,14 @@ describe('PendingTransactionTracker', function () {
let newBlock, oldBlock
newBlock = { number: '0x01' }
pendingTxTracker._checkPendingTxs = done
- pendingTxTracker.queryPendingTxs({oldBlock, newBlock})
+ pendingTxTracker.queryPendingTxs({ oldBlock, newBlock })
})
it('should call #_checkPendingTxs if oldBlock and the newBlock have a diff of greater then 1', function (done) {
let newBlock, oldBlock
oldBlock = { number: '0x01' }
newBlock = { number: '0x03' }
pendingTxTracker._checkPendingTxs = done
- pendingTxTracker.queryPendingTxs({oldBlock, newBlock})
+ pendingTxTracker.queryPendingTxs({ oldBlock, newBlock })
})
it('should not call #_checkPendingTxs if oldBlock and the newBlock have a diff of 1 or less', function (done) {
let newBlock, oldBlock
@@ -101,14 +101,14 @@ describe('PendingTransactionTracker', function () {
const err = new Error('should not call #_checkPendingTxs if oldBlock and the newBlock have a diff of 1 or less')
done(err)
}
- pendingTxTracker.queryPendingTxs({oldBlock, newBlock})
+ pendingTxTracker.queryPendingTxs({ oldBlock, newBlock })
done()
})
})
describe('#_checkPendingTx', function () {
it('should emit \'txFailed\' if the txMeta does not have a hash', function (done) {
- pendingTxTracker.once('txFailed', (txId, err) => {
+ pendingTxTracker.once('tx:failed', (txId, err) => {
assert(txId, txMetaNoHash.id, 'should pass txId')
done()
})
@@ -122,11 +122,11 @@ describe('PendingTransactionTracker', function () {
it('should emit \'txConfirmed\'', function (done) {
providerResultStub.eth_getTransactionByHash = {blockNumber: '0x01'}
- pendingTxTracker.once('txConfirmed', (txId) => {
+ pendingTxTracker.once('tx:confirmed', (txId) => {
assert(txId, txMeta.id, 'should pass txId')
done()
})
- pendingTxTracker.once('txFailed', (_, err) => { done(err) })
+ pendingTxTracker.once('tx:failed', (_, err) => { done(err) })
pendingTxTracker._checkPendingTx(txMeta)
})
})
@@ -188,7 +188,7 @@ describe('PendingTransactionTracker', function () {
]
const enoughForAllErrors = txList.concat(txList)
- pendingTxTracker.on('txFailed', (_, err) => done(err))
+ pendingTxTracker.on('tx:failed', (_, err) => done(err))
pendingTxTracker.getPendingTransactions = () => enoughForAllErrors
pendingTxTracker._resubmitTx = async (tx) => {
@@ -202,7 +202,7 @@ describe('PendingTransactionTracker', function () {
pendingTxTracker.resubmitPendingTxs()
})
it('should emit \'txFailed\' if it encountered a real error', function (done) {
- pendingTxTracker.once('txFailed', (id, err) => err.message === 'im some real error' ? txList[id - 1].resolve() : done(err))
+ pendingTxTracker.once('tx:failed', (id, err) => err.message === 'im some real error' ? txList[id - 1].resolve() : done(err))
pendingTxTracker.getPendingTransactions = () => txList
pendingTxTracker._resubmitTx = async (tx) => { throw new TypeError('im some real error') }
@@ -226,7 +226,7 @@ describe('PendingTransactionTracker', function () {
// Stubbing out current account state:
// Adding the fake tx:
- pendingTxTracker.once('txFailed', (txId, err) => {
+ pendingTxTracker.once('tx:failed', (txId, err) => {
assert(err, 'Should have a error')
done()
})
diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js
index 7bb193242..66772ff88 100644
--- a/test/unit/tx-controller-test.js
+++ b/test/unit/tx-controller-test.js
@@ -2,21 +2,19 @@ const assert = require('assert')
const ethUtil = require('ethereumjs-util')
const EthTx = require('ethereumjs-tx')
const ObservableStore = require('obs-store')
-const clone = require('clone')
const sinon = require('sinon')
const TransactionController = require('../../app/scripts/controllers/transactions')
-const TxProvideUtils = require('../../app/scripts/lib/tx-utils')
-const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper')
+const TxGasUtils = require('../../app/scripts/lib/tx-gas-utils')
+const { createStubedProvider } = require('../stub/provider')
const noop = () => true
const currentNetworkId = 42
const otherNetworkId = 36
const privKey = new Buffer('8718b9618a37d1fc78c436511fc6df3c8258d3250635bba617f33003270ec03e', 'hex')
-const { createStubedProvider } = require('../stub/provider')
describe('Transaction Controller', function () {
- let txController, engine, provider, providerResultStub
+ let txController, provider, providerResultStub
beforeEach(function () {
providerResultStub = {}
@@ -27,34 +25,97 @@ describe('Transaction Controller', function () {
networkStore: new ObservableStore(currentNetworkId),
txHistoryLimit: 10,
blockTracker: { getCurrentBlock: noop, on: noop, once: noop },
- ethStore: { getState: noop },
+ accountTracker: { store: { getState: noop } },
signTransaction: (ethTx) => new Promise((resolve) => {
ethTx.sign(privKey)
resolve()
}),
})
txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop })
- txController.txProviderUtils = new TxProvideUtils(txController.provider)
+ txController.txProviderUtils = new TxGasUtils(txController.provider)
+ })
+
+ describe('#getState', function () {
+ it('should return a state object with the right keys and datat types', function () {
+ const exposedState = txController.getState()
+ assert('unapprovedTxs' in exposedState, 'state should have the key unapprovedTxs')
+ assert('selectedAddressTxList' in exposedState, 'state should have the key selectedAddressTxList')
+ assert(typeof exposedState.unapprovedTxs === 'object', 'should be an object')
+ assert(Array.isArray(exposedState.selectedAddressTxList), 'should be an array')
+ })
+ })
+
+ describe('#getUnapprovedTxCount', function () {
+ it('should return the number of unapproved txs', function () {
+ txController.txStateManager._saveTxList([
+ { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 2, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 3, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} },
+ ])
+ const unapprovedTxCount = txController.getUnapprovedTxCount()
+ assert.equal(unapprovedTxCount, 3, 'should be 3')
+ })
})
+ describe('#getPendingTxCount', function () {
+ it('should return the number of pending txs', function () {
+ txController.txStateManager._saveTxList([
+ { id: 1, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 2, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 3, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {} },
+ ])
+ const pendingTxCount = txController.getPendingTxCount()
+ assert.equal(pendingTxCount, 3, 'should be 3')
+ })
+ })
+
+ describe('#getConfirmedTransactions', function () {
+ let address
+ beforeEach(function () {
+ address = '0xc684832530fcbddae4b4230a47e991ddcec2831d'
+ const txParams = {
+ 'from': address,
+ 'to': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ }
+ txController.txStateManager._saveTxList([
+ {id: 0, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams},
+ {id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams},
+ {id: 2, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams},
+ {id: 3, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams},
+ {id: 4, status: 'rejected', metamaskNetworkId: currentNetworkId, txParams},
+ {id: 5, status: 'approved', metamaskNetworkId: currentNetworkId, txParams},
+ {id: 6, status: 'signed', metamaskNetworkId: currentNetworkId, txParams},
+ {id: 7, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams},
+ {id: 8, status: 'failed', metamaskNetworkId: currentNetworkId, txParams},
+ ])
+ })
+
+ it('should return the number of confirmed txs', function () {
+ assert.equal(txController.nonceTracker.getConfirmedTransactions(address).length, 3)
+ })
+ })
+
+
describe('#newUnapprovedTransaction', function () {
let stub, txMeta, txParams
beforeEach(function () {
txParams = {
- 'from':'0xc684832530fcbddae4b4230a47e991ddcec2831d',
- 'to':'0xc684832530fcbddae4b4230a47e991ddcec2831d',
- },
+ 'from': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ 'to': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ }
txMeta = {
status: 'unapproved',
id: 1,
metamaskNetworkId: currentNetworkId,
txParams,
+ history: [],
}
- txController.addTx(txMeta)
- stub = sinon.stub(txController, 'addUnapprovedTransaction').returns(Promise.resolve(txMeta))
+ txController.txStateManager._saveTxList([txMeta])
+ stub = sinon.stub(txController, 'addUnapprovedTransaction').returns(Promise.resolve(txController.txStateManager.addTx(txMeta)))
})
afterEach(function () {
+ txController.txStateManager._saveTxList([])
stub.restore()
})
@@ -72,7 +133,7 @@ describe('Transaction Controller', function () {
txController.once('newUnaprovedTx', (txMetaFromEmit) => {
setTimeout(() => {
txController.setTxHash(txMetaFromEmit.id, '0x0')
- txController.setTxStatusSubmitted(txMetaFromEmit.id)
+ txController.txStateManager.setTxStatusSubmitted(txMetaFromEmit.id)
}, 10)
})
@@ -87,7 +148,7 @@ describe('Transaction Controller', function () {
it('should reject when finished and status is rejected', function (done) {
txController.once('newUnaprovedTx', (txMetaFromEmit) => {
setTimeout(() => {
- txController.setTxStatusRejected(txMetaFromEmit.id)
+ txController.txStateManager.setTxStatusRejected(txMetaFromEmit.id)
}, 10)
})
@@ -110,7 +171,7 @@ describe('Transaction Controller', function () {
assert(('txParams' in txMeta), 'should have a txParams')
assert(('history' in txMeta), 'should have a history')
- const memTxMeta = txController.getTx(txMeta.id)
+ const memTxMeta = txController.txStateManager.getTx(txMeta.id)
assert.deepEqual(txMeta, memTxMeta, `txMeta should be stored in txController after adding it\n expected: ${txMeta} \n got: ${memTxMeta}`)
addTxDefaultsStub.restore()
done()
@@ -120,10 +181,10 @@ describe('Transaction Controller', function () {
describe('#addTxDefaults', function () {
it('should add the tx defaults if their are none', function (done) {
- let txMeta = {
+ const txMeta = {
'txParams': {
- 'from':'0xc684832530fcbddae4b4230a47e991ddcec2831d',
- 'to':'0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ 'from': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ 'to': '0xc684832530fcbddae4b4230a47e991ddcec2831d',
},
}
providerResultStub.eth_gasPrice = '4a817c800'
@@ -131,7 +192,7 @@ describe('Transaction Controller', function () {
providerResultStub.eth_estimateGas = '5209'
txController.addTxDefaults(txMeta)
.then((txMetaWithDefaults) => {
- assert(txMetaWithDefaults.txParams.value, '0x0','should have added 0x0 as the value')
+ assert(txMetaWithDefaults.txParams.value, '0x0', 'should have added 0x0 as the value')
assert(txMetaWithDefaults.txParams.gasPrice, 'should have added the gas price')
assert(txMetaWithDefaults.txParams.gas, 'should have added the gas field')
done()
@@ -163,214 +224,31 @@ describe('Transaction Controller', function () {
})
})
- describe('#getTxList', function () {
- it('when new should return empty array', function () {
- var result = txController.getTxList()
- assert.ok(Array.isArray(result))
- assert.equal(result.length, 0)
- })
- })
-
describe('#addTx', function () {
- it('adds a tx returned in getTxList', function () {
- var tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
- txController.addTx(tx, noop)
- var result = txController.getTxList()
- assert.ok(Array.isArray(result))
- assert.equal(result.length, 1)
- assert.equal(result[0].id, 1)
- })
-
- it('does not override txs from other networks', function () {
- var tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
- var tx2 = { id: 2, status: 'confirmed', metamaskNetworkId: otherNetworkId, txParams: {} }
- txController.addTx(tx, noop)
- txController.addTx(tx2, noop)
- var result = txController.getFullTxList()
- var result2 = txController.getTxList()
- assert.equal(result.length, 2, 'txs were deleted')
- assert.equal(result2.length, 1, 'incorrect number of txs on network.')
- })
-
- it('cuts off early txs beyond a limit', function () {
- const limit = txController.txHistoryLimit
- for (let i = 0; i < limit + 1; i++) {
- const tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
- txController.addTx(tx, noop)
- }
- var result = txController.getTxList()
- assert.equal(result.length, limit, `limit of ${limit} txs enforced`)
- assert.equal(result[0].id, 1, 'early txs truncted')
- })
-
- it('cuts off early txs beyond a limit whether or not it is confirmed or rejected', function () {
- const limit = txController.txHistoryLimit
- for (let i = 0; i < limit + 1; i++) {
- const tx = { id: i, time: new Date(), status: 'rejected', metamaskNetworkId: currentNetworkId, txParams: {} }
- txController.addTx(tx, noop)
- }
- var result = txController.getTxList()
- assert.equal(result.length, limit, `limit of ${limit} txs enforced`)
- assert.equal(result[0].id, 1, 'early txs truncted')
- })
-
- it('cuts off early txs beyond a limit but does not cut unapproved txs', function () {
- var unconfirmedTx = { id: 0, time: new Date(), status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
- txController.addTx(unconfirmedTx, noop)
- const limit = txController.txHistoryLimit
- for (let i = 1; i < limit + 1; i++) {
- const tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
- txController.addTx(tx, noop)
- }
- var result = txController.getTxList()
- assert.equal(result.length, limit, `limit of ${limit} txs enforced`)
- assert.equal(result[0].id, 0, 'first tx should still be there')
- assert.equal(result[0].status, 'unapproved', 'first tx should be unapproved')
- assert.equal(result[1].id, 2, 'early txs truncted')
- })
- })
-
- describe('#setTxStatusSigned', function () {
- it('sets the tx status to signed', function () {
- var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
- txController.addTx(tx, noop)
- txController.setTxStatusSigned(1)
- var result = txController.getTxList()
- assert.ok(Array.isArray(result))
- assert.equal(result.length, 1)
- assert.equal(result[0].status, 'signed')
- })
-
- it('should emit a signed event to signal the exciton of callback', (done) => {
- this.timeout(10000)
- var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
- const noop = function () {
- assert(true, 'event listener has been triggered and noop executed')
- done()
- }
- txController.addTx(tx)
- txController.on('1:signed', noop)
- txController.setTxStatusSigned(1)
- })
- })
-
- describe('#setTxStatusRejected', function () {
- it('sets the tx status to rejected', function () {
- var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
- txController.addTx(tx)
- txController.setTxStatusRejected(1)
- var result = txController.getTxList()
- assert.ok(Array.isArray(result))
- assert.equal(result.length, 1)
- assert.equal(result[0].status, 'rejected')
- })
-
- it('should emit a rejected event to signal the exciton of callback', (done) => {
- this.timeout(10000)
- var tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
- txController.addTx(tx)
- const noop = function (err, txId) {
- assert(true, 'event listener has been triggered and noop executed')
- done()
- }
- txController.on('1:rejected', noop)
- txController.setTxStatusRejected(1)
- })
- })
-
- describe('#updateTx', function () {
- it('replaces the tx with the same id', function () {
- txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
- txController.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
- const tx1 = txController.getTx('1')
- tx1.status = 'blah'
- tx1.hash = 'foo'
- txController.updateTx(tx1)
- const savedResult = txController.getTx('1')
- assert.equal(savedResult.hash, 'foo')
- })
-
- it('updates gas price and adds history items', function () {
- const originalGasPrice = '0x01'
- const desiredGasPrice = '0x02'
-
+ it('should emit updates', function (done) {
const txMeta = {
id: '1',
status: 'unapproved',
metamaskNetworkId: currentNetworkId,
- txParams: {
- gasPrice: originalGasPrice,
- },
+ txParams: {},
}
+ const eventNames = ['update:badge', '1:unapproved']
+ const listeners = []
+ eventNames.forEach((eventName) => {
+ listeners.push(new Promise((resolve) => {
+ txController.once(eventName, (arg) => {
+ resolve(arg)
+ })
+ }))
+ })
+ Promise.all(listeners)
+ .then((returnValues) => {
+ assert.deepEqual(returnValues.pop(), txMeta, 'last event 1:unapproved should return txMeta')
+ done()
+ })
+ .catch(done)
txController.addTx(txMeta)
- const updatedTx = txController.getTx('1')
- // verify tx was initialized correctly
- assert.equal(updatedTx.history.length, 1, 'one history item (initial)')
- assert.equal(Array.isArray(updatedTx.history[0]), false, 'first history item is initial state')
- assert.deepEqual(updatedTx.history[0], txStateHistoryHelper.snapshotFromTxMeta(updatedTx), 'first history item is initial state')
- // modify value and updateTx
- updatedTx.txParams.gasPrice = desiredGasPrice
- txController.updateTx(updatedTx)
- // check updated value
- const result = txController.getTx('1')
- assert.equal(result.txParams.gasPrice, desiredGasPrice, 'gas price updated')
- // validate history was updated
- assert.equal(result.history.length, 2, 'two history items (initial + diff)')
- const expectedEntry = { op: 'replace', path: '/txParams/gasPrice', value: desiredGasPrice }
- assert.deepEqual(result.history[1], [expectedEntry], 'two history items (initial + diff)')
- })
- })
-
- describe('#getUnapprovedTxList', function () {
- it('returns unapproved txs in a hash', function () {
- txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
- txController.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
- const result = txController.getUnapprovedTxList()
- assert.equal(typeof result, 'object')
- assert.equal(result['1'].status, 'unapproved')
- assert.equal(result['2'], undefined)
- })
- })
-
- describe('#getTx', function () {
- it('returns a tx with the requested id', function () {
- txController.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
- txController.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
- assert.equal(txController.getTx('1').status, 'unapproved')
- assert.equal(txController.getTx('2').status, 'confirmed')
- })
- })
-
- describe('#getFilteredTxList', function () {
- it('returns a tx with the requested data', function () {
- const txMetas = [
- { id: 0, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
- { id: 1, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
- { id: 2, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
- { id: 3, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
- { id: 4, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
- { id: 5, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
- { id: 6, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
- { id: 7, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
- { id: 8, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
- { id: 9, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
- ]
- txMetas.forEach((txMeta) => txController.addTx(txMeta, noop))
- let filterParams
-
- filterParams = { status: 'unapproved', from: '0xaa' }
- assert.equal(txController.getFilteredTxList(filterParams).length, 3, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
- filterParams = { status: 'unapproved', to: '0xaa' }
- assert.equal(txController.getFilteredTxList(filterParams).length, 2, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
- filterParams = { status: 'confirmed', from: '0xbb' }
- assert.equal(txController.getFilteredTxList(filterParams).length, 3, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
- filterParams = { status: 'confirmed' }
- assert.equal(txController.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
- filterParams = { from: '0xaa' }
- assert.equal(txController.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
- filterParams = { to: '0xaa' }
- assert.equal(txController.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
})
})
@@ -404,11 +282,11 @@ describe('Transaction Controller', function () {
const pubStub = sinon.stub(txController, 'publishTransaction').callsFake(() => {
txController.setTxHash('1', originalValue)
- txController.setTxStatusSubmitted('1')
+ txController.txStateManager.setTxStatusSubmitted('1')
})
txController.approveTransaction(txMeta.id).then(() => {
- const result = txController.getTx(txMeta.id)
+ const result = txController.txStateManager.getTx(txMeta.id)
const params = result.txParams
assert.equal(params.gas, originalValue, 'gas unmodified')
@@ -431,4 +309,120 @@ describe('Transaction Controller', function () {
}).catch(done)
})
})
-}) \ No newline at end of file
+
+ describe('#updateAndApproveTransaction', function () {
+ let txMeta
+ beforeEach(function () {
+ txMeta = {
+ id: 1,
+ status: 'unapproved',
+ txParams: {
+ from: '0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ to: '0x1678a085c290ebd122dc42cba69373b5953b831d',
+ gasPrice: '0x77359400',
+ gas: '0x7b0d',
+ nonce: '0x4b',
+ },
+ metamaskNetworkId: currentNetworkId,
+ }
+ })
+ it('should update and approve transactions', function () {
+ txController.txStateManager.addTx(txMeta)
+ txController.updateAndApproveTransaction(txMeta)
+ const tx = txController.txStateManager.getTx(1)
+ assert.equal(tx.status, 'approved')
+ })
+ })
+
+ describe('#getChainId', function () {
+ it('returns 0 when the chainId is NaN', function () {
+ txController.networkStore = new ObservableStore(NaN)
+ assert.equal(txController.getChainId(), 0)
+ })
+ })
+
+ describe('#cancelTransaction', function () {
+ beforeEach(function () {
+ txController.txStateManager._saveTxList([
+ { id: 0, status: 'unapproved', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
+ { id: 1, status: 'rejected', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
+ { id: 2, status: 'approved', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
+ { id: 3, status: 'signed', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
+ { id: 4, status: 'submitted', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
+ { id: 5, status: 'confirmed', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
+ { id: 6, status: 'failed', txParams: {}, metamaskNetworkId: currentNetworkId, history: [{}] },
+ ])
+ })
+
+ it('should set the transaction to rejected from unapproved', async function () {
+ await txController.cancelTransaction(0)
+ assert.equal(txController.txStateManager.getTx(0).status, 'rejected')
+ })
+
+ })
+
+ describe('#publishTransaction', function () {
+ let hash, txMeta
+ beforeEach(function () {
+ hash = '0x2a5523c6fa98b47b7d9b6c8320179785150b42a16bcff36b398c5062b65657e8'
+ txMeta = {
+ id: 1,
+ status: 'unapproved',
+ txParams: {},
+ metamaskNetworkId: currentNetworkId,
+ }
+ providerResultStub.eth_sendRawTransaction = hash
+ })
+
+ it('should publish a tx, updates the rawTx when provided a one', async function () {
+ txController.txStateManager.addTx(txMeta)
+ await txController.publishTransaction(txMeta.id)
+ const publishedTx = txController.txStateManager.getTx(1)
+ assert.equal(publishedTx.hash, hash)
+ assert.equal(publishedTx.status, 'submitted')
+ })
+ })
+
+ describe('#getBalance', function () {
+ it('gets balance', function () {
+ sinon.stub(txController.accountTracker.store, 'getState').callsFake(() => {
+ return {
+ accounts: {
+ '0x1678a085c290ebd122dc42cba69373b5953b831d': {
+ address: '0x1678a085c290ebd122dc42cba69373b5953b831d',
+ balance: '0x00000000000000056bc75e2d63100000',
+ code: '0x',
+ nonce: '0x0',
+ },
+ '0xc684832530fcbddae4b4230a47e991ddcec2831d': {
+ address: '0xc684832530fcbddae4b4230a47e991ddcec2831d',
+ balance: '0x0',
+ code: '0x',
+ nonce: '0x0',
+ },
+ },
+ }
+ })
+ assert.equal(txController.pendingTxTracker.getBalance('0x1678a085c290ebd122dc42cba69373b5953b831d'), '0x00000000000000056bc75e2d63100000')
+ assert.equal(txController.pendingTxTracker.getBalance('0xc684832530fcbddae4b4230a47e991ddcec2831d'), '0x0')
+ })
+ })
+
+ describe('#getPendingTransactions', function () {
+ beforeEach(function () {
+ txController.txStateManager._saveTxList([
+ { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 2, status: 'rejected', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 3, status: 'approved', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 4, status: 'signed', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 5, status: 'submitted', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 6, status: 'confimed', metamaskNetworkId: currentNetworkId, txParams: {} },
+ { id: 7, status: 'failed', metamaskNetworkId: currentNetworkId, txParams: {} },
+ ])
+ })
+ it('should show only submitted transactions as pending transasction', function () {
+ assert(txController.pendingTxTracker.getPendingTransactions().length, 1)
+ assert(txController.pendingTxTracker.getPendingTransactions()[0].status, 'submitted')
+ })
+ })
+})
diff --git a/test/unit/tx-state-manager-test.js b/test/unit/tx-state-manager-test.js
new file mode 100644
index 000000000..464e50ee4
--- /dev/null
+++ b/test/unit/tx-state-manager-test.js
@@ -0,0 +1,241 @@
+const assert = require('assert')
+const clone = require('clone')
+const ObservableStore = require('obs-store')
+const TxStateManager = require('../../app/scripts/lib/tx-state-manager')
+const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper')
+const noop = () => true
+
+describe('TransactionStateManger', function () {
+ let txStateManager
+ const currentNetworkId = 42
+ const otherNetworkId = 2
+
+ beforeEach(function () {
+ txStateManager = new TxStateManager({
+ initState: {
+ transactions: [],
+ },
+ txHistoryLimit: 10,
+ getNetwork: () => currentNetworkId
+ })
+ })
+
+ describe('#setTxStatusSigned', function () {
+ it('sets the tx status to signed', function () {
+ let tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
+ txStateManager.addTx(tx, noop)
+ txStateManager.setTxStatusSigned(1)
+ let result = txStateManager.getTxList()
+ assert.ok(Array.isArray(result))
+ assert.equal(result.length, 1)
+ assert.equal(result[0].status, 'signed')
+ })
+
+ it('should emit a signed event to signal the exciton of callback', (done) => {
+ let tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
+ const noop = function () {
+ assert(true, 'event listener has been triggered and noop executed')
+ done()
+ }
+ txStateManager.addTx(tx)
+ txStateManager.on('1:signed', noop)
+ txStateManager.setTxStatusSigned(1)
+
+ })
+ })
+
+ describe('#setTxStatusRejected', function () {
+ it('sets the tx status to rejected', function () {
+ let tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
+ txStateManager.addTx(tx)
+ txStateManager.setTxStatusRejected(1)
+ let result = txStateManager.getTxList()
+ assert.ok(Array.isArray(result))
+ assert.equal(result.length, 1)
+ assert.equal(result[0].status, 'rejected')
+ })
+
+ it('should emit a rejected event to signal the exciton of callback', (done) => {
+ let tx = { id: 1, status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
+ txStateManager.addTx(tx)
+ const noop = function (err, txId) {
+ assert(true, 'event listener has been triggered and noop executed')
+ done()
+ }
+ txStateManager.on('1:rejected', noop)
+ txStateManager.setTxStatusRejected(1)
+ })
+ })
+
+ describe('#getFullTxList', function () {
+ it('when new should return empty array', function () {
+ let result = txStateManager.getTxList()
+ assert.ok(Array.isArray(result))
+ assert.equal(result.length, 0)
+ })
+ })
+
+ describe('#getTxList', function () {
+ it('when new should return empty array', function () {
+ let result = txStateManager.getTxList()
+ assert.ok(Array.isArray(result))
+ assert.equal(result.length, 0)
+ })
+ })
+
+ describe('#addTx', function () {
+ it('adds a tx returned in getTxList', function () {
+ let tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
+ txStateManager.addTx(tx, noop)
+ let result = txStateManager.getTxList()
+ assert.ok(Array.isArray(result))
+ assert.equal(result.length, 1)
+ assert.equal(result[0].id, 1)
+ })
+
+ it('does not override txs from other networks', function () {
+ let tx = { id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
+ let tx2 = { id: 2, status: 'confirmed', metamaskNetworkId: otherNetworkId, txParams: {} }
+ txStateManager.addTx(tx, noop)
+ txStateManager.addTx(tx2, noop)
+ let result = txStateManager.getFullTxList()
+ let result2 = txStateManager.getTxList()
+ assert.equal(result.length, 2, 'txs were deleted')
+ assert.equal(result2.length, 1, 'incorrect number of txs on network.')
+ })
+
+ it('cuts off early txs beyond a limit', function () {
+ const limit = txStateManager.txHistoryLimit
+ for (let i = 0; i < limit + 1; i++) {
+ const tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
+ txStateManager.addTx(tx, noop)
+ }
+ let result = txStateManager.getTxList()
+ assert.equal(result.length, limit, `limit of ${limit} txs enforced`)
+ assert.equal(result[0].id, 1, 'early txs truncted')
+ })
+
+ it('cuts off early txs beyond a limit whether or not it is confirmed or rejected', function () {
+ const limit = txStateManager.txHistoryLimit
+ for (let i = 0; i < limit + 1; i++) {
+ const tx = { id: i, time: new Date(), status: 'rejected', metamaskNetworkId: currentNetworkId, txParams: {} }
+ txStateManager.addTx(tx, noop)
+ }
+ let result = txStateManager.getTxList()
+ assert.equal(result.length, limit, `limit of ${limit} txs enforced`)
+ assert.equal(result[0].id, 1, 'early txs truncted')
+ })
+
+ it('cuts off early txs beyond a limit but does not cut unapproved txs', function () {
+ let unconfirmedTx = { id: 0, time: new Date(), status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }
+ txStateManager.addTx(unconfirmedTx, noop)
+ const limit = txStateManager.txHistoryLimit
+ for (let i = 1; i < limit + 1; i++) {
+ const tx = { id: i, time: new Date(), status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }
+ txStateManager.addTx(tx, noop)
+ }
+ let result = txStateManager.getTxList()
+ assert.equal(result.length, limit, `limit of ${limit} txs enforced`)
+ assert.equal(result[0].id, 0, 'first tx should still be there')
+ assert.equal(result[0].status, 'unapproved', 'first tx should be unapproved')
+ assert.equal(result[1].id, 2, 'early txs truncted')
+ })
+ })
+
+ describe('#updateTx', function () {
+ it('replaces the tx with the same id', function () {
+ txStateManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
+ txStateManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
+ const txMeta = txStateManager.getTx('1')
+ txMeta.hash = 'foo'
+ txStateManager.updateTx(txMeta)
+ let result = txStateManager.getTx('1')
+ assert.equal(result.hash, 'foo')
+ })
+
+ it('updates gas price and adds history items', function () {
+ const originalGasPrice = '0x01'
+ const desiredGasPrice = '0x02'
+
+ const txMeta = {
+ id: '1',
+ status: 'unapproved',
+ metamaskNetworkId: currentNetworkId,
+ txParams: {
+ gasPrice: originalGasPrice,
+ },
+ }
+
+ const updatedMeta = clone(txMeta)
+
+ txStateManager.addTx(txMeta)
+ const updatedTx = txStateManager.getTx('1')
+ // verify tx was initialized correctly
+ assert.equal(updatedTx.history.length, 1, 'one history item (initial)')
+ assert.equal(Array.isArray(updatedTx.history[0]), false, 'first history item is initial state')
+ assert.deepEqual(updatedTx.history[0], txStateHistoryHelper.snapshotFromTxMeta(updatedTx), 'first history item is initial state')
+ // modify value and updateTx
+ updatedTx.txParams.gasPrice = desiredGasPrice
+ txStateManager.updateTx(updatedTx)
+ // check updated value
+ const result = txStateManager.getTx('1')
+ assert.equal(result.txParams.gasPrice, desiredGasPrice, 'gas price updated')
+ // validate history was updated
+ assert.equal(result.history.length, 2, 'two history items (initial + diff)')
+ const expectedEntry = { op: 'replace', path: '/txParams/gasPrice', value: desiredGasPrice }
+ assert.deepEqual(result.history[1], [expectedEntry], 'two history items (initial + diff)')
+ })
+ })
+
+ describe('#getUnapprovedTxList', function () {
+ it('returns unapproved txs in a hash', function () {
+ txStateManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
+ txStateManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
+ const result = txStateManager.getUnapprovedTxList()
+ assert.equal(typeof result, 'object')
+ assert.equal(result['1'].status, 'unapproved')
+ assert.equal(result['2'], undefined)
+ })
+ })
+
+ describe('#getTx', function () {
+ it('returns a tx with the requested id', function () {
+ txStateManager.addTx({ id: '1', status: 'unapproved', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
+ txStateManager.addTx({ id: '2', status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams: {} }, noop)
+ assert.equal(txStateManager.getTx('1').status, 'unapproved')
+ assert.equal(txStateManager.getTx('2').status, 'confirmed')
+ })
+ })
+
+ describe('#getFilteredTxList', function () {
+ it('returns a tx with the requested data', function () {
+ const txMetas = [
+ { id: 0, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
+ { id: 1, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
+ { id: 2, status: 'unapproved', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
+ { id: 3, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
+ { id: 4, status: 'unapproved', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
+ { id: 5, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
+ { id: 6, status: 'confirmed', txParams: { from: '0xaa', to: '0xbb' }, metamaskNetworkId: currentNetworkId },
+ { id: 7, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
+ { id: 8, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
+ { id: 9, status: 'confirmed', txParams: { from: '0xbb', to: '0xaa' }, metamaskNetworkId: currentNetworkId },
+ ]
+ txMetas.forEach((txMeta) => txStateManager.addTx(txMeta, noop))
+ let filterParams
+
+ filterParams = { status: 'unapproved', from: '0xaa' }
+ assert.equal(txStateManager.getFilteredTxList(filterParams).length, 3, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
+ filterParams = { status: 'unapproved', to: '0xaa' }
+ assert.equal(txStateManager.getFilteredTxList(filterParams).length, 2, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
+ filterParams = { status: 'confirmed', from: '0xbb' }
+ assert.equal(txStateManager.getFilteredTxList(filterParams).length, 3, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
+ filterParams = { status: 'confirmed' }
+ assert.equal(txStateManager.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
+ filterParams = { from: '0xaa' }
+ assert.equal(txStateManager.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
+ filterParams = { to: '0xaa' }
+ assert.equal(txStateManager.getFilteredTxList(filterParams).length, 5, `getFilteredTxList - ${JSON.stringify(filterParams)}`)
+ })
+ })
+}) \ No newline at end of file
diff --git a/test/unit/tx-utils-test.js b/test/unit/tx-utils-test.js
index 43128b977..8ca13412e 100644
--- a/test/unit/tx-utils-test.js
+++ b/test/unit/tx-utils-test.js
@@ -1,8 +1,10 @@
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-utils')
+const TxUtils = require('../../app/scripts/lib/tx-gas-utils')
describe('txUtils', function () {
@@ -28,7 +30,7 @@ describe('txUtils', function () {
nonce: '0x3',
chainId: 42,
}
- const ethTx = txUtils.buildEthTxFromParams(txParams)
+ const ethTx = new Transaction(txParams)
assert.equal(ethTx.getChainId(), 42, 'chainId is set from tx params')
})
})