From fde69ea0baf32b5d2a6932b73f4772e983aef552 Mon Sep 17 00:00:00 2001 From: Frankie Date: Fri, 23 Dec 2016 12:34:12 -0800 Subject: [PATCH] fix some minor spelling mistakes and clean up code --- app/scripts/background.js | 2 +- app/scripts/metamask-controller.js | 10 ++----- app/scripts/transaction-manager.js | 42 ++++++++++++++++-------------- test/unit/tx-manager-test.js | 12 ++++----- 4 files changed, 32 insertions(+), 34 deletions(-) diff --git a/app/scripts/background.js b/app/scripts/background.js index d05ec989f..ca2efc114 100644 --- a/app/scripts/background.js +++ b/app/scripts/background.js @@ -97,7 +97,7 @@ function setupControllerConnection (stream) { // plugin badge text // -txManager.on('update', updateBadge) +txManager.on('updateBadge', updateBadge) function updateBadge () { var label = '' diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index 6aec825fe..4af5cd78d 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -21,7 +21,6 @@ module.exports = class MetamaskController { this.configManager = new ConfigManager(opts) this.keyringController = new KeyringController({ configManager: this.configManager, - txManager: this.txManager, getNetwork: this.getStateNetwork.bind(this), }) // notices @@ -40,6 +39,7 @@ module.exports = class MetamaskController { txList: this.configManager.getTxList(), txHistoryLimit: 40, setTxList: this.configManager.setTxList.bind(this.configManager), + getSelectedAccount: this.configManager.getSelectedAccount.bind(this.configManager), getGasMultiplier: this.configManager.getGasMultiplier.bind(this.configManager), getNetwork: this.getStateNetwork.bind(this), provider: this.provider, @@ -47,8 +47,6 @@ module.exports = class MetamaskController { }) this.publicConfigStore = this.initPublicConfigStore() - - var currentFiat = this.configManager.getCurrentFiat() || 'USD' this.configManager.setCurrentFiat(currentFiat) this.configManager.updateConversionRate() @@ -105,9 +103,6 @@ module.exports = class MetamaskController { signMessage: keyringController.signMessage.bind(keyringController), cancelMessage: keyringController.cancelMessage.bind(keyringController), - // forward directly to txManager - getUnapprovedTxList: txManager.getUnapprovedTxList.bind(txManager), - getFilteredTxList: txManager.getFilteredTxList.bind(txManager), // coinbase buyEth: this.buyEth.bind(this), // shapeshift @@ -251,11 +246,10 @@ module.exports = class MetamaskController { setupSigningListners (txParams) { var txId = txParams.metamaskId // apply event listeners for signing and formating events - this.txManager.once(`${txId}:formated`, this.keyringController.signTransaction.bind(this.keyringController)) + this.txManager.once(`${txId}:formatted`, this.keyringController.signTransaction.bind(this.keyringController)) this.keyringController.once(`${txId}:signed`, this.txManager.resolveSignedTransaction.bind(this.txManager)) } - enforceTxValidations (txParams) { if (('value' in txParams) && txParams.value.indexOf('-') === 0) { const msg = `Invalid transaction value of ${txParams.value} not a positive number.` diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js index 031993424..fd136a51b 100644 --- a/app/scripts/transaction-manager.js +++ b/app/scripts/transaction-manager.js @@ -12,10 +12,8 @@ module.exports = class TransactionManager extends EventEmitter { super() this.txList = opts.txList || [] this._setTxList = opts.setTxList - this._unconfTxCbs = {} this.txHistoryLimit = opts.txHistoryLimit - // txManager :: tx approvals and rejection cb's - + this.getSelectedAccount = opts.getSelectedAccount this.provider = opts.provider this.blockTracker = opts.blockTracker this.txProviderUtils = new TxProviderUtil(this.provider) @@ -25,9 +23,11 @@ module.exports = class TransactionManager extends EventEmitter { } getState () { + var selectedAccount = this.getSelectedAccount() return { transactions: this.getTxList(), unconfTxs: this.getUnapprovedTxList(), + selectedAccountTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAccount}), } } @@ -37,14 +37,21 @@ module.exports = class TransactionManager extends EventEmitter { } // Adds a tx to the txlist - addTx (txMeta, onTxDoneCb = noop) { + addTx (txMeta, onTxDoneCb = warn) { var txList = this.getTxList() var txHistoryLimit = this.txHistoryLimit + + // checks if the length of th tx history is + // longer then desired persistence limit + // and then if it is removes only confirmed + // or rejected tx's. + // not tx's that are pending or unapproved if (txList.length > txHistoryLimit - 1) { var index = txList.findIndex((metaTx) => metaTx.status === 'confirmed' || metaTx.status === 'rejected') - index ? txList.splice(index, index) : txList.shift() + txList.splice(index, 1) } txList.push(txMeta) + this._saveTxList(txList) // keep the onTxDoneCb around in a listener // for after approval/denial (requires user interaction) @@ -58,14 +65,14 @@ module.exports = class TransactionManager extends EventEmitter { onTxDoneCb(null, false) }) - this.emit('update') + this.emit('updateBadge') this.emit(`${txMeta.id}:unapproved`, txMeta) } // gets tx by Id and returns it getTx (txId, cb) { var txList = this.getTxList() - var txMeta = txList.find((txData) => txData.id === txId) + var txMeta = txList.find(txData => txData.id === txId) return cb ? cb(txMeta) : txMeta } @@ -73,7 +80,7 @@ module.exports = class TransactionManager extends EventEmitter { updateTx (txMeta) { var txId = txMeta.id var txList = this.getTxList() - var index = txList.findIndex((txData) => txData.id === txId) + var index = txList.findIndex(txData => txData.id === txId) txList[index] = txMeta this._saveTxList(txList) } @@ -119,16 +126,14 @@ module.exports = class TransactionManager extends EventEmitter { }, {}) } - approveTransaction (txId, cb) { + approveTransaction (txId, cb = warn) { this.setTxStatusSigned(txId) cb() } - cancelTransaction (txId, cb) { + cancelTransaction (txId, cb = warn) { this.setTxStatusRejected(txId) - if (cb && typeof cb === 'function') { - cb() - } + cb() } // formats txParams so the keyringController can sign it @@ -148,15 +153,14 @@ module.exports = class TransactionManager extends EventEmitter { txParams.gasLimit = normalize(txParams.gasLimit || txParams.gas) txParams.nonce = normalize(txParams.nonce) const ethTx = new Transaction(txParams) - // this.updateTxParams(txParams.metamaskId, ethTx) // listener is assigned in metamaskController - this.emit(`${txParams.metamaskId}:formated`, ethTx, address, txParams.metamaskId, cb) + this.emit(`${txParams.metamaskId}:formatted`, ethTx, address, txParams.metamaskId, cb) } // receives a signed tx object and updates the tx hash // and pass it to the cb to be sent off - resolveSignedTransaction ({tx, txId, cb}) { + resolveSignedTransaction ({tx, txId, cb = warn}) { // Add the tx hash to the persisted meta-tx object var txHash = ethUtil.bufferToHex(tx.hash()) var metaTx = this.getTx(txId) @@ -212,13 +216,13 @@ module.exports = class TransactionManager extends EventEmitter { // should update the status of the tx to 'signed'. setTxStatusSigned (txId) { this._setTxStatus(txId, 'signed') - this.emit('update') + this.emit('updateBadge') } // should update the status of the tx to 'rejected'. setTxStatusRejected (txId) { this._setTxStatus(txId, 'rejected') - this.emit('update') + this.emit('updateBadge') } setTxStatusConfirmed (txId) { @@ -281,4 +285,4 @@ module.exports = class TransactionManager extends EventEmitter { } -const noop = () => console.warn('noop was used no cb provided') +const warn = () => console.warn('warn was used no cb provided') diff --git a/test/unit/tx-manager-test.js b/test/unit/tx-manager-test.js index f09068a72..be16facad 100644 --- a/test/unit/tx-manager-test.js +++ b/test/unit/tx-manager-test.js @@ -80,7 +80,7 @@ describe('Transaction Manager', function() { } var result = txManager.getTxList() assert.equal(result.length, limit, `limit of ${limit} txs enforced`) - assert.equal(result[0].id, 0, 'first tx should still be their') + 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') }) @@ -168,15 +168,15 @@ describe('Transaction Manager', function() { var foop = 0 var zoop = 0 for (let i = 0; i < 10; ++i ){ - let evryOther = i % 2 + let everyOther = i % 2 txManager.addTx({ id: i, - status: evryOther ? 'unapproved' : 'confirmed', + status: everyOther ? 'unapproved' : 'confirmed', txParams: { - from: evryOther ? 'foop' : 'zoop', - to: evryOther ? 'zoop' : 'foop', + from: everyOther ? 'foop' : 'zoop', + to: everyOther ? 'zoop' : 'foop', } }, onTxDoneCb) - evryOther ? ++foop : ++zoop + everyOther ? ++foop : ++zoop } assert.equal(txManager.getFilteredTxList({status: 'confirmed', from: 'zoop'}).length, zoop) assert.equal(txManager.getFilteredTxList({status: 'confirmed', to: 'foop'}).length, zoop)