Skip to content

Commit

Permalink
fix: many small review comments (#1533)
Browse files Browse the repository at this point in the history
  • Loading branch information
erights authored Aug 17, 2020
1 parent 9d8ba97 commit ee8f782
Show file tree
Hide file tree
Showing 20 changed files with 86 additions and 97 deletions.
2 changes: 0 additions & 2 deletions packages/zoe/src/contractFacet/seat.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,12 +56,10 @@ export const makeZcfSeatAdminKit = (
assert.fail(msg);
},
getNotifier: () => {
assertExitedFalse();
return notifier;
},
hasExited: () => exited,
getProposal: () => {
assertExitedFalse();
return proposal;
},
getAmountAllocated: (keyword, brand) => {
Expand Down
3 changes: 1 addition & 2 deletions packages/zoe/src/contractSupport/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,9 @@ export { makeStateMachine } from './stateMachine';

export {
defaultAcceptanceMsg,
defaultRejectMsg,
trade,
swap,
assertProposalKeywords,
assertProposalShape,
assertIssuerKeywords,
satisfies,
assertUsesNatMath,
Expand Down
38 changes: 16 additions & 22 deletions packages/zoe/src/contractSupport/zoeHelpers.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ import { sameStructure } from '@agoric/same-structure';

import { MathKind } from '@agoric/ertp';
import { satisfiesWant } from '../contractFacet/offerSafety';
import { objectMap } from '../objArrayConversion';

import '../../exported';

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

const getKeysSorted = obj =>
Expand Down Expand Up @@ -41,8 +41,8 @@ const checkKeys = (actual, expected) => {
* @returns {FromToAllocations} allocations - new allocations
*
* @typedef FromToAllocations
* @property {AmountKeywordRecord} from
* @property {AmountKeywordRecord} to
* @property {Allocation} from
* @property {Allocation} to
*/
const calcNewAllocations = (
zcf,
Expand All @@ -68,10 +68,12 @@ const calcNewAllocations = (
return amount || amountToAdd;
};

const newFromAllocation = Object.fromEntries(
Object.entries(allocations.from).map(([keyword, allocAmount]) => {
return [keyword, subtract(allocAmount, fromLosses[keyword])];
}),
const newFromAllocation = objectMap(
allocations.from,
([keyword, allocAmount]) => [
keyword,
subtract(allocAmount, fromLosses[keyword]),
],
);

const allToKeywords = [
Expand All @@ -92,14 +94,6 @@ const calcNewAllocations = (
});
};

const mergeAllocations = (currentAllocation, allocation) => {
const newAllocation = {
...currentAllocation,
...allocation,
};
return newAllocation;
};

export const assertIssuerKeywords = (zcf, expected) => {
const { issuers } = zcf.getTerms();
const actual = getKeysSorted(issuers);
Expand Down Expand Up @@ -134,17 +128,17 @@ export const checkIfProposal = (seat, expected) => {
* Check whether an update to currentAllocation satisfies
* proposal.want. Note that this is half of the offer safety
* check; whether the allocation constitutes a refund is not
* checked. Allocation is merged with currentAllocation
* (allocations' values prevailing if the keywords are the same)
* checked. The update is merged with currentAllocation
* (update's values prevailing if the keywords are the same)
* to produce the newAllocation.
* @param {ContractFacet} zcf
* @param {ZCFSeat} seat
* @param {Allocation} allocation
* @param {AmountKeywordRecord} update
* @returns {boolean}
*/
export const satisfies = (zcf, seat, allocation) => {
export const satisfies = (zcf, seat, update) => {
const currentAllocation = seat.getCurrentAllocation();
const newAllocation = mergeAllocations(currentAllocation, allocation);
const newAllocation = { ...currentAllocation, ...update };
const proposal = seat.getProposal();
return satisfiesWant(zcf.getAmountMath, proposal, newAllocation);
};
Expand Down Expand Up @@ -223,7 +217,7 @@ export const trade = (zcf, keepLeft, tryRight) => {
* offer will be rejected with a message (provided by 'keepHandleInactiveMsg').
*
* TODO: If the try offer is no longer active, swap() should terminate with
* a useful error message, like defaultRejectMsg.
* a useful error message.
*
* If the swap fails, no assets are transferred, and the 'try' offer is rejected.
*
Expand Down Expand Up @@ -278,7 +272,7 @@ export const swap = (
* @param {OfferHandler} offerHandler
* @param {ExpectedRecord} expected
*/
export const assertProposalKeywords = (offerHandler, expected) =>
export const assertProposalShape = (offerHandler, expected) =>
/** @param {ZCFSeat} seat */
seat => {
const actual = seat.getProposal();
Expand Down
14 changes: 7 additions & 7 deletions packages/zoe/src/contracts/atomicSwap.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {
assertIssuerKeywords,
swap,
assertProposalKeywords,
assertProposalShape,
} from '../contractSupport';

import '../../exported';
Expand All @@ -28,18 +28,18 @@ const start = zcf => {
const { want, give } = firstSeat.getProposal();

/** @type {OfferHandler} */
const secondSeatOfferHandler = secondSeat =>
swap(zcf, firstSeat, secondSeat);
const matchingSeatOfferHandler = matchingSeat =>
swap(zcf, firstSeat, matchingSeat);

const secondSeatInvitation = zcf.makeInvitation(
secondSeatOfferHandler,
const matchingSeatInvitation = zcf.makeInvitation(
matchingSeatOfferHandler,
'matchOffer',
{
asset: give.Asset,
price: want.Price,
},
);
return secondSeatInvitation;
return matchingSeatInvitation;
};

const firstProposalExpected = harden({
Expand All @@ -48,7 +48,7 @@ const start = zcf => {
});

const creatorInvitation = zcf.makeInvitation(
assertProposalKeywords(makeMatchingInvitation, firstProposalExpected),
assertProposalShape(makeMatchingInvitation, firstProposalExpected),
'firstOffer',
);

Expand Down
8 changes: 4 additions & 4 deletions packages/zoe/src/contracts/autoswap.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import {
getInputPrice,
calcLiqValueToMint,
calcValueToRemove,
assertProposalKeywords,
assertProposalShape,
assertUsesNatMath,
trade,
} from '../contractSupport';
Expand Down Expand Up @@ -199,19 +199,19 @@ const start = async zcf => {

const makeAddLiquidityInvitation = () =>
zcf.makeInvitation(
assertProposalKeywords(addLiquidityHandler, addLiquidityExpected),
assertProposalShape(addLiquidityHandler, addLiquidityExpected),
'autoswap add liquidity',
);

const makeRemoveLiquidityInvitation = () =>
zcf.makeInvitation(
assertProposalKeywords(removeLiquidityHandler, removeLiquidityExpected),
assertProposalShape(removeLiquidityHandler, removeLiquidityExpected),
'autoswap remove liquidity',
);

const makeSwapInvitation = () =>
zcf.makeInvitation(
assertProposalKeywords(swapHandler, swapExpected),
assertProposalShape(swapHandler, swapExpected),
'autoswap swap',
);

Expand Down
21 changes: 11 additions & 10 deletions packages/zoe/src/contracts/coveredCall.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import {
swap,
assertIssuerKeywords,
assertProposalKeywords,
assertProposalShape,
} from '../contractSupport';

import '../../exported';
Expand All @@ -17,22 +17,23 @@ import '../../exported';
*
* In this contract, the expiry date is the deadline when the offer
* escrowing the underlying assets is cancelled. Therefore, the
* proposal for the underlying assets must have an exit record with
* proposal to sell the underlying assets must have an exit record with
* the key "afterDeadline".
*
* The invitation received by the covered call creator is the call
* option. It has this additional information in the invitation's
* value: { expirationDate, timerAuthority, underlyingAsset,
* strikePrice }
* value:
* { expirationDate, timerAuthority, underlyingAsset, strikePrice }
*
* The initial proposal should be:
* {
* give: { UnderlyingAsset: assetAmount }, want: { StrikePrice:
* priceAmount }, exit: { afterDeadline: { deadline: time, timer:
* timer } },
* give: { UnderlyingAsset: assetAmount },
* want: { StrikePrice: priceAmount },
* exit: { afterDeadline: { deadline: time, timer: timer } },
* }
* The result of the initial offer is a seat where the payout will
* eventually resolve to the strikePrice, and the offerResult is an
* eventually resolve either to the strikePrice or to a refund of the
* underlying asset. The offerResult is the call option, which is an
* assayable invitation to buy the underlying asset. Since the
* contract provides assurance that the underlying asset is available
* on the specified terms, the invitation itself can be traded as a
Expand All @@ -57,7 +58,7 @@ const start = zcf => {
});

return zcf.makeInvitation(
assertProposalKeywords(exerciseOption, exerciseOptionExpected),
assertProposalShape(exerciseOption, exerciseOptionExpected),
'exerciseOption',
harden({
expirationDate: exit.afterDeadline.deadline,
Expand All @@ -75,7 +76,7 @@ const start = zcf => {
});

const creatorInvitation = zcf.makeInvitation(
assertProposalKeywords(makeCallOption, makeCallOptionExpected),
assertProposalShape(makeCallOption, makeCallOptionExpected),
'makeCallOption',
);

Expand Down
6 changes: 3 additions & 3 deletions packages/zoe/src/contracts/mintPayments.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ const start = async zcf => {
const zcfMint = await zcf.makeZCFMint('Tokens');
// AWAIT

// Now ZCF has saved the issuer, brand, and local amountMath so that they
// Now that ZCF has saved the issuer, brand, and local amountMath, they
// can be accessed synchronously.
const { amountMath, issuer } = zcfMint.getIssuerRecord();

const mintPayment = (extent = 1000) => seat => {
const mintPayment = extent => seat => {
const amount = amountMath.make(extent);
// Synchronously mint and allocate amount to seat.
zcfMint.mintGains({ Token: amount }, seat);
Expand All @@ -42,7 +42,7 @@ const start = async zcf => {
const creatorFacet = {
// The creator of the instance can send invitations to anyone
// they wish to.
makeInvitation: extent =>
makeInvitation: (extent = 1000) =>
zcf.makeInvitation(mintPayment(extent), 'mint a payment'),
getTokenIssuer: () => issuer,
};
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/contracts/multipoolAutoswap/addLiquidity.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertProposalKeywords } from '../../contractSupport';
import { assertProposalShape } from '../../contractSupport';

import '../../../exported';

Expand All @@ -24,7 +24,7 @@ export const makeMakeAddLiquidityInvitation = (zcf, getPool) => {

const makeAddLiquidityInvitation = () =>
zcf.makeInvitation(
assertProposalKeywords(addLiquidity, addLiquidityExpected),
assertProposalShape(addLiquidity, addLiquidityExpected),
'multipool autoswap add liquidity',
);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertProposalKeywords } from '../../contractSupport';
import { assertProposalShape } from '../../contractSupport';

import '../../../exported';

Expand Down Expand Up @@ -26,7 +26,7 @@ export const makeMakeRemoveLiquidityInvitation = (zcf, getPool) => {

const makeRemoveLiquidityInvitation = () =>
zcf.makeInvitation(
assertProposalKeywords(removeLiquidity, removeLiquidityExpected),
assertProposalShape(removeLiquidity, removeLiquidityExpected),
'autoswap remove liquidity',
);
return makeRemoveLiquidityInvitation;
Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/contracts/multipoolAutoswap/swap.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { assertProposalKeywords, trade } from '../../contractSupport';
import { assertProposalShape, trade } from '../../contractSupport';

import '../../../exported';

Expand Down Expand Up @@ -128,7 +128,7 @@ export const makeMakeSwapInvitation = (

const makeSwapInvitation = () =>
zcf.makeInvitation(
assertProposalKeywords(swap, swapExpected),
assertProposalShape(swap, swapExpected),
'autoswap swap',
);

Expand Down
6 changes: 3 additions & 3 deletions packages/zoe/src/contracts/publicAuction.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import {
satisfies,
secondPriceLogic,
closeAuction,
assertProposalKeywords,
assertProposalShape,
} from '../contractSupport';

import '../../exported';
Expand Down Expand Up @@ -86,7 +86,7 @@ const start = zcf => {

const makeBidInvitation = () =>
zcf.makeInvitation(
assertProposalKeywords(bid, bidExpected),
assertProposalShape(bid, bidExpected),
'bid',
harden({
auctionedAssets,
Expand Down Expand Up @@ -114,7 +114,7 @@ const start = zcf => {
};

const creatorInvitation = zcf.makeInvitation(
assertProposalKeywords(sell, sellExpected),
assertProposalShape(sell, sellExpected),
'sellAssets',
);

Expand Down
4 changes: 2 additions & 2 deletions packages/zoe/src/contracts/sellItems.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import {
assertIssuerKeywords,
trade,
defaultAcceptanceMsg,
assertProposalKeywords,
assertProposalShape,
assertUsesNatMath,
} from '../contractSupport';

Expand Down Expand Up @@ -111,7 +111,7 @@ const start = zcf => {
give: { Money: null },
});
return zcf.makeInvitation(
assertProposalKeywords(buy, buyerExpected),
assertProposalShape(buy, buyerExpected),
'buyer',
);
},
Expand Down
6 changes: 4 additions & 2 deletions packages/zoe/src/internal-types.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@
* @param {InstanceAdmin} instanceAdmin - pass-by-copy data to use to make the seat
* @param {ProposalRecord} proposal
* @param {WeakStore<Brand, ERef<Purse>>} brandToPurse
* @param {{ offerResult?: PromiseRecord<OfferResult>, exitObj?: PromiseRecord<ExitObj>}} [promises={}]
* @param {{ offerResult?: ERef<OfferResult>, exitObj?: ERef<ExitObj>}} [options={}]
* @returns {ZoeSeatAdminKit}
*
* @typedef {Object} ZoeSeatAdmin
Expand Down Expand Up @@ -118,7 +118,9 @@
* customProperties?: {},
* ) => Payment} makeInvitation
* @property {() => void} shutdown
* @property {(issuerP: ERef<Issuer>, keyword: Keyword) => void} saveIssuer
* @property {(issuerP: ERef<Issuer>,
* keyword: Keyword
* ) => Promise<void>} saveIssuer
* @property {MakeZoeMint} makeZoeMint
* @property {MakeOfferlessSeat} makeOfferlessSeat
*/
Expand Down
Loading

0 comments on commit ee8f782

Please sign in to comment.