0

I am having a hard time creating a contract that uses the swapExactETHForTokens function.

I intend to create a contract that receives ETH and then any user can swap those ETH for a specific token whose recipient is the same contract. Freezing those tokens (A token burning contract).

I'm using something like this, following the uniswap docs as a guide:

uint256 deadline = block.timestamp + 1800;
address[] path = [WETH, UBI]; //Each with its respective declared address

InterfaceUniswapV2Router(UNISWAP_V2_ROUTER_ADDRESS).swapExactETHForTokens
{value: address(this).balance}(1, path, address(this), deadline);

So far so good, I set a deadline of let's say 30 minutes, and I can make those swaps without any problem, however, when those 30 minutes of the first swap are reached, the contract does not allow me to make any other swap, sending me the error:

Fail with error 'UniswapV2Router: EXPIRED'.

It doesn't matter if I exceed the gas limit and the gas price. I always get that error.

This is one of the failed transactions of one of the instances https://kovan.etherscan.io/tx/0x446ab53eb290c9f5a40b34944c3dcb960843bef55a7a4fc00302270823c17bcc

Am I doing something wrong?

2
  • 1
    Try printing the deadline and check the timestamp before calling the function. The error is from the ensure modifier which checks deadline should greater than timestamp. Commented Jul 28, 2021 at 6:17
  • It was a silly mistake, thanks for pointing it out, I realize that I was declaring the deadline outside the function, so the deadline was set according to the time the contract was deployed. Commented Jul 29, 2021 at 1:35

1 Answer 1

1

The mistake was to set the deadline outside the function that calls swapExactETHForTokens, so it was set according to the time the contract was deployed.

If a time variable needs to be used according to actual parameters at run time, then it is necessary to declare it or set it at the time of use. Silly mistake, but important after all.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.