Skip to content

Commit

Permalink
fix: revert client package
Browse files Browse the repository at this point in the history
  • Loading branch information
marslavish committed Mar 20, 2024
1 parent 6ee7ba9 commit 2534ca7
Show file tree
Hide file tree
Showing 21 changed files with 746 additions and 554 deletions.
31 changes: 0 additions & 31 deletions packages/client/__tests__/client-api.test.ts

This file was deleted.

46 changes: 0 additions & 46 deletions packages/client/__tests__/client-fetch.test.ts

This file was deleted.

47 changes: 47 additions & 0 deletions packages/client/__tests__/client-fetching.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import {
ChainRegistryClient,
ChainRegistryClientOptions
} from '../src/registry';

const timeout = 30000; // miliseconds

describe('Test client', () => {
let client;
beforeAll((done) => {
const options: ChainRegistryClientOptions = {
chainNames: ['osmosis']
};

client = new ChainRegistryClient(options);
client.fetchUrls().then(() => done());
}, timeout);

it(
'Test fetching chain Info',
() => {
const chainInfo = client.getChainInfo('osmosis');
expect(chainInfo.chainName).toEqual('osmosis');
},
timeout
);

it(
'Test chain util',
() => {
const chainUtil = client.getChainUtil('osmosis');
const asset = chainUtil.getAssetByDenom('uosmo');
expect(asset.name).toEqual('Osmosis');
},
timeout
);

it(
'Test fetching asset list',
() => {
const chainInfo = client.getChainInfo('osmosis');
const generated = client.getGeneratedAssetLists('osmosis');
expect(chainInfo.assetLists).toEqual(generated);
},
timeout
);
});
32 changes: 32 additions & 0 deletions packages/client/__tests__/client-generating.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import request from 'supertest';

import { ChainRegistryClient } from '../src/registry';

const timeout = 30000; // miliseconds

describe('Test client', () => {
it(
'test urls generated by client',
async () => {
const client = new ChainRegistryClient({
chainNames: ['osmosis', 'juno']
});
expect(client.urls.length).toEqual(5);

const host = 'https://raw.githubusercontent.com';
const responses = await Promise.all(
client.urls.map((url) =>
request(host)
.get(url.substring(host.length))
.timeout(timeout)
.expect(200)
)
);

responses?.forEach((res) => {
expect(JSON.parse(res?.text).$schema).toBeDefined();
});
},
timeout
);
});
80 changes: 59 additions & 21 deletions packages/client/__tests__/client-mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,42 +1,80 @@
import nock from 'nock';

import assets from '../../../__fixtures__/assets.json';
import chains from '../../../__fixtures__/chains.json';
import { ChainRegistryClient } from '../src/client';
import { ChainRegistryClient } from '../src/registry';

const osmosisChainData = {
$schema: '../chain.schema.json',
chain_name: 'osmosis',
status: 'live',
network_type: 'mainnet',
website: 'https://osmosis.zone/',
update_link:
'https://raw.githubusercontent.com/osmosis-labs/osmosis/main/chain.schema.json',
pretty_name: 'Osmosis',
chain_id: 'osmosis-1',
bech32_prefix: 'osmo',
daemon_name: 'osmosisd',
node_home: '$HOME/.osmosisd',
key_algos: ['secp256k1'],
slip44: 118
};

const osmosisAssetlistData = {
$schema: '../assetlist.schema.json',
chain_name: 'osmosis',
assets: [
{
description: 'The native token of Osmosis',
denom_units: [
{
denom: 'uosmo',
exponent: 0
},
{
denom: 'osmo',
exponent: 6
}
],
base: 'uosmo',
name: 'Osmosis',
display: 'osmo',
symbol: 'OSMO',
logo_URIs: {
png: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/images/osmo.png',
svg: 'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/images/osmo.svg'
},
coingecko_id: 'osmosis',
keywords: ['dex', 'staking']
}
]
};

const baseUrl = 'https://raw.githubusercontent.com';

function nockByChainName(chainName: string) {
function nockByChainName(chainName: string, chainData, assetlistData) {
const chainDataPath = `/cosmos/chain-registry/master/${chainName}/chain.json`;
const assetlistDataPath = `/cosmos/chain-registry/master/${chainName}/assetlist.json`;

const chainData = chains.find((chain) => chain.chain_name === chainName);
const assetlistData = assets.find((asset) => asset.chain_name === chainName);

nock(baseUrl).get(chainDataPath).reply(200, chainData);
nock(baseUrl).get(assetlistDataPath).reply(200, assetlistData);
}

describe('Test client', () => {
let client: ChainRegistryClient;

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

afterAll(() => {
nock.restore();
nockByChainName('osmosis', osmosisChainData, osmosisAssetlistData);
client = new ChainRegistryClient({
chainNames: ['osmosis']
});
await client.fetchUrls();
});

it('Test mock fetching chain data', () => {
const chainData = client.getChain('stargaze');
expect(chainData?.chain_name).toEqual('stargaze');
const chainInfo = client.getChainInfo('osmosis');
expect(chainInfo.chainName).toEqual('osmosis');
});

it('Test mock fetching asset list', () => {
const assetList = client.getChainAssetList('stargaze');
expect(assetList?.assets.length).toEqual(1);
const chainInfo = client.getChainInfo('osmosis');
expect(chainInfo.nativeAssetList.assets.length).toEqual(1);
});
});
133 changes: 133 additions & 0 deletions packages/client/__tests__/client.api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,133 @@
import assets from '../../../__fixtures__/assets.json';
import chains from '../../../__fixtures__/chains.json';

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('getAssetBySymbol', () => {
const asset = client.getAssetBySymbol('ION');
expect(asset?.base).toEqual('uion');
});

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

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

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

it('getExponentByDenom', () => {
const exponent = client.getExponentByDenom('uosmo');
expect(exponent).toEqual(6);
});

it('getExponentBySymbol', () => {
const exponent = client.getExponentBySymbol('ION');
expect(exponent).toEqual(6);
});

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

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

it('getTokenNameByDenom', () => {
const name = client.getTokenNameByDenom('uion');
expect(name).toEqual('Ion');
});

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

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

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

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

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

it('convertDisplayUnitToBaseUnit', () => {
const value = client.convertDisplayUnitToBaseUnit('OSMO', 0.000099);
expect(value).toEqual('99');
});
});
Loading

0 comments on commit 2534ca7

Please sign in to comment.