evmlib 0.4.9

Safe Network EVM
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
use crate::TX_TIMEOUT;
use crate::common::{Address, Calldata, TxHash};
use crate::transaction_config::{MaxFeePerGas, TransactionConfig};
use alloy::network::{Network, ReceiptResponse, TransactionBuilder};
use alloy::providers::{PendingTransactionBuilder, Provider};
use std::time::Duration;

pub(crate) const MAX_RETRIES: u8 = 3;
const DEFAULT_RETRY_INTERVAL_MS: u64 = 4000;
const BROADCAST_TRANSACTION_TIMEOUT_MS: u64 = 5000;
const WATCH_TIMEOUT_MS: u64 = 1000;

/// Gas information from a transaction
#[derive(Debug, Clone, Default)]
pub struct GasInfo {
    /// Estimated gas before buffer
    pub estimated_gas: u64,
    /// Gas limit with buffer (what was actually set on the transaction)
    pub gas_with_buffer: u64,
    /// Max fee per gas (EIP-1559)
    pub max_fee_per_gas: Option<u128>,
    /// Max priority fee per gas (EIP-1559)
    pub max_priority_fee_per_gas: Option<u128>,
    /// Actual gas used (from receipt)
    pub actual_gas_used: u64,
    /// Effective gas price paid (from receipt, in wei)
    pub effective_gas_price: u128,
    /// Total gas cost in wei (actual_gas_used * effective_gas_price)
    pub gas_cost_wei: u128,
}

impl std::fmt::Display for GasInfo {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        let gwei_cost = self.gas_cost_wei as f64 / 1e9;
        write!(
            f,
            "{gwei_cost:.6} gwei ({} gas @ {} wei/gas)",
            self.actual_gas_used, self.effective_gas_price
        )
    }
}

#[derive(thiserror::Error, Debug)]
pub enum TransactionError {
    #[error("Could not get current gas price: {0}")]
    CouldNotGetGasPrice(String),
    #[error("Gas price is above limit: {0}")]
    GasPriceAboveLimit(u128),
    #[error("Transaction failed to send: {0}")]
    TransactionFailedToSend(String),
    #[error("Transaction failed to confirm in time: {0}")]
    TransactionFailedToConfirm(String, Option<u64>), // Includes the nonce
    #[error("Transaction reverted with data")]
    TransactionReverted {
        message: String,
        revert_data: Option<alloy::primitives::Bytes>,
        nonce: Option<u64>,
    },
}

/// Execute an async closure that returns a result. Retry on failure.
pub(crate) async fn retry<F, Fut, T, E>(
    mut action: F,
    operation_id: &str,
    retry_interval_ms: Option<u64>,
) -> Result<T, E>
where
    F: FnMut() -> Fut + Send,
    Fut: std::future::Future<Output = Result<T, E>> + Send,
    E: std::fmt::Debug,
{
    let mut retries = 0;

    loop {
        match action().await {
            Ok(result) => return Ok(result),
            Err(err) => {
                if retries == MAX_RETRIES {
                    error!("{operation_id} failed after {retries} retries: {err:?}");
                    return Err(err);
                }

                retries += 1;
                let retry_interval_ms = retry_interval_ms.unwrap_or(DEFAULT_RETRY_INTERVAL_MS);
                let delay = Duration::from_millis(retry_interval_ms * retries.pow(2) as u64);

                warn!(
                    "Error trying {operation_id}: {err:?}. Retry #{retries} in {:?} second(s).",
                    delay.as_secs()
                );

                tokio::time::sleep(delay).await;
            }
        }
    }
}

