openzeppelin_relayer/domain/relayer/
util.rs

1/// This module provides utility functions and structures for managing and interacting
2/// with relayers within the application. It includes functions to retrieve relayers
3/// by ID, construct network relayers, and handle unsupported operations for specific
4/// relayer types.
5///
6/// The primary components of this module are:
7/// - `get_relayer_by_id`: Retrieves a relayer from the repository using its ID.
8/// - `get_network_relayer`: Constructs a network relayer using a relayer ID.
9/// - `get_network_relayer_by_model`: Constructs a network relayer using a relayer model.
10/// - `solana_not_supported`: Returns an error for unsupported Solana relayer operations.
11///
12/// These utilities are essential for the application's relayer management and
13/// interaction with the underlying repositories and factories.
14use crate::{
15    domain::relayer::{RelayerFactory, RelayerFactoryTrait},
16    jobs::JobProducerTrait,
17    models::{
18        ApiError, NetworkRepoModel, NotificationRepoModel, RelayerError, RelayerRepoModel,
19        SignerRepoModel, ThinDataAppState, TransactionRepoModel,
20    },
21    repositories::{
22        ApiKeyRepositoryTrait, NetworkRepository, PluginRepositoryTrait, RelayerRepository,
23        Repository, TransactionCounterTrait, TransactionRepository,
24    },
25};
26
27use super::NetworkRelayer;
28
29/// Retrieves a relayer by its ID from the repository.
30///
31/// # Arguments
32///
33/// * `relayer_id` - A string slice that holds the ID of the relayer.
34/// * `state` - A reference to the application state.
35///
36/// # Returns
37///
38/// * `Result<RelayerRepoModel, ApiError>` - Returns a `RelayerRepoModel` on success, or an
39///   `ApiError` on failure.
40pub async fn get_relayer_by_id<J, RR, TR, NR, NFR, SR, TCR, PR, AKR>(
41    relayer_id: String,
42    state: &ThinDataAppState<J, RR, TR, NR, NFR, SR, TCR, PR, AKR>,
43) -> Result<RelayerRepoModel, ApiError>
44where
45    J: JobProducerTrait + Send + Sync + 'static,
46    RR: RelayerRepository + Repository<RelayerRepoModel, String> + Send + Sync + 'static,
47    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
48    NR: NetworkRepository + Repository<NetworkRepoModel, String> + Send + Sync + 'static,
49    NFR: Repository<NotificationRepoModel, String> + Send + Sync + 'static,
50    SR: Repository<SignerRepoModel, String> + Send + Sync + 'static,
51    TCR: TransactionCounterTrait + Send + Sync + 'static,
52    PR: PluginRepositoryTrait + Send + Sync + 'static,
53    AKR: ApiKeyRepositoryTrait + Send + Sync + 'static,
54{
55    state
56        .relayer_repository
57        .get_by_id(relayer_id)
58        .await
59        .map_err(|e| e.into())
60}
61
62/// Retrieves a network relayer by its ID, constructing it using the relayer and signer models.
63///
64/// # Arguments
65///
66/// * `relayer_id` - A string slice that holds the ID of the relayer.
67/// * `state` - A reference to the application state.
68///
69/// # Returns
70///
71/// * `Result<NetworkRelayer, ApiError>` - Returns a `NetworkRelayer` on success, or an `ApiError`
72///   on failure.
73pub async fn get_network_relayer<J, RR, TR, NR, NFR, SR, TCR, PR, AKR>(
74    relayer_id: String,
75    state: &ThinDataAppState<J, RR, TR, NR, NFR, SR, TCR, PR, AKR>,
76) -> Result<NetworkRelayer<J, TR, RR, NR, TCR>, ApiError>
77where
78    J: JobProducerTrait + Send + Sync + 'static,
79    RR: RelayerRepository + Repository<RelayerRepoModel, String> + Send + Sync + 'static,
80    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
81    NR: NetworkRepository + Repository<NetworkRepoModel, String> + Send + Sync + 'static,
82    NFR: Repository<NotificationRepoModel, String> + Send + Sync + 'static,
83    SR: Repository<SignerRepoModel, String> + Send + Sync + 'static,
84    TCR: TransactionCounterTrait + Send + Sync + 'static,
85    PR: PluginRepositoryTrait + Send + Sync + 'static,
86    AKR: ApiKeyRepositoryTrait + Send + Sync + 'static,
87{
88    let relayer_model = get_relayer_by_id(relayer_id.clone(), state).await?;
89    let signer_model = state
90        .signer_repository
91        .get_by_id(relayer_model.signer_id.clone())
92        .await?;
93
94    RelayerFactory::create_relayer(relayer_model, signer_model, state)
95        .await
96        .map_err(|e| e.into())
97}
98
99/// Constructs a network relayer using a given relayer model.
100///
101/// # Arguments
102///
103/// * `relayer_model` - A `RelayerRepoModel` that holds the relayer data.
104/// * `state` - A reference to the application state.
105///
106/// # Returns
107///
108/// * `Result<NetworkRelayer, ApiError>` - Returns a `NetworkRelayer` on success, or an `ApiError`
109///   on failure.
110pub async fn get_network_relayer_by_model<J, RR, TR, NR, NFR, SR, TCR, PR, AKR>(
111    relayer_model: RelayerRepoModel,
112    state: &ThinDataAppState<J, RR, TR, NR, NFR, SR, TCR, PR, AKR>,
113) -> Result<NetworkRelayer<J, TR, RR, NR, TCR>, ApiError>
114where
115    J: JobProducerTrait + Send + Sync + 'static,
116    RR: RelayerRepository + Repository<RelayerRepoModel, String> + Send + Sync + 'static,
117    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
118    NR: NetworkRepository + Repository<NetworkRepoModel, String> + Send + Sync + 'static,
119    NFR: Repository<NotificationRepoModel, String> + Send + Sync + 'static,
120    SR: Repository<SignerRepoModel, String> + Send + Sync + 'static,
121    TCR: TransactionCounterTrait + Send + Sync + 'static,
122    PR: PluginRepositoryTrait + Send + Sync + 'static,
123    AKR: ApiKeyRepositoryTrait + Send + Sync + 'static,
124{
125    let signer_model = state
126        .signer_repository
127        .get_by_id(relayer_model.signer_id.clone())
128        .await?;
129
130    RelayerFactory::create_relayer(relayer_model, signer_model, state)
131        .await
132        .map_err(|e| e.into())
133}
134
135/// Returns an error indicating that the endpoint is not supported for Solana relayers.
136///
137/// # Returns
138///
139/// * `Result<T, RelayerError>` - Always returns a `RelayerError::NotSupported`.
140pub fn solana_not_supported_relayer<T>() -> Result<T, RelayerError> {
141    Err(RelayerError::NotSupported(
142        "Endpoint is not supported for Solana relayers".to_string(),
143    ))
144}