Skip to content
This repository has been archived by the owner on Nov 26, 2023. It is now read-only.

J4de - USSDRebalancer.sol#BuyUSSDSellCollateral has a loss of precision #183

Closed
sherlock-admin opened this issue May 23, 2023 · 0 comments
Closed
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Medium A valid Medium severity issue Reward A payout will be made for this issue

Comments

@sherlock-admin
Copy link
Contributor

sherlock-admin commented May 23, 2023

J4de

medium

USSDRebalancer.sol#BuyUSSDSellCollateral has a loss of precision

Summary

The BuyUSSDSellCollateral has a loss of precision causing collateral sells less than expected

Vulnerability Detail

File: USSDRebalancer.sol
117         if (collateralval > amountToBuyLeftUSD) {
118           // sell a portion of collateral and exit
119           if (collateral[i].pathsell.length > 0) {
120             uint256 amountBefore = IERC20Upgradeable(baseAsset).balanceOf(USSD);
121 >>          uint256 amountToSellUnits = IERC20Upgradeable(collateral[i].token).balanceOf(USSD) * ((amountToBuyLeftUSD * 1e18 / collateralval) / 1e18) / 1e18;
122             IUSSD(USSD).UniV3SwapInput(collateral[i].pathsell, amountToSellUnits);
123             amountToBuyLeftUSD -= (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);
124             DAItosell += (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);
125           } else {

If the collateralval is large and the amountToBuyLeftUSD is small, the result of ((amountToBuyLeftUSD * 1e18 / collateralval) / 1e18) is 0.

Impact

Collateral sells less than expected causing may depeg of USSD prices.

Code Snippet

https://github.com/USSDofficial/ussd-contracts/blob/f44c726371f3152634bcf0a3e630802e39dec49c/contracts/USSDRebalancer.sol#L121

Tool used

Manual Review

Recommendation

It is recommended to change the order of calculation

            uint256 amountBefore = IERC20Upgradeable(baseAsset).balanceOf(USSD);
-           uint256 amountToSellUnits = IERC20Upgradeable(collateral[i].token).balanceOf(USSD) * ((amountToBuyLeftUSD * 1e18 / collateralval) / 1e18) / 1e18;
+           uint256 amountToSellUnits = IERC20Upgradeable(collateral[i].token).balanceOf(USSD) *
+               amountToBuyLeftUSD * 1e18 / collateralval / 1e18 / 1e18;
            IUSSD(USSD).UniV3SwapInput(collateral[i].pathsell, amountToSellUnits);
            amountToBuyLeftUSD -= (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);
            DAItosell += (IERC20Upgradeable(baseAsset).balanceOf(USSD) - amountBefore);

Duplicate of #656

@github-actions github-actions bot closed this as completed Jun 5, 2023
@github-actions github-actions bot added High A valid High severity issue Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label labels Jun 5, 2023
@sherlock-admin sherlock-admin added Medium A valid Medium severity issue Reward A payout will be made for this issue and removed High A valid High severity issue labels Jun 23, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Duplicate A valid issue that is a duplicate of an issue with `Has Duplicates` label Medium A valid Medium severity issue Reward A payout will be made for this issue
Projects
None yet
Development

No branches or pull requests

1 participant