/// Generic function to send a transaction with retries.
/// Returns the transaction hash and gas information.
pub(crate) async fn send_transaction_with_retries<P, N>(
    provider: &P,
    calldata: Calldata,
    to: Address,
    tx_identifier: &str,
    transaction_config: &TransactionConfig,
) -> Result<(TxHash, GasInfo), TransactionError>
where
    P: Provider<N>,
    N: Network,
{
    let mut previous_nonce: Option<u64> = None;
    let mut retries: u8 = 0;

    loop {
        match send_transaction(
            provider,
            calldata.clone(),
            to,
            previous_nonce,
            tx_identifier,
            transaction_config,
        )
        .await
        {
            Ok((tx_hash, gas_info)) => break Ok((tx_hash, gas_info)),
            Err(err) => {
                if retries == MAX_RETRIES {
                    error!(
                        "Transaction {tx_identifier} failed after {retries} retries. Giving up. Error: {err:?}"
                    );
                    break Err(err);
                }

                match err {
                    TransactionError::CouldNotGetGasPrice(reason) => {
                        warn!("Could not get gas price: {reason}");
                    }
                    TransactionError::GasPriceAboveLimit(limit) => {
                        warn!("Gas price is above limit: {limit}");
                    }
                    TransactionError::TransactionFailedToSend(reason) => {
                        warn!("Transaction failed to send: {reason}");
                    }
                    TransactionError::TransactionFailedToConfirm(reason, nonce) => {
                        warn!("Transaction failed to confirm: {reason} (nonce: {nonce:?})");
                        previous_nonce = nonce;
                    }
                    TransactionError::TransactionReverted {
                        ref message,
                        ref revert_data,
                        ref nonce,
                    } => {
                        warn!(
                            "Transaction reverted: {message} (nonce: {nonce:?}, has_data: {})",
                            revert_data.is_some()
                        );
                        // Don't retry on revert - the transaction will keep reverting
                        error!(
                            "Transaction {tx_identifier} reverted. Not retrying. Error: {message}"
                        );
                        break Err(err);
                    }
                }

                retries += 1;

                let retry_interval_ms = DEFAULT_RETRY_INTERVAL_MS;
                let delay = Duration::from_millis(retry_interval_ms * retries.pow(2) as u64);

                warn!(
                    "Retrying transaction (attempt {}) in {} second(s).",
                    retries,
                    delay.as_secs(),
                );

                tokio::time::sleep(delay).await;

                continue;
            }
        }
    }
}

