From 8c96c343915914b3c52a88e2d469d8c2e57ed5d0 Mon Sep 17 00:00:00 2001 From: Lucas Jiang Date: Fri, 19 Apr 2024 15:25:08 +0800 Subject: [PATCH] change to chain registry util function for total assets' s liquidity function --- .../asset-list/hooks/queries/useAssets.ts | 26 ----------- .../hooks/queries/useTotalAssets.ts | 45 +++++++++---------- .../asset-list/utils/local-chain-registry.ts | 1 - 3 files changed, 22 insertions(+), 50 deletions(-) diff --git a/examples/asset-list/hooks/queries/useAssets.ts b/examples/asset-list/hooks/queries/useAssets.ts index c60df35e9..608d14e1f 100644 --- a/examples/asset-list/hooks/queries/useAssets.ts +++ b/examples/asset-list/hooks/queries/useAssets.ts @@ -68,33 +68,7 @@ export const useAssets = (chainName: string) => { }, [chainName, allBalances, priceMap]) -<<<<<<< HEAD const isLoading = [isAllBalanceLoading, isGeckoPricesLading].some(isLoading => isLoading === true) -======= - let displayAmount = '0' - if (assetBySymbol) { - displayAmount = convertBaseUnitToDisplayUnit(assets, symbol, amount) - } - - const prettyChainName = getChainNameByDenom(assets, denom) || '' - - return { - ...asset, - symbol, - logoUrl: asset?.logo_URIs?.png || asset?.logo_URIs?.svg, - prettyChainName, - displayAmount, - dollarValue, - amount, - denom, - }; - }).sort((a, b) => BigNumber(a.displayAmount).lte(b.displayAmount) ? 1 : -1) - } - return [] - }, [topTokens, chainName, allBalances, priceMap]) - - const isLoading = [isAllBalanceLoading, isTopTokensLoading, isGeckoPricesLading].some(isLoading => isLoading === true) ->>>>>>> 2e6b1810 (change to chain registry util function) return { data, isLoading, refetch: refetchAllBalances } } diff --git a/examples/asset-list/hooks/queries/useTotalAssets.ts b/examples/asset-list/hooks/queries/useTotalAssets.ts index b8934d223..65ac6580b 100644 --- a/examples/asset-list/hooks/queries/useTotalAssets.ts +++ b/examples/asset-list/hooks/queries/useTotalAssets.ts @@ -1,16 +1,21 @@ +import { convertBaseUnitToDollarValue } from '@chain-registry/utils'; +import { getAssetByDenom } from '@chain-registry/utils'; import { Coin } from '@cosmjs/stargate'; import { useChain } from '@cosmos-kit/react'; import { UseQueryResult } from '@tanstack/react-query'; import BigNumber from 'bignumber.js'; import { useEffect, useMemo } from 'react'; -import { useChainUtils } from '../useChainUtils'; import { usePrices } from './usePrices'; +import { assets } from '@/utils'; import { useQueryHooks } from './useQueryHooks'; + (BigInt.prototype as any).toJSON = function () { return this.toString(); }; +const sum = (items: (string | number | BigNumber)[]): BigNumber => items.reduce((total: BigNumber, cur) => total.plus(cur), new BigNumber(0)); + export const getPagination = (limit: bigint) => ({ limit, key: new Uint8Array(), @@ -19,6 +24,7 @@ export const getPagination = (limit: bigint) => ({ reverse: false, }); + export const useTotalAssets = (chainName: string) => { const { address } = useChain(chainName); @@ -77,8 +83,6 @@ export const useTotalAssets = (chainName: string) => { [Key in keyof AllQueries]: NonNullable; }; - const { calcCoinDollarValue } = useChainUtils(chainName); - const zero = new BigNumber(0); const data = useMemo(() => { @@ -91,29 +95,24 @@ export const useTotalAssets = (chainName: string) => { const { allBalances = [], delegations = [], - // lockedCoins = [], - // pools = [], prices = {}, } = queriesData; - const stakedTotal = delegations - ?.map((coin) => calcCoinDollarValue(prices, coin as Coin)) - .reduce((total, cur) => total.plus(cur), zero) - .toString(); - - const balancesTotal = allBalances - ?.filter(({ denom }) => !denom.startsWith('gamm') && prices[denom]) - .map((coin) => calcCoinDollarValue(prices, coin)) - .reduce((total, cur) => total.plus(cur), zero) - .toString(); - - let bondedTotal; - let liquidityTotal; - - const total = [stakedTotal, balancesTotal, bondedTotal, liquidityTotal] - .reduce((total, cur) => total.plus(cur || 0), zero) - .decimalPlaces(2) - .toString(); + const stakedTotal = sum( + delegations.map((coin) => { + const asset = getAssetByDenom(assets, coin?.denom || '', chainName) + return convertBaseUnitToDollarValue(assets, prices, asset?.symbol || '', coin?.amount || '', chainName) + }) + ) + + const balancesTotal = sum( + allBalances.map((coin) => { + const asset = getAssetByDenom(assets, coin.denom, chainName) + return convertBaseUnitToDollarValue(assets, prices, asset?.symbol || '', coin.amount, chainName) + }) + ) + + const total = sum([stakedTotal, balancesTotal]).decimalPlaces(2).toString() return { total, diff --git a/examples/asset-list/utils/local-chain-registry.ts b/examples/asset-list/utils/local-chain-registry.ts index 2425c94cd..d0b071107 100644 --- a/examples/asset-list/utils/local-chain-registry.ts +++ b/examples/asset-list/utils/local-chain-registry.ts @@ -12,4 +12,3 @@ export const isNativeAsset = (targetAsset: Asset, chainName: string) => { const nativeAssetList: AssetList[] = getNativeAssetLists(chainName, ibc, assets) return !!nativeAssetList.flatMap(al => al.assets).find(a => a.base === targetAsset.base) } -