Ethereum: Layer2 Revert Execution Error when Calling exactInputSingle
in Uniswap V3
Hello Stack Overflow Community,
I am working on swapping tokens in a base layer2 network using Uniswap V3 and ethers.js. I have successfully approved the transaction using the approve
function, but recently encountered an error that prevents me from executing the swap.
The issue occurs when calling exactInputSingle
with exact input parameters, specifically when calling exch.swapExactTokensForETHWithRate
. This specific use case has caused a revert execution error in Uniswap V3.
Background
Uniswap V3 provides several functions for swapping tokens on layer 2 networks. Two of the most common approaches are swapExactTokensForETH
and exch.swapExactTokensForETHWithRate
. While both functions allow for precise control over the swap execution, they have different parameter sets and usage patterns.
The Issue: Exact Input Single with Uniswap V3
When calling exactInputSingle
, it takes an object with specific parameters, including:
amountIn
(the amount of token to input)
amountOut
(the amount of token to output)
to
(the recipient’s address)
pairId
(the pair ID to use for the swap)
However, when using exactInputSingle
, we need to pass the exact input parameters to avoid any potential issues. One such parameter is gasPrice
, which specifies the gas price for the execution.
In Uniswap V3, calling exch.swapExactTokensForETHWithRate
with the same input parameters as exactInputSingle
will cause a revert execution error because of the following reasons:
- The gas price specified in
exch.swapExactTokensForETHWithRate
is not compatible with the gas price provided byexch.swapExactTokensForETHWithRate
. This mismatch can lead to an unexpected result, resulting in a revert.
- Even if we ignore the above reason, calling both functions with the same input parameters may still cause issues due to their different usage patterns.
Solution
To resolve this issue, you should use exch.swapExactTokensForETHWithRate
when swapping tokens exactly. Here’s an updated code snippet:
import * as Uniswap from '@uniswap/v3-core';
import { ethers } from 'ethers';
// Set the gas price for the execution
const gasPrice = new ethers.providers.GasPriceProvider(
process.env.ALLIANCE_GAS_PRICE,
new ethers.providers.KilohopProvider()
);
// Create a Uniswap instance with the provided gas price provider
const uniswap = new Uniswap({
provider: new ethers.providers.FlexGateway(
new ethers.providers.EthereumProvider(process.env.ETHEREUM_ADDRESS, process.env.ALLIANCE_URL),
gasPrice,
{
chainId: process.env.CHAIN_ID,
},
),
});
// Call the swap function
uniswap.swapExactTokensForETHWithRate(
tokensIn,
tokensOut,
ethTo,
[gasPrice],
{ amountIn: tokensIn, amountOut: tokensOut, to: ethTo }
);
By using exch.swapExactTokensForETHWithRate
with the correct gas price provider and passing the exact input parameters, you should be able to resolve the revert execution error caused by calling exactInputSingle
.
Conclusion
In summary, when swapping tokens exactly in Uniswap V3 using ethers.js, make sure to use exch.swapExactTokensForETHWithRate
instead of exactInputSingle
. This approach will ensure that your swap execution is correct and free from revert execution errors. If you still encounter issues, please provide more details about the error message or the specific code snippet involved.
Thank you for pointing out this issue in the original post!