polyte_clob/core/
chain.rs1use alloy::primitives::{address, Address};
2
3#[derive(Debug, Clone, Copy, PartialEq, Eq)]
4pub enum Chain {
5 PolygonMainnet,
6 PolygonAmoy,
7}
8
9impl Chain {
10 pub const fn chain_id(&self) -> u64 {
12 match self {
13 Chain::PolygonMainnet => 137,
14 Chain::PolygonAmoy => 80002,
15 }
16 }
17
18 pub const fn contracts(&self) -> Contracts {
20 match self {
21 Chain::PolygonMainnet => Contracts::POLYGON_MAINNET,
22 Chain::PolygonAmoy => Contracts::POLYGON_AMOY,
23 }
24 }
25
26 pub const fn from_chain_id(chain_id: u64) -> Option<Self> {
28 match chain_id {
29 137 => Some(Self::PolygonMainnet),
30 80002 => Some(Self::PolygonAmoy),
31 _ => None,
32 }
33 }
34}
35
36#[derive(Debug, Clone, Copy)]
38pub struct Contracts {
39 pub exchange: Address,
40 pub neg_risk_exchange: Address,
41 pub neg_risk_adapter: Address,
42 pub collateral: Address,
43 pub conditional_tokens: Address,
44}
45
46impl Contracts {
47 pub const POLYGON_MAINNET: Self = Self {
49 exchange: address!("4bFb41d5B3570DeFd03C39a9A4D8dE6Bd8B8982E"),
50 neg_risk_exchange: address!("C5d563A36AE78145C45a50134d48A1215220f80a"),
51 neg_risk_adapter: address!("d91E80cF2E7be2e162c6513ceD06f1dD0dA35296"),
52 collateral: address!("2791Bca1f2de4661ED88A30C99A7a9449Aa84174"),
53 conditional_tokens: address!("4D97DCd97eC945f40cF65F87097ACe5EA0476045"),
54 };
55
56 pub const POLYGON_AMOY: Self = Self {
58 exchange: address!("dFE02Eb6733538f8Ea35D585af8DE5958AD99E40"),
59 neg_risk_exchange: address!("d91E80cF2E7be2e162c6513ceD06f1dD0dA35296"),
60 neg_risk_adapter: address!("d0D0E471E88e0A8E7C304F2df3A0Cc7400fe4635"),
61 collateral: address!("9c4e1703476e875070ee25b56a58b008cfb8fa78"),
62 conditional_tokens: address!("69308FB512518e39F9b16112fA8d994F4e2Bf8bB"),
63 };
64}
65
66#[cfg(test)]
67mod tests {
68 use super::*;
69
70 #[test]
71 fn test_chain_id() {
72 assert_eq!(Chain::PolygonMainnet.chain_id(), 137);
73 assert_eq!(Chain::PolygonAmoy.chain_id(), 80002);
74 }
75
76 #[test]
77 fn test_from_chain_id() {
78 assert_eq!(Chain::from_chain_id(137), Some(Chain::PolygonMainnet));
79 assert_eq!(Chain::from_chain_id(80002), Some(Chain::PolygonAmoy));
80 assert_eq!(Chain::from_chain_id(1), None);
81 assert_eq!(Chain::from_chain_id(999), None);
82 }
83
84 #[test]
85 fn test_chain_contracts() {
86 let mainnet_contracts = Chain::PolygonMainnet.contracts();
87 assert_eq!(
88 mainnet_contracts.exchange,
89 Contracts::POLYGON_MAINNET.exchange
90 );
91
92 let amoy_contracts = Chain::PolygonAmoy.contracts();
93 assert_eq!(amoy_contracts.exchange, Contracts::POLYGON_AMOY.exchange);
94 }
95
96 #[test]
97 fn test_polygon_mainnet_addresses() {
98 let contracts = Contracts::POLYGON_MAINNET;
99
100 assert_ne!(contracts.exchange, Address::ZERO);
102 assert_ne!(contracts.neg_risk_exchange, Address::ZERO);
103 assert_ne!(contracts.neg_risk_adapter, Address::ZERO);
104 assert_ne!(contracts.collateral, Address::ZERO);
105 assert_ne!(contracts.conditional_tokens, Address::ZERO);
106 }
107
108 #[test]
109 fn test_polygon_amoy_addresses() {
110 let contracts = Contracts::POLYGON_AMOY;
111
112 assert_ne!(contracts.exchange, Address::ZERO);
114 assert_ne!(contracts.neg_risk_exchange, Address::ZERO);
115 assert_ne!(contracts.neg_risk_adapter, Address::ZERO);
116 assert_ne!(contracts.collateral, Address::ZERO);
117 assert_ne!(contracts.conditional_tokens, Address::ZERO);
118 }
119
120 #[test]
121 fn test_chain_is_copy() {
122 let chain = Chain::PolygonMainnet;
123 let _copy1 = chain;
124 let _copy2 = chain; }
126
127 #[test]
128 fn test_chain_equality() {
129 assert_eq!(Chain::PolygonMainnet, Chain::PolygonMainnet);
130 assert_eq!(Chain::PolygonAmoy, Chain::PolygonAmoy);
131 assert_ne!(Chain::PolygonMainnet, Chain::PolygonAmoy);
132 }
133}