Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: generate deployment artifacts with forc-deploy #4913

Merged
merged 13 commits into from
Aug 16, 2023
63 changes: 62 additions & 1 deletion forc-plugins/forc-client/src/op/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,20 @@ use crate::{
use anyhow::{bail, Context, Result};
use forc_pkg::{self as pkg, PackageManifestFile};
use forc_tx::Gas;
use forc_util::default_output_directory;
use fuel_core_client::client::types::TransactionStatus;
use fuel_core_client::client::FuelClient;
use fuel_crypto::fuel_types::ChainId;
use fuel_tx::{Output, Salt, TransactionBuilder};
use fuel_vm::prelude::*;
use futures::FutureExt;
use pkg::BuiltPackage;
use serde::{Deserialize, Serialize};
use std::time::Duration;
use std::{collections::BTreeMap, path::PathBuf};
use std::{
collections::BTreeMap,
path::{Path, PathBuf},
};
use sway_core::language::parsed::TreeType;
use sway_core::BuildTarget;
use tracing::info;
Expand All @@ -25,6 +31,38 @@ pub struct DeployedContract {
pub id: fuel_tx::ContractId,
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeploymentArtifact {
transaction_id: String,
salt: String,
network_endpoint: String,
chain_id: ChainId,
contract_id: String,
deployment_size: usize,
deployed_block_id: String,
}

impl DeploymentArtifact {
pub fn to_file(
&self,
output_dir: &Path,
pkg_name: &str,
contract_id: ContractId,
) -> Result<()> {
if !output_dir.exists() {
std::fs::create_dir_all(output_dir)?;
}

let deployment_artifact_json = format!("{pkg_name}-deployment-0x{contract_id}");
let deployments_path = output_dir
.join(deployment_artifact_json)
.with_extension("json");
let deployments_file = std::fs::File::create(deployments_path)?;
serde_json::to_writer_pretty(&deployments_file, &self)?;
Ok(())
}
}

type ContractSaltMap = BTreeMap<String, Salt>;

/// Takes the contract member salt inputs passed via the --salt option, validates them against
Expand Down Expand Up @@ -241,6 +279,7 @@ pub async fn deploy_pkg(
.await?;

let tx = Transaction::from(tx);
let chain_id = client.chain_info().await?.consensus_parameters.chain_id;

let deployment_request = client.submit_and_await_commit(&tx).map(|res| match res {
Ok(logs) => match logs {
Expand All @@ -254,6 +293,28 @@ pub async fn deploy_pkg(
info!("\nNetwork: {node_url}");
info!("Contract ID: 0x{contract_id}");
info!("Deployed in block {}", &block_id);

// Create a deployment artifact.
let deployment_size = bytecode.len();
let deployment_artifact = DeploymentArtifact {
transaction_id: format!("0x{}", tx.id(&chain_id)),
salt: format!("0x{}", salt),
network_endpoint: node_url.to_string(),
chain_id,
contract_id: format!("0x{}", contract_id),
deployment_size,
deployed_block_id: block_id,
};

let output_dir = command
.pkg
.output_directory
.as_ref()
.map(PathBuf::from)
.unwrap_or_else(|| default_output_directory(manifest.dir()))
.join("deployments");
deployment_artifact.to_file(&output_dir, pkg_name, contract_id)?;

Ok(contract_id)
}
e => {
Expand Down