1

I want to send both ether and ERC20 automatically from contract address to beneficiary. Following is the code that I've written to send ether -

contract FundTrans {    
    address owner;    
    constructor () public {
        owner = msg.sender;
    }    
    mapping (address => uint256) balances;    
    function () payable private {
       owner.transfer(msg.value);
    }    
}

I don't know how this will work for erc20. And if there are any improvements or correction in the current code please do give me suggestion.

Thanks in advance

1 Answer 1

1

Your code will send the ether sent to the contract to the owner. make your fallback public.

function () payable public {
       owner.transfer(msg.value);
    }    

This won't work for tokens sent to the contract. You will need to use the method approveAndCall on the ERC20. Problem is that not all ERC20 contracts implement approveAndCall.

If the function is implemented as described in the ethereum ERC20 standard wiki, your users should execute the function approveAndCall (see link above) in the token contract.

You should implement the following function in your contract.

function receiveApproval(address from, uint256 tokens, address token, bytes data) public {
    TokenERC20 tokenInstance =  TokenERC20(token);
    tokenInstance.transferFrom(from, owner, tokens);
}

As you can see, the annoying part here is that simply transferring the tokens to the contract won't work, because your contract will not know about the transfer and you will have to implement a function to manually check the balance and send the available tokens to your account. That is why the users will have to do approveAndCall which approve a number of tokens of a particular user to be moved by you and then calls you contract to let it know about the transfer.

Hope this helps.

4
  • so if a user sends erc20 to my contract address, there will be no way to automatically transfer that to my wallet? Is there any way to manually check balance inside my solidity contract at regular intervals?
    – dkpaul91
    May 26, 2018 at 12:48
  • No. And for automatically check, you can use services like Oraclize or ETH-Tempus (I wrote the last one). ETH-Tempus basically call your contract every certain time (as you indicate) and can run a function to send tokens, accessing the tokens contract, from your contract to your address. You can try it on rinkeby to test it. You can also check the token balance when someone sends you ether, the problem is that this is not predictable, but is simpler. If I answered your question please accept it.
    – Jaime
    May 26, 2018 at 12:55
  • Thanks for your answer. I'll try out those libraries that you've mentioned. So, even if I do use these services to check my erc20 token balance, won't the transaction require me to have some ether balance to pay for the gas?
    – dkpaul91
    May 28, 2018 at 5:45
  • Yes, you will need to be sure that your contract will be able to pay, however the cost of each call should be in the range of cents of dollar, You may try both of them and compare prices and complexity. For the case of Oraclize you will find a lot of examples, for the case of ETH-Tempus the example in the link is all you need, let me know how it goes.
    – Jaime
    May 28, 2018 at 5:49

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.