Skip to content
This repository has been archived by the owner on Jun 18, 2024. It is now read-only.

Commit

Permalink
feat: Add replacementUuid support in BundleRequest (#63)
Browse files Browse the repository at this point in the history
* feat: Add replacementUuid support in BundleRequest

* Switch from String -> uuid::Uuid for replacementUuid field
  • Loading branch information
TwelfthGhast authored Jan 24, 2024
1 parent 8df3eca commit 91135cc
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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]

Expand Down
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Expand Down
34 changes: 31 additions & 3 deletions src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use ethers::core::{
utils::keccak256,
};
use serde::{Deserialize, Serialize, Serializer};
use uuid::Uuid;

/// A bundle hash.
pub type BundleHash = H256;
Expand All @@ -29,7 +30,6 @@ impl From<Bytes> 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
Expand Down Expand Up @@ -65,6 +65,11 @@ pub struct BundleRequest {
#[serde(skip_serializing_if = "Option::is_none")]
max_timestamp: Option<u64>,

#[serde(rename = "replacementUuid")]
#[serde(skip_serializing_if = "Option::is_none")]
#[serde(serialize_with = "serialize_uuid_as_string")]
uuid: Option<Uuid>,

#[serde(rename = "stateBlockNumber")]
#[serde(skip_serializing_if = "Option::is_none")]
simulation_block: Option<U64>,
Expand All @@ -78,6 +83,15 @@ pub struct BundleRequest {
simulation_basefee: Option<u64>,
}

fn serialize_uuid_as_string<S>(x: &Option<Uuid>, s: S) -> Result<S::Ok, S::Error>
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<S>(txs: &[BundleTransaction], s: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
Expand Down Expand Up @@ -170,6 +184,18 @@ impl BundleRequest {
.collect()
}

/// Get a reference to the replacement uuid (if any).
pub fn uuid(&self) -> &Option<Uuid> {
&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<U64> {
self.target_block
Expand Down Expand Up @@ -402,6 +428,7 @@ pub struct BuilderEntry {
mod tests {
use super::*;
use std::str::FromStr;
use uuid::uuid;

#[test]
fn bundle_serialize() {
Expand Down Expand Up @@ -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}"#
);
}

Expand Down

0 comments on commit 91135cc

Please sign in to comment.