From b05e67b4b54258412d359745b1cd095987c67eb6 Mon Sep 17 00:00:00 2001 From: luca Date: Fri, 15 Dec 2023 14:15:38 +0800 Subject: [PATCH] feat: add getCoinGeckoIdByDenom method --- .../utils/__tests__/asset-list-util.test.js | 32 +++++++++++++++++ packages/utils/src/asset-list-util.ts | 34 ++++++++++++++++++- 2 files changed, 65 insertions(+), 1 deletion(-) diff --git a/packages/utils/__tests__/asset-list-util.test.js b/packages/utils/__tests__/asset-list-util.test.js index 8e025a383..c1c94cee3 100644 --- a/packages/utils/__tests__/asset-list-util.test.js +++ b/packages/utils/__tests__/asset-list-util.test.js @@ -8,6 +8,7 @@ import { getAssetByDenom, getChainDenomBySymbol, getDenomByCoinGeckoId, + getCoinGeckoIdByDenom, getExponentByDenom, getSymbolByChainDenom, noDecimals @@ -86,3 +87,34 @@ describe('tests for asset-list-util', () => { expect(re).toEqual('0.000099'); }); }); + +describe('getCoinGeckoIdByDenom', () => { + it('uosmo coingecko id', () => { + const id = getCoinGeckoIdByDenom(assets, 'uosmo'); + expect(id).toEqual('osmosis'); + }); + + it('ujkl coingecko id on testnet', () => { + const id = getCoinGeckoIdByDenom(assets, 'ujkl', { + allowTestnet: true, + excludedChainNames: ['jackal'] + }); + expect(id).toEqual('jackal'); + }); + + it('uluna coingecko id on terra2', () => { + const id = getCoinGeckoIdByDenom(assets, 'uluna', { + excludedChainNames: ['terra'] + }); + expect(id).toEqual('terra-luna-2'); + }); + + it('uusdc coingecko id without traces', () => { + const id = getCoinGeckoIdByDenom(assets, 'uusdc', { + customAssetFilter(asset) { + return !asset.traces; + } + }); + expect(id).toBeNull(); + }); +}); diff --git a/packages/utils/src/asset-list-util.ts b/packages/utils/src/asset-list-util.ts index e52424ca2..cb18995c3 100644 --- a/packages/utils/src/asset-list-util.ts +++ b/packages/utils/src/asset-list-util.ts @@ -1,4 +1,4 @@ -import { Asset, AssetDenomUnit } from '@chain-registry/types'; +import { Asset, AssetList, AssetDenomUnit } from '@chain-registry/types'; import BigNumber from 'bignumber.js'; export type CoinDenom = AssetDenomUnit['denom']; @@ -28,6 +28,38 @@ export function getDenomByCoinGeckoId( return assets.find((asset) => asset.coingecko_id === coinGeckoId).base; } +type GetCoinGeckoIdByDenomOptions = { + allowTestnet?: boolean; + customAssetFilter?: (asset: Asset) => boolean; + excludedChainNames?: string[]; +}; + +export function getCoinGeckoIdByDenom( + assets: AssetList[], + denom: CoinDenom, + { + allowTestnet = false, + customAssetFilter = () => true, + excludedChainNames = [] + }: GetCoinGeckoIdByDenomOptions = {} +): string | null { + const filteredAssetLists = assets.filter(({ chain_name }) => { + return ( + (allowTestnet || !chain_name.includes('testnet')) && + !excludedChainNames.includes(chain_name) + ); + }); + + const filteredAssets = filteredAssetLists + .flatMap(({ assets }) => assets) + .filter(({ coingecko_id }) => coingecko_id) + .filter(customAssetFilter); + + const asset = filteredAssets.find(({ base }) => base === denom); + + return asset?.coingecko_id ?? null; +} + export function getSymbolByChainDenom( assets: Asset[], denom: CoinDenom