Skip to content

Commit

Permalink
Merge pull request #4972 from bigscoop/coinex-fixes
Browse files Browse the repository at this point in the history
[coinex] Add exchange health
  • Loading branch information
timmolter authored Dec 2, 2024
2 parents 757a96b + 62a879c commit 9031b37
Show file tree
Hide file tree
Showing 8 changed files with 121 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
import org.knowm.xchange.coinex.dto.marketdata.CoinexAllMarketStatisticsV1;
import org.knowm.xchange.coinex.dto.marketdata.CoinexChainInfo;
import org.knowm.xchange.coinex.dto.marketdata.CoinexCurrencyPairInfo;
import org.knowm.xchange.coinex.dto.marketdata.CoinexMaintainInfo;
import org.knowm.xchange.coinex.dto.marketdata.CoinexMarketDepth;
import org.knowm.xchange.coinex.dto.marketdata.CoinexSingleMarketStatisticsV1;

Expand All @@ -34,6 +35,12 @@ CoinexResponse<CoinexAllMarketStatisticsV1> allMarketStatistics()
CoinexResponse<CoinexSingleMarketStatisticsV1> singleMarketStatistics(
@QueryParam("market") String market) throws IOException, CoinexException;

@GET
@Path("v2/maintain/info")
CoinexResponse<List<CoinexMaintainInfo>> maintainInfo()
throws IOException, CoinexException;


@GET
@Path("v2/spot/market")
CoinexResponse<List<CoinexCurrencyPairInfo>> marketStatus(@QueryParam("market") String markets)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
package org.knowm.xchange.coinex.config;

import java.time.Clock;
import lombok.Data;

@Data
public final class Config {

private Clock clock;

private static Config instance = new Config();

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

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

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

@Data
@Builder
@Jacksonized
public class CoinexMaintainInfo {

@JsonProperty("started_at")
private Instant startTime;

@JsonProperty("ended_at")
private Instant endTime;

@JsonProperty("scope")
private List<String> scope;

@JsonProperty("announce_enabled")
private Boolean announceEnabled;

@JsonProperty("announce_url")
private URI announceUrl;

@JsonProperty("protect_duration_start")
private Instant protectStart;

@JsonProperty("protect_duration_end")
private Instant protectEnd;

}
Original file line number Diff line number Diff line change
@@ -1,22 +1,27 @@
package org.knowm.xchange.coinex.service;

import java.io.IOException;
import java.time.Instant;
import java.util.Collection;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.commons.lang3.ObjectUtils;
import org.apache.commons.lang3.Validate;
import org.knowm.xchange.coinex.CoinexAdapters;
import org.knowm.xchange.coinex.CoinexErrorAdapter;
import org.knowm.xchange.coinex.CoinexExchange;
import org.knowm.xchange.coinex.config.Config;
import org.knowm.xchange.coinex.dto.CoinexException;
import org.knowm.xchange.coinex.dto.marketdata.CoinexAllMarketStatisticsV1;
import org.knowm.xchange.coinex.dto.marketdata.CoinexMaintainInfo;
import org.knowm.xchange.coinex.dto.marketdata.CoinexSingleMarketStatisticsV1;
import org.knowm.xchange.coinex.service.params.CoinexOrderBookParams;
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;
import org.knowm.xchange.service.marketdata.MarketDataService;
import org.knowm.xchange.service.marketdata.params.CurrencyPairsParam;
Expand All @@ -30,6 +35,38 @@ public CoinexMarketDataService(CoinexExchange exchange) {
super(exchange);
}

@Override
public ExchangeHealth getExchangeHealth() {
try {
List<CoinexMaintainInfo> coinexMaintainInfos = getCoinexMaintainInfo();

for (CoinexMaintainInfo coinexMaintainInfo: coinexMaintainInfos) {
Instant now = Instant.now(Config.getInstance().getClock());
if (ObjectUtils.allNotNull(coinexMaintainInfo.getStartTime(),
coinexMaintainInfo.getEndTime())) {
if (now.isAfter(coinexMaintainInfo.getStartTime()) && now.isBefore(
coinexMaintainInfo.getEndTime())) {
return ExchangeHealth.OFFLINE;
}
}

if (ObjectUtils.allNotNull(coinexMaintainInfo.getProtectStart(),
coinexMaintainInfo.getProtectEnd())) {
if (now.isAfter(coinexMaintainInfo.getProtectStart()) && now.isBefore(
coinexMaintainInfo.getProtectEnd())) {
return ExchangeHealth.OFFLINE;
}
}
}

} catch (IOException e) {
return ExchangeHealth.OFFLINE;
}

return ExchangeHealth.ONLINE;
}


@Override
public Ticker getTicker(CurrencyPair currencyPair, Object... args) throws IOException {
return getTicker((Instrument) currencyPair, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.knowm.xchange.coinex.dto.marketdata.CoinexAllMarketStatisticsV1;
import org.knowm.xchange.coinex.dto.marketdata.CoinexChainInfo;
import org.knowm.xchange.coinex.dto.marketdata.CoinexCurrencyPairInfo;
import org.knowm.xchange.coinex.dto.marketdata.CoinexMaintainInfo;
import org.knowm.xchange.coinex.dto.marketdata.CoinexMarketDepth;
import org.knowm.xchange.coinex.dto.marketdata.CoinexSingleMarketStatisticsV1;
import org.knowm.xchange.coinex.service.params.CoinexOrderBookParams;
Expand All @@ -21,6 +22,12 @@ public CoinexMarketDataServiceRaw(CoinexExchange exchange) {
super(exchange);
}


public List<CoinexMaintainInfo> getCoinexMaintainInfo() throws IOException {
return coinex.maintainInfo().getData();
}


public List<CoinexChainInfo> getAllCoinexChainInfos() throws IOException {
return new ArrayList<>(coinex.allChainInfos().getData().values());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,8 @@ public Collection<Order> getOrder(OrderQueryParams... orderQueryParams) throws I
OrderQueryParamInstrument params = (OrderQueryParamInstrument) orderQueryParams[0];

try {
CoinexOrder gateioOrder = orderStatus(params.getInstrument(), params.getOrderId());
return Collections.singletonList(CoinexAdapters.toOrder(gateioOrder));
CoinexOrder order = orderStatus(params.getInstrument(), params.getOrderId());
return Collections.singletonList(CoinexAdapters.toOrder(order));
} catch (CoinexException e) {
throw CoinexErrorAdapter.adapt(e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,18 @@
import org.knowm.xchange.dto.Order.OrderType;
import org.knowm.xchange.dto.marketdata.OrderBook;
import org.knowm.xchange.dto.marketdata.Ticker;
import org.knowm.xchange.dto.meta.ExchangeHealth;

class CoinexMarketDataServiceIntegration {

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

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


@Test
void valid_tickers() throws IOException {
List<Ticker> tickers = exchange.getMarketDataService().getTickers(null);
Expand Down
4 changes: 4 additions & 0 deletions xchange-coinex/src/test/resources/rest/common.http
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
### Get Maintenance Information
GET {{api_host}}/v2/maintain/info


0 comments on commit 9031b37

Please sign in to comment.