Skip to content

Commit

Permalink
feat: getChainsAndConnection helper
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jun 19, 2024
1 parent bfd5ed9 commit 7921211
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,8 @@ export const prepareLocalOrchestrationAccountKit = (
// TODO #9211 lookup denom from brand
if ('brand' in amount) throw Fail`ERTP Amounts not yet supported`;

// TODO consider using `getChainsAndConnection` but right now it's keyed by chain name
// and we only have chainId for destination.
const agoricChainInfo = await chainHub.getChainInfo('agoric');
const { transferChannel } = await chainHub.getConnectionInfo(
agoricChainInfo,
Expand Down
10 changes: 6 additions & 4 deletions packages/orchestration/src/proposals/start-stakeAtom.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { makeTracer } from '@agoric/internal';
import { makeStorageNodeChild } from '@agoric/internal/src/lib-chainStorage.js';
import { E } from '@endo/far';
import { makeChainHub } from '../utils/chainHub.js';
import { getChainsAndConnection, makeChainHub } from '../utils/chainHub.js';

/**
* @import {IBCConnectionID} from '@agoric/vats';
Expand Down Expand Up @@ -46,9 +46,11 @@ export const startStakeAtom = async ({

const chainHub = makeChainHub(await agoricNames);

const agoric = await chainHub.getChainInfo('agoric');
const cosmoshub = await chainHub.getChainInfo('cosmoshub');
const connectionInfo = await chainHub.getConnectionInfo(agoric, cosmoshub);
const [_, cosmoshub, connectionInfo] = await getChainsAndConnection(
chainHub,
'agoric',
'cosmoshub',
);

/** @type {StartUpgradableOpts<StakeIcaSF>} */
const startOpts = {
Expand Down
11 changes: 5 additions & 6 deletions packages/orchestration/src/proposals/start-stakeOsmo.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { makeTracer } from '@agoric/internal';
import { makeStorageNodeChild } from '@agoric/internal/src/lib-chainStorage.js';
import { E } from '@endo/far';
import { makeChainHub } from '../utils/chainHub.js';
import { getChainsAndConnection, makeChainHub } from '../utils/chainHub.js';

/**
* @import {IBCConnectionID} from '@agoric/vats';
Expand Down Expand Up @@ -47,11 +47,10 @@ export const startStakeOsmo = async ({

const chainHub = makeChainHub(await agoricNames);

const agoric = await chainHub.getChainInfo('agoric');
const osmosis = await chainHub.getChainInfo('osmosis');
const connectionInfo = await chainHub.getConnectionInfo(
agoric.chainId,
osmosis.chainId,
const [_, osmosis, connectionInfo] = await getChainsAndConnection(
chainHub,
'agoric',
'osmosis',
);

/** @type {StartUpgradableOpts<StakeIcaSF>} */
Expand Down
37 changes: 32 additions & 5 deletions packages/orchestration/src/utils/chainHub.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ const { Fail } = assert;
* @import {Zone} from '@agoric/zone';
*/

/**
* @template {string} K
* @typedef {K extends keyof KnownChains
* ? Omit<KnownChains[K], 'connections'>
* : ChainInfo} ActualChainInfo
*/

/** agoricNames key for ChainInfo hub */
export const CHAIN_KEY = 'chain';
/** namehub for connection info */
Expand Down Expand Up @@ -82,11 +89,7 @@ export const makeChainHub = (agoricNames, zone = makeHeapZone()) => {
/**
* @template {string} K
* @param {K} chainName
* @returns {Promise<
* K extends keyof KnownChains
* ? Omit<KnownChains[K], 'connections'>
* : ChainInfo
* >}
* @returns {Promise<ActualChainInfo<K>>}
*/
async getChainInfo(chainName) {
// Either from registerChain or memoized remote lookup()
Expand Down Expand Up @@ -177,3 +180,27 @@ export const registerChain = async (
// Bundle to pipeline IO
await Promise.all(promises);
};

/**
* @template {string} C1
* @template {string} C2
* @param {ChainHub} chainHub
* @param {C1} chainName1
* @param {C2} chainName2
* @returns {Promise<
* [ActualChainInfo<C1>, ActualChainInfo<C2>, IBCConnectionInfo]
* >}
*/
export const getChainsAndConnection = async (
chainHub,
chainName1,
chainName2,
) => {
const [chain1, chain2] = await Promise.all([
chainHub.getChainInfo(chainName1),
chainHub.getChainInfo(chainName2),
]);
const connectionInfo = await chainHub.getConnectionInfo(chain2, chain1);

return [chain1, chain2, connectionInfo];
};

0 comments on commit 7921211

Please sign in to comment.