From 6a542165eb2ce54940152ab5658cdb3ea2a47440 Mon Sep 17 00:00:00 2001 From: Ethan Fast Date: Thu, 24 Dec 2020 15:08:10 -0800 Subject: [PATCH 1/4] upgrade --- Cargo.toml | 4 ++-- src/model/python.rs | 9 +++++++++ src/nash/mod.rs | 11 ++++++++--- tests/any_exchange/mod.rs | 2 ++ tests/nash/account.rs | 4 +++- tests/nash/market.rs | 3 ++- tests/nash/websocket.rs | 1 + 7 files changed, 27 insertions(+), 7 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 48c7e526..1b7c7f8c 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,7 +12,7 @@ keywords = ["cryptocurrency", "exchange", "openlimits", "api"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["rust_gmp"] +default = ["rust_gmp", "python"] rust_gmp = ["nash-protocol/rust_gmp", "nash-native-client/rust_gmp"] num_bigint = ["nash-protocol/num_bigint", "nash-native-client/num_bigint"] python = ["pyo3"] @@ -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..1b8f7cc1 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..0791c82f 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,14 +739,14 @@ 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) + client: Client::new(None, client_id, None, environment, timeout, sign_states_loop_interval) .await .expect("Couldn't create Client."), } @@ -754,9 +758,10 @@ 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) + 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 c877088f..8ee5ece9 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 0826f181..ed9038e4 100644 --- a/tests/nash/account.rs +++ b/tests/nash/account.rs @@ -247,9 +247,11 @@ async fn init() -> Nash { secret: env::var("NASH_API_SECRET").expect("Couldn't get environment variable."), session: env::var("NASH_API_KEY").expect("Couldn't get environment variable."), }), - environment: Environment::Sandbox, + environment: Environment::Production, 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 550f5435..a23797b9 100644 --- a/tests/nash/market.rs +++ b/tests/nash/market.rs @@ -98,9 +98,10 @@ async fn init() -> Nash { secret: env::var("NASH_API_SECRET").expect("Couldn't get environment variable."), session: env::var("NASH_API_KEY").expect("Couldn't get environment variable."), }), - environment: Environment::Sandbox, + environment: Environment::Production, 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 a7ef2c0e..0db7b5d3 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; From f9012a5154520bdf2d752f32a30e56b9c5a8db28 Mon Sep 17 00:00:00 2001 From: Ethan Fast Date: Thu, 24 Dec 2020 15:17:30 -0800 Subject: [PATCH 2/4] add test --- tests/any_exchange/mod.rs | 8 -------- tests/nash/account.rs | 24 +++++++++++++++++++----- tests/nash/market.rs | 6 +----- tests/nash/websocket.rs | 4 ---- 4 files changed, 20 insertions(+), 22 deletions(-) diff --git a/tests/any_exchange/mod.rs b/tests/any_exchange/mod.rs index 6d9262c2..8ee5ece9 100644 --- a/tests/any_exchange/mod.rs +++ b/tests/any_exchange/mod.rs @@ -34,12 +34,8 @@ async fn _nash() -> Result { }), environment: Environment::Sandbox, client_id: 1, -<<<<<<< HEAD timeout: Duration::new(10,0), sign_states_loop_interval: None -======= - timeout: Duration::new(10, 0), ->>>>>>> master }; OpenLimits::instantiate(parameters).await } @@ -80,12 +76,8 @@ async fn _nash_websocket() -> OpenLimitsWs { &env::var("NASH_API_KEY").unwrap(), 1234, Environment::Sandbox, -<<<<<<< HEAD Duration::new(10,0), None -======= - Duration::new(10, 0), ->>>>>>> master ) .await; diff --git a/tests/nash/account.rs b/tests/nash/account.rs index bf178665..3cdb127b 100644 --- a/tests/nash/account.rs +++ b/tests/nash/account.rs @@ -15,6 +15,7 @@ use openlimits::{ use rust_decimal::prelude::{Decimal, FromStr}; use std::env; use std::time::Duration as NativeDuration; +use std::sync::Arc; #[tokio::test] async fn limit_buy() { @@ -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; @@ -247,15 +265,11 @@ async fn init() -> Nash { secret: env::var("NASH_API_SECRET").expect("Couldn't get environment variable."), session: env::var("NASH_API_KEY").expect("Couldn't get environment variable."), }), - environment: Environment::Production, + environment: Environment::Sandbox, client_id: 1, -<<<<<<< HEAD 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) -======= - timeout: NativeDuration::new(10, 0), ->>>>>>> master }; OpenLimits::instantiate(parameters) diff --git a/tests/nash/market.rs b/tests/nash/market.rs index 9b103aab..cbb425d6 100644 --- a/tests/nash/market.rs +++ b/tests/nash/market.rs @@ -98,14 +98,10 @@ async fn init() -> Nash { secret: env::var("NASH_API_SECRET").expect("Couldn't get environment variable."), session: env::var("NASH_API_KEY").expect("Couldn't get environment variable."), }), - environment: Environment::Production, + environment: Environment::Sandbox, client_id: 1, -<<<<<<< HEAD timeout: Duration::new(10,0), sign_states_loop_interval: Some(100) -======= - timeout: Duration::new(10, 0), ->>>>>>> master }; OpenLimits::instantiate(parameters) diff --git a/tests/nash/websocket.rs b/tests/nash/websocket.rs index 13273eff..df3a9f29 100644 --- a/tests/nash/websocket.rs +++ b/tests/nash/websocket.rs @@ -40,12 +40,8 @@ async fn init() -> OpenLimitsWs { &env::var("NASH_API_KEY").expect("Couldn't get environment variable."), 1234, Environment::Sandbox, -<<<<<<< HEAD Duration::new(10,0), None -======= - Duration::new(10, 0), ->>>>>>> master ) .await; From 6c848a006054bfea881f979f6f37c7f4997e88f8 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" <41898282+github-actions[bot]@users.noreply.github.com> Date: Thu, 24 Dec 2020 23:19:56 +0000 Subject: [PATCH 3/4] Format Rust code using rustfmt --- src/model/python.rs | 2 +- src/nash/mod.rs | 44 ++++++++++++++++++++++++++++----------- tests/any_exchange/mod.rs | 8 +++---- tests/nash/account.rs | 8 +++---- tests/nash/market.rs | 4 ++-- tests/nash/websocket.rs | 4 ++-- 6 files changed, 45 insertions(+), 25 deletions(-) diff --git a/src/model/python.rs b/src/model/python.rs index 1b8f7cc1..161666c2 100644 --- a/src/model/python.rs +++ b/src/model/python.rs @@ -213,7 +213,7 @@ impl<'a> FromPyObject<'a> for NashParameters { client_id, environment, timeout, - sign_states_loop_interval + sign_states_loop_interval, }) } } diff --git a/src/nash/mod.rs b/src/nash/mod.rs index 0791c82f..8c6b8b46 100644 --- a/src/nash/mod.rs +++ b/src/nash/mod.rs @@ -44,7 +44,7 @@ pub struct NashParameters { pub client_id: u64, pub environment: Environment, pub timeout: Duration, - pub sign_states_loop_interval: Option + pub sign_states_loop_interval: Option, } impl Clone for NashParameters { @@ -59,7 +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() + sign_states_loop_interval: self.sign_states_loop_interval.clone(), } } } @@ -74,7 +74,7 @@ async fn client_from_params_failable(params: NashParameters) -> Result { params.client_id, params.environment, params.timeout, - params.sign_states_loop_interval + params.sign_states_loop_interval, ) .await } @@ -85,7 +85,7 @@ async fn client_from_params_failable(params: NashParameters) -> Result { None, params.environment, params.timeout, - params.sign_states_loop_interval + params.sign_states_loop_interval, ) .await } @@ -739,16 +739,28 @@ pub struct NashWebsocket { } impl NashWebsocket { - pub async fn public(client_id: u64, sandbox: bool, timeout: Duration, sign_states_loop_interval: Option) -> 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, sign_states_loop_interval) - .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."), } } @@ -758,12 +770,20 @@ impl NashWebsocket { client_id: u64, environment: Environment, timeout: Duration, - sign_states_loop_interval: Option + sign_states_loop_interval: Option, ) -> Self { NashWebsocket { - client: Client::from_key_data(secret, session, None, client_id, environment, timeout, sign_states_loop_interval) - .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 8ee5ece9..ef8b8e78 100644 --- a/tests/any_exchange/mod.rs +++ b/tests/any_exchange/mod.rs @@ -34,8 +34,8 @@ async fn _nash() -> Result { }), environment: Environment::Sandbox, client_id: 1, - timeout: Duration::new(10,0), - sign_states_loop_interval: None + timeout: Duration::new(10, 0), + sign_states_loop_interval: None, }; OpenLimits::instantiate(parameters).await } @@ -76,8 +76,8 @@ async fn _nash_websocket() -> OpenLimitsWs { &env::var("NASH_API_KEY").unwrap(), 1234, Environment::Sandbox, - Duration::new(10,0), - None + Duration::new(10, 0), + None, ) .await; diff --git a/tests/nash/account.rs b/tests/nash/account.rs index 3cdb127b..f928a14f 100644 --- a/tests/nash/account.rs +++ b/tests/nash/account.rs @@ -14,8 +14,8 @@ use openlimits::{ }; use rust_decimal::prelude::{Decimal, FromStr}; use std::env; -use std::time::Duration as NativeDuration; use std::sync::Arc; +use std::time::Duration as NativeDuration; #[tokio::test] async fn limit_buy() { @@ -203,7 +203,7 @@ async fn get_order_history() { async fn test_concurrent_requests() { let exchange = init().await; let client = Arc::new(exchange); - async fn make_request(client: Arc, i: u64){ + async fn make_request(client: Arc, i: u64) { let req = client.get_account_balances(None).await; if !req.is_err() { println!("completed req {}", i); @@ -267,9 +267,9 @@ async fn init() -> Nash { }), environment: Environment::Sandbox, client_id: 1, - timeout: NativeDuration::new(10,0), + 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) + sign_states_loop_interval: Some(100), }; OpenLimits::instantiate(parameters) diff --git a/tests/nash/market.rs b/tests/nash/market.rs index cbb425d6..603524ef 100644 --- a/tests/nash/market.rs +++ b/tests/nash/market.rs @@ -100,8 +100,8 @@ async fn init() -> Nash { }), environment: Environment::Sandbox, client_id: 1, - timeout: Duration::new(10,0), - sign_states_loop_interval: Some(100) + 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 df3a9f29..1f70eaa6 100644 --- a/tests/nash/websocket.rs +++ b/tests/nash/websocket.rs @@ -40,8 +40,8 @@ async fn init() -> OpenLimitsWs { &env::var("NASH_API_KEY").expect("Couldn't get environment variable."), 1234, Environment::Sandbox, - Duration::new(10,0), - None + Duration::new(10, 0), + None, ) .await; From ee5338dc7628866ab615ea6f26d86734b27a7ab2 Mon Sep 17 00:00:00 2001 From: Ethan Fast Date: Thu, 24 Dec 2020 15:20:34 -0800 Subject: [PATCH 4/4] remove python feature default added for testing --- Cargo.toml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1b7c7f8c..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." @@ -12,7 +12,7 @@ keywords = ["cryptocurrency", "exchange", "openlimits", "api"] # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [features] -default = ["rust_gmp", "python"] +default = ["rust_gmp"] rust_gmp = ["nash-protocol/rust_gmp", "nash-native-client/rust_gmp"] num_bigint = ["nash-protocol/num_bigint", "nash-native-client/num_bigint"] python = ["pyo3"]