Skip to content

Commit

Permalink
fix: don't escalate if you've already escalated (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-savu authored Nov 7, 2024
1 parent b2d58a3 commit fd55090
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
17 changes: 9 additions & 8 deletions ethers-middleware/src/gas_escalator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,7 +257,7 @@ impl<M, E> EscalationTask<M, E> {
// Pop all transactions and re-insert those that have not been included yet
for _ in 0..len {
// this must never panic as we're explicitly within bounds
let (old_tx_hash, mut replacement_tx, time, priority) =
let (old_tx_hash, mut replacement_tx, old_creation_time, priority) =
txs.pop().expect("should have element in vector");

let receipt = self
Expand All @@ -272,12 +272,13 @@ impl<M, E> EscalationTask<M, E> {
let old_gas_price = replacement_tx.gas_price.expect("gas price must be set");
// Get the new gas price based on how much time passed since the
// tx was last broadcast
let new_gas_price = self
.escalator
.get_gas_price(old_gas_price, now.duration_since(time).as_secs());
let new_gas_price = self.escalator.get_gas_price(
old_gas_price,
now.duration_since(old_creation_time).as_secs(),
);

let new_txhash = if new_gas_price == old_gas_price {
old_tx_hash
let (new_txhash, new_creation_time) = if new_gas_price == old_gas_price {
(old_tx_hash, old_creation_time)
} else {
// bump the gas price
replacement_tx.gas_price = Some(new_gas_price);
Expand All @@ -297,7 +298,7 @@ impl<M, E> EscalationTask<M, E> {
new_gas_price = ?new_gas_price,
"escalated gas price"
);
new_tx_hash
(new_tx_hash, Instant::now())
}
Err(err) => {
if err.to_string().contains("nonce too low") {
Expand All @@ -317,7 +318,7 @@ impl<M, E> EscalationTask<M, E> {
}
}
};
txs.push((new_txhash, replacement_tx, time, priority));
txs.push((new_txhash, replacement_tx, new_creation_time, priority));
}
}
// after this big ugly loop, we dump everything back in
Expand Down
21 changes: 18 additions & 3 deletions ethers-middleware/tests/gas_escalator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use tokio::time::sleep;

#[tokio::test]
async fn gas_escalator_live() {
let anvil = Anvil::new().port(8545u16).block_time(2u64).spawn();
let anvil = Anvil::new().port(8545u16).block_time(10u64).spawn();
let chain_id = anvil.chain_id();
let provider = Provider::<Http>::try_from(anvil.endpoint()).unwrap();

Expand All @@ -24,9 +24,24 @@ async fn gas_escalator_live() {
let provider = SignerMiddleware::new(provider, wallet);

// wrap with escalator
let escalator = GeometricGasPrice::new(5.0, 1u64, Some(2_000_000_000_000u64));
// escalate every 2 seconds. We should only see 4-5 escalations in total
let escalator = GeometricGasPrice::new(1.1, 2u64, Some(2_000_000_000_000u64));
let provider = GasEscalatorMiddleware::new(provider, escalator, Frequency::Duration(300));

// set the gas price to 10 gwei, so we need to escalate twice
// this works but the tx still goes through regardless of its gas price for some reason
// reqwest::Client::new()
// .post(&format!("{}/", anvil.endpoint()))
// .json(&json!({
// "jsonrpc": "2.0",
// "method": "anvil_setMinGasPrice",
// "params": [10_000_000_000u64],
// "id": 1
// }))
// .send()
// .await
// .unwrap();

let nonce = provider.get_transaction_count(address, None).await.unwrap();
// 1 gwei default base fee
let gas_price = U256::from(1_000_000_000_u64);
Expand All @@ -37,6 +52,6 @@ async fn gas_escalator_live() {

let pending = provider.send_transaction(tx, None).await.expect("could not send");
let receipt = pending.await;
sleep(Duration::from_secs(3)).await;
sleep(Duration::from_secs(2)).await;
println!("receipt gas price: , hardcoded_gas_price: {}, receipt: {:?}", gas_price, receipt);
}

0 comments on commit fd55090

Please sign in to comment.