From ef9702b32e1854adad21a976afa0b83532b5dc32 Mon Sep 17 00:00:00 2001 From: ical10 Date: Wed, 8 Nov 2023 14:28:42 +0700 Subject: [PATCH] feat: Check if originChain matches activeChain for fetching PSP22s --- src/helpers/getPSP22Balances.ts | 163 ++++++++++++++++---------------- src/hooks/usePSP22Balances.ts | 16 +++- 2 files changed, 95 insertions(+), 84 deletions(-) diff --git a/src/helpers/getPSP22Balances.ts b/src/helpers/getPSP22Balances.ts index da35a70..9c56950 100644 --- a/src/helpers/getPSP22Balances.ts +++ b/src/helpers/getPSP22Balances.ts @@ -27,57 +27,57 @@ export const PSP22_TOKEN_BALANCE_SUBSCRIPTION_INTERVAL = 60000 export const getPSP22Balances = async ( api: ApiPromise, address: string | AccountId | undefined, + activeChain: string, formatterOptions?: BalanceFormatterOptions, ): Promise => { const psp22ContractMap: Record = {} - Object.entries(allPSP22Assets).forEach(([slug, tokenInfo]) => { - psp22ContractMap[slug] = new ContractPromise(api, psp22Abi, tokenInfo.metadata?.contractAddress) - }) - if (!address) { - const result = Object.values(allPSP22Assets).map(({ slug, decimals, symbol, iconPath }) => { - return { - tokenSlug: slug, - tokenDecimals: decimals, - tokenSymbol: symbol, - iconPath, - } - }) - + const result = Object.values(allPSP22Assets) + .filter(({ originChain }) => originChain === activeChain) + .map(({ slug, decimals, symbol, iconPath }) => { + return { + tokenSlug: slug, + tokenDecimals: decimals, + tokenSymbol: symbol, + iconPath, + } + }) return result } const result = await Promise.all( - Object.values(allPSP22Assets).map(async ({ slug, decimals, symbol, iconPath }) => { - let balance = new BN(0) - - const contract = psp22ContractMap[slug] - const _balanceOf = await contract.query['psp22::balanceOf']( - address, - { gasLimit: getMaxGasLimit(api) }, - address, - ) - const balanceObj = _balanceOf?.output?.toPrimitive() as Record - - balance = new BN( - _balanceOf.output ? (balanceObj.ok as string) || (balanceObj.Ok as string) : '0', - ) - - const data = { - tokenDecimals: decimals, - tokenSymbol: symbol, - balance, - } - - const balanceFormatted = parsePSP22Balance(data, formatterOptions) - return { - balanceFormatted, - tokenSlug: slug, - iconPath, - ...data, - } - }), + Object.values(allPSP22Assets) + .filter(({ originChain }) => originChain === activeChain) + .map(async ({ slug, decimals, symbol, iconPath }) => { + let balance = new BN(0) + + const contract = psp22ContractMap[slug] + const _balanceOf = await contract.query['psp22::balanceOf']( + address, + { gasLimit: getMaxGasLimit(api) }, + address, + ) + const balanceObj = _balanceOf?.output?.toPrimitive() as Record + + balance = new BN( + _balanceOf.output ? (balanceObj.ok as string) || (balanceObj.Ok as string) : '0', + ) + + const data = { + tokenDecimals: decimals, + tokenSymbol: symbol, + balance, + } + + const balanceFormatted = parsePSP22Balance(data, formatterOptions) + return { + balanceFormatted, + tokenSlug: slug, + iconPath, + ...data, + } + }), ) return result @@ -91,6 +91,7 @@ export const watchPSP22Balances = ( api: ApiPromise, address: string | AccountId | undefined, callback: (data: PSP22BalanceData[]) => void, + activeChain: string, formatterOptions?: BalanceFormatterOptions, ): VoidFunction | null => { const psp22ContractMap: Record = {} @@ -100,14 +101,16 @@ export const watchPSP22Balances = ( }) if (!address) { - const result = Object.values(allPSP22Assets).map(({ slug, decimals, symbol, iconPath }) => { - return { - tokenSlug: slug, - tokenDecimals: decimals, - tokenSymbol: symbol, - iconPath, - } - }) + const result = Object.values(allPSP22Assets) + .filter(({ originChain }) => originChain === activeChain) + .map(({ slug, decimals, symbol, iconPath }) => { + return { + tokenSlug: slug, + tokenDecimals: decimals, + tokenSymbol: symbol, + iconPath, + } + }) callback(result) return null } @@ -116,35 +119,37 @@ export const watchPSP22Balances = ( const fetchTokenBalances = async () => callback( await Promise.all( - Object.values(allPSP22Assets).map(async ({ slug, decimals, symbol, iconPath }) => { - let balance = new BN(0) - - const contract = psp22ContractMap[slug] - const _balanceOf = await contract.query['psp22::balanceOf']( - address, - { gasLimit: getMaxGasLimit(api) }, - address, - ) - const balanceObj = _balanceOf?.output?.toPrimitive() as Record - - balance = new BN( - _balanceOf.output ? (balanceObj.ok as string) || (balanceObj.Ok as string) : '0', - ) - - const data = { - tokenDecimals: decimals, - tokenSymbol: symbol, - balance, - } - - const balanceFormatted = parsePSP22Balance(data, formatterOptions) - return { - balanceFormatted, - tokenSlug: slug, - iconPath, - ...data, - } - }), + Object.values(allPSP22Assets) + .filter(({ originChain }) => originChain === activeChain) + .map(async ({ slug, decimals, symbol, iconPath }) => { + let balance = new BN(0) + + const contract = psp22ContractMap[slug] + const _balanceOf = await contract.query['psp22::balanceOf']( + address, + { gasLimit: getMaxGasLimit(api) }, + address, + ) + const balanceObj = _balanceOf?.output?.toPrimitive() as Record + + balance = new BN( + _balanceOf.output ? (balanceObj.ok as string) || (balanceObj.Ok as string) : '0', + ) + + const data = { + tokenDecimals: decimals, + tokenSymbol: symbol, + balance, + } + + const balanceFormatted = parsePSP22Balance(data, formatterOptions) + return { + balanceFormatted, + tokenSlug: slug, + iconPath, + ...data, + } + }), ), ) diff --git a/src/hooks/usePSP22Balances.ts b/src/hooks/usePSP22Balances.ts index 2865605..1b9c625 100644 --- a/src/hooks/usePSP22Balances.ts +++ b/src/hooks/usePSP22Balances.ts @@ -16,7 +16,7 @@ export const usePSP22Balances = ( watch?: boolean, formatterOptions?: BalanceFormatterOptions, ): PSP22BalanceData[] => { - const { api } = useInkathon() + const { api, activeChain } = useInkathon() const [balanceData, setBalanceData] = useState( [] satisfies PSP22BalanceData[], ) @@ -27,23 +27,29 @@ export const usePSP22Balances = ( setBalanceData(() => data) } - if (!api) { + if (!api || !activeChain) { setBalanceData([] as PSP22BalanceData[]) return } if (watch) { - const unsubscribe = watchPSP22Balances(api, address, updateBalanceData, formatterOptions) + const unsubscribe = watchPSP22Balances( + api, + address, + updateBalanceData, + activeChain.network, + formatterOptions, + ) unsubscribe && setUnsubscribes((prev) => [...prev, unsubscribe]) } else { - getPSP22Balances(api, address, formatterOptions).then(updateBalanceData) + getPSP22Balances(api, address, activeChain.network, formatterOptions).then(updateBalanceData) } return () => { unsubscribes.forEach((unsubscribe) => unsubscribe?.()) setUnsubscribes(() => []) } - }, [api, address]) + }, [api, address, activeChain]) return balanceData }