From 91135cc516344272be431b44a7dfda94a26579ae Mon Sep 17 00:00:00 2001 From: 12Ghast Date: Wed, 24 Jan 2024 12:17:34 +1100 Subject: [PATCH] feat: Add replacementUuid support in BundleRequest (#63) * feat: Add replacementUuid support in BundleRequest * Switch from String -> uuid::Uuid for replacementUuid field --- CHANGELOG.md | 1 + Cargo.toml | 1 + src/bundle.rs | 34 +++++++++++++++++++++++++++++++--- 3 files changed, 33 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 459c071..d245cad 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/), ## [Unreleased] - Fix simulation for broadcaster middleware (#58) +- Added support for `replacementUuid` field in `eth_sendBundle` ## [0.14.0] diff --git a/Cargo.toml b/Cargo.toml index d56cd14..1fe3156 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -20,6 +20,7 @@ thiserror = { version = "1.0.37", default-features = false } serde = "1" serde_json = "1" chrono = { version = "0.4.22", features = ["default", "serde"] } +uuid = "1.5" # HTTP url = { version = "2.3.1", default-features = false } diff --git a/src/bundle.rs b/src/bundle.rs index dd4b103..5056b03 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -5,6 +5,7 @@ use ethers::core::{ utils::keccak256, }; use serde::{Deserialize, Serialize, Serializer}; +use uuid::Uuid; /// A bundle hash. pub type BundleHash = H256; @@ -29,7 +30,6 @@ impl From for BundleTransaction { Self::Raw(tx) } } - /// A bundle that can be submitted to a Flashbots relay. /// /// The bundle can include your own transactions and transactions from @@ -65,6 +65,11 @@ pub struct BundleRequest { #[serde(skip_serializing_if = "Option::is_none")] max_timestamp: Option, + #[serde(rename = "replacementUuid")] + #[serde(skip_serializing_if = "Option::is_none")] + #[serde(serialize_with = "serialize_uuid_as_string")] + uuid: Option, + #[serde(rename = "stateBlockNumber")] #[serde(skip_serializing_if = "Option::is_none")] simulation_block: Option, @@ -78,6 +83,15 @@ pub struct BundleRequest { simulation_basefee: Option, } +fn serialize_uuid_as_string(x: &Option, s: S) -> Result +where + S: Serializer, +{ + // Don't need to handle None option here as handled by + // #[serde(skip_serializing_if = "Option::is_none")] + s.serialize_str(&x.unwrap().to_string()) +} + pub fn serialize_txs(txs: &[BundleTransaction], s: S) -> Result where S: Serializer, @@ -170,6 +184,18 @@ impl BundleRequest { .collect() } + /// Get a reference to the replacement uuid (if any). + pub fn uuid(&self) -> &Option { + &self.uuid + } + + /// Set the replacement uuid of the bundle. + /// This is used for bundle replacements or cancellations using eth_cancelBundle + pub fn set_uuid(mut self, uuid: Uuid) -> Self { + self.uuid = Some(uuid); + self + } + /// Get the target block (if any). pub fn block(&self) -> Option { self.target_block @@ -402,6 +428,7 @@ pub struct BuilderEntry { mod tests { use super::*; use std::str::FromStr; + use uuid::uuid; #[test] fn bundle_serialize() { @@ -431,14 +458,15 @@ mod tests { .set_max_timestamp(2000) .set_simulation_timestamp(1000) .set_simulation_block(1.into()) - .set_simulation_basefee(333333); + .set_simulation_basefee(333333) + .set_uuid(uuid!("67e55044-10b1-426f-9247-bb680e5fe0c8")); bundle.add_transaction(Bytes::from(vec![0x3])); bundle.add_revertible_transaction(Bytes::from(vec![0x4])); assert_eq!( &serde_json::to_string(&bundle).unwrap(), - r#"{"txs":["0x01","0x02","0x03","0x04"],"revertingTxHashes":["0xf2ee15ea639b73fa3db9b34a245bdfa015c260c598b211bf05a1ecc4b3e3b4f2","0xf343681465b9efe82c933c3e8748c70cb8aa06539c361de20f72eac04e766393"],"blockNumber":"0x2","minTimestamp":1000,"maxTimestamp":2000,"stateBlockNumber":"0x1","timestamp":1000,"baseFee":333333}"# + r#"{"txs":["0x01","0x02","0x03","0x04"],"revertingTxHashes":["0xf2ee15ea639b73fa3db9b34a245bdfa015c260c598b211bf05a1ecc4b3e3b4f2","0xf343681465b9efe82c933c3e8748c70cb8aa06539c361de20f72eac04e766393"],"blockNumber":"0x2","minTimestamp":1000,"maxTimestamp":2000,"replacementUuid":"67e55044-10b1-426f-9247-bb680e5fe0c8","stateBlockNumber":"0x1","timestamp":1000,"baseFee":333333}"# ); }