openzeppelin_relayer/domain/transaction/
util.rs

1//! This module provides utility functions for handling transactions within the application.
2//!
3//! It includes functions to retrieve transactions by ID, create relayer transactions, and
4//! handle unsupported operations for specific relayers. The module interacts with various
5//! repositories and factories to perform these operations.
6use actix_web::web::ThinData;
7
8use crate::{
9    domain::get_relayer_by_id,
10    jobs::JobProducerTrait,
11    models::{
12        ApiError, DefaultAppState, NetworkRepoModel, NotificationRepoModel, RelayerRepoModel,
13        SignerRepoModel, ThinDataAppState, TransactionError, TransactionRepoModel,
14    },
15    repositories::{
16        ApiKeyRepositoryTrait, NetworkRepository, PluginRepositoryTrait, RelayerRepository,
17        Repository, TransactionCounterTrait, TransactionRepository,
18    },
19};
20
21use super::{NetworkTransaction, RelayerTransactionFactory};
22
23/// Retrieves a transaction by its ID.
24///
25/// # Arguments
26///
27/// * `transaction_id` - A `String` representing the ID of the transaction to retrieve.
28/// * `state` - A reference to the application state, wrapped in `ThinData`.
29///
30/// # Returns
31///
32/// A `Result` containing a `TransactionRepoModel` if successful, or an `ApiError` if an error
33/// occurs.
34pub async fn get_transaction_by_id<J, RR, TR, NR, NFR, SR, TCR, PR, AKR>(
35    transaction_id: String,
36    state: &ThinDataAppState<J, RR, TR, NR, NFR, SR, TCR, PR, AKR>,
37) -> Result<TransactionRepoModel, ApiError>
38where
39    J: JobProducerTrait + Send + Sync + 'static,
40    RR: RelayerRepository + Repository<RelayerRepoModel, String> + Send + Sync + 'static,
41    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
42    NR: NetworkRepository + Repository<NetworkRepoModel, String> + Send + Sync + 'static,
43    NFR: Repository<NotificationRepoModel, String> + Send + Sync + 'static,
44    SR: Repository<SignerRepoModel, String> + Send + Sync + 'static,
45    TCR: TransactionCounterTrait + Send + Sync + 'static,
46    PR: PluginRepositoryTrait + Send + Sync + 'static,
47    AKR: ApiKeyRepositoryTrait + Send + Sync + 'static,
48{
49    state
50        .transaction_repository
51        .get_by_id(transaction_id)
52        .await
53        .map_err(|e| e.into())
54}
55
56/// Creates a relayer network transaction instance based on the relayer ID.
57///
58/// # Arguments
59///
60/// * `relayer_id` - A `String` representing the ID of the relayer.
61/// * `state` - A reference to the application state, wrapped in `ThinData`.
62///
63/// # Returns
64///
65/// A `Result` containing a `NetworkTransaction` if successful, or an `ApiError` if an error occurs.
66pub async fn get_relayer_transaction(
67    relayer_id: String,
68    state: &ThinData<DefaultAppState>,
69) -> Result<NetworkTransaction, ApiError> {
70    let relayer_model = get_relayer_by_id(relayer_id, state).await?;
71    let signer_model = state
72        .signer_repository
73        .get_by_id(relayer_model.signer_id.clone())
74        .await?;
75
76    RelayerTransactionFactory::create_transaction(
77        relayer_model,
78        signer_model,
79        state.relayer_repository(),
80        state.network_repository(),
81        state.transaction_repository(),
82        state.transaction_counter_store(),
83        state.job_producer(),
84    )
85    .await
86    .map_err(|e| e.into())
87}
88
89/// Creates a relayer network transaction using a relayer model.
90///
91/// # Arguments
92///
93/// * `relayer_model` - A `RelayerRepoModel` representing the relayer.
94/// * `state` - A reference to the application state, wrapped in `ThinData`.
95///
96/// # Returns
97///
98/// A `Result` containing a `NetworkTransaction` if successful, or an `ApiError` if an error occurs.
99pub async fn get_relayer_transaction_by_model(
100    relayer_model: RelayerRepoModel,
101    state: &ThinData<DefaultAppState>,
102) -> Result<NetworkTransaction, ApiError> {
103    let signer_model = state
104        .signer_repository
105        .get_by_id(relayer_model.signer_id.clone())
106        .await?;
107
108    RelayerTransactionFactory::create_transaction(
109        relayer_model,
110        signer_model,
111        state.relayer_repository(),
112        state.network_repository(),
113        state.transaction_repository(),
114        state.transaction_counter_store(),
115        state.job_producer(),
116    )
117    .await
118    .map_err(|e| e.into())
119}
120
121/// Returns an error indicating that Solana relayers are not supported.
122///
123/// # Returns
124///
125/// A `Result` that always contains a `TransactionError::NotSupported` error.
126pub fn solana_not_supported_transaction<T>() -> Result<T, TransactionError> {
127    Err(TransactionError::NotSupported(
128        "Endpoint is not supported for Solana relayers".to_string(),
129    ))
130}