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

2 stars 0 forks source link

Gas: Cache `totalSupply()` in `PoolTemplate.sol:rate()` #358

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:

File: PoolTemplate.sol
744:     function rate() external view returns (uint256) {
745:         if (totalSupply() > 0) {
746:             return (originalLiquidity() * MAGIC_SCALE_1E6) / totalSupply();
747:         } else {
748:             return 0;
749:         }
750:     }

Tools Used

VS Code

Recommended Mitigation Steps

Cache totalSupply() in a variable

0xean commented 2 years ago

dupe of #343 - warden is asked to group similar optimizations (cacheing) in the same findings.