“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 库与区块链进行交互。0 a# e% D. s! t% {
# U# t. N% U8 g: n P. J
Dapps 开发包括三个简单的步骤:
- 在区块链网络上部署智能合约
- 从部署的智能合约中读取数据
- 将交易发送到部署的智能合约- p, i$ i0 |4 n3 {! i
安装( f1 @- D6 d2 k5 U6 ?/ V. m" s
Python 2.7 +/ \. g8 N6 B9 n8 w3 m
Node.js+ C. B" L" ^3 L8 @( Q$ F: B
Truffle' U Y: R- }% I8 {) @
. u! i1 O. s) k0 \" {
npm install -g truffle* h3 }9 P9 R6 f
Pip! q& ~8 `7 u% M" u
npm i pip
web3.py; `$ l7 w' ]. u
pip install web33 L- H9 m& s# m' j5 d |1 P- M% ^
' V7 C/ j1 a! Y
Infura 项目 API2 t$ V% r; ?; x
前往 Infura 网站并注册。创建一个新项目并复制 Ropsten Infura RPC URL。我们将把我们的智能合约部署到 Ropsten 测试网络。
智能合约5 n& y" a1 K/ Y- H# |2 w$ @4 z
5 @ x3 v+ w' w X* t- g
每个程序员都用他们最喜欢的编程语言执行了一个“hello world”程序,以了解运行该语言的基础知识。这是我们使用 Solidity 语言编写的简单的“hello world”版本的智能合约,我们可以在区块链上添加问候语并检索它。Solidity 是编写智能合约最常用的语言,它编译为可以在节点上运行的以太坊虚拟机上执行的字节码。# x: a# p+ v; w8 y/ V& Q+ a3 M$ A; D
5 {1 V | |$ t
- pragma solidity ^0.5.7;- A! [0 i/ ^8 W5 a- ]$ l7 y0 Y) u
- contract greeter{' i" L# G5 n1 p+ D+ K
- string greeting;
- function greet(string memory _greeting)public{
- greeting=_greeting;
- }5 n; K6 r3 f- z6 l/ l& A
- function getGreeting() public view returns(string memory) {. w1 `# C, j8 y% M
- return greeting;- g8 x$ n3 I& f7 z; Z- \
- }
- }6 i7 I$ u" f3 q7 N
您可以通过传递字符串值使用 greet() 方法添加问候语,并使用 getGreting() 方法检索问候语。
1. 在区块链网络上部署智能合约
8 w9 N' B! P: _
a) 创建项目:# h) T2 N$ U. k5 y' @" G
mkdir pythonDapp: E, D0 W9 Q5 i0 S9 n
cd pythonDapp0 s6 q' O) W8 _% A2 W
truffle init& p7 I7 Z. k& M) R/ `# v
1 w" L' Y1 x7 Q) C% I$ ?
成功初始化项目后,转到您的文件夹并在 /contracts目录中创建 greeter.sol 文件。在网络上部署合约之前,我们必须编译它并构建工件。$ T4 {1 Y$ J4 R3 O
b) 智能合约的编译:因此,对于编译,我们将使用 Truffle solc 编译器。在您的主目录中,运行以下命令:5 p d$ N1 c% d* A) |
6 M! a: q) m& G2 W, v# G
# O8 m; j# k# F: `' p
truffle compile$ O! A: G$ J' ^" Y+ }1 {+ F
(or)2 L% s# v7 K- }1 r4 X3 g: R
truffle.cmd compile #(for windows only)8 A1 S9 U' O" C' J1 k, P( l
3 X# ~; q$ L& h# T7 u
: j6 b' o; b/ F) o9 C* D/ G# {
上面的命令将在 /contracts 目录中编译你的合约,并在 /build 目录中创建二进制工件文件 greeter.json。
c) 部署合约:打开您的 Python IDLE 编辑器,并在主目录 deploy.py 中使用以下代码创建一个新文件,然后在您的目录中运行 py deploy.py。
/ C6 s4 a ~2 Y, ~1 B3 N
- import json
- from web3 importWeb3, HTTPProvider
- from web3.contract importConciseContract* o' t6 [/ G' p( c
- # web3.py instance
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/<API key>"))
- print(w3.isConnected())# g! c) J! N o
- key="<Private Key here with 0x prefix>"+ m1 T9 Q6 j% _! k, ^: c4 U- g
- acct = w3.eth.account.privateKeyToAccount(key)
- # compile your smart contract with truffle first
- truffleFile = json.load(open('./build/contracts/greeter.json'))
- abi = truffleFile['abi']
- bytecode = truffleFile['bytecode']! A; n% C0 V7 {8 J7 K) `8 o
- contract= w3.eth.contract(bytecode=bytecode, abi=abi)' K. A$ M1 D: B! f" t2 ]: G- Y
- #building transaction) @# T6 M0 Z& G: \+ I
- construct_txn = contract.constructor().buildTransaction({& @2 J h2 ]+ C! v
- 'from': acct.address,$ `8 T* T5 K% u
- 'nonce': w3.eth.getTransactionCount(acct.address),
- 'gas': 1728712,! r* D- y2 o+ { D2 ?# A
- 'gasPrice': w3.toWei('21', 'gwei')})1 n3 W- q9 N0 \; h% d" O
- signed = acct.signTransaction(construct_txn)6 t3 F8 S) y* E* Z: b( B U/ c
- tx_hash=w3.eth.sendRawTransaction(signed.rawTransaction)
- print(tx_hash.hex())0 j f2 V3 |% i; m" I, Z8 {
- tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)' t/ U7 a( p. ^5 j# o9 y. u
- print("Contract Deployed At:", tx_receipt['contractAddress'])
3 ^8 A/ b# E6 |# l2 I/ [3 a- t
这是我所做的:
导入的 web3 库和所有其他必需的模块
通过指向 Ropsten Infura 节点启动 web3 提供程序
添加了用于签署交易的帐户地址和私钥。不要忘记在代码中添加您的凭据。5 W+ m; E N/ I4 L$ m% \$ r
通过指向 Truffle 编译的工件文件greeter.json的abi和字节码启动合约实例/ l8 C, \: u) { K
添加了带有随机数、gas、gasPrice 等参数的construct_txn。此处,gas 是指交易应在以太坊中使用和支付的最大计算资源量。gasPrice 是指在交易中使用该数量的 gas 时的最小 Ether 数量。to 指的是您发送交易的地址。仅当您将 Ether 发送到帐户或智能合约时才需要 to 参数。
使用我们的私钥签署交易并在网络上广播。 E! A9 S' z, N
在控制台中记录交易哈希和部署的合约地址。根据以太坊的说法,事务处理时间 <20 秒。所以你必须等待 20 秒才能获得部署的合约地址。您的后端现在已成功部署在以太坊区块链上。现在您可以使用此地址与您的智能合约进行交互。复制此合约地址。
2. 向部署的合约发送交易6 I0 m- B0 |& j# `
, p, v" E3 Q8 j3 ^$ A4 \- ?
在我们的合约中,有一个方法greet()。我们可以单独使用这种方法在我们的合同中添加问候语。让我们看看我们如何使用 web3.py 来做到这一点。打开您的 Python IDLE 编辑器并使用以下代码创建一个新文件 sign.py。然后在您的目录中运行 py sign.py。; H, m8 v9 t; Y y- b* ]$ Z2 k2 x
- import json
- from web3 importWeb3, HTTPProvider
- from web3.contract importConciseContract/ [9 s5 Y: x, ]1 j6 y8 |& f
- # compile your smart contract with truffle first
- truffleFile = json.load(open('./build/contracts/greeter.json'))8 n2 K& ~" u& w- N' N& u/ T% F
- abi = truffleFile['abi']
- bytecode = truffleFile['bytecode']# Z/ R9 |6 ^" ~! F C
- # web3.py instance. \/ \6 E8 ], V2 \
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/<API key>")) #modify9 p3 @/ U+ Y, [. A/ {
- print(w3.isConnected())0 [8 P" B5 l6 |0 }" f
- contract_address = Web3.toChecksumAddress("<Deployed Contract Address here>") #modify
- key="<Private key with 0x prefix here>"#modify
- acct = w3.eth.account.privateKeyToAccount(key)
- account_address= acct.address& F2 |2 W/ t8 c0 D5 H, E6 H
- # Instantiate and deploy contract- C. k: m' E- |5 N- b4 j. A9 `9 o
- contract = w3.eth.contract(abi=abi, bytecode=bytecode)
- # Contract instance& i2 c* ~7 S O( Y6 L# D; ?
- contract_instance = w3.eth.contract(abi=abi, address=contract_address)! R0 u) N6 S' b8 ~$ Y) H; _/ ]: m
- # Contract instance in concise mode
- #contract_instance = w3.eth.contract(abi=abi, address=contract_address, ContractFactoryClass=ConciseContract), i, Q: e; y% O; p: W
- tx = contract_instance.functions.greet("Hello all my goody people").buildTransaction({'nonce': w3.eth.getTransactionCount(account_address)})
- #Get tx receipt to get contract address7 C# u+ r6 X1 s$ P$ {
- signed_tx = w3.eth.account.signTransaction(tx, key)8 V2 h9 M# S% k G% v
- #tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
- hash= w3.eth.sendRawTransaction(signed_tx.rawTransaction)/ S, z4 \ [. M/ H" @( A, j$ ]' K
- print(hash.hex())
3 Z. D3 F& m6 ?/ z
& z2 x6 _9 ^5 I, Q$ ^7 q8 f
这是我所做的:
' _2 B+ r: M7 X9 o, R6 R
导入的 web3 库和所有其他必需的模块
通过指向 Ropsten Infura 节点启动 web3 提供程序2 |2 A* r$ d7 I8 J
添加了用于签署交易的帐户地址和私钥7 s! V' g4 g* h6 ?) _$ p4 t
通过指向 Truffle 编译的工件文件greeter.json的abi和字节码启动合约实例* o" T7 ]7 o: K
创建 tx 对象以添加问候语“hello all my goody people”并建立交易
使用我们的私钥签署交易并在网络上广播。
在控制台中记录交易哈希。您可以使用您的交易哈希在 etherscan 上检查交易状态。一旦交易被矿工验证,我们的问候语将被添加到区块链上。您可以使用 getGreeting() 函数检索问候语,如下所述。" l8 j; u+ G( A% k9 f
3. 从部署的智能合约中读取数据
" o( Q5 n6 p" f# l4 H
在我们的合约中,有一个方法 getGreeting() 可以检索我们在区块链中添加的问候语。我们将使用 web3.py 调用此方法 打开您的 Python IDLE 编辑器并使用以下代码创建一个新文件 read.py。运行 py read.py 读取问候语。
- import json
- from web3 importWeb3, HTTPProvider. r2 Y+ X/ }8 w5 z
- 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']
- # web3.py instance
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/<ApI Key here>"))
- print(w3.isConnected())
- contract_address = Web3.toChecksumAddress("<Deployed Contract Address here>")5 m% s- X# i9 t9 t8 p
- # Instantiate and deploy contract/ e; `) V. U) `7 a9 G
- contract = w3.eth.contract(abi=abi, bytecode=bytecode)' U6 s, S4 m3 G3 N8 C" ~ M
- # Contract instance
- 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)
- # Getters + Setters for web3.eth.contract object ConciseContract0 K) c* G. G+ N/ X3 J% M" z
- #print(format(contract_instance.getGreeting()))
- print('Contract value: {}'.format(contract_instance.functions.getGreeting().call()))
) K( S" {0 h( J$ ~
7 q* G& F$ U) |$ k; k
这是我所做的:
导入的 web3 库和所有其他必需的模块/ v: U4 A1 l" u$ Q9 \
通过指向 Ropsten Infura 节点启动 web3 提供程序
通过指向 abi 和 contract_Address 创建合约实例(来自上一步)2 g* ~! Z2 [. Z; B
调用 getGreeting() 方法并在控制台打印结果$ f/ Q+ g8 n" f- M8 f4 l
结论6 h! T* |' [. e* N" {$ N
您现在应该了解如何使用 web3.py 部署、交互和签署交易。让我们通过查看我使用 web3.py 创建的实时 DApp 并在此处使用 Flask 作为前端服务器来将所有部分组合在一起。它将帮助您更深入地了解使用 Python 进行 DApp 开发。- f; t& v- `% y% i
t( p# U& F# C: c1 j1 R
, i$ z( R0 l8 D& n/ ~
参考 @温室小书生室d 《python利用web3.py开发以太坊应用dapp的实战教程》