Skip to content
Draft
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
Original file line number Diff line number Diff line change
@@ -1,20 +1,12 @@
package org.knowm.xchange.gateio;

import jakarta.ws.rs.GET;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import org.knowm.xchange.gateio.dto.GateioException;
import org.knowm.xchange.gateio.dto.marketdata.*;

import java.io.IOException;
import java.util.List;
import org.knowm.xchange.gateio.dto.GateioException;
import org.knowm.xchange.gateio.dto.marketdata.GateioCurrencyChain;
import org.knowm.xchange.gateio.dto.marketdata.GateioCurrencyInfo;
import org.knowm.xchange.gateio.dto.marketdata.GateioCurrencyPairDetails;
import org.knowm.xchange.gateio.dto.marketdata.GateioOrderBook;
import org.knowm.xchange.gateio.dto.marketdata.GateioServerTime;
import org.knowm.xchange.gateio.dto.marketdata.GateioTicker;

@Path("api/v4")
@Produces(MediaType.APPLICATION_JSON)
Expand Down Expand Up @@ -43,6 +35,10 @@ List<GateioCurrencyChain> getCurrencyChains(@QueryParam("currency") String curre
@Path("spot/currency_pairs")
List<GateioCurrencyPairDetails> getCurrencyPairDetails() throws IOException, GateioException;

@GET
@Path("/futures/usdt/contracts")
List<GateioInstrumentDetails> getInstrumentDetails() throws IOException, GateioException;

@GET
@Path("spot/currency_pairs/{currency_pair}")
GateioCurrencyPairDetails getCurrencyPairDetails(@PathParam("currency_pair") String currencyPair)
Expand All @@ -52,4 +48,25 @@ GateioCurrencyPairDetails getCurrencyPairDetails(@PathParam("currency_pair") Str
@Path("spot/tickers")
List<GateioTicker> getTickers(@QueryParam("currency_pair") String currencyPair)
throws IOException, GateioException;

@GET
@Path("spot/candlesticks")
List<GateioSpotCandlestick> getSpotCandlesticks(
@QueryParam("currency_pair") String currencyPair,
@QueryParam("limit") Integer limit,
@QueryParam("from") Long from,
@QueryParam("to") Long to,
@QueryParam("interval") String interval)
throws IOException, GateioException;

@GET
@Path("futures/{settle}/candlesticks")
List<GateioFuturesCandlestick> getFuturesCandlesticks(
@PathParam("settle") String settle,
@QueryParam("contract") String contract,
@QueryParam("limit") Integer limit,
@QueryParam("from") Long from,
@QueryParam("to") Long to,
@QueryParam("interval") String interval)
throws IOException, GateioException;
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,13 @@
package org.knowm.xchange.gateio;

import java.math.BigDecimal;
import java.math.MathContext;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;
import lombok.experimental.UtilityClass;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.Order;
import org.knowm.xchange.dto.Order.OrderStatus;
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.account.FundingRecord;
import org.knowm.xchange.dto.marketdata.CandleStick;
import org.knowm.xchange.dto.marketdata.CandleStickData;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.meta.InstrumentMetaData;
Expand All @@ -21,20 +17,26 @@
import org.knowm.xchange.gateio.dto.account.GateioAccountBookRecord;
import org.knowm.xchange.gateio.dto.account.GateioOrder;
import org.knowm.xchange.gateio.dto.account.GateioWithdrawalRequest;
import org.knowm.xchange.gateio.dto.marketdata.GateioCurrencyPairDetails;
import org.knowm.xchange.gateio.dto.marketdata.GateioOrderBook;
import org.knowm.xchange.gateio.dto.marketdata.GateioTicker;
import org.knowm.xchange.gateio.dto.marketdata.*;
import org.knowm.xchange.gateio.dto.trade.GateioUserTrade;
import org.knowm.xchange.gateio.dto.trade.GateioUserTradeRaw;
import org.knowm.xchange.gateio.service.params.GateioWithdrawFundsParams;
import org.knowm.xchange.instrument.Instrument;

import java.math.BigDecimal;
import java.math.MathContext;
import java.time.Instant;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.stream.Collectors;

@UtilityClass
public class GateioAdapters {

public final BigDecimal PARTIALLY_FILLED_SCALE = new BigDecimal("0.1");

public String toString(Instrument instrument) {
public String toGateioInstrument(Instrument instrument) {
if (instrument == null) {
return null;
} else {
Expand Down Expand Up @@ -75,7 +77,7 @@ public OrderBook toOrderBook(GateioOrderBook gateioOrderBook, Instrument instrum
return new OrderBook(Date.from(gateioOrderBook.getGeneratedAt()), asks, bids);
}

public InstrumentMetaData toInstrumentMetaData(
public InstrumentMetaData currencyPairToInstrumentMetaData(
GateioCurrencyPairDetails gateioCurrencyPairDetails) {
return InstrumentMetaData.builder()
.tradingFee(gateioCurrencyPairDetails.getFee())
Expand All @@ -86,7 +88,23 @@ public InstrumentMetaData toInstrumentMetaData(
.build();
}

public String toString(OrderStatus orderStatus) {
public InstrumentMetaData instrumentToInstrumentMetaData(
GateioInstrumentDetails gateioInstrumentDetails) {
return InstrumentMetaData.builder()
.contractValue(gateioInstrumentDetails.getQuantoMultiplier())
.tradingFee(gateioInstrumentDetails.getTakerFeeRate())
.minimumAmount(gateioInstrumentDetails.getOrderSizeMin().multiply(gateioInstrumentDetails.getQuantoMultiplier()).stripTrailingZeros())
.maximumAmount(gateioInstrumentDetails.getOrderSizeMax().multiply(gateioInstrumentDetails.getQuantoMultiplier()).stripTrailingZeros())
.priceStepSize(gateioInstrumentDetails.getOrderPriceRound())
// no data, so suggest that equals to order min size
.amountStepSize(gateioInstrumentDetails.getOrderSizeMin().multiply(gateioInstrumentDetails.getQuantoMultiplier()).stripTrailingZeros())
.volumeScale(numberOfDecimals(gateioInstrumentDetails.getOrderSizeMin().multiply(gateioInstrumentDetails.getQuantoMultiplier()).stripTrailingZeros()))
.priceScale(numberOfDecimals(gateioInstrumentDetails.getOrderPriceRound()))
.contractValue(gateioInstrumentDetails.getQuantoMultiplier())
.build();
}

public String toGateioInstrument(OrderStatus orderStatus) {
switch (orderStatus) {
case OPEN:
return "open";
Expand Down Expand Up @@ -237,6 +255,47 @@ public Ticker toTicker(GateioTicker gateioTicker) {
.build();
}

public CandleStickData toCandleStickDataSpot(
List<GateioSpotCandlestick> gateioSpotCandlesticks, Instrument instrument) {
List<CandleStick> candleSticks =
gateioSpotCandlesticks.stream()
.map(
gateioSpotCandlestick ->
new CandleStick.Builder()
.timestamp(Instant.ofEpochSecond(gateioSpotCandlestick.getTimestamp()))
.open(gateioSpotCandlestick.getOpen())
.high(gateioSpotCandlestick.getHigh())
.low(gateioSpotCandlestick.getLow())
.close(gateioSpotCandlestick.getClose())
.volume(gateioSpotCandlestick.getVolume())
.quotaVolume(gateioSpotCandlestick.getQuoteVolume())
.completed(gateioSpotCandlestick.isCompleted())
.build())
.collect(Collectors.toList());

return new CandleStickData(instrument, candleSticks);
}

public CandleStickData toCandleStickDataFutures(
List<GateioFuturesCandlestick> gateioFuturesCandlesticks, Instrument instrument, BigDecimal contractValue) {
List<CandleStick> candleSticks =
gateioFuturesCandlesticks.stream()
.map(
gateioFuturesCandlestick ->
new CandleStick.Builder()
.timestamp(Instant.ofEpochSecond(gateioFuturesCandlestick.getTimestamp()))
.open(gateioFuturesCandlestick.getOpen())
.high(gateioFuturesCandlestick.getHigh())
.low(gateioFuturesCandlestick.getLow())
.close(gateioFuturesCandlestick.getClose())
.volume(convertContractSizeToVolume(gateioFuturesCandlestick.getVolume(), contractValue))
.quotaVolume(gateioFuturesCandlestick.getQuoteVolume())
.build())
.collect(Collectors.toList());

return new CandleStickData(instrument, candleSticks);
}

public FundingRecord toFundingRecords(GateioAccountBookRecord gateioAccountBookRecord) {
return FundingRecord.builder()
.internalId(gateioAccountBookRecord.getId())
Expand All @@ -248,4 +307,15 @@ public FundingRecord toFundingRecords(GateioAccountBookRecord gateioAccountBookR
.description(gateioAccountBookRecord.getTypeDescription())
.build();
}

private static int numberOfDecimals(BigDecimal value) {
double d = value.doubleValue();
return -(int) Math.round(Math.log10(d));
}

private static BigDecimal convertContractSizeToVolume(
BigDecimal size, BigDecimal contractValue) {
return size.multiply(contractValue).stripTrailingZeros();
}

}
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
package org.knowm.xchange.gateio;

import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;
import org.knowm.xchange.BaseExchange;
import org.knowm.xchange.ExchangeSpecification;
import org.knowm.xchange.dto.meta.ExchangeMetaData;
import org.knowm.xchange.dto.meta.InstrumentMetaData;
import org.knowm.xchange.gateio.dto.GateioExchangeType;
import org.knowm.xchange.gateio.service.GateioAccountService;
import org.knowm.xchange.gateio.service.GateioMarketDataService;
import org.knowm.xchange.gateio.service.GateioTradeService;
import org.knowm.xchange.instrument.Instrument;
import org.knowm.xchange.utils.nonce.CurrentTimeIncrementalNonceFactory;
import si.mazi.rescu.SynchronizedValueFactory;

public class GateioExchange extends BaseExchange {
import java.io.IOException;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.knowm.xchange.gateio.dto.GateioExchangeType.SPOT;

public class GateioExchange extends BaseExchange {
public static String EXCHANGE_TYPE = "Exchange_Type";
private final SynchronizedValueFactory<Long> nonceFactory =
new CurrentTimeIncrementalNonceFactory(TimeUnit.SECONDS);

Expand All @@ -30,6 +34,7 @@ protected void initServices() {
public ExchangeSpecification getDefaultExchangeSpecification() {

ExchangeSpecification specification = new ExchangeSpecification(this.getClass());
specification.setExchangeSpecificParametersItem(EXCHANGE_TYPE, SPOT);
specification.setSslUri("https://api.gateio.ws");
specification.setHost("gate.io");
specification.setExchangeName("Gateio");
Expand All @@ -48,5 +53,11 @@ public void remoteInit() throws IOException {
((GateioMarketDataService) marketDataService).getMetaDataByInstrument();

exchangeMetaData = new ExchangeMetaData(instruments, null, null, null, null);
((GateioMarketDataService) marketDataService).setInstrumentMetaDataMap(exchangeMetaData.getInstruments());
}

public boolean isFuturesEnabled() {
return GateioExchangeType.FUTURES.equals(
exchangeSpecification.getExchangeSpecificParametersItem(EXCHANGE_TYPE));
}
}
Original file line number Diff line number Diff line change
@@ -1,32 +1,16 @@
package org.knowm.xchange.gateio;

import jakarta.ws.rs.Consumes;
import jakarta.ws.rs.DELETE;
import jakarta.ws.rs.GET;
import jakarta.ws.rs.HeaderParam;
import jakarta.ws.rs.POST;
import jakarta.ws.rs.Path;
import jakarta.ws.rs.PathParam;
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.MediaType;
import java.io.IOException;
import java.util.List;
import org.knowm.xchange.gateio.dto.GateioException;
import org.knowm.xchange.gateio.dto.account.GateioAccountBookRecord;
import org.knowm.xchange.gateio.dto.account.GateioAddressRecord;
import org.knowm.xchange.gateio.dto.account.GateioCurrencyBalance;
import org.knowm.xchange.gateio.dto.account.GateioDepositAddress;
import org.knowm.xchange.gateio.dto.account.GateioDepositRecord;
import org.knowm.xchange.gateio.dto.account.GateioOrder;
import org.knowm.xchange.gateio.dto.account.GateioSubAccountTransfer;
import org.knowm.xchange.gateio.dto.account.GateioWithdrawStatus;
import org.knowm.xchange.gateio.dto.account.GateioWithdrawalRecord;
import org.knowm.xchange.gateio.dto.account.GateioWithdrawalRequest;
import org.knowm.xchange.gateio.dto.account.*;
import org.knowm.xchange.gateio.dto.trade.GateioUserTradeRaw;
import si.mazi.rescu.ParamsDigest;
import si.mazi.rescu.SynchronizedValueFactory;

import java.io.IOException;
import java.util.List;

@Path("api/v4")
@Produces(MediaType.APPLICATION_JSON)
public interface GateioV4Authenticated {
Expand Down Expand Up @@ -184,4 +168,16 @@ GateioWithdrawalRecord withdraw(
@HeaderParam("SIGN") ParamsDigest signer,
GateioWithdrawalRequest gateioWithdrawalRequest)
throws IOException, GateioException;

@POST
@Path("futures/{settle}/positions/{contract}/leverage")
@Consumes(MediaType.APPLICATION_JSON)
GateioPositionLeverageUpdate updatePositionLeverage(
@HeaderParam("KEY") String apiKey,
@HeaderParam("Timestamp") SynchronizedValueFactory<Long> timestamp,
@HeaderParam("SIGN") ParamsDigest signer,
@PathParam("settle") String settle,
@PathParam("contract") String contract,
@QueryParam("leverage") String leverage)
throws IOException, GateioException;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package org.knowm.xchange.gateio.dto;

public enum GateioExchangeType {
SPOT,
FUTURES,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package org.knowm.xchange.gateio.dto.account;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Getter;

import java.math.BigDecimal;

@Getter
public class GateioPositionLeverageUpdate {

@JsonProperty("leverage")
private final BigDecimal leverage;

@JsonProperty("cross_leverage_limit")
private final BigDecimal crossLeverageLimit;

public GateioPositionLeverageUpdate(
@JsonProperty("leverage") BigDecimal leverage,
@JsonProperty("cross_leverage_limit") BigDecimal crossLeverageLimit) {
this.leverage = leverage;
this.crossLeverageLimit = crossLeverageLimit;
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package org.knowm.xchange.gateio.dto.marketdata;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Data;

import java.math.BigDecimal;

@Data
public class GateioFuturesCandlestick {

@JsonProperty("t")
// in seconds
private long timestamp;
// size volume (contract size). Only returned if contract is not prefixed
@JsonProperty("v")
private BigDecimal volume;

@JsonProperty("c")
private BigDecimal close;

@JsonProperty("h")
private BigDecimal high;

@JsonProperty("l")
private BigDecimal low;

@JsonProperty("o")
private BigDecimal open;
// Trading volume (unit: Quote currency)
@JsonProperty("sum")
private BigDecimal quoteVolume;

}
Loading
Loading