Skip to content
This repository has been archived by the owner on Jan 29, 2019. It is now read-only.

Commit

Permalink
#185 use own OrderBook entity
Browse files Browse the repository at this point in the history
add orderbook endpint to rest api
add banner for rest api
  • Loading branch information
Valentin Stavetski committed Apr 28, 2018
1 parent 9700c0a commit e6bd740
Show file tree
Hide file tree
Showing 9 changed files with 129 additions and 10 deletions.
3 changes: 3 additions & 0 deletions common/src/main/kotlin/fund/cyber/markets/common/Env.kt
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ const val CASSANDRA_HOSTS_DEFAULT = "localhost"
const val CASSANDRA_PORT = "CASSANDRA_PORT"
const val CASSANDRA_PORT_DEFAULT = 9042

const val EXCHANGES_CONNECTOR_SERVICE_URL = "EXCHANGES_CONNECTOR_SERVICE_URL"
const val EXCHANGES_CONNECTOR_SERVICE_URL_DEFAULT = "http://localhost:8080"

const val EXCHANGES = "EXCHANGES"
const val EXCHANGES_DEFAULT = "bitfinex,bitflyer,binance,bitstamp,gdax,gemini,hitbtc,okcoin,okex,poloniex,etherdelta"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ data class Order (
val exchange: String,
val pair: TokensPair,
val type: OrderType,
val timestamp: Date,
val timestamp: Long,
val epochHour: Long,
val orderId: String,
val orderId: String?,
val amount: BigDecimal,
val price: BigDecimal
)
Expand All @@ -22,7 +22,7 @@ data class OrdersBatch (
)

