diff options
Diffstat (limited to 'app/scripts/transaction-manager.js')
-rw-r--r-- | app/scripts/transaction-manager.js | 65 |
1 files changed, 47 insertions, 18 deletions
diff --git a/app/scripts/transaction-manager.js b/app/scripts/transaction-manager.js index 6d0121afd..6299091f2 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,53 @@ 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({ + transactions: [], + gasMultiplier: 1, + }, opts.initState)) + this.memStore = new ObservableStore({}) + this.networkStore = opts.networkStore || new ObservableStore({}) + this.preferencesStore = opts.preferencesStore || new ObservableStore({}) this.txHistoryLimit = opts.txHistoryLimit - this.getSelectedAccount = opts.getSelectedAccount 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 a few different stores + this._updateMemstore() + this.store.subscribe(() => this._updateMemstore() ) + this.networkStore.subscribe(() => this._updateMemstore() ) + this.preferencesStore.subscribe(() => this._updateMemstore() ) } getState () { - var selectedAccount = this.getSelectedAccount() - return { - transactions: this.getTxList(), - unconfTxs: this.getUnapprovedTxList(), - selectedAccountTxList: this.getFilteredTxList({metamaskNetworkId: this.getNetwork(), from: selectedAccount}), - } + return this.memStore.getState() + } + + getNetwork () { + return this.networkStore.getState().network } -// Returns the tx list + getSelectedAddress () { + return this.preferencesStore.getState().selectedAddress + } + + // Returns the tx list getTxList () { let network = this.getNetwork() - return this.txList.filter(txMeta => txMeta.metamaskNetworkId === network) + let fullTxList = this.store.getState().transactions + 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 +129,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 +260,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 @@ -351,9 +372,17 @@ 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) + _saveTxList (transactions) { + this.store.updateState({ transactions }) + } + + _updateMemstore () { + const unapprovedTxs = this.getUnapprovedTxList() + const selectedAddressTxList = this.getFilteredTxList({ + from: this.getSelectedAddress(), + metamaskNetworkId: this.getNetwork(), + }) + this.memStore.updateState({ unapprovedTxs, selectedAddressTxList }) } } |