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

Merge master into the "Replace swayfmt PR" with fixes for excess newlines #2695

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
356 changes: 328 additions & 28 deletions Cargo.lock

Large diffs are not rendered by default.

6 changes: 5 additions & 1 deletion forc-plugins/forc-client/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,16 @@ anyhow = "1"
clap = { version = "3", features = ["derive", "env"] }
forc-pkg = { version = "0.22.1", path = "../../forc-pkg" }
forc-util = { version = "0.22.1", path = "../../forc-util" }
fuel-crypto = "0.6"
fuel-gql-client = { version = "0.10", default-features = false }
fuel-tx = "0.18"
fuel-vm = "0.15"
fuels-core = "0.21"
fuels-signers = "0.21"
fuels-types = "0.21"
futures = "0.3"
hex = "0.4.3"
serde = { version = "1.0" }
serde = "1.0"
serde_json = "1.0.73"
sway-core = { version = "0.22.1", path = "../../sway-core" }
sway-types = { version = "0.22.1", path = "../../sway-types" }
Expand Down
9 changes: 9 additions & 0 deletions forc-plugins/forc-client/src/ops/deploy/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,13 @@ pub struct DeployCommand {
/// Output the time elapsed over each part of the compilation process.
#[clap(long)]
pub time_phases: bool,
/// Do not sign the transaction
#[clap(long)]
pub unsigned: bool,
/// Set the transaction gas limit. Defaults to the maximum gas limit.
#[clap(long)]
pub gas_limit: Option<u64>,
/// Set the transaction gas price. Defaults to 0.
#[clap(long)]
pub gas_price: Option<u64>,
}
112 changes: 103 additions & 9 deletions forc-plugins/forc-client/src/ops/deploy/op.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,18 @@
use anyhow::{bail, Result};
use forc_pkg::{BuildOptions, ManifestFile};
use forc_pkg::{BuildOptions, Compiled, ManifestFile};
use fuel_crypto::Signature;
use fuel_gql_client::client::FuelClient;
use fuel_tx::{Output, Salt, StorageSlot, Transaction};
use fuel_vm::prelude::*;
use std::path::PathBuf;
use fuels_core::constants::{BASE_ASSET_ID, DEFAULT_SPENDABLE_COIN_AMOUNT};
use fuels_signers::{provider::Provider, wallet::Wallet};
use fuels_types::bech32::Bech32Address;
use std::{io::Write, path::PathBuf, str::FromStr};
use sway_core::TreeType;
use sway_utils::constants::DEFAULT_NODE_URL;
use tracing::info;

use super::cmd::DeployCommand;
use crate::ops::{deploy::cmd::DeployCommand, parameters::TxParameters};

pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
let curr_dir = if let Some(ref path) = command.path {
Expand Down Expand Up @@ -37,6 +41,9 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
build_profile,
release,
time_phases,
unsigned,
gas_limit,
gas_price,
} = command;

let build_options = BuildOptions {
Expand All @@ -59,12 +66,6 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
};

let compiled = forc_pkg::build_with_options(build_options)?;
let (tx, contract_id) = create_contract_tx(
compiled.bytecode,
Vec::<fuel_tx::Input>::new(),
Vec::<fuel_tx::Output>::new(),
compiled.storage_slots,
);

