Skip to content

Commit

Permalink
feat: add getCoinGeckoIdByDenom method
Browse files Browse the repository at this point in the history
  • Loading branch information
marslavish committed Dec 15, 2023
1 parent 899f598 commit b05e67b
Show file tree
Hide file tree
Showing 2 changed files with 65 additions and 1 deletion.
32 changes: 32 additions & 0 deletions packages/utils/__tests__/asset-list-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
getAssetByDenom,
getChainDenomBySymbol,
getDenomByCoinGeckoId,
getCoinGeckoIdByDenom,
getExponentByDenom,
getSymbolByChainDenom,
noDecimals
Expand Down Expand Up @@ -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();
});
});
34 changes: 33 additions & 1 deletion packages/utils/src/asset-list-util.ts
Original file line number Diff line number Diff line change
@@ -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'];
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit b05e67b

Please sign in to comment.