Skip to content

Commit

Permalink
feat(zoe): first transferable
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Jul 7, 2023
1 parent e95fd7c commit 0208b45
Showing 1 changed file with 113 additions and 0 deletions.
113 changes: 113 additions & 0 deletions packages/zoe/src/contracts/transferable-counter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
import { M } from '@endo/patterns';
import { prepareExo, prepareExoClass } from '@agoric/vat-data';
import { OfferHandlerI } from '../typeGuards.js';

/** @typedef {import('@agoric/vat-data').Baggage} Baggage */

const CounterDetails = harden({
count: M.bigint(),
});

const UseCounterI = M.interface('UseCounter', {
incr: M.call().returns(M.bigint()),
getDetails: M.call().returns(CounterDetails),
makeTransferInvitation: M.call().returns(M.promise()),
});

const ViewCounterI = M.interface('ViewCounter', {
view: M.call().returns(M.bigint()),
});

const TransferProposalShape = harden({
give: {},
want: {},
exit: {
onDemand: {},
},
});

/**
* @param {ZCF} zcf
* @param {{ count: bigint}} privateArgs
* @param {Baggage} instanceBaggage
*/
export const start = async (zcf, privateArgs, instanceBaggage) => {
const { count: startCount = 0n } = privateArgs;
assert.typeof(startCount, 'bigint');

// for use by upgraded versions.
const firstTime = !instanceBaggage.has('count');
if (firstTime) {
instanceBaggage.init('count', startCount);
}

let revokeUseCounter;

const makeUseCounter = prepareExoClass(
instanceBaggage,
'UseCounter',
UseCounterI,
count => {
assert(count === instanceBaggage.get('count'));
return harden({});
},
{
incr() {
const count = instanceBaggage.get('count') + 1n;
instanceBaggage.set(count);
return count;
},
getDetails() {
return harden({
count: instanceBaggage.get('count'),
});
},
makeTransferInvitation() {
const { self } = this;
const invitation = zcf.makeInvitation(
// eslint-disable-next-line no-use-before-define
transferHandler,
'transfer',
self.getDetails(),
TransferProposalShape,
);
revokeUseCounter(self);
return invitation;
},
},
{
getRevoker(revoke) {
revokeUseCounter = revoke;
},
},
);

const viewCounter = prepareExo(instanceBaggage, 'ViewCounter', ViewCounterI, {
view() {
return instanceBaggage.get('count');
},
});

const transferHandler = prepareExo(
instanceBaggage,
'TransferHandler',
OfferHandlerI,
{
/**
* @param {ZCFSeat} seat
*/
handle(seat) {
const count = instanceBaggage.get('count');
// TODO assert(seat.getCustomDetails().count === count);
seat.exit();
return makeUseCounter(count);
},
},
);

return harden({
creatorFacet: makeUseCounter(startCount),
publicFacet: viewCounter,
});
};
harden(start);

0 comments on commit 0208b45

Please sign in to comment.