Skip to content

Commit

Permalink
feat: provideOrchestration helper
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jun 17, 2024
1 parent 6b72a81 commit 05cd7ee
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 99 deletions.
42 changes: 7 additions & 35 deletions packages/orchestration/src/examples/sendAnywhere.contract.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { makeDurableZone } from '@agoric/zone/durable.js';
import { M, mustMatch } from '@endo/patterns';
import { withdrawFromSeat } from '@agoric/zoe/src/contractSupport/zoeHelpers.js';
import { InvitationShape } from '@agoric/zoe/src/typeGuards.js';
import { E } from '@endo/far';
import { withdrawFromSeat } from '@agoric/zoe/src/contractSupport/zoeHelpers.js';
import { M, mustMatch } from '@endo/patterns';

import { AmountShape } from '@agoric/ertp';
import { prepareRecorderKitMakers } from '@agoric/zoe/src/contractSupport/recorder.js';
import { prepareAsyncFlowTools } from '@agoric/async-flow';
import { prepareVowTools } from '@agoric/vow';
import { CosmosChainInfoShape } from '../typeGuards.js';
import { makeOrchestrationFacade } from '../facade.js';
import { prepareLocalChainAccountKit } from '../exos/local-chain-account-kit.js';
import { makeChainHub } from '../utils/chainHub.js';
import { provideOrchestration } from '../utils/start-helper.js';

