Skip to main content

Class: Semibook

The Semibook is a data structure for maintaining a cache of an offer list for one side (asks or bids) of a market.

While offer lists on-chain for a market A-B are symmetric (the offer lists are the same for the market B-A), a Semibook depends on the market:

  • Prices are in terms of quote tokens per base token
  • Volumes are in terms of base tokens

Hierarchy​

Implements​

Properties​

DEFAULT_TARGET_NUMBER_OF_TICKS​

β–ͺ Static Readonly DEFAULT_TARGET_NUMBER_OF_TICKS: 50

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:220


DEFAULT_CHUNK_SIZE​

β–ͺ Static Readonly DEFAULT_CHUNK_SIZE: 50

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:221


ba​

β€’ Readonly ba: BA

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:223


market​

β€’ Readonly market: Market

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:224


tickPriceHelper​

β€’ Readonly tickPriceHelper: TickPriceHelper

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:225


options​

β€’ Readonly options: ResolvedOptions

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:226


tradeManagement​

β€’ tradeManagement: Trade

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:232


optionsIdentifier​

β€’ optionsIdentifier: string

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:234


olKey​

β€’ olKey: OLKeyStruct

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:235


initializedAt​

β€’ Optional initializedAt: BlockWithoutParentHash

Inherited from​

StateLogSubscriber.initializedAt

Defined in​

@mangrovedao/reliable-event-subscriber/dist/logSubscriber.d.ts:14


lastSeenEventBlock​

β€’ Optional lastSeenEventBlock: BlockWithoutParentHash

Inherited from​

StateLogSubscriber.lastSeenEventBlock

Defined in​

@mangrovedao/reliable-event-subscriber/dist/logSubscriber.d.ts:15


cacheLock​

β€’ Protected cacheLock: Mutex

Inherited from​

StateLogSubscriber.cacheLock

Defined in​

@mangrovedao/reliable-event-subscriber/dist/stateLogSubscriber.d.ts:12

Methods​

connect​

β–Έ connect(market, ba, eventListener, options): Promise<Semibook>

Parameters​

NameType
marketMarket
baBA
eventListenerEventListener
optionsOptions

Returns​

Promise<Semibook>

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:237


copy​

β–Έ copy(state): State

Parameters​

NameType
stateState

Returns​

State

Overrides​

StateLogSubscriber.copy

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:268


addEventListener​

β–Έ addEventListener(listener): void

Parameters​

NameType
listenerEventListener

Returns​

void

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:316


removeEventListener​

β–Έ removeEventListener(listener): void

Parameters​

NameType
listenerEventListener

Returns​

void

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:320


requestOfferListPrefix​

β–Έ requestOfferListPrefix(options): Promise<Offer[]>

Parameters​

NameType
optionsOptions

Returns​

Promise<Offer[]>

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:324


offerInfo​

β–Έ offerInfo(offerId): Promise<Offer>

Returns struct containing offer details in the current offer list

Parameters​

NameType
offerIdnumber

Returns​

Promise<Offer>

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:344


config​

β–Έ config(): LocalConfig

Return config local to a semibook. Notes: Amounts are converted to human readable numbers. density is converted to public token units per gas used fee remains in basis points of the token being bought

Returns​

LocalConfig

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:374


permit​

β–Έ permit(data): Promise<ContractTransaction>

Sign permit data for buying outbound_tkn with spender's inbound_tkn See mangrove.ts.

Parameters​

NameType
dataOmit<SimplePermitData, "outbound_tkn" | "inbound_tkn">

Returns​

Promise<ContractTransaction>

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:396


size​

β–Έ size(): number

Returns the number of offers in the cache.

Returns​

number

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:407


getBestInCache​

β–Έ getBestInCache(): undefined | number

Returns the id of the best offer in the cache

Returns​

undefined | number

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:413


getBest​

β–Έ getBest(): Promise<undefined | Offer>

Returns the best offer if any

Returns​

Promise<undefined | Offer>

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:419


[iterator]​

