1

Here is a contract I deployed on my private network:

pragma solidity ^0.4.13;

contract test {
    string stat;
    address owner;

    event statChanged(string _stat);

    function test() {
        stat = "INIT";
        owner = msg.sender;
    }

    function() payable {
        stat = "PAID";
    }

    function getStat() constant returns (string) {
        return stat;
    }

    function getBalance() constant returns (uint) {
        return this.balance;
    }

    function getOwner() constant returns (address) {
        return owner;
    }

    function send2Owner() {
        owner.transfer(this.balance);
    }

    function update(string _stat) {
        stat = _stat;
        statChanged(_stat);
    }
}

I send ETH to the contract:

eth.sendTransaction({from: owner, to: contract, value: 1100000, gas: 200000, gasPrice: 1})

By running the contract .getBalance() I can see that the correct amount of ETH has arrived at the contract address, .getOwner() I can also see a good owner address too. Finally, I run .send2Owner to send the ETH back to owner:

var testContract = web3.eth.contract(...).at("...");
testContract.send2Owner();

Now I get this error:

Error: invalid address
    at web3.js:3879:15
    at web3.js:3705:20
    at web3.js:4948:28
    at map (<native code>)
    at web3.js:4947:12
    at web3.js:4973:18
    at web3.js:4998:23
    at web3.js:4061:16
    at apply (<native code>)
    at web3.js:4147:16

Why am I getting this error?

  • where you define the web3 var? – Gawey Jul 28 '17 at 6:46
  • To answer your question, I run the script in geth console by loadScript("send2Owner.js"). In the script contains something like the following before getting the error --> var testContract = web3.eth.contract(...).at("..."); var output = testContract.send2Owner(); console.log(output); – Consy Jul 28 '17 at 7:04
  • First check if the abi and the contract adress are the real adress before this web3.eth.contract(...).at("...");, i don't know how work in geth but for example if you use metamask you need a web3 injected item for the good conection with the account, so you problem is when you try to send cause this is diferent action , you can check balance and getOwner because this don't do any change in the blockchain. – Gawey Jul 28 '17 at 7:18
1

Missed a parameter when I launch .send2Owner()

var testContract = web3.eth.contract(...).at("...");
testContract.send2Owner();

This is the correct version

var testContract = web3.eth.contract(...).at("...");
testContract.send2Owner({from:eth.accounts[0]}); 
1

Try to set the default account and then execute:

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

Hope this helps...!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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