From d7580fa65b695899ccea3e5db92a131abafc9708 Mon Sep 17 00:00:00 2001 From: chikko80 <58805871+chikko80@users.noreply.github.com> Date: Mon, 13 Mar 2023 10:03:11 +1100 Subject: [PATCH] feat: add_transaction(), add_revertible_transaction() (#46) * added add_transaction()+add_revertible_transaction() * moved tests for new functions into own test case --- src/bundle.rs | 51 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 50 insertions(+), 1 deletion(-) diff --git a/src/bundle.rs b/src/bundle.rs index 2f3f998..dd4b103 100644 --- a/src/bundle.rs +++ b/src/bundle.rs @@ -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>(&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 @@ -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>(&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 { &self.transactions @@ -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( @@ -436,7 +485,7 @@ mod tests { "toAddress": "0x", "txHash": "0xa839ee83465657cac01adc1d50d96c1b586ed498120a84a64749c0034b4f19fa", "value": "0x" - } + } ], "stateBlockNumber": 5221585, "totalGasUsed": 42000