-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathconfig.rs
More file actions
132 lines (115 loc) · 3.24 KB
/
config.rs
File metadata and controls
132 lines (115 loc) · 3.24 KB
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
//! TOML config types, parsing, and validation.
use alloy_primitives::Address;
use serde::Deserialize;
use std::path::Path;
/// Top-level deploy configuration.
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub(crate) struct DeployConfig {
/// Chain configuration.
pub chain: ChainConfig,
/// Contract configurations.
#[serde(default)]
pub contracts: ContractsConfig,
}
/// Chain-level settings.
#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub(crate) struct ChainConfig {
/// The chain ID.
pub chain_id: u64,
}
/// All contract configurations.
#[derive(Debug, Deserialize, Default)]
pub(crate) struct ContractsConfig {
/// `AdminProxy` contract config (optional).
pub admin_proxy: Option<AdminProxyConfig>,
/// `Permit2` contract config (optional).
pub permit2: Option<Permit2Config>,
}
/// `AdminProxy` configuration.
#[derive(Debug, Deserialize)]
pub(crate) struct AdminProxyConfig {
/// Address to deploy at.
pub address: Address,
/// Owner address.
pub owner: Address,
}
/// `Permit2` configuration (Uniswap token approval manager).
#[derive(Debug, Deserialize)]
pub(crate) struct Permit2Config {
/// Address to deploy at.
pub address: Address,
}
impl DeployConfig {
/// Load and validate config from a TOML file.
pub(crate) fn load(path: &Path) -> eyre::Result<Self> {
let content = std::fs::read_to_string(path)?;
let config: Self = toml::from_str(&content)?;
config.validate()?;
Ok(config)
}
/// Validate config values.
fn validate(&self) -> eyre::Result<()> {
if let Some(ref ap) = self.contracts.admin_proxy {
eyre::ensure!(
!ap.owner.is_zero(),
"admin_proxy.owner must not be the zero address"
);
}
Ok(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_full_config() {
let toml = r#"
[chain]
chain_id = 1234
[contracts.admin_proxy]
address = "0x000000000000000000000000000000000000Ad00"
owner = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
"#;
let config: DeployConfig = toml::from_str(toml).unwrap();
assert_eq!(config.chain.chain_id, 1234);
assert!(config.contracts.admin_proxy.is_some());
config.validate().unwrap();
}
#[test]
fn reject_zero_owner() {
let toml = r#"
[chain]
chain_id = 1
[contracts.admin_proxy]
address = "0x000000000000000000000000000000000000Ad00"
owner = "0x0000000000000000000000000000000000000000"
"#;
let config: DeployConfig = toml::from_str(toml).unwrap();
assert!(config.validate().is_err());
}
#[test]
fn no_contracts_section() {
let toml = r#"
[chain]
chain_id = 1
"#;
let config: DeployConfig = toml::from_str(toml).unwrap();
config.validate().unwrap();
assert!(config.contracts.admin_proxy.is_none());
}
#[test]
fn admin_proxy_only() {
let toml = r#"
[chain]
chain_id = 1
[contracts.admin_proxy]
address = "0x000000000000000000000000000000000000Ad00"
owner = "0xf39Fd6e51aad88F6F4ce6aB8827279cffFb92266"
"#;
let config: DeployConfig = toml::from_str(toml).unwrap();
config.validate().unwrap();
assert!(config.contracts.admin_proxy.is_some());
}
}