1

I wrote an application to connect to the local blockchain. 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. Can someone help me understand the problem with this code. I am using 1.0.0-beta.34 version of web3.

Thanks in advance

1

Can you please also just call console.log(web3) instead of web3.eth.accounts[1] just to make sure that web3 is really set and using the right provider?

EDIT:

After rechecking the docs (http://web3js.readthedocs.io/en/1.0/index.html) I noticed in 1.0.0-beta of web3 they changed quite a lot.

The web3.eth.accounts contains functions to generate Ethereum accounts and sign transactions and data.

So there is no way to interact with the nodes accounts from web3.eth.accountsfunction.

But:

The web3-eth-personal package allows you to interact with the Ethereum node’s accounts.

So to achieve fetching accounts you can do something like:

web3.eth.personal.getAccounts().then(res => console.log(res[0]))


Also just to let you know: You won't be able to call the contract function as you are trying to...if you do want to do this from code you will need to sign the transaction.

  • Hi, I commented out that if-else block. I think you missed to notice that. Yes, the problem is i cannot access the provider. even i tried console.log(web3.eth) and you can find the console output here: github.com/katkol/Share/blob/master/web3_eth_log.txt – K Kolluru Apr 25 '18 at 10:25
  • Sorry about that, as you said I overlooked it. So I just rechecked docs of web3-beta and noticed that web3.eth.accounts was completly changed. To get the accounts I just did the following: web3.eth.personal.getAccounts().then(res => console.log(res[0])) – Kai Kälberer Apr 25 '18 at 10:49
1

First verify you are on the right chain. If your second account isn't there you could be connected to the wrong chain ID, network, etc. Then check your accounts. If you are using a local host, confirm your coinbase and etherbase with

eth.defaultAccount=eth.coinbase

Have you tried using ipc provider instead of http? Here is an example:

var web3 = new Web3(new Web3.providers.IpcProvider('/home/you/yourchain/datadir/geth.ipc', require('net')));

You can still connect using your local host address, but you can point it to the file running your chain an call over ipc/rpc.

Make sure all rpc flags, such as corsdomain "*" are enabled on your local chain, as well as "personal,eth,web3,net".

  • Hi, I tried to connect to this chain using truffle and deployed a contract from account 1, so I don't think there is a problem with chain ID. I will try with ipc aswell. – K Kolluru Apr 25 '18 at 6:53
0

First of all, be sure that you have number of accounts.
web3.eth.accounts[0] is first account in the array of accounts.
web3.eth.accounts[1] retrieve 2nd account of the network.

confirm with web3.eth.coinbase , It represent account[0]

Addition on answer

check whether the endpoint connection is true .

Web3.providers.HttpProvider("http://localhost:7545"));
            console.log(Web3);

If this do not throw any error and show web3 related console output then your end point is true.
After this check whether your code snippet is correct .

  • Hi, I am using ganache test network. It has 10 accounts with 100 ether balance. I tried to print eth0 account as well using web3.eth.accounts[0], it is also returning undefined. – K Kolluru Apr 24 '18 at 17:13
0

Only web3.eth.accounts[] is not working in web3 v 1.x.x beta version. I tried with web3.eth.getAccounts() function and then storing it in a var named accounts and tried to use accounts[1] and it too worked.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.