1
0
mirror of https://github.com/kremalicious/metamask-extension.git synced 2024-12-23 09:52:26 +01:00

preferences test fixes for tokens per account

This commit is contained in:
Esteban MIno 2018-07-25 17:46:31 -04:00
parent 2770df80e3
commit adee549c43

View File

@ -40,10 +40,9 @@ class PreferencesController {
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]
const addressTokens = this.store.getState().addressTokens
if (!(selectedAddress in addressTokens)) return []
return addressTokens[selectedAddress]
},
})
}
@ -88,12 +87,16 @@ class PreferencesController {
*/
setAddresses (addresses) {
const oldIdentities = this.store.getState().identities
const addressTokens = this.store.getState().addressTokens
const identities = addresses.reduce((ids, address, index) => {
const oldId = oldIdentities[address] || {}
ids[address] = {name: `Account ${index + 1}`, address, ...oldId}
return ids
}, {})
this.store.updateState({ identities })
for (const address in identities) {
if (!(address in addressTokens)) addressTokens[address] = []
}
this.store.updateState({ identities, addressTokens })
}
/**
@ -104,11 +107,13 @@ class PreferencesController {
*/
removeAddress (address) {
const identities = this.store.getState().identities
const addressTokens = this.store.getState().addressTokens
if (!identities[address]) {
throw new Error(`${address} can't be deleted cause it was not found`)
}
delete identities[address]
this.store.updateState({ identities })
delete addressTokens[address]
this.store.updateState({ identities, addressTokens })
// If the selected account is no longer valid,
// select an arbitrary other account:
@ -194,8 +199,10 @@ class PreferencesController {
setSelectedAddress (_address) {
return new Promise((resolve, reject) => {
const address = normalizeAddress(_address)
const tokens = this.store.getState().addressTokens[_address]
this.store.updateState({ selectedAddress: address, tokens: tokens })
const addressTokens = this.store.getState().addressTokens
if (!(address in addressTokens)) addressTokens[address] = []
const tokens = addressTokens[address]
this.store.updateState({ selectedAddress: address, tokens })
resolve()
})
}
@ -265,7 +272,7 @@ class PreferencesController {
removeToken (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 })