forked from kay-is/web3-from-zero
-
Notifications
You must be signed in to change notification settings - Fork 0
/
03-using-ens.html
73 lines (57 loc) · 2.19 KB
/
03-using-ens.html
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
<!DOCTYPE html>
<link rel="stylesheet" href="boilerplate/style.css" />
<script type="module" src="boilerplate/editor.js"></script>
<title>Web3 From Zero - Lesson 3</title>
<h1>3. Using the Ethereum Name Service</h1>
<p>
In the previous lesson, you learned about smart contracts and how to connect to them. The next
step is to make this work easier with ENS.
</p>
<p>
You learned that addresses are to the blockchain what IPs are to the internet. The same goes for
the Ethereum Name Service, short ENS. It's the blockchain equivalent of the Domain Name Service,
or DNS.
</p>
<p>
ENS can be used to make addresses more accessible for humans. This way you can connect to EOAs and
smart contracts with a human readable string.
</p>
<p>
You can register ENS domains just like regular domains at <a href="https://ens.domains/">the ENS
service website</a>.
</p>
<h2>Reading a Contract's Balance</h2>
<p>
Ethers.js works transparently with ENS, so we can simply replace the address from the last lesson
with an ENS domain that resolves to that address. In case of that address a corresponding domain
is <code>devdao.eth</code>.
</p>
<p>
Try it in the editor. Fetch the balance of the Developer DAO NFT contract and print it. But use
the <code>devdao.eth</code> ENS domain instead of the address!
</p>
<code-editor>
const provider = ethers.getDefaultProvider()
// write your code here!
<p>
const balance = await provider.getBalance("devdao.eth")
print(ethers.utils.formatUnits(balance) + " ETH")
</p>
</code-editor>
<blockquote>
Note: Keep in mind, the target blockchain address of a ENS domain can change. So if
you want to make sure that the same address is called every time, use the
address directly.
</blockquote>
<h2>Conclusion</h2>
<p>
ENS domains are a usability imporvement for the blockchain. Just like DNS domains were for the
internet in general. Remembering <code>devdao.eth</code> is much easier than a big hex number.
</p>
<p>
Ethers.js works transparently with ENS domains, so you can usually drop-in a domain in places
where you need an address.
</p>
<p>
In <a href="04-ercs.html">the next lesson</a>, we will learn about Ethereum Request for Comments, or short ERCs.
</p>