aboutsummaryrefslogtreecommitdiffstats
path: root/app/scripts/controllers/balances.js
diff options
context:
space:
mode:
authorDan Finlay <dan@danfinlay.com>2017-09-13 06:06:19 +0800
committerDan Finlay <dan@danfinlay.com>2017-09-13 06:06:19 +0800
commit53a467cd1e2ab50168b06d36a98effcfd3db3a49 (patch)
tree510f10de4b1eb223c66f96a0dff7f8d35f845a87 /app/scripts/controllers/balances.js
parent47387469680454c2df1211570bd924a7747cc628 (diff)
downloadtangerine-wallet-browser-53a467cd1e2ab50168b06d36a98effcfd3db3a49.tar
tangerine-wallet-browser-53a467cd1e2ab50168b06d36a98effcfd3db3a49.tar.gz
tangerine-wallet-browser-53a467cd1e2ab50168b06d36a98effcfd3db3a49.tar.bz2
tangerine-wallet-browser-53a467cd1e2ab50168b06d36a98effcfd3db3a49.tar.lz
tangerine-wallet-browser-53a467cd1e2ab50168b06d36a98effcfd3db3a49.tar.xz
tangerine-wallet-browser-53a467cd1e2ab50168b06d36a98effcfd3db3a49.tar.zst
tangerine-wallet-browser-53a467cd1e2ab50168b06d36a98effcfd3db3a49.zip
Some progress
Diffstat (limited to 'app/scripts/controllers/balances.js')
-rw-r--r--app/scripts/controllers/balances.js108
1 files changed, 108 insertions, 0 deletions
diff --git a/app/scripts/controllers/balances.js b/app/scripts/controllers/balances.js
new file mode 100644
index 000000000..b0b366628
--- /dev/null
+++ b/app/scripts/controllers/balances.js
@@ -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