β–Έ [iterator](): CacheIterator

Returns an iterator over the offers in the cache.

Returns​

CacheIterator

Implementation of​

Iterable.[iterator]

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:443


iter​

β–Έ iter(): CacheIterator

Convenience method for getting an iterator without having to call [Symbol.iterator]().

Returns​

CacheIterator

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:454


estimateVolume​

β–Έ estimateVolume(params): Promise<VolumeEstimate>

Volume estimator.

if you say estimateVolume({given:100,to:"buy"}),

it will give you an estimate of how much quote token you would have to spend to get 100 base tokens.

if you say estimateVolume({given:10,to:"sell"}),

it will given you an estimate of how much base tokens you'd have to buy in order to spend 10 quote tokens.

if you add a limitPrice field, only offers with that price or better will be considered.

So for instance, if you say {given:10,to:"sell",limitPrice:"2"}, estimateVolume will return the volume you will be able to receive if selling up to 10 quote at a max. price of 2 quote/base, i.e. a min. "price" of 1/2 = 0.5 base/quote.

The returned remainingFillVolume is how much of the given token that cannot be traded due to insufficient volume on the book / price becoming bad.

Parameters​

NameType
paramsVolumeParams

Returns​

Promise<VolumeEstimate>

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:480


simulateMarketOrder​

β–Έ simulateMarketOrder(maxTick, fillVolume, fillWants): Promise<{ totalGot: Big ; totalGave: Big ; feePaid: Big ; fillVolume: Big ; maxTickMatched?: number ; gas: BigNumber }>

Reproduces the logic of Mangrove's generalMarketOrder function faithfully with the exception of:

  • the overflow protections due to bounds on input sizes.
  • offers are assumed not to fail.

Parameters​

NameType
maxTicknumber
fillVolumeBig
fillWantsboolean

Returns​

Promise<{ totalGot: Big ; totalGave: Big ; feePaid: Big ; fillVolume: Big ; maxTickMatched?: number ; gas: BigNumber }>

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:513


isPriceBetter​

β–Έ isPriceBetter(price, referencePrice): boolean

Returns true if price is better than referencePrice; Otherwise, false is returned.

Parameters​

NameType
priceundefined | BigSource
referencePriceundefined | BigSource

Returns​

boolean

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:671


isPriceWorse​

β–Έ isPriceWorse(price, referencePrice): boolean

Returns true if price is worse than referencePrice; Otherwise, false is returned.

Parameters​

NameType
priceundefined | BigSource
referencePriceundefined | BigSource

Returns​

boolean

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:680


getMinimumVolume​

β–Έ getMinimumVolume(gasreq): Big

Determines the minimum volume required to stay above density limit for the given gasreq (with a minimum of 1 unit of outbound, since 0 gives is not allowed).

Parameters​

NameTypeDescription
gasreqnumberThe gas requirement for the offer.

Returns​

Big

The minimum volume required to stay above density limit.

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:691


getMaxGasReq​

β–Έ getMaxGasReq(): Promise<undefined | number>

Returns​

Promise<undefined | number>

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:701


stateInitialize​

β–Έ stateInitialize(block): Promise<ErrorOrState<State>>

Parameters​

NameType
blockBlockWithoutParentHash

Returns​

Promise<ErrorOrState<State>>

Overrides​

StateLogSubscriber.stateInitialize

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:873


stateHandleLog​

β–Έ stateHandleLog(state, log, event?): State

Parameters​

NameType
stateState
logLog
event?BookSubscriptionEvent

Returns​

State

Overrides​

StateLogSubscriber.stateHandleLog

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:922


rawLocalConfigToLocalConfig​

β–Έ rawLocalConfigToLocalConfig(local): LocalConfigFull

Parameters​

NameType
localLocalUnpackedStructOutput

Returns​

LocalConfigFull

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:1363


rawLocalConfigToLocalConfig​

β–Έ rawLocalConfigToLocalConfig(local, outboundDecimals): LocalConfigFull

Parameters​

