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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
use starknet_accounts::{Account, AccountError, ConnectedAccount, ExecutionV1, ExecutionV3};
use starknet_core::{
    types::{Call, FeeEstimate, Felt, InvokeTransactionResult, SimulatedTransaction},
    utils::{get_udc_deployed_address, UdcUniqueSettings, UdcUniqueness},
};

/// The default UDC address: 0x041a78e741e5af2fec34b695679bc6891742439f7afb8484ecd7766661ad02bf.
const UDC_ADDRESS: Felt = Felt::from_raw([
    121672436446604875,
    9333317513348225193,
    15685625669053253235,
    15144800532519055890,
]);

/// Selector for entrypoint `deployContract`.
const SELECTOR_DEPLOYCONTRACT: Felt = Felt::from_raw([
    469988280392664069,
    1439621915307882061,
    1265649739554438882,
    18249998464715511309,
]);

/// A contract factory that acts as a blueprint for deploying Starknet smart contracts using the
/// Universal Deployer Contract.
#[derive(Debug)]
pub struct ContractFactory<A> {
    class_hash: Felt,
    udc_address: Felt,
    account: A,
}

/// Abstraction over contract deployment via the UDC. This type uses `INVOKE` v1 transactions under
/// the hood, and hence pays transaction fees in ETH. To use v3 transactions for STRK fee payment,
/// use [`DeploymentV3`] instead.
#[must_use]
#[derive(Debug)]
pub struct DeploymentV1<'f, A> {
    factory: &'f ContractFactory<A>,
    constructor_calldata: Vec<Felt>,
    salt: Felt,
    unique: bool,
    // The following fields allow us to mimic an `Execution` API.
    nonce: Option<Felt>,
    max_fee: Option<Felt>,
    fee_estimate_multiplier: f64,
}

/// Abstraction over contract deployment via the UDC. This type uses `INVOKE` v3 transactions under
/// the hood, and hence pays transaction fees in STRK. To use v1 transactions for ETH fee payment,
/// use [`DeploymentV1`] instead.
#[must_use]
#[derive(Debug)]
pub struct DeploymentV3<'f, A> {
    factory: &'f ContractFactory<A>,
    constructor_calldata: Vec<Felt>,
    salt: Felt,
    unique: bool,
    // The following fields allow us to mimic an `Execution` API.
    nonce: Option<Felt>,
    gas: Option<u64>,
    gas_price: Option<u128>,
    gas_estimate_multiplier: f64,
    gas_price_estimate_multiplier: f64,
}

impl<A> ContractFactory<A> {
    /// Constructs a new [`ContractFactory`] from a class hash and an account.
    ///
    /// The [`ContractFactory`] created uses the default address for the Universal Deployer
    /// Contract. To use a custom UDC deployment, use [`new_with_udc`](fn.new_with_udc) instead.
    pub const fn new(class_hash: Felt, account: A) -> Self {
        Self::new_with_udc(class_hash, account, UDC_ADDRESS)
    }

    /// Constructs a new [`ContractFactory`] with a custom Universal Deployer Contract address.
    pub const fn new_with_udc(class_hash: Felt, account: A, udc_address: Felt) -> Self {
        Self {
            class_hash,
            udc_address,
            account,
        }
    }
}

