|
| 1 | +/* |
| 2 | + * Copyright 2019 namjug-kim |
| 3 | + * |
| 4 | + * LINE Corporation licenses this file to you under the Apache License, |
| 5 | + * version 2.0 (the "License"); you may not use this file except in compliance |
| 6 | + * with the License. You may obtain a copy of the License at: |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 12 | + * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 13 | + * License for the specific language governing permissions and limitations |
| 14 | + * under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package com.njkim.reactivecrypto.binance |
| 18 | + |
| 19 | +import com.fasterxml.jackson.databind.ObjectMapper |
| 20 | +import com.fasterxml.jackson.module.kotlin.readValue |
| 21 | +import com.njkim.reactivecrypto.binance.model.BinanceOrderBook |
| 22 | +import com.njkim.reactivecrypto.binance.model.BinanceResponseWrapper |
| 23 | +import com.njkim.reactivecrypto.binance.model.BinanceTickData |
| 24 | +import com.njkim.reactivecrypto.core.websocket.AbstractExchangeWebsocketClient |
| 25 | +import com.njkim.reactivecrypto.core.ExchangeJsonObjectMapper |
| 26 | +import com.njkim.reactivecrypto.core.common.model.ExchangeVendor |
| 27 | +import com.njkim.reactivecrypto.core.common.model.currency.CurrencyPair |
| 28 | +import com.njkim.reactivecrypto.core.common.model.order.OrderBook |
| 29 | +import com.njkim.reactivecrypto.core.common.model.order.TickData |
| 30 | +import com.njkim.reactivecrypto.core.common.model.order.TradeSideType.BUY |
| 31 | +import com.njkim.reactivecrypto.core.common.model.order.TradeSideType.SELL |
| 32 | +import com.njkim.reactivecrypto.core.common.util.toEpochMilli |
| 33 | +import mu.KotlinLogging |
| 34 | +import reactor.core.publisher.Flux |
| 35 | +import reactor.netty.http.client.HttpClient |
| 36 | +import java.time.ZonedDateTime |
| 37 | +import java.util.stream.Collectors |
| 38 | + |
| 39 | +class BinancePrivateWebsocketClient : AbstractExchangeWebsocketClient() { |
| 40 | + private val log = KotlinLogging.logger {} |
| 41 | + |
| 42 | + private val baseUri = "wss://stream.binance.com:9443" |
| 43 | + |
| 44 | + private val objectMapper: ObjectMapper = createJsonObjectMapper().objectMapper() |
| 45 | + |
| 46 | + override fun createJsonObjectMapper(): ExchangeJsonObjectMapper { |
| 47 | + return BinanceJsonObjectMapper() |
| 48 | + } |
| 49 | + |
| 50 | + override fun createTradeWebsocket(subscribeTargets: List<CurrencyPair>): Flux<TickData> { |
| 51 | + val streams = subscribeTargets.stream() |
| 52 | + .map { "${it.baseCurrency}${it.quoteCurrency}" } |
| 53 | + .map { it.toLowerCase() + "@trade" } |
| 54 | + .collect(Collectors.joining("/")) |
| 55 | + |
| 56 | + return HttpClient.create() |
| 57 | + .wiretap(log.isDebugEnabled) |
| 58 | + .websocket() |
| 59 | + .uri("$baseUri/stream?streams=$streams") |
| 60 | + .handle { inbound, _ -> inbound.receive().asString() } |
| 61 | + .map { objectMapper.readValue<BinanceResponseWrapper<BinanceTickData>>(it) } |
| 62 | + .map { it.data } |
| 63 | + .map { binanceTradeRawData -> |
| 64 | + TickData( |
| 65 | + binanceTradeRawData.tradeId.toString() + binanceTradeRawData.currencyPair + binanceTradeRawData.eventTime.toEpochMilli(), |
| 66 | + binanceTradeRawData.eventTime, |
| 67 | + binanceTradeRawData.price, |
| 68 | + binanceTradeRawData.quantity, |
| 69 | + binanceTradeRawData.currencyPair, |
| 70 | + ExchangeVendor.BINANCE, |
| 71 | + if (binanceTradeRawData.isMarketMaker) SELL else BUY |
| 72 | + ) |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + override fun createDepthSnapshot(subscribeTargets: List<CurrencyPair>): Flux<OrderBook> { |
| 77 | + val streams = subscribeTargets.stream() |
| 78 | + .map { "${it.baseCurrency}${it.quoteCurrency}" } |
| 79 | + .map { it.toLowerCase() + "@depth20" } |
| 80 | + .collect(Collectors.joining("/")) |
| 81 | + |
| 82 | + return HttpClient.create() |
| 83 | + .wiretap(log.isDebugEnabled) |
| 84 | + .websocket() |
| 85 | + .uri("$baseUri/stream?streams=$streams") |
| 86 | + .handle { inbound, _ -> inbound.receive().asString() } |
| 87 | + .map { objectMapper.readValue<BinanceResponseWrapper<BinanceOrderBook>>(it) } |
| 88 | + .map { |
| 89 | + OrderBook( |
| 90 | + "${it.data.lastUpdateId}", |
| 91 | + it.getCurrencyPair(), |
| 92 | + ZonedDateTime.now(), |
| 93 | + ExchangeVendor.BINANCE, |
| 94 | + it.data.getBids(), |
| 95 | + it.data.getAsks() |
| 96 | + ) |
| 97 | + } |
| 98 | + } |
| 99 | +} |
0 commit comments