Skip to content

Commit

Permalink
move TxSender directly into publisher
Browse files Browse the repository at this point in the history
  • Loading branch information
nategraf committed May 29, 2024
1 parent 3df1d0e commit 9a17584
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 66 deletions.
43 changes: 42 additions & 1 deletion apps/src/bin/publisher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@
use alloy_primitives::U256;
use alloy_sol_types::{sol, SolInterface, SolValue};
use anyhow::{Context, Result};
use apps::TxSender;
use clap::Parser;
use ethers::prelude::*;
use methods::IS_EVEN_ELF;
use risc0_ethereum_contracts::groth16;
use risc0_zkvm::{default_prover, ExecutorEnv, ProverOpts, VerifierContext};
Expand All @@ -32,6 +32,47 @@ sol! {
}
}

/// Wrapper of a `SignerMiddleware` client to send transactions to the given
/// contract's `Address`.
pub struct TxSender {
chain_id: u64,
client: SignerMiddleware<Provider<Http>, Wallet<k256::ecdsa::SigningKey>>,
contract: Address,
}

impl TxSender {
/// Creates a new `TxSender`.
pub fn new(chain_id: u64, rpc_url: &str, private_key: &str, contract: &str) -> Result<Self> {
let provider = Provider::<Http>::try_from(rpc_url)?;
let wallet: LocalWallet = private_key.parse::<LocalWallet>()?.with_chain_id(chain_id);
let client = SignerMiddleware::new(provider.clone(), wallet.clone());
let contract = contract.parse::<Address>()?;

Ok(TxSender {
chain_id,
client,
contract,
})
}

/// Send a transaction with the given calldata.
pub async fn send(&self, calldata: Vec<u8>) -> Result<Option<TransactionReceipt>> {
let tx = TransactionRequest::new()
.chain_id(self.chain_id)
.to(self.contract)
.from(self.client.address())
.data(calldata);

log::info!("Transaction request: {:?}", &tx);

let tx = self.client.send_transaction(tx, None).await?.await?;

log::info!("Transaction receipt: {:?}", &tx);

Ok(tx)
}
}

/// Arguments of the publisher CLI.
#[derive(Parser, Debug)]
#[clap(author, version, about, long_about = None)]
Expand Down
65 changes: 0 additions & 65 deletions apps/src/lib.rs

This file was deleted.

0 comments on commit 9a17584

Please sign in to comment.