diff --git a/crates/erc20_payment_lib/src/db/model/tx_dao.rs b/crates/erc20_payment_lib/src/db/model/tx_dao.rs index f19be0bb..44099a8c 100644 --- a/crates/erc20_payment_lib/src/db/model/tx_dao.rs +++ b/crates/erc20_payment_lib/src/db/model/tx_dao.rs @@ -10,8 +10,8 @@ pub struct TxDao { pub to_addr: String, pub chain_id: i64, pub gas_limit: Option, - pub max_fee_per_gas: String, - pub priority_fee: String, + pub max_fee_per_gas: Option, + pub priority_fee: Option, pub val: String, pub nonce: Option, pub processing: i64, diff --git a/crates/erc20_payment_lib/src/db/ops/tx_ops.rs b/crates/erc20_payment_lib/src/db/ops/tx_ops.rs index 43fec0aa..dda87bc9 100644 --- a/crates/erc20_payment_lib/src/db/ops/tx_ops.rs +++ b/crates/erc20_payment_lib/src/db/ops/tx_ops.rs @@ -253,7 +253,9 @@ async fn tx_test() -> sqlx::Result<()> { let mut tx_to_insert = TxDao { id: -1, - tx_hash: Some("0x13d8a54dec1c0a30f1cd5129f690c3e27b9aadd59504957bad4d247966dadae7".to_string()), + tx_hash: Some( + "0x13d8a54dec1c0a30f1cd5129f690c3e27b9aadd59504957bad4d247966dadae7".to_string(), + ), signed_raw_data: None, signed_date: Some(chrono::Utc::now()), broadcast_date: Some(chrono::Utc::now()), @@ -263,8 +265,8 @@ async fn tx_test() -> sqlx::Result<()> { to_addr: "0xbcfe9736a4f5bf2e43620061ff3001ea0d003c0f".to_string(), chain_id: 987789, gas_limit: Some(100000), - max_fee_per_gas: "110000000000".to_string(), - priority_fee: "5110000000000".to_string(), + max_fee_per_gas: Some("110000000000".to_string()), + priority_fee: Some("5110000000000".to_string()), val: "0".to_string(), nonce: Some(1), processing: 0, @@ -278,7 +280,7 @@ async fn tx_test() -> sqlx::Result<()> { engine_message: None, engine_error: None, first_processed: None, - confirm_date: None + confirm_date: None, }; let tx_from_insert = insert_tx(&conn, &tx_to_insert).await?; diff --git a/crates/erc20_payment_lib/src/sender/allowance.rs b/crates/erc20_payment_lib/src/sender/allowance.rs index 9c80b0bb..a3363b0c 100644 --- a/crates/erc20_payment_lib/src/sender/allowance.rs +++ b/crates/erc20_payment_lib/src/sender/allowance.rs @@ -22,10 +22,7 @@ pub async fn process_allowance( signer: &impl Signer, ) -> Result { let minimum_allowance: U256 = U256::max_value() / U256::from(2); - let chain_setup = payment_setup.get_chain_setup(allowance_request.chain_id)?; let web3 = payment_setup.get_provider(allowance_request.chain_id)?; - let max_fee_per_gas = chain_setup.max_fee_per_gas; - let priority_fee = chain_setup.priority_fee; let mut db_allowance = find_allowance( conn, @@ -129,8 +126,6 @@ pub async fn process_allowance( Address::from_str(&allowance_request.spender_addr).map_err(err_from!())?, allowance_request.chain_id as u64, None, - max_fee_per_gas, - priority_fee, )?; let mut db_transaction = conn.begin().await.map_err(err_from!())?; let web3_tx_dao = insert_tx(&mut *db_transaction, &approve_tx) diff --git a/crates/erc20_payment_lib/src/sender/batching.rs b/crates/erc20_payment_lib/src/sender/batching.rs index 709c46f7..68383603 100644 --- a/crates/erc20_payment_lib/src/sender/batching.rs +++ b/crates/erc20_payment_lib/src/sender/batching.rs @@ -115,9 +115,6 @@ pub async fn gather_transactions_batch_multi( let use_direct_method = payment_setup.contract_use_direct_method; let use_unpacked_method = payment_setup.contract_use_unpacked_method; - let max_fee_per_gas = chain_setup.max_fee_per_gas; - let priority_fee = chain_setup.priority_fee; - let max_per_batch = chain_setup.multi_contract_max_at_once; log::debug!("Processing token transfer {:?}", token_transfer); if let Some(token_addr) = token_transfer.token_addr.as_ref() { @@ -212,8 +209,6 @@ pub async fn gather_transactions_batch_multi( erc20_amounts[0], token_transfer.chain_id as u64, None, - max_fee_per_gas, - priority_fee, )? } else if let Some(multi_contract_address) = chain_setup.multi_contract_address { log::info!("Inserting transaction stub for ERC20 multi transfer contract: {:?} for {} distinct transfers", multi_contract_address, erc20_to.len()); @@ -225,8 +220,6 @@ pub async fn gather_transactions_batch_multi( erc20_amounts, token_transfer.chain_id as u64, None, - max_fee_per_gas, - priority_fee, use_direct_method, use_unpacked_method, )? @@ -272,7 +265,7 @@ pub async fn gather_transactions_batch( sum += U256::from_dec_str(&token_transfer.token_amount).map_err(err_from!())?; } - let Ok(chain_setup) = payment_setup.get_chain_setup(token_transfer.chain_id) else { + let Ok(_chain_setup) = payment_setup.get_chain_setup(token_transfer.chain_id) else { send_driver_event( &event_sender, DriverEventContent::TransactionFailed(TransactionFailedReason::InvalidChainId( @@ -286,9 +279,6 @@ pub async fn gather_transactions_batch( )); }; - let max_fee_per_gas = chain_setup.max_fee_per_gas; - let priority_fee = chain_setup.priority_fee; - log::debug!("Processing token transfer {:?}", token_transfer); let web3tx = if let Some(token_addr) = token_transfer.token_addr.as_ref() { create_erc20_transfer( @@ -298,8 +288,6 @@ pub async fn gather_transactions_batch( sum, token_transfer.chain_id as u64, None, - max_fee_per_gas, - priority_fee, )? } else { create_eth_transfer( @@ -307,8 +295,6 @@ pub async fn gather_transactions_batch( Address::from_str(&token_transfer.receiver_addr).map_err(err_from!())?, token_transfer.chain_id as u64, None, - max_fee_per_gas, - priority_fee, sum, ) }; diff --git a/crates/erc20_payment_lib/src/sender/process.rs b/crates/erc20_payment_lib/src/sender/process.rs index 0a5a26e8..a7517f31 100644 --- a/crates/erc20_payment_lib/src/sender/process.rs +++ b/crates/erc20_payment_lib/src/sender/process.rs @@ -143,8 +143,16 @@ pub async fn process_transaction( } } } - let max_fee_per_gas = - U256::from_dec_str(&web3_tx_dao.max_fee_per_gas).map_err(err_from!())?; + let mut max_fee_per_gas = if let Some(max_fee_per_gas) = &web3_tx_dao.max_fee_per_gas { + U256::from_dec_str(max_fee_per_gas).map_err(err_from!())? + } else { + chain_setup.max_fee_per_gas + }; + let max_priority_fee = if let Some(priority_fee) = &web3_tx_dao.priority_fee { + U256::from_dec_str(priority_fee).map_err(err_from!())? + } else { + chain_setup.priority_fee + }; if is_polygon_eco_mode { let blockchain_gas_price = web3 @@ -191,8 +199,7 @@ pub async fn process_transaction( } new_target_gas_u256 }; - let tx_priority_fee_u256 = - U256::from_dec_str(&web3_tx_dao.priority_fee).map_err(err_from!())?; + let tx_priority_fee_u256 = max_priority_fee; //max_fee_per_gas cannot be lower than priority fee if new_target_gas_u256 < tx_priority_fee_u256 { @@ -205,10 +212,11 @@ pub async fn process_transaction( new_target_gas_u256.to_gwei().map_err(err_from!())?, ); - web3_tx_dao.max_fee_per_gas = new_target_gas_u256.to_string(); + max_fee_per_gas = new_target_gas_u256; } } - + web3_tx_dao.max_fee_per_gas = Some(max_fee_per_gas.to_string()); + web3_tx_dao.priority_fee = Some(max_priority_fee.to_string()); web3_tx_dao.nonce = Some(nonce); nonce }; @@ -557,9 +565,23 @@ pub async fn process_transaction( .await .map_err(err_from!())?; - let tx_fee_per_gas = web3_tx_dao.max_fee_per_gas.to_gwei().map_err(err_from!())?; + let max_tx_fee_per_gas_str = + web3_tx_dao + .max_fee_per_gas + .clone() + .ok_or(err_create!(TransactionFailedError::new( + "Max fee per gas not found" + )))?; + let max_tx_priority_fee_str = + web3_tx_dao + .priority_fee + .clone() + .ok_or(err_create!(TransactionFailedError::new( + "Priority fee not found" + )))?; + let tx_fee_per_gas = max_tx_fee_per_gas_str.to_gwei().map_err(err_from!())?; let max_fee_per_gas = chain_setup.max_fee_per_gas.to_gwei().map_err(err_from!())?; - let tx_pr_fee_u256 = web3_tx_dao.priority_fee.to_u256().map_err(err_from!())?; + let tx_pr_fee_u256 = max_tx_priority_fee_str.to_u256().map_err(err_from!())?; let tx_pr_fee = tx_pr_fee_u256.to_gwei().map_err(err_from!())?; let config_priority_fee = chain_setup.priority_fee.to_gwei().map_err(err_from!())?; @@ -594,14 +616,14 @@ pub async fn process_transaction( fee_per_gas_bumped_10 = true; log::warn!( "Transaction max fee bumped more than 10% from {} to {} for tx: {}", - web3_tx_dao.max_fee_per_gas, + max_tx_fee_per_gas_str, chain_setup.max_fee_per_gas, web3_tx_dao.id ); } else { log::warn!( "Transaction max fee changed less than 10% more from {} to {} for tx: {}", - web3_tx_dao.max_fee_per_gas, + max_tx_fee_per_gas_str, chain_setup.max_fee_per_gas, web3_tx_dao.id ); @@ -616,12 +638,12 @@ pub async fn process_transaction( priority_fee_changed_10 = true; log::warn!( "Transaction priority fee bumped more than 10% from {} to {} for tx: {}", - web3_tx_dao.priority_fee, + max_tx_priority_fee_str, chain_setup.priority_fee, web3_tx_dao.id ); } else { - log::warn!("Transaction priority fee changed less than 10% more from {} to {} for tx: {}", web3_tx_dao.priority_fee, chain_setup.priority_fee, web3_tx_dao.id); + log::warn!("Transaction priority fee changed less than 10% more from {} to {} for tx: {}", max_tx_priority_fee_str, chain_setup.priority_fee, web3_tx_dao.id); } } @@ -659,8 +681,8 @@ pub async fn process_transaction( to_addr: tx.to_addr.clone(), chain_id: tx.chain_id, gas_limit: tx.gas_limit, - max_fee_per_gas: replacement_max_fee_per_gas.to_string(), - priority_fee: replacement_priority_fee.to_string(), + max_fee_per_gas: Some(replacement_max_fee_per_gas.to_string()), + priority_fee: Some(replacement_priority_fee.to_string()), val: tx.val.clone(), nonce: tx.nonce, processing: tx.processing, @@ -754,8 +776,13 @@ pub async fn process_transaction( .to_gwei() .map_err(err_from!())?; + let max_tx_fee_per_gas_str = + web3_tx_dao.max_fee_per_gas.clone().ok_or(err_create!( + TransactionFailedError::new("Max fee per gas not found") + ))?; + let tx_max_fee_per_gas_gwei = - web3_tx_dao.max_fee_per_gas.to_gwei().map_err(err_from!())?; + max_tx_fee_per_gas_str.to_gwei().map_err(err_from!())?; let assumed_min_priority_fee_gwei = if web3_tx_dao.chain_id == 137 { const POLYGON_MIN_PRIORITY_FEE_FOR_GAS_PRICE_CHECK: u32 = 30; Decimal::from(POLYGON_MIN_PRIORITY_FEE_FOR_GAS_PRICE_CHECK) diff --git a/crates/erc20_payment_lib/src/transaction.rs b/crates/erc20_payment_lib/src/transaction.rs index 019733d0..5c40bf6f 100644 --- a/crates/erc20_payment_lib/src/transaction.rs +++ b/crates/erc20_payment_lib/src/transaction.rs @@ -41,10 +41,22 @@ pub fn dao_to_call_request(web3_tx_dao: &TxDao) -> Result Result, - max_fee_per_gas: U256, - priority_fee: U256, amount: U256, ) -> TxDao { TxDao { @@ -119,8 +141,8 @@ pub fn create_eth_transfer( to_addr: format!("{to:#x}"), chain_id: chain_id as i64, gas_limit: gas_limit.map(|gas_limit| gas_limit as i64), - max_fee_per_gas: max_fee_per_gas.to_string(), - priority_fee: priority_fee.to_string(), + max_fee_per_gas: None, + priority_fee: None, val: amount.to_string(), nonce: None, processing: 1, @@ -149,8 +171,6 @@ pub fn create_eth_transfer_str( to_addr: String, chain_id: u64, gas_limit: Option, - max_fee_per_gas: String, - priority_fee: String, amount: String, ) -> TxDao { TxDao { @@ -160,8 +180,8 @@ pub fn create_eth_transfer_str( to_addr, chain_id: chain_id as i64, gas_limit: gas_limit.map(|gas_limit| gas_limit as i64), - max_fee_per_gas, - priority_fee, + max_fee_per_gas: None, + priority_fee: None, val: amount, nonce: None, processing: 1, @@ -192,8 +212,6 @@ pub fn create_erc20_transfer( erc20_amount: U256, chain_id: u64, gas_limit: Option, - max_fee_per_gas: U256, - priority_fee: U256, ) -> Result { Ok(TxDao { id: 0, @@ -202,8 +220,8 @@ pub fn create_erc20_transfer( to_addr: format!("{token:#x}"), chain_id: chain_id as i64, gas_limit: gas_limit.map(|gas_limit| gas_limit as i64), - max_fee_per_gas: max_fee_per_gas.to_string(), - priority_fee: priority_fee.to_string(), + max_fee_per_gas: None, + priority_fee: None, val: "0".to_string(), nonce: None, processing: 1, @@ -237,8 +255,6 @@ pub fn create_erc20_transfer_multi( erc20_amount: Vec, chain_id: u64, gas_limit: Option, - max_fee_per_gas: U256, - priority_fee: U256, direct: bool, unpacked: bool, ) -> Result { @@ -277,8 +293,8 @@ pub fn create_erc20_transfer_multi( to_addr: format!("{contract:#x}"), chain_id: chain_id as i64, gas_limit: gas_limit.map(|gas_limit| gas_limit as i64), - max_fee_per_gas: max_fee_per_gas.to_string(), - priority_fee: priority_fee.to_string(), + max_fee_per_gas: None, + priority_fee: None, val: "0".to_string(), nonce: None, processing: 1, @@ -307,8 +323,6 @@ pub fn create_erc20_approve( contract_to_approve: Address, chain_id: u64, gas_limit: Option, - max_fee_per_gas: U256, - priority_fee: U256, ) -> Result { Ok(TxDao { id: 0, @@ -317,8 +331,8 @@ pub fn create_erc20_approve( to_addr: format!("{token:#x}"), chain_id: chain_id as i64, gas_limit: gas_limit.map(|gas_limit| gas_limit as i64), - max_fee_per_gas: max_fee_per_gas.to_string(), - priority_fee: priority_fee.to_string(), + max_fee_per_gas: None, + priority_fee: None, val: "0".to_string(), nonce: None, processing: 1, @@ -392,8 +406,13 @@ pub async fn check_transaction( log::info!("Set gas limit basing on gas estimation: {gas_limit}"); web3_tx_dao.gas_limit = Some(gas_limit.as_u64() as i64); - let max_fee_per_gas = - U256::from_dec_str(&web3_tx_dao.max_fee_per_gas).map_err(err_from!())?; + let max_fee_per_gas = U256::from_dec_str( + &web3_tx_dao + .max_fee_per_gas + .clone() + .ok_or(err_custom_create!("max_fee_per_gas has to be set here"))?, + ) + .map_err(err_from!())?; let gas_needed_for_tx = U256::from_dec_str(&web3_tx_dao.val).map_err(err_from!())?; let maximum_gas_needed = gas_needed_for_tx + gas_limit * max_fee_per_gas; Ok(maximum_gas_needed)