Skip to content

Commit e9913ef

Browse files
committed
fix: use web3 rpc first when fetching balance
1 parent df488d7 commit e9913ef

File tree

1 file changed

+21
-9
lines changed

1 file changed

+21
-9
lines changed

utils/balance-hooks.ts

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { BlockbookAPIURL } from "@contexts/Transfer/constants";
22
import { isValidEthereumAddress } from "@pollum-io/sysweb3-utils";
3+
import { useWeb3 } from "components/Bridge/v3/context/Web";
34

45
import { useQuery } from "react-query";
56

@@ -13,15 +14,26 @@ export const useUtxoBalance = (xpub?: string) =>
1314
return parseInt(balanceInText) / Math.pow(10, 8);
1415
});
1516

16-
export const useNevmBalance = (address?: string) =>
17-
useQuery(["nevm", "balance", address], async () => {
17+
export const useNevmBalance = (address?: string) => {
18+
const web3 = useWeb3();
19+
return useQuery(["nevm", "balance", address], async () => {
1820
if (!address) return Promise.resolve(0);
19-
const url = `https://explorer.syscoin.org/api?module=account&action=eth_get_balance&address=${address}&tag=latest`;
20-
const ethBalanceInHex = await fetch(url)
21-
.then((res) => res.json())
22-
.then((rpcResp) => rpcResp.result);
23-
const valueAsNumber = parseInt(ethBalanceInHex, 16);
24-
if (isNaN(valueAsNumber)) return Promise.resolve(0);
25-
const ethBalance = valueAsNumber / Math.pow(10, 18);
21+
22+
let balRpc = await web3.eth.getBalance(address).then(parseInt).catch(() => undefined);
23+
24+
if (balRpc === undefined) {
25+
const url = `https://explorer.syscoin.org/api?module=account&action=eth_get_balance&address=${address}&tag=latest`;
26+
const ethBalanceInHex = await fetch(url)
27+
.then((res) => res.json())
28+
.then((rpcResp) => rpcResp.result);
29+
const valueAsNumber = parseInt(ethBalanceInHex, 16);
30+
if (isNaN(valueAsNumber)) {
31+
return 0;
32+
}
33+
balRpc = valueAsNumber;
34+
}
35+
36+
const ethBalance = balRpc / Math.pow(10, 18);
2637
return ethBalance;
2738
});
39+
};

0 commit comments

Comments
 (0)