Why does my solidity function jump to other unrelated functions in the contract?

37 Views Asked by At

I am developing a smart contract and when I run the applyReferralCode function with any address other than ownerAddress it fails. I debug in Remix and discover that during the function execution, it jumps to an unrelated function. The unrelated function will have a modifier such as onlyOwner or authorized. Because of the modifier on the unrelated function, the applyReferralCode function fails with the error !NOTAUTHORIZED. I do not know why it is even referencing the unrelated function. Am I using the Remix de-bugger wrong and misunderstanding the output?

function applyReferralCode(string memory _code) external {
        require(isRewardEnabled, "Referral rewards not enabled");
        require(!referralCodeApplied[msg.sender], "User already applied a referral code");
        require(refAddressWhitelisted[msg.sender], "Address not whitelisted");
        require(balanceOf(address(this)) >= (rewardReserveRatio * rewardAmount), "Not enough tokens in the contract, try again later");
        require(balanceOf(msg.sender) >= minTokens, "Must hold minimum tokens to apply referral code");

        // Find the creator address associated with the referral code
        address creatorAddress = address(0);  // Initialize to zero address
        for (uint i = 0; i < referralCodes.length; i++) {
            if (keccak256(abi.encodePacked(referralCodes[i].code)) == keccak256(abi.encodePacked(_code))) {
                creatorAddress = referralCodes[i].creatorAddress;
            break;
            }
        }
    
        require(creatorAddress != address(0), "Referral code not found");  // Ensure the referral code exists
    
        referrals.push(Referral({
            code: _code,
            referredAddress: msg.sender
        }));
        referralCodeApplied[msg.sender] = true;
    
        // Send reward to whitelisted address and creator attached to referral code
        _transfer(address(this), msg.sender, rewardAmount);  
        _transfer(address(this), creatorAddress, creatorAmount);

        removeRefWhitelistWallet(msg.sender);

        emit ReferralCodeApplied(string(_code));
    }

I have tried deploying with different compilier versions from 0.8.0 to 0.8.23 I have tried commenting out the unrelated function that it jumps to and it just finds another unrelated function to go to which causes the revert. I have tried running the function with the owner address and it is successful because the owner address meets the requirements of whatever unrelated function it jumps to. I have ensured that all of the require statements in the applyReferralCode function are met.

0

There are 0 best solutions below