[Ethereum] Send all Tokens and Eth to a Address automatically

contract-designerc-20soliditytokens

I have few child wallets and a parent wallet.
Whenever these child wallets get incoming ERC20 tokens or ETH. I want to send them automatically to the parent wallet.

I know I can do below to send the entire eth balance.

beneficiary.transfer(this.balance);

How can I do it for erc20 tokens.

Thanks in advance.

Best Answer

You are looking to effectively 'forward' your balance. A way to go about doing this is using a contract similar to the one highlighted Here.

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

}
Related Topic