const { entries } = Object;
const { Fail } = assert;
Expand Down Expand Up @@ -51,35 +45,13 @@ export const SingleAmountRecord = M.and(
* @param {Baggage} baggage
*/
export const start = async (zcf, privateArgs, baggage) => {
const zone = makeDurableZone(baggage);

const chainHub = makeChainHub(privateArgs.agoricNames);

// TODO once durability is settled, provide some helpers to reduce boilerplate
const { marshaller, ...orchPowers } = privateArgs;
const { makeRecorderKit } = prepareRecorderKitMakers(baggage, marshaller);
const makeLocalChainAccountKit = prepareLocalChainAccountKit(
zone,
makeRecorderKit,
const { chainHub, orchestrate, zone } = provideOrchestration(
zcf,
privateArgs.timerService,
chainHub,
baggage,
privateArgs,
privateArgs.marshaller,
);

const vowTools = prepareVowTools(zone.subZone('vows'));
const asyncFlowTools = prepareAsyncFlowTools(zone.subZone('asyncFlow'), {
vowTools,
});

const { orchestrate } = makeOrchestrationFacade({
zcf,
zone,
chainHub,
makeLocalChainAccountKit,
asyncFlowTools,
...orchPowers,
});

let contractAccount;

const findBrandInVBank = async brand => {
Expand Down
45 changes: 12 additions & 33 deletions packages/orchestration/src/examples/swapExample.contract.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,11 @@
import { prepareAsyncFlowTools } from '@agoric/async-flow';
import { StorageNodeShape } from '@agoric/internal';
import { TimerServiceShape } from '@agoric/time';
import { prepareVowTools } from '@agoric/vow';
import { prepareRecorderKitMakers } from '@agoric/zoe/src/contractSupport/recorder.js';
import { withdrawFromSeat } from '@agoric/zoe/src/contractSupport/zoeHelpers.js';
import { makeDurableZone } from '@agoric/zone/durable.js';
import { Far } from '@endo/far';
import { deeplyFulfilled } from '@endo/marshal';
import { M, objectMap } from '@endo/patterns';
import { prepareLocalChainAccountKit } from '../exos/local-chain-account-kit.js';
import { makeOrchestrationFacade } from '../facade.js';
import { makeChainHub } from '../utils/chainHub.js';
import { orcUtils } from '../utils/orc.js';
import { provideOrchestration } from '../utils/start-helper.js';

/**
* @import {Orchestrator, IcaAccount, CosmosValidatorAddress} from '../types.js'
Expand Down Expand Up @@ -59,10 +53,6 @@ export const makeNatAmountShape = (brand, min) =>
* @param {Baggage} baggage
*/
export const start = async (zcf, privateArgs, baggage) => {
const { brands } = zcf.getTerms();

const zone = makeDurableZone(baggage);

const {
agoricNames,
localchain,
Expand All @@ -72,31 +62,20 @@ export const start = async (zcf, privateArgs, baggage) => {
marshaller,
} = privateArgs;

const chainHub = makeChainHub(agoricNames);
const { makeRecorderKit } = prepareRecorderKitMakers(baggage, marshaller);
const makeLocalChainAccountKit = prepareLocalChainAccountKit(
zone,
makeRecorderKit,
const { orchestrate } = provideOrchestration(
zcf,
timerService,
chainHub,
baggage,
{
agoricNames,
localchain,
orchestrationService,
storageNode,
timerService,
},
marshaller,
);
const vowTools = prepareVowTools(zone.subZone('vows'));
const asyncFlowTools = prepareAsyncFlowTools(zone.subZone('asyncFlow'), {
vowTools,
});

const { orchestrate } = makeOrchestrationFacade({
localchain,
orchestrationService,
storageNode,
timerService,
zcf,
zone,
chainHub,
makeLocalChainAccountKit,
asyncFlowTools,
});
const { brands } = zcf.getTerms();

/** deprecated historical example */
/**
Expand Down
42 changes: 11 additions & 31 deletions packages/orchestration/src/examples/unbondExample.contract.js
Original file line number Diff line number Diff line change
@@ -1,12 +1,6 @@
import { makeDurableZone } from '@agoric/zone/durable.js';
import { Far } from '@endo/far';
import { M } from '@endo/patterns';
import { prepareRecorderKitMakers } from '@agoric/zoe/src/contractSupport/recorder.js';
import { prepareVowTools } from '@agoric/vow';
import { prepareAsyncFlowTools } from '@agoric/async-flow';
import { makeOrchestrationFacade } from '../facade.js';
import { makeChainHub } from '../utils/chainHub.js';
import { prepareLocalChainAccountKit } from '../exos/local-chain-account-kit.js';
import { provideOrchestration } from '../utils/start-helper.js';

/**
* @import {Orchestrator, IcaAccount, CosmosValidatorAddress} from '../types.js'
Expand Down Expand Up @@ -39,33 +33,19 @@ export const start = async (zcf, privateArgs, baggage) => {
marshaller,
timerService,
} = privateArgs;
const zone = makeDurableZone(baggage);

const chainHub = makeChainHub(agoricNames);
const { makeRecorderKit } = prepareRecorderKitMakers(baggage, marshaller);
const makeLocalChainAccountKit = prepareLocalChainAccountKit(
zone,
makeRecorderKit,
const { orchestrate } = provideOrchestration(
zcf,
privateArgs.timerService,
chainHub,
baggage,
{
agoricNames,
localchain,
orchestrationService,
storageNode,
timerService,
},
marshaller,
);
const vowTools = prepareVowTools(zone.subZone('vows'));
const asyncFlowTools = prepareAsyncFlowTools(zone.subZone('asyncFlow'), {
vowTools,
});

const { orchestrate } = makeOrchestrationFacade({
localchain,
orchestrationService,
storageNode,
timerService,
zcf,
zone,
chainHub: makeChainHub(agoricNames),
makeLocalChainAccountKit,
asyncFlowTools,
});

/** @type {OfferHandler} */
const unbondAndLiquidStake = orchestrate(
Expand Down
72 changes: 72 additions & 0 deletions packages/orchestration/src/utils/start-helper.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { prepareAsyncFlowTools } from '@agoric/async-flow';
import { prepareVowTools } from '@agoric/vow';
import { prepareRecorderKitMakers } from '@agoric/zoe/src/contractSupport/recorder.js';
import { makeDurableZone } from '@agoric/zone/durable.js';
import { prepareLocalChainAccountKit } from '../exos/local-chain-account-kit.js';
import { makeOrchestrationFacade } from '../facade.js';
import { makeChainHub } from './chainHub.js';

/**
* @import {PromiseKit} from '@endo/promise-kit'
* @import {LocalChain} from '@agoric/vats/src/localchain.js';
* @import {TimerService, TimerBrand} from '@agoric/time';
* @import {Baggage} from '@agoric/vat-data';
* @import {NameHub} from '@agoric/vats';
* @import {Remote} from '@agoric/vow';
* @import {OrchestrationService} from '../service.js';
*/

/**
* @typedef {{
* localchain: Remote<LocalChain>;
* orchestrationService: Remote<OrchestrationService>;
* storageNode: Remote<StorageNode>;
* timerService: Remote<TimerService>;
* agoricNames: Remote<NameHub>;
* }} OrchestrationPowers
*/

/**
* Helper that a contract start function can use to set up the objects needed
* for orchestration.
*
* @param {ZCF} zcf
* @param {Baggage} baggage
* @param {OrchestrationPowers} remotePowers
* @param {Marshaller} marshaller
*/
export const provideOrchestration = (
zcf,
baggage,
remotePowers,
marshaller,
) => {
const zone = makeDurableZone(baggage);

const chainHub = makeChainHub(remotePowers.agoricNames);

const { makeRecorderKit } = prepareRecorderKitMakers(baggage, marshaller);
const makeLocalChainAccountKit = prepareLocalChainAccountKit(
zone,
makeRecorderKit,
zcf,
remotePowers.timerService,
chainHub,
);

const vowTools = prepareVowTools(zone.subZone('vows'));
const asyncFlowTools = prepareAsyncFlowTools(zone.subZone('asyncFlow'), {
vowTools,
});

const facade = makeOrchestrationFacade({
zcf,
zone,
chainHub,
makeLocalChainAccountKit,
asyncFlowTools,
...remotePowers,
});
return { ...facade, chainHub, zone };
};
harden(provideOrchestration);

0 comments on commit 05cd7ee

Please sign in to comment.