@pendle/core-v2 0.5.1-beta-4 → 0.6.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/contracts/SuperComposableYield/ISuperComposableYield.sol +2 -0
- package/contracts/SuperComposableYield/implementations/RewardManager.sol +35 -20
- package/contracts/SuperComposableYield/implementations/SCYBase.sol +39 -19
- package/contracts/SuperComposableYield/implementations/SCYBaseWithRewards.sol +26 -14
- package/contracts/core/PendleBaseToken.sol +2 -2
- package/contracts/core/PendleMarket.sol +145 -61
- package/contracts/core/PendleMarketFactory.sol +30 -19
- package/contracts/core/{PendleOwnershipToken.sol → PendlePrincipalToken.sol} +14 -2
- package/contracts/core/PendleRouter.sol +23 -9
- package/contracts/core/PendleSCYImpl/AaveV3/PendleAaveV3SCY.sol +21 -20
- package/contracts/core/PendleSCYImpl/PendleBenQiErc20SCY.sol +22 -16
- package/contracts/core/PendleSCYImpl/PendleBtrflySCY.sol +13 -13
- package/contracts/core/PendleSCYImpl/PendleStETHSCY.sol +107 -0
- package/contracts/core/PendleSCYImpl/PendleYearnVaultSCY.sol +14 -15
- package/contracts/core/PendleYieldContractFactory.sol +29 -14
- package/contracts/core/PendleYieldToken.sol +130 -76
- package/contracts/core/RouterStatic.sol +16 -157
- package/contracts/core/actions/ActionCallback.sol +36 -18
- package/contracts/core/actions/ActionCore.sol +41 -88
- package/contracts/core/actions/ActionRedeem.sol +36 -0
- package/contracts/core/actions/ActionYT.sol +41 -59
- package/contracts/core/actions/base/{ActionSCYAndOTBase.sol → ActionSCYAndPTBase.sol} +59 -45
- package/contracts/core/actions/base/{ActionSCYAndYOBase.sol → ActionSCYAndPYBase.sol} +57 -19
- package/contracts/core/actions/base/ActionSCYAndYTBase.sol +31 -21
- package/contracts/interfaces/IPActionCore.sol +21 -28
- package/contracts/interfaces/IPActionRedeem.sol +17 -0
- package/contracts/interfaces/IPActionYT.sol +5 -12
- package/contracts/interfaces/IPAllAction.sol +2 -2
- package/contracts/interfaces/IPMarket.sol +33 -10
- package/contracts/interfaces/IPMarketAddRemoveCallback.sol +2 -2
- package/contracts/interfaces/IPMarketFactory.sol +3 -1
- package/contracts/interfaces/IPMarketSwapCallback.sol +1 -1
- package/contracts/interfaces/{IPOwnershipToken.sol → IPPrincipalToken.sol} +1 -1
- package/contracts/interfaces/IPRouterStatic.sol +7 -82
- package/contracts/interfaces/IPYieldContractFactory.sol +10 -2
- package/contracts/interfaces/IPYieldToken.sol +14 -5
- package/contracts/interfaces/IWstETH.sol +10 -0
- package/contracts/libraries/JoeLibrary.sol +1 -1
- package/contracts/libraries/math/LogExpMath.sol +1 -1
- package/contracts/libraries/math/MarketApproxLib.sol +128 -88
- package/contracts/libraries/math/MarketMathAux.sol +13 -13
- package/contracts/libraries/math/MarketMathCore.sol +62 -93
- package/contracts/libraries/math/Math.sol +14 -2
- package/contracts/periphery/PendleGovernanceManager.sol +5 -3
- package/package.json +3 -2
- package/typechain-types/ActionCallback.ts +5 -5
- package/typechain-types/ActionCore.ts +312 -234
- package/typechain-types/ActionRedeem.ts +125 -0
- package/typechain-types/ActionSCYAndPYBase.ts +223 -0
- package/typechain-types/ActionSCYAndYOBase.ts +129 -3
- package/typechain-types/ActionSCYAndYTBase.ts +149 -3
- package/typechain-types/ActionYT.ts +192 -94
- package/typechain-types/IPActionCore.ts +183 -231
- package/typechain-types/IPActionRedeem.ts +125 -0
- package/typechain-types/IPActionYT.ts +43 -91
- package/typechain-types/IPAllAction.ts +258 -321
- package/typechain-types/IPMarket.ts +150 -50
- package/typechain-types/IPMarketAddRemoveCallback.ts +10 -10
- package/typechain-types/IPMarketFactory.ts +28 -6
- package/typechain-types/IPMarketSwapCallback.ts +5 -5
- package/typechain-types/{IPOwnershipToken.ts → IPPrincipalToken.ts} +5 -5
- package/typechain-types/IPRouterStatic.ts +50 -530
- package/typechain-types/IPYieldContractFactory.ts +85 -19
- package/typechain-types/IPYieldToken.ts +138 -37
- package/typechain-types/ISuperComposableYield.ts +13 -0
- package/typechain-types/IWstETH.ts +149 -0
- package/typechain-types/PendleAaveV3SCY.ts +110 -0
- package/typechain-types/PendleBaseToken.ts +1 -1
- package/typechain-types/PendleBenQiErc20SCY.ts +55 -12
- package/typechain-types/PendleBtrflyScy.ts +27 -23
- package/typechain-types/PendleGovernanceManager.ts +5 -5
- package/typechain-types/PendleMarket.ts +197 -100
- package/typechain-types/PendleMarketFactory.ts +96 -34
- package/typechain-types/{PendleOwnershipToken.ts → PendlePrincipalToken.ts} +15 -6
- package/typechain-types/PendleRouter.ts +19 -0
- package/typechain-types/PendleRouterRedeemUpg.ts +125 -0
- package/typechain-types/PendleStEthSCY.ts +872 -0
- package/typechain-types/PendleYearnVaultScy.ts +27 -23
- package/typechain-types/PendleYieldContractFactory.ts +94 -28
- package/typechain-types/PendleYieldToken.ts +142 -57
- package/typechain-types/RewardManager.ts +21 -0
- package/typechain-types/RouterStatic.ts +50 -530
- package/typechain-types/SCYBase.ts +25 -0
- package/typechain-types/SCYBaseWithRewards.ts +64 -0
- package/typechain-types/factories/ActionCallback__factory.ts +2 -2
- package/typechain-types/factories/ActionCore__factory.ts +263 -79
- package/typechain-types/factories/ActionRedeem__factory.ts +105 -0
- package/typechain-types/factories/ActionSCYAndPYBase__factory.ts +200 -0
- package/typechain-types/factories/ActionSCYAndYOBase__factory.ts +148 -0
- package/typechain-types/factories/ActionSCYAndYTBase__factory.ts +173 -0
- package/typechain-types/factories/ActionYT__factory.ts +265 -56
- package/typechain-types/factories/IPActionCore__factory.ts +114 -78
- package/typechain-types/factories/IPActionRedeem__factory.ts +70 -0
- package/typechain-types/factories/IPActionYT__factory.ts +90 -54
- package/typechain-types/factories/IPAllAction__factory.ts +254 -138
- package/typechain-types/factories/IPMarketAddRemoveCallback__factory.ts +2 -2
- package/typechain-types/factories/IPMarketFactory__factory.ts +26 -1
- package/typechain-types/factories/IPMarketSwapCallback__factory.ts +1 -1
- package/typechain-types/factories/IPMarket__factory.ts +125 -13
- package/typechain-types/factories/{IPOwnershipToken__factory.ts → IPPrincipalToken__factory.ts} +8 -8
- package/typechain-types/factories/IPRouterStatic__factory.ts +12 -628
- package/typechain-types/factories/IPYieldContractFactory__factory.ts +72 -2
- package/typechain-types/factories/IPYieldToken__factory.ts +93 -7
- package/typechain-types/factories/ISuperComposableYield__factory.ts +13 -0
- package/typechain-types/factories/IWstETH__factory.ts +74 -0
- package/typechain-types/factories/PendleAaveV3SCY__factory.ts +97 -3
- package/typechain-types/factories/PendleBaseToken__factory.ts +1 -1
- package/typechain-types/factories/PendleBenQiErc20SCY__factory.ts +62 -7
- package/typechain-types/factories/PendleBtrflyScy__factory.ts +42 -19
- package/typechain-types/factories/PendleGovernanceManager__factory.ts +2 -2
- package/typechain-types/factories/PendleMarketFactory__factory.ts +66 -15
- package/typechain-types/factories/PendleMarket__factory.ts +170 -53
- package/typechain-types/factories/PendlePrincipalToken__factory.ts +540 -0
- package/typechain-types/factories/PendleRouterRedeemUpg__factory.ts +112 -0
- package/typechain-types/factories/PendleRouter__factory.ts +23 -1
- package/typechain-types/factories/PendleStEthSCY__factory.ts +716 -0
- package/typechain-types/factories/PendleYearnVaultScy__factory.ts +41 -18
- package/typechain-types/factories/PendleYieldContractFactory__factory.ts +75 -5
- package/typechain-types/factories/PendleYieldToken__factory.ts +102 -33
- package/typechain-types/factories/RewardManager__factory.ts +13 -0
- package/typechain-types/factories/RouterStatic__factory.ts +13 -629
- package/typechain-types/factories/SCYBaseWithRewards__factory.ts +60 -2
- package/typechain-types/factories/SCYBase__factory.ts +28 -2
- package/typechain-types/hardhat.d.ts +52 -16
- package/typechain-types/index.ts +14 -6
- package/typechain-types/factories/PendleOwnershipToken__factory.ts +0 -527
|
@@ -8,8 +8,24 @@ import "./MarketMathCore.sol";
|
|
|
8
8
|
struct ApproxParams {
|
|
9
9
|
uint256 guessMin;
|
|
10
10
|
uint256 guessMax;
|
|
11
|
-
uint256
|
|
12
|
-
uint256
|
|
11
|
+
uint256 guessOffchain; // pass 0 in to skip this variable
|
|
12
|
+
uint256 maxIteration; // every iteration, the diff between guessMin and guessMax will be divided by 2
|
|
13
|
+
uint256 eps; // the max eps between the returned result & the correct result, base 1e18. Normally this number will be set
|
|
14
|
+
// to 1e15 (1e18/1000 = 0.1%)
|
|
15
|
+
|
|
16
|
+
/// Further explanation of the eps. Take swapExactScyForPt for example. To calc the corresponding amount of Pt to swap out,
|
|
17
|
+
/// it's necessary to run an approximation algorithm, because by default there only exists the Pt to Scy formula
|
|
18
|
+
/// To approx, the 5 values above will have to be provided, and the approx process will run as follows:
|
|
19
|
+
/// mid = (guessMin + guessMax) / 2 // mid here is the current guess of the amount of Pt out
|
|
20
|
+
/// netScyNeed = calcSwapScyForExactPt(mid)
|
|
21
|
+
/// if (netScyNeed > exactScyIn) guessMax = mid - 1 // since the maximum Scy in can't exceed the exactScyIn
|
|
22
|
+
/// else guessMin = mid (1)
|
|
23
|
+
/// For the (1), since netScyNeed <= exactScyIn, the result might be usable. If the netScyNeed is within eps of
|
|
24
|
+
/// exactScyIn (ex eps=0.1% => we have used 99.9% the amount of Scy specified), mid will be chosen as the final guess result
|
|
25
|
+
|
|
26
|
+
/// for guessOffchain, this is to provide a shortcut to guessing. The offchain SDK can precalculate the exact result
|
|
27
|
+
/// before the tx is sent. When the tx reaches the contract, the guessOffchain will be checked first, and if it satisfies the
|
|
28
|
+
/// approximation, it will be used (and save all the guessing).
|
|
13
29
|
}
|
|
14
30
|
|
|
15
31
|
// solhint-disable reason-string, ordering
|
|
@@ -20,7 +36,13 @@ library MarketApproxLib {
|
|
|
20
36
|
using SCYIndexLib for SCYIndex;
|
|
21
37
|
using MarketMathCore for MarketState;
|
|
22
38
|
|
|
23
|
-
|
|
39
|
+
struct SlotSwapPtForExactScy {
|
|
40
|
+
uint256 ptInGuess;
|
|
41
|
+
int256 _assetToAccount;
|
|
42
|
+
uint256 netAssetOut;
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
function approxSwapPtForExactScy(
|
|
24
46
|
MarketState memory market,
|
|
25
47
|
SCYIndex index,
|
|
26
48
|
uint256 minScyOut,
|
|
@@ -30,7 +52,7 @@ library MarketApproxLib {
|
|
|
30
52
|
internal
|
|
31
53
|
pure
|
|
32
54
|
returns (
|
|
33
|
-
uint256, /*
|
|
55
|
+
uint256, /*netPtIn*/
|
|
34
56
|
uint256 /*netScyOut*/
|
|
35
57
|
)
|
|
36
58
|
{
|
|
@@ -50,55 +72,59 @@ library MarketApproxLib {
|
|
|
50
72
|
MarketPreCompute memory comp = market.getMarketPreCompute(index, blockTime);
|
|
51
73
|
|
|
52
74
|
if (approx.guessMax == type(uint256).max) {
|
|
53
|
-
approx.guessMax =
|
|
75
|
+
approx.guessMax = calcMaxPtIn(market.totalPt, comp.totalAsset);
|
|
54
76
|
}
|
|
55
77
|
|
|
56
78
|
/// ------------------------------------------------------------
|
|
57
79
|
/// BINARY SEARCH
|
|
58
80
|
/// ------------------------------------------------------------
|
|
59
81
|
|
|
60
|
-
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
82
|
+
for (uint256 iter = 0; iter < approx.maxIteration; ) {
|
|
83
|
+
// ++iter at end of loop
|
|
84
|
+
|
|
85
|
+
SlotSwapPtForExactScy memory slot;
|
|
86
|
+
slot.ptInGuess = getCurrentGuess(iter, approx);
|
|
65
87
|
|
|
66
88
|
/// ------------------------------------------------------------
|
|
67
89
|
/// CHECK SLOPE
|
|
68
90
|
/// ------------------------------------------------------------
|
|
69
91
|
(isSlopeNonNeg, largestGoodSlope) = updateSlope(
|
|
70
|
-
market.
|
|
71
|
-
|
|
92
|
+
market.totalPt,
|
|
93
|
+
slot.ptInGuess,
|
|
72
94
|
comp,
|
|
73
95
|
largestGoodSlope
|
|
74
96
|
);
|
|
75
97
|
if (!isSlopeNonNeg) {
|
|
76
|
-
approx.guessMax =
|
|
98
|
+
approx.guessMax = slot.ptInGuess;
|
|
77
99
|
continue;
|
|
78
100
|
}
|
|
79
101
|
|
|
80
102
|
/// ------------------------------------------------------------
|
|
81
103
|
/// CHECK ASSET
|
|
82
104
|
/// ------------------------------------------------------------
|
|
83
|
-
(
|
|
84
|
-
|
|
105
|
+
(slot._assetToAccount, ) = market.calcTrade(comp, slot.ptInGuess.neg());
|
|
106
|
+
slot.netAssetOut = slot._assetToAccount.Uint();
|
|
85
107
|
|
|
86
|
-
if (netAssetOut >= minAssetOut) {
|
|
87
|
-
approx.guessMax =
|
|
108
|
+
if (slot.netAssetOut >= minAssetOut) {
|
|
109
|
+
approx.guessMax = slot.ptInGuess;
|
|
88
110
|
/// ------------------------------------------------------------
|
|
89
111
|
/// CHECK IF ANSWER FOUND
|
|
90
112
|
/// ------------------------------------------------------------
|
|
91
|
-
if (Math.isAGreaterApproxB(netAssetOut, minAssetOut, approx.eps)) {
|
|
92
|
-
return (
|
|
113
|
+
if (Math.isAGreaterApproxB(slot.netAssetOut, minAssetOut, approx.eps)) {
|
|
114
|
+
return (slot.ptInGuess, index.assetToScy(slot.netAssetOut));
|
|
93
115
|
}
|
|
94
116
|
} else {
|
|
95
|
-
approx.guessMin =
|
|
117
|
+
approx.guessMin = slot.ptInGuess + 1;
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
unchecked {
|
|
121
|
+
++iter;
|
|
96
122
|
}
|
|
97
123
|
}
|
|
98
124
|
revert("approx fail");
|
|
99
125
|
}
|
|
100
126
|
|
|
101
|
-
function
|
|
127
|
+
function approxSwapExactScyForPt(
|
|
102
128
|
MarketState memory market,
|
|
103
129
|
SCYIndex index,
|
|
104
130
|
uint256 maxScyIn,
|
|
@@ -108,7 +134,7 @@ library MarketApproxLib {
|
|
|
108
134
|
internal
|
|
109
135
|
pure
|
|
110
136
|
returns (
|
|
111
|
-
uint256, /*
|
|
137
|
+
uint256, /*netPtOut*/
|
|
112
138
|
uint256 /*netScyIn*/
|
|
113
139
|
)
|
|
114
140
|
{
|
|
@@ -126,42 +152,43 @@ library MarketApproxLib {
|
|
|
126
152
|
MarketPreCompute memory comp = market.getMarketPreCompute(index, blockTime);
|
|
127
153
|
|
|
128
154
|
if (approx.guessMax == type(uint256).max) {
|
|
129
|
-
approx.guessMax =
|
|
155
|
+
approx.guessMax = calcMaxPtOut(market.totalPt, comp);
|
|
130
156
|
}
|
|
131
157
|
|
|
132
158
|
/// ------------------------------------------------------------
|
|
133
159
|
/// BINARY SEARCH
|
|
134
160
|
/// ------------------------------------------------------------
|
|
135
161
|
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
approx.maxIteration--;
|
|
139
|
-
}
|
|
140
|
-
uint256 otOutGuess = (approx.guessMin + approx.guessMax + 1) / 2;
|
|
162
|
+
for (uint256 iter = 0; iter < approx.maxIteration; ) {
|
|
163
|
+
uint256 ptOutGuess = getCurrentGuess(iter, approx);
|
|
141
164
|
|
|
142
165
|
/// ------------------------------------------------------------
|
|
143
166
|
/// CHECK ASSET
|
|
144
167
|
/// ------------------------------------------------------------
|
|
145
|
-
(int256 _assetToAccount, ) = market.calcTrade(comp,
|
|
168
|
+
(int256 _assetToAccount, ) = market.calcTrade(comp, ptOutGuess.Int());
|
|
146
169
|
uint256 netAssetIn = _assetToAccount.neg().Uint();
|
|
147
170
|
|
|
148
171
|
if (netAssetIn <= maxAssetIn) {
|
|
149
|
-
approx.guessMin =
|
|
172
|
+
approx.guessMin = ptOutGuess;
|
|
150
173
|
/// ------------------------------------------------------------
|
|
151
174
|
/// CHECK IF ANSWER FOUND
|
|
152
175
|
/// ------------------------------------------------------------
|
|
153
176
|
if (Math.isASmallerApproxB(netAssetIn, maxAssetIn, approx.eps)) {
|
|
154
|
-
return (
|
|
177
|
+
return (ptOutGuess, index.assetToScy(netAssetIn));
|
|
155
178
|
}
|
|
156
179
|
} else {
|
|
157
|
-
approx.guessMax =
|
|
180
|
+
approx.guessMax = ptOutGuess - 1;
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
unchecked {
|
|
184
|
+
++iter;
|
|
158
185
|
}
|
|
159
186
|
}
|
|
160
187
|
revert("approx fail");
|
|
161
188
|
}
|
|
162
189
|
|
|
163
190
|
struct SlotSwapExactScyForYt {
|
|
164
|
-
uint256
|
|
191
|
+
uint256 ptInGuess;
|
|
165
192
|
int256 _assetToAccount;
|
|
166
193
|
uint256 netAssetOut;
|
|
167
194
|
uint256 amountAssetNeedMore;
|
|
@@ -197,53 +224,56 @@ library MarketApproxLib {
|
|
|
197
224
|
MarketPreCompute memory comp = market.getMarketPreCompute(index, blockTime);
|
|
198
225
|
|
|
199
226
|
if (approx.guessMax == type(uint256).max) {
|
|
200
|
-
approx.guessMax =
|
|
227
|
+
approx.guessMax = calcMaxPtIn(market.totalPt, comp.totalAsset);
|
|
201
228
|
}
|
|
202
229
|
|
|
203
230
|
/// ------------------------------------------------------------
|
|
204
231
|
/// BINARY SEARCH
|
|
205
232
|
/// ------------------------------------------------------------
|
|
206
233
|
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
approx.maxIteration--;
|
|
210
|
-
}
|
|
234
|
+
for (uint256 iter = 0; iter < approx.maxIteration; ) {
|
|
235
|
+
// iter++ at end of loop
|
|
211
236
|
|
|
212
237
|
SlotSwapExactScyForYt memory slot;
|
|
213
|
-
// ytOutGuess =
|
|
214
|
-
|
|
238
|
+
// ytOutGuess = ptInGuess
|
|
239
|
+
|
|
240
|
+
slot.ptInGuess = getCurrentGuess(iter, approx);
|
|
215
241
|
|
|
216
242
|
/// ------------------------------------------------------------
|
|
217
243
|
/// CHECK SLOPE
|
|
218
244
|
/// ------------------------------------------------------------
|
|
219
245
|
(isSlopeNonNeg, largestGoodSlope) = updateSlope(
|
|
220
|
-
market.
|
|
221
|
-
slot.
|
|
246
|
+
market.totalPt,
|
|
247
|
+
slot.ptInGuess,
|
|
222
248
|
comp,
|
|
223
249
|
largestGoodSlope
|
|
224
250
|
);
|
|
225
251
|
if (!isSlopeNonNeg) {
|
|
226
|
-
approx.guessMax = slot.
|
|
252
|
+
approx.guessMax = slot.ptInGuess - 1;
|
|
227
253
|
continue;
|
|
228
254
|
}
|
|
229
255
|
|
|
230
256
|
/// ------------------------------------------------------------
|
|
231
257
|
/// CHECK ASSET
|
|
232
258
|
/// ------------------------------------------------------------
|
|
233
|
-
(slot._assetToAccount, ) = market.calcTrade(comp, slot.
|
|
259
|
+
(slot._assetToAccount, ) = market.calcTrade(comp, slot.ptInGuess.neg());
|
|
234
260
|
slot.netAssetOut = slot._assetToAccount.Uint();
|
|
235
|
-
slot.amountAssetNeedMore = slot.
|
|
261
|
+
slot.amountAssetNeedMore = slot.ptInGuess - slot.netAssetOut;
|
|
236
262
|
|
|
237
263
|
if (slot.amountAssetNeedMore <= maxAssetIn) {
|
|
238
|
-
approx.guessMin = slot.
|
|
264
|
+
approx.guessMin = slot.ptInGuess;
|
|
239
265
|
/// ------------------------------------------------------------
|
|
240
266
|
/// CHECK IF ANSWER FOUND
|
|
241
267
|
/// ------------------------------------------------------------
|
|
242
268
|
if (Math.isASmallerApproxB(slot.amountAssetNeedMore, maxAssetIn, approx.eps)) {
|
|
243
|
-
return (slot.
|
|
269
|
+
return (slot.ptInGuess, index.assetToScy(slot.amountAssetNeedMore));
|
|
244
270
|
}
|
|
245
271
|
} else {
|
|
246
|
-
approx.guessMax = slot.
|
|
272
|
+
approx.guessMax = slot.ptInGuess - 1;
|
|
273
|
+
}
|
|
274
|
+
|
|
275
|
+
unchecked {
|
|
276
|
+
++iter;
|
|
247
277
|
}
|
|
248
278
|
}
|
|
249
279
|
revert("approx fail");
|
|
@@ -277,106 +307,116 @@ library MarketApproxLib {
|
|
|
277
307
|
MarketPreCompute memory comp = market.getMarketPreCompute(index, blockTime);
|
|
278
308
|
|
|
279
309
|
if (approx.guessMax == type(uint256).max) {
|
|
280
|
-
approx.guessMax =
|
|
310
|
+
approx.guessMax = calcMaxPtOut(market.totalPt, comp);
|
|
281
311
|
}
|
|
282
312
|
|
|
283
313
|
/// ------------------------------------------------------------
|
|
284
314
|
/// BINARY SEARCH
|
|
285
315
|
/// ------------------------------------------------------------
|
|
286
316
|
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
uint256 otOutGuess = (approx.guessMin + approx.guessMax) / 2;
|
|
317
|
+
for (uint256 iter = 0; iter < approx.maxIteration; ) {
|
|
318
|
+
// iter++ at end of loop
|
|
319
|
+
|
|
320
|
+
// ytInGuess = ptOutGuess
|
|
321
|
+
uint256 ptOutGuess = getCurrentGuess(iter, approx);
|
|
293
322
|
|
|
294
323
|
/// ------------------------------------------------------------
|
|
295
324
|
/// CHECK ASSET
|
|
296
325
|
/// ------------------------------------------------------------
|
|
297
|
-
(int256 _assetToAccount, ) = market.calcTrade(comp,
|
|
326
|
+
(int256 _assetToAccount, ) = market.calcTrade(comp, ptOutGuess.Int());
|
|
298
327
|
uint256 netAssetOwed = _assetToAccount.neg().Uint();
|
|
299
328
|
|
|
300
|
-
// since
|
|
301
|
-
// there are enough Yt to pair with
|
|
329
|
+
// since ptOutGuess is between guessMin & guessMax, it's guaranteed that
|
|
330
|
+
// there are enough Yt to pair with ptOut
|
|
302
331
|
|
|
303
|
-
uint256 netAssetOut =
|
|
332
|
+
uint256 netAssetOut = ptOutGuess - netAssetOwed;
|
|
304
333
|
|
|
305
334
|
if (netAssetOut >= minAssetOut) {
|
|
306
|
-
approx.guessMax =
|
|
335
|
+
approx.guessMax = ptOutGuess;
|
|
307
336
|
/// ------------------------------------------------------------
|
|
308
337
|
/// CHECK IF ANSWER FOUND
|
|
309
338
|
/// ------------------------------------------------------------
|
|
310
339
|
if (Math.isAGreaterApproxB(netAssetOut, minAssetOut, approx.eps)) {
|
|
311
|
-
return (
|
|
340
|
+
return (ptOutGuess, index.assetToScy(netAssetOut));
|
|
312
341
|
}
|
|
313
342
|
} else {
|
|
314
|
-
approx.guessMin =
|
|
343
|
+
approx.guessMin = ptOutGuess + 1;
|
|
344
|
+
}
|
|
345
|
+
|
|
346
|
+
unchecked {
|
|
347
|
+
++iter;
|
|
315
348
|
}
|
|
316
349
|
}
|
|
317
350
|
revert("approx fail");
|
|
318
351
|
}
|
|
319
352
|
|
|
320
353
|
function updateSlope(
|
|
321
|
-
int256
|
|
322
|
-
uint256
|
|
354
|
+
int256 totalPt,
|
|
355
|
+
uint256 ptInGuess,
|
|
323
356
|
MarketPreCompute memory comp,
|
|
324
357
|
uint256 largestGoodSlope
|
|
325
358
|
) internal pure returns (bool isSlopeNonNeg, uint256 newLargestGoodSlop) {
|
|
326
|
-
if (
|
|
359
|
+
if (ptInGuess <= largestGoodSlope) {
|
|
327
360
|
return (true, largestGoodSlope);
|
|
328
361
|
}
|
|
329
362
|
// it's not guaranteed that the current slop is good
|
|
330
363
|
// we therefore have to recalculate the slope
|
|
331
|
-
int256 slope = slopeFactor(
|
|
332
|
-
if (slope >= 0) return (true,
|
|
364
|
+
int256 slope = slopeFactor(totalPt, ptInGuess.neg(), comp);
|
|
365
|
+
if (slope >= 0) return (true, ptInGuess);
|
|
333
366
|
else return (false, largestGoodSlope);
|
|
334
367
|
}
|
|
335
368
|
|
|
336
|
-
//
|
|
369
|
+
// ptToMarket < totalAsset && totalPt
|
|
337
370
|
function slopeFactor(
|
|
338
|
-
int256
|
|
339
|
-
int256
|
|
371
|
+
int256 totalPt,
|
|
372
|
+
int256 ptToAccount,
|
|
340
373
|
MarketPreCompute memory comp
|
|
341
374
|
) internal pure returns (int256) {
|
|
342
|
-
int256
|
|
343
|
-
int256
|
|
344
|
-
int256
|
|
375
|
+
int256 ptToMarket = -ptToAccount;
|
|
376
|
+
int256 diffAssetPtToMarket = comp.totalAsset - ptToMarket;
|
|
377
|
+
int256 sumPt = ptToMarket + totalPt;
|
|
345
378
|
|
|
346
|
-
require(
|
|
379
|
+
require(diffAssetPtToMarket > 0 && sumPt > 0, "invalid ptToMarket");
|
|
347
380
|
|
|
348
|
-
int256 part1 = (
|
|
349
|
-
|
|
381
|
+
int256 part1 = (ptToMarket * (totalPt + comp.totalAsset)).divDown(
|
|
382
|
+
sumPt * diffAssetPtToMarket
|
|
350
383
|
);
|
|
351
384
|
|
|
352
|
-
int256 part2 =
|
|
385
|
+
int256 part2 = sumPt.divDown(diffAssetPtToMarket).ln();
|
|
353
386
|
int256 part3 = Math.IONE.divDown(comp.rateScalar);
|
|
354
387
|
|
|
355
388
|
return comp.rateAnchor - (part1 - part2).mulDown(part3);
|
|
356
389
|
}
|
|
357
390
|
|
|
358
|
-
function
|
|
391
|
+
function calcMaxPtOut(int256 totalPt, MarketPreCompute memory comp)
|
|
359
392
|
internal
|
|
360
393
|
pure
|
|
361
394
|
returns (uint256)
|
|
362
395
|
{
|
|
363
|
-
int256 logitP = (Math.IONE.mulDown(comp.
|
|
396
|
+
int256 logitP = (Math.IONE.mulDown(comp.lnFeeRate) - comp.rateAnchor)
|
|
364
397
|
.mulDown(comp.rateScalar)
|
|
365
398
|
.exp();
|
|
366
399
|
int256 proportion = logitP.divDown(logitP + Math.IONE);
|
|
367
|
-
int256 numerator = proportion.mulDown(
|
|
368
|
-
int256
|
|
369
|
-
//
|
|
370
|
-
return (
|
|
400
|
+
int256 numerator = proportion.mulDown(totalPt + comp.totalAsset);
|
|
401
|
+
int256 maxPtOut = totalPt - numerator;
|
|
402
|
+
// only get 99.9% of the theoretical max to accommodate some precision issues
|
|
403
|
+
return (maxPtOut.Uint() * 999) / 1000;
|
|
371
404
|
}
|
|
372
405
|
|
|
373
|
-
function
|
|
374
|
-
return Math.min(
|
|
406
|
+
function calcMaxPtIn(int256 totalPt, int256 totalAsset) internal pure returns (uint256) {
|
|
407
|
+
return Math.min(totalPt, totalAsset).Uint() - 1;
|
|
375
408
|
}
|
|
376
409
|
|
|
377
410
|
function isValidApproxParams(ApproxParams memory approx) internal pure returns (bool) {
|
|
378
|
-
return (approx.guessMin <= approx.guessMax &&
|
|
379
|
-
|
|
380
|
-
|
|
411
|
+
return (approx.guessMin <= approx.guessMax && approx.eps <= Math.ONE);
|
|
412
|
+
}
|
|
413
|
+
|
|
414
|
+
function getCurrentGuess(uint256 iteration, ApproxParams memory approx)
|
|
415
|
+
internal
|
|
416
|
+
pure
|
|
417
|
+
returns (uint256)
|
|
418
|
+
{
|
|
419
|
+
if (iteration == 0 && approx.guessOffchain != 0) return approx.guessOffchain;
|
|
420
|
+
else return (approx.guessMin + approx.guessMax) / 2;
|
|
381
421
|
}
|
|
382
422
|
}
|
|
@@ -15,7 +15,7 @@ library MarketMathAux {
|
|
|
15
15
|
MarketState memory market,
|
|
16
16
|
SCYIndex index,
|
|
17
17
|
uint256 scyDesired,
|
|
18
|
-
uint256
|
|
18
|
+
uint256 ptDesired,
|
|
19
19
|
bool updateState
|
|
20
20
|
)
|
|
21
21
|
internal
|
|
@@ -24,7 +24,7 @@ library MarketMathAux {
|
|
|
24
24
|
uint256 lpToReserve,
|
|
25
25
|
uint256 lpToAccount,
|
|
26
26
|
uint256 scyUsed,
|
|
27
|
-
uint256
|
|
27
|
+
uint256 ptUsed
|
|
28
28
|
)
|
|
29
29
|
{
|
|
30
30
|
(
|
|
@@ -36,42 +36,42 @@ library MarketMathAux {
|
|
|
36
36
|
market,
|
|
37
37
|
index,
|
|
38
38
|
scyDesired.Int(),
|
|
39
|
-
|
|
39
|
+
ptDesired.Int(),
|
|
40
40
|
updateState
|
|
41
41
|
);
|
|
42
42
|
|
|
43
43
|
lpToReserve = _lpToReserve.Uint();
|
|
44
44
|
lpToAccount = _lpToAccount.Uint();
|
|
45
45
|
scyUsed = _scyUsed.Uint();
|
|
46
|
-
|
|
46
|
+
ptUsed = _otUsed.Uint();
|
|
47
47
|
}
|
|
48
48
|
|
|
49
49
|
function removeLiquidity(
|
|
50
50
|
MarketState memory market,
|
|
51
51
|
uint256 lpToRemove,
|
|
52
52
|
bool updateState
|
|
53
|
-
) internal pure returns (uint256 scyToAccount, uint256
|
|
54
|
-
(int256 _scyToAccount, int256
|
|
53
|
+
) internal pure returns (uint256 scyToAccount, uint256 netPtToAccount) {
|
|
54
|
+
(int256 _scyToAccount, int256 _ptToAccount) = MarketMathCore.removeLiquidityCore(
|
|
55
55
|
market,
|
|
56
56
|
lpToRemove.Int(),
|
|
57
57
|
updateState
|
|
58
58
|
);
|
|
59
59
|
|
|
60
60
|
scyToAccount = _scyToAccount.Uint();
|
|
61
|
-
|
|
61
|
+
netPtToAccount = _ptToAccount.Uint();
|
|
62
62
|
}
|
|
63
63
|
|
|
64
|
-
function
|
|
64
|
+
function swapExactPtForScy(
|
|
65
65
|
MarketState memory market,
|
|
66
66
|
SCYIndex index,
|
|
67
|
-
uint256
|
|
67
|
+
uint256 exactPtToMarket,
|
|
68
68
|
uint256 blockTime,
|
|
69
69
|
bool updateState
|
|
70
70
|
) internal pure returns (uint256 netScyToAccount, uint256 netScyToReserve) {
|
|
71
71
|
(int256 _netScyToAccount, int256 _netScyToReserve) = MarketMathCore.executeTradeCore(
|
|
72
72
|
market,
|
|
73
73
|
index,
|
|
74
|
-
|
|
74
|
+
exactPtToMarket.neg(),
|
|
75
75
|
blockTime,
|
|
76
76
|
updateState
|
|
77
77
|
);
|
|
@@ -80,17 +80,17 @@ library MarketMathAux {
|
|
|
80
80
|
netScyToReserve = _netScyToReserve.Uint();
|
|
81
81
|
}
|
|
82
82
|
|
|
83
|
-
function
|
|
83
|
+
function swapScyForExactPt(
|
|
84
84
|
MarketState memory market,
|
|
85
85
|
SCYIndex index,
|
|
86
|
-
uint256
|
|
86
|
+
uint256 exactPtToAccount,
|
|
87
87
|
uint256 blockTime,
|
|
88
88
|
bool updateState
|
|
89
89
|
) internal pure returns (uint256 netScyToMarket, uint256 netScyToReserve) {
|
|
90
90
|
(int256 _netScyToAccount, int256 _netScyToReserve) = MarketMathCore.executeTradeCore(
|
|
91
91
|
market,
|
|
92
92
|
index,
|
|
93
|
-
|
|
93
|
+
exactPtToAccount.Int(),
|
|
94
94
|
blockTime,
|
|
95
95
|
updateState
|
|
96
96
|
);
|