openzeppelin_relayer/domain/relayer/solana/rpc/methods/
mod.rs

1//! # Solana RPC Methods Module
2//!
3//! This module defines the `SolanaRpcMethods` trait which provides an asynchronous interface
4//! for various Solana-specific RPC operations. These operations include fee estimation,
5//! transaction processing (transfer, prepare, sign, and send), token retrieval, and feature
6//! queries.
7mod fee_estimate;
8mod get_features_enabled;
9mod get_supported_tokens;
10mod prepare_transaction;
11mod sign_and_send_transaction;
12mod sign_transaction;
13mod transfer_transaction;
14mod utils;
15mod validations;
16
17#[cfg(test)]
18mod test_setup;
19#[cfg(test)]
20use mockall::automock;
21
22use std::sync::Arc;
23
24#[cfg(test)]
25pub use test_setup::*;
26pub use validations::*;
27
28use crate::{
29    jobs::{JobProducer, JobProducerTrait},
30    models::{NetworkRepoModel, RelayerRepoModel, TransactionRepoModel},
31    repositories::{Repository, TransactionRepository, TransactionRepositoryStorage},
32    services::{JupiterServiceTrait, SolanaProviderTrait, SolanaSignTrait},
33};
34
35use super::*;
36
37#[cfg(test)]
38use crate::{jobs::MockJobProducerTrait, repositories::MockTransactionRepository};
39
40#[cfg(test)]
41use crate::services::{MockJupiterServiceTrait, MockSolanaProviderTrait, MockSolanaSignTrait};
42use async_trait::async_trait;
43
44use crate::{
45    models::{
46        FeeEstimateRequestParams, FeeEstimateResult, GetFeaturesEnabledRequestParams,
47        GetFeaturesEnabledResult, GetSupportedTokensRequestParams, GetSupportedTokensResult,
48        PrepareTransactionRequestParams, PrepareTransactionResult,
49        SignAndSendTransactionRequestParams, SignAndSendTransactionResult,
50        SignTransactionRequestParams, SignTransactionResult, TransferTransactionRequestParams,
51        TransferTransactionResult,
52    },
53    services::{JupiterService, SolanaProvider, SolanaSigner},
54};
55
56#[cfg_attr(test, automock)]
57#[async_trait]
58pub trait SolanaRpcMethods: Send + Sync {
59    async fn fee_estimate(
60        &self,
61        request: FeeEstimateRequestParams,
62    ) -> Result<FeeEstimateResult, SolanaRpcError>;
63    async fn transfer_transaction(
64        &self,
65        request: TransferTransactionRequestParams,
66    ) -> Result<TransferTransactionResult, SolanaRpcError>;
67    async fn prepare_transaction(
68        &self,
69        request: PrepareTransactionRequestParams,
70    ) -> Result<PrepareTransactionResult, SolanaRpcError>;
71    async fn sign_transaction(
72        &self,
73        request: SignTransactionRequestParams,
74    ) -> Result<SignTransactionResult, SolanaRpcError>;
75    async fn sign_and_send_transaction(
76        &self,
77        request: SignAndSendTransactionRequestParams,
78    ) -> Result<SignAndSendTransactionResult, SolanaRpcError>;
79    async fn get_supported_tokens(
80        &self,
81        request: GetSupportedTokensRequestParams,
82    ) -> Result<GetSupportedTokensResult, SolanaRpcError>;
83    async fn get_features_enabled(
84        &self,
85        request: GetFeaturesEnabledRequestParams,
86    ) -> Result<GetFeaturesEnabledResult, SolanaRpcError>;
87}
88
89pub type DefaultProvider = SolanaProvider;
90pub type DefaultSigner = SolanaSigner;
91pub type DefaultJupiterService = JupiterService;
92pub type DefaultJobProducer = JobProducer;
93pub type DefaultTransactionRepository = TransactionRepositoryStorage;
94
95#[cfg(test)]
96impl
97    SolanaRpcMethodsImpl<
98        MockSolanaProviderTrait,
99        MockSolanaSignTrait,
100        MockJupiterServiceTrait,
101        MockJobProducerTrait,
102        MockTransactionRepository,
103    >
104{
105    pub fn new_mock(
106        relayer: RelayerRepoModel,
107        network: NetworkRepoModel,
108        provider: Arc<MockSolanaProviderTrait>,
109        signer: Arc<MockSolanaSignTrait>,
110        jupiter_service: Arc<MockJupiterServiceTrait>,
111        job_producer: Arc<MockJobProducerTrait>,
112        transaction_repository: Arc<MockTransactionRepository>,
113    ) -> Self {
114        Self {
115            relayer,
116            network,
117            provider,
118            signer,
119            jupiter_service,
120            job_producer,
121            transaction_repository,
122        }
123    }
124}
125
126pub struct SolanaRpcMethodsImpl<P, S, J, JP, TR>
127where
128    P: SolanaProviderTrait + Send + Sync + 'static,
129    S: SolanaSignTrait + Send + Sync + 'static,
130    J: JupiterServiceTrait + Send + Sync + 'static,
131    JP: JobProducerTrait + Send + Sync + 'static,
132    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
133{
134    pub(crate) relayer: RelayerRepoModel,
135    pub(crate) network: NetworkRepoModel,
136    pub(crate) provider: Arc<P>,
137    pub(crate) signer: Arc<S>,
138    pub(crate) jupiter_service: Arc<J>,
139    pub(crate) job_producer: Arc<JP>,
140    pub(crate) transaction_repository: Arc<TR>,
141}
142
143pub type DefaultSolanaRpcMethodsImpl = SolanaRpcMethodsImpl<
144    DefaultProvider,
145    DefaultSigner,
146    DefaultJupiterService,
147    DefaultJobProducer,
148    DefaultTransactionRepository,
149>;
150
151impl<P, S, J, JP, TR> SolanaRpcMethodsImpl<P, S, J, JP, TR>
152where
153    P: SolanaProviderTrait + Send + Sync + 'static,
154    S: SolanaSignTrait + Send + Sync + 'static,
155    J: JupiterServiceTrait + Send + Sync + 'static,
156    JP: JobProducerTrait + Send + Sync + 'static,
157    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
158{
159    pub fn new(
160        relayer: RelayerRepoModel,
161        network: NetworkRepoModel,
162        provider: Arc<P>,
163        signer: Arc<S>,
164        jupiter_service: Arc<J>,
165        job_producer: Arc<JP>,
166        transaction_repository: Arc<TR>,
167    ) -> Self {
168        Self {
169            relayer,
170            network,
171            provider,
172            signer,
173            jupiter_service,
174            job_producer,
175            transaction_repository,
176        }
177    }
178}
179
180#[async_trait]
181impl<P, S, J, JP, TR> SolanaRpcMethods for SolanaRpcMethodsImpl<P, S, J, JP, TR>
182where
183    P: SolanaProviderTrait + Send + Sync,
184    S: SolanaSignTrait + Send + Sync,
185    J: JupiterServiceTrait + Send + Sync,
186    JP: JobProducerTrait + Send + Sync,
187    TR: TransactionRepository + Repository<TransactionRepoModel, String> + Send + Sync + 'static,
188{
189    async fn fee_estimate(
190        &self,
191        params: FeeEstimateRequestParams,
192    ) -> Result<FeeEstimateResult, SolanaRpcError> {
193        self.fee_estimate_impl(params).await
194    }
195
196    async fn prepare_transaction(
197        &self,
198        params: PrepareTransactionRequestParams,
199    ) -> Result<PrepareTransactionResult, SolanaRpcError> {
200        self.prepare_transaction_impl(params).await
201    }
202
203    async fn sign_transaction(
204        &self,
205        params: SignTransactionRequestParams,
206    ) -> Result<SignTransactionResult, SolanaRpcError> {
207        self.sign_transaction_impl(params).await
208    }
209
210    async fn sign_and_send_transaction(
211        &self,
212        params: SignAndSendTransactionRequestParams,
213    ) -> Result<SignAndSendTransactionResult, SolanaRpcError> {
214        self.sign_and_send_transaction_impl(params).await
215    }
216
217    async fn transfer_transaction(
218        &self,
219        params: TransferTransactionRequestParams,
220    ) -> Result<TransferTransactionResult, SolanaRpcError> {
221        self.transfer_transaction_impl(params).await
222    }
223
224    async fn get_supported_tokens(
225        &self,
226        params: GetSupportedTokensRequestParams,
227    ) -> Result<GetSupportedTokensResult, SolanaRpcError> {
228        self.get_supported_tokens_impl(params).await
229    }
230
231    async fn get_features_enabled(
232        &self,
233        params: GetFeaturesEnabledRequestParams,
234    ) -> Result<GetFeaturesEnabledResult, SolanaRpcError> {
235        self.get_features_enabled_impl(params).await
236    }
237}