-
Notifications
You must be signed in to change notification settings - Fork 49
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor useAssets with chain-registry util functions
- Loading branch information
1 parent
2b33881
commit b747303
Showing
6 changed files
with
215 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
25 changes: 25 additions & 0 deletions
25
examples/asset-list/hooks/newFunctionPlacedToChainRegistry.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
import { CoinGeckoUSD } from '@/utils/types'; | ||
import { AssetList } from '@chain-registry/types'; | ||
import { DenomPriceMap } from '@chain-registry/utils'; | ||
|
||
// put some may have issue function. will put to chain registry later, we will see. | ||
|
||
// replace mapCoinGeckoPricesToDenoms in chain-registry, use gecko_id to map denom, will have duplicate asset , cause original mapCoinGeckoPricesToDenoms function can not use | ||
export const mapCoinGeckoPricesToDenomsM = (assetLists: AssetList[], prices: Record<string, CoinGeckoUSD>): DenomPriceMap => { | ||
const symbolCoinGeckoIdMap = assetLists.flatMap(a => a.assets).reduce((map: any, asset) => { | ||
if (asset.coingecko_id) { | ||
map[asset.symbol] = prices[asset.coingecko_id] | ||
} | ||
return map | ||
}, {}) | ||
|
||
const final = assetLists.flatMap(a => a.assets).reduce((map: any, asset) => { | ||
if (asset.symbol && symbolCoinGeckoIdMap[asset.symbol]) { | ||
map[asset.base] = symbolCoinGeckoIdMap[asset.symbol].usd | ||
} | ||
return map | ||
}, {}) | ||
|
||
return final | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
import { DenomPriceMap } from '@chain-registry/utils'; | ||
import { useQuery } from '@tanstack/react-query'; | ||
import { handleError } from './useTopTokens'; | ||
import { CoinGeckoUSD } from '@/utils/types'; | ||
import mainnetAssets from 'chain-registry/mainnet/assets' | ||
import { mapCoinGeckoPricesToDenomsM } from '../newFunctionPlacedToChainRegistry'; | ||
import { assets } from '@/utils/local-chain-registry' | ||
|
||
const GECKO_PRICE_QUERY_KEY = 'gecko_prices' | ||
|
||
const getGeckoIds = () => { | ||
const geckoIds: string[] = [] | ||
mainnetAssets.forEach(assetList => { | ||
assetList.assets.forEach(asset => { | ||
if (asset.coingecko_id) { | ||
geckoIds.push(asset.coingecko_id) | ||
} | ||
}) | ||
}) | ||
return geckoIds | ||
} | ||
|
||
const fetchPrices = async ( | ||
geckoIds: string[] | ||
): Promise<Record<string, CoinGeckoUSD>> => { | ||
const url = `https://api.coingecko.com/api/v3/simple/price?ids=${geckoIds.join()}&vs_currencies=usd`; | ||
|
||
return fetch(url) | ||
.then(handleError) | ||
.then((res) => res.json()); | ||
}; | ||
|
||
export const useGeckoPrices = () => { | ||
const geckoIds = getGeckoIds() | ||
return useQuery({ | ||
queryKey: [GECKO_PRICE_QUERY_KEY, geckoIds], | ||
queryFn: () => fetchPrices(geckoIds), | ||
select: (geckoPrices): DenomPriceMap => mapCoinGeckoPricesToDenomsM(assets, geckoPrices), | ||
staleTime: Infinity, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import { useQuery } from '@tanstack/react-query'; | ||
|
||
type Token = { | ||
price: number; | ||
denom: string; | ||
symbol: string; | ||
liquidity: number; | ||
volume_24h: number; | ||
volume_24h_change: number; | ||
name: string; | ||
price_24h_change: number; | ||
price_7d_change: number; | ||
exponent: number; | ||
display: string; | ||
}; | ||
|
||
export const handleError = (resp: Response) => { | ||
if (!resp.ok) throw Error(resp.statusText); | ||
return resp; | ||
}; | ||
|
||
const fetchTokens = async (): Promise<Token[]> => { | ||
const url = 'https://api-osmosis.imperator.co/tokens/v2/all'; | ||
return fetch(url) | ||
.then(handleError) | ||
.then((res) => res.json()); | ||
}; | ||
|
||
const MAX_TOP_TOKENS = 60; | ||
|
||
const filterTopTokens = (tokens: Token[]) => { | ||
return tokens | ||
.sort((a, b) => b.liquidity - a.liquidity) | ||
.slice(0, MAX_TOP_TOKENS) | ||
}; | ||
|
||
export const useOsmosisToken = () => { | ||
return useQuery({ | ||
queryKey: ['osmosis_tokens'], | ||
queryFn: fetchTokens, | ||
select: filterTopTokens, | ||
staleTime: Infinity, | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
import { assets, chains, ibc } from 'chain-registry' | ||
import { Asset } from '@chain-registry/types' | ||
import { getChainNameByDenom } from '@chain-registry/utils' | ||
|
||
export { assets, chains, ibc } | ||
|
||
export const getAssetsByChainName = (chainName: string) => { | ||
return assets.filter(a => a.chain_name === chainName) | ||
} | ||
|
||
export const isNativeAsset = (targetAsset: Asset, chainName: string) => { | ||
const assetsByChain = assets.filter(a => a.chain_name === chainName) | ||
const chain_name = getChainNameByDenom(assetsByChain, targetAsset.base) | ||
switch (true) { | ||
case targetAsset.base.startsWith('factory/'): | ||
return false; | ||
|
||
case targetAsset.base.startsWith('ft') && chain_name === 'bitsong': | ||
return false; | ||
|
||
case targetAsset.base.startsWith('erc20/'): | ||
return true; | ||
|
||
case targetAsset.base.startsWith('ibc/'): | ||
return false; | ||
|
||
case targetAsset.base.startsWith('cw20:'): | ||
return true; | ||
|
||
default: | ||
if (!targetAsset.traces || !targetAsset.traces.length) { | ||
// asset.type_asset = 'sdk.coin' | ||
return true; | ||
} | ||
return false; | ||
} | ||
} | ||
|