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
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
use crate::{
    cmd::{self, call::FuncType},
    op::call::{
        missing_contracts::determine_missing_contracts,
        parser::{param_type_val_to_token, token_to_string},
        CallResponse,
    },
};
use anyhow::{anyhow, bail, Result};
use fuel_abi_types::abi::unified_program::UnifiedProgramABI;
use fuel_core_client::client::types::TransactionStatus;
use fuel_core_types::services::executor::{TransactionExecutionResult, TransactionExecutionStatus};
use fuels::{
    accounts::ViewOnlyAccount,
    client::FuelClient,
    programs::calls::{
        receipt_parser::ReceiptParser,
        traits::{ContractDependencyConfigurator, TransactionTuner},
        ContractCall,
    },
    types::transaction::Transaction,
};
use fuels_core::{
    codec::{
        encode_fn_selector, log_formatters_lookup, ABIDecoder, ABIEncoder, DecoderConfig,
        EncoderConfig, ErrorDetails, LogDecoder,
    },
    types::{
        param_types::ParamType,
        transaction_builders::{BuildableTransaction, ScriptBuildStrategy, VariableOutputPolicy},
        ContractId,
    },
};
use std::{collections::HashMap, str::FromStr, sync::Arc};

/// Calls a contract function with the given parameters
pub async fn call_function(
    contract_id: ContractId,
    abi: crate::cmd::call::AbiSource,
    function: FuncType,
    function_args: Vec<String>,
    cmd: cmd::Call,
) -> Result<CallResponse> {
    let cmd::Call {
        node,
        mode,
        caller,
        call_parameters,
        gas,
        mut output,
        external_contracts,
        contract_abis,
        variable_output,
        ..
    } = cmd;

    // Use the reusable function to create ABI map
    let abi_map = super::create_abi_map(contract_id, &abi, contract_abis).await?;

    // Get the main ABI for compatibility with existing code
    let abi = abi_map
        .get(&contract_id)
        .ok_or_else(|| anyhow!("Main contract ABI not found in abi_map"))?;

    let cmd::call::FuncType::Selector(selector) = function;

    let (encoded_data, output_param) =
        prepare_contract_call_data(&abi.unified, &selector, &function_args)?;

    // Setup connection to node
    let (wallet, tx_policies, base_asset_id) = super::setup_connection(&node, caller, &gas).await?;
    let call_parameters = cmd::call::CallParametersOpts {
        asset_id: call_parameters.asset_id.or(Some(base_asset_id)),
        ..call_parameters
    };

    // Create the contract call
    let call = ContractCall {
        contract_id,
        encoded_selector: encode_fn_selector(&selector),
        encoded_args: Ok(encoded_data),
        call_parameters: call_parameters.clone().into(),
        external_contracts: vec![], // set below
        output_param: output_param.clone(),
        is_payable: call_parameters.amount > 0,
        custom_assets: Default::default(),
        inputs: Vec::new(),
        outputs: Vec::new(),
    };

    // Setup variable output policy and log decoder
    let variable_output_policy = variable_output
        .map(VariableOutputPolicy::Exactly)
        .unwrap_or(VariableOutputPolicy::EstimateMinimum);
    let error_codes = abi
        .unified
        .error_codes
        .as_ref()
        .map_or(HashMap::new(), |error_codes| {
            error_codes
                .iter()
                .map(|(revert_code, error_details)| {
                    (
                        *revert_code,
                        ErrorDetails::new(
                            error_details.pos.pkg.clone(),
                            error_details.pos.file.clone(),
                            error_details.pos.line,
                            error_details.pos.column,
                            error_details.log_id.clone(),
                            error_details.msg.clone(),
                        ),
                    )
                })
                .collect()
        });
    let log_decoder = LogDecoder::new(log_formatters_lookup(vec![], contract_id), error_codes);

    // Execute the call based on execution mode
    let client = FuelClient::new(wallet.provider().url())
        .map_err(|e| anyhow!("Failed to create client: {e}"))?;
    let consensus_params = wallet.provider().consensus_parameters().await?;
    let chain_id = consensus_params.chain_id();

    // Get external contracts (either provided or auto-detected)
    let external_contracts = match external_contracts {
        Some(contracts) if contracts.first().is_some_and(|s| s.is_empty()) => vec![],
        Some(contracts) => {
            // Parse each contract ID
            contracts
                .into_iter()
                .filter(|s| !s.is_empty())
                .map(|s| {
                    ContractId::from_str(s.strip_prefix("0x").unwrap_or(&s))
                        .map_err(|e| anyhow!("Invalid contract ID '{}': {}", s, e))
                })
                .collect::<Result<Vec<_>>>()?
        }
        None => {
            // Automatically retrieve missing contract addresses from the call
            forc_tracing::println_warning(
                "Automatically retrieving missing contract addresses for the call",
            );
            let external_contracts = determine_missing_contracts(
                &call,
                wallet.provider(),
                &tx_policies,
                &variable_output_policy,
                &consensus_params,
                &log_decoder,
                &wallet,
            )
            .await?;
            if !external_contracts.is_empty() {
                forc_tracing::println_warning(
                    "Automatically provided external contract addresses with call (max 10):",
                );
                external_contracts.iter().for_each(|addr| {
                    forc_tracing::println_warning(&format!("- 0x{addr}"));
                });
            }
            external_contracts
        }
    };

    let tb = call
        .clone()
        .with_external_contracts(external_contracts)
        .transaction_builder(
            tx_policies,
            variable_output_policy,
            &consensus_params,
            call.inputs.clone(),
            &wallet,
        )
        .map_err(|e| anyhow!("Failed to initialize transaction builder: {e}"))?;

    #[cfg_attr(test, allow(unused_variables))]
    let (tx, tx_execution, storage_reads) = match mode {
        cmd::call::ExecutionMode::DryRun => {
            let tx = call
                .build_tx(tb, &wallet)
                .await
                .map_err(|e| anyhow!("Failed to build transaction: {e}"))?;
            let (tx_execs, storage_reads) = client
                .dry_run_opt_record_storage_reads(&[tx.clone().into()], None, None, None)
                .await
                .map_err(|e| anyhow!("Failed to dry run transaction: {e}"))?;
            let tx_exec = tx_execs
                .first()
                .ok_or(anyhow!(
                    "Failed to extract transaction from dry run execution"
                ))?
                .to_owned();
            (tx, tx_exec, storage_reads)
        }
        cmd::call::ExecutionMode::Simulate => {
            let tb = tb.with_build_strategy(ScriptBuildStrategy::StateReadOnly);
            let tx = call
                .build_tx(tb, &wallet)
                .await
                .map_err(|e| anyhow!("Failed to build transaction: {e}"))?;
            let gas_price = gas.map(|g| g.price).unwrap_or(Some(0));
            let (tx_execs, storage_reads) = client
                .dry_run_opt_record_storage_reads(&[tx.clone().into()], None, gas_price, None)
                .await
                .map_err(|e| anyhow!("Failed to dry run transaction: {e}"))?;
            let tx_exec = tx_execs
                .first()
                .ok_or(anyhow!(
                    "Failed to extract transaction from dry run execution"
                ))?
                .to_owned();
            (tx, tx_exec, storage_reads)
        }
        cmd::call::ExecutionMode::Live => {
            forc_tracing::println_action_green(
                "Sending transaction with wallet",
                &format!("0x{}", wallet.address()),
            );
            let tx = call
                .build_tx(tb, &wallet)
                .await
                .map_err(|e| anyhow!("Failed to build transaction: {e}"))?;
            let tx_status = client.submit_and_await_commit(&tx.clone().into()).await?;

            #[cfg_attr(test, allow(unused_variables))]
            let (block_height, tx_exec) = match tx_status {
                TransactionStatus::Success {
                    block_height,
                    program_state,
                    receipts,
                    total_gas,
                    total_fee,
                    ..
                } => (
                    block_height,
                    TransactionExecutionStatus {
                        id: tx.id(chain_id),
                        result: TransactionExecutionResult::Success {
                            result: program_state,
                            receipts: Arc::new(receipts),
                            total_gas,
                            total_fee,
                        },
                    },
                ),
                TransactionStatus::Failure {
                    total_gas,
                    total_fee,
                    program_state,
                    receipts,
                    block_height,
                    ..
                } => (
                    block_height,
                    TransactionExecutionStatus {
                        id: tx.id(chain_id),
                        result: TransactionExecutionResult::Failed {
                            result: program_state,
                            receipts: Arc::new(receipts),
                            total_gas,
                            total_fee,
                        },
                    },
                ),
                _ => bail!("Transaction status not found"),
            };

            #[cfg(not(test))]
            let storage_reads = client
                .storage_read_replay(&block_height)
                .await
                .map_err(|e| anyhow!("Failed to get storage reads: {e}"))?;

            #[cfg(test)]
            let storage_reads = vec![];

            (tx, tx_exec, storage_reads)
        }
    };

    let tx: fuel_tx::Transaction = tx.into();
    let fuel_tx::Transaction::Script(script) = &tx else {
        bail!("Transaction is not a script");
    };
    let receipts = tx_execution.result.receipts();

    // Generate execution trace events by stepping through VM interpreter
    #[cfg(test)]
    let trace_events: Vec<crate::op::call::trace::TraceEvent> = vec![];
    #[cfg(not(test))]
    let trace_events = {
        use crate::op::call::trace::interpret_execution_trace;
        interpret_execution_trace(
            wallet.provider(),
            &mode,
            &consensus_params,
            script,
            receipts,
            storage_reads,
            &abi_map,
        )
        .await
        .map_err(|e| anyhow!("Failed to generate execution trace: {e}"))?
    };

    // display detailed call info if verbosity is set
    if cmd.verbosity > 0 {
        // Convert labels from Vec to HashMap
        let labels: HashMap<ContractId, String> = cmd
            .label
            .as_ref()
            .map(|labels| labels.iter().cloned().collect())
            .unwrap_or_default();

        super::display_detailed_call_info(
            &tx_execution,
            script,
            &abi_map,
            cmd.verbosity,
            &mut output,
            &trace_events,
            &labels,
        )?;
    }

    // If the call reverted, exit early; return an error with the revert details
    if let Some((contract_id, revert_info)) =
        crate::op::call::trace::first_revert_info(&trace_events)
    {
        return Err(anyhow!(
            "Contract 0x{contract_id} reverted with code 0x{:x}",
            revert_info.revert_code
        ));
    }

    // Parse the result based on output format
    let mut receipt_parser = ReceiptParser::new(receipts, DecoderConfig::default());
    let result = match output {
        cmd::call::OutputFormat::Default | cmd::call::OutputFormat::Json => {
            let data = receipt_parser
                .extract_contract_call_data(contract_id)
                .ok_or(anyhow!("Failed to extract contract call data"))?;
            ABIDecoder::default()
                .decode_as_debug_str(&output_param, data.as_slice())
                .map_err(|e| anyhow!("Failed to decode as debug string: {e}"))?
        }
        cmd::call::OutputFormat::Raw => {
            let token = receipt_parser
                .parse_call(contract_id, &output_param)
                .map_err(|e| anyhow!("Failed to parse call data: {e}"))?;
            token_to_string(&token)
                .map_err(|e| anyhow!("Failed to convert token to string: {e}"))?
        }
    };

    // display tx info
    super::display_tx_info(
        tx_execution.id.to_string(),
        Some(result.clone()),
        &mode,
        &node,
    );

    // Start interactive debugger if requested
    if cmd.debug {
        start_debug_session(&client, &tx, abi).await?;
    }

    Ok(CallResponse {
        tx_hash: tx_execution.id.to_string(),
        result: Some(result),
        total_gas: *tx_execution.result.total_gas(),
        receipts: tx_execution.result.receipts().to_vec(),
        script: Some(script.to_owned()),
        trace_events,
    })
}

