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

Rinkeby Uniswap swapExactETHForTokens - Fail with error 'UniswapV2Router: EXPIRED'

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

请查看代码和详细信息

将 1 个以太币换成 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 上的交易结果:

控制台:“错误:事务已被 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。