Skip to content

Commit

Permalink
fix: Add a seat in mintGains() if none is provided
Browse files Browse the repository at this point in the history
fixes: #1696
  • Loading branch information
Chris-Hibbert committed Sep 15, 2020
1 parent febe37a commit 0efa57f
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 51 deletions.
96 changes: 48 additions & 48 deletions packages/zoe/src/contractFacet/contractFacet.js
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,50 @@ export function buildRootObject(_powers, _params, testJigSetter = undefined) {
E(zoeInstanceAdmin).replaceAllocations(seatHandleAllocations);
};

const makeEmptySeatKit = (exit = undefined) => {
const initialAllocation = harden({});
const proposal = cleanProposal(getAmountMath, harden({ exit }));
const { notifier, updater } = makeNotifierKit();
/** @type {PromiseRecord<ZoeSeatAdmin>} */
const zoeSeatAdminPromiseKit = makePromiseKit();
// Don't trigger Node.js's UnhandledPromiseRejectionWarning
zoeSeatAdminPromiseKit.promise.catch(_ => {});
const userSeatPromiseKit = makePromiseKit();
// Don't trigger Node.js's UnhandledPromiseRejectionWarning
userSeatPromiseKit.promise.catch(_ => {});
const seatHandle = makeHandle('SeatHandle');

const seatData = harden({
proposal,
initialAllocation,
notifier,
});
const { zcfSeat, zcfSeatAdmin } = makeZcfSeatAdminKit(
allSeatStagings,
zoeSeatAdminPromiseKit.promise,
seatData,
getAmountMath,
);
zcfSeatToZCFSeatAdmin.init(zcfSeat, zcfSeatAdmin);
zcfSeatToSeatHandle.init(zcfSeat, seatHandle);

const exitObj = makeExitObj(
seatData.proposal,
zoeSeatAdminPromiseKit.promise,
zcfSeatAdmin,
);

E(zoeInstanceAdmin)
.makeNoEscrowSeat(initialAllocation, proposal, exitObj, seatHandle)
.then(({ zoeSeatAdmin, notifier: zoeNotifier, userSeat }) => {
updateFromNotifier(updater, zoeNotifier);
zoeSeatAdminPromiseKit.resolve(zoeSeatAdmin);
userSeatPromiseKit.resolve(userSeat);
});

return { zcfSeat, userSeat: userSeatPromiseKit.promise };
};

/** @type {MakeZCFMint} */
const makeZCFMint = async (keyword, amountMathKind = MathKind.NAT) => {
assert(
Expand Down Expand Up @@ -172,11 +216,9 @@ export function buildRootObject(_powers, _params, testJigSetter = undefined) {
return mintyIssuerRecord;
},
mintGains: (gains, zcfSeat = undefined) => {
// TODO unimplemented
assert(
zcfSeat !== undefined,
details`On demand seat creation not yet implemented`,
);
if (zcfSeat === undefined) {
zcfSeat = makeEmptySeatKit().zcfSeat;
}
let totalToMint = mintyAmountMath.getEmpty();
const oldAllocation = zcfSeat.getCurrentAllocation();
const updates = objectMap(gains, ([seatKeyword, amountToAdd]) => {
Expand Down Expand Up @@ -305,49 +347,7 @@ export function buildRootObject(_powers, _params, testJigSetter = undefined) {
// Shutdown the entire vat and give payouts
shutdown: () => E(zoeInstanceAdmin).shutdown(),
makeZCFMint,
makeEmptySeatKit: (exit = undefined) => {
const initialAllocation = harden({});
const proposal = cleanProposal(getAmountMath, harden({ exit }));
const { notifier, updater } = makeNotifierKit();
/** @type {PromiseRecord<ZoeSeatAdmin>} */
const zoeSeatAdminPromiseKit = makePromiseKit();
// Don't trigger Node.js's UnhandledPromiseRejectionWarning
zoeSeatAdminPromiseKit.promise.catch(_ => {});
const userSeatPromiseKit = makePromiseKit();
// Don't trigger Node.js's UnhandledPromiseRejectionWarning
userSeatPromiseKit.promise.catch(_ => {});
const seatHandle = makeHandle('SeatHandle');

const seatData = harden({
proposal,
initialAllocation,
notifier,
});
const { zcfSeat, zcfSeatAdmin } = makeZcfSeatAdminKit(
allSeatStagings,
zoeSeatAdminPromiseKit.promise,
seatData,
getAmountMath,
);
zcfSeatToZCFSeatAdmin.init(zcfSeat, zcfSeatAdmin);
zcfSeatToSeatHandle.init(zcfSeat, seatHandle);

const exitObj = makeExitObj(
seatData.proposal,
zoeSeatAdminPromiseKit.promise,
zcfSeatAdmin,
);

E(zoeInstanceAdmin)
.makeNoEscrowSeat(initialAllocation, proposal, exitObj, seatHandle)
.then(({ zoeSeatAdmin, notifier: zoeNotifier, userSeat }) => {
updateFromNotifier(updater, zoeNotifier);
zoeSeatAdminPromiseKit.resolve(zoeSeatAdmin);
userSeatPromiseKit.resolve(userSeat);
});

return { zcfSeat, userSeat: userSeatPromiseKit.promise };
},
makeEmptySeatKit,

// The methods below are pure and have no side-effects //
getZoeService: () => zoeService,
Expand Down
13 changes: 10 additions & 3 deletions packages/zoe/test/unitTests/zcf/test-zcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -428,16 +428,23 @@ test(`zcf.makeZCFMint - SET`, async t => {
test(`zcf.makeZCFMint - mintGains - no args`, async t => {
const { zcf } = await setupZCFTest();
const zcfMint = await zcf.makeZCFMint('A', MathKind.SET);
// TODO: create seat if one is not provided
// https://github.com/Agoric/agoric-sdk/issues/1696
// TODO: improve messages
// https://github.com/Agoric/agoric-sdk/issues/1708
// @ts-ignore
t.throws(() => zcfMint.mintGains(), {
message: 'On demand seat creation not yet implemented',
message: 'Cannot convert undefined or null to object',
});
});

test(`zcf.makeZCFMint - mintGains - no seat`, async t => {
const { zcf } = await setupZCFTest();
const zcfMint = await zcf.makeZCFMint('A', MathKind.NAT);
const { amountMath, brand } = zcfMint.getIssuerRecord();
const zcfSeat = zcfMint.mintGains({ A: amountMath.make(4) });
t.truthy(zcfSeat);
t.deepEqual(zcfSeat.getAmountAllocated('A', brand), amountMath.make(4));
});

test(`zcf.makeZCFMint - mintGains - no gains`, async t => {
const { zcf } = await setupZCFTest();
const zcfMint = await zcf.makeZCFMint('A', MathKind.SET);
Expand Down

0 comments on commit 0efa57f

Please sign in to comment.