Skip to content

Commit

Permalink
Add option for tx gas dao
Browse files Browse the repository at this point in the history
  • Loading branch information
scx1332 committed Oct 24, 2023
1 parent f58933d commit 7bceff2
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 67 deletions.
4 changes: 2 additions & 2 deletions crates/erc20_payment_lib/src/db/model/tx_dao.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ pub struct TxDao {
pub to_addr: String,
pub chain_id: i64,
pub gas_limit: Option<i64>,
pub max_fee_per_gas: String,
pub priority_fee: String,
pub max_fee_per_gas: Option<String>,
pub priority_fee: Option<String>,
pub val: String,
pub nonce: Option<i64>,
pub processing: i64,
Expand Down
10 changes: 6 additions & 4 deletions crates/erc20_payment_lib/src/db/ops/tx_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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()),
Expand All @@ -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,
Expand All @@ -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?;
Expand Down
5 changes: 0 additions & 5 deletions crates/erc20_payment_lib/src/sender/allowance.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ pub async fn process_allowance(
signer: &impl Signer,
) -> Result<u32, PaymentError> {
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,
Expand Down Expand Up @@ -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)
Expand Down
16 changes: 1 addition & 15 deletions crates/erc20_payment_lib/src/sender/batching.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand Down Expand Up @@ -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());
Expand All @@ -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,
)?
Expand Down Expand Up @@ -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(
Expand All @@ -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(
Expand All @@ -298,17 +288,13 @@ pub async fn gather_transactions_batch(
sum,
token_transfer.chain_id as u64,
None,
max_fee_per_gas,
priority_fee,
)?
} else {
create_eth_transfer(
Address::from_str(&token_transfer.from_addr).map_err(err_from!())?,
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,
)
};
Expand Down
57 changes: 42 additions & 15 deletions crates/erc20_payment_lib/src/sender/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand All @@ -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
};
Expand Down Expand Up @@ -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!())?;

Expand Down Expand Up @@ -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
);
Expand All @@ -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);
}
}

Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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)
Expand Down
Loading

0 comments on commit 7bceff2

Please sign in to comment.