From 8854f0a22783393abdd185ce289ce5a5aa3b9aa1 Mon Sep 17 00:00:00 2001 From: Miguel de Elias Date: Thu, 11 Apr 2024 09:51:23 -0300 Subject: [PATCH] fix: increase estimated gas by 20% --- availability-oracle/src/contract.rs | 62 ++++++++++++++++++++--------- 1 file changed, 44 insertions(+), 18 deletions(-) diff --git a/availability-oracle/src/contract.rs b/availability-oracle/src/contract.rs index 450516c..e30fe89 100644 --- a/availability-oracle/src/contract.rs +++ b/availability-oracle/src/contract.rs @@ -101,15 +101,28 @@ impl StateManager for RewardsManagerContract { let num_subgraphs = ids.len() as u64; let tx = self.contract.set_denied_many(ids, statuses); - if let Err(err) = tx.call().await { - let message = err.decode_revert::().unwrap_or(err.to_string()); - error!(self.logger, "Transaction failed"; - "message" => message, - ); - } else { - tx.send().await?.await?; - METRICS.denied_subgraphs_total.inc_by(num_subgraphs); - } + // Calculate estimated gas + let estimated_gas_tx = tx + .estimate_gas() + .await; + + let estimated_gas = match estimated_gas_tx { + Ok(estimate) => estimate, + Err(err) => { + let message = err.decode_revert::().unwrap_or(err.to_string()); + error!(self.logger, "Transaction failed"; + "message" => message, + ); + // Return `Ok()` to avoid double error logging + return Ok(()); + } + }; + + // Increase the estimated gas by 20% + let increased_estimate = estimated_gas * U256::from(120) / U256::from(100); + + tx.gas(increased_estimate).send().await?.await?; + METRICS.denied_subgraphs_total.inc_by(num_subgraphs); } Ok(()) @@ -127,15 +140,28 @@ impl StateManager for SubgraphAvailabilityManagerContract { let oracle_index = U256::from(self.oracle_index); let tx = self.contract.vote_many(ids, statuses, oracle_index); - if let Err(err) = tx.call().await { - let message = err.decode_revert::().unwrap_or(err.to_string()); - error!(self.logger, "Transaction failed"; - "message" => message, - ); - } else { - tx.send().await?.await?; - METRICS.denied_subgraphs_total.inc_by(num_subgraphs); - } + // Calculate estimated gas + let estimated_gas_tx = tx + .estimate_gas() + .await; + + let estimated_gas = match estimated_gas_tx { + Ok(estimate) => estimate, + Err(err) => { + let message = err.decode_revert::().unwrap_or(err.to_string()); + error!(self.logger, "Transaction failed"; + "message" => message, + ); + // Return `Ok()` to avoid double error logging + return Ok(()); + } + }; + + // Increase the estimated gas by 20% + let increased_estimate = estimated_gas * U256::from(120) / U256::from(100); + + tx.gas(increased_estimate).send().await?.await?; + METRICS.denied_subgraphs_total.inc_by(num_subgraphs); } Ok(())