Skip to content

Commit

Permalink
Merge branch 'main' into improve_api
Browse files Browse the repository at this point in the history
  • Loading branch information
IAvecilla committed Jul 24, 2023
2 parents eef156c + 54c2145 commit be1b6b8
Show file tree
Hide file tree
Showing 4 changed files with 101 additions and 77 deletions.
11 changes: 8 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions examples/simple_payment/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!(
Expand Down
74 changes: 37 additions & 37 deletions src/zks_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand All @@ -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<T>(&self, transaction: T) -> Result<U256, ProviderError>
where
T: Debug + Serialize + Send + Sync;
Expand Down Expand Up @@ -191,7 +195,7 @@ pub trait ZKSProvider {
function_signature: &str,
function_parameters: Option<Vec<String>>,
overrides: Option<Overrides>,
) -> Result<(Vec<Token>, H256), ProviderError>
) -> Result<PendingTransaction<Self::ZKProvider>, ProviderError>
where
D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync;

Expand All @@ -202,7 +206,7 @@ pub trait ZKSProvider {
function_signature: &str,
function_parameters: Option<Vec<String>>,
overrides: Option<Overrides>,
) -> Result<(Vec<Token>, H256), ProviderError>
) -> Result<PendingTransaction<Self::Provider>, ProviderError>
where
D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync;

Expand All @@ -227,6 +231,9 @@ pub trait ZKSProvider {

#[async_trait]
impl<M: Middleware + ZKSProvider, S: Signer> ZKSProvider for SignerMiddleware<M, S> {
type Provider = <M as Middleware>::Provider;
type ZKProvider = <M as ZKSProvider>::ZKProvider;

async fn zk_estimate_gas<T>(&self, transaction: T) -> Result<U256, ProviderError>
where
T: Debug + Serialize + Send + Sync,
Expand Down Expand Up @@ -401,7 +408,7 @@ impl<M: Middleware + ZKSProvider, S: Signer> ZKSProvider for SignerMiddleware<M,
function_signature: &str,
function_parameters: Option<Vec<String>>,
overrides: Option<Overrides>,
) -> Result<(Vec<Token>, H256), ProviderError>
) -> Result<PendingTransaction<Self::ZKProvider>, ProviderError>
where
D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync,
{
Expand All @@ -423,7 +430,7 @@ impl<M: Middleware + ZKSProvider, S: Signer> ZKSProvider for SignerMiddleware<M,
function_signature: &str,
function_parameters: Option<Vec<String>>,
_overrides: Option<Overrides>,
) -> Result<(Vec<Token>, H256), ProviderError>
) -> Result<PendingTransaction<Self::Provider>, ProviderError>
where
D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync,
{
Expand All @@ -436,18 +443,9 @@ impl<M: Middleware + ZKSProvider, S: Signer> ZKSProvider for SignerMiddleware<M,
_overrides,
)
.await?;
let pending_transaction = self
.send_transaction(tx, None)
self.send_transaction(tx, None)
.await
.map_err(|e| ProviderError::CustomError(format!("Error sending transaction: {e:?}")))?;

let transaction_receipt = pending_transaction
.await?
.ok_or(ProviderError::CustomError(
"no transaction receipt".to_owned(),
))?;

Ok((Vec::new(), transaction_receipt.transaction_hash))
.map_err(|e| ProviderError::CustomError(format!("Error sending transaction: {e:?}")))
}

async fn send_transaction_eip712<T, D>(
Expand Down Expand Up @@ -486,6 +484,9 @@ impl<M: Middleware + ZKSProvider, S: Signer> ZKSProvider for SignerMiddleware<M,

#[async_trait]
impl<P: JsonRpcClient> ZKSProvider for Provider<P> {
type Provider = P;
type ZKProvider = P;

async fn zk_estimate_gas<T>(&self, transaction: T) -> Result<U256, ProviderError>
where
T: Debug + Serialize + Send + Sync,
Expand Down Expand Up @@ -751,7 +752,7 @@ impl<P: JsonRpcClient> ZKSProvider for Provider<P> {
function_signature: &str,
function_parameters: Option<Vec<String>>,
overrides: Option<Overrides>,
) -> Result<(Vec<Token>, H256), ProviderError>
) -> Result<PendingTransaction<Self::ZKProvider>, ProviderError>
where
D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync,
{
Expand Down Expand Up @@ -810,22 +811,12 @@ impl<P: JsonRpcClient> ZKSProvider for Provider<P> {
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<D>(
Expand All @@ -835,7 +826,7 @@ impl<P: JsonRpcClient> ZKSProvider for Provider<P> {
function_signature: &str,
function_parameters: Option<Vec<String>>,
_overrides: Option<Overrides>,
) -> Result<(Vec<Token>, H256), ProviderError>
) -> Result<PendingTransaction<Self::Provider>, ProviderError>
where
D: PrehashSigner<(RecoverableSignature, RecoveryId)> + Send + Sync,
{
Expand All @@ -848,10 +839,7 @@ impl<P: JsonRpcClient> ZKSProvider for Provider<P> {
_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(
Expand Down Expand Up @@ -1355,6 +1343,9 @@ mod tests {
.transfer(transfer_request, None)
.await
.unwrap()
.await
.unwrap()
.unwrap()
.transaction_hash;
let invalid_transaction_hash: H256 =
"0x84472204e445cb3cd5f3ce5e23abcc2892cda5e61b35855a7f0bb1562a6e30e7"
Expand Down Expand Up @@ -1748,6 +1739,9 @@ mod tests {
.transfer(transfer_request, None)
.await
.unwrap()
.await
.unwrap()
.unwrap()
.transaction_hash;
let invalid_transaction_hash: H256 =
"0x84472204e445cb3cd5f3ce5e23abcc2892cda5e61b35855a7f0bb1562a6e30e7"
Expand Down Expand Up @@ -1816,6 +1810,9 @@ mod tests {
None,
)
.await
.unwrap()
.await
.unwrap()
.unwrap();
let set_value = ZKSProvider::call(&era_provider, &call_request)
.await
Expand All @@ -1835,6 +1832,9 @@ mod tests {
None,
)
.await
.unwrap()
.await
.unwrap()
.unwrap();
let incremented_value = ZKSProvider::call(&era_provider, &call_request)
.await
Expand Down
Loading

0 comments on commit be1b6b8

Please sign in to comment.