Skip to content

Commit a73567d

Browse files
committed
add query ethereum blockchain tutorial
1 parent 165ad07 commit a73567d

File tree

4 files changed

+119
-0
lines changed

4 files changed

+119
-0
lines changed

Diff for: README.md

+1
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ This is a repository of all the tutorials of [The Python Code](https://www.thepy
119119
- [Zipf's Word Frequency Plot with Python](https://www.thepythoncode.com/article/plot-zipfs-law-using-matplotlib-python). ([code](general/zipf-curve))
120120
- [How to Plot Weather Temperature in Python](https://www.thepythoncode.com/article/interactive-weather-plot-with-matplotlib-and-requests). ([code](general/interactive-weather-plot/))
121121
- [How to Generate SVG Country Maps in Python](https://www.thepythoncode.com/article/generate-svg-country-maps-python). ([code](general/generate-svg-country-map))
122+
- [How to Query the Ethereum Blockchain with Python](https://www.thepythoncode.com/article/query-ethereum-blockchain-with-python). ([code](general/query-ethereum))
122123

123124

124125

Diff for: general/query-ethereum/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
# [How to Query the Ethereum Blockchain with Python](https://www.thepythoncode.com/article/query-ethereum-blockchain-with-python)

Diff for: general/query-ethereum/query_ethereum_blockchain.py

+116
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
from web3 import Web3
2+
3+
# infura API key
4+
API_KEY = "put your API key here"
5+
# change endpoint to mainnet or ropsten or any other of your account
6+
url = f"https://<endpoint>.infura.io/v3/{API_KEY}"
7+
8+
w3 = Web3(Web3.HTTPProvider(url))
9+
# see whether the connection is established
10+
res = w3.isConnected()
11+
print(res)
12+
13+
14+
# get latest block
15+
latest = w3.eth.get_block('latest')
16+
print(latest)
17+
# print the block number
18+
print(latest['number'])
19+
20+
21+
# query individual transactions
22+
transaction1 = w3.eth.get_transaction('0x0e3d45ec3e1d145842ce5bc56ad168e4a98508e0429da96c1ff89f11076da36d')
23+
print(transaction1)
24+
25+
26+
# use block number to query transactions
27+
transaction2 = w3.eth.get_transaction_by_block(15410924, 0)
28+
print(transaction2)
29+
30+
31+
# get number of transactions in a block
32+
transactionCount = w3.eth.get_transaction_count('0x486976656f6e2065752d68656176792d657163')
33+
print(transactionCount)
34+
35+
36+
# check if a block address is valid
37+
isValid = w3.isAddress('0xed44e77fb3408cd5ad415d7467af6f6783218fb74c3824de1258f6d266bcc7b7')
38+
print(isValid)
39+
40+
41+
# check if an address is a valid EIP-55 checksum address
42+
isChecksumAddressValid = Web3.isChecksumAddress('0x486976656f6e2065752d68656176792d657163')
43+
print(isChecksumAddressValid)
44+
45+
46+
# get balance of a block address
47+
balance = w3.eth.get_balance('0xd3CdA913deB6f67967B99D67aCDFa1712C293601')
48+
print(balance)
49+
50+
51+
# get proof of a block
52+
proof = w3.eth.get_proof('0x486976656f6e2065752d68656176792d657163', [0], 3391)
53+
print(proof)
54+
55+
56+
# get uncle of a block
57+
w3.eth.get_uncle_by_block(15410924, 0)
58+
59+
60+
nonce = w3.eth.getTransactionCount('0x610Ae88399fc1687FA7530Aac28eC2539c7d6d63', 'latest')
61+
# create a transaction
62+
transaction = {
63+
'to': '0x31B98D14007bDEe637298086988A0bBd31184523',
64+
'from': '0x31B98D14007bDEe63EREEDFT34544646MOI22',
65+
'value': 500,
66+
'gas': 10000,
67+
'maxFeePerGas': 1000000208,
68+
'nonce': nonce,
69+
}
70+
# send the transaction
71+
w3.eth.send_transaction(transaction)
72+
73+
74+
# sign a transaction
75+
signed = w3.eth.sign_transaction(
76+
dict(
77+
nonce=nonce,
78+
maxFeePerGas=34300000,
79+
maxPriorityFeePerGas=25000000,
80+
gas=100000,
81+
to='0xerecfBYWlB99D67aCDFa17EREFEerrtr73601',
82+
value=1,
83+
data=b'',
84+
)
85+
)
86+
87+
address = '0x706f6f6c696e2e636f6d21688947c8f76c4e92'
88+
abi = '[{"inputs":[{"internalType":"address","name":"account","type":"address"},{"internalType":"address","name":"minter_",........' # abi of the contract
89+
# create a contract object
90+
contract = w3.eth.contract(address=address, abi=abi)
91+
92+
93+
# total supply of the token
94+
totalSupply = contract.functions.totalSupply().call()
95+
print(totalSupply)
96+
97+
98+
# read the data and update the state
99+
contract.functions.storedvalue().call()
100+
tx_hash = contract.functions.updateValue(100).transact()
101+
102+
103+
# retrieve token metadata
104+
print(contract.functions.name().call())
105+
print(contract.functions.decimals().call())
106+
print(contract.functions.symbol().call())
107+
# output:
108+
# SHIBACHU
109+
# 9
110+
# SHIBACHU
111+
112+
113+
# find the account balance
114+
address = '0x5eaaf114aad1313e7440d2ff805ced993e566df'
115+
balance = contract.functions.balanceOf(address).call()
116+

Diff for: general/query-ethereum/requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
web3

0 commit comments

Comments
 (0)