Skip to content

Commit 75b27f0

Browse files
7i7owolovim
authored andcommitted
Update wolowim-part-1.mdx
frontmatter + code blocks
1 parent 7f73628 commit 75b27f0

File tree

1 file changed

+31
-18
lines changed

1 file changed

+31
-18
lines changed

pages/lessons/fundamentals/wolovim-part-1.mdx

Lines changed: 31 additions & 18 deletions
Original file line numberDiff line numberDiff 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+
114
# **A Developer's Guide to Ethereum, Pt. 1**
215

316
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
1932

2033
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:
2134

22-
```
35+
```python
2336
{
2437
"number": 1234567,
2538
"hash": "0xabc123...",
@@ -60,7 +73,7 @@ Configure the Ethereum node and Web3.py to communicate via the same protocol, e.
6073

6174
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:
6275

63-
```
76+
```python
6477
# read block data:
6578
w3.eth.get_block('latest')
6679

@@ -77,19 +90,19 @@ In this walkthrough, we’ll just be working within a Python interpreter. We won
7790
7891
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.
7992

80-
```
93+
```bash
8194
$ pip install ipython
8295
```
8396

8497
Web3.py is published under the name `web3`. Install the latest version like so:
8598

86-
```
99+
```bash
87100
$ pip install web3==6.0.0b8
88101
```
89102

90103
One more thing – we're going to simulate a blockchain later, which requires a couple more dependencies. You can install those via:
91104

92-
```
105+
```bash
93106
$ pip install "web3[tester]"
94107
```
95108

@@ -99,19 +112,19 @@ You’re all set up to go!
99112

100113
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.
101114

102-
```
115+
```bash
103116
$ ipython
104117
```
105118

106119
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:
107120

108-
```
121+
```python
109122
In [1]:
110123
```
111124

112125
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:
113126

114-
```
127+
```python
115128
In [1]: from web3 import Web3
116129
```
117130

@@ -126,7 +139,7 @@ In an Ethereum application, you will commonly need to convert currency denominat
126139
127140
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.
128141

129-
```
142+
```python
130143
In [2]: Web3.to_wei(1, 'ether')
131144
Out[2]: 1000000000000000000
132145

@@ -155,7 +168,7 @@ The EthereumTesterProvider connects to a simulated node and is handy for quick d
155168

156169
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:
157170

158-
```
171+
```python
159172
In [4]: w3 = Web3(Web3.EthereumTesterProvider())
160173
```
161174

@@ -165,7 +178,7 @@ Now you’re ready to surf the chain! That’s not a thing people say. I just ma
165178

166179
First things first, a sanity check:
167180

168-
```
181+
```python
169182
In [5]: w3.is_connected()
170183
Out[5]: True
171184
```
@@ -176,7 +189,7 @@ Since we’re using the tester provider, this isn’t a very valuable test, but
176189

177190
As a convenience, the tester provider created some accounts and preloaded them with test ether. First, let’s see a list of those accounts:
178191

179-
```
192+
```python
180193
In [6]: w3.eth.accounts
181194
Out[6]: ['0x7E5F4552091A69125d5DfCb7b8C2659029395Bdf',
182195
'0x2B5AD5c4795c026514f8317c7a215E218DcCD6cF',
@@ -187,14 +200,14 @@ If you run this command, you should see a list of ten strings that begin with `
187200

188201
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:
189202

190-
```
203+
```python
191204
In [7]: w3.eth.get_balance(w3.eth.accounts[0])
192205
Out[7]: 1000000000000000000000000
193206
```
194207

195208
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:
196209

197-
```
210+
```python
198211
In [8]: w3.from_wei(1000000000000000000000000, 'ether')
199212
Out[8]: Decimal('1000000')
200213
```
@@ -205,7 +218,7 @@ One million test ether — still not too shabby.
205218

206219
Let’s take a peek at the state of this simulated blockchain:
207220

208-
```
221+
```python
209222
In [9]: w3.eth.get_block('latest')
210223
Out[9]: AttributeDict({
211224
'number': 0,
@@ -226,7 +239,7 @@ A lot of information gets returned about a block, but just a couple things to po
226239

227240
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:
228241

229-
```
242+
```python
230243
In [10]: tx_hash = w3.eth.send_transaction({
231244
'from': w3.eth.accounts[0],
232245
'to': w3.eth.accounts[1],
@@ -242,7 +255,7 @@ This is typically the point where you’d wait for several seconds for your tran
242255

243256
Our simulated environment will add the transaction in a new block instantly, so we can immediately view the transaction:
244257

245-
```
258+
```python
246259
In [11]: w3.eth.get_transaction(tx_hash)
247260
Out[11]: AttributeDict({
248261
'hash': HexBytes('0x15e9fb95dc39...'),
@@ -259,7 +272,7 @@ You’ll see some familiar details here: the `from`, `to`, and `value` field
259272

260273
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.
261274

262-
```
275+
```python
263276
In [12]: w3.eth.get_balance(w3.eth.accounts[0])
264277
Out[12]: 999996999999999999979000
265278

0 commit comments

Comments
 (0)