async fn send_transaction<P, N>(
    provider: &P,
    calldata: Calldata,
    to: Address,
    mut nonce: Option<u64>,
    tx_identifier: &str,
    transaction_config: &TransactionConfig,
) -> Result<(TxHash, GasInfo), TransactionError>
where
    P: Provider<N>,
    N: Network,
{
    let eip1559_fees = get_eip1559_fees(provider, transaction_config).await?;

    debug!("eip1559 fees: {eip1559_fees:?}");

    let mut transaction_request = provider
        .transaction_request()
        .with_to(to)
        .with_input(calldata.clone());

    if let Some(fees) = &eip1559_fees {
        transaction_request.set_max_fee_per_gas(fees.max_fee_per_gas);
        transaction_request.set_max_priority_fee_per_gas(fees.max_priority_fee_per_gas);
    }

    // Estimate gas and add 20% buffer to avoid out-of-gas reverts
    // This is especially important for Arbitrum L2 where gas estimation can be tricky
    let estimated_gas = provider
        .estimate_gas(transaction_request.clone())
        .await
        .map_err(|e| {
            TransactionError::TransactionFailedToSend(format!("gas estimation failed: {e}"))
        })?;
    let gas_with_buffer = estimated_gas.saturating_mul(120) / 100;
    debug!("Estimated gas: {estimated_gas}, with 20% buffer: {gas_with_buffer}");
    transaction_request.set_gas_limit(gas_with_buffer);

    // Prepare gas info (actual values will be set from receipt)
    let mut gas_info = GasInfo {
        estimated_gas,
        gas_with_buffer,
        max_fee_per_gas: eip1559_fees.as_ref().map(|f| f.max_fee_per_gas),
        max_priority_fee_per_gas: eip1559_fees.as_ref().map(|f| f.max_priority_fee_per_gas),
        actual_gas_used: 0,
        effective_gas_price: 0,
        gas_cost_wei: 0,
    };

    // Retry with the same nonce to replace a stuck transaction
    if let Some(nonce) = nonce {
        transaction_request.set_nonce(nonce);
    } else {
        nonce = transaction_request.nonce();
    }

    let pending_tx_builder_result = tokio::time::timeout(
        Duration::from_millis(BROADCAST_TRANSACTION_TIMEOUT_MS),
        provider.send_transaction(transaction_request.clone()),
    )
    .await;

    let pending_tx_builder = match pending_tx_builder_result {
        Ok(Ok(pending_tx_builder)) => pending_tx_builder,
        Ok(Err(err)) => return Err(TransactionError::TransactionFailedToSend(err.to_string())),
        Err(_) => {
            return Err(TransactionError::TransactionFailedToSend(
                "timeout".to_string(),
            ));
        }
    };

    debug!(
        "{tx_identifier} transaction is pending with tx_hash: {:?}",
        pending_tx_builder.tx_hash()
    );

    let watch_result = retry(
        || async {
            PendingTransactionBuilder::from_config(
                provider.root().clone(),
                pending_tx_builder.inner().clone(),
            )
            .with_timeout(Some(TX_TIMEOUT))
            .watch()
            .await
        },
        "watching pending transaction",
        Some(WATCH_TIMEOUT_MS),
    )
    .await;

    match watch_result {
        Ok(tx_hash) => {
            // CRITICAL: .watch() only confirms the tx was mined, NOT that it succeeded.
            // We must fetch the receipt and check the status field.
            match provider.get_transaction_receipt(tx_hash).await {
                Ok(Some(receipt)) => {
                    let gas_used = receipt.gas_used();
                    let effective_gas_price = receipt.effective_gas_price();
                    let gas_cost_wei = (gas_used as u128).saturating_mul(effective_gas_price);

                    gas_info.actual_gas_used = gas_used;
                    gas_info.effective_gas_price = effective_gas_price;
                    gas_info.gas_cost_wei = gas_cost_wei;

                    if receipt.status() {
                        debug!("{tx_identifier} transaction with hash {tx_hash:?} succeeded");
                        info!(
                            "Gas details: estimated={}, buffer={}, actual={}, effective_price={} wei, total_cost={} wei",
                            gas_info.estimated_gas,
                            gas_info.gas_with_buffer,
                            gas_used,
                            effective_gas_price,
                            gas_cost_wei
                        );
                        Ok((tx_hash, gas_info))
                    } else {
                        // Transaction was mined but reverted (e.g., out of gas)
                        error!(
                            "{tx_identifier} transaction {tx_hash:?} was mined but reverted. \
                            Gas used: {gas_used}"
                        );
                        Err(TransactionError::TransactionReverted {
                            message: format!(
                                "Transaction was mined but execution failed (gas used: {gas_used})"
                            ),
                            revert_data: None,
                            nonce,
                        })
                    }
                }
                Ok(None) => {
                    // This shouldn't happen after .watch() succeeds, but handle it
                    warn!("{tx_identifier} transaction {tx_hash:?} receipt not found after watch");
                    Err(TransactionError::TransactionFailedToConfirm(
                        "Receipt not found after watch".to_string(),
                        nonce,
                    ))
                }
                Err(err) => {
                    error!("{tx_identifier} failed to get receipt for {tx_hash:?}: {err}");
                    Err(TransactionError::TransactionFailedToConfirm(
                        format!("Failed to get receipt: {err}"),
                        nonce,
                    ))
                }
            }
        }
        Err(err) => {
            // Try to get more details about the revert if available
            let revert_data = extract_revert_data(&err);

            if revert_data.is_some() {
                Err(TransactionError::TransactionReverted {
                    message: err.to_string(),
                    revert_data,
                    nonce,
                })
            } else {
                Err(TransactionError::TransactionFailedToConfirm(
                    err.to_string(),
                    nonce,
                ))
            }
        }
    }
}

