在进行网站或客户端开发时,就需要用到webjs。webjs库是一个javascript库,可以让你执行很多与区块链进行交互的任务。与Ajax类似,可以使用Webjs读写以太坊区块链。
区块链网络是一个点对点的对等网络,网络由各个节点相互连接构成,智能合约的代码与数据存储在区块链中。使用webjs可以向区块链网络中某个以太坊节点发出请求,与区块链网络进行交互。
webjs使用JSONRPC与Ethereum区块链通信,对区块链网络进行数据读写。这有点像使用jQueryajax库来读写web服务器上的数据。
这里记录一下Webjs的常用操作。
通过npm安装
npminstall--saveweb3@0.20
有了这个Web3以太坊编程接口,就可以与以太坊主网进行通信。
访问节点
constWeb3=requireconstrpcURL=""//RPCURLconstweb3=newWeb3constaddress=""//账户地址//读取address中的余额,余额单位是weiwebeth.getBalance=>{//余额单位从wei转换为etherbalance=webutils.fromWeiconsole.log})
调用读取智能合约
constWeb3=requireconstrpcURL=""//YourRCPURLgoeshereconstweb3=newWeb3constabi=[{"constant":true,"inputs":[],"name":"mintingFinished","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"name","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_spender","type":"address"},{"name":"_value","type":"uint256"}],"name":"approve","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"totalSupply","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_from","type":"address"},{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transferFrom","outputs":[],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"decimals","outputs":[{"name":"","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"unpause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"}],"name":"mint","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"paused","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"}],"name":"balanceOf","outputs":[{"name":"balance","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"finishMinting","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":false,"inputs":[],"name":"pause","outputs":[{"name":"","type":"bool"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"owner","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[],"name":"symbol","outputs":[{"name":"","type":"string"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_value","type":"uint256"}],"name":"transfer","outputs":[],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"_to","type":"address"},{"name":"_amount","type":"uint256"},{"name":"_releaseTime","type":"uint256"}],"name":"mintTimelocked","outputs":[{"name":"","type":"address"}],"payable":false,"type":"function"},{"constant":true,"inputs":[{"name":"_owner","type":"address"},{"name":"_spender","type":"address"}],"name":"allowance","outputs":[{"name":"remaining","type":"uint256"}],"payable":false,"type":"function"},{"constant":false,"inputs":[{"name":"newOwner","type":"address"}],"name":"transferOwnership","outputs":[],"payable":false,"type":"function"},{"anonymous":false,"inputs":[{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Mint","type":"event"},{"anonymous":false,"inputs":[],"name":"MintFinished","type":"event"},{"anonymous":false,"inputs":[],"name":"Pause","type":"event"},{"anonymous":false,"inputs":[],"name":"Unpause","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"owner","type":"address"},{"indexed":true,"name":"spender","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Approval","type":"event"},{"anonymous":false,"inputs":[{"indexed":true,"name":"from","type":"address"},{"indexed":true,"name":"to","type":"address"},{"indexed":false,"name":"value","type":"uint256"}],"name":"Transfer","type":"event"}]constaddress="0xd26114cd6EE289AccF82350c8d8487fedB8A0C07"constcontract=newwebeth.Contractcontract.methods.totalSupply.call=>{console.log})contract.methods.name.call=>{console.log})contract.methods.symbol.call=>{console.log})contract.methods.balanceOf.call=>{console.log})
准备账号
执行交易
执行部署
执行写函数调用
获取事件
//获取事件contract.getPastEvents.then{console.log});
区块查询
webeth.getBlockNumber.then//查询最新区块webeth.getBlock.then//查询某个区块中的指定交易信息consthash="0x66b3fd79a49dafe44507763e9b6739aa0810de2c15590ac22b5e2f0a3f502073"webeth.getTransactionFromBlock.then
常用工具操作
//根据最近几个区块,计算平均Gas价格webeth.getGasPrice.then=>{console.logconsole.log)})//评估Gas费webeth.estimateGas.then;
这个文档可以的,webjs–智能合约对象|奇客谷教程????
文章为作者独立观点,不代表 股票程序化软件自动交易接口观点