@pendle/core-v2 3.0.0-beta-11 → 3.0.0-beta-12

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 (316) hide show
  1. package/contracts/LiquidityMining/CrossChainMsg/PendleMsgReceiveEndpointUpg.sol +5 -23
  2. package/contracts/LiquidityMining/CrossChainMsg/PendleMsgReceiverAppUpg.sol +2 -5
  3. package/contracts/LiquidityMining/CrossChainMsg/PendleMsgSendEndpointUpg.sol +6 -20
  4. package/contracts/LiquidityMining/CrossChainMsg/PendleMsgSenderAppUpg.sol +6 -27
  5. package/contracts/LiquidityMining/CrossChainMsg/libraries/ExcessivelySafeCall.sol +1 -2
  6. package/contracts/LiquidityMining/CrossChainMsg/libraries/LayerZeroHelper.sol +1 -1
  7. package/contracts/LiquidityMining/GaugeController/PendleGaugeControllerBaseUpg.sol +16 -17
  8. package/contracts/LiquidityMining/GaugeController/PendleGaugeControllerMainchainUpg.sol +6 -7
  9. package/contracts/LiquidityMining/GaugeController/PendleGaugeControllerSidechainUpg.sol +7 -8
  10. package/contracts/LiquidityMining/PendleMerkleDistributor.sol +2 -11
  11. package/contracts/LiquidityMining/VeDistributor/PendleFeeDistributor.sol +14 -37
  12. package/contracts/LiquidityMining/VeDistributor/PendleFeeDistributorV2.sol +19 -34
  13. package/contracts/LiquidityMining/VotingController/PendleVotingControllerUpg.sol +4 -14
  14. package/contracts/LiquidityMining/VotingController/VotingControllerStorageUpg.sol +4 -16
  15. package/contracts/LiquidityMining/VotingEscrow/VotingEscrowPendleMainchain.sol +11 -39
  16. package/contracts/LiquidityMining/VotingEscrow/VotingEscrowPendleSidechain.sol +3 -10
  17. package/contracts/LiquidityMining/VotingEscrow/VotingEscrowTokenBase.sol +1 -1
  18. package/contracts/LiquidityMining/libraries/VeBalanceLib.sol +5 -20
  19. package/contracts/LiquidityMining/libraries/VeHistoryLib.sol +1 -3
  20. package/contracts/core/Market/MarketMathCore.sol +12 -37
  21. package/contracts/core/Market/OracleLib.sol +6 -24
  22. package/contracts/core/Market/PendleExternalRewardDistributor.sol +4 -10
  23. package/contracts/core/Market/PendleGauge.sol +3 -9
  24. package/contracts/core/Market/PendleMarket.sol +14 -39
  25. package/contracts/core/Market/PendleMarketFactory.sol +14 -21
  26. package/contracts/core/Market/v2/PendleGaugeV2.sol +8 -24
  27. package/contracts/core/Market/v2/PendleMarketFactoryV2.sol +7 -27
  28. package/contracts/core/Market/v2/PendleMarketV2.sol +13 -36
  29. package/contracts/core/Market/v3/PendleMarketFactoryV3.sol +137 -0
  30. package/contracts/core/Market/v3/PendleMarketV3.sol +360 -0
  31. package/contracts/core/RewardManager/PendleLinearDistributor.sol +143 -0
  32. package/contracts/core/RewardManager/RewardManager.sol +14 -7
  33. package/contracts/core/RewardManager/RewardManagerAbstract.sol +5 -17
  34. package/contracts/core/StandardizedYield/SYBase.sol +10 -40
  35. package/contracts/core/StandardizedYield/SYBaseUpg.sol +197 -0
  36. package/contracts/core/StandardizedYield/SYBaseWithRewards.sol +9 -41
  37. package/contracts/core/StandardizedYield/SYBaseWithRewardsUpg.sol +88 -0
  38. package/contracts/core/StandardizedYield/SYUtils.sol +1 -4
  39. package/contracts/core/StandardizedYield/StEthHelper.sol +3 -9
  40. package/contracts/core/StandardizedYield/implementations/AaveV3/PendleAaveV3SY.sol +94 -0
  41. package/contracts/core/StandardizedYield/implementations/AaveV3/libraries/AaveAdapterLib.sol +19 -0
  42. package/contracts/core/StandardizedYield/implementations/AaveV3/libraries/WadRayMath.sol +126 -0
  43. package/contracts/core/StandardizedYield/implementations/Ankr/PendleAnkrBNBSY.sol +4 -12
  44. package/contracts/core/StandardizedYield/implementations/Ape/sAPE.sol +5 -19
  45. package/contracts/core/StandardizedYield/implementations/BalancerStable/AuraEthxBbAWethSY.sol +4 -14
  46. package/contracts/core/StandardizedYield/implementations/BalancerStable/AuraSwEthBbAWethSYV2.sol +4 -14
  47. package/contracts/core/StandardizedYield/implementations/BalancerStable/{PendleAuraWethVethSY.sol → AuraWethVethSYUpg.sol} +13 -20
  48. package/contracts/core/StandardizedYield/implementations/BalancerStable/PendleAuraBbAWethSwethSY.sol +3 -7
  49. package/contracts/core/StandardizedYield/implementations/BalancerStable/PendleAuraWethAnkrethSYV2.sol +12 -29
  50. package/contracts/core/StandardizedYield/implementations/BalancerStable/PendleAuraWethRocketEthSYV2.sol +12 -29
  51. package/contracts/core/StandardizedYield/implementations/BalancerStable/PendleAuraWethStafiEthSY.sol +12 -29
  52. package/contracts/core/StandardizedYield/implementations/BalancerStable/PendleAuraWethWstethSYV2.sol +14 -39
  53. package/contracts/core/StandardizedYield/implementations/BalancerStable/PendleAuraWstethAnkreth.sol +7 -28
  54. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/ComposableStable/ComposableStableMath.sol +13 -30
  55. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/ComposableStable/ComposableStablePreviewProxy.sol +1 -1
  56. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/ComposableStable/ComposableStablePreviewV4.sol +50 -153
  57. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/ComposableStable/ComposableStablePreviewV5.sol +50 -157
  58. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/Linear/BbAPoolHelper.sol +4 -16
  59. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/Linear/LinearMath.sol +6 -25
  60. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/Linear/LinearPreview.sol +36 -34
  61. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/MetaStable/MetaStableMath.sol +4 -13
  62. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/MetaStable/MetaStablePreview.sol +20 -58
  63. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/PendleAuraBalancerStableLPSYV2.sol +28 -79
  64. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/PendleAuraBalancerStableLPSYV3Upg.sol +310 -0
  65. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/StablePoolUserData.sol +1 -3
  66. package/contracts/core/StandardizedYield/implementations/BalancerStable/base/StablePreviewBase.sol +2 -6
  67. package/contracts/core/StandardizedYield/implementations/BenQi/PendleQiTokenHelper.sol +5 -13
  68. package/contracts/core/StandardizedYield/implementations/BenQi/PendleQiTokenSY.sol +9 -29
  69. package/contracts/core/StandardizedYield/implementations/CamelotV1Volatile/CamelotRewardHelper.sol +2 -8
  70. package/contracts/core/StandardizedYield/implementations/CamelotV1Volatile/CamelotV1VolatileCommon.sol +1 -5
  71. package/contracts/core/StandardizedYield/implementations/CamelotV1Volatile/CamelotV1VolatileLpHelper.sol +8 -19
  72. package/contracts/core/StandardizedYield/implementations/CamelotV1Volatile/CamelotV1VolatilePreview.sol +7 -25
  73. package/contracts/core/StandardizedYield/implementations/CamelotV1Volatile/PendleCamelotV1VolatileSY.sol +8 -27
  74. package/contracts/core/StandardizedYield/implementations/ChainlinkRelayer/PendleChainlinkReceiver.sol +2 -2
  75. package/contracts/core/StandardizedYield/implementations/ChainlinkRelayer/PendleChainlinkRelayer.sol +4 -9
  76. package/contracts/core/StandardizedYield/implementations/Convex/PendleCurveFraxUsdcSY.sol +3 -15
  77. package/contracts/core/StandardizedYield/implementations/Convex/PendleCurveUsdd3CrvSY.sol +5 -23
  78. package/contracts/core/StandardizedYield/implementations/Convex/base/Curve3CrvPoolHelper.sol +10 -40
  79. package/contracts/core/StandardizedYield/implementations/Convex/base/CurveFraxUsdcPoolHelper.sol +2 -8
  80. package/contracts/core/StandardizedYield/implementations/Convex/base/CurveUsdd3CrvPoolHelper.sol +2 -7
  81. package/contracts/core/StandardizedYield/implementations/Convex/base/PendleConvexLPSY.sol +5 -20
  82. package/contracts/core/StandardizedYield/implementations/Flux/FluxTokenLib.sol +6 -4
  83. package/contracts/core/StandardizedYield/implementations/Flux/PendleFluxLendingSY.sol +3 -11
  84. package/contracts/core/StandardizedYield/implementations/GLP/GLPPreviewHelper.sol +3 -9
  85. package/contracts/core/StandardizedYield/implementations/GLP/GMTokenPricingHelper.sol +115 -0
  86. package/contracts/core/StandardizedYield/implementations/GLP/PendleGMV2TokenSY.sol +134 -0
  87. package/contracts/core/StandardizedYield/implementations/GLP/PendleGlpSY.sol +5 -19
  88. package/contracts/core/StandardizedYield/implementations/HMX/HLPPricingHelper.sol +3 -4
  89. package/contracts/core/StandardizedYield/implementations/HMX/PendleHlpSY.sol +18 -25
  90. package/contracts/core/StandardizedYield/implementations/Kyber/KyberMathHelper.sol +425 -0
  91. package/contracts/core/StandardizedYield/implementations/Kyber/KyberNftManagerBaseUpg.sol +438 -0
  92. package/contracts/core/StandardizedYield/implementations/Kyber/PendleKyberElasticSYUpg.sol +155 -0
  93. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/FullMath.sol +123 -0
  94. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/LiqDeltaMath.sol +17 -0
  95. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/LiquidityMath.sol +74 -0
  96. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/MathConstants.sol +19 -0
  97. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/QtyDeltaMath.sol +103 -0
  98. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/QuadMath.sol +31 -0
  99. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/ReinvestmentMath.sol +26 -0
  100. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/SafeCast.sol +73 -0
  101. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/SwapMath.sol +272 -0
  102. package/contracts/core/StandardizedYield/implementations/Kyber/libraries/TickMath.sol +223 -0
  103. package/contracts/core/StandardizedYield/implementations/MUX/MlpPricingHelper.sol +26 -0
  104. package/contracts/core/StandardizedYield/implementations/MUX/PendleMlpSY.sol +195 -0
  105. package/contracts/core/StandardizedYield/implementations/MantleStakedEth/PendleMantleStakedEthSY.sol +12 -41
  106. package/contracts/core/StandardizedYield/implementations/MantleStakedEth/PendleOracleForSyProxy.sol +1 -1
  107. package/contracts/core/StandardizedYield/implementations/PendleArbitrumStakedEthSY.sol +13 -42
  108. package/contracts/core/StandardizedYield/implementations/PendleBridgedLSDSY.sol +4 -11
  109. package/contracts/core/StandardizedYield/implementations/PendleERC4626SY.sol +4 -16
  110. package/contracts/core/StandardizedYield/implementations/PendleEUSDSY.sol +3 -11
  111. package/contracts/core/StandardizedYield/implementations/PendleGdaiSY.sol +3 -11
  112. package/contracts/core/StandardizedYield/implementations/PendleLooksStakingSY.sol +5 -18
  113. package/contracts/core/StandardizedYield/implementations/PendleSfrxEthSY.sol +3 -7
  114. package/contracts/core/StandardizedYield/implementations/PendleStargateLPSY.sol +5 -18
  115. package/contracts/core/StandardizedYield/implementations/PendleWbEthSY.sol +3 -10
  116. package/contracts/core/StandardizedYield/implementations/PendleWstEthSY.sol +4 -12
  117. package/contracts/core/StandardizedYield/implementations/Silo/PendleSiloWithIncentiveSY.sol +157 -0
  118. package/contracts/core/StandardizedYield/implementations/Stader/PendleETHXSY.sol +4 -12
  119. package/contracts/core/StandardizedYield/implementations/Swell/PendleSwETH.sol +4 -12
  120. package/contracts/core/StandardizedYield/implementations/Thena/PendleThenaSY.sol +9 -29
  121. package/contracts/core/StandardizedYield/implementations/Thena/ThenaLpHelper.sol +3 -10
  122. package/contracts/core/StandardizedYield/implementations/Thena/ThenaMath.sol +9 -30
  123. package/contracts/core/StandardizedYield/implementations/Thena/ThenaPreview.sol +10 -40
  124. package/contracts/core/YieldContracts/InterestManagerYT.sol +6 -8
  125. package/contracts/core/YieldContracts/PendlePrincipalToken.sol +1 -1
  126. package/contracts/core/YieldContracts/PendleYieldContractFactory.sol +2 -3
  127. package/contracts/core/YieldContracts/PendleYieldToken.sol +17 -44
  128. package/contracts/core/YieldContractsV2/InterestManagerYTV2.sol +5 -14
  129. package/contracts/core/YieldContractsV2/PendlePrincipalTokenV2.sol +1 -1
  130. package/contracts/core/YieldContractsV2/PendleYieldContractFactoryV2.sol +2 -3
  131. package/contracts/core/YieldContractsV2/PendleYieldTokenV2.sol +13 -48
  132. package/contracts/core/erc20/PendleERC20.sol +2 -8
  133. package/contracts/core/erc20/PendleERC20Permit.sol +2 -6
  134. package/contracts/core/erc20/PendleERC20PermitUpg.sol +82 -0
  135. package/contracts/core/erc20/PendleERC20Upg.sol +334 -0
  136. package/contracts/core/libraries/ArrayLib.sol +7 -8
  137. package/contracts/core/libraries/BaseSplitCodeFactory.sol +1 -6
  138. package/contracts/core/libraries/Errors.sol +19 -1
  139. package/contracts/core/libraries/ExpiryUtilsLib.sol +3 -26
  140. package/contracts/core/libraries/StringLib.sol +5 -21
  141. package/contracts/core/libraries/TokenHelper.sol +3 -3
  142. package/contracts/core/libraries/math/LogExpMath.sol +1 -4
  143. package/contracts/core/libraries/math/PMath.sol +13 -15
  144. package/contracts/interfaces/AaveV3/IAaveV3AToken.sol +15 -0
  145. package/contracts/interfaces/AaveV3/IAaveV3Pool.sol +11 -0
  146. package/contracts/interfaces/Balancer/IComposableStable.sol +3 -4
  147. package/contracts/interfaces/Balancer/IERC4626LinearPool.sol +2 -10
  148. package/contracts/interfaces/Balancer/IMetaStablePool.sol +3 -11
  149. package/contracts/interfaces/Balancer/IVault.sol +3 -8
  150. package/contracts/interfaces/BinanceEth/IWBETH.sol +1 -1
  151. package/contracts/interfaces/Camelot/ICamelotNFTFactory.sol +1 -3
  152. package/contracts/interfaces/Camelot/ICamelotNFTHandler.sol +2 -10
  153. package/contracts/interfaces/Camelot/ICamelotNitroPool.sol +0 -1
  154. package/contracts/interfaces/Camelot/ICamelotNitroPoolFactory.sol +3 -12
  155. package/contracts/interfaces/Camelot/ICamelotPair.sol +5 -25
  156. package/contracts/interfaces/Camelot/ICamelotRouter.sol +1 -5
  157. package/contracts/interfaces/Camelot/IXGrail.sol +3 -11
  158. package/contracts/interfaces/ConvexCurve/IBooster.sol +1 -3
  159. package/contracts/interfaces/Curve/ICrvPool.sol +3 -12
  160. package/contracts/interfaces/Curve/ITriCrvPool.sol +2 -9
  161. package/contracts/interfaces/Flux/IFluxErc20.sol +1 -1
  162. package/contracts/interfaces/Flux/IFluxInterestRateModel.sol +1 -5
  163. package/contracts/interfaces/GMX/IGMXPriceHelper.sol +6 -0
  164. package/contracts/interfaces/GMX/IGMXVault.sol +2 -9
  165. package/contracts/interfaces/GMX/IRewardRouterV2.sol +1 -4
  166. package/contracts/interfaces/HMX/IHLPStaking.sol +1 -1
  167. package/contracts/interfaces/HMX/IHMXCalculator.sol +1 -1
  168. package/contracts/interfaces/HMX/IHMXCompounder.sol +1 -1
  169. package/contracts/interfaces/HMX/IHMXStaking.sol +1 -1
  170. package/contracts/interfaces/HMX/IHMXVester.sol +1 -1
  171. package/contracts/interfaces/IApeStaking.sol +3 -9
  172. package/contracts/interfaces/IBenQiInterestRateModel.sol +1 -5
  173. package/contracts/interfaces/IDiamondCut.sol +1 -5
  174. package/contracts/interfaces/IDiamondLoupe.sol +1 -3
  175. package/contracts/interfaces/ILayerZeroReceiver.sol +1 -6
  176. package/contracts/interfaces/IPActionAddRemoveLiqV3.sol +10 -2
  177. package/contracts/interfaces/IPActionInfoStatic.sol +4 -7
  178. package/contracts/interfaces/IPActionMarketAuxStatic.sol +5 -9
  179. package/contracts/interfaces/IPActionMarketCoreStatic.sol +118 -58
  180. package/contracts/interfaces/IPActionMintRedeemStatic.sol +38 -21
  181. package/contracts/interfaces/IPActionMiscV3.sol +51 -26
  182. package/contracts/interfaces/IPActionStorageStatic.sol +5 -1
  183. package/contracts/interfaces/IPAllActionTypeV3.sol +1 -0
  184. package/contracts/interfaces/IPBulkSeller.sol +49 -0
  185. package/contracts/interfaces/IPBulkSellerFactory.sol +12 -0
  186. package/contracts/interfaces/IPBulkSellerSYCallback.sol +6 -0
  187. package/contracts/interfaces/IPFeeDistributor.sol +1 -3
  188. package/contracts/interfaces/IPFeeDistributorV2.sol +8 -7
  189. package/contracts/interfaces/IPGaugeController.sol +2 -8
  190. package/contracts/interfaces/IPGaugeControllerMainchain.sol +1 -5
  191. package/contracts/interfaces/IPInterestManagerYT.sol +3 -3
  192. package/contracts/interfaces/IPInterestManagerYTV2.sol +1 -4
  193. package/contracts/interfaces/IPLimitRouter.sol +24 -19
  194. package/contracts/interfaces/IPLinearDistributor.sol +21 -0
  195. package/contracts/interfaces/IPMarket.sol +4 -17
  196. package/contracts/interfaces/IPMarketFactory.sol +2 -13
  197. package/contracts/interfaces/IPMarketFactoryV3.sol +24 -0
  198. package/contracts/interfaces/IPMarketV3.sol +8 -0
  199. package/contracts/interfaces/IPOffchainStorage.sol +11 -0
  200. package/contracts/interfaces/IPPriceFeed.sol +6 -0
  201. package/contracts/interfaces/IPPtOracle.sol +2 -9
  202. package/contracts/interfaces/IPRouterHelper.sol +11 -7
  203. package/contracts/interfaces/IPRouterStatic.sol +1 -3
  204. package/contracts/interfaces/IPVotingController.sol +7 -19
  205. package/contracts/interfaces/IPVotingEscrowMainchain.sol +2 -8
  206. package/contracts/interfaces/IPYieldToken.sol +2 -8
  207. package/contracts/interfaces/IPYieldTokenV2.sol +1 -6
  208. package/contracts/interfaces/IPendlePreviewHelper.sol +2 -8
  209. package/contracts/interfaces/IRewardManager.sol +1 -4
  210. package/contracts/interfaces/IStETH.sol +1 -4
  211. package/contracts/interfaces/IStandardizedYield.sol +9 -16
  212. package/contracts/interfaces/IStargateLP.sol +0 -3
  213. package/contracts/interfaces/IStargateRouter.sol +1 -5
  214. package/contracts/interfaces/Kyber/IKyberElasticFactory.sol +157 -0
  215. package/contracts/interfaces/Kyber/IKyberElasticPool.sol +101 -0
  216. package/contracts/interfaces/Kyber/IKyberElasticRouter.sol +98 -0
  217. package/contracts/interfaces/Kyber/IKyberLiquidityMining.sol +209 -0
  218. package/contracts/interfaces/Kyber/IKyberMathHelper.sol +28 -0
  219. package/contracts/interfaces/Kyber/IKyberPositionManager.sol +179 -0
  220. package/contracts/interfaces/Lybra/IEUSD.sol +1 -1
  221. package/contracts/interfaces/MUX/IMUXRewardRouter.sol +139 -0
  222. package/contracts/interfaces/Silo/ISilo.sol +298 -0
  223. package/contracts/interfaces/Silo/ISiloIncentiveController.sol +16 -0
  224. package/contracts/interfaces/Silo/ISiloLens.sol +6 -0
  225. package/contracts/interfaces/Stader/IStaderStakeManager.sol +2 -2
  226. package/contracts/interfaces/Thena/IThenaFactory.sol +1 -5
  227. package/contracts/interfaces/Thena/IThenaGaugeV2.sol +1 -2
  228. package/contracts/interfaces/Thena/IThenaPair.sol +3 -5
  229. package/contracts/limit/LimitMathCore.sol +69 -60
  230. package/contracts/limit/{PendleOrderMixin.sol → LimitRouterBase.sol} +96 -79
  231. package/contracts/limit/PendleLimitRouter.sol +9 -13
  232. package/contracts/offchain-helpers/AutomateReady.sol +17 -32
  233. package/contracts/offchain-helpers/BaseSplitCodeFactoryContract.sol +6 -3
  234. package/contracts/offchain-helpers/BoringLpSeller.sol +4 -17
  235. package/contracts/offchain-helpers/BoringPtSeller.sol +2 -7
  236. package/contracts/offchain-helpers/Broadcaster.sol +1 -1
  237. package/contracts/offchain-helpers/BytesLib.sol +26 -14
  238. package/contracts/offchain-helpers/EmptyUUPS.sol +0 -1
  239. package/contracts/offchain-helpers/LegacyNFTHelper.sol +4 -23
  240. package/contracts/offchain-helpers/LimitBackendHelper.sol +25 -16
  241. package/contracts/offchain-helpers/MarketExchangeRateLib.sol +2 -13
  242. package/contracts/offchain-helpers/Multicall2.sol +2 -7
  243. package/contracts/offchain-helpers/PendleMulticallV1.sol +7 -7
  244. package/contracts/offchain-helpers/PendleMulticallV2.sol +12 -13
  245. package/contracts/offchain-helpers/PendleOffchainStorage.sol +59 -0
  246. package/contracts/offchain-helpers/SimulateHelper.sol +6 -7
  247. package/contracts/offchain-helpers/errors/SDKErrorsDirectory.sol +15 -1
  248. package/contracts/oracles/PendleLpOracleLib.sol +9 -10
  249. package/contracts/oracles/PendlePtOracle.sol +8 -19
  250. package/contracts/oracles/PendlePtOracleLib.sol +8 -13
  251. package/contracts/oracles/samples/PendleLpGlpOracle.sol +6 -10
  252. package/contracts/oracles/samples/PendlePtGlpOracle.sol +6 -10
  253. package/contracts/oracles/samples/PendlePtUsdChainlinkOracle.sol +8 -15
  254. package/contracts/router/ActionAddRemoveLiqV3.sol +147 -52
  255. package/contracts/router/ActionCallbackV3.sol +11 -4
  256. package/contracts/router/ActionMiscV3.sol +43 -33
  257. package/contracts/router/ActionSwapPTV3.sol +18 -6
  258. package/contracts/router/ActionSwapYTV3.sol +42 -14
  259. package/contracts/router/PendleRouterHelper.sol +72 -40
  260. package/contracts/router/PendleRouterV3.sol +50 -50
  261. package/contracts/router/base/ActionBase.sol +86 -43
  262. package/contracts/router/base/CallbackHelper.sol +18 -20
  263. package/contracts/router/base/MarketApproxLib.sol +36 -32
  264. package/contracts/router/swap-aggregator/PendleSwap.sol +31 -7
  265. package/contracts/router/swap-aggregator/kyberswap/interfaces/IAggregationExecutorOptimistic.sol +63 -0
  266. package/contracts/router/swap-aggregator/kyberswap/interfaces/IExecutorHelper.sol +314 -0
  267. package/contracts/router/swap-aggregator/kyberswap/interfaces/IExecutorHelperL2.sol +560 -0
  268. package/contracts/router/swap-aggregator/kyberswap/{IMetaAggregationRouterV2.sol → interfaces/IMetaAggregationRouterV2.sol} +11 -6
  269. package/contracts/router/swap-aggregator/kyberswap/{KyberInputScalingHelper.sol → l1-contracts/InputScalingHelper.sol} +70 -19
  270. package/contracts/router/swap-aggregator/kyberswap/{ScalingDataLib.sol → l1-contracts/ScalingDataLib.sol} +111 -53
  271. package/contracts/router/swap-aggregator/kyberswap/l2-contracts/BytesHelper.sol +29 -0
  272. package/contracts/router/swap-aggregator/kyberswap/l2-contracts/CalldataReader.sol +149 -0
  273. package/contracts/router/swap-aggregator/kyberswap/l2-contracts/CalldataWriter.sol +118 -0
  274. package/contracts/router/swap-aggregator/kyberswap/l2-contracts/Common.sol +42 -0
  275. package/contracts/router/swap-aggregator/kyberswap/l2-contracts/DexScaler.sol +382 -0
  276. package/contracts/router/swap-aggregator/kyberswap/l2-contracts/ExecutorReader.sol +61 -0
  277. package/contracts/router/swap-aggregator/kyberswap/l2-contracts/InputScalingHelperL2.sol +329 -0
  278. package/contracts/router/swap-aggregator/kyberswap/l2-contracts/ScalingDataL2Lib.sol +173 -0
  279. package/contracts/router/swap-aggregator/oneinch/I1inchAggregationRouterV5.sol +4 -10
  280. package/contracts/router/swap-aggregator/oneinch/OneInchAggregationRouterHelper.sol +20 -14
  281. package/deployments/1-core.json +5 -2
  282. package/deployments/1-markets/EPENDLE-SEP2024.json +12 -0
  283. package/deployments/1-markets/SFRAX-MARCH2024.json +10 -0
  284. package/deployments/1-markets/SILO-CRVUSD-MARCH2024.json +10 -0
  285. package/deployments/10-core.json +6 -3
  286. package/deployments/42161-core.json +5 -2
  287. package/deployments/42161-markets/AAVE-USDC-JUNE2024.json +11 -0
  288. package/deployments/42161-markets/ARBSILO-USDC-FEB2024.json +10 -0
  289. package/deployments/42161-markets/GM-ARB-USDC-MARCH2024.json +10 -0
  290. package/deployments/42161-markets/KYBER-AXLWSTETH-WSTETH-MARCH2024.json +10 -0
  291. package/deployments/42161-markets/MLP-MARCH2024.json +10 -0
  292. package/deployments/42161-markets/WSTETH-ARBITRUM-MARCH2024.json +11 -0
  293. package/deployments/42161-markets/WSTETHSILO-ETH-MARCH2024.json +11 -0
  294. package/deployments/56-core.json +5 -2
  295. package/deployments/merkle-distribution/2023-10-26.json +7300 -0
  296. package/deployments/merkle-distribution/2023-11-30.json +7730 -0
  297. package/package.json +8 -8
  298. package/contracts/limit/helpers/ArgumentsDecoder.sol +0 -47
  299. package/contracts/limit/helpers/Permitable.sol +0 -48
  300. package/contracts/limit/helpers/RevertReasonParser.sol +0 -75
  301. package/contracts/offchain-helpers/ChainlinkRelayerGelato.sol +0 -37
  302. package/contracts/offchain-helpers/PtAndLpToAsset.sol +0 -62
  303. package/contracts/offchain-helpers/router-static/PendleRouterStatic.sol +0 -19
  304. package/contracts/offchain-helpers/router-static/base/ActionInfoStatic.sol +0 -112
  305. package/contracts/offchain-helpers/router-static/base/ActionLimitOrdersStatic.sol +0 -145
  306. package/contracts/offchain-helpers/router-static/base/ActionMarketAuxStatic.sol +0 -171
  307. package/contracts/offchain-helpers/router-static/base/ActionMarketCoreStatic.sol +0 -582
  308. package/contracts/offchain-helpers/router-static/base/ActionMintRedeemStatic.sol +0 -116
  309. package/contracts/offchain-helpers/router-static/base/ActionStorageStatic.sol +0 -64
  310. package/contracts/offchain-helpers/router-static/base/ActionVePendleStatic.sol +0 -53
  311. package/contracts/offchain-helpers/router-static/base/StorageLayout.sol +0 -18
  312. package/contracts/router/swap-aggregator/kyberswap/IExecutorHelper.sol +0 -231
  313. package/deployments/1-markets/BALANCER-LP-AURA-VETH-WETH-SEP2024.json +0 -8
  314. /package/contracts/router/swap-aggregator/kyberswap/{IAggregationExecutor.sol → interfaces/IAggregationExecutor.sol} +0 -0
  315. /package/deployments/1-markets/{BALANCER-LP-AURA-BBAWETHSWETH-SEP-26-2024.json → deprecated/BALANCER-LP-AURA-BBAWETHSWETH-SEP-26-2024.json} +0 -0
  316. /package/deployments/1-markets/{BALANCER-LP-AURA-ETHX-BBAWETH-SEP-26-2024.json → deprecated/BALANCER-LP-AURA-ETHX-BBAWETH-SEP-26-2024.json} +0 -0
