Skip to content

Commit

Permalink
fix: tests bigger than 1
Browse files Browse the repository at this point in the history
  • Loading branch information
erights committed Aug 12, 2022
1 parent 4ca22aa commit 97da179
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 25 deletions.
21 changes: 12 additions & 9 deletions packages/zoe/src/contractFacet/offerSafety.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,19 @@ const { details: X } = assert;
const { entries } = Object;

/**
* Helper to perform satisfiesWant and satisfiesGive. How many times
* Helper to perform numWantsSatisfied and satisfiesGive. How many times
* does the `allocation` satisfy the `giveOrWant`?
*
* To prepare for multiples, satisfiesWant and satisfiesGive return 0 or 1.
* isOfferSafe will still be boolean. When we have Multiples, satisfiesWant and
* To prepare for multiples, numWantsSatisfied and satisfiesGive return 0 or 1.
* isOfferSafe will still be boolean. When we have Multiples, numWantsSatisfied and
* satisfiesGive will tell how many times the offer was matched.
*
* @param {AmountKeywordRecord} giveOrWant
* @param {AmountKeywordRecord} allocation
* @returns {number} If the giveOrWant is empty, then any allocation satisfies
* it an `Infinity` number of times.
*/
const satisfiesInternal = (giveOrWant = {}, allocation) => {
const numSatisfied = (giveOrWant = {}, allocation) => {
let multiples = Infinity;
for (const [keyword, requiredAmount] of entries(giveOrWant)) {
if (allocation[keyword] === undefined) {
Expand Down Expand Up @@ -64,11 +64,12 @@ const satisfiesInternal = (giveOrWant = {}, allocation) => {
* @param {AmountKeywordRecord} allocation - a record with keywords
* as keys and amounts as values. These amounts are the reallocation
* to be given to a user.
* @returns {number}
* @returns {number} If the want is empty, then any allocation satisfies
* it an `Infinity` number of times.
*/
export const satisfiesWant = (proposal, allocation) =>
satisfiesInternal(proposal.want, allocation);
harden(satisfiesWant);
export const numWantsSatisfied = (proposal, allocation) =>
numSatisfied(proposal.want, allocation);
harden(numWantsSatisfied);

/**
* For this allocation to count as a full refund, the allocated
Expand All @@ -84,6 +85,8 @@ harden(satisfiesWant);
* @param {AmountKeywordRecord} allocation - a record with keywords
* as keys and amounts as values. These amounts are the reallocation
* to be given to a user.
* @returns {number} If the give is empty, then any allocation satisfies
* it an `Infinity` number of times.
*/
// Commented out because not currently used
// const satisfiesGive = (proposal, allocation) =>
Expand All @@ -109,7 +112,7 @@ harden(satisfiesWant);
export const isOfferSafe = (proposal, allocation) => {
const { give, want, multiples } = proposal;
const howMany =
satisfiesInternal(give, allocation) + satisfiesInternal(want, allocation);
numSatisfied(give, allocation) + numSatisfied(want, allocation);
return howMany >= multiples;
};
harden(isOfferSafe);
4 changes: 2 additions & 2 deletions packages/zoe/src/contractSupport/zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { keyEQ, fit } from '@agoric/store';
import { E } from '@endo/eventual-send';
import { makePromiseKit } from '@endo/promise-kit';
import { AssetKind } from '@agoric/ertp';
import { satisfiesWant } from '../contractFacet/offerSafety.js';
import { numWantsSatisfied } from '../contractFacet/offerSafety.js';

export const defaultAcceptanceMsg = `The offer has been accepted. Once the contract has been completed, please check your payout`;

Expand Down Expand Up @@ -55,7 +55,7 @@ export const satisfies = (zcf, seat, update) => {
const currentAllocation = seat.getCurrentAllocation();
const newAllocation = { ...currentAllocation, ...update };
const proposal = seat.getProposal();
return satisfiesWant(proposal, newAllocation);
return numWantsSatisfied(proposal, newAllocation);
};

/** @type {Swap} */
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/zoeService/zoeSeat.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { E } from '@endo/eventual-send';
import { Far } from '@endo/marshal';

import { handlePKitWarning } from '../handleWarning.js';
import { satisfiesWant } from '../contractFacet/offerSafety.js';
import { numWantsSatisfied as numWantsSatisfied } from '../contractFacet/offerSafety.js';

import '../types.js';
import '../internal-types.js';
Expand Down Expand Up @@ -99,7 +99,7 @@ export const makeZoeSeatAdminKit = (

numWantsSatisfied: async () => {
return E.when(payoutPromiseKit.promise, () =>
satisfiesWant(proposal, currentAllocation),
numWantsSatisfied(proposal, currentAllocation),
);
},
});
Expand Down
36 changes: 25 additions & 11 deletions packages/zoe/test/unitTests/test-offerSafety.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import { test } from '@agoric/zoe/tools/prepare-test-env-ava.js';

import {
isOfferSafe,
satisfiesWant,
numWantsSatisfied,
} from '../../src/contractFacet/offerSafety.js';
import { setup } from './setupBasicMints.js';

Expand Down Expand Up @@ -38,7 +38,21 @@ test('isOfferSafe - more than want, more than give', t => {
const amounts = harden({ A: moola(10n), B: simoleans(7n), C: bucks(8n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 1);
t.is(numWantsSatisfied(proposal, amounts), 1);
});

test('isOfferSafe - much more than want, much more than give', t => {
const { moola, simoleans, bucks } = setup();
const proposal = harden({
give: { A: moola(8n) },
want: { B: simoleans(6n), C: bucks(7n) },
multiples: 1n,
exit: { waived: null },
});
const amounts = harden({ A: moola(100n), B: simoleans(70n), C: bucks(80n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(numWantsSatisfied(proposal, amounts), 11);
});

// more than want, less than give -> true
Expand All @@ -53,7 +67,7 @@ test('isOfferSafe - more than want, less than give', t => {
const amounts = harden({ A: moola(1n), B: simoleans(7n), C: bucks(8n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 1);
t.is(numWantsSatisfied(proposal, amounts), 1);
});

// more than want, equal to give -> true
Expand All @@ -68,7 +82,7 @@ test('isOfferSafe - more than want, equal to give', t => {
const amounts = harden({ A: moola(9n), B: simoleans(6n), C: bucks(7n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 1);
t.is(numWantsSatisfied(proposal, amounts), 1);
});

// less than want, more than give -> true
Expand All @@ -83,7 +97,7 @@ test('isOfferSafe - less than want, more than give', t => {
const amounts = harden({ A: moola(7n), B: simoleans(9n), C: bucks(19n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 0);
t.is(numWantsSatisfied(proposal, amounts), 0);
});

// less than want, less than give -> false
Expand All @@ -98,7 +112,7 @@ test('isOfferSafe - less than want, less than give', t => {
const amounts = harden({ A: moola(7n), B: simoleans(5n), C: bucks(6n) });

t.falsy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 0);
t.is(numWantsSatisfied(proposal, amounts), 0);
});

// less than want, equal to give -> true
Expand All @@ -113,7 +127,7 @@ test('isOfferSafe - less than want, equal to give', t => {
const amounts = harden({ A: moola(1n), B: simoleans(5n), C: bucks(7n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 0);
t.is(numWantsSatisfied(proposal, amounts), 0);
});

// equal to want, more than give -> true
Expand All @@ -128,7 +142,7 @@ test('isOfferSafe - equal to want, more than give', t => {
const amounts = harden({ A: moola(2n), B: simoleans(6n), C: bucks(8n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 1);
t.is(numWantsSatisfied(proposal, amounts), 1);
});

// equal to want, less than give -> true
Expand All @@ -143,7 +157,7 @@ test('isOfferSafe - equal to want, less than give', t => {
const amounts = harden({ A: moola(0n), B: simoleans(6n), C: bucks(0n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 1);
t.is(numWantsSatisfied(proposal, amounts), 1);
});

// equal to want, equal to give -> true
Expand All @@ -158,7 +172,7 @@ test('isOfferSafe - equal to want, equal to give', t => {
const amounts = harden({ A: moola(1n), B: simoleans(6n), C: bucks(7n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 1);
t.is(numWantsSatisfied(proposal, amounts), 1);
});

test('isOfferSafe - empty proposal', t => {
Expand All @@ -172,5 +186,5 @@ test('isOfferSafe - empty proposal', t => {
const amounts = harden({ A: moola(1n), B: simoleans(6n), C: bucks(7n) });

t.truthy(isOfferSafe(proposal, amounts));
t.is(satisfiesWant(proposal, amounts), 1);
t.is(numWantsSatisfied(proposal, amounts), Infinity);
});
2 changes: 1 addition & 1 deletion packages/zoe/test/unitTests/zcf/test-zcf.js
Original file line number Diff line number Diff line change
Expand Up @@ -1445,7 +1445,7 @@ test('numWantsSatisfied: no', async t => {
await zcfSeat.exit();
t.is(await E(userSeat).numWantsSatisfied(), 0);
});

test('numWantsSatisfied: yes', async t => {
const { zcf } = await setupZCFTest();
const doubloonMint = await zcf.makeZCFMint('Doubloons');
Expand Down

0 comments on commit 97da179

Please sign in to comment.