diff --git a/Cargo.toml b/Cargo.toml index 48c7e526..b8776a7f 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "openlimits" -version = "0.1.6" +version = "0.1.7" authors = ["steffel <2143646+steffenix@users.noreply.github.com>", "Ethan Fast "] edition = "2018" description = "A open source Rust high performance cryptocurrency trading API with support for multiple exchanges and language wrappers. Focused in safety, correctness and speed." @@ -40,5 +40,5 @@ sha2 = "0.9.1" url = "2.1.1" derive_more = "0.99" nash-protocol = { version = "=0.1.20", default-features = false, features = ["rustcrypto"] } -nash-native-client = { version = "=0.1.13", default-features = false, features = ["rustcrypto"] } +nash-native-client = { version = "=0.1.14", default-features = false, features = ["rustcrypto"] } pyo3 = { version = "0.12.3", optional = true } diff --git a/src/model/python.rs b/src/model/python.rs index 44ace6ba..161666c2 100644 --- a/src/model/python.rs +++ b/src/model/python.rs @@ -7,6 +7,7 @@ use crate::nash::{Environment, NashCredentials, NashParameters}; use pyo3::exceptions::PyException; use pyo3::prelude::{FromPyObject, IntoPy, PyObject, PyResult, Python, ToPyObject}; use pyo3::types::PyDict; +use std::time::Duration; // Python to Rust... @@ -199,12 +200,20 @@ impl<'a> FromPyObject<'a> for NashParameters { "timeout not included in nash credentials", ))? .extract()?; + let timeout = Duration::from_millis(timeout); + let sign_states_loop_interval: Option = py_dict + .get_item("timeout") + .ok_or(PyException::new_err( + "sign states loop interval not included in nash credentials", + ))? + .extract()?; Ok(NashParameters { affiliate_code, credentials, client_id, environment, timeout, + sign_states_loop_interval, }) } } diff --git a/src/nash/mod.rs b/src/nash/mod.rs index 7370b5b4..8c6b8b46 100644 --- a/src/nash/mod.rs +++ b/src/nash/mod.rs @@ -44,6 +44,7 @@ pub struct NashParameters { pub client_id: u64, pub environment: Environment, pub timeout: Duration, + pub sign_states_loop_interval: Option, } impl Clone for NashParameters { @@ -58,6 +59,7 @@ impl Clone for NashParameters { Environment::Dev(s) => Environment::Dev(s), }, timeout: self.timeout, + sign_states_loop_interval: self.sign_states_loop_interval.clone(), } } } @@ -72,6 +74,7 @@ async fn client_from_params_failable(params: NashParameters) -> Result { params.client_id, params.environment, params.timeout, + params.sign_states_loop_interval, ) .await } @@ -82,6 +85,7 @@ async fn client_from_params_failable(params: NashParameters) -> Result { None, params.environment, params.timeout, + params.sign_states_loop_interval, ) .await } @@ -735,16 +739,28 @@ pub struct NashWebsocket { } impl NashWebsocket { - pub async fn public(client_id: u64, sandbox: bool, timeout: Duration) -> Self { + pub async fn public( + client_id: u64, + sandbox: bool, + timeout: Duration, + sign_states_loop_interval: Option, + ) -> Self { let environment = if sandbox { Environment::Sandbox } else { Environment::Production }; NashWebsocket { - client: Client::new(None, client_id, None, environment, timeout) - .await - .expect("Couldn't create Client."), + client: Client::new( + None, + client_id, + None, + environment, + timeout, + sign_states_loop_interval, + ) + .await + .expect("Couldn't create Client."), } } @@ -754,11 +770,20 @@ impl NashWebsocket { client_id: u64, environment: Environment, timeout: Duration, + sign_states_loop_interval: Option, ) -> Self { NashWebsocket { - client: Client::from_key_data(secret, session, None, client_id, environment, timeout) - .await - .expect("Couldn't create Client."), + client: Client::from_key_data( + secret, + session, + None, + client_id, + environment, + timeout, + sign_states_loop_interval, + ) + .await + .expect("Couldn't create Client."), } } } diff --git a/tests/any_exchange/mod.rs b/tests/any_exchange/mod.rs index bc0ef0ac..ef8b8e78 100644 --- a/tests/any_exchange/mod.rs +++ b/tests/any_exchange/mod.rs @@ -35,6 +35,7 @@ async fn _nash() -> Result { environment: Environment::Sandbox, client_id: 1, timeout: Duration::new(10, 0), + sign_states_loop_interval: None, }; OpenLimits::instantiate(parameters).await } @@ -76,6 +77,7 @@ async fn _nash_websocket() -> OpenLimitsWs { 1234, Environment::Sandbox, Duration::new(10, 0), + None, ) .await; diff --git a/tests/nash/account.rs b/tests/nash/account.rs index b68367b4..f928a14f 100644 --- a/tests/nash/account.rs +++ b/tests/nash/account.rs @@ -14,6 +14,7 @@ use openlimits::{ }; use rust_decimal::prelude::{Decimal, FromStr}; use std::env; +use std::sync::Arc; use std::time::Duration as NativeDuration; #[tokio::test] @@ -198,6 +199,23 @@ async fn get_order_history() { println!("{:?}", resp); } +#[tokio::test] +async fn test_concurrent_requests() { + let exchange = init().await; + let client = Arc::new(exchange); + async fn make_request(client: Arc, i: u64) { + let req = client.get_account_balances(None).await; + if !req.is_err() { + println!("completed req {}", i); + } + } + let mut tasks = Vec::new(); + for i in 0..10 { + tasks.push(tokio::spawn(make_request(client.clone(), i))); + } + futures::future::join_all(tasks).await; +} + // #[tokio::test] // async fn get_all_open_orders() { // let mut exchange = init().await; @@ -250,6 +268,8 @@ async fn init() -> Nash { environment: Environment::Sandbox, client_id: 1, timeout: NativeDuration::new(10, 0), + // sign states in background, checking whether is required every 100s. None turns off the functionality + sign_states_loop_interval: Some(100), }; OpenLimits::instantiate(parameters) diff --git a/tests/nash/market.rs b/tests/nash/market.rs index 892b5328..603524ef 100644 --- a/tests/nash/market.rs +++ b/tests/nash/market.rs @@ -101,6 +101,7 @@ async fn init() -> Nash { environment: Environment::Sandbox, client_id: 1, timeout: Duration::new(10, 0), + sign_states_loop_interval: Some(100), }; OpenLimits::instantiate(parameters) diff --git a/tests/nash/websocket.rs b/tests/nash/websocket.rs index 99cec12b..1f70eaa6 100644 --- a/tests/nash/websocket.rs +++ b/tests/nash/websocket.rs @@ -41,6 +41,7 @@ async fn init() -> OpenLimitsWs { 1234, Environment::Sandbox, Duration::new(10, 0), + None, ) .await;