diff --git a/README.md b/README.md index 3f8f07d..f8462fc 100644 --- a/README.md +++ b/README.md @@ -89,7 +89,6 @@ To create a payment transaction, you need to provide the sender's address, the r ```rust use zksync::zks_provider::ZKSProvider; -let sender_address: zksync::Address = "0x36615Cf349d7F6344891B1e7CA7C72883F5dc049".parse().unwrap(); let receiver_address: zksync::Address = "0xa61464658AfeAf65CccaaFD3a512b69A83B77618".parse().unwrap(); let amount_to_transfer = zksync::U256::from(1); @@ -100,15 +99,21 @@ let mut payment_request = zksync::zks_wallet::TransferRequest::with(amount_to_tr #### Sending the Transaction -To send the payment transaction, you need to provide use the wallet and the transaction. You can send the transaction using the following code: +To send the payment transaction, you need to use the wallet and the transfer parameters. You can send the transaction using the following code: > In case you are wondering, the transaction is signed in the `send_transaction` method inside the transfer process. ```rust -let payment_response: zksync::TransactionReceipt = +let pending_payment = zk_wallet.transfer(payment_request, None).await.unwrap(); ``` +This will send the transaction to the node but the transaction will not be mined until we `await` on it. That will resolve to a `TransactionReceipt` confirming that the transfer went fine. + +```rust +let payment_response: zksync::TransactionReceipt = pending_payment.await.unwrap().unwrap(); +``` + #### Checking zkSync account balance ```rust diff --git a/examples/simple_payment/main.rs b/examples/simple_payment/main.rs index b608f48..4b2f91e 100644 --- a/examples/simple_payment/main.rs +++ b/examples/simple_payment/main.rs @@ -69,8 +69,10 @@ async fn main() { provider.get_balance(args.to, None).await.unwrap() ); - let payment_response: TransactionReceipt = - zk_wallet.transfer(payment_request, None).await.unwrap(); + let pending_payment_transaction = zk_wallet.transfer(payment_request, None).await.unwrap(); + + /* Waiting for the payment transaction */ + let payment_response: TransactionReceipt = pending_payment_transaction.await.unwrap().unwrap(); log::info!("{:?}", payment_response); log::debug!( diff --git a/src/zks_provider/mod.rs b/src/zks_provider/mod.rs index e55ca5f..00db2a3 100644 --- a/src/zks_provider/mod.rs +++ b/src/zks_provider/mod.rs @@ -16,6 +16,7 @@ use ethers::{ U64, }, }; +use ethers_contract::providers::PendingTransaction; use serde::Serialize; use serde_json::json; use std::{collections::HashMap, fmt::Debug, time::Duration}; @@ -39,6 +40,9 @@ use self::types::{ /// https://era.zksync.io/docs/api/api.html#zksync-era-json-rpc-methods #[async_trait] pub trait ZKSProvider { + type Provider: JsonRpcClient; + type ZKProvider: JsonRpcClient; + async fn zk_estimate_gas(&self, transaction: T) -> Result where T: Debug + Serialize + Send + Sync; @@ -191,7 +195,7 @@ pub trait ZKSProvider { function_signature: &str, function_parameters: Option>, overrides: Option, - ) -> Result<(Vec, H256), ProviderError> + ) -> Result, ProviderError> where D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync; @@ -202,7 +206,7 @@ pub trait ZKSProvider { function_signature: &str, function_parameters: Option>, overrides: Option, - ) -> Result<(Vec, H256), ProviderError> + ) -> Result, ProviderError> where D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync; @@ -227,6 +231,9 @@ pub trait ZKSProvider { #[async_trait] impl ZKSProvider for SignerMiddleware { + type Provider = ::Provider; + type ZKProvider = ::ZKProvider; + async fn zk_estimate_gas(&self, transaction: T) -> Result where T: Debug + Serialize + Send + Sync, @@ -401,7 +408,7 @@ impl ZKSProvider for SignerMiddleware>, overrides: Option, - ) -> Result<(Vec, H256), ProviderError> + ) -> Result, ProviderError> where D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync, { @@ -423,7 +430,7 @@ impl ZKSProvider for SignerMiddleware>, _overrides: Option, - ) -> Result<(Vec, H256), ProviderError> + ) -> Result, ProviderError> where D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync, { @@ -436,18 +443,9 @@ impl ZKSProvider for SignerMiddleware( @@ -486,6 +484,9 @@ impl ZKSProvider for SignerMiddleware ZKSProvider for Provider

