Skip to content

Commit d223605

Browse files
author
jagdeep sidhu
committed
remove stale web3 providers
sysweb3 has a getter on the provider which will reinit to the active network
1 parent 1fcfaf2 commit d223605

File tree

11 files changed

+64
-91
lines changed

11 files changed

+64
-91
lines changed

package.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@
6363
"@heroicons/react": "^1.0.6",
6464
"@reduxjs/toolkit": "^2.8.2",
6565
"@sidhujag/sysweb3-core": "^1.0.27",
66-
"@sidhujag/sysweb3-keyring": "^1.0.574",
66+
"@sidhujag/sysweb3-keyring": "^1.0.579",
6767
"@sidhujag/sysweb3-network": "^1.0.106",
6868
"@sidhujag/sysweb3-utils": "^1.1.271",
6969
"@tippyjs/react": "^4.2.6",
@@ -179,6 +179,7 @@
179179
"node-forge": "^1.3.2",
180180
"valibot": "^1.2.0",
181181
"glob": "^10.5.0",
182+
"test-exclude": "^7.0.0",
182183
"cookie": "^0.7.0",
183184
"tmp": "^0.2.4",
184185
"jws": "^4.0.1",

source/scripts/Background/controllers/MainController.ts

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -3413,14 +3413,11 @@ class MainController {
34133413
try {
34143414
// For background transaction updates, do not toggle UI loading flags
34153415

3416-
// Safe access to transaction objects with error handling
3417-
const web3Provider = this.ethereumTransaction?.web3Provider;
34183416
const txs =
34193417
await this.transactionsManager.utils.updateTransactionsFromCurrentAccount(
34203418
currentAccount,
34213419
isBitcoinBased,
34223420
activeNetwork.url,
3423-
web3Provider,
34243421
currentAccountTxs,
34253422
isPolling,
34263423
isRapidPolling
@@ -4053,16 +4050,11 @@ class MainController {
40534050
const startTime = Date.now();
40544051

40554052
try {
4056-
// Safe access to transaction objects with error handling
4057-
const web3Provider = this.ethereumTransaction?.web3Provider;
4058-
40594053
const updatedBalance =
40604054
await this.balancesManager.utils.getBalanceUpdatedForAccount(
40614055
currentAccount,
40624056
isBitcoinBased,
4063-
activeNetwork.url,
4064-
web3Provider
4065-
// No need to pass a provider - let the manager use its own
4057+
activeNetwork.url
40664058
);
40674059

40684060
// Calculate latency
@@ -5471,16 +5463,10 @@ class MainController {
54715463
throw new Error('NFT validation only supported on EVM networks');
54725464
}
54735465

5474-
const abortController = new AbortController();
5475-
const provider = new CustomJsonRpcProvider(
5476-
abortController.signal,
5477-
activeNetwork.url
5478-
);
5479-
54805466
return this.evmAssetsController.validateNftContract(
54815467
contractAddress,
54825468
walletAddress,
5483-
provider
5469+
this.ethereumTransaction.web3Provider
54845470
);
54855471
}
54865472

@@ -5737,23 +5723,11 @@ class MainController {
57375723
networkUrl: string
57385724
): Promise<string> {
57395725
try {
5740-
// For EVM networks, use the existing web3 provider
5741-
const provider = isBitcoinBased
5742-
? null
5743-
: this.ethereumTransaction.web3Provider;
5744-
5745-
if (!isBitcoinBased && !provider) {
5746-
console.error(
5747-
'[MainController] No web3 provider available for EVM network'
5748-
);
5749-
return '0';
5750-
}
57515726
const balance =
57525727
await this.balancesManager.utils.getBalanceUpdatedForAccount(
57535728
account,
57545729
isBitcoinBased,
5755-
networkUrl,
5756-
provider
5730+
networkUrl
57575731
);
57585732

57595733
return balance;

source/scripts/Background/controllers/assets/evm.ts

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ import isEmpty from 'lodash/isEmpty';
1515

1616
import { BatchBalanceController } from '../balances/BatchBalanceController';
1717
import { Queue } from '../transactions/queue';
18+
import { getController } from 'scripts/Background';
1819
import store from 'state/store';
1920
import {
2021
ITokenEthProps,
@@ -1189,16 +1190,7 @@ const EvmAssetsController = (): IEvmAssetsController => {
11891190
}
11901191
> => {
11911192
try {
1192-
const { activeNetwork } = store.getState().vault;
1193-
1194-
// Create a simple abort controller for the provider
1195-
const abortController = new AbortController();
1196-
1197-
// Create a provider for the current network
1198-
const provider = new CustomJsonRpcProvider(
1199-
abortController.signal,
1200-
activeNetwork.url
1201-
);
1193+
const provider = getController().wallet.ethereumTransaction.web3Provider;
12021194

12031195
// Use the discovery function which handles both ERC-721 and ERC-1155
12041196
const discovery = await discoverNftTokens(

source/scripts/Background/controllers/assets/syscoin.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -318,12 +318,13 @@ const SysAssetsControler = (): ISysAssetsController => {
318318
): Promise<ISysTokensAssetReponse[]> => {
319319
const requestOptions = 'details=tokenBalances&tokens=nonzero';
320320

321-
const { tokensAsset } = await fetchBackendAccountCached(
321+
const accountData = await fetchBackendAccountCached(
322322
ensureTrailingSlash(networkUrl),
323323
xpubOrAddress,
324324
requestOptions,
325325
true
326326
);
327+
const tokensAsset = accountData?.tokensAsset;
327328

328329
// IMPORTANT: For SPT tokens, we should ONLY use tokensAsset array
329330
// tokens array contains regular UTXO addresses without assetGuid

source/scripts/Background/controllers/balances/evm.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,12 @@ const EvmBalanceController = (
1313
currentAccount: IKeyringAccountState
1414
) => {
1515
const provider = web3Provider;
16+
if (!provider) {
17+
console.error(
18+
'[EvmBalanceController] Missing provider in getEvmBalanceForAccount'
19+
);
20+
return '0';
21+
}
1622

1723
const getBalance = await provider.getBalance(currentAccount.address);
1824

source/scripts/Background/controllers/balances/index.ts

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
1-
import {
2-
CustomJsonRpcProvider,
3-
IKeyringAccountState,
4-
} from '@sidhujag/sysweb3-keyring';
1+
import { IKeyringAccountState } from '@sidhujag/sysweb3-keyring';
2+
3+
import { getController } from 'scripts/Background';
54

65
import EvmBalanceController from './evm';
76
import SyscoinBalanceController from './syscoin';
@@ -11,8 +10,7 @@ const BalancesManager = (): IBalancesManager => {
1110
const getBalanceUpdatedForAccount = async (
1211
currentAccount: IKeyringAccountState,
1312
isBitcoinBased: boolean,
14-
networkUrl: string,
15-
provider: CustomJsonRpcProvider
13+
networkUrl: string
1614
) => {
1715
switch (isBitcoinBased) {
1816
case true:
@@ -24,12 +22,18 @@ const BalancesManager = (): IBalancesManager => {
2422
return getSysBalance;
2523

2624
case false:
27-
if (!provider) {
28-
throw new Error('No valid web3Provider for EVM balance fetching');
25+
// Always pull the latest provider at call time (prevents stale references across network switches)
26+
const web3Provider =
27+
getController().wallet.ethereumTransaction.web3Provider;
28+
if (!web3Provider) {
29+
console.error(
30+
'[BalancesManager] No web3 provider available for EVM balance fetch'
31+
);
32+
return '0';
2933
}
3034

3135
// Create EVM controller fresh with current provider
32-
const evmController = EvmBalanceController(provider);
36+
const evmController = EvmBalanceController(web3Provider);
3337
const getEvmBalance = await evmController.getEvmBalanceForAccount(
3438
currentAccount
3539
);

source/scripts/Background/controllers/balances/types.ts

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
1-
import {
2-
CustomJsonRpcProvider,
3-
IKeyringAccountState,
4-
} from '@sidhujag/sysweb3-keyring';
1+
import { IKeyringAccountState } from '@sidhujag/sysweb3-keyring';
52

63
export interface IEvmBalanceController {
74
getEvmBalanceForAccount: (
@@ -20,8 +17,7 @@ export interface IBalancesManagerUtils {
2017
getBalanceUpdatedForAccount: (
2118
currentAccount: IKeyringAccountState,
2219
isBitcoinBased: boolean,
23-
networkUrl: string,
24-
provider: CustomJsonRpcProvider | null
20+
networkUrl: string
2521
) => Promise<string>;
2622
}
2723

source/scripts/Background/controllers/transactions/index.ts

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
1-
import {
2-
CustomJsonRpcProvider,
3-
IKeyringAccountState,
4-
} from '@sidhujag/sysweb3-keyring';
1+
import { IKeyringAccountState } from '@sidhujag/sysweb3-keyring';
52

3+
import { getController } from 'scripts/Background';
64
import store from 'state/store';
75
import { IAccountTransactions } from 'state/vault/types';
86
import { isTransactionInBlock } from 'utils/transactionUtils';
@@ -44,7 +42,6 @@ const TransactionsManager = (): ITransactionsManager => {
4442
currentAccount: IKeyringAccountState,
4543
isBitcoinBased: boolean,
4644
activeNetworkUrl: string,
47-
web3Provider: CustomJsonRpcProvider,
4845
accountTransactions?: IAccountTransactions,
4946
isPolling?: boolean,
5047
isRapidPolling?: boolean
@@ -82,10 +79,9 @@ const TransactionsManager = (): ITransactionsManager => {
8279
activeNetworkUrl
8380
);
8481
} else {
85-
if (!web3Provider) {
86-
console.error('No valid web3Provider for EVM transaction polling');
87-
return [];
88-
}
82+
// Always pull the latest provider at call time (prevents stale references across network switches)
83+
const web3Provider =
84+
getController().wallet.ethereumTransaction.web3Provider;
8985
result = await EvmTransactionsController().pollingEvmTransactions(
9086
web3Provider,
9187
isPolling,

source/scripts/Background/controllers/transactions/syscoin.ts

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,15 @@ const SysTransactionController = (): ISysTransactionsController => {
1414
): Promise<ISysTransaction[]> => {
1515
const requestOptions = 'details=txs&pageSize=30';
1616

17-
const { transactions }: { transactions: ISysTransaction[] } =
18-
await fetchBackendAccountCached(
19-
networkUrl,
20-
xpubOrAddress,
21-
requestOptions,
22-
true
23-
);
17+
const accountData = await fetchBackendAccountCached(
18+
networkUrl,
19+
xpubOrAddress,
20+
requestOptions,
21+
true
22+
);
23+
const transactions = (accountData as any)?.transactions as
24+
| ISysTransaction[]
25+
| undefined;
2426

2527
// Ensure we always return an array, even if transactions is falsy
2628
return Array.isArray(transactions) ? transactions : [];
@@ -81,13 +83,15 @@ const SysTransactionController = (): ISysTransactionsController => {
8183
pageSize: number = 30
8284
): Promise<ISysTransaction[]> => {
8385
const requestOptions = `details=txs&page=${page}&pageSize=${pageSize}`;
84-
const { transactions }: { transactions: ISysTransaction[] } =
85-
await fetchBackendAccountCached(
86-
networkUrl,
87-
xpubOrAddress,
88-
requestOptions,
89-
true
90-
);
86+
const accountData = await fetchBackendAccountCached(
87+
networkUrl,
88+
xpubOrAddress,
89+
requestOptions,
90+
true
91+
);
92+
const transactions = (accountData as any)?.transactions as
93+
| ISysTransaction[]
94+
| undefined;
9195
return Array.isArray(transactions) ? transactions : [];
9296
};
9397

source/scripts/Background/controllers/transactions/types.ts

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -218,7 +218,6 @@ export interface ITransactionsManagerUtils {
218218
currentAccount: IKeyringAccountState,
219219
isBitcoinBased: boolean,
220220
activeNetworkUrl: string,
221-
web3Provider: CustomJsonRpcProvider,
222221
accountTransactions?: IAccountTransactions,
223222
isPolling?: boolean,
224223
isRapidPolling?: boolean

0 commit comments

Comments
 (0)