Function revertes only every few days

Hi there,

I have a question about a function in my contract. It's a function to redeem rewards. The user specify as input how much percent he wants as BNB and how much as Token.
The function works like expected but sometimes (for now its around 45 out of 2100 calls) the functions fails with "execution reverted" and I don't know why.

Here's the code:

// Claims rewards in BNB and or tokens based on user's choice, accounting for reward pool cap
    function redeemRewards(uint8 perc) external nonReentrant {
        if (perc > 100) revert ValueOutOfRange();

        uint256 userBalance = super.balanceOf(msg.sender);

        if (nextClaimDate[msg.sender] > block.timestamp)
            revert ClaimPeriodNotReached();
        if (userBalance == 0) revert NoHODLInWallet();

        uint256 currentBNBPool = address(this).balance;
        uint256 reward = currentBNBPool > bnbRewardPoolCap
            ? (bnbRewardPoolCap * userBalance) / rewardPoolShare
            : (currentBNBPool * userBalance) / rewardPoolShare;

        uint256 rewardReinvest = 0;
        uint256 rewardBNB = 0;
        uint256 nextClaim = block.timestamp + rewardClaimPeriod;

        unchecked {
            if (perc == 100) {
                rewardBNB = reward;
            } else if (perc == 0) {
                rewardReinvest = reward;
                nextClaim -= reinvestBonusCycle;
            } else {
                rewardBNB = (reward * perc) / 100;
                rewardReinvest = reward - rewardBNB;
            }
        }

        if (perc < 100) {

            address[] memory path = new address[](2);
            path[0] = PANCAKE_ROUTER.WETH();
            path[1] = address(this);

            PANCAKE_ROUTER.swapExactETHForTokens{value: rewardReinvest}(
                0,
                path,
                REINVEST_ADDRESS,
                block.timestamp + 360
            );
            uint256 transferredAmount = super.balanceOf(REINVEST_ADDRESS);
            userReinvested[msg.sender] += transferredAmount;
            totalHODLFromReinvests += transferredAmount;

            super._update(REINVEST_ADDRESS, msg.sender, transferredAmount);
        }

        if (rewardBNB > 0) {
            (bool success, ) = address(msg.sender).call{value: rewardBNB}("");
            if (!success) revert BNBTransferFailed();
            userBNBClaimed[msg.sender] += rewardBNB;
            totalBNBClaimed += rewardBNB;
        }
        nextClaimDate[msg.sender] = nextClaim;
    }

This is the token: https://bscscan.com/address/0x32b407ee915432be6d3f168bc1eff2a6f8b2034c

I checked not all failed transactions but it looks like it only fails on reinvests (input < 100), so I think it has something to do with the token swap.

I would be happy if someone has an idea why this happens

Edit:
You can find failed transactions here:
https://bscscan.com/txs?a=0x32b407ee915432be6d3f168bc1eff2a6f8b2034c&f=1

Kind regards
Julek

Hi, welcome to the community! :wave:

I do not find a failed transaction, so how to reproduce this error?

Hi,

you can filter by failed transactions. When you are on bscscan at transactions, you can click on the right side "View Failed Txns"

Why is this post flagged as spam?

I edited the post and inserted a link to the failed transactions.

I simulate the failed transaction with a higher gas, it works, so maybe you can increase the gas and then have a try.

1 Like

I use estimateGas and add 10k. So never thought that should be a problem.
Increased the limit now and will monitor it.

Thanks for taking a look into it.

Edit: I compared successfull transactions and they use only below 200k gas. The failed ones have a limit of around 250k. Could this really be the issue here?

Could u find a solution?
Are u Lp and Token owner?

There were no more errors after increasing the gas. I actually don't understand it, because the limit was around 250k and the normal usage is around 210k. But solved is solved.

Thanks for the help!