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
use fuel_gql_client::fuel_tx::{Address, Contract};
use fuels_core::abi_encoder::ABIEncoder;
use fuels_core::Tokenizable;
use fuels_types::errors::Error;

use fuels_types::bech32::Bech32Address;

pub struct Predicate {
    address: Bech32Address,
    code: Vec<u8>,
}

impl Predicate {
    pub fn new(code: Vec<u8>) -> Self {
        let address: Address = (*Contract::root_from_code(&code)).into();
        Self {
            address: address.into(),
            code,
        }
    }

    pub fn load_from(file_path: &str) -> Result<Self, Error> {
        Ok(Predicate::new(std::fs::read(file_path)?))
    }

    pub fn address(&self) -> &Bech32Address {
        &self.address
    }

    pub fn code(&self) -> Vec<u8> {
        self.code.clone()
    }

    /// Encode the predicate data with the given arguments.
    pub fn encode_data<D: Tokenizable>(&self, data: D) -> Result<Vec<u8>, Error> {
        Ok(ABIEncoder::encode(&[data.into_token()])?.resolve(0))
    }
}