问题描述
理想情况下,我需要 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 = 交换
控制台:“错误:交易已被 EVM 还原”
提前致谢
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)