@pendle/core-v2 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (182) hide show
  1. package/LICENSE +100 -0
  2. package/contracts/SuperComposableYield/ISuperComposableYield.sol +68 -0
  3. package/contracts/SuperComposableYield/SCYUtils.sol +14 -0
  4. package/contracts/SuperComposableYield/implementations/RewardManager.sol +117 -0
  5. package/contracts/SuperComposableYield/implementations/SCYBase.sol +126 -0
  6. package/contracts/SuperComposableYield/implementations/SCYBaseWithRewards.sol +79 -0
  7. package/contracts/core/PendleBaseToken.sol +39 -0
  8. package/contracts/core/PendleMarket.sol +248 -0
  9. package/contracts/core/PendleMarketFactory.sol +60 -0
  10. package/contracts/core/PendleOwnershipToken.sol +44 -0
  11. package/contracts/core/PendleSCYImpl/AaveV3/PendleAaveV3SCY.sol +139 -0
  12. package/contracts/core/PendleSCYImpl/AaveV3/WadRayMath.sol +126 -0
  13. package/contracts/core/PendleSCYImpl/PendleBenQiErc20SCY.sol +127 -0
  14. package/contracts/core/PendleSCYImpl/PendleBtrflySCY.sol +125 -0
  15. package/contracts/core/PendleSCYImpl/PendleYearnVaultSCY.sol +115 -0
  16. package/contracts/core/PendleYieldContractFactory.sol +109 -0
  17. package/contracts/core/PendleYieldToken.sol +240 -0
  18. package/contracts/core/misc/BoringOwnable.sol +62 -0
  19. package/contracts/core/misc/BoringOwnableUpg.sol +64 -0
  20. package/contracts/core/misc/PendleJoeSwapHelperUpg.sol +82 -0
  21. package/contracts/core/router/PendleRouterCoreUpg.sol +219 -0
  22. package/contracts/core/router/PendleRouterProxy.sol +64 -0
  23. package/contracts/core/router/PendleRouterYTUpg.sol +134 -0
  24. package/contracts/core/router/base/PendleRouterOTBaseUpg.sol +218 -0
  25. package/contracts/core/router/base/PendleRouterSCYAndForgeBaseUpg.sol +141 -0
  26. package/contracts/core/router/base/PendleRouterYTBaseUpg.sol +212 -0
  27. package/contracts/interfaces/IAToken.sol +34 -0
  28. package/contracts/interfaces/IAavePool.sol +41 -0
  29. package/contracts/interfaces/IAaveRewardsController.sol +32 -0
  30. package/contracts/interfaces/IBenQiComptroller.sol +33 -0
  31. package/contracts/interfaces/IJoePair.sol +98 -0
  32. package/contracts/interfaces/IJoeRouter01.sol +162 -0
  33. package/contracts/interfaces/IPBaseToken.sol +9 -0
  34. package/contracts/interfaces/IPFlashCallBack.sol +10 -0
  35. package/contracts/interfaces/IPGovernanceManager.sol +28 -0
  36. package/contracts/interfaces/IPMarket.sol +57 -0
  37. package/contracts/interfaces/IPMarketAddRemoveCallback.sol +18 -0
  38. package/contracts/interfaces/IPMarketFactory.sol +8 -0
  39. package/contracts/interfaces/IPMarketSwapCallback.sol +10 -0
  40. package/contracts/interfaces/IPOwnershipToken.sol +15 -0
  41. package/contracts/interfaces/IPPermissionsV2.sol +28 -0
  42. package/contracts/interfaces/IPRouterCore.sol +108 -0
  43. package/contracts/interfaces/IPRouterYT.sol +47 -0
  44. package/contracts/interfaces/IPYieldContractFactory.sol +36 -0
  45. package/contracts/interfaces/IPYieldToken.sol +19 -0
  46. package/contracts/interfaces/IQiErc20.sol +35 -0
  47. package/contracts/interfaces/IQiToken.sol +29 -0
  48. package/contracts/interfaces/IREDACTEDStaking.sol +35 -0
  49. package/contracts/interfaces/IWETH.sol +34 -0
  50. package/contracts/interfaces/IWXBTRFLY.sol +37 -0
  51. package/contracts/interfaces/IYearnVault.sol +32 -0
  52. package/contracts/libraries/JoeLibrary.sol +121 -0
  53. package/contracts/libraries/SCYIndex.sol +55 -0
  54. package/contracts/libraries/helpers/ExpiryUtilsLib.sol +166 -0
  55. package/contracts/libraries/math/FixedPoint.sol +208 -0
  56. package/contracts/libraries/math/LogExpMath.sol +515 -0
  57. package/contracts/libraries/math/MarketApproxLib.sol +140 -0
  58. package/contracts/libraries/math/MarketMathLib.sol +606 -0
  59. package/contracts/periphery/PendleGovernanceManager.sol +36 -0
  60. package/contracts/periphery/PermissionsV2.sol +23 -0
  61. package/package.json +76 -0
  62. package/typechain-types/BoringOwnable.ts +194 -0
  63. package/typechain-types/BoringOwnableData.ts +100 -0
  64. package/typechain-types/BoringOwnableUpg.ts +194 -0
  65. package/typechain-types/BoringOwnableUpgData.ts +100 -0
  66. package/typechain-types/ERC1967UpgradeUpgradeable.ts +97 -0
  67. package/typechain-types/ERC20.ts +417 -0
  68. package/typechain-types/IAToken.ts +331 -0
  69. package/typechain-types/IAavePool.ts +192 -0
  70. package/typechain-types/IAaveRewardsController.ts +148 -0
  71. package/typechain-types/IBeaconUpgradeable.ts +87 -0
  72. package/typechain-types/IBenQiComptroller.ts +125 -0
  73. package/typechain-types/IERC1822ProxiableUpgradeable.ts +87 -0
  74. package/typechain-types/IERC20.ts +300 -0
  75. package/typechain-types/IERC20Metadata.ts +339 -0
  76. package/typechain-types/IJoePair.ts +888 -0
  77. package/typechain-types/IJoeRouter01.ts +1107 -0
  78. package/typechain-types/IPBaseToken.ts +365 -0
  79. package/typechain-types/IPFlashCallback.ts +115 -0
  80. package/typechain-types/IPGovernanceManager.ts +84 -0
  81. package/typechain-types/IPMarket.ts +701 -0
  82. package/typechain-types/IPMarketAddRemoveCallback.ts +169 -0
  83. package/typechain-types/IPMarketFactory.ts +109 -0
  84. package/typechain-types/IPMarketSwapCallback.ts +115 -0
  85. package/typechain-types/IPOwnershipToken.ts +488 -0
  86. package/typechain-types/IPRouterCore.ts +759 -0
  87. package/typechain-types/IPRouterYT.ts +405 -0
  88. package/typechain-types/IPYieldContractFactory.ts +192 -0
  89. package/typechain-types/IPYieldToken.ts +525 -0
  90. package/typechain-types/IPermissionsV2.ts +87 -0
  91. package/typechain-types/IQiErc20.ts +402 -0
  92. package/typechain-types/IQiToken.ts +327 -0
  93. package/typechain-types/IREDACTEDStaking.ts +216 -0
  94. package/typechain-types/ISuperComposableYield.ts +652 -0
  95. package/typechain-types/IWETH.ts +380 -0
  96. package/typechain-types/IWXBTRFLY.ts +254 -0
  97. package/typechain-types/IYearnVault.ts +152 -0
  98. package/typechain-types/PendleAaveV3SCY.ts +901 -0
  99. package/typechain-types/PendleBaseToken.ts +505 -0
  100. package/typechain-types/PendleBenQiErc20SCY.ts +914 -0
  101. package/typechain-types/PendleBtrflyScy.ts +794 -0
  102. package/typechain-types/PendleGovernanceManager.ts +187 -0
  103. package/typechain-types/PendleJoeSwapHelperUpg.ts +97 -0
  104. package/typechain-types/PendleMarket.ts +1008 -0
  105. package/typechain-types/PendleMarketFactory.ts +246 -0
  106. package/typechain-types/PendleOwnershipToken.ts +628 -0
  107. package/typechain-types/PendleRouterCoreUpg.ts +780 -0
  108. package/typechain-types/PendleRouterProxy.ts +417 -0
  109. package/typechain-types/PendleRouterSCYAndForgeBaseUpg.ts +98 -0
  110. package/typechain-types/PendleRouterYTBaseUpg.ts +134 -0
  111. package/typechain-types/PendleRouterYTUpg.ts +453 -0
  112. package/typechain-types/PendleYearnVaultScy.ts +784 -0
  113. package/typechain-types/PendleYieldContractFactory.ts +479 -0
  114. package/typechain-types/PendleYieldToken.ts +1030 -0
  115. package/typechain-types/PermissionsV2.ts +87 -0
  116. package/typechain-types/Proxy.ts +57 -0
  117. package/typechain-types/RewardManager.ts +172 -0
  118. package/typechain-types/SCYBase.ts +734 -0
  119. package/typechain-types/SCYBaseWithRewards.ts +819 -0
  120. package/typechain-types/UUPSUpgradeable.ts +197 -0
  121. package/typechain-types/common.ts +44 -0
  122. package/typechain-types/factories/BoringOwnableData__factory.ts +90 -0
  123. package/typechain-types/factories/BoringOwnableUpgData__factory.ts +94 -0
  124. package/typechain-types/factories/BoringOwnableUpg__factory.ts +139 -0
  125. package/typechain-types/factories/BoringOwnable__factory.ts +149 -0
  126. package/typechain-types/factories/ERC1967UpgradeUpgradeable__factory.ts +75 -0
  127. package/typechain-types/factories/ERC20__factory.ts +348 -0
  128. package/typechain-types/factories/IAToken__factory.ts +225 -0
  129. package/typechain-types/factories/IAavePool__factory.ts +99 -0
  130. package/typechain-types/factories/IAaveRewardsController__factory.ts +78 -0
  131. package/typechain-types/factories/IBeaconUpgradeable__factory.ts +39 -0
  132. package/typechain-types/factories/IBenQiComptroller__factory.ts +59 -0
  133. package/typechain-types/factories/IERC1822ProxiableUpgradeable__factory.ts +43 -0
  134. package/typechain-types/factories/IERC20Metadata__factory.ts +248 -0
  135. package/typechain-types/factories/IERC20__factory.ts +203 -0
  136. package/typechain-types/factories/IJoePair__factory.ts +674 -0
  137. package/typechain-types/factories/IJoeRouter01__factory.ts +772 -0
  138. package/typechain-types/factories/IPBaseToken__factory.ts +271 -0
  139. package/typechain-types/factories/IPFlashCallback__factory.ts +49 -0
  140. package/typechain-types/factories/IPGovernanceManager__factory.ts +39 -0
  141. package/typechain-types/factories/IPMarketAddRemoveCallback__factory.ts +86 -0
  142. package/typechain-types/factories/IPMarketFactory__factory.ts +58 -0
  143. package/typechain-types/factories/IPMarketSwapCallback__factory.ts +53 -0
  144. package/typechain-types/factories/IPMarket__factory.ts +559 -0
  145. package/typechain-types/factories/IPOwnershipToken__factory.ts +349 -0
  146. package/typechain-types/factories/IPRouterCore__factory.ts +516 -0
  147. package/typechain-types/factories/IPRouterYT__factory.ts +246 -0
  148. package/typechain-types/factories/IPYieldContractFactory__factory.ts +117 -0
  149. package/typechain-types/factories/IPYieldToken__factory.ts +383 -0
  150. package/typechain-types/factories/IPermissionsV2__factory.ts +39 -0
  151. package/typechain-types/factories/IQiErc20__factory.ts +270 -0
  152. package/typechain-types/factories/IQiToken__factory.ts +219 -0
  153. package/typechain-types/factories/IREDACTEDStaking__factory.ts +107 -0
  154. package/typechain-types/factories/ISuperComposableYield__factory.ts +438 -0
  155. package/typechain-types/factories/IWETH__factory.ts +261 -0
  156. package/typechain-types/factories/IWXBTRFLY__factory.ts +131 -0
  157. package/typechain-types/factories/IYearnVault__factory.ts +74 -0
  158. package/typechain-types/factories/PendleAaveV3SCY__factory.ts +727 -0
  159. package/typechain-types/factories/PendleBaseToken__factory.ts +386 -0
  160. package/typechain-types/factories/PendleBenQiErc20SCY__factory.ts +753 -0
  161. package/typechain-types/factories/PendleBtrflyScy__factory.ts +652 -0
  162. package/typechain-types/factories/PendleGovernanceManager__factory.ts +149 -0
  163. package/typechain-types/factories/PendleJoeSwapHelperUpg__factory.ts +56 -0
  164. package/typechain-types/factories/PendleMarketFactory__factory.ts +207 -0
  165. package/typechain-types/factories/PendleMarket__factory.ts +891 -0
  166. package/typechain-types/factories/PendleOwnershipToken__factory.ts +565 -0
  167. package/typechain-types/factories/PendleRouterCoreUpg__factory.ts +620 -0
  168. package/typechain-types/factories/PendleRouterProxy__factory.ts +316 -0
  169. package/typechain-types/factories/PendleRouterSCYAndForgeBaseUpg__factory.ts +56 -0
  170. package/typechain-types/factories/PendleRouterYTBaseUpg__factory.ts +66 -0
  171. package/typechain-types/factories/PendleRouterYTUpg__factory.ts +363 -0
  172. package/typechain-types/factories/PendleYearnVaultScy__factory.ts +630 -0
  173. package/typechain-types/factories/PendleYieldContractFactory__factory.ts +368 -0
  174. package/typechain-types/factories/PendleYieldToken__factory.ts +814 -0
  175. package/typechain-types/factories/PermissionsV2__factory.ts +36 -0
  176. package/typechain-types/factories/Proxy__factory.ts +28 -0
  177. package/typechain-types/factories/RewardManager__factory.ts +89 -0
  178. package/typechain-types/factories/SCYBaseWithRewards__factory.ts +535 -0
  179. package/typechain-types/factories/SCYBase__factory.ts +479 -0
  180. package/typechain-types/factories/UUPSUpgradeable__factory.ts +115 -0
  181. package/typechain-types/hardhat.d.ts +564 -0
  182. package/typechain-types/index.ts +122 -0
