Node.js Web3/NodeJ不使用Pancakeswapv2上的SwapexactTokensForth

Node.js Web3/NodeJ不使用Pancakeswapv2上的SwapexactTokensForth,node.js,web3js,binance-smart-chain,Node.js,Web3js,Binance Smart Chain,我在BSC上为BNB卖一只象征性的兔子真的有问题。 以下是我所知道的有效方法 检查津贴表明我已批准在PCSv2路由器上交易兔子 增加津贴也是有效的 花费BNB购买BUNNY works(swapExactETHforTokens) 在BSC扫描上手动写入合同也有效 不起作用的是swapExactTokensForETH——它消耗了一些汽油,但吐出了: “从传输失败”。。。原因:“事务失败”,代码:“调用\u异常” 下面是node.js脚本中失败的事务 现在我所做的所有研究都表明,这是因为我需要

我在BSC上为BNB卖一只象征性的兔子真的有问题。 以下是我所知道的有效方法

  • 检查津贴表明我已批准在PCSv2路由器上交易兔子
  • 增加津贴也是有效的
  • 花费BNB购买BUNNY works(swapExactETHforTokens)
  • 在BSC扫描上手动写入合同也有效
  • 不起作用的是swapExactTokensForETH——它消耗了一些汽油,但吐出了: “从传输失败”。。。原因:“事务失败”,代码:“调用\u异常” 下面是node.js脚本中失败的事务

    现在我所做的所有研究都表明,这是因为我需要批准花钱的人——我已经这样做了。。。我怎么知道?我写的函数说它和直接查询BUNNY一样好 另一件奇怪的事情是,它工作得非常完美(没有额外的批准)-参见屏幕截图

    下面是路由器合同中BSCscan写入部分的成功事务

    如果有人能弄清楚为什么会抛出这个错误,那将是一个巨大的帮助 代码如下:

    const ethers = require('ethers');
    const Web3 = require('web3');
    const abi = require('human-standard-token-abi');
    const {ChainId, Token, TokenAmount, Fetcher: v2Fetcher, Pair, Route, Trade, TradeType, Percent} = require('@pancakeswap-libs/sdk-v2');
    
    const {JsonRpcProvider} = require("@ethersproject/providers");
    const url = 'https://bsc-dataseed1.binance.org/';
    const provider = new JsonRpcProvider('https://bsc-dataseed1.binance.org/');
    const web3 = new Web3(url);
    
    const secretKey = process.env.SECRETKEY;
    const walletAddress = process.env.WALLETADDRESS;
    const wallet = ethers.Wallet.fromMnemonic(secretKey);
    const account = wallet.connect(provider);
    
    const pcsRouterV2 = Web3.utils.toChecksumAddress('0x10ED43C718714eb63d5aA57B78B54704E256024E'); //v2 router
    const routerV2 = new ethers.Contract (pcsRouterV2, [
        'function getAmountsOut(uint amountIn, address[] memory path) public view returns (uint[] memory amounts)',
        'function swapExactETHForTokens(uint amountOutMin, address[] calldata path, address to, uint deadline) external payable returns (uint[] memory amounts)',
        'function swapExactTokensForETH(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)',
        'function swapExactTokensForTokens(uint amountIn, uint amountOutMin, address[] calldata path, address to, uint deadline) external returns (uint[] memory amounts)'
        ], account );
    
    const bunnyAddress = Web3.utils.toChecksumAddress('0xc9849e6fdb743d08faee3e34dd2d1bc69ea11a51');
    const wbnbAddress = Web3.utils.toChecksumAddress('0xbb4CdB9CBd36B01bD1cBaEBF2De08d9173bc095c'); // WBNB
    
    const gasApprovalLimit = 100000;
    const gasTradingLimit = 250000;
    const gasPrice = 5;
    
    const getAllowance = async (tickerTokenAddress, thisWalletAddress, liquidtyPoolRouter) => {
        var contract = new web3.eth.Contract(abi, tickerTokenAddress);
        let approvalLimit = await contract.methods.allowance(thisWalletAddress, liquidtyPoolRouter).call();
        let decimals = await contract.methods.decimals().call();
        // this comes back with 3 variables, an approval, an approval in token units and the decimals
        return [approvalLimit, approvalLimit / (10 ** decimals), decimals];
    }
    
    const getApproval = async (thisTokenAddress, approvalAmount, thisDecimals, walletAccount, liquidtyPoolRouter, thisGasPrice = gasPrice, thisGasLimit = gasApprovalLimit)  => {
        console.log(`getting approval`);
        let contract = new ethers.Contract(thisTokenAddress, abi, walletAccount);
        let approveResponse = await contract.approve(
            liquidtyPoolRouter, 
            ethers.utils.parseUnits(approvalAmount.toString(), thisDecimals),
            {
                gasLimit: thisGasLimit, 
                gasPrice: ethers.utils.parseUnits(thisGasPrice.toString(), 'gwei')
            });
        console.log(approveResponse);
    }
    
    const swapExactBNBForTokens = async (buyAddress, buyDecimals, tokensIn, tradeSlippage, thisGasPrice = gasPrice, thisGasLimit = gasTradingLimit) => {
        let amountIn = ethers.utils.parseUnits(tokensIn.toString(), buyDecimals);
        let amounts = await routerV2.getAmountsOut(amountIn, [wbnbAddress, buyAddress]);
        let amountOutMin = amounts[1].sub(amounts[1].mul(tradeSlippage).div(100));
        let tx = await routerV2.swapExactETHForTokens(
            amountOutMin,
            [wbnbAddress, buyAddress],
            walletAddress,
            Date.now() + 1000 * 60 * 10,
            {
                value: amountIn,
                gasLimit: thisGasLimit, 
                gasPrice: ethers.utils.parseUnits(thisGasPrice.toString(), 'gwei')
            }
        )
        console.log(`Transaction Submitted...`);
        let receipt = await tx.wait();
        console.log(receipt);
    }
    
    const swapExactTokensForBNB = async (sellAddress, sellDecimals, tokensIn, tradeSlippage, thisGasPrice = gasPrice, thisGasLimit = gasTradingLimit) => {
        let amountIn = ethers.utils.parseUnits(tokensIn.toString(), sellDecimals);
        let amounts = await routerV2.getAmountsOut(amountIn, [sellAddress, wbnbAddress]);
        let amountOutMin = amounts[1].sub(amounts[1].mul(tradeSlippage).div(100));
    
        let tx = await routerV2.swapExactTokensForETH(
            amountIn, 
            amountOutMin,
            [sellAddress, wbnbAddress],
            walletAddress,
            Date.now() + 1000 * 60 * 10,
            {
                gasLimit: thisGasLimit, 
                gasPrice: ethers.utils.parseUnits(thisGasPrice.toString(), 'gwei')
            }
        )
        console.log(`Transaction Submitted...`);
        let receipt = await tx.wait();
        console.log(receipt);
    }
    
    // USAGE
    // swapExactBNBForTokens(bunnyAddress, 18, 0.3, 2); // spending 0.3 BNB to buy BUNNY
    // swapExactTokensForBNB(bunnyAddress, 18, 3, 2); // spending 3 BUNNY to buy BNB
    getAllowance(bunnyAddress, walletAddress, pcsRouterV2).then((value) => {console.log('V2 ' + value);});
    
    这是您在tx:3299718614161746000中提供的金额

    这是您在successfultx:3299718614161745787中提供的金额,它的数量更少


    因此,我认为您提供的金额不正确,超过了您的余额。

    这太令人尴尬了,非常感谢