-1

When there are fees, normally contracts send tokens or BNB to wallets (i.e. marketing wallet) and auto add liquidity (swapandliquify) in BNB. I am attempting to replace BNB for both with BUSD. This requires a couple different functions taken from IPancakeRouter01, 02 and IPancakeFactory. Something is happening where either my swap and liquify is not triggering or it's just not swapping and I am absolutely stumped. Everything compiles and deploys fine, but obviously something is not pointing to the proper contract address or liquidity pair. My _transfer function is all good, I am sure of it. I am going to post the relevant parts of my code related to this issue.

//BUSD Contract Address
    address constant public BUSD = 0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56;

    IPancakeRouter02 _pancakeRouter = IPancakeRouter02(_pancakeRouterAddress);
    // Create a pancake pair for this new token
    pancakePair = IPancakeFactory(_pancakeRouter.factory()).createPair(address(this), BUSD);
    // set the rest of the contract variables
    pancakeRouter = _pancakeRouter;

function swapAndLiquify(uint256 contractTokenBalance) private lockTheSwap {
        
        uint256 totalFees = _buyLiquidityFee + _sellLiquidityFee + _buyMarketingFee + _sellMarketingFee + _buyWhaleBuybackFee + _sellWhaleBuybackFee;

        uint256 _totalMarketingFee = _buyMarketingFee + _sellMarketingFee;
        uint256 marketingPercent = _totalMarketingFee.div(totalFees);
        uint256 marketingQuota = marketingPercent.mul(contractTokenBalance);

        uint256 _totalWhaleBuybackFee = _buyWhaleBuybackFee + _sellWhaleBuybackFee;
        uint256 whaleBuybackPercent = _totalWhaleBuybackFee.div(totalFees);
        uint256 whaleBuybackQuota = whaleBuybackPercent.mul(contractTokenBalance);        
        
        // capture the contract's current ETH balance.
        // this is so that we can capture exactly the amount of ETH that the
        // swap creates, and not make the liquidity event include any ETH that
        // has been manually sent to the contract
        uint256 initialBalance = address(this).balance;     
        swapTokensForBNB(marketingQuota); 
        swapBNBForBUSD(address(this).balance);
        transferOutBUSD(marketingWallet, address(this).balance.sub(initialBalance));
        
        //transferOutBNB(marketingWallet, address(this).balance.sub(initialBalance));

        uint256 initialBalance2 = address(this).balance;     
        swapTokensForBNB(whaleBuybackQuota); 
        transferOutBUSD(whaleBuybackWallet, address(this).balance.sub(initialBalance2));
        
        //transferOutBNB(whaleBuybackWallet, address(this).balance.sub(initialBalance2));


        // split the contract balance into halves
        uint256 initialBalanceAfterUtility = address(this).balance;
        uint256 half = initialBalanceAfterUtility.div(2);
        uint256 otherHalf = initialBalanceAfterUtility.sub(half);

        swapTokensForBNB(half);
        swapBNBForBUSD(address(this).balance); 
        uint256 newBalance = address(this).balance.sub(initialBalanceAfterUtility);
        addLiquidity(otherHalf, newBalance);

        emit SwapAndLiquify(half, newBalance, otherHalf);
    }

    function swapTokensForBNB(uint256 tokenAmount) private {
        // generate the pancake pair path of token -> wbnb
        address[] memory path = new address[](2);
        path[0] = address(this);
        path[1] = pancakeRouter.WETH();

        _approve(address(this), address(pancakeRouter), tokenAmount);

        // make the swap
        pancakeRouter.swapExactTokensForETHSupportingFeeOnTransferTokens(
            tokenAmount,
            0, // accept any amount of BNB
            path,
            address(this),
            block.timestamp
        );
    }

    function transferOutBNB(address payable recipient, uint256 amount) private {
        recipient.transfer(amount);
    }

    function swapBNBForBUSD(uint256 tokenAmount) private{

        _approve(address(this), address(pancakeRouter), tokenAmount);

        address[] memory path = new address[](2);
        path[0] = pancakeRouter.WETH();
        path[1] = BUSD;  //pancakeRouter.BUSD();

        pancakeRouter.swapExactETHForTokensSupportingFeeOnTransferTokens(
            tokenAmount, 
            path, 
            address(this), 
            block.timestamp
            );
    }

    function transferOutBUSD(address payable recipient, uint256 amount) private{
        recipient.transfer(amount);
    }

    function addLiquidity(uint256 tokenAmount, uint256 bnbAmount) private {
        // approve token transfer to cover all possible scenarios
        _approve(address(this), address(pancakeRouter), tokenAmount);

        // add the liquidity
        pancakeRouter.addLiquidity( // the return values of function not will are handled
            address(this),
            BUSD,
            tokenAmount,
            bnbAmount,
            0, 
            0, 
            owner(), 
            block.timestamp
        );
    }

1 Answers1

-1

Instead of this,

//BUSD Contract Address
    address constant public BUSD = 0xe9e7CEA3DedcA5984780Bafc599bD69ADd087D56;

Try this,

IERC20 public immutable BUSD =
    IERC20(0x78867BbEeF44f2326bF8DDd1941a4439382EF2A7);

a) For constant variables, the value has to be fixed at compile-time

b) For immutable, the value can be assigned at construction time.

  • 1
    A good answer will always include an explanation why this would solve the issue, so that the OP and any future readers can learn from it. – Tyler2P Jan 31 '22 at 17:50