Skip to content

Commit

Permalink
Add option to disable unknown offer id error
Browse files Browse the repository at this point in the history
  • Loading branch information
maxencerb committed Apr 16, 2024
1 parent d7c9c66 commit 8709261
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 3 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
# Next version

- Add option to disable unknown offer id.

# 2.0.5-50

- Change gas estimation for kandel populate transaction.
Expand Down
9 changes: 9 additions & 0 deletions src/market.ts
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,11 @@ namespace Market {
*
* Defaults to `Semibook.DEFAULT_CHUNK_SIZE`. */
chunkSize?: number;

/**
* Whether to allow unknown offer IDs in the cache.
*/
allowUnknownId?: boolean;
};

/**
Expand Down Expand Up @@ -684,20 +689,24 @@ class Market {
return {
desiredVolume: semibookDesiredVolume,
chunkSize: opts.chunkSize,
allowUnknownId: opts.allowUnknownId,
};
} else if ("desiredPrice" in opts) {
return {
desiredPrice: opts.desiredPrice,
chunkSize: opts.chunkSize,
allowUnknownId: opts.allowUnknownId,
};
} else if ("targetNumberOfTicks" in opts) {
return {
targetNumberOfTicks: opts.targetNumberOfTicks,
chunkSize: opts.chunkSize,
allowUnknownId: opts.allowUnknownId,
};
} else {
return {
chunkSize: opts.chunkSize,
allowUnknownId: opts.allowUnknownId,
};
}
};
Expand Down
18 changes: 15 additions & 3 deletions src/semibook.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,11 @@ namespace Semibook {
*
* Defaults to `Semibook.DEFAULT_CHUNK_SIZE`. */
chunkSize?: number;

/**
* Whether to allow unknown offer IDs in the cache.
*/
allowUnknownId?: boolean;
};

/**
Expand Down Expand Up @@ -224,8 +229,7 @@ class Semibook
readonly market: Market;
readonly tickPriceHelper: TickPriceHelper;
readonly options: Semibook.ResolvedOptions; // complete and validated
readonly #cacheOperations: SemibookCacheOperations =
new SemibookCacheOperations();
readonly #cacheOperations: SemibookCacheOperations;

#eventListeners: Map<Semibook.EventListener, boolean> = new Map();

Expand Down Expand Up @@ -868,6 +872,8 @@ class Semibook
this.olKey = market.getOLKey(ba);

this.#eventListeners.set(eventListener, true);

this.#cacheOperations = new SemibookCacheOperations(options.allowUnknownId);
}

async stateInitialize(
Expand Down Expand Up @@ -1556,6 +1562,12 @@ class CacheIterator implements Semibook.CacheIterator {
// Only used internally, exposed for testing.
// All modifications of the cache must be called in a context where #cacheLock is acquired.
export class SemibookCacheOperations {
private allowUnknownId: boolean;

constructor(allowUnknownId = false) {
this.allowUnknownId = allowUnknownId;
}

// Marks an incomplete cache as known to be complete.
markComplete(state: Semibook.State): void {
if (state.isComplete) {
Expand Down Expand Up @@ -1645,7 +1657,7 @@ export class SemibookCacheOperations {
removeOfferDueToEvent(
state: Semibook.State,
id: number,
allowUnknownId = false,
allowUnknownId = this.allowUnknownId,
): Market.Offer | undefined {
const offer = state.offerCache.get(id);
if (offer === undefined) {
Expand Down

0 comments on commit 8709261

Please sign in to comment.