0

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.

1

The proper way to do this is as follows:

  1. 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).
  2. User calls buyWithTokens([amountToSpend]). The function calls transferFrom on the DAI contract to take the amount of tokens they specified.
  3. 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.

| improve this answer | |

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.