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
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ dist
cache
artifacts
typechain-types
out
cache_forge

contracts/hardhat-dependency-compiler

Expand Down
19 changes: 12 additions & 7 deletions contracts/carbon/Strategies.sol
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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));
}

/**
Expand Down Expand Up @@ -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);
Expand Down
10 changes: 10 additions & 0 deletions test/carbon/accuracy/data/ExtremeSrcTrade.json
Original file line number Diff line number Diff line change
Expand Up @@ -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"
}
]
Loading