Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add new target to forc #6621

Merged
merged 7 commits into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 9 additions & 3 deletions docs/book/src/forc/plugins/forc_client/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,21 @@ By default `--default-signer` flag would sign your transactions with the followi
0xde97d8624a438121b86a1956544bd72ed68cd69f2c99555b08b1e8c51ffd511c
```

## Interacting with the testnet
## Selecting a target network

To interact with the latest testnet, use the `--testnet` flag. When this flag is passed, transactions created by `forc-deploy` will be sent to the latest `testnet`.
By default, `local` is used for the target network. To interact with the latest testnet, use the `--testnet` flag. When this flag is passed, transactions created by `forc-deploy` will be sent to the latest `testnet`:

```sh
forc-deploy --testnet
```

It is also possible to pass the exact node URL while using `forc-deploy` or `forc-run` which can be done using `--node-url` flag.
The same can be done to target mainnet:

```sh
forc-deploy --mainnet
```

It is also possible to pass the exact node URL while using `forc-deploy` or `forc-run` which can be done using `--node-url` flag:

```sh
forc-deploy --node-url https://beta-3.fuel.network
Expand Down
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,6 +6,7 @@ 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";
Expand All @@ -15,6 +16,7 @@ pub const DEVNET_FAUCET_URL: &str = "https://faucet-devnet.fuel.network";
pub const TESTNET_FAUCET_URL: &str = "https://faucet-testnet.fuel.network";

pub const TESTNET_EXPLORER_URL: &str = "https://app.fuel.network";
JoshuaBatty marked this conversation as resolved.
Show resolved Hide resolved
pub const MAINNET_EXPLORER_URL: &str = "https://app.fuel.network";

/// Default PrivateKey to sign transactions submitted to local node.
pub const DEFAULT_PRIVATE_KEY: &str =
Expand Down
18 changes: 13 additions & 5 deletions forc-plugins/forc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,27 @@ pub struct NodeTarget {
/// If unspecified, checks the manifest's `network` table, then falls back
/// to `http://127.0.0.1:4000`
///
/// You can also use `--target` or `--testnet` to specify the Fuel node.
/// You can also use `--target`, `--testnet`, or `--mainnet` to specify the Fuel node.
#[clap(long, env = "FUEL_NODE_URL")]
pub node_url: Option<String>,

/// Use preset configurations for deploying to a specific target.
///
/// You can also use `--node-url` or `--testnet` to specify the Fuel node.
/// You can also use `--node-url`, `--testnet`, or `--mainnet` to specify the Fuel node.
///
/// Possible values are: [beta-1, beta-2, beta-3, beta-4, local]
/// Possible values are: [beta-1, beta-2, beta-3, beta-4, devnet, local, testnet, mainnet]
JoshuaBatty marked this conversation as resolved.
Show resolved Hide resolved
#[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.
/// You can also use `--node-url`, `--target`, or `--mainnet` to specify the Fuel node.
#[clap(long)]
pub testnet: bool,

/// Use preset configuration for mainnet.
///
/// You can also use `--node-url`, `--target`, or `--testnet` 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_default();
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_default();
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_default();
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();
}
38 changes: 27 additions & 11 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, MAINNET_EXPLORER_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,30 +50,41 @@ 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 faucet_url(&self) -> String {
pub fn mainnet() -> Self {
Target::Mainnet
}

pub fn faucet_url(&self) -> Option<String> {
match self {
Target::Beta2 => BETA_2_FAUCET_URL.to_string(),
Target::Beta3 => BETA_3_FAUCET_URL.to_string(),
Target::Beta4 => BETA_4_FAUCET_URL.to_string(),
Target::Beta5 => BETA_5_FAUCET_URL.to_string(),
Target::Devnet => DEVNET_FAUCET_URL.to_string(),
Target::Testnet => TESTNET_FAUCET_URL.to_string(),
Target::Local => "http://localhost:3000".to_string(),
Target::Beta2 => Some(BETA_2_FAUCET_URL.to_string()),
Target::Beta3 => Some(BETA_3_FAUCET_URL.to_string()),
Target::Beta4 => Some(BETA_4_FAUCET_URL.to_string()),
Target::Beta5 => Some(BETA_5_FAUCET_URL.to_string()),
Target::Devnet => Some(DEVNET_FAUCET_URL.to_string()),
Target::Testnet => Some(TESTNET_FAUCET_URL.to_string()),
Target::Mainnet => None,
Target::Local => Some("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::Mainnet => Some(MAINNET_EXPLORER_URL.to_string()),
_ => None,
}
}
Expand All @@ -88,15 +101,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 +127,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
19 changes: 13 additions & 6 deletions forc-plugins/forc-client/src/util/tx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,12 +181,19 @@ 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 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.\
\n-> {target} network faucet: {faucet_link}\
\nIf you are interacting with a local node, consider providing a chainConfig which funds your account.")
let target = Target::from_str(&chain_info.name).unwrap_or_default();
let message = if let Some(faucet_url) = target.faucet_url() {
format!(
"Your wallet does not have any funds to pay for the transaction.\
\n\nIf you are interacting with a testnet, consider using the faucet.\
\n-> {target} network faucet: {}/?address={first_account}\
\nIf you are interacting with a local node, consider providing a chainConfig which funds your account.",
faucet_url
)
} else {
"Your wallet does not have any funds to pay for the transaction.".to_string()
};
anyhow::bail!(message)
}
let selections =
format_base_asset_account_balances(&accounts, &account_balances, base_asset_id);
Expand Down
Loading
Loading