Skip to content

Commit

Permalink
Merge pull request #8568 from Agoric/ta/privateArgs-types
Browse files Browse the repository at this point in the history
typedefs for Zoe privateArgs
  • Loading branch information
mergify[bot] authored Nov 28, 2023
2 parents 0001c49 + 3e5255d commit 275a1bd
Show file tree
Hide file tree
Showing 8 changed files with 67 additions and 23 deletions.
7 changes: 3 additions & 4 deletions packages/inter-protocol/src/proposals/econ-behaviors.js
Original file line number Diff line number Diff line change
Expand Up @@ -462,9 +462,9 @@ export const startRewardDistributor = async ({
});

/**
* @type {Awaited<
* ReturnType<typeof import('../feeDistributor.js').makeFeeDistributor>
* > & { adminFacet: AdminFacet; instance: Instance }}
* @type {StartedInstanceKit<
* typeof import('@agoric/inter-protocol/src/feeDistributor.js').start
* >}
*/
const instanceKit = await E(zoe).startInstance(
feeDistributor,
Expand All @@ -473,7 +473,6 @@ export const startRewardDistributor = async ({
undefined,
'feeDistributor',
);
/** @type {ERef<import('../feeDistributor.js').FeeDestination>} */
await E(instanceKit.creatorFacet).setDestinations({
...(rewardDistributorDepositFacet && {
RewardDistributor: E(
Expand Down
2 changes: 1 addition & 1 deletion packages/inter-protocol/test/psm/test-psm.js
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ async function makePsmDriver(t, customTerms) {
// Each driver needs its own to avoid state pollution between tests
const mockChainStorage = makeMockChainStorageRoot();

/** @type {Awaited<ReturnType<import('../../src/psm/psm.js').start>>} */
/** @type {StartedInstanceKit<import('../../src/psm/psm.js').start>} */
const { creatorFacet, publicFacet } = await E(zoe).startInstance(
psmInstall,
harden({ AUSD: anchor.issuer }),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -276,6 +276,8 @@ export const buildRootObject = async () => {
).getAdminFacet();
const upgradeResult = await E(psmAdminFacet).upgradeContract(bundleId, {
...staticPrivateArgs,
// @ts-expect-error mock
feeMintAccess: undefined,
initialPoserInvitation,
});
// incremented from zero
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -245,6 +245,8 @@ export const buildRootObject = async () => {
const arAdminFacet = await E(governorFacets.creatorFacet).getAdminFacet();
const upgradeResult = await E(arAdminFacet).upgradeContract(bundleId, {
...staticPrivateArgs,
// @ts-expect-error mock
feeMintAccess: undefined,
initialPoserInvitation,
});
assert.equal(upgradeResult.incarnationNumber, 1);
Expand Down
2 changes: 1 addition & 1 deletion packages/vats/src/proposals/null-upgrade-zoe-proposal.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { E } from '@endo/far';
/**
* @param {BootstrapPowers & {
* consume: {
* vatAdminSvc: VatAdminSve;
* vatAdminSvc: VatAdminSvc;
* vatStore: MapStore<string, CreateVatResults>;
* };
* }} powers
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/contractFacet/types-ambient.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,4 +244,4 @@ type ContractStartFnResult<PF, CF> = {
creatorInvitation?: Promise<Invitation<R, A>> | undefined;
};
type ContractOf<S> = import('../zoeService/utils').ContractOf<S>;
type AdminFacet = import('../zoeService/utils').AdminFacet;
type AdminFacet = import('../zoeService/utils').AdminFacet<any>;
40 changes: 24 additions & 16 deletions packages/zoe/src/zoeService/utils.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,6 @@ type ContractFacet<T extends {} = {}> = {
readonly [P in keyof T]: T[P] extends Callable ? T[P] : never;
};

export type AdminFacet = {
// Completion, which is currently any
getVatShutdownPromise: () => Promise<any>;
upgradeContract: (
contractBundleId: string,
newPrivateArgs?: any,
) => Promise<VatUpgradeResults>;
restartContract: (newPrivateArgs?: any) => Promise<VatUpgradeResults>;
};

/**
* Installation of a contract, typed by its start function.
*/
Expand All @@ -46,11 +36,29 @@ type ContractStartFunction = (
baggage?: Baggage,
) => ERef<{ creatorFacet?: {}; publicFacet?: {} }>;

export type AdminFacet<SF extends ContractStartFunction> = {
// Completion, which is currently any
getVatShutdownPromise: () => Promise<any>;
upgradeContract: Parameters<SF>[1] extends undefined
? (contractBundleId: string) => Promise<VatUpgradeResults>
: (
contractBundleId: string,
newPrivateArgs: Parameters<SF>[1],
) => Promise<VatUpgradeResults>;
restartContract: Parameters<SF>[1] extends undefined
? () => Promise<VatUpgradeResults>
: (newPrivateArgs: Parameters<SF>[1]) => Promise<VatUpgradeResults>;
};

type StartParams<SF> = SF extends ContractStartFunction
? {
terms: ReturnType<Parameters<SF>[0]['getTerms']>;
privateArgs: Parameters<SF>[1];
}
? Parameters<SF>[1] extends undefined
? {
terms: ReturnType<Parameters<SF>[0]['getTerms']>;
}
: {
terms: ReturnType<Parameters<SF>[0]['getTerms']>;
privateArgs: Parameters<SF>[1];
}
: never;

type StartResult<S> = S extends (...args: any) => Promise<infer U>
Expand All @@ -72,13 +80,13 @@ type StartContractInstance<C> = (
publicFacet: C['publicFacet'];
instance: Instance;
creatorInvitation: C['creatorInvitation'];
adminFacet: AdminFacet;
adminFacet: AdminFacet<any>;
}>;

/** The result of `startInstance` */
export type StartedInstanceKit<SF> = {
instance: Instance<SF>;
adminFacet: AdminFacet;
adminFacet: AdminFacet<SF>;
// theses are empty by default. the return type will override
creatorFacet: {};
publicFacet: {};
Expand Down
33 changes: 33 additions & 0 deletions packages/zoe/src/zoeService/utils.test-d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import type { StartedInstanceKit } from './utils';

const someContractStartFn = (
zcf: ZCF,
privateArgs: { someNumber: number; someString: string },
) => {};

type PsmInstanceKit = StartedInstanceKit<typeof someContractStartFn>;

const psmInstanceKit: PsmInstanceKit = null as any;

// @ts-expect-error missing privateArgs argument
void psmInstanceKit.adminFacet.restartContract();

const partial = {
someNumber: 1,
};
// @ts-expect-error missing member of privateArgs argument
void psmInstanceKit.adminFacet.restartContract(partial);

// valid privateArgs now with 'marshaller'
void psmInstanceKit.adminFacet.restartContract({
...partial,
someString: 'str',
});

// @ts-expect-error missing member of privateArgs argument
void psmInstanceKit.adminFacet.upgradeContract('whatever', partial);
// valid privateArgs now with 'marshaller'
void psmInstanceKit.adminFacet.upgradeContract('whatever', {
...partial,
someString: 'str',
});

0 comments on commit 275a1bd

Please sign in to comment.