web3与智能合约交互的方法


1、创建一个web3对象
var Web3 = require("web3");
var web3 = new Web3();
web3.setProvider(new Web3.providers.HttpProvider("http://localhost:8545"));

2、使用web3.eth.Contract()创建一个已部署的智能合约实例。

var abi = [{"constant":true,"inputs":[],"name":"data","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"},{"inputs":[],"payable":false,"stateMutability":"nonpayable","type":"constructor"},{"constant":true,"inputs":[],"name":"getData","outputs":[{"name":"","type":"string"}],"payable":false,"stateMutability":"view","type":"function"}];

var address = '0x........';
var data = new web3.eth.Contract(abi,address);

address为合约的部署地址
3、调用合约函数
data.methods.方法().call(null,function(error, result){
    console.log("the data:"+result);
   });
详细可以进入第三方链接查看https://github.com/ethereumproject/wiki/wiki/JavaScript-API

本文由 我爱PHP169 作者:admin 发表,其版权均为 我爱PHP169 所有,文章内容系作者个人观点,不代表 我爱PHP169 对观点赞同或支持。如需转载,请注明文章来源。

发表回复