From d415f33dc96651e5bb4ede689220d89ad11fe9d9 Mon Sep 17 00:00:00 2001 From: luca Date: Mon, 18 Dec 2023 22:53:31 +0800 Subject: [PATCH 1/5] feat: add chainName as optional param --- .../utils/__tests__/asset-list-util.test.js | 73 ++++++++-------- packages/utils/src/asset-list-util.ts | 87 +++++++++++-------- 2 files changed, 92 insertions(+), 68 deletions(-) diff --git a/packages/utils/__tests__/asset-list-util.test.js b/packages/utils/__tests__/asset-list-util.test.js index c1c94cee3..a4940ebc9 100644 --- a/packages/utils/__tests__/asset-list-util.test.js +++ b/packages/utils/__tests__/asset-list-util.test.js @@ -15,39 +15,44 @@ import { } from '../src'; describe('tests for asset-list-util', () => { - let testAssets = []; - - beforeAll(() => { - testAssets = assets.find((it) => it.chain_name === 'osmosis')?.assets || []; - }); - - it('get osmosis asset by denom', () => { - const asset = getAssetByDenom(testAssets, 'uosmo'); - expect(asset.name).toEqual('Osmosis'); + const osmosisAssetList = assets.filter( + ({ chain_name }) => chain_name === 'osmosis' + ); + + it('getAssetByDenom', () => { + expect(() => getAssetByDenom(assets, 'uosmo')).toThrowError(); + const asset = getAssetByDenom(assets, 'uosmo', 'osmosis'); + expect(asset.base).toEqual('uosmo'); }); - it('get osmosis denom by geckoId', () => { - const denom = getDenomByCoinGeckoId(testAssets, 'osmosis'); - expect(denom).toEqual('uosmo'); + it('getDenomByCoinGeckoId', () => { + const denom1 = getDenomByCoinGeckoId(assets, 'jackal'); + expect(denom1).toEqual('ujkl'); + const denom2 = getDenomByCoinGeckoId(assets, 'stargaze', 'stargaze'); + expect(denom2).toEqual('ustars'); }); - it('get osmosis symbol by denom', () => { - const token = getSymbolByChainDenom(testAssets, 'uosmo'); - expect(token).toEqual('OSMO'); + it('getSymbolByChainDenom', () => { + const denom1 = getSymbolByChainDenom(assets, 'swth'); + expect(denom1).toEqual('SWTH'); + const denom2 = getSymbolByChainDenom(assets, 'uusdc', 'axelar'); + expect(denom2).toEqual('USDC'); }); - it('get osmosis denom by symbol', () => { - const denom = getChainDenomBySymbol(testAssets, 'OSMO'); - expect(denom).toEqual('uosmo'); + it('getChainDenomBySymbol', () => { + const denom1 = getChainDenomBySymbol(assets, 'OCTA'); + expect(denom1).toEqual('uocta'); + const denom2 = getChainDenomBySymbol(assets, 'NOM', 'nomic'); + expect(denom2).toEqual('unom'); }); - it('get osmosis exponent by denom', () => { - const exponent = getExponentByDenom(testAssets, 'uosmo'); + it('getExponentByDenom', () => { + const exponent = getExponentByDenom(assets, 'uosmo', 'osmosis'); expect(exponent).toEqual(6); }); - it('convert osmosis gecko price to denom price map', () => { - const priceMap = convertCoinGeckoPricesToDenomPriceMap(testAssets, { + it('convertCoinGeckoPricesToDenomPriceMap', () => { + const priceMap = convertCoinGeckoPricesToDenomPriceMap(osmosisAssetList, { osmosis: { usd: 0.498124 } @@ -55,36 +60,36 @@ describe('tests for asset-list-util', () => { expect(priceMap.uosmo).toEqual(0.498124); }); - it('convert number no decimals', () => { + it('noDecimals', () => { const re1 = noDecimals(1.12); expect(re1).toEqual('1'); const re2 = noDecimals(1.67); expect(re2).toEqual('1'); }); - it('convert osmosis base units to dollar value', () => { - const re = convertBaseUnitsToDollarValue( - testAssets, + it('convertBaseUnitsToDollarValue', () => { + const value = convertBaseUnitsToDollarValue( + osmosisAssetList, { uosmo: 1 }, 'OSMO', 5 ); - expect(re).toEqual('0.000005'); + expect(value).toEqual('0.000005'); }); - it('convert doller value to osmosis denom units', () => { - const re = convertDollarValueToDenomUnits( - testAssets, + it('convertDollarValueToDenomUnits', () => { + const value = convertDollarValueToDenomUnits( + osmosisAssetList, { uosmo: 1 }, 'OSMO', 0.00001 ); - expect(re).toEqual('10'); + expect(value).toEqual('10'); }); - it('convert osmosis base units to display units', () => { - const re = convertBaseUnitsToDisplayUnits(testAssets, 'OSMO', 99); - expect(re).toEqual('0.000099'); + it('convertBaseUnitsToDisplayUnits', () => { + const value = convertBaseUnitsToDisplayUnits(osmosisAssetList, 'OSMO', 99); + expect(value).toEqual('0.000099'); }); }); diff --git a/packages/utils/src/asset-list-util.ts b/packages/utils/src/asset-list-util.ts index cb18995c3..8c518e259 100644 --- a/packages/utils/src/asset-list-util.ts +++ b/packages/utils/src/asset-list-util.ts @@ -13,19 +13,46 @@ export interface PriceHash { [key: CoinDenom]: number; } -export function getAssetByDenom(assets: Asset[], denom: CoinDenom): Asset { - const asset = assets.find((asset) => asset.base === denom); - if (!asset) { - throw new Error(`Asset not found: ${denom}`); +function getAssetByKeyValue( + assets: AssetList[], + key: keyof Asset, + value: string, + chainName?: string +): Asset { + const filteredAssets = assets + .filter(({ chain_name }) => !chainName || chain_name === chainName) + .flatMap(({ assets }) => assets); + + const matchingAssets = filteredAssets.filter((asset) => asset[key] === value); + + if (matchingAssets.length === 0) { + throw new Error(`No asset found for ${key} '${value}'`); + } + + if (matchingAssets.length > 1) { + throw new Error( + `Ambiguity: ${matchingAssets.length} assets found for ${key} '${value}'` + ); } - return asset; + + return matchingAssets[0]; +} + +export function getAssetByDenom( + assets: AssetList[], + denom: CoinDenom, + chainName?: string +): Asset { + return getAssetByKeyValue(assets, 'base', denom, chainName); } export function getDenomByCoinGeckoId( - assets: Asset[], - coinGeckoId: string + assets: AssetList[], + coinGeckoId: string, + chainName?: string ): CoinDenom { - return assets.find((asset) => asset.coingecko_id === coinGeckoId).base; + return getAssetByKeyValue(assets, 'coingecko_id', coinGeckoId, chainName) + .base; } type GetCoinGeckoIdByDenomOptions = { @@ -61,44 +88,36 @@ export function getCoinGeckoIdByDenom( } export function getSymbolByChainDenom( - assets: Asset[], - denom: CoinDenom + assets: AssetList[], + denom: CoinDenom, + chainName?: string ): string { - const asset = getAssetByDenom(assets, denom); - const symbol = asset.symbol; - if (!symbol) { - return denom; - } - return symbol; + return getAssetByDenom(assets, denom, chainName).symbol; } export function getChainDenomBySymbol( - assets: Asset[], - token: string + assets: AssetList[], + symbol: string, + chainName?: string ): CoinDenom { - const asset = assets.find(({ symbol }) => symbol === token); - const base = asset?.base; - if (!base) { - console.log(`cannot find base for token ${token}`); - return null; - } - return base; + return getAssetByKeyValue(assets, 'symbol', symbol, chainName).base; } export function getExponentByDenom( - assets: Asset[], - denom: CoinDenom + assets: AssetList[], + denom: CoinDenom, + chainName?: string ): Exponent { - const asset = getAssetByDenom(assets, denom); + const asset = getAssetByDenom(assets, denom, chainName); const unit = asset.denom_units.find(({ denom }) => denom === asset.display); - return unit?.exponent || 0; + return unit?.exponent ?? 0; } export function convertCoinGeckoPricesToDenomPriceMap( - assets: Asset[], + assets: AssetList[], prices: Record ): PriceHash { - return Object.keys(prices).reduce((res, geckoId) => { + return Object.keys(prices).reduce((res: PriceHash, geckoId) => { const denom = getDenomByCoinGeckoId(assets, geckoId); res[denom] = prices[geckoId].usd; return res; @@ -110,7 +129,7 @@ export function noDecimals(num: number | string): string { } export function convertBaseUnitsToDollarValue( - assets: Asset[], + assets: AssetList[], prices: PriceHash, symbol: string, amount: string | number @@ -123,7 +142,7 @@ export function convertBaseUnitsToDollarValue( } export function convertDollarValueToDenomUnits( - assets: Asset[], + assets: AssetList[], prices: PriceHash, symbol: string, value: string | number @@ -136,7 +155,7 @@ export function convertDollarValueToDenomUnits( } export function convertBaseUnitsToDisplayUnits( - assets: Asset[], + assets: AssetList[], symbol: string, amount: string | number ): string { From baa8d1c224bfccbc5819e22a7e24aee28851ebb8 Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Mon, 18 Dec 2023 16:08:50 -0800 Subject: [PATCH 2/5] spacing --- packages/utils/__tests__/asset-list-util.test.js | 2 +- packages/utils/src/asset-list-util.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/packages/utils/__tests__/asset-list-util.test.js b/packages/utils/__tests__/asset-list-util.test.js index a4940ebc9..24e3ccf36 100644 --- a/packages/utils/__tests__/asset-list-util.test.js +++ b/packages/utils/__tests__/asset-list-util.test.js @@ -7,8 +7,8 @@ import { convertDollarValueToDenomUnits, getAssetByDenom, getChainDenomBySymbol, - getDenomByCoinGeckoId, getCoinGeckoIdByDenom, + getDenomByCoinGeckoId, getExponentByDenom, getSymbolByChainDenom, noDecimals diff --git a/packages/utils/src/asset-list-util.ts b/packages/utils/src/asset-list-util.ts index 8c518e259..20f9963c3 100644 --- a/packages/utils/src/asset-list-util.ts +++ b/packages/utils/src/asset-list-util.ts @@ -1,4 +1,4 @@ -import { Asset, AssetList, AssetDenomUnit } from '@chain-registry/types'; +import { Asset, AssetDenomUnit, AssetList } from '@chain-registry/types'; import BigNumber from 'bignumber.js'; export type CoinDenom = AssetDenomUnit['denom']; From 693050e6effc2d764204799154a4a90bc355acba Mon Sep 17 00:00:00 2001 From: Dan Lynch Date: Mon, 18 Dec 2023 16:45:15 -0800 Subject: [PATCH 3/5] add client tests --- packages/client/__tests__/client.api.test.ts | 87 ++++++++++++++++++++ packages/client/src/chain-util.ts | 44 +++++++--- packages/utils/src/asset-list-util.ts | 20 +++-- packages/utils/types/asset-list-util.d.ts | 28 ++++--- 4 files changed, 152 insertions(+), 27 deletions(-) create mode 100644 packages/client/__tests__/client.api.test.ts diff --git a/packages/client/__tests__/client.api.test.ts b/packages/client/__tests__/client.api.test.ts new file mode 100644 index 000000000..3fb0ccdcf --- /dev/null +++ b/packages/client/__tests__/client.api.test.ts @@ -0,0 +1,87 @@ +import { assets, chains } from 'chain-registry'; + +import { + ChainRegistryChainUtil, + ChainRegistryChainUtilOptions, + ChainRegistryClient, + ChainRegistryClientOptions +} from '../src'; + +describe('tests for asset-list-util', () => { + const assetLists = assets.filter( + ({ chain_name }) => chain_name === 'osmosis' + ); + const chainInfos = chains.filter( + ({ chain_name }) => chain_name === 'osmosis' + ); + + const regOptions: ChainRegistryClientOptions = { + chainNames: ['osmosis'], + chains: chainInfos, + assetLists + }; + + const regClient = new ChainRegistryClient(regOptions); + const chainInfo = regClient.getChainInfo('osmosis'); + const options: ChainRegistryChainUtilOptions = { + chainInfo, + chainName: 'osmosis' + }; + const client = new ChainRegistryChainUtil(options); + + it('getAssetByDenom', () => { + expect(() => client.getAssetByDenom('uosmo')).toThrowError(); + const asset = client.getAssetByDenom('uosmo'); + expect(asset.base).toEqual('uosmo'); + }); + + it('getDenomByCoinGeckoId', () => { + const denom1 = client.getDenomByCoinGeckoId('jackal'); + expect(denom1).toEqual('ujkl'); + const denom2 = client.getDenomByCoinGeckoId('stargaze'); + expect(denom2).toEqual('ustars'); + }); + + it('getSymbolByChainDenom', () => { + const denom1 = client.getSymbolByChainDenom('swth'); + expect(denom1).toEqual('SWTH'); + const denom2 = client.getSymbolByChainDenom('uusdc'); + expect(denom2).toEqual('USDC'); + }); + + it('getChainDenomBySymbol', () => { + const denom1 = client.getChainDenomBySymbol('OCTA'); + expect(denom1).toEqual('uocta'); + const denom2 = client.getChainDenomBySymbol('NOM'); + expect(denom2).toEqual('unom'); + }); + + it('getExponentByDenom', () => { + const exponent = client.getExponentByDenom('uosmo'); + expect(exponent).toEqual(6); + }); + + it('convertBaseUnitsToDollarValue', () => { + const value = client.convertBaseUnitsToDollarValue({ uosmo: 1 }, 'OSMO', 5); + expect(value).toEqual('0.000005'); + }); + + it('convertDollarValueToDenomUnits', () => { + const value = client.convertDollarValueToDenomUnits( + { uosmo: 1 }, + 'OSMO', + 0.00001 + ); + expect(value).toEqual('10'); + }); + + it('convertBaseUnitsToDisplayUnits', () => { + const value = client.convertBaseUnitsToDisplayUnits('OSMO', 99); + expect(value).toEqual('0.000099'); + }); + + it('uosmo coingecko id', () => { + const id = client.getCoinGeckoIdByDenom('uosmo'); + expect(id).toEqual('osmosis'); + }); +}); diff --git a/packages/client/src/chain-util.ts b/packages/client/src/chain-util.ts index 3ae332bf6..d1cebe911 100644 --- a/packages/client/src/chain-util.ts +++ b/packages/client/src/chain-util.ts @@ -1,4 +1,4 @@ -import { Asset } from '@chain-registry/types'; +import { Asset, AssetList } from '@chain-registry/types'; import type { CoinDenom, CoinGeckoUSD, @@ -12,6 +12,7 @@ import { convertDollarValueToDenomUnits, getAssetByDenom, getChainDenomBySymbol, + getCoinGeckoIdByDenom, getDenomByCoinGeckoId, getExponentByDenom, getSymbolByChainDenom, @@ -28,7 +29,7 @@ export interface ChainRegistryChainUtilOptions { export class ChainRegistryChainUtil { chainName: string; chainInfo: ChainInfo; - allAsset: Asset[]; + allAsset: AssetList[]; constructor(options: ChainRegistryChainUtilOptions) { this.chainName = options.chainName; @@ -43,23 +44,29 @@ export class ChainRegistryChainUtil { } getAssetByDenom(denom: CoinDenom): Asset { - return getAssetByDenom(this.allAsset, denom); + return getAssetByDenom(this.allAsset, denom, this.chainName); } getDenomByCoinGeckoId(coinGeckoId: string): CoinDenom { - return getDenomByCoinGeckoId(this.allAsset, coinGeckoId); + return getDenomByCoinGeckoId(this.allAsset, coinGeckoId, this.chainName); + } + + getCoinGeckoIdByDenom(coinGeckoId: string): CoinDenom { + return getCoinGeckoIdByDenom(this.allAsset, coinGeckoId, { + chainName: this.chainName + }); } getSymbolByChainDenom(denom: CoinDenom): string { - return getSymbolByChainDenom(this.allAsset, denom); + return getSymbolByChainDenom(this.allAsset, denom, this.chainName); } getChainDenomBySymbol(token: string): CoinDenom { - return getChainDenomBySymbol(this.allAsset, token); + return getChainDenomBySymbol(this.allAsset, token, this.chainName); } getExponentByDenom(denom: CoinDenom): Exponent { - return getExponentByDenom(this.allAsset, denom); + return getExponentByDenom(this.allAsset, denom, this.chainName); } convertCoinGeckoPricesToDenomPriceMap( @@ -77,7 +84,13 @@ export class ChainRegistryChainUtil { symbol: string, amount: string | number ): string { - return convertBaseUnitsToDollarValue(this.allAsset, prices, symbol, amount); + return convertBaseUnitsToDollarValue( + this.allAsset, + prices, + symbol, + amount, + this.chainName + ); } convertDollarValueToDenomUnits( @@ -85,13 +98,24 @@ export class ChainRegistryChainUtil { symbol: string, value: string | number ): string { - return convertDollarValueToDenomUnits(this.allAsset, prices, symbol, value); + return convertDollarValueToDenomUnits( + this.allAsset, + prices, + symbol, + value, + this.chainName + ); } convertBaseUnitsToDisplayUnits( symbol: string, amount: string | number ): string { - return convertBaseUnitsToDisplayUnits(this.allAsset, symbol, amount); + return convertBaseUnitsToDisplayUnits( + this.allAsset, + symbol, + amount, + this.chainName + ); } } diff --git a/packages/utils/src/asset-list-util.ts b/packages/utils/src/asset-list-util.ts index 20f9963c3..b56889aea 100644 --- a/packages/utils/src/asset-list-util.ts +++ b/packages/utils/src/asset-list-util.ts @@ -56,6 +56,7 @@ export function getDenomByCoinGeckoId( } type GetCoinGeckoIdByDenomOptions = { + chainName?: string; allowTestnet?: boolean; customAssetFilter?: (asset: Asset) => boolean; excludedChainNames?: string[]; @@ -65,6 +66,7 @@ export function getCoinGeckoIdByDenom( assets: AssetList[], denom: CoinDenom, { + chainName, allowTestnet = false, customAssetFilter = () => true, excludedChainNames = [] @@ -72,6 +74,7 @@ export function getCoinGeckoIdByDenom( ): string | null { const filteredAssetLists = assets.filter(({ chain_name }) => { return ( + (!chainName || chainName == chainName) && (allowTestnet || !chain_name.includes('testnet')) && !excludedChainNames.includes(chain_name) ); @@ -132,9 +135,10 @@ export function convertBaseUnitsToDollarValue( assets: AssetList[], prices: PriceHash, symbol: string, - amount: string | number + amount: string | number, + chainName?: string ): string { - const denom = getChainDenomBySymbol(assets, symbol); + const denom = getChainDenomBySymbol(assets, symbol, chainName); return new BigNumber(amount) .shiftedBy(-getExponentByDenom(assets, denom)) .multipliedBy(prices[denom]) @@ -145,9 +149,10 @@ export function convertDollarValueToDenomUnits( assets: AssetList[], prices: PriceHash, symbol: string, - value: string | number + value: string | number, + chainName?: string ): string { - const denom = getChainDenomBySymbol(assets, symbol); + const denom = getChainDenomBySymbol(assets, symbol, chainName); return new BigNumber(value) .dividedBy(prices[denom]) .shiftedBy(getExponentByDenom(assets, denom)) @@ -157,10 +162,11 @@ export function convertDollarValueToDenomUnits( export function convertBaseUnitsToDisplayUnits( assets: AssetList[], symbol: string, - amount: string | number + amount: string | number, + chainName?: string ): string { - const denom = getChainDenomBySymbol(assets, symbol); + const denom = getChainDenomBySymbol(assets, symbol, chainName); return new BigNumber(amount) - .shiftedBy(-getExponentByDenom(assets, denom)) + .shiftedBy(-getExponentByDenom(assets, denom, chainName)) .toString(); } diff --git a/packages/utils/types/asset-list-util.d.ts b/packages/utils/types/asset-list-util.d.ts index 390a2c0b3..d7f7fbf44 100644 --- a/packages/utils/types/asset-list-util.d.ts +++ b/packages/utils/types/asset-list-util.d.ts @@ -1,4 +1,4 @@ -import { Asset, AssetDenomUnit } from '@chain-registry/types'; +import { Asset, AssetDenomUnit, AssetList } from '@chain-registry/types'; export type CoinDenom = AssetDenomUnit['denom']; export type Exponent = AssetDenomUnit['exponent']; export interface CoinGeckoUSD { @@ -7,13 +7,21 @@ export interface CoinGeckoUSD { export interface PriceHash { [key: CoinDenom]: number; } -export declare function getAssetByDenom(assets: Asset[], denom: CoinDenom): Asset; -export declare function getDenomByCoinGeckoId(assets: Asset[], coinGeckoId: string): CoinDenom; -export declare function getSymbolByChainDenom(assets: Asset[], denom: CoinDenom): string; -export declare function getChainDenomBySymbol(assets: Asset[], token: string): CoinDenom; -export declare function getExponentByDenom(assets: Asset[], denom: CoinDenom): Exponent; -export declare function convertCoinGeckoPricesToDenomPriceMap(assets: Asset[], prices: Record): PriceHash; +export declare function getAssetByDenom(assets: AssetList[], denom: CoinDenom, chainName?: string): Asset; +export declare function getDenomByCoinGeckoId(assets: AssetList[], coinGeckoId: string, chainName?: string): CoinDenom; +type GetCoinGeckoIdByDenomOptions = { + chainName?: string; + allowTestnet?: boolean; + customAssetFilter?: (asset: Asset) => boolean; + excludedChainNames?: string[]; +}; +export declare function getCoinGeckoIdByDenom(assets: AssetList[], denom: CoinDenom, { chainName, allowTestnet, customAssetFilter, excludedChainNames }?: GetCoinGeckoIdByDenomOptions): string | null; +export declare function getSymbolByChainDenom(assets: AssetList[], denom: CoinDenom, chainName?: string): string; +export declare function getChainDenomBySymbol(assets: AssetList[], symbol: string, chainName?: string): CoinDenom; +export declare function getExponentByDenom(assets: AssetList[], denom: CoinDenom, chainName?: string): Exponent; +export declare function convertCoinGeckoPricesToDenomPriceMap(assets: AssetList[], prices: Record): PriceHash; export declare function noDecimals(num: number | string): string; -export declare function convertBaseUnitsToDollarValue(assets: Asset[], prices: PriceHash, symbol: string, amount: string | number): string; -export declare function convertDollarValueToDenomUnits(assets: Asset[], prices: PriceHash, symbol: string, value: string | number): string; -export declare function convertBaseUnitsToDisplayUnits(assets: Asset[], symbol: string, amount: string | number): string; +export declare function convertBaseUnitsToDollarValue(assets: AssetList[], prices: PriceHash, symbol: string, amount: string | number, chainName?: string): string; +export declare function convertDollarValueToDenomUnits(assets: AssetList[], prices: PriceHash, symbol: string, value: string | number, chainName?: string): string; +export declare function convertBaseUnitsToDisplayUnits(assets: AssetList[], symbol: string, amount: string | number, chainName?: string): string; +export {}; From 24aa0b8ccca5bc41b901174c43f0f04cccb69a8b Mon Sep 17 00:00:00 2001 From: luca Date: Tue, 19 Dec 2023 22:06:52 +0800 Subject: [PATCH 4/5] fix client tests not passing --- packages/client/__tests__/client.api.test.ts | 25 ++++++++++---------- packages/client/src/chain-util.ts | 16 +++++++------ packages/utils/src/asset-list-util.ts | 6 ++--- 3 files changed, 24 insertions(+), 23 deletions(-) diff --git a/packages/client/__tests__/client.api.test.ts b/packages/client/__tests__/client.api.test.ts index 3fb0ccdcf..98442516f 100644 --- a/packages/client/__tests__/client.api.test.ts +++ b/packages/client/__tests__/client.api.test.ts @@ -30,30 +30,29 @@ describe('tests for asset-list-util', () => { const client = new ChainRegistryChainUtil(options); it('getAssetByDenom', () => { - expect(() => client.getAssetByDenom('uosmo')).toThrowError(); const asset = client.getAssetByDenom('uosmo'); expect(asset.base).toEqual('uosmo'); }); it('getDenomByCoinGeckoId', () => { - const denom1 = client.getDenomByCoinGeckoId('jackal'); - expect(denom1).toEqual('ujkl'); - const denom2 = client.getDenomByCoinGeckoId('stargaze'); - expect(denom2).toEqual('ustars'); + const denom1 = client.getDenomByCoinGeckoId('osmosis'); + expect(denom1).toEqual('uosmo'); + const denom2 = client.getDenomByCoinGeckoId('ion'); + expect(denom2).toEqual('uion'); }); it('getSymbolByChainDenom', () => { - const denom1 = client.getSymbolByChainDenom('swth'); - expect(denom1).toEqual('SWTH'); - const denom2 = client.getSymbolByChainDenom('uusdc'); - expect(denom2).toEqual('USDC'); + const denom1 = client.getSymbolByChainDenom('uosmo'); + expect(denom1).toEqual('OSMO'); + const denom2 = client.getSymbolByChainDenom('uion'); + expect(denom2).toEqual('ION'); }); it('getChainDenomBySymbol', () => { - const denom1 = client.getChainDenomBySymbol('OCTA'); - expect(denom1).toEqual('uocta'); - const denom2 = client.getChainDenomBySymbol('NOM'); - expect(denom2).toEqual('unom'); + const denom1 = client.getChainDenomBySymbol('OSMO'); + expect(denom1).toEqual('uosmo'); + const denom2 = client.getChainDenomBySymbol('ION'); + expect(denom2).toEqual('uion'); }); it('getExponentByDenom', () => { diff --git a/packages/client/src/chain-util.ts b/packages/client/src/chain-util.ts index d1cebe911..042747372 100644 --- a/packages/client/src/chain-util.ts +++ b/packages/client/src/chain-util.ts @@ -34,13 +34,15 @@ export class ChainRegistryChainUtil { constructor(options: ChainRegistryChainUtilOptions) { this.chainName = options.chainName; this.chainInfo = options.chainInfo; - this.allAsset = this.chainInfo.assetLists.reduce( - (m, it) => { - [].push.apply(m, it.assets); - return m; - }, - [...this.chainInfo.nativeAssetLists.assets] - ); + this.allAsset = [ + { + assets: [ + ...this.chainInfo.nativeAssetLists.assets, + ...this.chainInfo.assetLists.flatMap(({ assets }) => assets) + ], + chain_name: this.chainName + } + ]; } getAssetByDenom(denom: CoinDenom): Asset { diff --git a/packages/utils/src/asset-list-util.ts b/packages/utils/src/asset-list-util.ts index b56889aea..980f85c73 100644 --- a/packages/utils/src/asset-list-util.ts +++ b/packages/utils/src/asset-list-util.ts @@ -74,7 +74,7 @@ export function getCoinGeckoIdByDenom( ): string | null { const filteredAssetLists = assets.filter(({ chain_name }) => { return ( - (!chainName || chainName == chainName) && + (!chainName || chain_name === chainName) && (allowTestnet || !chain_name.includes('testnet')) && !excludedChainNames.includes(chain_name) ); @@ -140,7 +140,7 @@ export function convertBaseUnitsToDollarValue( ): string { const denom = getChainDenomBySymbol(assets, symbol, chainName); return new BigNumber(amount) - .shiftedBy(-getExponentByDenom(assets, denom)) + .shiftedBy(-getExponentByDenom(assets, denom, chainName)) .multipliedBy(prices[denom]) .toString(); } @@ -155,7 +155,7 @@ export function convertDollarValueToDenomUnits( const denom = getChainDenomBySymbol(assets, symbol, chainName); return new BigNumber(value) .dividedBy(prices[denom]) - .shiftedBy(getExponentByDenom(assets, denom)) + .shiftedBy(getExponentByDenom(assets, denom, chainName)) .toString(); } From f3c8fe8fd73c9168ca1b8fc93881da54e3fe64c2 Mon Sep 17 00:00:00 2001 From: luca Date: Wed, 20 Dec 2023 22:26:14 +0800 Subject: [PATCH 5/5] clean up names --- packages/client/README.md | 6 ++--- packages/client/__tests__/client-mock.test.ts | 2 +- packages/client/__tests__/fetcher.test.ts | 2 +- packages/client/src/chain-info.ts | 2 +- packages/client/src/chain-util.ts | 27 ++++++++++--------- 5 files changed, 20 insertions(+), 19 deletions(-) diff --git a/packages/client/README.md b/packages/client/README.md index d2621d244..edc828a98 100644 --- a/packages/client/README.md +++ b/packages/client/README.md @@ -87,13 +87,13 @@ or get the `ChainInfo` object: const chainInfo: ChainInfo = registry.getChainInfo('osmosis'); // AssetList[] of the generated assets -const assetes: AssetList[] = chainInfo.assetLists; +const assets: AssetList[] = chainInfo.assetLists; // Chain const chain: Chain = chainInfo.chain; -// AssetList[] of the native assets -const assetes: AssetList[] = chainInfo.nativeAssetLists; +// Native asset list +const nativeAssetList: AssetList = chainInfo.nativeAssetList; ``` ## Related diff --git a/packages/client/__tests__/client-mock.test.ts b/packages/client/__tests__/client-mock.test.ts index 7e6ceef80..3d8615d56 100644 --- a/packages/client/__tests__/client-mock.test.ts +++ b/packages/client/__tests__/client-mock.test.ts @@ -75,6 +75,6 @@ describe('Test client', () => { it('Test mock fetching asset list', () => { const chainInfo = client.getChainInfo('osmosis'); - expect(chainInfo.nativeAssetLists.assets.length).toEqual(1); + expect(chainInfo.nativeAssetList.assets.length).toEqual(1); }); }); diff --git a/packages/client/__tests__/fetcher.test.ts b/packages/client/__tests__/fetcher.test.ts index cab9f5f2a..fef491938 100644 --- a/packages/client/__tests__/fetcher.test.ts +++ b/packages/client/__tests__/fetcher.test.ts @@ -44,7 +44,7 @@ describe('Test fetcher', () => { expect(chainInfo.chain).toEqual(osmosis); const osmosisAssets = fetcher.getChainAssetList('osmosis'); - expect(chainInfo.nativeAssetLists).toEqual(osmosisAssets); + expect(chainInfo.nativeAssetList).toEqual(osmosisAssets); const num = osmosisAssets.assets.length; const numGenerated = generated[0].assets.length; diff --git a/packages/client/src/chain-info.ts b/packages/client/src/chain-info.ts index c61b8841d..644604735 100644 --- a/packages/client/src/chain-info.ts +++ b/packages/client/src/chain-info.ts @@ -43,7 +43,7 @@ export class ChainInfo { get chain() { return this._chain; } - get nativeAssetLists() { + get nativeAssetList() { return this._assetList; } get assetLists() { diff --git a/packages/client/src/chain-util.ts b/packages/client/src/chain-util.ts index 042747372..c07c7c394 100644 --- a/packages/client/src/chain-util.ts +++ b/packages/client/src/chain-util.ts @@ -29,15 +29,16 @@ export interface ChainRegistryChainUtilOptions { export class ChainRegistryChainUtil { chainName: string; chainInfo: ChainInfo; - allAsset: AssetList[]; + + private _assets: AssetList[] = []; constructor(options: ChainRegistryChainUtilOptions) { this.chainName = options.chainName; this.chainInfo = options.chainInfo; - this.allAsset = [ + this._assets = [ { assets: [ - ...this.chainInfo.nativeAssetLists.assets, + ...this.chainInfo.nativeAssetList.assets, ...this.chainInfo.assetLists.flatMap(({ assets }) => assets) ], chain_name: this.chainName @@ -46,35 +47,35 @@ export class ChainRegistryChainUtil { } getAssetByDenom(denom: CoinDenom): Asset { - return getAssetByDenom(this.allAsset, denom, this.chainName); + return getAssetByDenom(this._assets, denom, this.chainName); } getDenomByCoinGeckoId(coinGeckoId: string): CoinDenom { - return getDenomByCoinGeckoId(this.allAsset, coinGeckoId, this.chainName); + return getDenomByCoinGeckoId(this._assets, coinGeckoId, this.chainName); } getCoinGeckoIdByDenom(coinGeckoId: string): CoinDenom { - return getCoinGeckoIdByDenom(this.allAsset, coinGeckoId, { + return getCoinGeckoIdByDenom(this._assets, coinGeckoId, { chainName: this.chainName }); } getSymbolByChainDenom(denom: CoinDenom): string { - return getSymbolByChainDenom(this.allAsset, denom, this.chainName); + return getSymbolByChainDenom(this._assets, denom, this.chainName); } getChainDenomBySymbol(token: string): CoinDenom { - return getChainDenomBySymbol(this.allAsset, token, this.chainName); + return getChainDenomBySymbol(this._assets, token, this.chainName); } getExponentByDenom(denom: CoinDenom): Exponent { - return getExponentByDenom(this.allAsset, denom, this.chainName); + return getExponentByDenom(this._assets, denom, this.chainName); } convertCoinGeckoPricesToDenomPriceMap( prices: Record ): PriceHash { - return convertCoinGeckoPricesToDenomPriceMap(this.allAsset, prices); + return convertCoinGeckoPricesToDenomPriceMap(this._assets, prices); } noDecimals(num: number | string): string { @@ -87,7 +88,7 @@ export class ChainRegistryChainUtil { amount: string | number ): string { return convertBaseUnitsToDollarValue( - this.allAsset, + this._assets, prices, symbol, amount, @@ -101,7 +102,7 @@ export class ChainRegistryChainUtil { value: string | number ): string { return convertDollarValueToDenomUnits( - this.allAsset, + this._assets, prices, symbol, value, @@ -114,7 +115,7 @@ export class ChainRegistryChainUtil { amount: string | number ): string { return convertBaseUnitsToDisplayUnits( - this.allAsset, + this._assets, symbol, amount, this.chainName