Skip to content

Commit

Permalink
feat(node-bindings): add methods for returning instance urls
Browse files Browse the repository at this point in the history
  • Loading branch information
yash-atreya committed Mar 21, 2024
1 parent d3af591 commit 3936402
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 0 deletions.
1 change: 1 addition & 0 deletions crates/node-bindings/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ serde_json.workspace = true
tempfile.workspace = true
thiserror.workspace = true
tracing.workspace = true
url.workspace = true

[dev-dependencies]
rand.workspace = true
11 changes: 11 additions & 0 deletions crates/node-bindings/src/anvil.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
time::{Duration, Instant},
};
use thiserror::Error;
use url::Url;

/// How long we will wait for anvil to indicate that it is ready.
const ANVIL_STARTUP_TIMEOUT_MILLIS: u64 = 10_000;
Expand Down Expand Up @@ -68,6 +69,16 @@ impl AnvilInstance {
pub fn ws_endpoint(&self) -> String {
format!("ws://localhost:{}", self.port)
}

/// Returns the HTTP endpoint url of this instance
pub fn endpoint_url(&self) -> Url {
Url::parse(&self.endpoint()).unwrap()
}

/// Returns the Websocket endpoint url of this instance
pub fn ws_endpoint_url(&self) -> Url {
Url::parse(&self.ws_endpoint()).unwrap()
}
}

impl Drop for AnvilInstance {
Expand Down
12 changes: 12 additions & 0 deletions crates/node-bindings/src/geth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use std::{
};
use tempfile::tempdir;
use thiserror::Error;
use url::Url;

/// How long we will wait for geth to indicate that it is ready.
const GETH_STARTUP_TIMEOUT: Duration = Duration::from_secs(10);
Expand Down Expand Up @@ -76,6 +77,16 @@ impl GethInstance {
format!("ws://localhost:{}", self.port)
}

/// Returns the HTTP endpoint url of this instance
pub fn endpoint_url(&self) -> Url {
Url::parse(&self.endpoint()).unwrap()
}

/// Returns the Websocket endpoint url of this instance
pub fn ws_endpoint_url(&self) -> Url {
Url::parse(&self.ws_endpoint()).unwrap()
}

/// Returns the path to this instances' IPC socket
pub fn ipc_path(&self) -> &Option<PathBuf> {
&self.ipc
Expand Down Expand Up @@ -680,6 +691,7 @@ mod tests {
fn run_with_tempdir(f: impl Fn(&Path)) {
let temp_dir = tempfile::tempdir().unwrap();
let temp_dir_path = temp_dir.path();
println!("temp dir: {:?}", temp_dir_path);
f(temp_dir_path);
#[cfg(not(windows))]
temp_dir.close().unwrap();
Expand Down

0 comments on commit 3936402

Please sign in to comment.