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
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
mod call_function;
pub mod list_functions;
mod missing_contracts;
mod parser;
pub mod trace;
mod transfer;

use crate::cmd::call::AbiSource;
use crate::{
    cmd,
    constants::DEFAULT_PRIVATE_KEY,
    op::call::{
        call_function::call_function, list_functions::list_contract_functions, transfer::transfer,
    },
    util::tx::{prompt_forc_wallet_password, select_local_wallet_account},
};
use anyhow::{anyhow, Result};
use fuel_abi_types::abi::{
    program::ProgramABI,
    unified_program::{UnifiedProgramABI, UnifiedTypeDeclaration},
};
use fuel_core_types::services::executor::TransactionExecutionStatus;
use fuel_tx::Receipt;
use fuels::{
    accounts::{
        provider::Provider, signers::private_key::PrivateKeySigner, wallet::Wallet, ViewOnlyAccount,
    },
    crypto::SecretKey,
};
use fuels_core::types::{transaction::TxPolicies, AssetId, ContractId};
use serde::{Deserialize, Serialize};
use std::{collections::HashMap, str::FromStr};

/// Response returned from a contract call operation
#[derive(Debug, Default, Clone, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub struct CallResponse {
    pub tx_hash: String,
    pub total_gas: u64,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub result: Option<String>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub receipts: Vec<Receipt>,
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub trace_events: Vec<trace::TraceEvent>,
    #[serde(rename = "Script", skip_serializing_if = "Option::is_none")]
    pub script: Option<fuel_tx::Script>,
}

/// A command for calling a contract function.
pub async fn call(operation: cmd::call::Operation, cmd: cmd::Call) -> anyhow::Result<CallResponse> {
    let is_json_mode = matches!(cmd.output, cmd::call::OutputFormat::Json);
    let response = match operation {
        cmd::call::Operation::ListFunctions { contract_id, abi } => {
            if let cmd::call::OutputFormat::Json = cmd.output {
                return Err(anyhow!("JSON output is not supported for list functions"));
            }

            let abi_map = create_abi_map(contract_id, &abi, cmd.contract_abis).await?;

            // Use the simplified list_contract_functions function
            list_contract_functions(&contract_id, &abi_map, &mut std::io::stdout())?;

            CallResponse::default()
        }
        cmd::call::Operation::DirectTransfer {
            recipient,
            amount,
            asset_id,
        } => {
            let cmd::Call {
                node,
                caller,
                gas,
                mut output,
                ..
            } = cmd;

            // Already validated that mode is ExecutionMode::Live
            let (wallet, tx_policies, base_asset_id) =
                setup_connection(&node, caller, &gas).await?;
            let asset_id = asset_id.unwrap_or(base_asset_id);

            transfer(
                &wallet,
                recipient,
                amount,
                asset_id,
                tx_policies,
                &node,
                &mut output,
            )
            .await?
        }
        cmd::call::Operation::CallFunction {
            contract_id,
            abi,
            function,
            function_args,
        } => {
            // Call the function with required parameters
            call_function(contract_id, abi, function, function_args, cmd).await?
        }
    };

    // If using JSON output mode, explicitly print the response for potential parsing/piping
    if is_json_mode {
        println!("{}", serde_json::to_string_pretty(&response).unwrap());
    }

    Ok(response)
}

/// Sets up the connection to the node and initializes common parameters
async fn setup_connection(
    node: &crate::NodeTarget,
    caller: cmd::call::Caller,
    gas: &Option<forc_tx::Gas>,
) -> anyhow::Result<(Wallet, TxPolicies, AssetId)> {
    let node_url = node.get_node_url(&None)?;
    let provider = Provider::connect(node_url).await?;
    let wallet = get_wallet(caller.signing_key, caller.wallet, provider).await?;
    let provider = wallet.provider();
    let tx_policies = gas.as_ref().map(Into::into).unwrap_or_default();
    let consensus_parameters = provider.consensus_parameters().await?;
    let base_asset_id = consensus_parameters.base_asset_id();

    Ok((wallet, tx_policies, *base_asset_id))
}

