Skip to content

Commit

Permalink
chore: sharedLocalAccount pattern
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Sep 27, 2024
1 parent 2783cba commit f5198ed
Show file tree
Hide file tree
Showing 13 changed files with 100 additions and 85 deletions.
10 changes: 8 additions & 2 deletions packages/orchestration/src/examples/send-anywhere.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { InvitationShape } from '@agoric/zoe/src/typeGuards.js';
import { M } from '@endo/patterns';
import { withOrchestration } from '../utils/start-helper.js';
import * as flows from './send-anywhere.flows.js';
import * as sharedFlows from './shared.flows.js';
import { prepareChainHubAdmin } from '../exos/chain-hub-admin.js';
import { AnyNatAmountShape } from '../typeGuards.js';

Expand Down Expand Up @@ -32,13 +33,18 @@ const contract = async (
zcf,
privateArgs,
zone,
{ atomicProvider, chainHub, orchestrateAll, zoeTools },
{ atomicProvider, chainHub, orchestrate, orchestrateAll, vowTools, zoeTools },
) => {
const creatorFacet = prepareChainHubAdmin(zone, chainHub);

const sharedLocalAccount = await atomicProvider.provideAsync(
'localAccount',
key => vowTools.when(orchestrate(key, {}, sharedFlows.makeLocalAccount)()),
);

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

Expand Down
23 changes: 7 additions & 16 deletions packages/orchestration/src/examples/send-anywhere.flows.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import { M, mustMatch } from '@endo/patterns';

/**
* @import {GuestInterface} from '@agoric/async-flow';
* @import {LocalOrchestrationAccountKit} from '../exos/local-orchestration-account.js';
* @import {ZoeTools} from '../utils/zoe-tools.js';
* @import {AtomicProvider} from '@agoric/store/src/stores/store-utils.js';
* @import {Orchestrator, OrchestrationFlow, OrchestrationAccount} from '../types.js';
* @import {Orchestrator, OrchestrationFlow, LocalAccountMethods} from '../types.js';
*/

const { entries } = Object;
Expand All @@ -18,14 +18,14 @@ const { entries } = Object;
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
* @param {object} ctx
* @param {AtomicProvider<string, any>} ctx.atomicProvider
* @param {GuestInterface<LocalOrchestrationAccountKit['holder']>} ctx.sharedLocalAccount
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {ZCFSeat} seat
* @param {{ chainName: string; destAddr: string }} offerArgs
*/
export const sendIt = async (
orch,
{ atomicProvider, zoeTools: { localTransfer, withdrawToSeat } },
{ sharedLocalAccount, zoeTools: { localTransfer, withdrawToSeat } },
seat,
offerArgs,
) => {
Expand All @@ -42,23 +42,14 @@ export const sendIt = async (
);
const chain = await orch.getChain(chainName);

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, localAccount, give);
await localTransfer(seat, sharedLocalAccount, give);

try {
await localAccount.transfer(
{ denom, value: amt.value },
await sharedLocalAccount.transfer(
{
value: destAddr,
encoding: 'bech32',
Expand All @@ -67,7 +58,7 @@ export const sendIt = async (
{ denom, value: amt.value },
);
} catch (e) {
await withdrawToSeat(localAccount, seat, give);
await withdrawToSeat(sharedLocalAccount, seat, give);
const errorMsg = `IBC Transfer failed ${q(e)}`;
seat.exit(errorMsg);
throw makeError(errorMsg);
Expand Down
17 changes: 17 additions & 0 deletions packages/orchestration/src/examples/shared.flows.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
/**
* @import {Orchestrator, OrchestrationFlow, LocalAccountMethods} from '../types.js';
*/

// in guest file (the orchestration functions)
// the second argument is all the endowments provided

/**
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
* @returns {Promise<LocalAccountMethods>}
*/
export const makeLocalAccount = async orch => {
const agoricChain = await orch.getChain('agoric');
return agoricChain.makeAccount();
};
harden(makeLocalAccount);
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { CosmosOrchestrationInvitationMakersI } from '../exos/cosmos-orchestrati
import { ChainAddressShape, DelegationShape } from '../typeGuards.js';
import { withOrchestration } from '../utils/start-helper.js';
import * as flows from './staking-combinations.flows.js';
import * as sharedFlows from './shared.flows.js';
import { prepareChainHubAdmin } from '../exos/chain-hub-admin.js';

/**
Expand Down Expand Up @@ -43,7 +44,7 @@ const contract = async (
zcf,
privateArgs,
zone,
{ atomicProvider, orchestrateAll, zoeTools, chainHub },
{ atomicProvider, orchestrate, orchestrateAll, zoeTools, chainHub, vowTools },
) => {
const StakingCombinationsInvitationMakersI = M.interface(
'StakingCombinationsInvitationMakersI',
Expand Down Expand Up @@ -117,8 +118,13 @@ const contract = async (
StakingCombinationsInvitationMakersI,
);

const sharedLocalAccount = await atomicProvider.provideAsync(
'localAccount',
key => vowTools.when(orchestrate(key, {}, sharedFlows.makeLocalAccount)()),
);

const orchFns = orchestrateAll(flows, {
atomicProvider,
sharedLocalAccount,
makeCombineInvitationMakers,
makeExtraInvitationMaker,
flows,
Expand Down
20 changes: 6 additions & 14 deletions packages/orchestration/src/examples/staking-combinations.flows.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +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 {LocalOrchestrationAccountKit} from '../exos/local-orchestration-account.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 @@ -49,7 +49,7 @@ harden(makeAccount);
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
* @param {object} ctx
* @param {AtomicProvider<string, any>} ctx.atomicProvider
* @param {GuestInterface<LocalOrchestrationAccountKit['holder']>} ctx.sharedLocalAccount
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {GuestInterface<CosmosOrchestrationAccount>} account
* @param {ZCFSeat} seat
Expand All @@ -58,7 +58,7 @@ harden(makeAccount);
*/
export const depositAndDelegate = async (
orch,
{ atomicProvider, zoeTools },
{ sharedLocalAccount, zoeTools },
account,
seat,
validator,
Expand All @@ -67,22 +67,14 @@ export const depositAndDelegate = async (
trace('depositAndDelegate', account, seat, validator);
mustMatch(validator, ChainAddressShape);

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

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

const address = account.getAddress();
try {
await localAccount.transfer(give.Stake, address);
await sharedLocalAccount.transfer(address, give.Stake);
} catch (cause) {
await zoeTools.withdrawToSeat(localAccount, seat, give);
await zoeTools.withdrawToSeat(sharedLocalAccount, seat, give);
const errMsg = makeError(`ibc transfer failed ${q(cause)}`);
seat.exit(errMsg);
throw errMsg;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,17 @@ Generated by [AVA](https://avajs.dev).
ChainHub_kindHandle: 'Alleged: kind',
ChainHub_singleton: 'Alleged: ChainHub',
brandDenom: {},
chainInfos: {},
chainInfos: {
agoric: {
chainId: 'agoric-3',
icqEnabled: false,
stakingTokens: [
{
denom: 'ubld',
},
],
},
},
connectionInfos: {},
denom: {},
},
Expand All @@ -37,16 +47,17 @@ Generated by [AVA](https://avajs.dev).
'ChainHub Admin_singleton': 'Alleged: ChainHub Admin',
'Send PF_kindHandle': 'Alleged: kind',
'Send PF_singleton': 'Alleged: Send PF',
atomicProvider: {},
atomicProvider: {
localAccount: 'Alleged: Local Orchestration Account Kit holder',
},
orchestration: {
localAccount: {
asyncFlow_kindHandle: 'Alleged: kind',
},
sendIt: {
asyncFlow_kindHandle: 'Alleged: kind',
endowments: {
0: {
atomicProvider: {
provideAsync_kindHandle: 'Alleged: kind',
provideAsync_singleton: 'Alleged: provideAsync',
},
zoeTools: {
localTransfer_kindHandle: 'Alleged: kind',
localTransfer_singleton: 'Alleged: localTransfer',
Expand All @@ -64,7 +75,9 @@ Generated by [AVA](https://avajs.dev).
LocalChainFacade_kindHandle: 'Alleged: kind',
Orchestrator_kindHandle: 'Alleged: kind',
RemoteChainFacade_kindHandle: 'Alleged: kind',
chainName: {},
chainName: {
agoric: 'Alleged: LocalChainFacade public',
},
ibcTools: {
IBCTransferSenderKit_kindHandle: 'Alleged: kind',
ibcResultWatcher_kindHandle: 'Alleged: kind',
Expand Down
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -102,13 +102,14 @@ Generated by [AVA](https://avajs.dev).
'ChainHub Admin_singleton': 'Alleged: ChainHub Admin',
CombinedInvitationMakers_kindHandle: 'Alleged: kind',
StakingCombinationsInvitationMakers_kindHandle: 'Alleged: kind',
atomicProvider: {
localAccount: 'Alleged: Local Orchestration Account Kit holder',
},
orchestration: {
depositAndDelegate: {
asyncFlow_kindHandle: 'Alleged: kind',
endowments: {
0: {
contractState_kindHandle: 'Alleged: kind',
contractState_singleton: 'Alleged: contractState',
flows: {
depositAndDelegate_kindHandle: 'Alleged: kind',
depositAndDelegate_singleton: 'Alleged: depositAndDelegate',
Expand All @@ -130,12 +131,13 @@ Generated by [AVA](https://avajs.dev).
},
},
},
localAccount: {
asyncFlow_kindHandle: 'Alleged: kind',
},
makeAccount: {
asyncFlow_kindHandle: 'Alleged: kind',
endowments: {
0: {
contractState_kindHandle: 'Alleged: kind',
contractState_singleton: 'Alleged: contractState',
flows: {
depositAndDelegate_kindHandle: 'Alleged: kind',
depositAndDelegate_singleton: 'Alleged: depositAndDelegate',
Expand All @@ -161,8 +163,6 @@ Generated by [AVA](https://avajs.dev).
asyncFlow_kindHandle: 'Alleged: kind',
endowments: {
0: {
contractState_kindHandle: 'Alleged: kind',
contractState_singleton: 'Alleged: contractState',
flows: {
depositAndDelegate_kindHandle: 'Alleged: kind',
depositAndDelegate_singleton: 'Alleged: depositAndDelegate',
Expand Down
Binary file not shown.
13 changes: 10 additions & 3 deletions packages/orchestration/test/fixtures/zoe-tools.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import { M } from '@endo/patterns';
import { withOrchestration } from '../../src/utils/start-helper.js';
import { prepareChainHubAdmin } from '../../src/exos/chain-hub-admin.js';
import * as flows from './zoe-tools.flows.js';
import * as sharedFlows from '../../src/examples/shared.flows.js';
import fetchedChainInfo from '../../src/fetched-chain-info.js';

const { values } = Object;
Expand Down Expand Up @@ -46,12 +47,17 @@ const contract = async (
zcf,
privateArgs,
zone,
{ atomicProvider, chainHub, orchestrateAll, zoeTools },
{ atomicProvider, chainHub, orchestrate, orchestrateAll, vowTools, zoeTools },
) => {
const creatorFacet = prepareChainHubAdmin(zone, chainHub);

const sharedLocalAccount = await atomicProvider.provideAsync(
'localAccount',
key => vowTools.when(orchestrate(key, {}, sharedFlows.makeLocalAccount)()),
);

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

Expand All @@ -60,7 +66,8 @@ const contract = async (
const assets = /** @type {AssetInfo[]} */ (
await E(E(privateArgs.agoricNames).lookup('vbankAsset')).values()
);
for (const chainName of ['agoric', 'cosmoshub']) {
// 'agoric' was registered by mocaLocalAccount above
for (const chainName of ['cosmoshub']) {
chainHub.registerChain(chainName, fetchedChainInfo[chainName]);
}
for (const brand of values(zcf.getTerms().brands)) {
Expand Down
Loading

0 comments on commit f5198ed

Please sign in to comment.