@@ -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