0

I am building a Smart Contract that can receive tokens and in exchange for the tokens received another token will be issued.

my problem is how can i make a function send automatically my tokens to the address of sender when the another erc20 token received

if you have code write it

1 Answer 1

1

This isn't possible using an ERC20 token, it would require another function call to your smart contract to say that the tokens have been sent.

There are ERCs which try to deal with the situation you have mentioned such as ERC223 which would add a tokenFallback function to contracts for handling token transfers. If you were dealing with ERC223 tokens then you use the tokenFallback function to send tokens in response to tokens being received.

To implement this with ERC20 tokens you would need the person exchanging tokens to give your contract a token allowance, and then call the claimTokens function.

(Very) Rough sample contract:

contract ExchangeTokens {

    address tokenToSend;

    function claimTokens(address tokenSent) {
        uint256 numTokens = ERC20(tokenSent).allowance(msg.sender, this);
        if (numTokens > 0) {
            ERC20(tokenSent).transferFrom(msg.sender, this, numTokens);
            ERC20(tokenToSend).transfer(msg.sender, numTokens);
        }
    }
}
7
  • First you state it isn't possible with ERC20 and then you give an implementation of it with ERC20, hmm? :) Apr 4, 2018 at 5:40
  • @LauriPeltonen His question asked "how can i make a function send automatically my tokens to the address of sender when the another erc20 token received" which is not possible, it is possible if you ask the user to manually call a function in your smart contract. The ERC223 contract I mentioned would also make his question possible.
    – Matt
    Apr 4, 2018 at 5:56
  • i want to create a token relay like this with the ERC20 because if i use the ERC223 my token relay will be not supported with the ERC20 wallet
    – user36849
    Apr 4, 2018 at 17:00
  • Have a read through the Solidity contracts in your link then github.com/bancorprotocol/contracts/blob/master/solidity/…
    – Matt
    Apr 4, 2018 at 21:32
  • Your question asked for something different, which is what my answer is answering
    – Matt
    Apr 4, 2018 at 21:33

Your Answer

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

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