Skip to content

Commit

Permalink
feat: atomicProvider service
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Sep 24, 2024
1 parent 2ccf887 commit 061f3cb
Show file tree
Hide file tree
Showing 13 changed files with 80 additions and 84 deletions.
11 changes: 2 additions & 9 deletions packages/orchestration/src/examples/send-anywhere.contract.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import { makeSharedStateRecord } from '@agoric/async-flow';
import { InvitationShape } from '@agoric/zoe/src/typeGuards.js';
import { M } from '@endo/patterns';
import { withOrchestration } from '../utils/start-helper.js';
Expand Down Expand Up @@ -33,19 +32,13 @@ const contract = async (
zcf,
privateArgs,
zone,
{ chainHub, orchestrateAll, zoeTools },
{ atomicProvider, chainHub, orchestrateAll, zoeTools },
) => {
const contractState = makeSharedStateRecord(
/** @type {{ account: OrchestrationAccount<any> | undefined }} */ {
localAccount: undefined,
},
);

const creatorFacet = prepareChainHubAdmin(zone, chainHub);

// orchestrate uses the names on orchestrationFns to do a "prepare" of the associated behavior
const orchFns = orchestrateAll(flows, {
contractState,
atomicProvider,
zoeTools,
});

Expand Down
24 changes: 14 additions & 10 deletions packages/orchestration/src/examples/send-anywhere.flows.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import { M, mustMatch } from '@endo/patterns';
/**
* @import {GuestInterface} from '@agoric/async-flow';
* @import {ZoeTools} from '../utils/zoe-tools.js';
* @import {Orchestrator, LocalAccountMethods, OrchestrationAccountI, OrchestrationFlow} from '../types.js';
* @import {AtomicProvider} from '@agoric/store/src/stores/store-utils.js';
* @import {Orchestrator, OrchestrationFlow, OrchestrationAccount} from '../types.js';
*/

const { entries } = Object;
Expand All @@ -17,14 +18,14 @@ const { entries } = Object;
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
* @param {object} ctx
* @param {{ localAccount?: OrchestrationAccountI & LocalAccountMethods }} ctx.contractState
* @param {AtomicProvider<string, any>} ctx.atomicProvider
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {ZCFSeat} seat
* @param {{ chainName: string; destAddr: string }} offerArgs
*/
export const sendIt = async (
orch,
{ contractState, zoeTools: { localTransfer, withdrawToSeat } },
{ atomicProvider, zoeTools: { localTransfer, withdrawToSeat } },
seat,
offerArgs,
) => {
Expand All @@ -41,19 +42,22 @@ export const sendIt = async (
);
const chain = await orch.getChain(chainName);

if (!contractState.localAccount) {
const agoricChain = await orch.getChain('agoric');
contractState.localAccount = await agoricChain.makeAccount();
}
const localAccount = await atomicProvider.provideAsync(
'localAccount',
async () => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
},
);

const info = await chain.getChainInfo();
const { chainId } = info;
assert(typeof chainId === 'string', 'bad chainId');

await localTransfer(seat, contractState.localAccount, give);
await localTransfer(seat, localAccount, give);

try {
await contractState.localAccount.transfer(
await localAccount.transfer(
{ denom, value: amt.value },
{
value: destAddr,
Expand All @@ -62,7 +66,7 @@ export const sendIt = async (
},
);
} catch (e) {
await withdrawToSeat(contractState.localAccount, seat, give);
await withdrawToSeat(localAccount, seat, give);
const errorMsg = `IBC Transfer failed ${q(e)}`;
seat.exit(errorMsg);
throw makeError(errorMsg);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
* The primary offer result is a power for invitation makers that can perform
* actions with an ICA account.
*/
import { makeSharedStateRecord } from '@agoric/async-flow';
import { AmountShape } from '@agoric/ertp';
import { M } from '@endo/patterns';
import { prepareCombineInvitationMakers } from '../exos/combine-invitation-makers.js';
Expand Down Expand Up @@ -44,18 +43,8 @@ const contract = async (
zcf,
privateArgs,
zone,
{ orchestrateAll, zoeTools, chainHub },
{ atomicProvider, orchestrateAll, zoeTools, chainHub },
) => {
const contractState = makeSharedStateRecord(
/**
* @type {{
* account: (OrchestrationAccount<any> & LocalAccountMethods) | undefined;
* }}
*/ {
localAccount: undefined,
},
);

const StakingCombinationsInvitationMakersI = M.interface(
'StakingCombinationsInvitationMakersI',
{
Expand Down Expand Up @@ -129,7 +118,7 @@ const contract = async (
);

const orchFns = orchestrateAll(flows, {
contractState,
atomicProvider,
makeCombineInvitationMakers,
makeExtraInvitationMaker,
flows,
Expand Down
24 changes: 15 additions & 9 deletions packages/orchestration/src/examples/staking-combinations.flows.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
* @import {GuestInterface} from '@agoric/async-flow';
* @import {Orchestrator, OrchestrationFlow, AmountArg, CosmosValidatorAddress, ChainAddress, LocalAccountMethods, OrchestrationAccountI} from '../types.js'
* @import {ContinuingOfferResult, InvitationMakers} from '@agoric/smart-wallet/src/types.js';
* @import {AtomicProvider} from '@agoric/store/src/stores/store-utils.js';
* @import {MakeCombineInvitationMakers} from '../exos/combine-invitation-makers.js';
* @import {CosmosOrchestrationAccount} from '../exos/cosmos-orchestration-account.js';
* @import {ZoeTools} from '../utils/zoe-tools.js';
Expand Down Expand Up @@ -48,7 +49,7 @@ harden(makeAccount);
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
* @param {object} ctx
* @param {{ localAccount?: OrchestrationAccountI & LocalAccountMethods }} ctx.contractState
* @param {AtomicProvider<string, any>} ctx.atomicProvider
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {GuestInterface<CosmosOrchestrationAccount>} account
* @param {ZCFSeat} seat
Expand All @@ -57,26 +58,31 @@ harden(makeAccount);
*/
export const depositAndDelegate = async (
orch,
{ contractState, zoeTools },
{ atomicProvider, zoeTools },
account,
seat,
validator,
) => {
await null;
trace('depositAndDelegate', account, seat, validator);
mustMatch(validator, ChainAddressShape);
if (!contractState.localAccount) {
const agoricChain = await orch.getChain('agoric');
contractState.localAccount = await agoricChain.makeAccount();
}

const localAccount = await atomicProvider.provideAsync(
'localAccount',
async () => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
},
);

const { give } = seat.getProposal();
await zoeTools.localTransfer(seat, contractState.localAccount, give);
await zoeTools.localTransfer(seat, localAccount, give);

const address = account.getAddress();
try {
await contractState.localAccount.transfer(give.Stake, address);
await localAccount.transfer(give.Stake, address);
} catch (cause) {
await zoeTools.withdrawToSeat(contractState.localAccount, seat, give);
await zoeTools.withdrawToSeat(localAccount, seat, give);
const errMsg = makeError(`ibc transfer failed ${q(cause)}`);
seat.exit(errMsg);
throw errMsg;
Expand Down
6 changes: 6 additions & 0 deletions packages/orchestration/src/utils/start-helper.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ 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 { makeAtomicProvider } from '@agoric/store/src/stores/store-utils.js';
import { makeChainHub } from '../exos/chain-hub.js';
import { prepareCosmosOrchestrationAccount } from '../exos/cosmos-orchestration-account.js';
import { prepareLocalChainFacade } from '../exos/local-chain-facade.js';
Expand Down Expand Up @@ -170,9 +171,14 @@ export const provideOrchestration = (
zones.contract.subZone('orchestration'),
);

const atomicProvider = makeAtomicProvider(
zones.contract.mapStore('atomicProvider'),
);

return {
...defaultOrchestrateKit,
makeOrchestrateKit,
atomicProvider,
chainHub,
vowTools,
asyncFlowTools,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,16 @@ Generated by [AVA](https://avajs.dev).
'ChainHub Admin_singleton': 'Alleged: ChainHub Admin',
'Send PF_kindHandle': 'Alleged: kind',
'Send PF_singleton': 'Alleged: Send PF',
atomicProvider: {},
orchestration: {
sendIt: {
asyncFlow_kindHandle: 'Alleged: kind',
endowments: {
0: {
contractState_kindHandle: 'Alleged: kind',
contractState_singleton: 'Alleged: contractState',
atomicProvider: {
provideAsync_kindHandle: 'Alleged: kind',
provideAsync_singleton: 'Alleged: provideAsync',
},
zoeTools: {
localTransfer_kindHandle: 'Alleged: kind',
localTransfer_singleton: 'Alleged: localTransfer',
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,7 @@ Generated by [AVA](https://avajs.dev).
denom: {},
},
contract: {
atomicProvider: {},
orchestration: {
unbondAndTransfer: {
asyncFlow_kindHandle: 'Alleged: kind',
Expand Down
Binary file not shown.
11 changes: 2 additions & 9 deletions packages/orchestration/test/fixtures/zoe-tools.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
* around `zoeTools.localTransfer` and `zoeTools.withdrawToSeat`
*/

import { makeSharedStateRecord } from '@agoric/async-flow';
import { InvitationShape } from '@agoric/zoe/src/typeGuards.js';
import { E } from '@endo/far';
import { M } from '@endo/patterns';
Expand Down Expand Up @@ -47,18 +46,12 @@ const contract = async (
zcf,
privateArgs,
zone,
{ chainHub, orchestrateAll, zoeTools },
{ atomicProvider, chainHub, orchestrateAll, zoeTools },
) => {
const contractState = makeSharedStateRecord(
/** @type {{ account: OrchestrationAccount<any> | undefined }} */ {
localAccount: undefined,
},
);

const creatorFacet = prepareChainHubAdmin(zone, chainHub);

const orchFns = orchestrateAll(flows, {
contractState,
atomicProvider,
zoeTools,
});

Expand Down
59 changes: 33 additions & 26 deletions packages/orchestration/test/fixtures/zoe-tools.flows.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ const { values } = Object;

/**
* @import {GuestInterface} from '@agoric/async-flow';
* @import {AtomicProvider} from '@agoric/store/src/stores/store-utils.js';
* @import {Orchestrator, LocalAccountMethods, OrchestrationAccountI, OrchestrationFlow, ChainAddress} from '@agoric/orchestration';
* @import {ZoeTools} from '../../src/utils/zoe-tools.js';
*/
Expand All @@ -23,14 +24,14 @@ const { values } = Object;
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
* @param {object} ctx
* @param {{ localAccount?: OrchestrationAccountI & LocalAccountMethods }} ctx.contractState
* @param {AtomicProvider<string, any>} ctx.atomicProvider
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {ZCFSeat} seat
* @param {{ destAddr: ChainAddress }} offerArgs
*/
export const depositSend = async (
orch,
{ contractState, zoeTools: { localTransfer, withdrawToSeat } },
{ atomicProvider, zoeTools: { localTransfer, withdrawToSeat } },
seat,
offerArgs,
) => {
Expand All @@ -40,18 +41,20 @@ export const depositSend = async (

const { give } = seat.getProposal();

await null;
if (!contractState.localAccount) {
const agoricChain = await orch.getChain('agoric');
contractState.localAccount = await agoricChain.makeAccount();
}
const localAccount = await atomicProvider.provideAsync(
'localAccount',
async () => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
},
);

await localTransfer(seat, contractState.localAccount, give);
await localTransfer(seat, localAccount, give);

try {
await contractState.localAccount.sendAll(destAddr, values(give));
await localAccount.sendAll(destAddr, values(give));
} catch (error) {
await withdrawToSeat(contractState.localAccount, seat, give);
await withdrawToSeat(localAccount, seat, give);
const errMsg = makeError(`SendAll failed ${q(error)}`);
seat.exit(errMsg);
throw errMsg;
Expand All @@ -67,25 +70,27 @@ harden(depositSend);
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
* @param {object} ctx
* @param {{ localAccount?: OrchestrationAccountI & LocalAccountMethods }} ctx.contractState
* @param {AtomicProvider<string, any>} ctx.atomicProvider
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {ZCFSeat} seat
*/
export const deposit = async (
orch,
{ contractState, zoeTools: { localTransfer } },
{ atomicProvider, zoeTools: { localTransfer } },
seat,
) => {
const { give } = seat.getProposal();

await null;
if (!contractState.localAccount) {
const agoricChain = await orch.getChain('agoric');
contractState.localAccount = await agoricChain.makeAccount();
}
const localAccount = await atomicProvider.provideAsync(
'localAccount',
async () => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
},
);

try {
await localTransfer(seat, contractState.localAccount, give);
await localTransfer(seat, localAccount, give);
} catch (e) {
seat.exit(e);
throw e;
Expand All @@ -100,25 +105,27 @@ harden(deposit);
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
* @param {object} ctx
* @param {{ localAccount?: OrchestrationAccountI & LocalAccountMethods }} ctx.contractState
* @param {AtomicProvider<string, any>} ctx.atomicProvider
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {ZCFSeat} seat
*/
export const withdraw = async (
orch,
{ contractState, zoeTools: { withdrawToSeat } },
{ atomicProvider, zoeTools: { withdrawToSeat } },
seat,
) => {
const { want } = seat.getProposal();

await null;
if (!contractState.localAccount) {
const agoricChain = await orch.getChain('agoric');
contractState.localAccount = await agoricChain.makeAccount();
}
const localAccount = await atomicProvider.provideAsync(
'localAccount',
async () => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
},
);

try {
await withdrawToSeat(contractState.localAccount, seat, want);
await withdrawToSeat(localAccount, seat, want);
} catch (e) {
seat.exit(e);
throw e;
Expand Down
Loading

0 comments on commit 061f3cb

Please sign in to comment.