Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding Unchecked Directive will Save Gas for BurnMath.sol#getAsset and BurnMath.sol#getCollateral functions #183

Open
code423n4 opened this issue Jan 10, 2022 · 1 comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")

Comments

@code423n4
Copy link
Contributor

Handle

Rhynorater

Vulnerability details

In BurnMath.sol we have the following function defined;

    function getAsset(IPair.State memory state, uint256 liquidityIn) internal pure returns (uint128 assetOut) {
        if (state.reserves.asset <= state.totalClaims.bond) return assetOut; 
        uint256 _assetOut = state.reserves.asset;
        _assetOut -= state.totalClaims.bond;
        _assetOut = _assetOut.mulDiv(liquidityIn, state.totalLiquidity);
        assetOut = _assetOut.toUint128();
}

Since the above if statement ensures that state.reserves.asset is not less than or equal to state.totalClaims.bond, it is impossible for the _assetOut -= state.totalClaims.bond; line to underflow. As a result, adding the unchecked directive around this will save on gas.

By the same reasoning, in the getCollateral function:

deficit -= state.reserves.asset;

is already checked by the

if (state.reserves.asset >= state.totalClaims.bond) {

if statement. Surrounding this with unchecked will also save on gas.

Lastly, this also applies in WithdrawMath.sol#getCollateral:

        if (state.reserves.asset >= state.totalClaims.bond) return collateralOut;
        uint256 deficit = state.totalClaims.bond;
        deficit -= state.reserves.asset;

The deficit will never underflow here, so adding unchecked will save on gas.

##References
https://github.com/code-423n4/2022-01-timeswap/blob/5960e07d39f2b4a60cfabde1bd51f4b1e62e7e85/Timeswap/Timeswap-V1-Core/contracts/libraries/BurnMath.sol#L22
https://github.com/code-423n4/2022-01-timeswap/blob/5960e07d39f2b4a60cfabde1bd51f4b1e62e7e85/Timeswap/Timeswap-V1-Core/contracts/libraries/BurnMath.sol#L41
https://github.com/code-423n4/2022-01-timeswap/blob/5960e07d39f2b4a60cfabde1bd51f4b1e62e7e85/Timeswap/Timeswap-V1-Core/contracts/libraries/WithdrawMath.sol#L33

@code423n4 code423n4 added bug Something isn't working G (Gas Optimization) labels Jan 10, 2022
code423n4 added a commit that referenced this issue Jan 10, 2022
@Mathepreneur Mathepreneur added the sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity") label Jan 18, 2022
@Mathepreneur
Copy link
Collaborator

@Mathepreneur Mathepreneur added the resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) label Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working G (Gas Optimization) resolved Finding has been patched by sponsor (sponsor pls link to PR containing fix) sponsor confirmed Sponsor agrees this is a problem and intends to fix it (OK to use w/ "disagree with severity")
Projects
None yet
Development

No branches or pull requests

2 participants