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.

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
    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 8:29
  • Thanks a bunch - adding contract.default({from: ...}) - fixed my issue. – Andrey Feb 7 at 14:57

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

  • Could you elaborate on this please? Why doesn't truffle do this by default? – PaulRBerg yesterday
  • 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. – Pramesh Bajracharya 10 hours ago

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]});
  }

Your Answer

 
discard

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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