fn prepare_contract_call_data(
    unified_program_abi: &UnifiedProgramABI,
    selector: &str,
    function_args: &[String],
) -> Result<(Vec<u8>, ParamType)> {
    let type_lookup = unified_program_abi
        .types
        .iter()
        .map(|decl| (decl.type_id, decl.clone()))
        .collect::<HashMap<_, _>>();

    // Find the function in the ABI
    let abi_func = unified_program_abi
        .functions
        .iter()
        .find(|f| f.name == selector)
        .cloned()
        .ok_or_else(|| anyhow!("Function '{selector}' not found in ABI"))?;

    // Validate number of arguments
    if abi_func.inputs.len() != function_args.len() {
        bail!(
            "Argument count mismatch for '{selector}': expected {}, got {}",
            abi_func.inputs.len(),
            function_args.len()
        );
    }

    // Parse function arguments to tokens
    let tokens = abi_func
        .inputs
        .iter()
        .zip(function_args)
        .map(|(type_application, arg)| {
            let param_type =
                ParamType::try_from_type_application(type_application, &type_lookup)
                    .map_err(|e| anyhow!("Failed to convert input type application: {e}"))?;
            param_type_val_to_token(&param_type, arg)
        })
        .collect::<Result<Vec<_>>>()?;

    // Get output parameter type
    let output_param = ParamType::try_from_type_application(&abi_func.output, &type_lookup)
        .map_err(|e| anyhow!("Failed to convert output type: {e}"))?;

    // Encode function arguments
    let abi_encoder = ABIEncoder::new(EncoderConfig::default());
    let encoded_data = abi_encoder
        .encode(&tokens)
        .map_err(|e| anyhow!("Failed to encode function arguments: {e}"))?;

    Ok((encoded_data, output_param))
}

