0

Here are two contract functions. The first works fine, the second displays the following error: "Warning! Error encountered during contract execution [Bad instruction]"

function DoTransferToAccount() {
    address objAddress = 0xA7432011366d6b26b4604eD18282cB53F7E07D74;
    objAddress.transfer(.05 ether);
}

function DoTransferToContract() {
    address objAddress = 0x29662dA5D870FFb39201A8320214421eE9fE9D25;
    objAddress.transfer(.05 ether);
}

Here is the javascript that calls the two functions:

myContractInstance.DoTransferToAccount.sendTransaction({from:"0x3d86B4D6f28554428E5AF38490DD1977691A3082"},function(error, result){
    if(!error) {
        console.log("#" + result + "#")
    } else {
        console.error(error);
    }
})

myContractInstance.DoTransferToContract.sendTransaction({from:"0x3d86B4D6f28554428E5AF38490DD1977691A3082"},function(error, result){
    if(!error) {
        console.log("#" + result + "#")
    } else {
        console.error(error);
    }
})

The contract already has a balance, so the functions don't need to transfer ether to the contract.

Why does DoTransferToContract() fail, when it is virtually identical to DoTransferToAccount()?

Here you can see the test transactions that I ran: https://ropsten.etherscan.io/address/0x272ff5b0ac9dfbbc37e19bf9430f88055f3013e1

This transaction worked: https://ropsten.etherscan.io/tx/0xc0390a4b179187bb285935e81e447129d31c3f024f1bc1237000a23c1156d518

This transaction failed: https://ropsten.etherscan.io/tx/0x6029f73b4a4e963e288707a1ce4fe32322ef2e4568b0a180bb99acdc41733352

I get the same error when I use the MetaMask interface to send ether to this contract. Does a contact need to have some special attribute to receive ether?

1

it needs a payable function. You can just use a fallback payable function to catch all ether sent to the contract

function() payable {
}

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