{ + type Provider = P; + type ZKProvider = P; + async fn zk_estimate_gas(&self, transaction: T) -> Result where T: Debug + Serialize + Send + Sync, @@ -751,7 +752,7 @@ impl ZKSProvider for Provider

{ function_signature: &str, function_parameters: Option>, overrides: Option, - ) -> Result<(Vec, H256), ProviderError> + ) -> Result, ProviderError> where D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync, { @@ -810,22 +811,12 @@ impl ZKSProvider for Provider

{ send_request = send_request.custom_data(Eip712Meta::new().custom_signature(signature.to_vec())); - let pending_transaction = self - .send_raw_transaction( - [&[EIP712_TX_TYPE], &*send_request.rlp_unsigned()] - .concat() - .into(), - ) - .await?; - - let transaction_receipt = pending_transaction - .await? - .ok_or(ProviderError::CustomError( - "no transaction receipt".to_owned(), - ))?; - - // TODO: decode function output. - Ok((Vec::new(), transaction_receipt.transaction_hash)) + self.send_raw_transaction( + [&[EIP712_TX_TYPE], &*send_request.rlp_unsigned()] + .concat() + .into(), + ) + .await } async fn send( @@ -835,7 +826,7 @@ impl ZKSProvider for Provider

{ function_signature: &str, function_parameters: Option>, _overrides: Option, - ) -> Result<(Vec, H256), ProviderError> + ) -> Result, ProviderError> where D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync, { @@ -848,10 +839,7 @@ impl ZKSProvider for Provider

{ _overrides, ) .await?; - let pending_transaction = self.send_transaction(tx, None).await?; - let transaction_hash = pending_transaction.tx_hash(); - - Ok((Vec::new(), transaction_hash)) + self.send_transaction(tx, None).await } async fn wait_for_finalize( @@ -1355,6 +1343,9 @@ mod tests { .transfer(transfer_request, None) .await .unwrap() + .await + .unwrap() + .unwrap() .transaction_hash; let invalid_transaction_hash: H256 = "0x84472204e445cb3cd5f3ce5e23abcc2892cda5e61b35855a7f0bb1562a6e30e7" @@ -1748,6 +1739,9 @@ mod tests { .transfer(transfer_request, None) .await .unwrap() + .await + .unwrap() + .unwrap() .transaction_hash; let invalid_transaction_hash: H256 = "0x84472204e445cb3cd5f3ce5e23abcc2892cda5e61b35855a7f0bb1562a6e30e7" @@ -1816,6 +1810,9 @@ mod tests { None, ) .await + .unwrap() + .await + .unwrap() .unwrap(); let set_value = ZKSProvider::call(&era_provider, &call_request) .await @@ -1835,6 +1832,9 @@ mod tests { None, ) .await + .unwrap() + .await + .unwrap() .unwrap(); let incremented_value = ZKSProvider::call(&era_provider, &call_request) .await diff --git a/src/zks_wallet/wallet.rs b/src/zks_wallet/wallet.rs index 604f5ef..86e544d 100644 --- a/src/zks_wallet/wallet.rs +++ b/src/zks_wallet/wallet.rs @@ -26,6 +26,7 @@ use ethers::{ Signature, TransactionReceipt, H160, H256, U256, }, }; +use ethers_contract::providers::PendingTransaction; use serde_json::Value; use std::{fs::File, io::BufReader, path::PathBuf, str::FromStr, sync::Arc}; @@ -154,7 +155,7 @@ where request: TransferRequest, // TODO: Support multiple-token transfers. _token: Option

, - ) -> Result> + ) -> Result::Provider>, ZKSWalletError> where M: ZKSProvider, { @@ -170,12 +171,7 @@ where // TODO: add block as an override. let pending_transaction = era_provider.send_transaction(transaction, None).await?; - - pending_transaction - .await? - .ok_or(ZKSWalletError::CustomError( - "no transaction receipt".to_owned(), - )) + Ok(pending_transaction) } pub async fn transfer_eip712( @@ -183,7 +179,7 @@ where request: &TransferRequest, // TODO: Support multiple-token transfers. _token: Option
, - ) -> Result> + ) -> Result::Provider>, ZKSWalletError> where M: ZKSProvider, { @@ -407,22 +403,13 @@ where .send_transaction_eip712(&self.l2_wallet, request.clone()) .await?; - let tx_receipt = era_provider - .get_transaction_receipt(response.1) - .await? - .ok_or(ZKSWalletError::CustomError( - "No transaction receipt for withdraw".to_owned(), - ))?; - - Ok(era_provider - .wait_for_finalize(tx_receipt, None, None) - .await?) + response.map_err(|e| ZKSWalletError::CustomError(format!("Error calling withdraw: {e}"))) } pub async fn finalize_withdraw( &self, tx_hash: H256, - ) -> Result> + ) -> Result::Provider>, ZKSWalletError> where M: ZKSProvider, { @@ -533,14 +520,10 @@ where Some(parameters.into()), None, ) - .await?; - - eth_provider - .get_transaction_receipt(response.1) - .await? - .ok_or(ZKSWalletError::CustomError( - "No transaction receipt for finalize withdraw".to_owned(), - )) + .await; + response.map_err(|e| { + ZKSWalletError::CustomError(format!("Error calling finalizeWithdrawal: {e}")) + }) } } @@ -589,9 +572,13 @@ mod zks_signer_tests { println!("Sender balance before: {sender_balance_before}"); println!("Receiver balance before: {receiver_balance_before}"); - let transfer_request = TransferRequest::with(amount_to_transfer, receiver_address) - .from(zk_wallet.l2_address()); - let receipt = zk_wallet.transfer(transfer_request, None).await.unwrap(); + let receipt = zk_wallet + .transfer(receiver_address, amount_to_transfer, None) + .await + .unwrap() + .await + .unwrap() + .unwrap(); assert_eq!(receipt.from, zk_wallet.l2_address()); assert_eq!(receipt.to.unwrap(), receiver_address); @@ -748,6 +735,9 @@ mod zks_signer_tests { let receipt = zk_wallet .transfer_eip712(&transfer_request, None) .await + .unwrap() + .await + .unwrap() .unwrap(); assert_eq!(receipt.from, zk_wallet.l2_address()); @@ -893,8 +883,19 @@ mod zks_signer_tests { // Withdraw let amount_to_withdraw: U256 = parse_units(1_u8, "ether").unwrap().into(); - let withdraw_request = WithdrawRequest::with(amount_to_withdraw).to(zk_wallet.l1_address()); - let tx_receipt = zk_wallet.withdraw(&withdraw_request).await.unwrap(); + let tx_receipt = zk_wallet + .withdraw(amount_to_withdraw, zk_wallet.l1_address()) + .await + .unwrap() + .await + .unwrap() + .unwrap(); + let tx_receipt = zk_wallet + .get_era_provider() + .unwrap() + .wait_for_finalize(tx_receipt.clone(), None, None) + .await + .unwrap(); assert_eq!( 1, tx_receipt.status.unwrap().as_u64(), @@ -921,6 +922,9 @@ mod zks_signer_tests { let tx_finalize_receipt = zk_wallet .finalize_withdraw(tx_receipt.transaction_hash) .await + .unwrap() + .await + .unwrap() .unwrap(); println!( @@ -990,9 +994,19 @@ mod zks_signer_tests { // Withdraw let amount_to_withdraw: U256 = parse_units(1_u8, "ether").unwrap().into(); - let withdraw_request = WithdrawRequest::with(amount_to_withdraw).to(zk_wallet.l1_address()); - let tx_receipt = zk_wallet.withdraw(&withdraw_request).await.unwrap(); - + let tx_receipt = zk_wallet + .withdraw(amount_to_withdraw, zk_wallet.l1_address()) + .await + .unwrap() + .await + .unwrap() + .unwrap(); + let tx_receipt = zk_wallet + .get_era_provider() + .unwrap() + .wait_for_finalize(tx_receipt, None, None) + .await + .unwrap(); assert_eq!( 1, tx_receipt.status.unwrap().as_u64(), @@ -1019,6 +1033,9 @@ mod zks_signer_tests { let tx_finalize_receipt = zk_wallet .finalize_withdraw(tx_receipt.transaction_hash) .await + .unwrap() + .await + .unwrap() .unwrap(); println!(