From 8371a549831f2fa878eda5560a03f1006c1dcebe Mon Sep 17 00:00:00 2001 From: JoscelynFarr Date: Fri, 26 Jan 2024 16:47:52 +0800 Subject: [PATCH] Fixed issue 65: https://github.com/sherlock-audit/2023-12-jojo-exchange-update-judging/issues/65 --- src/FundingRateArbitrage.sol | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/FundingRateArbitrage.sol b/src/FundingRateArbitrage.sol index a9141e6..6045823 100644 --- a/src/FundingRateArbitrage.sol +++ b/src/FundingRateArbitrage.sol @@ -25,6 +25,7 @@ contract FundingRateArbitrage is Ownable { using SafeERC20 for IERC20; using SignedDecimalMath for uint256; + using SignedDecimalMath for int256; address public immutable collateral; address public immutable jusdBank; @@ -90,8 +91,13 @@ contract FundingRateArbitrage is Ownable { uint256 usdcBuffer = IERC20(usdc).balanceOf(address(this)); uint256 collateralPrice = IJUSDBank(jusdBank).getCollateralPrice(collateral); (int256 perpNetValue,,,) = JOJODealer(jojoDealer).getTraderRisk(address(this)); - return - SafeCast.toUint256(perpNetValue) + collateralAmount.decimalMul(collateralPrice) + usdcBuffer - jusdBorrowed; + if (perpNetValue >= 0) { + return SafeCast.toUint256(perpNetValue) + collateralAmount.decimalMul(collateralPrice) + usdcBuffer + - jusdBorrowed; + } else { + return collateralAmount.decimalMul(collateralPrice) + usdcBuffer + - jusdBorrowed - perpNetValue.abs(); + } } /// @notice this function is to return the ratio between netValue and totalEarnUSDCBalance