Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Smart Contract Gas Fees Are Too High

My smart contract isn't scalable. The way it calculates each depositors reward causes huge gas fees. How can I fix this and make it efficient so potentially millions of users can use the smart contract

Here's the code:

/**
* Reward is based on the amount held, relative to total supply of tokens.
*/
function calculateReward(uint256 _amount, address _sender) internal view returns (uint256) {
uint256 reward = 0;
for (uint256 i= (depositor[_sender].time.sub(startTime)).div(timeWait);
i++){
uint count = penalty[i].totalPenaltiesInThisIndex;
while(count != 0){
if(penalty[i][count].time >= depositor[_sender].time && penalty[i][count].time <= now){
if (feePot > 0) {
reward += ((penalty[i][count].amount).mul(_amount)).div(penalty[i][count].deservers); // assuming that if feePot > 0 then also totalSupply > 0
}
}
count = count.sub(1);
}

if(count == 0){
if(penalty[i][count].time >= depositor[_sender].time && penalty[i][count].time <= now){
if (feePot > 0) {
reward += ((penalty[i][count].amount).mul(_amount)).div(penalty[i][count].deservers); // assuming that if feePot > 0 then also totalSupply > 0
}
}
}
}

// if (feePot > 0) {
// reward = (feePot.mul(_amount)).div((_totalSupply.sub(balances[owner]))); // assuming that if feePot > 0 then also totalSupply > 0
// }
return reward;
}