Skip to content

Commit

Permalink
feat: registerChainsAndAssets
Browse files Browse the repository at this point in the history
- helper function that takes chainInfo and assetInfo and registers the data in ChainHub
  • Loading branch information
0xpatrickdev committed Nov 26, 2024
1 parent c1b406c commit e72782d
Show file tree
Hide file tree
Showing 6 changed files with 57 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,7 @@ Generated by [AVA](https://avajs.dev).
denomHash: Function denomHash {},
prepareChainHubAdmin: Function prepareChainHubAdmin {},
prepareCosmosInterchainService: Function prepareCosmosInterchainService {},
registerChainsAndAssets: Function registerChainsAndAssets {},
withChainCapabilities: Function withChainCapabilities {},
withOrchestration: Function withOrchestration {},
}
Binary file not shown.
1 change: 1 addition & 0 deletions packages/orchestration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ export * from './src/utils/denomHash.js';

export { withOrchestration } from './src/utils/start-helper.js';
export { withChainCapabilities } from './src/chain-capabilities.js';
export { registerChainsAndAssets } from './src/utils/chain-hub-helper.js';
53 changes: 53 additions & 0 deletions packages/orchestration/src/utils/chain-hub-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* @import {ChainHub, CosmosChainInfo, Denom, DenomDetail} from '../types.js';
*/

/**
* Registers chains, connections, assets in the provided chainHub.
*
* If either is not provided, registration will be skipped.
*
* @param {ChainHub} chainHub
* @param {Record<string, Brand<'nat'>>} brands
* @param {Record<string, CosmosChainInfo> | undefined} chainInfo
* @param {Record<Denom, DenomDetail & { brandKey?: string }> | undefined} assetInfo
*/
export const registerChainsAndAssets = (
chainHub,
brands,
chainInfo,
assetInfo,
) => {
if (!chainInfo) {
console.log('No chain info provided, returning early.');
return;
}

const conns = {};
for (const [chainName, allInfo] of Object.entries(chainInfo)) {
const { connections, ...info } = allInfo;
chainHub.registerChain(chainName, info);
conns[info.chainId] = connections;
}
const registeredPairs = new Set();
for (const [pChainId, connInfos] of Object.entries(conns)) {
for (const [cChainId, connInfo] of Object.entries(connInfos)) {
const pair = [pChainId, cChainId].sort().join('');
if (!registeredPairs.has(pair)) {
chainHub.registerConnection(pChainId, cChainId, connInfo);
registeredPairs.add(pair);
}
}
}

if (!assetInfo) {
console.log('No asset info provided, returning early.');
return;
}
for (const [denom, info] of Object.entries(assetInfo)) {
const infoWithBrand = info.brandKey
? { ...info, brand: brands[info.brandKey] }
: info;
chainHub.registerAsset(denom, infoWithBrand);
}
};
2 changes: 2 additions & 0 deletions packages/orchestration/test/snapshots/exports.test.ts.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,5 +36,7 @@ Generated by [AVA](https://avajs.dev).
'denomHash',
'prepareChainHubAdmin',
'prepareCosmosInterchainService',
'registerChainsAndAssets',
'withChainCapabilities',
'withOrchestration',
]
Binary file modified packages/orchestration/test/snapshots/exports.test.ts.snap
Binary file not shown.

0 comments on commit e72782d

Please sign in to comment.