Skip to content

Commit

Permalink
feat(auctioneer): guard bidSpec
Browse files Browse the repository at this point in the history
  • Loading branch information
turadg committed Mar 16, 2023
1 parent b21ac6f commit 1233e09
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 10 deletions.
24 changes: 24 additions & 0 deletions packages/inter-protocol/src/auction/auctionBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,29 @@ const trace = makeTracer('AucBook', false);
* offerBidScaling: Ratio,
* })} BidSpec
*/
/**
*
* @param {Brand<'nat'>} currencyBrand
* @param {Brand<'nat'>} collateralBrand
*/
export const makeBidSpecShape = (currencyBrand, collateralBrand) => {
const currencyAmountShape = { brand: currencyBrand, value: M.nat() };
const collateralAmountShape = { brand: collateralBrand, value: M.nat() };
return M.splitRecord(
{ want: collateralAmountShape },
{
// xxx should have exactly one of these properties
offerPrice: makeBrandedRatioPattern(
currencyAmountShape,
collateralAmountShape,
),
offerBidScaling: makeBrandedRatioPattern(
currencyAmountShape,
currencyAmountShape,
),
},
);
};

/** @typedef {import('@agoric/vat-data').Baggage} Baggage */

Expand Down Expand Up @@ -145,6 +168,7 @@ export const makeAuctionBook = async (

let curAuctionPrice = zeroRatio;

// FIXME the maker callbacks return a non-durable object
const scaledBidBook = provide(baggage, 'scaledBidBook', () => {
const ratioPattern = makeBrandedRatioPattern(
currencyAmountShape,
Expand Down
24 changes: 14 additions & 10 deletions packages/inter-protocol/src/auction/auctioneer.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import { makeTracer, BASIS_POINTS } from '@agoric/internal';
import { FullProposalShape } from '@agoric/zoe/src/typeGuards.js';
import { appendToStoredArray } from '@agoric/store/src/stores/store-utils.js';
import { mustMatch } from '@agoric/store';
import { makeAuctionBook } from './auctionBook.js';
import { makeAuctionBook, makeBidSpecShape } from './auctionBook.js';
import { AuctionState } from './util.js';
import { makeScheduler } from './scheduler.js';
import { auctioneerParamTypes } from './params.js';
Expand Down Expand Up @@ -268,31 +268,35 @@ export const start = async (zcf, privateArgs, baggage) => {
M.splitRecord({ give: { Collateral: AmountShape } }),
);

const bidProposalShape = M.splitRecord(
{
give: { Currency: { brand: brands.Currency, value: M.nat() } },
},
{
want: M.or({ Collateral: AmountShape }, {}),
exit: FullProposalShape.exit,
},
);

const publicFacet = augmentPublicFacet(
harden({
/** @param {Brand<'nat'>} collateralBrand */
makeBidInvitation(collateralBrand) {
mustMatch(collateralBrand, BrandShape);
books.has(collateralBrand) ||
Fail`No book for brand ${collateralBrand}`;
const bidSpecShape = makeBidSpecShape(brands.Currency, collateralBrand);
/**
* @param {ZCFSeat} zcfSeat
* @param {import('./auctionBook.js').BidSpec} bidSpec
*/
const newBidHandler = (zcfSeat, bidSpec) => {
// xxx consider having Zoe guard the offerArgs with a provided shape
mustMatch(bidSpec, bidSpecShape);
const auctionBook = books.get(collateralBrand);
auctionBook.addOffer(bidSpec, zcfSeat, isActive());
return 'Your offer has been received';
};
const bidProposalShape = M.splitRecord(
{
give: { Currency: { brand: brands.Currency, value: M.nat() } },
},
{
want: M.or({ Collateral: AmountShape }, {}),
exit: FullProposalShape.exit,
},
);

return zcf.makeInvitation(
newBidHandler,
Expand Down

0 comments on commit 1233e09

Please sign in to comment.