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.

underdog
  • 1,190
  • 2
  • 12
  • 24

5 Answers5

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;
  }
Paul Razvan Berg
  • 17,902
  • 6
  • 73
  • 143
underdog
  • 1,190
  • 2
  • 12
  • 24
  • 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. – Prashant Prabhakar Singh Jul 29 '17 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 Jul 31 '17 at 08:29
  • Thanks a bunch - adding contract.default({from: ...}) - fixed my issue. – Andrey Feb 07 '18 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

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 web3@0.20.6 and Firefox 100.0.1

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

web3.eth.defaultAccount = web3.eth.accounts[0]
sola24
  • 1,238
  • 3
  • 20
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)