This repository has been archived by the owner on Mar 7, 2023. It is now read-only.
forked from knowm/XChange
-
Notifications
You must be signed in to change notification settings - Fork 2
[Blockchain] marketDataService implementation #4
Open
scuevas-bc
wants to merge
3
commits into
bcdc-tradeservice-implementation
Choose a base branch
from
bcdc-marketservice-implementation
base: bcdc-tradeservice-implementation
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
22 changes: 22 additions & 0 deletions
22
.../src/main/java/org/knowm/xchange/blockchain/dto/marketdata/BlockchainMarketDataOrder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package org.knowm.xchange.blockchain.dto.marketdata; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.annotation.JsonProperty; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.extern.jackson.Jacksonized; | ||
|
||
import java.math.BigDecimal; | ||
|
||
@Data | ||
@Builder | ||
@Jacksonized | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class BlockchainMarketDataOrder { | ||
|
||
@JsonProperty("px") | ||
private final BigDecimal price; | ||
@JsonProperty("qty") | ||
private final BigDecimal quantity; | ||
private final Long num; | ||
} |
23 changes: 23 additions & 0 deletions
23
...kchain/src/main/java/org/knowm/xchange/blockchain/dto/marketdata/BlockchainOrderBook.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
package org.knowm.xchange.blockchain.dto.marketdata; | ||
|
||
import com.fasterxml.jackson.annotation.JsonIgnoreProperties; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import lombok.Builder; | ||
import lombok.Data; | ||
import lombok.extern.jackson.Jacksonized; | ||
import org.knowm.xchange.currency.CurrencyPair; | ||
import org.knowm.xchange.utils.jackson.CurrencyPairDeserializer; | ||
|
||
import java.util.List; | ||
|
||
@Data | ||
@Builder | ||
@Jacksonized | ||
@JsonIgnoreProperties(ignoreUnknown = true) | ||
public class BlockchainOrderBook { | ||
|
||
@JsonDeserialize(using = CurrencyPairDeserializer.class) | ||
private final CurrencyPair symbol; | ||
private final List<BlockchainMarketDataOrder> bids; | ||
private final List<BlockchainMarketDataOrder> asks; | ||
} |
72 changes: 72 additions & 0 deletions
72
...chain/src/main/java/org/knowm/xchange/blockchain/service/BlockchainMarketDataService.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package org.knowm.xchange.blockchain.service; | ||
|
||
import org.knowm.xchange.blockchain.BlockchainAdapters; | ||
import org.knowm.xchange.blockchain.BlockchainAuthenticated; | ||
import org.knowm.xchange.blockchain.BlockchainErrorAdapter; | ||
import org.knowm.xchange.blockchain.BlockchainExchange; | ||
import org.knowm.xchange.blockchain.dto.BlockchainException; | ||
import org.knowm.xchange.client.ResilienceRegistries; | ||
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.marketdata.Trades; | ||
import org.knowm.xchange.exceptions.NotYetImplementedForExchangeException; | ||
import org.knowm.xchange.instrument.Instrument; | ||
import org.knowm.xchange.service.marketdata.MarketDataService; | ||
import org.knowm.xchange.service.marketdata.params.Params; | ||
|
||
import java.io.IOException; | ||
import java.util.List; | ||
|
||
import static org.knowm.xchange.blockchain.BlockchainConstants.NOT_IMPLEMENTED_YET; | ||
|
||
public class BlockchainMarketDataService extends BlockchainMarketDataServiceRaw implements MarketDataService { | ||
|
||
public BlockchainMarketDataService(BlockchainExchange exchange, BlockchainAuthenticated blockchainApi, ResilienceRegistries resilienceRegistries) { | ||
super(exchange, blockchainApi, resilienceRegistries); | ||
} | ||
|
||
@Override | ||
public Ticker getTicker(CurrencyPair currencyPair, Object... args) throws IOException { | ||
throw new NotYetImplementedForExchangeException(NOT_IMPLEMENTED_YET); | ||
} | ||
|
||
@Override | ||
public Ticker getTicker(Instrument instrument, Object... args) throws IOException { | ||
throw new NotYetImplementedForExchangeException(NOT_IMPLEMENTED_YET); | ||
} | ||
|
||
@Override | ||
public List<Ticker> getTickers(Params params) { | ||
throw new NotYetImplementedForExchangeException(NOT_IMPLEMENTED_YET); | ||
} | ||
|
||
@Override | ||
public OrderBook getOrderBook(CurrencyPair currencyPair, Object... args) throws IOException { | ||
try { | ||
return BlockchainAdapters.toOrderBook(this.getOrderBookL3(currencyPair)); | ||
} catch (BlockchainException e) { | ||
throw BlockchainErrorAdapter.adapt(e); | ||
} | ||
} | ||
|
||
@Override | ||
public OrderBook getOrderBook(Instrument instrument, Object... args) throws IOException { | ||
try { | ||
return BlockchainAdapters.toOrderBook(this.getOrderBookL3(BlockchainAdapters.toCurrencyPair(instrument))); | ||
} catch (BlockchainException e) { | ||
throw BlockchainErrorAdapter.adapt(e); | ||
} | ||
} | ||
|
||
@Override | ||
public Trades getTrades(CurrencyPair currencyPair, Object... args) throws IOException { | ||
throw new NotYetImplementedForExchangeException(NOT_IMPLEMENTED_YET); | ||
} | ||
|
||
@Override | ||
public Trades getTrades(Instrument instrument, Object... args) throws IOException { | ||
throw new NotYetImplementedForExchangeException(NOT_IMPLEMENTED_YET); | ||
} | ||
|
||
} |
28 changes: 28 additions & 0 deletions
28
...in/src/main/java/org/knowm/xchange/blockchain/service/BlockchainMarketDataServiceRaw.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
package org.knowm.xchange.blockchain.service; | ||
|
||
import org.knowm.xchange.blockchain.BlockchainAdapters; | ||
import org.knowm.xchange.blockchain.BlockchainAuthenticated; | ||
import org.knowm.xchange.blockchain.BlockchainExchange; | ||
import org.knowm.xchange.blockchain.dto.BlockchainException; | ||
import org.knowm.xchange.blockchain.dto.marketdata.BlockchainOrderBook; | ||
import org.knowm.xchange.client.ResilienceRegistries; | ||
import org.knowm.xchange.currency.CurrencyPair; | ||
|
||
import java.io.IOException; | ||
|
||
import static org.knowm.xchange.blockchain.BlockchainConstants.ENDPOINT_RATE_LIMIT; | ||
import static org.knowm.xchange.blockchain.BlockchainConstants.GET_ORDER_BOOK_L3; | ||
|
||
public class BlockchainMarketDataServiceRaw extends BlockchainBaseService{ | ||
|
||
protected BlockchainMarketDataServiceRaw(BlockchainExchange exchange, BlockchainAuthenticated blockchainApi, ResilienceRegistries resilienceRegistries) { | ||
super(exchange, blockchainApi, resilienceRegistries); | ||
} | ||
|
||
protected BlockchainOrderBook getOrderBookL3(CurrencyPair currencyPair) throws IOException, BlockchainException { | ||
return decorateApiCall(() -> this.blockchainApi.getOrderBookL3(BlockchainAdapters.toSymbol(currencyPair))) | ||
.withRetry(retry(GET_ORDER_BOOK_L3)) | ||
.withRateLimiter(rateLimiter(ENDPOINT_RATE_LIMIT)) | ||
.call(); | ||
} | ||
} |
53 changes: 53 additions & 0 deletions
53
.../src/test/java/org/knowm/xchange/blockchain/service/marketdata/MarketDataServiceTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
package org.knowm.xchange.blockchain.service.marketdata; | ||
|
||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.knowm.xchange.blockchain.BlockchainExchange; | ||
import org.knowm.xchange.blockchain.service.BlockchainBaseTest; | ||
import org.knowm.xchange.currency.CurrencyPair; | ||
import org.knowm.xchange.dto.marketdata.OrderBook; | ||
import org.knowm.xchange.exceptions.InternalServerException; | ||
import org.knowm.xchange.instrument.Instrument; | ||
import org.knowm.xchange.service.marketdata.MarketDataService; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
import static org.assertj.core.api.Assertions.catchThrowable; | ||
import static org.knowm.xchange.blockchain.service.utils.BlockchainConstants.*; | ||
|
||
public class MarketDataServiceTest extends BlockchainBaseTest { | ||
private MarketDataService service; | ||
|
||
@Before | ||
public void init() { | ||
BlockchainExchange exchange = createExchange(); | ||
service = exchange.getMarketDataService(); | ||
} | ||
|
||
@Test(timeout = 2000) | ||
public void getOrderBookSuccess() throws Exception { | ||
stubGet(ORDERBOOK_JSON, 200, URL_ORDERBOOOK_L3); | ||
OrderBook response = service.getOrderBook(CurrencyPair.BTC_USD); | ||
assertThat(response).isNotNull(); | ||
assertThat(response.getAsks().get(0).getLimitPrice()).isNotNull().isPositive(); | ||
assertThat(response.getBids().get(0).getLimitPrice()).isNotNull().isPositive(); | ||
} | ||
|
||
@Test(timeout = 2000) | ||
public void getOrderBookFailure() { | ||
stubGet(ORDERBOOK_FAILURE_JSON, 500, URL_ORDERBOOOK_L3); | ||
Throwable exception = catchThrowable(() -> service.getOrderBook(CurrencyPair.BTC_USD)); | ||
assertThat(exception) | ||
.isInstanceOf(InternalServerException.class) | ||
.hasMessage(STATUS_CODE_500); | ||
} | ||
|
||
@Test(timeout = 2000) | ||
public void getOrderBookByInstrumentSuccess() throws Exception { | ||
stubGet(ORDERBOOK_JSON, 200, URL_ORDERBOOOK_L3); | ||
Instrument instrument = CurrencyPair.BTC_USD; | ||
OrderBook response = service.getOrderBook(instrument); | ||
assertThat(response).isNotNull(); | ||
assertThat(response.getAsks().get(0).getLimitPrice()).isNotNull().isPositive(); | ||
assertThat(response.getBids().get(0).getLimitPrice()).isNotNull().isPositive(); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
an order or in order?