Skip to content

Commit

Permalink
fixup! review suggestions
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Feb 1, 2024
1 parent d879118 commit bd36e37
Show file tree
Hide file tree
Showing 5 changed files with 69 additions and 70 deletions.
42 changes: 20 additions & 22 deletions packages/zoe/src/contractSupport/prepare-ownable.js
Original file line number Diff line number Diff line change
Expand Up @@ -10,42 +10,42 @@ const TransferProposalShape = M.splitRecord({
},
});

/**
* @template {any} [U=any]
* @typedef {object} OwnableOptions
* @property {string} [uInterfaceName]
* The `interfaceName` of the underlying interface guard.
* Defaults to the uKindName
*/

/**
* @template {any} [U=any]
* @param {import('@agoric/base-zone').Zone} zone
* @param {MakeInvitation} makeInvitation
* @param {string} uKindName
* @param {string} uInterfaceName
* @param {(string|symbol)[]} uMethodNames
* @param {OwnableOptions} [options]
* @returns {(underlying: U) => U}
*/
export const prepareOwnable = (
zone,
makeInvitation,
uKindName,
uInterfaceName,
uMethodNames,
options = {},
) => {
const makeRevocableKit = prepareRevocableKit(
zone,
uKindName,
const { uInterfaceName = uKindName } = options;
const makeRevocableKit = prepareRevocableKit(zone, uKindName, uMethodNames, {
uInterfaceName,
uMethodNames,
{
extraMethodGuards: {
makeTransferInvitation: M.call().returns(M.promise()),
},
{
extraMethods: {
makeTransferInvitation() {
const {
state: {
// @ts-expect-error `this` typing
underlying,
},
facets: {
// @ts-expect-error `this` typing
revoker,
},
} = this;
// @ts-expect-error `this` typing
const { underlying } = this.state;
// @ts-expect-error `this` typing
const { revoker } = this.facets;
const customDetails = underlying.getInvitationCustomDetails();
// eslint-disable-next-line no-use-before-define
const transferHandler = makeTransferHandler(underlying);
Expand All @@ -60,7 +60,7 @@ export const prepareOwnable = (
return invitation;
},
},
);
});

const makeTransferHandler = zone.exoClass(
'TransferHandler',
Expand All @@ -70,9 +70,7 @@ export const prepareOwnable = (
}),
{
handle(seat) {
const {
state: { underlying },
} = this;
const { underlying } = this.state;
const { revocable } = makeRevocableKit(underlying);
seat.exit();
return revocable;
Expand Down
52 changes: 32 additions & 20 deletions packages/zoe/src/contractSupport/prepare-revocable.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,27 @@ const { Fail, quote: q } = assert;
* Forwards to the underlying exo object, until revoked
*/

/**
* @template {any} [U=any]
* @typedef {object} RevocableKitOptions
* @property {string} [uInterfaceName]
* The `interfaceName` of the underlying interface guard.
* Defaults to the uKindName
* @property {Record<
* string|symbol,
* import('@endo/patterns').MethodGuard
* >} [extraMethodGuards]
* For guarding the `extraMethods`, if you include them below. These appear
* only on the synthesized interface guard for the revocable caretaker, and
* do not necessarily correspond to any method of the underlying.
* @property {Record<
* string|symbol,
* (...args) => any
* >} [extraMethods]
* Extra methods adding behavior only to the revocable caretaker, and
* do not necessarily correspond to any methods of the underlying.
*/

/**
* Make an exo class kit for wrapping an underlying exo class,
* where the wrapper is a revocable forwarder
Expand All @@ -24,28 +45,23 @@ const { Fail, quote: q } = assert;
* @param {import('@agoric/base-zone').Zone} zone
* @param {string} uKindName
* The `kindName` of the underlying exo class
* @param {string} uInterfaceName
* The `interfaceName` of the underlying interface guard
* @param {(string|symbol)[]} uMethodNames
* The method names of the underlying exo class
* @param {Record<
* string|symbol,
* import('@endo/patterns').MethodGuard
* >} [extraMethodGuards]
* @param {Record<
* string|symbol,
* (...args) => any
* >} [extraMethods]
* The method names of the underlying exo class that should be represented
* by a transparently-forwarding method of the revocable caretaker.
* @param {RevocableKitOptions} [options]
* @returns {(underlying: U) => RevocableKit<U>}
*/
export const prepareRevocableKit = (
zone,
uKindName,
uInterfaceName,
uMethodNames,
extraMethodGuards = undefined,
extraMethods = undefined,
options = {},
) => {
const {
uInterfaceName = uKindName,
extraMethodGuards = undefined,
extraMethods = undefined,
} = options;
const RevocableIKit = harden({
revoker: M.interface(`${uInterfaceName}_revoker`, {
revoke: M.call().returns(M.boolean()),
Expand Down Expand Up @@ -87,12 +103,8 @@ export const prepareRevocableKit = (
{
// Use concise method syntax for exo methods
[name](...args) {
const {
state: {
// @ts-expect-error normal exo-this typing confusion
underlying,
},
} = this;
// @ts-expect-error normal exo-this typing confusion
const { underlying } = this.state;
underlying !== undefined ||
Fail`${q(revocableKindName)} revoked`;
return underlying[name](...args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,9 @@ test('test revoke defineVirtualExoClass', t => {
},
);

const makeRevocableUpCounterKit = prepareRevocableKit(
zone,
'UpCounter',
'UpCounter',
['incr'],
);
const makeRevocableUpCounterKit = prepareRevocableKit(zone, 'UpCounter', [
'incr',
]);

const makeUpCounterKit = x =>
makeRevocableUpCounterKit(makeUnderlyingUpCounter(x));
Expand Down Expand Up @@ -85,20 +82,17 @@ test('test revoke defineVirtualExoClassKit', t => {
const makeRevocableUpCounterKit = prepareRevocableKit(
zone,
'UpCounter',
'UpCounter',
['incr'],
{
selfRevoke: M.call().returns(M.boolean()),
},
{
selfRevoke() {
const {
facets: {
// @ts-expect-error typing this
revoker,
},
} = this;
return revoker.revoke();
extraMethodGuards: {
selfRevoke: M.call().returns(M.boolean()),
},
extraMethods: {
selfRevoke() {
// @ts-expect-error typing this
const { revoker } = this.facets;
return revoker.revoke();
},
},
},
);
Expand Down
11 changes: 3 additions & 8 deletions packages/zoe/test/unitTests/contracts/ownable-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const start = async (zcf, privateArgs, baggage) => {
const makeUnderlyingCounterKit = zone.exoClassKit(
'OwnableCounter',
{
counter: M.interface('Counter', {
counter: M.interface('OwnableCounter', {
incr: M.call().returns(M.bigint()),
// required by makePrepareOwnableClass
getInvitationCustomDetails: M.call().returns(
Expand All @@ -39,19 +39,15 @@ export const start = async (zcf, privateArgs, baggage) => {
return state.count;
},
getInvitationCustomDetails() {
const {
state: { count },
} = this;
const { count } = this.state;
return harden({
count,
});
},
},
viewer: {
view() {
const {
state: { count },
} = this;
const { count } = this.state;
return count;
},
},
Expand All @@ -61,7 +57,6 @@ export const start = async (zcf, privateArgs, baggage) => {
const makeOwnableCounter = prepareOwnable(
zone,
(...args) => zcf.makeInvitation(...args),
'Counter',
'OwnableCounter',
['incr', 'getInvitationCustomDetails'],
);
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/test/unitTests/contracts/test-ownable-counter.js
Original file line number Diff line number Diff line change
Expand Up @@ -67,10 +67,10 @@ test('zoe - ownable-counter contract', async t => {
]);

await t.throwsAsync(() => E(firstCounter).getInvitationCustomDetails(), {
message: '"Counter_caretaker" revoked',
message: '"OwnableCounter_caretaker" revoked',
});
await t.throwsAsync(() => E(firstCounter).incr(), {
message: '"Counter_caretaker" revoked',
message: '"OwnableCounter_caretaker" revoked',
});
t.is(await E(viewCounter).view(), 4n);

Expand Down

0 comments on commit bd36e37

Please sign in to comment.