Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[bybit] Upgrade unauthorized endpoints to api v5 #4770

Closed
wants to merge 6 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,13 @@
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<version>${version.junit}</version>
<scope>test</scope>
</dependency>

<!-- SLF4J implementation for use in examples -->
<dependency>
<groupId>ch.qos.logback</groupId>
Expand Down
5 changes: 5 additions & 0 deletions xchange-bybit/http-client.env.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"default": {
"base_url": "https://api.bybit.com"
}
}
2 changes: 2 additions & 0 deletions xchange-bybit/lombok.config
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
lombok.equalsAndHashCode.callSuper = call
lombok.tostring.callsuper = call
105 changes: 93 additions & 12 deletions xchange-bybit/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,29 +14,110 @@
<name>XChange Bybit</name>
<description>XChange implementation for Bybit</description>

<url>http://knowm.org/open-source/xchange/</url>
<inceptionYear>2022</inceptionYear>
<properties>
<version.maven-enforcer-plugin>3.2.1</version.maven-enforcer-plugin>
<version.mockito>5.3.1</version.mockito>
<version.sortpom-maven-plugin>3.2.0</version.sortpom-maven-plugin>
</properties>

<organization>
<name>Knowm Inc.</name>
<url>http://knowm.org/open-source/xchange/</url>
</organization>

<!-- Parent provides default configuration for dependencies -->
<dependencies>

<dependency>
<groupId>com.fasterxml.jackson.datatype</groupId>
<artifactId>jackson-datatype-jsr310</artifactId>
<version>${version.fasterxml}</version>
</dependency>

<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-ratelimiter</artifactId>
<version>${version.resilience4j}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>io.github.resilience4j</groupId>
<artifactId>resilience4j-retry</artifactId>
<version>${version.resilience4j}</version>
<exclusions>
<exclusion>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
</exclusion>
</exclusions>
</dependency>

<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter-engine</artifactId>
<scope>test</scope>
</dependency>

<dependency>
<groupId>org.knowm.xchange</groupId>
<artifactId>xchange-core</artifactId>
<version>${project.version}</version>
</dependency>

<dependency>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<scope>test</scope>
<groupId>org.wiremock</groupId>
<artifactId>wiremock</artifactId>
<scope>test</scope>
</dependency>

</dependencies>

</project>
<build>
<plugins>
<plugin>
<groupId>com.github.ekryd.sortpom</groupId>
<artifactId>sortpom-maven-plugin</artifactId>
<version>${version.sortpom-maven-plugin}</version>
<configuration>
<sortProperties>true</sortProperties>
<createBackupFile>false</createBackupFile>
<sortDependencies>groupId,artifactId</sortDependencies>
<sortModules>true</sortModules>
<nrOfIndentSpace>4</nrOfIndentSpace>
<sortPlugins>groupId,artifactId</sortPlugins>
<expandEmptyElements>false</expandEmptyElements>
</configuration>
<executions>
<execution>
<goals>
<goal>sort</goal>
</goals>
<phase>verify</phase>
</execution>
</executions>
</plugin>

<plugin>
<artifactId>maven-enforcer-plugin</artifactId>
<version>${version.maven-enforcer-plugin}</version>
<executions>
<execution>
<id>enforce</id>
<goals>
<goal>enforce</goal>
</goals>
<configuration>
<rules>
<dependencyConvergence/>
<banDuplicatePomDependencyVersions/>
<reactorModuleConvergence/>
</rules>
</configuration>
</execution>
</executions>
</plugin>

</plugins>
</build>

</project>
43 changes: 31 additions & 12 deletions xchange-bybit/src/main/java/org/knowm/xchange/bybit/Bybit.java
Original file line number Diff line number Diff line change
Expand Up @@ -6,28 +6,47 @@
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.List;
import org.knowm.xchange.bybit.dto.BybitCategorizedPayload;
import org.knowm.xchange.bybit.dto.BybitResult;
import org.knowm.xchange.bybit.dto.marketdata.BybitSymbol;
import org.knowm.xchange.bybit.dto.marketdata.BybitInstrumentInfo;
import org.knowm.xchange.bybit.dto.marketdata.BybitOrderBook;
import org.knowm.xchange.bybit.dto.marketdata.BybitServerTime;
import org.knowm.xchange.bybit.dto.marketdata.BybitTicker;
import org.knowm.xchange.bybit.service.BybitException;

@Path("")
@Produces(MediaType.APPLICATION_JSON)
public interface Bybit {

/**
* @apiSpec <a href="https://bybit-exchange.github.io/docs/inverse/#t-latestsymbolinfo">API</a>
*/

@GET
@Path("/v2/public/tickers")
BybitResult<List<BybitTicker>> getTicker24h(@QueryParam("symbol") String symbol)
@Path("/v5/market/time")
BybitResult<BybitServerTime> getServerTime() throws IOException, BybitException;


@GET
@Path("/v5/market/instruments-info")
BybitResult<BybitCategorizedPayload<BybitInstrumentInfo>> getInstrumentsInfo(
@QueryParam("category") String category
)
throws IOException, BybitException;

/**
* @apiSpec <a href="https://bybit-exchange.github.io/docs/inverse/#t-querysymbol">API</a>
*/

@GET
@Path("/v2/public/symbols")
BybitResult<List<BybitSymbol>> getSymbols() throws IOException, BybitException;
@Path("/v5/market/tickers")
BybitResult<BybitCategorizedPayload<BybitTicker>> getTickers(
@QueryParam("category") String category,
@QueryParam("symbol") String symbol
) throws IOException, BybitException;


@GET
@Path("/v5/market/orderbook")
BybitResult<BybitOrderBook> getOrderBook(
@QueryParam("category") String category,
@QueryParam("symbol") String symbol,
@QueryParam("limit") Integer limit
) throws IOException, BybitException;


}
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,28 @@
import java.util.Arrays;
import java.util.Date;
import java.util.List;
import java.util.stream.Collectors;
import lombok.experimental.UtilityClass;
import org.knowm.xchange.bybit.dto.BybitResult;
import org.knowm.xchange.bybit.dto.account.BybitBalance;
import org.knowm.xchange.bybit.dto.marketdata.BybitInstrumentInfo;
import org.knowm.xchange.bybit.dto.marketdata.BybitOrderBook;
import org.knowm.xchange.bybit.dto.marketdata.BybitTicker;
import org.knowm.xchange.bybit.dto.trade.BybitOrderDetails;
import org.knowm.xchange.bybit.service.BybitException;
import org.knowm.xchange.currency.Currency;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.account.Balance;
import org.knowm.xchange.dto.account.Wallet;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.meta.InstrumentMetaData;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.instrument.Instrument;

