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

Interacting with a Smart Contract on private network through Web3.js

我已经使用 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)
}

设置 infura 后,您可以使用他们 return 提供给您的门户 url 来创建您的提供商。只需将您的脚本编辑为:

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);