Skip to content

Commit

Permalink
feat: publish data on individual bids when there are changes
Browse files Browse the repository at this point in the history
Includes deleting the content for a bid when it is removed.
  • Loading branch information
Chris-Hibbert committed Jul 19, 2023
1 parent f2d94a4 commit 0bac932
Show file tree
Hide file tree
Showing 5 changed files with 108 additions and 26 deletions.
9 changes: 0 additions & 9 deletions packages/inter-protocol/src/auction/auctionBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,6 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
} else {
trace('added Offer ', price, stillWant.value);
priceBook.add(seat, price, stillWant, exitAfterBuy, timestamp);
helper.publishBidData();
}

void helper.publishBookData();
Expand Down Expand Up @@ -506,16 +505,10 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
exitAfterBuy,
timestamp,
);
void helper.publishBidData();
}

void helper.publishBookData();
},
publishBidData() {
const { state } = this;
state.scaledBidBook.publishOffers();
state.priceBook.publishOffers();
},
publishBookData() {
const { state } = this;

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

void facets.helper.publishBookData();
void facets.helper.publishBidData();
},
getCurrentPrice() {
return this.state.curAuctionPrice;
Expand Down Expand Up @@ -832,7 +824,6 @@ export const prepareAuctionBook = (baggage, zcf, makeRecorderKit) => {
},
);
void facets.helper.publishBookData();
void facets.helper.publishBidData();
},
stateShape: AuctionBookStateShape,
},
Expand Down
36 changes: 19 additions & 17 deletions packages/inter-protocol/src/auction/offerBook.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,12 +53,22 @@ const makeBidNode = (bidsNode, bidId) =>
E(bidsNode).makeChildNode(`bid${bidId}`);

const makeGetBidDataRecorder = (bidDataKits, bidDataKitPromises) => {
return key => {
const getBidDataRecorder = key => {
if (bidDataKitPromises.has(key)) {
return E.get(bidDataKitPromises.get(key)).recorder;
}
return bidDataKits.get(key).recorder;
};

const deleteNodeIfPresent = key => {
if (bidDataKitPromises.has(key) || bidDataKits.has(key)) {

// TODO(8063) delete node rather than erasing the data
return E(getBidDataRecorder(key)).writeFinal('');
}
}

return { getBidDataRecorder, deleteNodeIfPresent };
};

/** @typedef {ReturnType<import('@agoric/zoe/src/contractSupport/recorder.js').MakeRecorderKit>} RecorderKit */
Expand All @@ -78,7 +88,7 @@ export const prepareScaledBidBook = (baggage, makeRecorderKit) => {
const bidDataKits = baggage.get('bidDataKits');
/** @type {MapStore<string, Promise<RecorderKit>>} */
const bidDataKitPromises = makeScalarMapStore('bidDataKit Promises');
const getBidDataRecorder = makeGetBidDataRecorder(
const { getBidDataRecorder, deleteNodeIfPresent } = makeGetBidDataRecorder(
bidDataKits,
bidDataKitPromises,
);
Expand Down Expand Up @@ -111,7 +121,6 @@ export const prepareScaledBidBook = (baggage, makeRecorderKit) => {
const { bidScalingPattern, collateralBrand, records, bidsNode } =
this.state;
mustMatch(bidScaling, bidScalingPattern);

const seqNum = nextSequenceNumber(baggage);
const key = toScaledRateOfferKey(bidScaling, seqNum);

Expand Down Expand Up @@ -139,6 +148,7 @@ export const prepareScaledBidBook = (baggage, makeRecorderKit) => {
timestamp,
};
records.init(key, harden(bidderRecord));
this.self.publishOffer(bidderRecord);
return key;
},
/** @param {Ratio} bidScaling */
Expand Down Expand Up @@ -167,19 +177,13 @@ export const prepareScaledBidBook = (baggage, makeRecorderKit) => {
}),
);
},
publishOffers() {
const { records } = this.state;

for (const r of records.values()) {
this.self.publishOffer(r);
}
},
hasOrders() {
const { records } = this.state;
return records.getSize() > 0;
},
delete(key) {
const { records } = this.state;
void deleteNodeIfPresent(key);
bidDataKits.delete(key);
records.delete(key);
},
Expand All @@ -203,6 +207,7 @@ export const prepareScaledBidBook = (baggage, makeRecorderKit) => {
bidDataKits.delete(key);
}
records.delete(key);
void deleteNodeIfPresent(key);
}
}
},
Expand Down Expand Up @@ -231,7 +236,7 @@ export const preparePriceBook = (baggage, makeRecorderKit) => {
const bidDataKits = baggage.get('bidDataKits');
/** @type {MapStore<string, Promise<RecorderKit>>} */
const bidDataKitPromises = makeScalarMapStore('bidDataKit Promises');
const getBidDataRecorder = makeGetBidDataRecorder(
const { getBidDataRecorder, deleteNodeIfPresent } = makeGetBidDataRecorder(
bidDataKits,
bidDataKitPromises,
);
Expand Down Expand Up @@ -292,6 +297,7 @@ export const preparePriceBook = (baggage, makeRecorderKit) => {
timestamp,
});
records.init(key, bidderRecord);
this.self.publishOffer(bidderRecord);
return key;
},
offersAbove(price) {
Expand Down Expand Up @@ -319,18 +325,13 @@ export const preparePriceBook = (baggage, makeRecorderKit) => {
}),
);
},
publishOffers() {
const { records } = this.state;
for (const r of records.values()) {
this.self.publishOffer(r);
}
},
hasOrders() {
const { records } = this.state;
return records.getSize() > 0;
},
delete(key) {
const { records } = this.state;
void deleteNodeIfPresent(key);
bidDataKits.delete(key);
records.delete(key);
},
Expand All @@ -353,6 +354,7 @@ export const preparePriceBook = (baggage, makeRecorderKit) => {
if (bidDataKits.has(key)) {
bidDataKits.delete(key);
}
void deleteNodeIfPresent(key);
records.delete(key);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -259,3 +259,83 @@ Generated by [AVA](https://avajs.dev).
},
],
]

