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

Extend the supported input range of trade-by-source-amount #130

Merged
merged 6 commits into from
Jan 29, 2024
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 3 additions & 8 deletions contracts/carbon/Strategies.sol
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ 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 { SafeMathUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/math/SafeMathUpgradeable.sol";
lbeder marked this conversation as resolved.
Show resolved Hide resolved
import { SafeCastUpgradeable } from "@openzeppelin/contracts-upgradeable/utils/math/SafeCastUpgradeable.sol";
import { Address } from "@openzeppelin/contracts/utils/Address.sol";
import { MathEx } from "../utility/MathEx.sol";
Expand Down Expand Up @@ -127,6 +128,7 @@ abstract contract Strategies is Initializable {
using EnumerableSetUpgradeable for EnumerableSetUpgradeable.UintSet;
using Address for address payable;
using MathUpgradeable for uint256;
using SafeMathUpgradeable for uint256;
using SafeCastUpgradeable for uint256;

error NativeAmountMismatch();
Expand Down Expand Up @@ -779,7 +781,7 @@ abstract contract Strategies is Initializable {
uint256 temp4 = MathEx.mulDivC(temp1, temp1, factor);
uint256 temp5 = MathEx.mulDivC(temp3, A, factor);

(bool safe, uint256 sum) = tryAdd(temp4, temp5);
(bool safe, uint256 sum) = SafeMathUpgradeable.tryAdd(temp4, temp5);
lbeder marked this conversation as resolved.
Show resolved Hide resolved
if (safe) {
return MathEx.mulDivF(temp2, temp3 / factor, sum);
}
Expand Down Expand Up @@ -977,13 +979,6 @@ abstract contract Strategies is Initializable {
}
}

function tryAdd(uint256 x, uint256 y) private pure returns (bool safe, uint256 sum) {
unchecked {
sum = x + y;
safe = sum >= x;
}
}

function uncheckedInc(uint256 i) private pure returns (uint256 j) {
unchecked {
j = i + 1;
Expand Down
Loading