From 9ee77194d68366962c2ce718bf713bf3a410c501 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 --- .../hooks/queries/useTotalAssets.ts | 97 +++++++------------ .../asset-list/utils/local-chain-registry.ts | 12 ++- 2 files changed, 48 insertions(+), 61 deletions(-) diff --git a/examples/asset-list/hooks/queries/useTotalAssets.ts b/examples/asset-list/hooks/queries/useTotalAssets.ts index e482b37d5..379f5fadf 100644 --- a/examples/asset-list/hooks/queries/useTotalAssets.ts +++ b/examples/asset-list/hooks/queries/useTotalAssets.ts @@ -1,19 +1,25 @@ +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 { defaultChainName as osmoChainName } from '@/config'; -import { Pool } from 'osmo-query/dist/codegen/osmosis/gamm/pool-models/balancer/balancerPool'; -import { convertGammTokenToDollarValue } from '@/utils'; +import { assets } from '@/utils'; import { useQueryHooks } from './useQueryHooks'; +import { getPoolByGammName, convertGammTokenToDollarValue } from "@osmonauts/math"; +import { Pool } from "osmojs/dist/codegen/osmosis/gamm/v1beta1/balancerPool"; +import { Asset } from '@chain-registry/types'; + (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(), @@ -22,6 +28,7 @@ export const getPagination = (limit: bigint) => ({ reverse: false, }); + export const useTotalAssets = (chainName: string) => { const { address } = useChain(chainName); @@ -109,8 +116,6 @@ export const useTotalAssets = (chainName: string) => { [Key in keyof AllQueries]: NonNullable; }; - const { calcCoinDollarValue } = useChainUtils(chainName); - const zero = new BigNumber(0); const data = useMemo(() => { @@ -121,72 +126,44 @@ export const useTotalAssets = (chainName: string) => { ) as QueriesData; const { - allBalances, - delegations, + allBalances = [], + delegations = [], lockedCoins = [], pools = [], prices = {}, } = queriesData; - const stakedTotal = delegations - ?.map((coin) => calcCoinDollarValue(prices, coin)) - .reduce((total, cur) => total.plus(cur), zero) - .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 balancesTotal = allBalances - ?.filter(({ denom }) => !denom.startsWith('gamm') && prices[denom]) - .map((coin) => calcCoinDollarValue(prices, coin)) - .reduce((total, cur) => total.plus(cur), zero) - .toString(); + let bondedTotal = zero - let bondedTotal; - let liquidityTotal; + let concentrateTotal = zero // liquidity, in osmosis look like named concentrated liquidity pools if (isOsmosisChain) { - const liquidityCoins = (allBalances ?? []).filter(({ denom }) => - denom.startsWith('gamm') - ); - const gammTokenDenoms = [ - ...(liquidityCoins ?? []), - ...(lockedCoins ?? []), - ].map(({ denom }) => denom); - - const uniqueDenoms = [...new Set(gammTokenDenoms)]; - - const poolsMap: Record = pools - .filter(({ totalShares }) => uniqueDenoms.includes(totalShares.denom)) - .filter((pool) => !pool?.$typeUrl?.includes('stableswap')) - .filter(({ poolAssets }) => { - return poolAssets.every(({ token }) => { - const isGammToken = token.denom.startsWith('gamm/pool'); - return !isGammToken && prices[token.denom]; - }); - }) - .reduce((prev, cur) => ({ ...prev, [cur.totalShares.denom]: cur }), {}); - - bondedTotal = lockedCoins - .map((coin) => { - const poolData = poolsMap[coin.denom]; - if (!poolData) return '0'; - return convertGammTokenToDollarValue(coin, poolData, prices); - }) - .reduce((total, cur) => total.plus(cur), zero) - .toString(); - - liquidityTotal = liquidityCoins - .map((coin) => { - const poolData = poolsMap[coin.denom]; - if (!poolData) return '0'; - return convertGammTokenToDollarValue(coin, poolData, prices); - }) - .reduce((total, cur) => total.plus(cur), zero) - .toString(); + bondedTotal = sum( + lockedCoins + .map((coin) => { + const pool = getPoolByGammName(pools, coin.denom) + return convertGammTokenToDollarValue(assets.flatMap(a => a.assets) as Asset[], coin, pool, prices) + }) + ) + + // should have liquidity total too. } - const total = [stakedTotal, balancesTotal, bondedTotal, liquidityTotal] - .reduce((total, cur) => total.plus(cur || 0), zero) - .decimalPlaces(2) - .toString(); + const total = sum([stakedTotal, balancesTotal, bondedTotal]).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 7575913e6..45145962c 100644 --- a/examples/asset-list/utils/local-chain-registry.ts +++ b/examples/asset-list/utils/local-chain-registry.ts @@ -1,6 +1,9 @@ import { assets, chains, ibc } from 'chain-registry' -import { Asset } from '@chain-registry/types' +import { Asset, AssetList } from '@chain-registry/types' import { getChainNameByDenom } from '@chain-registry/utils' +import { DenomPriceMap } from '@chain-registry/utils'; +import { CoinGeckoUSD, PriceHash } from './types'; + export { assets, chains, ibc } @@ -36,3 +39,10 @@ export const isNativeAsset = (targetAsset: Asset, chainName: string) => { } } +export const convertDenomPriceToPriceHash = (geckoPrice: Record): PriceHash => { + const priceHash: PriceHash = {} + for (const [denom, price] of Object.entries(geckoPrice)) { + priceHash[denom] = price.usd + } + return priceHash +}