forked from MetaMask/metamask-extension
-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #264 from poanetwork/develop
NW release 4.11.0
- Loading branch information
Showing
111 changed files
with
6,654 additions
and
4,773 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,83 @@ | ||
const ObservableStore = require('obs-store') | ||
const extend = require('xtend') | ||
|
||
/** | ||
* @typedef {Object} CachedBalancesOptions | ||
* @property {Object} accountTracker An {@code AccountTracker} reference | ||
* @property {Function} getNetwork A function to get the current network | ||
* @property {Object} initState The initial controller state | ||
*/ | ||
|
||
/** | ||
* Background controller responsible for maintaining | ||
* a cache of account balances in local storage | ||
*/ | ||
class CachedBalancesController { | ||
/** | ||
* Creates a new controller instance | ||
* | ||
* @param {CachedBalancesOptions} [opts] Controller configuration parameters | ||
*/ | ||
constructor (opts = {}) { | ||
const { accountTracker, getNetwork } = opts | ||
|
||
this.accountTracker = accountTracker | ||
this.getNetwork = getNetwork | ||
|
||
const initState = extend({ | ||
cachedBalances: {}, | ||
}, opts.initState) | ||
this.store = new ObservableStore(initState) | ||
|
||
this._registerUpdates() | ||
} | ||
|
||
/** | ||
* Updates the cachedBalances property for the current network. Cached balances will be updated to those in the passed accounts | ||
* if balances in the passed accounts are truthy. | ||
* | ||
* @param {Object} obj The the recently updated accounts object for the current network | ||
* @returns {Promise<void>} | ||
*/ | ||
async updateCachedBalances ({ accounts }) { | ||
const network = await this.getNetwork() | ||
const balancesToCache = await this._generateBalancesToCache(accounts, network) | ||
this.store.updateState({ | ||
cachedBalances: balancesToCache, | ||
}) | ||
} | ||
|
||
_generateBalancesToCache (newAccounts, currentNetwork) { | ||
const { cachedBalances } = this.store.getState() | ||
const currentNetworkBalancesToCache = { ...cachedBalances[currentNetwork] } | ||
|
||
Object.keys(newAccounts).forEach(accountID => { | ||
const account = newAccounts[accountID] | ||
|
||
if (account.balance) { | ||
currentNetworkBalancesToCache[accountID] = account.balance | ||
} | ||
}) | ||
const balancesToCache = { | ||
...cachedBalances, | ||
[currentNetwork]: currentNetworkBalancesToCache, | ||
} | ||
|
||
return balancesToCache | ||
} | ||
|
||
/** | ||
* Sets up listeners and subscriptions which should trigger an update of cached balances. These updates will | ||
* happen when the current account changes. Which happens on block updates, as well as on network and account | ||
* selections. | ||
* | ||
* @private | ||
* | ||
*/ | ||
_registerUpdates () { | ||
const update = this.updateCachedBalances.bind(this) | ||
this.accountTracker.store.subscribe(update) | ||
} | ||
} | ||
|
||
module.exports = CachedBalancesController |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,64 +1,72 @@ | ||
const POA = 'poa' | ||
const DAI = 'dai' | ||
const POA_SOKOL = 'sokol' | ||
const MAINNET = 'mainnet' | ||
const ROPSTEN = 'ropsten' | ||
const RINKEBY = 'rinkeby' | ||
const KOVAN = 'kovan' | ||
const MAINNET = 'mainnet' | ||
const POA_SOKOL = 'sokol' | ||
const POA = 'poa' | ||
const DAI = 'dai' | ||
const GOERLI_TESTNET = 'goerli_testnet' | ||
const LOCALHOST = 'localhost' | ||
|
||
const POA_CODE = 99 | ||
const DAI_CODE = 100 | ||
const POA_SOKOL_CODE = 77 | ||
const MAINNET_CODE = 1 | ||
const ROPSTEN_CODE = 3 | ||
const RINKEYBY_CODE = 4 | ||
const RINKEBY_CODE = 4 | ||
const KOVAN_CODE = 42 | ||
const POA_SOKOL_CODE = 77 | ||
const POA_CODE = 99 | ||
const DAI_CODE = 100 | ||
const GOERLI_TESTNET_CODE = 5 | ||
|
||
const ROPSTEN_DISPLAY_NAME = 'Ropsten' | ||
const RINKEBY_DISPLAY_NAME = 'Rinkeby' | ||
const KOVAN_DISPLAY_NAME = 'Kovan' | ||
const POA_SOKOL_DISPLAY_NAME = 'Sokol' | ||
const POA_DISPLAY_NAME = 'POA Network' | ||
const DAI_DISPLAY_NAME = 'xDai Chain' | ||
const POA_SOKOL_DISPLAY_NAME = 'Sokol' | ||
const MAINNET_DISPLAY_NAME = 'Main Ethereum Network' | ||
const ROPSTEN_DISPLAY_NAME = 'Ropsten' | ||
const RINKEBY_DISPLAY_NAME = 'Rinkeby' | ||
const KOVAN_DISPLAY_NAME = 'Kovan' | ||
const GOERLI_TESTNET_DISPLAY_NAME = 'Görli Testnet' | ||
|
||
const DROPDOWN_ROPSTEN_DISPLAY_NAME = 'Ropsten Test Net' | ||
const DROPDOWN_RINKEBY_DISPLAY_NAME = 'Rinkeby Test Net' | ||
const DROPDOWN_KOVAN_DISPLAY_NAME = 'Kovan Test Net' | ||
const DROPDOWN_POA_SOKOL_DISPLAY_NAME = 'Sokol Network' | ||
const DROPDOWN_POA_DISPLAY_NAME = POA_DISPLAY_NAME | ||
const DROPDOWN_DAI_DISPLAY_NAME = DAI_DISPLAY_NAME | ||
const DROPDOWN_POA_SOKOL_DISPLAY_NAME = 'Sokol Network' | ||
const DROPDOWN_MAINNET_DISPLAY_NAME = 'Main Network' | ||
const DROPDOWN_ROPSTEN_DISPLAY_NAME = 'Ropsten Test Net' | ||
const DROPDOWN_RINKEBY_DISPLAY_NAME = 'Rinkeby Test Net' | ||
const DROPDOWN_KOVAN_DISPLAY_NAME = 'Kovan Test Net' | ||
const DROPDOWN_GOERLI_TESTNET_DISPLAY_NAME = 'Görli Test Net' | ||
|
||
module.exports = { | ||
POA, | ||
DAI, | ||
POA_SOKOL, | ||
MAINNET, | ||
ROPSTEN, | ||
RINKEBY, | ||
KOVAN, | ||
MAINNET, | ||
POA_SOKOL, | ||
POA, | ||
DAI, | ||
GOERLI_TESTNET, | ||
LOCALHOST, | ||
POA_CODE, | ||
DAI_CODE, | ||
POA_SOKOL_CODE, | ||
MAINNET_CODE, | ||
ROPSTEN_CODE, | ||
RINKEYBY_CODE, | ||
RINKEBY_CODE, | ||
KOVAN_CODE, | ||
POA_SOKOL_CODE, | ||
POA_CODE, | ||
DAI_CODE, | ||
GOERLI_TESTNET_CODE, | ||
POA_DISPLAY_NAME, | ||
DAI_DISPLAY_NAME, | ||
POA_SOKOL_DISPLAY_NAME, | ||
MAINNET_DISPLAY_NAME, | ||
ROPSTEN_DISPLAY_NAME, | ||
RINKEBY_DISPLAY_NAME, | ||
KOVAN_DISPLAY_NAME, | ||
MAINNET_DISPLAY_NAME, | ||
POA_SOKOL_DISPLAY_NAME, | ||
POA_DISPLAY_NAME, | ||
DAI_DISPLAY_NAME, | ||
DROPDOWN_ROPSTEN_DISPLAY_NAME, | ||
DROPDOWN_RINKEBY_DISPLAY_NAME, | ||
DROPDOWN_KOVAN_DISPLAY_NAME, | ||
DROPDOWN_POA_SOKOL_DISPLAY_NAME, | ||
GOERLI_TESTNET_DISPLAY_NAME, | ||
DROPDOWN_POA_DISPLAY_NAME, | ||
DROPDOWN_DAI_DISPLAY_NAME, | ||
DROPDOWN_POA_SOKOL_DISPLAY_NAME, | ||
DROPDOWN_MAINNET_DISPLAY_NAME, | ||
DROPDOWN_ROPSTEN_DISPLAY_NAME, | ||
DROPDOWN_RINKEBY_DISPLAY_NAME, | ||
DROPDOWN_KOVAN_DISPLAY_NAME, | ||
DROPDOWN_GOERLI_TESTNET_DISPLAY_NAME, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.