enum class OrderType {
SELL,
BUY,
BID,
ASK,
UNKNOWN
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package fund.cyber.markets.connector.api

import fund.cyber.markets.common.model.TokensPair
import fund.cyber.markets.connector.ConnectorRunner
import org.knowm.xchange.currency.CurrencyPair
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.GetMapping
Expand All @@ -21,8 +21,8 @@ class OrderbookEndpoint(
@RequestParam(value = "pair", required = true) tokensPair: String
): ResponseEntity<Any> {

val pair = CurrencyPair(tokensPair.substringBefore("_"), tokensPair.substringAfter("_"))
val orderbook = connectors[exchange.toUpperCase()]?.orderbooks?.get(pair)
val pair = TokensPair(tokensPair.substringBefore("_"), tokensPair.substringAfter("_"))
val orderbook = connectors[exchange.toUpperCase()]?.getOrderBookSnapshot(pair)

return if (orderbook == null) {
ResponseEntity(HttpStatus.NOT_FOUND)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,5 @@ import org.knowm.xchange.dto.marketdata.OrderBook

interface OrderbookConnector : Connector {
var orderbooks: MutableMap<CurrencyPair, OrderBook>
fun getOrderBookSnapshot(pair: TokensPair): fund.cyber.markets.common.model.OrderBook
fun getOrderBookSnapshot(pair: TokensPair): fund.cyber.markets.common.model.OrderBook?
}
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
package fund.cyber.markets.connector.orderbook

import fund.cyber.markets.common.MILLIS_TO_HOURS
import fund.cyber.markets.common.convert
import fund.cyber.markets.common.model.Order
import fund.cyber.markets.common.model.OrderType
import fund.cyber.markets.common.model.TokensPair
import fund.cyber.markets.connector.AbstarctXchangeConnector
import info.bitrich.xchangestream.core.ProductSubscription
import info.bitrich.xchangestream.core.StreamingExchangeFactory
import io.micrometer.core.instrument.MeterRegistry
import org.knowm.xchange.currency.CurrencyPair
import org.knowm.xchange.dto.marketdata.OrderBook
import org.knowm.xchange.dto.trade.LimitOrder
import org.springframework.kafka.core.KafkaTemplate
import java.util.*

class XchangeOrderbookConnector : AbstarctXchangeConnector, OrderbookConnector {
override var orderbooks: MutableMap<CurrencyPair, OrderBook> = mutableMapOf()
Expand Down Expand Up @@ -46,8 +52,35 @@ class XchangeOrderbookConnector : AbstarctXchangeConnector, OrderbookConnector {
}
}

override fun getOrderBookSnapshot(pair: TokensPair): fund.cyber.markets.common.model.OrderBook {
TODO("not implemented") //To change body of created functions use File | Settings | File Templates.
override fun getOrderBookSnapshot(pair: TokensPair): fund.cyber.markets.common.model.OrderBook? {
val orderBook = orderbooks[CurrencyPair(pair.base, pair.quote)] ?: return null
val timestamp: Long = orderBook.timeStamp?.time ?: Date().time

val asks = mutableListOf<Order>()
val bids = mutableListOf<Order>()

orderBook.asks.forEach { xchangeOrder ->
asks.add(convertOrder(xchangeOrder, pair, OrderType.ASK, timestamp))
}

orderBook.bids.forEach { xchangeOrder ->
asks.add(convertOrder(xchangeOrder, pair, OrderType.BID, timestamp))
}

return fund.cyber.markets.common.model.OrderBook(asks, bids, timestamp)
}

private fun convertOrder(xchangeOrder: LimitOrder, pair: TokensPair, type: OrderType, orderBookTimestamp: Long): Order {
val timestamp: Long = xchangeOrder.timestamp?.time ?: orderBookTimestamp

return Order(exchangeName,
pair,
type,
timestamp,
timestamp convert MILLIS_TO_HOURS,
xchangeOrder.id,
xchangeOrder.originalAmount,
xchangeOrder.limitPrice)
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package fund.cyber.markets.api.rest.configuration

import fund.cyber.markets.common.EXCHANGES_CONNECTOR_SERVICE_URL
import fund.cyber.markets.common.EXCHANGES_CONNECTOR_SERVICE_URL_DEFAULT
import org.springframework.beans.factory.annotation.Value
import org.springframework.context.annotation.Configuration

@Configuration
class RestApiConfiguration(
@Value("\${$EXCHANGES_CONNECTOR_SERVICE_URL:$EXCHANGES_CONNECTOR_SERVICE_URL_DEFAULT}")
val exchangesConnectorServiceUrl: String
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package fund.cyber.markets.api.rest.controller

import fund.cyber.markets.api.rest.configuration.RestApiConfiguration
import fund.cyber.markets.common.model.OrderBook
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RequestParam
import org.springframework.web.bind.annotation.RestController
import org.springframework.web.client.HttpClientErrorException
import org.springframework.web.client.RestTemplate
import org.springframework.web.util.UriComponentsBuilder
import reactor.core.publisher.Mono


const val ORDERBOOK_PATH = "/orderbook"

@RestController
class RawDataController {

@Autowired
private lateinit var configuration: RestApiConfiguration

private val restTemplate = RestTemplate()

@GetMapping("/orderbook")
fun getOrderBook(
@RequestParam exchange: String,
@RequestParam pair: String,
@RequestParam(required = false) ts: Long?
): Mono<OrderBook> {

val requestUri = configuration.exchangesConnectorServiceUrl + ORDERBOOK_PATH

val builder = UriComponentsBuilder.fromUriString(requestUri)
.queryParam("exchange", exchange)
.queryParam("pair", pair)

var response: OrderBook? = null
try {
response = restTemplate.getForObject<OrderBook>(builder.toUriString(), OrderBook::class.java)
} catch (e: HttpClientErrorException) {

}

return Mono.just(response!!)
}

}
2 changes: 2 additions & 0 deletions rest-api/src/main/resources/application.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
server:
port: 8085
21 changes: 21 additions & 0 deletions rest-api/src/main/resources/banner.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
_____ _____ _____ _____ _____ _____ _____
/\ \ /\ \ /\ \ /\ \ /\ \ /\ \ /\ \
/::\ \ /::\ \ /::\ \ /::\ \ /::\ \ /::\ \ /::\ \
/::::\ \ /::::\ \ /::::\ \ \:::\ \ /::::\ \ /::::\ \ \:::\ \
/::::::\ \ /::::::\ \ /::::::\ \ \:::\ \ /::::::\ \ /::::::\ \ \:::\ \
/:::/\:::\ \ /:::/\:::\ \ /:::/\:::\ \ \:::\ \ /:::/\:::\ \ /:::/\:::\ \ \:::\ \
/:::/__\:::\ \ /:::/__\:::\ \ /:::/__\:::\ \ \:::\ \ /:::/__\:::\ \ /:::/__\:::\ \ \:::\ \
/::::\ \:::\ \ /::::\ \:::\ \ \:::\ \:::\ \ /::::\ \ /::::\ \:::\ \ /::::\ \:::\ \ /::::\ \
/::::::\ \:::\ \ /::::::\ \:::\ \ ___\:::\ \:::\ \ /::::::\ \ /::::::\ \:::\ \ /::::::\ \:::\ \ ____ /::::::\ \
/:::/\:::\ \:::\____\ /:::/\:::\ \:::\ \ /\ \:::\ \:::\ \ /:::/\:::\ \ /:::/\:::\ \:::\ \ /:::/\:::\ \:::\____\ /\ \ /:::/\:::\ \
/:::/ \:::\ \:::| |/:::/__\:::\ \:::\____\/::\ \:::\ \:::\____\ /:::/ \:::\____\ /:::/ \:::\ \:::\____\/:::/ \:::\ \:::| |/::\ \/:::/ \:::\____\
\::/ |::::\ /:::|____|\:::\ \:::\ \::/ /\:::\ \:::\ \::/ / /:::/ \::/ / \::/ \:::\ /:::/ /\::/ \:::\ /:::|____|\:::\ /:::/ \::/ /
\/____|:::::\/:::/ / \:::\ \:::\ \/____/ \:::\ \:::\ \/____/ /:::/ / \/____/ \/____/ \:::\/:::/ / \/_____/\:::\/:::/ / \:::\/:::/ / \/____/
|:::::::::/ / \:::\ \:::\ \ \:::\ \:::\ \ /:::/ / \::::::/ / \::::::/ / \::::::/ /
|::|\::::/ / \:::\ \:::\____\ \:::\ \:::\____\ /:::/ / \::::/ / \::::/ / \::::/____/
|::| \::/____/ \:::\ \::/ / \:::\ /:::/ / \::/ / /:::/ / \::/____/ \:::\ \
|::| ~| \:::\ \/____/ \:::\/:::/ / \/____/ /:::/ / ~~ \:::\ \
|::| | \:::\ \ \::::::/ / /:::/ / \:::\ \
\::| | \:::\____\ \::::/ / /:::/ / \:::\____\
\:| | \::/ / \::/ / \::/ / \::/ /
\|___| \/____/ \/____/ \/____/ \/____/

0 comments on commit e6bd740

Please sign in to comment.