/// Extract revert data from a PendingTransactionError
///
/// When a transaction reverts, we try to extract the revert reason data.
/// This data can then be decoded by contract-specific error handlers.
fn extract_revert_data(
    err: &alloy::providers::PendingTransactionError,
) -> Option<alloy::primitives::Bytes> {
    // Try to extract from the error message/data
    // Alloy encodes revert data in the error, we just need to find it
    let err_str = err.to_string();

    // Look for revert data in the error string
    // Format is usually "revert: <hex data>" or contains the error signature
    if err_str.contains("revert") || err_str.contains("0x") {
        // Try to extract hex data from the error
        // This is a simplified approach - in reality the data structure varies
        debug!("Transaction reverted: {}", err_str);
    }

    // For now, we'll return None and handle this at a higher level
    // by using eth_call to simulate the transaction when it fails
    None
}

/// EIP-1559 fee parameters for a transaction.
#[derive(Debug, Clone, Copy)]
struct Eip1559Fees {
    max_fee_per_gas: u128,
    max_priority_fee_per_gas: u128,
}

async fn get_eip1559_fees<P: Provider<N>, N: Network>(
    provider: &P,
    transaction_config: &TransactionConfig,
) -> Result<Option<Eip1559Fees>, TransactionError> {
    match transaction_config.max_fee_per_gas {
        MaxFeePerGas::Auto => {
            debug!("Using Auto mode for gas fees");
            // Use EIP-1559 fee estimation which includes a buffer for base fee fluctuation
            let eip1559_fees = provider
                .estimate_eip1559_fees()
                .await
                .map_err(|err| TransactionError::CouldNotGetGasPrice(err.to_string()))?;
            Ok(Some(Eip1559Fees {
                max_fee_per_gas: eip1559_fees.max_fee_per_gas,
                max_priority_fee_per_gas: eip1559_fees.max_priority_fee_per_gas,
            }))
        }
        MaxFeePerGas::LimitedAuto(limit) => {
            debug!("Using LimitedAuto mode for gas fees with limit: {limit}");
            // Use EIP-1559 fee estimation for better accuracy
            let eip1559_fees = provider
                .estimate_eip1559_fees()
                .await
                .map_err(|err| TransactionError::CouldNotGetGasPrice(err.to_string()))?;

            if eip1559_fees.max_fee_per_gas > limit {
                warn!(
                    "Estimated max_fee_per_gas ({}) exceeds limit ({})",
                    eip1559_fees.max_fee_per_gas, limit
                );
                Err(TransactionError::GasPriceAboveLimit(limit))
            } else {
                Ok(Some(Eip1559Fees {
                    max_fee_per_gas: eip1559_fees.max_fee_per_gas,
                    max_priority_fee_per_gas: eip1559_fees.max_priority_fee_per_gas,
                }))
            }
        }
        MaxFeePerGas::Custom(max_fee) => {
            debug!("Using Custom mode for gas fees with max_fee: {max_fee}");
            // Use custom max fee with estimated priority fee
            let eip1559_fees = provider
                .estimate_eip1559_fees()
                .await
                .map_err(|err| TransactionError::CouldNotGetGasPrice(err.to_string()))?;

            if max_fee < eip1559_fees.max_fee_per_gas {
                warn!(
                    "Custom max_fee_per_gas ({}) is below estimated fee ({}). Transaction may be slow or fail.",
                    max_fee, eip1559_fees.max_fee_per_gas
                );
            }

            Ok(Some(Eip1559Fees {
                max_fee_per_gas: max_fee,
                max_priority_fee_per_gas: eip1559_fees.max_priority_fee_per_gas,
            }))
        }
        MaxFeePerGas::Unlimited => {
            debug!("Using Unlimited mode for gas fees (no fee parameters will be set)");
            Ok(None)
        }
    }
}