trufflesuite / truffle-hdwallet-provider

HD Wallet-enabled Web3 provider
MIT License
400 stars 167 forks source link

Account Not Found error #78

Closed rawadrifai closed 5 years ago

rawadrifai commented 5 years ago
var coinbase = "0x...";
var provider = new HDWalletProvider("....", "http://localhost:8545");
const truffleContract = TruffleContract(ERC20TokenABI);
truffleContract.setProvider(provider);

    // create new ERC20
    const result = await truffleContract.new(
      {
        from: coinbase,
        gas: gasLimit
      }
    );

It fails when I call a function of an initiated truffle contract.

Error: the string "Account not found" was thrown, throw an Error :) at process._tickCallback (internal/process/next_tick.js:68:7)

rawadrifai commented 5 years ago

More info on this: truffle migrate works fine if I configure my truffle.js correctly.

However, unit tests that declare a new HDWalletProvider fail as shown above.

joepagan commented 5 years ago

I am quite new to this myself, but, It looks like you are trying to use a coinbase eth wallet as the from account. I would love to be able to do this myself, but, I think you need to either auth the account, or, unlock it if you have the account password.

If I am correct your new HDWalletProvider auths a wallet itself when you pass it a "mnemonic". like so:

const mnemonic = process.env.WALLET_MNEMONIC;
const provider = new HDWalletProvider(mnemonic, process.env.WEB3_PROVIDER, 0);
const web3 = new Web3(provider);
const accounts = await web3.eth.getAccounts();
const web3account = accounts[0];

So web3account is an account you can use in your from property at this point.

Funnily enough, I want to do exactly this myself, and, I even posted a question earlier today which has now been answered.

Feel free to upvote me on stackexchange 💃

rawadrifai commented 5 years ago

@joepagan - which version of web3 are you using? I'm using 0.20.x and the await web3.eth.getAccounts() is returning undefined for me. I wonder if it's the web3 version.

So the only thing I am doing differently is hardcoding the account address as opposed to calling getAccounts. Are you saying the getAccounts auto unlocks the accounts?

Furthermore, are you able to do something similar with a private key instead of mnemonic?

Thank you and I will upvote you on stackexchange.

rawadrifai commented 5 years ago

btw - I'm not sure if by "Coinbase" you meant the company in San Francisco. They took their name from eth's coinbase (which is the default account). That's what I refer to in my code.

rawadrifai commented 5 years ago

more info - If I follow @joepagan instructions it will throw this error on web3.eth.getAccounts(): Error: Web3ProviderEngine does not support synchronous requests.

rawadrifai commented 5 years ago

ok I figured it out.. just needed to do coinbase.toLowerCase(). It now finds the account.