uniswap 无法在 sepolia 网络上工作,出现错误

问题描述 投票:0回答:1

我正在尝试使用 uniswap v3 交换两个 ERC20 代币,并在安全帽测试中其工作正常,但在 sepolia 测试网上不起作用。

这是智能合约:

// 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);
    }
}

这就是测试:

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);
  });
});

这是安全帽配置:

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;

以及测试后。通行证

然后我根据sepolia网络更改安全帽配置,并将代币地址更改为sepolia链上的代币地址。我用 sepolia 网络进行测试(npx Hardhat test --network sepolia)。但它给出了错误

如果有人知道解决方案请帮忙。

ethereum uniswap
1个回答
0
投票

您尝试访问的 Uniswap 路由器合约在 Sepolia 网络上不可用。

© www.soinside.com 2019 - 2024. All rights reserved.