From 2837c9dd5ac433c3da4c06ed6297120bb8d70fff Mon Sep 17 00:00:00 2001 From: Aneonex Software <69394046+aneonex@users.noreply.github.com> Date: Mon, 14 Nov 2022 00:29:19 +0300 Subject: [PATCH] Bybit API v3 --- .../datamodule/model/market/Bybit.kt | 87 +++++++++++-------- 1 file changed, 49 insertions(+), 38 deletions(-) diff --git a/dataModule/src/main/java/com/aneonex/bitcoinchecker/datamodule/model/market/Bybit.kt b/dataModule/src/main/java/com/aneonex/bitcoinchecker/datamodule/model/market/Bybit.kt index 61770111..69efc5a0 100644 --- a/dataModule/src/main/java/com/aneonex/bitcoinchecker/datamodule/model/market/Bybit.kt +++ b/dataModule/src/main/java/com/aneonex/bitcoinchecker/datamodule/model/market/Bybit.kt @@ -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 + ) { + 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) { - 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") } } \ No newline at end of file