Skip to content

Commit

Permalink
Merge pull request #59 from cosmology-tech/fix/optional-param
Browse files Browse the repository at this point in the history
Add chainName as optional param to utils
  • Loading branch information
pyramation committed Dec 20, 2023
2 parents 4be96c1 + 071e0cc commit 6b8dec2
Show file tree
Hide file tree
Showing 9 changed files with 264 additions and 113 deletions.
6 changes: 3 additions & 3 deletions packages/client/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion packages/client/__tests__/client-mock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
86 changes: 86 additions & 0 deletions packages/client/__tests__/client.api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
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', () => {
const asset = client.getAssetByDenom('uosmo');
expect(asset.base).toEqual('uosmo');
});

it('getDenomByCoinGeckoId', () => {
const denom1 = client.getDenomByCoinGeckoId('osmosis');
expect(denom1).toEqual('uosmo');
const denom2 = client.getDenomByCoinGeckoId('ion');
expect(denom2).toEqual('uion');
});

it('getSymbolByChainDenom', () => {
const denom1 = client.getSymbolByChainDenom('uosmo');
expect(denom1).toEqual('OSMO');
const denom2 = client.getSymbolByChainDenom('uion');
expect(denom2).toEqual('ION');
});

it('getChainDenomBySymbol', () => {
const denom1 = client.getChainDenomBySymbol('OSMO');
expect(denom1).toEqual('uosmo');
const denom2 = client.getChainDenomBySymbol('ION');
expect(denom2).toEqual('uion');
});

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');
});
});
2 changes: 1 addition & 1 deletion packages/client/__tests__/fetcher.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/chain-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ export class ChainInfo {
get chain() {
return this._chain;
}
get nativeAssetLists() {
get nativeAssetList() {
return this._assetList;
}
get assetLists() {
Expand Down
63 changes: 45 additions & 18 deletions packages/client/src/chain-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Asset } from '@chain-registry/types';
import { Asset, AssetList } from '@chain-registry/types';
import type {
CoinDenom,
CoinGeckoUSD,
Expand All @@ -12,6 +12,7 @@ import {
convertDollarValueToDenomUnits,
getAssetByDenom,
getChainDenomBySymbol,
getCoinGeckoIdByDenom,
getDenomByCoinGeckoId,
getExponentByDenom,
getSymbolByChainDenom,
Expand All @@ -28,44 +29,53 @@ export interface ChainRegistryChainUtilOptions {
export class ChainRegistryChainUtil {
chainName: string;
chainInfo: ChainInfo;
allAsset: Asset[];

private _assets: AssetList[] = [];

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._assets = [
{
assets: [
...this.chainInfo.nativeAssetList.assets,
...this.chainInfo.assetLists.flatMap(({ assets }) => assets)
],
chain_name: this.chainName
}
];
}

getAssetByDenom(denom: CoinDenom): Asset {
return getAssetByDenom(this.allAsset, denom);
return getAssetByDenom(this._assets, denom, this.chainName);
}

getDenomByCoinGeckoId(coinGeckoId: string): CoinDenom {
return getDenomByCoinGeckoId(this.allAsset, coinGeckoId);
return getDenomByCoinGeckoId(this._assets, coinGeckoId, this.chainName);
}

getCoinGeckoIdByDenom(coinGeckoId: string): CoinDenom {
return getCoinGeckoIdByDenom(this._assets, coinGeckoId, {
chainName: this.chainName
});
}

getSymbolByChainDenom(denom: CoinDenom): string {
return getSymbolByChainDenom(this.allAsset, denom);
return getSymbolByChainDenom(this._assets, denom, this.chainName);
}

getChainDenomBySymbol(token: string): CoinDenom {
return getChainDenomBySymbol(this.allAsset, token);
return getChainDenomBySymbol(this._assets, token, this.chainName);
}

getExponentByDenom(denom: CoinDenom): Exponent {
return getExponentByDenom(this.allAsset, denom);
return getExponentByDenom(this._assets, denom, this.chainName);
}

convertCoinGeckoPricesToDenomPriceMap(
prices: Record<string, CoinGeckoUSD>
): PriceHash {
return convertCoinGeckoPricesToDenomPriceMap(this.allAsset, prices);
return convertCoinGeckoPricesToDenomPriceMap(this._assets, prices);
}

noDecimals(num: number | string): string {
Expand All @@ -77,21 +87,38 @@ export class ChainRegistryChainUtil {
symbol: string,
amount: string | number
): string {
return convertBaseUnitsToDollarValue(this.allAsset, prices, symbol, amount);
return convertBaseUnitsToDollarValue(
this._assets,
prices,
symbol,
amount,
this.chainName
);
}

convertDollarValueToDenomUnits(
prices: PriceHash,
symbol: string,
value: string | number
): string {
return convertDollarValueToDenomUnits(this.allAsset, prices, symbol, value);
return convertDollarValueToDenomUnits(
this._assets,
prices,
symbol,
value,
this.chainName
);
}

convertBaseUnitsToDisplayUnits(
symbol: string,
amount: string | number
): string {
return convertBaseUnitsToDisplayUnits(this.allAsset, symbol, amount);
return convertBaseUnitsToDisplayUnits(
this._assets,
symbol,
amount,
this.chainName
);
}
}
75 changes: 40 additions & 35 deletions packages/utils/__tests__/asset-list-util.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,84 +7,89 @@ import {
convertDollarValueToDenomUnits,
getAssetByDenom,
getChainDenomBySymbol,
getDenomByCoinGeckoId,
getCoinGeckoIdByDenom,
getDenomByCoinGeckoId,
getExponentByDenom,
getSymbolByChainDenom,
noDecimals
} 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
}
});
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');
});
});

Expand Down
Loading

0 comments on commit 6b8dec2

Please sign in to comment.