11

With Truffle 3.0 when I call one of my contract's function, in console I have this error:

Uncaught (in promise) Error: invalid address

I found this workaround, and in my code I try this with no success.

this.web3Provided.eth.defaultAccount=this.web3Provided.eth.coinbase;

In truffle doc they sat I can set a from address in my config, but also this not work. from: '0x8c384d9f226ea92c99f7aa83340714a6f82a3161'

I also try with this after I have imported all truffle.config file:

TruffleConfig.networks[NODE_ENV].from = this.web3Provided.eth.coinbase;

Please help me.

5 Answers 5

13

Thank's to Truffle Gitter channel I figured out. I have to call defaults function on my truffle-contract's abstraction.

MyContract.defaults({from: …}) 

https://github.com/trufflesuite/truffle/tree/develop/packages/truffle-contract

buildContracts() {
    let contracts = {};
    let meta;

    this.props.contracts.forEach( _contract => {
      let {contract_name = ''} = _contract;
      meta = contract(_contract);
      meta.setProvider(this.web3Provided.currentProvider);
      meta.defaults({from: this.web3Provided.eth.coinbase});
      contracts[contract_name] = meta;
    });
    return contracts;
  }
3
  • 3
    Can you post an elaborated answer, like where you have added the line? I am also getting the same error while calling a contract's function using web3. I am using testrpc. The contract was deployed via truffle. Commented Jul 29, 2017 at 14:35
  • 3
    This is my build contract function, a simple function that loop my contract list. For each contract I had to set a default with a coinbase. buildContracts() { let contracts = {}; let meta; this.props.contracts.forEach( _contract => { let {contract_name = ''} = _contract; meta = contract(_contract); meta.setProvider(this.web3Provided.currentProvider); meta.defaults({from: this.web3Provided.eth.coinbase}); contracts[contract_name] = meta; }); return contracts; }
    – underdog
    Commented Jul 31, 2017 at 8:29
  • Thanks a bunch - adding contract.default({from: ...}) - fixed my issue.
    – Andrey
    Commented Feb 7, 2018 at 14:57
4

This worked for me. Hope my answer helps future readers.

You have to the set the defaultAccount to be used:

web3.eth.defaultAccount = web3.eth.accounts[0];

before using web3 instance.

Also, In my case where I'm using react-truffle box, the invalid address can be solved by adding.

this.state.web3.eth.defaultAccount = this.state.web3.eth.accounts[0];

before doing anything related to web3.

NOTE : Changing states in react like this is highly not recommended. Try using this.setState() to manipulate the web3 object's child. I haven't thought much about the app that I'm developing so for me if this works, I'M GOLDEN :D

2
  • Could you elaborate on this please? Why doesn't truffle do this by default? Commented Aug 19, 2018 at 0:56
  • I'm not sure why truffle doesn't do this by default, but my guess is web3.eth.accounts[0]; part which should not be statically defined. Commented Aug 20, 2018 at 6:27
2

I had an owner withdrawal function in my smart contract that would throw the following error:

Uncaught (in promise) Error: invalid address

I eventually realized that either Truffle or MetaMask expects an explicit from address.

This was my original function definition:

ownerWithdrawal() {
    return this.contractInstance.ownerWithdrawal(); // calls ownerWithdrawal() from Solidity contract
  }

Here is my new function definition with from address specified.

ownerWithdrawal() {
    return this.contractInstance.ownerWithdrawal({from: this.web3accounts[0]});
  }
1

using an old version of web3 for an outdated tutorial [email protected] and Firefox 100.0.1

this was what I added to react to get around the problem

web3.eth.defaultAccount = web3.eth.accounts[0]
0

Setting the default account for web3 or adding a from property will fix the error. You can consult the web3.eth documentation here (web3.eth documentation)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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