Smart Contract Gas Fees Are Too High

Dave90210Dave90210 Member Posts: 7 ✭✭
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<= ((depositor[_sender].time.sub(startTime)).div(timeWait)).add(now.sub(depositor[_sender].time)).div(timeWait);
i++){
uint count = penalty[i][0].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;
}

Sign In or Register to comment.