fuels_contract/
predicate.rs

1use fuel_gql_client::fuel_tx::{Address, Contract};
2use fuels_core::abi_encoder::ABIEncoder;
3use fuels_core::Tokenizable;
4use fuels_types::errors::Error;
5
6use fuels_types::bech32::Bech32Address;
7
8pub struct Predicate {
9    address: Bech32Address,
10    code: Vec<u8>,
11}
12
13impl Predicate {
14    pub fn new(code: Vec<u8>) -> Self {
15        let address: Address = (*Contract::root_from_code(&code)).into();
16        Self {
17            address: address.into(),
18            code,
19        }
20    }
21
22    pub fn load_from(file_path: &str) -> Result<Self, Error> {
23        Ok(Predicate::new(std::fs::read(file_path)?))
24    }
25
26    pub fn address(&self) -> &Bech32Address {
27        &self.address
28    }
29
30    pub fn code(&self) -> Vec<u8> {
31        self.code.clone()
32    }
33
34    /// Encode the predicate data with the given arguments.
35    pub fn encode_data<D: Tokenizable>(&self, data: D) -> Result<Vec<u8>, Error> {
36        Ok(ABIEncoder::encode(&[data.into_token()])?.resolve(0))
37    }
38}