@UtilityClass
public class BybitAdapters {

public static final List<String> QUOTE_CURRENCIES = Arrays.asList("USDT", "USDC", "BTC", "DAI");
Expand Down Expand Up @@ -80,10 +91,64 @@ public static LimitOrder adaptBybitOrderDetails(BybitOrderDetails bybitOrderResu
}

public static <T> BybitException createBybitExceptionFromResult(BybitResult<T> walletBalances) {
return new BybitException(
walletBalances.getRetCode(),
walletBalances.getRetMsg(),
walletBalances.getExtCode(),
walletBalances.getExtCode());
return BybitException.builder()
.retCode(walletBalances.getRetCode())
.retMsg(walletBalances.getRetMsg())
.extInfo(walletBalances.getExtInfo())
// .result(walletBalances.getResult())
.timestamp(walletBalances.getTimestamp())
.build();
}


public static String toSymbol(Instrument instrument) {
if (instrument == null) {
return null;
}
else {
return instrument.getBase().getCurrencyCode() + instrument.getCounter().getCurrencyCode();
}
}

public Ticker toTicker(BybitTicker gateioTicker, Instrument instrument) {
return new Ticker.Builder()
.instrument(instrument)
.last(gateioTicker.getLastPrice())
.bid(gateioTicker.getBestBidPrice())
.ask(gateioTicker.getBestAskPrice())
.high(gateioTicker.getHighPrice())
.low(gateioTicker.getLowPrice())
.volume(gateioTicker.getVolume24h())
.quoteVolume(gateioTicker.getTurnover24h())
.percentageChange(gateioTicker.getPrice24hPercentageChange())
.build();
}


public static InstrumentMetaData toInstrumentMetaData(BybitInstrumentInfo bybitInstrumentInfo) {
return new InstrumentMetaData.Builder()
.minimumAmount(bybitInstrumentInfo.getLotSizeFilter().getMinOrderQty())
.maximumAmount(bybitInstrumentInfo.getLotSizeFilter().getMaxOrderQty())
.counterMinimumAmount(bybitInstrumentInfo.getLotSizeFilter().getMinOrderAmt())
.counterMaximumAmount(bybitInstrumentInfo.getLotSizeFilter().getMaxOrderAmt())
.priceScale(bybitInstrumentInfo.getPriceFilter().getTickSize().scale())
.volumeScale(bybitInstrumentInfo.getLotSizeFilter().getBasePrecision().scale())
.amountStepSize(bybitInstrumentInfo.getLotSizeFilter().getBasePrecision())
.priceStepSize(bybitInstrumentInfo.getPriceFilter().getTickSize())
.build();
}


public OrderBook toOrderBook(BybitOrderBook bybitOrderBook, Instrument instrument) {
List<LimitOrder> asks = bybitOrderBook.getAsks().stream()
.map(priceSizeEntry -> new LimitOrder(OrderType.ASK, priceSizeEntry.getSize(), instrument, null, null, priceSizeEntry.getPrice()))
.collect(Collectors.toList());

List<LimitOrder> bids = bybitOrderBook.getBids().stream()
.map(priceSizeEntry -> new LimitOrder(OrderType.BID, priceSizeEntry.getSize(), instrument, null, null, priceSizeEntry.getPrice()))
.collect(Collectors.toList());

return new OrderBook(Date.from(bybitOrderBook.getTimestamp()), asks, bids);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
package org.knowm.xchange.bybit;

import java.util.HashMap;
import java.util.Map;
import lombok.SneakyThrows;
import lombok.experimental.UtilityClass;
import org.knowm.xchange.bybit.service.BybitException;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.exceptions.InstrumentNotValidException;

@UtilityClass
public class BybitErrorAdapter {

private final Map<String, Class<? extends ExchangeException>> EXCEPTION_BY_MESSAGE = new HashMap<>();

static {
EXCEPTION_BY_MESSAGE.put("Not supported symbols", InstrumentNotValidException.class);
}

@SneakyThrows
public ExchangeException adapt(BybitException e) {

if (EXCEPTION_BY_MESSAGE.containsKey(e.getRetMsg())) {
Class<? extends ExchangeException> a = EXCEPTION_BY_MESSAGE.get(e.getRetMsg());
return a.getConstructor(String.class, Throwable.class).newInstance(e.getMessage(), e);
}

return new ExchangeException(e.getMessage(), e);

}

}
Loading
Loading