mirror of
https://github.com/kremalicious/metamask-extension.git
synced 2024-12-23 09:52:26 +01:00
Some progress
This commit is contained in:
parent
4738746968
commit
53a467cd1e
28
app/scripts/controllers/balance.js
Normal file
28
app/scripts/controllers/balance.js
Normal file
@ -0,0 +1,28 @@
|
||||
const ObservableStore = require('obs-store')
|
||||
const normalizeAddress = require('eth-sig-util').normalize
|
||||
const extend = require('xtend')
|
||||
const PendingBalanceCalculator = require('../lib/pending-balance-calculator')
|
||||
|
||||
class BalanceController {
|
||||
|
||||
constructor (opts = {}) {
|
||||
const { address, ethQuery, txController } = opts
|
||||
this.ethQuery = ethQuery
|
||||
this.txController = txController
|
||||
|
||||
const initState = extend({
|
||||
ethBalance: undefined,
|
||||
}, opts.initState)
|
||||
this.store = new ObservableStore(initState)
|
||||
|
||||
const { getBalance, getPendingTransactions } = opts
|
||||
this.balanceCalc = new PendingBalanceCalculator({
|
||||
getBalance,
|
||||
getPendingTransactions,
|
||||
})
|
||||
this.updateBalance()
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
module.exports = BalanceController
|
108
app/scripts/controllers/balances.js
Normal file
108
app/scripts/controllers/balances.js
Normal file
@ -0,0 +1,108 @@
|
||||
const ObservableStore = require('obs-store')
|
||||
const normalizeAddress = require('eth-sig-util').normalize
|
||||
const extend = require('xtend')
|
||||
const BalanceController = require('./balance')
|
||||
|
||||
class BalancesController {
|
||||
|
||||
constructor (opts = {}) {
|
||||
const { ethStore, txController } = opts
|
||||
this.ethStore = ethStore
|
||||
this.txController = txController
|
||||
|
||||
const initState = extend({
|
||||
balances: [],
|
||||
}, opts.initState)
|
||||
this.store = new ObservableStore(initState)
|
||||
|
||||
this._initBalanceUpdating()
|
||||
}
|
||||
|
||||
// PUBLIC METHODS
|
||||
|
||||
setSelectedAddress (_address) {
|
||||
return new Promise((resolve, reject) => {
|
||||
const address = normalizeAddress(_address)
|
||||
this.store.updateState({ selectedAddress: address })
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
|
||||
getSelectedAddress (_address) {
|
||||
return this.store.getState().selectedAddress
|
||||
}
|
||||
|
||||
addToken (rawAddress, symbol, decimals) {
|
||||
const address = normalizeAddress(rawAddress)
|
||||
const newEntry = { address, symbol, decimals }
|
||||
|
||||
const tokens = this.store.getState().tokens
|
||||
const previousIndex = tokens.find((token, index) => {
|
||||
return token.address === address
|
||||
})
|
||||
|
||||
if (previousIndex) {
|
||||
tokens[previousIndex] = newEntry
|
||||
} else {
|
||||
tokens.push(newEntry)
|
||||
}
|
||||
|
||||
this.store.updateState({ tokens })
|
||||
return Promise.resolve()
|
||||
}
|
||||
|
||||
getTokens () {
|
||||
return this.store.getState().tokens
|
||||
}
|
||||
|
||||
updateFrequentRpcList (_url) {
|
||||
return this.addToFrequentRpcList(_url)
|
||||
.then((rpcList) => {
|
||||
this.store.updateState({ frequentRpcList: rpcList })
|
||||
return Promise.resolve()
|
||||
})
|
||||
}
|
||||
|
||||
setCurrentAccountTab (currentAccountTab) {
|
||||
return new Promise((resolve, reject) => {
|
||||
this.store.updateState({ currentAccountTab })
|
||||
resolve()
|
||||
})
|
||||
}
|
||||
|
||||
addToFrequentRpcList (_url) {
|
||||
const rpcList = this.getFrequentRpcList()
|
||||
const index = rpcList.findIndex((element) => { return element === _url })
|
||||
if (index !== -1) {
|
||||
rpcList.splice(index, 1)
|
||||
}
|
||||
if (_url !== 'http://localhost:8545') {
|
||||
rpcList.push(_url)
|
||||
}
|
||||
if (rpcList.length > 2) {
|
||||
rpcList.shift()
|
||||
}
|
||||
return Promise.resolve(rpcList)
|
||||
}
|
||||
|
||||
getFrequentRpcList () {
|
||||
return this.store.getState().frequentRpcList
|
||||
}
|
||||
//
|
||||
// PRIVATE METHODS
|
||||
//
|
||||
_initBalanceUpdating () {
|
||||
const store = this.ethStore.getState()
|
||||
const balances = store.accounts
|
||||
|
||||
for (let address in balances) {
|
||||
let updater = new BalancesController({
|
||||
address,
|
||||
ethQuery: this.ethQuery,
|
||||
txController: this.txController,
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
module.exports = BalancesController
|
@ -115,6 +115,12 @@ module.exports = class MetamaskController extends EventEmitter {
|
||||
})
|
||||
this.txController.on('newUnaprovedTx', opts.showUnapprovedTx.bind(opts))
|
||||
|
||||
// computed balances (accounting for pending transactions)
|
||||
this.balancesController = new BalancesController({
|
||||
ethStore: this.ethStore,
|
||||
txController: this.txController,
|
||||
})
|
||||
|
||||
// notices
|
||||
this.noticeController = new NoticeController({
|
||||
initState: initState.NoticeController,
|
||||
@ -647,4 +653,4 @@ module.exports = class MetamaskController extends EventEmitter {
|
||||
return Promise.resolve(rpcTarget)
|
||||
})
|
||||
}
|
||||
}
|
||||
}
|
||||
|
@ -96,3 +96,4 @@ function generateBalanceCalcWith (transactions, providerStub = zeroBn) {
|
||||
getPendingTransactions,
|
||||
})
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user