|
1 | 1 | use crate::traits::BundleDatastore; |
| 2 | +use alloy_primitives::TxHash; |
| 3 | +use alloy_primitives::hex::{FromHex, ToHexExt}; |
2 | 4 | use alloy_rpc_types_mev::EthSendBundle; |
3 | 5 | use anyhow::Result; |
4 | 6 | use sqlx::PgPool; |
@@ -28,12 +30,96 @@ impl PostgresDatastore { |
28 | 30 |
|
29 | 31 | #[async_trait::async_trait] |
30 | 32 | impl BundleDatastore for PostgresDatastore { |
31 | | - async fn insert_bundle(&self, _bundle: EthSendBundle) -> Result<Uuid> { |
32 | | - todo!() |
| 33 | + async fn insert_bundle(&self, bundle: EthSendBundle) -> Result<Uuid> { |
| 34 | + let id = Uuid::new_v4(); |
| 35 | + |
| 36 | + let txs: Vec<String> = bundle |
| 37 | + .txs |
| 38 | + .iter() |
| 39 | + .map(|tx| tx.encode_hex_upper_with_prefix()) |
| 40 | + .collect(); |
| 41 | + let reverting_tx_hashes: Vec<String> = bundle |
| 42 | + .reverting_tx_hashes |
| 43 | + .iter() |
| 44 | + .map(|h| h.encode_hex_with_prefix()) |
| 45 | + .collect(); |
| 46 | + let dropping_tx_hashes: Vec<String> = bundle |
| 47 | + .dropping_tx_hashes |
| 48 | + .iter() |
| 49 | + .map(|h| h.encode_hex_with_prefix()) |
| 50 | + .collect(); |
| 51 | + |
| 52 | + sqlx::query!( |
| 53 | + r#" |
| 54 | + INSERT INTO bundles ( |
| 55 | + id, txs, reverting_tx_hashes, dropping_tx_hashes, |
| 56 | + block_number, min_timestamp, max_timestamp, |
| 57 | + created_at, updated_at |
| 58 | + ) |
| 59 | + VALUES ($1, $2, $3, $4, $5, $6, $7, NOW(), NOW()) |
| 60 | + "#, |
| 61 | + id, |
| 62 | + &txs, |
| 63 | + &reverting_tx_hashes, |
| 64 | + &dropping_tx_hashes, |
| 65 | + bundle.block_number as i64, |
| 66 | + bundle.min_timestamp.map(|t| t as i64), |
| 67 | + bundle.max_timestamp.map(|t| t as i64), |
| 68 | + ) |
| 69 | + .execute(&self.pool) |
| 70 | + .await?; |
| 71 | + |
| 72 | + Ok(id) |
33 | 73 | } |
34 | 74 |
|
35 | | - async fn get_bundle(&self, _id: Uuid) -> Result<Option<EthSendBundle>> { |
36 | | - todo!() |
| 75 | + async fn get_bundle(&self, id: Uuid) -> Result<Option<EthSendBundle>> { |
| 76 | + let result = sqlx::query!( |
| 77 | + r#" |
| 78 | + SELECT txs, reverting_tx_hashes, dropping_tx_hashes, |
| 79 | + block_number, min_timestamp, max_timestamp |
| 80 | + FROM bundles |
| 81 | + WHERE id = $1 |
| 82 | + "#, |
| 83 | + id |
| 84 | + ) |
| 85 | + .fetch_optional(&self.pool) |
| 86 | + .await?; |
| 87 | + |
| 88 | + match result { |
| 89 | + Some(row) => { |
| 90 | + let txs: Result<Vec<alloy_primitives::Bytes>, _> = |
| 91 | + row.txs.into_iter().map(|tx_hex| tx_hex.parse()).collect(); |
| 92 | + |
| 93 | + let reverting_tx_hashes: Result<Vec<TxHash>, _> = row |
| 94 | + .reverting_tx_hashes |
| 95 | + .unwrap_or_default() |
| 96 | + .into_iter() |
| 97 | + .map(TxHash::from_hex) |
| 98 | + .collect(); |
| 99 | + |
| 100 | + let dropping_tx_hashes: Result<Vec<TxHash>, _> = row |
| 101 | + .dropping_tx_hashes |
| 102 | + .unwrap_or_default() |
| 103 | + .into_iter() |
| 104 | + .map(TxHash::from_hex) |
| 105 | + .collect(); |
| 106 | + |
| 107 | + Ok(Some(EthSendBundle { |
| 108 | + txs: txs?, |
| 109 | + block_number: row.block_number.unwrap_or(0) as u64, |
| 110 | + min_timestamp: row.min_timestamp.map(|t| t as u64), |
| 111 | + max_timestamp: row.max_timestamp.map(|t| t as u64), |
| 112 | + reverting_tx_hashes: reverting_tx_hashes?, |
| 113 | + replacement_uuid: None, |
| 114 | + dropping_tx_hashes: dropping_tx_hashes?, |
| 115 | + refund_percent: None, |
| 116 | + refund_recipient: None, |
| 117 | + refund_tx_hashes: Vec::new(), |
| 118 | + extra_fields: Default::default(), |
| 119 | + })) |
| 120 | + } |
| 121 | + None => Ok(None), |
| 122 | + } |
37 | 123 | } |
38 | 124 |
|
39 | 125 | async fn cancel_bundle(&self, _id: Uuid) -> Result<()> { |
|
0 commit comments