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::{RelayerFactory, RelayerFactoryTrait},
16    jobs::JobProducerTrait,
17    models::{
18        ApiError, NetworkRepoModel, NotificationRepoModel, RelayerError, RelayerRepoModel,
19        SignerRepoModel, ThinDataAppState, TransactionRepoModel,
20    },
21    repositories::{
22        NetworkRepository, PluginRepositoryTrait, RelayerRepository, Repository,
23        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>(
41    relayer_id: String,
42    state: &ThinDataAppState<J, RR, TR, NR, NFR, SR, TCR, PR>,
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{
54    state
55        .relayer_repository
56        .get_by_id(relayer_id)
57        .await
58        .map_err(|e| e.into())
59}
60
61/// Retrieves a network relayer by its ID, constructing it using the relayer and signer models.
62///
63/// # Arguments
64///
65/// * `relayer_id` - A string slice that holds the ID of the relayer.
66/// * `state` - A reference to the application state.
67///
68/// # Returns
69///
70/// * `Result<NetworkRelayer, ApiError>` - Returns a `NetworkRelayer` on success, or an `ApiError`
71///   on failure.
72pub async fn get_network_relayer<J, RR, TR, NR, NFR, SR, TCR, PR>(
73    relayer_id: String,
74    state: &ThinDataAppState<J, RR, TR, NR, NFR, SR, TCR, PR>,
75) -> Result<NetworkRelayer<J, TR, RR, NR, TCR>, ApiError>
76where
77    J: JobProducerTrait + Send + Sync + 'static,
78    RR: RelayerRepository + Repository<RelayerRepoModel, String> + Send + Sync + 'static,
79    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
80    NR: NetworkRepository + Repository<NetworkRepoModel, String> + Send + Sync + 'static,
81    NFR: Repository<NotificationRepoModel, String> + Send + Sync + 'static,
82    SR: Repository<SignerRepoModel, String> + Send + Sync + 'static,
83    TCR: TransactionCounterTrait + Send + Sync + 'static,
84    PR: PluginRepositoryTrait + Send + Sync + 'static,
85{
86    let relayer_model = get_relayer_by_id(relayer_id.clone(), state).await?;
87    let signer_model = state
88        .signer_repository
89        .get_by_id(relayer_model.signer_id.clone())
90        .await?;
91
92    RelayerFactory::create_relayer(relayer_model, signer_model, state)
93        .await
94        .map_err(|e| e.into())
95}
96
97/// Constructs a network relayer using a given relayer model.
98///
99/// # Arguments
100///
101/// * `relayer_model` - A `RelayerRepoModel` that holds the relayer data.
102/// * `state` - A reference to the application state.
103///
104/// # Returns
105///
106/// * `Result<NetworkRelayer, ApiError>` - Returns a `NetworkRelayer` on success, or an `ApiError`
107///   on failure.
108pub async fn get_network_relayer_by_model<J, RR, TR, NR, NFR, SR, TCR, PR>(
109    relayer_model: RelayerRepoModel,
110    state: &ThinDataAppState<J, RR, TR, NR, NFR, SR, TCR, PR>,
111) -> Result<NetworkRelayer<J, TR, RR, NR, TCR>, ApiError>
112where
113    J: JobProducerTrait + Send + Sync + 'static,
114    RR: RelayerRepository + Repository<RelayerRepoModel, String> + Send + Sync + 'static,
115    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
116    NR: NetworkRepository + Repository<NetworkRepoModel, String> + Send + Sync + 'static,
117    NFR: Repository<NotificationRepoModel, String> + Send + Sync + 'static,
118    SR: Repository<SignerRepoModel, String> + Send + Sync + 'static,
119    TCR: TransactionCounterTrait + Send + Sync + 'static,
120    PR: PluginRepositoryTrait + Send + Sync + 'static,
121{
122    let signer_model = state
123        .signer_repository
124        .get_by_id(relayer_model.signer_id.clone())
125        .await?;
126
127    RelayerFactory::create_relayer(relayer_model, signer_model, state)
128        .await
129        .map_err(|e| e.into())
130}
131
132/// Returns an error indicating that the endpoint is not supported for Solana relayers.
133///
134/// # Returns
135///
136/// * `Result<T, RelayerError>` - Always returns a `RelayerError::NotSupported`.
137pub fn solana_not_supported_relayer<T>() -> Result<T, RelayerError> {
138    Err(RelayerError::NotSupported(
139        "Endpoint is not supported for Solana relayers".to_string(),
140    ))
141}