diff --git a/test/unit/tx-controller-test.js b/test/unit/tx-controller-test.js index 47bfe66f8..e1e4ae8fe 100644 --- a/test/unit/tx-controller-test.js +++ b/test/unit/tx-controller-test.js @@ -7,12 +7,13 @@ const sinon = require('sinon') const TransactionController = require('../../app/scripts/controllers/transactions') const TxGasUtils = require('../../app/scripts/lib/tx-gas-utils') const txStateHistoryHelper = require('../../app/scripts/lib/tx-state-history-helper') +const TxStateManager = require('../../app/scripts/lib/tx-state-manager') +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 () { @@ -34,11 +35,11 @@ describe('Transaction Controller', function () { }), }) txController.nonceTracker.getNonceLock = () => Promise.resolve({ nextNonce: 0, releaseLock: noop }) - txController.txProviderUtils = new TxGasUtils(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 (){ + 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') @@ -71,13 +72,32 @@ describe('Transaction Controller', function () { }) }) + describe('#getConfirmedTransactions', function () { + let address + beforeEach(function () { + address = '0xc684832530fcbddae4b4230a47e991ddcec2831d' + const txParams = { + 'from': address, + 'to': '0xc684832530fcbddae4b4230a47e991ddcec2831d', + } + txController.txStateManager._saveTxList([ + {id: 1, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams}, + {id: 2, status: 'confirmed', metamaskNetworkId: currentNetworkId, txParams}, + {id: 3, status: 'confirmed', 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', @@ -157,10 +177,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' @@ -168,7 +188,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() @@ -177,6 +197,16 @@ describe('Transaction Controller', function () { }) }) + xdescribe('#updateAndApprovedTransaction', function () { + it('should update txMeta and approve status for Tx', async function () { + txController.txStateManager.addTx({ id: 0, status: 'unapproved', txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d', nonce: '0x1', value: '0xfffff' }, metamaskNetworkId: currentNetworkId }) + const txMeta = txController.txStateManager.getTx(0) + txMeta.value = '0xffffe' + provider.eth_sendRawTransaction = 0x7f9fade1c0d57a7af66ab4ead79fade1c0d57a7af66ab4ead7c2c2eb7b11a91385 + await txController.updateAndApproveTransaction(txMeta) + }) + }) + describe('#validateTxParams', function () { it('does not throw for positive values', function (done) { var sample = { @@ -205,9 +235,8 @@ describe('Transaction Controller', function () { const txMeta = { id: '1', status: 'unapproved', - id: 1, metamaskNetworkId: currentNetworkId, - txParams: {} + txParams: {}, } const eventNames = ['updateBadge', '1:unapproved'] @@ -286,4 +315,143 @@ describe('Transaction Controller', function () { }).catch(done) }) }) -}) \ No newline at end of file + + describe('#getChainId', function () { + it('returns 0 when the chainId is NaN', async function () { + txController.networkStore = new ObservableStore('hello') + assert.equal(txController.getChainId(), 0) + }) + }) + + describe('#publishTransaction', async function () { + beforeEach(function () { + const txMeta = [ + { id: 0, status: 'unapproved', txParams: { from: '0x1678a085c290ebd122dc42cba69373b5953b831d', nonce: '0x1', value: '0xfffff' }, rawTx: 'f8498080808080801ca00255b75b550cf112e18fd699f27d043d85348c29d7e8fd234799db890f7c272da0238121aed40c3141e63aae5335aaa3c9711d4907f28071f81f8d055b9f8435e0', metamaskNetworkId: currentNetworkId }, + ] + txController.txStateManager.addTx(txMeta) + }) + + it('should update rawTx of a transaction', async function () { + txController.publishTransaction(0, 'f84c01808080830fffff801ca0e23554ce6f82186402dcdcfff377793fdfa8a872a5670c0ffc002c9cd2f6827aa02a83dfce20aef4064a55320bda47394043af3cbfa63c4e029c54ba6281dcc70e') + }) + }) + + describe('#cancelTransaction', function () { + beforeEach(function () { + const txMetas = [ + { id: 0, status: 'unapproved', txParams: { }, metamaskNetworkId: currentNetworkId }, + { id: 1, status: 'rejected', txParams: { }, metamaskNetworkId: currentNetworkId }, + { id: 2, status: 'approved', txParams: { }, metamaskNetworkId: currentNetworkId }, + { id: 3, status: 'signed', txParams: { }, metamaskNetworkId: currentNetworkId }, + { id: 4, status: 'submitted', txParams: { }, metamaskNetworkId: currentNetworkId }, + { id: 5, status: 'confirmed', txParams: { }, metamaskNetworkId: currentNetworkId }, + { id: 6, status: 'failed', txParams: { }, metamaskNetworkId: currentNetworkId }, + ] + txMetas.forEach((txMeta) => txController.txStateManager.addTx(txMeta)) + }) + + it('should set the transaction to rejected from unapproved', async function () { + await txController.cancelTransaction(0) + assert(txController.txStateManager.getTx(0).status, 'rejected') + }) + + it('should set the transaction to rejected from rejected', async function () { + await txController.cancelTransaction(1) + assert(txController.txStateManager.getTx(1).status, 'rejected') + }) + + it('should set the transaction to rejected from approved', async function () { + await txController.cancelTransaction(2) + assert(txController.txStateManager.getTx(2).status, 'rejected') + }) + + it('should set the transaction to rejected from signed', async function () { + await txController.cancelTransaction(3) + assert(txController.txStateManager.getTx(3).status, 'rejected') + }) + + it('should set the transaction to rejected from submitted', async function () { + await txController.cancelTransaction(4) + assert(txController.txStateManager.getTx(4).status, 'rejected') + }) + + it('should set the transaction to rejected from confirmed', async function () { + await txController.cancelTransaction(5) + assert(txController.txStateManager.getTx(5).status, 'rejected') + }) + + it('should set the transaction to rejected from failed', async function () { + await txController.cancelTransaction(6) + assert(txController.txStateManager.getTx(6).status, 'rejected') + }) + + }) + + describe('#publishTransaction', function () { + let replaceRawTx, rawTx, hash, txMeta + beforeEach(function () { + rawTx = 'f86c808504a817c800827b0d940c62bb85faa3311a998d3aba8098c1235c564966880de0b6b3a7640000802aa08ff665feb887a25d4099e40e11f0fef93ee9608f404bd3f853dd9e84ed3317a6a02ec9d3d1d6e176d4d2593dd760e74ccac753e6a0ea0d00cc9789d0d7ff1f471d' + replaceRawTx = 'f84c01808080830fffff801ca0e23554ce6f82186402dcdcfff377793fdfa8a872a5670c0ffc002c9cd2f6827aa02a83dfce20aef4064a55320bda47394043af3cbfa63c4e029c54ba6281dcc70e' + txMeta = { + id: 1, + status: 'approved', + txParams: {}, + rawTx, + hash, + metamaskNetworkId: currentNetworkId, + } + providerResultStub.eth_sendRawTransaction = '0x2a5523c6fa98b47b7d9b6c8320179785150b42a16bcff36b398c5062b65657e8' + }) + it('should publish a tx, updates the rawTx when provided a one', async function () { + txController.txStateManager.addTx(txMeta) + await txController.publishTransaction(txMeta.id, replaceRawTx) + txController.setTxHash(1, '0x2a5523c6fa98b47b7d9b6c8320179785150b42a16bcff36b398c5062b65657e8') + assert.equal(txController.txStateManager.getTx(1).rawTx, replaceRawTx) + assert.notEqual(txController.txStateManager.getTx(1).rawTx, rawTx) + }) + }) + + describe('#getBalance', function () { + it('gets balance', function () { + sinon.stub(txController.ethStore, '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') + }) + }) + +})