1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
mod encode;
use crate::{
    cmd,
    constants::TX_SUBMIT_TIMEOUT_MS,
    util::{
        gas::get_script_gas_used,
        node_url::get_node_url,
        pkg::built_pkgs,
        tx::{prompt_forc_wallet_password, TransactionBuilderExt, WalletSelectionMode},
    },
};
use anyhow::{anyhow, bail, Context, Result};
use forc_pkg::{self as pkg, fuel_core_not_running, PackageManifestFile};
use forc_tracing::println_warning;
use forc_util::tx_utils::format_log_receipts;
use fuel_core_client::client::FuelClient;
use fuel_tx::{ContractId, Transaction, TransactionBuilder};
use fuels_accounts::provider::Provider;
use pkg::{manifest::build_profile::ExperimentalFlags, BuiltPackage};
use std::time::Duration;
use std::{path::PathBuf, str::FromStr};
use sway_core::language::parsed::TreeType;
use sway_core::BuildTarget;
use tokio::time::timeout;
use tracing::info;

use self::encode::ScriptCallHandler;

pub struct RanScript {
    pub receipts: Vec<fuel_tx::Receipt>,
}

/// Builds and runs script(s). If given path corresponds to a workspace, all runnable members will
/// be built and deployed.
///
/// Upon success, returns the receipts of each script in the order they are executed.
///
/// When running a single script, only that script's receipts are returned.
pub async fn run(command: cmd::Run) -> Result<Vec<RanScript>> {
    let mut command = command;
    if command.unsigned {
        println_warning("--unsigned flag is deprecated, please prefer using --default-signer. Assuming `--default-signer` is passed. This means your transaction will be signed by an account that is funded by fuel-core by default for testing purposes.");
        command.default_signer = true;
    }
    let mut receipts = Vec::new();
    let curr_dir = if let Some(path) = &command.pkg.path {
        PathBuf::from(path)
    } else {
        std::env::current_dir().map_err(|e| anyhow!("{:?}", e))?
    };
    let build_opts = build_opts_from_cmd(&command);
    let built_pkgs_with_manifest = built_pkgs(&curr_dir, &build_opts)?;
    let wallet_mode = if command.default_signer || command.signing_key.is_some() {
        WalletSelectionMode::Manual
    } else {
        let password = prompt_forc_wallet_password()?;
        WalletSelectionMode::ForcWallet(password)
    };
    for built in built_pkgs_with_manifest {
        if built
            .descriptor
            .manifest_file
            .check_program_type(&[TreeType::Script])
            .is_ok()
        {
            let pkg_receipts = run_pkg(
                &command,
                &built.descriptor.manifest_file,
                &built,
                &wallet_mode,
            )
            .await?;
            receipts.push(pkg_receipts);
        }
    }

    Ok(receipts)
}

pub async fn run_pkg(
    command: &cmd::Run,
    manifest: &PackageManifestFile,
    compiled: &BuiltPackage,
    wallet_mode: &WalletSelectionMode,
) -> Result<RanScript> {
    let node_url = get_node_url(&command.node, &manifest.network)?;

    let script_data = match (&command.data, &command.args) {
        (None, Some(args)) => {
            let minify_json_abi = true;
            let package_json_abi = compiled
                .json_abi_string(minify_json_abi)?
                .ok_or_else(|| anyhow::anyhow!("Missing json abi string"))?;
            let main_arg_handler = ScriptCallHandler::from_json_abi_str(&package_json_abi)?;
            let args = args.iter().map(|arg| arg.as_str()).collect::<Vec<_>>();
            main_arg_handler.encode_arguments(args.as_slice())?
        }
        (Some(_), Some(_)) => {
            bail!("Both --args and --data provided, must choose one.")
        }
        _ => {
            let input_data = command.data.as_deref().unwrap_or("");
            let data = input_data.strip_prefix("0x").unwrap_or(input_data);
            hex::decode(data).expect("Invalid hex")
        }
    };

    let contract_ids = command
        .contract
        .as_ref()
        .into_iter()
        .flat_map(|contracts| contracts.iter())
        .map(|contract| {
            ContractId::from_str(contract)
                .map_err(|e| anyhow!("Failed to parse contract id: {}", e))
        })
        .collect::<Result<Vec<ContractId>>>()?;

    let mut tb = TransactionBuilder::script(compiled.bytecode.bytes.clone(), script_data);
    tb.maturity(command.maturity.maturity.into())
        .add_contracts(contract_ids);

    let provider = Provider::connect(node_url.clone()).await?;

    let script_gas_limit = if compiled.bytecode.bytes.is_empty() {
        0
    } else if let Some(script_gas_limit) = command.gas.script_gas_limit {
        script_gas_limit
    // Dry run tx and get `gas_used`
    } else {
        get_script_gas_used(tb.clone().finalize_without_signature_inner(), &provider).await?
    };
    tb.script_gas_limit(script_gas_limit);

    let tx = tb
        .finalize_signed(
            Provider::connect(node_url.clone()).await?,
            command.default_signer,
            command.signing_key,
            wallet_mode,
        )
        .await?;

    if command.dry_run {
        info!("{:?}", tx);
        Ok(RanScript { receipts: vec![] })
    } else {
        let receipts = try_send_tx(
            node_url.as_str(),
            &tx.into(),
            command.pretty_print,
            command.simulate,
        )
        .await?;
        Ok(RanScript { receipts })
    }
}

