Submitted by GalloDaSballo, also found by KIntern_NA and bin2chen
The code to remove shares from Singularity is as follows:
https://github.com/Tapioca-DAO/tapioca-periph/blob/2ddbcb1cde03b548e13421b2dba66435d2ac8eb5/contracts/Magnetar/modules/MagnetarOptionModule.sol#L158-L159
Where share is computed in this way:
https://github.com/Tapioca-DAO/tapioca-periph/blob/2ddbcb1cde03b548e13421b2dba66435d2ac8eb5/contracts/Magnetar/modules/MagnetarOptionModule.sol#L153
The line is calculating: The (incorrectly rounded down) amount of shares of Yieldbox to burn in order to withdraw from Yieldbox the _removeAmount.
But the code is calling:
singularity_.removeAsset(data.user, removeAssetTo, share);
This is asking Singularity to remove a % (part) of the total assets in Singularity. Due to this, the line will stop working as soon as singularity has had any operation that generated interest.
Please see the formula used by Singularity for pricing asset:
https://github.com/Tapioca-DAO/Tapioca-bar/blob/c2031ac2e2667ac8f9ac48eaedae3dd52abef559/contracts/markets/singularity/SGLCommon.sol#L199-L216
As you can see, the fraction will be computed against _totalAsset.elastic + yieldBox.toShare(assetId, totalBorrow.elastic, false);. Meaning that the math will be incorrect as soon as any operation is done in Singularity
This Poc is built on the public repo: https://github.com/GalloDaSballo/yieldbox-foundry
We show how a change in interest will change fraction. In my local testing, fraction and shares are already out of sync. However, due to decimals it may be possible for them to be the same value, until some interest will make borrowElastic grow.
The unused function getFractionForAmount should help, minus some possible rounding considerations.
cryptotechmaker (Tapioca) confirmed, but disagreed with severity and commented via duplicate Issue #159:
