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

0 stars 0 forks source link

Avoiding unnecessary storage reads can save gas #154

Closed code423n4 closed 2 years ago

code423n4 commented 2 years ago

Handle

WatchPug

Vulnerability details

https://github.com/XDeFi-tech/xdefi-distribution/blob/3856a42df295183b40c6eee89307308f196612fe/contracts/XDEFIDistribution.sol#L156-L159

function withdrawableOf(uint256 tokenId_) public view returns (uint256 withdrawableXDEFI_) {
    Position storage position = positionOf[tokenId_];
    return _withdrawableGiven(position.units, position.depositedXDEFI, position.pointsCorrection);
}

Can be changed to:

function withdrawableOf(uint256 tokenId_) public view returns (uint256 withdrawableXDEFI_) {
    Position memory position = positionOf[tokenId_];
    return _withdrawableGiven(position.units, position.depositedXDEFI, position.pointsCorrection);
}

https://github.com/XDeFi-tech/xdefi-distribution/blob/3856a42df295183b40c6eee89307308f196612fe/contracts/XDEFIDistribution.sol#L298-L301

Position storage position = positionOf[tokenId_];
uint96 units = position.units;
uint88 depositedXDEFI = position.depositedXDEFI;
uint32 expiry = position.expiry;

Can be changed to:

Position memory position = positionOf[tokenId_];
uint96 units = position.units;
uint88 depositedXDEFI = position.depositedXDEFI;
uint32 expiry = position.expiry;
deluca-mike commented 2 years ago

This actually costs more gas, both on deploy and at runtime. Try it by comparing the gas report generated by npm run test before and after changing the 2 storage instances.