Skip to content

Commit

Permalink
improve examples and stubs
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Jun 4, 2024
1 parent 49b8da4 commit 2b40db5
Show file tree
Hide file tree
Showing 7 changed files with 106 additions and 33 deletions.
9 changes: 8 additions & 1 deletion packages/orchestration/src/examples/stakeAtom.contract.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/**
* @file Example contract that uses orchestration
*/
// TODO rename to "stakeIca" or something else that conveys is parameterized nature

import { makeTracer, StorageNodeShape } from '@agoric/internal';
import { TimerServiceShape } from '@agoric/time';
Expand All @@ -20,6 +21,11 @@ const trace = makeTracer('StakeAtom');
*/

export const meta = harden({
customTermsShape: {
hostConnectionId: M.string(),
controllerConnectionId: M.string(),
bondDenom: M.string(),
},
privateArgsShape: {
orchestration: M.remotable('orchestration'),
storageNode: StorageNodeShape,
Expand Down Expand Up @@ -68,7 +74,8 @@ export const start = async (zcf, privateArgs, baggage) => {
hostConnectionId,
controllerConnectionId,
);
// #9212 TODO do not fail if host does not have `async-icq` module;
// TODO https://github.com/Agoric/agoric-sdk/issues/9326
// Should not fail if host does not have `async-icq` module;
// communicate to OrchestrationAccount that it can't send queries
const icqConnection = await E(orchestration).provideICQConnection(
controllerConnectionId,
Expand Down
10 changes: 6 additions & 4 deletions packages/orchestration/src/examples/swapExample.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import { orcUtils } from '../utils/orc.js';
* @import {LocalChain} from '@agoric/vats/src/localchain.js';
* @import {Remote} from '@agoric/internal';
* @import {OrchestrationService} from '../service.js';
* @import {Baggage} from '@agoric/vat-data'
* @import {Zone} from '@agoric/zone';
*/

Expand All @@ -24,7 +25,6 @@ export const meta = {
orchestrationService: M.or(M.remotable('orchestration'), null),
storageNode: StorageNodeShape,
timerService: M.or(TimerServiceShape, null),
zone: M.any(),
},
upgradability: 'canUpgrade',
};
Expand All @@ -46,13 +46,15 @@ export const makeNatAmountShape = (brand, min) =>
* orchestrationService: Remote<OrchestrationService>;
* storageNode: Remote<StorageNode>;
* timerService: Remote<TimerService>;
* zone: Zone;
* }} privateArgs
* @param {Baggage} baggage
*/
export const start = async (zcf, privateArgs) => {
export const start = async (zcf, privateArgs, baggage) => {
const { brands } = zcf.getTerms();

const { localchain, orchestrationService, storageNode, timerService, zone } =
const zone = makeDurableZone(baggage);

const { localchain, orchestrationService, storageNode, timerService } =
privateArgs;

const { orchestrate } = makeOrchestrationFacade({
Expand Down
22 changes: 15 additions & 7 deletions packages/orchestration/src/exos/chainAccountKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ const UNPARSABLE_CHAIN_ADDRESS = 'UNPARSABLE_CHAIN_ADDRESS';

export const ChainAccountI = M.interface('ChainAccount', {
getAddress: M.call().returns(ChainAddressShape),
getBalance: M.callWhen(M.string()).returns(M.any()),
getBalances: M.callWhen().returns(M.any()),
getLocalAddress: M.call().returns(M.string()),
getRemoteAddress: M.call().returns(M.string()),
getPort: M.call().returns(M.remotable('Port')),
Expand Down Expand Up @@ -73,15 +75,21 @@ export const prepareChainAccountKit = zone =>
}),
{
account: {
/**
* @returns {ChainAddress}
*/
/** @returns {ChainAddress} */
getAddress() {
return NonNullish(
this.state.chainAddress,
'ICA channel creation acknowledgement not yet received.',
);
},
getBalance(_denom) {
// UNTIL https://github.com/Agoric/agoric-sdk/issues/9326
throw new Error('not yet implemented');
},
getBalances() {
// UNTIL https://github.com/Agoric/agoric-sdk/issues/9326
throw new Error('not yet implemented');
},
getLocalAddress() {
return NonNullish(
this.state.localAddress,
Expand Down Expand Up @@ -119,9 +127,7 @@ export const prepareChainAccountKit = zone =>
ack => parseTxPacket(ack),
);
},
/**
* Close the remote account
*/
/** Close the remote account */
async close() {
/// XXX what should the behavior be here? and `onClose`?
// - retrieve assets?
Expand All @@ -132,7 +138,9 @@ export const prepareChainAccountKit = zone =>
},
async deposit(payment) {
console.log('deposit got', payment);
throw new Error('not yet implemented');
console.error(
'FIXME deposit noop until https://github.com/Agoric/agoric-sdk/issues/9193',
);
},
/**
* get Purse for a brand to .withdraw() a Payment from the account
Expand Down
14 changes: 6 additions & 8 deletions packages/orchestration/src/exos/icqConnectionKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,12 @@ export const prepareICQConnectionKit = zone =>
* @param {Port} port
*/
port =>
/** @type {ICQConnectionKitState} */ (
harden({
port,
connection: undefined,
remoteAddress: undefined,
localAddress: undefined,
})
),
/** @type {ICQConnectionKitState} */ ({
port,
connection: undefined,
remoteAddress: undefined,
localAddress: undefined,
}),
{
connection: {
getLocalAddress() {
Expand Down
27 changes: 17 additions & 10 deletions packages/orchestration/src/exos/stakingAccountKit.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import {
MsgUndelegateResponse,
} from '@agoric/cosmic-proto/cosmos/staking/v1beta1/tx.js';
import { Any } from '@agoric/cosmic-proto/google/protobuf/any.js';
import { AmountShape } from '@agoric/ertp';
import { AmountShape, PaymentShape } from '@agoric/ertp';
import { makeTracer } from '@agoric/internal';
import { M } from '@agoric/vat-data';
import { TopicsRecordShape } from '@agoric/zoe/src/contractSupport/index.js';
Expand All @@ -38,10 +38,11 @@ import {
import { dateInSeconds } from '../utils/time.js';

/**
* @import {AmountArg, IcaAccount, ChainAddress, CosmosValidatorAddress, ICQConnection, StakingAccountActions, DenomAmount} from '../types.js';
* @import {AmountArg, IcaAccount, ChainAddress, CosmosValidatorAddress, ICQConnection, StakingAccountActions, DenomAmount, OrchestrationAccountI} from '../types.js';
* @import {RecorderKit, MakeRecorderKit} from '@agoric/zoe/src/contractSupport/recorder.js';
* @import {Coin} from '@agoric/cosmic-proto/cosmos/base/v1beta1/coin.js';
* @import {Delegation} from '@agoric/cosmic-proto/cosmos/staking/v1beta1/staking.js';
* @import {Remote} from '@agoric/internal';
* @import {TimerService} from '@agoric/time';
* @import {Zone} from '@agoric/zone';
*/
Expand All @@ -61,15 +62,18 @@ const { Fail } = assert;
* chainAddress: ChainAddress;
* icqConnection: ICQConnection;
* bondDenom: string;
* timer: TimerService;
* timer: Remote<TimerService>;
* }} State
*/

/** @see {OrchestrationAccountI} */
export const IcaAccountHolderI = M.interface('IcaAccountHolder', {
getPublicTopics: M.call().returns(TopicsRecordShape),
getAddress: M.call().returns(ChainAddressShape),
getBalance: M.callWhen().optional(M.string()).returns(CoinShape),
getBalances: M.callWhen().optional(M.string()).returns(M.arrayOf(CoinShape)),
delegate: M.callWhen(ChainAddressShape, AmountShape).returns(M.undefined()),
deposit: M.callWhen(PaymentShape).returns(M.undefined()),
redelegate: M.callWhen(
ChainAddressShape,
ChainAddressShape,
Expand Down Expand Up @@ -140,7 +144,7 @@ export const prepareStakingAccountKit = (zone, makeRecorderKit, zcf) => {
* @param {IcaAccount} io.account
* @param {StorageNode} io.storageNode
* @param {ICQConnection} io.icqConnection
* @param {TimerService} io.timer
* @param {Remote<TimerService>} io.timer
* @returns {State}
*/
(chainAddress, bondDenom, io) => {
Expand Down Expand Up @@ -221,9 +225,7 @@ export const prepareStakingAccountKit = (zone, makeRecorderKit, zcf) => {
return this.facets.holder.withdrawReward(validator);
}, 'WithdrawReward');
},
/**
* @param {Delegation[]} delegations
*/
/** @param {Delegation[]} delegations */
Undelegate(delegations) {
trace('Undelegate', delegations);

Expand Down Expand Up @@ -285,6 +287,13 @@ export const prepareStakingAccountKit = (zone, makeRecorderKit, zcf) => {

expect(result, trivialDelegateResponse, 'MsgDelegateResponse');
},
async deposit(payment) {
const { helper } = this.facets;
return E(helper.owned()).deposit(payment);
},
async getBalances() {
throw Error('not yet implemented');
},
/**
* _Assumes users has already sent funds to their ICA, until #9193
*
Expand Down Expand Up @@ -361,9 +370,7 @@ export const prepareStakingAccountKit = (zone, makeRecorderKit, zcf) => {
throw assert.error('Not implemented');
},

/**
* @param {Delegation[]} delegations
*/
/** @param {Delegation[]} delegations */
async undelegate(delegations) {
trace('undelegate', delegations);
const { helper } = this.facets;
Expand Down
56 changes: 54 additions & 2 deletions packages/orchestration/src/facade.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
/** @file Orchestration service */

import { E } from '@endo/far';
import { prepareStakingAccountKit } from './exos/stakingAccountKit.js';

/**
* @import {Zone} from '@agoric/zone';
Expand Down Expand Up @@ -34,6 +35,8 @@ const makeLocalChainFacade = localchain => {
pfmEnabled: true,
};
},

// @ts-expect-error FIXME promise resolution through membrane
async makeAccount() {
const account = await E(localchain).makeAccount();

Expand All @@ -42,6 +45,30 @@ const makeLocalChainFacade = localchain => {
console.log('deposit got', payment);
return E(account).deposit(payment);
},
async getAddress() {
const addressStr = await E(account).getAddress();
return {
address: addressStr,
chainId: 'agoric-3',
addressEncoding: 'bech32',
};
},
getBalance(_denom) {
// FIXME map denom to Brand
const brand = /** @type {any} */ (null);
return E(account).getBalance(brand);
},
getBalances() {
throw new Error('not yet implemented');
},
send(toAccount, amount) {
// FIXME implement
console.log('send got', toAccount, amount);
},
transfer(amount, destination, opts) {
// FIXME implement
console.log('transfer got', amount, destination, opts);
},
transferSteps(amount, msg) {
console.log('transferSteps got', amount, msg);
return Promise.resolve();
Expand All @@ -56,9 +83,12 @@ const makeLocalChainFacade = localchain => {
* @param {C} name
* @param {object} io
* @param {Remote<OrchestrationService>} io.orchestration
* @param {Remote<TimerService>} io.timer
* @param {ZCF} io.zcf
* @param {Zone} io.zone
* @returns {Chain<C>}
*/
const makeRemoteChainFacade = (name, { orchestration }) => {
const makeRemoteChainFacade = (name, { orchestration, timer, zcf, zone }) => {
const chainInfo = /** @type {CosmosChainInfo} */ ({
allegedName: name,
chainId: 'fixme',
Expand All @@ -70,6 +100,13 @@ const makeRemoteChainFacade = (name, { orchestration }) => {
allowedMessages: [],
allowedQueries: [],
});
const makeRecorderKit = () => anyVal;
const makeStakingAccountKit = prepareStakingAccountKit(
zone.subZone(name),
makeRecorderKit,
zcf,
);

return {
getChainInfo: async () => chainInfo,
/** @returns {Promise<OrchestrationAccount<C>>} */
Expand All @@ -80,10 +117,22 @@ const makeRemoteChainFacade = (name, { orchestration }) => {
const hostConnectionId = 'connection-1';
const controllerConnectionId = 'connection-2';

return E(orchestration).makeAccount(
const icaAccount = await E(orchestration).makeAccount(
hostConnectionId,
controllerConnectionId,
);

const address = await E(icaAccount).getAddress();

// FIXME look up real values
const bondDenom = name;
// @ts-expect-error FIXME promise resolution through membrane
return makeStakingAccountKit(address, bondDenom, {
account: icaAccount,
storageNode: anyVal,
icqConnection: anyVal,
timer,
}).holder;
},
};
};
Expand Down Expand Up @@ -133,6 +182,9 @@ export const makeOrchestrationFacade = ({

return makeRemoteChainFacade(name, {
orchestration: orchestrationService,
timer: timerService,
zcf,
zone,
});
},
makeLocalAccount() {
Expand Down
1 change: 0 additions & 1 deletion packages/orchestration/test/examples/swapExample.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@ test('start', async t => {
orchestrationService: bootstrap.orchestration,
storageNode: bootstrap.storage.rootNode,
timerService: bootstrap.timer,
zone: bootstrap.rootZone,
},
);

Expand Down

0 comments on commit 2b40db5

Please sign in to comment.