5

I am trying to send a transaction using the Etherscan write contract tool at the following address: https://kovan.etherscan.io/address/0x7a250d5630b4cf539739df2c5dacb4c659f2488d#writeContract

The method I wish to use is: swapExactTokensForToken

And the parameters are:

amountIn: 100000000000000000 (0.1 ETH)
amountOutMin: 1400000000000000000
path: [0xc02aaa39b223fe8d0a0e5c4f27ead9083c756cc2,0x25e1474170c4c0aa64fa98123bdc8db49d7802fa]
To: destination address
timestamp: 1602450476

I have tried that, but the transaction fails with an error 'TransferHelper: TRANSFER_FROM_FAILED'.

https://kovan.etherscan.io/tx/0x311805561520f7502befe6e1de6b49e85b9292314ac7a50c0b9fae18c84d8530

I had a look at the code fro TransferHelper.sol but I cannot find the problem.

Can someone explain why this fails?

4
  • Something doesn't add up. The error that you're getting implies that you need to first submit the transaction token.approve(uniswapContractAddress, amountIn) from your account. But you've indicated that amountIn is in ETH, so what's going on here??? Sep 30 '20 at 10:02
  • @goodvibration I think the amountIn is in ETH but I might be wrong. Sep 30 '20 at 10:04
  • NM, my mistake, uniswap uses WETH in order to emulate ETH as a token. Sep 30 '20 at 10:09
  • @goodvibration I'm a newbie and dont know the meaning of uniswapContractAddress. Please tell me about uniswapContractAddress. In my case, I deployed a smart contract (a bot). When approve in the case, uniswapContractAddress is the bot contract address, isnt it?
    – Huo Quile
    Nov 18 '20 at 7:42
5

The error that you're getting implies that you first need to submit from your account the transaction token.approve(uniswapContractAddress, amountIn), where token is an ERC20 contract at the address indicated by path[0].

6
  • Thanks for the feedback. Is the token.approve something I need to do each time I want to run the this swap transaction, or is it a generic approval for all future transactions from WETH here? Sep 30 '20 at 10:19
  • 1
    @TimothéeHENRY: You can approve a really large amount once, or the exact desired amount before each transfer. There are pros and cons to each approach, and the decision of which one to choose depends on your exact use-case. Sep 30 '20 at 10:21
  • I have sent an approval for a larger amount of WETH (kovan.etherscan.io/tx/…), but I am still getting the same error: kovan.etherscan.io/tx/… Sep 30 '20 at 11:14
  • @TimothéeHENRY: You did not approve the right address. Please follow the instructions in my answer, as to what address needs to be approved. Sep 30 '20 at 11:29
  • 2
    @TimothéeHENRY: The answer tells you to approve uniswapContractAddress using the token contract indicated by path[0] (i.e., by executing the approve function on that contract). Sep 30 '20 at 11:45
0

I am still note very sure of

token.approve(uniswapContractAddress, amountIn)

who can extend it more?

where the token is the target ERC20 token contract?

and what the uniswapContractAddress express?

enter image description here

0

To make the answer of @goodvibration more clear, the uniswapContractAddress is the address of Uniswap's Router.

This is outlined in this guide from Uniswap: https://docs.uniswap.org/protocol/V2/guides/smart-contract-integration/trading-from-a-smart-contract#approve

The address of Uniswap's Router on the mainnet for Uniswap V2 is 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D. Addresses of Routers on the testnets can be referenced here: https://docs.uniswap.org/protocol/V2/reference/smart-contracts/router-02

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.