Skip to content

Commit

Permalink
feat(orchestration): stakeAtom delegate
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed Apr 9, 2024
1 parent 764e4a8 commit 54d830f
Show file tree
Hide file tree
Showing 6 changed files with 360 additions and 18 deletions.
72 changes: 70 additions & 2 deletions packages/boot/test/bootstrapTests/test-orchestration.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { test as anyTest } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import type { TestFn } from 'ava';

import { Fail } from '@agoric/assert';
import { AmountMath } from '@agoric/ertp';
import type { start as stakeBldStart } from '@agoric/orchestration/src/contracts/stakeBld.contract.js';
import type { Instance } from '@agoric/zoe/src/zoeService/utils.js';
import { M, matches } from '@endo/patterns';
Expand Down Expand Up @@ -60,7 +61,6 @@ test.serial('stakeBld', async t => {

const current = await wd.getCurrentWalletRecord();
const latest = await wd.getLatestUpdateRecord();
console.log({ current, latest });
t.like(current, {
offerToPublicSubscriberPaths: [
// TODO publish something useful
Expand Down Expand Up @@ -88,7 +88,7 @@ test.serial('stakeBld', async t => {
});
});

test.serial('stakeAtom', async t => {
test.serial('stakeAtom - repl-style', async t => {
const {
buildProposal,
evalProposal,
Expand Down Expand Up @@ -120,4 +120,72 @@ test.serial('stakeAtom', async t => {
matches(account, M.remotable('ChainAccount')),
'account is a remotable',
);

const atomBrand = await EV(agoricNames).lookup('brand', 'ATOM');
const atomAmount = AmountMath.make(atomBrand, 10n);

const res = await EV(account).delegate('cosmosvaloper1test', atomAmount);
t.is(res, 'Success', 'delegate returns Success');
});

test.serial('stakeAtom - smart wallet', async t => {
const { agoricNamesRemotes } = t.context;

const wd = await t.context.walletFactoryDriver.provideSmartWallet(
'agoric1testStakAtom',
);

await wd.executeOffer({
id: 'request-account',
invitationSpec: {
source: 'agoricContract',
instancePath: ['stakeAtom'],
callPipe: [['makeCreateAccountInvitation']],
},
proposal: {},
});
t.like(wd.getCurrentWalletRecord(), {
offerToPublicSubscriberPaths: [
['request-account', { account: 'published.stakeAtom' }],
],
});
t.like(wd.getLatestUpdateRecord(), {
status: { id: 'request-account', numWantsSatisfied: 1 },
});

const { ATOM } = agoricNamesRemotes.brand;
ATOM || Fail`ATOM missing from agoricNames`;

await t.notThrowsAsync(
wd.executeOffer({
id: 'request-delegate-success',
invitationSpec: {
source: 'continuing',
previousOffer: 'request-account',
invitationMakerName: 'Delegate',
invitationArgs: ['cosmosvaloper1test', { brand: ATOM, value: 10n }],
},
proposal: {},
}),
);
t.like(wd.getLatestUpdateRecord(), {
status: { id: 'request-delegate-success', numWantsSatisfied: 1 },
});

await t.throwsAsync(
wd.executeOffer({
id: 'request-delegate-fail',
invitationSpec: {
source: 'continuing',
previousOffer: 'request-account',
invitationMakerName: 'Delegate',
invitationArgs: ['cosmosvaloper1fail', { brand: ATOM, value: 10n }],
},
proposal: {},
}),
{
message: 'ABCI code: 5: error handling packet: see events for details',
},
'delegate fails with invalid validator',
);
});
4 changes: 2 additions & 2 deletions packages/boot/test/tools/ibc/test-mocks.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// @ts-check

import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';
import { addParamsToVersion } from '../../../tools/ibc/mocks.js';
import { addParamsIfJsonVersion } from '../../../tools/ibc/mocks.js';

test('addParamsToVersion', t => {
const params = { address: 'cosmos1234' };
Expand All @@ -26,6 +26,6 @@ test('addParamsToVersion', t => {
];

for (const { version, expected, message } of scenarios) {
t.is(addParamsToVersion(version, params), expected, message);
t.is(addParamsIfJsonVersion(version, params), expected, message);
}
});
68 changes: 57 additions & 11 deletions packages/orchestration/src/contracts/stakeAtom.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,51 +2,97 @@
/**
* @file Example contract that uses orchestration
*/

import { makeTracer } from '@agoric/internal';
import { makeDurableZone } from '@agoric/zone/durable.js';
import { V as E } from '@agoric/vat-data/vow.js';
import { M } from '@endo/patterns';
import { prepareRecorderKitMakers } from '@agoric/zoe/src/contractSupport';
import { prepareStakingAccountHolder } from './stakingAccountHolder.js';

const trace = makeTracer('StakeAtom');
/**
* @import * as orchestration from '../types'
* @import * as vatData from '@agoric/vat-data'
* @import { Orchestration } from '../types.js';
* @import { Baggage } from '@agoric/vat-data';
* @import { IBCConnectionID } from '@agoric/vats';
*/

/**
* @typedef {{
* hostConnectionId: orchestration.ConnectionId;
* controllerConnectionId: orchestration.ConnectionId;
* hostConnectionId: IBCConnectionID;
* controllerConnectionId: IBCConnectionID;
* }} StakeAtomTerms
*/

/**
*
* @param {ZCF<StakeAtomTerms>} zcf
* @param {{
* orchestration: orchestration.Orchestration;
* orchestration: Orchestration;
* storageNode: StorageNode;
* marshaller: Marshaller;
* }} privateArgs
* @param {vatData.Baggage} baggage
* @param {Baggage} baggage
*/
export const start = async (zcf, privateArgs, baggage) => {
const { hostConnectionId, controllerConnectionId } = zcf.getTerms();
const { orchestration } = privateArgs;
const { orchestration, marshaller, storageNode } = privateArgs;

const zone = makeDurableZone(baggage);

const { makeRecorderKit } = prepareRecorderKitMakers(baggage, marshaller);

const makeStakingAccountHolder = prepareStakingAccountHolder(
baggage,
makeRecorderKit,
zcf,
);

async function createAccount() {
const account = await E(orchestration).createAccount(
hostConnectionId,
controllerConnectionId,
);
const accountAddress = await E(account).getAccountAddress();
trace('account address', accountAddress);
const { holder, invitationMakers } = makeStakingAccountHolder(
account,
storageNode,
accountAddress,
);
return {
publicSubscribers: holder.getPublicTopics(),
invitationMakers,
account: holder,
};
}

const publicFacet = zone.exo(
'StakeAtom',
M.interface('StakeAtomI', {
createAccount: M.callWhen().returns(M.remotable('ChainAccount')),
makeCreateAccountInvitation: M.call().returns(M.promise()),
}),
{
async createAccount() {
return E(orchestration).createAccount(
hostConnectionId,
controllerConnectionId,
trace('createAccount');
return createAccount().then(({ account }) => account);
},
makeCreateAccountInvitation() {
trace('makeCreateAccountInvitation');
return zcf.makeInvitation(
async seat => {
seat.exit();
return createAccount();
},
'wantStakingAccount',
undefined,
undefined,
);
},
},
);

return { publicFacet };
};

/** @typedef {typeof start} StakeAtomSF */
Loading

0 comments on commit 54d830f

Please sign in to comment.