Skip to content

Commit

Permalink
Merge pull request #80 from cosmology-tech/feat/improve-api
Browse files Browse the repository at this point in the history
improve chain-registry/cilent api
  • Loading branch information
marslavish authored Mar 19, 2024
2 parents 1dc7330 + cecb348 commit 87eacf3
Show file tree
Hide file tree
Showing 22 changed files with 520 additions and 716 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/run-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,5 @@ jobs:
run: cd ./packages/keplr && yarn test
- name: utils
run: cd ./packages/utils && yarn test
- name: client
run: cd ./packages/client && yarn test
31 changes: 31 additions & 0 deletions packages/client/__tests__/client-api.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import request from 'supertest';

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

const timeout = 30000;
const host = 'https://raw.githubusercontent.com';

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

return Promise.all(
client.urls.map((url) =>
request(host)
.get(url.slice(host.length))
.timeout(timeout)
.expect(200)
.then((response) => {
expect(JSON.parse(response?.text).$schema).toBeDefined();
})
)
);
},
timeout
);
});
46 changes: 46 additions & 0 deletions packages/client/__tests__/client-fetch.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import { ChainRegistryClient } from '../src';

const timeout = 30000;
const urls = [
'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/chain.json',
'https://raw.githubusercontent.com/cosmos/chain-registry/master/osmosis/assetlist.json',
'https://raw.githubusercontent.com/cosmos/chain-registry/master/juno/assetlist.json',
'https://raw.githubusercontent.com/cosmos/chain-registry/master/secretnetwork/assetlist.json',
'https://raw.githubusercontent.com/cosmos/chain-registry/master/_IBC/juno-osmosis.json',
'https://raw.githubusercontent.com/cosmos/chain-registry/master/_IBC/osmosis-secretnetwork.json'
];

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

beforeAll((done) => {
client = new ChainRegistryClient();
client.fetch({ urls }).then(() => done());
}, timeout);

it(
'Test chain registry',
() => {
expect(client.chains.length).toBe(1);
expect(client.assetLists.length).toBe(3);
expect(client.ibcInfo.length).toBe(2);
},
timeout
);

it(
'Test chain info',
() => {
const chainData = client.getChainFullData('osmosis');
const ibcAssetList = client.getChainIbcAssetList('osmosis');
expect(chainData.ibcAssetList).toEqual(ibcAssetList);

const osmosis = client.getChain('osmosis');
expect(chainData.chain).toEqual(osmosis);

const osmosisAssets = client.getChainAssetList('osmosis');
expect(chainData.assetList).toEqual(osmosisAssets);
},
timeout
);
});
47 changes: 0 additions & 47 deletions packages/client/__tests__/client-fetching.test.ts

This file was deleted.

32 changes: 0 additions & 32 deletions packages/client/__tests__/client-generating.test.ts

This file was deleted.

80 changes: 21 additions & 59 deletions packages/client/__tests__/client-mock.test.ts
Original file line number Diff line number Diff line change
@@ -1,80 +1,42 @@
import nock from 'nock';

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']
}
]
};
import assets from '../../../__fixtures__/assets.json';
import chains from '../../../__fixtures__/chains.json';
import { ChainRegistryClient } from '../src/client';

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

function nockByChainName(chainName: string, chainData, assetlistData) {
function nockByChainName(chainName: string) {
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;
let client: ChainRegistryClient;

beforeAll(async () => {
nockByChainName('osmosis', osmosisChainData, osmosisAssetlistData);
client = new ChainRegistryClient({
chainNames: ['osmosis']
});
await client.fetchUrls();
nockByChainName('stargaze');
client = new ChainRegistryClient({ chainNames: ['stargaze'] });
await client.fetch();
});

afterAll(() => {
nock.restore();
});

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

it('Test mock fetching asset list', () => {
const chainInfo = client.getChainInfo('osmosis');
expect(chainInfo.nativeAssetList.assets.length).toEqual(1);
const assetList = client.getChainAssetList('stargaze');
expect(assetList?.assets.length).toEqual(1);
});
});
86 changes: 0 additions & 86 deletions packages/client/__tests__/client.api.test.ts

This file was deleted.

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

const timeout = 30000;

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

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

it('Test fetching chain', () => {
const chain = client.getChain('osmosis');
expect(chain?.chain_name).toEqual('osmosis');
});

it(
'Test fetching chain ibc info',
async () => {
expect(client.ibcInfo.length).toEqual(0);

await client.fetch({ chainNames: ['juno'] });

expect(client.urls.length).toEqual(5);
expect(client.chains.length).toEqual(2);
expect(client.ibcInfo.length).toEqual(1);
expect(client.ibcInfo[0].chain_1.chain_name).toEqual('juno');
},
timeout
);

it('Test fetching asset list', () => {
const chainData = client.getChainFullData('osmosis');
const assetList = client.getChainAssetList('osmosis');
expect(chainData.assetList).toEqual(assetList);
});

it('Test chain utils', () => {
const chainUtils = client.getChainUtils('osmosis');
const asset = chainUtils.getAssetByDenom('uosmo');
expect(asset.name).toEqual('Osmosis');
});
});
Loading

0 comments on commit 87eacf3

Please sign in to comment.