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