Search code examples
blockchainethereumsoliditysmartcontractsweb3js

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

Solution

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