Skip to content

Commit b7301b5

Browse files
Merge pull request #322 from reown-com/fix/ethers-balance
fix: clear balance if the sdk fails to get it
2 parents ea2c3d7 + e0b59fb commit b7301b5

File tree

4 files changed

+80
-51
lines changed

4 files changed

+80
-51
lines changed

.changeset/few-cherries-leave.md

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
---
2+
'@reown/appkit-ethers5-react-native': patch
3+
'@reown/appkit-ethers-react-native': patch
4+
'@reown/appkit-wagmi-react-native': patch
5+
'@reown/appkit-auth-ethers-react-native': patch
6+
'@reown/appkit-auth-wagmi-react-native': patch
7+
'@reown/appkit-coinbase-ethers-react-native': patch
8+
'@reown/appkit-coinbase-wagmi-react-native': patch
9+
'@reown/appkit-common-react-native': patch
10+
'@reown/appkit-core-react-native': patch
11+
'@reown/appkit-scaffold-react-native': patch
12+
'@reown/appkit-scaffold-utils-react-native': patch
13+
'@reown/appkit-siwe-react-native': patch
14+
'@reown/appkit-ui-react-native': patch
15+
'@reown/appkit-wallet-react-native': patch
16+
---
17+
18+
fix: clear balance if the sdk fails to get it

packages/ethers/src/client.ts

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -830,29 +830,33 @@ export class AppKit extends AppKitScaffold {
830830
const chain = this.chains.find(c => c.chainId === chainId);
831831
const token = this.options?.tokens?.[chainId];
832832

833-
if (chain) {
834-
const jsonRpcProvider = new JsonRpcProvider(chain.rpcUrl, {
835-
chainId,
836-
name: chain.name
837-
});
833+
try {
834+
if (chain) {
835+
const jsonRpcProvider = new JsonRpcProvider(chain.rpcUrl, {
836+
chainId,
837+
name: chain.name
838+
});
838839

839-
if (jsonRpcProvider) {
840-
if (token) {
841-
// Get balance from custom token address
842-
const erc20 = new Contract(token.address, erc20ABI, jsonRpcProvider);
843-
// @ts-expect-error
844-
const decimals = await erc20.decimals();
845-
// @ts-expect-error
846-
const symbol = await erc20.symbol();
847-
// @ts-expect-error
848-
const balanceOf = await erc20.balanceOf(address);
849-
this.setBalance(formatUnits(balanceOf, decimals), symbol);
850-
} else {
851-
const balance = await jsonRpcProvider.getBalance(address);
852-
const formattedBalance = formatEther(balance);
853-
this.setBalance(formattedBalance, chain.currency);
840+
if (jsonRpcProvider) {
841+
if (token) {
842+
// Get balance from custom token address
843+
const erc20 = new Contract(token.address, erc20ABI, jsonRpcProvider);
844+
// @ts-expect-error
845+
const decimals = await erc20.decimals();
846+
// @ts-expect-error
847+
const symbol = await erc20.symbol();
848+
// @ts-expect-error
849+
const balanceOf = await erc20.balanceOf(address);
850+
this.setBalance(formatUnits(balanceOf, decimals), symbol);
851+
} else {
852+
const balance = await jsonRpcProvider.getBalance(address);
853+
const formattedBalance = formatEther(balance);
854+
this.setBalance(formattedBalance, chain.currency);
855+
}
854856
}
855857
}
858+
} catch {
859+
this.setBalance(undefined, undefined);
856860
}
857861
}
858862
}

packages/ethers5/src/client.ts

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -809,29 +809,32 @@ export class AppKit extends AppKitScaffold {
809809
if (chainId && this.chains) {
810810
const chain = this.chains.find(c => c.chainId === chainId);
811811
const token = this.options?.tokens?.[chainId];
812-
813-
if (chain) {
814-
const jsonRpcProvider = new ethers.providers.JsonRpcProvider(chain.rpcUrl, {
815-
chainId,
816-
name: chain.name
817-
});
818-
if (jsonRpcProvider) {
819-
if (token) {
820-
// Get balance from custom token address
821-
const erc20 = new Contract(token.address, erc20ABI, jsonRpcProvider);
822-
// @ts-expect-error
823-
const decimals = await erc20.decimals();
824-
// @ts-expect-error
825-
const symbol = await erc20.symbol();
826-
// @ts-expect-error
827-
const balanceOf = await erc20.balanceOf(address);
828-
this.setBalance(utils.formatUnits(balanceOf, decimals), symbol);
829-
} else {
830-
const balance = await jsonRpcProvider.getBalance(address);
831-
const formattedBalance = utils.formatEther(balance);
832-
this.setBalance(formattedBalance, chain.currency);
812+
try {
813+
if (chain) {
814+
const jsonRpcProvider = new ethers.providers.JsonRpcProvider(chain.rpcUrl, {
815+
chainId,
816+
name: chain.name
817+
});
818+
if (jsonRpcProvider) {
819+
if (token) {
820+
// Get balance from custom token address
821+
const erc20 = new Contract(token.address, erc20ABI, jsonRpcProvider);
822+
// @ts-expect-error
823+
const decimals = await erc20.decimals();
824+
// @ts-expect-error
825+
const symbol = await erc20.symbol();
826+
// @ts-expect-error
827+
const balanceOf = await erc20.balanceOf(address);
828+
this.setBalance(utils.formatUnits(balanceOf, decimals), symbol);
829+
} else {
830+
const balance = await jsonRpcProvider.getBalance(address);
831+
const formattedBalance = utils.formatEther(balance);
832+
this.setBalance(formattedBalance, chain.currency);
833+
}
833834
}
834835
}
836+
} catch {
837+
this.setBalance(undefined, undefined);
835838
}
836839
}
837840
}

packages/wagmi/src/client.ts

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -519,18 +519,22 @@ export class AppKit extends AppKitScaffold {
519519

520520
private async syncBalance(address: Hex, chainId: number) {
521521
const chain = this.wagmiConfig.chains.find((c: Chain) => c.id === chainId);
522-
if (chain) {
523-
const balance = await getBalance(this.wagmiConfig, {
524-
address,
525-
chainId: chain.id,
526-
token: this.options?.tokens?.[chainId]?.address as Hex
527-
});
528-
const formattedBalance = formatUnits(balance.value, balance.decimals);
529-
this.setBalance(formattedBalance, balance.symbol);
522+
try {
523+
if (chain) {
524+
const balance = await getBalance(this.wagmiConfig, {
525+
address,
526+
chainId: chain.id,
527+
token: this.options?.tokens?.[chainId]?.address as Hex
528+
});
529+
const formattedBalance = formatUnits(balance.value, balance.decimals);
530+
this.setBalance(formattedBalance, balance.symbol);
530531

531-
return;
532+
return;
533+
}
534+
this.setBalance(undefined, undefined);
535+
} catch {
536+
this.setBalance(undefined, undefined);
532537
}
533-
this.setBalance(undefined, undefined);
534538
}
535539

536540
private async syncConnectedWalletInfo(connector: GetAccountReturnType['connector']) {

0 commit comments

Comments
 (0)