1

If a user sends a token, let's call it ABC, to the contract that issued that token, can the contract send back ether to the user?

1

Yes, for example:

contract ABCToken {
    function sell(uint abcAmount) public {
        uint ethAmount = myFunc(abcAmount);
        msg.sender.transfer(ethAmount);
    }
}

Of course, you probably want to make sure that this user (msg.sender) owns the specified amount of ABC tokens...

1

Adding to goodVibration's answer, you want to make sure the user actually sent what they say they sent.

contract ABCToken {
    function sell(uint abcAmount) public {
        require(token.transferFrom(msg.sender, address(this), abcAmount)); 
        uint ethAmount = myFunc(abcAmount);
        msg.sender.transfer(ethAmount);
    }
}

Transfer from requires the sender to first send an approve() to the token contract to set an allowance the above function can use to retrieve the user's tokens. The 2-step process is usually coordinated client-side or in another contract.

Hope it helps.

  • 1
    Thanks. Risk of scribbling it up freestyle. Fixed. – Rob Hitchens - B9lab Nov 12 '18 at 15:57

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.