Skip to content
Audit finding Intermediate

MagnetarHelper.getFractionForAmount uses the wrong rounding and will yield the wrong result

The helper function MagnetarHelper.getFractionForAmount rounds up when converting the borrowed amount to shares, while the actual withdrawal logic in Singularity rounds down. This mismatch means the helper returns an incorrect fraction, so a user withdrawing based on that fraction could receive a different amount than intended.

GalloDaSballo Follow with GalloDaSballo

Originally disclosed on Code4rena

Submitted by GalloDaSballo, also found by GalloDaSballo

MagnetarHelper.getFractionForAmount is used to determine the fraction (shares) that will be received when an amount is deposited. The code needs to compute the totalShares and then determine what the fraction is going to be.

The logic is used in Singularity._removeAsset and since it’s tied to a withdrawal, the rounding will be down. However, in MagnetarHelper the round is up.

https://github.com/Tapioca-DAO/tapioca-periph/blob/2ddbcb1cde03b548e13421b2dba66435d2ac8eb5/contracts/Magnetar/MagnetarHelper.sol#L208-L219

    function getFractionForAmount(ISingularity singularity, uint256 amount) external view returns (uint256 fraction) {
        (uint128 totalAssetShare, uint128 totalAssetBase) = singularity.totalAsset();
        (uint128 totalBorrowElastic,) = singularity.totalBorrow();
        uint256 assetId = singularity.assetId();

        IYieldBox yieldBox = IYieldBox(singularity.yieldBox());

        uint256 share = yieldBox.toShare(assetId, amount, false);
        uint256 allShare = totalAssetShare + yieldBox.toShare(assetId, totalBorrowElastic, true); /// @audit Round UP for Debt

        fraction = allShare == 0 ? share : (share * totalAssetBase) / allShare;
    }

See the actual implementation in Singularity:

https://github.com/Tapioca-DAO/Tapioca-bar/blob/c2031ac2e2667ac8f9ac48eaedae3dd52abef559/contracts/markets/singularity/SGLCommon.sol#L199-L216

    function _removeAsset(address from, address to, uint256 fraction) internal returns (uint256 share) {
        if (totalAsset.base == 0) {
            return 0;
        }
        Rebase memory _totalAsset = totalAsset;
        uint256 allShare = _totalAsset.elastic + yieldBox.toShare(assetId, totalBorrow.elastic, false);
        share = (fraction * allShare) / _totalAsset.base;
    }

This will cause issues when calculating how much fraction to withdraw based on the amount required by the withdrawer.

Mitigation

Change the rounding direction:

        uint256 allShare = totalAssetShare + yieldBox.toShare(assetId, totalBorrowElastic, down);

Assessed type

Math

cryptotechmaker (Tapioca) confirmed and commented:

PR here.


The original report as published on Code4rena, by GalloDaSballo. Images and videos are our own copies; raw HTML is shown as text. View it on Code4rena.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

How it worked

Root cause

Likely the helper function uses round-up when converting the borrowed amount to shares, but the actual withdrawal logic uses round-down, causing inconsistent fraction calculations.

Impact

The helper may return an incorrect fraction, so a withdrawal based on it could yield a different amount than intended; the text does not quantify the loss.

What the attacker needed

None

The fix

The rounding direction should be changed to round down, matching the withdrawal logic.

Explained by bugbounty.wiki with AI from the original and checked against quotes from it; it can still be wrong. Read the original before relying on a detail.

Not written yet. The AI write-up for this report has not been written yet. The Original tab links to the source.

When you withdraw from a savings pool, the system calculates how many 'shares' you get for your amount. This report says one helper function does that calculation with a different rounding rule than the real withdrawal code, so the helper's answer can be slightly wrong. That could make a user withdraw a bit more or less than they expected.

More by GalloDaSballo

View all

30 more disclosed reports, best first.

More from Tapioca Invitational

View all

The newest disclosed reports for this program.

Similar reports

Same bug class and techniques, clearest first.