fuels_programs/contract/
regular.rs

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
use std::{default::Default, fmt::Debug, path::Path};

use fuel_tx::{Bytes32, ContractId, Salt, StorageSlot};
use fuels_accounts::Account;
use fuels_core::{
    constants::WORD_SIZE,
    error,
    types::{
        bech32::Bech32ContractId,
        errors::Result,
        transaction::TxPolicies,
        transaction_builders::{Blob, CreateTransactionBuilder},
    },
    Configurables,
};

use super::{
    compute_contract_id_and_state_root, validate_path_and_extension, BlobsNotUploaded, Contract,
    Loader, StorageConfiguration,
};

// In a mod so that we eliminate the footgun of getting the private `code` field without applying
// configurables
mod code_types {
    use fuels_core::Configurables;

    #[derive(Debug, Clone, PartialEq)]
    pub struct Regular {
        code: Vec<u8>,
        configurables: Configurables,
    }

    impl Regular {
        pub(crate) fn new(code: Vec<u8>, configurables: Configurables) -> Self {
            Self {
                code,
                configurables,
            }
        }

        pub(crate) fn with_code(self, code: Vec<u8>) -> Self {
            Self { code, ..self }
        }

        pub(crate) fn with_configurables(self, configurables: Configurables) -> Self {
            Self {
                configurables,
                ..self
            }
        }

        pub(crate) fn code(&self) -> Vec<u8> {
            let mut code = self.code.clone();
            self.configurables.update_constants_in(&mut code);
            code
        }
    }
}
pub use code_types::*;

impl Contract<Regular> {
    pub fn with_code(self, code: Vec<u8>) -> Self {
        Self {
            code: self.code.with_code(code),
            salt: self.salt,
            storage_slots: self.storage_slots,
        }
    }

    pub fn with_configurables(self, configurables: impl Into<Configurables>) -> Self {
        Self {
            code: self.code.with_configurables(configurables.into()),
            ..self
        }
    }

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

    pub fn contract_id(&self) -> ContractId {
        self.compute_roots().0
    }

    pub fn code_root(&self) -> Bytes32 {
        self.compute_roots().1
    }

    pub fn state_root(&self) -> Bytes32 {
        self.compute_roots().2
    }

    fn compute_roots(&self) -> (ContractId, Bytes32, Bytes32) {
        compute_contract_id_and_state_root(&self.code(), &self.salt, &self.storage_slots)
    }

    /// Loads a contract from a binary file. Salt and storage slots are loaded as well, depending on the configuration provided.
    pub fn load_from(
        binary_filepath: impl AsRef<Path>,
        config: LoadConfiguration,
    ) -> Result<Contract<Regular>> {
        let binary_filepath = binary_filepath.as_ref();
        validate_path_and_extension(binary_filepath, "bin")?;

        let binary = std::fs::read(binary_filepath).map_err(|e| {
            std::io::Error::new(
                e.kind(),
                format!("failed to read binary: {binary_filepath:?}: {e}"),
            )
        })?;

        let storage_slots = super::determine_storage_slots(config.storage, binary_filepath)?;

        Ok(Contract {
            code: Regular::new(binary, config.configurables),
            salt: config.salt,
            storage_slots,
        })
    }

    /// Creates a regular contract with the given code, salt, and storage slots.
    pub fn regular(
        code: Vec<u8>,
        salt: Salt,
        storage_slots: Vec<StorageSlot>,
    ) -> Contract<Regular> {
        Contract {
            code: Regular::new(code, Configurables::default()),
            salt,
            storage_slots,
        }
    }

    /// Deploys a compiled contract to a running node.
    /// To deploy a contract, you need an account with enough assets to pay for deployment.
    /// This account will also receive the change.
    pub async fn deploy(
        self,
        account: &impl Account,
        tx_policies: TxPolicies,
    ) -> Result<Bech32ContractId> {
        let contract_id = self.contract_id();
        let state_root = self.state_root();
        let salt = self.salt;
        let storage_slots = self.storage_slots;

        let mut tb = CreateTransactionBuilder::prepare_contract_deployment(
            self.code.code(),
            contract_id,
            state_root,
            salt,
            storage_slots.to_vec(),
            tx_policies,
        )
        .with_max_fee_estimation_tolerance(0.05);

        account.add_witnesses(&mut tb)?;
        account.adjust_for_fee(&mut tb, 0).await?;

        let provider = account.try_provider()?;

        let tx = tb.build(provider).await?;

        provider
            .send_transaction_and_await_commit(tx)
            .await?
            .check(None)?;

        Ok(contract_id.into())
    }

    /// Deploys a compiled contract to a running node if a contract with
    /// the corresponding [`ContractId`] doesn't exist.
    pub async fn deploy_if_not_exists(
        self,
        account: &impl Account,
        tx_policies: TxPolicies,
    ) -> Result<Bech32ContractId> {
        let contract_id = Bech32ContractId::from(self.contract_id());
        let provider = account.try_provider()?;
        if provider.contract_exists(&contract_id).await? {
            Ok(contract_id)
        } else {
            self.deploy(account, tx_policies).await
        }
    }

    /// Converts a regular contract into a loader contract, splitting the code into blobs.
    pub fn convert_to_loader(
        self,
        max_words_per_blob: usize,
    ) -> Result<Contract<Loader<BlobsNotUploaded>>> {
        if max_words_per_blob == 0 {
            return Err(error!(Other, "blob size must be greater than 0"));
        }
        let blobs = self
            .code()
            .chunks(max_words_per_blob.saturating_mul(WORD_SIZE))
            .map(|chunk| Blob::new(chunk.to_vec()))
            .collect();

        Contract::loader_from_blobs(blobs, self.salt, self.storage_slots)
    }

    /// Deploys the contract either as a regular contract or as a loader contract if it exceeds the size limit.
    pub async fn smart_deploy(
        self,
        account: &impl Account,
        tx_policies: TxPolicies,
        max_words_per_blob: usize,
    ) -> Result<Bech32ContractId> {
        let provider = account.try_provider()?;
        let max_contract_size = provider
            .consensus_parameters()
            .contract_params()
            .contract_max_size() as usize;

        if self.code().len() <= max_contract_size {
            self.deploy(account, tx_policies).await
        } else {
            self.convert_to_loader(max_words_per_blob)?
                .deploy(account, tx_policies)
                .await
        }
    }
}

/// Configuration for contract deployment.
#[derive(Debug, Clone, Default)]
pub struct LoadConfiguration {
    pub(crate) storage: StorageConfiguration,
    pub(crate) configurables: Configurables,
    pub(crate) salt: Salt,
}

impl LoadConfiguration {
    pub fn new(
        storage: StorageConfiguration,
        configurables: impl Into<Configurables>,
        salt: impl Into<Salt>,
    ) -> Self {
        Self {
            storage,
            configurables: configurables.into(),
            salt: salt.into(),
        }
    }

    pub fn with_storage_configuration(mut self, storage: StorageConfiguration) -> Self {
        self.storage = storage;
        self
    }

    pub fn with_configurables(mut self, configurables: impl Into<Configurables>) -> Self {
        self.configurables = configurables.into();
        self
    }

    pub fn with_salt(mut self, salt: impl Into<Salt>) -> Self {
        self.salt = salt.into();
        self
    }
}