1

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

Test 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

test fail

if any one knows the solution please help.

2 Answers 2

1

The Uniswap Router contract you are trying access not available on the Sepolia network.

Sign up to request clarification or add additional context in comments.

Comments

0

We also had such problem and after investigation we found few issues in Uniswap contracts deployed in sepolia, how did we solve it, we used last official contracts artifacts from packages @uniswap/v3-core and @uniswap/v3-periphery for deployment it to Sepolia

const {ethers} = require("hardhat"); async function main() { // get Sepholia balance here https://www.alchemy.com/faucets/ethereum-sepolia const [deployer] = await ethers.getSigners(); // console.log(deployer); console.log("Deploying contracts with the account:", deployer.address); const accountBalance1 = await deployer.provider.getBalance(deployer.address); // const weiAmount = (await deployer.getBalance()).toString(); console.log("Account balance:", (await ethers.utils.formatEther(accountBalance1))); const wethContract = await ethers.getContractFactory("WETH9"); const weth = await wethContract.deploy(); const IUniswapV3FactoryJson = require('@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json'); const IUniswapV3Factory = new ethers.ContractFactory( IUniswapV3FactoryJson.abi, IUniswapV3FactoryJson.bytecode, deployer ); const IUniswapV3FactoryContract = await IUniswapV3Factory.deploy(); const SwapRouterJson = require('@uniswap/v3-periphery/artifacts/contracts/SwapRouter.sol/SwapRouter.json'); const SwapRouter = new ethers.ContractFactory( SwapRouterJson.abi, SwapRouterJson.bytecode, deployer ); const SwapRouterContract = await SwapRouter.deploy( IUniswapV3FactoryContract.address, weth.address ); const INonfungiblePositionManagerJson = require('@uniswap/v3-periphery/artifacts/contracts/NonfungiblePositionManager.sol/NonfungiblePositionManager.json'); const INonfungiblePositionManager = new ethers.ContractFactory( INonfungiblePositionManagerJson.abi, INonfungiblePositionManagerJson.bytecode, deployer ); const INonfungiblePositionManagerContract = await INonfungiblePositionManager.deploy( IUniswapV3FactoryContract.address, weth.address, '0x0000000000000000000000000000000000000000' // ??? ); console.log( 'IUniswapV3FactoryContract', IUniswapV3FactoryContract.address, 'SwapRouterContract', SwapRouterContract.address, 'INonfungiblePositionManagerContract', INonfungiblePositionManagerContract.address, 'WETH', weth.address ); } main() .then(() => process.exit(0)) .catch((error) => { console.error(error); process.exit(1); }); 

For example https://sepolia.etherscan.io/address/0xb41c59dBcC7E583B4cDC7d8FbD14a69F74fC5E1A#code (SwapRouter)

And as a result it works without any errors https://sepolia.etherscan.io/tx/0x52200c1b6fe26941476c6e7084d7caa68a494d8d92743ddc2c3d3812c87a87a6

Also we faced with such issues like address comparison in pools, some tokens are not supported with WETH when WETH is token0

ATTENTION: make sure you have same versions of packages

{ "name": "solidity", "version": "1.0.0", "description": "", "main": "index.js", "scripts": { "test": "echo \"Error: no test specified\" && exit 1" }, "author": "", "license": "ISC", "devDependencies": { "@ethereum-waffle/mock-contract": "^4.0.4", "@nomicfoundation/hardhat-toolbox": "^5.0.0", "@nomicfoundation/hardhat-verify": "^2.0.9", "@openzeppelin/contracts": "^3.4.2-solc-0.7", "@uniswap/lib": "^4.0.1-alpha", "@uniswap/v2-core": "^1.0.1", "@uniswap/v2-periphery": "^1.1.0-beta.0", "@uniswap/v3-core": "^1.0.1", "@uniswap/v3-periphery": "^1.4.4", "chai": "^5.1.1", "hardhat": "^2.22.6", "hardhat-gas-reporter": "^2.2.0" } } 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.