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,7 +204,12 @@ 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';
if (networkId === LOCALHOST_RPC_URL || networkId === rpcUrl) {
await Promise.all(addresses.map(this._updateAccount.bind(this)));
} else {
switch (chainId) { switch (chainId) {
case CHAIN_IDS.MAINNET: case CHAIN_IDS.MAINNET:
await this._updateAccountsViaBalanceChecker( await this._updateAccountsViaBalanceChecker(
@ -268,6 +278,7 @@ export default class AccountTracker {
await Promise.all(addresses.map(this._updateAccount.bind(this))); await Promise.all(addresses.map(this._updateAccount.bind(this)));
} }
} }
}
/** /**
* Updates the current balance of an account. * Updates the current balance of an account.

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