Skip to content

Commit

Permalink
feat: add forc contract-id and forc predicate-id commands (FuelLa…
Browse files Browse the repository at this point in the history
…bs#4338)

## Description
Adds `forc contract-id` and `forc predicate-id` commands for contract id and predicate root detection.

closes FuelLabs#3444.
  • Loading branch information
kayagokalp authored Apr 4, 2023
1 parent d7df106 commit 8e30dc2
Show file tree
Hide file tree
Showing 17 changed files with 276 additions and 42 deletions.
62 changes: 36 additions & 26 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/book/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -68,10 +68,12 @@
- [forc check](./forc/commands/forc_check.md)
- [forc clean](./forc/commands/forc_clean.md)
- [forc completions](./forc/commands/forc_completions.md)
- [forc contract-id](./forc/commands/forc_contract-id.md)
- [forc init](./forc/commands/forc_init.md)
- [forc new](./forc/commands/forc_new.md)
- [forc parse-bytecode](./forc/commands/forc_parse-bytecode.md)
- [forc plugins](./forc/commands/forc_plugins.md)
- [forc predicate-root](./forc/commands/forc_predicate-root.md)
- [forc test](./forc/commands/forc_test.md)
- [forc update](./forc/commands/forc_update.md)
- [forc template](./forc/commands/forc_template.md)
Expand Down
1 change: 1 addition & 0 deletions docs/book/src/forc/commands/forc_contract-id.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# forc contract-id
1 change: 1 addition & 0 deletions docs/book/src/forc/commands/forc_predicate-root.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# forc predicate-root
34 changes: 31 additions & 3 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl Default for MemberFilter {
}

impl MemberFilter {
/// Returns a new `BuildFilter` that only builds scripts.
/// Returns a new `MemberFilter` that only builds scripts.
pub fn only_scripts() -> Self {
Self {
build_contracts: false,
Expand All @@ -336,7 +336,7 @@ impl MemberFilter {
}
}

/// Returns a new `BuildFilter` that only builds contracts.
/// Returns a new `MemberFilter` that only builds contracts.
pub fn only_contracts() -> Self {
Self {
build_contracts: true,
Expand All @@ -346,7 +346,17 @@ impl MemberFilter {
}
}

/// Filter given target of output nodes according to the this `BuildFilter`.
/// Returns a new `MemberFilter`, that only builds predicates.
pub fn only_predicates() -> Self {
Self {
build_contracts: false,
build_scripts: false,
build_predicates: true,
build_libraries: false,
}
}

/// Filter given target of output nodes according to the this `MemberFilter`.
pub fn filter_outputs(
&self,
build_plan: &BuildPlan,
Expand Down Expand Up @@ -756,6 +766,24 @@ impl BuildPlan {
.map(|(n, p)| (n.clone(), p.clone()))
})
}

/// Returns a salt for the given pinned package if it is a contract and `None` for libraries.
pub fn salt(&self, pinned: &Pinned) -> Option<fuel_tx::Salt> {
let graph = self.graph();
let node_ix = graph
.node_indices()
.find(|node_ix| graph[*node_ix] == *pinned);
node_ix.and_then(|node| {
graph
.edges_directed(node, Direction::Incoming)
.map(|e| match e.weight().kind {
DepKind::Library => None,
DepKind::Contract { salt } => Some(salt),
})
.next()
.flatten()
})
}
}

impl Programs {
Expand Down
3 changes: 2 additions & 1 deletion forc-plugins/forc-client/src/cmd/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@ use clap::Parser;
use fuel_crypto::SecretKey;

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

#[derive(Debug, Default, Parser)]
#[clap(bin_name = "forc deploy", version)]
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-tx/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ path = "src/main.rs"
anyhow = "1"
clap = { version = "3", features = ["derive", "env"] }
devault = "0.1"
forc-util = { version = "0.35.5", path = "../../forc-util" }
fuel-tx = { workspace = true, features = ["serde"] }
serde = "1.0"
serde_json = { version = "1" }
Expand Down
11 changes: 1 addition & 10 deletions forc-plugins/forc-tx/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
use clap::{Args, Parser};
use devault::Devault;
use forc_util::Salt;
use serde::{Deserialize, Serialize};
use std::path::PathBuf;
use thiserror::Error;
Expand Down Expand Up @@ -117,16 +118,6 @@ pub struct Maturity {
pub maturity: u32,
}

/// Added salt used to derive the contract ID.
#[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>,
}

/// Transaction input.
#[derive(Debug, Parser, Deserialize, Serialize)]
#[clap(name = "input")]
Expand Down
3 changes: 3 additions & 0 deletions forc-util/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,12 @@ repository.workspace = true
annotate-snippets = { version = "0.9", features = ["color"] }
ansi_term = "0.12"
anyhow = "1"
clap = { version = "3.1", features = ["cargo", "derive", "env"] }
dirs = "3.0.2"
forc-tracing = { version = "0.35.5", path = "../forc-tracing" }
fuel-tx = { workspace = true, features = ["serde"] }
hex = "0.4.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.73"
sway-core = { version = "0.35.5", path = "../sway-core" }
sway-error = { version = "0.35.5", path = "../sway-error" }
Expand Down
Loading

0 comments on commit 8e30dc2

Please sign in to comment.