Skip to content

Commit 6ffb72a

Browse files
committed
Fix gas simulation while using price multiplier
This is a funny little bug where we copy/pasted the options setting code into the simulation and in fact the gas limit multiplier specifically is not useful, or only use in the context of a set gas limit becuase we already set max gas. This change modifies the options application to ensure that gas price and gas limit multipliers are only applied when they can succeed
1 parent 8fa3669 commit 6ffb72a

2 files changed

Lines changed: 38 additions & 24 deletions

File tree

web30/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "web30"
3-
version = "1.12.2"
3+
version = "1.12.3"
44
authors = ["Michal Papierski", "Jehan Tremback", "Justin Kilpatrick", "Christian Borst"]
55
description = "Async endian safe web3 library"
66
license = "Apache-2.0"

web30/src/client/transactions.rs

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -391,40 +391,54 @@ impl Web3 {
391391
transaction.set_gas_limit(gas.limit);
392392
transaction.set_gas_price(gas.price);
393393

394+
let gas_limit_option_set = options
395+
.iter()
396+
.any(|opt| matches!(opt, SendTxOption::GasLimit(_)));
397+
394398
for option in options {
395399
match option {
396400
SendTxOption::GasMaxFee(gp) | SendTxOption::GasPrice(gp) => {
397-
transaction.set_gas_limit(gp)
401+
transaction.set_gas_price(gp)
398402
}
399403
SendTxOption::GasPriorityFee(gp) => transaction.set_priority_fee(gp),
400404
SendTxOption::GasLimitMultiplier(glm) => {
401-
let f32_gas = gas.limit.to_u128();
402-
let val = if let Some(v) = f32_gas {
403-
// convert to f32, multiply, then convert back, this
404-
// will be lossy but you want an exact price you can set it
405-
((v as f32 * glm) as u128).into()
406-
} else {
407-
// gas price is insanely high, best effort rounding
408-
// perhaps we should panic here
409-
gas.price * (glm.round() as u128).into()
410-
};
411-
transaction.set_gas_limit(val);
405+
// only apply this if gas limit is set. Otherwise we are using max gas already
406+
// and applying a multiplier would likely push us over the balance limit, a multiplier
407+
// lower than 1 is fine in this case as it reduces gas
408+
if gas_limit_option_set || glm < 1.0 {
409+
let f32_gas = gas.limit.to_u128();
410+
let val = if let Some(v) = f32_gas {
411+
// convert to f32, multiply, then convert back, this
412+
// will be lossy but you want an exact price you can set it
413+
((v as f32 * glm) as u128).into()
414+
} else {
415+
// gas price is insanely high, best effort rounding
416+
// perhaps we should panic here
417+
gas.price * (glm.round() as u128).into()
418+
};
419+
transaction.set_gas_limit(val);
420+
}
412421
}
413422
SendTxOption::GasLimit(gl) => transaction.set_gas_limit(gl),
414423
SendTxOption::Nonce(n) => transaction.set_nonce(n),
415424
SendTxOption::AccessList(list) => transaction.set_access_list(list),
416425
SendTxOption::GasPriceMultiplier(gm) | SendTxOption::GasMaxFeeMultiplier(gm) => {
417-
let f32_gas = gas.price.to_u128();
418-
let val = if let Some(v) = f32_gas {
419-
// convert to f32, multiply, then convert back, this
420-
// will be lossy but you want an exact price you can set it
421-
((v as f32 * gm) as u128).into()
422-
} else {
423-
// gas price is insanely high, best effort rounding
424-
// perhaps we should panic here
425-
gas.price * (gm.round() as u128).into()
426-
};
427-
transaction.set_gas_price(val);
426+
// same reasoning as gas limit multiplier, we are already using max gas for the default gas
427+
// price and our balance. So we can't do a higher price unless the gas limit has been set lower
428+
// than max
429+
if gas_limit_option_set || gm < 1.0 {
430+
let f32_gas = gas.price.to_u128();
431+
let val = if let Some(v) = f32_gas {
432+
// convert to f32, multiply, then convert back, this
433+
// will be lossy but you want an exact price you can set it
434+
((v as f32 * gm) as u128).into()
435+
} else {
436+
// gas price is insanely high, best effort rounding
437+
// perhaps we should panic here
438+
gas.price * (gm.round() as u128).into()
439+
};
440+
transaction.set_gas_price(val);
441+
}
428442
}
429443
SendTxOption::NetworkId(_) => {
430444
return Err(Web3Error::BadInput(

0 commit comments

Comments
 (0)