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 55367de commit f5d2aa0
Show file tree
Hide file tree
Showing 4 changed files with 50 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';
48 changes: 48 additions & 0 deletions packages/orchestration/src/utils/chain-hub-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* @import {ChainHub, CosmosChainInfo, Denom} from '../types.js';
*/

/**
* @param {ChainHub} chainHub
* @param {Record<string, Brand<'nat'>>} brands
* @param {Record<string, CosmosChainInfo>} chainInfo
* @param {Record<
* Denom,
* {
* chainName: string;
* baseName: string;
* baseDenom: Denom;
* brandKey?: string;
* }
* >} assetInfo
*/
export const registerChainsAndAssets = (
chainHub,
brands,
chainInfo,
assetInfo,
) => {
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);
}
}
}

for (const [denom, info] of Object.entries(assetInfo)) {
const infoWithBrand = info.brandKey
? { ...info, brand: brands[info.brandKey] }
: info;
chainHub.registerAsset(denom, infoWithBrand);
}
};

0 comments on commit f5d2aa0

Please sign in to comment.