“DApp”代表去中心化应用程序。与传统应用程序一样,去中心化应用程序也有前端(客户端)和后端(服务器端)。DApp 的用户界面可以用任何语言编写(就像传统应用程序一样),并且可以调用其后端。那么,Dapps 与传统应用程序有何不同?DApp 的后端代码运行在分散的对等网络(即区块链)上。您可能听说过 BitTorrent、Tor、Popcorn Time——它们是在点对点网络上运行但不在区块链上运行的 DApp。区块链 DApp 有自己的后端代码,称为智能合约,可以部署到区块链(最常见的是以太坊)。智能合约定义了整个 DApp 的逻辑。它是使用区块链作为后端的命脉。那么Python 开发人员可以在哪里利用它?重点来了——web3。Web3 是一组库,允许您与本地或远程以太坊区块链进行交互。简单地说,web3 是与您的后端(区块链)通信的桥梁。幸运的是,以太坊开发人员已经制作了一个 python 库 web3.py 用于与以太坊进行交互。它的 API 源自 web3 的 JavaScript 版本。因此,除了 web3.js,我们也可以通过 web3.py 库与区块链进行交互。
Dapps 开发包括三个简单的步骤:
- 在区块链网络上部署智能合约
- 从部署的智能合约中读取数据
- 将交易发送到部署的智能合约) ^! |: H* ^8 f- Q) I: Q& r
安装: J% s) u" {3 n/ T" F1 l% S
Python 2.7 +
Node.js( M! M* Z! ~( Y K
Truffle' x) f0 [0 m* a& A C% v( r
L7 ?3 o3 [' w W( n( F
- S- ]& C& G' ~7 u) Y/ t
npm install -g truffle o) y( E/ i! ]% @
Pip% |: L S& C7 V9 Q& q4 _0 I4 M# v& M
npm i pip, E/ s# r5 h* H$ J% u
web3.py- ?5 Q+ I$ Q1 C
pip install web3
0 Z+ U w) W4 ?1 E) e
Infura 项目 API3 Q G; [7 _& ~5 C
前往 Infura 网站并注册。创建一个新项目并复制 Ropsten Infura RPC URL。我们将把我们的智能合约部署到 Ropsten 测试网络。
8 F& j! J% C6 P" N8 d
智能合约
; W6 x. R4 K6 X) \
每个程序员都用他们最喜欢的编程语言执行了一个“hello world”程序,以了解运行该语言的基础知识。这是我们使用 Solidity 语言编写的简单的“hello world”版本的智能合约,我们可以在区块链上添加问候语并检索它。Solidity 是编写智能合约最常用的语言,它编译为可以在节点上运行的以太坊虚拟机上执行的字节码。/ }, @1 x8 g) I, R* ]/ U A
' R E X, [3 v+ a+ T! U
- pragma solidity ^0.5.7;5 M, a' y7 W& V
- contract greeter{4 s3 \' N! ^1 P- t1 N
- string greeting;
- function greet(string memory _greeting)public{" m. C% i* \: x: s( V' N
- greeting=_greeting;
- }
- function getGreeting() public view returns(string memory) {; S* T, t E, h
- return greeting;+ R1 K: {" D) {( b$ j
- }- w8 C( N0 {& Z/ A7 ?
- }# r6 q5 f( C, |' i
您可以通过传递字符串值使用 greet() 方法添加问候语,并使用 getGreting() 方法检索问候语。
: f+ i' u+ j! I* R4 |
1. 在区块链网络上部署智能合约
5 F2 y/ N0 W d' q' g
a) 创建项目:
mkdir pythonDapp
cd pythonDapp- h3 r$ h+ r: g
truffle init
) z7 R% k9 q0 q7 h# s6 p
成功初始化项目后,转到您的文件夹并在 /contracts目录中创建 greeter.sol 文件。在网络上部署合约之前,我们必须编译它并构建工件。
6 x1 V. h! e4 m* C+ a1 P& r' S7 n
b) 智能合约的编译:因此,对于编译,我们将使用 Truffle solc 编译器。在您的主目录中,运行以下命令:) I" ?. x& W3 X5 b, a
( P9 B/ T5 G3 J D% R& y1 w/ w
) ^$ E' e$ J. U6 w6 F, c) |
truffle compile. ]+ S! h) M- h4 K! {
(or)6 v. B( j6 W. s6 l1 N
truffle.cmd compile #(for windows only)
上面的命令将在 /contracts 目录中编译你的合约,并在 /build 目录中创建二进制工件文件 greeter.json。# e& Z/ t/ t; N) }
c) 部署合约:打开您的 Python IDLE 编辑器,并在主目录 deploy.py 中使用以下代码创建一个新文件,然后在您的目录中运行 py deploy.py。
- import json" b6 d$ Q8 Y: F! A% }7 j/ O
- from web3 importWeb3, HTTPProvider
- from web3.contract importConciseContract/ x5 Q5 V# x5 A9 u* v$ T. a7 b7 C |3 o
- # web3.py instance
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/<API key>"))0 \4 M# M% P7 M
- print(w3.isConnected())
- key="<Private Key here with 0x prefix>"/ e- J3 h C' u5 K; b2 T2 r
- acct = w3.eth.account.privateKeyToAccount(key)4 K. w7 A( G8 {) M" l
- # compile your smart contract with truffle first
- truffleFile = json.load(open('./build/contracts/greeter.json'))
- abi = truffleFile['abi']) {/ n& {- h* ]0 W, U2 z
- bytecode = truffleFile['bytecode']
- contract= w3.eth.contract(bytecode=bytecode, abi=abi)
- #building transaction8 l9 F( C' z' F3 e+ F/ r8 i/ L( }
- construct_txn = contract.constructor().buildTransaction({
- 'from': acct.address,
- 'nonce': w3.eth.getTransactionCount(acct.address),
- 'gas': 1728712,
- 'gasPrice': w3.toWei('21', 'gwei')})
- signed = acct.signTransaction(construct_txn)$ D2 D7 d$ R: t% O2 r5 X
- tx_hash=w3.eth.sendRawTransaction(signed.rawTransaction)
- print(tx_hash.hex())
- tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)5 W0 p9 e% g W) M5 |% \
- print("Contract Deployed At:", tx_receipt['contractAddress'])
) P/ b' ]' ]7 E: D$ b+ m9 n
这是我所做的:+ p+ Q* u4 r$ }9 m( Y
导入的 web3 库和所有其他必需的模块
通过指向 Ropsten Infura 节点启动 web3 提供程序( f3 L5 g' C1 n, a4 i0 |& E
添加了用于签署交易的帐户地址和私钥。不要忘记在代码中添加您的凭据。, m) A' _# Y- \; d
通过指向 Truffle 编译的工件文件greeter.json的abi和字节码启动合约实例
添加了带有随机数、gas、gasPrice 等参数的construct_txn。此处,gas 是指交易应在以太坊中使用和支付的最大计算资源量。gasPrice 是指在交易中使用该数量的 gas 时的最小 Ether 数量。to 指的是您发送交易的地址。仅当您将 Ether 发送到帐户或智能合约时才需要 to 参数。9 S1 L4 J i9 G3 F+ I0 _+ w' P: z ~
使用我们的私钥签署交易并在网络上广播。
在控制台中记录交易哈希和部署的合约地址。根据以太坊的说法,事务处理时间 <20 秒。所以你必须等待 20 秒才能获得部署的合约地址。您的后端现在已成功部署在以太坊区块链上。现在您可以使用此地址与您的智能合约进行交互。复制此合约地址。, N; L% F8 { P+ T
2. 向部署的合约发送交易1 L4 t0 T# E3 Q( i L; t2 \# X; e4 @
在我们的合约中,有一个方法greet()。我们可以单独使用这种方法在我们的合同中添加问候语。让我们看看我们如何使用 web3.py 来做到这一点。打开您的 Python IDLE 编辑器并使用以下代码创建一个新文件 sign.py。然后在您的目录中运行 py sign.py。, B+ m O& d3 [- j2 K) g7 ?- E
- import json" y% s* x6 t! I* D8 [1 M4 T6 v: o* T
- from web3 importWeb3, HTTPProvider \3 d4 K9 y. G+ I1 p ~) [, W
- from web3.contract importConciseContract
- # compile your smart contract with truffle first
- truffleFile = json.load(open('./build/contracts/greeter.json'))
- abi = truffleFile['abi']
- bytecode = truffleFile['bytecode']) T$ T7 x4 |4 O0 Y& |4 O7 x; F4 o
- # web3.py instance
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/<API key>")) #modify$ S- g! R9 {& X2 L
- print(w3.isConnected())9 [; c/ ^- w' M) E! R
- contract_address = Web3.toChecksumAddress("<Deployed Contract Address here>") #modify
- key="<Private key with 0x prefix here>"#modify) }/ V. Y1 r2 k+ D1 R4 b7 v' U* V
- acct = w3.eth.account.privateKeyToAccount(key): O+ e% R; I, X, p
- account_address= acct.address
- # Instantiate and deploy contract
- contract = w3.eth.contract(abi=abi, bytecode=bytecode)
- # Contract instance. r3 f h* f1 L4 d8 n5 p
- contract_instance = w3.eth.contract(abi=abi, address=contract_address)
- # Contract instance in concise mode
- #contract_instance = w3.eth.contract(abi=abi, address=contract_address, ContractFactoryClass=ConciseContract)- d6 R& e2 Q" S+ v, [
- tx = contract_instance.functions.greet("Hello all my goody people").buildTransaction({'nonce': w3.eth.getTransactionCount(account_address)})
- #Get tx receipt to get contract address
- signed_tx = w3.eth.account.signTransaction(tx, key)
- #tx_receipt = w3.eth.getTransactionReceipt(tx_hash)1 g* d E/ a R) A' u# P- U
- hash= w3.eth.sendRawTransaction(signed_tx.rawTransaction)
- print(hash.hex())
9 [$ T$ k4 J3 _/ y
8 _; {4 t# M' B' |/ ~! v& b
这是我所做的:- C0 ]3 p2 S. ] o7 f' t/ Q
导入的 web3 库和所有其他必需的模块. F: g( A& J9 b& V, R
通过指向 Ropsten Infura 节点启动 web3 提供程序
添加了用于签署交易的帐户地址和私钥
通过指向 Truffle 编译的工件文件greeter.json的abi和字节码启动合约实例7 i5 I& Q/ e9 H; ]
创建 tx 对象以添加问候语“hello all my goody people”并建立交易
使用我们的私钥签署交易并在网络上广播。
在控制台中记录交易哈希。您可以使用您的交易哈希在 etherscan 上检查交易状态。一旦交易被矿工验证,我们的问候语将被添加到区块链上。您可以使用 getGreeting() 函数检索问候语,如下所述。) O' _9 z& e& ~. ^
3. 从部署的智能合约中读取数据7 D% z! f7 r; l: Y3 n
在我们的合约中,有一个方法 getGreeting() 可以检索我们在区块链中添加的问候语。我们将使用 web3.py 调用此方法 打开您的 Python IDLE 编辑器并使用以下代码创建一个新文件 read.py。运行 py read.py 读取问候语。9 d$ Y) g o. p7 M
- % i. m2 w3 @7 [2 G2 M: [3 f# [3 V
- import json* U" w9 J0 ^+ e5 q. {6 b
- from web3 importWeb3, HTTPProvider# q6 v& N4 J2 \9 P6 ]% m+ x, n
- from web3.contract importConciseContract
- # compile your smart contract with truffle first
- truffleFile = json.load(open('./build/contracts/greeter.json'))8 U# i# g0 q5 x3 g2 O
- abi = truffleFile['abi']0 z# O8 ^0 j) I, y& L2 ?% F
- bytecode = truffleFile['bytecode']
- # web3.py instance
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/<ApI Key here>"))
- print(w3.isConnected())
- contract_address = Web3.toChecksumAddress("<Deployed Contract Address here>")9 d& h6 i" y+ G' \) l) T
- # Instantiate and deploy contract
- contract = w3.eth.contract(abi=abi, bytecode=bytecode)$ K r8 i! ~) \! O
- # Contract instance2 \6 f7 W" l1 ]9 m, c* E
- contract_instance = w3.eth.contract(abi=abi, address=contract_address)
- # Contract instance in concise mode
- #contract_instance = w3.eth.contract(abi=abi, address=contract_address, ContractFactoryClass=ConciseContract)* J# ]9 e2 B. Y' U8 s
- # Getters + Setters for web3.eth.contract object ConciseContract
- #print(format(contract_instance.getGreeting()))- Z7 `* E4 g$ g$ r( J
- print('Contract value: {}'.format(contract_instance.functions.getGreeting().call()))
$ w2 {, j7 F9 i5 }# |
这是我所做的: [# |9 k" G4 |0 B3 Y4 p& B% d
导入的 web3 库和所有其他必需的模块, Z6 d" {. X" F9 i2 P
通过指向 Ropsten Infura 节点启动 web3 提供程序: }0 ?% I0 `3 u
通过指向 abi 和 contract_Address 创建合约实例(来自上一步)
调用 getGreeting() 方法并在控制台打印结果
结论
6 \( Q: b2 h8 F: ~
您现在应该了解如何使用 web3.py 部署、交互和签署交易。让我们通过查看我使用 web3.py 创建的实时 DApp 并在此处使用 Flask 作为前端服务器来将所有部分组合在一起。它将帮助您更深入地了解使用 Python 进行 DApp 开发。
: C: C1 j; i ], Z% x5 |* E
参考 @温室小书生室d 《python利用web3.py开发以太坊应用dapp的实战教程》4 O/ U! u3 z7 [8 b* _ B- m