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

Commit

Permalink
feat: add_transaction(), add_revertible_transaction() (#46)
Browse files Browse the repository at this point in the history
* added add_transaction()+add_revertible_transaction()

* moved tests for new functions into own test case
  • Loading branch information
chikko80 authored Mar 12, 2023
1 parent 1c3007d commit d7580fa
Showing 1 changed file with 50 additions and 1 deletion.
51 changes: 50 additions & 1 deletion src/bundle.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,16 @@ impl BundleRequest {
self
}

/// Adds a transaction to the bundle request.
///
/// This function takes a mutable reference to `self` and adds the specified
/// transaction to the `transactions` vector. The added transaction can either
/// be a novel transaction that you have crafted, or it can be from one of the
/// mempool APIs.
pub fn add_transaction<T: Into<BundleTransaction>>(&mut self, tx: T) {
self.transactions.push(tx.into());
}

/// Adds a revertible transaction to the bundle request.
///
/// This differs from [`BundleRequest::push_transaction`] in that the bundle will still be
Expand All @@ -126,6 +136,24 @@ impl BundleRequest {
self
}

/// Adds a revertible transaction to the bundle request.
///
/// This function takes a mutable reference to `self` and adds the specified
/// revertible transaction to the `transactions` vector. The added transaction can either
/// be a novel transaction that you have crafted, or it can be from one of the
/// mempool APIs. Unlike the `push_transaction` method, the bundle will still be considered
/// valid even if the added transaction reverts.
pub fn add_revertible_transaction<T: Into<BundleTransaction>>(&mut self, tx: T) {
let tx = tx.into();
self.transactions.push(tx.clone());

let tx_hash: H256 = match tx {
BundleTransaction::Signed(inner) => inner.hash(),
BundleTransaction::Raw(inner) => keccak256(inner).into(),
};
self.revertible_transaction_hashes.push(tx_hash);
}

/// Get a reference to the transactions currently in the bundle request.
pub fn transactions(&self) -> &Vec<BundleTransaction> {
&self.transactions
Expand Down Expand Up @@ -393,6 +421,27 @@ mod tests {
);
}

#[test]
fn bundle_serialize_add_transactions() {
let mut bundle = BundleRequest::new()
.push_transaction(Bytes::from(vec![0x1]))
.push_revertible_transaction(Bytes::from(vec![0x2]))
.set_block(2.into())
.set_min_timestamp(1000)
.set_max_timestamp(2000)
.set_simulation_timestamp(1000)
.set_simulation_block(1.into())
.set_simulation_basefee(333333);

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}"#
);
}

#[test]
fn simulated_bundle_deserialize() {
let simulated_bundle: SimulatedBundle = serde_json::from_str(
Expand Down Expand Up @@ -436,7 +485,7 @@ mod tests {
"toAddress": "0x",
"txHash": "0xa839ee83465657cac01adc1d50d96c1b586ed498120a84a64749c0034b4f19fa",
"value": "0x"
}
}
],
"stateBlockNumber": 5221585,
"totalGasUsed": 42000
Expand Down

0 comments on commit d7580fa

Please sign in to comment.