Skip to content

Commit

Permalink
feat: send-anywhere inits chainHub
Browse files Browse the repository at this point in the history
- split start-send-anywhere.js into proposal and start script so we can pass options
- init-send-anywhere.js takes `--chainInfo` and `--assetInfo` options
- send-anywhere.contrac.js registers chainInfo and assetInfo from privateArgs in ChainHub
  • Loading branch information
0xpatrickdev committed Nov 26, 2024
1 parent 647e79d commit 2c1661d
Show file tree
Hide file tree
Showing 5 changed files with 152 additions and 54 deletions.
35 changes: 33 additions & 2 deletions multichain-testing/test/send-anywhere.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,22 +10,53 @@ import { createWallet } from '../tools/wallet.js';
import { AmountMath } from '@agoric/ertp';
import { makeQueryClient } from '../tools/query.js';
import type { Amount } from '@agoric/ertp/src/types.js';
import chainInfo from '../starship-chain-info.js';
import { denomHash, withChainCapabilities } from '@agoric/orchestration';

const test = anyTest as TestFn<SetupContextWithWallets>;

const accounts = ['osmosis1', 'osmosis2', 'cosmoshub1', 'cosmoshub2'];

const contractName = 'sendAnywhere';
const contractBuilder =
'../packages/builders/scripts/testing/start-send-anywhere.js';
'../packages/builders/scripts/testing/init-send-anywhere.js';

test.before(async t => {
const { deleteTestKeys, setupTestKeys, ...rest } = await commonSetup(t);
deleteTestKeys(accounts).catch();
const wallets = await setupTestKeys(accounts);
t.context = { ...rest, wallets, deleteTestKeys };
const { startContract } = rest;
await startContract(contractName, contractBuilder);

const assetInfo = {
uosmo: {
baseName: 'osmosis',
chainName: 'osmosis',
baseDenom: 'uosmo',
},
[`ibc/${denomHash({ denom: 'uosmo', channelId: chainInfo.agoric.connections['osmosislocal'].transferChannel.channelId })}`]:
{
baseName: 'osmosis',
chainName: 'agoric',
baseDenom: 'uosmo',
},
uatom: {
baseName: 'cosmoshub',
chainName: 'cosmoshub',
baseDenom: 'uatom',
},
[`ibc/${denomHash({ denom: 'uatom', channelId: chainInfo.agoric.connections['gaialocal'].transferChannel.channelId })}`]:
{
baseName: 'cosmoshub',
chainName: 'agoric',
baseDenom: 'uatom',
},
};

await startContract(contractName, contractBuilder, {
chainInfo: JSON.stringify(withChainCapabilities(chainInfo)),
assetInfo: JSON.stringify(assetInfo),
});
});

