fathom_web3/contract/ens/
registry.rs1use crate::{
4 api::Eth,
5 contract::{Contract, Options},
6 signing::NameHash,
7 types::{Address, TransactionId},
8 Transport,
9};
10
11type ContractError = crate::contract::Error;
12
13const ENS_REGISTRY_ADDRESS: &str = "00000000000C2E074eC69A0dFb2997BA6C7d2e1e";
14
15#[derive(Debug, Clone)]
24pub struct Registry<T: Transport> {
25 contract: Contract<T>,
26}
27
28impl<T: Transport> Registry<T> {
29 pub fn new(eth: Eth<T>) -> Self {
31 let address = ENS_REGISTRY_ADDRESS.parse().expect("Parsing Address");
32
33 let json = include_bytes!("ENSRegistry.json");
35
36 let contract = Contract::from_json(eth, address, json).expect("Contract Creation");
37
38 Self { contract }
39 }
40}
41
42impl<T: Transport> Registry<T> {
43 pub async fn owner(&self, node: NameHash) -> Result<Address, ContractError> {
47 let options = Options::default();
48
49 self.contract.query("owner", node, None, options, None).await
50 }
51
52 pub async fn resolver(&self, node: NameHash) -> Result<Address, ContractError> {
56 let options = Options::default();
57
58 self.contract.query("resolver", node, None, options, None).await
59 }
60
61 pub async fn ttl(&self, node: NameHash) -> Result<u64, ContractError> {
65 let options = Options::default();
66
67 self.contract.query("ttl", node, None, options, None).await
68 }
69
70 pub async fn set_owner(
74 &self,
75 from: Address,
76 node: NameHash,
77 owner: Address,
78 ) -> Result<TransactionId, ContractError> {
79 let options = Options::default();
80
81 let id = self.contract.call("setOwner", (node, owner), from, options).await?;
82
83 Ok(TransactionId::Hash(id))
84 }
85
86 pub async fn set_resolver(
90 &self,
91 from: Address,
92 node: NameHash,
93 resolver: Address,
94 ) -> Result<TransactionId, ContractError> {
95 let options = Options::default();
96
97 let id = self
98 .contract
99 .call("setResolver", (node, resolver), from, options)
100 .await?;
101
102 Ok(TransactionId::Hash(id))
103 }
104
105 pub async fn set_ttl(&self, from: Address, node: NameHash, ttl: u64) -> Result<TransactionId, ContractError> {
109 let options = Options::default();
110
111 let id = self.contract.call("setTTL", (node, ttl), from, options).await?;
112
113 Ok(TransactionId::Hash(id))
114 }
115
116 pub async fn set_subnode_owner(
120 &self,
121 from: Address,
122 node: NameHash,
123 label: [u8; 32],
124 owner: Address,
125 ) -> Result<TransactionId, ContractError> {
126 let options = Options::default();
127
128 let id = self
129 .contract
130 .call("setSubnodeOwner", (node, label, owner), from, options)
131 .await?;
132
133 Ok(TransactionId::Hash(id))
134 }
135
136 pub async fn set_record(
140 &self,
141 from: Address,
142 node: NameHash,
143 owner: Address,
144 resolver: Address,
145 ttl: u64,
146 ) -> Result<TransactionId, ContractError> {
147 let options = Options::default();
148
149 let id = self
150 .contract
151 .call("setRecord", (node, owner, resolver, ttl), from, options)
152 .await?;
153
154 Ok(TransactionId::Hash(id))
155 }
156
157 pub async fn set_subnode_record(
161 &self,
162 from: Address,
163 node: NameHash,
164 label: [u8; 32],
165 owner: Address,
166 resolver: Address,
167 ttl: u64,
168 ) -> Result<TransactionId, ContractError> {
169 let options = Options::default();
170
171 let id = self
172 .contract
173 .call("setSubnodeRecord", (node, label, owner, resolver, ttl), from, options)
174 .await?;
175
176 Ok(TransactionId::Hash(id))
177 }
178
179 pub async fn set_approval_for_all(
183 &self,
184 from: Address,
185 operator: Address,
186 approved: bool,
187 ) -> Result<TransactionId, ContractError> {
188 let options = Options::default();
189
190 let id = self
191 .contract
192 .call("setApprovalForAll", (operator, approved), from, options)
193 .await?;
194
195 Ok(TransactionId::Hash(id))
196 }
197
198 pub async fn check_approval(&self, owner: Address, operator: Address) -> Result<bool, ContractError> {
202 let options = Options::default();
203
204 self.contract
205 .query("isApprovedForAll", (owner, operator), None, options, None)
206 .await
207 }
208
209 pub async fn check_record_existence(&self, node: NameHash) -> Result<bool, ContractError> {
213 let options = Options::default();
214
215 self.contract.query("recordExists", node, None, options, None).await
216 }
217}