blockchain - ethers.js swapExactETHForTokens 和 swapExactTokensForTokens 上的煎饼交换
问题描述
我在使用以下代码时遇到问题,我无法在 pancake swap 上购买。I get the following errors depending on which function i call swapExactETHForTokens
or swapExactTokensForTokens
:
https://bscscan.com/tx/0x18285588819662c93543dba5650d4471e62a504900b9a089f09dea4970698352
https://bscscan.com/tx/0x38ae19f6b677f072a82ac7c5528d445d3fc45288b1004f205d479edffee97b2f
这是我正在使用的代码:
const ethers = require('ethers')
require('dotenv').config({ path: __dirname + '/./../../.env' })
const config = {
wbnb: '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c',
safemoon: '0x8076c74c5e3f5852037f31ff0093eeb8c8add8d3',
pancakeSwapRouter: '0x10ed43c718714eb63d5aa57b78b54704e256024e',
slippage: 12,
}
const provider = new ethers.providers.WebSocketProvider(
'wss://bsc-ws-node.nariox.org:443'
)
const wallet = new ethers.Wallet.fromMnemonic(process.env.MNEMONIC)
const account = wallet.connect(provider)
const pancakeswap = new ethers.Contract(
config.pancakeSwapRouter,
[
'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
'function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)',
],
account
)
const wbnb = new ethers.Contract(
config.wbnb,
['function approve(address spender, uint amount) public returns(bool)'],
account
)
const buyToken = async () => {
try {
const deadline = Math.floor(Date.now() / 1000) + 60 * 20
const tokenIn = config.wbnb
const tokenOut = config.safemoon
const amountIn = ethers.utils.parseUnits('0.001', 'ether')
const amounts = await pancakeswap.getAmountsOut(amountIn, [
tokenIn,
tokenOut,
])
const amountOutMin = amounts[1].sub(amounts[1].div(`${config.slippage}`))
console.log(`
Buying new token
tokenIn: ${amountIn} ${tokenIn} (WBNB)
tokenOut: ${amountOutMin} ${tokenOut}
`)
// const tx = await pancakeswap.swapExactTokensForTokens(
// amountIn,
// amountOutMin,
// [tokenIn, tokenOut],
// account.address,
// deadline,
// {
// gasPrice: provider.getGasPrice(),
// gasLimit: 100000,
// }
// )
const tx = await pancakeswap.swapExactETHForTokens(
amountOutMin,
[tokenIn, tokenOut],
account.address,
deadline,
{
gasPrice: provider.getGasPrice(),
gasLimit: 100000,
}
)
const receipt = await tx.wait()
console.log('buyToken receipt')
console.log(receipt)
} catch (error) {
console.log(error)
}
}
const approve = async () => {
const valueToapprove = ethers.utils.parseUnits('0.01', 'ether')
const tx = await wbnb.approve(pancakeswap.address, valueToapprove, {
gasPrice: provider.getGasPrice(),
gasLimit: 100000,
})
console.log('Approving...')
const receipt = await tx.wait()
console.log('Approve receipt')
console.log(receipt)
}
const main = async () => {
await approve()
await buyToken()
process.exit()
}
main()
swapExactTokensForTokens
批准有效,但我无法弄清楚or出了什么问题swapExactETHForTokens
。需要打电话批准吗?似乎对任何一种方式都没有帮助。感谢您的输入。
解决方案
只需使用swapExactETHForTokens
并尝试value
在选项中添加一些。
const token = "Your token";
const amountIn = ethers.utils.parseUnits("0.0001", 'ether');
await approve(amountIn);
const amounts = await router.getAmountsOut(amountIn, [
addresses.WBNB,
token
])
const amountOutMin = amounts[1].sub(amounts[1].div(12))
const tx = await router.swapExactETHForTokens(
0,
[addresses.WBNB, token],
addresses.recipient,
Date.now() + 1000 * 60 * 10,
{
gasPrice: provider.getGasPrice(),
gasLimit: 310000,
value: amountIn
}
);
如果可行,请不要忘记标记为已接受的答案。谢谢 (-:
推荐阅读
- c# - 如何在 c# windows 窗体中使用 n 层架构用数据库值填充组合框
- jquery - 单击角度链接后添加/删除类
- git - RTC(理性团队音乐会)中的进出是什么意思?
- java - 我的应用程序跳过启动意图并执行下一行代码?
- typescript - 检测 Word 上的更改(API Office)
- powershell - 导入 .csv AzureADUser 脚本仅返回 100 个条目
- solidity - windows10无法安装web3 js
- internet-explorer - 剑道网格过滤器值
- python-3.x - Bigquery CSV 文件加载失败
- node.js - 在Nodejs上加密角度解密数据