Skip to content

Commit

Permalink
add new target to forc
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Oct 7, 2024
1 parent 66bb430 commit 64a1fc0
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 16 deletions.
2 changes: 2 additions & 0 deletions forc-plugins/forc-client/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ pub const BETA_4_ENDPOINT_URL: &str = "https://beta-4.fuel.network";
pub const BETA_5_ENDPOINT_URL: &str = "https://beta-5.fuel.network";
pub const DEVNET_ENDPOINT_URL: &str = "https://devnet.fuel.network";
pub const TESTNET_ENDPOINT_URL: &str = "https://testnet.fuel.network";
pub const MAINNET_ENDPOINT_URL: &str = "https://mainnet.fuel.network";

pub const BETA_2_FAUCET_URL: &str = "https://faucet-beta-2.fuel.network";
pub const BETA_3_FAUCET_URL: &str = "https://faucet-beta-3.fuel.network";
pub const BETA_4_FAUCET_URL: &str = "https://faucet-beta-4.fuel.network";
pub const BETA_5_FAUCET_URL: &str = "https://faucet-beta-5.fuel.network";
pub const DEVNET_FAUCET_URL: &str = "https://faucet-devnet.fuel.network";
pub const TESTNET_FAUCET_URL: &str = "https://faucet-testnet.fuel.network";
pub const MAINNET_FAUCET_URL: &str = "https://faucet-mainnet.fuel.network";

pub const TESTNET_EXPLORER_URL: &str = "https://app.fuel.network";

