-
Notifications
You must be signed in to change notification settings - Fork 28
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: add new exchange 'Huobi Global'
- Loading branch information
1 parent
61bc858
commit e052926
Showing
21 changed files
with
624 additions
and
168 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* Copyright 2019 namjug-kim | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
apply plugin: 'kotlin' | ||
apply plugin: 'org.jetbrains.kotlin.jvm' | ||
|
||
version '1.0-SNAPSHOT' | ||
|
||
dependencies { | ||
compile "org.jetbrains.kotlin:kotlin-stdlib-jdk8" | ||
|
||
compile project(':reactive-crypto-core') | ||
} | ||
|
||
compileKotlin { | ||
kotlinOptions.jvmTarget = "1.8" | ||
} | ||
compileTestKotlin { | ||
kotlinOptions.jvmTarget = "1.8" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
173 changes: 173 additions & 0 deletions
173
...global/src/main/kotlin/com/njkim/reactivecrypto/huobiglobal/HuobiGlobalWebsocketClient.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,173 @@ | ||
/* | ||
* Copyright 2019 namjug-kim | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.njkim.reactivecrypto.huobiglobal | ||
|
||
import com.fasterxml.jackson.databind.ObjectMapper | ||
import com.fasterxml.jackson.module.kotlin.readValue | ||
import com.njkim.reactivecrypto.core.ExchangeJsonObjectMapper | ||
import com.njkim.reactivecrypto.core.common.model.ExchangeVendor | ||
import com.njkim.reactivecrypto.core.common.model.currency.CurrencyPair | ||
import com.njkim.reactivecrypto.core.common.model.order.OrderBook | ||
import com.njkim.reactivecrypto.core.common.model.order.TickData | ||
import com.njkim.reactivecrypto.core.common.util.toEpochMilli | ||
import com.njkim.reactivecrypto.core.netty.HeartBeatHandler | ||
import com.njkim.reactivecrypto.core.websocket.AbstractExchangeWebsocketClient | ||
import com.njkim.reactivecrypto.huobiglobal.model.HuobiTickDataWrapper | ||
import com.njkim.reactivecrypto.huobiglobal.model.HuobiOrderBook | ||
import com.njkim.reactivecrypto.huobiglobal.model.HuobiSubscribeResponse | ||
import io.netty.buffer.ByteBuf | ||
import io.netty.buffer.ByteBufInputStream | ||
import io.netty.channel.ChannelHandlerContext | ||
import io.netty.handler.codec.ByteToMessageDecoder | ||
import io.netty.handler.codec.compression.JdkZlibDecoder | ||
import io.netty.handler.codec.compression.ZlibWrapper | ||
import io.netty.handler.codec.http.websocketx.TextWebSocketFrame | ||
import mu.KotlinLogging | ||
import org.apache.commons.codec.Charsets | ||
import org.apache.commons.lang3.StringUtils | ||
import org.springframework.util.StreamUtils | ||
import reactor.core.publisher.Flux | ||
import reactor.netty.http.client.HttpClient | ||
import java.time.ZonedDateTime | ||
import java.util.concurrent.TimeUnit | ||
import kotlin.streams.toList | ||
|
||
@Suppress("IMPLICIT_CAST_TO_ANY") | ||
open class HuobiGlobalWebsocketClient( | ||
private val baseUri: String = "wss://api.huobi.pro/ws" | ||
) : AbstractExchangeWebsocketClient() { | ||
private val log = KotlinLogging.logger {} | ||
|
||
private val objectMapper: ObjectMapper = createJsonObjectMapper().objectMapper() | ||
|
||
override fun createJsonObjectMapper(): ExchangeJsonObjectMapper { | ||
return HuobiJsonObjectMapper() | ||
} | ||
|
||
override fun createDepthSnapshot(subscribeTargets: List<CurrencyPair>): Flux<OrderBook> { | ||
val subscribeMessages = subscribeTargets.stream() | ||
.map { "${it.targetCurrency.name.toLowerCase()}${it.baseCurrency.name.toLowerCase()}" } | ||
.map { "{\"sub\": \"market.$it.depth.step0\",\"id\": \"$it\"}" } | ||
.toList() | ||
|
||
return HttpClient.create() | ||
.wiretap(log.isDebugEnabled) | ||
.tcpConfiguration { tcp -> | ||
tcp.doOnConnected { connection -> | ||
connection.addHandler(JdkZlibDecoder(ZlibWrapper.GZIP, true)) | ||
connection.addHandler(PingPongHandler()) | ||
connection.addHandler( | ||
"heartBeat", | ||
HeartBeatHandler( | ||
false, | ||
2000, | ||
TimeUnit.MILLISECONDS, | ||
1000 | ||
) { "{\"ping\": ${ZonedDateTime.now().toEpochMilli()}}" } | ||
) | ||
} | ||
} | ||
.websocket() | ||
.uri(baseUri) | ||
.handle { inbound, outbound -> | ||
outbound.sendString(Flux.fromIterable<String>(subscribeMessages)) | ||
.then() | ||
.thenMany(inbound.receive().asString()) | ||
} | ||
.doOnNext { log.debug { it } } | ||
.filter { it.contains("\"ch\"") } | ||
.map { objectMapper.readValue<HuobiSubscribeResponse<HuobiOrderBook>>(it) } | ||
.map { | ||
OrderBook( | ||
"${it.currencyPair}${it.ts.toEpochMilli()}", | ||
it.currencyPair, | ||
ZonedDateTime.now(), | ||
ExchangeVendor.HUOBI_GLOBAL, | ||
it.tick.getBids(), | ||
it.tick.getAsks() | ||
) | ||
} | ||
} | ||
|
||
override fun createTradeWebsocket(subscribeTargets: List<CurrencyPair>): Flux<TickData> { | ||
val subscribeMessages = subscribeTargets.stream() | ||
.map { "${it.targetCurrency.name.toLowerCase()}${it.baseCurrency.name.toLowerCase()}" } | ||
.map { "{\"sub\": \"market.$it.trade.detail\",\"id\": \"$it\"}" } | ||
.toList() | ||
|
||
return HttpClient.create() | ||
.wiretap(log.isDebugEnabled) | ||
.tcpConfiguration { tcp -> | ||
tcp.doOnConnected { connection -> | ||
connection.addHandler(JdkZlibDecoder(ZlibWrapper.GZIP, true)) | ||
connection.addHandler(PingPongHandler()) | ||
connection.addHandler( | ||
"heartBeat", | ||
HeartBeatHandler( | ||
false, | ||
2000, | ||
TimeUnit.MILLISECONDS, | ||
1000 | ||
) { "{\"ping\": ${ZonedDateTime.now().toEpochMilli()}}" } | ||
) | ||
} | ||
} | ||
.websocket() | ||
.uri(baseUri) | ||
.handle { inbound, outbound -> | ||
outbound.sendString(Flux.fromIterable<String>(subscribeMessages)) | ||
.then() | ||
.thenMany(inbound.receive().asString()) | ||
} | ||
.filter { it.contains("\"ch\"") } | ||
.map { objectMapper.readValue<HuobiSubscribeResponse<HuobiTickDataWrapper>>(it) } | ||
.flatMapIterable { | ||
it.tick.data | ||
.map { huobiKoreaTickData -> | ||
TickData( | ||
huobiKoreaTickData.id.toPlainString(), | ||
huobiKoreaTickData.ts, | ||
huobiKoreaTickData.price, | ||
huobiKoreaTickData.amount, | ||
it.currencyPair, | ||
ExchangeVendor.HUOBI_GLOBAL, | ||
huobiKoreaTickData.direction | ||
) | ||
} | ||
.toList() | ||
} | ||
} | ||
|
||
/** | ||
* server sent ping {"ping" : $epochMilli } | ||
* client response pong {"pong" : $epochMilli } | ||
*/ | ||
private inner class PingPongHandler : ByteToMessageDecoder() { | ||
override fun decode(ctx: ChannelHandlerContext, msg: ByteBuf, out: MutableList<Any>) { | ||
ByteBufInputStream(msg).use { | ||
val response = StreamUtils.copyToString(it, Charsets.UTF_8) | ||
if (StringUtils.contains(response, "ping")) { | ||
val replace = response.replace("ping", "pong") | ||
ctx.channel().writeAndFlush(TextWebSocketFrame(replace)) | ||
} else { | ||
val uncompressed = msg.alloc().buffer().writeBytes(response.toByteArray()) | ||
out.add(uncompressed) | ||
} | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
26 changes: 26 additions & 0 deletions
26
...global/src/main/kotlin/com/njkim/reactivecrypto/huobiglobal/model/HuobiTickDataWrapper.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
/* | ||
* Copyright 2019 namjug-kim | ||
* | ||
* LINE Corporation licenses this file to you under the Apache License, | ||
* version 2.0 (the "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at: | ||
* | ||
* https://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT | ||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the | ||
* License for the specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package com.njkim.reactivecrypto.huobiglobal.model | ||
|
||
import java.math.BigDecimal | ||
import java.time.ZonedDateTime | ||
|
||
data class HuobiTickDataWrapper( | ||
val id: BigDecimal, | ||
val ts: ZonedDateTime, | ||
val data: List<HuobiTickData> | ||
) |
Oops, something went wrong.