let node_url = match &manifest.network {
Some(network) => &network.url,
Expand All @@ -75,6 +76,47 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {

let client = FuelClient::new(node_url)?;

let (mut tx, contract_id) = if unsigned {
create_contract_tx(
compiled.bytecode,
Vec::<fuel_tx::Input>::new(),
Vec::<fuel_tx::Output>::new(),
compiled.storage_slots,
)
} else {
let mut wallet_address = String::new();
print!(
"Please provide the address of the wallet you are going to sign this transaction with:"
);
std::io::stdout().flush()?;
std::io::stdin().read_line(&mut wallet_address)?;
let address = Bech32Address::from_str(wallet_address.trim())?;
let locked_wallet = Wallet::from_address(address, Some(Provider::new(client.clone())));
let tx_parameters = TxParameters::new(gas_limit, gas_price);
create_signed_contract_tx(compiled, locked_wallet, tx_parameters).await?
};

if !unsigned {
// Ask for the signature and add it as a witness
let mut signature = String::new();
print!("Please provide the signature for this transaction:");
std::io::stdout().flush()?;
std::io::stdin().read_line(&mut signature)?;

let signature = Signature::from_str(signature.trim())?;
let witness = vec![Witness::from(signature.as_ref())];

let mut witnesses: Vec<Witness> = tx.witnesses().to_vec();

match witnesses.len() {
0 => tx.set_witnesses(witness),
_ => {
witnesses.extend(witness);
tx.set_witnesses(witnesses)
}
}
}

match client.submit(&tx).await {
Ok(logs) => {
info!("Logs:\n{:?}", logs);
Expand All @@ -84,6 +126,58 @@ pub async fn deploy(command: DeployCommand) -> Result<fuel_tx::ContractId> {
}
}

async fn create_signed_contract_tx(
compiled_contract: Compiled,
signer_wallet: Wallet,
tx_parameters: TxParameters,
) -> Result<(Transaction, fuel_tx::ContractId)> {
let maturity = 0;
let bytecode_witness_index = 0;
let witnesses = vec![compiled_contract.bytecode.clone().into()];

let salt = Salt::new([0; 32]);

let contract = Contract::from(compiled_contract.bytecode);
let root = contract.root();

let mut storage_slots = compiled_contract.storage_slots;
storage_slots.sort();
let state_root = Contract::initial_state_root(storage_slots.iter());
let contract_id = contract.id(&salt, &root, &state_root);
info!("Contract id: 0x{}", hex::encode(contract_id));

let outputs: Vec<Output> = vec![
Output::contract_created(contract_id, state_root),
// Note that the change will be computed by the node.
// Here we only have to tell the node who will own the change and its asset ID.
// For now we use the BASE_ASSET_ID constant
Output::change(signer_wallet.address().into(), 0, BASE_ASSET_ID),
];
let coin_witness_index = 1;

let inputs = signer_wallet
.get_asset_inputs_for_amount(
AssetId::default(),
DEFAULT_SPENDABLE_COIN_AMOUNT,
coin_witness_index,
)
.await?;
let tx = Transaction::create(
tx_parameters.gas_price,
tx_parameters.gas_limit,
maturity,
bytecode_witness_index,
salt,
storage_slots,
inputs,
outputs,
witnesses,
);

println!("Tx id to sign {}", tx.id());
Ok((tx, contract_id))
}

fn create_contract_tx(
compiled_contract: Vec<u8>,
inputs: Vec<Input>,
Expand Down
1 change: 1 addition & 0 deletions forc-plugins/forc-client/src/ops/mod.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
pub mod deploy;
pub mod parameters;
pub mod run;
4 changes: 0 additions & 4 deletions forc-plugins/forc-client/src/ops/run/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,6 @@ pub struct RunCommand {
#[clap(long)]
pub minify_json_storage_slots: bool,

/// Set the transaction byte price. Defaults to 0.
#[clap(long)]
pub byte_price: Option<u64>,

/// Set the transaction gas limit. Defaults to the maximum gas limit.
#[clap(long)]
pub gas_limit: Option<u64>,
Expand Down
1 change: 0 additions & 1 deletion forc-plugins/forc-client/src/ops/run/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,2 @@
pub mod cmd;
pub mod op;
mod parameters;
2 changes: 1 addition & 1 deletion forc-plugins/forc-client/src/ops/run/op.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use std::{path::PathBuf, str::FromStr};
use sway_core::TreeType;
use tracing::info;

use super::{cmd::RunCommand, parameters::TxParameters};
use crate::ops::{parameters::TxParameters, run::cmd::RunCommand};

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

Expand Down
8 changes: 8 additions & 0 deletions sway-core/src/declaration_engine/declaration_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use std::fmt;

use sway_types::{Span, Spanned};

use crate::type_system::{CopyTypes, TypeMapping};

use super::declaration_engine::de_look_up_decl_id;

/// An ID used to refer to an item in the [DeclarationEngine](super::declaration_engine::DeclarationEngine)
Expand Down Expand Up @@ -49,6 +51,12 @@ impl Spanned for DeclarationId {
}
}

impl CopyTypes for DeclarationId {
fn copy_types(&mut self, type_mapping: &TypeMapping) {
de_look_up_decl_id(self.clone()).copy_types(type_mapping)
}
}

impl DeclarationId {
pub(super) fn new(index: usize, span: Span) -> DeclarationId {
DeclarationId(index, span)
Expand Down
15 changes: 15 additions & 0 deletions sway-core/src/declaration_engine/declaration_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ use crate::{
TypedImplTrait, TypedStorageDeclaration, TypedStructDeclaration, TypedTraitDeclaration,
TypedTraitFn,
},
type_system::{CopyTypes, TypeMapping},
CompileError, TypedFunctionDeclaration,
};

Expand Down Expand Up @@ -54,6 +55,20 @@ impl fmt::Display for DeclarationWrapper {
}
}

impl CopyTypes for DeclarationWrapper {
fn copy_types(&mut self, type_mapping: &TypeMapping) {
match self {
DeclarationWrapper::Unknown => {}
DeclarationWrapper::Function(decl) => decl.copy_types(type_mapping),
DeclarationWrapper::Trait(decl) => decl.copy_types(type_mapping),
DeclarationWrapper::TraitFn(decl) => decl.copy_types(type_mapping),
DeclarationWrapper::TraitImpl(decl) => decl.copy_types(type_mapping),
DeclarationWrapper::Struct(decl) => decl.copy_types(type_mapping),
DeclarationWrapper::Storage(_) => {}
}
}
}

impl DeclarationWrapper {
/// friendly name string used for error reporting.
fn friendly_name(&self) -> &'static str {
Expand Down
Loading