/// Helper function to load ABI from file, URL, or raw string
async fn load_abi(abi: &AbiSource) -> anyhow::Result<String> {
    match abi {
        AbiSource::File(path) => std::fs::read_to_string(path)
            .map_err(|e| anyhow!("Failed to read ABI file at {:?}: {}", path, e)),
        AbiSource::Url(url) => {
            let response = reqwest::get(url.clone())
                .await
                .map_err(|e| anyhow!("Failed to fetch ABI from URL {}: {}", url, e))?;
            let bytes = response
                .bytes()
                .await
                .map_err(|e| anyhow!("Failed to read response body from URL {}: {}", url, e))?;
            String::from_utf8(bytes.to_vec())
                .map_err(|e| anyhow!("Failed to parse response as UTF-8 from URL {}: {}", url, e))
        }
        AbiSource::String(json_str) => {
            // Validate that it's valid JSON
            serde_json::from_str::<serde_json::Value>(json_str)
                .map_err(|e| anyhow!("Invalid JSON in ABI string: {}", e))?;
            Ok(json_str.to_owned())
        }
    }
}

/// Get the wallet to use for the call - based on optionally provided signing key and wallet flag.
async fn get_wallet(
    signing_key: Option<SecretKey>,
    use_wallet: bool,
    provider: Provider,
) -> Result<Wallet> {
    match (signing_key, use_wallet) {
        (None, false) => {
            let secret_key = SecretKey::from_str(DEFAULT_PRIVATE_KEY).unwrap();
            let signer = PrivateKeySigner::new(secret_key);
            let wallet = Wallet::new(signer, provider);
            forc_tracing::println_warning(&format!(
                "No signing key or wallet flag provided. Using default signer: 0x{}",
                wallet.address()
            ));
            Ok(wallet)
        }
        (Some(secret_key), false) => {
            let signer = PrivateKeySigner::new(secret_key);
            let wallet = Wallet::new(signer, provider);
            forc_tracing::println_warning(&format!(
                "Using account {} derived from signing key...",
                wallet.address()
            ));
            Ok(wallet)
        }
        (None, true) => {
            let password = prompt_forc_wallet_password()?;
            let wallet = select_local_wallet_account(&password, &provider).await?;
            Ok(wallet)
        }
        (Some(secret_key), true) => {
            forc_tracing::println_warning(
                "Signing key is provided while requesting to use forc-wallet. Using signing key...",
            );
            let signer = PrivateKeySigner::new(secret_key);
            let wallet = Wallet::new(signer, provider);
            Ok(wallet)
        }
    }
}

#[derive(Debug, Clone)]
pub struct Abi {
    source: AbiSource,
    program: ProgramABI,
    unified: UnifiedProgramABI,
    // TODO: required for vm interpreter step through
    // ↳ gh issue: https://github.com/FuelLabs/sway/issues/7197
    #[allow(dead_code)]
    type_lookup: HashMap<usize, UnifiedTypeDeclaration>,
}

impl Abi {
    /// Set the source of the ABI after creation
    pub fn with_source(mut self, source: AbiSource) -> Self {
        self.source = source;
        self
    }
}

impl FromStr for Abi {
    type Err = String;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let program: ProgramABI =
            serde_json::from_str(s).map_err(|err| format!("failed to parse ABI: {err}"))?;

        let unified = UnifiedProgramABI::from_counterpart(&program)
            .map_err(|err| format!("conversion to unified ABI format failed: {err}"))?;

        let type_lookup = unified
            .types
            .iter()
            .map(|decl| (decl.type_id, decl.clone()))
            .collect::<HashMap<_, _>>();

        Ok(Self {
            source: AbiSource::String(s.to_string()),
            program,
            unified,
            type_lookup,
        })
    }
}

/// Displays transaction information
pub(crate) fn display_tx_info(
    tx_hash: String,
    result: Option<String>,
    mode: &cmd::call::ExecutionMode,
    node: &crate::NodeTarget,
) {
    // print tx hash and result
    forc_tracing::println_label_green("tx hash:", &tx_hash);
    if let Some(ref result) = result {
        forc_tracing::println_label_green("result:", result);
    }

    // display transaction url if live mode
    if *mode == cmd::call::ExecutionMode::Live {
        if let Some(explorer_url) = node.get_explorer_url() {
            forc_tracing::println_label_green(
                "\nView transaction:",
                &format!("{explorer_url}/tx/0x{tx_hash}\n"),
            );
        }
    }
}

