Skip to content

Commit

Permalink
feat: add --target and --testnet parameters to forc-deploy (Fu…
Browse files Browse the repository at this point in the history
…elLabs#4764)

## Description
closes FuelLabs#4759.

Adds --target parameter to forc-deploy which can take `beta-2`, `beta-3`
or `latest`. If nothing is specified it defaults to `latest`. If
`beta-2` or `beta-3` is specified, preset values are used for node-url
and gas price so that users can deploy with a single parameter. So `forc
deploy --target beta-3` would work but since forc-deploy is not
compatible with beta-3 that feature will fail until we have a beta-4
released.

Also adds `--testnet` flag which is translated into a `--target beta-3`.
So once a beta network release is done with this additions, users will
be able to simply use

```console
forc deploy --testnet
```
to deploy to the latest test-net supported by that forc-deploy version
## Checklist

- [ ] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [ ] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [ ] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Joshua Batty <[email protected]>
  • Loading branch information
kayagokalp and JoshuaBatty authored Jul 7, 2023
1 parent d8ee8a2 commit 2fc902a
Show file tree
Hide file tree
Showing 4 changed files with 102 additions and 1 deletion.
9 changes: 9 additions & 0 deletions forc-plugins/forc-client/src/cmd/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use clap::Parser;
use fuel_crypto::SecretKey;

pub use crate::util::Target;
pub use forc::cli::shared::{BuildOutput, BuildProfile, Minify, Pkg, Print};
pub use forc_tx::{Gas, Maturity};
pub use forc_util::tx_utils::Salt;
Expand Down Expand Up @@ -50,4 +51,12 @@ pub struct Command {
/// Sign the deployment transaction manually.
#[clap(long)]
pub manual_signing: bool,
/// Use preset configurations for deploying to a specific target.
///
/// Possible values are: [beta-1, beta-2, beta-3, latest]
#[clap(long)]
pub target: Option<Target>,
/// Use preset configuration for the latest testnet.
#[clap(long)]
pub testnet: bool,
}
2 changes: 2 additions & 0 deletions forc-plugins/forc-client/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,6 @@ mod util;
pub mod default {
/// Default to localhost to favour the common case of testing.
pub const NODE_URL: &str = sway_utils::constants::DEFAULT_NODE_URL;
pub const BETA_2_ENDPOINT_URL: &str = "node-beta-2.fuel.network/graphql";
pub const BETA_3_ENDPOINT_URL: &str = "beta-3.fuel.network/graphql";
}
46 changes: 45 additions & 1 deletion forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use crate::{
cmd,
cmd::{self, deploy::Target},
util::{
pkg::built_pkgs,
tx::{TransactionBuilderExt, WalletSelectionMode, TX_SUBMIT_TIMEOUT_MS},
},
};
use anyhow::{bail, Context, Result};
use forc_pkg::{self as pkg, PackageManifestFile};
use forc_tx::Gas;
use fuel_core_client::client::types::TransactionStatus;
use fuel_core_client::client::FuelClient;
use fuel_tx::{Output, Salt, TransactionBuilder};
Expand Down Expand Up @@ -77,6 +78,7 @@ fn validate_and_parse_salts<'a>(
///
/// When deploying a single contract, only that contract's ID is returned.
pub async fn deploy(command: cmd::Deploy) -> Result<Vec<DeployedContract>> {
let command = apply_target(command)?;
let mut contract_ids = Vec::new();
let curr_dir = if let Some(ref path) = command.pkg.path {
PathBuf::from(path)
Expand Down Expand Up @@ -153,6 +155,48 @@ pub async fn deploy(command: cmd::Deploy) -> Result<Vec<DeployedContract>> {
Ok(contract_ids)
}

/// Applies specified target information to the provided arguments.
///
/// Basically provides preset configurations for known test-nets.
fn apply_target(command: cmd::Deploy) -> Result<cmd::Deploy> {
let deploy_to_latest_testnet = command.testnet;
let target = if deploy_to_latest_testnet {
if command.target.is_some() {
bail!("Both `--testnet` and `--target` were specified: must choose one")
}
Some(Target::Beta3)
} else {
command.target.clone()
};

if let Some(target) = target {
match target {
cmd::deploy::Target::Beta2 | cmd::deploy::Target::Beta3 => {
// If the user did not specified a gas price, we can use `1` as a gas price for
// beta test-nets.
let gas_price = if command.gas.price == 0 {
1
} else {
command.gas.price
};

let target_url = Some(target.target_url().to_string());
Ok(cmd::Deploy {
gas: Gas {
price: gas_price,
..command.gas
},
node_url: target_url,
..command
})
}
cmd::deploy::Target::LATEST => Ok(command),
}
} else {
Ok(command)
}
}

/// Deploy a single pkg given deploy command and the manifest file
pub async fn deploy_pkg(
command: &cmd::Deploy,
Expand Down
46 changes: 46 additions & 0 deletions forc-plugins/forc-client/src/util/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,49 @@
use std::str::FromStr;

pub(crate) mod encode;
pub(crate) mod pkg;
pub(crate) mod tx;

use crate::default::{BETA_2_ENDPOINT_URL, BETA_3_ENDPOINT_URL, NODE_URL};

#[derive(Debug, Clone)]
/// Possible target values that forc-client can interact with.
pub enum Target {
Beta2,
Beta3,
LATEST,
}

impl Default for Target {
fn default() -> Self {
Self::LATEST
}
}

impl Target {
pub fn target_url(&self) -> &str {
match self {
Target::Beta2 => BETA_2_ENDPOINT_URL,
Target::Beta3 => BETA_3_ENDPOINT_URL,
Target::LATEST => NODE_URL,
}
}
}

impl FromStr for Target {
type Err = anyhow::Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s == "latest" {
Ok(Target::LATEST)
} else if s == "beta-2" {
Ok(Target::Beta2)
} else if s == "beta-3" {
Ok(Target::Beta3)
} else {
anyhow::bail!(
"invalid testnet name provided. Possible values are 'beta-2', 'beta-3', 'latest'."
)
}
}
}

0 comments on commit 2fc902a

Please sign in to comment.