From ed171ca53e63d8e9fc32d33a981b722d0c4923d1 Mon Sep 17 00:00:00 2001 From: scx1332 Date: Fri, 20 Oct 2023 19:58:48 +0200 Subject: [PATCH] removed allow_max_fee_greater_than_priority_fee --- config-payments.toml | 1 - crates/erc20_payment_lib/src/config.rs | 1 - .../erc20_payment_lib/src/sender/process.rs | 83 ++++++++++++------- crates/erc20_payment_lib/src/setup.rs | 4 - .../src/config_setup.rs | 1 - 5 files changed, 55 insertions(+), 35 deletions(-) diff --git a/config-payments.toml b/config-payments.toml index 4d39a66d..c6702309 100644 --- a/config-payments.toml +++ b/config-payments.toml @@ -61,7 +61,6 @@ transaction-timeout = 100 token = { address = "0x0B220b82F3eA3B7F6d9A1D8ab58930C064A2b5Bf", symbol = "GLM" } # multi-contract = { address = "0x50100d4faf5f3b09987dea36dc2eddd57a3e561b", max-at-once = 10 } confirmation-blocks = 1 -allow-max-fee-greater-than-priority-fee = true block-explorer-url = "https://polygonscan.com" [chain.dev] diff --git a/crates/erc20_payment_lib/src/config.rs b/crates/erc20_payment_lib/src/config.rs index 8a6cafe8..a8a7eb3c 100644 --- a/crates/erc20_payment_lib/src/config.rs +++ b/crates/erc20_payment_lib/src/config.rs @@ -81,7 +81,6 @@ pub struct Chain { pub faucet_eth_amount: Option, pub faucet_glm_amount: Option, pub block_explorer_url: Option, - pub allow_max_fee_greater_than_priority_fee: Option, } #[derive(Deserialize, Debug, Clone)] diff --git a/crates/erc20_payment_lib/src/sender/process.rs b/crates/erc20_payment_lib/src/sender/process.rs index 98abe4ff..75191ab8 100644 --- a/crates/erc20_payment_lib/src/sender/process.rs +++ b/crates/erc20_payment_lib/src/sender/process.rs @@ -5,6 +5,7 @@ use crate::db::ops::{ use crate::error::PaymentError; use crate::error::*; use crate::{err_create, err_custom_create, err_from}; +use rust_decimal::prelude::Zero; use rust_decimal::Decimal; use sqlx::SqlitePool; use std::str::FromStr; @@ -27,7 +28,9 @@ use crate::transaction::check_transaction; use crate::transaction::find_receipt; use crate::transaction::send_transaction; use crate::transaction::sign_transaction_with_callback; -use crate::utils::{datetime_from_u256_timestamp, get_env_bool_value, u256_to_rust_dec}; +use crate::utils::{ + datetime_from_u256_timestamp, get_env_bool_value, rust_dec_to_u256, u256_to_rust_dec, +}; #[derive(Debug)] pub enum ProcessTransactionResult { @@ -72,9 +75,7 @@ pub async fn process_transaction( let is_polygon_eco_mode = chain_setup.chain_id == 137 && get_env_bool_value("POLYGON_ECO_MODE") - && chain_setup - .allow_max_fee_greater_than_priority_fee - .unwrap_or(false); + ; let web3 = payment_setup.get_provider(chain_id).map_err(|_e| { err_create!(TransactionFailedError::new(&format!( @@ -151,17 +152,55 @@ pub async fn process_transaction( let blockchain_gas_price = web3 .eth() .block(BlockId::Number(BlockNumber::Latest)) - .await.map_err(err_from!())? + .await + .map_err(err_from!())? .ok_or(err_create!(TransactionFailedError::new( "Failed to get latest block from RPC node" )))? - .base_fee_per_gas.ok_or(err_create!(TransactionFailedError::new( + .base_fee_per_gas + .ok_or(err_create!(TransactionFailedError::new( "Failed to get base_fee_per_gas from RPC node" )))?; - if blockchain_gas_price * 11 < max_fee_per_gas * 10 { - log::warn!("Eco mode activated. Sending transaction with lower base fee"); - web3_tx_dao.max_fee_per_gas = (blockchain_gas_price + U256::from(1000000000)).to_string(); + let extra_gas = if let Ok(extra_gas) = std::env::var("POLYGON_ECO_MODE_EXTRA_GAS") { + let mut extra_gas = Decimal::from_str_exact(&extra_gas).map_err(|err| { + err_custom_create!("POLYGON_ECO_MODE_EXTRA_GAS has to be decimal format {err}") + })?; + + if extra_gas > Decimal::from(30) { + log::warn!("Extra gas is too high, setting to 30"); + extra_gas = Decimal::from(30); + } + if extra_gas < Decimal::from(-10) { + log::warn!("Extra gas is too low, setting to -10"); + extra_gas = Decimal::from(-10); + } + extra_gas + } else { + Decimal::zero() + }; + + let new_target_gas_u256 = if extra_gas >= Decimal::zero() { + let extra_gas_u256 = rust_dec_to_u256(extra_gas, Some(9)).map_err(err_from!())?; + blockchain_gas_price + extra_gas_u256 + } else { + let extra_gas_u256 = rust_dec_to_u256(extra_gas, Some(9)).map_err(err_from!())?; + let min_base_price = U256::from(1_000_000_000u64); + + let mut new_target_gas_u256 = blockchain_gas_price - extra_gas_u256; + if blockchain_gas_price < min_base_price + extra_gas_u256 { + new_target_gas_u256 = min_base_price; + } + new_target_gas_u256 + }; + + if new_target_gas_u256 * 11 < max_fee_per_gas * 10 { + log::warn!("Eco mode activated. Sending transaction with lower base fee. Blockchain base fee: {} Gwei, Tx base fee: {} Gwei", + u256_to_rust_dec(blockchain_gas_price, Some(9)).map_err(err_from!())?, + u256_to_rust_dec(new_target_gas_u256, Some(9)).map_err(err_from!())?, + ); + + web3_tx_dao.max_fee_per_gas = new_target_gas_u256.to_string(); } } @@ -499,17 +538,14 @@ pub async fn process_transaction( let config_priority_fee = u256_to_rust_dec(chain_setup.priority_fee, Some(9)).map_err(err_from!())?; - if !chain_setup - .allow_max_fee_greater_than_priority_fee - .unwrap_or(false) - && tx_priority_fee > tx_fee_per_gas + if tx_priority_fee > tx_fee_per_gas { log::error!( - "Transaction priority fee is greater than max fee per gas for tx: {}. Setup allow_max_fee_greater_than_priority_fee if chain supports it", + "Transaction priority fee is greater than max fee per gas for tx: {}", web3_tx_dao.id ); return Err(err_custom_create!( - "Transaction priority fee is greater than max fee per gas for tx: {}. Setup allow_max_fee_greater_than_priority_fee if chain supports it", + "Transaction priority fee is greater than max fee per gas for tx: {}", web3_tx_dao.id )); } @@ -568,7 +604,7 @@ pub async fn process_transaction( if fee_per_gas_changed || priority_fee_changed { let mut send_replacement_tx = false; let mut replacement_priority_fee = chain_setup.priority_fee; - let replacement_max_fee_per_gas = chain_setup.max_fee_per_gas; + let mut replacement_max_fee_per_gas = chain_setup.max_fee_per_gas; if priority_fee_changed_10 && fee_per_gas_bumped_10 { send_replacement_tx = true; } else if fee_per_gas_bumped_10 && !priority_fee_changed_10 { @@ -576,18 +612,9 @@ pub async fn process_transaction( tx_priority_fee_u256 * U256::from(11) / U256::from(10) + U256::from(1); if replacement_priority_fee > replacement_max_fee_per_gas { - if chain_setup - .allow_max_fee_greater_than_priority_fee - .unwrap_or(false) - { - //polygon is allowing to send transactions with priority fee greater than max fee per gas - //no additional fixes are needed - } else { - //on other networks this fix is needed - //priority fee cannot be greater than max fee per gas - //it should cover very niche case, because almost always priority fee is lower than max fee per gas - replacement_priority_fee = replacement_max_fee_per_gas; - } + //priority fee cannot be greater than max fee per gas + //it should cover very niche case, because priority fee is lower than max fee per gas + replacement_max_fee_per_gas = replacement_priority_fee; } log::warn!( "Replacement priority fee is bumped by 10% from {} to {}", diff --git a/crates/erc20_payment_lib/src/setup.rs b/crates/erc20_payment_lib/src/setup.rs index ab03f0ad..976b3790 100644 --- a/crates/erc20_payment_lib/src/setup.rs +++ b/crates/erc20_payment_lib/src/setup.rs @@ -38,7 +38,6 @@ pub struct ChainSetup { pub transaction_timeout: u64, pub skip_multi_contract_check: bool, pub confirmation_blocks: u64, - pub allow_max_fee_greater_than_priority_fee: Option, pub faucet_eth_amount: Option, pub faucet_glm_amount: Option, pub block_explorer_url: Option, @@ -156,9 +155,6 @@ impl PaymentSetup { currency_gas_symbol: chain_config.1.currency_symbol.clone(), faucet_eth_amount, faucet_glm_amount, - allow_max_fee_greater_than_priority_fee: chain_config - .1 - .allow_max_fee_greater_than_priority_fee, block_explorer_url: chain_config.1.block_explorer_url.clone(), chain_id: chain_config.1.chain_id, }, diff --git a/crates/erc20_payment_lib_test/src/config_setup.rs b/crates/erc20_payment_lib_test/src/config_setup.rs index 68ebc9af..844c5600 100644 --- a/crates/erc20_payment_lib_test/src/config_setup.rs +++ b/crates/erc20_payment_lib_test/src/config_setup.rs @@ -29,7 +29,6 @@ pub async fn create_default_config_setup(proxy_url_base: &str, proxy_key: &str) }), transaction_timeout: 25, confirmation_blocks: 1, - allow_max_fee_greater_than_priority_fee: Some(false), faucet_eth_amount: Some(10.0), faucet_glm_amount: Some(20.0), block_explorer_url: Some("http://127.0.0.1:4000".to_string()),