|
1 | | -import { upperCaseSymbols } from '../handy.ts' |
2 | | -import { BookChange, BookTicker, DerivativeTicker, Exchange, Trade } from '../types.ts' |
| 1 | +import { asNumberIfValid, upperCaseSymbols } from '../handy.ts' |
| 2 | +import { BookChange, BookTicker, DerivativeTicker, Exchange, Liquidation, Trade } from '../types.ts' |
3 | 3 | import { Mapper, PendingTickerInfoHelper } from './mapper.ts' |
4 | 4 |
|
5 | 5 | export class BitgetTradesMapper implements Mapper<'bitget' | 'bitget-futures', Trade> { |
@@ -155,6 +155,182 @@ export class BitgetDerivativeTickerMapper implements Mapper<'bitget-futures', De |
155 | 155 | } |
156 | 156 | } |
157 | 157 |
|
| 158 | +export class BitgetV3TradesMapper implements Mapper<'bitget' | 'bitget-futures', Trade> { |
| 159 | + constructor(private readonly _exchange: Exchange) {} |
| 160 | + |
| 161 | + canHandle(message: BitgetV3TradeMessage) { |
| 162 | + return message.arg.topic === 'publicTrade' && message.action === 'update' |
| 163 | + } |
| 164 | + |
| 165 | + getFilters(symbols?: string[]) { |
| 166 | + symbols = upperCaseSymbols(symbols) |
| 167 | + |
| 168 | + return [ |
| 169 | + { |
| 170 | + channel: 'publicTrade', |
| 171 | + symbols |
| 172 | + } as const |
| 173 | + ] |
| 174 | + } |
| 175 | + |
| 176 | + *map(message: BitgetV3TradeMessage, localTimestamp: Date): IterableIterator<Trade> { |
| 177 | + for (const trade of message.data) { |
| 178 | + yield { |
| 179 | + type: 'trade', |
| 180 | + symbol: message.arg.symbol, |
| 181 | + exchange: this._exchange, |
| 182 | + id: trade.i, |
| 183 | + price: Number(trade.p), |
| 184 | + amount: Number(trade.v), |
| 185 | + side: trade.S === 'buy' ? 'buy' : 'sell', |
| 186 | + timestamp: new Date(Number(trade.T)), |
| 187 | + localTimestamp |
| 188 | + } |
| 189 | + } |
| 190 | + } |
| 191 | +} |
| 192 | + |
| 193 | +export class BitgetV3BookChangeMapper implements Mapper<'bitget' | 'bitget-futures', BookChange> { |
| 194 | + constructor(private readonly _exchange: Exchange) {} |
| 195 | + |
| 196 | + canHandle(message: BitgetV3OrderbookMessage) { |
| 197 | + return message.arg.topic === 'books' && (message.action === 'snapshot' || message.action === 'update') |
| 198 | + } |
| 199 | + |
| 200 | + getFilters(symbols?: string[]) { |
| 201 | + symbols = upperCaseSymbols(symbols) |
| 202 | + |
| 203 | + return [ |
| 204 | + { |
| 205 | + channel: 'books', |
| 206 | + symbols |
| 207 | + } as const |
| 208 | + ] |
| 209 | + } |
| 210 | + |
| 211 | + *map(message: BitgetV3OrderbookMessage, localTimestamp: Date): IterableIterator<BookChange> { |
| 212 | + for (const orderbookData of message.data) { |
| 213 | + yield { |
| 214 | + type: 'book_change', |
| 215 | + symbol: message.arg.symbol, |
| 216 | + exchange: this._exchange, |
| 217 | + isSnapshot: message.action === 'snapshot', |
| 218 | + bids: orderbookData.b.map(mapPriceLevel), |
| 219 | + asks: orderbookData.a.map(mapPriceLevel), |
| 220 | + timestamp: new Date(Number(orderbookData.ts)), |
| 221 | + localTimestamp |
| 222 | + } |
| 223 | + } |
| 224 | + } |
| 225 | +} |
| 226 | + |
| 227 | +export class BitgetV3BookTickerMapper implements Mapper<'bitget' | 'bitget-futures', BookTicker> { |
| 228 | + constructor(private readonly _exchange: Exchange) {} |
| 229 | + |
| 230 | + canHandle(message: BitgetV3BBoMessage) { |
| 231 | + return message.arg.topic === 'books1' && message.action === 'snapshot' |
| 232 | + } |
| 233 | + |
| 234 | + getFilters(symbols?: string[]) { |
| 235 | + symbols = upperCaseSymbols(symbols) |
| 236 | + |
| 237 | + return [ |
| 238 | + { |
| 239 | + channel: 'books1', |
| 240 | + symbols |
| 241 | + } as const |
| 242 | + ] |
| 243 | + } |
| 244 | + |
| 245 | + *map(message: BitgetV3BBoMessage, localTimestamp: Date): IterableIterator<BookTicker> { |
| 246 | + for (const bboMessage of message.data) { |
| 247 | + yield { |
| 248 | + type: 'book_ticker', |
| 249 | + symbol: message.arg.symbol, |
| 250 | + exchange: this._exchange, |
| 251 | + askAmount: bboMessage.a[0] ? asNumberIfValid(bboMessage.a[0][1]) : undefined, |
| 252 | + askPrice: bboMessage.a[0] ? asNumberIfValid(bboMessage.a[0][0]) : undefined, |
| 253 | + bidPrice: bboMessage.b[0] ? asNumberIfValid(bboMessage.b[0][0]) : undefined, |
| 254 | + bidAmount: bboMessage.b[0] ? asNumberIfValid(bboMessage.b[0][1]) : undefined, |
| 255 | + timestamp: new Date(Number(bboMessage.ts)), |
| 256 | + localTimestamp |
| 257 | + } |
| 258 | + } |
| 259 | + } |
| 260 | +} |
| 261 | + |
| 262 | +export class BitgetV3DerivativeTickerMapper implements Mapper<'bitget-futures', DerivativeTicker> { |
| 263 | + private readonly pendingTickerInfoHelper = new PendingTickerInfoHelper() |
| 264 | + |
| 265 | + canHandle(message: BitgetV3TickerMessage) { |
| 266 | + return message.arg.topic === 'ticker' && (message.action === 'snapshot' || message.action === 'update') |
| 267 | + } |
| 268 | + |
| 269 | + getFilters(symbols?: string[]) { |
| 270 | + symbols = upperCaseSymbols(symbols) |
| 271 | + |
| 272 | + return [ |
| 273 | + { |
| 274 | + channel: 'ticker', |
| 275 | + symbols |
| 276 | + } as const |
| 277 | + ] |
| 278 | + } |
| 279 | + |
| 280 | + *map(message: BitgetV3TickerMessage, localTimestamp: Date): IterableIterator<DerivativeTicker> { |
| 281 | + for (const tickerMessage of message.data) { |
| 282 | + const pendingTickerInfo = this.pendingTickerInfoHelper.getPendingTickerInfo(message.arg.symbol, 'bitget-futures') |
| 283 | + |
| 284 | + pendingTickerInfo.updateIndexPrice(Number(tickerMessage.indexPrice)) |
| 285 | + pendingTickerInfo.updateMarkPrice(Number(tickerMessage.markPrice)) |
| 286 | + pendingTickerInfo.updateOpenInterest(Number(tickerMessage.openInterest)) |
| 287 | + pendingTickerInfo.updateLastPrice(Number(tickerMessage.lastPrice)) |
| 288 | + pendingTickerInfo.updateTimestamp(new Date(Number(message.ts))) |
| 289 | + |
| 290 | + if (tickerMessage.nextFundingTime !== '' && tickerMessage.nextFundingTime !== '0') { |
| 291 | + pendingTickerInfo.updateFundingTimestamp(new Date(Number(tickerMessage.nextFundingTime))) |
| 292 | + pendingTickerInfo.updateFundingRate(Number(tickerMessage.fundingRate)) |
| 293 | + } |
| 294 | + |
| 295 | + if (pendingTickerInfo.hasChanged()) { |
| 296 | + yield pendingTickerInfo.getSnapshot(localTimestamp) |
| 297 | + } |
| 298 | + } |
| 299 | + } |
| 300 | +} |
| 301 | + |
| 302 | +export class BitgetV3LiquidationsMapper implements Mapper<'bitget-futures', Liquidation> { |
| 303 | + canHandle(message: BitgetV3LiquidationMessage) { |
| 304 | + return message.arg.topic === 'liquidation' && message.action === 'update' |
| 305 | + } |
| 306 | + |
| 307 | + getFilters() { |
| 308 | + return [ |
| 309 | + { |
| 310 | + channel: 'liquidation', |
| 311 | + symbols: undefined |
| 312 | + } as const |
| 313 | + ] |
| 314 | + } |
| 315 | + |
| 316 | + *map(message: BitgetV3LiquidationMessage, localTimestamp: Date): IterableIterator<Liquidation> { |
| 317 | + for (const liquidation of message.data) { |
| 318 | + yield { |
| 319 | + type: 'liquidation', |
| 320 | + symbol: liquidation.symbol, |
| 321 | + exchange: 'bitget-futures', |
| 322 | + id: undefined, |
| 323 | + price: Number(liquidation.price), |
| 324 | + amount: Number(liquidation.amount), |
| 325 | + // Bitget side is position side, normalized side is the liquidated aggressor side. |
| 326 | + side: liquidation.side === 'buy' ? 'sell' : 'buy', |
| 327 | + timestamp: new Date(Number(liquidation.ts)), |
| 328 | + localTimestamp |
| 329 | + } |
| 330 | + } |
| 331 | + } |
| 332 | +} |
| 333 | + |
158 | 334 | type BitgetTradeMessage = { |
159 | 335 | action: 'update' |
160 | 336 | arg: { instType: 'SPOT'; channel: 'trade'; instId: 'OPUSDT' } |
@@ -214,3 +390,62 @@ type BitgetTickerMessage = { |
214 | 390 | ] |
215 | 391 | ts: 1730332823220 |
216 | 392 | } |
| 393 | + |
| 394 | +type BitgetV3TradeMessage = { |
| 395 | + action: 'snapshot' | 'update' |
| 396 | + arg: { instType: string; topic: 'publicTrade'; symbol: string } |
| 397 | + data: { i: string; p: string; v: string; S: 'buy' | 'sell'; T: string; L: string; isRPI?: string }[] |
| 398 | + ts: number |
| 399 | +} |
| 400 | + |
| 401 | +type BitgetV3BookLevel = [string, string] |
| 402 | + |
| 403 | +type BitgetV3OrderbookMessage = { |
| 404 | + action: 'snapshot' | 'update' |
| 405 | + arg: { instType: string; topic: 'books'; symbol: string } |
| 406 | + data: { a: BitgetV3BookLevel[]; b: BitgetV3BookLevel[]; checksum: number; seq: number; pseq: number; ts: string }[] |
| 407 | + ts: number |
| 408 | +} |
| 409 | + |
| 410 | +type BitgetV3BBoMessage = { |
| 411 | + action: 'snapshot' |
| 412 | + arg: { instType: string; topic: 'books1'; symbol: string } |
| 413 | + data: { a: BitgetV3BookLevel[]; b: BitgetV3BookLevel[]; checksum: number; seq: number; pseq: number; ts: string }[] |
| 414 | + ts: number |
| 415 | +} |
| 416 | + |
| 417 | +type BitgetV3TickerMessage = { |
| 418 | + action: 'snapshot' | 'update' |
| 419 | + arg: { instType: string; topic: 'ticker'; symbol: string } |
| 420 | + data: [ |
| 421 | + { |
| 422 | + highPrice24h: string |
| 423 | + lowPrice24h: string |
| 424 | + openPrice24h: string |
| 425 | + lastPrice: string |
| 426 | + turnover24h: string |
| 427 | + volume24h: string |
| 428 | + bid1Price: string |
| 429 | + ask1Price: string |
| 430 | + bid1Size: string |
| 431 | + ask1Size: string |
| 432 | + price24hPcnt: string |
| 433 | + indexPrice: string |
| 434 | + markPrice: string |
| 435 | + fundingRate: string |
| 436 | + openInterest: string |
| 437 | + deliveryTime: string |
| 438 | + deliveryStartTime: string |
| 439 | + deliveryStatus: string |
| 440 | + nextFundingTime: string |
| 441 | + } |
| 442 | + ] |
| 443 | + ts: number |
| 444 | +} |
| 445 | + |
| 446 | +type BitgetV3LiquidationMessage = { |
| 447 | + action: 'update' |
| 448 | + arg: { instType: string; topic: 'liquidation' } |
| 449 | + data: { symbol: string; side: 'buy' | 'sell'; price: string; amount: string; ts: string }[] |
| 450 | + ts: number |
| 451 | +} |
0 commit comments