|
|
@ -34,16 +34,17 @@ class PreferencesController { |
|
|
|
}, opts.initState) |
|
|
|
}, opts.initState) |
|
|
|
|
|
|
|
|
|
|
|
this.diagnostics = opts.diagnostics |
|
|
|
this.diagnostics = opts.diagnostics |
|
|
|
|
|
|
|
this.network = opts.network |
|
|
|
this.store = new ObservableStore(initState) |
|
|
|
this.store = new ObservableStore(initState) |
|
|
|
|
|
|
|
this._defineTokensGetter(this.store._state) |
|
|
|
Object.defineProperty(this.store._state, 'tokens', { |
|
|
|
this.network.providerStore.subscribe(({ type }) => { |
|
|
|
get: () => { |
|
|
|
|
|
|
|
const selectedAddress = this.store.getState().selectedAddress |
|
|
|
const selectedAddress = this.store.getState().selectedAddress |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
if (!(selectedAddress in addressTokens)) return [] |
|
|
|
if (!(type in addressTokens)) addressTokens[type] = {} |
|
|
|
return addressTokens[selectedAddress] |
|
|
|
if (!(selectedAddress in addressTokens[type])) addressTokens[type][selectedAddress] = [] |
|
|
|
}, |
|
|
|
const tokens = addressTokens[type][selectedAddress] |
|
|
|
|
|
|
|
this.store.updateState({ tokens }) |
|
|
|
|
|
|
|
|
|
|
|
}) |
|
|
|
}) |
|
|
|
} |
|
|
|
} |
|
|
|
// PUBLIC METHODS
|
|
|
|
// PUBLIC METHODS
|
|
|
@ -88,13 +89,15 @@ class PreferencesController { |
|
|
|
setAddresses (addresses) { |
|
|
|
setAddresses (addresses) { |
|
|
|
const oldIdentities = this.store.getState().identities |
|
|
|
const oldIdentities = this.store.getState().identities |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
|
|
|
|
const providerType = this.network.providerStore.getState().type |
|
|
|
const identities = addresses.reduce((ids, address, index) => { |
|
|
|
const identities = addresses.reduce((ids, address, index) => { |
|
|
|
const oldId = oldIdentities[address] || {} |
|
|
|
const oldId = oldIdentities[address] || {} |
|
|
|
ids[address] = {name: `Account ${index + 1}`, address, ...oldId} |
|
|
|
ids[address] = {name: `Account ${index + 1}`, address, ...oldId} |
|
|
|
return ids |
|
|
|
return ids |
|
|
|
}, {}) |
|
|
|
}, {}) |
|
|
|
for (const address in identities) { |
|
|
|
for (const address in identities) { |
|
|
|
if (!(address in addressTokens)) addressTokens[address] = [] |
|
|
|
if (!(providerType in addressTokens)) addressTokens[providerType] = {} |
|
|
|
|
|
|
|
if (!(address in addressTokens[providerType])) addressTokens[providerType][address] = [] |
|
|
|
} |
|
|
|
} |
|
|
|
this.store.updateState({ identities, addressTokens }) |
|
|
|
this.store.updateState({ identities, addressTokens }) |
|
|
|
} |
|
|
|
} |
|
|
@ -134,12 +137,16 @@ class PreferencesController { |
|
|
|
addAddresses (addresses) { |
|
|
|
addAddresses (addresses) { |
|
|
|
const identities = this.store.getState().identities |
|
|
|
const identities = this.store.getState().identities |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
|
|
|
|
const providerType = this.network.providerStore.getState().type |
|
|
|
|
|
|
|
|
|
|
|
addresses.forEach((address) => { |
|
|
|
addresses.forEach((address) => { |
|
|
|
// skip if already exists
|
|
|
|
// skip if already exists
|
|
|
|
if (identities[address]) return |
|
|
|
if (identities[address]) return |
|
|
|
// add missing identity
|
|
|
|
// add missing identity
|
|
|
|
const identityCount = Object.keys(identities).length |
|
|
|
const identityCount = Object.keys(identities).length |
|
|
|
if (!(address in addressTokens)) addressTokens[address] = [] |
|
|
|
|
|
|
|
|
|
|
|
if (!(providerType in addressTokens)) addressTokens[providerType] = {} |
|
|
|
|
|
|
|
if (!(address in addressTokens[providerType])) addressTokens[providerType][address] = [] |
|
|
|
identities[address] = { name: `Account ${identityCount + 1}`, address } |
|
|
|
identities[address] = { name: `Account ${identityCount + 1}`, address } |
|
|
|
}) |
|
|
|
}) |
|
|
|
this.store.updateState({ identities, addressTokens }) |
|
|
|
this.store.updateState({ identities, addressTokens }) |
|
|
@ -200,8 +207,14 @@ class PreferencesController { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
return new Promise((resolve, reject) => { |
|
|
|
const address = normalizeAddress(_address) |
|
|
|
const address = normalizeAddress(_address) |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
if (!(address in addressTokens)) addressTokens[address] = [] |
|
|
|
const providerType = this.network.providerStore.getState().type |
|
|
|
const tokens = addressTokens[address] |
|
|
|
|
|
|
|
|
|
|
|
if (!(providerType in addressTokens)) addressTokens[providerType] = {} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!(address in addressTokens[providerType])) addressTokens[providerType][address] = [] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const tokens = addressTokens[providerType][address] |
|
|
|
|
|
|
|
|
|
|
|
this.store.updateState({ selectedAddress: address, tokens }) |
|
|
|
this.store.updateState({ selectedAddress: address, tokens }) |
|
|
|
resolve() |
|
|
|
resolve() |
|
|
|
}) |
|
|
|
}) |
|
|
@ -256,7 +269,10 @@ class PreferencesController { |
|
|
|
|
|
|
|
|
|
|
|
const selectedAddress = this.store.getState().selectedAddress |
|
|
|
const selectedAddress = this.store.getState().selectedAddress |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
addressTokens[selectedAddress] = tokens |
|
|
|
const providerType = this.network.providerStore.getState().type |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (!(providerType in addressTokens)) addressTokens[providerType] = {} |
|
|
|
|
|
|
|
addressTokens[providerType][selectedAddress] = tokens |
|
|
|
this.store.updateState({ addressTokens }) |
|
|
|
this.store.updateState({ addressTokens }) |
|
|
|
|
|
|
|
|
|
|
|
return Promise.resolve(tokens) |
|
|
|
return Promise.resolve(tokens) |
|
|
@ -272,9 +288,9 @@ class PreferencesController { |
|
|
|
removeToken (rawAddress) { |
|
|
|
removeToken (rawAddress) { |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
const selectedAddress = this.store.getState().selectedAddress |
|
|
|
const selectedAddress = this.store.getState().selectedAddress |
|
|
|
|
|
|
|
const providerType = this.network.providerStore.getState().type |
|
|
|
const updatedTokens = addressTokens[selectedAddress].filter(token => token.address !== rawAddress) |
|
|
|
const updatedTokens = addressTokens[providerType][selectedAddress].filter(token => token.address !== rawAddress) |
|
|
|
addressTokens[selectedAddress] = updatedTokens |
|
|
|
addressTokens[providerType][selectedAddress] = updatedTokens |
|
|
|
this.store.updateState({ addressTokens, tokens: updatedTokens }) |
|
|
|
this.store.updateState({ addressTokens, tokens: updatedTokens }) |
|
|
|
|
|
|
|
|
|
|
|
return Promise.resolve(updatedTokens) |
|
|
|
return Promise.resolve(updatedTokens) |
|
|
@ -402,6 +418,26 @@ class PreferencesController { |
|
|
|
//
|
|
|
|
//
|
|
|
|
// PRIVATE METHODS
|
|
|
|
// PRIVATE METHODS
|
|
|
|
//
|
|
|
|
//
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
|
|
|
|
* Getter definition for the `tokens` property of store |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
* @param {object} object Store state |
|
|
|
|
|
|
|
* |
|
|
|
|
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
_defineTokensGetter (object) { |
|
|
|
|
|
|
|
Object.defineProperty(object, 'tokens', { |
|
|
|
|
|
|
|
get: () => { |
|
|
|
|
|
|
|
const selectedAddress = this.store.getState().selectedAddress |
|
|
|
|
|
|
|
const addressTokens = this.store.getState().addressTokens |
|
|
|
|
|
|
|
const providerType = this.network.providerStore.getState().type |
|
|
|
|
|
|
|
if (!(providerType in addressTokens)) addressTokens[providerType] = {} |
|
|
|
|
|
|
|
if (!(selectedAddress in addressTokens[providerType])) return [] |
|
|
|
|
|
|
|
return addressTokens[providerType][selectedAddress] |
|
|
|
|
|
|
|
}, |
|
|
|
|
|
|
|
}) |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
module.exports = PreferencesController |
|
|
|
module.exports = PreferencesController |
|
|
|