'Can someone help me figure out how to minimize the storage size of these function?

I have created a contract with maximum bytes storage, I trying to minimize some function inside the contract, because I'm worry that if the contract starting interact will over block gas limit and stop the contract working on chain. I will deploy this contract on Binance Smart Chain.

Here is the function I think cause me huge storage size:

  1. function payQueue()

    function payQueue() private {
            for (uint index = nextPayIndex; index < deposits.length - 1; index++) {
                Deposit storage deposit = deposits[index];
                uint balance = token.balanceOf(address(this));
                User storage user = users[userids[deposit.account]];
        
                
    
    uint half = available / 2;
                uint needPay = deposit.amount * payMultiplier - deposit.allocated;
                if (needPay == 0) continue;
                if (half >= needPay) {
                    if (balance < needPay) return;
                    available -= needPay;
                    deposit.allocated += needPay;
                    deposit.paid = true;
                    user.disableDeposit = false;
                    user.totalAllocated += needPay;
                    totalAllocated += needPay;
                    emit UserMsg(userids[deposit.account], "Dividend", needPay);
                    nextPayIndex = index + 1;
                } else {
                    if (balance < half) return;
                    available -= half;
                    deposit.allocated = deposit.allocated + half;
                    user.totalAllocated += half;
                    totalAllocated += half;
                    emit UserMsg(userids[deposit.account], "Dividend", half);
                }
                break;
            }
            uint share = userShare();
            if (share == 0) return;
            for (uint index = nextPayIndex; index < deposits.length - 1; index++) {
                Deposit storage deposit = deposits[index];
                uint needPay = deposit.amount * payMultiplier - deposit.allocated;
                uint balance = token.balanceOf(address(this));
                if (needPay == 0) continue;
                uint topay = share * needPay / 1e18;
                User storage user = users[userids[deposit.account]];
                if (topay >= needPay) {
                    if (balance < needPay) return;
                    if (available < needPay) return;
                    token.safeTransfer(deposit.account, needPay);
                    available -= needPay;
                    deposit.allocated = deposit.allocated + needPay;
                    deposit.paid = true;
                    user.disableDeposit = false;
                    user.totalAllocated += needPay;
                    totalAllocated += needPay;
                    emit UserMsg(userids[deposit.account], "Dividend", needPay);
                    nextPayIndex = index + 1;
                } else {
                    if (balance < topay) return;
                    if (available < topay) return;
                    deposit.allocated = deposit.allocated + topay;
                    available -= topay;
                    user.totalAllocated += topay;
                    totalAllocated += topay;
                    emit UserMsg(userids[deposit.account], "Dividend", topay);
                }
            }
        }
    
    function userShare() private view returns (uint share) {
        if (deposits.length <= 1) return 0;
    
    uint totalRequired = (totalDeposit - deposits[deposits.length - 1].amount) * 2;
    if (totalRequired <= totalAllocated) return 0;
    uint needs = totalRequired - totalAllocated;
    if (needs == 0) return 0;
    share = available * 1e18 / needs;
    
    }
    
  2. function processDeposit()

    function processDeposit(address referer, uint units) private returns (uint value) {
        uint userid = userids[msg.sender];
        User storage user = users[userid];
        if (userid == 0) {
            totalUsers += 1;
            userid = totalUsers;
            userids[msg.sender] = userid;
            user.checkpoint = block.timestamp;
            emit UserMsg(userid, "Joined", 0);
        }
        if (user.account == address(0)) {
            user.account = msg.sender;
        }
        require(user.disableDeposit != true, "Pending Withdraws");
        user.disableDeposit = true;
        if (user.referer == address(0)) {
            if (users[userids[referer]].deposits.length > 0 && referer != msg.sender) {
                user.referer = referer;
                processLevelUpdate(referer, msg.sender);
            }
        }
        require(units > 0);
        require(units <= maxUnits, "Over than Max Units");
        value = units * price;
        token.safeTransferFrom(msg.sender, address(this), value);
        totalDeposit += value;
    
        Deposit memory deposit;
        deposit.amount = value;
        deposit.account = msg.sender;
        deposit.checkpoint = block.timestamp;
    
        emit UserMsg(userids[msg.sender], "Deposit", value);
        user.deposits.push(deposits.length);
        deposits.push(deposit);
        user.totalDeposit += value;
        available += value;
    
        uint companyOut = value * companyFee / 1000;
        token.safeTransfer(companyWallet, companyOut);
        uint treasuryOut = value * treasuryRate / 1000;
        token.safeTransfer(treasuryWallet, treasuryOut);
        uint poolOut = value * poolRate / 1000;
        token.safeTransfer(poolWallet, poolOut);
        uint techOut = value * techFee / 1000;
        token.safeTransfer(techWallet, techOut);
        uint commi = companyOut + treasuryOut + poolOut + techOut;
        emit Commission(commi);
        available -= commi;
        return commi;
    }
    

Million thanks for helping out! Much appreciated!



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source