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

chore: Replace bybit with crypto.com #248

Merged
merged 5 commits into from
Dec 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
23 changes: 11 additions & 12 deletions src/xrc-tests/src/mock_responses/exchanges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,18 +63,17 @@ where
(timestamp * 1_000) as i64,
1677584399999i64
]]),
Exchange::Bybit(_) => json!({
Exchange::Crypto(_) => json!({
"result": {
"list": [
[
(timestamp * 1_000).to_string(),
rate,
"1.00",
"1.00",
"1.00",
"1.00",
"1.00",
]
"data": [
{
"o": rate,
"h": "1.00",
"l": "1.00",
"c": "1.00",
"v": "0.0000",
"t": timestamp * 1_000
}
]
}
}),
Expand Down Expand Up @@ -102,6 +101,6 @@ pub fn build_common_responses(
xrc::Exchange::GateIo(_) => Some("42.64"),
xrc::Exchange::Mexc(_) => Some("46.101"),
xrc::Exchange::Poloniex(_) => Some("46.022"),
xrc::Exchange::Bybit(_) => Some("41.96000000"),
xrc::Exchange::Crypto(_) => Some("41.96000000"),
})
}
21 changes: 10 additions & 11 deletions src/xrc-tests/src/mock_responses/stablecoin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,18 +41,17 @@ fn sample_stablecoin_json(exchange: &Exchange) -> ResponseBody {
1677584340000i64,
1677584399999i64
]]),
Exchange::Bybit(_) => json!({
Exchange::Crypto(_) => json!({
"result": {
"list": [
[
"1614596340000",
"0.99",
"1.00",
"1.00",
"1.00",
"1.00",
"1.00",
]
"data": [
{
"o": "0.99",
"h": "1.00",
"l": "1.00",
"c": "1.00",
"v": "0.0000",
"t": 1614596340000i64
}
]
}
}),
Expand Down
2 changes: 1 addition & 1 deletion src/xrc-tests/src/tests/basic_exchange_rates.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ fn basic_exchange_rates() {
xrc::Exchange::GateIo(_) => Some("3.90"),
xrc::Exchange::Mexc(_) => Some("3.911"),
xrc::Exchange::Poloniex(_) => Some("4.005"),
xrc::Exchange::Bybit(_) => Some("3.91"),
xrc::Exchange::Crypto(_) => Some("3.91"),
},
)
.chain(mock_responses::exchanges::build_common_responses(
Expand Down
2 changes: 1 addition & 1 deletion src/xrc-tests/src/tests/caching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ fn caching() {
xrc::Exchange::GateIo(_) => Some("3.90"),
xrc::Exchange::Mexc(_) => Some("3.911"),
xrc::Exchange::Poloniex(_) => Some("4.005"),
xrc::Exchange::Bybit(_) => Some("3.91"),
xrc::Exchange::Crypto(_) => Some("3.91"),
},
)
.chain(mock_responses::exchanges::build_common_responses(
Expand Down
8 changes: 4 additions & 4 deletions src/xrc-tests/src/tests/get_icp_xdr_rate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use crate::{
/// i. For all requests in the following test, this should result in a CXDR/USD with the following rates: [ 1336769190, 1336769190 ].
/// 1. The XRC retrieves the ICP/USDT rates from the mock exchange responses (request 1 responses).
/// i. For request 1, this should result in the following rates discovered:
/// GateIo Okx Bybit Mexc Coinbase KuCoin Poloniex
/// GateIo Okx Crypto Mexc Coinbase KuCoin Poloniex
/// [ 3900000000, 3900000000, 3910000000, 3911000000, 3920000000, 3920000000, 4005000000, ]
/// 2. The XRC retrieves the stablecoin rates from the mock exchanges.
/// i. For request 1, DAI: [ 950000000, 990000000, 990000000, 1000000000, 1020000000, 1030927835 ]
Expand Down Expand Up @@ -74,7 +74,7 @@ fn get_icp_xdr_rate() {
xrc::Exchange::GateIo(_) => Some("3.90"),
xrc::Exchange::Mexc(_) => Some("3.911"),
xrc::Exchange::Poloniex(_) => Some("4.005"),
xrc::Exchange::Bybit(_) => Some("3.91"),
xrc::Exchange::Crypto(_) => Some("3.91"),
},
)
// Request 2 mock exchange responses.
Expand All @@ -88,7 +88,7 @@ fn get_icp_xdr_rate() {
xrc::Exchange::GateIo(_) => Some("4.28"),
xrc::Exchange::Mexc(_) => Some("4.291"),
xrc::Exchange::Poloniex(_) => Some("4.38"),
xrc::Exchange::Bybit(_) => Some("4.29"),
xrc::Exchange::Crypto(_) => Some("4.29"),
},
))
// Request 3 mock exchange responses.
Expand All @@ -102,7 +102,7 @@ fn get_icp_xdr_rate() {
xrc::Exchange::GateIo(_) => Some("5.16"),
xrc::Exchange::Mexc(_) => Some("5.171"),
xrc::Exchange::Poloniex(_) => Some("5.26"),
xrc::Exchange::Bybit(_) => Some("5.17"),
xrc::Exchange::Crypto(_) => Some("5.17"),
},
))
.chain(mock_responses::stablecoin::build_responses(
Expand Down
4 changes: 2 additions & 2 deletions src/xrc-tests/src/tests/misbehavior.rs
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ fn misbehavior() {
xrc::Exchange::GateIo(_) => Some("3.90"),
xrc::Exchange::Mexc(_) => Some("3.911"),
xrc::Exchange::Poloniex(_) => Some("4.005"),
xrc::Exchange::Bybit(_) => Some("100000.0"),
xrc::Exchange::Crypto(_) => Some("100000.0"),
},
)
.chain(mock_responses::exchanges::build_responses(
Expand All @@ -191,7 +191,7 @@ fn misbehavior() {
xrc::Exchange::GateIo(_) => Some("42.64"),
xrc::Exchange::Mexc(_) => Some("46.101"),
xrc::Exchange::Poloniex(_) => Some("46.022"),
xrc::Exchange::Bybit(_) => Some("10000.96000000"),
xrc::Exchange::Crypto(_) => Some("10000.96000000"),
},
))
.chain(mock_responses::stablecoin::build_responses(
Expand Down
65 changes: 36 additions & 29 deletions src/xrc/src/exchanges.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ macro_rules! exchanges {

}

exchanges! { Coinbase, KuCoin, Okx, GateIo, Mexc, Poloniex, Bybit }
exchanges! { Coinbase, KuCoin, Okx, GateIo, Mexc, Poloniex, Crypto }

/// Used to determine how to parse the extracted value returned from
/// [extract_rate]'s `extract_fn` argument.
Expand Down Expand Up @@ -441,22 +441,29 @@ impl IsExchange for Poloniex {
}
}

/// Bybit
/// Crypto
#[derive(Deserialize)]
struct BybitResponse {
result: BybitResponseResult,
struct CryptoResponse {
result: CryptoResponseResult,
}

#[derive(Deserialize)]
struct BybitResponseResult {
list: ByBitResponseResultList,
struct CryptoResponseResult {
data: Vec<CryptoResponseResultData>,
}

type ByBitResponseResultList = Vec<(String, String, String, String, String, String, String)>;
#[derive(Deserialize)]
struct CryptoResponseResultData {
o: String,
}

impl IsExchange for Bybit {
impl IsExchange for Crypto {
fn get_base_url(&self) -> &str {
"https://api.bybit.com/v5/market/kline?category=linear&symbol=BASE_ASSETQUOTE_ASSET&interval=1&start=START_TIME&limit=1"
"https://api.crypto.com/exchange/v1/public/get-candlestick?instrument_name=BASE_ASSET_QUOTE_ASSET&timeframe=1m&start_ts=START_TIME&count=1"
}

fn supports_ipv6(&self) -> bool {
true
}

fn format_start_time(&self, timestamp: u64) -> String {
Expand All @@ -465,12 +472,12 @@ impl IsExchange for Bybit {
}

fn extract_rate(&self, bytes: &[u8]) -> Result<u64, ExtractError> {
extract_rate(bytes, |response: BybitResponse| {
extract_rate(bytes, |response: CryptoResponse| {
response
.result
.list
.data
.get(0)
.map(|kline| ExtractedValue::Str(kline.1.clone()))
.map(|kline| ExtractedValue::Str(kline.o.clone()))
})
}
}
Expand All @@ -497,8 +504,8 @@ mod test {
assert_eq!(exchange.to_string(), "Mexc");
let exchange = Exchange::Poloniex(Poloniex);
assert_eq!(exchange.to_string(), "Poloniex");
let exchange = Exchange::Bybit(Bybit);
assert_eq!(exchange.to_string(), "Bybit");
let exchange = Exchange::Crypto(Crypto);
assert_eq!(exchange.to_string(), "Crypto");
}

/// The function tests if the if the macro correctly generates derive copies by
Expand Down Expand Up @@ -532,9 +539,9 @@ mod test {
let query_string = poloniex.get_url("btc", "icp", timestamp);
assert_eq!(query_string, "https://api.poloniex.com/markets/BTC_ICP/candles?interval=MINUTE_1&startTime=1661523960000&endTime=1661523960001");

let bybit = Bybit;
let query_string = bybit.get_url("btc", "icp", timestamp);
assert_eq!(query_string, "https://api.bybit.com/v5/market/kline?category=linear&symbol=BTCICP&interval=1&start=1661523960000&limit=1");
let Crypto = Crypto;
nategurian marked this conversation as resolved.
Show resolved Hide resolved
let query_string = Crypto.get_url("btc", "icp", timestamp);
assert_eq!(query_string, "https://api.Crypto.com/v5/market/kline?category=linear&symbol=BTCICP&interval=1&start=1661523960000&limit=1");
}

/// The function test if the information about IPv6 support is correct.
Expand All @@ -552,8 +559,8 @@ mod test {
assert!(!mexc.supports_ipv6());
let poloniex = Poloniex;
assert!(!poloniex.supports_ipv6());
let bybit = Bybit;
assert!(!bybit.supports_ipv6());
let Crypto = Crypto;
assert!(!Crypto.supports_ipv6());
}

/// The function tests if the USD asset type is correct.
Expand All @@ -571,8 +578,8 @@ mod test {
assert_eq!(mexc.supported_usd_asset(), usdt_asset());
let poloniex = Poloniex;
assert_eq!(poloniex.supported_usd_asset(), usdt_asset());
let bybit = Bybit;
assert_eq!(bybit.supported_usd_asset(), usdt_asset());
let Crypto = Crypto;
assert_eq!(Crypto.supported_usd_asset(), usdt_asset());
}

/// The function tests if the supported stablecoins are correct.
Expand Down Expand Up @@ -602,9 +609,9 @@ mod test {
poloniex.supported_stablecoin_pairs(),
&[(DAI, USDT), (USDT, USDC)]
);
let bybit = Bybit;
let Crypto = Crypto;
assert_eq!(
bybit.supported_stablecoin_pairs(),
Crypto.supported_stablecoin_pairs(),
&[(DAI, USDT), (USDC, USDT)]
);
}
Expand Down Expand Up @@ -663,12 +670,12 @@ mod test {
assert!(matches!(extracted_rate, Ok(rate) if rate == 46_022_000_000));
}

/// The function tests if the Bybit struct returns the correct exchange rate.
/// The function tests if the Crypto struct returns the correct exchange rate.
#[test]
fn extract_rate_from_bybit() {
let bybit = Bybit;
let query_response = load_file("test-data/exchanges/bybit.json");
let extracted_rate = bybit.extract_rate(&query_response);
fn extract_rate_from_crypto() {
let crypto = Crypto;
let query_response = load_file("test-data/exchanges/crypto.json");
let extracted_rate = crypto.extract_rate(&query_response);
assert!(matches!(extracted_rate, Ok(rate) if rate == 47_328_300_000));
}

Expand Down Expand Up @@ -727,7 +734,7 @@ mod test {
assert_eq!(exchange.max_response_bytes(), ONE_KIB);
let exchange = Exchange::Poloniex(Poloniex);
assert_eq!(exchange.max_response_bytes(), ONE_KIB);
let exchange = Exchange::Bybit(Bybit);
let exchange = Exchange::Crypto(Crypto);
assert_eq!(exchange.max_response_bytes(), ONE_KIB);
}

Expand Down
21 changes: 0 additions & 21 deletions src/xrc/test-data/exchanges/bybit.json

This file was deleted.

17 changes: 17 additions & 0 deletions src/xrc/test-data/exchanges/crypto.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"id": -1,
"method": "public/get-candlestick",
"code": 0,
"result": {
"interval": "1m",
"data": [ {
"o": "47.3283",
"h": "47.3587",
"l": "47.324",
"c": "47.350",
"v": "0.0000",
"t": 1700127660000
} ],
"instrument_name": "ICP_USDT"
}
}
Loading