Skip to content

Commit

Permalink
Fix format
Browse files Browse the repository at this point in the history
  • Loading branch information
IAvecilla committed Sep 13, 2024
1 parent 49953de commit f1a82bb
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 16 deletions.
54 changes: 38 additions & 16 deletions examples/paymaster/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use std::str::FromStr;
use std::sync::Arc;
use zksync_web3_rs::core::abi::{Contract, Token};
use zksync_web3_rs::eip712::{Eip712Meta, Eip712TransactionRequest, PaymasterParams};
use zksync_web3_rs::ZKSWallet;
use zksync_web3_rs::zks_provider::ZKSProvider;
use zksync_web3_rs::ZKSWallet;

static ERA_PROVIDER_URL: &str = "http://127.0.0.1:3050";
static PK: &str = "0x27593fea79697e947890ecbecce7901b0008345e5d7259710d0dd5e500d040be";
Expand Down Expand Up @@ -46,24 +46,34 @@ pub struct PaymasterFlow {
}

impl PaymasterFlow {
pub fn new(private_key: String,
paymaster_address: H160,
chain_id: u64,
provider:Provider<Http>) -> Self {
let paymaster_contract = Contract::load(PAYMASTER_ABI.as_bytes()).expect("Failed to load the paymaster ABI");
let paymaster_general_fn = paymaster_contract.function("general").expect("Failed to get the general function");
let wallet = Wallet::from_str(private_key.as_str()).expect("Failed to create wallet from private key");
pub fn new(
private_key: String,
paymaster_address: H160,
chain_id: u64,
provider: Provider<Http>,
) -> Self {
let paymaster_contract =
Contract::load(PAYMASTER_ABI.as_bytes()).expect("Failed to load the paymaster ABI");
let paymaster_general_fn = paymaster_contract
.function("general")
.expect("Failed to get the general function");
let wallet = Wallet::from_str(private_key.as_str())
.expect("Failed to create wallet from private key");
let signer = Wallet::with_chain_id(wallet, chain_id);
let zk_wallet = ZKSWallet::new(signer, None, Some(provider.clone()), None).unwrap();
let era_provider = zk_wallet.get_era_provider().expect("Failed to get era provider from zk wallet");
let paymaster_encoded_input = paymaster_general_fn.encode_input(&[Token::Bytes(vec![])]).expect("Failed to encode paymaster input");

let era_provider = zk_wallet
.get_era_provider()
.expect("Failed to get era provider from zk wallet");
let paymaster_encoded_input = paymaster_general_fn
.encode_input(&[Token::Bytes(vec![])])
.expect("Failed to encode paymaster input");

Self {
paymaster: paymaster_address,
amount: U256::from_dec_str("1").expect("Failed to parse amount"),
paymaster_encoded_input,
zk_wallet,
era_provider
era_provider,
}
}

Expand All @@ -75,12 +85,13 @@ impl PaymasterFlow {
.value(self.amount)
.custom_data(Eip712Meta::new().paymaster_params(PaymasterParams {
paymaster: self.paymaster,
paymaster_input: self.paymaster_encoded_input.clone()
paymaster_input: self.paymaster_encoded_input.clone(),
}))
}

async fn send_transaction(&self) -> anyhow::Result<PendingTransaction<Http>> {
let result = self.era_provider
let result = self
.era_provider
.send_transaction_eip712(&self.zk_wallet.l2_wallet, self.tx_request())
.await?;
Ok(result)
Expand All @@ -92,7 +103,18 @@ async fn main() {
let era_provider = Provider::try_from(ERA_PROVIDER_URL).unwrap();
let paymaster_address = Address::from_str(PAYMASTER_ADDRESS).unwrap();
let chain_id = era_provider.get_chainid().await.unwrap();
let flow = PaymasterFlow::new(PK.to_string(), paymaster_address, chain_id.as_u64(), era_provider.clone());
let tx = flow.send_transaction().await.unwrap().await.unwrap().unwrap();
let flow = PaymasterFlow::new(
PK.to_string(),
paymaster_address,
chain_id.as_u64(),
era_provider.clone(),
);
let tx = flow
.send_transaction()
.await
.unwrap()
.await
.unwrap()
.unwrap();
println!("Transaction sent: {:#?}", tx);
}
1 change: 1 addition & 0 deletions src/zks_provider/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -726,6 +726,7 @@ impl<P: JsonRpcClient> ZKSProvider for Provider<P> {
.sign_typed_data(&signable_data)
.await
.map_err(|e| ProviderError::CustomError(format!("error signing transaction: {e}")))?;
println!("signature: {:#?}", signature);
request = request.custom_data(custom_data.custom_signature(signature.to_vec()));
let encoded_rlp = &*request
.rlp_signed(signature)
Expand Down

0 comments on commit f1a82bb

Please sign in to comment.