通过Web3.js在专用网络上与智能合约交互

时间:2018-02-07 10:05:09

标签: blockchain ethereum solidity smartcontracts web3js

我已经使用mist和geth在私人网络上部署了我的智能合约。

现在的困惑是:我如何通过Web3.js与智能合约互动。

这是我的剧本:

if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            // set the provider you want from Web3.providers
            web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:8545"));
        }
web3.eth.defaultAccount = web3.eth.accounts[0];
var CoursetroContract = web3.eth.contract(YOUR ABI);

var Coursetro = CoursetroContract.at('PASTE CONTRACT ADDRESS HERE');
console.log(Coursetro);

当我尝试以下命令时:

> web3.providers
{
  HttpProvider: function(host, timeout, user, password),
  IpcProvider: function(path, net)
}

1 个答案:

答案 0 :(得分:3)

设置infura后,您可以使用他们返回给您的门户网址网址来创建您的提供商。只需编辑您的脚本,如下所示:

if (typeof web3 !== 'undefined') {
            web3 = new Web3(web3.currentProvider);
        } else {
            // set the provider you want from Web3.providers
            web3 = new Web3(new Web3.providers.HttpProvider(<your infura.io url here>));
        }
web3.eth.defaultAccount = web3.eth.accounts[0];
var CoursetroContract = web3.eth.contract(YOUR ABI);

var Coursetro = CoursetroContract.at('PASTE CONTRACT ADDRESS HERE');
console.log(Coursetro);