Was wondering if there is already an open source solidity code with method to sweep erc20 tokens received in contract address to destination address by calling an in-contract method such as "sweep()".

The objective is to sweep received erc20 tokens in the contract to two pre-defined wallet addresses, by calling the method in the contract.



The problem with receiving tokens is that a contract can't know when it receives tokens - there is no transaction to the contract which receives the tokens as the tokens are exchanged only inside the token contract's balances. There are lots of posts about it, here's one for example: Detect token transaction to a contract

So basically what you'd need to do is something like this:

1) Create a way for the contract to know that it has received tokens. Probably someone from outside the blockchain has to tell the contract.

2) The contract has to check its own balance inside the desired token contract(s) - unless your contract can trust the information from step 1 (and the balance information is relayed to it from backend). If it can trust the information then it doesn't have to check the balances as it already knows them.

3) The contract calls the token contracts' transfer function

  • Understood, however was wondering if there is an open source example code to a function to send the erc20 tokens contained in a smart contract by calling a smart contract method from the owner. It will have to account for using the remaining gas received from calling the method as well. – Zinxer Jan 31 '20 at 9:21
  • Your contract just has to call the transfer function of the token contract. The tricky part is finding out what tokens your contract owns (in which token contract the tokens are registered). – Lauri Peltonen Jan 31 '20 at 10:39

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.