Skip to content

Commit

Permalink
fetcher
Browse files Browse the repository at this point in the history
  • Loading branch information
pyramation committed Nov 13, 2024
1 parent 1b487a7 commit a3a99fd
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 31 deletions.
1 change: 1 addition & 0 deletions v2/packages/client/__tests__/client-fetching.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ describe('Test client', () => {
let client: ChainRegistryClient;
beforeAll(() => {
const options: ChainRegistryClientOptions = {
// add camelCase
chainNames: ['osmosis']
};

Expand Down
5 changes: 2 additions & 3 deletions v2/packages/client/__tests__/client.api.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
import { assetLists as _assetLists, chains } from '@chain-registry/v2';

import {
ChainRegistryChainUtil,
ChainRegistryChainUtilOptions,
ChainRegistryClient,
ChainRegistryClientOptions
} from '../src';
import { assetLists as _assetLists, chains } from '../test-utils';

describe('tests for asset-list-util', () => {
const assetLists = _assetLists.filter(
Expand Down Expand Up @@ -84,7 +83,7 @@ describe('tests for asset-list-util', () => {

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

it('getChainNameByDenom', () => {
Expand Down
20 changes: 10 additions & 10 deletions v2/packages/client/setup-jest.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import nock from 'nock';

import { assets, chains, ibc } from './test-utils';
import { assetLists, chains, ibc } from './test-utils';

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

Expand All @@ -14,35 +14,35 @@ beforeAll(() => {
// 'https://raw.githubusercontent.com/cosmos/chain-registry/master/_IBC/osmosis-secretnetwork.json'
nock(baseUrl)
.get('/cosmos/chain-registry/master/osmosis/chain.json')
.reply(200, chains.find(c => c.chain_name === 'osmosis'));
.reply(200, chains.find(c => c.chainName === 'osmosis'));
nock(baseUrl)
.get('/cosmos/chain-registry/master/osmosis/assetlist.json')
.reply(200, assets.find(c => c.chain_name === 'osmosis'));
.reply(200, assetLists.find(c => c.chainName === 'osmosis'));

nock(baseUrl)
.get('/cosmos/chain-registry/master/stargaze/chain.json')
.reply(200, chains.find(c => c.chain_name === 'stargaze'));
.reply(200, chains.find(c => c.chainName === 'stargaze'));
nock(baseUrl)
.get('/cosmos/chain-registry/master/stargaze/assetlist.json')
.reply(200, assets.find(c => c.chain_name === 'stargaze'));
.reply(200, assetLists.find(c => c.chainName === 'stargaze'));

nock(baseUrl)
.get('/cosmos/chain-registry/master/juno/chain.json')
.reply(200, chains.find(c => c.chain_name === 'juno'));
.reply(200, chains.find(c => c.chainName === 'juno'));
nock(baseUrl)
.get('/cosmos/chain-registry/master/juno/assetlist.json')
.reply(200, assets.find(c => c.chain_name === 'juno'));
.reply(200, assetLists.find(c => c.chainName === 'juno'));

nock(baseUrl)
.get('/cosmos/chain-registry/master/secretnetwork/assetlist.json')
.reply(200, assets.find(c => c.chain_name === 'secretnetwork'));
.reply(200, assetLists.find(c => c.chainName === 'secretnetwork'));

nock(baseUrl)
.get('/cosmos/chain-registry/master/_IBC/juno-osmosis.json')
.reply(200, ibc.find(i => i.chain_1.chain_name === 'juno' && i.chain_2.chain_name==='osmosis'));
.reply(200, ibc.find(i => i.chain1.chainName === 'juno' && i.chain2.chainName==='osmosis'));
nock(baseUrl)
.get('/cosmos/chain-registry/master/_IBC/osmosis-secretnetwork.json')
.reply(200, ibc.find(i => i.chain_1.chain_name === 'osmosis' && i.chain_2.chain_name==='secretnetwork'));
.reply(200, ibc.find(i => i.chain1.chainName === 'osmosis' && i.chain2.chainName==='secretnetwork'));
});


Expand Down
35 changes: 17 additions & 18 deletions v2/packages/client/src/fetcher.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { AssetList as IAssetList, Chain as IChain, IBCData as IIBCData} from '@chain-registry/interfaces';
import {
AssetList,
Chain,
Expand Down Expand Up @@ -107,11 +106,11 @@ export class ChainRegistryFetcher {
return chainInfo;
}

upsertChain(data: IChain) {
upsertChain(data: Chain) {
const found = this._chains.find((chain) => {
return (
chain.chainName === data.chain_name &&
chain.networkType === data.network_type
chain.chainName === data.chainName &&
chain.networkType === data.networkType
);
});

Expand All @@ -124,8 +123,8 @@ export class ChainRegistryFetcher {

this._chains = this._chains.map((chain) => {
if (
chain.chainName === data.chain_name &&
chain.networkType === data.network_type
chain.chainName === data.chainName &&
chain.networkType === data.networkType
) {
return convertedData;
} else {
Expand All @@ -134,9 +133,9 @@ export class ChainRegistryFetcher {
});
}

updateAssetList(data: IAssetList) {
updateAssetList(data: AssetList) {
const found = this._assetLists.find((list) => {
return list.chainName === data.chain_name;
return list.chainName === data.chainName;
});

const convertedData: AssetList = keysToCamel(data);
Expand All @@ -147,19 +146,19 @@ export class ChainRegistryFetcher {
}

this._assetLists = this._assetLists.map((list) => {
if (list.chainName === data.chain_name) {
if (list.chainName === data.chainName) {
return convertedData;
} else {
return list;
}
});
}

upsertIbcData(data: IIBCData) {
upsertIbcData(data: IBCData) {
const found = this._ibcData.find((info) => {
return (
info.chain1.chainName === data.chain_1.chain_name &&
info.chain2.chainName === data.chain_2.chain_name
info.chain1.chainName === data.chain1.chainName &&
info.chain2.chainName === data.chain2.chainName
);
});

Expand All @@ -172,8 +171,8 @@ export class ChainRegistryFetcher {

this._ibcData = this._ibcData.map((info) => {
if (
info.chain1.chainName === data.chain_1.chain_name &&
info.chain2.chainName === data.chain_2.chain_name
info.chain1.chainName === data.chain1.chainName &&
info.chain2.chainName === data.chain2.chainName
) {
return convertedData;
} else {
Expand All @@ -182,18 +181,18 @@ export class ChainRegistryFetcher {
});
}

update(data: IChain | IAssetList | IIBCData) {
update(data: Chain | AssetList | IBCData) {
if (!data.$schema) throw new Error('not a registered JSON schema type');
const type = basename(data.$schema, '.schema.json');
switch (type) {
case 'chain':
this.upsertChain(data as IChain);
this.upsertChain(data as Chain);
break;
case 'assetlist':
this.updateAssetList(data as IAssetList);
this.updateAssetList(data as AssetList);
break;
case 'ibc_data':
this.upsertIbcData(data as IIBCData);
this.upsertIbcData(data as IBCData);
break;
default:
throw new Error('unknown schema type');
Expand Down

0 comments on commit a3a99fd

Please sign in to comment.