简体   繁体   中英

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

I have deployed my smart contract on private network using mist and geth.

Now confusion is that : How can I interact with smart contract through Web3.js.

This is my script :

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

When I tried below commands:

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

Once you set up infura you can use the portal url that they return to you to create your provider. just edit your script to look like:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM