Skip to content

Commit

Permalink
Add enum for deploy types
Browse files Browse the repository at this point in the history
  • Loading branch information
IAvecilla committed Dec 27, 2023
1 parent 82bdc39 commit 2f6db87
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 11 deletions.
5 changes: 3 additions & 2 deletions src/eip712/transaction_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,7 +299,7 @@ impl TryFrom<DeployRequest> for Eip712TransactionRequest {
))
})?,
))?;
let create_function = contract_deployer.function(&request.deploy_type)?;
let create_function = contract_deployer.function(&request.deploy_type.to_string())?;

let bytecode_hash = hash_bytecode(&request.contract_bytecode).map_err(|e| {
ZKRequestError::CustomError(format!("Error hashing contract bytecode {e:?}"))
Expand All @@ -319,7 +319,8 @@ impl TryFrom<DeployRequest> for Eip712TransactionRequest {
.into()
}
};
let salt = request.salt.unwrap_or_else(|| [0_u8; 32]);

let salt = request.salt.unwrap_or([0_u8; 32]);
let data = encode_function_data(create_function, (salt, bytecode_hash, call_data))?;

let contract_deployer_address = Address::from_str(CONTRACT_DEPLOYER_ADDR).map_err(|e| {
Expand Down
5 changes: 3 additions & 2 deletions src/tests/wallet_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ mod zks_signer_tests {
use crate::zks_provider::ZKSProvider;
use crate::zks_utils::{ERA_CHAIN_ID, ETH_CHAIN_ID};
use crate::zks_wallet::{
CallRequest, DeployRequest, DepositRequest, TransferRequest, WithdrawRequest, ZKSWallet,
CallRequest, DepositRequest, TransferRequest, WithdrawRequest, ZKSWallet,
{DeployRequest, DeployType},
};
use ethers::abi::Tokenize;
use ethers::contract::abigen;
Expand Down Expand Up @@ -332,7 +333,7 @@ mod zks_signer_tests {
rand::thread_rng().fill_bytes(&mut salt[..]);
let deploy_request = DeployRequest::with(contract.abi, contract.bin.to_vec(), vec![])
.from(zk_wallet.l2_address())
.deploy_type("create2")
.deploy_type(DeployType::Create2)
.salt(salt);
let first_deploy_contract_address = zk_wallet.deploy(&deploy_request).await.unwrap();
let deploy_result = era_provider
Expand Down
7 changes: 5 additions & 2 deletions src/zks_wallet/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,11 @@ pub use errors::{ZKRequestError, ZKSWalletError};

mod requests;
pub use requests::{
call_request::CallRequest, deploy_request::DeployRequest, deposit_request::DepositRequest,
transfer_request::TransferRequest, withdraw_request::WithdrawRequest,
call_request::CallRequest,
deploy_request::{DeployRequest, DeployType},
deposit_request::DepositRequest,
transfer_request::TransferRequest,
withdraw_request::WithdrawRequest,
};

mod wallet;
Expand Down
25 changes: 20 additions & 5 deletions src/zks_wallet/requests/deploy_request.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
use ethers::{abi::Abi, types::Address};
use std::fmt::Debug;
use std::{fmt::Debug, fmt::Display};

#[derive(Clone, Debug)]
pub enum DeployType {
Create,
Create2,
}

impl Display for DeployType {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
DeployType::Create => write!(f, "create"),
DeployType::Create2 => write!(f, "create2"),
}
}
}

#[derive(Clone, Debug)]
pub struct DeployRequest {
Expand All @@ -8,7 +23,7 @@ pub struct DeployRequest {
pub constructor_parameters: Vec<String>,
pub from: Address,
pub factory_deps: Option<Vec<Vec<u8>>>,
pub deploy_type: String,
pub deploy_type: DeployType,
pub salt: Option<[u8; 32]>,
}

Expand All @@ -24,7 +39,7 @@ impl DeployRequest {
constructor_parameters,
from: Default::default(),
factory_deps: None,
deploy_type: "create".to_string(),
deploy_type: DeployType::Create,
salt: None,
}
}
Expand All @@ -39,8 +54,8 @@ impl DeployRequest {
self
}

pub fn deploy_type(mut self, deploy_type: &str) -> Self {
self.deploy_type = deploy_type.to_string();
pub fn deploy_type(mut self, deploy_type: DeployType) -> Self {
self.deploy_type = deploy_type;
self
}

Expand Down

0 comments on commit 2f6db87

Please sign in to comment.