package/LICENSE ADDED
@@ -0,0 +1,100 @@
1
+ Business Source License 1.1
2
+
3
+ License text copyright (c) 2017 MariaDB Corporation Ab, All Rights Reserved.
4
+ "Business Source License" is a trademark of MariaDB Corporation Ab.
5
+
6
+ -----------------------------------------------------------------------------
7
+
8
+ Parameters
9
+
10
+ Licensor: Uniswap Labs
11
+
12
+ Licensed Work: Uniswap V3 Core
13
+ The Licensed Work is (c) 2021 Uniswap Labs
14
+
15
+ Additional Use Grant: Any uses listed and defined at
16
+ v3-core-license-grants.uniswap.eth
17
+
18
+ Change Date: The earlier of 2023-04-01 or a date specified at
19
+ v3-core-license-date.uniswap.eth
20
+
21
+ Change License: GNU General Public License v2.0 or later
22
+
23
+ -----------------------------------------------------------------------------
24
+
25
+ Terms
26
+
27
+ The Licensor hereby grants you the right to copy, modify, create derivative
28
+ works, redistribute, and make non-production use of the Licensed Work. The
29
+ Licensor may make an Additional Use Grant, above, permitting limited
30
+ production use.
31
+
32
+ Effective on the Change Date, or the fourth anniversary of the first publicly
33
+ available distribution of a specific version of the Licensed Work under this
34
+ License, whichever comes first, the Licensor hereby grants you rights under
35
+ the terms of the Change License, and the rights granted in the paragraph
36
+ above terminate.
37
+
38
+ If your use of the Licensed Work does not comply with the requirements
39
+ currently in effect as described in this License, you must purchase a
40
+ commercial license from the Licensor, its affiliated entities, or authorized
41
+ resellers, or you must refrain from using the Licensed Work.
42
+
43
+ All copies of the original and modified Licensed Work, and derivative works
44
+ of the Licensed Work, are subject to this License. This License applies
45
+ separately for each version of the Licensed Work and the Change Date may vary
46
+ for each version of the Licensed Work released by Licensor.
47
+
48
+ You must conspicuously display this License on each original or modified copy
49
+ of the Licensed Work. If you receive the Licensed Work in original or
50
+ modified form from a third party, the terms and conditions set forth in this
51
+ License apply to your use of that work.
52
+
53
+ Any use of the Licensed Work in violation of this License will automatically
54
+ terminate your rights under this License for the current and all other
55
+ versions of the Licensed Work.
56
+
57
+ This License does not grant you any right in any trademark or logo of
58
+ Licensor or its affiliates (provided that you may use a trademark or logo of
59
+ Licensor as expressly required by this License).
60
+
61
+ TO THE EXTENT PERMITTED BY APPLICABLE LAW, THE LICENSED WORK IS PROVIDED ON
62
+ AN "AS IS" BASIS. LICENSOR HEREBY DISCLAIMS ALL WARRANTIES AND CONDITIONS,
63
+ EXPRESS OR IMPLIED, INCLUDING (WITHOUT LIMITATION) WARRANTIES OF
64
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE, NON-INFRINGEMENT, AND
65
+ TITLE.
66
+
67
+ MariaDB hereby grants you permission to use this License’s text to license
68
+ your works, and to refer to it using the trademark "Business Source License",
69
+ as long as you comply with the Covenants of Licensor below.
70
+
71
+ -----------------------------------------------------------------------------
72
+
73
+ Covenants of Licensor
74
+
75
+ In consideration of the right to use this License’s text and the "Business
76
+ Source License" name and trademark, Licensor covenants to MariaDB, and to all
77
+ other recipients of the licensed work to be provided by Licensor:
78
+
79
+ 1. To specify as the Change License the GPL Version 2.0 or any later version,
80
+ or a license that is compatible with GPL Version 2.0 or a later version,
81
+ where "compatible" means that software provided under the Change License can
82
+ be included in a program with software provided under GPL Version 2.0 or a
83
+ later version. Licensor may specify additional Change Licenses without
84
+ limitation.
85
+
86
+ 2. To either: (a) specify an additional grant of rights to use that does not
87
+ impose any additional restriction on the right granted in this License, as
88
+ the Additional Use Grant; or (b) insert the text "None".
89
+
90
+ 3. To specify a Change Date.
91
+
92
+ 4. Not to modify this License in any other way.
93
+
94
+ -----------------------------------------------------------------------------
95
+
96
+ Notice
97
+
98
+ The Business Source License (this document, or the "License") is not an Open
99
+ Source license. However, the Licensed Work will eventually be made available
100
+ under an Open Source License, as stated in this License.
@@ -0,0 +1,68 @@
1
+ // SPDX-License-Identifier: GPL-3.0-or-later
2
+ /*
3
+ * MIT License
4
+ * ===========
5
+ *
6
+ * Permission is hereby granted, free of charge, to any person obtaining a copy
7
+ * of this software and associated documentation files (the "Software"), to deal
8
+ * in the Software without restriction, including without limitation the rights
9
+ * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
+ * copies of the Software, and to permit persons to whom the Software is
11
+ * furnished to do so, subject to the following conditions:
12
+ *
13
+ * The above copyright notice and this permission notice shall be included in all
14
+ * copies or substantial portions of the Software.
15
+ *
16
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
+ * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
+ * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
+ * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
+ * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
+ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22
+ */
23
+
24
+ pragma solidity 0.8.9;
25
+ import "openzeppelin-solidity/contracts/token/ERC20/extensions/IERC20Metadata.sol";
26
+
27
+ interface ISuperComposableYield is IERC20Metadata {
28
+ function mint(
29
+ address receiver,
30
+ address baseTokenIn,
31
+ uint256 minAmountScyOut
32
+ ) external returns (uint256 amountScyOut);
33
+
34
+ function redeem(
35
+ address receiver,
36
+ address baseTokenOut,
37
+ uint256 minAmountBaseOut
38
+ ) external returns (uint256 amountBaseOut);
39
+
40
+ function updateGlobalReward() external;
41
+
42
+ function updateUserReward(address user) external;
43
+
44
+ /// @notice if user == msg.sender, receiver can be any addresses
45
+ /// else, receiver must be msg.sender
46
+ function redeemReward(address user, address receiver)
47
+ external
48
+ returns (uint256[] memory outAmounts);
49
+
50
+ /**
51
+ * @notice scyIndexCurrent.mulDown(scyBalance) must return the asset balance of the account
52
+ * @notice vice-versa, if a user uses some amount of tokens equivalent to X asset, the amount of scy
53
+ he can mint must be X.divDown(scyIndexCurrent)
54
+ * @dev SCYUtils's assetToScy & scyToAsset should be used instead of raw multiplication
55
+ & division
56
+ */
57
+ function scyIndexCurrent() external returns (uint256);
58
+
59
+ function scyIndexStored() external view returns (uint256);
60
+
61
+ function getBaseTokens() external view returns (address[] memory);
62
+
63
+ function isValidBaseToken(address token) external view returns (bool);
64
+
65
+ function getRewardTokens() external view returns (address[] memory);
66
+
67
+ function assetDecimals() external view returns (uint8);
68
+ }
@@ -0,0 +1,14 @@
1
+ // SPDX-License-Identifier: GPL-3.0-or-later
2
+ pragma solidity 0.8.9;
3
+
4
+ library SCYUtils {
5
+ uint256 internal constant ONE = 1e18;
6
+
7
+ function scyToAsset(uint256 scyIndex, uint256 scyAmount) internal pure returns (uint256) {
8
+ return (scyAmount * scyIndex) / ONE;
9
+ }
10
+
11
+ function assetToScy(uint256 scyIndex, uint256 assetAmount) internal pure returns (uint256) {
12
+ return (assetAmount * ONE) / scyIndex;
13
+ }
14
+ }
@@ -0,0 +1,117 @@
1
+ // SPDX-License-Identifier: GPL-3.0-or-later
2
+ pragma solidity 0.8.9;
3
+ import "../ISuperComposableYield.sol";
4
+ import "openzeppelin-solidity/contracts/token/ERC20/utils/SafeERC20.sol";
5
+ import "../../libraries/math/FixedPoint.sol";
6
+
7
+ abstract contract RewardManager {
8
+ using SafeERC20 for IERC20;
9
+ using FixedPoint for uint256;
10
+
11
+ struct GlobalReward {
12
+ uint256 index;
13
+ uint256 lastBalance;
14
+ }
15
+
16
+ struct UserReward {
17
+ uint256 lastIndex;
18
+ uint256 accruedReward;
19
+ }
20
+
21
+ uint256 internal constant INITIAL_REWARD_INDEX = 1;
22
+
23
+ mapping(address => GlobalReward) public globalReward;
24
+ mapping(address => mapping(address => UserReward)) public userReward;
25
+
26
+ function _doTransferOutRewardsForUser(address user, address receiver)
27
+ internal
28
+ virtual
29
+ returns (uint256[] memory outAmounts)
30
+ {
31
+ address[] memory rewardTokens = getRewardTokens();
32
+
33
+ outAmounts = new uint256[](rewardTokens.length);
34
+ for (uint256 i = 0; i < rewardTokens.length; ++i) {
35
+ address token = rewardTokens[i];
36
+
37
+ outAmounts[i] = userReward[user][token].accruedReward;
38
+ userReward[user][token].accruedReward = 0;
39
+
40
+ globalReward[token].lastBalance -= outAmounts[i];
41
+
42
+ if (outAmounts[i] != 0) {
43
+ IERC20(token).safeTransfer(receiver, outAmounts[i]);
44
+ }
45
+ }
46
+ }
47
+
48
+ function _updateUserReward(
49
+ address user,
50
+ uint256 balanceOfUser,
51
+ uint256 totalSupply
52
+ ) internal virtual {
53
+ address[] memory rewardTokens = getRewardTokens();
54
+ _updateGlobalReward(rewardTokens, totalSupply);
55
+ _updateUserRewardSkipGlobal(rewardTokens, user, balanceOfUser);
56
+ }
57
+
58
+ function _updateGlobalReward(address[] memory rewardTokens, uint256 totalSupply)
59
+ internal
60
+ virtual
61
+ {
62
+ _redeemExternalReward();
63
+
64
+ _initGlobalReward(rewardTokens);
65
+
66
+ for (uint256 i = 0; i < rewardTokens.length; ++i) {
67
+ address token = rewardTokens[i];
68
+
69
+ uint256 currentRewardBalance = IERC20(token).balanceOf(address(this));
70
+
71
+ if (totalSupply != 0) {
72
+ globalReward[token].index += (currentRewardBalance -
73
+ globalReward[token].lastBalance).divDown(totalSupply);
74
+ }
75
+
76
+ globalReward[token].lastBalance = currentRewardBalance;
77
+ }
78
+ }
79
+
80
+ function _updateUserRewardSkipGlobal(
81
+ address[] memory rewardTokens,
82
+ address user,
83
+ uint256 balanceOfUser
84
+ ) internal virtual {
85
+ for (uint256 i = 0; i < rewardTokens.length; ++i) {
86
+ address token = rewardTokens[i];
87
+
88
+ uint256 userLastIndex = userReward[user][token].lastIndex;
89
+
90
+ if (userLastIndex == globalReward[token].index) continue;
91
+
92
+ if (userLastIndex == 0) {
93
+ // first time receiving this reward
94
+ userReward[user][token].lastIndex = globalReward[token].index;
95
+ continue;
96
+ }
97
+
98
+ uint256 rewardAmountPerUnit = globalReward[token].index - userLastIndex;
99
+ uint256 rewardFromUnit = balanceOfUser.mulDown(rewardAmountPerUnit);
100
+
101
+ userReward[user][token].accruedReward += rewardFromUnit;
102
+ userReward[user][token].lastIndex = globalReward[token].index;
103
+ }
104
+ }
105
+
106
+ function _initGlobalReward(address[] memory rewardTokens) internal virtual {
107
+ for (uint256 i = 0; i < rewardTokens.length; ++i) {
108
+ if (globalReward[rewardTokens[i]].index == 0) {
109
+ globalReward[rewardTokens[i]].index = INITIAL_REWARD_INDEX;
110
+ }
111
+ }
112
+ }
113
+
114
+ function getRewardTokens() public view virtual returns (address[] memory);
115
+
116
+ function _redeemExternalReward() internal virtual;
117
+ }
@@ -0,0 +1,126 @@
1
+ // SPDX-License-Identifier: GPL-3.0-or-later
2
+ pragma solidity 0.8.9;
3
+ import "../ISuperComposableYield.sol";
4
+ import "./RewardManager.sol";
5
+ import "openzeppelin-solidity/contracts/token/ERC20/utils/SafeERC20.sol";
6
+ import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
7
+ import "../../libraries/math/FixedPoint.sol";
8
+ import "../SCYUtils.sol";
9
+
10
+ /**
11
+ # CONDITIONS TO USE THIS PRESET:
12
+ - the token's balance must be static (i.e not increase on its own). Some examples of tokens don't
13
+ satisfy this restriction is AaveV2's aToken
14
+
15
+ */
16
+ abstract contract SCYBase is ERC20, ISuperComposableYield {
17
+ using SafeERC20 for IERC20;
18
+ using FixedPoint for uint256;
19
+
20
+ uint8 private immutable _scyDecimals;
21
+ uint8 private immutable _assetDecimals;
22
+
23
+ mapping(address => uint256) internal lastBalanceOf;
24
+
25
+ constructor(
26
+ string memory _name,
27
+ string memory _symbol,
28
+ uint8 __scyDecimals,
29
+ uint8 __assetDecimals
30
+ ) ERC20(_name, _symbol) {
31
+ _scyDecimals = __scyDecimals;
32
+ _assetDecimals = __assetDecimals;
33
+ }
34
+
35
+ /*///////////////////////////////////////////////////////////////
36
+ DEPOSIT/REDEEM USING BASE TOKENS
37
+ //////////////////////////////////////////////////////////////*/
38
+
39
+ function mint(
40
+ address receiver,
41
+ address baseTokenIn,
42
+ uint256 minAmountScyOut
43
+ ) public virtual override returns (uint256 amountScyOut) {
44
+ require(isValidBaseToken(baseTokenIn), "invalid base token");
45
+
46
+ uint256 amountBaseIn = _afterReceiveToken(baseTokenIn);
47
+
48
+ amountScyOut = _deposit(baseTokenIn, amountBaseIn);
49
+
50
+ require(amountScyOut >= minAmountScyOut, "insufficient out");
51
+
52
+ _mint(receiver, amountScyOut);
53
+ }
54
+
55
+ function redeem(
56
+ address receiver,
57
+ address baseTokenOut,
58
+ uint256 minAmountBaseOut
59
+ ) public virtual override returns (uint256 amountBaseOut) {
60
+ require(isValidBaseToken(baseTokenOut), "invalid base token");
61
+
62
+ uint256 amountScyRedeem = balanceOf(address(this));
63
+
64
+ _burn(address(this), amountScyRedeem);
65
+
66
+ amountBaseOut = _redeem(baseTokenOut, amountScyRedeem);
67
+
68
+ require(amountBaseOut >= minAmountBaseOut, "insufficient out");
69
+
70
+ IERC20(baseTokenOut).safeTransfer(receiver, amountBaseOut);
71
+ _afterSendToken(baseTokenOut);
72
+ }
73
+
74
+ function _deposit(address token, uint256 amountBase)
75
+ internal
76
+ virtual
77
+ returns (uint256 amountScyOut);
78
+
79
+ function _redeem(address token, uint256 amountScy)
80
+ internal
81
+ virtual
82
+ returns (uint256 amountBaseOut);
83
+
84
+ /*///////////////////////////////////////////////////////////////
85
+ SCY-INDEX
86
+ //////////////////////////////////////////////////////////////*/
87
+
88
+ function scyIndexCurrent() public virtual override returns (uint256 res);
89
+
90
+ function scyIndexStored() public view virtual override returns (uint256 res);
91
+
92
+ /*///////////////////////////////////////////////////////////////
93
+ MISC METADATA FUNCTIONS
94
+ //////////////////////////////////////////////////////////////*/
95
+
96
+ function decimals() public view virtual override(ERC20, IERC20Metadata) returns (uint8) {
97
+ return _scyDecimals;
98
+ }
99
+
100
+ function assetDecimals() public view virtual returns (uint8) {
101
+ return _assetDecimals;
102
+ }
103
+
104
+ function getBaseTokens() public view virtual override returns (address[] memory res);
105
+
106
+ function isValidBaseToken(address token) public view virtual override returns (bool);
107
+
108
+ /// @dev token should not be address(this) since it's expected that all scy in this contract
109
+ /// are for redemption, so updating the mapping is redundant
110
+ function _afterReceiveToken(address token) internal virtual returns (uint256 res) {
111
+ assert(token != address(this));
112
+ uint256 curBalance = IERC20(token).balanceOf(address(this));
113
+ res = curBalance - lastBalanceOf[token];
114
+ lastBalanceOf[token] = curBalance;
115
+ }
116
+
117
+ /// @dev token should not be address(this)
118
+ function _afterSendToken(address token) internal virtual {
119
+ assert(token != address(this));
120
+ lastBalanceOf[token] = IERC20(token).balanceOf(address(this));
121
+ }
122
+
123
+ /*///////////////////////////////////////////////////////////////
124
+ TRANSFER HOOKS
125
+ //////////////////////////////////////////////////////////////*/
126
+ }
@@ -0,0 +1,79 @@
1
+ // SPDX-License-Identifier: GPL-3.0-or-later
2
+ pragma solidity 0.8.9;
3
+ import "../ISuperComposableYield.sol";
4
+ import "./RewardManager.sol";
5
+ import "./SCYBase.sol";
6
+ import "openzeppelin-solidity/contracts/token/ERC20/utils/SafeERC20.sol";
7
+ import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
8
+ import "../../libraries/math/FixedPoint.sol";
9
+
10
+ /**
11
+ # CONDITIONS TO USE THIS PRESET:
12
+ - the token's balance must be static (i.e not increase on its own). Some examples of tokens don't
13
+ satisfy this restriction is AaveV2's aToken
14
+
15
+ */
16
+ abstract contract SCYBaseWithRewards is SCYBase, RewardManager {
17
+ using SafeERC20 for IERC20;
18
+ using FixedPoint for uint256;
19
+
20
+ constructor(
21
+ string memory _name,
22
+ string memory _symbol,
23
+ uint8 __scydecimals,
24
+ uint8 __assetDecimals
25
+ )
26
+ SCYBase(_name, _symbol, __scydecimals, __assetDecimals)
27
+ RewardManager()
28
+ // solhint-disable-next-line no-empty-blocks
29
+ {
30
+
31
+ }
32
+
33
+ /*///////////////////////////////////////////////////////////////
34
+ REWARDS-RELATED
35
+ //////////////////////////////////////////////////////////////*/
36
+
37
+ function redeemReward(address user, address receiver)
38
+ public
39
+ virtual
40
+ override
41
+ returns (uint256[] memory outAmounts)
42
+ {
43
+ if (user != msg.sender) require(receiver == user, "invalid receiver");
44
+ else require(receiver != address(0), "zero address");
45
+
46
+ _updateUserReward(user, balanceOf(user), totalSupply());
47
+ outAmounts = _doTransferOutRewardsForUser(user, receiver);
48
+ }
49
+
50
+ function updateGlobalReward() public virtual override {
51
+ address[] memory rewardTokens = getRewardTokens();
52
+ _updateGlobalReward(rewardTokens, totalSupply());
53
+ }
54
+
55
+ function updateUserReward(address user) public virtual override {
56
+ _updateUserReward(user, balanceOf(user), totalSupply());
57
+ }
58
+
59
+ function getRewardTokens()
60
+ public
61
+ view
62
+ virtual
63
+ override(ISuperComposableYield, RewardManager)
64
+ returns (address[] memory);
65
+
66
+ /*///////////////////////////////////////////////////////////////
67
+ TRANSFER HOOKS
68
+ //////////////////////////////////////////////////////////////*/
69
+ function _beforeTokenTransfer(
70
+ address from,
71
+ address to,
72
+ uint256 /*amount*/
73
+ ) internal virtual override {
74
+ address[] memory rewardTokens = getRewardTokens();
75
+ _updateGlobalReward(rewardTokens, totalSupply());
76
+ if (from != address(0)) _updateUserRewardSkipGlobal(rewardTokens, from, balanceOf(from));
77
+ if (to != address(0)) _updateUserRewardSkipGlobal(rewardTokens, to, balanceOf(to));
78
+ }
79
+ }
@@ -0,0 +1,39 @@
1
+ // SPDX-License-Identifier: GPL-3.0-or-later
2
+ pragma solidity 0.8.9;
3
+
4
+ import "openzeppelin-solidity/contracts/token/ERC20/ERC20.sol";
5
+ import "openzeppelin-solidity/contracts/utils/cryptography/ECDSA.sol";
6
+ import "../interfaces/IPBaseToken.sol";
7
+
8
+ abstract contract PendleBaseToken is ERC20, IPBaseToken {
9
+ uint8 private immutable _decimals;
10
+
11
+ uint256 public immutable timeCreated;
12
+ uint256 public immutable expiry;
13
+ address public immutable factory;
14
+
15
+ event Burn(address indexed user, uint256 amount);
16
+
17
+ event Mint(address indexed user, uint256 amount);
18
+
19
+ constructor(
20
+ string memory _name,
21
+ string memory _symbol,
22
+ uint8 __decimals,
23
+ uint256 _expiry
24
+ ) ERC20(_name, _symbol) {
25
+ require(_expiry > block.timestamp, "INVALID_EXPIRY");
26
+ _decimals = __decimals;
27
+ timeCreated = block.timestamp;
28
+ expiry = _expiry;
29
+ factory = msg.sender;
30
+ }
31
+
32
+ function decimals() public view virtual override(ERC20, IERC20Metadata) returns (uint8) {
33
+ return _decimals;
34
+ }
35
+
36
+ function isExpired() public view virtual returns (bool res) {
37
+ res = (block.timestamp >= expiry);
38
+ }
39
+ }