% H* p* T) Q( G2 R2 C2 o+ \
“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 库与区块链进行交互。# Y2 A& w. A( R( n+ P' n9 ?* v
Dapps 开发包括三个简单的步骤:
- 在区块链网络上部署智能合约
- 从部署的智能合约中读取数据
- 将交易发送到部署的智能合约
6 D k( Z' X i9 ?- V6 ^
安装4 z" r; c; H* L4 Y4 J! R
Python 2.7 +
Node.js
Truffle
% o# E7 N4 D4 c+ S0 r/ E& x
npm install -g truffle* Z4 D) \- Q4 K2 M) w' s) L; C0 |
Pip
npm i pip
web3.py
pip install web3 r2 b# b0 d/ c
Infura 项目 API
前往 Infura 网站并注册。创建一个新项目并复制 Ropsten Infura RPC URL。我们将把我们的智能合约部署到 Ropsten 测试网络。
% K3 [6 V6 O8 C! i+ U0 S! e
智能合约
) `: Q6 n; S! b3 c& L
每个程序员都用他们最喜欢的编程语言执行了一个“hello world”程序,以了解运行该语言的基础知识。这是我们使用 Solidity 语言编写的简单的“hello world”版本的智能合约,我们可以在区块链上添加问候语并检索它。Solidity 是编写智能合约最常用的语言,它编译为可以在节点上运行的以太坊虚拟机上执行的字节码。6 @- M- f1 j+ n3 E0 d- \
- pragma solidity ^0.5.7;
- contract greeter{
- string greeting;7 t& v$ S. R. g* a5 B0 @% y
- function greet(string memory _greeting)public{ u4 v G/ t' p# H8 U8 v) K# ^
- greeting=_greeting;
- }6 g5 X, m# F/ f
- function getGreeting() public view returns(string memory) {
- return greeting;
- }0 M8 M; S) w! p2 N( ^
- }+ ]5 z7 `: Y# S. y; v( P& P
1 Q4 d2 x3 L8 L, \9 P
您可以通过传递字符串值使用 greet() 方法添加问候语,并使用 getGreting() 方法检索问候语。% l( P! k+ S( d. C: j; N: N
1. 在区块链网络上部署智能合约1 y* ]) d% O& _ g4 `# R% Q
4 B! {2 D7 h9 @2 {3 x
a) 创建项目:
% W' j" ~$ i( a' S' [! ~/ U
mkdir pythonDapp- T% K' z: |" u/ V/ f# m. N; {
cd pythonDapp5 u7 T. L+ G2 K7 @2 @
truffle init2 S6 f( _1 q+ w5 n
3 Q ` O- I/ x: l6 y
成功初始化项目后,转到您的文件夹并在 /contracts目录中创建 greeter.sol 文件。在网络上部署合约之前,我们必须编译它并构建工件。
( W# ]" D& g! f- |( K, j
b) 智能合约的编译:因此,对于编译,我们将使用 Truffle solc 编译器。在您的主目录中,运行以下命令:
truffle compile- a/ _! c& y: p4 l
(or)# U, u7 V$ y' R2 W7 Z2 p
truffle.cmd compile #(for windows only)
* b: v3 A9 k6 ^% I1 Q( h+ D: t, }5 x
上面的命令将在 /contracts 目录中编译你的合约,并在 /build 目录中创建二进制工件文件 greeter.json。
c) 部署合约:打开您的 Python IDLE 编辑器,并在主目录 deploy.py 中使用以下代码创建一个新文件,然后在您的目录中运行 py deploy.py。- r, x( }+ m% v" m! H K! D
' _* Q1 E; f+ {* O7 ^1 N& `
- import json. x1 f" ?: f# ]7 H% ]
- from web3 importWeb3, HTTPProvider5 I! g# R% d- j+ ~( {" x7 Z
- from web3.contract importConciseContract
- # web3.py instance
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/<API key>"))- i% |* Y, G* w1 f! K
- print(w3.isConnected())
- key="<Private Key here with 0x prefix>"! h( o/ N* O$ k* X k }
- acct = w3.eth.account.privateKeyToAccount(key)
- # compile your smart contract with truffle first1 ^0 a$ {" o- O; _6 v$ |$ J. J% L
- truffleFile = json.load(open('./build/contracts/greeter.json'))
- abi = truffleFile['abi']3 i) @' U$ [; Y" w! |+ D3 I9 D
- bytecode = truffleFile['bytecode']* Z( q2 S7 b8 I( X
- contract= w3.eth.contract(bytecode=bytecode, abi=abi)
- #building transaction$ e6 f* [6 y# U5 a+ d! Q# T! h
- construct_txn = contract.constructor().buildTransaction({& r" m% v8 T- O- E" `% e
- 'from': acct.address,) t6 R& c9 c( T$ }
- 'nonce': w3.eth.getTransactionCount(acct.address),
- 'gas': 1728712, @' F% R; |0 |0 m3 Q" ]1 R9 X4 |, v
- 'gasPrice': w3.toWei('21', 'gwei')})
- signed = acct.signTransaction(construct_txn)
- tx_hash=w3.eth.sendRawTransaction(signed.rawTransaction). g( x9 c* a- O& M- ?
- print(tx_hash.hex()), `- F ?% g3 I- F: t
- tx_receipt = w3.eth.waitForTransactionReceipt(tx_hash)) L8 r& E: E1 Z* c7 k6 @" l
- print("Contract Deployed At:", tx_receipt['contractAddress'])
这是我所做的:* ]2 W: g3 J( |
8 n; S6 T& N N4 O7 U
导入的 web3 库和所有其他必需的模块
通过指向 Ropsten Infura 节点启动 web3 提供程序3 p T# H8 B# t r; j$ {* D
添加了用于签署交易的帐户地址和私钥。不要忘记在代码中添加您的凭据。
通过指向 Truffle 编译的工件文件greeter.json的abi和字节码启动合约实例
添加了带有随机数、gas、gasPrice 等参数的construct_txn。此处,gas 是指交易应在以太坊中使用和支付的最大计算资源量。gasPrice 是指在交易中使用该数量的 gas 时的最小 Ether 数量。to 指的是您发送交易的地址。仅当您将 Ether 发送到帐户或智能合约时才需要 to 参数。
使用我们的私钥签署交易并在网络上广播。
在控制台中记录交易哈希和部署的合约地址。根据以太坊的说法,事务处理时间 <20 秒。所以你必须等待 20 秒才能获得部署的合约地址。您的后端现在已成功部署在以太坊区块链上。现在您可以使用此地址与您的智能合约进行交互。复制此合约地址。- v! n5 }, M! W
2. 向部署的合约发送交易+ |1 g! N* O5 k* L
在我们的合约中,有一个方法greet()。我们可以单独使用这种方法在我们的合同中添加问候语。让我们看看我们如何使用 web3.py 来做到这一点。打开您的 Python IDLE 编辑器并使用以下代码创建一个新文件 sign.py。然后在您的目录中运行 py sign.py。 i; q; l: \7 C" p
- import json
- from web3 importWeb3, HTTPProvider
- from web3.contract importConciseContract5 v0 J0 L- b3 n
- # compile your smart contract with truffle first
- truffleFile = json.load(open('./build/contracts/greeter.json'))
- abi = truffleFile['abi']5 E9 O$ j8 T' |
- bytecode = truffleFile['bytecode']
- # web3.py instance
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/v3/<API key>")) #modify
- print(w3.isConnected())
- contract_address = Web3.toChecksumAddress("<Deployed Contract Address here>") #modify" _1 r' c8 {! f* u7 w
- key="<Private key with 0x prefix here>"#modify
- acct = w3.eth.account.privateKeyToAccount(key)
- account_address= acct.address# Y3 x% o+ [: S, B1 B
- # Instantiate and deploy contract# m q- q4 k/ w
- contract = w3.eth.contract(abi=abi, bytecode=bytecode)
- # 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)3 o) |( I# y, P0 P
- tx = contract_instance.functions.greet("Hello all my goody people").buildTransaction({'nonce': w3.eth.getTransactionCount(account_address)})
- #Get tx receipt to get contract address3 \! `3 k7 w4 R$ q9 N0 o+ V$ E- z
- signed_tx = w3.eth.account.signTransaction(tx, key)3 p$ ?/ ^" v: [1 _' U
- #tx_receipt = w3.eth.getTransactionReceipt(tx_hash)
- hash= w3.eth.sendRawTransaction(signed_tx.rawTransaction)
- print(hash.hex())
这是我所做的:4 Z' ~- c0 s* J, j9 s+ I
+ x) Z) ` v$ a m& F p
导入的 web3 库和所有其他必需的模块
通过指向 Ropsten Infura 节点启动 web3 提供程序) N* B' x: z( U
添加了用于签署交易的帐户地址和私钥
通过指向 Truffle 编译的工件文件greeter.json的abi和字节码启动合约实例9 C Z, O5 p1 L# j- t% y, J
创建 tx 对象以添加问候语“hello all my goody people”并建立交易
使用我们的私钥签署交易并在网络上广播。
在控制台中记录交易哈希。您可以使用您的交易哈希在 etherscan 上检查交易状态。一旦交易被矿工验证,我们的问候语将被添加到区块链上。您可以使用 getGreeting() 函数检索问候语,如下所述。
3. 从部署的智能合约中读取数据
" _- W) _7 D3 c, }7 O% D
在我们的合约中,有一个方法 getGreeting() 可以检索我们在区块链中添加的问候语。我们将使用 web3.py 调用此方法 打开您的 Python IDLE 编辑器并使用以下代码创建一个新文件 read.py。运行 py read.py 读取问候语。
- 7 x. i a2 j+ ]
- import json8 M% i5 [- |% }( F7 V& {" ~8 m4 U
- from web3 importWeb3, HTTPProvider
- from web3.contract importConciseContract8 t. }' i" B5 G5 o
- # compile your smart contract with truffle first7 |8 ~6 x- A+ b
- truffleFile = json.load(open('./build/contracts/greeter.json'))8 J; M6 K& T0 V4 L9 M
- abi = truffleFile['abi']
- bytecode = truffleFile['bytecode']6 w- L( v5 v" k4 z- k/ h
- # web3.py instance
- w3 = Web3(HTTPProvider("https://ropsten.infura.io/<ApI Key here>"))6 T# l) z) V. D/ n1 K) _
- print(w3.isConnected())4 j( a, E% M& p) v; }$ b1 G$ r
- contract_address = Web3.toChecksumAddress("<Deployed Contract Address here>"): {( M [% _+ Z
- # Instantiate and deploy contract
- contract = w3.eth.contract(abi=abi, bytecode=bytecode)4 M5 Q* p7 c4 \
- # Contract instance6 D3 I( z7 t% L6 l
- contract_instance = w3.eth.contract(abi=abi, address=contract_address)- G) c( f4 y7 K4 D+ Y- [
- # Contract instance in concise mode
- #contract_instance = w3.eth.contract(abi=abi, address=contract_address, ContractFactoryClass=ConciseContract)9 N9 F J1 o+ k" L3 Q l6 ^- G
- # Getters + Setters for web3.eth.contract object ConciseContract
- #print(format(contract_instance.getGreeting()))
- print('Contract value: {}'.format(contract_instance.functions.getGreeting().call()))
这是我所做的:) l' P% b1 \* B
导入的 web3 库和所有其他必需的模块. N- C. D" q- t: u1 m5 b* p
通过指向 Ropsten Infura 节点启动 web3 提供程序
通过指向 abi 和 contract_Address 创建合约实例(来自上一步)$ O. c% x4 ~# U. n% p; U3 c
调用 getGreeting() 方法并在控制台打印结果
结论5 n! L! B; y; p6 k( B# H8 \0 t
" ]( g2 Z' N- W3 O9 ]! g
您现在应该了解如何使用 web3.py 部署、交互和签署交易。让我们通过查看我使用 web3.py 创建的实时 DApp 并在此处使用 Flask 作为前端服务器来将所有部分组合在一起。它将帮助您更深入地了解使用 Python 进行 DApp 开发。
2 ?5 m4 q9 t# N3 l1 ^' v- j
5 w9 f7 l+ W* L3 {' Z5 \% L
参考 @温室小书生室d 《python利用web3.py开发以太坊应用dapp的实战教程》 H& L1 ]. h1 m9 [ G