Skip to content

Commit

Permalink
Merge pull request #83 from cosmology-tech/feat/improve-utils
Browse files Browse the repository at this point in the history
improve chain-registry/utils api
  • Loading branch information
marslavish authored Mar 19, 2024
2 parents 87eacf3 + 6712608 commit 2a182e6
Show file tree
Hide file tree
Showing 31 changed files with 977 additions and 580 deletions.
2 changes: 1 addition & 1 deletion packages/client/__tests__/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,6 @@ describe('Test client', () => {
it('Test chain utils', () => {
const chainUtils = client.getChainUtils('osmosis');
const asset = chainUtils.getAssetByDenom('uosmo');
expect(asset.name).toEqual('Osmosis');
expect(asset?.name).toEqual('Osmosis');
});
});
86 changes: 66 additions & 20 deletions packages/client/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,17 @@
import { ChainRegistryClient, ChainUtils } from '../src';
import { ChainRegistryClient, ChainRegistryUtils } from '../src';

const timeout = 30000;

describe('tests for asset-list-util', () => {
let utils: ChainUtils;
describe('tests for chain registry utils', () => {
let utils: ChainRegistryUtils;

beforeAll(async () => {
const client = new ChainRegistryClient({ chainNames: ['osmosis'] });
await client.fetch();

const { assetList, ibcAssetList } = client.getChainFullData('osmosis');

utils = new ChainUtils({
utils = new ChainRegistryUtils({
chainName: 'osmosis',
assetList,
ibcAssetList
Expand All @@ -20,7 +20,12 @@ describe('tests for asset-list-util', () => {

it('getAssetByDenom', () => {
const asset = utils.getAssetByDenom('uosmo');
expect(asset.base).toEqual('uosmo');
expect(asset?.base).toEqual('uosmo');
});

it('getAssetBySymbol', () => {
const asset = utils.getAssetBySymbol('ION');
expect(asset?.base).toEqual('uion');
});

it('getDenomByCoinGeckoId', () => {
Expand All @@ -30,17 +35,17 @@ describe('tests for asset-list-util', () => {
expect(denom2).toEqual('uion');
});

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

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

Expand All @@ -49,27 +54,68 @@ describe('tests for asset-list-util', () => {
expect(exponent).toEqual(6);
});

it('convertBaseUnitsToDollarValue', () => {
const value = utils.convertBaseUnitsToDollarValue({ uosmo: 1 }, 'OSMO', 5);
it('getExponentBySymbol', () => {
const exponent = utils.getExponentBySymbol('ION');
expect(exponent).toEqual(6);
});

it('getCoinGeckoIdByDenom', () => {
const id = utils.getCoinGeckoIdByDenom('uosmo');
expect(id).toEqual('osmosis');
});

it('getTokenLogoByDenom', () => {
const logo = utils.getTokenLogoByDenom('uosmo');
expect(logo).toEqual(
'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/images/osmo.png'
);
});

it('getTokenNameByDenom', () => {
const name = utils.getTokenNameByDenom('uosmo');
expect(name).toEqual('Osmosis');
});

it('getChainNameByDenom', () => {
const name = utils.getChainNameByDenom(
'ibc/8E697BDABE97ACE8773C6DF7402B2D1D5104DD1EEABE12608E3469B7F64C15BA'
);
expect(name).toEqual('jackal');
});

it('mapCoinGeckoPricesToDenoms', () => {
const prices = {
osmosis: { usd: 0.0001 },
ion: { usd: 0.0002 }
};
const priceMap = utils.mapCoinGeckoPricesToDenoms(prices);
expect(priceMap).toEqual({
uosmo: 0.0001,
uion: 0.0002
});
});

it('convertBaseUnitToDollarValue', () => {
const value = utils.convertBaseUnitToDollarValue({ uosmo: 1 }, 'OSMO', 5);
expect(value).toEqual('0.000005');
});

it('convertDollarValueToDenomUnits', () => {
const value = utils.convertDollarValueToDenomUnits(
it('convertDollarValueToBaseUnit', () => {
const value = utils.convertDollarValueToBaseUnit(
{ uosmo: 1 },
'OSMO',
0.00001
);
expect(value).toEqual('10');
});

it('convertBaseUnitsToDisplayUnits', () => {
const value = utils.convertBaseUnitsToDisplayUnits('OSMO', 99);
it('convertBaseUnitToDisplayUnit', () => {
const value = utils.convertBaseUnitToDisplayUnit('OSMO', 99);
expect(value).toEqual('0.000099');
});

it('uosmo coingecko id', () => {
const id = utils.getCoinGeckoIdByDenom('uosmo');
expect(id).toEqual('osmosis');
it('convertDisplayUnitToBaseUnit', () => {
const value = utils.convertDisplayUnitToBaseUnit('OSMO', 0.000099);
expect(value).toEqual('99');
});
});
120 changes: 0 additions & 120 deletions packages/client/src/chain-utils.ts

This file was deleted.

4 changes: 2 additions & 2 deletions packages/client/src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { basename } from 'bfs-path';
import { getAssetLists } from '@chain-registry/utils';
import { AssetList, Chain, IBCInfo } from '@chain-registry/types';

import { ChainUtils } from './chain-utils';
import { ChainRegistryUtils } from './utils';

const DEFAULT_BASE_URL =
'https://raw.githubusercontent.com/cosmos/chain-registry/master';
Expand Down Expand Up @@ -80,7 +80,7 @@ export class ChainRegistryClient {
}

public getChainUtils(chainName: string) {
return new ChainUtils({
return new ChainRegistryUtils({
chainName,
assetList: this.getChainAssetList(chainName),
ibcAssetList: this.getChainIbcAssetList(chainName)
Expand Down
2 changes: 1 addition & 1 deletion packages/client/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
export * from './utils';
export * from './client';
export * from './chain-utils';
Loading

0 comments on commit 2a182e6

Please sign in to comment.