Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new utils #58

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading