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        NetworkRepository, PluginRepositoryTrait, RelayerRepository, Repository,
17        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>(
35    transaction_id: String,
36    state: &ThinDataAppState<J, RR, TR, NR, NFR, SR, TCR, PR>,
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{
48    state
49        .transaction_repository
50        .get_by_id(transaction_id)
51        .await
52        .map_err(|e| e.into())
53}
54
55/// Creates a relayer network transaction instance based on the relayer ID.
56///
57/// # Arguments
58///
59/// * `relayer_id` - A `String` representing the ID of the relayer.
60/// * `state` - A reference to the application state, wrapped in `ThinData`.
61///
62/// # Returns
63///
64/// A `Result` containing a `NetworkTransaction` if successful, or an `ApiError` if an error occurs.
65pub async fn get_relayer_transaction(
66    relayer_id: String,
67    state: &ThinData<DefaultAppState>,
68) -> Result<NetworkTransaction, ApiError> {
69    let relayer_model = get_relayer_by_id(relayer_id, state).await?;
70    let signer_model = state
71        .signer_repository
72        .get_by_id(relayer_model.signer_id.clone())
73        .await?;
74
75    RelayerTransactionFactory::create_transaction(
76        relayer_model,
77        signer_model,
78        state.relayer_repository(),
79        state.network_repository(),
80        state.transaction_repository(),
81        state.transaction_counter_store(),
82        state.job_producer(),
83    )
84    .await
85    .map_err(|e| e.into())
86}
87
88/// Creates a relayer network transaction using a relayer model.
89///
90/// # Arguments
91///
92/// * `relayer_model` - A `RelayerRepoModel` representing the relayer.
93/// * `state` - A reference to the application state, wrapped in `ThinData`.
94///
95/// # Returns
96///
97/// A `Result` containing a `NetworkTransaction` if successful, or an `ApiError` if an error occurs.
98pub async fn get_relayer_transaction_by_model(
99    relayer_model: RelayerRepoModel,
100    state: &ThinData<DefaultAppState>,
101) -> Result<NetworkTransaction, ApiError> {
102    let signer_model = state
103        .signer_repository
104        .get_by_id(relayer_model.signer_id.clone())
105        .await?;
106
107    RelayerTransactionFactory::create_transaction(
108        relayer_model,
109        signer_model,
110        state.relayer_repository(),
111        state.network_repository(),
112        state.transaction_repository(),
113        state.transaction_counter_store(),
114        state.job_producer(),
115    )
116    .await
117    .map_err(|e| e.into())
118}
119
120/// Returns an error indicating that Solana relayers are not supported.
121///
122/// # Returns
123///
124/// A `Result` that always contains a `TransactionError::NotSupported` error.
125pub fn solana_not_supported_transaction<T>() -> Result<T, TransactionError> {
126    Err(TransactionError::NotSupported(
127        "Endpoint is not supported for Solana relayers".to_string(),
128    ))
129}