Rinkeby Uniswap swapExactETHForTokens - 失败并出现错误“UniswapV2Router: EXPIRED”

问题描述

理想情况下,我需要 web3 或 ethers 中正确交易格式的示例, 它可以使用 Rinkeby 上的 UniswapV2Router 将 WETH 交换为 ERC20 或将 ERC20 交换为 WETH, 我想,我有错误的交易格式,也许是因为 gasPrice 或 gasLimit,但我不明白它发生在哪里,所以我尝试增加 gasPrice(100 Gwei) 和 gasLimit(8,000,000) 但它仍然失败,我还将“amountOutMin”减少到 1, 交易截止时间是 20 分钟,但它在几秒钟内失败

请看代码和详细信息

用 1 个 Ether 交换 UNI(WETH 和 ETH 余额在发件人地址上超过 5) 交易截止时间:20分钟 UNI地址:0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984 UniswapV2Router:0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D 另一个小问题,当你用 ETH 交换 ERC20 时,是从发送者余额中取出 WETH 还是 ETH?

const swap  = async () => {
try{
    const chainId = ChainId.RINKEBY 

    const tokenAddress = "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984" 
    const uni = await Fetcher.fetchTokenData(chainId,tokenAddress)
    const weth = WETH[chainId]
    const pair = await Fetcher.fetchPairData(uni,weth) 
    const route = new Route([pair],weth)  
    const Trade = new Trade(route,new TokenAmount(weth,'100000000000000000'),TradeType.EXACT_INPUT) 

    console.log('1 WETH for',route.midPrice.toSignificant(6),' UNI')
    console.log('1 UNI for',route.midPrice.invert().toSignificant(6),' WETH')
    console.log('Trade price 1 WETH for ',Trade.executionPrice.toSignificant(6),' UNI') 

    const accounts =  await web3.eth.getAccounts()
    const account = accounts[0] 
    const slippagetolerance = new Percent('20','100')
    const path = [weth.address,uni.address ]
    const to = account 
    
    // function toHex(currencyAmount) {
    //     return `0x${currencyAmount.raw.toString(16)}`
    // } 
    // const amountOutMin = toHex(Trade.minimumAmountOut(slippagetolerance))
    // const value = toHex(Trade.inputAmount)

  
    const uniswap = await new web3.eth.Contract(abi,"0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D") 
    const Now = moment().unix()  
    const DEADLINE = Now + 60 *20   

    console.log('Sending...') 
    let txn = await uniswap.methods.swapExactETHForTokens(  1,path,to,DEADLINE   ).send( { 
        from: account,gasLimit: 8000000,gasPrice: web3.utils.toWei('100','Gwei'),value: web3.utils.toWei('1','Ether')  
    })
    console.log(`Txn: https://rinkeby.etherscan.io/tx/${txn.transactionHash}`) 

}catch(e){
    console.log(e)
}

}

module.exports = 交换

rinkeby etherscan 上的交易结果:

enter image description here

控制台:“错误:交易已被 EVM 还原”

提前致谢

解决方法

这是一个将 ETH 换成 UNI 的例子。我正在使用 ethJS。

const WETH_ADDRESS = "0xc778417e063141139fce010982780140aa0cd5ab";
const UNI_ADDRESS = "0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984";
const path = [WETH_ADDRESS,UNI_ADDRESS];
const ethAmount = ethers.utils.parseEther("0.1");

const nowInSeconds = Math.floor(Date.now() / 1000)
const expiryDate = nowInSeconds + 900;

const txn = await uniswapV2Contract.swapExactETHForTokens(
    0,path,user.address,expiryDate,{
        gasLimit: 1000000,gasPrice: ethers.utils.parseUnits("10","gwei"),value: ethAmount
    }
)
const res = await txn.wait();
    

当您调用 swapExactETHForTokens 方法时,它将使用 ETH 而不是 WETH。如果您想与 WETH 交换,您应该调用 swapExactTokensForTokens。