diff options
migration 7 - break off TransactionManager substate
Diffstat (limited to 'app/scripts')
-rw-r--r-- | app/scripts/metamask-controller.js | 21 | ||||
-rw-r--r-- | app/scripts/migrations/007.js | 38 | ||||
-rw-r--r-- | app/scripts/migrations/index.js | 1 | ||||
-rw-r--r-- | app/scripts/transaction-manager.js | 54 |
4 files changed, 89 insertions, 25 deletions
diff --git a/app/scripts/metamask-controller.js b/app/scripts/metamask-controller.js index b6a096488..468e7d285 100644 --- a/app/scripts/metamask-controller.js +++ b/app/scripts/metamask-controller.js @@ -68,11 +68,9 @@ module.exports = class MetamaskController extends EventEmitter { // tx mgmt this.txManager = new TxManager({ - txList: this.configManager.getTxList(), + initState: initState.TxManager, txHistoryLimit: 40, - setTxList: this.configManager.setTxList.bind(this.configManager), getSelectedAddress: this.preferencesController.getSelectedAddress.bind(this.preferencesController), - getGasMultiplier: this.configManager.getGasMultiplier.bind(this.configManager), getNetwork: this.getStateNetwork.bind(this), signTransaction: this.keyringController.signTransaction.bind(this.keyringController), provider: this.provider, @@ -107,11 +105,14 @@ module.exports = class MetamaskController extends EventEmitter { this.preferencesController.store.subscribe((state) => { this.store.updateState({ PreferencesController: state }) }) + this.txManager.store.subscribe((state) => { + this.store.updateState({ TransactionManager: state }) + }) // manual mem state subscriptions this.ethStore.on('update', this.sendUpdate.bind(this)) this.keyringController.memStore.subscribe(this.sendUpdate.bind(this)) - this.txManager.on('update', this.sendUpdate.bind(this)) + this.txManager.memStore.subscribe(this.sendUpdate.bind(this)) this.messageManager.memStore.subscribe(this.sendUpdate.bind(this)) } @@ -177,7 +178,7 @@ module.exports = class MetamaskController extends EventEmitter { }, this.state, this.ethStore.getState(), - this.txManager.getState(), + this.txManager.memStore.getState(), this.messageManager.memStore.getState(), this.keyringController.memStore.getState(), this.preferencesController.store.getState(), @@ -245,11 +246,13 @@ module.exports = class MetamaskController extends EventEmitter { saveAccountLabel: nodeify(keyringController.saveAccountLabel).bind(keyringController), exportAccount: nodeify(keyringController.exportAccount).bind(keyringController), - // signing methods + // txManager approveTransaction: txManager.approveTransaction.bind(txManager), cancelTransaction: txManager.cancelTransaction.bind(txManager), - signMessage: this.signMessage.bind(this), - cancelMessage: messageManager.rejectMsg.bind(messageManager), + + // messageManager + signMessage: this.signMessage.bind(this), + cancelMessage: messageManager.rejectMsg.bind(messageManager), // notices checkNotices: noticeController.updateNoticesList.bind(noticeController), @@ -586,7 +589,7 @@ module.exports = class MetamaskController extends EventEmitter { setGasMultiplier (gasMultiplier, cb) { try { - this.configManager.setGasMultiplier(gasMultiplier) + this.txManager.setGasMultiplier(gasMultiplier) cb() } catch (err) { cb(err) diff --git a/app/scripts/migrations/007.js b/app/scripts/migrations/007.js new file mode 100644 index 000000000..3ae8cdc2d --- /dev/null +++ b/app/scripts/migrations/007.js @@ -0,0 +1,38 @@ +const version = 7 + +/* + +This migration breaks out the TransactionManager substate + +*/ + +const extend = require('xtend') + +module.exports = { + version, + + migrate: function (versionedData) { + versionedData.meta.version = version + try { + const state = versionedData.data + const newState = transformState(state) + versionedData.data = newState + } catch (err) { + console.warn(`MetaMask Migration #${version}` + err.stack) + } + return Promise.resolve(versionedData) + }, +} + +function transformState (state) { + const newState = extend(state, { + TransactionManager: { + transactions: state.transactions || [], + gasMultiplier: state.gasMultiplier || 1, + }, + }) + delete newState.transactions + delete newState.gasMultiplier + + return newState +} diff --git a/app/scripts/migrations/index.js b/app/scripts/migrations/index.js index 17c191448..d608f5314 100644 --- a/app/scripts/migrations/index.js +++ b/app/scripts/migrations/index.js @@ -17,4 +17,5 @@ module.exports = [ require('./004'), require('./005'), require('./006'), + require('./007'), ] diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js index 6fecdba39..7949fb854 100644 --- a/app/scripts/transaction-manager.js +++ b/app/scripts/transaction-manager.js @@ -2,6 +2,7 @@ const EventEmitter = require('events') const async = require('async') const extend = require('xtend') const Semaphore = require('semaphore') +const ObservableStore = require('obs-store') const ethUtil = require('ethereumjs-util') const BN = require('ethereumjs-util').BN const TxProviderUtil = require('./lib/tx-utils') @@ -10,33 +11,46 @@ const createId = require('./lib/random-id') module.exports = class TransactionManager extends EventEmitter { constructor (opts) { super() - this.txList = opts.txList || [] - this._setTxList = opts.setTxList + this.store = new ObservableStore(extend({ + txList: [], + gasMultiplier: 1, + }, opts.initState)) + this.memStore = new ObservableStore({}) + // this.networkStore = opts.networkStore || new ObservableStore({}) + this.getNetwork = opts.getNetwork + this.txHistoryLimit = opts.txHistoryLimit this.getSelectedAddress = opts.getSelectedAddress this.provider = opts.provider this.blockTracker = opts.blockTracker this.txProviderUtils = new TxProviderUtil(this.provider) this.blockTracker.on('block', this.checkForTxInBlock.bind(this)) - this.getGasMultiplier = opts.getGasMultiplier - this.getNetwork = opts.getNetwork this.signEthTx = opts.signTransaction this.nonceLock = Semaphore(1) + + // memstore is computed from diskStore + this._updateMemstore() + this.store.subscribe(() => this._updateMemstore() ) + // this.networkStore.subscribe(() => this._updateMemstore() ) } getState () { - var selectedAddress = this.getSelectedAddress() - return { - transactions: this.getTxList(), - unapprovedTxs: this.getUnapprovedTxList(), - selectedAddressTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAddress}), - } + return this.memStore.getState() } -// Returns the tx list + // Returns the tx list getTxList () { let network = this.getNetwork() - return this.txList.filter(txMeta => txMeta.metamaskNetworkId === network) + let fullTxList = this.store.getState().txList + return fullTxList.filter(txMeta => txMeta.metamaskNetworkId === network) + } + + getGasMultiplier () { + return this.store.getState().gasMultiplier + } + + setGasMultiplier (gasMultiplier) { + return this.store.updateState({ gasMultiplier }) } // Adds a tx to the txlist @@ -108,7 +122,7 @@ module.exports = class TransactionManager extends EventEmitter { id: txId, time: time, status: 'unapproved', - gasMultiplier: this.getGasMultiplier() || 1, + gasMultiplier: this.getGasMultiplier(), metamaskNetworkId: this.getNetwork(), txParams: txParams, } @@ -239,7 +253,7 @@ module.exports = class TransactionManager extends EventEmitter { getTxsByMetaData (key, value, txList = this.getTxList()) { return txList.filter((txMeta) => { - if (key in txMeta.txParams) { + if (txMeta.txParams[key]) { return txMeta.txParams[key] === value } else { return txMeta[key] === value @@ -352,8 +366,16 @@ module.exports = class TransactionManager extends EventEmitter { // Saves the new/updated txList. // Function is intended only for internal use _saveTxList (txList) { - this.txList = txList - this._setTxList(txList) + this.store.updateState({ txList }) + } + + _updateMemstore () { + const unapprovedTxs = this.getUnapprovedTxList() + const selectedAddressTxList = this.getFilteredTxList({ + from: this.getSelectedAddress(), + metamaskNetworkId: this.getNetwork(), + }) + this.memStore.updateState({ unapprovedTxs, selectedAddressTxList }) } } |