fuels_programs/calls/
script_call.rsuse std::{collections::HashSet, fmt::Debug};
use fuel_tx::{ContractId, Output};
use fuels_core::types::{
bech32::Bech32ContractId,
errors::{error, Result},
input::Input,
};
use itertools::chain;
use crate::calls::utils::{generate_contract_inputs, generate_contract_outputs, sealed};
#[derive(Debug, Clone)]
pub struct ScriptCall {
pub script_binary: Vec<u8>,
pub encoded_args: Result<Vec<u8>>,
pub inputs: Vec<Input>,
pub outputs: Vec<Output>,
pub external_contracts: Vec<Bech32ContractId>,
}
impl ScriptCall {
pub fn with_outputs(mut self, outputs: Vec<Output>) -> Self {
self.outputs = outputs;
self
}
pub fn with_inputs(mut self, inputs: Vec<Input>) -> Self {
self.inputs = inputs;
self
}
pub(crate) fn prepare_inputs_outputs(&self) -> Result<(Vec<Input>, Vec<Output>)> {
let contract_ids: HashSet<ContractId> = self
.external_contracts
.iter()
.map(|bech32| bech32.into())
.collect();
let num_of_contracts = contract_ids.len();
let inputs = chain!(generate_contract_inputs(contract_ids), self.inputs.clone(),).collect();
let outputs = chain!(
generate_contract_outputs(num_of_contracts),
self.outputs.clone(),
)
.collect();
Ok((inputs, outputs))
}
pub(crate) fn compute_script_data(&self) -> Result<Vec<u8>> {
self.encoded_args
.as_ref()
.map(|b| b.to_owned())
.map_err(|e| error!(Codec, "cannot encode script call arguments: {e}"))
}
}
impl sealed::Sealed for ScriptCall {}