50470 sc insight inefficient design in distributeyieldwithlimit arctoken creates unnecessary gas consumption
Description
Brief / Intro
Vulnerability Details
Root Cause Analysis
function distributeYieldWithLimit(...) external {
// ... setup code ...
// ❌ FIRST LOOP: Calculate effectiveTotalSupply
uint256 effectiveTotalSupply = 0;
for (uint256 i = 0; i < totalHolders; i++) {
address holder = $.holders.at(i);
if (_isYieldAllowed(holder)) { // ❌ First call to _isYieldAllowed
effectiveTotalSupply += balanceOf(holder);
}
}
// ... transfer and setup logic ...
// ❌ SECOND LOOP: Distribute yield
for (uint256 i = 0; i < batchSize; i++) {
uint256 holderIndex = startIndex + i;
address holder = $.holders.at(holderIndex);
if (!_isYieldAllowed(holder)) { // ❌ Second call to _isYieldAllowed for same holder
continue;
}
uint256 holderBalance = balanceOf(holder);
if (holderBalance > 0) {
uint256 share = (totalAmount * holderBalance) / effectiveTotalSupply;
if (share > 0) {
yToken.safeTransfer(holder, share);
amountDistributed += share;
}
}
}
}The Gas Waste Problem
Impact Details
Gas Consumption Analysis
References
Recommended Mitigation
Proof of Concept
Previous49939 sc high initial timestamp mismatch might lead to users being able to spin twice in the same dayNext49932 sc insight there are five separate but similar implementations of a binary search that can be condensed into one function
Was this helpful?