0

I want to allow my users to deposit funds, and have all those funds sent to a mainwallet cos its gonna be very small amounts of ether ($2-$5 worth) for alot of users. So id imagine i can create a forwarding contract for each user so i can detect payments to the contract and have it all in one place instead of spending my whole week manually transferring small amounts from each account. I suppose theres a script for that.Which approach would you reccomend and if the forwarding contract approach, can you explain whats wrong with this contract. Im getting alot of warnings

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() payable {
        if (!destinationAddress.send(msg.value))
            throw;
  }

  /**
   * 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() {
    if (!destinationAddress.send(this.balance))
          throw;
  }
}
0

The code works as intended. You are getting some warnings because you are not defining the visibility of the functions and you are also using throw which is deprecated.

You can achieve the same results with the following code without warnings.

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);
  }

}
0

The flush function will never move the funds to your address. What you should do is just have the flush function send everything in the contract to destinationAddress

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.