2

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.

4 Answers 4

2

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

}
3
  • Let me know if you have any questions or if this is too low-level. Commented Jun 8, 2018 at 16:56
  • I'm more concerned about the erc20 tokens more than the ETH itself.
    – Strikers
    Commented Jun 11, 2018 at 5:35
  • @VigneshKarthikeyan Would forwarding ETH using this approach be cheaper than manually forwarding it using a regular transaction (vs. smart contract)? If yes, how would one calculate the savings in gas costs? If not, presumably the main benefit is that the forwarding is automatic?
    – S.O.S
    Commented Jan 18, 2021 at 1:07
1

It's not really possible to do this automatically with the current, ERC20, token standard - and it's seen as a failing, and inconsistency, of this early standard.

That is why new 'advanced' token standards such as ERC223, ERC777 & ERC827 are being developed to handle the situation where the transfer of tokens to a contract can trigger an action.

1
  • So, you are saying with ERC20 we cannot trigger action when the tokens are sent to contract. SO instead can we have a scheduled event to check if we have token balance in the wallet and send them all to the parent wallet. And one more thing if we do not have ETH balance and have only ERC20 tokens how can pay the gas fees for transfer.
    – Strikers
    Commented Mar 15, 2018 at 4:05
1

Agree with @SteveJackson. This is a limitation of ERC20.

If these child contracts will be contracts then you can implement the CanReclaimToken solution. https://github.com/OpenZeppelin/openzeppelin-solidity/blob/master/contracts/ownership/CanReclaimToken.sol

This sets up the contracts to allow for recovery of arbitrary tokens that end up in the contract's possession. The Owner passes in the address of the ERC20 to recover the balance of unexpected tokens.

In practical terms, you can consider making a software client that maintains a list of known ERC20 contracts by observing a source then observes those token contracts to see if any transactions arrive for any of the child contracts.

It would then be a matter of invoking the reclaimToken() as Owner.

Certainly, some implementation details to work out but it would be a workable approach if this an important issue.

Hope it helps.

0

You need to follow the below steps. Consider this as abstract steps, you will have to implement it further.

  1. Define the interface of the token with its transfer method in it.
  2. On the contract create a variable for that interface and assign the token contract address to it.
  3. On the transfer function call token.transfer(beneficiary,tokenBalance

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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