NameType
localLocalUnpackedStructOutput
outboundDecimalsnumber

Returns​

LocalConfigFull

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:1370


rawOfferSlimToOfferSlim​

β–Έ rawOfferSlimToOfferSlim(raw): OfferSlim

Parameters​

NameType
rawRawOfferSlim

Returns​

OfferSlim

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:1389


rawIdToId​

β–Έ rawIdToId(rawId): undefined | number

Parameters​

NameType
rawIdBigNumber

Returns​

undefined | number

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:1412


idToRawId​

β–Έ idToRawId(id): BigNumber

Parameters​

NameType
idundefined | number

Returns​

BigNumber

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:1417


getIsVolumeDesiredForAsks​

β–Έ getIsVolumeDesiredForAsks(opts): boolean

Parameters​

NameType
optsBookOptions

Returns​

boolean

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:1454


getIsVolumeDesiredForBids​

β–Έ getIsVolumeDesiredForBids(opts): boolean

Parameters​

NameType
optsBookOptions

Returns​

boolean

Defined in​

@mangrovedao/mangrove.js/src/semibook.ts:1465


checkIfLastSeenEventBlockExists​

β–Έ checkIfLastSeenEventBlockExists(): void

Returns​

void

Inherited from​

StateLogSubscriber.checkIfLastSeenEventBlockExists

Defined in​

@mangrovedao/reliable-event-subscriber/dist/stateLogSubscriber.d.ts:16


getLatestState​

β–Έ getLatestState(): State

Returns​

State

Inherited from​

StateLogSubscriber.getLatestState

Defined in​

@mangrovedao/reliable-event-subscriber/dist/stateLogSubscriber.d.ts:17


initialize​

β–Έ initialize(wantedBlock): Promise<InitializeErrorOrBlock>

Parameters​

NameType
wantedBlockBlockWithoutParentHash

Returns​

Promise<InitializeErrorOrBlock>

Inherited from​

StateLogSubscriber.initialize

Defined in​

@mangrovedao/reliable-event-subscriber/dist/stateLogSubscriber.d.ts:18


handleLog​

β–Έ handleLog(log, event?): Promise<void>

handle received log by creating new cached state if we found a block that is newer than our cache. Then let implementation stateHandleLog modify the state.

Parameters​

NameType
logLog
event?BookSubscriptionEvent

Returns​

Promise<void>

Inherited from​

StateLogSubscriber.handleLog

Defined in​

@mangrovedao/reliable-event-subscriber/dist/stateLogSubscriber.d.ts:27


rollback​

β–Έ rollback(block): void

Parameters​

NameType
blockBlock

Returns​

void

Inherited from​

StateLogSubscriber.rollback

Defined in​

@mangrovedao/reliable-event-subscriber/dist/stateLogSubscriber.d.ts:28

  • Hierarchy
  • Implements
  • Properties
    • DEFAULT_TARGET_NUMBER_OF_TICKS
    • DEFAULT_CHUNK_SIZE
    • ba
    • market
    • tickPriceHelper
    • options
    • tradeManagement
    • optionsIdentifier
    • olKey
    • initializedAt
    • lastSeenEventBlock
    • cacheLock
  • Methods
    • connect
    • copy
    • addEventListener
    • removeEventListener
    • requestOfferListPrefix
    • offerInfo
    • config
    • permit
    • size
    • getBestInCache
    • getBest
    • iterator
    • iter
    • estimateVolume
    • simulateMarketOrder
    • isPriceBetter
    • isPriceWorse
    • getMinimumVolume
    • getMaxGasReq
    • stateInitialize
    • stateHandleLog
    • rawLocalConfigToLocalConfig
    • rawLocalConfigToLocalConfig
    • rawOfferSlimToOfferSlim
    • rawIdToId
    • idToRawId
    • getIsVolumeDesiredForAsks
    • getIsVolumeDesiredForBids
    • checkIfLastSeenEventBlockExists
    • getLatestState
    • initialize
    • handleLog
    • rollback