Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update to latest nash client which supports concurrent requests #153

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "openlimits"
version = "0.1.6"
version = "0.1.7"
authors = ["steffel <[email protected]>", "Ethan Fast <[email protected]>"]
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."
Expand Down Expand Up @@ -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 }
9 changes: 9 additions & 0 deletions src/model/python.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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...

Expand Down Expand Up @@ -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<u64> = 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,
})
}
}
Expand Down
39 changes: 32 additions & 7 deletions src/nash/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ pub struct NashParameters {
pub client_id: u64,
pub environment: Environment,
pub timeout: Duration,
pub sign_states_loop_interval: Option<u64>,
}

impl Clone for NashParameters {
Expand All @@ -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(),
}
}
}
Expand All @@ -72,6 +74,7 @@ async fn client_from_params_failable(params: NashParameters) -> Result<Client> {
params.client_id,
params.environment,
params.timeout,
params.sign_states_loop_interval,
)
.await
}
Expand All @@ -82,6 +85,7 @@ async fn client_from_params_failable(params: NashParameters) -> Result<Client> {
None,
params.environment,
params.timeout,
params.sign_states_loop_interval,
)
.await
}
Expand Down Expand Up @@ -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<u64>,
) -> 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."),
}
}

Expand All @@ -754,11 +770,20 @@ impl NashWebsocket {
client_id: u64,
environment: Environment,
timeout: Duration,
sign_states_loop_interval: Option<u64>,
) -> 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."),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions tests/any_exchange/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ async fn _nash() -> Result<Nash> {
environment: Environment::Sandbox,
client_id: 1,
timeout: Duration::new(10, 0),
sign_states_loop_interval: None,
};
OpenLimits::instantiate(parameters).await
}
Expand Down Expand Up @@ -76,6 +77,7 @@ async fn _nash_websocket() -> OpenLimitsWs<NashWebsocket> {
1234,
Environment::Sandbox,
Duration::new(10, 0),
None,
)
.await;

Expand Down
20 changes: 20 additions & 0 deletions tests/nash/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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<Nash>, 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;
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/nash/market.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions tests/nash/websocket.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ async fn init() -> OpenLimitsWs<NashWebsocket> {
1234,
Environment::Sandbox,
Duration::new(10, 0),
None,
)
.await;

Expand Down