test.after(async t => {
Expand Down
2 changes: 1 addition & 1 deletion packages/boot/test/orchestration/restart-contracts.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ test.serial('send-anywhere', async t => {

t.log('start send-anywhere');
await evalProposal(
buildProposal('@agoric/builders/scripts/testing/start-send-anywhere.js'),
buildProposal('@agoric/builders/scripts/testing/init-send-anywhere.js'),
);

t.log('making offer');
Expand Down
73 changes: 73 additions & 0 deletions packages/builders/scripts/testing/init-send-anywhere.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
import { makeHelpers } from '@agoric/deploy-script-support';
import {
getManifest,
startSendAnywhere,
} from '@agoric/orchestration/src/proposals/start-send-anywhere.js';
import { parseArgs } from 'node:util';

/**
* @import {ParseArgsConfig} from 'node:util'
*/

const chainInfoUsage = 'use --chainInfo chainName:CosmosChainInfo ...';
const assetInfoUsage =
'use --assetInfo denom:DenomInfo & {brandKey?: string} ...';

/** @type {ParseArgsConfig['options']} */
const parserOpts = {
chainInfo: { type: 'string' },
assetInfo: { type: 'string' },
};

/** @type {import('@agoric/deploy-script-support/src/externalTypes.js').CoreEvalBuilder} */
export const defaultProposalBuilder = async (
{ publishRef, install },
options,
) =>
harden({
sourceSpec: '@agoric/orchestration/src/proposals/start-send-anywhere.js',
getManifestCall: [
getManifest.name,
{
installationRef: publishRef(
install(
'@agoric/orchestration/src/examples/send-anywhere.contract.js',
),
),
options,
},
],
});

/** @type {import('@agoric/deploy-script-support/src/externalTypes.js').DeployScriptFunction} */
export default async (homeP, endowments) => {
const { scriptArgs } = endowments;

const {
values: { chainInfo, assetInfo },
} = parseArgs({
args: scriptArgs,
options: parserOpts,
});

const parseChainInfo = () => {
if (!chainInfo) throw Error(chainInfoUsage);
if (typeof chainInfo !== 'string') throw Error('chainInfo must be string');
return JSON.parse(chainInfo);
};
const parseAssetInfo = () => {
if (!assetInfo) throw Error(assetInfoUsage);
if (typeof assetInfo !== 'string') throw Error('assetInfo must be string');
return JSON.parse(assetInfo);
};
const opts = harden({
chainInfo: parseChainInfo(),
assetInfo: parseAssetInfo(),
});

const { writeCoreEval } = await makeHelpers(homeP, endowments);

await writeCoreEval(startSendAnywhere.name, utils =>
defaultProposalBuilder(utils, opts),
);
};
11 changes: 11 additions & 0 deletions packages/orchestration/src/examples/send-anywhere.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@ import { M } from '@endo/patterns';
import { prepareChainHubAdmin } from '../exos/chain-hub-admin.js';
import { AnyNatAmountShape } from '../typeGuards.js';
import { withOrchestration } from '../utils/start-helper.js';
import { registerChainsAndAssets } from '../utils/chain-hub-helper.js';
import * as flows from './send-anywhere.flows.js';
import * as sharedFlows from './shared.flows.js';

/**
* @import {Vow} from '@agoric/vow';
* @import {Zone} from '@agoric/zone';
* @import {OrchestrationPowers, OrchestrationTools} from '../utils/start-helper.js';
* @import {CosmosChainInfo, Denom, DenomDetail} from '@agoric/orchestration';
*/

export const SingleNatAmountRecord = M.and(
Expand All @@ -27,6 +29,8 @@ harden(SingleNatAmountRecord);
* @param {ZCF} zcf
* @param {OrchestrationPowers & {
* marshaller: Marshaller;
* chainInfo: Record<string, CosmosChainInfo>;
* assetInfo: Record<Denom, DenomDetail & { brandKey?: string }>;
* }} privateArgs
* @param {Zone} zone
* @param {OrchestrationTools} tools
Expand Down Expand Up @@ -82,6 +86,13 @@ export const contract = async (
},
);

registerChainsAndAssets(
chainHub,
zcf.getTerms().brands,
privateArgs.chainInfo,
privateArgs.assetInfo,
);

return { publicFacet, creatorFacet };
};
harden(contract);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,3 @@
/**
* @file This is for use in tests in a3p-integration
* Unlike most builder scripts, this one includes the proposal exports as well.
*/
import {
deeplyFulfilledObject,
makeTracer,
Expand All @@ -12,6 +8,7 @@ import { E } from '@endo/far';
/// <reference types="@agoric/vats/src/core/types-ambient"/>
/**
* @import {Installation} from '@agoric/zoe/src/zoeService/utils.js';
* @import {CosmosChainInfo, Denom, DenomDetail} from '@agoric/orchestration';
*/

const trace = makeTracer('StartSA', true);
Expand All @@ -28,28 +25,37 @@ const trace = makeTracer('StartSA', true);
* };
* };
* }} powers
* @param {{
* options: {
* chainInfo: Record<string, CosmosChainInfo>;
* assetInfo: Record<Denom, DenomDetail & { brandKey?: string }>;
* };
* }} config
*/
export const startSendAnywhere = async ({
consume: {
agoricNames,
board,
chainStorage,
chainTimerService,
cosmosInterchainService,
localchain,
startUpgradable,
},
installation: {
consume: { sendAnywhere },
},
instance: {
// @ts-expect-error unknown instance
produce: { sendAnywhere: produceInstance },
},
issuer: {
consume: { IST },
export const startSendAnywhere = async (
{
consume: {
agoricNames,
board,
chainStorage,
chainTimerService,
cosmosInterchainService,
localchain,
startUpgradable,
},
installation: {
consume: { sendAnywhere },
},
instance: {
// @ts-expect-error unknown instance
produce: { sendAnywhere: produceInstance },
},
issuer: {
consume: { IST },
},
},
}) => {
{ options: { chainInfo, assetInfo } },
) => {
trace(startSendAnywhere.name);

const marshaller = await E(board).getReadonlyMarshaller();
Expand All @@ -64,6 +70,8 @@ export const startSendAnywhere = async ({
'send-anywhere',
),
timerService: chainTimerService,
chainInfo,
assetInfo,
}),
);

Expand All @@ -78,7 +86,7 @@ export const startSendAnywhere = async ({
};
harden(startSendAnywhere);

export const getManifest = ({ restoreRef }, { installationRef }) => {
export const getManifest = ({ restoreRef }, { installationRef, options }) => {
return {
manifest: {
[startSendAnywhere.name]: {
Expand Down Expand Up @@ -106,31 +114,6 @@ export const getManifest = ({ restoreRef }, { installationRef }) => {
installations: {
sendAnywhere: restoreRef(installationRef),
},
options,
};
};

/** @type {import('@agoric/deploy-script-support/src/externalTypes.js').CoreEvalBuilder} */
export const defaultProposalBuilder = async ({ publishRef, install }) =>
harden({
// Somewhat unorthodox, source the exports from this builder module
sourceSpec: '@agoric/builders/scripts/testing/start-send-anywhere.js',
getManifestCall: [
getManifest.name,
{
installationRef: publishRef(
install(
'@agoric/orchestration/src/examples/send-anywhere.contract.js',
),
),
},
],
});

/** @type {import('@agoric/deploy-script-support/src/externalTypes.js').DeployScriptFunction} */
export default async (homeP, endowments) => {
// import dynamically so the module can work in CoreEval environment
const dspModule = await import('@agoric/deploy-script-support');
const { makeHelpers } = dspModule;
const { writeCoreEval } = await makeHelpers(homeP, endowments);
await writeCoreEval(startSendAnywhere.name, defaultProposalBuilder);
};

0 comments on commit 2c1661d

Please sign in to comment.