简体   繁体   English

ethers.js,uniswapV3 上的交换失败 tx

[英]ethers.js, Swap on uniswapV3 failed tx

Im trying to use exactInput() function for UniV3 interface but when trying to execute the code the transactions fails https://goerli.etherscan.io/tx/0xb0d5e4b491610b9db8d98cc938008ba2a4e1a06e67b05ed87ac6c0ca3ad61dab我正在尝试将exactInput() function 用于 UniV3 接口,但是当尝试执行代码时,交易失败https://goerli.etherscan.io/tx/0xb0d5e4b491610b9db8d98cc938008ba2a4e1a06e67b05ed87ac6c0ca3ad61dab
I know eth send shows 0 in this one but even especifying amount it fails, I dont know what to change.. I have checked many codes out there and cant see the mistake, please could someone give me some advice?我知道 eth send 在这个中显示 0 但即使指定数量也失败了,我不知道要更改什么。我已经检查了很多代码但看不出错误,请有人给我一些建议吗?

const {abi: V3SwapRouterABI} = require('@uniswap/v3-periphery/artifacts/contracts/interfaces/ISwapRouter.sol/ISwapRouter.json')
const { ethers } = require("ethers")
require("dotenv").config()

const INFURA_URL_TESTNET = process.env.INFURA_URL_TESTNET
const PRIVATE_KEY = process.env.PRIVATE_KEY
const WALLET_ADDRESS = process.env.WALLET_ADDRESS
// now you can call sendTransaction 


const wethToken= "0xB4FBF271143F4FBf7B91A5ded31805e42b2208d6"
const Uni= "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984"
const UniswapRouter="0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45"
const UniV3Contract = new ethers.Contract(
    UniswapRouter,
    V3SwapRouterABI
)

const provider = new ethers.providers.JsonRpcProvider(INFURA_URL_TESTNET)
const wallet = new ethers.Wallet(PRIVATE_KEY)
const signer = wallet.connect(provider)


const FEE_SIZE = 3

function encodePath(path, fees) {
    if (path.length != fees.length + 1) {
        throw new Error('path/fee lengths do not match')
    }

    let encoded = '0x'
    for (let i = 0; i < fees.length; i++) {
        // 20 byte encoding of the address
        encoded += path[i].slice(2)
        // 3 byte encoding of the fee
        encoded += fees[i].toString(16).padStart(2 * FEE_SIZE, '0')
    }
    // encode the final token
    encoded += path[path.length - 1].slice(2)

    return encoded.toLowerCase()
    }

async function getToken() {

    const path = encodePath([wethToken, Uni], [3000])
    const deadline = Math.floor(Date.now()/1000) + (60*10)  

    const params = {
        path: path,
        recipient: WALLET_ADDRESS,
        deadline: deadline,
        amountIn: ethers.utils.parseEther('0.01'),
        amountOutMinimum: 0
    }

    const encodedData = UniV3Contract.interface.encodeFunctionData("exactInput", [params])

    const txArg = {
        to: UniswapRouter,
        from: WALLET_ADDRESS,
        data: encodedData,
        gasLimit: ethers.utils.hexlify(1000000)
    }

    const tx = await signer.sendTransaction(txArg)
    console.log('tx: ', tx)
    const receipt = tx.wait()
    console.log('receipt: ', receipt)

}

module.exports = { getToken 

You will need to remove the Deadline.. The new router 0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45 moved deadline to the multi-call function (since the router is designed to be multi-call)您需要删除截止日期。新路由器0x68b3465833fb72A70ecDF485E0e4C7bD8665Fc45将截止日期移至多呼叫 function(因为路由器设计为多呼叫)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM