joshstevens19 / simple-uniswap-sdk

Uniswap SDK which handles the routes automatically for you, changes in trade quotes reactive subscriptions, exposure to formatted easy to understand information, bringing back the best trade quotes automatically, generating transactions for you and much more.
MIT License
188 stars 95 forks source link

Using this to swap erc-20 tokens, is there a need for providing approval before executing the swap? #39

Closed thegitparticle closed 2 years ago

thegitparticle commented 2 years ago

Uniswap requires a wallet give it access/approval to use ERC-20 tokens in the wallet to be accessed before executing a swap. This has be done once for each new token you would use for a swap.

How do I get this approval part using this library or is it already taken care while executing the trade?

niZmosis commented 2 years ago

Once you call the trade() function on the Factory, it will return you with a bunch of things, in there is an approvalTransaction you may call. If the approvalTransaction is undefined, then they are already approved for that token and amount.

thegitparticle commented 2 years ago

Would the trade go through if I do not call the approvalTransaction?

niZmosis commented 2 years ago

I won't be able to swap if that approvalTransaction function exists since you haven't approved the contract to spend your tokens yet. If the user has approved before and didn't swap, and comes back later on, that approvalTransaction will be null and you can just go ahead and swap.