Skip to content

Commit

Permalink
Bybit API v3
Browse files Browse the repository at this point in the history
  • Loading branch information
aneonex committed Nov 13, 2022
1 parent e96c082 commit 2837c9d
Showing 1 changed file with 49 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,55 +2,66 @@ package com.aneonex.bitcoinchecker.datamodule.model.market

import com.aneonex.bitcoinchecker.datamodule.model.CheckerInfo
import com.aneonex.bitcoinchecker.datamodule.model.CurrencyPairInfo
import com.aneonex.bitcoinchecker.datamodule.model.Market
import com.aneonex.bitcoinchecker.datamodule.model.Ticker
import com.aneonex.bitcoinchecker.datamodule.model.market.generic.SimpleMarket
import com.aneonex.bitcoinchecker.datamodule.util.forEachJSONObject
import org.json.JSONObject

class Bybit : Market(NAME, TTS_NAME, null) {
companion object {
private const val NAME = "Bybit"
private const val TTS_NAME = NAME
private const val URL = "https://api.bybit.com/v2/public/tickers?symbol=%1\$s"
private const val URL_CURRENCY_PAIRS = "https://api.bybit.com/v2/public/symbols"
class Bybit : SimpleMarket(
"Bybit",
"https://api.bybit.com/spot/v3/public/symbols",
"https://api.bybit.com/spot/v3/public/quote/ticker/24hr?symbol=%1\$s"
) {
override fun parseCurrencyPairsFromJsonObject(
requestId: Int,
jsonObject: JSONObject,
pairs: MutableList<CurrencyPairInfo>
) {
jsonObject
.getJSONObject("result")
.getJSONArray("list")
.forEachJSONObject { market ->
if(market.optString("showStatus") == "1") {
pairs.add(
CurrencyPairInfo(
market.getString("baseCoin"),
market.getString("quoteCoin"),
market.getString("name")
)
)
}
}
}

override fun getCurrencyPairsUrl(requestId: Int): String {
return URL_CURRENCY_PAIRS
}
override fun parseTickerFromJsonObject(
requestId: Int,
jsonObject: JSONObject,
ticker: Ticker,
checkerInfo: CheckerInfo
) {
jsonObject
.getJSONObject("result")
.apply {
ticker.ask = getDouble("ap")
ticker.bid = getDouble("bp")

override fun parseCurrencyPairsFromJsonObject(requestId: Int, jsonObject: JSONObject, pairs: MutableList<CurrencyPairInfo>) {
jsonObject.getJSONArray("result").forEachJSONObject { market ->
val instrumentName = market.getString("name")
val quoteCurrency = market.getString("quote_currency")
var baseCurrency = market.getString("base_currency")
ticker.high = getDouble("h")
ticker.low = getDouble("l")

// Detect futures
if(!instrumentName.endsWith(quoteCurrency)) {
baseCurrency = market.getString("alias").replace(quoteCurrency, "-")
}
ticker.last = getDouble("lp")

pairs.add( CurrencyPairInfo(
baseCurrency,
quoteCurrency,
instrumentName
))
}
}
ticker.vol = getDouble("v")
ticker.volQuote = getDouble("qv")

override fun getUrl(requestId: Int, checkerInfo: CheckerInfo): String {
return String.format(URL, checkerInfo.currencyPairId)
ticker.timestamp = getLong("t")
}
}

override fun parseTickerFromJsonObject(requestId: Int, jsonObject: JSONObject, ticker: Ticker, checkerInfo: CheckerInfo) {
jsonObject.getJSONArray("result").getJSONObject(0).apply {
ticker.ask = getDouble("ask_price")
ticker.bid = getDouble("bid_price")
ticker.high = getDouble("high_price_24h")
ticker.low = getDouble("low_price_24h")
ticker.last = getDouble("last_price")
ticker.vol = getDouble("turnover_24h")
// ticker.timestamp = getLong("ts")
}
override fun parseErrorFromJsonObject(
requestId: Int,
jsonObject: JSONObject,
checkerInfo: CheckerInfo?
): String? {
return jsonObject.getString("retMsg")
}
}

0 comments on commit 2837c9d

Please sign in to comment.