-
Notifications
You must be signed in to change notification settings - Fork 29
/
Copy path02-connect-to-contracts.html
135 lines (110 loc) · 4.34 KB
/
02-connect-to-contracts.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
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
<!DOCTYPE html>
<link rel="stylesheet" href="boilerplate/style.css" />
<script type="module" src="boilerplate/editor.js"></script>
<title>Web3 From Zero - Lesson 2</title>
<h1>2. Connecting to Smart Contract</h1>
<p>
Smart contracts are the main feature that distinguishes Ethereum from Bitcoin. A smart contract is
a program running on the Ethereum network, and it can access the blockchain data.
</p>
<p>
You write smart contracts in the Solidity programming language. Like Java, Solidity gets compiled
to a bytecode that runs in a virtual machine. In this case, it's called the Ethereum Virtual
Machine, or EVM for short.
</p>
<h2>Interacting with Smart Contracts</h2>
<p>To interact with a smart contract from JavaScript, you need three things:</p>
<ol>
<li>The address that points to the smart contract</li>
<li>The full or partial Application Binary Interface of the contract</li>
<li>A provider that is connected to the Ethereum network</li>
</ol>
<p>
In the last lesson, you learned, every EOA and smart contract has an address; this is how you find
the contract on the blockchain.
</p>
<pre>
const address = "0x25ed58c027921E14D86380eA2646E3a1B5C55A8b"
</pre>
<br>
<p>
To figure out what functions a contract has to offer, you need an Application Binary Interface, or short ABI, a list
of function signatures of functions you want to call later. Ethers.js uses the ABI to determine what arguments a
function expects and what values it returned when it finished its work.
</p>
<p>
When using Ethers.js, you can define the ABI as an array of strings, where the strings are
Solidity function signatures, just like you would write them in the Solidity programming language.
</p>
<p>Here is an example of an ABI for one function:</p>
<pre>
const abi = [
"function ownerOf(uint256 tokenId) external view returns (address owner)",
]
</pre>
<br>
<p>
Functions get defined with the <code>function</code> keyword followed by a name and then the
arguments in paratheses. Solidity is statically typed and doesn't support floating points but
integer types of different sizes.
</p>
<p>
In the example above the function takes one argument of type <code>uint256</code> and it's named
<code>tokenId</code>.
</p>
<p>
The <code>external</code> keyword is an access modifier that marks the function as callable from
outside of the contract.
</p>
<p>
The <code>view</code> keyword is crucial too because it tells the EVM that the function won't
modify any on-chain state; it will just read it. <code>view</code> enables you to call the
function without having a wallet because the Ethereum RPC you use can read all data from its local
copy of the blockchain.
</p>
<p>
The <code>returns</code> keyword tells you what you get back when you call the function. In this
case, it's the owner's address of a token.
</p>
<p>
The next step is to create a new contract instance and call the <code>ownerOf</code> function
defined by the ABI!
</p>
<p>Try it yourself in the editor!</p>
<code-editor>
const contractAddress = "0x25ed58c027921E14D86380eA2646E3a1B5C55A8b"
const contractAbi = [
"function ownerOf(uint256 tokenId) external view returns (address owner)"
]
const provider = ethers.getDefaultProvider()
// Write your code here!
<p>
const smartContract = new ethers.Contract(
contractAddress,
contractAbi,
provider,
)
const owner = await smartContract.ownerOf(2)
print(owner)
</p>
</code-editor>
<p>
The owner of the contract, like everything on the blockchain, is located at an address, and this
is just what the function returned. The address could point to another contract that created our
contract, as so called factory contract. The address could also point to an EOA, which is the case
in this example.
</p>
<h2>Conclusion</h2>
<p>
This lesson taught you about smart contract interaction. How to find them on the blockchain with
the help of a provider and an address and how to call its functions with an ABI.
</p>
<p>
An ABI is just an array of strings, these strings are function signatures like you would write
them in Solidity, the programming language powering Ethereum.
</p>
<p>
If you found these addresses a bit cumbersome to work with, I have good news for you.
<a href="03-using-ens.html">The next lesson</a> is about the blockchain equivalent of DNS:
<a href="https://ens.domains/">The Etherum Name Service.</a>
</p>