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::JobProducer,
11    models::{ApiError, AppState, RelayerRepoModel, TransactionError, TransactionRepoModel},
12    repositories::Repository,
13};
14
15use super::{NetworkTransaction, RelayerTransactionFactory};
16
17/// Retrieves a transaction by its ID.
18///
19/// # Arguments
20///
21/// * `transaction_id` - A `String` representing the ID of the transaction to retrieve.
22/// * `state` - A reference to the application state, wrapped in `ThinData`.
23///
24/// # Returns
25///
26/// A `Result` containing a `TransactionRepoModel` if successful, or an `ApiError` if an error
27/// occurs.
28pub async fn get_transaction_by_id(
29    transaction_id: String,
30    state: &ThinData<AppState<JobProducer>>,
31) -> Result<TransactionRepoModel, ApiError> {
32    state
33        .transaction_repository
34        .get_by_id(transaction_id)
35        .await
36        .map_err(|e| e.into())
37}
38
39/// Creates a relayer network transaction instance based on the relayer ID.
40///
41/// # Arguments
42///
43/// * `relayer_id` - A `String` representing the ID of the relayer.
44/// * `state` - A reference to the application state, wrapped in `ThinData`.
45///
46/// # Returns
47///
48/// A `Result` containing a `NetworkTransaction` if successful, or an `ApiError` if an error occurs.
49pub async fn get_relayer_transaction(
50    relayer_id: String,
51    state: &ThinData<AppState<JobProducer>>,
52) -> Result<NetworkTransaction, ApiError> {
53    let relayer_model = get_relayer_by_id(relayer_id, state).await?;
54    let signer_model = state
55        .signer_repository
56        .get_by_id(relayer_model.signer_id.clone())
57        .await?;
58
59    RelayerTransactionFactory::create_transaction(
60        relayer_model,
61        signer_model,
62        state.relayer_repository(),
63        state.network_repository(),
64        state.transaction_repository(),
65        state.transaction_counter_store(),
66        state.job_producer(),
67    )
68    .await
69    .map_err(|e| e.into())
70}
71
72/// Creates a relayer network transaction using a relayer model.
73///
74/// # Arguments
75///
76/// * `relayer_model` - A `RelayerRepoModel` representing the relayer.
77/// * `state` - A reference to the application state, wrapped in `ThinData`.
78///
79/// # Returns
80///
81/// A `Result` containing a `NetworkTransaction` if successful, or an `ApiError` if an error occurs.
82pub async fn get_relayer_transaction_by_model(
83    relayer_model: RelayerRepoModel,
84    state: &ThinData<AppState<JobProducer>>,
85) -> Result<NetworkTransaction, ApiError> {
86    let signer_model = state
87        .signer_repository
88        .get_by_id(relayer_model.signer_id.clone())
89        .await?;
90
91    RelayerTransactionFactory::create_transaction(
92        relayer_model,
93        signer_model,
94        state.relayer_repository(),
95        state.network_repository(),
96        state.transaction_repository(),
97        state.transaction_counter_store(),
98        state.job_producer(),
99    )
100    .await
101    .map_err(|e| e.into())
102}
103
104/// Returns an error indicating that Solana relayers are not supported.
105///
106/// # Returns
107///
108/// A `Result` that always contains a `TransactionError::NotSupported` error.
109pub fn solana_not_supported_transaction<T>() -> Result<T, TransactionError> {
110    Err(TransactionError::NotSupported(
111        "Endpoint is not supported for Solana relayers".to_string(),
112    ))
113}