Skip to content

Commit

Permalink
fix: update checkIfProposal and rejectIfNotProposal
Browse files Browse the repository at this point in the history
They should use the structure as the proposal.
  • Loading branch information
Chris-Hibbert committed Apr 7, 2020
1 parent 80f866c commit 7cdf09d
Show file tree
Hide file tree
Showing 7 changed files with 128 additions and 44 deletions.
30 changes: 14 additions & 16 deletions packages/zoe/src/contractSupport/zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,26 @@ export const makeZoeHelpers = zoe => {

// Compare the keys of actual with expected keys and reject offer if
// not sameStructure. If expectedKeys is undefined, no comparison occurs.
const rejectIf = (
const rejectKeysIf = (
inviteHandle,
actual,
expectedKeys,
expected,
msg = defaultRejectMsg,
// eslint-disable-next-line consistent-return
) => {
if (expectedKeys !== undefined) {
const expected = [...expectedKeys]; // in case hardened
expected.sort();
if (!sameStructure(getKeysSorted(actual), harden(expected))) {
if (expected !== undefined) {
if (!sameStructure(getKeysSorted(actual), getKeysSorted(expected))) {
return rejectOffer(inviteHandle, msg);
}
}
};
// Compare actual to expected keys. If expectedKeys is
// Compare actual keys to expected keys. If expectedKeys is
// undefined, return true trivially.
const check = (actual, expectedKeys) => {
if (expectedKeys === undefined) {
const checkKeys = (actual, expected) => {
if (expected === undefined) {
return true;
}
return sameStructure(getKeys(actual), expectedKeys);
return sameStructure(getKeysSorted(actual), getKeysSorted(expected));
};

const helpers = harden({
Expand All @@ -56,19 +54,19 @@ export const makeZoeHelpers = zoe => {
},
rejectIfNotProposal: (inviteHandle, expected) => {
const { proposal: actual } = zoe.getOffer(inviteHandle);
rejectIf(inviteHandle, actual.give, expected.give);
rejectIf(inviteHandle, actual.want, expected.want);
rejectIf(inviteHandle, actual.exit, expected.exit);
rejectKeysIf(inviteHandle, actual.give, expected.give);
rejectKeysIf(inviteHandle, actual.want, expected.want);
rejectKeysIf(inviteHandle, actual.exit, expected.exit);
},
checkIfProposal: (inviteHandle, expected) => {
const { proposal: actual } = zoe.getOffer(inviteHandle);
return (
// Check that the "give" keys match expected keys.
check(actual.give, expected.give) &&
checkKeys(actual.give, expected.give) &&
// Check that the "want" keys match expected keys.
check(actual.want, expected.want) &&
checkKeys(actual.want, expected.want) &&
// Check that the "exit" key (i.e. "onDemand") matches the expected key.
check(actual.exit, expected.exit)
checkKeys(actual.exit, expected.exit)
);
},
getActiveOffers: handles =>
Expand Down
5 changes: 4 additions & 1 deletion packages/zoe/src/contracts/atomicSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,10 @@ export const makeContract = harden(zoe => {
const makeFirstOfferInvite = () => {
const seat = harden({
makeFirstOffer: () => {
const expected = harden({ give: ['Asset'], want: ['Price'] });
const expected = harden({
give: { Asset: null },
want: { Price: null },
});
rejectIfNotProposal(inviteHandle, expected);
return makeMatchingInvite(inviteHandle);
},
Expand Down
16 changes: 8 additions & 8 deletions packages/zoe/src/contracts/autoswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,12 +47,12 @@ export const makeContract = harden(zoe => {
swap: () => {
const { proposal } = zoe.getOffer(inviteHandle);
const giveTokenA = harden({
give: ['TokenA'],
want: ['TokenB'],
give: { TokenA: null },
want: { TokenB: null },
});
const giveTokenB = harden({
give: ['TokenB'],
want: ['TokenA'],
give: { TokenB: null },
want: { TokenA: null },
});
let giveKeyword;
let wantKeyword;
Expand Down Expand Up @@ -115,8 +115,8 @@ export const makeContract = harden(zoe => {
},
addLiquidity: () => {
const expected = harden({
give: ['TokenA', 'TokenB'],
want: ['Liquidity'],
give: { TokenA: null, TokenB: null },
want: { Liquidity: null },
});
rejectIfNotProposal(inviteHandle, expected);

Expand Down Expand Up @@ -188,8 +188,8 @@ export const makeContract = harden(zoe => {
},
removeLiquidity: () => {
const expected = harden({
want: ['TokenA', 'TokenB'],
give: ['Liquidity'],
give: { Liquidity: null },
want: { TokenA: null, TokenB: null },
});
rejectIfNotProposal(inviteHandle, expected);

Expand Down
10 changes: 5 additions & 5 deletions packages/zoe/src/contracts/coveredCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ export const makeContract = harden(zoe => {
const seat = harden({
exercise: () => {
const expected = harden({
give: ['StrikePrice'],
want: ['UnderlyingAsset'],
give: { StrikePrice: null },
want: { UnderlyingAsset: null },
});
rejectIfNotProposal(inviteHandle, expected);
const rejectMsg = `The covered call option is expired.`;
Expand All @@ -54,9 +54,9 @@ export const makeContract = harden(zoe => {
const seat = harden({
makeCallOption: () => {
const expected = harden({
give: ['UnderlyingAsset'],
want: ['StrikePrice'],
exit: ['afterDeadline'],
give: { UnderlyingAsset: null },
want: { StrikePrice: null },
exit: { afterDeadline: null },
});
rejectIfNotProposal(inviteHandle, expected);
return makeCallOptionInvite(inviteHandle);
Expand Down
10 changes: 8 additions & 2 deletions packages/zoe/src/contracts/publicAuction.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,10 @@ export const makeContract = harden(zoe => {
if (allBidHandles.length >= numBidsAllowed) {
throw rejectOffer(inviteHandle, `No further bids allowed.`);
}
const expected = harden({ give: ['Bid'], want: ['Asset'] });
const expected = harden({
give: { Bid: null },
want: { Asset: null },
});
rejectIfNotProposal(inviteHandle, expected);
if (!canTradeWith(sellerInviteHandle, inviteHandle)) {
const rejectMsg = `Bid was under minimum bid or for the wrong assets`;
Expand Down Expand Up @@ -74,7 +77,10 @@ export const makeContract = harden(zoe => {
if (auctionedAssets) {
throw rejectOffer(inviteHandle, `assets already present`);
}
const expected = harden({ give: ['Asset'], want: ['Bid'] });
const expected = harden({
give: { Asset: null },
want: { Bid: null },
});
rejectIfNotProposal(inviteHandle, expected);
// Save the valid offer
sellerInviteHandle = inviteHandle;
Expand Down
8 changes: 4 additions & 4 deletions packages/zoe/src/contracts/simpleExchange.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,12 @@ export const makeContract = harden(zoe => {
const seat = harden({
addOrder: () => {
const buyAssetForPrice = harden({
give: [PRICE],
want: [ASSET],
give: { Price: null },
want: { Asset: null },
});
const sellAssetForPrice = harden({
give: [ASSET],
want: [PRICE],
give: { Asset: null },
want: { Price: null },
});
if (checkIfProposal(inviteHandle, sellAssetForPrice)) {
// Save the valid offer and try to match
Expand Down
93 changes: 85 additions & 8 deletions packages/zoe/test/unitTests/contracts/helpers/test-zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -120,14 +120,20 @@ test('ZoeHelpers rejectIfNotProposal', t => {
t.doesNotThrow(() =>
rejectIfNotProposal(
offerHandles[0],
harden({ want: ['Asset'], give: ['Price'] }),
harden({
want: { Asset: null },
give: { Price: null },
}),
),
);
t.throws(
() =>
rejectIfNotProposal(
offerHandles[1],
harden({ want: ['Assets'], give: ['Price'] }),
harden({
want: { Assets: null },
give: { Price: null },
}),
),
/The offer was invalid. Please check your refund./,
`had the wrong wants`,
Expand All @@ -136,7 +142,10 @@ test('ZoeHelpers rejectIfNotProposal', t => {
() =>
rejectIfNotProposal(
offerHandles[2],
harden({ want: ['Asset'], give: ['Price2'] }),
harden({
want: { Asset: null },
give: { Price2: null },
}),
),
/The offer was invalid. Please check your refund./,
`had the wrong offer`,
Expand All @@ -145,7 +154,11 @@ test('ZoeHelpers rejectIfNotProposal', t => {
() =>
rejectIfNotProposal(
offerHandles[3],
harden({ want: ['Asset'], give: ['Price'], exit: ['Waived'] }),
harden({
want: { Asset: null },
give: { Price: null },
exit: { Waived: null },
}),
),
/The offer was invalid. Please check your refund./,
`had the wrong exit rule`,
Expand All @@ -161,7 +174,11 @@ test('ZoeHelpers rejectIfNotProposal', t => {
() =>
rejectIfNotProposal(
offerHandles[4],
harden({ want: ['Asset'], give: ['Price'], exit: ['waived'] }),
harden({
want: { Asset: null },
give: { Price: null },
exit: { waived: null },
}),
),
/The offer was invalid. Please check your refund./,
`had the wrong exit rule`,
Expand All @@ -170,7 +187,11 @@ test('ZoeHelpers rejectIfNotProposal', t => {
() =>
rejectIfNotProposal(
offerHandles[5],
harden({ want: ['Asset'], give: ['Price'], exit: ['waived'] }),
harden({
want: { Asset: null },
give: { Price: null },
exit: { waived: null },
}),
),
/The offer was invalid. Please check your refund./,
`had the wrong want`,
Expand Down Expand Up @@ -219,14 +240,21 @@ test('ZoeHelpers checkIfProposal', t => {
t.ok(
checkIfProposal(
harden({}),
harden({ want: ['Asset'], give: ['Price'], exit: ['onDemand'] }),
harden({
want: { Asset: null },
give: { Price: null },
exit: { onDemand: null },
}),
),
`want, give, and exit match expected`,
);
t.notOk(
checkIfProposal(
harden({}),
harden({ want: ['Asset2'], give: ['Price'] }),
harden({
want: { Asset2: null },
give: { Price: null },
}),
),
`want was not as expected`,
);
Expand All @@ -239,6 +267,55 @@ test('ZoeHelpers checkIfProposal', t => {
}
});

test('ZoeHelpers checkIfProposal multiple keys', t => {
t.plan(2);
const { moolaR, simoleanR, bucksR, moola, simoleans, bucks } = setup();
const mockZoe = harden({
getInstanceRecord: () =>
harden({
issuerKeywordRecord: {
Asset: moolaR.issuer,
Fee: bucksR.issuer,
Price: simoleanR.issuer,
},
}),
getAmountMaths: () => {},
getZoeService: () => {},
getOffer: _handle => {
return harden({
proposal: {
want: { Asset: moola(4), Fee: bucks(1) },
give: { Price: simoleans(16) },
exit: { onDemand: null },
},
});
},
});
const { checkIfProposal } = makeZoeHelpers(mockZoe);
t.ok(
checkIfProposal(
harden({}),
harden({
want: { Asset: null, Fee: null },
give: { Price: null },
exit: { onDemand: null },
}),
),
`want, give, and exit match expected`,
);
t.ok(
checkIfProposal(
harden({}),
harden({
want: { Fee: null, Asset: null },
give: { Price: null },
exit: { onDemand: null },
}),
),
`want (reversed), give, and exit match expected`,
);
});

test('ZoeHelpers getActiveOffers', t => {
t.plan(1);
const { moolaR, simoleanR } = setup();
Expand Down

0 comments on commit 7cdf09d

Please sign in to comment.