简体   繁体   English

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

[英]Rinkeby Uniswap swapExactETHForTokens - Fail with error 'UniswapV2Router: EXPIRED'

Ideally, I need example of correct transaction format in web3 or ethers, Where it can swap WETH for ERC20 or ERC20 for WETH using UniswapV2Router on Rinkeby, I think, I'm having wrong transaction format, maybe it's because of gasPrice or gasLimit, but I don't understand where it happens, So I tried with the increased gasPrice(100 Gwei) and gasLimit(8,000,000) but it's still failing, I also decreased the "amountOutMin" to 1, Transaction deadline is 20 minutes but it's failiing in a few seconds理想情况下,我需要 web3 或 ethers 中正确交易格式的示例,它可以使用 Rinkeby 上的 UniswapV2Router 将 WETH 换成 ERC20 或 ERC20 换成 WETH,我想,我的交易格式错误,可能是因为 gasPrice 或 gasLimit,但是我不明白它发生在哪里,所以我尝试增加gasPrice(100 Gwei)和gasLimit(8,000,000)但它仍然失败,我还将“amountOutMin”减少到1,交易截止日期是20分钟但它失败了几秒钟

Please have a look at code and details请查看代码和详细信息

Swap 1 Ether for UNI (WETH and ETH balances are more than 5 on sender's address) transaction deadline: 20 minute UNI address: 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984 UniswapV2Router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D Another small question, when you swap ETH for ERC20 does it takes WETH or ETH from senders balance? Swap 1 Ether for UNI (WETH and ETH balances are more than 5 on sender's address) transaction deadline: 20 minute UNI address: 0x1f9840a85d5aF5bf1D1762F925BDADdC4201F984 UniswapV2Router: 0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D Another small question, when you swap ETH for ERC20 does it takes WETH or ETH from senders balance?

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 = swap module.exports = 交换

Transaction results on rinkeby etherscan: rinkeby etherscan 上的交易结果: 在此处输入图像描述

Console: "Error: Transaction has been reverted by the EVM "控制台:“错误:交易已被 EVM 还原”

Thanks in advance提前致谢

Here is an example of swapping ETH to UNI.这是将 ETH 交换为 UNI 的示例。 I am using ethJS.我正在使用 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();
    

When you call the method swapExactETHForTokens, it would be taking ETH and not WETH.当您调用方法 swapExactETHForTokens 时,它将使用 ETH 而不是 WETH。 If you would like to swap with WETH, you should call swapExactTokensForTokens.如果你想用 WETH 进行交换,你应该调用 swapExactTokensForTokens。

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

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