Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9207 orchestrator stub #9208

Merged
merged 5 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .eslintignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
# also ignored in packages/cosmic-proto/.eslintignore, but IDE's pick up the root config
packages/cosmic-proto/src/codegen/
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
"prettier-plugin-jsdoc": "^1.0.0",
"prettier-plugin-sh": "^0.14.0",
"type-coverage": "^2.27.1",
"typedoc": "^0.25.12",
"typedoc": "^0.25.13",
"typedoc-plugin-markdown": "^3.17.1",
"typescript": "^5.5.0-dev.20240327",
"typescript-eslint": "^7.3.1"
Expand Down
7 changes: 3 additions & 4 deletions packages/boot/test/bootstrapTests/test-vat-orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
MsgDelegateResponse,
} from '@agoric/cosmic-proto/cosmos/staking/v1beta1/tx.js';
import { Any } from '@agoric/cosmic-proto/google/protobuf/any';
import type { ChainAccount, Orchestration } from '@agoric/orchestration';
import type { ChainAccount, OrchestrationService } from '@agoric/orchestration';
import { decodeBase64 } from '@endo/base64';
import { M, matches } from '@endo/patterns';
import { makeWalletFactoryContext } from './walletFactory.ts';
Expand Down Expand Up @@ -100,7 +100,7 @@ test('ICA connection can be closed', async t => {
runUtils: { EV },
} = t.context;

const orchestration: Orchestration =
const orchestration: OrchestrationService =
await EV.vat('bootstrap').consumeItem('orchestration');

const account = await EV(orchestration).createAccount(
Expand All @@ -109,8 +109,7 @@ test('ICA connection can be closed', async t => {
);
t.truthy(account, 'createAccount returns an account');

const res = await EV(account).close();
t.is(res, 'Connection closed');
await EV(account).close();

await t.throwsAsync(EV(account).executeEncodedTx([delegateMsgSuccess]), {
message: 'Connection closed',
Expand Down
3 changes: 2 additions & 1 deletion packages/cosmic-proto/.eslintignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ node_modules/
dist/
main/
module/
coverage/
coverage/
src/codegen/
4 changes: 4 additions & 0 deletions packages/cosmic-proto/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,10 @@
"types": "./dist/codegen/cosmos/tx/v1beta1/tx.d.ts",
"default": "./dist/codegen/cosmos/tx/v1beta1/tx.js"
},
"./cosmos/staking/v1beta1/staking.js": {
"types": "./dist/codegen/cosmos/staking/v1beta1/staking.d.ts",
"default": "./dist/codegen/cosmos/staking/v1beta1/staking.js"
},
"./cosmos/staking/v1beta1/tx.js": {
"types": "./dist/codegen/cosmos/staking/v1beta1/tx.d.ts",
"default": "./dist/codegen/cosmos/staking/v1beta1/tx.js"
Expand Down
2 changes: 1 addition & 1 deletion packages/orchestration/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,4 @@
export * from './src/types.js';
export * from './src/utils/address.js';
export * from './src/utils/tx.js';
export * from './src/orchestration.js';
export * from './src/service.js';
1 change: 1 addition & 0 deletions packages/orchestration/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"@agoric/network": "^0.1.0",
"@agoric/notifier": "^0.6.2",
"@agoric/store": "^0.9.2",
"@agoric/time": "^0.3.2",
"@agoric/vat-data": "^0.5.2",
"@agoric/vats": "^0.15.1",
"@agoric/zoe": "^0.26.2",
Expand Down
4 changes: 2 additions & 2 deletions packages/orchestration/src/contracts/stakeAtom.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { prepareStakingAccountHolder } from './stakingAccountHolder.js';

const trace = makeTracer('StakeAtom');
/**
* @import { Orchestration } from '../types.js';
* @import { OrchestrationService } from '../service.js'
* @import { Baggage } from '@agoric/vat-data';
* @import { IBCConnectionID } from '@agoric/vats';
*/
Expand All @@ -27,7 +27,7 @@ const trace = makeTracer('StakeAtom');
*
* @param {ZCF<StakeAtomTerms>} zcf
* @param {{
* orchestration: Orchestration;
* orchestration: OrchestrationService;
* storageNode: StorageNode;
* marshaller: Marshaller;
* }} privateArgs
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const { Fail } = assert;
* @typedef {{
* topicKit: RecorderKit<StakingAccountNotification>;
* account: ChainAccount;
* chainAddress: ChainAddress;
* chainAddress: string;
* }} State
*/

Expand Down Expand Up @@ -71,7 +71,7 @@ export const prepareStakingAccountHolder = (baggage, makeRecorderKit, zcf) => {
/**
* @param {ChainAccount} account
* @param {StorageNode} storageNode
* @param {ChainAddress} chainAddress
* @param {string} chainAddress
* @returns {State}
*/
(account, storageNode, chainAddress) => {
Expand All @@ -93,6 +93,8 @@ export const prepareStakingAccountHolder = (baggage, makeRecorderKit, zcf) => {
getUpdater() {
return this.state.topicKit.recorder;
},
// TODO move this beneath the Orchestration abstraction,
// to the OrchestrationAccount provided by createAccount()
/**
* _Assumes users has already sent funds to their ICA, until #9193
* @param {string} validatorAddress
Expand Down
102 changes: 102 additions & 0 deletions packages/orchestration/src/contracts/swapExample.contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
// @ts-check
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Per feedback in #9198 it's maybe best to move these to /tools or /examples. I am planning to move the current contents of /contracts to /examples in that PR to avoid potential confusion

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think whatever we do should be consistent across the repository. I'm going to leave this for a later PR in which we execute a plan wholistically

import { Fail } from '@agoric/assert';
import { AmountMath, AmountShape } from '@agoric/ertp';
import { E, Far } from '@endo/far';
import { M } from '@endo/patterns';
import { makeOrchestrationFacade } from '../facade.js';
import { orcUtils } from '../utils/orc.js';

/**
* @import {Orchestrator, ChainAccount, CosmosValidatorAddress} from '../types.js'
* @import {TimerService} from '@agoric/time';
* @import {ERef} from '@endo/far'
* @import {OrchestrationService} from '../service.js';
* @import {Zone} from '@agoric/zone';
*/

/**
* @param {ZCF} zcf
* @param {{
* orchestrationService: ERef<OrchestrationService>;
* storageNode: ERef<StorageNode>;
* timerService: ERef<TimerService>;
* zone: Zone;
* }} privateArgs
*/
export const start = async (zcf, privateArgs) => {
const { orchestrationService, storageNode, timerService, zone } = privateArgs;

const { orchestrate } = makeOrchestrationFacade({
zone,
timerService,
zcf,
storageNode,
orchestrationService,
});

/** deprecated historical example */
/** @type {OfferHandler} */
const swapAndStakeHandler = orchestrate(
'LSTTia',
{ zcf },
// eslint-disable-next-line no-shadow -- this `zcf` is enclosed in a membrane
async (/** @type {Orchestrator} */ orch, { zcf }, seat, offerArgs) => {
const { give } = seat.getProposal();
!AmountMath.isEmpty(give.USDC.value) || Fail`Must provide USDC.`;

const celestia = await orch.getChain('celestia');
const agoric = await orch.getChain('agoric');

const [celestiaAccount, localAccount] = await Promise.all([
celestia.createAccount(),
agoric.createAccount(),
]);

const tiaAddress = await celestiaAccount.getChainAddress();

// deposit funds from user seat to LocalChainAccount
const seatKit = zcf.makeEmptySeatKit();
zcf.atomicRearrange(harden([[seat, seatKit.zcfSeat, give]]));
// seat.exit() // exit user seat now, or later?
const payment = await E(seatKit.userSeat).getPayout('USDC');
await localAccount.deposit(payment);

// build swap instructions with orcUtils library
const transferMsg = orcUtils.makeOsmosisSwap({
destChain: 'celestia',
destAddress: tiaAddress,
amountIn: give.USDC,
brandOut: offerArgs.staked.brand,
slippage: 0.03,
});

await localAccount
.transferSteps(give.USDC, transferMsg)
.then(_txResult =>
celestiaAccount.delegate(offerArgs.validator, offerArgs.staked),
)
.catch(e => console.error(e));

// XXX close localAccount?
return celestiaAccount; // should be continuing inv since this is an offer?
},
);

const makeSwapAndStakeInvitation = () =>
zcf.makeInvitation(
swapAndStakeHandler,
'Swap for TIA and stake',
undefined,
harden({
give: { USDC: AmountShape },
want: {}, // XXX ChainAccount Ownable?
exit: M.any(),
}),
);

const publicFacet = Far('SwapAndStake Public Facet', {
makeSwapAndStakeInvitation,
});

return harden({ publicFacet });
};
87 changes: 87 additions & 0 deletions packages/orchestration/src/contracts/unbondExample.contract.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// @ts-check
import { Fail } from '@agoric/assert';
import { AmountMath, AmountShape } from '@agoric/ertp';
import { Far } from '@endo/far';
import { M } from '@endo/patterns';
import { makeOrchestrationFacade } from '../facade.js';

/**
* @import {Orchestrator, ChainAccount, CosmosValidatorAddress} from '../types.js'
* @import {TimerService} from '@agoric/time';
* @import {ERef} from '@endo/far'
* @import {OrchestrationService} from '../service.js';
* @import {Zone} from '@agoric/zone';
*/

/**
* @param {ZCF} zcf
* @param {{
* orchestrationService: ERef<OrchestrationService>;
* storageNode: ERef<StorageNode>;
* timerService: ERef<TimerService>;
* zone: Zone;
* }} privateArgs
*/
export const start = async (zcf, privateArgs) => {
const { orchestrationService, storageNode, timerService, zone } = privateArgs;

const { orchestrate } = makeOrchestrationFacade({
zone,
timerService,
zcf,
storageNode,
orchestrationService,
});

/** @type {OfferHandler} */
const unbondAndLiquidStake = orchestrate(
'LSTTia',
{ zcf },
// eslint-disable-next-line no-shadow -- this `zcf` is enclosed in a membrane
async (/** @type {Orchestrator} */ orch, { zcf }, seat, _offerArgs) => {
console.log('zcf within the membrane', zcf);
const { give } = seat.getProposal();
!AmountMath.isEmpty(give.USDC.value) || Fail`Must provide USDC.`;

// We would actually alreaady have the account from the orchestrator
// ??? could these be passed in? It would reduce the size of this handler,
// keeping it focused on long-running operations.
const celestia = await orch.getChain('celestia');
const celestiaAccount = await celestia.createAccount();

const delegations = await celestiaAccount.getDelegations();
const [undelegation] = await celestiaAccount.undelegate(delegations);

// wait for the undelegations to be complete (may take weeks)
await undelegation.completion;
Comment on lines +53 to +56
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍 highlighting this change for other reviewers


// ??? should this be synchronous? depends on how names are resolved.
const stride = await orch.getChain('stride');
const strideAccount = await stride.createAccount();

// TODO the `TIA` string actually needs to be the Brand from AgoricNames
const tiaAmt = await celestiaAccount.getBalance('TIA');
await celestiaAccount.transfer(tiaAmt, strideAccount.getChainAddress());

await strideAccount.liquidStake(tiaAmt);
},
);

const makeUnbondAndLiquidStakeInvitation = () =>
zcf.makeInvitation(
unbondAndLiquidStake,
'Unbond and liquid stake',
undefined,
harden({
give: { USDC: AmountShape },
want: {}, // XXX ChainAccount Ownable?
exit: M.any(),
}),
);

const publicFacet = Far('SwapAndStake Public Facet', {
makeUnbondAndLiquidStakeInvitation,
});

return harden({ publicFacet });
};
47 changes: 47 additions & 0 deletions packages/orchestration/src/facade.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// @ts-check
/** @file Orchestration service */

/**
* @import {Zone} from '@agoric/zone';
* @import {TimerService} from '@agoric/time';
* @import {OrchestrationService} from './service.js';
* @import {OrchestrationHandlerMaker} from './types.js';
*/

/**
*
* @param {{
* zone: Zone;
* timerService: ERef<TimerService>;
* zcf: ERef<ZCF>;
* storageNode: ERef<StorageNode>;
* orchestrationService: ERef<OrchestrationService>;
* }} powers
*/
export const makeOrchestrationFacade = ({
zone,
timerService,
zcf,
storageNode,
orchestrationService,
}) => {
console.log('makeOrchestrationFacade got', {
zone,
timerService,
zcf,
storageNode,
orchestrationService,
});

return {
/**
* @type {OrchestrationHandlerMaker}
*/
orchestrate(durableName, ctx, fn) {
console.log('orchestrate got', durableName, ctx, fn);
throw new Error('Not yet implemented');
},
};
};
harden(makeOrchestrationFacade);
/** @typedef {ReturnType<typeof makeOrchestrationFacade>} OrchestrationFacade */
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,11 @@
import { V as E } from '@agoric/vat-data/vow.js';
import { Far } from '@endo/far';

/** @import { AttenuatedNetwork, Orchestration, OrchestrationVat } from '../types' */
/**
* @import { AttenuatedNetwork } from '../types'
* @import { OrchestrationService } from '../service.js'
* @import { OrchestrationVat } from '../vat-orchestration.js'
*/

/**
* @param {BootstrapPowers & {
Expand Down Expand Up @@ -67,7 +71,7 @@ export const setupOrchestrationVat = async (
/**
* @param {BootstrapPowers & {
* consume: {
* orchestration: Orchestration;
* orchestration: OrchestrationService;
* };
* }} powers
* @param {object} _options
Expand Down
Loading
Loading