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 19, 2024
1 parent 2e6b181 commit 9ee7719
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 61 deletions.
97 changes: 37 additions & 60 deletions examples/asset-list/hooks/queries/useTotalAssets.ts
Original file line number Diff line number Diff line change
@@ -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(),
Expand All @@ -22,6 +28,7 @@ export const getPagination = (limit: bigint) => ({
reverse: false,
});


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

Expand Down Expand Up @@ -109,8 +116,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 @@ -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<string, Pool> = 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,
Expand Down
12 changes: 11 additions & 1 deletion examples/asset-list/utils/local-chain-registry.ts
Original file line number Diff line number Diff line change
@@ -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 }

Expand Down Expand Up @@ -36,3 +39,10 @@ export const isNativeAsset = (targetAsset: Asset, chainName: string) => {
}
}

export const convertDenomPriceToPriceHash = (geckoPrice: Record<string, CoinGeckoUSD>): PriceHash => {
const priceHash: PriceHash = {}
for (const [denom, price] of Object.entries(geckoPrice)) {
priceHash[denom] = price.usd
}
return priceHash
}

0 comments on commit 9ee7719

Please sign in to comment.