From 75548e03affd07699c7be04e1641659df13b603d Mon Sep 17 00:00:00 2001 From: Phuong Nguyen Date: Thu, 8 Sep 2022 18:50:32 -0700 Subject: [PATCH 1/5] Added API Keys --- CHANGELOG.md | 1 + README.md | 4 ++++ workspaces/src/network/betanet.rs | 2 +- workspaces/src/network/mainnet.rs | 2 +- workspaces/src/network/sandbox.rs | 2 +- workspaces/src/network/testnet.rs | 2 +- workspaces/src/rpc/client.rs | 13 +++++++++---- 7 files changed, 18 insertions(+), 8 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index bb5838b4..312bcb14 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -4,6 +4,7 @@ ### Added +- RPC API Keys used to interact with services such as Pagoda Console. - [Import a couple functions over from near_crypto for PublicKey](https://github.com/near/workspaces-rs/pull/265) - Impl `Ord`, `PartialOrd`, `Hash`, `BorshSerialize`, `BorshDeserialize`, `Display`, and `FromStr` for `PublicKey` - NOTE: Borsh bytes format is the same as near-sdk, where it is in the form of [bytes_len, key_type, key_data..] diff --git a/README.md b/README.md index 3bb8aab0..82461cab 100644 --- a/README.md +++ b/README.md @@ -344,5 +344,9 @@ These environment variables will be useful if there was ever a snag hit: - `NEAR_RPC_TIMEOUT_SECS`: The default is 10 seconds, but this is the amount of time before timing out waiting for a RPC service when talking to the sandbox or any other network such as testnet. - `NEAR_SANDBOX_BIN_PATH`: Set this to our own prebuilt `neard-sandbox` bin path if we want to use a non-default version of the sandbox or configure nearcore with our own custom features that we want to test in workspaces. +<<<<<<< HEAD - `NEAR_SANDBOX_MAX_PAYLOAD_SIZE`: Sets the max payload size for sending transaction commits to sandbox. The default is 1gb and is necessary for patching large states. - `NEAR_SANDBOX_MAX_FILES`: Set the max amount of files that can be opened at a time in the sandbox. If none is specified, the default size of 4096 will be used. The actual near chain will use over 10,000 in practice, but for testing this should be much lower since we do not have a constantly running blockchain unless our tests take up that much time. +======= +- `NEAR_RPC_API_KEY`: This is the API key necessary for communicating with RPC nodes. This is useful when interacting with services such as Pagoda Console or a service that can access RPC metrics. +>>>>>>> 1fa5b81 (Added API Keys) diff --git a/workspaces/src/network/betanet.rs b/workspaces/src/network/betanet.rs index 56302e32..c97eaee9 100644 --- a/workspaces/src/network/betanet.rs +++ b/workspaces/src/network/betanet.rs @@ -29,7 +29,7 @@ pub struct Betanet { impl FromNetworkBuilder for Betanet { async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> crate::result::Result { let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into()); - let client = Client::new(&rpc_url); + let client = Client::new(&rpc_url)?; client.wait_for_rpc().await?; Ok(Self { diff --git a/workspaces/src/network/mainnet.rs b/workspaces/src/network/mainnet.rs index 7d331310..55787adf 100644 --- a/workspaces/src/network/mainnet.rs +++ b/workspaces/src/network/mainnet.rs @@ -30,7 +30,7 @@ pub struct Mainnet { impl FromNetworkBuilder for Mainnet { async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result { let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into()); - let client = Client::new(&rpc_url); + let client = Client::new(&rpc_url)?; client.wait_for_rpc().await?; Ok(Self { diff --git a/workspaces/src/network/sandbox.rs b/workspaces/src/network/sandbox.rs index 2710bbc3..c038e3c9 100644 --- a/workspaces/src/network/sandbox.rs +++ b/workspaces/src/network/sandbox.rs @@ -85,7 +85,7 @@ impl FromNetworkBuilder for Sandbox { } }; - let client = Client::new(&server.rpc_addr()); + let client = Client::new(&server.rpc_addr())?; client.wait_for_rpc().await?; // Server locks some ports on startup due to potential port collision, so we need diff --git a/workspaces/src/network/testnet.rs b/workspaces/src/network/testnet.rs index 3f015940..50655302 100644 --- a/workspaces/src/network/testnet.rs +++ b/workspaces/src/network/testnet.rs @@ -39,7 +39,7 @@ pub struct Testnet { impl FromNetworkBuilder for Testnet { async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result { let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into()); - let client = Client::new(&rpc_url); + let client = Client::new(&rpc_url)?; client.wait_for_rpc().await?; Ok(Self { diff --git a/workspaces/src/rpc/client.rs b/workspaces/src/rpc/client.rs index 91632779..312af37c 100644 --- a/workspaces/src/rpc/client.rs +++ b/workspaces/src/rpc/client.rs @@ -61,15 +61,20 @@ pub struct Client { } impl Client { - pub(crate) fn new(rpc_addr: &str) -> Self { + pub(crate) fn new(rpc_addr: &str) -> Result { let connector = JsonRpcClient::new_client(); - let rpc_client = connector.connect(rpc_addr); + let mut rpc_client = connector.connect(rpc_addr); + if let Ok(api_key) = std::env::var("NEAR_RPC_API_KEY") { + let api_key = near_jsonrpc_client::auth::ApiKey::new(api_key) + .map_err(|e| ErrorKind::DataConversion.custom(e))?; + rpc_client = rpc_client.header(api_key); + } - Self { + Ok(Self { rpc_client, rpc_addr: rpc_addr.into(), access_key_nonces: RwLock::new(HashMap::new()), - } + }) } pub(crate) async fn query_broadcast_tx( From a7e71ce58383f675b739c32f12d3f3e4725be24a Mon Sep 17 00:00:00 2001 From: Yasir Shariff Date: Mon, 18 Sep 2023 07:39:55 +0300 Subject: [PATCH 2/5] add: custom network --- workspaces/Cargo.toml | 2 +- workspaces/src/lib.rs | 3 ++ workspaces/src/network/custom.rs | 54 ++++++++++++++++++++++++++++++ workspaces/src/network/mod.rs | 2 ++ workspaces/src/worker/mod.rs | 14 +++++++- workspaces/tests/custom_network.rs | 17 ++++++++++ 6 files changed, 90 insertions(+), 2 deletions(-) create mode 100644 workspaces/src/network/custom.rs create mode 100644 workspaces/tests/custom_network.rs diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml index b954a74d..353c5c5a 100644 --- a/workspaces/Cargo.toml +++ b/workspaces/Cargo.toml @@ -57,7 +57,7 @@ test-log = { version = "0.2.8", default-features = false, features = ["trace"] } tracing-subscriber = { version = "0.3.5", features = ["env-filter"] } [features] -default = ["install", "interop_sdk"] +default = ["install", "interop_sdk", "unstable"] install = [] # Install the sandbox binary during compile time interop_sdk = ["near-sdk"] unstable = ["cargo_metadata"] diff --git a/workspaces/src/lib.rs b/workspaces/src/lib.rs index bff3b9fd..73f45532 100644 --- a/workspaces/src/lib.rs +++ b/workspaces/src/lib.rs @@ -29,3 +29,6 @@ pub use worker::{ betanet, mainnet, mainnet_archival, sandbox, testnet, testnet_archival, with_betanet, with_mainnet, with_mainnet_archival, with_sandbox, with_testnet, with_testnet_archival, Worker, }; + +#[cfg(feature = "unstable")] +pub use worker::{custom, with_custom}; diff --git a/workspaces/src/network/custom.rs b/workspaces/src/network/custom.rs new file mode 100644 index 00000000..dab51c29 --- /dev/null +++ b/workspaces/src/network/custom.rs @@ -0,0 +1,54 @@ +use crate::network::{Info, NetworkClient, NetworkInfo}; +use crate::result::Result; +use crate::rpc::client::Client; +use std::path::PathBuf; + +use super::builder::{FromNetworkBuilder, NetworkBuilder}; + +/// Holds information about a custom network. +pub struct Custom { + client: Client, + info: Info, +} + +#[async_trait::async_trait] +impl FromNetworkBuilder for Custom { + async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result { + let rpc_url = build + .rpc_addr + .expect("rpc address should be provided for custom network"); + let client = Client::new(&rpc_url)?; + client.wait_for_rpc().await?; + + Ok(Self { + client, + info: Info { + name: build.name.into(), + root_id: "near".parse().unwrap(), + keystore_path: PathBuf::from(".near-credentials/mainnet/"), + rpc_url: url::Url::parse(&rpc_url).expect("custom provided url should be valid"), + }, + }) + } +} + +impl std::fmt::Debug for Custom { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("Custom") + .field("root_id", &self.info.root_id) + .field("rpc_url", &self.info.rpc_url) + .finish() + } +} + +impl NetworkClient for Custom { + fn client(&self) -> &Client { + &self.client + } +} + +impl NetworkInfo for Custom { + fn info(&self) -> &Info { + &self.info + } +} diff --git a/workspaces/src/network/mod.rs b/workspaces/src/network/mod.rs index af373d67..147a0802 100644 --- a/workspaces/src/network/mod.rs +++ b/workspaces/src/network/mod.rs @@ -11,12 +11,14 @@ pub(crate) mod builder; pub(crate) mod variants; pub mod betanet; +pub mod custom; pub mod mainnet; pub mod testnet; pub(crate) use variants::DEV_ACCOUNT_SEED; pub use self::betanet::Betanet; +pub use self::custom::Custom; pub use self::info::Info; pub use self::mainnet::Mainnet; pub use self::sandbox::Sandbox; diff --git a/workspaces/src/worker/mod.rs b/workspaces/src/worker/mod.rs index 9d9aa839..bd297eb8 100644 --- a/workspaces/src/worker/mod.rs +++ b/workspaces/src/worker/mod.rs @@ -4,7 +4,7 @@ use std::fmt; use std::sync::Arc; use crate::network::builder::NetworkBuilder; -use crate::network::{Betanet, Mainnet, Sandbox, Testnet}; +use crate::network::{Betanet, Custom, Mainnet, Sandbox, Testnet}; use crate::{Network, Result}; /// The `Worker` type allows us to interact with any NEAR related networks, such @@ -76,6 +76,10 @@ pub fn betanet<'a>() -> NetworkBuilder<'a, Betanet> { NetworkBuilder::new("betanet") } +pub fn custom<'a>(rpc_url: &str) -> NetworkBuilder<'a, Custom> { + NetworkBuilder::new("custom").rpc_addr(rpc_url) +} + /// Run a locally scoped task where a [`sandbox`] instanced [`Worker`] is supplied. pub async fn with_sandbox(task: F) -> Result where @@ -129,3 +133,11 @@ where { Ok(task(betanet().await?).await) } + +pub async fn with_custom(task: F, rpc_url: &str) -> Result +where + F: Fn(Worker) -> T, + T: core::future::Future, +{ + Ok(task(custom(rpc_url).await?).await) +} diff --git a/workspaces/tests/custom_network.rs b/workspaces/tests/custom_network.rs new file mode 100644 index 00000000..5ed05949 --- /dev/null +++ b/workspaces/tests/custom_network.rs @@ -0,0 +1,17 @@ +/// URL to the Pagoda API to use for testnet. +pub const PAGODA_TESTNET_RPC_URL: &str = "https://near-testnet.api.pagoda.co/rpc/v1/"; + +#[tokio::test] +async fn test_custom_network() -> anyhow::Result<()> { + if std::env::var("NEAR_RPC_API_KEY").is_err() { + // skip the test + return Ok(()); + } + + let worker = workspaces::custom(PAGODA_TESTNET_RPC_URL).await?; + let res = worker.view_block().await?; + + assert!(res.height() > 0); + + Ok(()) +} From ac3760c10d5651e4d8143a1730b0c41024db6d55 Mon Sep 17 00:00:00 2001 From: Yasir Shariff Date: Mon, 18 Sep 2023 07:47:53 +0300 Subject: [PATCH 3/5] upd: test --- workspaces/tests/custom_network.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/workspaces/tests/custom_network.rs b/workspaces/tests/custom_network.rs index 5ed05949..ed83a08e 100644 --- a/workspaces/tests/custom_network.rs +++ b/workspaces/tests/custom_network.rs @@ -3,11 +3,14 @@ pub const PAGODA_TESTNET_RPC_URL: &str = "https://near-testnet.api.pagoda.co/rpc #[tokio::test] async fn test_custom_network() -> anyhow::Result<()> { + // `NEAR_RPC_API_KEY="xxxx" cargo test --package workspaces --test custom_network -- test_custom_network --exact --nocapture` if std::env::var("NEAR_RPC_API_KEY").is_err() { // skip the test + println!("NEAR_RPC_API_KEY is not set, skipping the test"); return Ok(()); } + // Reference to what can be called by this network: https://docs.pagoda.co/endpoints let worker = workspaces::custom(PAGODA_TESTNET_RPC_URL).await?; let res = worker.view_block().await?; From 7347c7d8d1754dac2cfd9274f870bd7865c96082 Mon Sep 17 00:00:00 2001 From: Yasir Shariff Date: Mon, 18 Sep 2023 08:18:55 +0300 Subject: [PATCH 4/5] upd: using examples & readme --- README.md | 2 -- examples/Cargo.toml | 7 +++++-- {workspaces/tests => examples/src}/custom_network.rs | 6 +++--- workspaces/Cargo.toml | 2 +- workspaces/src/worker/mod.rs | 1 + 5 files changed, 10 insertions(+), 8 deletions(-) rename {workspaces/tests => examples/src}/custom_network.rs (81%) diff --git a/README.md b/README.md index 82461cab..c4eb0e2d 100644 --- a/README.md +++ b/README.md @@ -347,6 +347,4 @@ These environment variables will be useful if there was ever a snag hit: <<<<<<< HEAD - `NEAR_SANDBOX_MAX_PAYLOAD_SIZE`: Sets the max payload size for sending transaction commits to sandbox. The default is 1gb and is necessary for patching large states. - `NEAR_SANDBOX_MAX_FILES`: Set the max amount of files that can be opened at a time in the sandbox. If none is specified, the default size of 4096 will be used. The actual near chain will use over 10,000 in practice, but for testing this should be much lower since we do not have a constantly running blockchain unless our tests take up that much time. -======= - `NEAR_RPC_API_KEY`: This is the API key necessary for communicating with RPC nodes. This is useful when interacting with services such as Pagoda Console or a service that can access RPC metrics. ->>>>>>> 1fa5b81 (Added API Keys) diff --git a/examples/Cargo.toml b/examples/Cargo.toml index 4acb3570..3316ac80 100644 --- a/examples/Cargo.toml +++ b/examples/Cargo.toml @@ -17,8 +17,7 @@ serde_json = { version = "1.0" } tokio = { version = "1.10.0", features = ["full"] } tracing = "0.1" tracing-subscriber = { version = "0.3.5", features = ["env-filter"] } -workspaces = { path = "../workspaces", features = ["experimental"] } - +workspaces = { path = "../workspaces", features = ["experimental", "unstable"] } [[example]] name = "async_transaction" @@ -83,3 +82,7 @@ path = "src/tx_status.rs" [[example]] name = "noop" path = "src/noop.rs" + +[[example]] +name = "custom_network" +path = "src/custom_network.rs" diff --git a/workspaces/tests/custom_network.rs b/examples/src/custom_network.rs similarity index 81% rename from workspaces/tests/custom_network.rs rename to examples/src/custom_network.rs index ed83a08e..0ea71409 100644 --- a/workspaces/tests/custom_network.rs +++ b/examples/src/custom_network.rs @@ -1,12 +1,12 @@ /// URL to the Pagoda API to use for testnet. pub const PAGODA_TESTNET_RPC_URL: &str = "https://near-testnet.api.pagoda.co/rpc/v1/"; -#[tokio::test] -async fn test_custom_network() -> anyhow::Result<()> { +#[tokio::main] +async fn main() -> anyhow::Result<()> { // `NEAR_RPC_API_KEY="xxxx" cargo test --package workspaces --test custom_network -- test_custom_network --exact --nocapture` if std::env::var("NEAR_RPC_API_KEY").is_err() { // skip the test - println!("NEAR_RPC_API_KEY is not set, skipping the test"); + println!("NEAR_RPC_API_KEY is not set, skipping the example"); return Ok(()); } diff --git a/workspaces/Cargo.toml b/workspaces/Cargo.toml index 353c5c5a..b954a74d 100644 --- a/workspaces/Cargo.toml +++ b/workspaces/Cargo.toml @@ -57,7 +57,7 @@ test-log = { version = "0.2.8", default-features = false, features = ["trace"] } tracing-subscriber = { version = "0.3.5", features = ["env-filter"] } [features] -default = ["install", "interop_sdk", "unstable"] +default = ["install", "interop_sdk"] install = [] # Install the sandbox binary during compile time interop_sdk = ["near-sdk"] unstable = ["cargo_metadata"] diff --git a/workspaces/src/worker/mod.rs b/workspaces/src/worker/mod.rs index bd297eb8..d531efe6 100644 --- a/workspaces/src/worker/mod.rs +++ b/workspaces/src/worker/mod.rs @@ -134,6 +134,7 @@ where Ok(task(betanet().await?).await) } +#[allow(dead_code)] pub async fn with_custom(task: F, rpc_url: &str) -> Result where F: Fn(Worker) -> T, From c0704b300d0bab655657b978b5907f1b61c8d814 Mon Sep 17 00:00:00 2001 From: Yasir Shariff Date: Tue, 19 Sep 2023 06:08:07 +0300 Subject: [PATCH 5/5] upd: address review --- README.md | 3 +-- examples/src/custom_network.rs | 23 ++++++++++++----------- workspaces/src/network/betanet.rs | 2 +- workspaces/src/network/builder.rs | 12 ++++++++++++ workspaces/src/network/custom.rs | 2 +- workspaces/src/network/mainnet.rs | 2 +- workspaces/src/network/sandbox.rs | 2 +- workspaces/src/network/testnet.rs | 2 +- workspaces/src/rpc/client.rs | 4 ++-- workspaces/src/worker/mod.rs | 3 +++ 10 files changed, 35 insertions(+), 20 deletions(-) diff --git a/README.md b/README.md index c4eb0e2d..2e68e9cc 100644 --- a/README.md +++ b/README.md @@ -344,7 +344,6 @@ These environment variables will be useful if there was ever a snag hit: - `NEAR_RPC_TIMEOUT_SECS`: The default is 10 seconds, but this is the amount of time before timing out waiting for a RPC service when talking to the sandbox or any other network such as testnet. - `NEAR_SANDBOX_BIN_PATH`: Set this to our own prebuilt `neard-sandbox` bin path if we want to use a non-default version of the sandbox or configure nearcore with our own custom features that we want to test in workspaces. -<<<<<<< HEAD - `NEAR_SANDBOX_MAX_PAYLOAD_SIZE`: Sets the max payload size for sending transaction commits to sandbox. The default is 1gb and is necessary for patching large states. - `NEAR_SANDBOX_MAX_FILES`: Set the max amount of files that can be opened at a time in the sandbox. If none is specified, the default size of 4096 will be used. The actual near chain will use over 10,000 in practice, but for testing this should be much lower since we do not have a constantly running blockchain unless our tests take up that much time. -- `NEAR_RPC_API_KEY`: This is the API key necessary for communicating with RPC nodes. This is useful when interacting with services such as Pagoda Console or a service that can access RPC metrics. +- `NEAR_RPC_API_KEY`: This is the API key necessary for communicating with RPC nodes. This is useful when interacting with services such as Pagoda Console or a service that can access RPC metrics. This is not a **hard** requirement, but it is recommended to running the Pagoda example in the examples folder. diff --git a/examples/src/custom_network.rs b/examples/src/custom_network.rs index 0ea71409..0811f0ce 100644 --- a/examples/src/custom_network.rs +++ b/examples/src/custom_network.rs @@ -3,18 +3,19 @@ pub const PAGODA_TESTNET_RPC_URL: &str = "https://near-testnet.api.pagoda.co/rpc #[tokio::main] async fn main() -> anyhow::Result<()> { - // `NEAR_RPC_API_KEY="xxxx" cargo test --package workspaces --test custom_network -- test_custom_network --exact --nocapture` - if std::env::var("NEAR_RPC_API_KEY").is_err() { - // skip the test - println!("NEAR_RPC_API_KEY is not set, skipping the example"); + // `NEAR_RPC_API_KEY="xxx" cargo run --package examples --example custom_network` + if let Ok(val) = std::env::var("NEAR_RPC_API_KEY") { + // Reference to what can be called by this network: https://docs.pagoda.co/endpoints + let worker = workspaces::custom(PAGODA_TESTNET_RPC_URL) + .api_key(&val) + .await?; + let res = worker.view_block().await?; + + assert!(res.height() > 0); return Ok(()); } - // Reference to what can be called by this network: https://docs.pagoda.co/endpoints - let worker = workspaces::custom(PAGODA_TESTNET_RPC_URL).await?; - let res = worker.view_block().await?; - - assert!(res.height() > 0); - - Ok(()) + // skip the test + println!("NEAR_RPC_API_KEY is not set, skipping the example"); + return Ok(()); } diff --git a/workspaces/src/network/betanet.rs b/workspaces/src/network/betanet.rs index c97eaee9..be0badd4 100644 --- a/workspaces/src/network/betanet.rs +++ b/workspaces/src/network/betanet.rs @@ -29,7 +29,7 @@ pub struct Betanet { impl FromNetworkBuilder for Betanet { async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> crate::result::Result { let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into()); - let client = Client::new(&rpc_url)?; + let client = Client::new(&rpc_url, build.api_key)?; client.wait_for_rpc().await?; Ok(Self { diff --git a/workspaces/src/network/builder.rs b/workspaces/src/network/builder.rs index 3e281e22..0cea9155 100644 --- a/workspaces/src/network/builder.rs +++ b/workspaces/src/network/builder.rs @@ -29,6 +29,7 @@ pub struct NetworkBuilder<'a, T> { pub(crate) name: &'a str, pub(crate) rpc_addr: Option, pub(crate) validator_key: Option, + pub(crate) api_key: Option, _network: PhantomData, } @@ -54,6 +55,7 @@ impl<'a, T> NetworkBuilder<'a, T> { name, rpc_addr: None, validator_key: None, + api_key: None, _network: PhantomData, } } @@ -69,6 +71,16 @@ impl<'a, T> NetworkBuilder<'a, T> { self.rpc_addr = Some(addr.into()); self } + + /// Sets the API key for this network. Useful for setting the API key to an RPC + /// server that requires it. + /// + /// Note that if you're using a custom network, the burden is on you to ensure that + /// the methods you're calling are supported by the RPC server you're connecting to. + pub fn api_key(mut self, api_key: &str) -> Self { + self.api_key = Some(api_key.into()); + self + } } // So far, only Sandbox makes use of validator_key. diff --git a/workspaces/src/network/custom.rs b/workspaces/src/network/custom.rs index dab51c29..c47296ae 100644 --- a/workspaces/src/network/custom.rs +++ b/workspaces/src/network/custom.rs @@ -17,7 +17,7 @@ impl FromNetworkBuilder for Custom { let rpc_url = build .rpc_addr .expect("rpc address should be provided for custom network"); - let client = Client::new(&rpc_url)?; + let client = Client::new(&rpc_url, build.api_key)?; client.wait_for_rpc().await?; Ok(Self { diff --git a/workspaces/src/network/mainnet.rs b/workspaces/src/network/mainnet.rs index 55787adf..a4d8b949 100644 --- a/workspaces/src/network/mainnet.rs +++ b/workspaces/src/network/mainnet.rs @@ -30,7 +30,7 @@ pub struct Mainnet { impl FromNetworkBuilder for Mainnet { async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result { let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into()); - let client = Client::new(&rpc_url)?; + let client = Client::new(&rpc_url, build.api_key)?; client.wait_for_rpc().await?; Ok(Self { diff --git a/workspaces/src/network/sandbox.rs b/workspaces/src/network/sandbox.rs index c038e3c9..d307e3ab 100644 --- a/workspaces/src/network/sandbox.rs +++ b/workspaces/src/network/sandbox.rs @@ -85,7 +85,7 @@ impl FromNetworkBuilder for Sandbox { } }; - let client = Client::new(&server.rpc_addr())?; + let client = Client::new(&server.rpc_addr(), build.api_key)?; client.wait_for_rpc().await?; // Server locks some ports on startup due to potential port collision, so we need diff --git a/workspaces/src/network/testnet.rs b/workspaces/src/network/testnet.rs index 50655302..006e338e 100644 --- a/workspaces/src/network/testnet.rs +++ b/workspaces/src/network/testnet.rs @@ -39,7 +39,7 @@ pub struct Testnet { impl FromNetworkBuilder for Testnet { async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result { let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into()); - let client = Client::new(&rpc_url)?; + let client = Client::new(&rpc_url, build.api_key)?; client.wait_for_rpc().await?; Ok(Self { diff --git a/workspaces/src/rpc/client.rs b/workspaces/src/rpc/client.rs index 312af37c..ff515db8 100644 --- a/workspaces/src/rpc/client.rs +++ b/workspaces/src/rpc/client.rs @@ -61,10 +61,10 @@ pub struct Client { } impl Client { - pub(crate) fn new(rpc_addr: &str) -> Result { + pub(crate) fn new(rpc_addr: &str, api_key: Option) -> Result { let connector = JsonRpcClient::new_client(); let mut rpc_client = connector.connect(rpc_addr); - if let Ok(api_key) = std::env::var("NEAR_RPC_API_KEY") { + if let Some(api_key) = api_key { let api_key = near_jsonrpc_client::auth::ApiKey::new(api_key) .map_err(|e| ErrorKind::DataConversion.custom(e))?; rpc_client = rpc_client.header(api_key); diff --git a/workspaces/src/worker/mod.rs b/workspaces/src/worker/mod.rs index d531efe6..f90eb96c 100644 --- a/workspaces/src/worker/mod.rs +++ b/workspaces/src/worker/mod.rs @@ -76,6 +76,9 @@ pub fn betanet<'a>() -> NetworkBuilder<'a, Betanet> { NetworkBuilder::new("betanet") } +/// Connect to a custom network, and grab a [`Worker`] that can interact with it. +/// +/// Note: the burden of ensuring the methods that are able to be called are left up to the user. pub fn custom<'a>(rpc_url: &str) -> NetworkBuilder<'a, Custom> { NetworkBuilder::new("custom").rpc_addr(rpc_url) }