Skip to content

Commit

Permalink
feat(auction): auction publish all bids off-chain
Browse files Browse the repository at this point in the history
  • Loading branch information
Chris-Hibbert committed May 4, 2023
1 parent 609421b commit f29eab8
Show file tree
Hide file tree
Showing 7 changed files with 233 additions and 21 deletions.
57 changes: 53 additions & 4 deletions packages/inter-protocol/src/auction/auctionBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,29 @@ export const makeOfferSpecShape = (bidBrand, collateralBrand) => {
* @property {Amount<'nat'> | null} collateralAvailable The amount of collateral remaining
*/

/**
* @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 {Array<ScaledBidData>} scaledBids
* @property {Array<PricedBidData>} pricedBids
*/

/**
* @param {Baggage} baggage
* @param {ZCF} zcf
Expand All @@ -120,6 +143,7 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
priceAuthority: M.any(),
updatingOracleQuote: M.any(),
bookDataKit: M.any(),
bidDataKit: M.any(),
priceBook: M.any(),
scaledBidBook: M.any(),
startCollateral: M.any(),
Expand All @@ -137,9 +161,9 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
* @param {Brand<'nat'>} bidBrand
* @param {Brand<'nat'>} collateralBrand
* @param {PriceAuthority} pAuthority
* @param {StorageNode} node
* @param {Array<StorageNode>} nodes
*/
(bidBrand, collateralBrand, pAuthority, node) => {
(bidBrand, collateralBrand, pAuthority, nodes) => {
assertAllDefined({ bidBrand, collateralBrand, pAuthority });
const zeroBid = makeEmpty(bidBrand);
const zeroRatio = makeRatioFromAmounts(
Expand All @@ -166,12 +190,19 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
collateralBrand,
);

const [scheduleNode, bidsNode] = nodes;
const bookDataKit = makeRecorderKit(
node,
scheduleNode,
/** @type {import('@agoric/zoe/src/contractSupport/recorder.js').TypedMatcher<BookDataNotification>} */ (
M.any()
),
);
const bidDataKit = makeRecorderKit(
bidsNode,
/** @type {import('@agoric/zoe/src/contractSupport/recorder.js').TypedMatcher<BidDataNotification>} */ (
M.any()
),
);

return {
collateralBrand,
Expand All @@ -185,6 +216,7 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
updatingOracleQuote: zeroRatio,

bookDataKit,
bidDataKit,

priceBook,
scaledBidBook,
Expand Down Expand Up @@ -381,6 +413,7 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
} else {
trace('added Offer ', price, stillWant.value);
priceBook.add(seat, price, stillWant, exitAfterBuy);
helper.publishBidData();
}

helper.publishBookData();
Expand Down Expand Up @@ -435,10 +468,20 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
seat.exit();
} else {
scaledBidBook.add(seat, bidScaling, stillWant, exitAfterBuy);
helper.publishBidData();
}

helper.publishBookData();
},
publishBidData() {
const { state } = this;
// XXX should this be compressed somewhat? lots of redundant brands.
state.bidDataKit.recorder.write({
scaledBids: state.scaledBidBook.publishOffers(),
// @ts-expect-error how to convince TS these ratios are non-null?
pricedBids: state.priceBook.publishOffers(),
});
},
publishBookData() {
const { state } = this;

Expand Down Expand Up @@ -602,6 +645,7 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
}

facets.helper.publishBookData();
facets.helper.publishBidData();
},
getCurrentPrice() {
return this.state.curAuctionPrice;
Expand Down Expand Up @@ -691,18 +735,22 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
getDataUpdates() {
return this.state.bookDataKit.subscriber;
},
getBidDataUpdates() {
return this.state.bidDataKit.subscriber;
},
getPublicTopics() {
return {
bookData: makeRecorderTopic(
'Auction schedule',
this.state.bookDataKit,
),
bids: makeRecorderTopic('Auction Bids', this.state.bidDataKit),
};
},
},
},
{
finish: ({ state }) => {
finish: ({ state, facets }) => {
const { collateralBrand, bidBrand, priceAuthority } = state;
assertAllDefined({ collateralBrand, bidBrand, priceAuthority });
void E.when(
Expand Down Expand Up @@ -735,6 +783,7 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
});
},
);
facets.helper.publishBidData();
},
stateShape: AuctionBookStateShape,
},
Expand Down
11 changes: 9 additions & 2 deletions packages/inter-protocol/src/auction/auctioneer.js
Original file line number Diff line number Diff line change
Expand Up @@ -653,6 +653,9 @@ export const start = async (zcf, privateArgs, baggage) => {
getBookDataUpdates(brand) {
return books.get(brand).getDataUpdates();
},
getBidDataUpdates(brand) {
return books.get(brand).getBidDataUpdates();
},
getPublicTopics(brand) {
if (brand) {
return books.get(brand).getPublicTopics();
Expand Down Expand Up @@ -681,13 +684,17 @@ export const start = async (zcf, privateArgs, baggage) => {

const bookId = `book${bookCounter}`;
bookCounter += 1;
const bNode = await E(privateArgs.storageNode).makeChildNode(bookId);
const bNode = E(privateArgs.storageNode).makeChildNode(bookId);
const nodes = await Promise.all([
E(bNode).makeChildNode('schedule'),
E(bNode).makeChildNode('bids'),
]);

const newBook = await makeAuctionBook(
brands.Bid,
brand,
priceAuthority,
bNode,
nodes,
);

// These three store.init() calls succeed or fail atomically
Expand Down
20 changes: 20 additions & 0 deletions packages/inter-protocol/src/auction/offerBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,16 @@ export const prepareScaledBidBook = baggage =>
const { records } = this.state;
return [...records.entries(M.gte(toBidScalingComparator(bidScaling)))];
},
publishOffers() {
const { records } = this.state;
return [...records.values()].map(r => {
return harden({
bidScaling: r.bidScaling,
wanted: r.wanted,
exitAfterBuy: r.exitAfterBuy,
});
});
},
hasOrders() {
const { records } = this.state;
return records.getSize() > 0;
Expand Down Expand Up @@ -186,6 +196,16 @@ export const preparePriceBook = baggage =>
const { records } = this.state;
return [...records.entries(M.gte(toPartialOfferKey(price)))];
},
publishOffers() {
const { records } = this.state;
return [...records.values()].map(r => {
return harden({
price: r.price,
wanted: r.wanted,
exitAfterBuy: r.exitAfterBuy,
});
});
},
hasOrders() {
const { records } = this.state;
return records.getSize() > 0;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,38 @@ Generated by [AVA](https://avajs.dev).
[
[
'published.auction.book0',
'published.auction.book0.bids',
{
pricedBids: [
{
exitAfterBuy: false,
price: {
denominator: {
brand: {
iface: 'Alleged: Collateral brand',
},
value: 200n,
},
numerator: {
brand: {
iface: 'Alleged: Bid brand',
},
value: 250n,
},
},
wanted: {
brand: {
iface: 'Alleged: Collateral brand',
},
value: 200n,
},
},
],
scaledBids: [],
},
],
[
'published.auction.book0.schedule',
{
collateralAvailable: {
brand: {
Expand Down
Binary file not shown.
Loading

0 comments on commit f29eab8

Please sign in to comment.