Skip to content

Commit

Permalink
fixup! feat(localchain): add .transfer() helper to LocalChainAccount
Browse files Browse the repository at this point in the history
  • Loading branch information
0xpatrickdev committed May 23, 2024
1 parent 8a2ff2b commit 68c8762
Show file tree
Hide file tree
Showing 8 changed files with 40 additions and 27 deletions.
15 changes: 8 additions & 7 deletions packages/orchestration/src/examples/stakeBld.contract.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,9 @@ import { deeplyFulfilled } from '@endo/marshal';
import { M } from '@endo/patterns';
import { prepareLocalChainAccountKit } from '../exos/local-chain-account-kit.js';
import { prepareMockChainInfo } from '../utils/mockChainInfo.js';
import { makeTimestampHelper } from '../utils/time.js';

/**
* @import {TimerService} from '@agoric/time';
* @import {TimerBrand, TimerService} from '@agoric/time';
*/

const trace = makeTracer('StakeBld');
Expand All @@ -27,12 +26,14 @@ const trace = makeTracer('StakeBld');
* marshaller: Marshaller;
* storageNode: StorageNode;
* timerService: TimerService;
* timerBrand: TimerBrand;
* }} privateArgs
* @param {import("@agoric/vat-data").Baggage} baggage
*/
export const start = async (zcf, privateArgs, baggage) => {
const { BLD } = zcf.getTerms().brands;
const BLD = zcf.getTerms().brands.In;

// XXX is this safe to call before prepare statements are completed?
const bldAmountShape = await E(BLD).getAmountShape();

const zone = makeDurableZone(baggage);
Expand All @@ -46,13 +47,12 @@ export const start = async (zcf, privateArgs, baggage) => {
// Would expect this to be instantiated elsewhere, and passed in as a reference
const agoricChainInfo = prepareMockChainInfo(zone);

const timestampHelper = await makeTimestampHelper(privateArgs.timerService);

const makeLocalChainAccountKit = prepareLocalChainAccountKit(
baggage,
makeRecorderKit,
zcf,
timestampHelper,
privateArgs.timerService,
privateArgs.timerBrand,
agoricChainInfo,
);

Expand All @@ -69,7 +69,7 @@ export const start = async (zcf, privateArgs, baggage) => {
const publicFacet = zone.exo(
'StakeBld',
M.interface('StakeBldI', {
makeAccount: M.callWhen().returns(M.remotable('LocalChainAccount')),
makeAccount: M.callWhen().returns(M.remotable('LocalChainAccountHolder')),
makeAcountInvitationMaker: M.callWhen().returns(InvitationShape),
makeStakeBldInvitation: M.callWhen().returns(InvitationShape),
}),
Expand All @@ -84,6 +84,7 @@ export const start = async (zcf, privateArgs, baggage) => {
withdrawFromSeat(zcf, seat, give),
);
await E(holder).deposit(In);
seat.exit();
return harden({
publicSubscribers: holder.getPublicTopics(),
invitationMakers,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,7 @@ export const start = async (zcf, privateArgs) => {
// deposit funds from user seat to LocalChainAccount
const payments = await withdrawFromSeat(zcf, seat, give);
await deeplyFulfilled(objectMap(payments, localAccount.deposit));
seat.exit();

// build swap instructions with orcUtils library
const transferMsg = orcUtils.makeOsmosisSwap({
Expand Down
18 changes: 13 additions & 5 deletions packages/orchestration/src/exos/local-chain-account-kit.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,15 @@ import {
ChainAddressShape,
IBCTransferOptionsShape,
} from '../typeGuards.js';
import { makeTimestampHelper } from '../utils/time.js';

/**
* @import {LocalChainAccount} from '@agoric/vats/src/localchain.js';
* @import {AmountArg, ChainAddress, DenomAmount, IBCMsgTransferOptions, CosmosChainInfo} from '@agoric/orchestration';
* @import {RecorderKit, MakeRecorderKit} from '@agoric/zoe/src/contractSupport/recorder.js'.
* @import {Baggage} from '@agoric/vat-data';
* @import {TimerService, TimerBrand} from '@agoric/time';
* @import {TimestampHelper} from '../utils/time.js';
*/

// partial until #8879
Expand All @@ -31,7 +36,7 @@ const { Fail } = assert;

/**
* @typedef {{
* topicKit: import('@agoric/zoe/src/contractSupport/recorder.js').RecorderKit<LocalChainAccountNotification>;
* topicKit: RecorderKit<LocalChainAccountNotification>;
* account: LocalChainAccount | null;
* address: ChainAddress['address'];
* }} State
Expand All @@ -56,19 +61,22 @@ const PUBLIC_TOPICS = {
};

/**
* @param {import('@agoric/swingset-liveslots').Baggage} baggage
* @param {import('@agoric/zoe/src/contractSupport/recorder.js').MakeRecorderKit} makeRecorderKit
* @param {Baggage} baggage
* @param {MakeRecorderKit} makeRecorderKit
* @param {ZCF} zcf
* @param {import('../utils/time.js').TimestampHelper} timestampHelper
* @param {TimerService} timerService
* @param {TimerBrand} timerBrand
* @param {AgoricChainInfo} agoricChainInfo
*/
export const prepareLocalChainAccountKit = (
baggage,
makeRecorderKit,
zcf,
timestampHelper,
timerService,
timerBrand,
agoricChainInfo,
) => {
const timestampHelper = makeTimestampHelper(timerService, timerBrand);
const makeAccountHolderKit = prepareExoClassKit(
baggage,
'Account Holder',
Expand Down
11 changes: 8 additions & 3 deletions packages/orchestration/src/proposals/start-stakeBld.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export const startStakeBld = async ({
consume: {
board,
chainStorage,
chainTimerService,
chainTimerService: chainTimerServiceP,
localchain,
startUpgradable,
},
Expand All @@ -34,7 +34,11 @@ export const startStakeBld = async ({
// NB: committee must only publish what it intended to be public
const marshaller = await E(board).getPublishingMarshaller();

// FIXME this isn't detecting missing privateArgs
const [timerService, timerBrand] = await Promise.all([
chainTimerServiceP,
chainTimerServiceP.then(ts => E(ts).getTimerBrand()),
]);

/** @type {StartUpgradableOpts<import('../../src/examples/stakeBld.contract.js').start>} */
const startOpts = {
label: 'stakeBld',
Expand All @@ -43,7 +47,8 @@ export const startStakeBld = async ({
terms: {},
privateArgs: {
localchain: await localchain,
timerService: await chainTimerService,
timerService,
timerBrand,
storageNode,
marshaller,
},
Expand Down
8 changes: 4 additions & 4 deletions packages/orchestration/src/utils/time.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@ export const NANOSECONDS_PER_SECOND = 1_000_000_000n;

/**
* @param {TimerService} timer
* @param {TimerBrand} timerBrand
*/
export async function makeTimestampHelper(timer) {
const timerBrand = await E(timer).getTimerBrand();
return {
export function makeTimestampHelper(timer, timerBrand) {
return harden({
/**
* Takes the current time from ChainTimerService and adds a relative
* time to determine a timeout timestamp in nanoseconds.
Expand All @@ -32,7 +32,7 @@ export async function makeTimestampHelper(timer) {
NANOSECONDS_PER_SECOND
);
},
};
});
}

/** @typedef {Awaited<ReturnType<typeof makeTimestampHelper>>} TimestampHelper */
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,14 @@ const coreEval = async (

const { publicFacet } = await E(zoe).startInstance(
installation,
{ BLD: stake.issuer },
{ In: stake.issuer },
{},
{
localchain,
marshaller,
storageNode: storage.rootNode,
timerService: timer,
timerBrand: timer.getTimerBrand(),
},
);
return { publicFacet, zoe };
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ import { makeFakeLocalchainBridge } from '../supports.js';
import { prepareLocalChainAccountKit } from '../../src/exos/local-chain-account-kit.js';
import { prepareMockChainInfo } from '../../src/utils/mockChainInfo.js';
import { ChainAddress } from '../../src/orchestration-api.js';
import {
makeTimestampHelper,
NANOSECONDS_PER_SECOND,
} from '../../src/utils/time.js';
import { NANOSECONDS_PER_SECOND } from '../../src/utils/time.js';

test('localChainAccountKit - transfer', async t => {
const bootstrap = async () => {
Expand Down Expand Up @@ -57,7 +54,6 @@ test('localChainAccountKit - transfer', async t => {

t.log('chainInfo mocked via `prepareMockChainInfo` until #8879');
const agoricChainInfo = prepareMockChainInfo(rootZone.subZone('chainInfo'));
const timestampHelper = await makeTimestampHelper(timer);

t.log('exo setup - prepareLocalChainAccountKit');
const baggage = makeScalarBigMapStore<string, unknown>('baggage', {
Expand All @@ -69,7 +65,8 @@ test('localChainAccountKit - transfer', async t => {
makeRecorderKit,
// @ts-expect-error mocked zcf. use `stake-bld.contract.test.ts` to test LCA with offer
Far('MockZCF', {}),
timestampHelper,
timer,
timer.getTimerBrand(),
agoricChainInfo,
);

Expand Down
2 changes: 1 addition & 1 deletion packages/orchestration/test/utils/time.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ test('makeTimestampHelper - getCurrentTimestamp', async t => {
const timerBrand = timer.getTimerBrand();
t.is(timer.getCurrentTimestamp().absValue, 0n, 'current time is 0n');

const { getTimeoutTimestampNS } = await makeTimestampHelper(timer);
const { getTimeoutTimestampNS } = makeTimestampHelper(timer, timerBrand);
await null;
t.is(
await getTimeoutTimestampNS(),
Expand Down

0 comments on commit 68c8762

Please sign in to comment.