Skip to content

Commit

Permalink
refactor: add type to bid record kit; move type declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed Jul 24, 2023
1 parent 45fe766 commit 9c2ed56
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 30 deletions.
36 changes: 9 additions & 27 deletions packages/inter-protocol/src/auction/auctionBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,11 @@ import { E } from '@endo/captp';
import { observeNotifier } from '@agoric/notifier';

import { makeNatAmountShape } from '../contractSupport.js';
import { preparePriceBook, prepareScaledBidBook } from './offerBook.js';
import {
BidsDataNotificationShape,
preparePriceBook,
prepareScaledBidBook,
} from './offerBook.js';
import {
isScaledBidPriceHigher,
makeBrandedRatioPattern,
Expand Down Expand Up @@ -123,31 +127,6 @@ export const BookDataNotificationShape = M.splitRecord(
);
harden(BookDataNotificationShape);

/**
* @typedef {object} ScaledBidData
* @property {Ratio} bidScaling
* @property {Amount<'nat'>} wanted
* @property {boolean} exitAfterBuy
*/

/**
* @typedef {object} PricedBidData
* @property {Ratio} price
* @property {Amount<'nat'>} wanted
* @property {boolean} exitAfterBuy
*/

/**
* @typedef {object} BidDataNotification
* @property {ScaledBidData[]} scaledBids
* @property {PricedBidData[]} pricedBids
*/
export const BidDataNotificationShape = {
scaledBids: M.arrayOf(M.any()),
pricedBids: M.arrayOf(M.any()),
};
harden(BidDataNotificationShape);

/**
* @param {Baggage} baggage
* @param {ZCF} zcf
Expand Down Expand Up @@ -226,10 +205,13 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
BookDataNotificationShape
),
);

/** @typedef {import('./offerBook.js').BidDataNotification} BidDataNotification */

const bidsDataKit = makeRecorderKit(
bidsNode,
/** @type {import('@agoric/zoe/src/contractSupport/recorder.js').TypedMatcher<BidDataNotification>} */ (
BidDataNotificationShape
BidsDataNotificationShape
),
);

Expand Down
65 changes: 62 additions & 3 deletions packages/inter-protocol/src/auction/offerBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// discount/markup from the current oracle price.

import { E } from '@endo/captp';
import { AmountMath, BrandShape } from '@agoric/ertp';
import { AmountMath, BrandShape, AmountShape, RatioShape } from '@agoric/ertp';
import { StorageNodeShape } from '@agoric/internal';
import { M, mustMatch } from '@agoric/store';
import {
Expand All @@ -12,6 +12,7 @@ import {
provide,
} from '@agoric/vat-data';
import { makePromiseKit } from '@endo/promise-kit';
import { TimestampShape } from '@agoric/time';

import {
toBidScalingComparator,
Expand Down Expand Up @@ -78,6 +79,56 @@ const makeGetBidDataRecorder = (bidDataKits, bidDataKitPromises) => {
* >} RecorderKit
*/

/**
* @typedef {object} ScaledBidData
* @property {Ratio} bidScaling
* @property {Amount<'nat'>} wanted
* @property {boolean} exitAfterBuy
*/

/**
* @typedef {object} PricedBidData
* @property {Ratio} price
* @property {Amount<'nat'>} wanted
* @property {boolean} exitAfterBuy
*/

/**
* @typedef {object} BidDataNotification
* @property {ScaledBidData[]} scaledBids
* @property {PricedBidData[]} pricedBids
*/

export const BidDataNotificationShape = M.or(
{
price: RatioShape,
originalWant: AmountShape,
remainingWant: AmountShape,
exitAfterBuy: M.boolean(),
timestamp: TimestampShape,
balance: AmountShape,
sequence: M.bigint(),
},
{
bidScaling: RatioShape,
originalWant: AmountShape,
remainingWant: AmountShape,
exitAfterBuy: M.boolean(),
timestamp: TimestampShape,
balance: AmountShape,
sequence: M.bigint(),
},
// XXX for deletion. Should go away when we can actually delete
M.string(),
);
harden(BidDataNotificationShape);

export const BidsDataNotificationShape = {
scaledBids: M.arrayOf(BidDataNotificationShape),
pricedBids: M.arrayOf(BidDataNotificationShape),
};
harden(BidsDataNotificationShape);

/**
* Prices in this book are expressed as percentage of the full oracle price
* snapshot taken when the auction started. .4 is 60% off. 1.1 is 10% above
Expand Down Expand Up @@ -134,7 +185,11 @@ export const prepareScaledBidBook = (baggage, makeRecorderKit) => {
const bidDataKitP = makePromiseKit();
bidDataKitPromises.init(key, bidDataKitP.promise);
E.when(makeBidNode(bidsNode, seqNum), childBidNode => {
const recorderKit = makeRecorderKit(childBidNode);
const recorderKit = makeRecorderKit(
childBidNode,
/** @type {import('@agoric/zoe/src/contractSupport/recorder.js').TypedMatcher<BidDataNotification>} */
(BidDataNotificationShape),
);
bidDataKits.init(key, recorderKit);
bidDataKitP.resolve(recorderKit);
bidDataKitPromises.delete(key);
Expand Down Expand Up @@ -281,7 +336,11 @@ export const preparePriceBook = (baggage, makeRecorderKit) => {
const bidDataKitP = makePromiseKit();
bidDataKitPromises.init(key, bidDataKitP.promise);
E.when(makeBidNode(bidsNode, seqNum), childBidNode => {
const recorderKit = makeRecorderKit(childBidNode);
const recorderKit = makeRecorderKit(
childBidNode,
/** @type {import('@agoric/zoe/src/contractSupport/recorder.js').TypedMatcher<BidDataNotification>} */
(BidDataNotificationShape),
);
bidDataKits.init(key, recorderKit);
bidDataKitP.resolve(recorderKit);
bidDataKitPromises.delete(key);
Expand Down

0 comments on commit 9c2ed56

Please sign in to comment.