This repository has been archived by the owner on Nov 15, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Periphery.sol
853 lines (764 loc) · 36.8 KB
/
Periphery.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
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
// SPDX-License-Identifier: AGPL-3.0-only
pragma solidity 0.8.13;
// External references
import { ERC20 } from "@rari-capital/solmate/src/tokens/ERC20.sol";
import { SafeTransferLib } from "@rari-capital/solmate/src/utils/SafeTransferLib.sol";
import { FixedMath } from "./external/FixedMath.sol";
import { BalancerVault, IAsset } from "./external/balancer/Vault.sol";
import { BalancerPool } from "./external/balancer/Pool.sol";
import { IERC3156FlashBorrower } from "./external/flashloan/IERC3156FlashBorrower.sol";
// Internal references
import { Errors } from "@sense-finance/v1-utils/src/libs/Errors.sol";
import { Levels } from "@sense-finance/v1-utils/src/libs/Levels.sol";
import { Trust } from "@sense-finance/v1-utils/src/Trust.sol";
import { BaseAdapter as Adapter } from "./adapters/abstract/BaseAdapter.sol";
import { BaseFactory as AdapterFactory } from "./adapters/abstract/factories/BaseFactory.sol";
import { Divider } from "./Divider.sol";
import { PoolManager } from "@sense-finance/v1-fuse/src/PoolManager.sol";
interface SpaceFactoryLike {
function create(address, uint256) external returns (address);
function pools(address adapter, uint256 maturity) external view returns (address);
}
/// @title Periphery
contract Periphery is Trust, IERC3156FlashBorrower {
using FixedMath for uint256;
using SafeTransferLib for ERC20;
using Levels for uint256;
/* ========== PUBLIC CONSTANTS ========== */
/// @notice Lower bound on the amount of Claim tokens one can swap in for Target
uint256 public constant MIN_YT_SWAP_IN = 0.000001e18;
/// @notice Acceptable error when estimating the tokens resulting from a specific swap
uint256 public constant PRICE_ESTIMATE_ACCEPTABLE_ERROR = 0.00000001e18;
/* ========== PUBLIC IMMUTABLES ========== */
/// @notice Sense core Divider address
Divider public immutable divider;
/// @notice Sense core Divider address
BalancerVault public immutable balancerVault;
/* ========== PUBLIC MUTABLE STORAGE ========== */
/// @notice Sense core Divider address
PoolManager public poolManager;
/// @notice Sense core Divider address
SpaceFactoryLike public spaceFactory;
/// @notice adapter factories -> is supported
mapping(address => bool) public factories;
/// @notice adapter -> bool
mapping(address => bool) public verified;
/* ========== DATA STRUCTURES ========== */
struct PoolLiquidity {
ERC20[] tokens;
uint256[] amounts;
uint256 minBptOut;
}
constructor(
address _divider,
address _poolManager,
address _spaceFactory,
address _balancerVault
) Trust(msg.sender) {
divider = Divider(_divider);
poolManager = PoolManager(_poolManager);
spaceFactory = SpaceFactoryLike(_spaceFactory);
balancerVault = BalancerVault(_balancerVault);
}
/* ========== SERIES / ADAPTER MANAGEMENT ========== */
/// @notice Sponsor a new Series in any adapter previously onboarded onto the Divider
/// @dev Called by an external address, initializes a new series in the Divider
/// @param adapter Adapter to associate with the Series
/// @param maturity Maturity date for the Series, in units of unix time
/// @param withPool Whether to deploy a Space pool or not (only works for unverified adapters)
function sponsorSeries(
address adapter,
uint256 maturity,
bool withPool
) external returns (address pt, address yt) {
(, address stake, uint256 stakeSize) = Adapter(adapter).getStakeAndTarget();
// Transfer stakeSize from sponsor into this contract
ERC20(stake).safeTransferFrom(msg.sender, address(this), stakeSize);
// Approve divider to withdraw stake assets
ERC20(stake).safeApprove(address(divider), stakeSize);
(pt, yt) = divider.initSeries(adapter, maturity, msg.sender);
// Space pool is always created for verified adapters whilst is optional for unverified ones.
// Automatically queueing series is only for verified adapters
if (verified[adapter]) {
if (address(poolManager) == address(0)) {
spaceFactory.create(adapter, maturity);
} else {
poolManager.queueSeries(adapter, maturity, spaceFactory.create(adapter, maturity));
}
} else {
if (withPool) {
spaceFactory.create(adapter, maturity);
}
}
emit SeriesSponsored(adapter, maturity, msg.sender);
}
/// @notice Deploy and onboard a Adapter
/// @dev Called by external address, deploy a new Adapter via an Adapter Factory
/// @param f Factory to use
/// @param target Target to onboard
/// @param data Additional encoded data needed to deploy the adapter
function deployAdapter(
address f,
address target,
bytes memory data
) external returns (address adapter) {
if (!factories[f]) revert Errors.FactoryNotSupported();
adapter = AdapterFactory(f).deployAdapter(target, data);
emit AdapterDeployed(adapter);
_verifyAdapter(adapter, true);
_onboardAdapter(adapter, true);
}
/* ========== LIQUIDITY UTILS ========== */
/// @notice Swap Target to Principal Tokens of a particular series
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param tBal Balance of Target to sell
/// @param minAccepted Min accepted amount of PT
/// @return ptBal amount of PT received
function swapTargetForPTs(
address adapter,
uint256 maturity,
uint256 tBal,
uint256 minAccepted
) external returns (uint256 ptBal) {
ERC20(Adapter(adapter).target()).safeTransferFrom(msg.sender, address(this), tBal); // pull target
return _swapTargetForPTs(adapter, maturity, tBal, minAccepted);
}
/// @notice Swap Underlying to Principal Tokens of a particular series
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param uBal Balance of Underlying to sell
/// @param minAccepted Min accepted amount of PT
/// @return ptBal amount of PT received
function swapUnderlyingForPTs(
address adapter,
uint256 maturity,
uint256 uBal,
uint256 minAccepted
) external returns (uint256 ptBal) {
ERC20 underlying = ERC20(Adapter(adapter).underlying());
underlying.safeTransferFrom(msg.sender, address(this), uBal); // pull underlying
uint256 tBal = Adapter(adapter).wrapUnderlying(uBal); // wrap underlying into target
ptBal = _swapTargetForPTs(adapter, maturity, tBal, minAccepted);
}
/// @notice Swap Target to Yield Tokens of a particular series
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param targetIn Balance of Target to sell
/// @param targetToBorrow Balance of Target to borrow
/// @param minOut Min accepted amount of YT
/// @return targetBal amount of Target sent back
/// @return ytBal amount of YT received
function swapTargetForYTs(
address adapter,
uint256 maturity,
uint256 targetIn,
uint256 targetToBorrow,
uint256 minOut
) external returns (uint256 targetBal, uint256 ytBal) {
ERC20(Adapter(adapter).target()).safeTransferFrom(msg.sender, address(this), targetIn);
(targetBal, ytBal) = _flashBorrowAndSwapToYTs(adapter, maturity, targetIn, targetToBorrow, minOut);
ERC20(Adapter(adapter).target()).safeTransfer(msg.sender, targetBal);
ERC20(divider.yt(adapter, maturity)).safeTransfer(msg.sender, ytBal);
}
/// @notice Swap Underlying to Yield of a particular series
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param underlyingIn Balance of Underlying to sell
/// @param targetToBorrow Balance of Target to borrow
/// @param minOut Min accepted amount of YT
/// @return targetBal amount of Target sent back
/// @return ytBal amount of YT received
function swapUnderlyingForYTs(
address adapter,
uint256 maturity,
uint256 underlyingIn,
uint256 targetToBorrow,
uint256 minOut
) external returns (uint256 targetBal, uint256 ytBal) {
ERC20 underlying = ERC20(Adapter(adapter).underlying());
underlying.safeTransferFrom(msg.sender, address(this), underlyingIn); // Pull Underlying
// Wrap Underlying into Target and swap it for YTs
uint256 targetIn = Adapter(adapter).wrapUnderlying(underlyingIn);
(targetBal, ytBal) = _flashBorrowAndSwapToYTs(adapter, maturity, targetIn, targetToBorrow, minOut);
ERC20(Adapter(adapter).target()).safeTransfer(msg.sender, targetBal);
ERC20(divider.yt(adapter, maturity)).safeTransfer(msg.sender, ytBal);
}
/// @notice Swap Principal Tokens for Target of a particular series
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param ptBal Balance of PT to sell
/// @param minAccepted Min accepted amount of Target
function swapPTsForTarget(
address adapter,
uint256 maturity,
uint256 ptBal,
uint256 minAccepted
) external returns (uint256 tBal) {
tBal = _swapPTsForTarget(adapter, maturity, ptBal, minAccepted); // swap Principal Tokens for target
ERC20(Adapter(adapter).target()).safeTransfer(msg.sender, tBal); // transfer target to msg.sender
}
/// @notice Swap Principal Tokens for Underlying of a particular series
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param ptBal Balance of PT to sell
/// @param minAccepted Min accepted amount of Target
function swapPTsForUnderlying(
address adapter,
uint256 maturity,
uint256 ptBal,
uint256 minAccepted
) external returns (uint256 uBal) {
uint256 tBal = _swapPTsForTarget(adapter, maturity, ptBal, minAccepted); // swap Principal Tokens for target
uBal = Adapter(adapter).unwrapTarget(tBal); // unwrap target into underlying
ERC20(Adapter(adapter).underlying()).safeTransfer(msg.sender, uBal); // transfer underlying to msg.sender
}
/// @notice Swap YT for Target of a particular series
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param ytBal Balance of Yield Tokens to swap
function swapYTsForTarget(
address adapter,
uint256 maturity,
uint256 ytBal
) external returns (uint256 tBal) {
tBal = _swapYTsForTarget(msg.sender, adapter, maturity, ytBal);
ERC20(Adapter(adapter).target()).safeTransfer(msg.sender, tBal);
}
/// @notice Swap YT for Underlying of a particular series
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param ytBal Balance of Yield Tokens to swap
function swapYTsForUnderlying(
address adapter,
uint256 maturity,
uint256 ytBal
) external returns (uint256 uBal) {
uint256 tBal = _swapYTsForTarget(msg.sender, adapter, maturity, ytBal);
uBal = Adapter(adapter).unwrapTarget(tBal);
ERC20(Adapter(adapter).underlying()).safeTransfer(msg.sender, uBal);
}
/// @notice Adds liquidity providing target
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param tBal Balance of Target to provide
/// @param mode 0 = issues and sell YT, 1 = issue and hold YT
/// @param minBptOut Minimum BPT the user will accept out for this transaction
/// @dev see return description of _addLiquidity
function addLiquidityFromTarget(
address adapter,
uint256 maturity,
uint256 tBal,
uint8 mode,
uint256 minBptOut
)
external
returns (
uint256 tAmount,
uint256 issued,
uint256 lpShares
)
{
ERC20(Adapter(adapter).target()).safeTransferFrom(msg.sender, address(this), tBal);
(tAmount, issued, lpShares) = _addLiquidity(adapter, maturity, tBal, mode, minBptOut);
}
/// @notice Adds liquidity providing underlying
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param uBal Balance of Underlying to provide
/// @param mode 0 = issues and sell YT, 1 = issue and hold YT
/// @param minBptOut Minimum BPT the user will accept out for this transaction
/// @dev see return description of _addLiquidity
function addLiquidityFromUnderlying(
address adapter,
uint256 maturity,
uint256 uBal,
uint8 mode,
uint256 minBptOut
)
external
returns (
uint256 tAmount,
uint256 issued,
uint256 lpShares
)
{
ERC20 underlying = ERC20(Adapter(adapter).underlying());
underlying.safeTransferFrom(msg.sender, address(this), uBal);
// Wrap Underlying into Target
uint256 tBal = Adapter(adapter).wrapUnderlying(uBal);
(tAmount, issued, lpShares) = _addLiquidity(adapter, maturity, tBal, mode, minBptOut);
}
/// @notice Removes liquidity providing an amount of LP tokens and returns target
/// @dev More info on `minAmountsOut`: https://github.com/balancer-labs/docs-developers/blob/main/resources/joins-and-exits/pool-exits.md#minamountsout
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param lpBal Balance of LP tokens to provide
/// @param minAmountsOut minimum accepted amounts of PTs and Target given the amount of LP shares provided
/// @param minAccepted only used when removing liquidity on/after maturity and its the min accepted when swapping Principal Tokens to underlying
/// @param intoTarget if true, it will try to swap PTs into Target. Will revert if there's not enough liquidity to perform the swap.
/// @return tBal amount of target received and ptBal amount of Principal Tokens (in case it's called after maturity and redeem is restricted)
function removeLiquidity(
address adapter,
uint256 maturity,
uint256 lpBal,
uint256[] memory minAmountsOut,
uint256 minAccepted,
bool intoTarget
) external returns (uint256 tBal, uint256 ptBal) {
(tBal, ptBal) = _removeLiquidity(adapter, maturity, lpBal, minAmountsOut, minAccepted, intoTarget);
ERC20(Adapter(adapter).target()).safeTransfer(msg.sender, tBal); // Send Target back to the User
}
/// @notice Removes liquidity providing an amount of LP tokens and returns underlying
/// @dev More info on `minAmountsOut`: https://github.com/balancer-labs/docs-developers/blob/main/resources/joins-and-exits/pool-exits.md#minamountsout
/// @param adapter Adapter address for the Series
/// @param maturity Maturity date for the Series
/// @param lpBal Balance of LP tokens to provide
/// @param minAmountsOut minimum accepted amounts of PTs and Target given the amount of LP shares provided
/// @param minAccepted only used when removing liquidity on/after maturity and its the min accepted when swapping Principal Tokens to underlying
/// @param intoTarget if true, it will try to swap PTs into Target. Will revert if there's not enough liquidity to perform the swap.
/// @return uBal amount of underlying received and ptBal Principal Tokens (in case it's called after maturity and redeem is restricted or intoTarget is false)
function removeLiquidityAndUnwrapTarget(
address adapter,
uint256 maturity,
uint256 lpBal,
uint256[] memory minAmountsOut,
uint256 minAccepted,
bool intoTarget
) external returns (uint256 uBal, uint256 ptBal) {
uint256 tBal;
(tBal, ptBal) = _removeLiquidity(adapter, maturity, lpBal, minAmountsOut, minAccepted, intoTarget);
ERC20(Adapter(adapter).underlying()).safeTransfer(msg.sender, uBal = Adapter(adapter).unwrapTarget(tBal)); // Send Underlying back to the User
}
/// @notice Migrates liquidity position from one series to another
/// @dev More info on `minAmountsOut`: https://github.com/balancer-labs/docs-developers/blob/main/resources/joins-and-exits/pool-exits.md#minamountsout
/// @param srcAdapter Adapter address for the source Series
/// @param dstAdapter Adapter address for the destination Series
/// @param srcMaturity Maturity date for the source Series
/// @param dstMaturity Maturity date for the destination Series
/// @param lpBal Balance of LP tokens to provide
/// @param minAmountsOut Minimum accepted amounts of PTs and Target given the amount of LP shares provided
/// @param minAccepted Min accepted amount of target when swapping Principal Tokens (only used when removing liquidity on/after maturity)
/// @param mode 0 = issues and sell YT, 1 = issue and hold YT
/// @param intoTarget if true, it will try to swap PTs into Target. Will revert if there's not enough liquidity to perform the swap
/// @param minBptOut Minimum BPT the user will accept out for this transaction
/// @dev see return description of _addLiquidity. It also returns amount of PTs (in case it's called after maturity and redeem is restricted or inttoTarget is false)
function migrateLiquidity(
address srcAdapter,
address dstAdapter,
uint256 srcMaturity,
uint256 dstMaturity,
uint256 lpBal,
uint256[] memory minAmountsOut,
uint256 minAccepted,
uint8 mode,
bool intoTarget,
uint256 minBptOut
)
external
returns (
uint256 tAmount,
uint256 issued,
uint256 lpShares,
uint256 ptBal
)
{
if (Adapter(srcAdapter).target() != Adapter(dstAdapter).target()) revert Errors.TargetMismatch();
uint256 tBal;
(tBal, ptBal) = _removeLiquidity(srcAdapter, srcMaturity, lpBal, minAmountsOut, minAccepted, intoTarget);
(tAmount, issued, lpShares) = _addLiquidity(dstAdapter, dstMaturity, tBal, mode, minBptOut);
}
/* ========== ADMIN ========== */
/// @notice Enable or disable a factory
/// @param f Factory's address
/// @param isOn Flag setting this factory to enabled or disabled
function setFactory(address f, bool isOn) external requiresTrust {
if (factories[f] == isOn) revert Errors.ExistingValue();
factories[f] = isOn;
emit FactoryChanged(f, isOn);
}
/// @notice Update the address for the Space Factory
/// @param newSpaceFactory The Space Factory addresss to set
function setSpaceFactory(address newSpaceFactory) external requiresTrust {
emit SpaceFactoryChanged(address(spaceFactory), newSpaceFactory);
spaceFactory = SpaceFactoryLike(newSpaceFactory);
}
/// @notice Update the address for the Pool Manager
/// @param newPoolManager The Pool Manager addresss to set
function setPoolManager(address newPoolManager) external requiresTrust {
emit PoolManagerChanged(address(poolManager), newPoolManager);
poolManager = PoolManager(newPoolManager);
}
/// @dev Verifies an Adapter and optionally adds the Target to the money market
/// @param adapter Adapter to verify
function verifyAdapter(address adapter, bool addToPool) public requiresTrust {
_verifyAdapter(adapter, addToPool);
}
function _verifyAdapter(address adapter, bool addToPool) private {
verified[adapter] = true;
if (addToPool && address(poolManager) != address(0)) poolManager.addTarget(Adapter(adapter).target(), adapter);
emit AdapterVerified(adapter);
}
/// @notice Onboard a single Adapter w/o needing a factory
/// @dev Called by a trusted address, approves Target for issuance, and onboards adapter to the Divider
/// @param adapter Adapter to onboard
/// @param addAdapter Whether to call divider.addAdapter or not (useful e.g when upgrading Periphery)
function onboardAdapter(address adapter, bool addAdapter) public {
if (!divider.permissionless() && !isTrusted[msg.sender]) revert Errors.OnlyPermissionless();
_onboardAdapter(adapter, addAdapter);
}
function _onboardAdapter(address adapter, bool addAdapter) private {
ERC20 target = ERC20(Adapter(adapter).target());
target.safeApprove(address(divider), type(uint256).max);
target.safeApprove(address(adapter), type(uint256).max);
ERC20(Adapter(adapter).underlying()).safeApprove(address(adapter), type(uint256).max);
if (addAdapter) divider.addAdapter(adapter);
emit AdapterOnboarded(adapter);
}
/* ========== INTERNAL UTILS ========== */
function _swap(
address assetIn,
address assetOut,
uint256 amountIn,
bytes32 poolId,
uint256 minAccepted
) internal returns (uint256 amountOut) {
// approve vault to spend tokenIn
ERC20(assetIn).safeApprove(address(balancerVault), amountIn);
BalancerVault.SingleSwap memory request = BalancerVault.SingleSwap({
poolId: poolId,
kind: BalancerVault.SwapKind.GIVEN_IN,
assetIn: IAsset(assetIn),
assetOut: IAsset(assetOut),
amount: amountIn,
userData: hex""
});
BalancerVault.FundManagement memory funds = BalancerVault.FundManagement({
sender: address(this),
fromInternalBalance: false,
recipient: payable(address(this)),
toInternalBalance: false
});
amountOut = balancerVault.swap(request, funds, minAccepted, type(uint256).max);
emit Swapped(msg.sender, poolId, assetIn, assetOut, amountIn, amountOut, msg.sig);
}
function _swapPTsForTarget(
address adapter,
uint256 maturity,
uint256 ptBal,
uint256 minAccepted
) internal returns (uint256 tBal) {
address principalToken = divider.pt(adapter, maturity);
ERC20(principalToken).safeTransferFrom(msg.sender, address(this), ptBal); // pull principal
BalancerPool pool = BalancerPool(spaceFactory.pools(adapter, maturity));
tBal = _swap(principalToken, Adapter(adapter).target(), ptBal, pool.getPoolId(), minAccepted); // swap Principal Tokens for underlying
}
function _swapTargetForPTs(
address adapter,
uint256 maturity,
uint256 tBal,
uint256 minAccepted
) internal returns (uint256 ptBal) {
address principalToken = divider.pt(adapter, maturity);
BalancerPool pool = BalancerPool(spaceFactory.pools(adapter, maturity));
ptBal = _swap(Adapter(adapter).target(), principalToken, tBal, pool.getPoolId(), minAccepted); // swap target for Principal Tokens
ERC20(principalToken).safeTransfer(msg.sender, ptBal); // transfer bought principal to user
}
function _swapYTsForTarget(
address sender,
address adapter,
uint256 maturity,
uint256 ytBal
) internal returns (uint256 tBal) {
address yt = divider.yt(adapter, maturity);
// Because there's some margin of error in the pricing functions here, smaller
// swaps will be unreliable. Tokens with more than 18 decimals are not supported.
if (ytBal * 10**(18 - ERC20(yt).decimals()) <= MIN_YT_SWAP_IN) revert Errors.SwapTooSmall();
BalancerPool pool = BalancerPool(spaceFactory.pools(adapter, maturity));
// Transfer YTs into this contract if needed
if (sender != address(this)) ERC20(yt).safeTransferFrom(msg.sender, address(this), ytBal);
// Calculate target to borrow by calling AMM
bytes32 poolId = pool.getPoolId();
(uint256 pti, uint256 targeti) = pool.getIndices();
(ERC20[] memory tokens, uint256[] memory balances, ) = balancerVault.getPoolTokens(poolId);
// Determine how much Target we'll need in to get `ytBal` balance of PT out
// (space doesn't directly use of the fields from `SwapRequest` beyond `poolId`, so the values after are placeholders)
uint256 targetToBorrow = BalancerPool(pool).onSwap(
BalancerPool.SwapRequest({
kind: BalancerVault.SwapKind.GIVEN_OUT,
tokenIn: tokens[targeti],
tokenOut: tokens[pti],
amount: ytBal,
poolId: poolId,
lastChangeBlock: 0,
from: address(0),
to: address(0),
userData: ""
}),
balances[targeti],
balances[pti]
);
// Flash borrow target (following actions in `onFlashLoan`)
tBal = _flashBorrowAndSwapFromYTs(adapter, maturity, ytBal, targetToBorrow);
}
/// @return tAmount if mode = 0, target received from selling YTs, otherwise, returns 0
/// @return issued returns amount of YTs issued (and received) except first provision which returns 0
/// @return lpShares Space LP shares received given the liquidity added
function _addLiquidity(
address adapter,
uint256 maturity,
uint256 tBal,
uint8 mode,
uint256 minBptOut
)
internal
returns (
uint256 tAmount,
uint256 issued,
uint256 lpShares
)
{
// (1) compute target, issue PTs & YTs & add liquidity to space
(issued, lpShares) = _computeIssueAddLiq(adapter, maturity, tBal, minBptOut);
if (issued > 0) {
// issue = 0 means that we are on the first pool provision or that the pt:target ratio is 0:target
if (mode == 0) {
// (2) Sell YTs
tAmount = _swapYTsForTarget(address(this), adapter, maturity, issued);
// (3) Send remaining Target back to the User
ERC20(Adapter(adapter).target()).safeTransfer(msg.sender, tAmount);
} else {
// (4) Send YTs back to the User
ERC20(divider.yt(adapter, maturity)).safeTransfer(msg.sender, issued);
}
}
}
/// @dev Calculates amount of Principal Tokens in target terms (see description on `_computeTarget`) then issues
/// PTs and YTs with the calculated amount and finally adds liquidity to space with the PTs issued
/// and the diff between the target initially passed and the calculated amount
function _computeIssueAddLiq(
address adapter,
uint256 maturity,
uint256 tBal,
uint256 minBptOut
) internal returns (uint256 issued, uint256 lpShares) {
BalancerPool pool = BalancerPool(spaceFactory.pools(adapter, maturity));
// Compute target
(ERC20[] memory tokens, uint256[] memory balances, ) = balancerVault.getPoolTokens(pool.getPoolId());
(uint256 pti, uint256 targeti) = pool.getIndices(); // Ensure we have the right token Indices
// We do not add Principal Token liquidity if it haven't been initialized yet
bool ptInitialized = balances[pti] != 0;
uint256 ptBalInTarget = ptInitialized ? _computeTarget(adapter, balances[pti], balances[targeti], tBal) : 0;
// Issue PT & YT (skip if first pool provision)
issued = ptBalInTarget > 0 ? divider.issue(adapter, maturity, ptBalInTarget) : 0;
// Add liquidity to Space & send the LP Shares to recipient
uint256[] memory amounts = new uint256[](2);
amounts[targeti] = tBal - ptBalInTarget;
amounts[pti] = issued;
lpShares = _addLiquidityToSpace(pool, PoolLiquidity(tokens, amounts, minBptOut));
}
/// @dev Based on pt:target ratio from current pool reserves and tBal passed
/// calculates amount of tBal needed so as to issue PTs that would keep the ratio
function _computeTarget(
address adapter,
uint256 ptiBal,
uint256 targetiBal,
uint256 tBal
) internal returns (uint256 tBalForIssuance) {
return
tBal.fmul(
ptiBal.fdiv(
Adapter(adapter).scale().fmul(FixedMath.WAD - Adapter(adapter).ifee()).fmul(targetiBal) + ptiBal
)
);
}
function _removeLiquidity(
address adapter,
uint256 maturity,
uint256 lpBal,
uint256[] memory minAmountsOut,
uint256 minAccepted,
bool intoTarget
) internal returns (uint256 tBal, uint256 ptBal) {
address target = Adapter(adapter).target();
address pt = divider.pt(adapter, maturity);
BalancerPool pool = BalancerPool(spaceFactory.pools(adapter, maturity));
bytes32 poolId = pool.getPoolId();
// (0) Pull LP tokens from sender
ERC20(address(pool)).safeTransferFrom(msg.sender, address(this), lpBal);
// (1) Remove liquidity from Space
uint256 _ptBal;
(tBal, _ptBal) = _removeLiquidityFromSpace(poolId, pt, target, minAmountsOut, lpBal);
if (divider.mscale(adapter, maturity) > 0) {
if (uint256(Adapter(adapter).level()).redeemRestricted()) {
ptBal = _ptBal;
} else {
// (2) Redeem Principal Tokens for Target
tBal += divider.redeem(adapter, maturity, _ptBal);
}
} else {
// (2) Sell Principal Tokens for Target (if there are)
if (_ptBal > 0 && intoTarget) {
tBal += _swap(pt, target, _ptBal, poolId, minAccepted);
} else {
ptBal = _ptBal;
}
}
if (ptBal > 0) ERC20(pt).safeTransfer(msg.sender, ptBal); // Send PT back to the User
}
/// @notice Initiates a flash loan of Target, swaps target amount to PTs and combines
/// @param adapter adapter
/// @param maturity maturity
/// @param ytBalIn YT amount the user has sent in
/// @param amountToBorrow target amount to borrow
/// @return tBal amount of Target obtained from a sale of YTs
function _flashBorrowAndSwapFromYTs(
address adapter,
uint256 maturity,
uint256 ytBalIn,
uint256 amountToBorrow
) internal returns (uint256 tBal) {
ERC20 target = ERC20(Adapter(adapter).target());
uint256 decimals = target.decimals();
uint256 acceptableError = decimals < 9 ? 1 : PRICE_ESTIMATE_ACCEPTABLE_ERROR / 10**(18 - decimals);
bytes memory data = abi.encode(adapter, uint256(maturity), ytBalIn, ytBalIn - acceptableError, true);
bool result = Adapter(adapter).flashLoan(this, address(target), amountToBorrow, data);
if (!result) revert Errors.FlashBorrowFailed();
tBal = target.balanceOf(address(this));
}
/// @notice Initiates a flash loan of Target, issues PTs/YTs and swaps the PTs to Target
/// @param adapter adapter
/// @param maturity taturity
/// @param targetIn Target amount the user has sent in
/// @param amountToBorrow Target amount to borrow
/// @param minOut minimum amount of Target accepted out for the issued PTs
/// @return targetBal amount of Target remaining after the flashloan has been paid back
/// @return ytBal amount of YTs issued with the borrowed Target and the Target sent in
function _flashBorrowAndSwapToYTs(
address adapter,
uint256 maturity,
uint256 targetIn,
uint256 amountToBorrow,
uint256 minOut
) internal returns (uint256 targetBal, uint256 ytBal) {
bytes memory data = abi.encode(adapter, uint256(maturity), targetIn, minOut, false);
bool result = Adapter(adapter).flashLoan(this, Adapter(adapter).target(), amountToBorrow, data);
if (!result) revert Errors.FlashBorrowFailed();
targetBal = ERC20(Adapter(adapter).target()).balanceOf(address(this));
ytBal = ERC20(divider.yt(adapter, maturity)).balanceOf(address(this));
emit YTsPurchased(msg.sender, adapter, maturity, targetIn, targetBal, ytBal);
}
/// @dev ERC-3156 Flash loan callback
function onFlashLoan(
address initiator,
address, /* token */
uint256 amountBorrrowed,
uint256, /* fee */
bytes calldata data
) external returns (bytes32) {
(address adapter, uint256 maturity, uint256 amountIn, uint256 minOut, bool ytToTarget) = abi.decode(
data,
(address, uint256, uint256, uint256, bool)
);
if (msg.sender != address(adapter)) revert Errors.FlashUntrustedBorrower();
if (initiator != address(this)) revert Errors.FlashUntrustedLoanInitiator();
BalancerPool pool = BalancerPool(spaceFactory.pools(adapter, maturity));
if (ytToTarget) {
ERC20 target = ERC20(Adapter(adapter).target());
// Swap Target for PTs
uint256 ptBal = _swap(
address(target),
divider.pt(adapter, maturity),
target.balanceOf(address(this)),
pool.getPoolId(),
minOut // min pt out
);
// Combine PTs and YTs
divider.combine(adapter, maturity, ptBal < amountIn ? ptBal : amountIn);
} else {
// Issue PTs and YTs
divider.issue(adapter, maturity, amountIn + amountBorrrowed);
ERC20 pt = ERC20(divider.pt(adapter, maturity));
// Swap PTs for Target
_swap(
address(pt),
Adapter(adapter).target(),
pt.balanceOf(address(this)),
pool.getPoolId(),
minOut // min Target out
); // minOut should be close to amountBorrrowed so that minimal Target dust is sent back to the caller
// Flashloaner contract will revert if not enough Target has been swapped out to pay back the loan
}
return keccak256("ERC3156FlashBorrower.onFlashLoan");
}
function _addLiquidityToSpace(BalancerPool pool, PoolLiquidity memory liq) internal returns (uint256 lpBal) {
bytes32 poolId = pool.getPoolId();
IAsset[] memory assets = _convertERC20sToAssets(liq.tokens);
for (uint8 i; i < liq.tokens.length; i++) {
// Tokens and amounts must be in same order
liq.tokens[i].safeApprove(address(balancerVault), liq.amounts[i]);
}
// Behaves like EXACT_TOKENS_IN_FOR_BPT_OUT, user sends precise quantities of tokens,
// and receives an estimated but unknown (computed at run time) quantity of BPT
BalancerVault.JoinPoolRequest memory request = BalancerVault.JoinPoolRequest({
assets: assets,
maxAmountsIn: liq.amounts,
userData: abi.encode(liq.amounts, liq.minBptOut),
fromInternalBalance: false
});
balancerVault.joinPool(poolId, address(this), msg.sender, request);
lpBal = ERC20(address(pool)).balanceOf(msg.sender);
}
function _removeLiquidityFromSpace(
bytes32 poolId,
address pt,
address target,
uint256[] memory minAmountsOut,
uint256 lpBal
) internal returns (uint256 tBal, uint256 ptBal) {
// ExitPoolRequest params
(ERC20[] memory tokens, , ) = balancerVault.getPoolTokens(poolId);
IAsset[] memory assets = _convertERC20sToAssets(tokens);
BalancerVault.ExitPoolRequest memory request = BalancerVault.ExitPoolRequest({
assets: assets,
minAmountsOut: minAmountsOut,
userData: abi.encode(lpBal),
toInternalBalance: false
});
balancerVault.exitPool(poolId, address(this), payable(address(this)), request);
tBal = ERC20(target).balanceOf(address(this));
ptBal = ERC20(pt).balanceOf(address(this));
}
/// @notice From: https://github.com/balancer-labs/balancer-examples/blob/master/packages/liquidity-provision/contracts/LiquidityProvider.sol#L33
/// @dev This helper function is a fast and cheap way to convert between IERC20[] and IAsset[] types
function _convertERC20sToAssets(ERC20[] memory tokens) internal pure returns (IAsset[] memory assets) {
assembly {
assets := tokens
}
}
/* ========== LOGS ========== */
event FactoryChanged(address indexed factory, bool indexed isOn);
event SpaceFactoryChanged(address oldSpaceFactory, address newSpaceFactory);
event PoolManagerChanged(address oldPoolManager, address newPoolManager);
event SeriesSponsored(address indexed adapter, uint256 indexed maturity, address indexed sponsor);
event AdapterDeployed(address indexed adapter);
event AdapterOnboarded(address indexed adapter);
event AdapterVerified(address indexed adapter);
event YTsPurchased(
address indexed sender,
address adapter,
uint256 maturity,
uint256 targetIn,
uint256 targetReturned,
uint256 ytOut
);
event Swapped(
address indexed sender,
bytes32 indexed poolId,
address assetIn,
address assetOut,
uint256 amountIn,
uint256 amountOut,
bytes4 indexed sig
);
}