fuels_programs/responses/
call.rs

1use std::fmt::Debug;
2
3use fuel_tx::{Bytes32, Receipt};
4use fuels_core::{
5    codec::{LogDecoder, LogResult},
6    traits::{Parameterize, Tokenizable},
7    types::errors::Result,
8};
9
10/// [`CallResponse`] is a struct that is returned by a call to the contract or script. Its value
11/// field holds the decoded typed value returned by the contract's method. The other field holds all
12/// the receipts returned by the call.
13#[derive(Debug)]
14// ANCHOR: call_response
15pub struct CallResponse<D> {
16    pub value: D,
17    pub receipts: Vec<Receipt>,
18    pub gas_used: u64,
19    pub log_decoder: LogDecoder,
20    pub tx_id: Option<Bytes32>,
21}
22// ANCHOR_END: call_response
23
24impl<D> CallResponse<D> {
25    /// Get the gas used from ScriptResult receipt
26    fn get_gas_used(receipts: &[Receipt]) -> u64 {
27        receipts
28            .iter()
29            .rfind(|r| matches!(r, Receipt::ScriptResult { .. }))
30            .expect("could not retrieve ScriptResult")
31            .gas_used()
32            .expect("could not retrieve gas used from ScriptResult")
33    }
34
35    pub fn new(
36        value: D,
37        receipts: Vec<Receipt>,
38        log_decoder: LogDecoder,
39        tx_id: Option<Bytes32>,
40    ) -> Self {
41        Self {
42            value,
43            gas_used: Self::get_gas_used(&receipts),
44            receipts,
45            log_decoder,
46            tx_id,
47        }
48    }
49
50    pub fn decode_logs(&self) -> LogResult {
51        self.log_decoder.decode_logs(&self.receipts)
52    }
53
54    pub fn decode_logs_with_type<T: Tokenizable + Parameterize + 'static>(&self) -> Result<Vec<T>> {
55        self.log_decoder.decode_logs_with_type::<T>(&self.receipts)
56    }
57}