Skip to content

Commit

Permalink
chore: Replace bybit with crypto.com (#248)
Browse files Browse the repository at this point in the history
ByBit is not performing well on mainnet, so we have decided to replace
it with Crypto.com as it appears to be a valid source of information in
the US and worldwide.
  • Loading branch information
dfinity-ryancroote authored Dec 1, 2023
1 parent 9ee933f commit b6b25bc
Show file tree
Hide file tree
Showing 9 changed files with 88 additions and 83 deletions.
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::CryptoCom(_) => 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::CryptoCom(_) => 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::CryptoCom(_) => 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::CryptoCom(_) => 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::CryptoCom(_) => 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::CryptoCom(_) => 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::CryptoCom(_) => 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::CryptoCom(_) => 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::CryptoCom(_) => 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::CryptoCom(_) => Some("10000.96000000"),
},
))
.chain(mock_responses::stablecoin::build_responses(
Expand Down
73 changes: 42 additions & 31 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, CryptoCom }

/// 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 CryptoCom {
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,14 +472,18 @@ 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()))
})
}

fn supported_stablecoin_pairs(&self) -> &[(&str, &str)] {
&[(DAI, USDT), (USDT, USDC)]
}
}

#[cfg(test)]
Expand All @@ -497,8 +508,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::CryptoCom(CryptoCom);
assert_eq!(exchange.to_string(), "CryptoCom");
}

/// The function tests if the if the macro correctly generates derive copies by
Expand Down Expand Up @@ -532,9 +543,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 = CryptoCom;
let query_string = crypto.get_url("btc", "icp", timestamp);
assert_eq!(query_string, "https://api.crypto.com/exchange/v1/public/get-candlestick?instrument_name=BTC_ICP&timeframe=1m&start_ts=1661523960000&count=1");
}

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

/// The function tests if the USD asset type is correct.
Expand All @@ -571,8 +582,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 = CryptoCom;
assert_eq!(crypto.supported_usd_asset(), usdt_asset());
}

/// The function tests if the supported stablecoins are correct.
Expand Down Expand Up @@ -602,10 +613,10 @@ mod test {
poloniex.supported_stablecoin_pairs(),
&[(DAI, USDT), (USDT, USDC)]
);
let bybit = Bybit;
let crypto = CryptoCom;
assert_eq!(
bybit.supported_stablecoin_pairs(),
&[(DAI, USDT), (USDC, USDT)]
crypto.supported_stablecoin_pairs(),
&[(DAI, USDT), (USDT, USDC)]
);
}

Expand Down Expand Up @@ -663,12 +674,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 = CryptoCom;
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,15 +738,15 @@ 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::CryptoCom(CryptoCom);
assert_eq!(exchange.max_response_bytes(), ONE_KIB);
}

#[test]
#[cfg(not(feature = "ipv4-support"))]
fn is_available() {
let available_exchanges_count = EXCHANGES.iter().filter(|e| e.is_available()).count();
assert_eq!(available_exchanges_count, 3);
assert_eq!(available_exchanges_count, 4);
}

#[test]
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"
}
}

0 comments on commit b6b25bc

Please sign in to comment.