Skip to content

Commit

Permalink
feat: add E(userSeat).wasWantSatisfied()
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Aug 6, 2022
1 parent 7f294a6 commit 3f06af2
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 0 deletions.
9 changes: 9 additions & 0 deletions packages/zoe/src/zoeService/zoeSeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { makePromiseKit } from '@endo/promise-kit';
import { makeNotifierKit } from '@agoric/notifier';
import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';
import { AmountMath } from '@agoric/ertp';

import { handlePKitWarning } from '../handleWarning.js';

Expand Down Expand Up @@ -95,6 +96,14 @@ export const makeZoeSeatAdminKit = (

getCurrentAllocationJig: async () => currentAllocation,
getAllocationNotifierJig: async () => notifier,

wasWantSatisfied: async () => {
return E.when(payoutPromiseKit.promise, () =>
Object.keys(proposal.want).every(kwd =>
AmountMath.isGTE(currentAllocation[kwd], proposal.want[kwd]),
),
);
},
});

return { userSeat, zoeSeatAdmin, notifier };
Expand Down
73 changes: 73 additions & 0 deletions packages/zoe/test/unitTests/zcf/test-zcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -1413,3 +1413,76 @@ test(`zcf.setOfferFilter - legal lists`, async t => {
t.is(await zcf.setOfferFilter(['fooOffer', 'barOffer']), undefined);
t.is(await zcf.setOfferFilter(['fooOffer: ', 'bar Offer']), undefined);
});

test('wasWantSatisfied: no', async t => {
const { zcf } = await setupZCFTest();
const doubloonMint = await zcf.makeZCFMint('Doubloons');
const yenMint = await zcf.makeZCFMint('Yen');
const { brand: doubloonBrand } = doubloonMint.getIssuerRecord();
const { brand: yenBrand } = yenMint.getIssuerRecord();
const yenAmount = AmountMath.make(yenBrand, 100n);
const proposal = harden({
give: { DownPayment: yenAmount },
want: { Bonus: AmountMath.make(doubloonBrand, 1_000_000n) },
});

const { zcfSeat: mintSeat, userSeat: payoutSeat } = zcf.makeEmptySeatKit();
yenMint.mintGains(harden({ Cost: yenAmount }), mintSeat);
mintSeat.exit();
const payout = await E(payoutSeat).getPayout('Cost');
const payment = { DownPayment: payout };

const { zcfSeat, userSeat } = await makeOffer(
zcf.getZoeService(),
zcf,
proposal,
payment,
);

await zcfSeat.exit();
t.false(await E(userSeat).wasWantSatisfied());
});

test('wasWantSatisfied: yes', async t => {
const { zcf } = await setupZCFTest();
const doubloonMint = await zcf.makeZCFMint('Doubloons');
const { brand: doubloonBrand } = doubloonMint.getIssuerRecord();
const doubloonAmount = AmountMath.make(doubloonBrand, 100n);

const proposal = harden({
want: { Bonus: doubloonAmount },
});
const { zcfSeat, userSeat } = await makeOffer(
zcf.getZoeService(),
zcf,
proposal,
);
doubloonMint.mintGains(harden({ Bonus: doubloonAmount }), zcfSeat);

await zcfSeat.exit();
t.true(await E(userSeat).wasWantSatisfied());
});

test('wasWantSatisfied as promise', async t => {
const { zcf } = await setupZCFTest();
const doubloonMint = await zcf.makeZCFMint('Doubloons');
const { brand: doubloonBrand } = doubloonMint.getIssuerRecord();
const doubloonAmount = AmountMath.make(doubloonBrand, 100n);

const proposal = harden({
want: { Bonus: doubloonAmount },
});
const { zcfSeat, userSeat } = await makeOffer(
zcf.getZoeService(),
zcf,
proposal,
);

const outcome = E.when(E(userSeat).wasWantSatisfied(), result =>
t.true(result),
);
doubloonMint.mintGains(harden({ Bonus: doubloonAmount }), zcfSeat);

await zcfSeat.exit();
await outcome;
});

0 comments on commit 3f06af2

Please sign in to comment.