## multiple bidders at one auction step

> Under "published", the "auction.books.book0.bids" node is delegated to the auctioneer contract.
> The example below illustrates the schema of the data published there.
>
> See also board marshalling conventions (_to appear_).
[
[
'published.auction.books.book0.bids.bid1001',
'',
],
[
'published.auction.books.book0.bids.bid1002',
{
balance: {
brand: Object @Alleged: Bid brand {},
value: 116n,
},
exitAfterBuy: false,
originalWant: {
brand: Object @Alleged: Collateral brand {},
value: 200n,
},
price: {
denominator: {
brand: Object @Alleged: Collateral brand {},
value: 200n,
},
numerator: {
brand: Object @Alleged: Bid brand {},
value: 232n,
},
},
remainingWant: {
brand: Object @Alleged: Collateral brand {},
value: 100n,
},
sequence: 1002n,
timestamp: {
absValue: 167n,
timerBrand: Object @Alleged: timerBrand {},
},
},
],
[
'published.auction.books.book0.bids.bid1003',
{
balance: {
brand: Object @Alleged: Bid brand {},
value: 210n,
},
exitAfterBuy: false,
originalWant: {
brand: Object @Alleged: Collateral brand {},
value: 200n,
},
price: {
denominator: {
brand: Object @Alleged: Collateral brand {},
value: 200n,
},
numerator: {
brand: Object @Alleged: Bid brand {},
value: 210n,
},
},
remainingWant: {
brand: Object @Alleged: Collateral brand {},
value: 200n,
},
sequence: 1003n,
timestamp: {
absValue: 167n,
timerBrand: Object @Alleged: timerBrand {},
},
},
],
]
Binary file not shown.
9 changes: 9 additions & 0 deletions packages/inter-protocol/test/auction/test-auctionContract.js
Original file line number Diff line number Diff line change
Expand Up @@ -1340,6 +1340,15 @@ test.serial('multiple bidders at one auction step', async t => {
await driver.advanceTo(now, 'wait');

t.true(await E(seat1).hasExited());

const doc = {
node: 'auction.books.book0.bids',
owner: 'the auctioneer contract',
pattern: 'mockChainStorageRoot.auction',
replacement: 'published.auction',
};
await documentStorageSchema(t, driver.mockChainStorage, doc);

t.false(await E(seat2).hasExited());
await E(seat2).tryExit();

Expand Down

0 comments on commit 0bac932

Please sign in to comment.