Skip to content

Commit

Permalink
Merge pull request #40 from cosmology-tech/fix/client-property-camelcase
Browse files Browse the repository at this point in the history
Fix/client property camelcase
  • Loading branch information
pyramation committed Jul 17, 2023
2 parents 48a32e2 + 9823e75 commit af2dbd0
Show file tree
Hide file tree
Showing 12 changed files with 417 additions and 115 deletions.
30 changes: 30 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ A unified store of chains info, assets, asset lists, and IBC channels for the Co

## example

Fetch data from chain-registry:

```js
import { assets, chains, ibc } from 'chain-registry';

Expand Down Expand Up @@ -63,6 +65,34 @@ will output:
}
```

Dynamically fetch data:

```js
import { ChainRegistryClient } from '@chain-registry/client';

// create an instance of ChainRegistryClient by passing in the chain names
const client = new ChainRegistryClient({
chainNames: [
'osmosis',
'juno',
'stargaze'
]
});

// chain info, assets and ibc data will be downloaded dynamically by invoking fetchUrls method
await client.fetchUrls();
// get chain data
const chain = client.getChain('osmosis');
// get asset list
const assetList = client.getChainAssetList('osmosis');
// get ibc data
const ibcData = client.getChainIbcData('osmosis);
// get asset list (including ibc assets)
const generatedAssetList = client.getGeneratedAssetLists('osmosis);

```


## packages

#### [chain-registry](packages/chain-registry)
Expand Down
50 changes: 0 additions & 50 deletions packages/client/__tests__/class.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: 80 additions & 0 deletions packages/client/__tests__/client-mock.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
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']
}
]
};

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

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

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

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

it('Test mock fetching asset list', () => {
const chainInfo = client.getChainInfo('osmosis');
expect(chainInfo.nativeAssetLists.assets.length).toEqual(1);
});
});
57 changes: 57 additions & 0 deletions packages/client/__tests__/fetcher.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import {
ChainRegistryFetcher,
ChainRegistryFetcherOptions
} from '../src/fetcher';

const timeout = 30000; // miliseconds

describe('Test fetcher', () => {
let fetcher;
beforeAll((done) => {
const options: ChainRegistryFetcherOptions = {
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'
]
};

fetcher = new ChainRegistryFetcher(options);
fetcher.fetchUrls().then(() => done());
}, timeout);

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

it(
'Test chain info',
(done) => {
const chainInfo = fetcher.getChainInfo('osmosis');
const generated = fetcher.getGeneratedAssetLists('osmosis');
expect(chainInfo.assetLists).toEqual(generated);

const osmosis = fetcher.getChain('osmosis');
expect(chainInfo.chain).toEqual(osmosis);

const osmosisAssets = fetcher.getChainAssetList('osmosis');
expect(chainInfo.nativeAssetLists).toEqual(osmosisAssets);

const num = osmosisAssets.assets.length;
const numGenerated = generated[0].assets.length;
expect(numGenerated > num).toBe(true);

done();
},
timeout
);
});
3 changes: 3 additions & 0 deletions packages/client/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
"@babel/preset-typescript": "^7.21.4",
"@types/jest": "^29.5.1",
"@types/sha.js": "^2.4.0",
"@types/supertest": "2.0.12",
"@typescript-eslint/eslint-plugin": "5.59.0",
"@typescript-eslint/parser": "5.59.0",
"babel-core": "7.0.0-bridge.0",
Expand All @@ -66,8 +67,10 @@
"eslint-plugin-unused-imports": "2.0.0",
"jest": "^29.5.0",
"long": "^5.2.0",
"nock": "13.3.2",
"prettier": "^2.8.7",
"regenerator-runtime": "^0.13.11",
"supertest": "6.3.3",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4"
},
Expand Down
26 changes: 13 additions & 13 deletions packages/client/src/chain-info.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,38 +4,38 @@ import { getAssetLists } from '@chain-registry/utils';
import { ChainRegistryFetcher } from './fetcher';

export interface ChainInfoOptions {
chain_name: string;
chainName: string;
fetcher: ChainRegistryFetcher;
}

export class ChainInfo {
chain_name: string;
chainName: string;
fetcher: ChainRegistryFetcher;

protected _chain: Chain;
protected _asset_list: AssetList;
protected _asset_lists: AssetList[];
protected _ibc_data: IBCInfo[] = [];
protected _assetList: AssetList;
protected _assetLists: AssetList[];
protected _ibcData: IBCInfo[] = [];

constructor(options: ChainInfoOptions) {
this.chain_name = options.chain_name;
this.chainName = options.chainName;
this.fetcher = options.fetcher;

this.refresh();
}

refresh() {
this._asset_list = this.fetcher.getChainAssetList(this.chain_name);
this._ibc_data = this.fetcher.getChainIbcData(this.chain_name);
this._chain = this.fetcher.getChain(this.chain_name);
this._assetList = this.fetcher.getChainAssetList(this.chainName);
this._ibcData = this.fetcher.getChainIbcData(this.chainName);
this._chain = this.fetcher.getChain(this.chainName);

const supportedChains = this._ibc_data.reduce((m, v) => {
const supportedChains = this._ibcData.reduce((m, v) => {
if (!m.includes(v.chain_1.chain_name)) m.push(v.chain_1.chain_name);
if (!m.includes(v.chain_2.chain_name)) m.push(v.chain_2.chain_name);
return m;
}, []);

this._asset_lists = this.fetcher.assetLists.filter((list) =>
this._assetLists = this.fetcher.assetLists.filter((list) =>
supportedChains.includes(list.chain_name)
);
}
Expand All @@ -44,9 +44,9 @@ export class ChainInfo {
return this._chain;
}
get nativeAssetLists() {
return this._asset_list;
return this._assetList;
}
get assetLists() {
return getAssetLists(this.chain_name, this._ibc_data, this._asset_lists);
return getAssetLists(this.chainName, this._ibcData, this._assetLists);
}
}
Loading

0 comments on commit af2dbd0

Please sign in to comment.