async fn try_send_tx(
    node_url: &str,
    tx: &Transaction,
    pretty_print: bool,
    simulate: bool,
) -> Result<Vec<fuel_tx::Receipt>> {
    let client = FuelClient::new(node_url)?;

    match client.health().await {
        Ok(_) => timeout(
            Duration::from_millis(TX_SUBMIT_TIMEOUT_MS),
            send_tx(&client, tx, pretty_print, simulate),
        )
        .await
        .with_context(|| format!("timeout waiting for {:?} to be included in a block", tx))?,
        Err(_) => Err(fuel_core_not_running(node_url)),
    }
}

async fn send_tx(
    client: &FuelClient,
    tx: &Transaction,
    pretty_print: bool,
    simulate: bool,
) -> Result<Vec<fuel_tx::Receipt>> {
    let outputs = {
        if !simulate {
            let status = client.submit_and_await_commit(tx).await?;

            match status {
                fuel_core_client::client::types::TransactionStatus::Success {
                    receipts, ..
                } => receipts,
                fuel_core_client::client::types::TransactionStatus::Failure {
                    receipts, ..
                } => receipts,
                _ => vec![],
            }
        } else {
            let txs = vec![tx.clone()];
            let receipts = client.dry_run(txs.as_slice()).await?;
            let receipts = receipts
                .first()
                .map(|tx| &tx.result)
                .map(|res| res.receipts());
            match receipts {
                Some(receipts) => receipts.to_vec(),
                None => vec![],
            }
        }
    };
    if !outputs.is_empty() {
        info!("{}", format_log_receipts(&outputs, pretty_print)?);
    }
    Ok(outputs)
}

fn build_opts_from_cmd(cmd: &cmd::Run) -> pkg::BuildOpts {
    pkg::BuildOpts {
        pkg: pkg::PkgOpts {
            path: cmd.pkg.path.clone(),
            offline: cmd.pkg.offline,
            terse: cmd.pkg.terse,
            locked: cmd.pkg.locked,
            output_directory: cmd.pkg.output_directory.clone(),
            ipfs_node: cmd.pkg.ipfs_node.clone().unwrap_or_default(),
        },
        print: pkg::PrintOpts {
            ast: cmd.print.ast,
            dca_graph: cmd.print.dca_graph.clone(),
            dca_graph_url_format: cmd.print.dca_graph_url_format.clone(),
            asm: cmd.print.asm(),
            bytecode: cmd.print.bytecode,
            bytecode_spans: false,
            ir: cmd.print.ir(),
            reverse_order: cmd.print.reverse_order,
        },
        minify: pkg::MinifyOpts {
            json_abi: cmd.minify.json_abi,
            json_storage_slots: cmd.minify.json_storage_slots,
        },
        build_target: BuildTarget::default(),
        build_profile: cmd.build_profile.build_profile.clone(),
        release: cmd.build_profile.release,
        error_on_warnings: cmd.build_profile.error_on_warnings,
        time_phases: cmd.print.time_phases,
        metrics_outfile: cmd.print.metrics_outfile.clone(),
        binary_outfile: cmd.build_output.bin_file.clone(),
        debug_outfile: cmd.build_output.debug_file.clone(),
        tests: false,
        member_filter: pkg::MemberFilter::only_scripts(),
        experimental: ExperimentalFlags {
            new_encoding: !cmd.no_encoding_v1,
        },
    }
}