Skip to content

Commit

Permalink
Merge pull request #1607 from Agoric/addShutdown
Browse files Browse the repository at this point in the history
fix: add shutdown() to atomicSwap, coveredCall, and sellItems.
  • Loading branch information
Chris-Hibbert authored Aug 25, 2020
2 parents 2d36848 + cdc09a1 commit 773d1ce
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 16 deletions.
3 changes: 2 additions & 1 deletion packages/zoe/src/contractFacet/seat.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,8 @@ export const makeZcfSeatAdminKit = (
let currentAllocation = harden(seatData.initialAllocation);
let exited = false; // seat is "active"

const assertExitedFalse = () => assert(!exited, `seat has been exited`);
const assertExitedFalse = () =>
assert(!exited, details`seat has been exited`);

/** @type {ZCFSeatAdmin} */
const zcfSeatAdmin = harden({
Expand Down
7 changes: 5 additions & 2 deletions packages/zoe/src/contracts/atomicSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,11 @@ const start = zcf => {
const { want, give } = firstSeat.getProposal();

/** @type {OfferHandler} */
const matchingSeatOfferHandler = matchingSeat =>
swap(zcf, firstSeat, matchingSeat);
const matchingSeatOfferHandler = matchingSeat => {
const swapResult = swap(zcf, firstSeat, matchingSeat);
zcf.shutdown();
return swapResult;
};

const matchingSeatInvitation = zcf.makeInvitation(
matchingSeatOfferHandler,
Expand Down
4 changes: 3 additions & 1 deletion packages/zoe/src/contracts/coveredCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,9 @@ const start = zcf => {
const rejectMsg = `The covered call option is expired.`;

const exerciseOption = exerciserSeat => {
return swap(zcf, sellerSeat, exerciserSeat, rejectMsg);
const swapResult = swap(zcf, sellerSeat, exerciserSeat, rejectMsg);
zcf.shutdown();
return swapResult;
};

const exerciseOptionExpected = harden({
Expand Down
27 changes: 18 additions & 9 deletions packages/zoe/src/contracts/sellItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,11 @@ import '../../exported';
* available to sell, and the money should be pricePerItem times the number of
* items requested.
*
* When all the items have been sold, the contract will terminate, triggering
* the creator's payout. If the creator has an onDemand exit clause, they can
* exit early to collect their winnings. The remaining items will still be
* available for sale, but the creator won't be able to collect later earnings.
*
* @type {ContractStartFn}
*/
const start = zcf => {
Expand All @@ -46,6 +51,15 @@ const start = zcf => {
return defaultAcceptanceMsg;
};

/** @type {SellItemsPublicFacet} */
const publicFacet = {
getAvailableItems: () => {
assert(sellerSeat && !sellerSeat.hasExited(), `no items are for sale`);
return sellerSeat.getAmountAllocated('Items');
},
getItemsIssuer: () => issuers.Items,
};

const buy = buyerSeat => {
const currentItemsForSale = sellerSeat.getAmountAllocated('Items');
const providedMoney = buyerSeat.getAmountAllocated('Money');
Expand Down Expand Up @@ -83,16 +97,11 @@ const start = zcf => {

// The buyer's offer has been processed.
buyerSeat.exit();
return defaultAcceptanceMsg;
};

/** @type {SellItemsPublicFacet} */
const publicFacet = {
getAvailableItems: () => {
assert(sellerSeat && !sellerSeat.hasExited(), `no items are for sale`);
return sellerSeat.getAmountAllocated('Items');
},
getItemsIssuer: () => issuers.Items,
if (itemsMath.isEmpty(publicFacet.getAvailableItems())) {
zcf.shutdown();
}
return defaultAcceptanceMsg;
};

/** @type {SellItemsCreatorFacet} */
Expand Down
2 changes: 1 addition & 1 deletion packages/zoe/src/zoeService/zoe.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,7 +291,7 @@ function makeZoe(vatAdminSvc, zcfBundleName = undefined) {
},
shutdown: () => {
exitAllSeats();
adminNode.terminate();
E(adminNode).terminate();
},
makeZoeMint,
};
Expand Down
2 changes: 0 additions & 2 deletions packages/zoe/test/unitTests/contracts/test-sellTickets.js
Original file line number Diff line number Diff line change
Expand Up @@ -507,8 +507,6 @@ test(`mint and sell opera tickets`, async t => {

const operaPurse = moolaIssuer.makeEmptyPurse();

await E(sellItemsCreatorSeat).tryExit();

const moneyPayment = await E(sellItemsCreatorSeat).getPayout('Money');
await E(operaPurse).deposit(moneyPayment);
const currentPurseBalance = await E(operaPurse).getCurrentAmount();
Expand Down

0 comments on commit 773d1ce

Please sign in to comment.