2
votes

PS: am using web3 beta-37 (Since every version has its own issues) Anything under the line web3.eth.getAccounts in the deploy function isn't working. When i run the code it just shows nothing! Here is the code:

const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const {interface , bytecode} = require('./compile');

const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/v3/my_project_id');

const web3 = new Web3(provider);

const deploy = async() => {
    const accounts = await web3.eth.getAccounts();
    console.log('Attempting to deploy from account', accounts[0]);
    const result = await new web3.eth.Contract(JSON.parse(interface))
                             .deploy({data: bytecode, arguments: ['Hi There!']})
                             .send({'from': accounts[0], 'gas': '1000000'});

    console.log('Contract deployed to: ', result.options.address);
};
deploy();

Also, testing this in mocha shows error while it works when using ganache-cli

const assert = require('assert');
const HDWalletProvider = require('truffle-hdwallet-provider');
const Web3 = require('web3');
const {bytecode , interface} = require('../compile');

const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/v3/project_id');



let accounts;

beforeEach(async () => {
  //Get a list of all accounts
  accounts = await web3.eth.getAccounts();

});

describe('Inbox', () => {
  it('address', () => {
    assert.ok(accounts);
  })

Here are the results with different versions: beta-46: Cannot read property map() of undefined One answer on stackexchange says use beta-36 to fix this issue

beta-36 and beta-37: Nothing after the getAccounts() statement gets executed, blank screen.

beta-26: core.addProviders is not a function

1
The code seems fine to me. I shouldn't be saying this but you don't actually have my_project_id in the Infura link, right? Apart from that, it seems weird that it is not working? What version of web3 do you use? Can you verify that the provider is set correctly (perhaps through debugging)?nikos fotiadis
@nikosfotiadis on console.log(provider) it shows me a big object with 10 address array as well (with one address). So i guess provider is set correctly and no am not using my_project_id. Also web3 version is beta-37.akshay sharma
can you share your compile.js file? and does Attempting to deploy from account this line get console logged?Iftifar Taz
@IftifarTaz it's fine am sure ....I've run test in mocha... This thing was working perfectly with ganache... And No... nothing after const accounts = await web3.eth.getAccounts(); gets console loggedakshay sharma

1 Answers

1
votes

I also had the same issue.

Try to remove the "/v3" part from the infura url:

const provider = new HDWalletProvider(mnemonic,
'https://rinkeby.infura.io/my_project_id');

Hope this helps.