openzeppelin_relayer/domain/transaction/mod.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
//! This module defines the core transaction handling logic for different blockchain networks,
//! including Ethereum (EVM), Solana, and Stellar. It provides a unified interface for preparing,
//! submitting, handling, canceling, replacing, signing, and validating transactions across these
//! networks. The module also includes a factory for creating network-specific transaction handlers
//! based on relayer and repository information.
//!
//! The main components of this module are:
//! - `Transaction` trait: Defines the operations for handling transactions.
//! - `NetworkTransaction` enum: Represents a transaction for different network types.
//! - `RelayerTransactionFactory`: A factory for creating network transactions.
//!
//! The module leverages async traits to handle asynchronous operations and uses the `eyre` crate
//! for error handling.
use crate::{
jobs::JobProducer,
models::{
EvmNetwork, NetworkType, RelayerRepoModel, SignerRepoModel, TransactionError,
TransactionRepoModel,
},
repositories::{
InMemoryRelayerRepository, InMemoryTransactionCounter, InMemoryTransactionRepository,
RelayerRepositoryStorage,
},
services::{
get_network_extra_fee_calculator_service, get_solana_network_provider, EvmGasPriceService,
EvmProvider, EvmSignerFactory,
},
};
use async_trait::async_trait;
use eyre::Result;
#[cfg(test)]
use mockall::automock;
use std::sync::Arc;
mod evm;
mod solana;
mod stellar;
mod util;
pub use evm::*;
pub use solana::*;
pub use stellar::*;
pub use util::*;
/// A trait that defines the operations for handling transactions across different networks.
#[cfg_attr(test, automock)]
#[async_trait]
#[allow(dead_code)]
pub trait Transaction {
/// Prepares a transaction for submission.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be prepared.
///
/// # Returns
///
/// A `Result` containing the prepared `TransactionRepoModel` or a `TransactionError`.
async fn prepare_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError>;
/// Submits a transaction to the network.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be submitted.
///
/// # Returns
///
/// A `Result` containing the submitted `TransactionRepoModel` or a `TransactionError`.
async fn submit_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError>;
/// Resubmits a transaction with updated parameters.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be resubmitted.
///
/// # Returns
///
/// A `Result` containing the resubmitted `TransactionRepoModel` or a `TransactionError`.
async fn resubmit_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError>;
/// Handles the status of a transaction.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction whose status is to be
/// handled.
///
/// # Returns
///
/// A `Result` containing the updated `TransactionRepoModel` or a `TransactionError`.
async fn handle_transaction_status(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError>;
/// Cancels a transaction.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be canceled.
///
/// # Returns
///
/// A `Result` containing the canceled `TransactionRepoModel` or a `TransactionError`.
async fn cancel_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError>;
/// Replaces a transaction with a new one.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be replaced.
///
/// # Returns
///
/// A `Result` containing the new `TransactionRepoModel` or a `TransactionError`.
async fn replace_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError>;
/// Signs a transaction.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be signed.
///
/// # Returns
///
/// A `Result` containing the signed `TransactionRepoModel` or a `TransactionError`.
async fn sign_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError>;
/// Validates a transaction.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be validated.
///
/// # Returns
///
/// A `Result` containing a boolean indicating the validity of the transaction or a
/// `TransactionError`.
async fn validate_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<bool, TransactionError>;
}
/// An enum representing a transaction for different network types.
pub enum NetworkTransaction {
Evm(Box<DefaultEvmTransaction>),
Solana(SolanaRelayerTransaction),
Stellar(StellarRelayerTransaction),
}
#[async_trait]
impl Transaction for NetworkTransaction {
/// Prepares a transaction for submission based on the network type.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be prepared.
///
/// # Returns
///
/// A `Result` containing the prepared `TransactionRepoModel` or a `TransactionError`.
async fn prepare_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError> {
match self {
NetworkTransaction::Evm(relayer) => relayer.prepare_transaction(tx).await,
NetworkTransaction::Solana(relayer) => relayer.prepare_transaction(tx).await,
NetworkTransaction::Stellar(relayer) => relayer.prepare_transaction(tx).await,
}
}
/// Submits a transaction to the network based on the network type.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be submitted.
///
/// # Returns
///
/// A `Result` containing the submitted `TransactionRepoModel` or a `TransactionError`.
async fn submit_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError> {
match self {
NetworkTransaction::Evm(relayer) => relayer.submit_transaction(tx).await,
NetworkTransaction::Solana(relayer) => relayer.submit_transaction(tx).await,
NetworkTransaction::Stellar(relayer) => relayer.submit_transaction(tx).await,
}
}
/// Resubmits a transaction with updated parameters based on the network type.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be resubmitted.
///
/// # Returns
///
/// A `Result` containing the resubmitted `TransactionRepoModel` or a `TransactionError`.
async fn resubmit_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError> {
match self {
NetworkTransaction::Evm(relayer) => relayer.resubmit_transaction(tx).await,
NetworkTransaction::Solana(relayer) => relayer.resubmit_transaction(tx).await,
NetworkTransaction::Stellar(relayer) => relayer.resubmit_transaction(tx).await,
}
}
/// Handles the status of a transaction based on the network type.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction whose status is to be
/// handled.
///
/// # Returns
///
/// A `Result` containing the updated `TransactionRepoModel` or a `TransactionError`.
async fn handle_transaction_status(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError> {
match self {
NetworkTransaction::Evm(relayer) => relayer.handle_transaction_status(tx).await,
NetworkTransaction::Solana(relayer) => relayer.handle_transaction_status(tx).await,
NetworkTransaction::Stellar(relayer) => relayer.handle_transaction_status(tx).await,
}
}
/// Cancels a transaction based on the network type.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be canceled.
///
/// # Returns
///
/// A `Result` containing the canceled `TransactionRepoModel` or a `TransactionError`.
async fn cancel_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError> {
match self {
NetworkTransaction::Evm(relayer) => relayer.cancel_transaction(tx).await,
NetworkTransaction::Solana(_) => solana_not_supported_transaction(),
NetworkTransaction::Stellar(relayer) => relayer.cancel_transaction(tx).await,
}
}
/// Replaces a transaction with a new one based on the network type.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be replaced.
///
/// # Returns
///
/// A `Result` containing the new `TransactionRepoModel` or a `TransactionError`.
async fn replace_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError> {
match self {
NetworkTransaction::Evm(relayer) => relayer.replace_transaction(tx).await,
NetworkTransaction::Solana(_) => solana_not_supported_transaction(),
NetworkTransaction::Stellar(relayer) => relayer.replace_transaction(tx).await,
}
}
/// Signs a transaction based on the network type.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be signed.
///
/// # Returns
///
/// A `Result` containing the signed `TransactionRepoModel` or a `TransactionError`.
async fn sign_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<TransactionRepoModel, TransactionError> {
match self {
NetworkTransaction::Evm(relayer) => relayer.sign_transaction(tx).await,
NetworkTransaction::Solana(relayer) => relayer.sign_transaction(tx).await,
NetworkTransaction::Stellar(relayer) => relayer.sign_transaction(tx).await,
}
}
/// Validates a transaction based on the network type.
///
/// # Arguments
///
/// * `tx` - A `TransactionRepoModel` representing the transaction to be validated.
///
/// # Returns
///
/// A `Result` containing a boolean indicating the validity of the transaction or a
/// `TransactionError`.
async fn validate_transaction(
&self,
tx: TransactionRepoModel,
) -> Result<bool, TransactionError> {
match self {
NetworkTransaction::Evm(relayer) => relayer.validate_transaction(tx).await,
NetworkTransaction::Solana(relayer) => relayer.validate_transaction(tx).await,
NetworkTransaction::Stellar(relayer) => relayer.validate_transaction(tx).await,
}
}
}
/// A trait for creating network transactions.
#[allow(dead_code)]
pub trait RelayerTransactionFactoryTrait {
/// Creates a network transaction based on the relayer and repository information.
///
/// # Arguments
///
/// * `relayer` - A `RelayerRepoModel` representing the relayer.
/// * `relayer_repository` - An `Arc` to the `RelayerRepositoryStorage`.
/// * `transaction_repository` - An `Arc` to the `InMemoryTransactionRepository`.
/// * `job_producer` - An `Arc` to the `JobProducer`.
///
/// # Returns
///
/// A `Result` containing the created `NetworkTransaction` or a `TransactionError`.
fn create_transaction(
relayer: RelayerRepoModel,
relayer_repository: Arc<RelayerRepositoryStorage<InMemoryRelayerRepository>>,
transaction_repository: Arc<InMemoryTransactionRepository>,
job_producer: Arc<JobProducer>,
) -> Result<NetworkTransaction, TransactionError>;
}
/// A factory for creating relayer transactions.
pub struct RelayerTransactionFactory;
#[allow(dead_code)]
impl RelayerTransactionFactory {
/// Creates a network transaction based on the relayer, signer, and repository information.
///
/// # Arguments
///
/// * `relayer` - A `RelayerRepoModel` representing the relayer.
/// * `signer` - A `SignerRepoModel` representing the signer.
/// * `relayer_repository` - An `Arc` to the `RelayerRepositoryStorage`.
/// * `transaction_repository` - An `Arc` to the `InMemoryTransactionRepository`.
/// * `transaction_counter_store` - An `Arc` to the `InMemoryTransactionCounter`.
/// * `job_producer` - An `Arc` to the `JobProducer`.
///
/// # Returns
///
/// A `Result` containing the created `NetworkTransaction` or a `TransactionError`.
pub fn create_transaction(
relayer: RelayerRepoModel,
signer: SignerRepoModel,
relayer_repository: Arc<RelayerRepositoryStorage<InMemoryRelayerRepository>>,
transaction_repository: Arc<InMemoryTransactionRepository>,
transaction_counter_store: Arc<InMemoryTransactionCounter>,
job_producer: Arc<JobProducer>,
) -> Result<NetworkTransaction, TransactionError> {
match relayer.network_type {
NetworkType::Evm => {
let network = match EvmNetwork::from_network_str(&relayer.network) {
Ok(network) => network,
Err(e) => return Err(TransactionError::NetworkConfiguration(e.to_string())),
};
let rpc_url = relayer
.custom_rpc_urls
.as_ref()
.and_then(|urls| urls.first().cloned())
.or_else(|| {
network
.public_rpc_urls()
.and_then(|urls| urls.first().cloned())
.map(String::from)
})
.ok_or_else(|| {
TransactionError::NetworkConfiguration("No RPC URLs configured".to_string())
})?;
let evm_provider: EvmProvider = EvmProvider::new(&rpc_url)
.map_err(|e| TransactionError::NetworkConfiguration(e.to_string()))?;
let signer_service = EvmSignerFactory::create_evm_signer(&signer)?;
let network_extra_fee_calculator =
get_network_extra_fee_calculator_service(network, evm_provider.clone());
let price_calculator = PriceCalculator::new(
EvmGasPriceService::new(evm_provider.clone(), network),
network_extra_fee_calculator,
);
Ok(NetworkTransaction::Evm(Box::new(
DefaultEvmTransaction::new(
relayer,
evm_provider,
relayer_repository,
transaction_repository,
transaction_counter_store,
job_producer,
price_calculator,
signer_service,
)?,
)))
}
NetworkType::Solana => {
let solana_provider = Arc::new(get_solana_network_provider(
&relayer.network,
relayer.custom_rpc_urls.clone(),
)?);
Ok(NetworkTransaction::Solana(SolanaRelayerTransaction::new(
relayer,
relayer_repository,
solana_provider,
transaction_repository,
job_producer,
)?))
}
NetworkType::Stellar => {
Ok(NetworkTransaction::Stellar(StellarRelayerTransaction::new(
relayer,
relayer_repository,
transaction_repository,
job_producer,
)?))
}
}
}
}