Skip to content

Commit

Permalink
refactor for readability
Browse files Browse the repository at this point in the history
  • Loading branch information
sdankel committed Aug 18, 2023
1 parent 8768f1f commit 7424c60
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 49 deletions.
90 changes: 41 additions & 49 deletions forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,63 +199,55 @@ pub async fn deploy(command: cmd::Deploy) -> Result<Vec<DeployedContract>> {

/// Applies specified target information to the provided arguments.
///
/// Basically provides preset configurations for known test-nets.
/// Provides preset configurations for known testnets.
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")
}
if command.node_url.is_some() {
bail!("Both `--testnet` and `--node-url` were specified: must choose one")
}
Some(Target::Beta4)
} else {
command.target.clone()
let target = match (
command.testnet,
command.target.clone(),
command.node_url.clone(),
) {
(true, None, None) => Some(Target::Beta4),
(false, Some(target), None) => Some(target),
(false, None, Some(node_url)) => Target::from_target_url(node_url.as_str()),
(false, None, None) => None,
_ => bail!("Only one of `--testnet`, `--target`, or `--node-url` should be specified"),
};

// If the user specified a testnet target, we can override the gas price and limit.
if let Some(target) = target {
if command.node_url.is_some() {
bail!("Both `--target` and `--node-url` were specified: must choose one")
}
match target {
cmd::deploy::Target::Beta2
| cmd::deploy::Target::Beta3
| cmd::deploy::Target::Beta4 => {
// 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
};
if target.is_testnet() {
// If the user did not specify 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
};

// fuel_tx::ConsensusParameters::DEFAULT.max_gas_per_tx is the default value for
// the gas.limit field.
let gas_limit = if command.gas.limit
== fuel_tx::ConsensusParameters::DEFAULT.max_gas_per_tx
&& target == cmd::deploy::Target::Beta4
{
1
} else {
command.gas.limit
};
// fuel_tx::ConsensusParameters::DEFAULT.max_gas_per_tx is the default value for
// the gas.limit field.
let gas_limit = if command.gas.limit
== fuel_tx::ConsensusParameters::DEFAULT.max_gas_per_tx
&& target == cmd::deploy::Target::Beta4
{
1
} else {
command.gas.limit
};

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

Ok(command)
}

/// Deploy a single pkg given deploy command and the manifest file
Expand Down
17 changes: 17 additions & 0 deletions forc-plugins/forc-client/src/util/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,23 @@ impl Target {
Target::Local => NODE_URL,
}
}

pub fn from_target_url(target_url: &str) -> Option<Self> {
match target_url {
BETA_2_ENDPOINT_URL => Some(Target::Beta2),
BETA_3_ENDPOINT_URL => Some(Target::Beta3),
BETA_4_ENDPOINT_URL => Some(Target::Beta4),
NODE_URL => Some(Target::Local),
_ => None,
}
}

pub fn is_testnet(&self) -> bool {
match self {
Target::Beta2 | Target::Beta3 | Target::Beta4 => true,
Target::Local => false,
}
}
}

impl FromStr for Target {
Expand Down

0 comments on commit 7424c60

Please sign in to comment.