Skip to content

Commit

Permalink
Merge pull request #4931 from bigscoop/exchange-health
Browse files Browse the repository at this point in the history
[general] Add getting of exchange health
  • Loading branch information
timmolter authored Aug 16, 2024
2 parents b86f9d0 + 94cc31c commit 26953cd
Show file tree
Hide file tree
Showing 12 changed files with 145 additions and 16 deletions.
Original file line number Diff line number Diff line change
@@ -1,19 +1,21 @@
package org.knowm.xchange.binance.dto.meta;

import com.fasterxml.jackson.annotation.JsonProperty;
import lombok.Builder;
import lombok.Value;
import lombok.extern.jackson.Jacksonized;

@Value
@Builder
@Jacksonized
public class BinanceSystemStatus {

// 0: normal,1:system maintenance
@JsonProperty private String status;
// normal or system maintenance
@JsonProperty private String msg;
@JsonProperty
String status;

public String getStatus() {
return status;
}
// normal or system maintenance
@JsonProperty
String msg;

public String getMsg() {
return msg;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.dto.meta.ExchangeHealth;
import org.knowm.xchange.dto.trade.LimitOrder;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.instrument.Instrument;
Expand All @@ -35,6 +36,20 @@ public BinanceMarketDataService(
super(exchange, resilienceRegistries);
}


@Override
public ExchangeHealth getExchangeHealth() {
try {
if (getSystemStatus().getStatus().equals("0")) {
return ExchangeHealth.ONLINE;
}
} catch (IOException e) {
return ExchangeHealth.OFFLINE;
}
return ExchangeHealth.OFFLINE;
}


/**
* optional parameters provided in the args array:
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,6 @@ public List<FundingRecord> getFundingHistory(TradeHistoryParams params) {
@Override
public BinanceSystemStatus getSystemStatus() {
LOG.warn("getSystemStatus: {}", NOT_SUPPORTED);
return new BinanceSystemStatus();
return BinanceSystemStatus.builder().build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,17 @@
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.meta.ExchangeHealth;
import org.knowm.xchange.instrument.Instrument;

public class MarketDataServiceIntegration extends BinanceExchangeIntegration {

@Test
public void exchange_health() {
assertThat(exchange.getMarketDataService().getExchangeHealth()).isEqualTo(ExchangeHealth.ONLINE);
}


@Test
public void valid_timestamp() {

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package org.knowm.xchange.dto.meta;

public enum ExchangeHealth {
ONLINE,
OFFLINE,

/**
* Can only cancel the order but not place order
*/
CANCEL_ONLY;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,13 @@
import java.util.List;
import org.knowm.xchange.Exchange;
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.*;
import org.knowm.xchange.dto.marketdata.CandleStickData;
import org.knowm.xchange.dto.marketdata.FundingRate;
import org.knowm.xchange.dto.marketdata.FundingRates;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.marketdata.Trades;
import org.knowm.xchange.dto.meta.ExchangeHealth;
import org.knowm.xchange.exceptions.ExchangeException;
import org.knowm.xchange.exceptions.NotAvailableFromExchangeException;
import org.knowm.xchange.exceptions.NotYetImplementedForExchangeException;
Expand All @@ -25,6 +31,15 @@
*/
public interface MarketDataService extends BaseService {

/**
* Get exchange health
*
* @return The exchange health
*/
default ExchangeHealth getExchangeHealth() {
return ExchangeHealth.ONLINE;
}

/**
* Get a ticker representing the current exchange rate
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,25 @@
import jakarta.ws.rs.Produces;
import jakarta.ws.rs.QueryParam;
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)
public interface Gateio {

@GET
@Path("spot/time")
GateioServerTime getServerTime() throws IOException, GateioException;


@GET
@Path("spot/currencies")
List<GateioCurrencyInfo> getCurrencies() throws IOException, GateioException;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package org.knowm.xchange.gateio.config;

import java.time.Clock;
import lombok.Data;
import si.mazi.rescu.IRestProxyFactory;
import si.mazi.rescu.RestProxyFactoryImpl;

@Data
public class Config {
public final class Config {

private Class<? extends IRestProxyFactory> restProxyFactoryClass = RestProxyFactoryImpl.class;

private Clock clock;

private static Config instance = new Config();

private Config() {
clock = Clock.systemDefaultZone();
}

public static Config getInstance() {
return instance;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package org.knowm.xchange.gateio.dto.marketdata;

import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.Instant;
import lombok.Builder;
import lombok.Data;
import lombok.extern.jackson.Jacksonized;

@Data
@Builder
@Jacksonized
public class GateioServerTime {

@JsonProperty("server_time")
Instant time;

}
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.knowm.xchange.gateio.service;

import java.io.IOException;
import java.time.Duration;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Objects;
Expand All @@ -11,10 +13,12 @@
import org.knowm.xchange.currency.CurrencyPair;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.meta.ExchangeHealth;
import org.knowm.xchange.dto.meta.InstrumentMetaData;
import org.knowm.xchange.gateio.GateioAdapters;
import org.knowm.xchange.gateio.GateioErrorAdapter;
import org.knowm.xchange.gateio.GateioExchange;
import org.knowm.xchange.gateio.config.Config;
import org.knowm.xchange.gateio.dto.GateioException;
import org.knowm.xchange.gateio.dto.marketdata.GateioCurrencyInfo;
import org.knowm.xchange.gateio.dto.marketdata.GateioCurrencyPairDetails;
Expand All @@ -30,12 +34,28 @@ public GateioMarketDataService(GateioExchange exchange) {
super(exchange);
}

@Override
public ExchangeHealth getExchangeHealth() {
try {
Instant serverTime = getGateioServerTime().getTime();
Instant localTime = Instant.now(Config.getInstance().getClock());

// timestamps shouldn't diverge by more than 10 minutes
if (Duration.between(serverTime, localTime).toMinutes() < 10) {
return ExchangeHealth.ONLINE;
}
} catch (GateioException | IOException e) {
return ExchangeHealth.OFFLINE;
}

return ExchangeHealth.OFFLINE;
}

@Override
public Ticker getTicker(CurrencyPair currencyPair, Object... args) throws IOException {
return getTicker((Instrument) currencyPair, args);
}


@Override
public Ticker getTicker(Instrument instrument, Object... args) throws IOException {
Objects.requireNonNull(instrument);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
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;
import org.knowm.xchange.instrument.Instrument;

Expand All @@ -19,6 +20,11 @@ public GateioMarketDataServiceRaw(GateioExchange exchange) {
}


public GateioServerTime getGateioServerTime() throws IOException {
return gateio.getServerTime();
}


public List<GateioTicker> getGateioTickers(Instrument instrument) throws IOException {
return gateio.getTickers(GateioAdapters.toString(instrument));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.knowm.xchange.gateio.service;

import static org.assertj.core.api.Assertions.assertThat;

import org.junit.jupiter.api.Test;
import org.knowm.xchange.ExchangeFactory;
import org.knowm.xchange.dto.meta.ExchangeHealth;
import org.knowm.xchange.gateio.GateioExchange;

class GateioMarketDataServiceIntegration {

GateioExchange exchange = ExchangeFactory.INSTANCE.createExchange(GateioExchange.class);

@Test
void exchange_health() {
ExchangeHealth actual = exchange.getMarketDataService().getExchangeHealth();
assertThat(actual).isEqualTo(ExchangeHealth.ONLINE);
}

}

0 comments on commit 26953cd

Please sign in to comment.