You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: pages/lessons/fundamentals/wolovim-part-1.mdx
+31-18Lines changed: 31 additions & 18 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -1,3 +1,16 @@
1
+
---
2
+
title: A Developer's Guide to Ethereum, Pt. 1
3
+
description:
4
+
Introduction to Ethereum with web3.py and Python.
5
+
icons:
6
+
[
7
+
'web3.py',
8
+
'python',
9
+
'ethereum',
10
+
'etherscan',
11
+
]
12
+
---
13
+
1
14
# **A Developer's Guide to Ethereum, Pt. 1**
2
15
3
16
So, you’ve heard about this [Ethereum](https://ethereum.org/) thing and are ready to venture down the rabbit hole? This post will quickly cover some blockchain basics, then get you interacting with a simulated Ethereum node – reading block data, checking account balances, and sending transactions. Along the way, we’ll highlight the differences between traditional ways of building apps and this new decentralized paradigm.
@@ -19,7 +32,7 @@ Again, if any of these are untrue, or you don’t plan to reproduce the code in
19
32
20
33
There are many ways to describe Ethereum, but at its heart is a blockchain. Blockchains are made up of a series of blocks, so let’s start there. In the simplest terms, each block on the Ethereum blockchain is just some metadata and a list of transactions. In JSON format, that looks something like this:
21
34
22
-
```
35
+
```python
23
36
{
24
37
"number": 1234567,
25
38
"hash": "0xabc123...",
@@ -60,7 +73,7 @@ Configure the Ethereum node and Web3.py to communicate via the same protocol, e.
60
73
61
74
Once Web3.py is properly configured, you can begin to interact with the blockchain. Here’s a couple of Web3.py usage examples as a preview of what’s to come:
62
75
63
-
```
76
+
```python
64
77
# read block data:
65
78
w3.eth.get_block('latest')
66
79
@@ -77,19 +90,19 @@ In this walkthrough, we’ll just be working within a Python interpreter. We won
77
90
78
91
First, install [IPython](https://ipython.org/) for a user-friendly environment to explore in. IPython offers tab completion, among other features, making it much easier to see what’s possible within Web3.py.
79
92
80
-
```
93
+
```bash
81
94
$ pip install ipython
82
95
```
83
96
84
97
Web3.py is published under the name `web3`. Install the latest version like so:
85
98
86
-
```
99
+
```bash
87
100
$ pip install web3==6.0.0b8
88
101
```
89
102
90
103
One more thing – we're going to simulate a blockchain later, which requires a couple more dependencies. You can install those via:
91
104
92
-
```
105
+
```bash
93
106
$ pip install "web3[tester]"
94
107
```
95
108
@@ -99,19 +112,19 @@ You’re all set up to go!
99
112
100
113
Open up a new Python environment by running `ipython` in your terminal. This is comparable to running `python`, but comes with more bells and whistles.
101
114
102
-
```
115
+
```bash
103
116
$ ipython
104
117
```
105
118
106
119
This will print out some information about the versions of Python and IPython you’re running, then you should see a prompt waiting for input:
107
120
108
-
```
121
+
```python
109
122
In [1]:
110
123
```
111
124
112
125
You’re looking at an interactive Python shell now. Essentially, its a sandbox to play in. If you’ve made it this far, its time to import Web.py:
113
126
114
-
```
127
+
```python
115
128
In [1]: from web3 import Web3
116
129
```
117
130
@@ -126,7 +139,7 @@ In an Ethereum application, you will commonly need to convert currency denominat
126
139
127
140
Try converting some values to and from wei. Note that there are [names for many of the denominations](https://web3py.readthedocs.io/en/stable/examples.html#converting-currency-denominations) in between ether and wei. One of the better known among them is **gwei**, as it’s often how transaction fees are represented.
128
141
129
-
```
142
+
```python
130
143
In [2]: Web3.to_wei(1, 'ether')
131
144
Out[2]: 1000000000000000000
132
145
@@ -155,7 +168,7 @@ The EthereumTesterProvider connects to a simulated node and is handy for quick d
155
168
156
169
That simulated node is called **[eth-tester](https://github.com/ethereum/eth-tester)** and we installed it as part of the `pip install "web3[tester]"` command. Configuring Web3.py to use this tester provider is as simple as:
157
170
158
-
```
171
+
```python
159
172
In [4]: w3 = Web3(Web3.EthereumTesterProvider())
160
173
```
161
174
@@ -165,7 +178,7 @@ Now you’re ready to surf the chain! That’s not a thing people say. I just ma
165
178
166
179
First things first, a sanity check:
167
180
168
-
```
181
+
```python
169
182
In [5]: w3.is_connected()
170
183
Out[5]: True
171
184
```
@@ -176,7 +189,7 @@ Since we’re using the tester provider, this isn’t a very valuable test, but
176
189
177
190
As a convenience, the tester provider created some accounts and preloaded them with test ether. First, let’s see a list of those accounts:
@@ -187,14 +200,14 @@ If you run this command, you should see a list of ten strings that begin with `
187
200
188
201
As mentioned, the tester provider has preloaded each of these accounts with some test ether. Let’s find out how much is in the first account:
189
202
190
-
```
203
+
```python
191
204
In [7]: w3.eth.get_balance(w3.eth.accounts[0])
192
205
Out[7]: 1000000000000000000000000
193
206
```
194
207
195
208
That’s a lot of zeros! Before you go laughing all the way to the fake bank, recall that lesson about currency denominations from earlier. Ether values are represented in the smallest denomination, wei. Convert that to ether:
196
209
197
-
```
210
+
```python
198
211
In [8]: w3.from_wei(1000000000000000000000000, 'ether')
199
212
Out[8]: Decimal('1000000')
200
213
```
@@ -205,7 +218,7 @@ One million test ether — still not too shabby.
205
218
206
219
Let’s take a peek at the state of this simulated blockchain:
207
220
208
-
```
221
+
```python
209
222
In [9]: w3.eth.get_block('latest')
210
223
Out[9]: AttributeDict({
211
224
'number': 0,
@@ -226,7 +239,7 @@ A lot of information gets returned about a block, but just a couple things to po
226
239
227
240
We’re stuck at block zero until there’s a transaction to mine, so let’s give it one. Send a few test ether from one account to another:
228
241
229
-
```
242
+
```python
230
243
In [10]: tx_hash = w3.eth.send_transaction({
231
244
'from': w3.eth.accounts[0],
232
245
'to': w3.eth.accounts[1],
@@ -242,7 +255,7 @@ This is typically the point where you’d wait for several seconds for your tran
242
255
243
256
Our simulated environment will add the transaction in a new block instantly, so we can immediately view the transaction:
244
257
245
-
```
258
+
```python
246
259
In [11]: w3.eth.get_transaction(tx_hash)
247
260
Out[11]: AttributeDict({
248
261
'hash': HexBytes('0x15e9fb95dc39...'),
@@ -259,7 +272,7 @@ You’ll see some familiar details here: the `from`, `to`, and `value` field
259
272
260
273
We can also easily verify the success of this transaction by checking the balances of the two accounts involved. Three ether should have moved from one to another.
0 commit comments