Uniswap / v2-core

🦄 🦄 Core smart contracts of Uniswap V2
https://uniswap.org/docs
GNU General Public License v3.0
2.92k stars 3.13k forks source link

Fail with error 'UniswapV2Router: EXPIRED' #129

Closed santanaruben closed 3 years ago

santanaruben commented 3 years ago

Hello friends, 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?

santanaruben commented 3 years ago

The mistake was to declare 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.