-1

I am a complete noob with solidity and smart contracts. I was looking for a script that solves the following problem. User sends ether to a smart contract then the smart contract forwards the eth amount to a main address. So i found this

pragma solidity ^0.4.18;

/**
 * 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() public {
    destinationAddress = msg.sender;
  }

  /**
   * Default function; Gets called when Ether is deposited, and forwards it to the destination address
   */
  function() payable public {
        destinationAddress.transfer(msg.value);
  }

  /**
   * It is possible that funds were sent to this address before the contract was deployed.
   * We can flush those funds to the destination address.
   */
  function flush() public {
    destinationAddress.transfer(this.balance);
  }

}

1st question: does this script work? does it do what I described?

2nd question: where is the place to put in my main address? my address is 0x7e0fE0Bd87F84906bc19438fb5F932e189Dd127e how should my code look like after inserting it?

I will send 0.15 ETH for the best answer. thank you.

  • What's the purpose here? – ferit May 20 '18 at 20:35
1

It should work fine. I updated it to solc 0.4.21 so it's more current, and removed the ^ because that causes unwanted ambiguity in some cases. Also added event emitters to observe best practices. Removed the comments to cut down on visual noise.

pragma solidity 0.4.21;

contract Forwarder {

  address public destinationAddress;
  event LogForwarded(address indexed sender, uint amount);
  event LogFlushed(address indexed sender, uint amount);

  function Forwarder() public {
    destinationAddress = msg.sender;
  }

  function() payable public {
    emit LogForwarded(msg.sender, msg.value);
    destinationAddress.transfer(msg.value);
  }

  function flush() public {
    emit LogFlushed(msg.sender, address(this).balance);
    destinationAddress.transfer(address(this).balance);
  }

}

Hope it helps.

0

The contract works. As you will deploy the contract, your main address will be the address of the account that you use to deploy the contract. This is done in the constructor function of your contract:

function Forwarder() public {
    destinationAddress = msg.sender;
  }

This function runs only once when you deploy the contract. it is assigning the value of the address of the person who deploys de contract to the destinationAddress

I 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.