use alloy::providers::{Provider, ProviderBuilder};
use alloy::signers::local::PrivateKeySigner;
use std::str::FromStr;
use std::sync::Arc;
use url::Url;
use crate::types::AtomicMatchApiBundle;
use crate::ExternalMatchClient;
const RPC_URL: &str = env!("RPC_URL");
pub type Wallet = Arc<dyn Provider>;
pub fn build_renegade_client() -> Result<ExternalMatchClient, eyre::Error> {
let api_key = std::env::var("EXTERNAL_MATCH_KEY").unwrap();
let api_secret = std::env::var("EXTERNAL_MATCH_SECRET").unwrap();
let client = ExternalMatchClient::new_sepolia_client(&api_key, &api_secret).unwrap();
Ok(client)
}
pub async fn get_signer() -> Result<Wallet, eyre::Error> {
let url = Url::parse(RPC_URL).unwrap();
let pkey = std::env::var("PKEY").unwrap();
let wallet = PrivateKeySigner::from_str(&pkey).unwrap();
let provider = ProviderBuilder::new().wallet(wallet).on_http(url);
Ok(Arc::new(provider))
}
pub async fn execute_bundle(
wallet: &Wallet,
bundle: AtomicMatchApiBundle,
) -> Result<(), eyre::Error> {
println!("Submitting bundle...\n");
let tx = bundle.settlement_tx.clone();
let hash = wallet.send_transaction(tx).await?.watch().await?;
println!("Successfully submitted transaction: {:#x}", hash);
Ok(())
}