impl<A> ContractFactory<A>
where
    A: Account,
{
    /// Generates an instance of [`DeploymentV1`] for sending `INVOKE` v1 transactions for the
    /// contract deployment. Pays transaction fees in `ETH`.
    pub const fn deploy_v1(
        &self,
        constructor_calldata: Vec<Felt>,
        salt: Felt,
        unique: bool,
    ) -> DeploymentV1<'_, A> {
        DeploymentV1 {
            factory: self,
            constructor_calldata,
            salt,
            unique,
            nonce: None,
            max_fee: None,
            fee_estimate_multiplier: 1.1,
        }
    }

    /// Generates an instance of [`DeploymentV3`] for sending `INVOKE` v3 transactions for the
    /// contract deployment. Pays transaction fees in `STRK`.
    pub const fn deploy_v3(
        &self,
        constructor_calldata: Vec<Felt>,
        salt: Felt,
        unique: bool,
    ) -> DeploymentV3<'_, A> {
        DeploymentV3 {
            factory: self,
            constructor_calldata,
            salt,
            unique,
            nonce: None,
            gas: None,
            gas_price: None,
            gas_estimate_multiplier: 1.5,
            gas_price_estimate_multiplier: 1.5,
        }
    }

    /// Generates an instance of [`DeploymentV1`] for sending `INVOKE` v1 transactions for the
    /// contract deployment. Pays transaction fees in `ETH`.
    #[deprecated = "use version specific variants (`deploy_v1` & `deploy_v3`) instead"]
    pub const fn deploy(
        &self,
        constructor_calldata: Vec<Felt>,
        salt: Felt,
        unique: bool,
    ) -> DeploymentV1<'_, A> {
        self.deploy_v1(constructor_calldata, salt, unique)
    }
}

impl<'f, A> DeploymentV1<'f, A> {
    /// Returns a new [`DeploymentV1`] with the `nonce`.
    pub fn nonce(self, nonce: Felt) -> Self {
        Self {
            nonce: Some(nonce),
            ..self
        }
    }

    /// Returns a new [`DeploymentV1`] with the `max_fee`.
    pub fn max_fee(self, max_fee: Felt) -> Self {
        Self {
            max_fee: Some(max_fee),
            ..self
        }
    }

    /// Returns a new [`DeploymentV1`] with the fee estimate multiplier. The multiplier is used
    /// when transaction fee is not manually specified and must be fetched from a
    /// [`Provider`](starknet_providers::Provider) instead.
    pub fn fee_estimate_multiplier(self, fee_estimate_multiplier: f64) -> Self {
        Self {
            fee_estimate_multiplier,
            ..self
        }
    }
}

impl<'f, A> DeploymentV3<'f, A> {
    /// Returns a new [`DeploymentV3`] with the `nonce`.
    pub fn nonce(self, nonce: Felt) -> Self {
        Self {
            nonce: Some(nonce),
            ..self
        }
    }

    /// Returns a new [`DeploymentV3`] with the `gas`.
    pub fn gas(self, gas: u64) -> Self {
        Self {
            gas: Some(gas),
            ..self
        }
    }

    /// Returns a new [`DeploymentV3`] with the `gas_price`.
    pub fn gas_price(self, gas_price: u128) -> Self {
        Self {
            gas_price: Some(gas_price),
            ..self
        }
    }

    /// Returns a new [`DeploymentV3`] with the gas amount estimate multiplier.  The multiplier is
    /// used when the gas amount is not manually specified and must be fetched from a
    /// [`Provider`](starknet_providers::Provider) instead.
    pub fn gas_estimate_multiplier(self, gas_estimate_multiplier: f64) -> Self {
        Self {
            gas_estimate_multiplier,
            ..self
        }
    }

    /// Returns a new [`DeploymentV3`] with the gas price estimate multiplier.  The multiplier is
    /// used when the gas price is not manually specified and must be fetched from a
    /// [`Provider`](starknet_providers::Provider) instead.
    pub fn gas_price_estimate_multiplier(self, gas_price_estimate_multiplier: f64) -> Self {
        Self {
            gas_price_estimate_multiplier,
            ..self
        }
    }
}

impl<'f, A> DeploymentV1<'f, A>
where
    A: Account,
{
    /// Calculate the resulting contract address without sending a transaction.
    pub fn deployed_address(&self) -> Felt {
        get_udc_deployed_address(
            self.salt,
            self.factory.class_hash,
            &if self.unique {
                UdcUniqueness::Unique(UdcUniqueSettings {
                    deployer_address: self.factory.account.address(),
                    udc_contract_address: self.factory.udc_address,
                })
            } else {
                UdcUniqueness::NotUnique
            },
            &self.constructor_calldata,
        )
    }
}

