Skip to content

Commit 4f0a23a

Browse files
committed
refactor: recv_window to std::time::Duration + fixes of some old code
1 parent 1d4e661 commit 4f0a23a

File tree

30 files changed

+195
-161
lines changed

30 files changed

+195
-161
lines changed

Cargo.lock

Lines changed: 2 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

examples/binance/market_perp.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, str::FromStr as _};
1+
use std::{env, str::FromStr as _, time::Duration};
22

33
use v_exchanges::prelude::*;
44

@@ -20,9 +20,9 @@ async fn main() {
2020

2121
if let (Ok(key), Ok(secret)) = (env::var("BINANCE_TIGER_READ_PUBKEY"), env::var("BINANCE_TIGER_READ_SECRET")) {
2222
binance.auth(key, secret.into());
23-
let balance_usdt = binance.asset_balance("USDT".into(), symbol.instrument, Some(10_000)).await.unwrap();
23+
let balance_usdt = binance.asset_balance("USDT".into(), symbol.instrument, Some(Duration::from_millis(10_000))).await.unwrap();
2424
dbg!(&balance_usdt);
25-
let balances = binance.balances(symbol.instrument, Some(10_000)).await.unwrap();
25+
let balances = binance.balances(symbol.instrument, Some(Duration::from_millis(10_000))).await.unwrap();
2626
dbg!(&balances);
2727
} else {
2828
eprintln!("BINANCE_TIGER_READ_PUBKEY or BINANCE_TIGER_READ_SECRET is missing, skipping private API methods.");

examples/binance/orders.rs

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,6 @@
1-
use std::env;
1+
use std::{env, time::Duration};
22

3-
use v_exchanges::{
4-
Binance,
5-
binance::perp::account::{IncomeRequest, OrderRequest, OrderType, PositionSide, TimeInForce},
6-
prelude::*,
7-
};
3+
use v_exchanges::{Binance, binance::perp::account::IncomeRequest, prelude::*};
84

95
#[tokio::main]
106
async fn main() {
@@ -26,7 +22,7 @@ async fn main() {
2622
page: None,
2723
};
2824

29-
match v_exchanges::binance::perp::account::income_history(&binance, income_req, Some(5000)).await {
25+
match v_exchanges::binance::perp::account::income_history(&binance, income_req, Some(Duration::from_millis(5000))).await {
3026
Ok(records) => {
3127
println!("Found {} income records:", records.len());
3228
for record in records.iter().take(5) {
@@ -58,7 +54,7 @@ async fn main() {
5854
new_client_order_id: None,
5955
};
6056
61-
match v_exchanges::binance::perp::account::place_order(&binance, order_req, Some(5000)).await {
57+
match v_exchanges::binance::perp::account::place_order(&binance, order_req, Some(Duration::from_millis(5000))).await {
6258
Ok(response) => {
6359
println!("Order placed successfully!");
6460
println!(" Order ID: {}", response.order_id);

examples/bybit/market.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::{env, str::FromStr as _};
1+
use std::{env, str::FromStr as _, time::Duration};
22

33
use v_exchanges::{Bybit, prelude::*};
44

@@ -35,7 +35,7 @@ async fn private(c: &dyn Exchange, symbol: Symbol) {
3535
let balances = c.balances(symbol.instrument, None).await.unwrap();
3636
println!("{balances:?}");
3737

38-
let balance_usdc = c.asset_balance("USDC".into(), symbol.instrument, Some(5000)).await.unwrap();
38+
let balance_usdc = c.asset_balance("USDC".into(), symbol.instrument, Some(Duration::from_millis(5000))).await.unwrap();
3939
println!("{balance_usdc:?}");
4040
}
4141

examples/healthcheck.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use std::env;
1+
use std::{env, time::Duration};
22

33
use v_exchanges::prelude::*;
44

@@ -27,7 +27,7 @@ async fn check_binance() {
2727
let mut binance = ExchangeName::Binance.init_client();
2828
binance.auth(key, secret.into());
2929

30-
match binance.balances(Instrument::Perp, Some(5000)).await {
30+
match binance.balances(Instrument::Perp, Some(Duration::from_millis(5000))).await {
3131
Ok(_) => println!("✅ Binance: API key is valid and active"),
3232
Err(e) => println!("❌ Binance: API key error - {}", e),
3333
}
@@ -47,7 +47,7 @@ async fn check_bybit() {
4747
let mut bybit = ExchangeName::Bybit.init_client();
4848
bybit.auth(key, secret.into());
4949

50-
match bybit.balances(Instrument::Perp, Some(5000)).await {
50+
match bybit.balances(Instrument::Perp, Some(Duration::from_millis(5000))).await {
5151
Ok(_) => println!("✅ Bybit: API key is valid and active"),
5252
Err(e) => println!("❌ Bybit: API key error - {}", e),
5353
}
@@ -113,7 +113,7 @@ async fn check_mexc() {
113113
let mut mexc = ExchangeName::Mexc.init_client();
114114
mexc.auth(key, secret.into());
115115

116-
match mexc.balances(Instrument::Perp, Some(5000)).await {
116+
match mexc.balances(Instrument::Perp, Some(Duration::from_millis(5000))).await {
117117
Ok(_) => println!("✅ MEXC: API key is valid and active"),
118118
Err(e) => {
119119
let err_str = e.to_string();

v_exchanges/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ ignored = ["derive-new", "color-eyre", "serde", "tokio", "v_utils"]
2626
[features]
2727
default = ["major", "data"]
2828

29-
full = ["major", "data", "bitflyer", "coincheck"]
30-
major = ["binance", "bybit", "kucoin", "mexc"]
29+
full = ["major", "data", "mexc", "kucoin", "bitflyer", "coincheck"]
30+
major = ["binance", "bybit"]
3131

3232
binance = ["v_exchanges_adapters/binance"]
3333
bybit = ["v_exchanges_adapters/bybit"]

v_exchanges/src/binance/market.rs

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,13 @@ use crate::{
2020
};
2121

2222
// klines {{{
23-
pub async fn klines(client: &v_exchanges_adapters::Client, symbol: Symbol, tf: BinanceTimeframe, range: RequestRange, recv_window: Option<u16>) -> Result<Klines, ExchangeError> {
23+
pub async fn klines(
24+
client: &v_exchanges_adapters::Client,
25+
symbol: Symbol,
26+
tf: BinanceTimeframe,
27+
range: RequestRange,
28+
recv_window: Option<std::time::Duration>,
29+
) -> Result<Klines, ExchangeError> {
2430
recv_window_check!(recv_window, GetOptions::<BinanceOptions>::default_options(client));
2531
//TODO: test if embedding params into the url works more consistently (comp number of pairs axum-site is ablle ot get)
2632
range.ensure_allowed(1..=1000, tf.as_ref())?;
@@ -112,7 +118,7 @@ pub async fn open_interest(
112118
symbol: Symbol,
113119
tf: BinanceTimeframe,
114120
range: RequestRange,
115-
recv_window: Option<u16>,
121+
recv_window: Option<std::time::Duration>,
116122
) -> Result<Vec<OpenInterest>, ExchangeError> {
117123
recv_window_check!(recv_window, GetOptions::<BinanceOptions>::default_options(client));
118124
range.ensure_allowed(1..=500, tf.as_ref())?;

v_exchanges/src/binance/mod.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,18 +27,18 @@ impl Exchange for Binance {
2727
self.update_default_option(BinanceOption::Secret(secret));
2828
}
2929

30-
fn set_recv_window(&mut self, recv_window: u16) {
30+
fn set_recv_window(&mut self, recv_window: std::time::Duration) {
3131
self.update_default_option(BinanceOption::RecvWindow(recv_window));
3232
}
3333

34-
async fn exchange_info(&self, instrument: Instrument, recv_window: Option<u16>) -> ExchangeResult<ExchangeInfo> {
34+
async fn exchange_info(&self, instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<ExchangeInfo> {
3535
match instrument {
3636
Instrument::Perp => perp::general::exchange_info(self, recv_window).await,
3737
_ => unimplemented!(),
3838
}
3939
}
4040

41-
async fn klines(&self, symbol: Symbol, tf: Timeframe, range: RequestRange, recv_window: Option<u16>) -> ExchangeResult<Klines> {
41+
async fn klines(&self, symbol: Symbol, tf: Timeframe, range: RequestRange, recv_window: Option<std::time::Duration>) -> ExchangeResult<Klines> {
4242
match symbol.instrument {
4343
Instrument::Spot | Instrument::Margin => market::klines(self, symbol, tf.try_into()?, range, recv_window).await,
4444
Instrument::Perp => market::klines(self, symbol, tf.try_into()?, range, recv_window).await,
@@ -49,15 +49,15 @@ impl Exchange for Binance {
4949
}
5050
}
5151

52-
async fn prices(&self, pairs: Option<Vec<Pair>>, instrument: Instrument, recv_window: Option<u16>) -> ExchangeResult<BTreeMap<Pair, f64>> {
52+
async fn prices(&self, pairs: Option<Vec<Pair>>, instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<BTreeMap<Pair, f64>> {
5353
match instrument {
5454
Instrument::Spot | Instrument::Margin => spot::market::prices(self, pairs, recv_window).await,
5555
Instrument::Perp => perp::market::prices(self, pairs, recv_window).await,
5656
_ => Err(ExchangeError::Method(MethodError::MethodNotImplemented { exchange: self.name(), instrument })),
5757
}
5858
}
5959

60-
async fn price(&self, symbol: Symbol, recv_window: Option<u16>) -> ExchangeResult<f64> {
60+
async fn price(&self, symbol: Symbol, recv_window: Option<std::time::Duration>) -> ExchangeResult<f64> {
6161
match symbol.instrument {
6262
Instrument::Spot | Instrument::Margin => spot::market::price(self, symbol.pair, recv_window).await,
6363
Instrument::Perp => perp::market::price(self, symbol.pair, recv_window).await,
@@ -68,7 +68,7 @@ impl Exchange for Binance {
6868
}
6969
}
7070

71-
async fn open_interest(&self, symbol: Symbol, tf: Timeframe, range: RequestRange, recv_window: Option<u16>) -> ExchangeResult<Vec<crate::core::OpenInterest>> {
71+
async fn open_interest(&self, symbol: Symbol, tf: Timeframe, range: RequestRange, recv_window: Option<std::time::Duration>) -> ExchangeResult<Vec<crate::core::OpenInterest>> {
7272
match symbol.instrument {
7373
Instrument::Perp => market::open_interest(self, symbol, tf.try_into()?, range, recv_window).await,
7474
_ => Err(ExchangeError::Method(MethodError::MethodNotSupported {
@@ -78,14 +78,14 @@ impl Exchange for Binance {
7878
}
7979
}
8080

81-
async fn asset_balance(&self, asset: Asset, instrument: Instrument, recv_window: Option<u16>) -> ExchangeResult<AssetBalance> {
81+
async fn asset_balance(&self, asset: Asset, instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<AssetBalance> {
8282
match instrument {
8383
Instrument::Perp => perp::account::asset_balance(self, asset, recv_window).await,
8484
_ => unimplemented!(),
8585
}
8686
}
8787

88-
async fn balances(&self, instrument: Instrument, recv_window: Option<u16>) -> ExchangeResult<Balances> {
88+
async fn balances(&self, instrument: Instrument, recv_window: Option<std::time::Duration>) -> ExchangeResult<Balances> {
8989
match instrument {
9090
Instrument::Perp => {
9191
let prices = self.prices(None, instrument, recv_window).await?;

v_exchanges/src/binance/perp/account.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ use crate::{
1717

1818
// balance {{{
1919
//DUP: difficult to escape duplicating half the [balances] method due to a) not requiring usd value b) binance not having individual asset balance endpoint
20-
pub async fn asset_balance(client: &v_exchanges_adapters::Client, asset: Asset, recv_window: Option<u16>) -> ExchangeResult<AssetBalance> {
20+
pub async fn asset_balance(client: &v_exchanges_adapters::Client, asset: Asset, recv_window: Option<std::time::Duration>) -> ExchangeResult<AssetBalance> {
2121
use v_exchanges_adapters::GetOptions;
2222
recv_window_check!(recv_window, GetOptions::<BinanceOptions>::default_options(client));
2323
assert!(client.is_authenticated::<BinanceOption>());
@@ -38,7 +38,7 @@ pub async fn asset_balance(client: &v_exchanges_adapters::Client, asset: Asset,
3838
Ok(balance)
3939
}
4040

41-
pub async fn balances(client: &v_exchanges_adapters::Client, recv_window: Option<u16>, prices: &BTreeMap<Pair, f64>) -> ExchangeResult<Balances> {
41+
pub async fn balances(client: &v_exchanges_adapters::Client, recv_window: Option<std::time::Duration>, prices: &BTreeMap<Pair, f64>) -> ExchangeResult<Balances> {
4242
use v_exchanges_adapters::GetOptions;
4343
recv_window_check!(recv_window, GetOptions::<BinanceOptions>::default_options(client));
4444
assert!(client.is_authenticated::<BinanceOption>());
@@ -87,7 +87,7 @@ pub async fn balances(client: &v_exchanges_adapters::Client, recv_window: Option
8787
// Order Placement {{{
8888

8989
/// Place a new order on Binance Futures
90-
pub async fn place_order(client: &v_exchanges_adapters::Client, request: OrderRequest, recv_window: Option<u16>) -> ExchangeResult<OrderResponse> {
90+
pub async fn place_order(client: &v_exchanges_adapters::Client, request: OrderRequest, recv_window: Option<std::time::Duration>) -> ExchangeResult<OrderResponse> {
9191
use v_exchanges_adapters::GetOptions;
9292
recv_window_check!(recv_window, GetOptions::<BinanceOptions>::default_options(client));
9393
assert!(client.is_authenticated::<BinanceOption>());
@@ -145,7 +145,7 @@ pub async fn place_order(client: &v_exchanges_adapters::Client, request: OrderRe
145145
// Income History {{{
146146

147147
/// Query income history
148-
pub async fn income_history(client: &v_exchanges_adapters::Client, request: IncomeRequest, recv_window: Option<u16>) -> ExchangeResult<Vec<IncomeRecord>> {
148+
pub async fn income_history(client: &v_exchanges_adapters::Client, request: IncomeRequest, recv_window: Option<std::time::Duration>) -> ExchangeResult<Vec<IncomeRecord>> {
149149
use v_exchanges_adapters::GetOptions;
150150
recv_window_check!(recv_window, GetOptions::<BinanceOptions>::default_options(client));
151151
assert!(client.is_authenticated::<BinanceOption>());

v_exchanges/src/binance/perp/general.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use crate::{
1414
};
1515
//TODO: general endpoints, like ping and exchange info
1616

17-
pub async fn exchange_info(client: &v_exchanges_adapters::Client, recv_window: Option<u16>) -> Result<ExchangeInfo, ExchangeError> {
17+
pub async fn exchange_info(client: &v_exchanges_adapters::Client, recv_window: Option<std::time::Duration>) -> Result<ExchangeInfo, ExchangeError> {
1818
recv_window_check!(recv_window, GetOptions::<BinanceOptions>::default_options(client));
1919
let mut options = vec![BinanceOption::HttpUrl(BinanceHttpUrl::FuturesUsdM)];
2020
if let Some(rw) = recv_window {

0 commit comments

Comments
 (0)