mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Refactor eth-store into account-tracker
EthStore was only being used for tracking account balances and nonces now, so I removed its block-tracking duties, renamed it account-tracker, and removed it as a dependency from `KeyringController`, so that KRC can go live on without a hard dep on it.
This commit is contained in:
parent
977405fc7d
commit
11c8c07bfc
@ -2,7 +2,7 @@ const ObservableStore = require('obs-store')
|
|||||||
const extend = require('xtend')
|
const extend = require('xtend')
|
||||||
const BalanceController = require('./balance')
|
const BalanceController = require('./balance')
|
||||||
|
|
||||||
class BalancesController {
|
class ComputedbalancesController {
|
||||||
|
|
||||||
constructor (opts = {}) {
|
constructor (opts = {}) {
|
||||||
const { ethStore, txController } = opts
|
const { ethStore, txController } = opts
|
||||||
@ -61,4 +61,4 @@ class BalancesController {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
module.exports = BalancesController
|
module.exports = ComputedbalancesController
|
@ -22,7 +22,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
this.provider = opts.provider
|
this.provider = opts.provider
|
||||||
this.blockTracker = opts.blockTracker
|
this.blockTracker = opts.blockTracker
|
||||||
this.signEthTx = opts.signTransaction
|
this.signEthTx = opts.signTransaction
|
||||||
this.ethStore = opts.ethStore
|
this.accountTracker = opts.accountTracker
|
||||||
|
|
||||||
this.nonceTracker = new NonceTracker({
|
this.nonceTracker = new NonceTracker({
|
||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
@ -52,7 +52,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
nonceTracker: this.nonceTracker,
|
nonceTracker: this.nonceTracker,
|
||||||
getBalance: (address) => {
|
getBalance: (address) => {
|
||||||
const account = this.ethStore.getState().accounts[address]
|
const account = this.accountTracker.getState().accounts[address]
|
||||||
if (!account) return
|
if (!account) return
|
||||||
return account.balance
|
return account.balance
|
||||||
},
|
},
|
||||||
@ -73,7 +73,7 @@ module.exports = class TransactionController extends EventEmitter {
|
|||||||
this.blockTracker.on('rawBlock', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker))
|
this.blockTracker.on('rawBlock', this.pendingTxTracker.checkForTxInBlock.bind(this.pendingTxTracker))
|
||||||
// this is a little messy but until ethstore has been either
|
// this is a little messy but until ethstore has been either
|
||||||
// removed or redone this is to guard against the race condition
|
// removed or redone this is to guard against the race condition
|
||||||
// where ethStore hasent been populated by the results yet
|
// where accountTracker hasent been populated by the results yet
|
||||||
this.blockTracker.once('latest', () => {
|
this.blockTracker.once('latest', () => {
|
||||||
this.blockTracker.on('latest', this.pendingTxTracker.resubmitPendingTxs.bind(this.pendingTxTracker))
|
this.blockTracker.on('latest', this.pendingTxTracker.resubmitPendingTxs.bind(this.pendingTxTracker))
|
||||||
})
|
})
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
/* Ethereum Store
|
/* Account Tracker
|
||||||
*
|
*
|
||||||
* This module is responsible for tracking any number of accounts
|
* This module is responsible for tracking any number of accounts
|
||||||
* and caching their current balances & transaction counts.
|
* and caching their current balances & transaction counts.
|
@ -4,7 +4,7 @@ const promiseToCallback = require('promise-to-callback')
|
|||||||
const pipe = require('pump')
|
const pipe = require('pump')
|
||||||
const Dnode = require('dnode')
|
const Dnode = require('dnode')
|
||||||
const ObservableStore = require('obs-store')
|
const ObservableStore = require('obs-store')
|
||||||
const EthStore = require('./lib/eth-store')
|
const AccountTracker = require('./lib/account-tracker')
|
||||||
const EthQuery = require('eth-query')
|
const EthQuery = require('eth-query')
|
||||||
const streamIntoProvider = require('web3-stream-provider/handler')
|
const streamIntoProvider = require('web3-stream-provider/handler')
|
||||||
const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
|
const setupMultiplex = require('./lib/stream-utils.js').setupMultiplex
|
||||||
@ -81,19 +81,25 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
// eth data query tools
|
// eth data query tools
|
||||||
this.ethQuery = new EthQuery(this.provider)
|
this.ethQuery = new EthQuery(this.provider)
|
||||||
this.ethStore = new EthStore({
|
|
||||||
provider: this.provider,
|
|
||||||
blockTracker: this.provider,
|
|
||||||
})
|
|
||||||
|
|
||||||
// key mgmt
|
// key mgmt
|
||||||
this.keyringController = new KeyringController({
|
this.keyringController = new KeyringController({
|
||||||
initState: initState.KeyringController,
|
initState: initState.KeyringController,
|
||||||
ethStore: this.ethStore,
|
accountTracker: this.accountTracker,
|
||||||
getNetwork: this.networkController.getNetworkState.bind(this.networkController),
|
getNetwork: this.networkController.getNetworkState.bind(this.networkController),
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// account tracker watches balances, nonces, and any code at their address.
|
||||||
|
this.accountTracker = new AccountTracker({
|
||||||
|
provider: this.provider,
|
||||||
|
blockTracker: this.provider,
|
||||||
|
})
|
||||||
this.keyringController.on('newAccount', (address) => {
|
this.keyringController.on('newAccount', (address) => {
|
||||||
this.preferencesController.setSelectedAddress(address)
|
this.preferencesController.setSelectedAddress(address)
|
||||||
|
this.accountTracker.addAccount(address)
|
||||||
|
})
|
||||||
|
this.keyringController.on('removedAccount', (address) => {
|
||||||
|
this.accountTracker.removeAccount(address)
|
||||||
})
|
})
|
||||||
|
|
||||||
// address book controller
|
// address book controller
|
||||||
@ -112,13 +118,13 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
provider: this.provider,
|
provider: this.provider,
|
||||||
blockTracker: this.provider,
|
blockTracker: this.provider,
|
||||||
ethQuery: this.ethQuery,
|
ethQuery: this.ethQuery,
|
||||||
ethStore: this.ethStore,
|
accountTracker: this.accountTracker,
|
||||||
})
|
})
|
||||||
this.txController.on('newUnaprovedTx', opts.showUnapprovedTx.bind(opts))
|
this.txController.on('newUnaprovedTx', opts.showUnapprovedTx.bind(opts))
|
||||||
|
|
||||||
// computed balances (accounting for pending transactions)
|
// computed balances (accounting for pending transactions)
|
||||||
this.balancesController = new BalancesController({
|
this.balancesController = new BalancesController({
|
||||||
ethStore: this.ethStore,
|
accountTracker: this.accountTracker,
|
||||||
txController: this.txController,
|
txController: this.txController,
|
||||||
})
|
})
|
||||||
this.networkController.on('networkDidChange', () => {
|
this.networkController.on('networkDidChange', () => {
|
||||||
@ -177,7 +183,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
|
|
||||||
// manual mem state subscriptions
|
// manual mem state subscriptions
|
||||||
this.networkController.store.subscribe(this.sendUpdate.bind(this))
|
this.networkController.store.subscribe(this.sendUpdate.bind(this))
|
||||||
this.ethStore.subscribe(this.sendUpdate.bind(this))
|
this.accountTracker.subscribe(this.sendUpdate.bind(this))
|
||||||
this.txController.memStore.subscribe(this.sendUpdate.bind(this))
|
this.txController.memStore.subscribe(this.sendUpdate.bind(this))
|
||||||
this.balancesController.store.subscribe(this.sendUpdate.bind(this))
|
this.balancesController.store.subscribe(this.sendUpdate.bind(this))
|
||||||
this.messageManager.memStore.subscribe(this.sendUpdate.bind(this))
|
this.messageManager.memStore.subscribe(this.sendUpdate.bind(this))
|
||||||
@ -260,7 +266,7 @@ module.exports = class MetamaskController extends EventEmitter {
|
|||||||
isInitialized,
|
isInitialized,
|
||||||
},
|
},
|
||||||
this.networkController.store.getState(),
|
this.networkController.store.getState(),
|
||||||
this.ethStore.getState(),
|
this.accountTracker.getState(),
|
||||||
this.txController.memStore.getState(),
|
this.txController.memStore.getState(),
|
||||||
this.messageManager.memStore.getState(),
|
this.messageManager.memStore.getState(),
|
||||||
this.personalMessageManager.memStore.getState(),
|
this.personalMessageManager.memStore.getState(),
|
||||||
|
Loading…
Reference in New Issue
Block a user