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 ==========================

 

  

转载于:https://www.cnblogs.com/bigdata2013lee/p/8366684.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值