-
Notifications
You must be signed in to change notification settings - Fork 17
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve API consistency #85
Changes from 28 commits
b824eb2
710c4b7
ec28030
8199608
2ef7d7a
981cfe3
c669388
bd40b5b
01789f8
cb2f4f1
9e9d92f
3cbd7b0
5ab4ec7
b29a21a
01d399f
85818be
6d3a8b3
aa399f4
760cd5d
f33c543
cda5391
eef156c
be1b6b8
d510941
75ec65d
97b5125
995813a
2e30826
238066b
79e727a
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,18 @@ | ||
use super::{rlp_append_option, Eip712Meta}; | ||
use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr}; | ||
|
||
use super::{hash_bytecode, rlp_append_option, Eip712Meta}; | ||
use crate::{ | ||
zks_utils::{EIP712_TX_TYPE, ERA_CHAIN_ID, MAX_PRIORITY_FEE_PER_GAS}, | ||
zks_wallet::Overrides, | ||
zks_utils::{ | ||
self, CONTRACT_DEPLOYER_ADDR, EIP712_TX_TYPE, ERA_CHAIN_ID, MAX_PRIORITY_FEE_PER_GAS, | ||
}, | ||
zks_wallet::{DeployRequest, Overrides, TransferRequest, WithdrawRequest, ZKRequestError}, | ||
}; | ||
use ethers::{ | ||
abi::{Abi, HumanReadableParser, ParseError}, | ||
types::{transaction::eip2930::AccessList, Address, Bytes, Signature, U256, U64}, | ||
utils::rlp::{Encodable, RlpStream}, | ||
}; | ||
use ethers_contract::encode_function_data; | ||
use serde::{Deserialize, Serialize}; | ||
|
||
// TODO: Not all the fields are optional. This was copied from the JS implementation. | ||
|
@@ -229,3 +235,98 @@ impl Default for Eip712TransactionRequest { | |
} | ||
} | ||
} | ||
|
||
impl TryFrom<WithdrawRequest> for Eip712TransactionRequest { | ||
type Error = ZKRequestError; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The |
||
|
||
fn try_from(request: WithdrawRequest) -> Result<Self, Self::Error> { | ||
let contract_address = | ||
Address::from_str(zks_utils::CONTRACTS_L2_ETH_TOKEN_ADDR).map_err(|e| { | ||
ZKRequestError::CustomError(format!("Error getting L2 ETH token address {e:?}")) | ||
})?; | ||
let function_signature = "function withdraw(address _l1Receiver) external payable override"; | ||
let function = HumanReadableParser::parse_function(function_signature) | ||
.map_err(ParseError::LexerError)?; | ||
let function_args = function.decode_input(&zks_utils::encode_args( | ||
&function, | ||
&[format!("{:?}", request.to)], | ||
)?)?; | ||
let data: Bytes = function.encode_input(&function_args)?.into(); | ||
|
||
Ok(Eip712TransactionRequest::new() | ||
.r#type(EIP712_TX_TYPE) | ||
.to(contract_address) | ||
.value(request.amount) | ||
.from(request.from) | ||
.data(data)) | ||
} | ||
} | ||
|
||
impl From<TransferRequest> for Eip712TransactionRequest { | ||
fn from(request: TransferRequest) -> Self { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as my comment for |
||
Eip712TransactionRequest::new() | ||
.r#type(EIP712_TX_TYPE) | ||
.to(request.to) | ||
.value(request.amount) | ||
.from(request.from) | ||
} | ||
} | ||
|
||
impl TryFrom<DeployRequest> for Eip712TransactionRequest { | ||
type Error = ZKRequestError; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as my comment for |
||
|
||
fn try_from(request: DeployRequest) -> Result<Self, Self::Error> { | ||
let mut contract_deployer_path = PathBuf::from(env!("CARGO_MANIFEST_DIR")); | ||
contract_deployer_path.push("src/abi/ContractDeployer.json"); | ||
|
||
let custom_data = Eip712Meta::new().factory_deps({ | ||
let mut factory_deps = Vec::new(); | ||
if let Some(factory_dependencies) = request.factory_deps { | ||
factory_deps.extend(factory_dependencies); | ||
} | ||
factory_deps.push(request.contract_bytecode.clone()); | ||
factory_deps | ||
}); | ||
|
||
let contract_deployer = Abi::load(BufReader::new( | ||
File::open(contract_deployer_path).map_err(|e| { | ||
ZKRequestError::CustomError(format!( | ||
"Error opening contract deployer abi file {e:?}" | ||
)) | ||
})?, | ||
))?; | ||
let create = contract_deployer.function("create")?; | ||
|
||
// TODO: User could provide this instead of defaulting. | ||
let salt = [0_u8; 32]; | ||
let bytecode_hash = hash_bytecode(&request.contract_bytecode).map_err(|e| { | ||
ZKRequestError::CustomError(format!("Error hashing contract bytecode {e:?}")) | ||
})?; | ||
let call_data: Bytes = match ( | ||
request.contract_abi.constructor(), | ||
request.constructor_parameters.is_empty(), | ||
) { | ||
(None, false) => { | ||
return Err(ZKRequestError::CustomError( | ||
"Constructor not present".to_owned(), | ||
)) | ||
} | ||
(None, true) | (Some(_), true) => Bytes::default(), | ||
(Some(constructor), false) => { | ||
zks_utils::encode_constructor_args(constructor, &request.constructor_parameters)? | ||
.into() | ||
} | ||
}; | ||
|
||
let data = encode_function_data(create, (salt, bytecode_hash, call_data))?; | ||
|
||
let contract_deployer_address = Address::from_str(CONTRACT_DEPLOYER_ADDR).map_err(|e| { | ||
ZKRequestError::CustomError(format!("Error getting contract deployer address {e:?}")) | ||
})?; | ||
Ok(Eip712TransactionRequest::new() | ||
.r#type(EIP712_TX_TYPE) | ||
.to(contract_deployer_address) | ||
.custom_data(custom_data) | ||
.data(data)) | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Are we testing examples in the CI?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No, we are not at the moment