Skip to main content

varro/
builder.rs

1use std::time::Duration;
2
3use eyre::Result;
4use ethers_core::types::{Address, H256};
5
6use crate::{
7    metrics::Metrics, L1Client, rollup::RollupNode, OutputOracle, client::Varro, config::Config,
8};
9
10/// VarroBuilder
11///
12/// A builder for the [Varro] client.
13#[derive(Debug, Default, Clone)]
14pub struct VarroBuilder {
15    /// An L1 [L1Client]
16    pub l1_client: Option<L1Client>,
17    /// A [RollupNode] client
18    pub rollup_node: Option<RollupNode>,
19    /// An output oracle contract
20    pub output_oracle: Option<OutputOracle>,
21    /// Whether to use non-finalized L1 data to propose L2 blocks.
22    pub allow_non_finalized: Option<bool>,
23    /// The proposer
24    pub proposer: Option<Address>,
25    /// The proposer's private key used to send the output transactions.
26    pub output_private_key: Option<H256>,
27    /// The polling interval
28    pub polling_interval: Option<Duration>,
29    /// An _optional_ metrics server
30    pub metrics: Option<Metrics>,
31}
32
33impl TryFrom<Config> for VarroBuilder {
34    type Error = eyre::Report;
35
36    fn try_from(conf: Config) -> std::result::Result<Self, Self::Error> {
37        Ok(Self {
38            l1_client: Some(conf.get_l1_client()?),
39            rollup_node: Some(conf.get_rollup_node_client()?),
40            output_oracle: Some(conf.get_output_oracle()?),
41            allow_non_finalized: Some(conf.allow_non_finalized),
42            proposer: Some(conf.output_oracle_address),
43            output_private_key: Some(conf.get_output_private_key()?),
44            polling_interval: Some(conf.polling_interval),
45            metrics: None,
46        })
47    }
48}
49
50impl VarroBuilder {
51    /// Creates a new [VarroBuilder].
52    pub fn new() -> Self {
53        Self::default()
54    }
55
56    /// Sets the metrics server for the [Varro] client.
57    pub fn with_metrics(&mut self, metrics: Metrics) -> &mut Self {
58        self.metrics = Some(metrics);
59        self
60    }
61
62    /// Sets the L1 client for the [Varro] client.
63    pub fn with_l1_client(&mut self, l1_client: L1Client) -> &mut Self {
64        self.l1_client = Some(l1_client);
65        self
66    }
67
68    /// Sets the rollup node for the [Varro] client.
69    pub fn with_rollup_node(&mut self, rollup_node: RollupNode) -> &mut Self {
70        self.rollup_node = Some(rollup_node);
71        self
72    }
73
74    /// Sets the output oracle for the [Varro] client.
75    pub fn with_output_oracle(&mut self, output_oracle: OutputOracle) -> &mut Self {
76        self.output_oracle = Some(output_oracle);
77        self
78    }
79
80    /// Sets whether to use non-finalized L1 data to propose L2 blocks.
81    pub fn with_allow_non_finalized(&mut self, allow_non_finalized: bool) -> &mut Self {
82        self.allow_non_finalized = Some(allow_non_finalized);
83        self
84    }
85
86    /// Sets the proposer for the [Varro] client.
87    pub fn with_proposer(&mut self, proposer: Address) -> &mut Self {
88        self.proposer = Some(proposer);
89        self
90    }
91
92    /// Sets the proposer's private key used to send the output transactions.
93    /// for the [Varro] client.
94    pub fn with_output_private_key(&mut self, output_private_key: H256) -> &mut Self {
95        self.output_private_key = Some(output_private_key);
96        self
97    }
98
99    /// Sets the polling interval for the [Varro] client.
100    pub fn with_polling_interval(&mut self, polling_interval: Duration) -> &mut Self {
101        self.polling_interval = Some(polling_interval);
102        self
103    }
104
105    /// Builds the [Varro] client.
106    pub fn build(self) -> Result<Varro> {
107        Ok(Varro::try_from(self)?)
108    }
109}