-
Notifications
You must be signed in to change notification settings - Fork 212
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test: basic-flows.contract.js for testing async-flow continuing offers
- Loading branch information
1 parent
bc12cbd
commit 8330fba
Showing
11 changed files
with
350 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
packages/builders/scripts/orchestration/init-basic-flows.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
import { makeHelpers } from '@agoric/deploy-script-support'; | ||
import { startBasicFlows } from '@agoric/orchestration/src/proposals/start-basic-flows.js'; | ||
|
||
/** @type {import('@agoric/deploy-script-support/src/externalTypes.js').CoreEvalBuilder} */ | ||
export const defaultProposalBuilder = async ({ publishRef, install }) => { | ||
return harden({ | ||
sourceSpec: '@agoric/orchestration/src/proposals/start-basic-flows.js', | ||
getManifestCall: [ | ||
'getManifestForContract', | ||
{ | ||
installKeys: { | ||
basicFlows: publishRef( | ||
install( | ||
'@agoric/orchestration/src/examples/basic-flows.contract.js', | ||
), | ||
), | ||
}, | ||
}, | ||
], | ||
}); | ||
}; | ||
|
||
export default async (homeP, endowments) => { | ||
const { writeCoreEval } = await makeHelpers(homeP, endowments); | ||
await writeCoreEval(startBasicFlows.name, defaultProposalBuilder); | ||
}; |
72 changes: 72 additions & 0 deletions
72
packages/orchestration/src/examples/basic-flows.contract.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
/** | ||
* @file Primarily a testing fixture, but also serves as an example of how to | ||
* leverage basic functionality of the Orchestration API with async-flow. | ||
*/ | ||
import { InvitationShape } from '@agoric/zoe/src/typeGuards.js'; | ||
import { M, mustMatch } from '@endo/patterns'; | ||
import { provideOrchestration } from '../utils/start-helper.js'; | ||
|
||
/** | ||
* @import {Baggage} from '@agoric/vat-data'; | ||
* @import {Orchestrator} from '@agoric/orchestration'; | ||
* @import {OrchestrationPowers} from '../utils/start-helper.js'; | ||
*/ | ||
|
||
/** | ||
* Create an account on a Cosmos chain and return a continuing offer with | ||
* invitations makers for Delegate, WithdrawRewards, Transfer, etc. | ||
* | ||
* @param {Orchestrator} orch | ||
* @param {undefined} _ctx | ||
* @param {ZCFSeat} seat | ||
* @param {{ chainName: string }} offerArgs | ||
*/ | ||
const makeOrchAccountHandler = async (orch, _ctx, seat, { chainName }) => { | ||
seat.exit(); // no funds exchanged | ||
mustMatch(chainName, M.string()); | ||
const remoteChain = await orch.getChain(chainName); | ||
const cosmosAccount = await remoteChain.makeAccount(); | ||
return cosmosAccount.asContinuingOffer(); | ||
}; | ||
|
||
/** | ||
* @param {ZCF} zcf | ||
* @param {OrchestrationPowers & { | ||
* marshaller: Marshaller; | ||
* }} privateArgs | ||
* @param {Baggage} baggage | ||
*/ | ||
export const start = async (zcf, privateArgs, baggage) => { | ||
const { orchestrate, zone } = provideOrchestration( | ||
zcf, | ||
baggage, | ||
privateArgs, | ||
privateArgs.marshaller, | ||
); | ||
|
||
/** @type {OfferHandler} */ | ||
const makeOrchAccount = orchestrate( | ||
'makeOrchAccount', | ||
undefined, | ||
makeOrchAccountHandler, | ||
); | ||
|
||
const publicFacet = zone.exo( | ||
'Basic Flows Public Facet', | ||
M.interface('Basic Flows PF', { | ||
makeOrchAccountInvitation: M.callWhen().returns(InvitationShape), | ||
}), | ||
{ | ||
makeOrchAccountInvitation() { | ||
return zcf.makeInvitation( | ||
makeOrchAccount, | ||
'Make an Orchestration Account', | ||
); | ||
}, | ||
}, | ||
); | ||
|
||
return { publicFacet }; | ||
}; | ||
|
||
/** @typedef {typeof start} BasicFlowsSF */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,97 @@ | ||
/** | ||
* @file A proposal to start the basic flows contract. | ||
*/ | ||
import { makeTracer } from '@agoric/internal'; | ||
import { makeStorageNodeChild } from '@agoric/internal/src/lib-chainStorage.js'; | ||
import { E } from '@endo/far'; | ||
|
||
/** | ||
* @import {BasicFlowsSF} from '../examples/basic-flows.contract.js'; | ||
*/ | ||
|
||
const trace = makeTracer('StartBasicFlows', true); | ||
const contractName = 'basicFlows'; | ||
|
||
/** | ||
* See `@agoric/builders/builders/scripts/orchestration/init-basic-flows.js` for | ||
* the accompanying proposal builder. Run `agoric run | ||
* packages/builders/scripts/orchestration/init-basic-flows.js` to build the | ||
* contract and proposal files. | ||
* | ||
* @param {BootstrapPowers} powers | ||
*/ | ||
export const startBasicFlows = async ({ | ||
consume: { | ||
agoricNames, | ||
board, | ||
chainStorage, | ||
chainTimerService, | ||
cosmosInterchainService, | ||
localchain, | ||
startUpgradable, | ||
}, | ||
installation: { | ||
// @ts-expect-error not a WellKnownName | ||
consume: { [contractName]: installation }, | ||
}, | ||
instance: { | ||
// @ts-expect-error not a WellKnownName | ||
produce: { [contractName]: produceInstance }, | ||
}, | ||
}) => { | ||
trace(`start ${contractName}`); | ||
await null; | ||
|
||
const storageNode = await makeStorageNodeChild(chainStorage, contractName); | ||
const marshaller = await E(board).getPublishingMarshaller(); | ||
|
||
/** @type {StartUpgradableOpts<BasicFlowsSF>} */ | ||
const startOpts = { | ||
label: 'basicFlows', | ||
installation, | ||
terms: undefined, | ||
privateArgs: { | ||
agoricNames: await agoricNames, | ||
orchestrationService: await cosmosInterchainService, | ||
localchain: await localchain, | ||
storageNode, | ||
marshaller, | ||
timerService: await chainTimerService, | ||
}, | ||
}; | ||
|
||
const { instance } = await E(startUpgradable)(startOpts); | ||
produceInstance.resolve(instance); | ||
}; | ||
harden(startBasicFlows); | ||
|
||
export const getManifestForContract = ( | ||
{ restoreRef }, | ||
{ installKeys, ...options }, | ||
) => { | ||
return { | ||
manifest: { | ||
[startBasicFlows.name]: { | ||
consume: { | ||
agoricNames: true, | ||
board: true, | ||
chainStorage: true, | ||
chainTimerService: true, | ||
cosmosInterchainService: true, | ||
localchain: true, | ||
startUpgradable: true, | ||
}, | ||
installation: { | ||
consume: { [contractName]: true }, | ||
}, | ||
instance: { | ||
produce: { [contractName]: true }, | ||
}, | ||
}, | ||
}, | ||
installations: { | ||
[contractName]: restoreRef(installKeys[contractName]), | ||
}, | ||
options, | ||
}; | ||
}; |
86 changes: 86 additions & 0 deletions
86
packages/orchestration/test/examples/basic-flows.contract.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js'; | ||
import type { TestFn } from 'ava'; | ||
import { setUpZoeForTest } from '@agoric/zoe/tools/setup-zoe.js'; | ||
import type { Instance } from '@agoric/zoe/src/zoeService/utils.js'; | ||
import { E, getInterfaceOf } from '@endo/far'; | ||
import path from 'path'; | ||
import { commonSetup } from '../supports.js'; | ||
|
||
const dirname = path.dirname(new URL(import.meta.url).pathname); | ||
|
||
const contractName = 'basic-flows'; | ||
const contractFile = `${dirname}/../../src/examples/${contractName}.contract.js`; | ||
type StartFn = | ||
typeof import('../../src/examples/basic-flows.contract.js').start; | ||
|
||
type TestContext = Awaited<ReturnType<typeof commonSetup>> & { | ||
zoe: ZoeService; | ||
instance: Instance<StartFn>; | ||
}; | ||
|
||
const test = anyTest as TestFn<TestContext>; | ||
|
||
test.before(async t => { | ||
const setupContext = await commonSetup(t); | ||
const { | ||
bootstrap: { storage }, | ||
commonPrivateArgs, | ||
} = setupContext; | ||
|
||
const { zoe, bundleAndInstall } = await setUpZoeForTest(); | ||
|
||
t.log('contract coreEval', contractName); | ||
const installation = await bundleAndInstall(contractFile); | ||
|
||
const storageNode = await E(storage.rootNode).makeChildNode(contractName); | ||
const { instance } = await E(zoe).startInstance( | ||
installation, | ||
undefined, | ||
{}, | ||
{ ...commonPrivateArgs, storageNode }, | ||
); | ||
|
||
t.context = { | ||
...setupContext, | ||
zoe, | ||
instance, | ||
}; | ||
}); | ||
|
||
const chainConfigs = { | ||
agoric: { addressPrefix: 'agoric1fakeLCAAddress' }, | ||
cosmoshub: { addressPrefix: 'cosmos1test' }, | ||
}; | ||
|
||
const orchestrationAccountScenario = test.macro({ | ||
title: (_, chainName: string) => | ||
`orchestrate - ${chainName} makeOrchAccount returns a ContinuingOfferResult`, | ||
exec: async (t, chainName: string) => { | ||
const config = chainConfigs[chainName as keyof typeof chainConfigs]; | ||
if (!config) { | ||
return t.fail(`Unknown chain: ${chainName}`); | ||
} | ||
|
||
const { zoe, instance } = t.context; | ||
const publicFacet = await E(zoe).getPublicFacet(instance); | ||
const inv = E(publicFacet).makeOrchAccountInvitation(); | ||
const userSeat = E(zoe).offer(inv, {}, undefined, { chainName }); | ||
// @ts-expect-error TODO: type expected offer result | ||
const { holder, invitationMakers, publicSubscribers } = | ||
await E(userSeat).getOfferResult(); | ||
|
||
t.regex(getInterfaceOf(holder)!, /Orchestration (.*) holder/); | ||
t.regex(getInterfaceOf(invitationMakers)!, /invitationMakers/); | ||
|
||
const { description, storagePath, subscriber } = publicSubscribers.account; | ||
t.regex(description, /Account holder/); | ||
|
||
const expectedStoragePath = `mockChainStorageRoot.basic-flows.${config.addressPrefix}`; | ||
t.is(storagePath, expectedStoragePath); | ||
|
||
t.regex(getInterfaceOf(subscriber)!, /Durable Publish Kit subscriber/); | ||
}, | ||
}); | ||
|
||
test(orchestrationAccountScenario, 'agoric'); | ||
test(orchestrationAccountScenario, 'cosmoshub'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
+5 Bytes
(100%)
packages/orchestration/test/examples/snapshots/sendAnywhere.test.ts.snap
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Binary file modified
BIN
-5 Bytes
(99%)
packages/orchestration/test/examples/snapshots/unbondExample.test.ts.snap
Binary file not shown.