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β
StateLogSubscriber
<State
,BookSubscriptionEvent
>β³
Semibook
Implementsβ
Iterable
<Offer
>
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β
Name | Type |
---|---|
market | Market |
ba | BA |
eventListener | EventListener |
options | Options |
Returnsβ
Promise
<Semibook
>
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:237
copyβ
βΈ copy(state
): State
Parametersβ
Name | Type |
---|---|
state | State |
Returnsβ
Overridesβ
StateLogSubscriber.copy
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:268
addEventListenerβ
βΈ addEventListener(listener
): void
Parametersβ
Name | Type |
---|---|
listener | EventListener |
Returnsβ
void
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:316
removeEventListenerβ
βΈ removeEventListener(listener
): void
Parametersβ
Name | Type |
---|---|
listener | EventListener |
Returnsβ
void
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:320
requestOfferListPrefixβ
βΈ requestOfferListPrefix(options
): Promise
<Offer
[]>
Parametersβ
Name | Type |
---|---|
options | Options |
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β
Name | Type |
---|---|
offerId | number |
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β
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β
Name | Type |
---|---|
data | Omit <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β
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β
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β
Name | Type |
---|---|
params | VolumeParams |
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β
Name | Type |
---|---|
maxTick | number |
fillVolume | Big |
fillWants | boolean |
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β
Name | Type |
---|---|
price | undefined | BigSource |
referencePrice | undefined | 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β
Name | Type |
---|---|
price | undefined | BigSource |
referencePrice | undefined | 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β
Name | Type | Description |
---|---|---|
gasreq | number | The 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β
Name | Type |
---|---|
block | BlockWithoutParentHash |
Returnsβ
Promise
<ErrorOrState
<State
>>
Overridesβ
StateLogSubscriber.stateInitialize
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:873
stateHandleLogβ
βΈ stateHandleLog(state
, log
, event?
): State
Parametersβ
Name | Type |
---|---|
state | State |
log | Log |
event? | BookSubscriptionEvent |
Returnsβ
Overridesβ
StateLogSubscriber.stateHandleLog
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:922
rawLocalConfigToLocalConfigβ
βΈ rawLocalConfigToLocalConfig(local
): LocalConfigFull
Parametersβ
Name | Type |
---|---|
local | LocalUnpackedStructOutput |
Returnsβ
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:1363
rawLocalConfigToLocalConfigβ
βΈ rawLocalConfigToLocalConfig(local
, outboundDecimals
): LocalConfigFull
Parametersβ
Name | Type |
---|---|
local | LocalUnpackedStructOutput |
outboundDecimals | number |
Returnsβ
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:1370
rawOfferSlimToOfferSlimβ
βΈ rawOfferSlimToOfferSlim(raw
): OfferSlim
Parametersβ
Name | Type |
---|---|
raw | RawOfferSlim |
Returnsβ
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:1389
rawIdToIdβ
βΈ rawIdToId(rawId
): undefined
| number
Parametersβ
Name | Type |
---|---|
rawId | BigNumber |
Returnsβ
undefined
| number
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:1412
idToRawIdβ
βΈ idToRawId(id
): BigNumber
Parametersβ
Name | Type |
---|---|
id | undefined | number |
Returnsβ
BigNumber
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:1417
getIsVolumeDesiredForAsksβ
βΈ getIsVolumeDesiredForAsks(opts
): boolean
Parametersβ
Name | Type |
---|---|
opts | BookOptions |
Returnsβ
boolean
Defined inβ
@mangrovedao/mangrove.js/src/semibook.ts:1454
getIsVolumeDesiredForBidsβ
βΈ getIsVolumeDesiredForBids(opts
): boolean
Parametersβ
Name | Type |
---|---|
opts | BookOptions |
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β
Inherited fromβ
StateLogSubscriber.getLatestState
Defined inβ
@mangrovedao/reliable-event-subscriber/dist/stateLogSubscriber.d.ts:17
initializeβ
βΈ initialize(wantedBlock
): Promise
<InitializeErrorOrBlock
>
Parametersβ
Name | Type |
---|---|
wantedBlock | BlockWithoutParentHash |
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β
Name | Type |
---|---|
log | Log |
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β
Name | Type |
---|---|
block | Block |
Returnsβ
void
Inherited fromβ
StateLogSubscriber.rollback
Defined inβ
@mangrovedao/reliable-event-subscriber/dist/stateLogSubscriber.d.ts:28