Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: refactors #1

Merged
merged 3 commits into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .idea/.gitignore

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions .idea/bundler.iml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 8 additions & 0 deletions .idea/modules.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions .idea/vcs.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,4 @@ rand = "0.8.5"
serde = "1.0.216"
serde_json = "1.0.134"
tokio = {version = "1.42.0", features = ["full"]}
thiserror = "2.0.9"
29 changes: 16 additions & 13 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,9 @@ pub mod utils;
mod tests {
use eyre::Ok;

use crate::utils::{
evm::generate_random_calldata,
types::{Bundle, Envelope},
};
use crate::utils::core::bundle::Bundle;
use crate::utils::core::envelope::Envelope;
use crate::utils::evm::generate_random_calldata;

#[tokio::test]
async fn test_bundle_retrieval() {
Expand All @@ -20,9 +19,9 @@ mod tests {
}

#[tokio::test]
async fn test_send_bundle_with_target() -> eyre::Result<()> {
async fn test_send_bundle_with_target() {
// will fail until a tWVM funded EOA (pk) is provided
let private_key = String::from("");
let private_key = String::from("ABCD");

let mut envelopes: Vec<Envelope> = vec![];

Expand All @@ -33,22 +32,24 @@ mod tests {
let envelope = Envelope::new()
.data(Some(envelope_data))
.target(Some(envelope_target))
.build()?;
.build()
.unwrap();
envelopes.push(envelope);
}

let bundle_tx = Bundle::new()
.private_key(private_key)
.envelopes(envelopes)
.build()
.expect("REASON")
.propagate()
.await?;
.await
.unwrap();
assert_eq!(bundle_tx.len(), 66);
Ok(())
}

#[tokio::test]
async fn test_send_bundle_without_target() -> eyre::Result<()> {
async fn test_send_bundle_without_target() {
// will fail until a tWVM funded EOA (pk) is provided, take care about nonce if same wallet is used as in test_send_bundle_with_target
let private_key = String::from("");

Expand All @@ -60,17 +61,19 @@ mod tests {
let envelope = Envelope::new()
.data(Some(envelope_data))
.target(None)
.build()?;
.build()
.unwrap();
envelopes.push(envelope);
}

let bundle_tx = Bundle::new()
.private_key(private_key)
.envelopes(envelopes)
.build()
.unwrap()
.propagate()
.await?;
.await
.unwrap();
assert_eq!(bundle_tx.len(), 66);
Ok(())
}
}
75 changes: 75 additions & 0 deletions src/utils/core/bundle.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
use crate::utils::constants::ADDRESS_BABE1;
use crate::utils::core::bundle_data::BundleData;
use crate::utils::core::bundle_tx_metadata::BundleTxMetadata;
use crate::utils::core::envelope::Envelope;
use crate::utils::errors::Error;
use crate::utils::evm::{create_bundle, retrieve_bundle_data, retrieve_bundle_tx};

#[derive(Debug, Default)]
pub struct Bundle {
pub envelopes: Option<Vec<Envelope>>,
pub private_key: Option<String>,
}

impl Bundle {
pub fn new() -> Self {
Bundle {
envelopes: None,
private_key: None,
}
}

pub fn private_key(mut self, key: String) -> Self {
self.private_key = Some(key);
self
}

pub fn envelopes(mut self, envelopes: Vec<Envelope>) -> Self {
self.envelopes = Some(envelopes);
self
}

pub fn add_envelope(mut self, envelope: Envelope) -> Self {
self.envelopes.get_or_insert(Vec::new()).push(envelope);
self
}

pub fn build(self) -> Result<Bundle, Error> {
let envelopes = self
.envelopes
.filter(|e| !e.is_empty())
.ok_or(Error::EnvelopesNeeded)?;
let private_key = self
.private_key
.filter(|p| !p.is_empty())
.ok_or(Error::PrivateKeyNeeded)?;

Ok(Bundle {
envelopes: Some(envelopes),
private_key: Some(private_key),
})
}
pub async fn propagate(self) -> Result<String, Error> {
let envelopes = self.envelopes.ok_or(Error::EnvelopesNeeded)?;
let private_key = self.private_key.ok_or(Error::PrivateKeyNeeded)?;

let tx = create_bundle(envelopes, private_key)
.await
.map_err(|_| Error::BundleNotCreated)?;
let hash = tx.tx_hash().to_string();
Ok(hash)
}

pub async fn retrieve_envelopes(bundle_txid: String) -> Result<BundleData, Error> {
let bundle: BundleTxMetadata = retrieve_bundle_tx(bundle_txid)
.await
.map_err(|_| Error::BundleRetrievalProblem)?;
// assert the bundle versioning by checking target address
if bundle.to.to_lowercase() != ADDRESS_BABE1.to_string().to_ascii_lowercase() {
return Err(Error::UnverifiedAddress);
}

let res: BundleData = retrieve_bundle_data(bundle.calldata).await;
Ok(res)
}
}
32 changes: 32 additions & 0 deletions src/utils/core/bundle_data.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
use crate::utils::core::envelope::Envelope;
use crate::utils::core::tx_envelope_writer::TxEnvelopeWrapper;
use crate::utils::errors::Error;
use crate::utils::evm::create_envelope;
use alloy::consensus::TxEnvelope;
use borsh_derive::{BorshDeserialize, BorshSerialize};

#[derive(
Debug,
Default,
serde::Serialize,
serde::Deserialize,
PartialEq,
BorshSerialize,
BorshDeserialize,
)]
pub struct BundleData {
pub envelopes: Vec<TxEnvelopeWrapper>,
}

impl BundleData {
pub fn from(envelopes: Vec<TxEnvelopeWrapper>) -> Self {
BundleData { envelopes }
}

pub async fn create_envelope(
private_key: Option<&str>,
envelope: Envelope,
) -> Result<TxEnvelope, Error> {
create_envelope(private_key, envelope).await
}
}
20 changes: 20 additions & 0 deletions src/utils/core/bundle_tx_metadata.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BundleTxMetadata {
pub block_number: String,
pub block_hash: String,
pub calldata: String,
pub to: String,
}

impl BundleTxMetadata {
pub fn from(block_number: String, block_hash: String, calldata: String, to: String) -> Self {
BundleTxMetadata {
block_number,
block_hash,
calldata,
to,
}
}
}
38 changes: 38 additions & 0 deletions src/utils/core/envelope.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
use serde::{Deserialize, Serialize};

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Envelope {
pub data: Option<Vec<u8>>,
pub target: Option<String>,
}

impl Envelope {
pub fn new() -> Self {
Self {
data: None,
target: None,
}
}

pub fn data(mut self, data: Option<Vec<u8>>) -> Self {
self.data = data;
self
}

pub fn target(mut self, target: Option<String>) -> Self {
self.target = target;
self
}

pub fn build(self) -> eyre::Result<Self> {
let data = self
.clone()
.data
.ok_or_else(|| eyre::eyre!("data field is required"))?;
assert_ne!(data.len(), 0);
Ok(Self {
data: self.data,
target: self.target,
})
}
}
17 changes: 17 additions & 0 deletions src/utils/core/envelope_signature.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
use borsh_derive::{BorshDeserialize, BorshSerialize};

#[derive(
Clone,
Debug,
Default,
serde::Serialize,
serde::Deserialize,
PartialEq,
BorshSerialize,
BorshDeserialize,
)]
pub struct EnvelopeSignature {
pub y_parity: bool,
pub r: String,
pub s: String,
}
6 changes: 6 additions & 0 deletions src/utils/core/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub mod bundle;
pub mod bundle_data;
pub mod bundle_tx_metadata;
pub mod envelope;
pub mod envelope_signature;
pub mod tx_envelope_writer;
Loading