@@ -0,0 +1,272 @@
1
+ // SPDX-License-Identifier: MIT
2
+ pragma solidity >=0.8.0;
3
+
4
+ import {MathConstants as C} from "./MathConstants.sol";
5
+ import {FullMath} from "./FullMath.sol";
6
+ import {QuadMath} from "./QuadMath.sol";
7
+ import {SafeCast} from "./SafeCast.sol";
8
+
9
+ /**
10
+ * --------- [DIRECT COPIED FROM KYBERSWAP] ---------
11
+ */
12
+
13
+ /// @title Contains helper functions for swaps
14
+ library SwapMath {
15
+ using SafeCast for uint256;
16
+ using SafeCast for int256;
17
+
18
+ /// @dev Computes the actual swap input / output amounts to be deducted or added,
19
+ /// the swap fee to be collected and the resulting sqrtP.
20
+ /// @notice nextSqrtP should not exceed targetSqrtP.
21
+ /// @param liquidity active base liquidity + reinvest liquidity
22
+ /// @param currentSqrtP current sqrt price
23
+ /// @param targetSqrtP sqrt price limit the new sqrt price can take
24
+ /// @param feeInFeeUnits swap fee in basis points
25
+ /// @param specifiedAmount the amount remaining to be used for the swap
26
+ /// @param isExactInput true if specifiedAmount refers to input amount, false if specifiedAmount refers to output amount
27
+ /// @param isToken0 true if specifiedAmount is in token0, false if specifiedAmount is in token1
28
+ /// @return usedAmount actual amount to be used for the swap
29
+ /// @return returnedAmount output qty to be accumulated if isExactInput = true, input qty if isExactInput = false
30
+ /// @return deltaL collected swap fee, to be incremented to reinvest liquidity
31
+ /// @return nextSqrtP the new sqrt price after the computed swap step
32
+ function computeSwapStep(
33
+ uint256 liquidity,
34
+ uint160 currentSqrtP,
35
+ uint160 targetSqrtP,
36
+ uint256 feeInFeeUnits,
37
+ int256 specifiedAmount,
38
+ bool isExactInput,
39
+ bool isToken0
40
+ ) internal pure returns (int256 usedAmount, int256 returnedAmount, uint256 deltaL, uint160 nextSqrtP) {
41
+ // in the event currentSqrtP == targetSqrtP because of tick movements, return
42
+ // eg. swapped up tick where specified price limit is on an initialised tick
43
+ // then swapping down tick will cause next tick to be the same as the current tick
44
+ if (currentSqrtP == targetSqrtP) return (0, 0, 0, currentSqrtP);
45
+ usedAmount = calcReachAmount(liquidity, currentSqrtP, targetSqrtP, feeInFeeUnits, isExactInput, isToken0);
46
+
47
+ if ((isExactInput && usedAmount > specifiedAmount) || (!isExactInput && usedAmount <= specifiedAmount)) {
48
+ usedAmount = specifiedAmount;
49
+ } else {
50
+ nextSqrtP = targetSqrtP;
51
+ }
52
+
53
+ uint256 absDelta = usedAmount >= 0 ? uint256(usedAmount) : usedAmount.revToUint256();
54
+ if (nextSqrtP == 0) {
55
+ deltaL = estimateIncrementalLiquidity(
56
+ absDelta,
57
+ liquidity,
58
+ currentSqrtP,
59
+ feeInFeeUnits,
60
+ isExactInput,
61
+ isToken0
62
+ );
63
+ nextSqrtP = calcFinalPrice(absDelta, liquidity, deltaL, currentSqrtP, isExactInput, isToken0).toUint160();
64
+ } else {
65
+ deltaL = calcIncrementalLiquidity(absDelta, liquidity, currentSqrtP, nextSqrtP, isExactInput, isToken0);
66
+ }
67
+ returnedAmount = calcReturnedAmount(liquidity, currentSqrtP, nextSqrtP, deltaL, isExactInput, isToken0);
68
+ }
69
+
70
+ /// @dev calculates the amount needed to reach targetSqrtP from currentSqrtP
71
+ /// @dev we cast currentSqrtP and targetSqrtP to uint256 as they are multiplied by TWO_FEE_UNITS or feeInFeeUnits
72
+ function calcReachAmount(
73
+ uint256 liquidity,
74
+ uint256 currentSqrtP,
75
+ uint256 targetSqrtP,
76
+ uint256 feeInFeeUnits,
77
+ bool isExactInput,
78
+ bool isToken0
79
+ ) internal pure returns (int256 reachAmount) {
80
+ uint256 absPriceDiff;
81
+ unchecked {
82
+ absPriceDiff = (currentSqrtP >= targetSqrtP) ? (currentSqrtP - targetSqrtP) : (targetSqrtP - currentSqrtP);
83
+ }
84
+ if (isExactInput) {
85
+ // we round down so that we avoid taking giving away too much for the specified input
86
+ // ie. require less input qty to move ticks
87
+ if (isToken0) {
88
+ // numerator = 2 * liquidity * absPriceDiff
89
+ // denominator = currentSqrtP * (2 * targetSqrtP - currentSqrtP * feeInFeeUnits / FEE_UNITS)
90
+ // overflow should not happen because the absPriceDiff is capped to ~5%
91
+ uint256 denominator = C.TWO_FEE_UNITS * targetSqrtP - feeInFeeUnits * currentSqrtP;
92
+ uint256 numerator = FullMath.mulDivFloor(liquidity, C.TWO_FEE_UNITS * absPriceDiff, denominator);
93
+ reachAmount = FullMath.mulDivFloor(numerator, C.TWO_POW_96, currentSqrtP).toInt256();
94
+ } else {
95
+ // numerator = 2 * liquidity * absPriceDiff * currentSqrtP
96
+ // denominator = 2 * currentSqrtP - targetSqrtP * feeInFeeUnits / FEE_UNITS
97
+ // overflow should not happen because the absPriceDiff is capped to ~5%
98
+ uint256 denominator = C.TWO_FEE_UNITS * currentSqrtP - feeInFeeUnits * targetSqrtP;
99
+ uint256 numerator = FullMath.mulDivFloor(liquidity, C.TWO_FEE_UNITS * absPriceDiff, denominator);
100
+ reachAmount = FullMath.mulDivFloor(numerator, currentSqrtP, C.TWO_POW_96).toInt256();
101
+ }
102
+ } else {
103
+ // we will perform negation as the last step
104
+ // we round down so that we require less output qty to move ticks
105
+ if (isToken0) {
106
+ // numerator: (liquidity)(absPriceDiff)(2 * currentSqrtP - deltaL * (currentSqrtP + targetSqrtP))
107
+ // denominator: (currentSqrtP * targetSqrtP) * (2 * currentSqrtP - deltaL * targetSqrtP)
108
+ // overflow should not happen because the absPriceDiff is capped to ~5%
109
+ uint256 denominator = C.TWO_FEE_UNITS * currentSqrtP - feeInFeeUnits * targetSqrtP;
110
+ uint256 numerator = denominator - feeInFeeUnits * currentSqrtP;
111
+ numerator = FullMath.mulDivFloor(liquidity << C.RES_96, numerator, denominator);
112
+ reachAmount = (FullMath.mulDivFloor(numerator, absPriceDiff, currentSqrtP) / targetSqrtP).revToInt256();
113
+ } else {
114
+ // numerator: liquidity * absPriceDiff * (TWO_FEE_UNITS * targetSqrtP - feeInFeeUnits * (targetSqrtP + currentSqrtP))
115
+ // denominator: (TWO_FEE_UNITS * targetSqrtP - feeInFeeUnits * currentSqrtP)
116
+ // overflow should not happen because the absPriceDiff is capped to ~5%
117
+ uint256 denominator = C.TWO_FEE_UNITS * targetSqrtP - feeInFeeUnits * currentSqrtP;
118
+ uint256 numerator = denominator - feeInFeeUnits * targetSqrtP;
119
+ numerator = FullMath.mulDivFloor(liquidity, numerator, denominator);
120
+ reachAmount = FullMath.mulDivFloor(numerator, absPriceDiff, C.TWO_POW_96).revToInt256();
121
+ }
122
+ }
123
+ }
124
+
125
+ /// @dev estimates deltaL, the swap fee to be collected based on amount specified
126
+ /// for the final swap step to be performed,
127
+ /// where the next (temporary) tick will not be crossed
128
+ function estimateIncrementalLiquidity(
129
+ uint256 absDelta,
130
+ uint256 liquidity,
131
+ uint160 currentSqrtP,
132
+ uint256 feeInFeeUnits,
133
+ bool isExactInput,
134
+ bool isToken0
135
+ ) internal pure returns (uint256 deltaL) {
136
+ if (isExactInput) {
137
+ if (isToken0) {
138
+ // deltaL = feeInFeeUnits * absDelta * currentSqrtP / 2
139
+ deltaL = FullMath.mulDivFloor(currentSqrtP, absDelta * feeInFeeUnits, C.TWO_FEE_UNITS << C.RES_96);
140
+ } else {
141
+ // deltaL = feeInFeeUnits * absDelta * / (currentSqrtP * 2)
142
+ // Because nextSqrtP = (liquidity + absDelta / currentSqrtP) * currentSqrtP / (liquidity + deltaL)
143
+ // so we round up deltaL, to round down nextSqrtP
144
+ deltaL = FullMath.mulDivFloor(C.TWO_POW_96, absDelta * feeInFeeUnits, C.TWO_FEE_UNITS * currentSqrtP);
145
+ }
146
+ } else {
147
+ // obtain the smaller root of the quadratic equation
148
+ // ax^2 - 2bx + c = 0 such that b > 0, and x denotes deltaL
149
+ uint256 a = feeInFeeUnits;
150
+ uint256 b = (C.FEE_UNITS - feeInFeeUnits) * liquidity;
151
+ uint256 c = feeInFeeUnits * liquidity * absDelta;
152
+ if (isToken0) {
153
+ // a = feeInFeeUnits
154
+ // b = (FEE_UNITS - feeInFeeUnits) * liquidity - FEE_UNITS * absDelta * currentSqrtP
155
+ // c = feeInFeeUnits * liquidity * absDelta * currentSqrtP
156
+ b -= FullMath.mulDivFloor(C.FEE_UNITS * absDelta, currentSqrtP, C.TWO_POW_96);
157
+ c = FullMath.mulDivFloor(c, currentSqrtP, C.TWO_POW_96);
158
+ } else {
159
+ // a = feeInFeeUnits
160
+ // b = (FEE_UNITS - feeInFeeUnits) * liquidity - FEE_UNITS * absDelta / currentSqrtP
161
+ // c = liquidity * feeInFeeUnits * absDelta / currentSqrtP
162
+ b -= FullMath.mulDivFloor(C.FEE_UNITS * absDelta, C.TWO_POW_96, currentSqrtP);
163
+ c = FullMath.mulDivFloor(c, C.TWO_POW_96, currentSqrtP);
164
+ }
165
+ deltaL = QuadMath.getSmallerRootOfQuadEqn(a, b, c);
166
+ }
167
+ }
168
+
169
+ /// @dev calculates deltaL, the swap fee to be collected for an intermediate swap step,
170
+ /// where the next (temporary) tick will be crossed
171
+ function calcIncrementalLiquidity(
172
+ uint256 absDelta,
173
+ uint256 liquidity,
174
+ uint160 currentSqrtP,
175
+ uint160 nextSqrtP,
176
+ bool isExactInput,
177
+ bool isToken0
178
+ ) internal pure returns (uint256 deltaL) {
179
+ if (isToken0) {
180
+ // deltaL = nextSqrtP * (liquidity / currentSqrtP +/- absDelta)) - liquidity
181
+ // needs to be minimum
182
+ uint256 tmp1 = FullMath.mulDivFloor(liquidity, C.TWO_POW_96, currentSqrtP);
183
+ uint256 tmp2 = isExactInput ? tmp1 + absDelta : tmp1 - absDelta;
184
+ uint256 tmp3 = FullMath.mulDivFloor(nextSqrtP, tmp2, C.TWO_POW_96);
185
+ // in edge cases where liquidity or absDelta is small
186
+ // liquidity might be greater than nextSqrtP * ((liquidity / currentSqrtP) +/- absDelta))
187
+ // due to rounding
188
+ deltaL = (tmp3 > liquidity) ? tmp3 - liquidity : 0;
189
+ } else {
190
+ // deltaL = (liquidity * currentSqrtP +/- absDelta) / nextSqrtP - liquidity
191
+ // needs to be minimum
192
+ uint256 tmp1 = FullMath.mulDivFloor(liquidity, currentSqrtP, C.TWO_POW_96);
193
+ uint256 tmp2 = isExactInput ? tmp1 + absDelta : tmp1 - absDelta;
194
+ uint256 tmp3 = FullMath.mulDivFloor(tmp2, C.TWO_POW_96, nextSqrtP);
195
+ // in edge cases where liquidity or absDelta is small
196
+ // liquidity might be greater than nextSqrtP * ((liquidity / currentSqrtP) +/- absDelta))
197
+ // due to rounding
198
+ deltaL = (tmp3 > liquidity) ? tmp3 - liquidity : 0;
199
+ }
200
+ }
201
+
202
+ /// @dev calculates the sqrt price of the final swap step
203
+ /// where the next (temporary) tick will not be crossed
204
+ function calcFinalPrice(
205
+ uint256 absDelta,
206
+ uint256 liquidity,
207
+ uint256 deltaL,
208
+ uint160 currentSqrtP,
209
+ bool isExactInput,
210
+ bool isToken0
211
+ ) internal pure returns (uint256) {
212
+ if (isToken0) {
213
+ // if isExactInput: swap 0 -> 1, sqrtP decreases, we round up
214
+ // else swap: 1 -> 0, sqrtP increases, we round down
215
+ uint256 tmp = FullMath.mulDivFloor(absDelta, currentSqrtP, C.TWO_POW_96);
216
+ if (isExactInput) {
217
+ return FullMath.mulDivCeiling(liquidity + deltaL, currentSqrtP, liquidity + tmp);
218
+ } else {
219
+ return FullMath.mulDivFloor(liquidity + deltaL, currentSqrtP, liquidity - tmp);
220
+ }
221
+ } else {
222
+ // if isExactInput: swap 1 -> 0, sqrtP increases, we round down
223
+ // else swap: 0 -> 1, sqrtP decreases, we round up
224
+ uint256 tmp = FullMath.mulDivFloor(absDelta, C.TWO_POW_96, currentSqrtP);
225
+ if (isExactInput) {
226
+ return FullMath.mulDivFloor(liquidity + tmp, currentSqrtP, liquidity + deltaL);
227
+ } else {
228
+ return FullMath.mulDivCeiling(liquidity - tmp, currentSqrtP, liquidity + deltaL);
229
+ }
230
+ }
231
+ }
232
+
233
+ /// @dev calculates returned output | input tokens in exchange for specified amount
234
+ /// @dev round down when calculating returned output (isExactInput) so we avoid sending too much
235
+ /// @dev round up when calculating returned input (!isExactInput) so we get desired output amount
236
+ function calcReturnedAmount(
237
+ uint256 liquidity,
238
+ uint160 currentSqrtP,
239
+ uint160 nextSqrtP,
240
+ uint256 deltaL,
241
+ bool isExactInput,
242
+ bool isToken0
243
+ ) internal pure returns (int256 returnedAmount) {
244
+ if (isToken0) {
245
+ if (isExactInput) {
246
+ // minimise actual output (<0, make less negative) so we avoid sending too much
247
+ // returnedAmount = deltaL * nextSqrtP - liquidity * (currentSqrtP - nextSqrtP)
248
+ returnedAmount =
249
+ FullMath.mulDivCeiling(deltaL, nextSqrtP, C.TWO_POW_96).toInt256() +
250
+ FullMath.mulDivFloor(liquidity, currentSqrtP - nextSqrtP, C.TWO_POW_96).revToInt256();
251
+ } else {
252
+ // maximise actual input (>0) so we get desired output amount
253
+ // returnedAmount = deltaL * nextSqrtP + liquidity * (nextSqrtP - currentSqrtP)
254
+ returnedAmount =
255
+ FullMath.mulDivCeiling(deltaL, nextSqrtP, C.TWO_POW_96).toInt256() +
256
+ FullMath.mulDivCeiling(liquidity, nextSqrtP - currentSqrtP, C.TWO_POW_96).toInt256();
257
+ }
258
+ } else {
259
+ // returnedAmount = (liquidity + deltaL)/nextSqrtP - (liquidity)/currentSqrtP
260
+ // if exactInput, minimise actual output (<0, make less negative) so we avoid sending too much
261
+ // if exactOutput, maximise actual input (>0) so we get desired output amount
262
+ returnedAmount =
263
+ FullMath.mulDivCeiling(liquidity + deltaL, C.TWO_POW_96, nextSqrtP).toInt256() +
264
+ FullMath.mulDivFloor(liquidity, C.TWO_POW_96, currentSqrtP).revToInt256();
265
+ }
266
+
267
+ if (isExactInput && returnedAmount == 1) {
268
+ // rounding make returnedAmount == 1
269
+ returnedAmount = 0;
270
+ }
271
+ }
272
+ }
@@ -0,0 +1,223 @@
1
+ // SPDX-License-Identifier: GPL-2.0-or-later
2
+ pragma solidity >=0.8.0;
3
+
4
+ /**
5
+ * --------- [DIRECT COPIED FROM KYBERSWAP] ---------
6
+ */
7
+
8
+ /// @title Math library for computing sqrt prices from ticks and vice versa
9
+ /// @notice Computes sqrt price for ticks of size 1.0001, i.e. sqrt(1.0001^tick) as fixed point Q64.96 numbers. Supports
10
+ /// prices between 2**-128 and 2**128
11
+ library TickMath {
12
+ /// @dev The minimum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**-128
13
+ int24 internal constant MIN_TICK = -887272;
14
+ /// @dev The maximum tick that may be passed to #getSqrtRatioAtTick computed from log base 1.0001 of 2**128
15
+ int24 internal constant MAX_TICK = -MIN_TICK;
16
+
17
+ /// @dev The minimum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MIN_TICK)
18
+ uint160 internal constant MIN_SQRT_RATIO = 4295128739;
19
+ /// @dev The maximum value that can be returned from #getSqrtRatioAtTick. Equivalent to getSqrtRatioAtTick(MAX_TICK)
20
+ uint160 internal constant MAX_SQRT_RATIO = 1461446703485210103287273052203988822378723970342;
21
+
22
+ /// @notice Calculates sqrt(1.0001^tick) * 2^96
23
+ /// @dev Throws if |tick| > max tick
24
+ /// @param tick The input tick for the above formula
25
+ /// @return sqrtP A Fixed point Q64.96 number representing the sqrt of the ratio of the two assets (token1/token0)
26
+ /// at the given tick
27
+ function getSqrtRatioAtTick(int24 tick) internal pure returns (uint160 sqrtP) {
28
+ unchecked {
29
+ uint256 absTick = uint256(tick < 0 ? -int256(tick) : int256(tick));
30
+ require(absTick <= uint256(int256(MAX_TICK)), "T");
31
+
32
+ // do bitwise comparison, if i-th bit is turned on,
33
+ // multiply ratio by hardcoded values of sqrt(1.0001^-(2^i)) * 2^128
34
+ // where 0 <= i <= 19
35
+ uint256 ratio = (absTick & 0x1 != 0)
36
+ ? 0xfffcb933bd6fad37aa2d162d1a594001
37
+ : 0x100000000000000000000000000000000;
38
+ if (absTick & 0x2 != 0) ratio = (ratio * 0xfff97272373d413259a46990580e213a) >> 128;
39
+ if (absTick & 0x4 != 0) ratio = (ratio * 0xfff2e50f5f656932ef12357cf3c7fdcc) >> 128;
40
+ if (absTick & 0x8 != 0) ratio = (ratio * 0xffe5caca7e10e4e61c3624eaa0941cd0) >> 128;
41
+ if (absTick & 0x10 != 0) ratio = (ratio * 0xffcb9843d60f6159c9db58835c926644) >> 128;
42
+ if (absTick & 0x20 != 0) ratio = (ratio * 0xff973b41fa98c081472e6896dfb254c0) >> 128;
43
+ if (absTick & 0x40 != 0) ratio = (ratio * 0xff2ea16466c96a3843ec78b326b52861) >> 128;
44
+ if (absTick & 0x80 != 0) ratio = (ratio * 0xfe5dee046a99a2a811c461f1969c3053) >> 128;
45
+ if (absTick & 0x100 != 0) ratio = (ratio * 0xfcbe86c7900a88aedcffc83b479aa3a4) >> 128;
46
+ if (absTick & 0x200 != 0) ratio = (ratio * 0xf987a7253ac413176f2b074cf7815e54) >> 128;
47
+ if (absTick & 0x400 != 0) ratio = (ratio * 0xf3392b0822b70005940c7a398e4b70f3) >> 128;
48
+ if (absTick & 0x800 != 0) ratio = (ratio * 0xe7159475a2c29b7443b29c7fa6e889d9) >> 128;
49
+ if (absTick & 0x1000 != 0) ratio = (ratio * 0xd097f3bdfd2022b8845ad8f792aa5825) >> 128;
50
+ if (absTick & 0x2000 != 0) ratio = (ratio * 0xa9f746462d870fdf8a65dc1f90e061e5) >> 128;
51
+ if (absTick & 0x4000 != 0) ratio = (ratio * 0x70d869a156d2a1b890bb3df62baf32f7) >> 128;
52
+ if (absTick & 0x8000 != 0) ratio = (ratio * 0x31be135f97d08fd981231505542fcfa6) >> 128;
53
+ if (absTick & 0x10000 != 0) ratio = (ratio * 0x9aa508b5b7a84e1c677de54f3e99bc9) >> 128;
54
+ if (absTick & 0x20000 != 0) ratio = (ratio * 0x5d6af8dedb81196699c329225ee604) >> 128;
55
+ if (absTick & 0x40000 != 0) ratio = (ratio * 0x2216e584f5fa1ea926041bedfe98) >> 128;
56
+ if (absTick & 0x80000 != 0) ratio = (ratio * 0x48a170391f7dc42444e8fa2) >> 128;
57
+
58
+ // take reciprocal for positive tick values
59
+ if (tick > 0) ratio = type(uint256).max / ratio;
60
+
61
+ // this divides by 1<<32 rounding up to go from a Q128.128 to a Q128.96.
62
+ // we then downcast because we know the result always fits within 160 bits due to our tick input constraint
63
+ // we round up in the division so getTickAtSqrtRatio of the output price is always consistent
64
+ sqrtP = uint160((ratio >> 32) + (ratio % (1 << 32) == 0 ? 0 : 1));
65
+ }
66
+ }
67
+
68
+ /// @notice Calculates the greatest tick value such that getRatioAtTick(tick) <= ratio
69
+ /// @dev Throws in case sqrtP < MIN_SQRT_RATIO, as MIN_SQRT_RATIO is the lowest value getRatioAtTick may
70
+ /// ever return.
71
+ /// @param sqrtP The sqrt ratio for which to compute the tick as a Q64.96
72
+ /// @return tick The greatest tick for which the ratio is less than or equal to the input ratio
73
+ function getTickAtSqrtRatio(uint160 sqrtP) internal pure returns (int24 tick) {
74
+ // second inequality must be < because the price can never reach the price at the max tick
75
+ require(sqrtP >= MIN_SQRT_RATIO && sqrtP < MAX_SQRT_RATIO, "R");
76
+ uint256 ratio = uint256(sqrtP) << 32;
77
+
78
+ uint256 r = ratio;
79
+ uint256 msb = 0;
80
+
81
+ unchecked {
82
+ assembly {
83
+ let f := shl(7, gt(r, 0xFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFFF))
84
+ msb := or(msb, f)
85
+ r := shr(f, r)
86
+ }
87
+ assembly {
88
+ let f := shl(6, gt(r, 0xFFFFFFFFFFFFFFFF))
89
+ msb := or(msb, f)
90
+ r := shr(f, r)
91
+ }
92
+ assembly {
93
+ let f := shl(5, gt(r, 0xFFFFFFFF))
94
+ msb := or(msb, f)
95
+ r := shr(f, r)
96
+ }
97
+ assembly {
98
+ let f := shl(4, gt(r, 0xFFFF))
99
+ msb := or(msb, f)
100
+ r := shr(f, r)
101
+ }
102
+ assembly {
103
+ let f := shl(3, gt(r, 0xFF))
104
+ msb := or(msb, f)
105
+ r := shr(f, r)
106
+ }
107
+ assembly {
108
+ let f := shl(2, gt(r, 0xF))
109
+ msb := or(msb, f)
110
+ r := shr(f, r)
111
+ }
112
+ assembly {
113
+ let f := shl(1, gt(r, 0x3))
114
+ msb := or(msb, f)
115
+ r := shr(f, r)
116
+ }
117
+ assembly {
118
+ let f := gt(r, 0x1)
119
+ msb := or(msb, f)
120
+ }
121
+
122
+ if (msb >= 128) r = ratio >> (msb - 127);
123
+ else r = ratio << (127 - msb);
124
+
125
+ int256 log_2 = (int256(msb) - 128) << 64;
126
+
127
+ assembly {
128
+ r := shr(127, mul(r, r))
129
+ let f := shr(128, r)
130
+ log_2 := or(log_2, shl(63, f))
131
+ r := shr(f, r)
132
+ }
133
+ assembly {
134
+ r := shr(127, mul(r, r))
135
+ let f := shr(128, r)
136
+ log_2 := or(log_2, shl(62, f))
137
+ r := shr(f, r)
138
+ }
139
+ assembly {
140
+ r := shr(127, mul(r, r))
141
+ let f := shr(128, r)
142
+ log_2 := or(log_2, shl(61, f))
143
+ r := shr(f, r)
144
+ }
145
+ assembly {
146
+ r := shr(127, mul(r, r))
147
+ let f := shr(128, r)
148
+ log_2 := or(log_2, shl(60, f))
149
+ r := shr(f, r)
150
+ }
151
+ assembly {
152
+ r := shr(127, mul(r, r))
153
+ let f := shr(128, r)
154
+ log_2 := or(log_2, shl(59, f))
155
+ r := shr(f, r)
156
+ }
157
+ assembly {
158
+ r := shr(127, mul(r, r))
159
+ let f := shr(128, r)
160
+ log_2 := or(log_2, shl(58, f))
161
+ r := shr(f, r)
162
+ }
163
+ assembly {
164
+ r := shr(127, mul(r, r))
165
+ let f := shr(128, r)
166
+ log_2 := or(log_2, shl(57, f))
167
+ r := shr(f, r)
168
+ }
169
+ assembly {
170
+ r := shr(127, mul(r, r))
171
+ let f := shr(128, r)
172
+ log_2 := or(log_2, shl(56, f))
173
+ r := shr(f, r)
174
+ }
175
+ assembly {
176
+ r := shr(127, mul(r, r))
177
+ let f := shr(128, r)
178
+ log_2 := or(log_2, shl(55, f))
179
+ r := shr(f, r)
180
+ }
181
+ assembly {
182
+ r := shr(127, mul(r, r))
183
+ let f := shr(128, r)
184
+ log_2 := or(log_2, shl(54, f))
185
+ r := shr(f, r)
186
+ }
187
+ assembly {
188
+ r := shr(127, mul(r, r))
189
+ let f := shr(128, r)
190
+ log_2 := or(log_2, shl(53, f))
191
+ r := shr(f, r)
192
+ }
193
+ assembly {
194
+ r := shr(127, mul(r, r))
195
+ let f := shr(128, r)
196
+ log_2 := or(log_2, shl(52, f))
197
+ r := shr(f, r)
198
+ }
199
+ assembly {
200
+ r := shr(127, mul(r, r))
201
+ let f := shr(128, r)
202
+ log_2 := or(log_2, shl(51, f))
203
+ r := shr(f, r)
204
+ }
205
+ assembly {
206
+ r := shr(127, mul(r, r))
207
+ let f := shr(128, r)
208
+ log_2 := or(log_2, shl(50, f))
209
+ }
210
+
211
+ int256 log_sqrt10001 = log_2 * 255738958999603826347141; // 128.128 number
212
+
213
+ int24 tickLow = int24((log_sqrt10001 - 3402992956809132418596140100660247210) >> 128);
214
+ int24 tickHi = int24((log_sqrt10001 + 291339464771989622907027621153398088495) >> 128);
215
+
216
+ tick = tickLow == tickHi ? tickLow : getSqrtRatioAtTick(tickHi) <= sqrtP ? tickHi : tickLow;
217
+ }
218
+ }
219
+
220
+ function getMaxNumberTicks(int24 _tickDistance) internal pure returns (uint24 numTicks) {
221
+ return uint24(TickMath.MAX_TICK / _tickDistance) * 2;
222
+ }
223
+ }
@@ -0,0 +1,26 @@
1
+ // SPDX-License-Identifier: GPL-3.0-or-later
2
+ pragma solidity ^0.8.17;
3
+
4
+ import "@openzeppelin/contracts/proxy/utils/UUPSUpgradeable.sol";
5
+ import "../../../libraries/BoringOwnableUpgradeable.sol";
6
+ import "../../../../interfaces/IPPriceFeed.sol";
7
+ import "../../../../interfaces/IPOffchainStorage.sol";
8
+
9
+ contract MlpPricingHelper is BoringOwnableUpgradeable, UUPSUpgradeable, IPPriceFeed {
10
+ address public immutable pendleStorage;
11
+ bytes32 public constant KEY = keccak256("MLP.price");
12
+
13
+ constructor(address _pendleStorage) initializer {
14
+ pendleStorage = _pendleStorage;
15
+ }
16
+
17
+ function initialize() external initializer {
18
+ __BoringOwnable_init();
19
+ }
20
+
21
+ function _authorizeUpgrade(address newImplementation) internal override onlyOwner {}
22
+
23
+ function getPrice() external view returns (uint256) {
24
+ return IPOffchainStorage(pendleStorage).getUint256(KEY);
25
+ }
26
+ }