Skip to content

Commit

Permalink
Split client interaction commands into a forc-client plugin (FuelLabs…
Browse files Browse the repository at this point in the history
  • Loading branch information
kayagokalp authored Aug 18, 2022
1 parent 0e50890 commit 1ed1cf1
Show file tree
Hide file tree
Showing 31 changed files with 221 additions and 80 deletions.
9 changes: 8 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,7 @@ jobs:
cargo install --debug --path ./forc-plugins/forc-fmt
cargo install --debug --path ./forc-plugins/forc-lsp
cargo install --debug --path ./forc-plugins/forc-explore
cargo install --debug --path ./forc-plugins/forc-client
- name: Install mdbook-forc-documenter
uses: actions-rs/cargo@v1
with:
Expand Down Expand Up @@ -608,6 +609,12 @@ jobs:
command: install
args: --profile=release --path ./forc-plugins/forc-explore

- name: Install Forc-Client
uses: actions-rs/cargo@v1
with:
command: install
args: --profile=release --path ./forc-plugins/forc-client

- name: Prep Assets
id: prep_assets
env:
Expand All @@ -618,7 +625,7 @@ jobs:
ZIP_FILE_NAME=forc-binaries-${{ env.PLATFORM_NAME }}_${{ env.ARCH }}.tar.gz
echo "ZIP_FILE_NAME=$ZIP_FILE_NAME" >> $GITHUB_ENV
mkdir -pv ./forc-binaries
for binary in forc forc-fmt forc-lsp forc-explore; do
for binary in forc forc-fmt forc-lsp forc-explore forc-client; do
cp $(which ${binary}) ./forc-binaries
done
tar -czvf $ZIP_FILE_NAME ./forc-binaries
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/gh-pages.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ jobs:
cargo install --debug --path ./forc-plugins/forc-fmt
cargo install --debug --path ./forc-plugins/forc-lsp
cargo install --debug --path ./forc-plugins/forc-explore
cargo install --debug --path ./forc-plugins/forc-client
- name: Install mdbook-forc-documenter
uses: actions-rs/cargo@v1
with:
Expand Down
28 changes: 23 additions & 5 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ members = [
"forc-plugins/forc-fmt",
"forc-plugins/forc-fmt-v2",
"forc-plugins/forc-lsp",
"forc-plugins/forc-client",
"forc-util",
"scripts/examples-checker",
"scripts/mdbook-forc-documenter",
Expand Down
5 changes: 3 additions & 2 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,17 +63,18 @@
- [forc check](./forc/commands/forc_check.md)
- [forc clean](./forc/commands/forc_clean.md)
- [forc completions](./forc/commands/forc_completions.md)
- [forc deploy](./forc/commands/forc_deploy.md)
- [forc init](./forc/commands/forc_init.md)
- [forc json-abi](./forc/commands/forc_json-abi.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 run](./forc/commands/forc_run.md)
- [forc test](./forc/commands/forc_test.md)
- [forc update](./forc/commands/forc_update.md)
- [forc template](./forc/commands/forc_template.md)
- [Plugins](./forc/plugins.md)
- [forc client](./forc_client.md)
- [forc deploy](./forc_deploy.md)
- [forc run](./forc_run.md)
- [forc explore](./forc_explore.md)
- [forc fmt](./forc_fmt.md)
- [forc lsp](./forc_lsp.md)
3 changes: 3 additions & 0 deletions docs/src/forc_client.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# forc-client

Forc plugin for interacting with a Fuel node.
1 change: 1 addition & 0 deletions docs/src/forc_deploy.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# forc deploy
1 change: 1 addition & 0 deletions docs/src/forc_run.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# forc run
34 changes: 34 additions & 0 deletions forc-plugins/forc-client/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
[package]
name = "forc-client"
version = "0.19.2"
edition = "2021"

[dependencies]
anyhow = "1"
clap = { version = "3", features = ["derive", "env"] }
forc-pkg = { version = "0.19.2", path = "../../forc-pkg" }
forc-util = { version = "0.19.2", path = "../../forc-util" }
fuel-gql-client = { version = "0.9", default-features = false }
fuel-tx = "0.13"
fuel-vm = "0.12"
futures = "0.3"
hex = "0.4.3"
serde = { version = "1.0" }
serde_json = "1.0.73"
sway-core = { version = "0.19.2", path = "../../sway-core" }
sway-types = { version = "0.19.2", path = "../../sway-types" }
sway-utils = { version = "0.19.2", path = "../../sway-utils" }
tokio = { version = "1.8", features = ["macros", "rt-multi-thread", "process"] }
tracing = "0.1"


[[bin]]
name = "forc-deploy"
path = "src/bin/deploy/main.rs"

[[bin]]
name = "forc-run"
path = "src/bin/run/main.rs"

[lib]
path = "src/lib.rs"
15 changes: 15 additions & 0 deletions forc-plugins/forc-client/src/bin/deploy/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use forc_client::ops::deploy::{cmd::DeployCommand, op::deploy};
use forc_util::init_tracing_subscriber;
use std::process;

use clap::Parser;

#[tokio::main]
pub async fn main() {
init_tracing_subscriber();
let command = DeployCommand::parse();
if let Err(err) = deploy(command).await {
eprintln!("Error: {:?}", err);
process::exit(1);
}
}
15 changes: 15 additions & 0 deletions forc-plugins/forc-client/src/bin/run/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use forc_client::ops::run::{cmd::RunCommand, op::run};
use forc_util::init_tracing_subscriber;
use std::process;

use clap::Parser;

#[tokio::main]
async fn main() {
init_tracing_subscriber();
let command = RunCommand::parse();
if let Err(err) = run(command).await {
eprintln!("Error: {:?}", err);
process::exit(1);
}
}
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod ops;
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
use crate::ops::forc_deploy;
use anyhow::{bail, Result};
use clap::Parser;

/// Deploy contract project.
/// Crafts a contract deployment transaction then sends it to a running node.
#[derive(Debug, Default, Parser)]
pub struct Command {
#[clap(bin_name = "forc deploy", version)]
pub struct DeployCommand {
/// Path to the project, if not specified, current working directory will be used.
#[clap(short, long)]
pub path: Option<String>,
Expand Down Expand Up @@ -73,10 +70,3 @@ pub struct Command {
#[clap(long)]
pub time_phases: bool,
}

pub(crate) async fn exec(command: Command) -> Result<()> {
match forc_deploy::deploy(command).await {
Err(e) => bail!("{}", e),
_ => Ok(()),
}
}
2 changes: 2 additions & 0 deletions forc-plugins/forc-client/src/ops/deploy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod cmd;
pub mod op;
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
use crate::cli::{BuildCommand, DeployCommand};
use crate::ops::forc_build;
use anyhow::{bail, Result};
use forc_pkg::ManifestFile;
use forc_pkg::{BuildOptions, ManifestFile};
use fuel_gql_client::client::FuelClient;
use fuel_tx::{Output, Salt, StorageSlot, Transaction};
use fuel_vm::prelude::*;
Expand All @@ -10,6 +8,8 @@ use sway_core::TreeType;
use sway_utils::constants::DEFAULT_NODE_URL;
use tracing::info;

use super::cmd::DeployCommand;

pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
let curr_dir = if let Some(ref path) = command.path {
PathBuf::from(path)
Expand Down Expand Up @@ -39,7 +39,7 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
time_phases,
} = command;

let build_command = BuildCommand {
let build_options = BuildOptions {
path,
print_ast,
print_finalized_asm,
Expand All @@ -58,7 +58,7 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
time_phases,
};

let compiled = forc_build::build(build_command)?;
let compiled = forc_pkg::build_with_options(build_options)?;
let (tx, contract_id) = create_contract_tx(
compiled.bytecode,
Vec::<fuel_tx::Input>::new(),
Expand Down
2 changes: 2 additions & 0 deletions forc-plugins/forc-client/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
pub mod deploy;
pub mod run;
Original file line number Diff line number Diff line change
@@ -1,11 +1,10 @@
use crate::ops::forc_run;
use anyhow::{bail, Result};
use clap::Parser;

/// Run script project.
/// Crafts a script transaction then sends it to a running node.
#[derive(Debug, Default, Parser)]
pub struct Command {
#[clap(bin_name = "forc run", version)]
pub struct RunCommand {
/// Hex string of data to input to script.
#[clap(short, long)]
pub data: Option<String>,
Expand Down Expand Up @@ -115,10 +114,3 @@ pub struct Command {
#[clap(long)]
pub simulate: bool,
}

pub(crate) async fn exec(command: Command) -> Result<()> {
match forc_run::run(command).await {
Err(e) => bail!("{}", e),
_ => Ok(()),
}
}
3 changes: 3 additions & 0 deletions forc-plugins/forc-client/src/ops/run/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
pub mod cmd;
pub mod op;
mod parameters;
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
use crate::cli::{BuildCommand, RunCommand};
use crate::ops::forc_build;
use crate::utils::defaults::NODE_URL;
use crate::utils::parameters::TxParameters;
use anyhow::{anyhow, bail, Result};
use forc_pkg::{fuel_core_not_running, ManifestFile};
use forc_pkg::{fuel_core_not_running, BuildOptions, ManifestFile};
use fuel_gql_client::client::FuelClient;
use fuel_tx::Transaction;
use futures::TryFutureExt;
use std::path::PathBuf;
use std::str::FromStr;
use std::{path::PathBuf, str::FromStr};
use sway_core::TreeType;
use tracing::info;

use super::{cmd::RunCommand, parameters::TxParameters};

pub const NODE_URL: &str = "http://127.0.0.1:4000";

pub async fn run(command: RunCommand) -> Result<Vec<fuel_tx::Receipt>> {
let path_dir = if let Some(path) = &command.path {
PathBuf::from(path)
Expand All @@ -25,7 +24,7 @@ pub async fn run(command: RunCommand) -> Result<Vec<fuel_tx::Receipt>> {
let data = format_hex_data(input_data);
let script_data = hex::decode(data).expect("Invalid hex");

let build_command = BuildCommand {
let build_options = BuildOptions {
path: command.path,
print_ast: command.print_ast,
print_finalized_asm: command.print_finalized_asm,
Expand All @@ -44,7 +43,7 @@ pub async fn run(command: RunCommand) -> Result<Vec<fuel_tx::Receipt>> {
time_phases: command.time_phases,
};

let compiled = forc_build::build(build_command)?;
let compiled = forc_pkg::build_with_options(build_options)?;
let contracts = command.contract.unwrap_or_default();
let (inputs, outputs) = get_tx_inputs_and_outputs(contracts);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#[derive(Debug)]
pub(crate) struct TxParameters {
pub struct TxParameters {
pub byte_price: u64,
pub gas_limit: u64,
pub gas_price: u64,
Expand Down
7 changes: 1 addition & 6 deletions forc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -25,19 +25,14 @@ forc-pkg = { version = "0.19.2", path = "../forc-pkg" }
forc-util = { version = "0.19.2", path = "../forc-util" }
fs_extra = "1.2"
fuel-asm = "0.6"
fuel-crypto = "0.5"
fuel-gql-client = { version = "0.9", default-features = false }
fuel-tx = "0.13"
fuel-vm = "0.12"
futures = "0.3"
hex = "0.4.3"
serde = { version = "1.0", features = ["derive"] }
serde_json = "1.0.73"
sway-core = { version = "0.19.2", path = "../sway-core" }
sway-types = { version = "0.19.2", path = "../sway-types" }
sway-utils = { version = "0.19.2", path = "../sway-utils" }
term-table = "1.3"
tokio = { version = "1.8.0", features = ["macros", "rt-multi-thread", "process"] }
tokio = { version = "1.8.0", features = ["macros", "rt-multi-thread"] }
toml = "0.5"
toml_edit = "0.13"
tracing = "0.1"
Expand Down
2 changes: 0 additions & 2 deletions forc/src/cli/commands/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,11 @@ pub mod build;
pub mod check;
pub mod clean;
pub mod completions;
pub mod deploy;
pub mod init;
pub mod json_abi;
pub mod new;
pub mod parse_bytecode;
pub mod plugins;
pub mod run;
pub mod template;
pub mod test;
pub mod update;
Loading

0 comments on commit 1ed1cf1

Please sign in to comment.