【问题标题】:Transaction Fail when trying to swap on Uniswap V3 programmatically尝试以编程方式在 Uniswap V3 上交换时事务失败
【发布时间】:2022-10-22 02:04:33
【问题描述】:

我正在尝试创建一个脚本来在 Uniswap V3 上执行交换。 下面的代码在 Goerli Testnet 网络上运行良好,但在 Mainnet 上运行良好。

为了在主网上运行:

  • 我已更改令牌地址以匹配主网地址,并将 INFURA_URL 更改为主网 url,但我不断收到失败的交易..
const address0 = '0xC02aaA39b223FE8D0A0e5C4F27eAD9083C756Cc2'
const address1 = '0x6B175474E89094C44Da98b954EedeAC495271d0F'

似乎错误可能来自 swapRouterAddress 但不确定..

此外,当我使用 Uniswap 网站手动执行交换时,我可以看到交换是使用 Uniswap V3:路由器 2 执行的,但即使我在脚本上使用此路由器地址,交易仍然失败..

有谁知道如何解决这个问题?

非常感谢 !

const { ethers } = require('ethers')
const { abi: IUniswapV3PoolABI } = require('@uniswap/v3-core/artifacts/contracts/interfaces/IUniswapV3Pool.sol/IUniswapV3Pool.json')
const { abi: SwapRouterABI} = require('@uniswap/v3-periphery/artifacts/contracts/interfaces/ISwapRouter.sol/ISwapRouter.json')
const { abi: UniswapV3Factory } = require('@uniswap/v3-core/artifacts/contracts/UniswapV3Factory.sol/UniswapV3Factory.json')
const { getPoolImmutables, getPoolState } = require('./helpers')
const ERC20ABI = require('./abi.json')

require('dotenv').config()
const INFURA_URL_TESTNET = process.env.INFURA_URL_TESTNET

// Wallets

const WALLETS = [process.env.WALLET_ADDRESS_1];
var SECRET = new Object();
SECRET[process.env.WALLET_ADDRESS_1] = process.env.WALLET_SECRET_1;

// Provider

const provider = new ethers.providers.JsonRpcProvider(INFURA_URL_TESTNET) // Goerli
const swapRouterAddress = '0xE592427A0AEce92De3Edee1F18E0157C05861564'

// Wrapped Ether

const name0 = 'Wrapped Ether'
const symbol0 = 'WETH'
const decimals0 = 18
const address0 = '0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6'

// Token info

const name1 = 'Token'
const decimals1 = 18
const address1 = '0x11fE4B6AE13d2a6055C8D9cF65c55bac32B5d844' // DAI

// SWAP

const factoryAddress = '0x1F98431c8aD98523631AE4a59f267346ea31F984'

async function buy(WETH_amount) {

    const factoryContract = new ethers.Contract(
        factoryAddress,
        UniswapV3Factory,
        provider
    )

    const poolAddress = await factoryContract.getPool(address0, address1, 500)

    for (const WALLET of WALLETS)
    {
        const poolContract = new ethers.Contract(
            poolAddress,
            IUniswapV3PoolABI,
            provider
        )
    
        const immutables = await getPoolImmutables(poolContract)
        const state = await getPoolState(poolContract)
    
        var WALLET_SECRET = SECRET[WALLET];
        const wallet = new ethers.Wallet(WALLET_SECRET)
        const connectedWallet = wallet.connect(provider)
    
        const swapRouterContract = new ethers.Contract(
            swapRouterAddress,
            SwapRouterABI,
            provider
        )
    
        const inputAmount = WETH_amount
        // .001 => 1 000 000 000 000 000
        const amountIn = ethers.utils.parseUnits(
            inputAmount.toString(),
            decimals0
        )
    
        const params = {
            tokenIn: immutables.token1,
            tokenOut: immutables.token0,
            fee: immutables.fee,
            recipient: WALLET,
            deadline: Math.floor(Date.now() / 1000) + (60 * 5),
            amountIn: amountIn,
            amountOutMinimum: 0,
            sqrtPriceLimitX96: 0,
        }
    
        const transaction = swapRouterContract.connect(connectedWallet).exactInputSingle(
            params,
            {
            gasLimit: ethers.utils.hexlify(1000000)
            }
        ).then(transaction => {
            console.log(transaction)
        })
    }
}

// MAIN

buy(WETH_amount_=0.001)

【问题讨论】:

    标签: javascript token swap uniswap


    【解决方案1】:

    请注意,WETH 的代币地址在主网上是不同的——您的代码在 Goerli 上有 WETH 的地址。一些令牌地址见https://docs.uniswap.org/protocol/reference/deployments

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2023-03-11
      • 1970-01-01
      • 2022-10-26
      • 1970-01-01
      • 2021-05-30
      • 1970-01-01
      • 2022-07-29
      • 2020-02-19
      相关资源
      最近更新 更多