Skip to content

Commit

Permalink
Added network builder for mainnet, testnet, betanet (#221)
Browse files Browse the repository at this point in the history
  • Loading branch information
ChaoticTempest authored Jun 20, 2023
1 parent ea65434 commit 26ca5ae
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 65 deletions.
17 changes: 11 additions & 6 deletions workspaces/src/network/betanet.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
use crate::network::builder::{FromNetworkBuilder, NetworkBuilder};
use crate::network::{Info, NetworkClient, NetworkInfo};
use crate::rpc::client::Client;

use std::path::PathBuf;

const RPC_URL: &str = "https://rpc.betanet.near.org";
/// URL to the betanet RPC node provided by near.org.
pub const RPC_URL: &str = "https://rpc.betanet.near.org";

/// Betanet related configuration for interacting with betanet. Look at
/// [`workspaces::betanet`] for how to spin up a [`Worker`] that can be
Expand All @@ -20,18 +23,20 @@ pub struct Betanet {
info: Info,
}

impl Betanet {
pub(crate) async fn new() -> crate::result::Result<Self> {
let client = Client::new(RPC_URL);
#[async_trait::async_trait]
impl FromNetworkBuilder for Betanet {
async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> crate::result::Result<Self> {
let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into());
let client = Client::new(&rpc_url);
client.wait_for_rpc().await?;

Ok(Self {
client,
info: Info {
name: "betanet".into(),
name: build.name.into(),
root_id: "near".parse().unwrap(),
keystore_path: PathBuf::from(".near-credentials/betanet/"),
rpc_url: RPC_URL.into(),
rpc_url,
},
})
}
Expand Down
36 changes: 14 additions & 22 deletions workspaces/src/network/mainnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,13 @@ use crate::result::Result;
use crate::rpc::client::Client;
use std::path::PathBuf;

const RPC_URL: &str = "https://rpc.mainnet.near.org";
const ARCHIVAL_URL: &str = "https://archival-rpc.mainnet.near.org";
use super::builder::{FromNetworkBuilder, NetworkBuilder};

/// URL to the mainnet RPC node provided by near.org.
pub const RPC_URL: &str = "https://rpc.mainnet.near.org";

/// URL to the mainnet archival RPC node provided by near.org.
pub const ARCHIVAL_URL: &str = "https://archival-rpc.mainnet.near.org";

/// Mainnet related configuration for interacting with mainnet. Look at
/// [`workspaces::mainnet`] and [`workspaces::mainnet_archival`] for how to
Expand All @@ -21,33 +26,20 @@ pub struct Mainnet {
info: Info,
}

impl Mainnet {
pub(crate) async fn new() -> Result<Self> {
let client = Client::new(RPC_URL);
client.wait_for_rpc().await?;

Ok(Self {
client,
info: Info {
name: "mainnet".into(),
root_id: "near".parse().unwrap(),
keystore_path: PathBuf::from(".near-credentials/mainnet/"),
rpc_url: RPC_URL.into(),
},
})
}

pub(crate) async fn archival() -> Result<Self> {
let client = Client::new(ARCHIVAL_URL);
#[async_trait::async_trait]
impl FromNetworkBuilder for Mainnet {
async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result<Self> {
let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into());
let client = Client::new(&rpc_url);
client.wait_for_rpc().await?;

Ok(Self {
client,
info: Info {
name: "mainnet-archival".into(),
name: build.name.into(),
root_id: "near".parse().unwrap(),
keystore_path: PathBuf::from(".near-credentials/mainnet/"),
rpc_url: ARCHIVAL_URL.into(),
rpc_url,
},
})
}
Expand Down
7 changes: 4 additions & 3 deletions workspaces/src/network/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,18 @@
//!
//! Currently the builtin network types are [`Mainnet`], [`Testnet`], and [`Sandbox`].

mod betanet;
mod config;
mod info;
mod mainnet;
mod sandbox;
mod server;
mod testnet;

pub(crate) mod builder;
pub(crate) mod variants;

pub mod betanet;
pub mod mainnet;
pub mod testnet;

pub(crate) use variants::DEV_ACCOUNT_SEED;

pub use self::betanet::Betanet;
Expand Down
39 changes: 16 additions & 23 deletions workspaces/src/network/testnet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,22 @@ use url::Url;

use near_primitives::views::ExecutionStatusView;

use crate::network::builder::{FromNetworkBuilder, NetworkBuilder};
use crate::network::Info;
use crate::network::{AllowDevAccountCreation, NetworkClient, NetworkInfo, TopLevelAccountCreator};
use crate::result::{Execution, ExecutionDetails, ExecutionFinalResult, ExecutionOutcome, Result};
use crate::rpc::{client::Client, tool};
use crate::types::{AccountId, InMemorySigner, SecretKey};
use crate::{Account, Contract, CryptoHash, Network, Worker};

const RPC_URL: &str = "https://rpc.testnet.near.org";
const HELPER_URL: &str = "https://helper.testnet.near.org";
const ARCHIVAL_URL: &str = "https://archival-rpc.testnet.near.org";
/// URL to the testnet RPC node provided by near.org.
pub const RPC_URL: &str = "https://rpc.testnet.near.org";

/// URL to the helper contract used to create top-level-accounts (TLA) provided by near.org.
pub const HELPER_URL: &str = "https://helper.testnet.near.org";

/// URL to the testnet archival RPC node provided by near.org.
pub const ARCHIVAL_URL: &str = "https://archival-rpc.testnet.near.org";

/// Testnet related configuration for interacting with testnet. Look at
/// [`workspaces::testnet`] and [`workspaces::testnet_archival`] for how
Expand All @@ -29,33 +35,20 @@ pub struct Testnet {
info: Info,
}

impl Testnet {
pub(crate) async fn new() -> Result<Self> {
let client = Client::new(RPC_URL);
client.wait_for_rpc().await?;

Ok(Self {
client,
info: Info {
name: "testnet".into(),
root_id: AccountId::from_str("testnet").unwrap(),
keystore_path: PathBuf::from(".near-credentials/testnet/"),
rpc_url: RPC_URL.into(),
},
})
}

pub(crate) async fn archival() -> Result<Self> {
let client = Client::new(ARCHIVAL_URL);
#[async_trait]
impl FromNetworkBuilder for Testnet {
async fn from_builder<'a>(build: NetworkBuilder<'a, Self>) -> Result<Self> {
let rpc_url = build.rpc_addr.unwrap_or_else(|| RPC_URL.into());
let client = Client::new(&rpc_url);
client.wait_for_rpc().await?;

Ok(Self {
client,
info: Info {
name: "testnet-archival".into(),
name: build.name.into(),
root_id: AccountId::from_str("testnet").unwrap(),
keystore_path: PathBuf::from(".near-credentials/testnet/"),
rpc_url: ARCHIVAL_URL.into(),
rpc_url,
},
})
}
Expand Down
22 changes: 11 additions & 11 deletions workspaces/src/worker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,37 +43,37 @@ impl<T: fmt::Debug> fmt::Debug for Worker<T> {
}

/// Spin up a new sandbox instance, and grab a [`Worker`] that interacts with it.
pub fn sandbox() -> NetworkBuilder<'static, Sandbox> {
pub fn sandbox<'a>() -> NetworkBuilder<'a, Sandbox> {
NetworkBuilder::new("sandbox")
}

/// Connect to the [testnet](https://explorer.testnet.near.org/) network, and grab
/// a [`Worker`] that can interact with it.
pub async fn testnet() -> Result<Worker<Testnet>> {
Ok(Worker::new(Testnet::new().await?))
pub fn testnet<'a>() -> NetworkBuilder<'a, Testnet> {
NetworkBuilder::new("testnet")
}

/// Connect to the [testnet archival](https://near-nodes.io/intro/node-types#archival-node)
/// network, and grab a [`Worker`] that can interact with it.
pub async fn testnet_archival() -> Result<Worker<Testnet>> {
Ok(Worker::new(Testnet::archival().await?))
pub fn testnet_archival<'a>() -> NetworkBuilder<'a, Testnet> {
NetworkBuilder::new("testnet-archival").rpc_addr(crate::network::testnet::ARCHIVAL_URL)
}

/// Connect to the [mainnet](https://explorer.near.org/) network, and grab
/// a [`Worker`] that can interact with it.
pub async fn mainnet() -> Result<Worker<Mainnet>> {
Ok(Worker::new(Mainnet::new().await?))
pub fn mainnet<'a>() -> NetworkBuilder<'a, Mainnet> {
NetworkBuilder::new("mainnet")
}

/// Connect to the [mainnet archival](https://near-nodes.io/intro/node-types#archival-node)
/// network, and grab a [`Worker`] that can interact with it.
pub async fn mainnet_archival() -> Result<Worker<Mainnet>> {
Ok(Worker::new(Mainnet::archival().await?))
pub fn mainnet_archival<'a>() -> NetworkBuilder<'a, Mainnet> {
NetworkBuilder::new("mainnet-archival").rpc_addr(crate::network::mainnet::ARCHIVAL_URL)
}

/// Connect to the betanet network, and grab a [`Worker`] that can interact with it.
pub async fn betanet() -> Result<Worker<Betanet>> {
Ok(Worker::new(Betanet::new().await?))
pub fn betanet<'a>() -> NetworkBuilder<'a, Betanet> {
NetworkBuilder::new("betanet")
}

/// Run a locally scoped task where a [`sandbox`] instanced [`Worker`] is supplied.
Expand Down

0 comments on commit 26ca5ae

Please sign in to comment.