impl<'f, A> DeploymentV3<'f, A>
where
    A: Account,
{
    /// Calculate the resulting contract address without sending a transaction.
    pub fn deployed_address(&self) -> Felt {
        get_udc_deployed_address(
            self.salt,
            self.factory.class_hash,
            &if self.unique {
                UdcUniqueness::Unique(UdcUniqueSettings {
                    deployer_address: self.factory.account.address(),
                    udc_contract_address: self.factory.udc_address,
                })
            } else {
                UdcUniqueness::NotUnique
            },
            &self.constructor_calldata,
        )
    }
}

impl<'f, A> DeploymentV1<'f, A>
where
    A: ConnectedAccount + Sync,
{
    /// Estimates transaction fees from a [`Provider`](starknet_providers::Provider).
    pub async fn estimate_fee(&self) -> Result<FeeEstimate, AccountError<A::SignError>> {
        let execution: ExecutionV1<'_, A> = self.into();
        execution.estimate_fee().await
    }

    /// Simulates the transaction from a [`Provider`](starknet_providers::Provider). Transaction
    /// validation and fee transfer can be skipped.
    pub async fn simulate(
        &self,
        skip_validate: bool,
        skip_fee_charge: bool,
    ) -> Result<SimulatedTransaction, AccountError<A::SignError>> {
        let execution: ExecutionV1<'_, A> = self.into();
        execution.simulate(skip_validate, skip_fee_charge).await
    }

    /// Signs and broadcasts the transaction to the network.
    pub async fn send(&self) -> Result<InvokeTransactionResult, AccountError<A::SignError>> {
        let execution: ExecutionV1<'_, A> = self.into();
        execution.send().await
    }
}

impl<'f, A> DeploymentV3<'f, A>
where
    A: ConnectedAccount + Sync,
{
    /// Estimates transaction fees from a [`Provider`](starknet_providers::Provider).
    pub async fn estimate_fee(&self) -> Result<FeeEstimate, AccountError<A::SignError>> {
        let execution: ExecutionV3<'_, A> = self.into();
        execution.estimate_fee().await
    }

    /// Simulates the transaction from a [`Provider`](starknet_providers::Provider). Transaction
    /// validation and fee transfer can be skipped.
    pub async fn simulate(
        &self,
        skip_validate: bool,
        skip_fee_charge: bool,
    ) -> Result<SimulatedTransaction, AccountError<A::SignError>> {
        let execution: ExecutionV3<'_, A> = self.into();
        execution.simulate(skip_validate, skip_fee_charge).await
    }

    /// Signs and broadcasts the transaction to the network.
    pub async fn send(&self) -> Result<InvokeTransactionResult, AccountError<A::SignError>> {
        let execution: ExecutionV3<'_, A> = self.into();
        execution.send().await
    }
}

impl<'f, A> From<&DeploymentV1<'f, A>> for ExecutionV1<'f, A> {
    fn from(value: &DeploymentV1<'f, A>) -> Self {
        let mut calldata = vec![
            value.factory.class_hash,
            value.salt,
            if value.unique { Felt::ONE } else { Felt::ZERO },
            value.constructor_calldata.len().into(),
        ];
        calldata.extend_from_slice(&value.constructor_calldata);

        let execution = Self::new(
            vec![Call {
                to: value.factory.udc_address,
                selector: SELECTOR_DEPLOYCONTRACT,
                calldata,
            }],
            &value.factory.account,
        );

        let execution = if let Some(nonce) = value.nonce {
            execution.nonce(nonce)
        } else {
            execution
        };

        let execution = if let Some(max_fee) = value.max_fee {
            execution.max_fee(max_fee)
        } else {
            execution
        };

        execution.fee_estimate_multiplier(value.fee_estimate_multiplier)
    }
}

