openzeppelin_relayer/domain/transaction/util.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
//! This module provides utility functions for handling transactions within the application.
//!
//! It includes functions to retrieve transactions by ID, create relayer transactions, and
//! handle unsupported operations for specific relayers. The module interacts with various
//! repositories and factories to perform these operations.
use actix_web::web::ThinData;
use crate::{
domain::get_relayer_by_id,
jobs::JobProducer,
models::{ApiError, AppState, RelayerRepoModel, TransactionError, TransactionRepoModel},
repositories::Repository,
};
use super::{NetworkTransaction, RelayerTransactionFactory};
/// Retrieves a transaction by its ID.
///
/// # Arguments
///
/// * `transaction_id` - A `String` representing the ID of the transaction to retrieve.
/// * `state` - A reference to the application state, wrapped in `ThinData`.
///
/// # Returns
///
/// A `Result` containing a `TransactionRepoModel` if successful, or an `ApiError` if an error
/// occurs.
pub async fn get_transaction_by_id(
transaction_id: String,
state: &ThinData<AppState<JobProducer>>,
) -> Result<TransactionRepoModel, ApiError> {
state
.transaction_repository
.get_by_id(transaction_id)
.await
.map_err(|e| e.into())
}
/// Creates a relayer network transaction instance based on the relayer ID.
///
/// # Arguments
///
/// * `relayer_id` - A `String` representing the ID of the relayer.
/// * `state` - A reference to the application state, wrapped in `ThinData`.
///
/// # Returns
///
/// A `Result` containing a `NetworkTransaction` if successful, or an `ApiError` if an error occurs.
pub async fn get_relayer_transaction(
relayer_id: String,
state: &ThinData<AppState<JobProducer>>,
) -> Result<NetworkTransaction, ApiError> {
let relayer_model = get_relayer_by_id(relayer_id, state).await?;
let signer_model = state
.signer_repository
.get_by_id(relayer_model.signer_id.clone())
.await?;
RelayerTransactionFactory::create_transaction(
relayer_model,
signer_model,
state.relayer_repository(),
state.transaction_repository(),
state.transaction_counter_store(),
state.job_producer(),
)
.map_err(|e| e.into())
}
/// Creates a relayer network transaction using a relayer model.
///
/// # Arguments
///
/// * `relayer_model` - A `RelayerRepoModel` representing the relayer.
/// * `state` - A reference to the application state, wrapped in `ThinData`.
///
/// # Returns
///
/// A `Result` containing a `NetworkTransaction` if successful, or an `ApiError` if an error occurs.
pub async fn get_relayer_transaction_by_model(
relayer_model: RelayerRepoModel,
state: &ThinData<AppState<JobProducer>>,
) -> Result<NetworkTransaction, ApiError> {
let signer_model = state
.signer_repository
.get_by_id(relayer_model.signer_id.clone())
.await?;
RelayerTransactionFactory::create_transaction(
relayer_model,
signer_model,
state.relayer_repository(),
state.transaction_repository(),
state.transaction_counter_store(),
state.job_producer(),
)
.map_err(|e| e.into())
}
/// Returns an error indicating that Solana relayers are not supported.
///
/// # Returns
///
/// A `Result` that always contains a `TransactionError::NotSupported` error.
pub fn solana_not_supported_transaction<T>() -> Result<T, TransactionError> {
Err(TransactionError::NotSupported(
"Endpoint is not supported for Solana relayers".to_string(),
))
}