From 0a61381500cc7e7d91e879d027c603bcbe7b19ca Mon Sep 17 00:00:00 2001 From: wenty22 Date: Tue, 17 Dec 2024 16:21:42 +0800 Subject: [PATCH] feat: Use rpc in chainsConfig to get balance --- .release/.changeset/gorgeous-lies-dance.md | 5 +++++ .release/.changeset/pre.json | 1 + apps/canonical-bridge-ui/next-env.d.ts | 2 +- packages/canonical-bridge-widget/CHANGELOG.md | 6 ++++++ packages/canonical-bridge-widget/package.json | 2 +- .../aggregator/components/TokenBalancesProvider.tsx | 3 +++ .../src/modules/aggregator/hooks/useSelection.ts | 3 ++- .../src/modules/aggregator/shared/getTokenBalances.ts | 9 +++++++-- 8 files changed, 26 insertions(+), 5 deletions(-) create mode 100644 .release/.changeset/gorgeous-lies-dance.md diff --git a/.release/.changeset/gorgeous-lies-dance.md b/.release/.changeset/gorgeous-lies-dance.md new file mode 100644 index 00000000..66c2f55f --- /dev/null +++ b/.release/.changeset/gorgeous-lies-dance.md @@ -0,0 +1,5 @@ +--- +"@bnb-chain/canonical-bridge-widget": patch +--- + +Use rpc in chainsConfig to get balance diff --git a/.release/.changeset/pre.json b/.release/.changeset/pre.json index 6431714a..7a2e2ee5 100644 --- a/.release/.changeset/pre.json +++ b/.release/.changeset/pre.json @@ -7,6 +7,7 @@ }, "changesets": [ "cold-baboons-serve", + "gorgeous-lies-dance", "happy-insects-give", "hip-news-reflect", "honest-adults-mate", diff --git a/apps/canonical-bridge-ui/next-env.d.ts b/apps/canonical-bridge-ui/next-env.d.ts index 4f11a03d..a4a7b3f5 100644 --- a/apps/canonical-bridge-ui/next-env.d.ts +++ b/apps/canonical-bridge-ui/next-env.d.ts @@ -2,4 +2,4 @@ /// // NOTE: This file should not be edited -// see https://nextjs.org/docs/basic-features/typescript for more information. +// see https://nextjs.org/docs/pages/building-your-application/configuring/typescript for more information. diff --git a/packages/canonical-bridge-widget/CHANGELOG.md b/packages/canonical-bridge-widget/CHANGELOG.md index 59e7c57f..94ebf961 100644 --- a/packages/canonical-bridge-widget/CHANGELOG.md +++ b/packages/canonical-bridge-widget/CHANGELOG.md @@ -1,5 +1,11 @@ # @bnb-chain/canonical-bridge-widget +## 0.5.14-alpha.5 + +### Patch Changes + +- Use rpc in chainsConfig to get balance + ## 0.5.14-alpha.4 ### Patch Changes diff --git a/packages/canonical-bridge-widget/package.json b/packages/canonical-bridge-widget/package.json index 0ce90003..1417f95b 100644 --- a/packages/canonical-bridge-widget/package.json +++ b/packages/canonical-bridge-widget/package.json @@ -1,6 +1,6 @@ { "name": "@bnb-chain/canonical-bridge-widget", - "version": "0.5.14-alpha.4", + "version": "0.5.14-alpha.5", "description": "canonical bridge widget", "author": "bnb-chain", "private": false, diff --git a/packages/canonical-bridge-widget/src/modules/aggregator/components/TokenBalancesProvider.tsx b/packages/canonical-bridge-widget/src/modules/aggregator/components/TokenBalancesProvider.tsx index 9eafe58c..c06ef30d 100644 --- a/packages/canonical-bridge-widget/src/modules/aggregator/components/TokenBalancesProvider.tsx +++ b/packages/canonical-bridge-widget/src/modules/aggregator/components/TokenBalancesProvider.tsx @@ -11,8 +11,10 @@ import { setIsLoadingTokenBalances, setTokenBalances } from '@/modules/aggregato import { useTronWeb } from '@/core/hooks/useTronWeb'; import { useSolanaAccount } from '@/modules/wallet/hooks/useSolanaAccount'; import { useTronAccount } from '@/modules/wallet/hooks/useTronAccount'; +import { useAggregator } from '@/modules/aggregator/components/AggregatorProvider'; export function TokenBalancesProvider() { + const { chainConfigs } = useAggregator(); const { address } = useAccount(); const { address: solanaAddress } = useSolanaAccount(); const { address: tronAddress } = useTronAccount(); @@ -41,6 +43,7 @@ export function TokenBalancesProvider() { evmParams: { account: address, chain: chains?.find((item) => item.id === fromChain?.id), + chainConfig: chainConfigs?.find((item) => item.id === fromChain?.id), }, solanaParams: { account: solanaAddress, diff --git a/packages/canonical-bridge-widget/src/modules/aggregator/hooks/useSelection.ts b/packages/canonical-bridge-widget/src/modules/aggregator/hooks/useSelection.ts index 47b53301..596ed4cd 100644 --- a/packages/canonical-bridge-widget/src/modules/aggregator/hooks/useSelection.ts +++ b/packages/canonical-bridge-widget/src/modules/aggregator/hooks/useSelection.ts @@ -216,7 +216,7 @@ export function useSelection() { } function useSortedTokens() { - const { transferConfig } = useAggregator(); + const { transferConfig, chainConfigs } = useAggregator(); const { getTokenPrice } = useTokenPrice(); const { address } = useAccount(); @@ -243,6 +243,7 @@ function useSortedTokens() { evmParams: { account: address, chain: chains?.find((item) => item.id === fromChainId), + chainConfig: chainConfigs?.find((item) => item.id === fromChainId), }, solanaParams: { account: solanaAddress, diff --git a/packages/canonical-bridge-widget/src/modules/aggregator/shared/getTokenBalances.ts b/packages/canonical-bridge-widget/src/modules/aggregator/shared/getTokenBalances.ts index 56e698ff..df22c27d 100644 --- a/packages/canonical-bridge-widget/src/modules/aggregator/shared/getTokenBalances.ts +++ b/packages/canonical-bridge-widget/src/modules/aggregator/shared/getTokenBalances.ts @@ -5,7 +5,7 @@ import { Connection, LAMPORTS_PER_SOL, PublicKey } from '@solana/web3.js'; import { TOKEN_PROGRAM_ID } from '@solana/spl-token'; import axios from 'axios'; -import { ChainType, IBridgeToken } from '@/modules/aggregator/types'; +import { ChainType, IBridgeToken, IChainConfig } from '@/modules/aggregator/types'; import { ERC20_TOKEN } from '@/core/contract/abi'; import { isChainOrTokenCompatible } from '@/modules/aggregator/shared/isChainOrTokenCompatible'; import { isSameAddress } from '@/core/utils/address'; @@ -23,6 +23,7 @@ export async function getTokenBalances({ evmParams: { account?: string; chain?: Chain; + chainConfig?: IChainConfig; }; solanaParams: { account?: string; @@ -54,6 +55,7 @@ export async function getTokenBalances({ account: evmParams.account, chain: evmParams.chain, tokens: compatibleTokens, + chainConfig: evmParams.chainConfig, }); } @@ -61,19 +63,22 @@ async function getEvmTokenBalances({ account, chain, tokens, + chainConfig, }: { account?: string; chain?: Chain; tokens?: IBridgeToken[]; + chainConfig?: IChainConfig; }) { try { if (!chain || !account || !tokens?.length) { return {}; } + const rpcUrl = chainConfig?.rpcUrl; const client = createPublicClient({ chain, - transport: http(), + transport: http(rpcUrl), }); const contracts = tokens.map((item) => ({