Say I have a X token and Y token and I want to swap X tokens in exchange for Y tokens. How can i do that? Its like during crowdsale, instead of using ether to buy the Y token I want to accomplish the same using X token.
The proper way to do this is as follows:
- User sets allowance for your "crowdsale" contract to transfer tokens, by calling
approve([contract], [largeNumber])on the DAI contract (or whatever erc20 you want the user to spend).
- User calls
buyWithTokens([amountToSpend]). The function calls
transferFromon the DAI contract to take the amount of tokens they specified.
- The function then transfers the purchased tokens to the user.
This is more gas efficient, simpler, and far safer (no caveats) than the previous answer.