tardis-dev 17.0.0 → 17.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +18 -0
- package/dist/computable/booksnapshot.d.ts.map +1 -1
- package/dist/computable/booksnapshot.js +86 -36
- package/dist/computable/booksnapshot.js.map +1 -1
- package/dist/computable/computable.d.ts.map +1 -1
- package/dist/computable/computable.js +3 -1
- package/dist/computable/computable.js.map +1 -1
- package/dist/computable/context.d.ts +4 -0
- package/dist/computable/context.d.ts.map +1 -0
- package/dist/computable/context.js +9 -0
- package/dist/computable/context.js.map +1 -0
- package/dist/consts.d.ts +2 -2
- package/dist/consts.d.ts.map +1 -1
- package/dist/consts.js +2 -1
- package/dist/consts.js.map +1 -1
- package/dist/handy.d.ts.map +1 -1
- package/dist/handy.js +4 -2
- package/dist/handy.js.map +1 -1
- package/dist/instrumentinfo.d.ts +4 -1
- package/dist/instrumentinfo.d.ts.map +1 -1
- package/dist/mappers/gemini.d.ts +2 -1
- package/dist/mappers/gemini.d.ts.map +1 -1
- package/dist/mappers/gemini.js +82 -5
- package/dist/mappers/gemini.js.map +1 -1
- package/dist/mappers/index.d.ts +1 -1
- package/dist/mappers/okex.d.ts.map +1 -1
- package/dist/mappers/okex.js +26 -14
- package/dist/mappers/okex.js.map +1 -1
- package/dist/orderbook.d.ts.map +1 -1
- package/dist/orderbook.js +6 -13
- package/dist/orderbook.js.map +1 -1
- package/dist/priceleveltree.d.ts +27 -0
- package/dist/priceleveltree.d.ts.map +1 -0
- package/dist/priceleveltree.js +165 -0
- package/dist/priceleveltree.js.map +1 -0
- package/dist/realtimefeeds/gemini.d.ts +1 -0
- package/dist/realtimefeeds/gemini.d.ts.map +1 -1
- package/dist/realtimefeeds/gemini.js +14 -14
- package/dist/realtimefeeds/gemini.js.map +1 -1
- package/example.js +50 -16
- package/package.json +15 -20
- package/src/computable/booksnapshot.ts +104 -41
- package/src/computable/computable.ts +5 -1
- package/src/computable/context.ts +12 -0
- package/src/consts.ts +2 -1
- package/src/handy.ts +4 -2
- package/src/instrumentinfo.ts +5 -0
- package/src/mappers/gemini.ts +132 -8
- package/src/mappers/okex.ts +39 -18
- package/src/orderbook.ts +9 -15
- package/src/priceleveltree.ts +188 -0
- package/src/realtimefeeds/gemini.ts +15 -14
|
@@ -2,6 +2,7 @@ import { decimalPlaces } from '../handy.ts'
|
|
|
2
2
|
import { OrderBook, OnLevelRemovedCB } from '../orderbook.ts'
|
|
3
3
|
import { BookChange, BookPriceLevel, BookSnapshot, Optional } from '../types.ts'
|
|
4
4
|
import { Computable } from './computable.ts'
|
|
5
|
+
import { ComputableContext, registerContextualFactory } from './context.ts'
|
|
5
6
|
|
|
6
7
|
type BookSnapshotComputableOptions = {
|
|
7
8
|
name?: string
|
|
@@ -12,10 +13,18 @@ type BookSnapshotComputableOptions = {
|
|
|
12
13
|
onCrossedLevelRemoved?: OnLevelRemovedCB
|
|
13
14
|
}
|
|
14
15
|
|
|
15
|
-
export const computeBookSnapshots =
|
|
16
|
-
|
|
17
|
-
() =>
|
|
18
|
-
|
|
16
|
+
export const computeBookSnapshots = (options: BookSnapshotComputableOptions): (() => Computable<BookSnapshot>) => {
|
|
17
|
+
const factory = () => new BookSnapshotComputable(options)
|
|
18
|
+
registerContextualFactory(factory, (context) => new BookSnapshotComputable(options, context))
|
|
19
|
+
return factory
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
type OrderBookHandle = {
|
|
23
|
+
orderBook: OrderBook
|
|
24
|
+
isOwner: boolean
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
const sharedOrderBooksKey = Symbol('bookSnapshotOrderBooks')
|
|
19
28
|
|
|
20
29
|
const emptyBookLevel = {
|
|
21
30
|
price: undefined,
|
|
@@ -41,26 +50,33 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
41
50
|
|
|
42
51
|
private readonly _type = 'book_snapshot'
|
|
43
52
|
private readonly _orderBook: OrderBook
|
|
53
|
+
private readonly _ownsOrderBook: boolean
|
|
44
54
|
private readonly _depth: number
|
|
45
55
|
private readonly _interval: number
|
|
46
56
|
private readonly _name: string
|
|
47
57
|
private readonly _grouping: number | undefined
|
|
48
58
|
private readonly _groupingDecimalPlaces: number | undefined
|
|
59
|
+
private readonly _removeCrossedLevels: boolean
|
|
49
60
|
|
|
50
61
|
private _lastUpdateTimestamp: Date = new Date(-1)
|
|
51
62
|
private _bids: Optional<BookPriceLevel>[] = []
|
|
52
63
|
private _asks: Optional<BookPriceLevel>[] = []
|
|
64
|
+
private _groupedBidBoundary: number | undefined
|
|
65
|
+
private _groupedAskBoundary: number | undefined
|
|
53
66
|
|
|
54
|
-
constructor(
|
|
67
|
+
constructor(
|
|
68
|
+
{ depth, name, interval, removeCrossedLevels, grouping, onCrossedLevelRemoved }: BookSnapshotComputableOptions,
|
|
69
|
+
context?: ComputableContext
|
|
70
|
+
) {
|
|
55
71
|
this._depth = depth
|
|
56
72
|
this._interval = interval
|
|
57
73
|
this._grouping = grouping
|
|
58
74
|
this._groupingDecimalPlaces = this._grouping ? decimalPlaces(this._grouping) : undefined
|
|
75
|
+
this._removeCrossedLevels = Boolean(removeCrossedLevels)
|
|
59
76
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
})
|
|
77
|
+
const orderBookHandle = getOrderBookHandle(context, removeCrossedLevels, onCrossedLevelRemoved)
|
|
78
|
+
this._orderBook = orderBookHandle.orderBook
|
|
79
|
+
this._ownsOrderBook = orderBookHandle.isOwner
|
|
64
80
|
|
|
65
81
|
// initialize all bids/asks levels to empty ones
|
|
66
82
|
for (let i = 0; i < this._depth; i++) {
|
|
@@ -122,11 +138,18 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
122
138
|
}
|
|
123
139
|
|
|
124
140
|
public _update(bookChange: BookChange) {
|
|
125
|
-
this.
|
|
141
|
+
if (this._ownsOrderBook) {
|
|
142
|
+
this._orderBook.update(bookChange)
|
|
143
|
+
}
|
|
126
144
|
|
|
127
145
|
if (this._grouping !== undefined) {
|
|
128
|
-
|
|
129
|
-
this.
|
|
146
|
+
const updateBothGroupedSides = bookChange.isSnapshot || this._removeCrossedLevels
|
|
147
|
+
if (updateBothGroupedSides || this._groupedSideMayHaveChanged(bookChange.bids, this._groupedBidBoundary, false)) {
|
|
148
|
+
this._groupedBidBoundary = this._updateSideGrouped(this._orderBook.bids(), this._bids, false)
|
|
149
|
+
}
|
|
150
|
+
if (updateBothGroupedSides || this._groupedSideMayHaveChanged(bookChange.asks, this._groupedAskBoundary, true)) {
|
|
151
|
+
this._groupedAskBoundary = this._updateSideGrouped(this._orderBook.asks(), this._asks, true)
|
|
152
|
+
}
|
|
130
153
|
} else {
|
|
131
154
|
this._updatedNotGrouped()
|
|
132
155
|
}
|
|
@@ -134,6 +157,20 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
134
157
|
this._lastUpdateTimestamp = bookChange.timestamp
|
|
135
158
|
}
|
|
136
159
|
|
|
160
|
+
private _groupedSideMayHaveChanged(changes: BookPriceLevel[], boundary: number | undefined, roundUp: boolean) {
|
|
161
|
+
if (changes.length === 0) return false
|
|
162
|
+
if (boundary === undefined) return true
|
|
163
|
+
|
|
164
|
+
for (const level of changes) {
|
|
165
|
+
const groupedPrice = this._getGroupedPrice(level.price, roundUp)
|
|
166
|
+
if (roundUp ? groupedPrice <= boundary : groupedPrice >= boundary) {
|
|
167
|
+
return true
|
|
168
|
+
}
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
return false
|
|
172
|
+
}
|
|
173
|
+
|
|
137
174
|
private _updatedNotGrouped() {
|
|
138
175
|
const bidsIterable = this._orderBook.bids()
|
|
139
176
|
const asksIterable = this._orderBook.asks()
|
|
@@ -143,7 +180,7 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
143
180
|
const newBid = bidLevelResult.done ? emptyBookLevel : bidLevelResult.value
|
|
144
181
|
|
|
145
182
|
if (levelsChanged(this._bids[i], newBid)) {
|
|
146
|
-
this._bids[i] = {
|
|
183
|
+
this._bids[i] = { price: newBid.price, amount: newBid.amount }
|
|
147
184
|
this._bookChanged = true
|
|
148
185
|
}
|
|
149
186
|
|
|
@@ -151,61 +188,56 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
151
188
|
const newAsk = askLevelResult.done ? emptyBookLevel : askLevelResult.value
|
|
152
189
|
|
|
153
190
|
if (levelsChanged(this._asks[i], newAsk)) {
|
|
154
|
-
this._asks[i] = {
|
|
191
|
+
this._asks[i] = { price: newAsk.price, amount: newAsk.amount }
|
|
155
192
|
this._bookChanged = true
|
|
156
193
|
}
|
|
157
194
|
}
|
|
158
195
|
}
|
|
159
196
|
|
|
160
|
-
private
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
return (pricePow - remainder * pow) / pow
|
|
167
|
-
}
|
|
197
|
+
private _getGroupedPrice(price: number, roundUp: boolean) {
|
|
198
|
+
if (this._groupingDecimalPlaces === 0) {
|
|
199
|
+
const remainder = price % this._grouping!
|
|
200
|
+
return price - remainder + (roundUp && remainder > 0 ? this._grouping! : 0)
|
|
201
|
+
}
|
|
168
202
|
|
|
169
|
-
private _getGroupedPriceForAsks = (price: number) => {
|
|
170
203
|
const pow = Math.pow(10, this._groupingDecimalPlaces!)
|
|
171
204
|
const pricePow = price * pow
|
|
172
205
|
const groupPow = this._grouping! * pow
|
|
173
206
|
const remainder = (pricePow % groupPow) / pow
|
|
174
207
|
|
|
175
|
-
return (pricePow - remainder * pow + (remainder > 0 ? groupPow : 0)) / pow
|
|
208
|
+
return (pricePow - remainder * pow + (roundUp && remainder > 0 ? groupPow : 0)) / pow
|
|
176
209
|
}
|
|
177
210
|
|
|
178
211
|
private _updateSideGrouped(
|
|
179
212
|
newLevels: IterableIterator<BookPriceLevel>,
|
|
180
213
|
existingGroupedLevels: Optional<BookPriceLevel>[],
|
|
181
|
-
|
|
214
|
+
roundUp: boolean
|
|
182
215
|
) {
|
|
183
216
|
let currentGroupedPrice: number | undefined = undefined
|
|
184
217
|
let aggAmount = 0
|
|
185
218
|
let currentDepth = 0
|
|
186
219
|
|
|
187
220
|
for (const notGroupedLevel of newLevels) {
|
|
188
|
-
const groupedPrice =
|
|
221
|
+
const groupedPrice = this._getGroupedPrice(notGroupedLevel.price, roundUp)
|
|
189
222
|
|
|
190
223
|
if (currentGroupedPrice == undefined) {
|
|
191
224
|
currentGroupedPrice = groupedPrice
|
|
192
225
|
}
|
|
193
226
|
|
|
194
227
|
if (currentGroupedPrice != groupedPrice) {
|
|
195
|
-
const
|
|
196
|
-
|
|
197
|
-
|
|
198
|
-
|
|
199
|
-
|
|
200
|
-
|
|
201
|
-
existingGroupedLevels[currentDepth] = groupedLevel
|
|
228
|
+
const existingGroupedLevel = existingGroupedLevels[currentDepth]
|
|
229
|
+
if (existingGroupedLevel.amount !== aggAmount || existingGroupedLevel.price !== currentGroupedPrice) {
|
|
230
|
+
existingGroupedLevels[currentDepth] = {
|
|
231
|
+
price: currentGroupedPrice,
|
|
232
|
+
amount: aggAmount
|
|
233
|
+
}
|
|
202
234
|
this._bookChanged = true
|
|
203
235
|
}
|
|
204
236
|
|
|
205
237
|
currentDepth++
|
|
206
238
|
|
|
207
239
|
if (currentDepth === this._depth) {
|
|
208
|
-
|
|
240
|
+
return currentGroupedPrice
|
|
209
241
|
}
|
|
210
242
|
|
|
211
243
|
currentGroupedPrice = groupedPrice
|
|
@@ -216,16 +248,18 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
216
248
|
}
|
|
217
249
|
|
|
218
250
|
if (currentDepth < this._depth && aggAmount > 0) {
|
|
219
|
-
const
|
|
220
|
-
|
|
221
|
-
|
|
222
|
-
|
|
223
|
-
|
|
224
|
-
|
|
225
|
-
existingGroupedLevels[currentDepth] = groupedLevel
|
|
251
|
+
const existingGroupedLevel = existingGroupedLevels[currentDepth]
|
|
252
|
+
if (existingGroupedLevel.amount !== aggAmount || existingGroupedLevel.price !== currentGroupedPrice) {
|
|
253
|
+
existingGroupedLevels[currentDepth] = {
|
|
254
|
+
price: currentGroupedPrice,
|
|
255
|
+
amount: aggAmount
|
|
256
|
+
}
|
|
226
257
|
this._bookChanged = true
|
|
227
258
|
}
|
|
259
|
+
currentDepth++
|
|
228
260
|
}
|
|
261
|
+
|
|
262
|
+
return currentDepth === this._depth ? currentGroupedPrice : undefined
|
|
229
263
|
}
|
|
230
264
|
|
|
231
265
|
public _getSnapshot(bookChange: BookChange) {
|
|
@@ -252,3 +286,32 @@ class BookSnapshotComputable implements Computable<BookSnapshot> {
|
|
|
252
286
|
return Math.floor(timestamp.valueOf() / this._interval)
|
|
253
287
|
}
|
|
254
288
|
}
|
|
289
|
+
|
|
290
|
+
function getOrderBookHandle(
|
|
291
|
+
context: ComputableContext | undefined,
|
|
292
|
+
removeCrossedLevels: boolean | undefined,
|
|
293
|
+
onCrossedLevelRemoved: OnLevelRemovedCB | undefined
|
|
294
|
+
): OrderBookHandle {
|
|
295
|
+
if (context === undefined || onCrossedLevelRemoved !== undefined) {
|
|
296
|
+
return {
|
|
297
|
+
orderBook: new OrderBook({ removeCrossedLevels, onCrossedLevelRemoved }),
|
|
298
|
+
isOwner: true
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
|
|
302
|
+
let sharedOrderBooks = context.get(sharedOrderBooksKey) as Map<boolean, OrderBook> | undefined
|
|
303
|
+
if (sharedOrderBooks === undefined) {
|
|
304
|
+
sharedOrderBooks = new Map()
|
|
305
|
+
context.set(sharedOrderBooksKey, sharedOrderBooks)
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
const key = removeCrossedLevels === true
|
|
309
|
+
const sharedOrderBook = sharedOrderBooks.get(key)
|
|
310
|
+
if (sharedOrderBook !== undefined) {
|
|
311
|
+
return { orderBook: sharedOrderBook, isOwner: false }
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
const orderBook = new OrderBook({ removeCrossedLevels })
|
|
315
|
+
sharedOrderBooks.set(key, orderBook)
|
|
316
|
+
return { orderBook, isOwner: true }
|
|
317
|
+
}
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import { Disconnect, Exchange, NormalizedData } from '../types.ts'
|
|
2
|
+
import { ComputableContext, createWithContext } from './context.ts'
|
|
2
3
|
|
|
3
4
|
export type Computable<T extends NormalizedData> = {
|
|
4
5
|
readonly sourceDataTypes: string[]
|
|
@@ -70,7 +71,10 @@ class Computables {
|
|
|
70
71
|
}
|
|
71
72
|
|
|
72
73
|
if (this._computables[exchange]![id] === undefined) {
|
|
73
|
-
|
|
74
|
+
const context: ComputableContext = new Map()
|
|
75
|
+
this._computables[exchange]![id] = createComputablesMap(
|
|
76
|
+
this._computablesFactories.map((factory) => createWithContext(factory, context))
|
|
77
|
+
)
|
|
74
78
|
}
|
|
75
79
|
|
|
76
80
|
return this._computables[exchange]![id]!
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
export type ComputableContext = Map<symbol, unknown>
|
|
2
|
+
|
|
3
|
+
const contextualFactories = new WeakMap<Function, (context: ComputableContext) => unknown>()
|
|
4
|
+
|
|
5
|
+
export function registerContextualFactory<T>(factory: () => T, contextualFactory: (context: ComputableContext) => T) {
|
|
6
|
+
contextualFactories.set(factory, contextualFactory)
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
export function createWithContext<T>(factory: () => T, context: ComputableContext): T {
|
|
10
|
+
const contextualFactory = contextualFactories.get(factory)
|
|
11
|
+
return contextualFactory === undefined ? factory() : (contextualFactory(context) as T)
|
|
12
|
+
}
|
package/src/consts.ts
CHANGED
|
@@ -186,6 +186,7 @@ const OKEX_FUTURES_CHANNELS = [
|
|
|
186
186
|
'tickers',
|
|
187
187
|
'open-interest',
|
|
188
188
|
'mark-price',
|
|
189
|
+
'funding-rate',
|
|
189
190
|
'price-limit',
|
|
190
191
|
'status',
|
|
191
192
|
'instruments',
|
|
@@ -292,7 +293,7 @@ const FTX_CHANNELS = [
|
|
|
292
293
|
'busy'
|
|
293
294
|
] as const
|
|
294
295
|
|
|
295
|
-
const GEMINI_CHANNELS = ['trade', 'l2_updates', 'auction_open', 'auction_indicative', 'auction_result'] as const
|
|
296
|
+
const GEMINI_CHANNELS = ['trade', 'l2_updates', 'auction_open', 'auction_indicative', 'auction_result', 'depth', 'bookTicker'] as const
|
|
296
297
|
|
|
297
298
|
const BITFLYER_CHANNELS = ['lightning_executions', 'lightning_board_snapshot', 'lightning_board', 'lightning_ticker'] as const
|
|
298
299
|
|
package/src/handy.ts
CHANGED
|
@@ -640,8 +640,10 @@ async function _downloadFile(requestOptions: RequestOptions, url: string, downlo
|
|
|
640
640
|
// based on https://github.com/nodejs/node/issues/28172 - only reliable way to consume response stream and avoiding all the 'gotchas'
|
|
641
641
|
let responseHeaders: Record<string, string> = {}
|
|
642
642
|
await new Promise<void>((resolve, reject) => {
|
|
643
|
-
const
|
|
644
|
-
|
|
643
|
+
const protocol = new URL(url).protocol
|
|
644
|
+
const requestClient = protocol === 'http:' ? http : https
|
|
645
|
+
const req = requestClient
|
|
646
|
+
.get(url, { ...requestOptions, ...(protocol === 'http:' ? { agent: undefined } : {}) }, (res) => {
|
|
645
647
|
const { statusCode } = res
|
|
646
648
|
if (statusCode !== 200) {
|
|
647
649
|
// read the error response text and throw it as an HttpError
|
package/src/instrumentinfo.ts
CHANGED
|
@@ -109,6 +109,8 @@ export type ContractType =
|
|
|
109
109
|
| 'interest_rate_swap'
|
|
110
110
|
| 'repo'
|
|
111
111
|
| 'index'
|
|
112
|
+
| 'linear_xperp'
|
|
113
|
+
| 'inverse_xperp'
|
|
112
114
|
|
|
113
115
|
export type UnderlyingType = 'native' | 'equity' | 'commodity' | 'fixed_income' | 'fx' | 'index' | 'pre_market'
|
|
114
116
|
|
|
@@ -179,13 +181,16 @@ export interface InstrumentInfo {
|
|
|
179
181
|
amountIncrement?: number
|
|
180
182
|
contractMultiplier?: number
|
|
181
183
|
minTradeAmount?: number
|
|
184
|
+
minNotional?: number
|
|
182
185
|
makerFee?: number
|
|
183
186
|
takerFee?: number
|
|
184
187
|
quanto?: boolean
|
|
185
188
|
inverse?: boolean
|
|
186
189
|
settlementCurrency?: string
|
|
187
190
|
underlyingIndex?: string
|
|
191
|
+
underlyingType?: UnderlyingType
|
|
188
192
|
contractType?: ContractType
|
|
193
|
+
expiry?: string
|
|
189
194
|
quoteCurrency?: string
|
|
190
195
|
type?: string
|
|
191
196
|
}[]
|
package/src/mappers/gemini.ts
CHANGED
|
@@ -1,14 +1,18 @@
|
|
|
1
|
-
import { upperCaseSymbols } from '../handy.ts'
|
|
2
|
-
import { BookChange, Trade } from '../types.ts'
|
|
1
|
+
import { asNonZeroNumberOrUndefined, fromMicroSecondsToDate, upperCaseSymbols } from '../handy.ts'
|
|
2
|
+
import { BookChange, BookTicker, Trade } from '../types.ts'
|
|
3
3
|
import { Mapper } from './mapper.ts'
|
|
4
|
-
import { exchangeMappers } from './registry.ts'
|
|
4
|
+
import { exchangeMappers, mapper } from './registry.ts'
|
|
5
5
|
|
|
6
|
-
|
|
6
|
+
const GEMINI_V3_API_SWITCH_DATE = new Date('2026-07-24T00:00:00.000Z')
|
|
7
7
|
|
|
8
8
|
export const geminiMappers = exchangeMappers({
|
|
9
9
|
gemini: {
|
|
10
|
-
trades: () => geminiTradesMapper,
|
|
11
|
-
bookChanges: (
|
|
10
|
+
trades: mapper([{ until: GEMINI_V3_API_SWITCH_DATE, use: () => geminiTradesMapper }, { use: () => new GeminiV3TradesMapper() }]),
|
|
11
|
+
bookChanges: mapper([
|
|
12
|
+
{ until: GEMINI_V3_API_SWITCH_DATE, use: () => geminiBookChangeMapper },
|
|
13
|
+
{ use: () => new GeminiV3BookChangeMapper() }
|
|
14
|
+
]),
|
|
15
|
+
bookTickers: () => new GeminiV3BookTickerMapper()
|
|
12
16
|
}
|
|
13
17
|
})
|
|
14
18
|
|
|
@@ -87,7 +91,7 @@ type GeminiL2Updates = {
|
|
|
87
91
|
type: 'l2_updates'
|
|
88
92
|
symbol: string
|
|
89
93
|
changes: GeminiBookLevel[]
|
|
90
|
-
auction_events
|
|
94
|
+
auction_events?: any[]
|
|
91
95
|
}
|
|
92
96
|
|
|
93
97
|
type GeminiTrade = {
|
|
@@ -97,5 +101,125 @@ type GeminiTrade = {
|
|
|
97
101
|
timestamp: number
|
|
98
102
|
price: string
|
|
99
103
|
quantity: string
|
|
100
|
-
side: 'sell' | 'buy'
|
|
104
|
+
side: 'sell' | 'buy' | 'block'
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
class GeminiV3TradesMapper implements Mapper<'gemini', Trade> {
|
|
108
|
+
canHandle(message: GeminiV3Trade | GeminiV3BookTicker | GeminiV3DepthUpdate) {
|
|
109
|
+
return 't' in message
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
getFilters(symbols?: string[]) {
|
|
113
|
+
return [{ channel: 'trade', symbols: upperCaseSymbols(symbols) } as const]
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
*map(message: GeminiV3Trade, localTimestamp: Date): IterableIterator<Trade> {
|
|
117
|
+
yield {
|
|
118
|
+
type: 'trade',
|
|
119
|
+
symbol: message.s.toUpperCase(),
|
|
120
|
+
exchange: 'gemini',
|
|
121
|
+
id: message.t.toString(),
|
|
122
|
+
price: Number(message.p),
|
|
123
|
+
amount: Number(message.q),
|
|
124
|
+
side: message.m ? 'sell' : 'buy',
|
|
125
|
+
timestamp: fromMicroSecondsToDate(Math.floor(message.E / 1000)),
|
|
126
|
+
localTimestamp
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
class GeminiV3BookChangeMapper implements Mapper<'gemini', BookChange> {
|
|
132
|
+
private readonly symbolsWithSnapshot = new Set<string>()
|
|
133
|
+
|
|
134
|
+
canHandle(message: GeminiV3DepthUpdate) {
|
|
135
|
+
return message.e === 'depthUpdate'
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
getFilters(symbols?: string[]) {
|
|
139
|
+
return [{ channel: 'depth', symbols: upperCaseSymbols(symbols) } as const]
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
*map(message: GeminiV3DepthUpdate, localTimestamp: Date): IterableIterator<BookChange> {
|
|
143
|
+
yield {
|
|
144
|
+
type: 'book_change',
|
|
145
|
+
symbol: message.s.toUpperCase(),
|
|
146
|
+
exchange: 'gemini',
|
|
147
|
+
isSnapshot: this.isSnapshot(message.s),
|
|
148
|
+
bids: message.b.map(this.mapBookLevel),
|
|
149
|
+
asks: message.a.map(this.mapBookLevel),
|
|
150
|
+
timestamp: fromMicroSecondsToDate(Math.floor(message.E / 1000)),
|
|
151
|
+
localTimestamp
|
|
152
|
+
}
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
private isSnapshot(symbol: string) {
|
|
156
|
+
// With snapshot=-1 Gemini sends a full book as the first depthUpdate per symbol on a new connection.
|
|
157
|
+
// A historical replay started mid-connection cannot distinguish that snapshot from a delta using the payload alone.
|
|
158
|
+
const isSnapshot = this.symbolsWithSnapshot.has(symbol) === false
|
|
159
|
+
this.symbolsWithSnapshot.add(symbol)
|
|
160
|
+
return isSnapshot
|
|
161
|
+
}
|
|
162
|
+
|
|
163
|
+
private mapBookLevel([price, amount]: GeminiV3BookLevel) {
|
|
164
|
+
return { price: Number(price), amount: Number(amount) }
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
class GeminiV3BookTickerMapper implements Mapper<'gemini', BookTicker> {
|
|
169
|
+
canHandle(message: GeminiV3Trade | GeminiV3BookTicker | GeminiV3DepthUpdate) {
|
|
170
|
+
return 'B' in message
|
|
171
|
+
}
|
|
172
|
+
|
|
173
|
+
getFilters(symbols?: string[]) {
|
|
174
|
+
return [{ channel: 'bookTicker', symbols: upperCaseSymbols(symbols) } as const]
|
|
175
|
+
}
|
|
176
|
+
|
|
177
|
+
*map(message: GeminiV3BookTicker, localTimestamp: Date): IterableIterator<BookTicker> {
|
|
178
|
+
yield {
|
|
179
|
+
type: 'book_ticker',
|
|
180
|
+
symbol: message.s.toUpperCase(),
|
|
181
|
+
exchange: 'gemini',
|
|
182
|
+
askAmount: asNonZeroNumberOrUndefined(message.A),
|
|
183
|
+
askPrice: asNonZeroNumberOrUndefined(message.a),
|
|
184
|
+
bidAmount: asNonZeroNumberOrUndefined(message.B),
|
|
185
|
+
bidPrice: asNonZeroNumberOrUndefined(message.b),
|
|
186
|
+
timestamp: fromMicroSecondsToDate(Math.floor(message.E / 1000)),
|
|
187
|
+
localTimestamp
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/** @see https://developer.gemini.com/websocket/streams#trade-stream */
|
|
193
|
+
type GeminiV3Trade = {
|
|
194
|
+
E: number
|
|
195
|
+
s: string
|
|
196
|
+
t: number
|
|
197
|
+
p: string
|
|
198
|
+
q: string
|
|
199
|
+
m: boolean
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/** @see https://developer.gemini.com/websocket/streams#l2-differential-depth-streams */
|
|
203
|
+
type GeminiV3DepthUpdate = {
|
|
204
|
+
e: 'depthUpdate'
|
|
205
|
+
E: number
|
|
206
|
+
s: string
|
|
207
|
+
U: number
|
|
208
|
+
u: number
|
|
209
|
+
b: GeminiV3BookLevel[]
|
|
210
|
+
a: GeminiV3BookLevel[]
|
|
211
|
+
}
|
|
212
|
+
type GeminiV3BookLevel = [string, string]
|
|
213
|
+
|
|
214
|
+
/** @see https://developer.gemini.com/websocket/streams#book-ticker */
|
|
215
|
+
type GeminiV3BookTicker = {
|
|
216
|
+
u: number
|
|
217
|
+
E: number
|
|
218
|
+
s: string
|
|
219
|
+
b: string
|
|
220
|
+
B: string
|
|
221
|
+
a: string
|
|
222
|
+
A: string
|
|
223
|
+
c?: string
|
|
224
|
+
C?: string
|
|
101
225
|
}
|
package/src/mappers/okex.ts
CHANGED
|
@@ -9,6 +9,7 @@ const OKEX_V5_API_SWITCH_DATE = new Date('2021-12-23T00:00:00.000Z')
|
|
|
9
9
|
const OKEX_V5_TBT_BOOK_TICKER_RELEASE_DATE = new Date('2022-05-06T00:00:00.000Z')
|
|
10
10
|
const OKX_PUBLIC_BOOKS_CHANNEL_START_DATE = new Date('2023-02-25T00:00:00.000Z')
|
|
11
11
|
const OKX_PUBLIC_BOOKS_CHANNEL_END_DATE = new Date('2023-03-09T00:00:00.000Z')
|
|
12
|
+
const OKEX_USDC_INDEX_SWITCH_DATE = new Date('2023-04-10T08:40:00.000Z')
|
|
12
13
|
const OKEX_PUBLIC_BOOKS_SWITCH_DATE = new Date('2026-05-21T00:00:00.000Z')
|
|
13
14
|
const OKEX_SPOT_DEPTH_L2_TBT_SWITCH_DATE = new Date('2020-04-10T00:00:00.000Z')
|
|
14
15
|
const OKEX_FUTURES_DEPTH_L2_TBT_SWITCH_DATE = new Date('2019-12-05T00:00:00.000Z')
|
|
@@ -33,7 +34,11 @@ export const okexMappers = exchangeMappers({
|
|
|
33
34
|
}),
|
|
34
35
|
derivativeTickers: mapper([
|
|
35
36
|
{ until: OKEX_V5_API_SWITCH_DATE, use: () => new OkexDerivativeTickerMapper('okex-futures') },
|
|
36
|
-
{
|
|
37
|
+
{
|
|
38
|
+
until: OKEX_USDC_INDEX_SWITCH_DATE,
|
|
39
|
+
use: () => new OkexV5DerivativeTickerMapper('okex-futures', { useLegacyUSDCIndex: true })
|
|
40
|
+
},
|
|
41
|
+
{ use: () => new OkexV5DerivativeTickerMapper('okex-futures', { useLegacyUSDCIndex: false }) }
|
|
37
42
|
]),
|
|
38
43
|
liquidations: mapper([
|
|
39
44
|
{ until: OKEX_V5_API_SWITCH_DATE, use: () => new OkexLiquidationsMapper('okex-futures', { market: 'futures' }) },
|
|
@@ -46,7 +51,11 @@ export const okexMappers = exchangeMappers({
|
|
|
46
51
|
bookChanges: okexBookChangesMapper('okex-swap', { market: 'swap', depthL2TbtSwitchDate: OKEX_SWAP_DEPTH_L2_TBT_SWITCH_DATE }),
|
|
47
52
|
derivativeTickers: mapper([
|
|
48
53
|
{ until: OKEX_V5_API_SWITCH_DATE, use: () => new OkexDerivativeTickerMapper('okex-swap') },
|
|
49
|
-
{
|
|
54
|
+
{
|
|
55
|
+
until: OKEX_USDC_INDEX_SWITCH_DATE,
|
|
56
|
+
use: () => new OkexV5DerivativeTickerMapper('okex-swap', { useLegacyUSDCIndex: true })
|
|
57
|
+
},
|
|
58
|
+
{ use: () => new OkexV5DerivativeTickerMapper('okex-swap', { useLegacyUSDCIndex: false }) }
|
|
50
59
|
]),
|
|
51
60
|
liquidations: mapper([
|
|
52
61
|
{ until: OKEX_V5_API_SWITCH_DATE, use: () => new OkexLiquidationsMapper('okex-swap', { market: 'swap' }) },
|
|
@@ -366,11 +375,14 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
366
375
|
private readonly pendingTickerInfoHelper = new PendingTickerInfoHelper()
|
|
367
376
|
private readonly _indexPrices = new Map<string, number>()
|
|
368
377
|
|
|
369
|
-
private _futuresChannels = ['tickers', 'open-interest', 'mark-price', 'index-tickers'] as const
|
|
378
|
+
private _futuresChannels = ['tickers', 'open-interest', 'mark-price', 'index-tickers', 'funding-rate'] as const
|
|
370
379
|
|
|
371
380
|
private _swapChannels = ['tickers', 'open-interest', 'mark-price', 'index-tickers', 'funding-rate'] as const
|
|
372
381
|
|
|
373
|
-
constructor(
|
|
382
|
+
constructor(
|
|
383
|
+
private readonly _exchange: Exchange,
|
|
384
|
+
private readonly _options: { useLegacyUSDCIndex: boolean }
|
|
385
|
+
) {}
|
|
374
386
|
|
|
375
387
|
canHandle(message: any) {
|
|
376
388
|
const channels = this._exchange === 'okex-futures' ? this._futuresChannels : this._swapChannels
|
|
@@ -388,14 +400,7 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
388
400
|
const channels = this._exchange === 'okex-futures' ? this._futuresChannels : this._swapChannels
|
|
389
401
|
return channels.map((channel) => {
|
|
390
402
|
if (channel === 'index-tickers') {
|
|
391
|
-
const indexes =
|
|
392
|
-
symbols !== undefined
|
|
393
|
-
? symbols.map((s) => {
|
|
394
|
-
const symbolParts = s.split('-')
|
|
395
|
-
const quotePart = symbolParts[1] === 'USDC' ? 'USD' : symbolParts[1]
|
|
396
|
-
return `${symbolParts[0]}-${quotePart}`
|
|
397
|
-
})
|
|
398
|
-
: undefined
|
|
403
|
+
const indexes = symbols !== undefined ? symbols.map((symbol) => this._getIndexSymbol(symbol)) : undefined
|
|
399
404
|
|
|
400
405
|
return {
|
|
401
406
|
channel,
|
|
@@ -429,11 +434,7 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
429
434
|
|
|
430
435
|
for (const dataMessage of message.data) {
|
|
431
436
|
const pendingTickerInfo = this.pendingTickerInfoHelper.getPendingTickerInfo(dataMessage.instId, this._exchange)
|
|
432
|
-
const
|
|
433
|
-
const quotePart = symbolParts[1] === 'USDC' ? 'USD' : symbolParts[1]
|
|
434
|
-
|
|
435
|
-
const indexSymbol = `${symbolParts[0]}-${quotePart}`
|
|
436
|
-
|
|
437
|
+
const indexSymbol = this._getIndexSymbol(dataMessage.instId)
|
|
437
438
|
const indexPrice = this._indexPrices.get(indexSymbol)
|
|
438
439
|
|
|
439
440
|
if (indexPrice !== undefined) {
|
|
@@ -473,6 +474,10 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
473
474
|
if (fundingRateMessage.nextFundingRate !== undefined && fundingRateMessage.nextFundingRate !== '') {
|
|
474
475
|
pendingTickerInfo.updatePredictedFundingRate(Number(fundingRateMessage.nextFundingRate))
|
|
475
476
|
}
|
|
477
|
+
|
|
478
|
+
if (fundingRateMessage.ts !== undefined) {
|
|
479
|
+
pendingTickerInfo.updateTimestamp(new Date(Number(fundingRateMessage.ts)))
|
|
480
|
+
}
|
|
476
481
|
}
|
|
477
482
|
|
|
478
483
|
if (message.arg.channel === 'tickers') {
|
|
@@ -491,6 +496,13 @@ class OkexV5DerivativeTickerMapper implements Mapper<'okex-futures' | 'okex-swap
|
|
|
491
496
|
}
|
|
492
497
|
}
|
|
493
498
|
}
|
|
499
|
+
|
|
500
|
+
private _getIndexSymbol(instrumentId: string) {
|
|
501
|
+
const symbolParts = instrumentId.split('-')
|
|
502
|
+
const quoteCurrency = symbolParts[1].split('_')[0]
|
|
503
|
+
const indexQuoteCurrency = quoteCurrency === 'USDC' && this._options.useLegacyUSDCIndex ? 'USD' : quoteCurrency
|
|
504
|
+
return `${symbolParts[0]}-${indexQuoteCurrency}`
|
|
505
|
+
}
|
|
494
506
|
}
|
|
495
507
|
|
|
496
508
|
class OkexV5LiquidationsMapper implements Mapper<OKEX_EXCHANGES, Liquidation> {
|
|
@@ -808,7 +820,16 @@ type OkexV5IndexTickerMessage = {
|
|
|
808
820
|
|
|
809
821
|
type OkexV5FundingRateMessage = {
|
|
810
822
|
arg: { channel: 'funding-rate'; instId: string }
|
|
811
|
-
data: [
|
|
823
|
+
data: [
|
|
824
|
+
{
|
|
825
|
+
fundingRate: '0.00048105' | undefined
|
|
826
|
+
fundingTime: '1640131200000'
|
|
827
|
+
instId: string
|
|
828
|
+
instType: 'SWAP' | 'FUTURES'
|
|
829
|
+
nextFundingRate: ''
|
|
830
|
+
ts?: '1640131200000'
|
|
831
|
+
}
|
|
832
|
+
]
|
|
812
833
|
}
|
|
813
834
|
|
|
814
835
|
type OkexV5LiquidationMessage = {
|
package/src/orderbook.ts
CHANGED
|
@@ -1,6 +1,5 @@
|
|
|
1
|
-
import {
|
|
2
|
-
import
|
|
3
|
-
import { BookChange, BookPriceLevel, Writeable } from './types.ts'
|
|
1
|
+
import { PriceLevelTree } from './priceleveltree.ts'
|
|
2
|
+
import { BookChange, BookPriceLevel } from './types.ts'
|
|
4
3
|
|
|
5
4
|
export type OnLevelRemovedCB = (
|
|
6
5
|
bookChange: BookChange,
|
|
@@ -11,8 +10,8 @@ export type OnLevelRemovedCB = (
|
|
|
11
10
|
) => void
|
|
12
11
|
|
|
13
12
|
export class OrderBook {
|
|
14
|
-
private readonly _bids = new
|
|
15
|
-
private readonly _asks = new
|
|
13
|
+
private readonly _bids = new PriceLevelTree(-1)
|
|
14
|
+
private readonly _asks = new PriceLevelTree(1)
|
|
16
15
|
private readonly _removeCrossedLevels: boolean | undefined
|
|
17
16
|
private readonly _onCrossedLevelRemoved: OnLevelRemovedCB | undefined
|
|
18
17
|
|
|
@@ -147,18 +146,13 @@ export class OrderBook {
|
|
|
147
146
|
}
|
|
148
147
|
}
|
|
149
148
|
|
|
150
|
-
function applyPriceLevelChanges(tree:
|
|
149
|
+
function applyPriceLevelChanges(tree: PriceLevelTree, priceLevelChanges: BookPriceLevel[]) {
|
|
151
150
|
for (const priceLevel of priceLevelChanges) {
|
|
152
|
-
|
|
153
|
-
const nodeExists = node !== null
|
|
154
|
-
const levelShouldBeRemoved = priceLevel.amount === 0
|
|
155
|
-
|
|
156
|
-
if (nodeExists && levelShouldBeRemoved) {
|
|
151
|
+
if (priceLevel.amount === 0) {
|
|
157
152
|
tree.remove(priceLevel)
|
|
158
|
-
|
|
159
|
-
node.amount = priceLevel.amount
|
|
160
|
-
} else if (levelShouldBeRemoved === false) {
|
|
161
|
-
tree.insert({ ...priceLevel })
|
|
153
|
+
continue
|
|
162
154
|
}
|
|
155
|
+
|
|
156
|
+
tree.upsert(priceLevel)
|
|
163
157
|
}
|
|
164
158
|
}
|