/// Starts an interactive debugging session with the given transaction and ABI
async fn start_debug_session(
    fuel_client: &FuelClient,
    tx: &fuel_tx::Transaction,
    abi: &super::Abi,
) -> Result<()> {
    // Create debugger instance from the existing fuel client
    let mut debugger = forc_debug::debugger::Debugger::from_client(fuel_client.clone())
        .await
        .map_err(|e| anyhow!("Failed to create debugger: {e}"))?;

    // Create temporary files for transaction and ABI (auto-cleaned when dropped)
    let mut tx_file = tempfile::Builder::new()
        .suffix(".json")
        .tempfile()
        .map_err(|e| anyhow!("Failed to create temp transaction file: {e}"))?;
    serde_json::to_writer_pretty(&mut tx_file, tx)
        .map_err(|e| anyhow!("Failed to write transaction to temp file: {e}"))?;

    let mut abi_file = tempfile::Builder::new()
        .suffix(".json")
        .tempfile()
        .map_err(|e| anyhow!("Failed to create temp ABI file: {e}"))?;
    serde_json::to_writer_pretty(&mut abi_file, &abi.program)
        .map_err(|e| anyhow!("Failed to write ABI to temp file: {e}"))?;

    // Prepare the start_tx command string for the CLI
    let tx_cmd = format!(
        "start_tx {} {}",
        tx_file.path().to_string_lossy(),
        abi_file.path().to_string_lossy()
    );

    // Start the interactive CLI session with the prepared command
    let mut cli = forc_debug::cli::Cli::new()
        .map_err(|e| anyhow!("Failed to create debug CLI interface: {e}"))?;
    cli.run(&mut debugger, Some(tx_cmd))
        .await
        .map_err(|e| anyhow!("Interactive debugging session failed: {e}"))?;

    Ok(())
}

