Skip to content

Commit

Permalink
feat: complete depositAndDelegate flow
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Sep 7, 2024
1 parent 56afc43 commit 928bdfc
Show file tree
Hide file tree
Showing 6 changed files with 167 additions and 46 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,21 @@
* 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';
import { CosmosOrchestrationInvitationMakersInterface } from '../exos/cosmos-orchestration-account.js';
import { ChainAddressShape, DelegationShape } from '../typeGuards.js';
import { withOrchestration } from '../utils/start-helper.js';
import * as flows from './staking-combinations.flows.js';
import { prepareChainHubAdmin } from '../exos/chain-hub-admin.js';

/**
* @import {GuestInterface} from '@agoric/async-flow';
* @import {ContinuingOfferResult} from '@agoric/smart-wallet/src/types.js';
* @import {TimerService} from '@agoric/time';
* @import {LocalChain} from '@agoric/vats/src/localchain.js';
* @import {NameHub} from '@agoric/vats';
* @import {Vow} from '@agoric/vow';
* @import {Remote} from '@agoric/internal';
* @import {Zone} from '@agoric/zone';
* @import {CosmosInterchainService} from '../exos/cosmos-interchain-service.js';
Expand All @@ -46,13 +46,28 @@ const emptyOfferShape = harden({
* storageNode: Remote<StorageNode>;
* marshaller: Marshaller;
* timerService: Remote<TimerService>;
* }} privateArgs
* }} _privateArgs
* @param {Zone} zone
* @param {OrchestrationTools} tools
*/
const contract = async (zcf, privateArgs, zone, { orchestrateAll }) => {
const contract = async (
zcf,
_privateArgs,
zone,
{ orchestrateAll, zoeTools, chainHub },
) => {
const contractState = makeSharedStateRecord(
/**
* @type {{
* account: (OrchestrationAccount<any> & LocalAccountMethods) | undefined;
* }}
*/ {
localAccount: undefined,
},
);

const ExtraInvitationMakerInterface = M.interface('', {
DepositAndDelegate: M.call(M.array()).returns(M.promise()),
DepositAndDelegate: M.call().returns(M.promise()),
UndelegateAndTransfer: M.call(
M.arrayOf(DelegationShape),
ChainAddressShape,
Expand All @@ -71,15 +86,22 @@ const contract = async (zcf, privateArgs, zone, { orchestrateAll }) => {
const { account } = this.state;

return zcf.makeInvitation(
(seat, validatorAddr, amountArg) =>
/**
* @param {ZCFSeat} seat
* @param {{ validator: CosmosValidatorAddress }} offerArgs
*/
(seat, { validator }) =>
// eslint-disable-next-line no-use-before-define -- defined by orchestrateAll, necessarily after this
orchFns.depositAndDelegate(account, seat, validatorAddr, amountArg),
orchFns.depositAndDelegate(account, seat, validator),
'Deposit and delegate',
undefined,
{
give: {
Stake: AmountShape,
},
want: {},
// user cannot exit their seat; contract must exit it.
exit: { waived: M.null() },
},
);
},
Expand Down Expand Up @@ -113,12 +135,16 @@ const contract = async (zcf, privateArgs, zone, { orchestrateAll }) => {
);

const orchFns = orchestrateAll(flows, {
contractState,
makeCombineInvitationMakers,
makeExtraInvitationMaker,
flows,
zcf,
zoeTools,
});

const creatorFacet = prepareChainHubAdmin(zone, chainHub);

const publicFacet = zone.exo('publicFacet', undefined, {
makeAccount() {
return zcf.makeInvitation(
Expand All @@ -130,7 +156,7 @@ const contract = async (zcf, privateArgs, zone, { orchestrateAll }) => {
},
});

return harden({ publicFacet });
return harden({ publicFacet, creatorFacet });
};

export const start = withOrchestration(contract);
Expand Down
43 changes: 33 additions & 10 deletions packages/orchestration/src/examples/staking-combinations.flows.js
Original file line number Diff line number Diff line change
@@ -1,11 +1,16 @@
/**
* @import {GuestInterface} from '@agoric/async-flow';
* @import {Orchestrator, OrchestrationFlow, AmountArg, CosmosValidatorAddress, ChainAddress} from '../types.js'
* @import {Orchestrator, OrchestrationFlow, AmountArg, CosmosValidatorAddress, ChainAddress, LocalAccountMethods, OrchestrationAccountI} from '../types.js'
* @import {ContinuingOfferResult, InvitationMakers} from '@agoric/smart-wallet/src/types.js';
* @import {MakeCombineInvitationMakers} from '../exos/combine-invitation-makers.js';
* @import {CosmosOrchestrationAccount} from '../exos/cosmos-orchestration-account.js';
* @import {ZoeTools} from '../utils/zoe-tools.js';
*/

import { mustMatch } from '@endo/patterns';
import { makeError } from '@endo/errors';
import { ChainAddressShape } from '../typeGuards.js';

/**
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} orch
Expand Down Expand Up @@ -38,25 +43,43 @@ harden(makeAccount);

/**
* @satisfies {OrchestrationFlow}
* @param {Orchestrator} _orch
* @param {object} _ctx
* @param {Orchestrator} orch
* @param {object} ctx
* @param {{ localAccount?: OrchestrationAccountI & LocalAccountMethods }} ctx.contractState
* @param {GuestInterface<ZoeTools>} ctx.zoeTools
* @param {GuestInterface<CosmosOrchestrationAccount>} account
* @param {ZCFSeat} seat
* @param {CosmosValidatorAddress} validator
* @param {AmountArg} amount
* @returns {Promise<string>}
*/
export const depositAndDelegate = async (
_orch,
_ctx,
orch,
{ contractState, zoeTools },
account,
seat,
validator,
amount,
) => {
console.log('depositAndDelegate', account, seat, validator, amount);
// TODO deposit the amount
await account.delegate(validator, amount);
await null;
console.log('depositAndDelegate', account, seat, validator);
mustMatch(validator, ChainAddressShape);
if (!contractState.localAccount) {
const agoricChain = await orch.getChain('agoric');
contractState.localAccount = await agoricChain.makeAccount();
}
const { give } = seat.getProposal();
await zoeTools.localTransfer(seat, contractState.localAccount, give);

// @ts-expect-error Type 'GuestInterface<() => HostInterface<ChainAddress>>' has no call signatures.
const address = account.getAddress();
try {
await contractState.localAccount.transfer(give.Stake, address);
} catch (cause) {
// TODO, put funds back on user seat and exit
// https://github.com/Agoric/agoric-sdk/issues/9925
throw makeError('ibc transfer failed', undefined, { cause });
}
seat.exit();
await account.delegate(validator, give.Stake);
return 'guest depositAndDelegate complete';
};
harden(depositAndDelegate);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,17 @@ Generated by [AVA](https://avajs.dev).
unwrapMap: 'Alleged: weakMapStore',
},
contract: {
'ChainHub Admin_kindHandle': 'Alleged: kind',
'ChainHub Admin_singleton': 'Alleged: ChainHub Admin',
CombinedInvitationMakers_kindHandle: 'Alleged: kind',
ContinuingInvitationExampleInvitationMakers_kindHandle: 'Alleged: kind',
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 @@ -44,13 +48,19 @@ Generated by [AVA](https://avajs.dev).
makeCombineInvitationMakers_singleton: 'Alleged: makeCombineInvitationMakers',
makeExtraInvitationMaker_kindHandle: 'Alleged: kind',
makeExtraInvitationMaker_singleton: 'Alleged: makeExtraInvitationMaker',
zoeTools: {
localTransfer_kindHandle: 'Alleged: kind',
localTransfer_singleton: 'Alleged: localTransfer',
},
},
},
},
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 @@ -63,13 +73,19 @@ Generated by [AVA](https://avajs.dev).
makeCombineInvitationMakers_singleton: 'Alleged: makeCombineInvitationMakers',
makeExtraInvitationMaker_kindHandle: 'Alleged: kind',
makeExtraInvitationMaker_singleton: 'Alleged: makeExtraInvitationMaker',
zoeTools: {
localTransfer_kindHandle: 'Alleged: kind',
localTransfer_singleton: 'Alleged: localTransfer',
},
},
},
},
undelegateAndTransfer: {
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 @@ -82,6 +98,10 @@ Generated by [AVA](https://avajs.dev).
makeCombineInvitationMakers_singleton: 'Alleged: makeCombineInvitationMakers',
makeExtraInvitationMaker_kindHandle: 'Alleged: kind',
makeExtraInvitationMaker_singleton: 'Alleged: makeExtraInvitationMaker',
zoeTools: {
localTransfer_kindHandle: 'Alleged: kind',
localTransfer_singleton: 'Alleged: localTransfer',
},
},
},
},
Expand All @@ -96,7 +116,8 @@ Generated by [AVA](https://avajs.dev).
Orchestrator_kindHandle: 'Alleged: kind',
RemoteChainFacade_kindHandle: 'Alleged: kind',
chainName: {
osmosis: 'Alleged: RemoteChainFacade public',
agoric: 'Alleged: LocalChainFacade public',
cosmoshub: 'Alleged: RemoteChainFacade public',
},
ibcTools: {
IBCTransferSenderKit_kindHandle: 'Alleged: kind',
Expand Down
Binary file not shown.
Loading

0 comments on commit 928bdfc

Please sign in to comment.