signet_tx_cache/
types.rs

1//! The endpoints for the transaction cache.
2use alloy::{consensus::TxEnvelope, primitives::B256};
3use serde::{Deserialize, Serialize};
4use signet_bundle::SignetEthBundle;
5use signet_types::SignedOrder;
6
7/// A bundle response from the transaction cache, containing a UUID and a
8/// [`SignetEthBundle`].
9#[derive(Debug, Clone, Serialize, Deserialize)]
10pub struct TxCacheBundle {
11    /// The bundle id (a UUID)
12    pub id: uuid::Uuid,
13    /// The bundle itself
14    pub bundle: SignetEthBundle,
15}
16
17impl TxCacheBundle {
18    /// Create a new bundle response from a bundle and an id.
19    pub const fn new(bundle: SignetEthBundle, id: uuid::Uuid) -> Self {
20        Self { id, bundle }
21    }
22
23    /// Create a new bundle response from a bundle and an id.
24    #[deprecated = "Use `Self::new` instead"]
25    pub const fn from_bundle_and_id(bundle: SignetEthBundle, id: uuid::Uuid) -> Self {
26        Self::new(bundle, id)
27    }
28
29    /// Convert the bundle response to a [`SignetEthBundle`].
30    pub fn into_bundle(self) -> SignetEthBundle {
31        self.bundle
32    }
33
34    /// Convert the bundle response to a [uuid::Uuid].
35    pub fn into_id(self) -> uuid::Uuid {
36        self.id
37    }
38
39    /// The bundle id.
40    pub const fn id(&self) -> uuid::Uuid {
41        self.id
42    }
43
44    /// The bundle itself.
45    pub const fn bundle(&self) -> &SignetEthBundle {
46        &self.bundle
47    }
48}
49
50/// A response from the transaction cache, containing a single bundle.
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct TxCacheBundleResponse {
53    /// The bundle.
54    pub bundle: TxCacheBundle,
55}
56
57impl From<TxCacheBundle> for TxCacheBundleResponse {
58    fn from(bundle: TxCacheBundle) -> Self {
59        Self { bundle }
60    }
61}
62
63impl From<TxCacheBundleResponse> for TxCacheBundle {
64    fn from(response: TxCacheBundleResponse) -> Self {
65        response.bundle
66    }
67}
68
69impl TxCacheBundleResponse {
70    /// Create a new bundle response from a bundle.
71    pub const fn new(bundle: TxCacheBundle) -> Self {
72        Self { bundle }
73    }
74
75    /// Create a new bundle response from a bundle.
76    #[deprecated = "Use `From::from` instead, `Self::new` in const contexts"]
77    pub const fn from_bundle(bundle: TxCacheBundle) -> Self {
78        Self::new(bundle)
79    }
80
81    /// Convert the bundle response to a [`SignetEthBundle`].
82    #[deprecated = "Use `this.bundle` instead."]
83    pub fn into_bundle(self) -> TxCacheBundle {
84        self.bundle
85    }
86}
87
88/// Response from the transaction cache `bundles` endpoint, containing a list of bundles.
89#[derive(Debug, Clone, Serialize, Deserialize)]
90pub struct TxCacheBundlesResponse {
91    /// the list of bundles
92    pub bundles: Vec<TxCacheBundle>,
93}
94
95impl From<Vec<TxCacheBundle>> for TxCacheBundlesResponse {
96    fn from(bundles: Vec<TxCacheBundle>) -> Self {
97        Self { bundles }
98    }
99}
100
101impl From<TxCacheBundlesResponse> for Vec<TxCacheBundle> {
102    fn from(response: TxCacheBundlesResponse) -> Self {
103        response.bundles
104    }
105}
106
107impl TxCacheBundlesResponse {
108    /// Create a new bundle response from a list of bundles.
109    pub const fn new(bundles: Vec<TxCacheBundle>) -> Self {
110        Self { bundles }
111    }
112
113    /// Create a new bundle response from a list of bundles.
114    #[deprecated = "Use `From::from` instead, `Self::new` in const contexts"]
115    pub const fn from_bundles(bundles: Vec<TxCacheBundle>) -> Self {
116        Self::new(bundles)
117    }
118
119    /// Convert the bundle response to a list of [`SignetEthBundle`].
120    #[deprecated = "Use `this.bundles` instead."]
121    pub fn into_bundles(self) -> Vec<TxCacheBundle> {
122        self.bundles
123    }
124}
125
126/// Represents a response to successfully adding or updating a bundle in the transaction cache.
127#[derive(Debug, Copy, Clone, Serialize, Deserialize)]
128pub struct TxCacheSendBundleResponse {
129    /// The bundle id (a UUID)
130    pub id: uuid::Uuid,
131}
132
133impl TxCacheSendBundleResponse {
134    /// Create a new bundle response from a bundle id.
135    pub const fn new(id: uuid::Uuid) -> Self {
136        Self { id }
137    }
138}
139
140impl From<uuid::Uuid> for TxCacheSendBundleResponse {
141    fn from(id: uuid::Uuid) -> Self {
142        Self { id }
143    }
144}
145
146impl From<TxCacheSendBundleResponse> for uuid::Uuid {
147    fn from(response: TxCacheSendBundleResponse) -> Self {
148        response.id
149    }
150}
151
152/// Response from the transaction cache `transactions` endpoint, containing a list of transactions.
153#[derive(Debug, Clone, Serialize, Deserialize)]
154pub struct TxCacheTransactionsResponse {
155    /// The list of transactions.
156    pub transactions: Vec<TxEnvelope>,
157}
158
159impl From<Vec<TxEnvelope>> for TxCacheTransactionsResponse {
160    fn from(transactions: Vec<TxEnvelope>) -> Self {
161        Self { transactions }
162    }
163}
164
165impl From<TxCacheTransactionsResponse> for Vec<TxEnvelope> {
166    fn from(response: TxCacheTransactionsResponse) -> Self {
167        response.transactions
168    }
169}
170
171impl TxCacheTransactionsResponse {
172    /// Instantiate a new transaction response from a list of transactions.
173    pub const fn new(transactions: Vec<TxEnvelope>) -> Self {
174        Self { transactions }
175    }
176
177    /// Create a new transaction response from a list of transactions.
178    #[deprecated = "Use `From::from` instead, or `Self::new` in const contexts"]
179    pub const fn from_transactions(transactions: Vec<TxEnvelope>) -> Self {
180        Self::new(transactions)
181    }
182
183    /// Convert the transaction response to a list of [`TxEnvelope`].
184    #[deprecated = "Use `this.transactions` instead."]
185    pub fn into_transactions(self) -> Vec<TxEnvelope> {
186        self.transactions
187    }
188}
189
190/// Response from the transaction cache to successfully adding a transaction.
191#[derive(Debug, Clone, Copy, Serialize, Deserialize)]
192pub struct TxCacheSendTransactionResponse {
193    /// The transaction hash
194    pub tx_hash: B256,
195}
196
197impl From<B256> for TxCacheSendTransactionResponse {
198    fn from(tx_hash: B256) -> Self {
199        Self { tx_hash }
200    }
201}
202
203impl From<TxCacheSendTransactionResponse> for B256 {
204    fn from(response: TxCacheSendTransactionResponse) -> Self {
205        response.tx_hash
206    }
207}
208
209impl TxCacheSendTransactionResponse {
210    /// Create a new transaction response from a transaction hash.
211    pub const fn new(tx_hash: B256) -> Self {
212        Self { tx_hash }
213    }
214
215    /// Create a new transaction response from a transaction hash.
216    #[deprecated = "Use `From::from` instead, or `Self::new` in const contexts"]
217    pub const fn from_tx_hash(tx_hash: B256) -> Self {
218        Self { tx_hash }
219    }
220
221    /// Convert the transaction response to a transaction hash.
222    #[deprecated = "Use `this.tx_hash` instead."]
223    pub const fn into_tx_hash(self) -> B256 {
224        self.tx_hash
225    }
226}
227
228/// Response from the transaction cache `orders` endpoint, containing a list of signed orders.
229#[derive(Debug, Clone, Serialize, Deserialize)]
230pub struct TxCacheOrdersResponse {
231    /// The list of signed orders.
232    pub orders: Vec<SignedOrder>,
233}
234
235impl From<Vec<SignedOrder>> for TxCacheOrdersResponse {
236    fn from(orders: Vec<SignedOrder>) -> Self {
237        Self { orders }
238    }
239}
240
241impl From<TxCacheOrdersResponse> for Vec<SignedOrder> {
242    fn from(response: TxCacheOrdersResponse) -> Self {
243        response.orders
244    }
245}
246
247impl TxCacheOrdersResponse {
248    /// Create a new order response from a list of orders.
249    pub const fn new(orders: Vec<SignedOrder>) -> Self {
250        Self { orders }
251    }
252
253    /// Create a new order response from a list of orders.
254    #[deprecated = "Use `From::from` instead, `Self::new` in const contexts"]
255    pub const fn from_orders(orders: Vec<SignedOrder>) -> Self {
256        Self { orders }
257    }
258
259    /// Convert the order response to a list of [`SignedOrder`].
260    #[deprecated = "Use `this.orders` instead."]
261    pub fn into_orders(self) -> Vec<SignedOrder> {
262        self.orders
263    }
264}