#[cfg(test)]
pub mod tests {
    use super::*;
    use crate::{
        cmd,
        op::call::{call, get_wallet, PrivateKeySigner},
    };
    use fuel_tx::field::Outputs;
    use fuels::{crypto::SecretKey, prelude::*};
    use std::path::PathBuf;

    fn get_contract_call_cmd(
        id: ContractId,
        node_url: &str,
        secret_key: SecretKey,
        selector: &str,
        args: Vec<&str>,
    ) -> cmd::Call {
        cmd::Call {
            address: (*id).into(),
            abi: Some(cmd::call::AbiSource::File(PathBuf::from(
                "test/data/contract_with_types/contract_with_types-abi.json",
            ))),
            function: Some(selector.to_string()),
            function_args: args.into_iter().map(String::from).collect(),
            node: crate::NodeTarget {
                node_url: Some(node_url.to_string()),
                ..Default::default()
            },
            caller: cmd::call::Caller {
                signing_key: Some(secret_key),
                wallet: false,
            },
            call_parameters: Default::default(),
            mode: cmd::call::ExecutionMode::DryRun,
            gas: None,
            external_contracts: None,
            contract_abis: None,
            label: None,
            output: cmd::call::OutputFormat::Raw,
            list_functions: false,
            variable_output: None,
            verbosity: 0,
            debug: false,
        }
    }

    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::random(&mut rand::thread_rng());
        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)
    }

    #[tokio::test]
    async fn contract_call_with_abi() {
        let (_, id, provider, secret_key) = get_contract_instance().await;
        let node_url = provider.url();

        // test_empty_no_return
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_empty_no_return", vec![]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "()");

        // test_empty
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_empty", vec![]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "()");

        // test_unit
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_unit", vec!["()"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "()");

        // test_u8
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_u8", vec!["255"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "255");

        // test_u16
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_u16", vec!["65535"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "65535");

        // test_u32
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_u32", vec!["4294967295"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "4294967295"
        );

        // test_u64
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_u64",
            vec!["18446744073709551615"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "18446744073709551615"
        );

        // test_u128
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_u128",
            vec!["340282366920938463463374607431768211455"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "340282366920938463463374607431768211455"
        );

        // test_u256
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_u256",
            vec!["115792089237316195423570985008687907853269984665640564039457584007913129639935"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "115792089237316195423570985008687907853269984665640564039457584007913129639935"
        );

        // test b256
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_b256",
            vec!["0000000000000000000000000000000000000000000000000000000000000042"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "0x0000000000000000000000000000000000000000000000000000000000000042"
        );

        // test_b256 - fails if 0x prefix provided since it extracts input as an external contract; we don't want to do this so explicitly provide the external contract as empty
        let mut cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_b256",
            vec!["0x0000000000000000000000000000000000000000000000000000000000000042"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        cmd.external_contracts = Some(vec![]);
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "0x0000000000000000000000000000000000000000000000000000000000000042"
        );

        // test_bytes
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_bytes", vec!["0x42"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "0x42");

        // test bytes without 0x prefix
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_bytes", vec!["42"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "0x42");

        // test_str
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_str", vec!["fuel"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "fuel");

        // test str array
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_str_array",
            vec!["fuel rocks"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "fuel rocks"
        );

        // test str array - fails if length mismatch
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_str_array", vec!["fuel"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap_err().to_string(),
            "string array length mismatch: expected 10, got 4"
        );

        // test str slice
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_str_slice",
            vec!["fuel rocks 42"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "fuel rocks 42"
        );

        // test tuple
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_tuple", vec!["(42, true)"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(42, true)"
        );

        // test array
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_array",
            vec!["[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "[42, 42, 42, 42, 42, 42, 42, 42, 42, 42]"
        );

        // test_array - fails if different types
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_array", vec!["[42, true]"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap_err().to_string(),
            "failed to parse u64 value: true"
        );

        // test_array - succeeds if length not matched!?
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_array", vec!["[42, 42]"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert!(call(operation, cmd)
            .await
            .unwrap()
            .result
            .unwrap()
            .starts_with("[42, 42, 0,"));

        // test_vector
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_vector", vec!["[42, 42]"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "[42, 42]"
        );

        // test_vector - fails if different types
        let cmd =
            get_contract_call_cmd(id, node_url, secret_key, "test_vector", vec!["[42, true]"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap_err().to_string(),
            "failed to parse u64 value: true"
        );

        // test_struct - Identity { name: str[2], id: u64 }
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_struct", vec!["{fu, 42}"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "{fu, 42}"
        );

        // test_struct - fails if incorrect inner attribute length
        let cmd =
            get_contract_call_cmd(id, node_url, secret_key, "test_struct", vec!["{fuel, 42}"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap_err().to_string(),
            "string array length mismatch: expected 2, got 4"
        );

        // test_struct - succeeds if missing inner final attribute; default value is used
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_struct", vec!["{fu}"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "{fu, 0}"
        );

        // test_struct - succeeds to use default values for all attributes if missing
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_struct", vec!["{}"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "{\0\0, 0}"
        );

        // test_enum
        let cmd =
            get_contract_call_cmd(id, node_url, secret_key, "test_enum", vec!["(Active:true)"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(Active:true)"
        );

        // test_enum - succeeds if using index
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_enum", vec!["(1:56)"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(Pending:56)"
        );

        // test_enum - fails if variant not found
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_enum", vec!["(A:true)"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap_err().to_string(),
            "failed to find index of variant: A"
        );

        // test_enum - fails if variant value incorrect
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_enum", vec!["(Active:3)"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap_err().to_string(),
            "failed to parse `Active` variant enum value: 3"
        );

        // test_enum - fails if variant value is missing
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_enum", vec!["(Active:)"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap_err().to_string(),
            "enum must have exactly two parts `(variant:value)`: (Active:)"
        );

        // test_option - encoded like an enum
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_option", vec!["(0:())"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(None:())"
        );

        // test_option - encoded like an enum; none value ignored
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_option", vec!["(0:42)"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(None:())"
        );

        // test_option - encoded like an enum; some value
        let cmd = get_contract_call_cmd(id, node_url, secret_key, "test_option", vec!["(1:42)"]);
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(Some:42)"
        );
    }

    #[tokio::test]
    async fn contract_call_with_abi_complex() {
        let (_, id, provider, secret_key) = get_contract_instance().await;
        let node_url = provider.url();

        // test_complex_struct
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_struct_with_generic",
            vec!["{42, fuel}"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "{42, fuel}"
        );

        // test_enum_with_generic
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_enum_with_generic",
            vec!["(value:32)"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(value:32)"
        );

        // test_enum_with_complex_generic
        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_enum_with_complex_generic",
            vec!["(value:{42, fuel})"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(value:{42, fuel})"
        );

        let cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "test_enum_with_complex_generic",
            vec!["(container:{{42, fuel}, fuel})"],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap().result.unwrap(),
            "(container:{{42, fuel}, fuel})"
        );
    }

    #[tokio::test]
    async fn contract_value_forwarding() {
        let (_, id, provider, secret_key) = get_contract_instance().await;
        let node_url = provider.url();

        let consensus_parameters = provider.consensus_parameters().await.unwrap();
        let base_asset_id = consensus_parameters.base_asset_id();
        let get_recipient_balance = |addr: Address, provider: Provider| async move {
            provider
                .get_asset_balance(&addr, base_asset_id)
                .await
                .unwrap()
        };
        let get_contract_balance = |id: ContractId, provider: Provider| async move {
            provider
                .get_contract_asset_balance(&id, base_asset_id)
                .await
                .unwrap()
        };

        // contract call transfer funds to another contract
        let (_, id_2, _, _) = get_contract_instance().await;
        let (amount, asset_id, recipient) = (
            "1",
            &format!("{{0x{base_asset_id}}}"),
            &format!("(ContractId:{{0x{id_2}}})"),
        );
        let mut cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "transfer",
            vec![amount, asset_id, recipient],
        );
        let operation = cmd.validate_and_get_operation().unwrap();
        cmd.call_parameters = cmd::call::CallParametersOpts {
            amount: amount.parse::<u64>().unwrap(),
            asset_id: Some(*base_asset_id),
            gas_forwarded: None,
        };
        // validate balance is unchanged (dry-run)
        let call_response = call(operation.clone(), cmd.clone()).await.unwrap();
        assert_eq!(call_response.result.unwrap(), "()");
        assert_eq!(call_response.script.unwrap().outputs().len(), 2);
        cmd.mode = cmd::call::ExecutionMode::Live;
        assert_eq!(call(operation, cmd).await.unwrap().result.unwrap(), "()");
        assert_eq!(get_contract_balance(id_2, provider.clone()).await, 1);
        assert_eq!(get_contract_balance(id, provider.clone()).await, 1);

        // contract call transfer funds to another address
        let random_wallet = Wallet::random(&mut rand::thread_rng(), provider.clone());
        let (amount, asset_id, recipient) = (
            "2",
            &format!("{{0x{base_asset_id}}}"),
            &format!("(Address:{{0x{}}})", random_wallet.address()),
        );
        let mut cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "transfer",
            vec![amount, asset_id, recipient],
        );
        cmd.call_parameters = cmd::call::CallParametersOpts {
            amount: amount.parse::<u64>().unwrap(),
            asset_id: Some(*base_asset_id),
            gas_forwarded: None,
        };
        cmd.mode = cmd::call::ExecutionMode::Live;
        let operation = cmd.validate_and_get_operation().unwrap();
        let call_response = call(operation, cmd).await.unwrap();
        assert_eq!(call_response.result.unwrap(), "()");
        assert_eq!(call_response.script.unwrap().outputs().len(), 3);
        assert_eq!(
            get_recipient_balance(random_wallet.address(), provider.clone()).await,
            2
        );
        assert_eq!(get_contract_balance(id, provider.clone()).await, 1);

        // contract call transfer funds to another address
        // specify amount x, provide amount x - 1
        // fails with panic reason 'NotEnoughBalance'
        let random_wallet = Wallet::random(&mut rand::thread_rng(), provider.clone());
        let (amount, asset_id, recipient) = (
            "5",
            &format!("{{0x{base_asset_id}}}"),
            &format!("(Address:{{0x{}}})", random_wallet.address()),
        );
        let mut cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "transfer",
            vec![amount, asset_id, recipient],
        );
        cmd.call_parameters = cmd::call::CallParametersOpts {
            amount: amount.parse::<u64>().unwrap() - 3,
            asset_id: Some(*base_asset_id),
            gas_forwarded: None,
        };
        cmd.mode = cmd::call::ExecutionMode::Live;
        let operation = cmd.validate_and_get_operation().unwrap();
        assert_eq!(
            call(operation, cmd).await.unwrap_err().to_string(),
            "Failed to parse call data: codec: `ReceiptDecoder`: failed to find matching receipts entry for Unit"
        );
        assert_eq!(get_contract_balance(id, provider.clone()).await, 1);

        // contract call transfer funds to another address
        // specify amount x, provide amount x + 5; should succeed
        let random_wallet = Wallet::random(&mut rand::thread_rng(), provider.clone());
        let (amount, asset_id, recipient) = (
            "3",
            &format!("{{0x{base_asset_id}}}"),
            &format!("(Address:{{0x{}}})", random_wallet.address()),
        );
        let mut cmd = get_contract_call_cmd(
            id,
            node_url,
            secret_key,
            "transfer",
            vec![amount, asset_id, recipient],
        );
        cmd.call_parameters = cmd::call::CallParametersOpts {
            amount: amount.parse::<u64>().unwrap() + 5,
            asset_id: Some(*base_asset_id),
            gas_forwarded: None,
        };
        cmd.mode = cmd::call::ExecutionMode::Live;
        let operation = cmd.validate_and_get_operation().unwrap();
        let call_response = call(operation, cmd).await.unwrap();
        assert_eq!(call_response.result.unwrap(), "()");
        assert_eq!(call_response.script.unwrap().outputs().len(), 3);
        assert_eq!(
            get_recipient_balance(random_wallet.address(), provider.clone()).await,
            3
        );
        assert_eq!(get_contract_balance(id, provider.clone()).await, 6); // extra amount (5) is forwarded to the contract
    }
}