diff --git a/.gitignore b/.gitignore index ffaf7e02..2a3952cd 100644 --- a/.gitignore +++ b/.gitignore @@ -11,6 +11,8 @@ dist cache artifacts typechain-types +out +cache_forge contracts/hardhat-dependency-compiler diff --git a/contracts/carbon/Strategies.sol b/contracts/carbon/Strategies.sol index 9dda945a..8d1e1f28 100644 --- a/contracts/carbon/Strategies.sol +++ b/contracts/carbon/Strategies.sol @@ -2,8 +2,9 @@ pragma solidity 0.8.19; import { Initializable } from "@openzeppelin/contracts-upgradeable/proxy/utils/Initializable.sol"; import { EnumerableSetUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/structs/EnumerableSetUpgradeable.sol"; -import { MathUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/math/MathUpgradeable.sol"; -import { SafeCastUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/math/SafeCastUpgradeable.sol"; +import { Math } from "@openzeppelin/contracts/utils/math/Math.sol"; +import { SafeMath } from "@openzeppelin/contracts/utils/math/SafeMath.sol"; +import { SafeCast } from "@openzeppelin/contracts/utils/math/SafeCast.sol"; import { Address } from "@openzeppelin/contracts/utils/Address.sol"; import { MathEx } from "../utility/MathEx.sol"; import { InvalidIndices } from "../utility/Utils.sol"; @@ -126,8 +127,7 @@ uint8 constant STRATEGY_UPDATE_REASON_TRADE = 1; abstract contract Strategies is Initializable { using EnumerableSetUpgradeable for EnumerableSetUpgradeable.UintSet; using Address for address payable; - using MathUpgradeable for uint256; - using SafeCastUpgradeable for uint256; + using SafeCast for uint256; error NativeAmountMismatch(); error BalanceMismatch(); @@ -774,11 +774,16 @@ abstract contract Strategies is Initializable { uint256 factor1 = MathEx.minFactor(temp1, temp1); uint256 factor2 = MathEx.minFactor(temp3, A); - uint256 factor = MathUpgradeable.max(factor1, factor2); + uint256 factor = Math.max(factor1, factor2); uint256 temp4 = MathEx.mulDivC(temp1, temp1, factor); uint256 temp5 = MathEx.mulDivC(temp3, A, factor); - return MathEx.mulDivF(temp2, temp3 / factor, temp4 + temp5); + + (bool safe, uint256 sum) = SafeMath.tryAdd(temp4, temp5); + if (safe) { + return MathEx.mulDivF(temp2, temp3 / factor, sum); + } + return temp2 / (A + MathEx.mulDivC(temp1, temp1, temp3)); } /** @@ -813,7 +818,7 @@ abstract contract Strategies is Initializable { uint256 factor1 = MathEx.minFactor(temp1, temp1); uint256 factor2 = MathEx.minFactor(temp2, temp3); - uint256 factor = MathUpgradeable.max(factor1, factor2); + uint256 factor = Math.max(factor1, factor2); uint256 temp4 = MathEx.mulDivC(temp1, temp1, factor); uint256 temp5 = MathEx.mulDivF(temp2, temp3, factor); diff --git a/test/carbon/accuracy/data/ExtremeSrcTrade.json b/test/carbon/accuracy/data/ExtremeSrcTrade.json index 70b2ef41..3af32c42 100644 --- a/test/carbon/accuracy/data/ExtremeSrcTrade.json +++ b/test/carbon/accuracy/data/ExtremeSrcTrade.json @@ -5108,5 +5108,15 @@ "inputAmount": "912345678", "implReturn": "11263526807791440437174508207861", "specReturn": "11263526807791494477914130651426" + }, + { + "liquidity": "969467735247045389207437", + "lowestRate": "33333333333333.01391180980424255864136284799315035343170166015625", + "highestRate": "198019801980197.408654344705869476683801622129976749420166015625", + "marginalRate": "104452559155538.8882958589671163947507815520699425817338168004935", + "tradeBy": "SourceAmount", + "inputAmount": "15120357940", + "implReturn": "924248818871126217204242", + "specReturn": "924248818871128843570750.388118986304" } ]