-
Notifications
You must be signed in to change notification settings - Fork 33
Expand file tree
/
Copy pathchains.rs
More file actions
216 lines (194 loc) · 6.84 KB
/
chains.rs
File metadata and controls
216 lines (194 loc) · 6.84 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
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
// Copyright (c) 2019, MASQ (https://masq.ai) and/or its affiliates. All rights reserved.
use crate::blockchains::blockchain_records::{BlockchainRecord, CHAINS};
use crate::constants::{
BASE_MAINNET_FULL_IDENTIFIER, BASE_SEPOLIA_FULL_IDENTIFIER, DEFAULT_CHAIN,
DEV_CHAIN_FULL_IDENTIFIER, ETH_MAINNET_FULL_IDENTIFIER, ETH_ROPSTEN_FULL_IDENTIFIER,
POLYGON_AMOY_FULL_IDENTIFIER, POLYGON_MAINNET_FULL_IDENTIFIER,
};
use serde_derive::{Deserialize, Serialize};
use std::fmt::{Display, Formatter};
#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum Chain {
EthMainnet,
EthRopsten,
PolyMainnet,
PolyAmoy,
BaseMainnet,
BaseSepolia,
Dev,
}
impl Default for Chain {
fn default() -> Self {
DEFAULT_CHAIN
}
}
impl From<&str> for Chain {
fn from(str: &str) -> Self {
if str == POLYGON_MAINNET_FULL_IDENTIFIER {
Chain::PolyMainnet
} else if str == ETH_MAINNET_FULL_IDENTIFIER {
Chain::EthMainnet
} else if str == BASE_MAINNET_FULL_IDENTIFIER {
Chain::BaseMainnet
} else if str == BASE_SEPOLIA_FULL_IDENTIFIER {
Chain::BaseSepolia
} else if str == POLYGON_AMOY_FULL_IDENTIFIER {
Chain::PolyAmoy
} else if str == ETH_ROPSTEN_FULL_IDENTIFIER {
Chain::EthRopsten
} else if str == DEV_CHAIN_FULL_IDENTIFIER {
Chain::Dev
} else {
panic!("Clap let in a wrong value for chain: '{}'; if this happens we need to track down the slit", str)
}
}
}
impl Display for Chain {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let chain_name = match self {
Chain::EthMainnet => ETH_MAINNET_FULL_IDENTIFIER,
Chain::EthRopsten => ETH_ROPSTEN_FULL_IDENTIFIER,
Chain::PolyMainnet => POLYGON_MAINNET_FULL_IDENTIFIER,
Chain::PolyAmoy => POLYGON_AMOY_FULL_IDENTIFIER,
Chain::BaseMainnet => BASE_MAINNET_FULL_IDENTIFIER,
Chain::BaseSepolia => BASE_SEPOLIA_FULL_IDENTIFIER,
Chain::Dev => DEV_CHAIN_FULL_IDENTIFIER,
};
write!(f, "{}", chain_name)
}
}
impl Chain {
pub fn rec(&self) -> &BlockchainRecord {
CHAINS
.iter()
.find(|b| &b.self_id == self)
.unwrap_or_else(|| panic!("BlockchainRecord for '{:?}' doesn't exist", self))
//untested panic - but works as an expect()
}
pub fn is_mainnet(&self) -> bool {
Self::mainnets()
.iter()
.any(|mainnet_chain| mainnet_chain == self)
}
fn mainnets() -> &'static [Chain] {
&[Chain::PolyMainnet, Chain::BaseMainnet, Chain::EthMainnet]
}
}
pub fn chain_from_chain_identifier_opt(identifier: &str) -> Option<Chain> {
return_record_opt_standard_impl(&|b: &&BlockchainRecord| b.literal_identifier == identifier)
.map(|record| record.self_id)
}
fn return_record_opt_standard_impl(
closure: &dyn Fn(&&BlockchainRecord) -> bool,
) -> Option<&BlockchainRecord> {
return_record_opt_body(closure, &CHAINS)
}
fn return_record_opt_body<'a>(
closure: &dyn Fn(&&'a BlockchainRecord) -> bool,
collection_of_chains: &'a [BlockchainRecord],
) -> Option<&'a BlockchainRecord> {
let filtered = collection_of_chains
.iter()
.filter(closure)
.collect::<Vec<&BlockchainRecord>>();
match filtered.len() {
0 => None,
1 => Some(filtered[0]),
_ => panic!("Non-unique identifier used to query a BlockchainRecord"),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
#[should_panic(expected = "Non-unique identifier used to query a BlockchainRecord")]
fn return_record_opt_panics_if_more_records_meet_the_condition_from_the_closure() {
let searched_name = "BruhBruh";
let mut record_one = make_defaulted_blockchain_record();
record_one.literal_identifier = searched_name;
let mut record_two = make_defaulted_blockchain_record();
record_two.literal_identifier = "Jooodooo";
let mut record_three = make_defaulted_blockchain_record();
record_three.literal_identifier = searched_name;
let collection = [record_one, record_two, record_three];
let _ = return_record_opt_body(
&|b: &&BlockchainRecord| b.literal_identifier == searched_name,
&collection,
);
}
#[test]
fn return_record_opt_standard_impl_uses_the_right_collection_of_chains() {
CHAINS.iter().for_each(|record| {
assert_eq!(
record,
return_record_opt_standard_impl(
&|b: &&BlockchainRecord| b.num_chain_id == record.num_chain_id
)
.unwrap()
)
});
}
#[test]
#[should_panic(
expected = "Clap let in a wrong value for chain: 'olala'; if this happens we need to track down the slit"
)]
fn gibberish_causes_a_panic() {
let _ = Chain::from("olala");
}
fn make_defaulted_blockchain_record<'a>() -> BlockchainRecord {
BlockchainRecord {
num_chain_id: 0,
self_id: Chain::PolyMainnet,
literal_identifier: "",
gas_price_safe_ceiling_minor: 0,
default_pending_payable_interval_sec: 0,
contract: Default::default(),
contract_creation_block: 0,
}
}
#[test]
fn is_mainnet_knows_about_all_mainnets() {
let searched_str = "mainnet";
assert_mainnet_exist();
CHAINS.iter().for_each(|blockchain_record| {
if blockchain_record.literal_identifier.contains(searched_str) {
let chain = blockchain_record.self_id;
assert_eq!(chain.is_mainnet(), true)
}
})
}
#[test]
fn display_is_properly_implemented() {
let chains = [
Chain::EthMainnet,
Chain::EthRopsten,
Chain::PolyMainnet,
Chain::PolyAmoy,
Chain::BaseMainnet,
Chain::BaseSepolia,
Chain::Dev,
];
let strings = chains
.iter()
.map(|chain| chain.to_string())
.collect::<Vec<_>>();
assert_eq!(
strings,
vec![
ETH_MAINNET_FULL_IDENTIFIER.to_string(),
ETH_ROPSTEN_FULL_IDENTIFIER.to_string(),
POLYGON_MAINNET_FULL_IDENTIFIER.to_string(),
POLYGON_AMOY_FULL_IDENTIFIER.to_string(),
BASE_MAINNET_FULL_IDENTIFIER.to_string(),
BASE_SEPOLIA_FULL_IDENTIFIER.to_string(),
DEV_CHAIN_FULL_IDENTIFIER.to_string(),
]
);
}
fn assert_mainnet_exist() {
assert!(CHAINS
.iter()
.find(|blockchain_record| blockchain_record.literal_identifier.contains("mainnet"))
.is_some());
}
}