web3.js icon indicating copy to clipboard operation
web3.js copied to clipboard

Connecting to local blockchain using web3 and HttpProvider

Open katkol opened this issue 8 years ago • 3 comments

I wrote an application to connect to Ganache test network. Here is the code:

var Web3 = require('web3');
var contract = require('./PublishService_abi.js');
var PublishServiceContract;
var web3 = new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));

function init() {
  //initializing web3 to access blockchain
  initweb3();
}

async function initweb3() {
    try {
        /*//To make sure not to overwrite the already set provider when in mist, check first if the web3 is available
        if (typeof web3 !== 'undefined') {
                web3 = await new Web3(web3.currentProvider);
            console.log("inside if");
        } else {
            // create an instance of web3 using the HTTP provider
                web3 = await new Web3(new Web3.providers.HttpProvider("http://localhost:7545"));
            console.log("inside else");
        }*/
        console.log(web3.eth.accounts[1]);
        var PublishServiceContractAddress = "0x0da136781e562e28fb1c0b3efc8ce9d8b4e226f2";
        PublishServiceContract = await new web3.eth.Contract(contract,PublishServiceContractAddress);
        //console.log(PublishServiceContract.options.jsonInterface); 
        await addServiceProducer1("LC1","SP1","location:inside;reading:degree","scattr","ngac");
    }
    catch(err) {
        console.log(err);
    }
}

async function addServiceProducer1(s1,s2,s3,s4,s5) {
    try{
        console.log(s1 +"\n" + s2 +"\n" + s3 +"\n" + s4 +"\n" + s5 +"\n" );
        const instance = await PublishServiceContract.methods.addEntry(s1,web3.eth.accounts[1],s2,s3,s4,s5).send({from:web3.eth.accounts[1]});
        console.log("sp details added successfully");
    }
    catch(err) {
        console.log(err);   
    }
}

init();

The statement console.log(web3.eth.accounts[1]); returns undefined, where it should return the second account address. I am using 1.0.0-beta.34 version of web3. I receive the below error when I tried to call functions of the contract in the addServiceProducer1 function.

Error: No "from" address specified in neither the given options, nor the default options.

I also tried with statement var web3 = new Web3(Web3.givenProvider || 'http://127.0.0.1:7545'); as shown in web3 documentation http://web3js.readthedocs.io/en/1.0/web3.html#version, but it too returns undefined

Thanks in advance

katkol avatar Apr 24 '18 19:04 katkol

You should use web3.eth.getAccounts(). Documentation: http://web3js.readthedocs.io/en/1.0/web3-eth.html#getaccounts

nivida avatar Aug 09 '18 20:08 nivida

If you are using [email protected] web3.eth.getAccounts() you will get the array of accounts that exist on the Node (If Node is set as provider) and If you are using [email protected] web3.eth.accounts() will do the trick.

Note: Suppose yours are node has only one account and you try to access accounts[1] i.e 2nd Account in your fetched array, it will be undefined as it does not exist.

dev1644 avatar Jan 07 '19 13:01 dev1644

Closed because of the ongoing clean up of the issue list. Feel free to ask this in our gitter channel or on stackoverflow.

nivida avatar Apr 16 '19 09:04 nivida