Skip to content

Commit

Permalink
add & delete tokens per account
Browse files Browse the repository at this point in the history
  • Loading branch information
estebanmino committed Jul 25, 2018
1 parent 3667f3c commit 2770df8
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion app/scripts/controllers/detect-tokens.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ class DetectTokensController {
set preferences (preferences) {
if (!preferences) { return }
this._preferences = preferences
preferences.store.subscribe(({ tokens }) => { this.tokenAddresses = tokens.map((obj) => { return obj.address }) })
preferences.store.subscribe(({ tokens = [] }) => { this.tokenAddresses = tokens.map((obj) => { return obj.address }) })
preferences.store.subscribe(({ selectedAddress }) => {
if (this.selectedAddress !== selectedAddress) {
this.selectedAddress = selectedAddress
Expand Down
33 changes: 26 additions & 7 deletions app/scripts/controllers/preferences.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ class PreferencesController {
const initState = extend({
frequentRpcList: [],
currentAccountTab: 'history',
addressTokens: {},
tokens: [],
useBlockie: false,
featureFlags: {},
Expand All @@ -35,6 +36,16 @@ class PreferencesController {
this.diagnostics = opts.diagnostics

this.store = new ObservableStore(initState)

Object.defineProperty(this.store._state, 'tokens', {
get: () => {
const selectedAddress = this.store.getState().selectedAddress
const tokens = this.store.getState().addressTokens
// TODO when create vault
if (!(selectedAddress in tokens)) return []
return tokens[selectedAddress]
},
})
}
// PUBLIC METHODS

Expand Down Expand Up @@ -117,14 +128,16 @@ class PreferencesController {
*/
addAddresses (addresses) {
const identities = this.store.getState().identities
const addressTokens = this.store.getState().addressTokens
addresses.forEach((address) => {
// skip if already exists
if (identities[address]) return
// add missing identity
const identityCount = Object.keys(identities).length
if (!(address in addressTokens)) addressTokens[address] = []
identities[address] = { name: `Account ${identityCount + 1}`, address }
})
this.store.updateState({ identities })
this.store.updateState({ identities, addressTokens })
}

/*
Expand Down Expand Up @@ -181,7 +194,8 @@ class PreferencesController {
setSelectedAddress (_address) {
return new Promise((resolve, reject) => {
const address = normalizeAddress(_address)
this.store.updateState({ selectedAddress: address })
const tokens = this.store.getState().addressTokens[_address]
this.store.updateState({ selectedAddress: address, tokens: tokens })
resolve()
})
}
Expand Down Expand Up @@ -233,7 +247,10 @@ class PreferencesController {
tokens.push(newEntry)
}

this.store.updateState({ tokens })
const selectedAddress = this.store.getState().selectedAddress
const addressTokens = this.store.getState().addressTokens
addressTokens[selectedAddress] = tokens
this.store.updateState({ addressTokens })

return Promise.resolve(tokens)
}
Expand All @@ -246,11 +263,13 @@ class PreferencesController {
*
*/
removeToken (rawAddress) {
const tokens = this.store.getState().tokens

const updatedTokens = tokens.filter(token => token.address !== rawAddress)
const addressTokens = this.store.getState().addressTokens
const selectedAddress = this.store.getState().selectedAddress

const updatedTokens = addressTokens[selectedAddress].filter(token => token.address !== rawAddress)
addressTokens[selectedAddress] = updatedTokens
this.store.updateState({ addressTokens, tokens: updatedTokens })

this.store.updateState({ tokens: updatedTokens })
return Promise.resolve(updatedTokens)
}

Expand Down

0 comments on commit 2770df8

Please sign in to comment.