circles_types/runner.rs
1use alloy_primitives::Address;
2use async_trait::async_trait;
3use serde::{Deserialize, Serialize};
4
5// Re-export from our base types
6use crate::TransactionRequest;
7
8/// Batch transaction runner trait
9/// Allows multiple transactions to be batched and executed atomically
10#[async_trait]
11pub trait BatchRun {
12 /// Transaction receipt type - using generic to avoid viem dependency
13 type TransactionReceipt;
14 /// Error type for batch operations
15 type Error;
16
17 /// Add a transaction to the batch
18 fn add_transaction(&mut self, tx: TransactionRequest);
19
20 /// Execute all batched transactions
21 /// Returns single transaction receipt for the entire batch
22 async fn run(self) -> Result<Self::TransactionReceipt, Self::Error>;
23}
24
25/// Contract runner trait for executing blockchain operations
26/// This is the base trait that all contract runners must implement
27#[async_trait]
28pub trait ContractRunner {
29 /// Public client type - using generic to avoid specific dependencies
30 type PublicClient;
31 /// Transaction receipt type
32 type TransactionReceipt;
33 /// Batch runner type
34 type BatchRunner: BatchRun;
35 /// Error type for operations
36 type Error;
37
38 /// The address of the account (if available)
39 fn address(&self) -> Option<Address>;
40
41 /// The public client for reading blockchain state
42 fn public_client(&self) -> &Self::PublicClient;
43
44 /// Initialize the runner
45 async fn init(&mut self) -> Result<(), Self::Error>;
46
47 /// Estimate gas for a transaction
48 async fn estimate_gas(&self, tx: &TransactionRequest) -> Result<u64, Self::Error>;
49
50 /// Call a contract (read-only)
51 async fn call(&self, tx: &TransactionRequest) -> Result<String, Self::Error>;
52
53 /// Resolve an ENS name to an address
54 async fn resolve_name(&self, name: &str) -> Result<Option<Address>, Self::Error>;
55
56 /// Send one or more transactions
57 /// Safe: batches all transactions atomically and returns single TransactionReceipt
58 async fn send_transaction(
59 &self,
60 txs: Vec<TransactionRequest>,
61 ) -> Result<Self::TransactionReceipt, Self::Error>;
62
63 /// Create a batch transaction runner (if supported)
64 /// This allows multiple transactions to be executed atomically in a single on-chain transaction
65 /// Typically used with Safe multisig or other smart contract wallets
66 ///
67 /// Returns a BatchRun instance for adding transactions and executing them as a batch
68 fn send_batch_transaction(&self) -> Option<Self::BatchRunner>;
69}
70
71/// Configuration for contract runners
72#[derive(Debug, Clone, Serialize, Deserialize)]
73pub struct RunnerConfig {
74 /// RPC endpoint URL
75 pub rpc_url: String,
76 /// Chain ID
77 pub chain_id: u64,
78 /// Default gas limit
79 pub default_gas_limit: Option<u64>,
80 /// Default gas price
81 pub default_gas_price: Option<u64>,
82}