Skip to content

Commit dc836a4

Browse files
committed
Allow alloy to estimate tx gas
1 parent 0513a4b commit dc836a4

File tree

2 files changed

+35
-49
lines changed

2 files changed

+35
-49
lines changed

crates/core/src/driver/mod.rs

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
//! The test driver handles the compilation and execution of the test cases.
22
33
use alloy::json_abi::JsonAbi;
4-
use alloy::primitives::Bytes;
4+
use alloy::network::TransactionBuilder;
5+
use alloy::rpc::types::TransactionReceipt;
56
use alloy::rpc::types::trace::geth::GethTrace;
6-
use alloy::rpc::types::{TransactionInput, TransactionReceipt};
77
use alloy::{
8-
primitives::{Address, TxKind, map::HashMap},
8+
primitives::{Address, map::HashMap},
99
rpc::types::{
1010
TransactionRequest,
1111
trace::geth::{AccountState, DiffMode},
@@ -135,21 +135,17 @@ where
135135
std::any::type_name::<T>()
136136
);
137137

138-
let tx = match input.legacy_transaction(
139-
self.config.network_id,
140-
nonce,
141-
&self.deployed_contracts,
142-
&self.deployed_abis,
143-
) {
144-
Ok(tx) => {
145-
tracing::debug!("Legacy transaction data: {tx:#?}");
146-
tx
147-
}
148-
Err(err) => {
149-
tracing::error!("Failed to construct legacy transaction: {err:?}");
150-
return Err(err);
151-
}
152-
};
138+
let tx =
139+
match input.legacy_transaction(nonce, &self.deployed_contracts, &self.deployed_abis) {
140+
Ok(tx) => {
141+
tracing::debug!("Legacy transaction data: {tx:#?}");
142+
tx
143+
}
144+
Err(err) => {
145+
tracing::error!("Failed to construct legacy transaction: {err:?}");
146+
return Err(err);
147+
}
148+
};
153149

154150
tracing::trace!("Executing transaction for input: {input:?}");
155151

@@ -201,6 +197,9 @@ where
201197

202198
for contracts in contract_map.values() {
203199
for (contract_name, contract) in contracts {
200+
let tracing_span = tracing::info_span!("Deploying contract", contract_name);
201+
let _guard = tracing_span.enter();
202+
204203
tracing::debug!(
205204
"Contract name is: {:?} and the input name is: {:?}",
206205
&contract_name,
@@ -228,16 +227,14 @@ where
228227
std::any::type_name::<T>()
229228
);
230229

231-
let tx = TransactionRequest {
232-
from: Some(input.caller),
233-
to: Some(TxKind::Create),
234-
gas_price: Some(5_000_000),
235-
gas: Some(5_000_000),
236-
chain_id: Some(self.config.network_id),
237-
nonce: Some(nonce),
238-
input: TransactionInput::new(Bytes::from(code.into_bytes())),
239-
..Default::default()
240-
};
230+
// We are using alloy for building and submitting the transactions and it will
231+
// automatically fill in all of the missing fields from the provider that we
232+
// are using.
233+
let code = alloy::hex::decode(&code)?;
234+
let tx = TransactionRequest::default()
235+
.nonce(nonce)
236+
.from(input.caller)
237+
.with_deploy_code(code);
241238

242239
let receipt = match node.execute_transaction(tx) {
243240
Ok(receipt) => receipt,

crates/format/src/input.rs

Lines changed: 10 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@ use std::collections::HashMap;
33
use alloy::{
44
hex,
55
json_abi::{Function, JsonAbi},
6-
primitives::{Address, Bytes, TxKind},
7-
rpc::types::{TransactionInput, TransactionRequest},
6+
network::TransactionBuilder,
7+
primitives::{Address, Bytes},
8+
rpc::types::TransactionRequest,
89
};
910
use alloy_primitives::U256;
1011
use alloy_sol_types::SolValue;
@@ -220,30 +221,18 @@ impl Input {
220221
/// Parse this input into a legacy transaction.
221222
pub fn legacy_transaction(
222223
&self,
223-
chain_id: u64,
224224
nonce: u64,
225225
deployed_contracts: &HashMap<String, Address>,
226226
deployed_abis: &HashMap<String, JsonAbi>,
227227
) -> anyhow::Result<TransactionRequest> {
228-
let to = match self.method {
229-
Method::Deployer => Some(TxKind::Create),
230-
_ => Some(TxKind::Call(
231-
self.instance_to_address(&self.instance, deployed_contracts)?,
232-
)),
233-
};
234-
235228
let input_data = self.encoded_input(deployed_abis, deployed_contracts)?;
236-
237-
Ok(TransactionRequest {
238-
from: Some(self.caller),
239-
to,
240-
nonce: Some(nonce),
241-
chain_id: Some(chain_id),
242-
gas_price: Some(5_000_000),
243-
gas: Some(5_000_000),
244-
input: TransactionInput::new(input_data),
245-
..Default::default()
246-
})
229+
let transaction_request = TransactionRequest::default().nonce(nonce);
230+
match self.method {
231+
Method::Deployer => Ok(transaction_request.with_deploy_code(input_data)),
232+
_ => Ok(transaction_request
233+
.to(self.instance_to_address(&self.instance, deployed_contracts)?)
234+
.input(input_data.into())),
235+
}
247236
}
248237
}
249238

0 commit comments

Comments
 (0)