@pendle/core-v2 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +100 -0
- package/contracts/SuperComposableYield/ISuperComposableYield.sol +68 -0
- package/contracts/SuperComposableYield/SCYUtils.sol +14 -0
- package/contracts/SuperComposableYield/implementations/RewardManager.sol +117 -0
- package/contracts/SuperComposableYield/implementations/SCYBase.sol +126 -0
- package/contracts/SuperComposableYield/implementations/SCYBaseWithRewards.sol +79 -0
- package/contracts/core/PendleBaseToken.sol +39 -0
- package/contracts/core/PendleMarket.sol +248 -0
- package/contracts/core/PendleMarketFactory.sol +60 -0
- package/contracts/core/PendleOwnershipToken.sol +44 -0
- package/contracts/core/PendleSCYImpl/AaveV3/PendleAaveV3SCY.sol +139 -0
- package/contracts/core/PendleSCYImpl/AaveV3/WadRayMath.sol +126 -0
- package/contracts/core/PendleSCYImpl/PendleBenQiErc20SCY.sol +127 -0
- package/contracts/core/PendleSCYImpl/PendleBtrflySCY.sol +125 -0
- package/contracts/core/PendleSCYImpl/PendleYearnVaultSCY.sol +115 -0
- package/contracts/core/PendleYieldContractFactory.sol +109 -0
- package/contracts/core/PendleYieldToken.sol +240 -0
- package/contracts/core/misc/BoringOwnable.sol +62 -0
- package/contracts/core/misc/BoringOwnableUpg.sol +64 -0
- package/contracts/core/misc/PendleJoeSwapHelperUpg.sol +82 -0
- package/contracts/core/router/PendleRouterCoreUpg.sol +219 -0
- package/contracts/core/router/PendleRouterProxy.sol +64 -0
- package/contracts/core/router/PendleRouterYTUpg.sol +134 -0
- package/contracts/core/router/base/PendleRouterOTBaseUpg.sol +218 -0
- package/contracts/core/router/base/PendleRouterSCYAndForgeBaseUpg.sol +141 -0
- package/contracts/core/router/base/PendleRouterYTBaseUpg.sol +212 -0
- package/contracts/interfaces/IAToken.sol +34 -0
- package/contracts/interfaces/IAavePool.sol +41 -0
- package/contracts/interfaces/IAaveRewardsController.sol +32 -0
- package/contracts/interfaces/IBenQiComptroller.sol +33 -0
- package/contracts/interfaces/IJoePair.sol +98 -0
- package/contracts/interfaces/IJoeRouter01.sol +162 -0
- package/contracts/interfaces/IPBaseToken.sol +9 -0
- package/contracts/interfaces/IPFlashCallBack.sol +10 -0
- package/contracts/interfaces/IPGovernanceManager.sol +28 -0
- package/contracts/interfaces/IPMarket.sol +57 -0
- package/contracts/interfaces/IPMarketAddRemoveCallback.sol +18 -0
- package/contracts/interfaces/IPMarketFactory.sol +8 -0
- package/contracts/interfaces/IPMarketSwapCallback.sol +10 -0
- package/contracts/interfaces/IPOwnershipToken.sol +15 -0
- package/contracts/interfaces/IPPermissionsV2.sol +28 -0
- package/contracts/interfaces/IPRouterCore.sol +108 -0
- package/contracts/interfaces/IPRouterYT.sol +47 -0
- package/contracts/interfaces/IPYieldContractFactory.sol +36 -0
- package/contracts/interfaces/IPYieldToken.sol +19 -0
- package/contracts/interfaces/IQiErc20.sol +35 -0
- package/contracts/interfaces/IQiToken.sol +29 -0
- package/contracts/interfaces/IREDACTEDStaking.sol +35 -0
- package/contracts/interfaces/IWETH.sol +34 -0
- package/contracts/interfaces/IWXBTRFLY.sol +37 -0
- package/contracts/interfaces/IYearnVault.sol +32 -0
- package/contracts/libraries/JoeLibrary.sol +121 -0
- package/contracts/libraries/SCYIndex.sol +55 -0
- package/contracts/libraries/helpers/ExpiryUtilsLib.sol +166 -0
- package/contracts/libraries/math/FixedPoint.sol +208 -0
- package/contracts/libraries/math/LogExpMath.sol +515 -0
- package/contracts/libraries/math/MarketApproxLib.sol +140 -0
- package/contracts/libraries/math/MarketMathLib.sol +606 -0
- package/contracts/periphery/PendleGovernanceManager.sol +36 -0
- package/contracts/periphery/PermissionsV2.sol +23 -0
- package/package.json +76 -0
- package/typechain-types/BoringOwnable.ts +194 -0
- package/typechain-types/BoringOwnableData.ts +100 -0
- package/typechain-types/BoringOwnableUpg.ts +194 -0
- package/typechain-types/BoringOwnableUpgData.ts +100 -0
- package/typechain-types/ERC1967UpgradeUpgradeable.ts +97 -0
- package/typechain-types/ERC20.ts +417 -0
- package/typechain-types/IAToken.ts +331 -0
- package/typechain-types/IAavePool.ts +192 -0
- package/typechain-types/IAaveRewardsController.ts +148 -0
- package/typechain-types/IBeaconUpgradeable.ts +87 -0
- package/typechain-types/IBenQiComptroller.ts +125 -0
- package/typechain-types/IERC1822ProxiableUpgradeable.ts +87 -0
- package/typechain-types/IERC20.ts +300 -0
- package/typechain-types/IERC20Metadata.ts +339 -0
- package/typechain-types/IJoePair.ts +888 -0
- package/typechain-types/IJoeRouter01.ts +1107 -0
- package/typechain-types/IPBaseToken.ts +365 -0
- package/typechain-types/IPFlashCallback.ts +115 -0
- package/typechain-types/IPGovernanceManager.ts +84 -0
- package/typechain-types/IPMarket.ts +701 -0
- package/typechain-types/IPMarketAddRemoveCallback.ts +169 -0
- package/typechain-types/IPMarketFactory.ts +109 -0
- package/typechain-types/IPMarketSwapCallback.ts +115 -0
- package/typechain-types/IPOwnershipToken.ts +488 -0
- package/typechain-types/IPRouterCore.ts +759 -0
- package/typechain-types/IPRouterYT.ts +405 -0
- package/typechain-types/IPYieldContractFactory.ts +192 -0
- package/typechain-types/IPYieldToken.ts +525 -0
- package/typechain-types/IPermissionsV2.ts +87 -0
- package/typechain-types/IQiErc20.ts +402 -0
- package/typechain-types/IQiToken.ts +327 -0
- package/typechain-types/IREDACTEDStaking.ts +216 -0
- package/typechain-types/ISuperComposableYield.ts +652 -0
- package/typechain-types/IWETH.ts +380 -0
- package/typechain-types/IWXBTRFLY.ts +254 -0
- package/typechain-types/IYearnVault.ts +152 -0
- package/typechain-types/PendleAaveV3SCY.ts +901 -0
- package/typechain-types/PendleBaseToken.ts +505 -0
- package/typechain-types/PendleBenQiErc20SCY.ts +914 -0
- package/typechain-types/PendleBtrflyScy.ts +794 -0
- package/typechain-types/PendleGovernanceManager.ts +187 -0
- package/typechain-types/PendleJoeSwapHelperUpg.ts +97 -0
- package/typechain-types/PendleMarket.ts +1008 -0
- package/typechain-types/PendleMarketFactory.ts +246 -0
- package/typechain-types/PendleOwnershipToken.ts +628 -0
- package/typechain-types/PendleRouterCoreUpg.ts +780 -0
- package/typechain-types/PendleRouterProxy.ts +417 -0
- package/typechain-types/PendleRouterSCYAndForgeBaseUpg.ts +98 -0
- package/typechain-types/PendleRouterYTBaseUpg.ts +134 -0
- package/typechain-types/PendleRouterYTUpg.ts +453 -0
- package/typechain-types/PendleYearnVaultScy.ts +784 -0
- package/typechain-types/PendleYieldContractFactory.ts +479 -0
- package/typechain-types/PendleYieldToken.ts +1030 -0
- package/typechain-types/PermissionsV2.ts +87 -0
- package/typechain-types/Proxy.ts +57 -0
- package/typechain-types/RewardManager.ts +172 -0
- package/typechain-types/SCYBase.ts +734 -0
- package/typechain-types/SCYBaseWithRewards.ts +819 -0
- package/typechain-types/UUPSUpgradeable.ts +197 -0
- package/typechain-types/common.ts +44 -0
- package/typechain-types/factories/BoringOwnableData__factory.ts +90 -0
- package/typechain-types/factories/BoringOwnableUpgData__factory.ts +94 -0
- package/typechain-types/factories/BoringOwnableUpg__factory.ts +139 -0
- package/typechain-types/factories/BoringOwnable__factory.ts +149 -0
- package/typechain-types/factories/ERC1967UpgradeUpgradeable__factory.ts +75 -0
- package/typechain-types/factories/ERC20__factory.ts +348 -0
- package/typechain-types/factories/IAToken__factory.ts +225 -0
- package/typechain-types/factories/IAavePool__factory.ts +99 -0
- package/typechain-types/factories/IAaveRewardsController__factory.ts +78 -0
- package/typechain-types/factories/IBeaconUpgradeable__factory.ts +39 -0
- package/typechain-types/factories/IBenQiComptroller__factory.ts +59 -0
- package/typechain-types/factories/IERC1822ProxiableUpgradeable__factory.ts +43 -0
- package/typechain-types/factories/IERC20Metadata__factory.ts +248 -0
- package/typechain-types/factories/IERC20__factory.ts +203 -0
- package/typechain-types/factories/IJoePair__factory.ts +674 -0
- package/typechain-types/factories/IJoeRouter01__factory.ts +772 -0
- package/typechain-types/factories/IPBaseToken__factory.ts +271 -0
- package/typechain-types/factories/IPFlashCallback__factory.ts +49 -0
- package/typechain-types/factories/IPGovernanceManager__factory.ts +39 -0
- package/typechain-types/factories/IPMarketAddRemoveCallback__factory.ts +86 -0
- package/typechain-types/factories/IPMarketFactory__factory.ts +58 -0
- package/typechain-types/factories/IPMarketSwapCallback__factory.ts +53 -0
- package/typechain-types/factories/IPMarket__factory.ts +559 -0
- package/typechain-types/factories/IPOwnershipToken__factory.ts +349 -0
- package/typechain-types/factories/IPRouterCore__factory.ts +516 -0
- package/typechain-types/factories/IPRouterYT__factory.ts +246 -0
- package/typechain-types/factories/IPYieldContractFactory__factory.ts +117 -0
- package/typechain-types/factories/IPYieldToken__factory.ts +383 -0
- package/typechain-types/factories/IPermissionsV2__factory.ts +39 -0
- package/typechain-types/factories/IQiErc20__factory.ts +270 -0
- package/typechain-types/factories/IQiToken__factory.ts +219 -0
- package/typechain-types/factories/IREDACTEDStaking__factory.ts +107 -0
- package/typechain-types/factories/ISuperComposableYield__factory.ts +438 -0
- package/typechain-types/factories/IWETH__factory.ts +261 -0
- package/typechain-types/factories/IWXBTRFLY__factory.ts +131 -0
- package/typechain-types/factories/IYearnVault__factory.ts +74 -0
- package/typechain-types/factories/PendleAaveV3SCY__factory.ts +727 -0
- package/typechain-types/factories/PendleBaseToken__factory.ts +386 -0
- package/typechain-types/factories/PendleBenQiErc20SCY__factory.ts +753 -0
- package/typechain-types/factories/PendleBtrflyScy__factory.ts +652 -0
- package/typechain-types/factories/PendleGovernanceManager__factory.ts +149 -0
- package/typechain-types/factories/PendleJoeSwapHelperUpg__factory.ts +56 -0
- package/typechain-types/factories/PendleMarketFactory__factory.ts +207 -0
- package/typechain-types/factories/PendleMarket__factory.ts +891 -0
- package/typechain-types/factories/PendleOwnershipToken__factory.ts +565 -0
- package/typechain-types/factories/PendleRouterCoreUpg__factory.ts +620 -0
- package/typechain-types/factories/PendleRouterProxy__factory.ts +316 -0
- package/typechain-types/factories/PendleRouterSCYAndForgeBaseUpg__factory.ts +56 -0
- package/typechain-types/factories/PendleRouterYTBaseUpg__factory.ts +66 -0
- package/typechain-types/factories/PendleRouterYTUpg__factory.ts +363 -0
- package/typechain-types/factories/PendleYearnVaultScy__factory.ts +630 -0
- package/typechain-types/factories/PendleYieldContractFactory__factory.ts +368 -0
- package/typechain-types/factories/PendleYieldToken__factory.ts +814 -0
- package/typechain-types/factories/PermissionsV2__factory.ts +36 -0
- package/typechain-types/factories/Proxy__factory.ts +28 -0
- package/typechain-types/factories/RewardManager__factory.ts +89 -0
- package/typechain-types/factories/SCYBaseWithRewards__factory.ts +535 -0
- package/typechain-types/factories/SCYBase__factory.ts +479 -0
- package/typechain-types/factories/UUPSUpgradeable__factory.ts +115 -0
- package/typechain-types/hardhat.d.ts +564 -0
- package/typechain-types/index.ts +122 -0
|
@@ -0,0 +1,515 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
2
|
+
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated
|
|
3
|
+
// documentation files (the “Software”), to deal in the Software without restriction, including without limitation the
|
|
4
|
+
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to
|
|
5
|
+
// permit persons to whom the Software is furnished to do so, subject to the following conditions:
|
|
6
|
+
|
|
7
|
+
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the
|
|
8
|
+
// Software.
|
|
9
|
+
|
|
10
|
+
// THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
|
|
11
|
+
// WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
|
|
12
|
+
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR
|
|
13
|
+
// OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
14
|
+
|
|
15
|
+
pragma solidity 0.8.9;
|
|
16
|
+
|
|
17
|
+
/* solhint-disable */
|
|
18
|
+
|
|
19
|
+
/**
|
|
20
|
+
* @dev Exponentiation and logarithm functions for 18 decimal fixed point numbers (both base and exponent/argument).
|
|
21
|
+
*
|
|
22
|
+
* Exponentiation and logarithm with arbitrary bases (x^y and log_x(y)) are implemented by conversion to natural
|
|
23
|
+
* exponentiation and logarithm (where the base is Euler's number).
|
|
24
|
+
*
|
|
25
|
+
* @author Fernando Martinelli - @fernandomartinelli
|
|
26
|
+
* @author Sergio Yuhjtman - @sergioyuhjtman
|
|
27
|
+
* @author Daniel Fernandez - @dmf7z
|
|
28
|
+
*/
|
|
29
|
+
library LogExpMath {
|
|
30
|
+
// All fixed point multiplications and divisions are inlined. This means we need to divide by ONE when multiplying
|
|
31
|
+
// two numbers, and multiply by ONE when dividing them.
|
|
32
|
+
|
|
33
|
+
// All arguments and return values are 18 decimal fixed point numbers.
|
|
34
|
+
int256 constant ONE_18 = 1e18;
|
|
35
|
+
|
|
36
|
+
// Internally, intermediate values are computed with higher precision as 20 decimal fixed point numbers, and in the
|
|
37
|
+
// case of ln36, 36 decimals.
|
|
38
|
+
int256 constant ONE_20 = 1e20;
|
|
39
|
+
int256 constant ONE_36 = 1e36;
|
|
40
|
+
|
|
41
|
+
// The domain of natural exponentiation is bound by the word size and number of decimals used.
|
|
42
|
+
//
|
|
43
|
+
// Because internally the result will be stored using 20 decimals, the largest possible result is
|
|
44
|
+
// (2^255 - 1) / 10^20, which makes the largest exponent ln((2^255 - 1) / 10^20) = 130.700829182905140221.
|
|
45
|
+
// The smallest possible result is 10^(-18), which makes largest negative argument
|
|
46
|
+
// ln(10^(-18)) = -41.446531673892822312.
|
|
47
|
+
// We use 130.0 and -41.0 to have some safety margin.
|
|
48
|
+
int256 constant MAX_NATURAL_EXPONENT = 130e18;
|
|
49
|
+
int256 constant MIN_NATURAL_EXPONENT = -41e18;
|
|
50
|
+
|
|
51
|
+
// Bounds for ln_36's argument. Both ln(0.9) and ln(1.1) can be represented with 36 decimal places in a fixed point
|
|
52
|
+
// 256 bit integer.
|
|
53
|
+
int256 constant LN_36_LOWER_BOUND = ONE_18 - 1e17;
|
|
54
|
+
int256 constant LN_36_UPPER_BOUND = ONE_18 + 1e17;
|
|
55
|
+
|
|
56
|
+
uint256 constant MILD_EXPONENT_BOUND = 2**254 / uint256(ONE_20);
|
|
57
|
+
|
|
58
|
+
// 18 decimal constants
|
|
59
|
+
int256 constant x0 = 128000000000000000000; // 2ˆ7
|
|
60
|
+
int256 constant a0 = 38877084059945950922200000000000000000000000000000000000; // eˆ(x0) (no decimals)
|
|
61
|
+
int256 constant x1 = 64000000000000000000; // 2ˆ6
|
|
62
|
+
int256 constant a1 = 6235149080811616882910000000; // eˆ(x1) (no decimals)
|
|
63
|
+
|
|
64
|
+
// 20 decimal constants
|
|
65
|
+
int256 constant x2 = 3200000000000000000000; // 2ˆ5
|
|
66
|
+
int256 constant a2 = 7896296018268069516100000000000000; // eˆ(x2)
|
|
67
|
+
int256 constant x3 = 1600000000000000000000; // 2ˆ4
|
|
68
|
+
int256 constant a3 = 888611052050787263676000000; // eˆ(x3)
|
|
69
|
+
int256 constant x4 = 800000000000000000000; // 2ˆ3
|
|
70
|
+
int256 constant a4 = 298095798704172827474000; // eˆ(x4)
|
|
71
|
+
int256 constant x5 = 400000000000000000000; // 2ˆ2
|
|
72
|
+
int256 constant a5 = 5459815003314423907810; // eˆ(x5)
|
|
73
|
+
int256 constant x6 = 200000000000000000000; // 2ˆ1
|
|
74
|
+
int256 constant a6 = 738905609893065022723; // eˆ(x6)
|
|
75
|
+
int256 constant x7 = 100000000000000000000; // 2ˆ0
|
|
76
|
+
int256 constant a7 = 271828182845904523536; // eˆ(x7)
|
|
77
|
+
int256 constant x8 = 50000000000000000000; // 2ˆ-1
|
|
78
|
+
int256 constant a8 = 164872127070012814685; // eˆ(x8)
|
|
79
|
+
int256 constant x9 = 25000000000000000000; // 2ˆ-2
|
|
80
|
+
int256 constant a9 = 128402541668774148407; // eˆ(x9)
|
|
81
|
+
int256 constant x10 = 12500000000000000000; // 2ˆ-3
|
|
82
|
+
int256 constant a10 = 113314845306682631683; // eˆ(x10)
|
|
83
|
+
int256 constant x11 = 6250000000000000000; // 2ˆ-4
|
|
84
|
+
int256 constant a11 = 106449445891785942956; // eˆ(x11)
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* @dev Exponentiation (x^y) with unsigned 18 decimal fixed point base and exponent.
|
|
88
|
+
*
|
|
89
|
+
* Reverts if ln(x) * y is smaller than `MIN_NATURAL_EXPONENT`, or larger than `MAX_NATURAL_EXPONENT`.
|
|
90
|
+
*/
|
|
91
|
+
function pow(uint256 x, uint256 y) internal pure returns (uint256) {
|
|
92
|
+
if (y == 0) {
|
|
93
|
+
// We solve the 0^0 indetermination by making it equal one.
|
|
94
|
+
return uint256(ONE_18);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
if (x == 0) {
|
|
98
|
+
return 0;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// Instead of computing x^y directly, we instead rely on the properties of logarithms and exponentiation to
|
|
102
|
+
// arrive at that result. In particular, exp(ln(x)) = x, and ln(x^y) = y * ln(x). This means
|
|
103
|
+
// x^y = exp(y * ln(x)).
|
|
104
|
+
|
|
105
|
+
// The ln function takes a signed value, so we need to make sure x fits in the signed 256 bit range.
|
|
106
|
+
require(x < 2**255, "X out of bounds");
|
|
107
|
+
int256 x_int256 = int256(x);
|
|
108
|
+
|
|
109
|
+
// We will compute y * ln(x) in a single step. Depending on the value of x, we can either use ln or ln_36. In
|
|
110
|
+
// both cases, we leave the division by ONE_18 (due to fixed point multiplication) to the end.
|
|
111
|
+
|
|
112
|
+
// This prevents y * ln(x) from overflowing, and at the same time guarantees y fits in the signed 256 bit range.
|
|
113
|
+
require(y < MILD_EXPONENT_BOUND, "Y out of bounds");
|
|
114
|
+
int256 y_int256 = int256(y);
|
|
115
|
+
|
|
116
|
+
int256 logx_times_y;
|
|
117
|
+
if (LN_36_LOWER_BOUND < x_int256 && x_int256 < LN_36_UPPER_BOUND) {
|
|
118
|
+
int256 ln_36_x = _ln_36(x_int256);
|
|
119
|
+
|
|
120
|
+
// ln_36_x has 36 decimal places, so multiplying by y_int256 isn't as straightforward, since we can't just
|
|
121
|
+
// bring y_int256 to 36 decimal places, as it might overflow. Instead, we perform two 18 decimal
|
|
122
|
+
// multiplications and add the results: one with the first 18 decimals of ln_36_x, and one with the
|
|
123
|
+
// (downscaled) last 18 decimals.
|
|
124
|
+
logx_times_y = ((ln_36_x / ONE_18) *
|
|
125
|
+
y_int256 +
|
|
126
|
+
((ln_36_x % ONE_18) * y_int256) /
|
|
127
|
+
ONE_18);
|
|
128
|
+
} else {
|
|
129
|
+
logx_times_y = _ln(x_int256) * y_int256;
|
|
130
|
+
}
|
|
131
|
+
logx_times_y /= ONE_18;
|
|
132
|
+
|
|
133
|
+
// Finally, we compute exp(y * ln(x)) to arrive at x^y
|
|
134
|
+
require(
|
|
135
|
+
MIN_NATURAL_EXPONENT <= logx_times_y && logx_times_y <= MAX_NATURAL_EXPONENT,
|
|
136
|
+
"Product out of bounds"
|
|
137
|
+
);
|
|
138
|
+
|
|
139
|
+
return uint256(exp(logx_times_y));
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/**
|
|
143
|
+
* @dev Natural exponentiation (e^x) with signed 18 decimal fixed point exponent.
|
|
144
|
+
*
|
|
145
|
+
* Reverts if `x` is smaller than MIN_NATURAL_EXPONENT, or larger than `MAX_NATURAL_EXPONENT`.
|
|
146
|
+
*/
|
|
147
|
+
function exp(int256 x) internal pure returns (int256) {
|
|
148
|
+
require(x >= MIN_NATURAL_EXPONENT && x <= MAX_NATURAL_EXPONENT, "Invalid exponent");
|
|
149
|
+
|
|
150
|
+
if (x < 0) {
|
|
151
|
+
// We only handle positive exponents: e^(-x) is computed as 1 / e^x. We can safely make x positive since it
|
|
152
|
+
// fits in the signed 256 bit range (as it is larger than MIN_NATURAL_EXPONENT).
|
|
153
|
+
// Fixed point division requires multiplying by ONE_18.
|
|
154
|
+
return ((ONE_18 * ONE_18) / exp(-x));
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
// First, we use the fact that e^(x+y) = e^x * e^y to decompose x into a sum of powers of two, which we call x_n,
|
|
158
|
+
// where x_n == 2^(7 - n), and e^x_n = a_n has been precomputed. We choose the first x_n, x0, to equal 2^7
|
|
159
|
+
// because all larger powers are larger than MAX_NATURAL_EXPONENT, and therefore not present in the
|
|
160
|
+
// decomposition.
|
|
161
|
+
// At the end of this process we will have the product of all e^x_n = a_n that apply, and the remainder of this
|
|
162
|
+
// decomposition, which will be lower than the smallest x_n.
|
|
163
|
+
// exp(x) = k_0 * a_0 * k_1 * a_1 * ... + k_n * a_n * exp(remainder), where each k_n equals either 0 or 1.
|
|
164
|
+
// We mutate x by subtracting x_n, making it the remainder of the decomposition.
|
|
165
|
+
|
|
166
|
+
// The first two a_n (e^(2^7) and e^(2^6)) are too large if stored as 18 decimal numbers, and could cause
|
|
167
|
+
// intermediate overflows. Instead we store them as plain integers, with 0 decimals.
|
|
168
|
+
// Additionally, x0 + x1 is larger than MAX_NATURAL_EXPONENT, which means they will not both be present in the
|
|
169
|
+
// decomposition.
|
|
170
|
+
|
|
171
|
+
// For each x_n, we test if that term is present in the decomposition (if x is larger than it), and if so deduct
|
|
172
|
+
// it and compute the accumulated product.
|
|
173
|
+
|
|
174
|
+
int256 firstAN;
|
|
175
|
+
if (x >= x0) {
|
|
176
|
+
x -= x0;
|
|
177
|
+
firstAN = a0;
|
|
178
|
+
} else if (x >= x1) {
|
|
179
|
+
x -= x1;
|
|
180
|
+
firstAN = a1;
|
|
181
|
+
} else {
|
|
182
|
+
firstAN = 1; // One with no decimal places
|
|
183
|
+
}
|
|
184
|
+
|
|
185
|
+
// We now transform x into a 20 decimal fixed point number, to have enhanced precision when computing the
|
|
186
|
+
// smaller terms.
|
|
187
|
+
x *= 100;
|
|
188
|
+
|
|
189
|
+
// `product` is the accumulated product of all a_n (except a0 and a1), which starts at 20 decimal fixed point
|
|
190
|
+
// one. Recall that fixed point multiplication requires dividing by ONE_20.
|
|
191
|
+
int256 product = ONE_20;
|
|
192
|
+
|
|
193
|
+
if (x >= x2) {
|
|
194
|
+
x -= x2;
|
|
195
|
+
product = (product * a2) / ONE_20;
|
|
196
|
+
}
|
|
197
|
+
if (x >= x3) {
|
|
198
|
+
x -= x3;
|
|
199
|
+
product = (product * a3) / ONE_20;
|
|
200
|
+
}
|
|
201
|
+
if (x >= x4) {
|
|
202
|
+
x -= x4;
|
|
203
|
+
product = (product * a4) / ONE_20;
|
|
204
|
+
}
|
|
205
|
+
if (x >= x5) {
|
|
206
|
+
x -= x5;
|
|
207
|
+
product = (product * a5) / ONE_20;
|
|
208
|
+
}
|
|
209
|
+
if (x >= x6) {
|
|
210
|
+
x -= x6;
|
|
211
|
+
product = (product * a6) / ONE_20;
|
|
212
|
+
}
|
|
213
|
+
if (x >= x7) {
|
|
214
|
+
x -= x7;
|
|
215
|
+
product = (product * a7) / ONE_20;
|
|
216
|
+
}
|
|
217
|
+
if (x >= x8) {
|
|
218
|
+
x -= x8;
|
|
219
|
+
product = (product * a8) / ONE_20;
|
|
220
|
+
}
|
|
221
|
+
if (x >= x9) {
|
|
222
|
+
x -= x9;
|
|
223
|
+
product = (product * a9) / ONE_20;
|
|
224
|
+
}
|
|
225
|
+
|
|
226
|
+
// x10 and x11 are unnecessary here since we have high enough precision already.
|
|
227
|
+
|
|
228
|
+
// Now we need to compute e^x, where x is small (in particular, it is smaller than x9). We use the Taylor series
|
|
229
|
+
// expansion for e^x: 1 + x + (x^2 / 2!) + (x^3 / 3!) + ... + (x^n / n!).
|
|
230
|
+
|
|
231
|
+
int256 seriesSum = ONE_20; // The initial one in the sum, with 20 decimal places.
|
|
232
|
+
int256 term; // Each term in the sum, where the nth term is (x^n / n!).
|
|
233
|
+
|
|
234
|
+
// The first term is simply x.
|
|
235
|
+
term = x;
|
|
236
|
+
seriesSum += term;
|
|
237
|
+
|
|
238
|
+
// Each term (x^n / n!) equals the previous one times x, divided by n. Since x is a fixed point number,
|
|
239
|
+
// multiplying by it requires dividing by ONE_20, but dividing by the non-fixed point n values does not.
|
|
240
|
+
|
|
241
|
+
term = ((term * x) / ONE_20) / 2;
|
|
242
|
+
seriesSum += term;
|
|
243
|
+
|
|
244
|
+
term = ((term * x) / ONE_20) / 3;
|
|
245
|
+
seriesSum += term;
|
|
246
|
+
|
|
247
|
+
term = ((term * x) / ONE_20) / 4;
|
|
248
|
+
seriesSum += term;
|
|
249
|
+
|
|
250
|
+
term = ((term * x) / ONE_20) / 5;
|
|
251
|
+
seriesSum += term;
|
|
252
|
+
|
|
253
|
+
term = ((term * x) / ONE_20) / 6;
|
|
254
|
+
seriesSum += term;
|
|
255
|
+
|
|
256
|
+
term = ((term * x) / ONE_20) / 7;
|
|
257
|
+
seriesSum += term;
|
|
258
|
+
|
|
259
|
+
term = ((term * x) / ONE_20) / 8;
|
|
260
|
+
seriesSum += term;
|
|
261
|
+
|
|
262
|
+
term = ((term * x) / ONE_20) / 9;
|
|
263
|
+
seriesSum += term;
|
|
264
|
+
|
|
265
|
+
term = ((term * x) / ONE_20) / 10;
|
|
266
|
+
seriesSum += term;
|
|
267
|
+
|
|
268
|
+
term = ((term * x) / ONE_20) / 11;
|
|
269
|
+
seriesSum += term;
|
|
270
|
+
|
|
271
|
+
term = ((term * x) / ONE_20) / 12;
|
|
272
|
+
seriesSum += term;
|
|
273
|
+
|
|
274
|
+
// 12 Taylor terms are sufficient for 18 decimal precision.
|
|
275
|
+
|
|
276
|
+
// We now have the first a_n (with no decimals), and the product of all other a_n present, and the Taylor
|
|
277
|
+
// approximation of the exponentiation of the remainder (both with 20 decimals). All that remains is to multiply
|
|
278
|
+
// all three (one 20 decimal fixed point multiplication, dividing by ONE_20, and one integer multiplication),
|
|
279
|
+
// and then drop two digits to return an 18 decimal value.
|
|
280
|
+
|
|
281
|
+
return (((product * seriesSum) / ONE_20) * firstAN) / 100;
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* @dev Logarithm (log(arg, base), with signed 18 decimal fixed point base and argument.
|
|
286
|
+
*/
|
|
287
|
+
function log(int256 arg, int256 base) internal pure returns (int256) {
|
|
288
|
+
// This performs a simple base change: log(arg, base) = ln(arg) / ln(base).
|
|
289
|
+
|
|
290
|
+
// Both logBase and logArg are computed as 36 decimal fixed point numbers, either by using ln_36, or by
|
|
291
|
+
// upscaling.
|
|
292
|
+
|
|
293
|
+
int256 logBase;
|
|
294
|
+
if (LN_36_LOWER_BOUND < base && base < LN_36_UPPER_BOUND) {
|
|
295
|
+
logBase = _ln_36(base);
|
|
296
|
+
} else {
|
|
297
|
+
logBase = _ln(base) * ONE_18;
|
|
298
|
+
}
|
|
299
|
+
|
|
300
|
+
int256 logArg;
|
|
301
|
+
if (LN_36_LOWER_BOUND < arg && arg < LN_36_UPPER_BOUND) {
|
|
302
|
+
logArg = _ln_36(arg);
|
|
303
|
+
} else {
|
|
304
|
+
logArg = _ln(arg) * ONE_18;
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
// When dividing, we multiply by ONE_18 to arrive at a result with 18 decimal places
|
|
308
|
+
return (logArg * ONE_18) / logBase;
|
|
309
|
+
}
|
|
310
|
+
|
|
311
|
+
/**
|
|
312
|
+
* @dev Natural logarithm (ln(a)) with signed 18 decimal fixed point argument.
|
|
313
|
+
*/
|
|
314
|
+
function ln(int256 a) internal pure returns (int256) {
|
|
315
|
+
// The real natural logarithm is not defined for negative numbers or zero.
|
|
316
|
+
require(a > 0, "out of bounds");
|
|
317
|
+
if (LN_36_LOWER_BOUND < a && a < LN_36_UPPER_BOUND) {
|
|
318
|
+
return _ln_36(a) / ONE_18;
|
|
319
|
+
} else {
|
|
320
|
+
return _ln(a);
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
|
|
324
|
+
/**
|
|
325
|
+
* @dev Internal natural logarithm (ln(a)) with signed 18 decimal fixed point argument.
|
|
326
|
+
*/
|
|
327
|
+
function _ln(int256 a) private pure returns (int256) {
|
|
328
|
+
if (a < ONE_18) {
|
|
329
|
+
// Since ln(a^k) = k * ln(a), we can compute ln(a) as ln(a) = ln((1/a)^(-1)) = - ln((1/a)). If a is less
|
|
330
|
+
// than one, 1/a will be greater than one, and this if statement will not be entered in the recursive call.
|
|
331
|
+
// Fixed point division requires multiplying by ONE_18.
|
|
332
|
+
return (-_ln((ONE_18 * ONE_18) / a));
|
|
333
|
+
}
|
|
334
|
+
|
|
335
|
+
// First, we use the fact that ln^(a * b) = ln(a) + ln(b) to decompose ln(a) into a sum of powers of two, which
|
|
336
|
+
// we call x_n, where x_n == 2^(7 - n), which are the natural logarithm of precomputed quantities a_n (that is,
|
|
337
|
+
// ln(a_n) = x_n). We choose the first x_n, x0, to equal 2^7 because the exponential of all larger powers cannot
|
|
338
|
+
// be represented as 18 fixed point decimal numbers in 256 bits, and are therefore larger than a.
|
|
339
|
+
// At the end of this process we will have the sum of all x_n = ln(a_n) that apply, and the remainder of this
|
|
340
|
+
// decomposition, which will be lower than the smallest a_n.
|
|
341
|
+
// ln(a) = k_0 * x_0 + k_1 * x_1 + ... + k_n * x_n + ln(remainder), where each k_n equals either 0 or 1.
|
|
342
|
+
// We mutate a by subtracting a_n, making it the remainder of the decomposition.
|
|
343
|
+
|
|
344
|
+
// For reasons related to how `exp` works, the first two a_n (e^(2^7) and e^(2^6)) are not stored as fixed point
|
|
345
|
+
// numbers with 18 decimals, but instead as plain integers with 0 decimals, so we need to multiply them by
|
|
346
|
+
// ONE_18 to convert them to fixed point.
|
|
347
|
+
// For each a_n, we test if that term is present in the decomposition (if a is larger than it), and if so divide
|
|
348
|
+
// by it and compute the accumulated sum.
|
|
349
|
+
|
|
350
|
+
int256 sum = 0;
|
|
351
|
+
if (a >= a0 * ONE_18) {
|
|
352
|
+
a /= a0; // Integer, not fixed point division
|
|
353
|
+
sum += x0;
|
|
354
|
+
}
|
|
355
|
+
|
|
356
|
+
if (a >= a1 * ONE_18) {
|
|
357
|
+
a /= a1; // Integer, not fixed point division
|
|
358
|
+
sum += x1;
|
|
359
|
+
}
|
|
360
|
+
|
|
361
|
+
// All other a_n and x_n are stored as 20 digit fixed point numbers, so we convert the sum and a to this format.
|
|
362
|
+
sum *= 100;
|
|
363
|
+
a *= 100;
|
|
364
|
+
|
|
365
|
+
// Because further a_n are 20 digit fixed point numbers, we multiply by ONE_20 when dividing by them.
|
|
366
|
+
|
|
367
|
+
if (a >= a2) {
|
|
368
|
+
a = (a * ONE_20) / a2;
|
|
369
|
+
sum += x2;
|
|
370
|
+
}
|
|
371
|
+
|
|
372
|
+
if (a >= a3) {
|
|
373
|
+
a = (a * ONE_20) / a3;
|
|
374
|
+
sum += x3;
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
if (a >= a4) {
|
|
378
|
+
a = (a * ONE_20) / a4;
|
|
379
|
+
sum += x4;
|
|
380
|
+
}
|
|
381
|
+
|
|
382
|
+
if (a >= a5) {
|
|
383
|
+
a = (a * ONE_20) / a5;
|
|
384
|
+
sum += x5;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
if (a >= a6) {
|
|
388
|
+
a = (a * ONE_20) / a6;
|
|
389
|
+
sum += x6;
|
|
390
|
+
}
|
|
391
|
+
|
|
392
|
+
if (a >= a7) {
|
|
393
|
+
a = (a * ONE_20) / a7;
|
|
394
|
+
sum += x7;
|
|
395
|
+
}
|
|
396
|
+
|
|
397
|
+
if (a >= a8) {
|
|
398
|
+
a = (a * ONE_20) / a8;
|
|
399
|
+
sum += x8;
|
|
400
|
+
}
|
|
401
|
+
|
|
402
|
+
if (a >= a9) {
|
|
403
|
+
a = (a * ONE_20) / a9;
|
|
404
|
+
sum += x9;
|
|
405
|
+
}
|
|
406
|
+
|
|
407
|
+
if (a >= a10) {
|
|
408
|
+
a = (a * ONE_20) / a10;
|
|
409
|
+
sum += x10;
|
|
410
|
+
}
|
|
411
|
+
|
|
412
|
+
if (a >= a11) {
|
|
413
|
+
a = (a * ONE_20) / a11;
|
|
414
|
+
sum += x11;
|
|
415
|
+
}
|
|
416
|
+
|
|
417
|
+
// a is now a small number (smaller than a_11, which roughly equals 1.06). This means we can use a Taylor series
|
|
418
|
+
// that converges rapidly for values of `a` close to one - the same one used in ln_36.
|
|
419
|
+
// Let z = (a - 1) / (a + 1).
|
|
420
|
+
// ln(a) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
|
|
421
|
+
|
|
422
|
+
// Recall that 20 digit fixed point division requires multiplying by ONE_20, and multiplication requires
|
|
423
|
+
// division by ONE_20.
|
|
424
|
+
int256 z = ((a - ONE_20) * ONE_20) / (a + ONE_20);
|
|
425
|
+
int256 z_squared = (z * z) / ONE_20;
|
|
426
|
+
|
|
427
|
+
// num is the numerator of the series: the z^(2 * n + 1) term
|
|
428
|
+
int256 num = z;
|
|
429
|
+
|
|
430
|
+
// seriesSum holds the accumulated sum of each term in the series, starting with the initial z
|
|
431
|
+
int256 seriesSum = num;
|
|
432
|
+
|
|
433
|
+
// In each step, the numerator is multiplied by z^2
|
|
434
|
+
num = (num * z_squared) / ONE_20;
|
|
435
|
+
seriesSum += num / 3;
|
|
436
|
+
|
|
437
|
+
num = (num * z_squared) / ONE_20;
|
|
438
|
+
seriesSum += num / 5;
|
|
439
|
+
|
|
440
|
+
num = (num * z_squared) / ONE_20;
|
|
441
|
+
seriesSum += num / 7;
|
|
442
|
+
|
|
443
|
+
num = (num * z_squared) / ONE_20;
|
|
444
|
+
seriesSum += num / 9;
|
|
445
|
+
|
|
446
|
+
num = (num * z_squared) / ONE_20;
|
|
447
|
+
seriesSum += num / 11;
|
|
448
|
+
|
|
449
|
+
// 6 Taylor terms are sufficient for 36 decimal precision.
|
|
450
|
+
|
|
451
|
+
// Finally, we multiply by 2 (non fixed point) to compute ln(remainder)
|
|
452
|
+
seriesSum *= 2;
|
|
453
|
+
|
|
454
|
+
// We now have the sum of all x_n present, and the Taylor approximation of the logarithm of the remainder (both
|
|
455
|
+
// with 20 decimals). All that remains is to sum these two, and then drop two digits to return a 18 decimal
|
|
456
|
+
// value.
|
|
457
|
+
|
|
458
|
+
return (sum + seriesSum) / 100;
|
|
459
|
+
}
|
|
460
|
+
|
|
461
|
+
/**
|
|
462
|
+
* @dev Intrnal high precision (36 decimal places) natural logarithm (ln(x)) with signed 18 decimal fixed point argument,
|
|
463
|
+
* for x close to one.
|
|
464
|
+
*
|
|
465
|
+
* Should only be used if x is between LN_36_LOWER_BOUND and LN_36_UPPER_BOUND.
|
|
466
|
+
*/
|
|
467
|
+
function _ln_36(int256 x) private pure returns (int256) {
|
|
468
|
+
// Since ln(1) = 0, a value of x close to one will yield a very small result, which makes using 36 digits
|
|
469
|
+
// worthwhile.
|
|
470
|
+
|
|
471
|
+
// First, we transform x to a 36 digit fixed point value.
|
|
472
|
+
x *= ONE_18;
|
|
473
|
+
|
|
474
|
+
// We will use the following Taylor expansion, which converges very rapidly. Let z = (x - 1) / (x + 1).
|
|
475
|
+
// ln(x) = 2 * (z + z^3 / 3 + z^5 / 5 + z^7 / 7 + ... + z^(2 * n + 1) / (2 * n + 1))
|
|
476
|
+
|
|
477
|
+
// Recall that 36 digit fixed point division requires multiplying by ONE_36, and multiplication requires
|
|
478
|
+
// division by ONE_36.
|
|
479
|
+
int256 z = ((x - ONE_36) * ONE_36) / (x + ONE_36);
|
|
480
|
+
int256 z_squared = (z * z) / ONE_36;
|
|
481
|
+
|
|
482
|
+
// num is the numerator of the series: the z^(2 * n + 1) term
|
|
483
|
+
int256 num = z;
|
|
484
|
+
|
|
485
|
+
// seriesSum holds the accumulated sum of each term in the series, starting with the initial z
|
|
486
|
+
int256 seriesSum = num;
|
|
487
|
+
|
|
488
|
+
// In each step, the numerator is multiplied by z^2
|
|
489
|
+
num = (num * z_squared) / ONE_36;
|
|
490
|
+
seriesSum += num / 3;
|
|
491
|
+
|
|
492
|
+
num = (num * z_squared) / ONE_36;
|
|
493
|
+
seriesSum += num / 5;
|
|
494
|
+
|
|
495
|
+
num = (num * z_squared) / ONE_36;
|
|
496
|
+
seriesSum += num / 7;
|
|
497
|
+
|
|
498
|
+
num = (num * z_squared) / ONE_36;
|
|
499
|
+
seriesSum += num / 9;
|
|
500
|
+
|
|
501
|
+
num = (num * z_squared) / ONE_36;
|
|
502
|
+
seriesSum += num / 11;
|
|
503
|
+
|
|
504
|
+
num = (num * z_squared) / ONE_36;
|
|
505
|
+
seriesSum += num / 13;
|
|
506
|
+
|
|
507
|
+
num = (num * z_squared) / ONE_36;
|
|
508
|
+
seriesSum += num / 15;
|
|
509
|
+
|
|
510
|
+
// 8 Taylor terms are sufficient for 36 decimal precision.
|
|
511
|
+
|
|
512
|
+
// All that remains is multiplying by 2 (non fixed point).
|
|
513
|
+
return seriesSum * 2;
|
|
514
|
+
}
|
|
515
|
+
}
|
|
@@ -0,0 +1,140 @@
|
|
|
1
|
+
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
2
|
+
pragma solidity 0.8.9;
|
|
3
|
+
|
|
4
|
+
import "./FixedPoint.sol";
|
|
5
|
+
import "./MarketMathLib.sol";
|
|
6
|
+
|
|
7
|
+
// solhint-disable reason-string, ordering
|
|
8
|
+
library MarketApproxLib {
|
|
9
|
+
using FixedPoint for uint256;
|
|
10
|
+
using FixedPoint for int256;
|
|
11
|
+
using LogExpMath for int256;
|
|
12
|
+
using SCYIndexLib for SCYIndex;
|
|
13
|
+
using MarketMathLib for MarketParameters;
|
|
14
|
+
|
|
15
|
+
struct ApproxSwapExactScyForYtSlot {
|
|
16
|
+
uint256 low;
|
|
17
|
+
uint256 high;
|
|
18
|
+
bool isAcceptableAnswerExisted;
|
|
19
|
+
uint256 currentYtOutGuess;
|
|
20
|
+
uint256 otToMarket;
|
|
21
|
+
uint256 scyReceived;
|
|
22
|
+
uint256 totalScyToMintYo;
|
|
23
|
+
uint256 netYoFromScy;
|
|
24
|
+
bool isResultAcceptable;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
function approxSwapExactScyForOt(
|
|
28
|
+
MarketParameters memory marketImmutable,
|
|
29
|
+
SCYIndex index,
|
|
30
|
+
uint256 exactScyIn,
|
|
31
|
+
uint256 blockTime,
|
|
32
|
+
uint256 netOtOutGuessMin,
|
|
33
|
+
uint256 netOtOutGuessMax
|
|
34
|
+
) internal pure returns (uint256 netOtOut) {
|
|
35
|
+
require(exactScyIn > 0, "invalid scy in");
|
|
36
|
+
require(
|
|
37
|
+
netOtOutGuessMin >= 0 && netOtOutGuessMax >= 0 && netOtOutGuessMin <= netOtOutGuessMax,
|
|
38
|
+
"invalid guess"
|
|
39
|
+
);
|
|
40
|
+
|
|
41
|
+
uint256 low = netOtOutGuessMin;
|
|
42
|
+
uint256 high = netOtOutGuessMax;
|
|
43
|
+
bool isAcceptableAnswerExisted;
|
|
44
|
+
|
|
45
|
+
while (low != high) {
|
|
46
|
+
uint256 currentOtOutGuess = (low + high + 1) / 2;
|
|
47
|
+
MarketParameters memory market = MarketMathLib.deepCloneMarket(marketImmutable);
|
|
48
|
+
|
|
49
|
+
(uint256 netScyNeed, ) = market.swapScyForExactOt(index, currentOtOutGuess, blockTime);
|
|
50
|
+
bool isResultAcceptable = (netScyNeed <= exactScyIn);
|
|
51
|
+
if (isResultAcceptable) {
|
|
52
|
+
low = currentOtOutGuess;
|
|
53
|
+
isAcceptableAnswerExisted = true;
|
|
54
|
+
} else high = currentOtOutGuess - 1;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
require(isAcceptableAnswerExisted, "guess fail");
|
|
58
|
+
netOtOut = low;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
function approxSwapOtForExactScy(
|
|
62
|
+
MarketParameters memory marketImmutable,
|
|
63
|
+
SCYIndex index,
|
|
64
|
+
uint256 exactScyOut,
|
|
65
|
+
uint256 blockTime,
|
|
66
|
+
uint256 netOtInGuessMin,
|
|
67
|
+
uint256 netOtInGuessMax
|
|
68
|
+
) internal pure returns (uint256 netOtIn) {
|
|
69
|
+
require(exactScyOut > 0, "invalid scy in");
|
|
70
|
+
require(
|
|
71
|
+
netOtInGuessMin >= 0 && netOtInGuessMax >= 0 && netOtInGuessMin <= netOtInGuessMax,
|
|
72
|
+
"invalid guess"
|
|
73
|
+
);
|
|
74
|
+
|
|
75
|
+
uint256 low = netOtInGuessMin;
|
|
76
|
+
uint256 high = netOtInGuessMax;
|
|
77
|
+
bool isAcceptableAnswerExisted;
|
|
78
|
+
|
|
79
|
+
while (low != high) {
|
|
80
|
+
uint256 currentOtInGuess = (low + high) / 2;
|
|
81
|
+
MarketParameters memory market = MarketMathLib.deepCloneMarket(marketImmutable);
|
|
82
|
+
|
|
83
|
+
(uint256 netScyToAccount, ) = market.swapExactOtForScy(
|
|
84
|
+
index,
|
|
85
|
+
currentOtInGuess,
|
|
86
|
+
blockTime
|
|
87
|
+
);
|
|
88
|
+
bool isResultAcceptable = (netScyToAccount >= exactScyOut);
|
|
89
|
+
if (isResultAcceptable) {
|
|
90
|
+
high = currentOtInGuess;
|
|
91
|
+
isAcceptableAnswerExisted = true;
|
|
92
|
+
} else {
|
|
93
|
+
low = currentOtInGuess + 1;
|
|
94
|
+
}
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
require(isAcceptableAnswerExisted, "guess fail");
|
|
98
|
+
netOtIn = high;
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
function approxSwapExactScyForYt(
|
|
102
|
+
MarketParameters memory marketImmutable,
|
|
103
|
+
SCYIndex index,
|
|
104
|
+
uint256 exactScyIn,
|
|
105
|
+
uint256 blockTime,
|
|
106
|
+
uint256 netYtOutGuessMin,
|
|
107
|
+
uint256 netYtOutGuessMax
|
|
108
|
+
) internal pure returns (uint256 netYtOut) {
|
|
109
|
+
require(exactScyIn > 0, "invalid scy in");
|
|
110
|
+
require(netYtOutGuessMin >= 0 && netYtOutGuessMax >= 0, "invalid guess");
|
|
111
|
+
|
|
112
|
+
ApproxSwapExactScyForYtSlot memory slot;
|
|
113
|
+
|
|
114
|
+
slot.low = netYtOutGuessMin;
|
|
115
|
+
slot.high = netYtOutGuessMax;
|
|
116
|
+
|
|
117
|
+
while (slot.low != slot.high) {
|
|
118
|
+
slot.currentYtOutGuess = (slot.low + slot.high + 1) / 2;
|
|
119
|
+
MarketParameters memory market = MarketMathLib.deepCloneMarket(marketImmutable);
|
|
120
|
+
|
|
121
|
+
slot.otToMarket = slot.currentYtOutGuess;
|
|
122
|
+
|
|
123
|
+
(slot.scyReceived, ) = market.swapExactOtForScy(index, slot.otToMarket, blockTime);
|
|
124
|
+
|
|
125
|
+
slot.totalScyToMintYo = slot.scyReceived + exactScyIn;
|
|
126
|
+
|
|
127
|
+
slot.netYoFromScy = index.scyToAsset(slot.totalScyToMintYo);
|
|
128
|
+
|
|
129
|
+
bool isResultAcceptable = (slot.netYoFromScy >= slot.currentYtOutGuess);
|
|
130
|
+
|
|
131
|
+
if (isResultAcceptable) {
|
|
132
|
+
slot.low = slot.currentYtOutGuess;
|
|
133
|
+
slot.isAcceptableAnswerExisted = true;
|
|
134
|
+
} else slot.high = slot.currentYtOutGuess - 1;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
require(slot.isAcceptableAnswerExisted, "guess fail");
|
|
138
|
+
netYtOut = slot.low;
|
|
139
|
+
}
|
|
140
|
+
}
|