-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathYieldMath.sol
747 lines (642 loc) · 39.1 KB
/
YieldMath.sol
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
// SPDX-License-Identifier: GPL-3.0-or-later
pragma solidity >=0.8.15;
/*
__ ___ _ _
\ \ / (_) | | | | ██╗ ██╗██╗███████╗██╗ ██████╗ ███╗ ███╗ █████╗ ████████╗██╗ ██╗
\ \_/ / _ ___| | __| | ╚██╗ ██╔╝██║██╔════╝██║ ██╔══██╗████╗ ████║██╔══██╗╚══██╔══╝██║ ██║
\ / | |/ _ \ |/ _` | ╚████╔╝ ██║█████╗ ██║ ██║ ██║██╔████╔██║███████║ ██║ ███████║
| | | | __/ | (_| | ╚██╔╝ ██║██╔══╝ ██║ ██║ ██║██║╚██╔╝██║██╔══██║ ██║ ██╔══██║
|_| |_|\___|_|\__,_| ██║ ██║███████╗███████╗██████╔╝██║ ╚═╝ ██║██║ ██║ ██║ ██║ ██║
yieldprotocol.com ╚═╝ ╚═╝╚══════╝╚══════╝╚═════╝ ╚═╝ ╚═╝╚═╝ ╚═╝ ╚═╝ ╚═╝ ╚═╝
*/
import {Exp64x64} from "./Exp64x64.sol";
import {Math64x64} from "./Math64x64.sol";
import {CastU256U128} from "@yield-protocol/utils-v2/contracts/cast/CastU256U128.sol";
import {CastU128I128} from "@yield-protocol/utils-v2/contracts/cast/CastU128I128.sol";
/// Ethereum smart contract library implementing Yield Math model with yield bearing tokens.
/// @dev see Mikhail Vladimirov (ABDK) explanations of the math: https://hackmd.io/gbnqA3gCTR6z-F0HHTxF-A#Yield-Math
library YieldMath {
using Math64x64 for int128;
using Math64x64 for uint128;
using Math64x64 for int256;
using Math64x64 for uint256;
using Exp64x64 for uint128;
using Exp64x64 for int128;
using CastU256U128 for uint256;
using CastU128I128 for uint128;
uint128 public constant WAD = 1e18;
uint128 public constant ONE = 0x10000000000000000; // In 64.64
uint256 public constant MAX = type(uint128).max; // Used for overflow checks
/* CORE FUNCTIONS
******************************************************************************************************************/
/* ----------------------------------------------------------------------------------------------------------------
┌───────────────────────────────┐ .-:::::::::::-.
┌──────────────┐ │ │ .:::::::::::::::::.
│$ $│ \│ │/ : _______ __ __ :
│ ┌────────────┴─┐ \│ │/ :: | || | | |::
│ │$ $│ │ fyTokenOutForSharesIn │ ::: | ___|| |_| |:::
│$│ ┌────────────┴─┐ ────────▶ │ │ ────────▶ ::: | |___ | |:::
└─┤ │$ $│ │ │ ::: | ___||_ _|:::
│$│ `sharesIn` │ /│ │\ ::: | | | | :::
└─┤ │ /│ │\ :: |___| |___| ::
│$ $│ │ \(^o^)/ │ : ???? :
└──────────────┘ │ YieldMath │ `:::::::::::::::::'
└───────────────────────────────┘ `-:::::::::::-'
*/
/// Calculates the amount of fyToken a user would get for given amount of shares.
/// https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
/// @param sharesReserves yield bearing vault shares reserve amount
/// @param fyTokenReserves fyToken reserves amount
/// @param sharesIn shares amount to be traded
/// @param timeTillMaturity time till maturity in seconds e.g. 90 days in seconds
/// @param k time till maturity coefficient, multiplied by 2^64. e.g. 25 years in seconds
/// @param g fee coefficient, multiplied by 2^64 -- sb under 1.0 for selling shares to pool
/// @param c price of shares in terms of their base, multiplied by 2^64
/// @param mu (μ) Normalization factor -- starts as c at initialization
/// @return fyTokenOut the amount of fyToken a user would get for given amount of shares
function fyTokenOutForSharesIn(
uint128 sharesReserves, // z
uint128 fyTokenReserves, // x
uint128 sharesIn, // x == Δz
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128) {
unchecked {
require(c > 0 && mu > 0, "YieldMath: c and mu must be positive");
uint128 a = _computeA(timeTillMaturity, k, g);
uint256 sum;
{
/* https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
y = fyToken reserves
z = shares reserves
x = Δz (sharesIn)
y - ( sum )^( invA )
y - (( Za ) + ( Ya ) - ( Zxa ) )^( invA )
Δy = y - ( c/μ * (μz)^(1-t) + y^(1-t) - c/μ * (μz + μx)^(1-t) )^(1 / (1 - t))
*/
uint256 normalizedSharesReserves;
require((normalizedSharesReserves = mu.mulu(sharesReserves)) <= MAX, "YieldMath: Rate overflow (nsr)");
// za = c/μ * (normalizedSharesReserves ** a)
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 za;
require(
(za = c.div(mu).mulu(uint128(normalizedSharesReserves).pow(a, ONE))) <= MAX,
"YieldMath: Rate overflow (za)"
);
// ya = fyTokenReserves ** a
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 ya = fyTokenReserves.pow(a, ONE);
// normalizedSharesIn = μ * sharesIn
uint256 normalizedSharesIn;
require((normalizedSharesIn = mu.mulu(sharesIn)) <= MAX, "YieldMath: Rate overflow (nsi)");
// zx = normalizedSharesReserves + sharesIn * μ
uint256 zx;
require((zx = normalizedSharesReserves + normalizedSharesIn) <= MAX, "YieldMath: Too many shares in");
// zxa = c/μ * zx ** a
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 zxa;
require((zxa = c.div(mu).mulu(uint128(zx).pow(a, ONE))) <= MAX, "YieldMath: Rate overflow (zxa)");
sum = za + ya - zxa;
require(sum <= (za + ya), "YieldMath: Sum underflow");
}
// result = fyTokenReserves - (sum ** (1/a))
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 fyTokenOut;
require(
(fyTokenOut = uint256(fyTokenReserves) - sum.u128().pow(ONE, a)) <= MAX,
"YieldMath: Rounding error"
);
require(fyTokenOut <= fyTokenReserves, "YieldMath: > fyToken reserves");
return uint128(fyTokenOut);
}
}
/* ----------------------------------------------------------------------------------------------------------------
.-:::::::::::-. ┌───────────────────────────────┐
.:::::::::::::::::. │ │
: _______ __ __ : \│ │/ ┌──────────────┐
:: | || | | |:: \│ │/ │$ $│
::: | ___|| |_| |::: │ sharesOutForFYTokenIn │ │ ┌────────────┴─┐
::: | |___ | |::: ────────▶ │ │ ────────▶ │ │$ $│
::: | ___||_ _|::: │ │ │$│ ┌────────────┴─┐
::: | | | | ::: /│ │\ └─┤ │$ $│
:: |___| |___| :: /│ │\ │$│ SHARES │
: `fyTokenIn` : │ \(^o^)/ │ └─┤ ???? │
`:::::::::::::::::' │ YieldMath │ │$ $│
`-:::::::::::-' └───────────────────────────────┘ └──────────────┘
*/
/// Calculates the amount of shares a user would get for certain amount of fyToken.
/// @param sharesReserves shares reserves amount
/// @param fyTokenReserves fyToken reserves amount
/// @param fyTokenIn fyToken amount to be traded
/// @param timeTillMaturity time till maturity in seconds
/// @param k time till maturity coefficient, multiplied by 2^64
/// @param g fee coefficient, multiplied by 2^64
/// @param c price of shares in terms of Dai, multiplied by 2^64
/// @param mu (μ) Normalization factor -- starts as c at initialization
/// @return amount of Shares a user would get for given amount of fyToken
function sharesOutForFYTokenIn(
uint128 sharesReserves,
uint128 fyTokenReserves,
uint128 fyTokenIn,
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128) {
unchecked {
require(c > 0 && mu > 0, "YieldMath: c and mu must be positive");
return
_sharesOutForFYTokenIn(
sharesReserves,
fyTokenReserves,
fyTokenIn,
_computeA(timeTillMaturity, k, g),
c,
mu
);
}
}
/// @dev Splitting sharesOutForFYTokenIn in two functions to avoid stack depth limits.
function _sharesOutForFYTokenIn(
uint128 sharesReserves,
uint128 fyTokenReserves,
uint128 fyTokenIn,
uint128 a,
int128 c,
int128 mu
) private pure returns (uint128) {
/* https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
y = fyToken reserves
z = shares reserves
x = Δy (fyTokenIn)
z - ( rightTerm )
z - (invMu) * ( Za ) + ( Ya ) - ( Yxa ) / (c / μ) )^( invA )
Δz = z - 1/μ * ( ( (c / μ) * (μz)^(1-t) + y^(1-t) - (y + x)^(1-t) ) / (c / μ) )^(1 / (1 - t))
*/
unchecked {
// normalizedSharesReserves = μ * sharesReserves
uint256 normalizedSharesReserves;
require((normalizedSharesReserves = mu.mulu(sharesReserves)) <= MAX, "YieldMath: Rate overflow (nsr)");
uint128 rightTerm;
{
uint256 zaYaYxa;
{
// za = c/μ * (normalizedSharesReserves ** a)
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 za;
require(
(za = c.div(mu).mulu(uint128(normalizedSharesReserves).pow(a, ONE))) <= MAX,
"YieldMath: Rate overflow (za)"
);
// ya = fyTokenReserves ** a
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 ya = fyTokenReserves.pow(a, ONE);
// yxa = (fyTokenReserves + x) ** a # x is aka Δy
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 yxa = (fyTokenReserves + fyTokenIn).pow(a, ONE);
require((zaYaYxa = (za + ya - yxa)) <= MAX, "YieldMath: Rate overflow (yxa)");
}
rightTerm = uint128( // Cast zaYaYxa/(c/μ).pow(1/a).div(μ) from int128 to uint128 - always positive
int128( // Cast zaYaYxa/(c/μ).pow(1/a) from uint128 to int128 - always < zaYaYxa/(c/μ)
uint128( // Cast zaYaYxa/(c/μ) from int128 to uint128 - always positive
zaYaYxa.divu(uint128(c.div(mu))) // Cast c/μ from int128 to uint128 - always positive
).pow(uint128(ONE), a) // Cast 2^64 from int128 to uint128 - always positive
).div(mu)
);
}
require(rightTerm <= sharesReserves, "YieldMath: Rate underflow");
return sharesReserves - rightTerm;
}
}
/* ----------------------------------------------------------------------------------------------------------------
.-:::::::::::-. ┌───────────────────────────────┐
.:::::::::::::::::. │ │ ┌──────────────┐
: _______ __ __ : \│ │/ │$ $│
:: | || | | |:: \│ │/ │ ┌────────────┴─┐
::: | ___|| |_| |::: │ fyTokenInForSharesOut │ │ │$ $│
::: | |___ | |::: ────────▶ │ │ ────────▶ │$│ ┌────────────┴─┐
::: | ___||_ _|::: │ │ └─┤ │$ $│
::: | | | | ::: /│ │\ │$│ │
:: |___| |___| :: /│ │\ └─┤ `sharesOut` │
: ???? : │ \(^o^)/ │ │$ $│
`:::::::::::::::::' │ YieldMath │ └──────────────┘
`-:::::::::::-' └───────────────────────────────┘
*/
/// Calculates the amount of fyToken a user could sell for given amount of Shares.
/// @param sharesReserves shares reserves amount
/// @param fyTokenReserves fyToken reserves amount
/// @param sharesOut Shares amount to be traded
/// @param timeTillMaturity time till maturity in seconds
/// @param k time till maturity coefficient, multiplied by 2^64
/// @param g fee coefficient, multiplied by 2^64
/// @param c price of shares in terms of Dai, multiplied by 2^64
/// @param mu (μ) Normalization factor -- starts as c at initialization
/// @return fyTokenIn the amount of fyToken a user could sell for given amount of Shares
function fyTokenInForSharesOut(
uint128 sharesReserves,
uint128 fyTokenReserves,
uint128 sharesOut,
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128) {
/* https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
y = fyToken reserves
z = shares reserves
x = Δz (sharesOut)
( sum )^( invA ) - y
( Za ) + ( Ya ) - ( Zxa )^( invA ) - y
Δy = ( c/μ * (μz)^(1-t) + y^(1-t) - c/μ * (μz - μx)^(1-t) )^(1 / (1 - t)) - y
*/
unchecked {
require(c > 0 && mu > 0, "YieldMath: c and mu must be positive");
uint128 a = _computeA(timeTillMaturity, k, g);
uint256 sum;
{
// normalizedSharesReserves = μ * sharesReserves
uint256 normalizedSharesReserves;
require((normalizedSharesReserves = mu.mulu(sharesReserves)) <= MAX, "YieldMath: Rate overflow (nsr)");
// za = c/μ * (normalizedSharesReserves ** a)
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 za;
require(
(za = c.div(mu).mulu(uint128(normalizedSharesReserves).pow(a, ONE))) <= MAX,
"YieldMath: Rate overflow (za)"
);
// ya = fyTokenReserves ** a
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 ya = fyTokenReserves.pow(a, ONE);
// normalizedSharesOut = μ * sharesOut
uint256 normalizedSharesOut;
require((normalizedSharesOut = mu.mulu(sharesOut)) <= MAX, "YieldMath: Rate overflow (nso)");
// zx = normalizedSharesReserves + sharesOut * μ
require(normalizedSharesReserves >= normalizedSharesOut, "YieldMath: Too many shares in");
uint256 zx = normalizedSharesReserves - normalizedSharesOut;
// zxa = c/μ * zx ** a
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 zxa = c.div(mu).mulu(uint128(zx).pow(a, ONE));
// sum = za + ya - zxa
// z < MAX, y < MAX, a < 1. It can only underflow, not overflow.
require((sum = za + ya - zxa) <= MAX, "YieldMath: > fyToken reserves");
}
// result = fyTokenReserves - (sum ** (1/a))
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 result;
require(
(result = uint256(uint128(sum).pow(ONE, a)) - uint256(fyTokenReserves)) <= MAX,
"YieldMath: Rounding error"
);
return uint128(result);
}
}
/* ----------------------------------------------------------------------------------------------------------------
┌───────────────────────────────┐ .-:::::::::::-.
┌──────────────┐ │ │ .:::::::::::::::::.
│$ $│ \│ │/ : _______ __ __ :
│ ┌────────────┴─┐ \│ │/ :: | || | | |::
│ │$ $│ │ sharesInForFYTokenOut │ ::: | ___|| |_| |:::
│$│ ┌────────────┴─┐ ────────▶ │ │ ────────▶ ::: | |___ | |:::
└─┤ │$ $│ │ │ ::: | ___||_ _|:::
│$│ SHARES │ /│ │\ ::: | | | | :::
└─┤ ???? │ /│ │\ :: |___| |___| ::
│$ $│ │ \(^o^)/ │ : `fyTokenOut` :
└──────────────┘ │ YieldMath │ `:::::::::::::::::'
└───────────────────────────────┘ `-:::::::::::-'
*/
/// @param sharesReserves yield bearing vault shares reserve amount
/// @param fyTokenReserves fyToken reserves amount
/// @param fyTokenOut fyToken amount to be traded
/// @param timeTillMaturity time till maturity in seconds e.g. 90 days in seconds
/// @param k time till maturity coefficient, multiplied by 2^64. e.g. 25 years in seconds
/// @param g fee coefficient, multiplied by 2^64 -- sb under 1.0 for selling shares to pool
/// @param c price of shares in terms of their base, multiplied by 2^64
/// @param mu (μ) Normalization factor -- starts as c at initialization
/// @return result the amount of shares a user would have to pay for given amount of fyToken
function sharesInForFYTokenOut(
uint128 sharesReserves,
uint128 fyTokenReserves,
uint128 fyTokenOut,
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128) {
unchecked {
require(c > 0 && mu > 0, "YieldMath: c and mu must be positive");
return
_sharesInForFYTokenOut(
sharesReserves,
fyTokenReserves,
fyTokenOut,
_computeA(timeTillMaturity, k, g),
c,
mu
);
}
}
/// @dev Splitting sharesInForFYTokenOut in two functions to avoid stack depth limits
function _sharesInForFYTokenOut(
uint128 sharesReserves,
uint128 fyTokenReserves,
uint128 fyTokenOut,
uint128 a,
int128 c,
int128 mu
) private pure returns (uint128) {
/* https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
y = fyToken reserves
z = shares reserves
x = Δy (fyTokenOut)
1/μ * ( subtotal )^( invA ) - z
1/μ * (( Za ) + ( Ya ) - ( Yxa )) / (c/μ) )^( invA ) - z
Δz = 1/μ * (( c/μ * μz^(1-t) + y^(1-t) - (y - x)^(1-t)) / (c/μ) )^(1 / (1 - t)) - z
*/
unchecked {
// normalizedSharesReserves = μ * sharesReserves
require(mu.mulu(sharesReserves) <= MAX, "YieldMath: Rate overflow (nsr)");
// za = c/μ * (normalizedSharesReserves ** a)
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 za = c.div(mu).mulu(uint128(mu.mulu(sharesReserves)).pow(a, ONE));
require(za <= MAX, "YieldMath: Rate overflow (za)");
// ya = fyTokenReserves ** a
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 ya = fyTokenReserves.pow(a, ONE);
// yxa = (fyTokenReserves - x) ** aß
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 yxa = (fyTokenReserves - fyTokenOut).pow(a, ONE);
require(fyTokenOut <= fyTokenReserves, "YieldMath: Underflow (yxa)");
uint256 zaYaYxa;
require((zaYaYxa = (za + ya - yxa)) <= MAX, "YieldMath: Rate overflow (zyy)");
int128 subtotal = int128(ONE).div(mu).mul(
(uint128(zaYaYxa.divu(uint128(c.div(mu)))).pow(uint128(ONE), uint128(a))).i128()
);
// subtotal is calculated as a positive fraction multiplied by a uint so it cannot underflow when casting to uint and its ok to use a raw casting
uint128 sharesOut = uint128(subtotal) - sharesReserves;
require(sharesOut <= uint128(subtotal), "YieldMath: Underflow error");
return sharesOut;
}
}
/// Calculates the max amount of fyToken a user could sell.
/// @param sharesReserves yield bearing vault shares reserve amount
/// @param fyTokenReserves fyToken reserves amount
/// @param timeTillMaturity time till maturity in seconds e.g. 90 days in seconds
/// @param k time till maturity coefficient, multiplied by 2^64. e.g. 25 years in seconds
/// @param g fee coefficient, multiplied by 2^64 -- sb over 1.0 for buying shares from the pool
/// @param c price of shares in terms of their base, multiplied by 2^64
/// @return fyTokenIn the max amount of fyToken a user could sell
function maxFYTokenIn(
uint128 sharesReserves,
uint128 fyTokenReserves,
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128 fyTokenIn) {
/* https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
Y = fyToken reserves
Z = shares reserves
y = maxFYTokenIn
( sum )^( invA ) - Y
( Za ) + ( Ya ) )^( invA ) - Y
Δy = ( c/μ * (μz)^(1-t) + Y^(1-t) )^(1 / (1 - t)) - Y
*/
unchecked {
require(c > 0 && mu > 0, "YieldMath: c and mu must be positive");
uint128 a = _computeA(timeTillMaturity, k, g);
uint256 sum;
{
// normalizedSharesReserves = μ * sharesReserves
uint256 normalizedSharesReserves;
require((normalizedSharesReserves = mu.mulu(sharesReserves)) <= MAX, "YieldMath: Rate overflow (nsr)");
// za = c/μ * (normalizedSharesReserves ** a)
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 za;
require(
(za = c.div(mu).mulu(uint128(normalizedSharesReserves).pow(a, ONE))) <= MAX,
"YieldMath: Rate overflow (za)"
);
// ya = fyTokenReserves ** a
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 ya = fyTokenReserves.pow(a, ONE);
// sum = za + ya
// z < MAX, y < MAX, a < 1. It can only underflow, not overflow.
require((sum = za + ya) <= MAX, "YieldMath: > fyToken reserves");
}
// result = (sum ** (1/a)) - fyTokenReserves
// The “pow(x, y, z)” function not only calculates x^(y/z) but also normalizes the result to
// fit into 64.64 fixed point number, i.e. it actually calculates: x^(y/z) * (2^63)^(1 - y/z)
uint256 result;
require(
(result = uint256(uint128(sum).pow(ONE, a)) - uint256(fyTokenReserves)) <= MAX,
"YieldMath: Rounding error"
);
fyTokenIn = uint128(result);
}
}
/// Calculates the max amount of fyToken a user could get.
/// https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
/// @param sharesReserves yield bearing vault shares reserve amount
/// @param fyTokenReserves fyToken reserves amount
/// @param timeTillMaturity time till maturity in seconds e.g. 90 days in seconds
/// @param k time till maturity coefficient, multiplied by 2^64. e.g. 25 years in seconds
/// @param g fee coefficient, multiplied by 2^64 -- sb under 1.0 for selling shares to pool
/// @param c price of shares in terms of their base, multiplied by 2^64
/// @param mu (μ) Normalization factor -- c at initialization
/// @return fyTokenOut the max amount of fyToken a user could get
function maxFYTokenOut(
uint128 sharesReserves,
uint128 fyTokenReserves,
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128 fyTokenOut) {
unchecked {
require(c > 0 && mu > 0, "YieldMath: c and mu must be positive");
int128 a = int128(_computeA(timeTillMaturity, k, g));
/*
y = maxFyTokenOut
Y = fyTokenReserves (virtual)
Z = sharesReserves
Y - ( ( numerator ) / ( denominator ) )^invA
Y - ( ( ( Za ) + ( Ya ) ) / ( denominator ) )^invA
y = Y - ( ( c/μ * (μZ)^a + Y^a ) / ( c/μ + 1 ) )^(1/a)
*/
// za = c/μ * ((μ * (sharesReserves / 1e18)) ** a)
int128 za = c.div(mu).mul(mu.mul(sharesReserves.divu(WAD)).pow(a));
// ya = (fyTokenReserves / 1e18) ** a
int128 ya = fyTokenReserves.divu(WAD).pow(a);
// numerator = za + ya
int128 numerator = za.add(ya);
// denominator = c/u + 1
int128 denominator = c.div(mu).add(int128(ONE));
// rightTerm = (numerator / denominator) ** (1/a)
int128 rightTerm = numerator.div(denominator).pow(int128(ONE).div(a));
// maxFYTokenOut_ = fyTokenReserves - (rightTerm * 1e18)
require((fyTokenOut = fyTokenReserves - uint128(rightTerm.mulu(WAD))) <= MAX, "YieldMath: Underflow error");
require(fyTokenOut <= fyTokenReserves, "YieldMath: Underflow error");
}
}
/// Calculates the max amount of base a user could sell.
/// https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
/// @param sharesReserves yield bearing vault shares reserve amount
/// @param fyTokenReserves fyToken reserves amount
/// @param timeTillMaturity time till maturity in seconds e.g. 90 days in seconds
/// @param k time till maturity coefficient, multiplied by 2^64. e.g. 25 years in seconds
/// @param g fee coefficient, multiplied by 2^64 -- sb under 1.0 for selling shares to pool
/// @param c price of shares in terms of their base, multiplied by 2^64
/// @param mu (μ) Normalization factor -- c at initialization
/// @return sharesIn Calculates the max amount of base a user could sell.
function maxSharesIn(
uint128 sharesReserves, // z
uint128 fyTokenReserves, // x
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128 sharesIn) {
unchecked {
require(c > 0 && mu > 0, "YieldMath: c and mu must be positive");
int128 a = int128(_computeA(timeTillMaturity, k, g));
/*
y = maxSharesIn_
Y = fyTokenReserves (virtual)
Z = sharesReserves
1/μ ( ( numerator ) / ( denominator ) )^invA - Z
1/μ ( ( ( Za ) + ( Ya ) ) / ( denominator ) )^invA - Z
y = 1/μ ( ( c/μ * (μZ)^a + Y^a ) / ( c/u + 1 ) )^(1/a) - Z
*/
// za = c/μ * ((μ * (sharesReserves / 1e18)) ** a)
int128 za = c.div(mu).mul(mu.mul(sharesReserves.divu(WAD)).pow(a));
// ya = (fyTokenReserves / 1e18) ** a
int128 ya = fyTokenReserves.divu(WAD).pow(a);
// numerator = za + ya
int128 numerator = za.add(ya);
// denominator = c/u + 1
int128 denominator = c.div(mu).add(int128(ONE));
// leftTerm = 1/μ * (numerator / denominator) ** (1/a)
int128 leftTerm = int128(ONE).div(mu).mul(numerator.div(denominator).pow(int128(ONE).div(a)));
// maxSharesIn_ = (leftTerm * 1e18) - sharesReserves
require((sharesIn = uint128(leftTerm.mulu(WAD)) - sharesReserves) <= MAX, "YieldMath: Underflow error");
require(sharesIn <= uint128(leftTerm.mulu(WAD)), "YieldMath: Underflow error");
}
}
/*
This function is not needed as it's return value is driven directly by the shares liquidity of the pool
https://hackmd.io/lRZ4mgdrRgOpxZQXqKYlFw?view#MaxSharesOut
function maxSharesOut(
uint128 sharesReserves, // z
uint128 fyTokenReserves, // x
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128 maxSharesOut_) {} */
/// Calculates the total supply invariant.
/// https://docs.google.com/spreadsheets/d/14K_McZhlgSXQfi6nFGwDvDh4BmOu6_Hczi_sFreFfOE/
/// @param sharesReserves yield bearing vault shares reserve amount
/// @param fyTokenReserves fyToken reserves amount
/// @param totalSupply total supply
/// @param timeTillMaturity time till maturity in seconds e.g. 90 days in seconds
/// @param k time till maturity coefficient, multiplied by 2^64. e.g. 25 years in seconds
/// @param g fee coefficient, multiplied by 2^64 -- use under 1.0 (g2)
/// @param c price of shares in terms of their base, multiplied by 2^64
/// @param mu (μ) Normalization factor -- c at initialization
/// @return result Calculates the total supply invariant.
function invariant(
uint128 sharesReserves, // z
uint128 fyTokenReserves, // x
uint256 totalSupply, // s
uint128 timeTillMaturity,
int128 k,
int128 g,
int128 c,
int128 mu
) public pure returns (uint128 result) {
if (totalSupply == 0) return 0;
int128 a = int128(_computeA(timeTillMaturity, k, g));
result = _invariant(sharesReserves, fyTokenReserves, totalSupply, a, c, mu);
}
/// @param sharesReserves yield bearing vault shares reserve amount
/// @param fyTokenReserves fyToken reserves amount
/// @param totalSupply total supply
/// @param a 1 - g * t computed
/// @param c price of shares in terms of their base, multiplied by 2^64
/// @param mu (μ) Normalization factor -- c at initialization
/// @return result Calculates the total supply invariant.
function _invariant(
uint128 sharesReserves, // z
uint128 fyTokenReserves, // x
uint256 totalSupply, // s
int128 a,
int128 c,
int128 mu
) internal pure returns (uint128 result) {
unchecked {
require(c > 0 && mu > 0, "YieldMath: c and mu must be positive");
/*
y = invariant
Y = fyTokenReserves (virtual)
Z = sharesReserves
s = total supply
c/μ ( ( numerator ) / ( denominator ) )^invA / s
c/μ ( ( ( Za ) + ( Ya ) ) / ( denominator ) )^invA / s
y = c/μ ( ( c/μ * (μZ)^a + Y^a ) / ( c/u + 1 ) )^(1/a) / s
*/
// za = c/μ * ((μ * (sharesReserves / 1e18)) ** a)
int128 za = c.div(mu).mul(mu.mul(sharesReserves.divu(WAD)).pow(a));
// ya = (fyTokenReserves / 1e18) ** a
int128 ya = fyTokenReserves.divu(WAD).pow(a);
// numerator = za + ya
int128 numerator = za.add(ya);
// denominator = c/u + 1
int128 denominator = c.div(mu).add(int128(ONE));
// topTerm = c/μ * (numerator / denominator) ** (1/a)
int128 topTerm = c.div(mu).mul((numerator.div(denominator)).pow(int128(ONE).div(a)));
result = uint128((topTerm.mulu(WAD) * WAD) / totalSupply);
}
}
/* UTILITY FUNCTIONS
******************************************************************************************************************/
function _computeA(
uint128 timeTillMaturity,
int128 k,
int128 g
) private pure returns (uint128) {
// t = k * timeTillMaturity
int128 t = k.mul(timeTillMaturity.fromUInt());
require(t >= 0, "YieldMath: t must be positive"); // Meaning neither T or k can be negative
// a = (1 - gt)
int128 a = int128(ONE).sub(g.mul(t));
require(a > 0, "YieldMath: Too far from maturity");
require(a <= int128(ONE), "YieldMath: g must be positive");
return uint128(a);
}
}