Skip to content

Commit

Permalink
change to chain registry util function for total assets' s liquidity …
Browse files Browse the repository at this point in the history
…function
  • Loading branch information
cmaaawscodepipelinedeploy committed Apr 22, 2024
1 parent e9cee19 commit 8c96c34
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 50 deletions.
26 changes: 0 additions & 26 deletions examples/asset-list/hooks/queries/useAssets.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
}
45 changes: 22 additions & 23 deletions examples/asset-list/hooks/queries/useTotalAssets.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -19,6 +24,7 @@ export const getPagination = (limit: bigint) => ({
reverse: false,
});


export const useTotalAssets = (chainName: string) => {
const { address } = useChain(chainName);

Expand Down Expand Up @@ -77,8 +83,6 @@ export const useTotalAssets = (chainName: string) => {
[Key in keyof AllQueries]: NonNullable<AllQueries[Key]['data']>;
};

const { calcCoinDollarValue } = useChainUtils(chainName);

const zero = new BigNumber(0);

const data = useMemo(() => {
Expand All @@ -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,
Expand Down
1 change: 0 additions & 1 deletion examples/asset-list/utils/local-chain-registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}

0 comments on commit 8c96c34

Please sign in to comment.