50 PancakeswapV3 兑换代币为BNB用到exactInputSingle总报错

是这样,我想把钱包里的代币用脚本换成BNB,然后用node.js写了个脚本。一开始用的pancakeV2的swapExactTokensForETHSupportingFeeOnTransferTokens函数,然后一直卖出失败。之后换了V3还是不行。实在不知道怎么回事了,求大佬帮忙。

代码如下:

// 夹子脚本卖出代币代码 Pancakeswap Router V3 卖出代币
const sellToken = async (account, tokenContract, gasLimit, gasPrice, value = 100) => {
  try {
    const sellTokenContract = new ethers.Contract(tokenContract, swapAbi, account);
    const contract = new ethers.Contract(Pancake_ROUTER3_ADDRESS, abiV3, account); // 确保 abi3 是 V3 路由器的 ABI
    const accountAddress = account.address;
    const tokenBalance = await erc20(account, tokenContract).balanceOf(accountAddress);

    console.log('执行到这里');
    const decimals = 18; // 这是代币的小数位数,对于ETH和许多ERC-20代币来说,这个值是18
    const amountOutMin = ethers.utils.parseUnits("0.0001", decimals);
    const amountIn = tokenBalance.mul(value).div(100);

    // 预授权
    const approve = await sellTokenContract.approve(Pancake_ROUTER3_ADDRESS, amountIn);
    const receipt_approve = await approve.wait();
    if (receipt_approve && receipt_approve.blockNumber && receipt_approve.status === 1) {
      console.log(`Approved ${explore}${receipt_approve.transactionHash}`);

      // 使用 V3 的 exactInputSingle 方法进行交易
      const params = {
        tokenIn: tokenContract,
        tokenOut: BSC_Token_CONTRACT, // 假设为 BNB 的地址
        fee: 3000, // 以 0.3% 的费率为例,具体值根据实际情况
        recipient: accountAddress,
        deadline: Math.floor(Date.now() / 1000) + 60 * 10,
        amountIn: amountIn,
        amountOutMinimum: amountOutMin,
        sqrtPriceLimitX96: 0 // 如果没有特定的价格限制,这个值可以是 0
      };
      try {
      const swap_txn = await contract.exactInputSingle(params, {
        'gasLimit': gasLimit,
        'gasPrice': gasPrice,
      });
      const receipt = await swap_txn.wait();
      if (receipt && receipt.blockNumber && receipt.status === 1) { // 0 - failed, 1 - success
        console.log(chalk.green(`Transaction ${explore}${receipt.transactionHash} mined, status success`));
      } else if (receipt && receipt.blockNumber && receipt.status === 0) {
        console.log(chalk.red(`Transaction ${explore}${receipt.transactionHash} mined, status failed`));
      } else {
        console.log(`Transaction ${explore}${receipt.transactionHash} not mined`);
      }
    } catch (error) {
      console.error(`Error during the swap transaction: ${error}`);
    }
    }
  } catch (error) {
    handleError(error, 'An error occurred while trying to sell tokens.');
  }
};

能完成授权了,但是到这一步总是过不去,网上也找到原因。v3的路由地址、ABI,以及params内的数据都对着,但是就是执行不了。求大佬解惑。 调用exactInputSingle函数执行失败的链接:https://bscscan.com/tx/0x19be489bf2f7a72172ad39fad57dfd3bed4ea83eb469777232272b31d85fcc6d

是不是我选错函数了,还是什么情况...求大佬帮帮忙

请先 登录 后评论

最佳答案 2024-05-10 12:20

就你提问的这个链接指向的币对(0x6385fae4C6510d0A70D41f4bfA3529faD850AEcA, 0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c,5000)来说,问题的原因是pancake上没有这个代币池子,那你肯定会交易失败。

v3 的池子, 都是(币A,币B,交易费率), 三者共同确认一个池子。 交易费率一般是500/3000/10000 这种; v2 的池子少了交易费率, 所以就是这样的。(币A, 币B)

给你查了一下, 你提问的这个代币和wbnb就没有v3 的池子,只有一个V2 的池子,链接在这里。https://bscscan.com/address/0xbd904ed9dcb39342b03148f7947163082498eb8b#tokentxns

请先 登录 后评论

其它 2 个回答

Wade - Footprint Analytics CTO
  擅长:数据分析,GameFi,NFT
请先 登录 后评论
用户_20014
请先 登录 后评论
  • 3 关注
  • 0 收藏,820 浏览
  • 提出于 2024-05-09 10:34