From 24abf767186a02ba35b955ed525ba7afc8b753b5 Mon Sep 17 00:00:00 2001 From: z Date: Sat, 1 Feb 2025 04:00:40 +1300 Subject: [PATCH] forc-client devnet node url and faucet url support --- forc-plugins/forc-client/src/constants.rs | 7 ++- forc-plugins/forc-client/src/lib.rs | 47 ++++++++++++++----- forc-plugins/forc-client/src/util/node_url.rs | 36 +++++++++----- forc-plugins/forc-client/src/util/target.rs | 30 ++++++++---- forc-plugins/forc-client/tests/deploy.rs | 16 +++++++ 5 files changed, 100 insertions(+), 36 deletions(-) diff --git a/forc-plugins/forc-client/src/constants.rs b/forc-plugins/forc-client/src/constants.rs index 1b195f733e6..8470c9f4362 100644 --- a/forc-plugins/forc-client/src/constants.rs +++ b/forc-plugins/forc-client/src/constants.rs @@ -1,12 +1,15 @@ /// Default to localhost to favour the common case of testing. pub const NODE_URL: &str = sway_utils::constants::DEFAULT_NODE_URL; -pub const TESTNET_ENDPOINT_URL: &str = "https://testnet.fuel.network"; + pub const MAINNET_ENDPOINT_URL: &str = "https://mainnet.fuel.network"; +pub const TESTNET_ENDPOINT_URL: &str = "https://testnet.fuel.network"; +pub const DEVNET_ENDPOINT_URL: &str = "https://devnet.fuel.network"; pub const TESTNET_FAUCET_URL: &str = "https://faucet-testnet.fuel.network"; +pub const DEVNET_FAUCET_URL: &str = "https://faucet-devnet.fuel.network"; -pub const TESTNET_EXPLORER_URL: &str = "https://app-testnet.fuel.network"; pub const MAINNET_EXPLORER_URL: &str = "https://app.fuel.network"; +pub const TESTNET_EXPLORER_URL: &str = "https://app-testnet.fuel.network"; /// Default PrivateKey to sign transactions submitted to local node. pub const DEFAULT_PRIVATE_KEY: &str = diff --git a/forc-plugins/forc-client/src/lib.rs b/forc-plugins/forc-client/src/lib.rs index 495a5d359c7..8c7db84fdc6 100644 --- a/forc-plugins/forc-client/src/lib.rs +++ b/forc-plugins/forc-client/src/lib.rs @@ -26,17 +26,23 @@ pub struct NodeTarget { #[clap(long)] pub target: Option, + /// Use preset configuration for mainnet. + /// + /// You can also use `--node-url`, `--target`, or `--testnet` to specify the Fuel node. + #[clap(long)] + pub mainnet: bool, + /// Use preset configuration for testnet. /// /// You can also use `--node-url`, `--target`, or `--mainnet` to specify the Fuel node. #[clap(long)] pub testnet: bool, - /// Use preset configuration for mainnet. + /// Use preset configuration for devnet. /// /// You can also use `--node-url`, `--target`, or `--testnet` to specify the Fuel node. #[clap(long)] - pub mainnet: bool, + pub devnet: bool, } impl NodeTarget { @@ -45,12 +51,13 @@ impl NodeTarget { match ( self.testnet, self.mainnet, + self.devnet, self.target.clone(), self.node_url.clone(), ) { - (true, false, None, None) => Target::testnet().explorer_url(), - (false, true, None, None) => Target::mainnet().explorer_url(), - (false, false, Some(target), None) => target.explorer_url(), + (true, false, _, None, None) => Target::testnet().explorer_url(), + (false, true, _, None, None) => Target::mainnet().explorer_url(), + (false, false, _, Some(target), None) => target.explorer_url(), _ => None, } } @@ -60,28 +67,43 @@ impl NodeTarget { mod tests { use super::*; + #[test] + fn test_get_explorer_url_mainnet() { + let node = NodeTarget { + target: Some(Target::Mainnet), + node_url: None, + mainnet: false, + testnet: false, + devnet: false, + }; + let actual = node.get_explorer_url().unwrap(); + assert_eq!("https://app.fuel.network", actual); + } + #[test] fn test_get_explorer_url_testnet() { let node = NodeTarget { target: Some(Target::Testnet), node_url: None, - testnet: false, mainnet: false, + testnet: false, + devnet: false, }; let actual = node.get_explorer_url().unwrap(); assert_eq!("https://app-testnet.fuel.network", actual); } #[test] - fn test_get_explorer_url_mainnet() { + fn test_get_explorer_url_devnet() { let node = NodeTarget { - target: Some(Target::Mainnet), + target: Some(Target::Devnet), node_url: None, - testnet: false, mainnet: false, + testnet: false, + devnet: true, }; - let actual = node.get_explorer_url().unwrap(); - assert_eq!("https://app.fuel.network", actual); + let actual = node.get_explorer_url(); + assert_eq!(None, actual); } #[test] @@ -89,8 +111,9 @@ mod tests { let node = NodeTarget { target: Some(Target::Local), node_url: None, - testnet: false, mainnet: false, + testnet: false, + devnet: false, }; let actual = node.get_explorer_url(); assert_eq!(None, actual); diff --git a/forc-plugins/forc-client/src/util/node_url.rs b/forc-plugins/forc-client/src/util/node_url.rs index 32ffab797e1..404af4a34eb 100644 --- a/forc-plugins/forc-client/src/util/node_url.rs +++ b/forc-plugins/forc-client/src/util/node_url.rs @@ -14,14 +14,16 @@ pub fn get_node_url( let node_url = match ( node_target.testnet, node_target.mainnet, + node_target.devnet, node_target.target.clone(), node_target.node_url.clone(), ) { - (true, false, None, None) => Target::testnet().target_url(), - (false, true, None, None) => Target::mainnet().target_url(), - (false, false, Some(target), None) => target.target_url(), - (false, false, None, Some(node_url)) => node_url, - (false, false, None, None) => manifest_network + (true, false, false, None, None) => Target::testnet().target_url(), + (false, true, false, None, None) => Target::mainnet().target_url(), + (false, false, true, None, None) => Target::devnet().target_url(), + (false, false, false, Some(target), None) => target.target_url(), + (false, false, false, None, Some(node_url)) => node_url, + (false, false, false, None, None) => manifest_network .as_ref() .map(|nw| &nw.url[..]) .unwrap_or(crate::constants::NODE_URL) @@ -39,8 +41,9 @@ fn test_get_node_url_testnet() { let input = NodeTarget { target: None, node_url: None, - testnet: true, mainnet: false, + testnet: true, + devnet: false, }; let actual = get_node_url(&input, &None).unwrap(); @@ -52,8 +55,9 @@ fn test_get_node_url_mainnet() { let input = NodeTarget { target: None, node_url: None, - testnet: false, mainnet: true, + testnet: false, + devnet: false, }; let actual = get_node_url(&input, &None).unwrap(); @@ -65,8 +69,9 @@ fn test_get_node_url_target_mainnet() { let input = NodeTarget { target: Some(Target::Mainnet), node_url: None, - testnet: false, mainnet: false, + testnet: false, + devnet: false, }; let actual = get_node_url(&input, &None).unwrap(); assert_eq!("https://mainnet.fuel.network", actual); @@ -77,8 +82,9 @@ fn test_get_node_url_target_testnet() { let input = NodeTarget { target: Some(Target::Testnet), node_url: None, - testnet: false, mainnet: false, + testnet: false, + devnet: false, }; let actual = get_node_url(&input, &None).unwrap(); @@ -90,8 +96,9 @@ fn test_get_node_url_default() { let input = NodeTarget { target: None, node_url: None, - testnet: false, mainnet: false, + testnet: false, + devnet: false, }; let actual = get_node_url(&input, &None).unwrap(); @@ -103,8 +110,9 @@ fn test_get_node_url_local() { let input = NodeTarget { target: Some(Target::Local), node_url: None, - testnet: false, mainnet: false, + testnet: false, + devnet: false, }; let actual = get_node_url(&input, &None).unwrap(); assert_eq!("http://127.0.0.1:4000", actual); @@ -118,8 +126,9 @@ fn test_get_node_url_local_testnet() { let input = NodeTarget { target: Some(Target::Local), node_url: None, - testnet: true, mainnet: false, + testnet: true, + devnet: false, }; get_node_url(&input, &None).unwrap(); } @@ -132,8 +141,9 @@ fn test_get_node_url_same_url() { let input = NodeTarget { target: Some(Target::Testnet), node_url: Some("testnet.fuel.network".to_string()), - testnet: false, mainnet: false, + testnet: false, + devnet: false, }; get_node_url(&input, &None).unwrap(); } diff --git a/forc-plugins/forc-client/src/util/target.rs b/forc-plugins/forc-client/src/util/target.rs index bbc5679e844..ec754a190fc 100644 --- a/forc-plugins/forc-client/src/util/target.rs +++ b/forc-plugins/forc-client/src/util/target.rs @@ -1,6 +1,6 @@ use crate::constants::{ - MAINNET_ENDPOINT_URL, MAINNET_EXPLORER_URL, NODE_URL, TESTNET_ENDPOINT_URL, - TESTNET_EXPLORER_URL, TESTNET_FAUCET_URL, + DEVNET_ENDPOINT_URL, DEVNET_FAUCET_URL, MAINNET_ENDPOINT_URL, MAINNET_EXPLORER_URL, NODE_URL, + TESTNET_ENDPOINT_URL, TESTNET_EXPLORER_URL, TESTNET_FAUCET_URL, }; use anyhow::{bail, Result}; use serde::{Deserialize, Serialize}; @@ -9,8 +9,9 @@ use std::str::FromStr; #[derive(Debug, Clone, PartialEq, Eq, Deserialize, Serialize)] /// Possible target values that forc-client can interact with. pub enum Target { - Testnet, Mainnet, + Testnet, + Devnet, Local, } @@ -23,8 +24,9 @@ impl Default for Target { impl Target { pub fn target_url(&self) -> String { let url = match self { - Target::Testnet => TESTNET_ENDPOINT_URL, Target::Mainnet => MAINNET_ENDPOINT_URL, + Target::Testnet => TESTNET_ENDPOINT_URL, + Target::Devnet => DEVNET_ENDPOINT_URL, Target::Local => NODE_URL, }; url.to_string() @@ -34,6 +36,7 @@ impl Target { match target_url { TESTNET_ENDPOINT_URL => Some(Target::Testnet), MAINNET_ENDPOINT_URL => Some(Target::Mainnet), + DEVNET_ENDPOINT_URL => Some(Target::Devnet), NODE_URL => Some(Target::Local), _ => None, } @@ -43,6 +46,10 @@ impl Target { Target::Local } + pub fn devnet() -> Self { + Target::Devnet + } + pub fn testnet() -> Self { Target::Testnet } @@ -53,16 +60,18 @@ impl Target { pub fn faucet_url(&self) -> Option { match self { - Target::Testnet => Some(TESTNET_FAUCET_URL.to_string()), Target::Mainnet => None, + Target::Testnet => Some(TESTNET_FAUCET_URL.to_string()), + Target::Devnet => Some(DEVNET_FAUCET_URL.to_string()), Target::Local => Some("http://localhost:3000".to_string()), } } pub fn explorer_url(&self) -> Option { match self { - Target::Testnet => Some(TESTNET_EXPLORER_URL.to_string()), Target::Mainnet => Some(MAINNET_EXPLORER_URL.to_string()), + Target::Testnet => Some(TESTNET_EXPLORER_URL.to_string()), + Target::Devnet => None, _ => None, } } @@ -76,11 +85,13 @@ impl FromStr for Target { "Fuel Sepolia Testnet" => Ok(Target::Testnet), "Ignition" => Ok(Target::Mainnet), "local" => Ok(Target::Local), + "Devnet" | "devnet" => Ok(Target::Devnet), _ => bail!( - "'{s}' is not a valid target name. Possible values: '{}', '{}', '{}'", + "'{s}' is not a valid target name. Possible values: '{}', '{}', '{}', '{}'", Target::Testnet, Target::Mainnet, - Target::Local + Target::Local, + Target::Devnet, ), } } @@ -89,8 +100,9 @@ impl FromStr for Target { impl std::fmt::Display for Target { fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { let s = match self { - Target::Testnet => "Fuel Sepolia Testnet", Target::Mainnet => "Ignition", + Target::Testnet => "Fuel Sepolia Testnet", + Target::Devnet => "Devnet", // TODO Target::Local => "local", }; write!(f, "{}", s) diff --git a/forc-plugins/forc-client/tests/deploy.rs b/forc-plugins/forc-client/tests/deploy.rs index 00a49ecbae7..b12d42b620e 100644 --- a/forc-plugins/forc-client/tests/deploy.rs +++ b/forc-plugins/forc-client/tests/deploy.rs @@ -361,6 +361,7 @@ async fn test_simple_deploy() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -403,6 +404,7 @@ async fn test_deploy_submit_only() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -450,6 +452,7 @@ async fn test_deploy_fresh_proxy() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -502,6 +505,7 @@ async fn test_proxy_contract_re_routes_call() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -560,6 +564,7 @@ async fn test_proxy_contract_re_routes_call() { target: None, testnet: false, mainnet: false, + devnet: false, }; let pkg = Pkg { path: Some(tmp_dir.path().display().to_string()), @@ -635,6 +640,7 @@ async fn test_non_owner_fails_to_set_target() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -738,6 +744,7 @@ async fn chunked_deploy() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -771,6 +778,7 @@ async fn chunked_deploy_re_routes_calls() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -814,6 +822,7 @@ async fn chunked_deploy_with_proxy_re_routes_call() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -847,6 +856,7 @@ async fn can_deploy_script() { target: None, testnet: false, mainnet: false, + devnet: false, }; let pkg = Pkg { path: Some(tmp_dir.path().display().to_string()), @@ -878,6 +888,7 @@ async fn deploy_script_calls() { target: None, testnet: false, mainnet: false, + devnet: false, }; let pkg = Pkg { path: Some(tmp_dir.path().display().to_string()), @@ -910,6 +921,7 @@ async fn deploy_script_calls() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg, @@ -1001,6 +1013,7 @@ async fn can_deploy_predicates() { target: None, testnet: false, mainnet: false, + devnet: false, }; let pkg = Pkg { path: Some(tmp_dir.path().display().to_string()), @@ -1032,6 +1045,7 @@ async fn deployed_predicate_call() { target: None, testnet: false, mainnet: false, + devnet: false, }; let pkg = Pkg { path: Some(tmp_dir.path().display().to_string()), @@ -1178,6 +1192,7 @@ async fn call_with_forc_generated_overrides(node_url: &str, contract_id: Contrac target: None, testnet: false, mainnet: false, + devnet: false, }; let pkg = Pkg { path: Some(tmp_dir.path().display().to_string()), @@ -1288,6 +1303,7 @@ async fn offset_shifted_abi_works() { target: None, testnet: false, mainnet: false, + devnet: false, }; let cmd = cmd::Deploy { pkg,