Skip to content

Commit

Permalink
fix(rust): change Binance futures symbols to lowercase.
Browse files Browse the repository at this point in the history
  • Loading branch information
nkaz001 committed Oct 1, 2024
1 parent 5890f0b commit f49910f
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 8 deletions.
2 changes: 2 additions & 0 deletions connector/src/binancefutures/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ impl Connector for BinanceFutures {
id: u64,
ev_tx: UnboundedSender<PublishMessage>,
) {
// Binance futures symbols must be lowercase to subscribe to the WebSocket stream.
let symbol = symbol.to_lowercase();
let mut instruments = self.instruments.lock().unwrap();
if instruments.contains_key(&symbol) {
let order_manager = self.order_manager.lock().unwrap();
Expand Down
6 changes: 3 additions & 3 deletions connector/src/binancefutures/msg/rest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use hftbacktest::types::{OrdType, Side, Status, TimeInForce};
use serde::Deserialize;

use super::{from_str_to_side, from_str_to_status, from_str_to_tif, from_str_to_type};
use crate::utils::{from_str_to_f64, from_str_to_f64_opt, to_uppercase};
use crate::utils::{from_str_to_f64, from_str_to_f64_opt, to_lowercase};

#[derive(Deserialize, Debug)]
#[serde(untagged)]
Expand Down Expand Up @@ -56,7 +56,7 @@ pub struct OrderResponse {
pub stop_price: f64,
#[serde(rename = "closePosition")]
pub close_position: bool,
#[serde(deserialize_with = "to_uppercase")]
#[serde(deserialize_with = "to_lowercase")]
pub symbol: String,
// for Coin-M futures
// pub pair: String,
Expand Down Expand Up @@ -132,7 +132,7 @@ pub struct PositionInformationV2 {
pub notional: f64,
#[serde(rename = "isolatedWallet")]
pub isolated_wallet: String,
#[serde(deserialize_with = "to_uppercase")]
#[serde(deserialize_with = "to_lowercase")]
pub symbol: String,
#[serde(rename = "unRealizedProfit")]
pub unrealized_pnl: String,
Expand Down
10 changes: 5 additions & 5 deletions connector/src/binancefutures/msg/stream.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use hftbacktest::types::{OrdType, Side, Status, TimeInForce};
use serde::Deserialize;

use super::{from_str_to_side, from_str_to_status, from_str_to_tif, from_str_to_type};
use crate::utils::{from_str_to_f64, to_uppercase};
use crate::utils::{from_str_to_f64, to_lowercase};

#[derive(Deserialize, Debug)]
#[serde(tag = "e")]
Expand All @@ -26,7 +26,7 @@ pub struct Depth {
#[serde(rename = "E")]
pub event_time: i64,
#[serde(rename = "s")]
#[serde(deserialize_with = "to_uppercase")]
#[serde(deserialize_with = "to_lowercase")]
pub symbol: String,
// for Coin-M futures
// #[serde(rename = "ps")]
Expand All @@ -50,7 +50,7 @@ pub struct Trade {
#[serde(rename = "E")]
pub event_time: i64,
#[serde(rename = "s")]
#[serde(deserialize_with = "to_uppercase")]
#[serde(deserialize_with = "to_lowercase")]
pub symbol: String,
#[serde(rename = "t")]
pub id: i64,
Expand Down Expand Up @@ -102,7 +102,7 @@ pub struct Balance {
#[derive(Deserialize, Debug)]
pub struct Position {
#[serde(rename = "s")]
#[serde(deserialize_with = "to_uppercase")]
#[serde(deserialize_with = "to_lowercase")]
pub symbol: String,
#[serde(rename = "pa")]
#[serde(deserialize_with = "from_str_to_f64")]
Expand Down Expand Up @@ -139,7 +139,7 @@ pub struct OrderTradeUpdate {
#[derive(Deserialize, Debug)]
pub struct Order {
#[serde(rename = "s")]
#[serde(deserialize_with = "to_uppercase")]
#[serde(deserialize_with = "to_lowercase")]
pub symbol: String,
#[serde(rename = "c")]
pub client_order_id: String,
Expand Down
8 changes: 8 additions & 0 deletions connector/src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,14 @@ where
Ok(s.to_uppercase())
}

pub fn to_lowercase<'de, D>(deserializer: D) -> Result<String, D::Error>
where
D: Deserializer<'de>,
{
let s: &str = Deserialize::deserialize(deserializer)?;
Ok(s.to_lowercase())
}

pub fn sign_hmac_sha256(secret: &str, s: &str) -> String {
let mut mac = Hmac::<Sha256>::new_from_slice(secret.as_bytes()).unwrap();
mac.update(s.as_bytes());
Expand Down

0 comments on commit f49910f

Please sign in to comment.