20,000 USDC
View results
Submission Details
Severity: gas
Valid

[G-02] - Multiple reads of `storage` variable which can be cached to save users gas cost.

Details

SLOAD (is the opcode used of read storage variable) cost 100 units of gas and MLOAD & MSTORE (are the opcodes used to read and write to memory respectivily) cost 3, 3 units of gas respectivily.

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Staking.sol#L65-L66

- if (_balance > balance) {
- uint256 _diff = _balance - balance;
+ uint256 balance_ = balance;
+ if (_balance > balance_) {
+ uint256 _diff = _balance - balance_;

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Staking.sol#L85-L86

https://github.com/Cyfrin/2023-07-beedle/blob/main/src/Staking.sol#L92

function updateFor(address recipient) public {
update();
+
uint256 _supplied = balances[recipient];
+
+ uint256 index_ = index;
+
if (_supplied > 0) {
uint256 _supplyIndex = supplyIndex[recipient];
- supplyIndex[recipient] = index;
+ supplyIndex[recipient] = index_;
- uint256 _delta = index - _supplyIndex;
+ uint256 _delta = index_ - _supplyIndex;
+
if (_delta > 0) {
uint256 _share = _supplied * _delta / 1e18;
claimable[recipient] += _share;
}
} else {
- supplyIndex[recipient] = index;
+ supplyIndex[recipient] = index_;
}
}

Support

FAQs

Can't find an answer? Chat with us on Discord, Twitter or Linkedin.