forge_verify/
retry.rs

1use clap::{builder::RangedU64ValueParser, Parser};
2use foundry_common::retry::Retry;
3use std::time::Duration;
4
5/// Retry config used when waiting for verification
6pub const RETRY_CHECK_ON_VERIFY: RetryArgs = RetryArgs { retries: 8, delay: 15 };
7
8/// Retry config used when waiting for a created contract
9pub const RETRY_VERIFY_ON_CREATE: RetryArgs = RetryArgs { retries: 15, delay: 5 };
10
11/// Retry arguments for contract verification.
12#[derive(Clone, Copy, Debug, Parser)]
13#[command(about = "Allows to use retry arguments for contract verification")] // override doc
14pub struct RetryArgs {
15    /// Number of attempts for retrying verification.
16    #[arg(
17        long,
18        value_parser = RangedU64ValueParser::<u32>::new().range(1..),
19        default_value = "5",
20    )]
21    pub retries: u32,
22
23    /// Optional delay to apply in between verification attempts, in seconds.
24    #[arg(
25        long,
26        value_parser = RangedU64ValueParser::<u32>::new().range(0..=180),
27        default_value = "5",
28    )]
29    pub delay: u32,
30}
31
32impl Default for RetryArgs {
33    fn default() -> Self {
34        RETRY_VERIFY_ON_CREATE
35    }
36}
37
38impl RetryArgs {
39    /// Converts the arguments into a `Retry` instance.
40    pub fn into_retry(self) -> Retry {
41        Retry::new(self.retries, Duration::from_secs(self.delay as u64))
42    }
43}
44
45#[cfg(test)]
46mod tests {
47    use super::*;
48
49    #[test]
50    fn test_cli() {
51        let args = RetryArgs::parse_from(["foundry-cli", "--retries", "10"]);
52        assert_eq!(args.retries, 10);
53        assert_eq!(args.delay, 5);
54
55        let args = RetryArgs::parse_from(["foundry-cli", "--delay", "10"]);
56        assert_eq!(args.retries, 5);
57        assert_eq!(args.delay, 10);
58
59        let args = RetryArgs::parse_from(["foundry-cli", "--retries", "10", "--delay", "10"]);
60        assert_eq!(args.retries, 10);
61        assert_eq!(args.delay, 10);
62    }
63}