Skip to content

Commit

Permalink
chore: make fuel-tx optional to forc-util (FuelLabs#4668)
Browse files Browse the repository at this point in the history
## Description
closes FuelLabs#4667.

To be more WASM friendly and increase reuseability of `forc-util` this
PR hides fuel-tx related utilities under `fuel-tx` flag.
  • Loading branch information
kayagokalp authored Jun 16, 2023
1 parent a413feb commit 26581f5
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 42 deletions.
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/cmd/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use fuel_crypto::SecretKey;

pub use forc::cli::shared::{BuildOutput, BuildProfile, Minify, Pkg, Print};
pub use forc_tx::{Gas, Maturity};
pub use forc_util::Salt;
pub use forc_util::tx_utils::Salt;

#[derive(Debug, Default, Parser)]
#[clap(bin_name = "forc deploy", version)]
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/op/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use crate::{
};
use anyhow::{anyhow, bail, Context, Result};
use forc_pkg::{self as pkg, fuel_core_not_running, PackageManifestFile};
use forc_util::format_log_receipts;
use forc_util::tx_utils::format_log_receipts;
use fuel_core_client::client::FuelClient;
use fuel_tx::{ContractId, Transaction, TransactionBuilder};
use pkg::BuiltPackage;
Expand Down
2 changes: 1 addition & 1 deletion forc-plugins/forc-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use clap::{Args, Parser};
use devault::Devault;
use forc_util::Salt;
use forc_util::tx_utils::Salt;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use thiserror::Error;
Expand Down
5 changes: 4 additions & 1 deletion forc-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ anyhow = "1"
clap = { version = "3.1", features = ["cargo", "derive", "env"] }
dirs = "3.0.2"
forc-tracing = { version = "0.40.1", path = "../forc-tracing" }
fuel-tx = { workspace = true, features = ["serde"] }
fuel-tx = { workspace = true, features = ["serde"], optional = true }
hex = "0.4.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.73"
Expand All @@ -27,3 +27,6 @@ tracing = "0.1"
tracing-subscriber = { version = "0.3", features = ["ansi", "env-filter", "json"] }
unicode-xid = "0.2.2"
walkdir = "2.3.3"

[features]
default = ["fuel-tx"]
81 changes: 45 additions & 36 deletions forc-util/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,13 @@ use annotate_snippets::{
};
use ansi_term::Colour;
use anyhow::{bail, Result};
use clap::Args;
use forc_tracing::{println_red_err, println_yellow_err};
use serde::{Deserialize, Serialize};
use std::str;
use std::{ffi::OsStr, process::Termination};
use std::{
fmt::Display,
path::{Path, PathBuf},
};
use sway_core::fuel_prelude::fuel_tx;
use sway_core::language::parsed::TreeType;
use sway_error::error::CompileError;
use sway_error::warning::CompileWarning;
Expand Down Expand Up @@ -132,44 +129,56 @@ macro_rules! forc_result_bail {
};
}

/// Added salt used to derive the contract ID.
#[derive(Debug, Args, Default, Deserialize, Serialize)]
pub struct Salt {
#[cfg(feature = "fuel-tx")]
pub mod tx_utils {

use anyhow::Result;
use clap::Args;
use serde::{Deserialize, Serialize};
use sway_core::fuel_prelude::fuel_tx;

/// Added salt used to derive the contract ID.
///
/// By default, this is `0x0000000000000000000000000000000000000000000000000000000000000000`.
#[clap(long = "salt")]
pub salt: Option<fuel_tx::Salt>,
}

/// Format `Log` and `LogData` receipts.
pub fn format_log_receipts(receipts: &[fuel_tx::Receipt], pretty_print: bool) -> Result<String> {
let mut receipt_to_json_array = serde_json::to_value(receipts)?;
for (rec_index, receipt) in receipts.iter().enumerate() {
let rec_value = receipt_to_json_array.get_mut(rec_index).ok_or_else(|| {
anyhow::anyhow!(
"Serialized receipts does not contain {} th index",
rec_index
)
})?;
match receipt {
fuel_tx::Receipt::LogData { data, .. } => {
if let Some(v) = rec_value.pointer_mut("/LogData/data") {
*v = hex::encode(data).into();
#[derive(Debug, Args, Default, Deserialize, Serialize)]
pub struct Salt {
/// Added salt used to derive the contract ID.
///
/// By default, this is `0x0000000000000000000000000000000000000000000000000000000000000000`.
#[clap(long = "salt")]
pub salt: Option<fuel_tx::Salt>,
}

/// Format `Log` and `LogData` receipts.
pub fn format_log_receipts(
receipts: &[fuel_tx::Receipt],
pretty_print: bool,
) -> Result<String> {
let mut receipt_to_json_array = serde_json::to_value(receipts)?;
for (rec_index, receipt) in receipts.iter().enumerate() {
let rec_value = receipt_to_json_array.get_mut(rec_index).ok_or_else(|| {
anyhow::anyhow!(
"Serialized receipts does not contain {} th index",
rec_index
)
})?;
match receipt {
fuel_tx::Receipt::LogData { data, .. } => {
if let Some(v) = rec_value.pointer_mut("/LogData/data") {
*v = hex::encode(data).into();
}
}
}
fuel_tx::Receipt::ReturnData { data, .. } => {
if let Some(v) = rec_value.pointer_mut("/ReturnData/data") {
*v = hex::encode(data).into();
fuel_tx::Receipt::ReturnData { data, .. } => {
if let Some(v) = rec_value.pointer_mut("/ReturnData/data") {
*v = hex::encode(data).into();
}
}
_ => {}
}
_ => {}
}
}
if pretty_print {
Ok(serde_json::to_string_pretty(&receipt_to_json_array)?)
} else {
Ok(serde_json::to_string(&receipt_to_json_array)?)
if pretty_print {
Ok(serde_json::to_string_pretty(&receipt_to_json_array)?)
} else {
Ok(serde_json::to_string(&receipt_to_json_array)?)
}
}
}

Expand Down
2 changes: 1 addition & 1 deletion forc/src/cli/commands/contract_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use crate::{
ops::forc_contract_id,
};
use clap::Parser;
use forc_util::{ForcResult, Salt};
use forc_util::{tx_utils::Salt, ForcResult};

/// Determine contract-id for a contract. For workspaces outputs all contract ids in the workspace.
#[derive(Debug, Parser)]
Expand Down
2 changes: 1 addition & 1 deletion forc/src/cli/commands/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use ansi_term::Colour;
use clap::Parser;
use forc_pkg as pkg;
use forc_test::{TestFilter, TestRunnerCount, TestedPackage};
use forc_util::{format_log_receipts, ForcError, ForcResult};
use forc_util::{tx_utils::format_log_receipts, ForcError, ForcResult};
use tracing::info;

/// Run the Sway unit tests for the current project.
Expand Down

0 comments on commit 26581f5

Please sign in to comment.