fuels_contract/
call_response.rs

1use crate::logs::LogDecoder;
2use fuel_tx::Receipt;
3use fuels_core::{Parameterize, Tokenizable};
4use fuels_types::errors::Error;
5
6/// [`FuelCallResponse`] is a struct that is returned by a call to the contract or script. Its value
7/// field holds the decoded typed value returned by the contract's method. The other field holds all
8/// the receipts returned by the call.
9//
10/// The name is `FuelCallResponse` instead of `CallResponse` because it would be ambiguous with the
11/// `CALL` opcode.
12#[derive(Debug)]
13// ANCHOR: fuel_call_response
14pub struct FuelCallResponse<D> {
15    pub value: D,
16    pub receipts: Vec<Receipt>,
17    pub gas_used: u64,
18    pub log_decoder: LogDecoder,
19}
20// ANCHOR_END: fuel_call_response
21
22impl<D> FuelCallResponse<D> {
23    /// Get the gas used from ScriptResult receipt
24    fn get_gas_used(receipts: &[Receipt]) -> u64 {
25        receipts
26            .iter()
27            .rfind(|r| matches!(r, Receipt::ScriptResult { .. }))
28            .expect("could not retrieve ScriptResult")
29            .gas_used()
30            .expect("could not retrieve gas used from ScriptResult")
31    }
32
33    pub fn new(value: D, receipts: Vec<Receipt>, log_decoder: LogDecoder) -> Self {
34        Self {
35            value,
36            gas_used: Self::get_gas_used(&receipts),
37            receipts,
38            log_decoder,
39        }
40    }
41
42    pub fn get_logs(&self) -> Result<Vec<String>, Error> {
43        self.log_decoder.get_logs(&self.receipts)
44    }
45
46    pub fn get_logs_with_type<T: Tokenizable + Parameterize>(&self) -> Result<Vec<T>, Error> {
47        self.log_decoder.get_logs_with_type::<T>(&self.receipts)
48    }
49}