BSC链Pancake Swap出售带税代币报错(Pancake: K)求助
BSC链上出售带销售税代币失败问题排查
在BSC链上开发基于ethers.js和Node.js的加密机器人,通过Pancake Swap进行代币兑换时遇到问题:出售无销售税的代币(如PEPE)一切正常,但出售带销售税的代币(如Yoshi)时交易无法完成,报错信息为:execution reverted: Pancake: K。
相关逻辑代码
//Sell Tokens or Convert to BNB exports.processSell = async (ctx, tokenAddress, percentage, provider) => { try { const chat = ctx.chat.id; await connectToDb(); const wallet = await walletSchema.findOne({ chat }); // Validate amount percentage const parsedAmountPercentage = parseInt(percentage); if (isNaN(parsedAmountPercentage) || parsedAmountPercentage < 10 || parsedAmountPercentage > 100) { ctx.reply("Invalid amount percentage. Please enter a number between 10 and 100.", Markup.inlineKeyboard([ [{ text: "Enter Again", callback_data: `/enter_percentage ${tokenAddress}` }, { text: "Cancel Buy", callback_data: '/close' } ] ]).resize()); return; } // Convert amount percentage to actual amount based on token balance const walletResponse = await Moralis.EvmApi.wallets.getWalletTokenBalancesPrice({ address: wallet.walletId, chain: EvmChain.BSC }); const walletData = walletResponse.toJSON().result; const token = walletData.find(token => token.token_address.toLowerCase() === tokenAddress.toLowerCase()); const slippage = wallet.sellSlip || 5; if (!token) { ctx.reply("Token not found in your wallet."); return; } if (token.usd_value < 4.5) { ctx.reply('You should have $4.5 usd value of tokens to swap them.'); return; } const tokenBalance = token.balance_formatted; let amount = (parsedAmountPercentage / 105) * tokenBalance; console.log("Token amount", amount); let tokenAmtSlippage = amount * (1 - slippage / 100); tokenAmtSlippage = tokenAmtSlippage.toFixed(0); // Convert to string to append .01 or .99 tokenAmtSlippage = parseFloat(tokenAmtSlippage + (Math.random() > 0.5 ? '.99' : '.01')); console.log("Token slippage amount", tokenAmtSlippage); // Check if user has sufficient token balance to sell if (amount > tokenBalance) { ctx.reply("Insufficient token balance."); return; } // Proceed with swapping tokens back to BNB const privateKey = await wallet.decryptKey(); const signer = new ethers.Wallet(privateKey, provider); // Approve Router Contract to spend tokens const tokenContract = new ethers.Contract(tokenAddress, TOKEN_BSC_ABI, signer); const routerAddress = PANCAKE_ROUTER; const routerAbi = TOKEN_BSC_ABI; const router = new ethers.Contract(routerAddress, routerAbi, signer); const deadline = Math.floor(Date.now() / 1000) + 60 * 20; // 20 minutes from now const path = [tokenAddress, '0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c']; // Swap token to BNB const amountOutMin = 0; // Minimum amount of BNB to receive // Approve tokens for spending by router const amountWei = ethers.parseUnits(tokenAmtSlippage.toString(), 'ether'); // Approve tokens for spending by router const approvalTx = await tokenContract.approve(routerAddress, amountWei); await approvalTx.wait(); // Execute the swap const tx = await router.swapExactTokensForETH( amountWei, amountOutMin, path, signer.address, deadline, ); // Wait for transaction to be mined const receipt = await tx.wait(); ctx.reply(`Transaction Successfull! You swapped ${amount.toFixed(4)} tokens to BNB.`); } catch (error) { ctx.reply(`Transaction reverted due to gas fees, please select more amount of tokens or set slippage accordingly.`) console.log('Error', error) } }
Pancake Swap兑换BNB的ABI
const TOKEN_BSC_ABI = [ { "inputs": [], "name": "WETH", "outputs": [ { "internalType": "address", "name": "", "type": "address" } ], "stateMutability": "view", "type": "function" }, { "inputs": [ { "internalType": "uint256", "name": "amountOut", "type": "uint256" }, { "internalType": "uint256", "name": "amountInMax", "type": "uint256" }, { "internalType": "address[]", "name": "path", "type": "address[]" }, { "internalType": "address", "name": "to", "type": "address" }, { "internalType": "uint256", "name": "deadline", "type": "uint256" } ], "name": "swapExactTokensForETH", "outputs": [ { "internalType": "uint256[]", "name": "amounts", "type": "uint256[]" } ], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "spender", "type": "address" }, { "internalType": "uint256", "name": "amount", "type": "uint256" } ], "name": "approve", "outputs": [ { "internalType": "bool", "name": "", "type": "bool" } ], "stateMutability": "nonpayable", "type": "function" }, { "inputs": [ { "internalType": "address", "name": "owner", "type": "address" }, { "internalType": "address", "name": "spender", "type": "address" } ], "name": "allowance", "outputs": [ { "internalType": "uint256", "name": "", "type": "uint256" } ], "stateMutability": "view", "type": "function" } ]; // Connect to BSC network const PANCAKE_ROUTER = "0x10ED43C718714eb63d5aA57B78B54704E256024E";
已尝试的解决方法
- 将滑点设置为25%
- 查阅并遵循Pancake Swap官方文档
问题仍未解决,需要实现让用户出售带销售税代币的功能。
内容的提问来源于stack exchange,提问作者Devgan Tarun
相关产品推荐
相关产品推荐

