I am trying to swap two ERC20 tokens using uniswap v3 and in hardhat test its working correctly but its not working on sepolia testnet.
this is the smart contract :
// SPDX-License-Identifier: GPL-2.0-or-later pragma solidity ^0.7.6; pragma abicoder v2; import '@uniswap/v3-periphery/contracts/libraries/TransferHelper.sol'; import '@uniswap/v3-periphery/contracts/interfaces/ISwapRouter.sol'; contract SwapExamples { ISwapRouter public constant swapRouter = ISwapRouter(0xE592427A0AEce92De3Edee1F18E0157C05861564); uint24 public constant poolFee = 3000; function swapExactInputSingle(uint256 amountIn,address _tokenIn,address _tokenOut) external returns (uint256 amountOut) { TransferHelper.safeTransferFrom(_tokenIn, msg.sender, address(this), amountIn); TransferHelper.safeApprove(_tokenIn, address(swapRouter), amountIn); ISwapRouter.ExactInputSingleParams memory params = ISwapRouter.ExactInputSingleParams({ tokenIn: _tokenIn, tokenOut: _tokenOut, fee: poolFee, recipient: msg.sender, deadline: block.timestamp, amountIn: amountIn, amountOutMinimum: 0, sqrtPriceLimitX96: 0 }); amountOut = swapRouter.exactInputSingle(params); } } And this is the test :
import { expect } from "chai"; import { ethers } from "hardhat"; // const DAI = "0x6B175474E89094C44Da98b954EedeAC495271d0F"; const WETH9 = "0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2"; // MAINNET // const WETH9 = "0xfFf9976782d46CC05630D1f6eBAb18b2324d6B14"; // Sepolia const USDC = "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48"; // MAINNET // const USDC = "0x6f14C02Fc1F78322cFd7d707aB90f18baD3B54f5" // Sepolia describe("swapExamples", function () { it("swapExactInputSingle", async function () { const accounts = await ethers.getSigners(); const weth = await ethers.getContractAt("IWETH",WETH9); const usdc = await ethers.getContractAt("IERC20",USDC); const SwapExamples = await ethers.getContractFactory("SwapExamples"); const swapExamples = await SwapExamples.deploy(); await swapExamples.waitForDeployment(); const amountIn = BigInt("1000000000000000000"); await weth.connect(accounts[0])?.deposit({value:amountIn}); await weth.connect(accounts[0]).approve(swapExamples.getAddress(),amountIn); await swapExamples.swapExactInputSingle(amountIn,WETH9,USDC); console.log("USDC balance : ",await usdc.balanceOf(accounts[0].address)); console.log(accounts[0].address); }); }); and this is the hardhat config :
import { HardhatUserConfig } from "hardhat/config"; import "@nomicfoundation/hardhat-toolbox"; import { privateKey1, privateKey2 } from "./constants/constants"; const config: HardhatUserConfig = { solidity: "0.7.6", networks:{ hardhat:{ forking:{ url:"https://mainnet.infura.io/v3/42237fbfbd2a472c88e935a4bfac5aac" } } } }; export default config; and after the test . Its pass
and then i change the hardhat config as per sepolia network and change the token addresses to token address on sepolia chain . And i test with sepolia network (npx hardhat test --network sepolia) .But its giving error that
if any one knows the solution please help.

