Skip to content

Commit

Permalink
feat: l1 to l2 eth deposit (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
vasyl-ivanchuk authored Dec 11, 2024
1 parent d724465 commit 68bf194
Show file tree
Hide file tree
Showing 7 changed files with 405 additions and 13 deletions.
61 changes: 61 additions & 0 deletions examples/deposit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
use alloy::{
network::EthereumWallet,
primitives::{address, U256},
providers::ProviderBuilder,
signers::local::PrivateKeySigner,
};
use alloy_zksync::{
provider::{zksync_provider, ZksyncProviderWithWallet, ETHER_L1_ADDRESS},
wallet::ZksyncWallet,
};
use anyhow::Result;

#[tokio::main]
async fn main() -> Result<()> {
// standard RPC urls for the L1 and L2 local nodes spun up by ZKSync CLI:
// More general info on the local setup can be found here:
// https://docs.zksync.io/zksync-era/tooling/local-setup/dockerized-l1-l2-nodes
// and how to spin it up locally:
// https://docs.zksync.io/zksync-era/tooling/zksync-cli/running-a-node
let l1_rpc_url = "http://127.0.0.1:8545".parse()?;
let l2_rpc_url = "http://127.0.0.1:3050".parse()?;
// one of the test rich wallets created by the local setup
// https://github.com/matter-labs/local-setup/blob/main/rich-wallets.json
let signer: PrivateKeySigner =
"0x7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110"
.parse()
.expect("should parse private key");
let wallet = EthereumWallet::from(signer.clone());

let l1_provider = ProviderBuilder::new()
.with_recommended_fillers()
.wallet(wallet)
.on_http(l1_rpc_url);

let zksync_wallet: ZksyncWallet = ZksyncWallet::from(signer.clone());
let zksync_provider = zksync_provider()
.with_recommended_fillers()
.wallet(zksync_wallet)
.on_http(l2_rpc_url);

// use another test rich wallet as a receiver
// https://github.com/matter-labs/local-setup/blob/main/rich-wallets.json
let receiver = address!("a61464658AfeAf65CccaaFD3a512b69A83B77618");
// 0.00007 ETH
let deposit_amount = U256::from(70000000000000_u64);
let l1_token_address = ETHER_L1_ADDRESS;
let deposit_l1_receipt = zksync_provider
.deposit(l1_token_address, receiver, deposit_amount, &l1_provider)
.await
.unwrap();

let deposit_l2_receipt = deposit_l1_receipt
.get_l2_tx()?
.with_required_confirmations(1)
.with_timeout(Some(std::time::Duration::from_secs(60 * 5)))
.get_receipt()
.await?;

println!("L2 deposit transaction receipt: {:#?}", deposit_l2_receipt);
Ok(())
}
57 changes: 57 additions & 0 deletions src/contracts/l1/bridge_hub.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
alloy::sol! {
#[allow(missing_docs)]
struct L2TransactionRequestDirect {
uint256 chainId;
uint256 mintValue;
address l2Contract;
uint256 l2Value;
bytes l2Calldata;
uint256 l2GasLimit;
uint256 l2GasPerPubdataByteLimit;
bytes[] factoryDeps;
address refundRecipient;
}

#[allow(missing_docs)]
struct L2CanonicalTransaction {
uint256 txType;
uint256 from;
uint256 to;
uint256 gasLimit;
uint256 gasPerPubdataByteLimit;
uint256 maxFeePerGas;
uint256 maxPriorityFeePerGas;
uint256 paymaster;
uint256 nonce;
uint256 value;
uint256[4] reserved;
bytes data;
bytes signature;
uint256[] factoryDeps;
bytes paymasterInput;
bytes reservedDynamic;
}

#[allow(missing_docs)]
#[sol(rpc)]
contract Bridgehub {
function requestL2TransactionDirect(
L2TransactionRequestDirect memory request
) external payable returns (bytes32 canonicalTxHash);

function l2TransactionBaseCost(
uint256 _chainId,
uint256 _gasPrice,
uint256 _l2GasLimit,
uint256 _l2GasPerPubdataByteLimit
) external view returns (uint256);

event NewPriorityRequest(
uint256 txId,
bytes32 txHash,
uint64 expirationTimestamp,
L2CanonicalTransaction transaction,
bytes[] factoryDeps
);
}
}
1 change: 1 addition & 0 deletions src/contracts/l1/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod bridge_hub;
1 change: 1 addition & 0 deletions src/contracts/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
pub mod l1;
pub mod l2;
4 changes: 2 additions & 2 deletions src/network/transaction_request/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ impl TransactionRequest {
self
}

/// Builder-pattern method for building a ZKsync EIP-712 create2 tranasaction.
/// Builder-pattern method for building a ZKsync EIP-712 create2 transaction.
pub fn with_create2_params(
self,
salt: B256,
Expand All @@ -140,7 +140,7 @@ impl TransactionRequest {
.with_factory_deps(factory_deps))
}

/// Builder-pattern method for building a ZKsync EIP-712 create tranasaction.
/// Builder-pattern method for building a ZKsync EIP-712 create transaction.
pub fn with_create_params(
self,
code: Vec<u8>,
Expand Down
Loading

0 comments on commit 68bf194

Please sign in to comment.