以太坊JSON-RPC客户端

ethjsonrpc的Python项目详细描述


使用json-rpc接口的以太坊python客户端

  • 完成:实现所有62个json-rpc方法和几个特定于客户机的方法
  • 提供一个高级接口,用于在区块链上创建合同并调用合同方法

重要提示

API还不稳定,请在升级时小心使用。

安装

在安装ethjsonrpc之前,可能需要其他库和工具。

在ubuntu 16.04上:

$ sudo apt install python-minimal
$ sudo apt install gcc
$ sudo apt install virtualenv  # optional but recommended
$ sudo apt install libpython-dev
$ sudo apt install libssl-dev

在ubuntu 14.04上:

$ sudo apt-get install python-virtualenv  # optional but recommended
$ sudo apt-get install libpython-dev
$ sudo apt-get install libssl-dev

安装ethjsonrpc:

$ pip install ethjsonrpc

确保有一个运行以太坊客户机(如geth)的节点用于库连接。

示例

>>>fromethjsonrpcimportEthJsonRpc# to use Parity-specific methods, import ParityEthJsonRpc>>>c=EthJsonRpc('127.0.0.1',8545)>>>c.net_version()u'1'>>>c.web3_clientVersion()u'Geth/v1.3.3/linux/go1.5.1'>>>c.eth_gasPrice()50000000000>>>c.eth_blockNumber()828948

高级功能

这些示例假定以下是简单的一致性契约:

contract Example {

    string s;

    function set_s(string new_s) {
        s = new_s;
    }

    function get_s() returns (string) {
        return s;
    }
}

按如下方式编译:

$ solc --binary stdout example.sol

设置

>>>compiled='606060405261020f806100136000396000f30060606040526000357c01000000000000000000000000000000000000000000000000000000009004806375d74f3914610044578063e7aab290146100bd57610042565b005b61004f600450610191565b60405180806020018281038252838181518152602001915080519060200190808383829060006004602084601f0104600302600f01f150905090810190601f1680156100af5780820380516001836020036101000a031916815260200191505b509250505060405180910390f35b61010d6004803590602001906004018035906020019191908080601f016020809104026020016040519081016040528093929190818152602001838380828437820191505050505050905061010f565b005b806000600050908051906020019082805482825590600052602060002090601f01602090048101928215610160579182015b8281111561015f578251826000505591602001919060010190610141565b5b50905061018b919061016d565b80821115610187576000818150600090555060010161016d565b5090565b50505b50565b60206040519081016040528060008152602001506000600050805480601f0160208091040260200160405190810160405280929190818152602001828054801561020057820191906000526020600020905b8154815290600101906020018083116101e357829003601f168201915b5050505050905061020c565b9056'>>>fromethjsonrpcimportEthJsonRpc# to use Parity-specific methods, import ParityEthJsonRpc>>>c=EthJsonRpc('127.0.0.1',8545)

在区块链上创建合同

>>># continued from above>>>contract_tx=c.create_contract(c.eth_coinbase(),compiled,gas=300000)>>># wait here for the contract to be created when a new block is mined>>>contract_addr=c.get_contract_address(contract_tx)>>>contract_addru'0x24988147f2f2300450103d8c42c43182cf226857'

使用事务调用契约函数(存储数据)

>>># continued from above>>>tx=c.call_with_transaction(c.eth_coinbase(),contract_addr,'set_s(string)',['Hello, world'])>>>txu'0x15bde63d79466e3db5169a913bb2069130ca387033d2ff2e29f4dfbef1bc6e0d'

在本地区块链上调用契约函数(读取数据)

>>># continued from above>>>results=c.call(contract_addr,'get_s()',[],['string'])>>>results['Hello, world']

其他示例

有关其他示例,请参见test.py

实现了json-rpc方法

  • web3_客户端版本
  • web3_sha3
  • 网络版
  • 网络监听
  • 净利润
  • eth_协议版本
  • ETH同步
  • eth_coinbase
  • ETH_矿业
  • eth_哈希率
  • 埃思·加斯普里斯
  • ETH账户
  • eth_区块号
  • 获得平衡
  • eth_getstorageat
  • eth-gettransactioncount
  • eth_getBlockTransactionCountByHash
  • eth_getBlockTransactionCountByNumber
  • eth\u getUncleCountByBlockHash
  • eth\u getUncleCountByBlockNumber
  • eth_getcode
  • 以太符号
  • ETH发送事务
  • eth嫒sendrawtransaction
  • 以太电话
  • ETH_估算气体
  • eth_getblockbyhash
  • eth_getblockbynumber
  • eth gettransactionbyhash
  • eth-gettransactionbyblockhashandindex
  • eth gettransactionbyblocknumberandindex
  • eth-gettransactionreceipt
  • eth_getunclebyblockhashandix
  • eth\u getunclebyblocknumberandindex
  • eth_getcompilers
  • ETH_编译性
  • eth_compilell
  • eth_compileserpent
  • 新过滤器
  • eth_newblockfilter
  • eth_newpendingtransactionfilter
  • ETH_卸载筛选器
  • eth_getfilterchanges
  • eth_getfilterlogs
  • eth_getlogs
  • 工作
  • eth_submitwork
  • eth嫒submithashrate
  • 数据库输入字符串
  • 数据库获取字符串
  • db_puthex
  • 数据库获取十六进制
  • shh_版本
  • 嘘嘘嘘
  • 嘘嘘新身份
  • 嘘嘘有身份
  • shh_newgroup
  • shh_addtogroup
  • shh_newfilter
  • shh_卸载筛选器
  • 嘘,换个过滤器
  • shh_getmessages

仅奇偶校验json-rpc方法

要使用这些方法,请确保

  • 以客户身份运行奇偶校验
  • 使用--tracing on选项运行
  • 使用这个库的ParityEthJsonRpcclient(而不是普通的EthJsonRpcclient)

方法:

  • 跟踪过滤器
  • 跟踪获取
  • 跟踪事务
  • 轨迹块

欢迎加入QQ群-->: 979659372 Python中文网_新手群

推荐PyPI第三方库


热门话题
安卓中通过TCP发送时java文件损坏   Java测试预期=异常失败,断言错误   java ssh4 LazyInitializationException   java是否可以在集合中添加重复项?   java是一个免费的开源数据库管理工具   java是否可以在导出的html中嵌入图像   编译器构造解释一些东西,并用Java运行生成的字节码?   java KeyPairGenerator未生成随机密钥   java使用正则表达式生成字符串而不是匹配字符串   java中的多线程线程间通信   具有复合密钥的java Hibernate合并问题   java不能在MainActivity类之外使用SharedReferences变量   正在获取切割/零件异常消息。使用java Spring ControllerAdvice   java tomcat漏洞讨论   javascript在Java中实现“system”命令   java如何在JUnit5*中加载*Spring测试上下文之前获取回调?   java bluej关于缺少返回语句   JavaSpringBoot@ModelAttribute包含使用字符串键的其他模型   从可选中抛出异常。ifPresent()Java 8