|
1 | | -import type { Chain } from "../../chains/types.js"; |
2 | 1 | import { |
3 | | - getChainDecimals, |
4 | | - getChainNativeCurrencyName, |
5 | | - getChainSymbol, |
6 | | -} from "../../chains/utils.js"; |
| 2 | + getWalletBalance as apiGetWalletBalance, |
| 3 | + configure, |
| 4 | +} from "@thirdweb-dev/api"; |
| 5 | +import type { Chain } from "../../chains/types.js"; |
7 | 6 | import type { ThirdwebClient } from "../../client/client.js"; |
8 | | -import { getContract } from "../../contract/contract.js"; |
9 | | -import { eth_getBalance } from "../../rpc/actions/eth_getBalance.js"; |
10 | | -import { getRpcClient } from "../../rpc/rpc.js"; |
11 | | -import { toTokens } from "../../utils/units.js"; |
12 | 7 | import type { Account } from "../interfaces/wallet.js"; |
13 | 8 |
|
14 | 9 | type GetTokenBalanceOptions = { |
@@ -43,33 +38,40 @@ export async function getTokenBalance( |
43 | 38 | options: GetTokenBalanceOptions, |
44 | 39 | ): Promise<GetTokenBalanceResult> { |
45 | 40 | const { account, client, chain, tokenAddress } = options; |
46 | | - // erc20 case |
47 | | - if (tokenAddress) { |
48 | | - // load balanceOf dynamically to avoid circular dependency |
49 | | - const { getBalance } = await import( |
50 | | - "../../extensions/erc20/read/getBalance.js" |
51 | | - ); |
52 | | - return getBalance({ |
| 41 | + |
| 42 | + // Configure the API client with credentials from the thirdweb client |
| 43 | + configure({ |
| 44 | + clientId: client.clientId, |
| 45 | + secretKey: client.secretKey, |
| 46 | + }); |
| 47 | + |
| 48 | + const response = await apiGetWalletBalance({ |
| 49 | + path: { |
53 | 50 | address: account.address, |
54 | | - contract: getContract({ address: tokenAddress, chain, client }), |
55 | | - }); |
| 51 | + }, |
| 52 | + query: { |
| 53 | + chainId: [chain.id], |
| 54 | + ...(tokenAddress && { tokenAddress }), |
| 55 | + }, |
| 56 | + }); |
| 57 | + |
| 58 | + if (!response.data?.result || response.data.result.length === 0) { |
| 59 | + throw new Error("No balance data returned from API"); |
56 | 60 | } |
57 | | - // native token case |
58 | | - const rpcRequest = getRpcClient({ chain, client }); |
59 | 61 |
|
60 | | - const [nativeSymbol, nativeDecimals, nativeName, nativeBalance] = |
61 | | - await Promise.all([ |
62 | | - getChainSymbol(chain), |
63 | | - getChainDecimals(chain), |
64 | | - getChainNativeCurrencyName(chain), |
65 | | - eth_getBalance(rpcRequest, { address: account.address }), |
66 | | - ]); |
| 62 | + // Get the first result (should match our chain) |
| 63 | + const balanceData = response.data.result[0]; |
| 64 | + |
| 65 | + if (!balanceData) { |
| 66 | + throw new Error("Balance data not found for the specified chain"); |
| 67 | + } |
67 | 68 |
|
| 69 | + // Transform API response to match the existing GetTokenBalanceResult interface |
68 | 70 | return { |
69 | | - decimals: nativeDecimals, |
70 | | - displayValue: toTokens(nativeBalance, nativeDecimals), |
71 | | - name: nativeName, |
72 | | - symbol: nativeSymbol, |
73 | | - value: nativeBalance, |
| 71 | + decimals: balanceData.decimals, |
| 72 | + displayValue: balanceData.displayValue, |
| 73 | + name: balanceData.name, |
| 74 | + symbol: balanceData.symbol, |
| 75 | + value: BigInt(balanceData.value), |
74 | 76 | }; |
75 | 77 | } |
0 commit comments