code-423n4 / 2022-01-insure-findings

2 stars 0 forks source link

Gas: Cache `totalSupply()` in `IndexTemplate:rate()` #305

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

Dravee

Vulnerability details

Impact

SLOADs are expensive

Proof of Concept

Here, totalSupply() is loaded twice from storage:

512:     function rate() external view returns (uint256) {
513:         if (totalSupply() > 0) { 
514:             return (totalLiquidity() * MAGIC_SCALE_1E6) / totalSupply();
515:         } else {
516:             return 0;
517:         }
518:     }

Tools Used

VS Code

Recommended Mitigation Steps

Cache totalSupply() in a variable

0xean commented 2 years ago

dupe of #301

0xean commented 2 years ago

warden is asked to submit the same optimization(s) in the same ticket.