/// Prints receipts and trace to the writer based on verbosity level
pub(crate) fn display_detailed_call_info(
    tx: &TransactionExecutionStatus,
    script: &fuel_tx::Script,
    abis: &HashMap<ContractId, Abi>,
    verbosity: u8,
    writer: &mut impl std::io::Write,
    trace_events: &[trace::TraceEvent],
    labels: &HashMap<ContractId, String>,
) -> Result<()> {
    if verbosity >= 4 {
        forc_tracing::println_label_green(
            "transaction script:\n",
            &serde_json::to_string_pretty(script).unwrap(),
        );
    }
    if verbosity >= 3 {
        let formatted_receipts =
            forc_util::tx_utils::format_log_receipts(tx.result.receipts(), true)
                .map_err(|e| anyhow!("Failed to format receipts: {}", e))?;
        forc_tracing::println_label_green("receipts:", &formatted_receipts);
    }
    if verbosity >= 2 {
        trace::display_transaction_trace(*tx.result.total_gas(), trace_events, labels, writer)
            .map_err(|e| anyhow!("Failed to display transaction trace: {e}"))?;
    }
    if verbosity >= 1 {
        let logs = tx
            .result
            .receipts()
            .iter()
            .filter_map(|receipt| match receipt {
                Receipt::LogData {
                    id,
                    rb,
                    data: Some(data),
                    ..
                } => {
                    let default_program_abi = ProgramABI::default();
                    let program_abi = abis
                        .get(id)
                        .map(|abi| &abi.program)
                        .unwrap_or(&default_program_abi);
                    forc_util::tx_utils::decode_fuel_vm_log_data(&rb.to_string(), data, program_abi)
                        .ok()
                        .map(|decoded| decoded.value)
                }
                _ => None,
            })
            .collect::<Vec<_>>();

        // print logs if there are any
        if !logs.is_empty() {
            forc_tracing::println_green_bold("logs:");
            for log in logs.iter() {
                writeln!(writer, "  {log:#}")?;
            }
        }
    }
    Ok(())
}

/// Create a HashMap of contract ABIs from a main ABI and optional additional contract ABIs
/// This is a reusable function for both call_function and list_functions operations
pub async fn create_abi_map(
    main_contract_id: ContractId,
    main_abi: &AbiSource,
    additional_contract_abis: Option<Vec<(ContractId, AbiSource)>>,
) -> anyhow::Result<HashMap<ContractId, Abi>> {
    // Load main ABI
    let main_abi_str = load_abi(main_abi).await?;
    let main_abi = Abi::from_str(&main_abi_str)
        .map_err(|e| anyhow!("Failed to parse main ABI: {}", e))?
        .with_source(main_abi.clone());

    // Start with main contract ABI
    let mut abi_map = HashMap::from([(main_contract_id, main_abi)]);

    // Load additional contract ABIs if provided
    if let Some(contract_abis) = additional_contract_abis {
        for (contract_id, abi_path) in contract_abis {
            match load_abi(&abi_path).await {
                Ok(abi_str) => match Abi::from_str(&abi_str) {
                    Ok(additional_abi) => {
                        abi_map.insert(contract_id, additional_abi.with_source(abi_path.clone()));
                        forc_tracing::println_action_green(
                            "Loaded additional ABI for contract",
                            &format!("0x{contract_id}"),
                        );
                    }
                    Err(e) => {
                        forc_tracing::println_warning(&format!(
                            "Failed to parse ABI for contract 0x{contract_id}: {e}"
                        ));
                    }
                },
                Err(e) => {
                    forc_tracing::println_warning(&format!(
                        "Failed to load ABI for contract 0x{contract_id}: {e}"
                    ));
                }
            }
        }
    }

    Ok(abi_map)
}

#[cfg(test)]
pub(crate) mod tests {
    use super::*;
    use fuels::prelude::*;

    abigen!(Contract(
        name = "TestContract",
        abi = "forc-client/test/data/contract_with_types/contract_with_types-abi.json"
    ));

    pub async fn get_contract_instance() -> (TestContract<Wallet>, ContractId, Provider, SecretKey)
    {
        let secret_key = SecretKey::from_str(DEFAULT_PRIVATE_KEY).unwrap();
        let signer = PrivateKeySigner::new(secret_key);
        let coins = setup_single_asset_coins(signer.address(), AssetId::zeroed(), 1, 1_000_000);
        let provider = setup_test_provider(coins, vec![], None, None)
            .await
            .unwrap();
        let wallet = get_wallet(Some(secret_key), false, provider.clone())
            .await
            .unwrap();

        let id = Contract::load_from(
            "test/data/contract_with_types/contract_with_types.bin",
            LoadConfiguration::default(),
        )
        .unwrap()
        .deploy(&wallet, TxPolicies::default())
        .await
        .unwrap()
        .contract_id;

        let instance = TestContract::new(id, wallet.clone());

        (instance, id, provider, secret_key)
    }
}