ethereum開發環境構建(client端基於python)


1. 安裝go-eth和solidity編譯器

  a. mac系統

    brew tap ethereum/ethereum

    brew install ethereum solidity

  b. ubuntu系統

    sudo add-apt-repository ppa:ethereum/ethereum

    sudo apt-get update

    sudo apt-get install ethereum solc

2. 配置私有鏈

  a. 獲取私有鏈配置數據

    git clone https://github.com/janx/ethereum-bootstrap

  b.  配置私有鏈

    cd ethereum-bootstrap    #進入倉庫目錄

    ./bin/import_keys.sh  #導入測試賬戶私鑰

    ./bin/private_blockchain_init.sh  #初始化blockchain

  c. 啟動私有鏈節點  

    geth --datadir data --networkid 31415926 --rpc --rpccorsdomain "*" --rpcapi="db,eth,net,web3,personal,miner,admin" --nodiscover console

    如果要啟動公鏈節點,可以這樣:

    geth --fast --cache=1024 --rpc --rpccorsdomain "*" --rpcapi="db,eth,net,web3,personal,miner,admin" console

 

至此,一個私有鏈環境搭建完畢, 下面開始寫python代碼與此節點交互

 

3. 安裝python客戶端

  a. 安裝python3編譯器

    詳情參考 https://www.python.org/

  b. 安裝第三方庫

    pip3 install web3

    pip3 install py-solc

 

4. 編寫第一個demo 

import time
from web3 import Web3, HTTPProvider


# 連接私有鏈節點
w3 = Web3(HTTPProvider("http://localhost:8545"))

# 獲取前面兩個賬戶
from_user = w3.eth.accounts[0]
to_user = w3.eth.accounts[1]

# 解鎖賬戶
w3.personal.unlockAccount(from_user, '123')
w3.personal.unlockAccount(to_user, '123')

# 開始挖礦
w3.miner.start(1)

# 查詢兩個賬戶的余額
print(w3.eth.getBalance(from_user))
print(w3.eth.getBalance(to_user))

# 從第1個賬戶轉123塊到第2個賬戶
w3.eth.sendTransaction({'to': to_user, 'from': from_user, 'value': 123})

# 休息15秒,再來查詢他們第余額,觀察是否變化
time.sleep(15)
print(w3.eth.getBalance(from_user))
print(w3.eth.getBalance(to_user))

 

==========================   end ==========================

 

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM