From 3f06af2084e28681f40e038590b37eefb1f4c0c2 Mon Sep 17 00:00:00 2001 From: Chris Hibbert Date: Fri, 5 Aug 2022 17:49:45 -0700 Subject: [PATCH] feat: add E(userSeat).wasWantSatisfied() --- packages/zoe/src/zoeService/zoeSeat.js | 9 +++ packages/zoe/test/unitTests/zcf/test-zcf.js | 73 +++++++++++++++++++++ 2 files changed, 82 insertions(+) diff --git a/packages/zoe/src/zoeService/zoeSeat.js b/packages/zoe/src/zoeService/zoeSeat.js index b38e1b244d6..abf3199c6e8 100644 --- a/packages/zoe/src/zoeService/zoeSeat.js +++ b/packages/zoe/src/zoeService/zoeSeat.js @@ -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'; @@ -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 }; diff --git a/packages/zoe/test/unitTests/zcf/test-zcf.js b/packages/zoe/test/unitTests/zcf/test-zcf.js index eecedccef50..35c253a6734 100644 --- a/packages/zoe/test/unitTests/zcf/test-zcf.js +++ b/packages/zoe/test/unitTests/zcf/test-zcf.js @@ -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; +});