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

Saurabh_Singh - UnderFlow revert in the amirX::swap function because of (fBalance - iBalance) substraction. #220

Open
sherlock-admin2 opened this issue Nov 13, 2024 · 0 comments

Comments

@sherlock-admin2
Copy link
Contributor

sherlock-admin2 commented Nov 13, 2024

Saurabh_Singh

Medium

UnderFlow revert in the amirX::swap function because of (fBalance - iBalance) substraction.

summary

The amirX::swap function will revert each time when the value of fBalance is less then iBalance since a swap is performed.

Vulnerability Detail

Since a swap is performed the value of token of a address can be increase or decreased depend on swap.
if the balance decreased (fBalance < IBalance) the below line will cause a underflow revert, becasue the value of a variable cannot be less then 0 in solidity.

https://github.com/sherlock-audit/2024-11-telcoin/blob/main/telcoin-audit/contracts/swap/AmirX.sol#L93

##Impact
The underflow revert will cause the contract to not perfrom the intended task of swap function.

##Recommendation

 function swap(
        address wallet,
        bool directional,
        StablecoinSwap memory ss,
        DefiSwap memory defi
    ) external payable onlyRole(SWAPPER_ROLE) whenNotPaused {
        // checks if it will fail
        if (ss.destination != address(0)) _verifyStablecoinSwap(wallet, ss);
        if (defi.walletData.length != 0) _verifyDefiSwap(wallet, defi);

        if (directional) {
            // if only defi swap
            if (ss.destination == address(0)) _defiSwap(wallet, defi);
            else {
                // if defi then stablecoin swap
                //check balance to adjust second swap
                uint256 iBalance = ERC20(ss.origin).balanceOf(wallet);
                if (defi.walletData.length != 0) _defiSwap(wallet, defi);
                uint256 fBalance = ERC20(ss.origin).balanceOf(wallet);
                //change balance to reflect change
-               if (fBalance - iBalance != 0) ss.oAmount = fBalance - iBalance;
+              if (fBalance - iBalance != 0){
+                 ss.oAmount = (fBalance >= iBalance ) ? (fBalance - iBalance) : iBalance - fBalance;
+                }
                _stablecoinSwap(wallet, ss);
            }
        } else {
            // if stablecoin swap
            _stablecoinSwap(wallet, ss);
            // if only stablecoin swap
            if (defi.walletData.length != 0) _defiSwap(wallet, defi);
        }
    }
@sherlock-admin3 sherlock-admin3 changed the title Wobbly Mandarin Raccoon - UnderFlow revert in the amirX::swap function because of (fBalance - iBalance) substraction. Saurabh_Singh - UnderFlow revert in the amirX::swap function because of (fBalance - iBalance) substraction. Nov 17, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant