-
Notifications
You must be signed in to change notification settings - Fork 214
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
113 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); |