5

I've been trying to create a contract that forwards any ETH it receives to the account that published the contract.

I've tried several variations on this code, including hard-coding the address, removing the flush function, and others. It deploys successfully, but when I send Ether to the contract, it fails to execute with the warning:

Warning! Error encountered during contract execution [Bad jump destination]

I've consulted the documentation, and browser-solidity suggests it should work correctly. What am I missing?

Complete source code

pragma solidity ^0.4.2; /** * Contract that will forward any incoming Ether to its creator */ contract Forwarder { // Address to which any funds sent to this contract will be forwarded address public destinationAddress; /** * Create the contract, and set the destination address to that of the creator */ function Forwarder() { destinationAddress = msg.sender; } /** * Default function; Gets called when Ether is deposited, and forwards it to the destination address */ function() { if (!destinationAddress.send(this.balance)) throw; } } 

Example of a failed transaction.

What am I missing?

1 Answer 1

3

Number 1: The Fallback function must be payable, like this:

function () payable { if (msg.value > 0) { if (!destinationAddress.send(msg.value)) throw; // also reverts the transfer. } } 

The keyword payable is required for the function to be able to receive Ether.

Number 2: The contract needs to have enough gas for it to send the funds back to it's Creator address.

Sending a simple transaction with only 21000 gas will result in Out Of Gas error. See: https://etherscan.io/tx/0xe2603426571e996a915abd17af2e1605432283db44bce54d19350a770ed76434).

I've tested this by sending 50000 gas and it worked. See: https://etherscan.io/address/0x104ea4435b2ed36f36dc403b3638d82ec6a21bb7

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.