I have a contract that creates another contract in a function. I am testing the contract using Ganache. When calling this function using geth, it works fine. But when calling it using my dapp (yeoman, gulp, bower, etc.) it gives me "Error: invalid address" via the browser console.

I have copy and pasted the logic from my dapp into geth and it works as expected on geth, and the Ganache UI shows the transaction.

The following is the code:

web3 = new Web3(new Web3.providers.HttpProvider("HTTP://"));

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

var myContract = web3.eth.contract([abi left out for readability]);    
var contractAddress = '0xae01c97dc970ad45f7be5d23ba72ae24640e90b1';
var contract = myContract.at(contractAddress);

function sendPurchaseTrade() {
    contract.makePurchaseTrade('0x427d25b7c0B71C1944df51765c55Bea1961e0a44', {value: 200000, gas: 3000000});

The respective HTML page has a button with onclick="sendPurchaseTrade();".

Non state changing calls to the contract work with the dapp.

Any ideas as to why the exact same code will work on geth but not in my dapp?

  • Does web3.eth.defaultAccount contain the address you expect it to?
    – user19510
    Apr 2, 2018 at 1:02
  • It does indeed. (shown via console.log())
    – Soliditer
    Apr 2, 2018 at 1:02
  • The code works outside of the function. I'm guessing some sort of scope issue.
    – Soliditer
    Apr 2, 2018 at 1:32

1 Answer 1


The function containing the contract transaction had to have the web3.eth.default = web3.eth.accounts[0] statement inside of it.

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.