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

[Bug]: Balance does not update on duplicate chainId network #13690 (#14245)

This commit is contained in:
dragana8 2022-11-04 17:29:45 +01:00 committed by GitHub
parent 4245f24a2e
commit 02b34dcd4a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 72 additions and 58 deletions

View File

@ -14,7 +14,10 @@ import log from 'loglevel';
import pify from 'pify'; import pify from 'pify';
import { ethers } from 'ethers'; import { ethers } from 'ethers';
import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi'; import SINGLE_CALL_BALANCES_ABI from 'single-call-balance-checker-abi';
import { CHAIN_IDS } from '../../../shared/constants/network'; import {
CHAIN_IDS,
LOCALHOST_RPC_URL,
} from '../../../shared/constants/network';
import { import {
SINGLE_CALL_BALANCES_ADDRESS, SINGLE_CALL_BALANCES_ADDRESS,
@ -50,6 +53,7 @@ export default class AccountTracker {
* @param {object} opts.provider - An EIP-1193 provider instance that uses the current global network * @param {object} opts.provider - An EIP-1193 provider instance that uses the current global network
* @param {object} opts.blockTracker - A block tracker, which emits events for each new block * @param {object} opts.blockTracker - A block tracker, which emits events for each new block
* @param {Function} opts.getCurrentChainId - A function that returns the `chainId` for the current global network * @param {Function} opts.getCurrentChainId - A function that returns the `chainId` for the current global network
* @param {Function} opts.getNetworkIdentifier - A function that returns the current network
*/ */
constructor(opts = {}) { constructor(opts = {}) {
const initState = { const initState = {
@ -69,6 +73,7 @@ export default class AccountTracker {
// bind function for easier listener syntax // bind function for easier listener syntax
this._updateForBlock = this._updateForBlock.bind(this); this._updateForBlock = this._updateForBlock.bind(this);
this.getCurrentChainId = opts.getCurrentChainId; this.getCurrentChainId = opts.getCurrentChainId;
this.getNetworkIdentifier = opts.getNetworkIdentifier;
this.ethersProvider = new ethers.providers.Web3Provider(this._provider); this.ethersProvider = new ethers.providers.Web3Provider(this._provider);
} }
@ -199,73 +204,79 @@ export default class AccountTracker {
const { accounts } = this.store.getState(); const { accounts } = this.store.getState();
const addresses = Object.keys(accounts); const addresses = Object.keys(accounts);
const chainId = this.getCurrentChainId(); const chainId = this.getCurrentChainId();
const networkId = this.getNetworkIdentifier();
const rpcUrl = 'http://127.0.0.1:8545';
switch (chainId) { if (networkId === LOCALHOST_RPC_URL || networkId === rpcUrl) {
case CHAIN_IDS.MAINNET: await Promise.all(addresses.map(this._updateAccount.bind(this)));
await this._updateAccountsViaBalanceChecker( } else {
addresses, switch (chainId) {
SINGLE_CALL_BALANCES_ADDRESS, case CHAIN_IDS.MAINNET:
); await this._updateAccountsViaBalanceChecker(
break; addresses,
SINGLE_CALL_BALANCES_ADDRESS,
);
break;
case CHAIN_IDS.GOERLI: case CHAIN_IDS.GOERLI:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
addresses, addresses,
SINGLE_CALL_BALANCES_ADDRESS_GOERLI, SINGLE_CALL_BALANCES_ADDRESS_GOERLI,
); );
break; break;
case CHAIN_IDS.SEPOLIA: case CHAIN_IDS.SEPOLIA:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
addresses, addresses,
SINGLE_CALL_BALANCES_ADDRESS_SEPOLIA, SINGLE_CALL_BALANCES_ADDRESS_SEPOLIA,
); );
break; break;
case CHAIN_IDS.BSC: case CHAIN_IDS.BSC:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
addresses, addresses,
SINGLE_CALL_BALANCES_ADDRESS_BSC, SINGLE_CALL_BALANCES_ADDRESS_BSC,
); );
break; break;
case CHAIN_IDS.OPTIMISM: case CHAIN_IDS.OPTIMISM:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
addresses, addresses,
SINGLE_CALL_BALANCES_ADDRESS_OPTIMISM, SINGLE_CALL_BALANCES_ADDRESS_OPTIMISM,
); );
break; break;
case CHAIN_IDS.POLYGON: case CHAIN_IDS.POLYGON:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
addresses, addresses,
SINGLE_CALL_BALANCES_ADDRESS_POLYGON, SINGLE_CALL_BALANCES_ADDRESS_POLYGON,
); );
break; break;
case CHAIN_IDS.AVALANCHE: case CHAIN_IDS.AVALANCHE:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
addresses, addresses,
SINGLE_CALL_BALANCES_ADDRESS_AVALANCHE, SINGLE_CALL_BALANCES_ADDRESS_AVALANCHE,
); );
break; break;
case CHAIN_IDS.FANTOM: case CHAIN_IDS.FANTOM:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
addresses, addresses,
SINGLE_CALL_BALANCES_ADDRESS_FANTOM, SINGLE_CALL_BALANCES_ADDRESS_FANTOM,
); );
break; break;
case CHAIN_IDS.ARBITRUM: case CHAIN_IDS.ARBITRUM:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
addresses, addresses,
SINGLE_CALL_BALANCES_ADDRESS_ARBITRUM, SINGLE_CALL_BALANCES_ADDRESS_ARBITRUM,
); );
break; break;
default: default:
await Promise.all(addresses.map(this._updateAccount.bind(this))); await Promise.all(addresses.map(this._updateAccount.bind(this)));
}
} }
} }

View File

@ -539,6 +539,9 @@ export default class MetamaskController extends EventEmitter {
getCurrentChainId: this.networkController.getCurrentChainId.bind( getCurrentChainId: this.networkController.getCurrentChainId.bind(
this.networkController, this.networkController,
), ),
getNetworkIdentifier: this.networkController.getNetworkIdentifier.bind(
this.networkController,
),
}); });
// start and stop polling for balances based on activeControllerConnections // start and stop polling for balances based on activeControllerConnections