impl<'f, A> From<&DeploymentV3<'f, A>> for ExecutionV3<'f, A> {
    fn from(value: &DeploymentV3<'f, A>) -> Self {
        let mut calldata = vec![
            value.factory.class_hash,
            value.salt,
            if value.unique { Felt::ONE } else { Felt::ZERO },
            value.constructor_calldata.len().into(),
        ];
        calldata.extend_from_slice(&value.constructor_calldata);

        let execution = Self::new(
            vec![Call {
                to: value.factory.udc_address,
                selector: SELECTOR_DEPLOYCONTRACT,
                calldata,
            }],
            &value.factory.account,
        );

        let execution = if let Some(nonce) = value.nonce {
            execution.nonce(nonce)
        } else {
            execution
        };

        let execution = if let Some(gas) = value.gas {
            execution.gas(gas)
        } else {
            execution
        };

        let execution = if let Some(gas_price) = value.gas_price {
            execution.gas_price(gas_price)
        } else {
            execution
        };

        let execution = execution.gas_estimate_multiplier(value.gas_estimate_multiplier);

        execution.gas_price_estimate_multiplier(value.gas_price_estimate_multiplier)
    }
}
#[cfg(test)]
mod tests {
    use starknet_accounts::{ExecutionEncoding, SingleOwnerAccount};
    use starknet_core::chain_id;
    use starknet_providers::SequencerGatewayProvider;
    use starknet_signers::{LocalWallet, SigningKey};

    use super::*;

    #[test]
    #[cfg_attr(target_arch = "wasm32", wasm_bindgen_test::wasm_bindgen_test)]
    fn test_deployed_address_unique() {
        let factory = ContractFactory::new(
            Felt::from_hex("0x2bfd9564754d9b4a326da62b2f22b8fea7bbeffd62da4fcaea986c323b7aeb")
                .unwrap(),
            SingleOwnerAccount::new(
                SequencerGatewayProvider::starknet_alpha_sepolia(),
                LocalWallet::from_signing_key(SigningKey::from_random()),
                Felt::from_hex("0xb1461de04c6a1aa3375bdf9b7723a8779c082ffe21311d683a0b15c078b5dc")
                    .unwrap(),
                chain_id::SEPOLIA,
                ExecutionEncoding::Legacy,
            ),
        );

        let unique_address_v1 = factory
            .deploy_v1(
                vec![Felt::from_hex("0x1234").unwrap()],
                Felt::from_hex("0x3456").unwrap(),
                true,
            )
            .deployed_address();
        let unique_address_v3 = factory
            .deploy_v3(
                vec![Felt::from_hex("0x1234").unwrap()],
                Felt::from_hex("0x3456").unwrap(),
                true,
            )
            .deployed_address();

        let not_unique_address_v1 = factory
            .deploy_v1(
                vec![Felt::from_hex("0x1234").unwrap()],
                Felt::from_hex("0x3456").unwrap(),
                false,
            )
            .deployed_address();
        let not_unique_address_v3 = factory
            .deploy_v3(
                vec![Felt::from_hex("0x1234").unwrap()],
                Felt::from_hex("0x3456").unwrap(),
                false,
            )
            .deployed_address();

        assert_eq!(
            unique_address_v1,
            Felt::from_hex("0x36e05bcd41191387bc2f04ed9cad4776a75df3b748b0246a5d217a988474181")
                .unwrap()
        );
        assert_eq!(
            unique_address_v3,
            Felt::from_hex("0x36e05bcd41191387bc2f04ed9cad4776a75df3b748b0246a5d217a988474181")
                .unwrap()
        );

        assert_eq!(
            not_unique_address_v1,
            Felt::from_hex("0x3a320b6aa0b451b22fba90b5d75b943932649137c09a86a5cf4853031be70c1")
                .unwrap()
        );
        assert_eq!(
            not_unique_address_v3,
            Felt::from_hex("0x3a320b6aa0b451b22fba90b5d75b943932649137c09a86a5cf4853031be70c1")
                .unwrap()
        );
    }
}