Skip to content

Commit

Permalink
feat: Add blobs execution to example (#210)
Browse files Browse the repository at this point in the history
* add config files

* execute scenario with 20 accounts and 200 deploy tx per account

* rename methods and add transfer_eth and withdraw_eth methods

* add deploy_erc20 scenario

* add mint_erc20 scenario and transfer_erc20 scenario
  • Loading branch information
toni-calvin authored Mar 26, 2024
1 parent ac63f74 commit 42f3f81
Show file tree
Hide file tree
Showing 10 changed files with 201 additions and 23 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ Cargo.lock
!/etc/env/base
!/etc/env/dev.toml
!/etc/env/dev_validium.toml
!/etc/env/main_demo_rollup_blobs.toml
!/etc/env/main_demo_validium_blobs.toml
!/etc/env/dev_validium_docker.toml
!/etc/env/docker.toml
!/etc/env/ext-node.toml
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,10 @@ demo_validium_calldata:
zk && zk clean --all && zk env validium_calldata && zk init --validium-mode --run-observability && zk server

demo_validium_blobs:
zk && zk clean --all && zk env validium_blobs && zk init --validium-mode --run-observability && zk server
zk && zk clean --all && zk env main_demo_validium_blobs && zk init --validium-mode --run-observability && zk server

demo_rollup_calldata:
zk && zk clean --all && zk env rollup_calldata && zk init --run-observability && zk server

demo_rollup_blobs:
zk && zk clean --all && zk env rollup_blobs && zk init --run-observability && zk server
zk && zk clean --all && zk env main_demo_rollup_blobs && zk init --run-observability && zk server
13 changes: 13 additions & 0 deletions etc/env/main_demo_rollup_blobs.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[chain.state_keeper]
transaction_slots=5000
block_commit_deadline_ms=1800000
max_pubdata_per_batch=240000

[eth_sender.gas_adjuster]
internal_enforced_l1_gas_price=45000000000

[eth_sender.sender]
pubdata_sending_mode="Blobs"

[_metadata]
base = ["dev.toml"]
12 changes: 12 additions & 0 deletions etc/env/main_demo_validium_blobs.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[chain.state_keeper]
transaction_slots=5000
block_commit_deadline_ms=1800000

[eth_sender.gas_adjuster]
internal_enforced_l1_gas_price=45000000000

[eth_sender.sender]
pubdata_sending_mode="Blobs"

[_metadata]
base = ["dev_validium.toml"]
2 changes: 1 addition & 1 deletion validium_mode_example/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ In this example we're going to run some transactions.
Once the server is running, run this command in other terminal:

```sh
cargo run --release --bin zksync_full_stack
cargo run --release --bin validium_mode_example
```

This test does the following:
Expand Down
4 changes: 2 additions & 2 deletions validium_mode_example/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
pub static ERA_PROVIDER_URL: &str = "http://127.0.0.1:3050";
pub static PRIVATE_KEY: &str = "7726827caac94a7f9e1b160f7ea819f172f7b6f9d2a97f992c38edeab82d4110";

pub static CONTRACT_BIN: &str = include_str!("../ERC20.bin");
pub static CONTRACT_ABI: &str = include_str!("../ERC20.abi");
pub static ERC20_BIN: &str = include_str!("../ERC20.bin");
pub static ERC20_ABI: &str = include_str!("../ERC20.abi");

pub static L1_URL: &str = "http://localhost:8545";
54 changes: 45 additions & 9 deletions validium_mode_example/src/helpers/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ use zksync_web3_rs::{
providers::{Middleware, Provider},
signers::{LocalWallet, Signer},
zks_provider::ZKSProvider,
zks_wallet::{DeployRequest, DepositRequest},
zks_wallet::{DeployRequest, DepositRequest, TransferRequest, WithdrawRequest},
ZKSWallet,
};

Expand Down Expand Up @@ -61,7 +61,7 @@ pub async fn zks_wallet(
.unwrap()
}

pub async fn deposit(
pub async fn deposit_eth(
from: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
to: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
) {
Expand All @@ -77,10 +77,12 @@ pub async fn deposit(
.expect("Failed to perform deposit transaction");
}

pub async fn deploy(zks_wallet: Arc<ZKSWallet<Provider<Http>, SigningKey>>) -> TransactionReceipt {
pub async fn deploy_erc20(
zks_wallet: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
) -> TransactionReceipt {
// Read both files from disk:
let abi = Abi::load(constants::CONTRACT_ABI.as_bytes()).unwrap();
let contract_bin = hex::decode(constants::CONTRACT_BIN).unwrap().to_vec();
let abi = Abi::load(constants::ERC20_ABI.as_bytes()).unwrap();
let contract_bin = hex::decode(constants::ERC20_BIN).unwrap().to_vec();

// DeployRequest sets the parameters for the constructor call and the deployment transaction.
let request = DeployRequest::with(
Expand All @@ -106,7 +108,7 @@ pub async fn deploy(zks_wallet: Arc<ZKSWallet<Provider<Http>, SigningKey>>) -> T
l2_deploy_tx_receipt
}

pub async fn mint(
pub async fn mint_erc20(
zks_wallet: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
erc20_address: H160,
) -> TransactionReceipt {
Expand Down Expand Up @@ -134,7 +136,24 @@ pub async fn mint(
.unwrap()
}

pub async fn transfer(
pub async fn transfer_eth(
from: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
to: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
) {
println!(
"{}",
format!("Transfer eth in {:?}", to.l2_address()).bright_yellow()
);
let amount = parse_units("11", "ether").unwrap();
let mut request = TransferRequest::new(amount.into());
request.to = to.l2_address();

from.transfer(&request, None)
.await
.expect("Failed to perform transfer transaction");
}

pub async fn transfer_erc20(
zks_wallet: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
erc20_address: H160,
) -> TransactionReceipt {
Expand Down Expand Up @@ -188,6 +207,23 @@ pub async fn wait_for_l2_tx_details(tx_hash: H256) -> TransactionDetails {
}
}

pub async fn withdraw_eth(
from: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
to: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
) {
println!(
"{}",
format!("Withdrawing in {:?}", to.l2_address()).bright_yellow()
);
let amount = parse_units("11", "ether").unwrap();
let mut request = WithdrawRequest::new(amount.into());
request.to = to.l2_address();

from.withdraw(&request)
.await
.expect("Failed to perform withdraw transaction");
}

pub async fn wait_for_tx_receipt(client: Arc<Provider<Http>>, tx_hash: H256) -> TransactionReceipt {
loop {
let receipt = client
Expand Down Expand Up @@ -240,7 +276,7 @@ pub async fn create_funded_account(
main_wallet: Arc<ZKSWallet<Provider<Http>, SigningKey>>,
) -> Arc<ZKSWallet<Provider<Http>, SigningKey>> {
let to = create_account(l1_provider, l2_provider).await;
let _ = deposit(main_wallet, to.clone()).await;
let _ = deposit_eth(main_wallet, to.clone()).await;
to
}

Expand All @@ -256,7 +292,7 @@ pub async fn create_funded_accounts(
let from = main_wallet.clone();
let to = create_account(l1_provider, l2_provider).await;
accounts.push(to.clone());
let _ = deposit(from, to).await;
let _ = deposit_eth(from, to).await;
}
accounts
}
Expand Down
6 changes: 3 additions & 3 deletions validium_mode_example/src/helpers/tx_kind.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ impl TxKind {
.bright_yellow()
);
match self {
TxKind::Deploy => super::deploy(zks_wallet).await,
TxKind::Mint => super::mint(zks_wallet, erc20_address).await,
TxKind::Transfer => super::transfer(zks_wallet, erc20_address).await,
TxKind::Deploy => super::deploy_erc20(zks_wallet).await,
TxKind::Mint => super::mint_erc20(zks_wallet, erc20_address).await,
TxKind::Transfer => super::transfer_erc20(zks_wallet, erc20_address).await,
}
}
}
Expand Down
6 changes: 4 additions & 2 deletions validium_mode_example/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ use validium_mode_example::scenario;

#[tokio::main(flavor = "current_thread")]
async fn main() {
//scenario::run(1, 1, helpers::TxKind::Deploy).await;
scenario::basic().await;
//scenario::run(20, 200, TxKind::Deploy).await;
//scenario::basic().await;
scenario::mint_erc20(5, 5).await;
scenario::transfer_erc20(5, 5).await;
}
121 changes: 117 additions & 4 deletions validium_mode_example/src/scenario.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ pub async fn run(accounts_count: usize, txs_per_account: usize, txs_kind: helper

// In case that the txs_type is not Deploy we need the contract deployed.
println!("{}", "Initial deploy".bright_yellow());
let erc20_address = helpers::deploy(main_wallet.clone())
let erc20_address = helpers::deploy_erc20(main_wallet.clone())
.await
.contract_address
.unwrap();
Expand All @@ -140,6 +140,119 @@ pub async fn run(accounts_count: usize, txs_per_account: usize, txs_kind: helper
}
}

// Each account will deploy txs_per_account ERC20 contracts
pub async fn deploy_erc20(n_accounts: usize, txs_per_account: usize) {
println!("{}", "Running deploy_erc20 scenario\n".bright_red().bold());
let l1_provider = helpers::l1_provider();
let l2_provider = helpers::l2_provider();
let main_wallet = Arc::new(helpers::zks_wallet(&l1_provider, &l2_provider).await);
let accounts = helpers::create_funded_accounts(
n_accounts,
&l1_provider,
&l2_provider,
main_wallet.clone(),
)
.await;

let l2_txs_hashes = helpers::send_transactions(
helpers::TxKind::Deploy,
Default::default(),
accounts,
txs_per_account,
)
.await;

let data = ScenarioData::collect(
l2_txs_hashes,
l1_provider,
l2_provider,
main_wallet.get_era_provider().unwrap(),
)
.await;

for (_batch_number, batch_data) in data.0.iter() {
println!("{batch_data}");
}
}

pub async fn mint_erc20(n_accounts: usize, txs_per_account: usize) {
println!("{}", "Running mint_erc20 scenario\n".bright_red().bold());
let l1_provider = helpers::l1_provider();
let l2_provider = helpers::l2_provider();
let main_wallet = Arc::new(helpers::zks_wallet(&l1_provider, &l2_provider).await);
let accounts = helpers::create_funded_accounts(
n_accounts,
&l1_provider,
&l2_provider,
main_wallet.clone(),
)
.await;

let deploy_receipt = helpers::deploy_erc20(main_wallet.clone()).await;
let erc20_address = deploy_receipt.contract_address.unwrap();

let l2_txs_hashes = helpers::send_transactions(
helpers::TxKind::Mint,
erc20_address,
accounts,
txs_per_account,
)
.await;

let data = ScenarioData::collect(
l2_txs_hashes,
l1_provider,
l2_provider,
main_wallet.get_era_provider().unwrap(),
)
.await;

for (_batch_number, batch_data) in data.0.iter() {
println!("{batch_data}");
}
}

pub async fn transfer_erc20(n_accounts: usize, txs_per_account: usize) {
println!(
"{}",
"Running transfer_erc20 scenario\n".bright_red().bold()
);
let l1_provider = helpers::l1_provider();
let l2_provider = helpers::l2_provider();
let main_wallet = Arc::new(helpers::zks_wallet(&l1_provider, &l2_provider).await);
let accounts = helpers::create_funded_accounts(
n_accounts,
&l1_provider,
&l2_provider,
main_wallet.clone(),
)
.await;

let deploy_receipt = helpers::deploy_erc20(main_wallet.clone()).await;
let erc20_address = deploy_receipt.contract_address.unwrap();
let _ = helpers::mint_erc20(main_wallet.clone(), erc20_address).await;

let l2_txs_hashes = helpers::send_transactions(
helpers::TxKind::Transfer,
erc20_address,
accounts,
txs_per_account,
)
.await;

let data = ScenarioData::collect(
l2_txs_hashes,
l1_provider,
l2_provider,
main_wallet.get_era_provider().unwrap(),
)
.await;

for (_batch_number, batch_data) in data.0.iter() {
println!("{batch_data}");
}
}

pub async fn basic() {
println!(
"{}",
Expand All @@ -153,10 +266,10 @@ pub async fn basic() {
let account =
helpers::create_funded_account(&l1_provider, &l2_provider, main_wallet.clone()).await;

let deploy_receipt = helpers::deploy(account.clone()).await;
let deploy_receipt = helpers::deploy_erc20(account.clone()).await;
let erc20_address = deploy_receipt.contract_address.unwrap();
let mint_receipt = helpers::mint(account.clone(), erc20_address).await;
let transfer_receipt = helpers::transfer(account.clone(), erc20_address).await;
let mint_receipt = helpers::mint_erc20(account.clone(), erc20_address).await;
let transfer_receipt = helpers::transfer_erc20(account.clone(), erc20_address).await;

println!(
"{}",
Expand Down

0 comments on commit 42f3f81

Please sign in to comment.