Skip to content

Commit

Permalink
Merge pull request #139 from mangrovedao/feature/mangrove.js/market-i…
Browse files Browse the repository at this point in the history
…mprovements

Feature/mangrove.js/market improvements
  • Loading branch information
espendk committed Jan 16, 2022
2 parents 41c031b + e682c60 commit d664237
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 21 deletions.
1 change: 1 addition & 0 deletions packages/mangrove.js/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
- `Market.{subscribe|consoleAsks|consoleBids|prettyPrint}` are no longer `async`
- Removed `fromId` and `blockNumber` from `Market.BookOptions`
- `Market.Offer.{prev|next}` are now `undefined` if there is no previous/next offer
- `Market.getPivot` now throws `Error` if the order book cache is insufficient to determine a pivot

# 0.0.8

Expand Down
40 changes: 24 additions & 16 deletions packages/mangrove.js/src/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ namespace Market {

export type Offer = {
id: number;
prev: number;
next: number;
prev: number | undefined;
next: number | undefined;
gasprice: number;
maker: string;
gasreq: number;
Expand Down Expand Up @@ -204,8 +204,8 @@ class Market {
this.#updateBook("bids");
}

#semibookCallback({ cbArg, ba, event, ethersEvent }: SemibookEvent): void {
this.#updateBook(ba);
#semibookCallback({ cbArg, event, ethersEvent }: SemibookEvent): void {
this.#updateBook(cbArg.ba);
for (const [cb, params] of this.#subscriptions) {
if (params.type === "once") {
if (!("filter" in params) || params.filter(cbArg, event, ethersEvent)) {
Expand Down Expand Up @@ -294,26 +294,34 @@ class Market {
/* Given a price, find the id of the immediately-better offer in the
book. */
getPivot(ba: "asks" | "bids", price: Bigish): number {
// we select as pivot the immediately-better offer
// the actual ordering in the offer list is lexicographic
// We select as pivot the immediately-better offer.
// The actual ordering in the offer list is lexicographic
// price * gasreq (or price^{-1} * gasreq)
// we ignore the gasreq comparison because we may not
// We ignore the gasreq comparison because we may not
// know the gasreq (could be picked by offer contract)
price = Big(price);
const comparison = ba === "asks" ? "gt" : "lt";
let latest_id = 0;
for (const [i, offer] of this.#book[ba].entries()) {
let lastSeenOffer: Market.Offer;
let pivotFound = false;
for (const offer of this.#book[ba]) {
lastSeenOffer = offer;
if (offer.price[comparison](price)) {
pivotFound = true;
break;
}
latest_id = offer.id;
if (i === this.#book[ba].length) {
throw new Error(
"Impossible to safely determine a pivot. Please restart with a larger maxOffers."
);
}
}
return latest_id;
if (pivotFound) {
return lastSeenOffer.prev || 0;
}
// If we reached the end of the offer list (which is possible empty), use the last offer as pivot
if (lastSeenOffer?.next === undefined) {
return lastSeenOffer?.id || 0;
} else {
// The semibook cache is incomplete
throw new Error(
"Impossible to safely determine a pivot. Please restart with a larger maxOffers."
);
}
}

/** Determine the price from gives or wants depending on whether you're working with bids or asks. */
Expand Down
5 changes: 0 additions & 5 deletions packages/mangrove.js/src/semibook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ let canConstructSemibook = false;

export type SemibookEvent = {
cbArg: Market.BookSubscriptionCbArgument;
ba: "bids" | "asks";
event: Market.BookSubscriptionEvent;
ethersEvent: ethers.Event;
};
Expand Down Expand Up @@ -218,7 +217,6 @@ export class Semibook implements Iterable<Market.Offer> {
offer: offer,
ba: this.ba,
},
ba: this.ba,
event,
ethersEvent,
});
Expand All @@ -238,7 +236,6 @@ export class Semibook implements Iterable<Market.Offer> {
takerGives: inbound_tkn.fromUnits(event.args.takerGives),
mgvData: event.args.mgvData,
},
ba: this.ba,
event,
ethersEvent,
});
Expand All @@ -257,7 +254,6 @@ export class Semibook implements Iterable<Market.Offer> {
takerWants: outbound_tkn.fromUnits(event.args.takerWants),
takerGives: inbound_tkn.fromUnits(event.args.takerGives),
},
ba: this.ba,
event,
ethersEvent,
});
Expand All @@ -274,7 +270,6 @@ export class Semibook implements Iterable<Market.Offer> {
ba: this.ba,
offer: removedOffer,
},
ba: this.ba,
event,
ethersEvent,
});
Expand Down

0 comments on commit d664237

Please sign in to comment.