Expand Down
8 changes: 7 additions & 1 deletion forc-plugins/forc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,15 @@ pub struct NodeTarget {
/// Possible values are: [beta-1, beta-2, beta-3, beta-4, local]
#[clap(long)]
pub target: Option<Target>,
/// Use preset configuration for the latest testnet.
/// Use preset configuration for testnet.
///
/// You can also use `--node-url` or `--target` to specify the Fuel node.
#[clap(long)]
pub testnet: bool,

/// Use preset configuration for mainnet.
///
/// You can also use `--node-url` or `--target` to specify the Fuel node.
#[clap(long)]
pub mainnet: bool,
}
6 changes: 3 additions & 3 deletions forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -192,7 +192,7 @@ async fn deploy_chunked(

let storage_slots = resolve_storage_slots(command, compiled)?;
let chain_info = provider.chain_info().await?;
let target = Target::from_str(&chain_info.name).unwrap_or(Target::testnet());
let target = Target::from_str(&chain_info.name).unwrap_or(Target::local());
let contract_url = match target.explorer_url() {
Some(explorer_url) => format!("{explorer_url}/contract/0x"),
None => "".to_string(),
Expand Down Expand Up @@ -257,7 +257,7 @@ async fn deploy_new_proxy(
.into();

let chain_info = provider.chain_info().await?;
let target = Target::from_str(&chain_info.name).unwrap_or(Target::testnet());
let target = Target::from_str(&chain_info.name).unwrap_or(Target::local());
let contract_url = match target.explorer_url() {
Some(explorer_url) => format!("{explorer_url}/contract/0x"),
None => "".to_string(),
Expand Down Expand Up @@ -948,7 +948,7 @@ fn create_deployment_artifact(
let contract_id = ContractId::from_str(&deployment_artifact.contract_id).unwrap();
let pkg_name = manifest.project_name();

let target = Target::from_str(&chain_info.name).unwrap_or(Target::testnet());
let target = Target::from_str(&chain_info.name).unwrap_or(Target::local());
let (contract_url, block_url) = match target.explorer_url() {
Some(explorer_url) => (
format!("{explorer_url}/contract/0x"),
Expand Down
54 changes: 47 additions & 7 deletions forc-plugins/forc-client/src/util/node_url.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,18 +12,22 @@ pub fn get_node_url(
) -> Result<String> {
let node_url = match (
node_target.testnet,
node_target.mainnet,
node_target.target.clone(),
node_target.node_url.clone(),
) {
(true, None, None) => Target::testnet().target_url(),
(false, Some(target), None) => target.target_url(),
(false, None, Some(node_url)) => node_url,
(false, None, None) => manifest_network
(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
.as_ref()
.map(|nw| &nw.url[..])
.unwrap_or(crate::constants::NODE_URL)
.to_string(),
_ => bail!("Only one of `--testnet`, `--target`, or `--node-url` should be specified"),
_ => bail!(
"Only one of `--testnet`, `--mainnet`, `--target`, or `--node-url` should be specified"
),
};

Ok(node_url)
Expand All @@ -35,29 +39,57 @@ fn test_get_node_url_testnet() {
target: None,
node_url: None,
testnet: true,
mainnet: false,
};

let actual = get_node_url(&input, &None).unwrap();
assert_eq!("https://testnet.fuel.network", actual);
}

#[test]
fn test_get_node_url_mainnet() {
let input = NodeTarget {
target: None,
node_url: None,
testnet: false,
mainnet: true,
};

let actual = get_node_url(&input, &None).unwrap();
assert_eq!("https://mainnet.fuel.network", actual);
}

#[test]
fn test_get_node_url_target_devnet() {
let input = NodeTarget {
target: Some(Target::Devnet),
node_url: None,
testnet: false,
mainnet: false,
};
let actual = get_node_url(&input, &None).unwrap();
assert_eq!("https://devnet.fuel.network", actual);
}

#[test]
fn test_get_node_url_target_mainnet() {
let input = NodeTarget {
target: Some(Target::Mainnet),
node_url: None,
testnet: false,
mainnet: false,
};
let actual = get_node_url(&input, &None).unwrap();
assert_eq!("https://mainnet.fuel.network", actual);
}

#[test]
fn test_get_node_url_target_testnet() {
let input = NodeTarget {
target: Some(Target::Testnet),
node_url: None,
testnet: false,
mainnet: false,
};

let actual = get_node_url(&input, &None).unwrap();
Expand All @@ -70,6 +102,7 @@ fn test_get_node_url_beta5() {
target: Some(Target::Beta5),
node_url: None,
testnet: false,
mainnet: false,
};
let actual = get_node_url(&input, &None).unwrap();
assert_eq!("https://beta-5.fuel.network", actual);
Expand All @@ -81,6 +114,7 @@ fn test_get_node_url_beta4() {
target: None,
node_url: Some("https://beta-4.fuel.network".to_string()),
testnet: false,
mainnet: false,
};
let actual = get_node_url(&input, &None).unwrap();
assert_eq!("https://beta-4.fuel.network", actual);
Expand All @@ -95,6 +129,7 @@ fn test_get_node_url_url_beta4_manifest() {
target: None,
node_url: None,
testnet: false,
mainnet: false,
};

let actual = get_node_url(&input, &Some(network)).unwrap();
Expand All @@ -107,6 +142,7 @@ fn test_get_node_url_default() {
target: None,
node_url: None,
testnet: false,
mainnet: false,
};

let actual = get_node_url(&input, &None).unwrap();
Expand All @@ -119,6 +155,7 @@ fn test_get_node_url_beta3() {
target: Some(Target::Beta3),
node_url: None,
testnet: false,
mainnet: false,
};
let actual = get_node_url(&input, &None).unwrap();
assert_eq!("https://beta-3.fuel.network", actual);
Expand All @@ -130,33 +167,36 @@ fn test_get_node_url_local() {
target: Some(Target::Local),
node_url: None,
testnet: false,
mainnet: false,
};
let actual = get_node_url(&input, &None).unwrap();
assert_eq!("http://127.0.0.1:4000", actual);
}

#[test]
#[should_panic(
expected = "Only one of `--testnet`, `--target`, or `--node-url` should be specified"
expected = "Only one of `--testnet`, `--mainnet`, `--target`, or `--node-url` should be specified"
)]
fn test_get_node_url_local_testnet() {
let input = NodeTarget {
target: Some(Target::Local),
node_url: None,
testnet: true,
mainnet: false,
};
get_node_url(&input, &None).unwrap();
}

#[test]
#[should_panic(
expected = "Only one of `--testnet`, `--target`, or `--node-url` should be specified"
expected = "Only one of `--testnet`, `--mainnet`, `--target`, or `--node-url` should be specified"
)]
fn test_get_node_url_same_url() {
let input = NodeTarget {
target: Some(Target::Beta3),
node_url: Some("beta-3.fuel.network".to_string()),
testnet: false,
mainnet: false,
};
get_node_url(&input, &None).unwrap();
}
25 changes: 21 additions & 4 deletions forc-plugins/forc-client/src/util/target.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
use crate::constants::{
BETA_2_ENDPOINT_URL, BETA_2_FAUCET_URL, BETA_3_ENDPOINT_URL, BETA_3_FAUCET_URL,
BETA_4_ENDPOINT_URL, BETA_4_FAUCET_URL, BETA_5_ENDPOINT_URL, BETA_5_FAUCET_URL,
DEVNET_ENDPOINT_URL, DEVNET_FAUCET_URL, NODE_URL, TESTNET_ENDPOINT_URL, TESTNET_EXPLORER_URL,
TESTNET_FAUCET_URL,
DEVNET_ENDPOINT_URL, DEVNET_FAUCET_URL, MAINNET_ENDPOINT_URL, NODE_URL, TESTNET_ENDPOINT_URL,
TESTNET_EXPLORER_URL, TESTNET_FAUCET_URL,
};
use anyhow::{bail, Result};
use serde::{Deserialize, Serialize};
Expand All @@ -17,6 +17,7 @@ pub enum Target {
Beta5,
Devnet,
Testnet,
Mainnet,
Local,
}

Expand All @@ -35,6 +36,7 @@ impl Target {
Target::Beta5 => BETA_5_ENDPOINT_URL,
Target::Devnet => DEVNET_ENDPOINT_URL,
Target::Testnet => TESTNET_ENDPOINT_URL,
Target::Mainnet => MAINNET_ENDPOINT_URL,
Target::Local => NODE_URL,
};
url.to_string()
Expand All @@ -48,15 +50,24 @@ impl Target {
BETA_5_ENDPOINT_URL => Some(Target::Beta5),
DEVNET_ENDPOINT_URL => Some(Target::Devnet),
TESTNET_ENDPOINT_URL => Some(Target::Testnet),
MAINNET_ENDPOINT_URL => Some(Target::Mainnet),
NODE_URL => Some(Target::Local),
_ => None,
}
}

pub fn local() -> Self {
Target::Local
}

pub fn testnet() -> Self {
Target::Testnet
}

pub fn mainnet() -> Self {
Target::Mainnet
}

pub fn faucet_url(&self) -> String {
match self {
Target::Beta2 => BETA_2_FAUCET_URL.to_string(),
Expand All @@ -65,13 +76,16 @@ impl Target {
Target::Beta5 => BETA_5_FAUCET_URL.to_string(),
Target::Devnet => DEVNET_FAUCET_URL.to_string(),
Target::Testnet => TESTNET_FAUCET_URL.to_string(),
Target::Mainnet => TESTNET_FAUCET_URL.to_string(),
Target::Local => "http://localhost:3000".to_string(),
}
}

pub fn explorer_url(&self) -> Option<String> {
match self {
Target::Testnet | Target::Devnet => Some(TESTNET_EXPLORER_URL.to_string()),
Target::Testnet | Target::Mainnet | Target::Devnet => {
Some(TESTNET_EXPLORER_URL.to_string())
}
_ => None,
}
}
Expand All @@ -88,15 +102,17 @@ impl FromStr for Target {
"beta-5" => Ok(Target::Beta5),
"devnet" => Ok(Target::Devnet),
"testnet" => Ok(Target::Testnet),
"mainnet" => Ok(Target::Mainnet),
"local" => Ok(Target::Local),
_ => bail!(
"'{s}' is not a valid target name. Possible values: '{}', '{}', '{}', '{}', '{}', '{}', '{}'",
"'{s}' is not a valid target name. Possible values: '{}', '{}', '{}', '{}', '{}', '{}', '{}', '{}'",
Target::Beta2,
Target::Beta3,
Target::Beta4,
Target::Beta5,
Target::Devnet,
Target::Testnet,
Target::Mainnet,
Target::Local
),
}
Expand All @@ -112,6 +128,7 @@ impl std::fmt::Display for Target {
Target::Beta5 => "beta-5",
Target::Devnet => "devnet",
Target::Testnet => "testnet",
Target::Mainnet => "mainnet",
Target::Local => "local",
};
write!(f, "{}", s)
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/util/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ pub(crate) async fn select_account(
let first_account = accounts
.get(&0)
.ok_or_else(|| anyhow::anyhow!("No account derived for this wallet"))?;
let target = Target::from_str(&chain_info.name).unwrap_or(Target::testnet());
let target = Target::from_str(&chain_info.name).unwrap_or(Target::local());
let faucet_link = format!("{}/?address={first_account}", target.faucet_url());
anyhow::bail!("Your wallet does not have any funds to pay for the transaction.\
\n\nIf you are interacting with a testnet consider using the faucet.\
Expand Down
Loading

0 comments on commit 64a1fc0

Please sign in to comment.