gas improvement in withdraw & withdrawAll #18
Labels
bug
Something isn't working
fixed-in-upstream-repo
This task has been implemented in the upstream repo
G (Gas Optimization)
resolved
sponsor acknowledged
Handle
gpersoon
Vulnerability details
Impact
The functions withdraw and withdrawAll retrieve the value of marketIndexOfToken[token] twice.
The second time its stored in the variable marketIndex.
This could be optimized to save a bit of gas
Proof of Concept
// https://github.com/code-423n4/2021-08-floatcapital/blob/main/contracts/contracts/Staker.sol#L948
function withdraw(address token, uint256 amount) external {
ILongShort(longShort).updateSystemState(marketIndexOfToken[token]);
uint32 marketIndex = marketIndexOfToken[token];
function withdrawAll(address token) external {
ILongShort(longShort).updateSystemState(marketIndexOfToken[token]);
uint32 marketIndex = marketIndexOfToken[token];
Tools Used
Recommended Mitigation Steps
Replace:
ILongShort(longShort).updateSystemState(marketIndexOfToken[token]);
uint32 marketIndex = marketIndexOfToken[token];
With:
uint32 marketIndex = marketIndexOfToken[token];
ILongShort(longShort).updateSystemState(marketIndex);
The text was updated successfully, but these errors were encountered: