@kamino-finance/klend-sdk 3.2.26 → 4.0.1

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 (725) hide show
  1. package/dist/classes/action.d.ts +1 -0
  2. package/dist/classes/action.d.ts.map +1 -0
  3. package/dist/classes/action.js +1132 -1188
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/curve.d.ts +1 -0
  6. package/dist/classes/curve.d.ts.map +1 -0
  7. package/dist/classes/curve.js +1 -1
  8. package/dist/classes/curve.js.map +1 -1
  9. package/dist/classes/fraction.d.ts +1 -0
  10. package/dist/classes/fraction.d.ts.map +1 -0
  11. package/dist/classes/fraction.js +7 -6
  12. package/dist/classes/fraction.js.map +1 -1
  13. package/dist/classes/index.d.ts +1 -0
  14. package/dist/classes/index.d.ts.map +1 -0
  15. package/dist/classes/index.js.map +1 -1
  16. package/dist/classes/jupiterPerps.d.ts +1 -0
  17. package/dist/classes/jupiterPerps.d.ts.map +1 -0
  18. package/dist/classes/jupiterPerps.js +7 -18
  19. package/dist/classes/jupiterPerps.js.map +1 -1
  20. package/dist/classes/manager.d.ts +1 -0
  21. package/dist/classes/manager.d.ts.map +1 -0
  22. package/dist/classes/manager.js +132 -159
  23. package/dist/classes/manager.js.map +1 -1
  24. package/dist/classes/market.d.ts +2 -1
  25. package/dist/classes/market.d.ts.map +1 -0
  26. package/dist/classes/market.js +622 -716
  27. package/dist/classes/market.js.map +1 -1
  28. package/dist/classes/obligation.d.ts +1 -0
  29. package/dist/classes/obligation.d.ts.map +1 -0
  30. package/dist/classes/obligation.js +60 -62
  31. package/dist/classes/obligation.js.map +1 -1
  32. package/dist/classes/reserve.d.ts +1 -0
  33. package/dist/classes/reserve.d.ts.map +1 -0
  34. package/dist/classes/reserve.js +121 -130
  35. package/dist/classes/reserve.js.map +1 -1
  36. package/dist/classes/shared.d.ts +2 -1
  37. package/dist/classes/shared.d.ts.map +1 -0
  38. package/dist/classes/shared.js.map +1 -1
  39. package/dist/classes/utils.d.ts +1 -0
  40. package/dist/classes/utils.d.ts.map +1 -0
  41. package/dist/classes/utils.js +3 -3
  42. package/dist/classes/utils.js.map +1 -1
  43. package/dist/classes/vault.d.ts +1 -0
  44. package/dist/classes/vault.d.ts.map +1 -0
  45. package/dist/classes/vault.js +334 -354
  46. package/dist/classes/vault.js.map +1 -1
  47. package/dist/client_kamino_manager.d.ts +1 -0
  48. package/dist/client_kamino_manager.d.ts.map +1 -0
  49. package/dist/client_kamino_manager.js +323 -326
  50. package/dist/client_kamino_manager.js.map +1 -1
  51. package/dist/idl_codegen/accounts/LendingMarket.d.ts +1 -0
  52. package/dist/idl_codegen/accounts/LendingMarket.d.ts.map +1 -0
  53. package/dist/idl_codegen/accounts/LendingMarket.js +89 -55
  54. package/dist/idl_codegen/accounts/LendingMarket.js.map +1 -1
  55. package/dist/idl_codegen/accounts/Obligation.d.ts +1 -0
  56. package/dist/idl_codegen/accounts/Obligation.d.ts.map +1 -0
  57. package/dist/idl_codegen/accounts/Obligation.js +87 -56
  58. package/dist/idl_codegen/accounts/Obligation.js.map +1 -1
  59. package/dist/idl_codegen/accounts/ReferrerState.d.ts +1 -0
  60. package/dist/idl_codegen/accounts/ReferrerState.d.ts.map +1 -0
  61. package/dist/idl_codegen/accounts/ReferrerState.js +22 -33
  62. package/dist/idl_codegen/accounts/ReferrerState.js.map +1 -1
  63. package/dist/idl_codegen/accounts/ReferrerTokenState.d.ts +1 -0
  64. package/dist/idl_codegen/accounts/ReferrerTokenState.d.ts.map +1 -0
  65. package/dist/idl_codegen/accounts/ReferrerTokenState.js +35 -37
  66. package/dist/idl_codegen/accounts/ReferrerTokenState.js.map +1 -1
  67. package/dist/idl_codegen/accounts/Reserve.d.ts +1 -0
  68. package/dist/idl_codegen/accounts/Reserve.d.ts.map +1 -0
  69. package/dist/idl_codegen/accounts/Reserve.js +60 -49
  70. package/dist/idl_codegen/accounts/Reserve.js.map +1 -1
  71. package/dist/idl_codegen/accounts/ShortUrl.d.ts +1 -0
  72. package/dist/idl_codegen/accounts/ShortUrl.d.ts.map +1 -0
  73. package/dist/idl_codegen/accounts/ShortUrl.js +22 -33
  74. package/dist/idl_codegen/accounts/ShortUrl.js.map +1 -1
  75. package/dist/idl_codegen/accounts/UserMetadata.d.ts +1 -0
  76. package/dist/idl_codegen/accounts/UserMetadata.d.ts.map +1 -0
  77. package/dist/idl_codegen/accounts/UserMetadata.js +34 -37
  78. package/dist/idl_codegen/accounts/UserMetadata.js.map +1 -1
  79. package/dist/idl_codegen/accounts/UserState.d.ts +1 -0
  80. package/dist/idl_codegen/accounts/UserState.d.ts.map +1 -0
  81. package/dist/idl_codegen/accounts/UserState.js +77 -48
  82. package/dist/idl_codegen/accounts/UserState.js.map +1 -1
  83. package/dist/idl_codegen/accounts/index.d.ts +1 -0
  84. package/dist/idl_codegen/accounts/index.d.ts.map +1 -0
  85. package/dist/idl_codegen/accounts/index.js.map +1 -1
  86. package/dist/idl_codegen/errors/anchor.d.ts +1 -0
  87. package/dist/idl_codegen/errors/anchor.d.ts.map +1 -0
  88. package/dist/idl_codegen/errors/anchor.js +270 -216
  89. package/dist/idl_codegen/errors/anchor.js.map +1 -1
  90. package/dist/idl_codegen/errors/custom.d.ts +1 -0
  91. package/dist/idl_codegen/errors/custom.d.ts.map +1 -0
  92. package/dist/idl_codegen/errors/custom.js +560 -448
  93. package/dist/idl_codegen/errors/custom.js.map +1 -1
  94. package/dist/idl_codegen/errors/index.d.ts +1 -0
  95. package/dist/idl_codegen/errors/index.d.ts.map +1 -0
  96. package/dist/idl_codegen/errors/index.js.map +1 -1
  97. package/dist/idl_codegen/instructions/borrowObligationLiquidity.d.ts +1 -0
  98. package/dist/idl_codegen/instructions/borrowObligationLiquidity.d.ts.map +1 -0
  99. package/dist/idl_codegen/instructions/borrowObligationLiquidity.js.map +1 -1
  100. package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.d.ts +1 -0
  101. package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.d.ts.map +1 -0
  102. package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.js.map +1 -1
  103. package/dist/idl_codegen/instructions/depositObligationCollateral.d.ts +1 -0
  104. package/dist/idl_codegen/instructions/depositObligationCollateral.d.ts.map +1 -0
  105. package/dist/idl_codegen/instructions/depositObligationCollateral.js.map +1 -1
  106. package/dist/idl_codegen/instructions/depositReserveLiquidity.d.ts +1 -0
  107. package/dist/idl_codegen/instructions/depositReserveLiquidity.d.ts.map +1 -0
  108. package/dist/idl_codegen/instructions/depositReserveLiquidity.js.map +1 -1
  109. package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.d.ts +1 -0
  110. package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.d.ts.map +1 -0
  111. package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.js.map +1 -1
  112. package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.d.ts +1 -0
  113. package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.d.ts.map +1 -0
  114. package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.js.map +1 -1
  115. package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.d.ts +1 -0
  116. package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.d.ts.map +1 -0
  117. package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.js.map +1 -1
  118. package/dist/idl_codegen/instructions/idlMissingTypes.d.ts +1 -0
  119. package/dist/idl_codegen/instructions/idlMissingTypes.d.ts.map +1 -0
  120. package/dist/idl_codegen/instructions/idlMissingTypes.js.map +1 -1
  121. package/dist/idl_codegen/instructions/index.d.ts +1 -0
  122. package/dist/idl_codegen/instructions/index.d.ts.map +1 -0
  123. package/dist/idl_codegen/instructions/index.js.map +1 -1
  124. package/dist/idl_codegen/instructions/initFarmsForReserve.d.ts +1 -0
  125. package/dist/idl_codegen/instructions/initFarmsForReserve.d.ts.map +1 -0
  126. package/dist/idl_codegen/instructions/initFarmsForReserve.js.map +1 -1
  127. package/dist/idl_codegen/instructions/initLendingMarket.d.ts +1 -0
  128. package/dist/idl_codegen/instructions/initLendingMarket.d.ts.map +1 -0
  129. package/dist/idl_codegen/instructions/initLendingMarket.js.map +1 -1
  130. package/dist/idl_codegen/instructions/initObligation.d.ts +1 -0
  131. package/dist/idl_codegen/instructions/initObligation.d.ts.map +1 -0
  132. package/dist/idl_codegen/instructions/initObligation.js.map +1 -1
  133. package/dist/idl_codegen/instructions/initObligationFarmsForReserve.d.ts +1 -0
  134. package/dist/idl_codegen/instructions/initObligationFarmsForReserve.d.ts.map +1 -0
  135. package/dist/idl_codegen/instructions/initObligationFarmsForReserve.js.map +1 -1
  136. package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.d.ts +1 -0
  137. package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.d.ts.map +1 -0
  138. package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.js.map +1 -1
  139. package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts +1 -0
  140. package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts.map +1 -0
  141. package/dist/idl_codegen/instructions/initReferrerTokenState.js.map +1 -1
  142. package/dist/idl_codegen/instructions/initReserve.d.ts +1 -0
  143. package/dist/idl_codegen/instructions/initReserve.d.ts.map +1 -0
  144. package/dist/idl_codegen/instructions/initReserve.js.map +1 -1
  145. package/dist/idl_codegen/instructions/initUserMetadata.d.ts +1 -0
  146. package/dist/idl_codegen/instructions/initUserMetadata.d.ts.map +1 -0
  147. package/dist/idl_codegen/instructions/initUserMetadata.js.map +1 -1
  148. package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.d.ts +1 -0
  149. package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.d.ts.map +1 -0
  150. package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.js.map +1 -1
  151. package/dist/idl_codegen/instructions/redeemFees.d.ts +1 -0
  152. package/dist/idl_codegen/instructions/redeemFees.d.ts.map +1 -0
  153. package/dist/idl_codegen/instructions/redeemFees.js.map +1 -1
  154. package/dist/idl_codegen/instructions/redeemReserveCollateral.d.ts +1 -0
  155. package/dist/idl_codegen/instructions/redeemReserveCollateral.d.ts.map +1 -0
  156. package/dist/idl_codegen/instructions/redeemReserveCollateral.js.map +1 -1
  157. package/dist/idl_codegen/instructions/refreshObligation.d.ts +1 -0
  158. package/dist/idl_codegen/instructions/refreshObligation.d.ts.map +1 -0
  159. package/dist/idl_codegen/instructions/refreshObligation.js.map +1 -1
  160. package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.d.ts +1 -0
  161. package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.d.ts.map +1 -0
  162. package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.js.map +1 -1
  163. package/dist/idl_codegen/instructions/refreshReserve.d.ts +1 -0
  164. package/dist/idl_codegen/instructions/refreshReserve.d.ts.map +1 -0
  165. package/dist/idl_codegen/instructions/refreshReserve.js.map +1 -1
  166. package/dist/idl_codegen/instructions/refreshReservesBatch.d.ts +1 -0
  167. package/dist/idl_codegen/instructions/refreshReservesBatch.d.ts.map +1 -0
  168. package/dist/idl_codegen/instructions/refreshReservesBatch.js.map +1 -1
  169. package/dist/idl_codegen/instructions/repayObligationLiquidity.d.ts +1 -0
  170. package/dist/idl_codegen/instructions/repayObligationLiquidity.d.ts.map +1 -0
  171. package/dist/idl_codegen/instructions/repayObligationLiquidity.js.map +1 -1
  172. package/dist/idl_codegen/instructions/requestElevationGroup.d.ts +1 -0
  173. package/dist/idl_codegen/instructions/requestElevationGroup.d.ts.map +1 -0
  174. package/dist/idl_codegen/instructions/requestElevationGroup.js.map +1 -1
  175. package/dist/idl_codegen/instructions/socializeLoss.d.ts +1 -0
  176. package/dist/idl_codegen/instructions/socializeLoss.d.ts.map +1 -0
  177. package/dist/idl_codegen/instructions/socializeLoss.js.map +1 -1
  178. package/dist/idl_codegen/instructions/updateEntireReserveConfig.d.ts +1 -0
  179. package/dist/idl_codegen/instructions/updateEntireReserveConfig.d.ts.map +1 -0
  180. package/dist/idl_codegen/instructions/updateEntireReserveConfig.js.map +1 -1
  181. package/dist/idl_codegen/instructions/updateLendingMarket.d.ts +1 -0
  182. package/dist/idl_codegen/instructions/updateLendingMarket.d.ts.map +1 -0
  183. package/dist/idl_codegen/instructions/updateLendingMarket.js.map +1 -1
  184. package/dist/idl_codegen/instructions/updateLendingMarketOwner.d.ts +1 -0
  185. package/dist/idl_codegen/instructions/updateLendingMarketOwner.d.ts.map +1 -0
  186. package/dist/idl_codegen/instructions/updateLendingMarketOwner.js.map +1 -1
  187. package/dist/idl_codegen/instructions/updateReserveConfig.d.ts +1 -0
  188. package/dist/idl_codegen/instructions/updateReserveConfig.d.ts.map +1 -0
  189. package/dist/idl_codegen/instructions/updateReserveConfig.js.map +1 -1
  190. package/dist/idl_codegen/instructions/updateSingleReserveConfig.d.ts +1 -0
  191. package/dist/idl_codegen/instructions/updateSingleReserveConfig.d.ts.map +1 -0
  192. package/dist/idl_codegen/instructions/updateSingleReserveConfig.js.map +1 -1
  193. package/dist/idl_codegen/instructions/withdrawObligationCollateral.d.ts +1 -0
  194. package/dist/idl_codegen/instructions/withdrawObligationCollateral.d.ts.map +1 -0
  195. package/dist/idl_codegen/instructions/withdrawObligationCollateral.js.map +1 -1
  196. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.d.ts +1 -0
  197. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.d.ts.map +1 -0
  198. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js.map +1 -1
  199. package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts +1 -0
  200. package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts.map +1 -0
  201. package/dist/idl_codegen/instructions/withdrawProtocolFee.js.map +1 -1
  202. package/dist/idl_codegen/instructions/withdrawReferrerFees.d.ts +1 -0
  203. package/dist/idl_codegen/instructions/withdrawReferrerFees.d.ts.map +1 -0
  204. package/dist/idl_codegen/instructions/withdrawReferrerFees.js.map +1 -1
  205. package/dist/idl_codegen/programId.d.ts +1 -0
  206. package/dist/idl_codegen/programId.d.ts.map +1 -0
  207. package/dist/idl_codegen/programId.js.map +1 -1
  208. package/dist/idl_codegen/types/AssetTier.d.ts +1 -0
  209. package/dist/idl_codegen/types/AssetTier.d.ts.map +1 -0
  210. package/dist/idl_codegen/types/AssetTier.js +12 -18
  211. package/dist/idl_codegen/types/AssetTier.js.map +1 -1
  212. package/dist/idl_codegen/types/BigFractionBytes.d.ts +1 -0
  213. package/dist/idl_codegen/types/BigFractionBytes.d.ts.map +1 -0
  214. package/dist/idl_codegen/types/BigFractionBytes.js +2 -0
  215. package/dist/idl_codegen/types/BigFractionBytes.js.map +1 -1
  216. package/dist/idl_codegen/types/BorrowRateCurve.d.ts +1 -0
  217. package/dist/idl_codegen/types/BorrowRateCurve.d.ts.map +1 -0
  218. package/dist/idl_codegen/types/BorrowRateCurve.js +2 -1
  219. package/dist/idl_codegen/types/BorrowRateCurve.js.map +1 -1
  220. package/dist/idl_codegen/types/CurvePoint.d.ts +1 -0
  221. package/dist/idl_codegen/types/CurvePoint.d.ts.map +1 -0
  222. package/dist/idl_codegen/types/CurvePoint.js +2 -0
  223. package/dist/idl_codegen/types/CurvePoint.js.map +1 -1
  224. package/dist/idl_codegen/types/ElevationGroup.d.ts +1 -0
  225. package/dist/idl_codegen/types/ElevationGroup.d.ts.map +1 -0
  226. package/dist/idl_codegen/types/ElevationGroup.js +10 -0
  227. package/dist/idl_codegen/types/ElevationGroup.js.map +1 -1
  228. package/dist/idl_codegen/types/FeeCalculation.d.ts +1 -0
  229. package/dist/idl_codegen/types/FeeCalculation.d.ts.map +1 -0
  230. package/dist/idl_codegen/types/FeeCalculation.js +8 -12
  231. package/dist/idl_codegen/types/FeeCalculation.js.map +1 -1
  232. package/dist/idl_codegen/types/InitObligationArgs.d.ts +1 -0
  233. package/dist/idl_codegen/types/InitObligationArgs.d.ts.map +1 -0
  234. package/dist/idl_codegen/types/InitObligationArgs.js +2 -0
  235. package/dist/idl_codegen/types/InitObligationArgs.js.map +1 -1
  236. package/dist/idl_codegen/types/LastUpdate.d.ts +1 -0
  237. package/dist/idl_codegen/types/LastUpdate.d.ts.map +1 -0
  238. package/dist/idl_codegen/types/LastUpdate.js +7 -0
  239. package/dist/idl_codegen/types/LastUpdate.js.map +1 -1
  240. package/dist/idl_codegen/types/ObligationCollateral.d.ts +1 -0
  241. package/dist/idl_codegen/types/ObligationCollateral.d.ts.map +1 -0
  242. package/dist/idl_codegen/types/ObligationCollateral.js +14 -0
  243. package/dist/idl_codegen/types/ObligationCollateral.js.map +1 -1
  244. package/dist/idl_codegen/types/ObligationLiquidity.d.ts +1 -0
  245. package/dist/idl_codegen/types/ObligationLiquidity.d.ts.map +1 -0
  246. package/dist/idl_codegen/types/ObligationLiquidity.js +17 -1
  247. package/dist/idl_codegen/types/ObligationLiquidity.js.map +1 -1
  248. package/dist/idl_codegen/types/PriceHeuristic.d.ts +1 -0
  249. package/dist/idl_codegen/types/PriceHeuristic.d.ts.map +1 -0
  250. package/dist/idl_codegen/types/PriceHeuristic.js +6 -0
  251. package/dist/idl_codegen/types/PriceHeuristic.js.map +1 -1
  252. package/dist/idl_codegen/types/PythConfiguration.d.ts +1 -0
  253. package/dist/idl_codegen/types/PythConfiguration.d.ts.map +1 -0
  254. package/dist/idl_codegen/types/PythConfiguration.js +2 -0
  255. package/dist/idl_codegen/types/PythConfiguration.js.map +1 -1
  256. package/dist/idl_codegen/types/ReserveCollateral.d.ts +1 -0
  257. package/dist/idl_codegen/types/ReserveCollateral.d.ts.map +1 -0
  258. package/dist/idl_codegen/types/ReserveCollateral.js +8 -0
  259. package/dist/idl_codegen/types/ReserveCollateral.js.map +1 -1
  260. package/dist/idl_codegen/types/ReserveConfig.d.ts +1 -0
  261. package/dist/idl_codegen/types/ReserveConfig.d.ts.map +1 -0
  262. package/dist/idl_codegen/types/ReserveConfig.js +78 -5
  263. package/dist/idl_codegen/types/ReserveConfig.js.map +1 -1
  264. package/dist/idl_codegen/types/ReserveFarmKind.d.ts +1 -0
  265. package/dist/idl_codegen/types/ReserveFarmKind.d.ts.map +1 -0
  266. package/dist/idl_codegen/types/ReserveFarmKind.js +8 -12
  267. package/dist/idl_codegen/types/ReserveFarmKind.js.map +1 -1
  268. package/dist/idl_codegen/types/ReserveFees.d.ts +1 -0
  269. package/dist/idl_codegen/types/ReserveFees.d.ts.map +1 -0
  270. package/dist/idl_codegen/types/ReserveFees.js +16 -0
  271. package/dist/idl_codegen/types/ReserveFees.js.map +1 -1
  272. package/dist/idl_codegen/types/ReserveLiquidity.d.ts +1 -0
  273. package/dist/idl_codegen/types/ReserveLiquidity.d.ts.map +1 -0
  274. package/dist/idl_codegen/types/ReserveLiquidity.js +43 -1
  275. package/dist/idl_codegen/types/ReserveLiquidity.js.map +1 -1
  276. package/dist/idl_codegen/types/ReserveStatus.d.ts +1 -0
  277. package/dist/idl_codegen/types/ReserveStatus.d.ts.map +1 -0
  278. package/dist/idl_codegen/types/ReserveStatus.js +12 -18
  279. package/dist/idl_codegen/types/ReserveStatus.js.map +1 -1
  280. package/dist/idl_codegen/types/ScopeConfiguration.d.ts +1 -0
  281. package/dist/idl_codegen/types/ScopeConfiguration.d.ts.map +1 -0
  282. package/dist/idl_codegen/types/ScopeConfiguration.js +6 -0
  283. package/dist/idl_codegen/types/ScopeConfiguration.js.map +1 -1
  284. package/dist/idl_codegen/types/SwitchboardConfiguration.d.ts +1 -0
  285. package/dist/idl_codegen/types/SwitchboardConfiguration.d.ts.map +1 -0
  286. package/dist/idl_codegen/types/SwitchboardConfiguration.js +3 -0
  287. package/dist/idl_codegen/types/SwitchboardConfiguration.js.map +1 -1
  288. package/dist/idl_codegen/types/TokenInfo.d.ts +1 -0
  289. package/dist/idl_codegen/types/TokenInfo.d.ts.map +1 -0
  290. package/dist/idl_codegen/types/TokenInfo.js +27 -4
  291. package/dist/idl_codegen/types/TokenInfo.js.map +1 -1
  292. package/dist/idl_codegen/types/UpdateConfigMode.d.ts +1 -0
  293. package/dist/idl_codegen/types/UpdateConfigMode.d.ts.map +1 -0
  294. package/dist/idl_codegen/types/UpdateConfigMode.js +188 -282
  295. package/dist/idl_codegen/types/UpdateConfigMode.js.map +1 -1
  296. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts +1 -0
  297. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts.map +1 -0
  298. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js +41 -33
  299. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js.map +1 -1
  300. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts +1 -0
  301. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts.map +1 -0
  302. package/dist/idl_codegen/types/UpdateLendingMarketMode.js +72 -108
  303. package/dist/idl_codegen/types/UpdateLendingMarketMode.js.map +1 -1
  304. package/dist/idl_codegen/types/WithdrawalCaps.d.ts +1 -0
  305. package/dist/idl_codegen/types/WithdrawalCaps.d.ts.map +1 -0
  306. package/dist/idl_codegen/types/WithdrawalCaps.js +4 -0
  307. package/dist/idl_codegen/types/WithdrawalCaps.js.map +1 -1
  308. package/dist/idl_codegen/types/index.d.ts +1 -0
  309. package/dist/idl_codegen/types/index.d.ts.map +1 -0
  310. package/dist/idl_codegen/types/index.js.map +1 -1
  311. package/dist/idl_codegen/zero_padding/ObligationZP.d.ts +1 -0
  312. package/dist/idl_codegen/zero_padding/ObligationZP.d.ts.map +1 -0
  313. package/dist/idl_codegen/zero_padding/ObligationZP.js +83 -54
  314. package/dist/idl_codegen/zero_padding/ObligationZP.js.map +1 -1
  315. package/dist/idl_codegen/zero_padding/index.d.ts +1 -0
  316. package/dist/idl_codegen/zero_padding/index.d.ts.map +1 -0
  317. package/dist/idl_codegen/zero_padding/index.js.map +1 -1
  318. package/dist/idl_codegen_jupiter_perps/accounts/Pool.d.ts +1 -0
  319. package/dist/idl_codegen_jupiter_perps/accounts/Pool.d.ts.map +1 -0
  320. package/dist/idl_codegen_jupiter_perps/accounts/Pool.js +39 -42
  321. package/dist/idl_codegen_jupiter_perps/accounts/Pool.js.map +1 -1
  322. package/dist/idl_codegen_jupiter_perps/accounts/index.d.ts +1 -0
  323. package/dist/idl_codegen_jupiter_perps/accounts/index.d.ts.map +1 -0
  324. package/dist/idl_codegen_jupiter_perps/accounts/index.js.map +1 -1
  325. package/dist/idl_codegen_jupiter_perps/programId.d.ts +1 -0
  326. package/dist/idl_codegen_jupiter_perps/programId.d.ts.map +1 -0
  327. package/dist/idl_codegen_jupiter_perps/programId.js.map +1 -1
  328. package/dist/idl_codegen_jupiter_perps/types/Fees.d.ts +1 -0
  329. package/dist/idl_codegen_jupiter_perps/types/Fees.d.ts.map +1 -0
  330. package/dist/idl_codegen_jupiter_perps/types/Fees.js +9 -0
  331. package/dist/idl_codegen_jupiter_perps/types/Fees.js.map +1 -1
  332. package/dist/idl_codegen_jupiter_perps/types/Limit.d.ts +1 -0
  333. package/dist/idl_codegen_jupiter_perps/types/Limit.d.ts.map +1 -0
  334. package/dist/idl_codegen_jupiter_perps/types/Limit.js +3 -0
  335. package/dist/idl_codegen_jupiter_perps/types/Limit.js.map +1 -1
  336. package/dist/idl_codegen_jupiter_perps/types/PoolApr.d.ts +1 -0
  337. package/dist/idl_codegen_jupiter_perps/types/PoolApr.d.ts.map +1 -0
  338. package/dist/idl_codegen_jupiter_perps/types/PoolApr.js +3 -0
  339. package/dist/idl_codegen_jupiter_perps/types/PoolApr.js.map +1 -1
  340. package/dist/idl_codegen_jupiter_perps/types/index.d.ts +1 -0
  341. package/dist/idl_codegen_jupiter_perps/types/index.d.ts.map +1 -0
  342. package/dist/idl_codegen_jupiter_perps/types/index.js.map +1 -1
  343. package/dist/idl_codegen_kamino_vault/accounts/Reserve.d.ts +1 -0
  344. package/dist/idl_codegen_kamino_vault/accounts/Reserve.d.ts.map +1 -0
  345. package/dist/idl_codegen_kamino_vault/accounts/Reserve.js +58 -47
  346. package/dist/idl_codegen_kamino_vault/accounts/Reserve.js.map +1 -1
  347. package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts +1 -0
  348. package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts.map +1 -0
  349. package/dist/idl_codegen_kamino_vault/accounts/VaultState.js +51 -47
  350. package/dist/idl_codegen_kamino_vault/accounts/VaultState.js.map +1 -1
  351. package/dist/idl_codegen_kamino_vault/accounts/index.d.ts +1 -0
  352. package/dist/idl_codegen_kamino_vault/accounts/index.d.ts.map +1 -0
  353. package/dist/idl_codegen_kamino_vault/accounts/index.js.map +1 -1
  354. package/dist/idl_codegen_kamino_vault/errors/anchor.d.ts +1 -0
  355. package/dist/idl_codegen_kamino_vault/errors/anchor.d.ts.map +1 -0
  356. package/dist/idl_codegen_kamino_vault/errors/anchor.js +270 -216
  357. package/dist/idl_codegen_kamino_vault/errors/anchor.js.map +1 -1
  358. package/dist/idl_codegen_kamino_vault/errors/custom.d.ts +1 -0
  359. package/dist/idl_codegen_kamino_vault/errors/custom.d.ts.map +1 -0
  360. package/dist/idl_codegen_kamino_vault/errors/custom.js +120 -96
  361. package/dist/idl_codegen_kamino_vault/errors/custom.js.map +1 -1
  362. package/dist/idl_codegen_kamino_vault/errors/index.d.ts +1 -0
  363. package/dist/idl_codegen_kamino_vault/errors/index.d.ts.map +1 -0
  364. package/dist/idl_codegen_kamino_vault/errors/index.js.map +1 -1
  365. package/dist/idl_codegen_kamino_vault/instructions/deposit.d.ts +1 -0
  366. package/dist/idl_codegen_kamino_vault/instructions/deposit.d.ts.map +1 -0
  367. package/dist/idl_codegen_kamino_vault/instructions/deposit.js.map +1 -1
  368. package/dist/idl_codegen_kamino_vault/instructions/index.d.ts +1 -0
  369. package/dist/idl_codegen_kamino_vault/instructions/index.d.ts.map +1 -0
  370. package/dist/idl_codegen_kamino_vault/instructions/index.js.map +1 -1
  371. package/dist/idl_codegen_kamino_vault/instructions/initVault.d.ts +1 -0
  372. package/dist/idl_codegen_kamino_vault/instructions/initVault.d.ts.map +1 -0
  373. package/dist/idl_codegen_kamino_vault/instructions/initVault.js.map +1 -1
  374. package/dist/idl_codegen_kamino_vault/instructions/invest.d.ts +1 -0
  375. package/dist/idl_codegen_kamino_vault/instructions/invest.d.ts.map +1 -0
  376. package/dist/idl_codegen_kamino_vault/instructions/invest.js.map +1 -1
  377. package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.d.ts +1 -0
  378. package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.d.ts.map +1 -0
  379. package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.js.map +1 -1
  380. package/dist/idl_codegen_kamino_vault/instructions/withdraw.d.ts +1 -0
  381. package/dist/idl_codegen_kamino_vault/instructions/withdraw.d.ts.map +1 -0
  382. package/dist/idl_codegen_kamino_vault/instructions/withdraw.js.map +1 -1
  383. package/dist/idl_codegen_kamino_vault/programId.d.ts +1 -0
  384. package/dist/idl_codegen_kamino_vault/programId.d.ts.map +1 -0
  385. package/dist/idl_codegen_kamino_vault/programId.js.map +1 -1
  386. package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.d.ts +1 -0
  387. package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.d.ts.map +1 -0
  388. package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.js +2 -0
  389. package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.js.map +1 -1
  390. package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.d.ts +1 -0
  391. package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.d.ts.map +1 -0
  392. package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.js +2 -1
  393. package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.js.map +1 -1
  394. package/dist/idl_codegen_kamino_vault/types/CurvePoint.d.ts +1 -0
  395. package/dist/idl_codegen_kamino_vault/types/CurvePoint.d.ts.map +1 -0
  396. package/dist/idl_codegen_kamino_vault/types/CurvePoint.js +2 -0
  397. package/dist/idl_codegen_kamino_vault/types/CurvePoint.js.map +1 -1
  398. package/dist/idl_codegen_kamino_vault/types/LastUpdate.d.ts +1 -0
  399. package/dist/idl_codegen_kamino_vault/types/LastUpdate.d.ts.map +1 -0
  400. package/dist/idl_codegen_kamino_vault/types/LastUpdate.js +7 -0
  401. package/dist/idl_codegen_kamino_vault/types/LastUpdate.js.map +1 -1
  402. package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.d.ts +1 -0
  403. package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.d.ts.map +1 -0
  404. package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.js +6 -0
  405. package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.js.map +1 -1
  406. package/dist/idl_codegen_kamino_vault/types/PythConfiguration.d.ts +1 -0
  407. package/dist/idl_codegen_kamino_vault/types/PythConfiguration.d.ts.map +1 -0
  408. package/dist/idl_codegen_kamino_vault/types/PythConfiguration.js +2 -0
  409. package/dist/idl_codegen_kamino_vault/types/PythConfiguration.js.map +1 -1
  410. package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.d.ts +1 -0
  411. package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.d.ts.map +1 -0
  412. package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.js +8 -0
  413. package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.js.map +1 -1
  414. package/dist/idl_codegen_kamino_vault/types/ReserveConfig.d.ts +1 -0
  415. package/dist/idl_codegen_kamino_vault/types/ReserveConfig.d.ts.map +1 -0
  416. package/dist/idl_codegen_kamino_vault/types/ReserveConfig.js +77 -5
  417. package/dist/idl_codegen_kamino_vault/types/ReserveConfig.js.map +1 -1
  418. package/dist/idl_codegen_kamino_vault/types/ReserveFees.d.ts +1 -0
  419. package/dist/idl_codegen_kamino_vault/types/ReserveFees.d.ts.map +1 -0
  420. package/dist/idl_codegen_kamino_vault/types/ReserveFees.js +16 -0
  421. package/dist/idl_codegen_kamino_vault/types/ReserveFees.js.map +1 -1
  422. package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.d.ts +1 -0
  423. package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.d.ts.map +1 -0
  424. package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.js +41 -1
  425. package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.js.map +1 -1
  426. package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.d.ts +1 -0
  427. package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.d.ts.map +1 -0
  428. package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.js +6 -0
  429. package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.js.map +1 -1
  430. package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.d.ts +1 -0
  431. package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.d.ts.map +1 -0
  432. package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.js +3 -0
  433. package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.js.map +1 -1
  434. package/dist/idl_codegen_kamino_vault/types/TokenInfo.d.ts +1 -0
  435. package/dist/idl_codegen_kamino_vault/types/TokenInfo.d.ts.map +1 -0
  436. package/dist/idl_codegen_kamino_vault/types/TokenInfo.js +27 -4
  437. package/dist/idl_codegen_kamino_vault/types/TokenInfo.js.map +1 -1
  438. package/dist/idl_codegen_kamino_vault/types/VaultAllocation.d.ts +1 -0
  439. package/dist/idl_codegen_kamino_vault/types/VaultAllocation.d.ts.map +1 -0
  440. package/dist/idl_codegen_kamino_vault/types/VaultAllocation.js +8 -0
  441. package/dist/idl_codegen_kamino_vault/types/VaultAllocation.js.map +1 -1
  442. package/dist/idl_codegen_kamino_vault/types/WithdrawalCaps.d.ts +1 -0
  443. package/dist/idl_codegen_kamino_vault/types/WithdrawalCaps.d.ts.map +1 -0
  444. package/dist/idl_codegen_kamino_vault/types/WithdrawalCaps.js +4 -0
  445. package/dist/idl_codegen_kamino_vault/types/WithdrawalCaps.js.map +1 -1
  446. package/dist/idl_codegen_kamino_vault/types/index.d.ts +1 -0
  447. package/dist/idl_codegen_kamino_vault/types/index.d.ts.map +1 -0
  448. package/dist/idl_codegen_kamino_vault/types/index.js.map +1 -1
  449. package/dist/index.d.ts +1 -0
  450. package/dist/index.d.ts.map +1 -0
  451. package/dist/index.js.map +1 -1
  452. package/dist/lending_operations/index.d.ts +1 -0
  453. package/dist/lending_operations/index.d.ts.map +1 -0
  454. package/dist/lending_operations/index.js.map +1 -1
  455. package/dist/lending_operations/repay_with_collateral_calcs.d.ts +1 -0
  456. package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -0
  457. package/dist/lending_operations/repay_with_collateral_calcs.js +1 -1
  458. package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
  459. package/dist/lending_operations/repay_with_collateral_operations.d.ts +1 -0
  460. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -0
  461. package/dist/lending_operations/repay_with_collateral_operations.js +15 -24
  462. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  463. package/dist/leverage/calcs.d.ts +1 -0
  464. package/dist/leverage/calcs.d.ts.map +1 -0
  465. package/dist/leverage/calcs.js +119 -132
  466. package/dist/leverage/calcs.js.map +1 -1
  467. package/dist/leverage/index.d.ts +1 -0
  468. package/dist/leverage/index.d.ts.map +1 -0
  469. package/dist/leverage/index.js.map +1 -1
  470. package/dist/leverage/instructions.d.ts +1 -0
  471. package/dist/leverage/instructions.d.ts.map +1 -0
  472. package/dist/leverage/instructions.js.map +1 -1
  473. package/dist/leverage/operations.d.ts +1 -0
  474. package/dist/leverage/operations.d.ts.map +1 -0
  475. package/dist/leverage/operations.js +75 -84
  476. package/dist/leverage/operations.js.map +1 -1
  477. package/dist/leverage/utils.d.ts +1 -0
  478. package/dist/leverage/utils.d.ts.map +1 -0
  479. package/dist/leverage/utils.js +73 -88
  480. package/dist/leverage/utils.js.map +1 -1
  481. package/dist/lib.d.ts +1 -0
  482. package/dist/lib.d.ts.map +1 -0
  483. package/dist/lib.js.map +1 -1
  484. package/dist/referrals/index.d.ts +1 -0
  485. package/dist/referrals/index.d.ts.map +1 -0
  486. package/dist/referrals/index.js.map +1 -1
  487. package/dist/referrals/instructions.d.ts +1 -0
  488. package/dist/referrals/instructions.d.ts.map +1 -0
  489. package/dist/referrals/instructions.js +8 -17
  490. package/dist/referrals/instructions.js.map +1 -1
  491. package/dist/referrals/operations.d.ts +2 -1
  492. package/dist/referrals/operations.d.ts.map +1 -0
  493. package/dist/referrals/operations.js +119 -143
  494. package/dist/referrals/operations.js.map +1 -1
  495. package/dist/utils/ObligationType.d.ts +1 -0
  496. package/dist/utils/ObligationType.d.ts.map +1 -0
  497. package/dist/utils/ObligationType.js +16 -7
  498. package/dist/utils/ObligationType.js.map +1 -1
  499. package/dist/utils/api.d.ts +1 -0
  500. package/dist/utils/api.d.ts.map +1 -0
  501. package/dist/utils/api.js +10 -21
  502. package/dist/utils/api.js.map +1 -1
  503. package/dist/utils/ata.d.ts +1 -0
  504. package/dist/utils/ata.d.ts.map +1 -0
  505. package/dist/utils/ata.js +25 -38
  506. package/dist/utils/ata.js.map +1 -1
  507. package/dist/utils/constants.d.ts +1 -0
  508. package/dist/utils/constants.d.ts.map +1 -0
  509. package/dist/utils/constants.js +1 -1
  510. package/dist/utils/constants.js.map +1 -1
  511. package/dist/utils/idl.d.ts +1 -0
  512. package/dist/utils/idl.d.ts.map +1 -0
  513. package/dist/utils/idl.js.map +1 -1
  514. package/dist/utils/index.d.ts +1 -0
  515. package/dist/utils/index.d.ts.map +1 -0
  516. package/dist/utils/index.js.map +1 -1
  517. package/dist/utils/instruction.d.ts +1 -0
  518. package/dist/utils/instruction.d.ts.map +1 -0
  519. package/dist/utils/instruction.js +116 -143
  520. package/dist/utils/instruction.js.map +1 -1
  521. package/dist/utils/kamino.d.ts +1 -0
  522. package/dist/utils/kamino.d.ts.map +1 -0
  523. package/dist/utils/kamino.js +4 -15
  524. package/dist/utils/kamino.js.map +1 -1
  525. package/dist/utils/layout.d.ts +1 -0
  526. package/dist/utils/layout.d.ts.map +1 -0
  527. package/dist/utils/layout.js.map +1 -1
  528. package/dist/utils/lookupTable.d.ts +1 -0
  529. package/dist/utils/lookupTable.d.ts.map +1 -0
  530. package/dist/utils/lookupTable.js +14 -27
  531. package/dist/utils/lookupTable.js.map +1 -1
  532. package/dist/utils/managerTypes.d.ts +1 -0
  533. package/dist/utils/managerTypes.d.ts.map +1 -0
  534. package/dist/utils/managerTypes.js +32 -3
  535. package/dist/utils/managerTypes.js.map +1 -1
  536. package/dist/utils/oracle.d.ts +2 -1
  537. package/dist/utils/oracle.d.ts.map +1 -0
  538. package/dist/utils/oracle.js +71 -85
  539. package/dist/utils/oracle.js.map +1 -1
  540. package/dist/utils/pubkey.d.ts +1 -0
  541. package/dist/utils/pubkey.d.ts.map +1 -0
  542. package/dist/utils/pubkey.js +7 -9
  543. package/dist/utils/pubkey.js.map +1 -1
  544. package/dist/utils/rpc.d.ts +1 -0
  545. package/dist/utils/rpc.d.ts.map +1 -0
  546. package/dist/utils/rpc.js +48 -70
  547. package/dist/utils/rpc.js.map +1 -1
  548. package/dist/utils/seeds.d.ts +1 -0
  549. package/dist/utils/seeds.d.ts.map +1 -0
  550. package/dist/utils/seeds.js.map +1 -1
  551. package/dist/utils/sendTransactionsUtils.d.ts +1 -0
  552. package/dist/utils/sendTransactionsUtils.d.ts.map +1 -0
  553. package/dist/utils/sendTransactionsUtils.js +82 -82
  554. package/dist/utils/sendTransactionsUtils.js.map +1 -1
  555. package/dist/utils/slots.d.ts +1 -0
  556. package/dist/utils/slots.d.ts.map +1 -0
  557. package/dist/utils/slots.js.map +1 -1
  558. package/dist/utils/syncNative.d.ts +1 -0
  559. package/dist/utils/syncNative.d.ts.map +1 -0
  560. package/dist/utils/syncNative.js.map +1 -1
  561. package/dist/utils/userMetadata.d.ts +1 -0
  562. package/dist/utils/userMetadata.d.ts.map +1 -0
  563. package/dist/utils/userMetadata.js +41 -53
  564. package/dist/utils/userMetadata.js.map +1 -1
  565. package/package.json +14 -8
  566. package/src/classes/action.ts +2884 -0
  567. package/src/classes/curve.ts +29 -0
  568. package/src/classes/fraction.ts +65 -0
  569. package/src/classes/index.ts +10 -0
  570. package/src/classes/jupiterPerps.ts +14 -0
  571. package/src/classes/manager.ts +746 -0
  572. package/src/classes/market.ts +1459 -0
  573. package/src/classes/obligation.ts +1315 -0
  574. package/src/classes/reserve.ts +1980 -0
  575. package/src/classes/shared.ts +57 -0
  576. package/src/classes/utils.ts +192 -0
  577. package/src/classes/vault.ts +808 -0
  578. package/src/client.ts +446 -0
  579. package/src/client_kamino_manager.ts +791 -0
  580. package/src/global.d.ts +1 -0
  581. package/src/idl_codegen/accounts/LendingMarket.ts +364 -0
  582. package/src/idl_codegen/accounts/Obligation.ts +349 -0
  583. package/src/idl_codegen/accounts/ReferrerState.ts +97 -0
  584. package/src/idl_codegen/accounts/ReferrerTokenState.ts +145 -0
  585. package/src/idl_codegen/accounts/Reserve.ts +248 -0
  586. package/src/idl_codegen/accounts/ShortUrl.ts +97 -0
  587. package/src/idl_codegen/accounts/UserMetadata.ts +142 -0
  588. package/src/idl_codegen/accounts/UserState.ts +301 -0
  589. package/src/idl_codegen/accounts/index.ts +19 -0
  590. package/src/idl_codegen/errors/anchor.ts +773 -0
  591. package/src/idl_codegen/errors/custom.ts +1633 -0
  592. package/src/idl_codegen/errors/index.ts +62 -0
  593. package/src/idl_codegen/instructions/borrowObligationLiquidity.ts +82 -0
  594. package/src/idl_codegen/instructions/deleteReferrerStateAndShortUrl.ts +30 -0
  595. package/src/idl_codegen/instructions/depositObligationCollateral.ts +62 -0
  596. package/src/idl_codegen/instructions/depositReserveLiquidity.ts +90 -0
  597. package/src/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.ts +98 -0
  598. package/src/idl_codegen/instructions/flashBorrowReserveLiquidity.ts +82 -0
  599. package/src/idl_codegen/instructions/flashRepayReserveLiquidity.ts +83 -0
  600. package/src/idl_codegen/instructions/idlMissingTypes.ts +62 -0
  601. package/src/idl_codegen/instructions/index.ts +138 -0
  602. package/src/idl_codegen/instructions/initFarmsForReserve.ts +62 -0
  603. package/src/idl_codegen/instructions/initLendingMarket.ts +50 -0
  604. package/src/idl_codegen/instructions/initObligation.ts +52 -0
  605. package/src/idl_codegen/instructions/initObligationFarmsForReserve.ts +60 -0
  606. package/src/idl_codegen/instructions/initReferrerStateAndShortUrl.ts +50 -0
  607. package/src/idl_codegen/instructions/initReferrerTokenState.ts +46 -0
  608. package/src/idl_codegen/instructions/initReserve.ts +74 -0
  609. package/src/idl_codegen/instructions/initUserMetadata.ts +50 -0
  610. package/src/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.ts +139 -0
  611. package/src/idl_codegen/instructions/redeemFees.ts +50 -0
  612. package/src/idl_codegen/instructions/redeemReserveCollateral.ts +94 -0
  613. package/src/idl_codegen/instructions/refreshObligation.ts +24 -0
  614. package/src/idl_codegen/instructions/refreshObligationFarmsForReserve.ts +62 -0
  615. package/src/idl_codegen/instructions/refreshReserve.ts +40 -0
  616. package/src/idl_codegen/instructions/refreshReservesBatch.ts +29 -0
  617. package/src/idl_codegen/instructions/repayObligationLiquidity.ts +64 -0
  618. package/src/idl_codegen/instructions/requestElevationGroup.ts +40 -0
  619. package/src/idl_codegen/instructions/socializeLoss.ts +48 -0
  620. package/src/idl_codegen/instructions/updateEntireReserveConfig.ts +45 -0
  621. package/src/idl_codegen/instructions/updateLendingMarket.ts +43 -0
  622. package/src/idl_codegen/instructions/updateLendingMarketOwner.ts +28 -0
  623. package/src/idl_codegen/instructions/updateReserveConfig.ts +52 -0
  624. package/src/idl_codegen/instructions/updateSingleReserveConfig.ts +48 -0
  625. package/src/idl_codegen/instructions/withdrawObligationCollateral.ts +68 -0
  626. package/src/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.ts +102 -0
  627. package/src/idl_codegen/instructions/withdrawProtocolFee.ts +62 -0
  628. package/src/idl_codegen/instructions/withdrawReferrerFees.ts +54 -0
  629. package/src/idl_codegen/programId.ts +13 -0
  630. package/src/idl_codegen/types/AssetTier.ts +118 -0
  631. package/src/idl_codegen/types/BigFractionBytes.ts +67 -0
  632. package/src/idl_codegen/types/BorrowRateCurve.ts +60 -0
  633. package/src/idl_codegen/types/CurvePoint.ts +64 -0
  634. package/src/idl_codegen/types/ElevationGroup.ts +133 -0
  635. package/src/idl_codegen/types/FeeCalculation.ts +90 -0
  636. package/src/idl_codegen/types/InitObligationArgs.ts +61 -0
  637. package/src/idl_codegen/types/LastUpdate.ts +95 -0
  638. package/src/idl_codegen/types/ObligationCollateral.ts +128 -0
  639. package/src/idl_codegen/types/ObligationLiquidity.ts +159 -0
  640. package/src/idl_codegen/types/PriceHeuristic.ts +81 -0
  641. package/src/idl_codegen/types/PythConfiguration.ts +56 -0
  642. package/src/idl_codegen/types/ReserveCollateral.ts +104 -0
  643. package/src/idl_codegen/types/ReserveConfig.ts +467 -0
  644. package/src/idl_codegen/types/ReserveFarmKind.ts +90 -0
  645. package/src/idl_codegen/types/ReserveFees.ts +122 -0
  646. package/src/idl_codegen/types/ReserveLiquidity.ts +286 -0
  647. package/src/idl_codegen/types/ReserveStatus.ts +120 -0
  648. package/src/idl_codegen/types/ScopeConfiguration.ts +85 -0
  649. package/src/idl_codegen/types/SwitchboardConfiguration.ts +67 -0
  650. package/src/idl_codegen/types/TokenInfo.ts +190 -0
  651. package/src/idl_codegen/types/UpdateConfigMode.ts +1440 -0
  652. package/src/idl_codegen/types/UpdateLendingMarketConfigValue.ts +376 -0
  653. package/src/idl_codegen/types/UpdateLendingMarketMode.ts +570 -0
  654. package/src/idl_codegen/types/WithdrawalCaps.ts +86 -0
  655. package/src/idl_codegen/types/index.ts +276 -0
  656. package/src/idl_codegen/zero_padding/ObligationZP.ts +186 -0
  657. package/src/idl_codegen/zero_padding/index.ts +1 -0
  658. package/src/idl_codegen_jupiter_perps/accounts/Pool.ts +155 -0
  659. package/src/idl_codegen_jupiter_perps/accounts/index.ts +2 -0
  660. package/src/idl_codegen_jupiter_perps/programId.ts +7 -0
  661. package/src/idl_codegen_jupiter_perps/types/Fees.ts +130 -0
  662. package/src/idl_codegen_jupiter_perps/types/Limit.ts +72 -0
  663. package/src/idl_codegen_jupiter_perps/types/PoolApr.ts +69 -0
  664. package/src/idl_codegen_jupiter_perps/types/index.ts +6 -0
  665. package/src/idl_codegen_kamino_vault/accounts/Reserve.ts +221 -0
  666. package/src/idl_codegen_kamino_vault/accounts/VaultState.ts +214 -0
  667. package/src/idl_codegen_kamino_vault/accounts/index.ts +4 -0
  668. package/src/idl_codegen_kamino_vault/errors/anchor.ts +764 -0
  669. package/src/idl_codegen_kamino_vault/errors/custom.ts +344 -0
  670. package/src/idl_codegen_kamino_vault/errors/index.ts +49 -0
  671. package/src/idl_codegen_kamino_vault/instructions/deposit.ts +54 -0
  672. package/src/idl_codegen_kamino_vault/instructions/index.ts +10 -0
  673. package/src/idl_codegen_kamino_vault/instructions/initVault.ts +35 -0
  674. package/src/idl_codegen_kamino_vault/instructions/invest.ts +60 -0
  675. package/src/idl_codegen_kamino_vault/instructions/updateReserveAllocation.ts +58 -0
  676. package/src/idl_codegen_kamino_vault/instructions/withdraw.ts +81 -0
  677. package/src/idl_codegen_kamino_vault/programId.ts +7 -0
  678. package/src/idl_codegen_kamino_vault/types/BigFractionBytes.ts +61 -0
  679. package/src/idl_codegen_kamino_vault/types/BorrowRateCurve.ts +55 -0
  680. package/src/idl_codegen_kamino_vault/types/CurvePoint.ts +61 -0
  681. package/src/idl_codegen_kamino_vault/types/LastUpdate.ts +90 -0
  682. package/src/idl_codegen_kamino_vault/types/PriceHeuristic.ts +78 -0
  683. package/src/idl_codegen_kamino_vault/types/PythConfiguration.ts +56 -0
  684. package/src/idl_codegen_kamino_vault/types/ReserveCollateral.ts +104 -0
  685. package/src/idl_codegen_kamino_vault/types/ReserveConfig.ts +426 -0
  686. package/src/idl_codegen_kamino_vault/types/ReserveFees.ts +118 -0
  687. package/src/idl_codegen_kamino_vault/types/ReserveLiquidity.ts +268 -0
  688. package/src/idl_codegen_kamino_vault/types/ScopeConfiguration.ts +85 -0
  689. package/src/idl_codegen_kamino_vault/types/SwitchboardConfiguration.ts +64 -0
  690. package/src/idl_codegen_kamino_vault/types/TokenInfo.ts +172 -0
  691. package/src/idl_codegen_kamino_vault/types/VaultAllocation.ts +115 -0
  692. package/src/idl_codegen_kamino_vault/types/WithdrawalCaps.ts +86 -0
  693. package/src/idl_codegen_kamino_vault/types/index.ts +30 -0
  694. package/src/index.ts +1 -0
  695. package/src/lending_operations/index.ts +2 -0
  696. package/src/lending_operations/repay_with_collateral_calcs.ts +71 -0
  697. package/src/lending_operations/repay_with_collateral_operations.ts +318 -0
  698. package/src/leverage/calcs.ts +465 -0
  699. package/src/leverage/index.ts +4 -0
  700. package/src/leverage/instructions.ts +144 -0
  701. package/src/leverage/operations.ts +1814 -0
  702. package/src/leverage/utils.ts +273 -0
  703. package/src/lib.ts +9 -0
  704. package/src/referrals/index.ts +2 -0
  705. package/src/referrals/instructions.ts +114 -0
  706. package/src/referrals/operations.ts +325 -0
  707. package/src/utils/ObligationType.ts +171 -0
  708. package/src/utils/api.ts +59 -0
  709. package/src/utils/ata.ts +195 -0
  710. package/src/utils/constants.ts +81 -0
  711. package/src/utils/idl.ts +4 -0
  712. package/src/utils/index.ts +16 -0
  713. package/src/utils/instruction.ts +258 -0
  714. package/src/utils/kamino.ts +12 -0
  715. package/src/utils/layout.ts +118 -0
  716. package/src/utils/lookupTable.ts +55 -0
  717. package/src/utils/managerTypes.ts +456 -0
  718. package/src/utils/oracle.ts +337 -0
  719. package/src/utils/pubkey.ts +261 -0
  720. package/src/utils/rpc.ts +108 -0
  721. package/src/utils/seeds.ts +183 -0
  722. package/src/utils/sendTransactionsUtils.ts +207 -0
  723. package/src/utils/slots.ts +28 -0
  724. package/src/utils/syncNative.ts +22 -0
  725. package/src/utils/userMetadata.ts +401 -0
@@ -0,0 +1,1315 @@
1
+ /* eslint-disable max-classes-per-file */
2
+ import { PublicKey } from '@solana/web3.js';
3
+ import Decimal from 'decimal.js';
4
+ import { KaminoReserve } from './reserve';
5
+ import { Obligation } from '../idl_codegen/accounts';
6
+ import { ElevationGroupDescription, KaminoMarket } from './market';
7
+ import BN from 'bn.js';
8
+ import { Fraction } from './fraction';
9
+ import {
10
+ ObligationCollateral,
11
+ ObligationCollateralFields,
12
+ ObligationLiquidity,
13
+ ObligationLiquidityFields,
14
+ } from '../idl_codegen/types';
15
+ import { positiveOrZero, valueOrZero } from './utils';
16
+ import { isNotNullPubkey, PubkeyHashMap, U64_MAX } from '../utils';
17
+ import { ActionType } from './action';
18
+
19
+ export type Position = {
20
+ reserveAddress: PublicKey;
21
+ mintAddress: PublicKey;
22
+ /**
23
+ * Amount of tokens in lamports, including decimal places for interest accrued (no borrow factor weighting)
24
+ */
25
+ amount: Decimal;
26
+ /**
27
+ * Market value of the position in USD (no borrow factor weighting)
28
+ */
29
+ marketValueRefreshed: Decimal;
30
+ };
31
+
32
+ export type ObligationStats = {
33
+ userTotalDeposit: Decimal;
34
+ userTotalBorrow: Decimal;
35
+ userTotalBorrowBorrowFactorAdjusted: Decimal;
36
+ borrowLimit: Decimal;
37
+ borrowLiquidationLimit: Decimal;
38
+ borrowUtilization: Decimal;
39
+ netAccountValue: Decimal;
40
+ loanToValue: Decimal;
41
+ liquidationLtv: Decimal;
42
+ leverage: Decimal;
43
+ potentialElevationGroupUpdate: number;
44
+ };
45
+
46
+ interface BorrowStats {
47
+ borrows: Map<PublicKey, Position>;
48
+ userTotalBorrow: Decimal;
49
+ userTotalBorrowBorrowFactorAdjusted: Decimal;
50
+ positions: number;
51
+ }
52
+
53
+ interface DepositStats {
54
+ deposits: Map<PublicKey, Position>;
55
+ userTotalDeposit: Decimal;
56
+ userTotalCollateralDeposit: Decimal;
57
+ borrowLimit: Decimal;
58
+ liquidationLtv: Decimal;
59
+ borrowLiquidationLimit: Decimal;
60
+ }
61
+
62
+ export class KaminoObligation {
63
+ obligationAddress: PublicKey;
64
+
65
+ state: Obligation;
66
+
67
+ /**
68
+ * Deposits stored in a map of reserve address to position
69
+ */
70
+ deposits: Map<PublicKey, Position>;
71
+
72
+ /**
73
+ * Borrows stored in a map of reserve address to position
74
+ */
75
+ borrows: Map<PublicKey, Position>;
76
+
77
+ refreshedStats: ObligationStats;
78
+
79
+ obligationTag: number;
80
+
81
+ /**
82
+ * Initialise a new Obligation from the deserialized state
83
+ * @param market
84
+ * @param obligationAddress
85
+ * @param obligation
86
+ * @param collateralExchangeRates - rates from the market by reserve address, will be calculated if not provided
87
+ * @param cumulativeBorrowRates - rates from the market by reserve address, will be calculated if not provided
88
+ */
89
+ constructor(
90
+ market: KaminoMarket,
91
+ obligationAddress: PublicKey,
92
+ obligation: Obligation,
93
+ collateralExchangeRates: Map<PublicKey, Decimal>,
94
+ cumulativeBorrowRates: Map<PublicKey, Decimal>
95
+ ) {
96
+ this.obligationAddress = obligationAddress;
97
+ this.state = obligation;
98
+ const { borrows, deposits, refreshedStats } = this.calculatePositions(
99
+ market,
100
+ obligation.deposits,
101
+ obligation.borrows,
102
+ obligation.elevationGroup,
103
+ collateralExchangeRates,
104
+ cumulativeBorrowRates
105
+ );
106
+ this.deposits = deposits;
107
+ this.borrows = borrows;
108
+ this.refreshedStats = refreshedStats;
109
+ this.obligationTag = obligation.tag.toNumber();
110
+ }
111
+
112
+ static async load(kaminoMarket: KaminoMarket, obligationAddress: PublicKey): Promise<KaminoObligation | null> {
113
+ const res = await kaminoMarket.getConnection().getAccountInfoAndContext(obligationAddress);
114
+ if (res.value === null) {
115
+ return null;
116
+ }
117
+ const accInfo = res.value;
118
+ if (!accInfo.owner.equals(kaminoMarket.programId)) {
119
+ throw new Error("account doesn't belong to this program");
120
+ }
121
+ const obligation = Obligation.decode(accInfo.data);
122
+
123
+ if (obligation === null) {
124
+ return null;
125
+ }
126
+ const { collateralExchangeRates, cumulativeBorrowRates } = KaminoObligation.getRatesForObligation(
127
+ kaminoMarket,
128
+ obligation,
129
+ res.context.slot
130
+ );
131
+ return new KaminoObligation(
132
+ kaminoMarket,
133
+ obligationAddress,
134
+ obligation,
135
+ collateralExchangeRates,
136
+ cumulativeBorrowRates
137
+ );
138
+ }
139
+
140
+ static async loadAll(
141
+ kaminoMarket: KaminoMarket,
142
+ obligationAddresses: PublicKey[],
143
+ slot?: number
144
+ ): Promise<(KaminoObligation | null)[]> {
145
+ let currentSlot = slot;
146
+ let obligations: (Obligation | null)[];
147
+ if (!currentSlot) {
148
+ [currentSlot, obligations] = await Promise.all([
149
+ kaminoMarket.getConnection().getSlot(),
150
+ Obligation.fetchMultiple(kaminoMarket.getConnection(), obligationAddresses, kaminoMarket.programId),
151
+ ]);
152
+ } else {
153
+ obligations = await Obligation.fetchMultiple(
154
+ kaminoMarket.getConnection(),
155
+ obligationAddresses,
156
+ kaminoMarket.programId
157
+ );
158
+ }
159
+ const cumulativeBorrowRates = new PubkeyHashMap<PublicKey, Decimal>();
160
+ const collateralExchangeRates = new PubkeyHashMap<PublicKey, Decimal>();
161
+ for (const obligation of obligations) {
162
+ if (obligation !== null) {
163
+ KaminoObligation.addRatesForObligation(
164
+ kaminoMarket,
165
+ obligation,
166
+ collateralExchangeRates,
167
+ cumulativeBorrowRates,
168
+ currentSlot
169
+ );
170
+ }
171
+ }
172
+
173
+ return obligations.map((obligation, i) => {
174
+ if (obligation === null) {
175
+ return null;
176
+ }
177
+ return new KaminoObligation(
178
+ kaminoMarket,
179
+ obligationAddresses[i],
180
+ obligation,
181
+ collateralExchangeRates,
182
+ cumulativeBorrowRates
183
+ );
184
+ });
185
+ }
186
+
187
+ /**
188
+ * @returns the obligation borrows as a list
189
+ */
190
+ getBorrows(): Array<Position> {
191
+ return [...this.borrows.values()];
192
+ }
193
+
194
+ /**
195
+ * @returns the obligation borrows as a list
196
+ */
197
+ getDeposits(): Array<Position> {
198
+ return [...this.deposits.values()];
199
+ }
200
+
201
+ /**
202
+ * @returns the total deposited value of the obligation (sum of all deposits)
203
+ */
204
+ getDepositedValue(): Decimal {
205
+ return new Fraction(this.state.depositedValueSf).toDecimal();
206
+ }
207
+
208
+ /**
209
+ * @returns the total borrowed value of the obligation (sum of all borrows -- no borrow factor)
210
+ */
211
+ getBorrowedMarketValue(): Decimal {
212
+ return new Fraction(this.state.borrowedAssetsMarketValueSf).toDecimal();
213
+ }
214
+
215
+ /**
216
+ * @returns the total borrowed value of the obligation (sum of all borrows -- with borrow factor weighting)
217
+ */
218
+ getBorrowedMarketValueBFAdjusted(): Decimal {
219
+ return new Fraction(this.state.borrowFactorAdjustedDebtValueSf).toDecimal();
220
+ }
221
+
222
+ /**
223
+ * @returns total borrow power of the obligation, relative to max LTV of each asset's reserve
224
+ */
225
+ getAllowedBorrowValue(): Decimal {
226
+ return new Fraction(this.state.allowedBorrowValueSf).toDecimal();
227
+ }
228
+
229
+ /**
230
+ * @returns the borrow value at which the obligation gets liquidatable
231
+ * (relative to the liquidation threshold of each asset's reserve)
232
+ */
233
+ getUnhealthyBorrowValue(): Decimal {
234
+ return new Fraction(this.state.unhealthyBorrowValueSf).toDecimal();
235
+ }
236
+
237
+ /**
238
+ *
239
+ * @returns Market value of the deposit in the specified obligation collateral/deposit asset (USD)
240
+ */
241
+ getDepositMarketValue(deposit: ObligationCollateral): Decimal {
242
+ return new Fraction(deposit.marketValueSf).toDecimal();
243
+ }
244
+
245
+ getBorrowByReserve(reserve: PublicKey): Position | undefined {
246
+ return this.borrows.get(reserve);
247
+ }
248
+
249
+ getDepositByReserve(reserve: PublicKey): Position | undefined {
250
+ return this.deposits.get(reserve);
251
+ }
252
+
253
+ getBorrowByMint(mint: PublicKey): Position | undefined {
254
+ for (const value of this.borrows.values()) {
255
+ if (value.mintAddress.equals(mint)) {
256
+ return value;
257
+ }
258
+ }
259
+ return undefined;
260
+ }
261
+
262
+ getDepositByMint(mint: PublicKey): Position | undefined {
263
+ for (const value of this.deposits.values()) {
264
+ if (value.mintAddress.equals(mint)) {
265
+ return value;
266
+ }
267
+ }
268
+ return undefined;
269
+ }
270
+
271
+ /**
272
+ *
273
+ * @returns Market value of the borrow in the specified obligation liquidity/borrow asset (USD) (no borrow factor weighting)
274
+ */
275
+ getBorrowMarketValue(borrow: ObligationLiquidity): Decimal {
276
+ return new Fraction(borrow.marketValueSf).toDecimal();
277
+ }
278
+
279
+ /**
280
+ *
281
+ * @returns Market value of the borrow in the specified obligation liquidity/borrow asset (USD) (with borrow factor weighting)
282
+ */
283
+ getBorrowMarketValueBFAdjusted(borrow: ObligationLiquidity): Decimal {
284
+ return new Fraction(borrow.borrowFactorAdjustedMarketValueSf).toDecimal();
285
+ }
286
+
287
+ /**
288
+ * Calculate the current ratio of borrowed value to deposited value
289
+ */
290
+ loanToValue(): Decimal {
291
+ if (this.refreshedStats.userTotalDeposit.eq(0)) {
292
+ return new Decimal(0);
293
+ }
294
+ return this.refreshedStats.userTotalBorrowBorrowFactorAdjusted.div(this.refreshedStats.userTotalDeposit);
295
+ }
296
+
297
+ /**
298
+ * @returns the total number of positions (deposits + borrows)
299
+ */
300
+ getNumberOfPositions(): number {
301
+ return this.deposits.size + this.borrows.size;
302
+ }
303
+
304
+ getNetAccountValue(): Decimal {
305
+ return this.refreshedStats.netAccountValue;
306
+ }
307
+
308
+ /**
309
+ * @returns the potential elevation groups the obligation qualifies for
310
+ */
311
+ getElevationGroups(kaminoMarket: KaminoMarket): Array<number> {
312
+ const reserves = new PubkeyHashMap<PublicKey, KaminoReserve>();
313
+ for (const deposit of this.state.deposits.values()) {
314
+ if (isNotNullPubkey(deposit.depositReserve) && !reserves.has(deposit.depositReserve)) {
315
+ reserves.set(deposit.depositReserve, kaminoMarket.getReserveByAddress(deposit.depositReserve)!);
316
+ }
317
+ }
318
+ for (const borrow of this.state.borrows.values()) {
319
+ if (isNotNullPubkey(borrow.borrowReserve) && !reserves.has(borrow.borrowReserve)) {
320
+ reserves.set(borrow.borrowReserve, kaminoMarket.getReserveByAddress(borrow.borrowReserve)!);
321
+ }
322
+ }
323
+ return KaminoObligation.getElevationGroupsForReserves([...reserves.values()]);
324
+ }
325
+
326
+ static getElevationGroupsForReserves(reserves: Array<KaminoReserve>): Array<number> {
327
+ const elevationGroupsCounts = new Map<number, number>();
328
+ for (const reserve of reserves) {
329
+ for (const elevationGroup of reserve.state.config.elevationGroups) {
330
+ if (elevationGroup !== 0) {
331
+ const count = elevationGroupsCounts.get(elevationGroup);
332
+ if (count) {
333
+ elevationGroupsCounts.set(elevationGroup, count + 1);
334
+ } else {
335
+ elevationGroupsCounts.set(elevationGroup, 1);
336
+ }
337
+ }
338
+ }
339
+ }
340
+ const activeElevationGroups = new Array<number>();
341
+ for (const [group, count] of elevationGroupsCounts.entries()) {
342
+ if (count === reserves.length) {
343
+ activeElevationGroups.push(group);
344
+ }
345
+ }
346
+ return activeElevationGroups;
347
+ }
348
+
349
+ simulateDepositChange(
350
+ obligationDeposits: ObligationCollateral[],
351
+ changeInLamports: number,
352
+ changeReserve: PublicKey,
353
+ collateralExchangeRates: Map<PublicKey, Decimal>
354
+ ): ObligationCollateral[] {
355
+ const newDeposits: ObligationCollateral[] = [];
356
+ for (let i = 0; i < obligationDeposits.length; i++) {
357
+ if (obligationDeposits[i].depositReserve.equals(changeReserve)) {
358
+ const coll: ObligationCollateralFields = { ...obligationDeposits[i] };
359
+ const exchangeRate = collateralExchangeRates.get(changeReserve)!;
360
+ const changeInCollateral = new Decimal(changeInLamports).mul(exchangeRate);
361
+ const updatedDeposit = new Decimal(obligationDeposits[i].depositedAmount.toNumber()).add(changeInCollateral);
362
+ coll.depositedAmount = new BN(positiveOrZero(updatedDeposit).toString());
363
+ newDeposits.push(new ObligationCollateral(coll));
364
+ } else {
365
+ newDeposits.push(obligationDeposits[i]);
366
+ }
367
+ }
368
+
369
+ return newDeposits;
370
+ }
371
+
372
+ simulateBorrowChange(
373
+ obligationBorrows: ObligationLiquidity[],
374
+ changeInLamports: number,
375
+ changeReserve: PublicKey
376
+ ): ObligationLiquidity[] {
377
+ const newBorrows: ObligationLiquidity[] = [];
378
+ for (let i = 0; i < obligationBorrows.length; i++) {
379
+ if (obligationBorrows[i].borrowReserve.equals(changeReserve)) {
380
+ const borrow: ObligationLiquidityFields = { ...obligationBorrows[i] };
381
+ const newBorrowedAmount: Decimal = new Fraction(borrow.borrowedAmountSf).toDecimal().add(changeInLamports);
382
+ const newBorrowedAmountSf = Fraction.fromDecimal(positiveOrZero(newBorrowedAmount)).getValue();
383
+ borrow.borrowedAmountSf = newBorrowedAmountSf;
384
+ newBorrows.push(new ObligationLiquidity(borrow));
385
+ } else {
386
+ newBorrows.push(obligationBorrows[i]);
387
+ }
388
+ }
389
+
390
+ return newBorrows;
391
+ }
392
+
393
+ /**
394
+ * Calculate the newly modified stats of the obligation
395
+ */
396
+ // TODO: Shall we set up position limits?
397
+ getSimulatedObligationStats(params: {
398
+ amountCollateral?: Decimal;
399
+ amountDebt?: Decimal;
400
+ action: ActionType;
401
+ mintCollateral?: PublicKey;
402
+ mintDebt?: PublicKey;
403
+ market: KaminoMarket;
404
+ reserves: Map<PublicKey, KaminoReserve>;
405
+ slot: number;
406
+ elevationGroupOverride?: number;
407
+ }): {
408
+ stats: ObligationStats;
409
+ deposits: Map<PublicKey, Position>;
410
+ borrows: Map<PublicKey, Position>;
411
+ } {
412
+ const { amountCollateral, amountDebt, action, mintCollateral, mintDebt, market } = params;
413
+ let newStats = { ...this.refreshedStats };
414
+
415
+ const { collateralExchangeRates, cumulativeBorrowRates } = KaminoObligation.getRatesForObligation(
416
+ market,
417
+ this.state,
418
+ params.slot
419
+ );
420
+
421
+ const elevationGroup = params.elevationGroupOverride ?? this.state.elevationGroup;
422
+
423
+ let newDeposits: Map<PublicKey, Position> = new PubkeyHashMap<PublicKey, Position>([...this.deposits.entries()]);
424
+ let newBorrows: Map<PublicKey, Position> = new PubkeyHashMap<PublicKey, Position>([...this.borrows.entries()]);
425
+
426
+ // Any action can impact both deposit stats and borrow stats if elevation group is changed
427
+ // so we have to recalculate the entire position, not just an updated deposit or borrow
428
+ // as both LTVs and borrow factors can change, affecting all calcs
429
+
430
+ const collateralReserve = mintCollateral ? market.getReserveByMint(mintCollateral)!.address : undefined;
431
+ const debtReserve = mintDebt ? market.getReserveByMint(mintDebt)!.address : undefined;
432
+
433
+ let newObligationDeposits = this.state.deposits;
434
+ let newObligationBorrows = this.state.borrows;
435
+
436
+ switch (action) {
437
+ case 'deposit': {
438
+ if (amountCollateral === undefined || mintCollateral === undefined) {
439
+ throw Error('amountCollateral & mintCollateral are required for deposit action');
440
+ }
441
+ newObligationDeposits = this.simulateDepositChange(
442
+ this.state.deposits,
443
+ amountCollateral.toNumber(),
444
+ collateralReserve!,
445
+ collateralExchangeRates
446
+ );
447
+ break;
448
+ }
449
+ case 'borrow': {
450
+ if (amountDebt === undefined || mintDebt === undefined) {
451
+ throw Error('amountDebt & mintDebt are required for borrow action');
452
+ }
453
+
454
+ newObligationBorrows = this.simulateBorrowChange(this.state.borrows, amountDebt.toNumber(), debtReserve!);
455
+ break;
456
+ }
457
+ case 'repay': {
458
+ if (amountDebt === undefined || mintDebt === undefined) {
459
+ throw Error('amountDebt & mintDebt are required for repay action');
460
+ }
461
+ newObligationBorrows = this.simulateBorrowChange(this.state.borrows, amountDebt.neg().toNumber(), debtReserve!);
462
+
463
+ break;
464
+ }
465
+
466
+ case 'withdraw': {
467
+ if (amountCollateral === undefined || mintCollateral === undefined) {
468
+ throw Error('amountCollateral & mintCollateral are required for withdraw action');
469
+ }
470
+ newObligationDeposits = this.simulateDepositChange(
471
+ this.state.deposits,
472
+ amountCollateral.neg().toNumber(),
473
+ collateralReserve!,
474
+ collateralExchangeRates
475
+ );
476
+ break;
477
+ }
478
+ case 'depositAndBorrow': {
479
+ if (
480
+ amountCollateral === undefined ||
481
+ amountDebt === undefined ||
482
+ mintCollateral === undefined ||
483
+ mintDebt === undefined
484
+ ) {
485
+ throw Error('amountColl & amountDebt & mintCollateral & mintDebt are required for depositAndBorrow action');
486
+ }
487
+ newObligationDeposits = this.simulateDepositChange(
488
+ this.state.deposits,
489
+ amountCollateral.toNumber(),
490
+ collateralReserve!,
491
+ collateralExchangeRates
492
+ );
493
+ newObligationBorrows = this.simulateBorrowChange(this.state.borrows, amountDebt.toNumber(), debtReserve!);
494
+ break;
495
+ }
496
+ case 'repayAndWithdraw': {
497
+ if (
498
+ amountCollateral === undefined ||
499
+ amountDebt === undefined ||
500
+ mintCollateral === undefined ||
501
+ mintDebt === undefined
502
+ ) {
503
+ throw Error('amountColl & amountDebt & mintCollateral & mintDebt are required for repayAndWithdraw action');
504
+ }
505
+ newObligationDeposits = this.simulateDepositChange(
506
+ this.state.deposits,
507
+ amountCollateral.neg().toNumber(),
508
+ collateralReserve!,
509
+ collateralExchangeRates
510
+ );
511
+ newObligationBorrows = this.simulateBorrowChange(this.state.borrows, amountDebt.neg().toNumber(), debtReserve!);
512
+ break;
513
+ }
514
+ default: {
515
+ throw Error(`Invalid action type ${action} for getSimulatedObligationStats`);
516
+ }
517
+ }
518
+
519
+ const { borrows, deposits, refreshedStats } = this.calculatePositions(
520
+ market,
521
+ newObligationDeposits,
522
+ newObligationBorrows,
523
+ elevationGroup,
524
+ collateralExchangeRates,
525
+ cumulativeBorrowRates
526
+ );
527
+
528
+ newStats = refreshedStats;
529
+ newDeposits = deposits;
530
+ newBorrows = borrows;
531
+
532
+ newStats.netAccountValue = newStats.userTotalDeposit.minus(newStats.userTotalBorrow);
533
+ newStats.loanToValue = valueOrZero(
534
+ newStats.userTotalBorrowBorrowFactorAdjusted.dividedBy(newStats.userTotalDeposit)
535
+ );
536
+ newStats.leverage = valueOrZero(newStats.userTotalDeposit.dividedBy(newStats.netAccountValue));
537
+
538
+ return {
539
+ stats: newStats,
540
+ deposits: newDeposits,
541
+ borrows: newBorrows,
542
+ };
543
+ }
544
+
545
+ estimateObligationInterestRate = (
546
+ market: KaminoMarket,
547
+ reserve: KaminoReserve,
548
+ borrow: ObligationLiquidity,
549
+ currentSlot: number
550
+ ): Decimal => {
551
+ const estimatedCumulativeBorrowRate = reserve.getEstimatedCumulativeBorrowRate(
552
+ currentSlot,
553
+ market.state.referralFeeBps
554
+ );
555
+
556
+ const currentCumulativeBorrowRate = KaminoObligation.getCumulativeBorrowRate(borrow);
557
+
558
+ if (estimatedCumulativeBorrowRate.gt(currentCumulativeBorrowRate)) {
559
+ return estimatedCumulativeBorrowRate.div(currentCumulativeBorrowRate);
560
+ }
561
+
562
+ return new Decimal(0);
563
+ };
564
+
565
+ private calculatePositions(
566
+ market: KaminoMarket,
567
+ obligationDeposits: ObligationCollateral[],
568
+ obligationBorrows: ObligationLiquidity[],
569
+ elevationGroup: number,
570
+ collateralExchangeRates: Map<PublicKey, Decimal>,
571
+ cumulativeBorrowRates: Map<PublicKey, Decimal>
572
+ ): {
573
+ borrows: Map<PublicKey, Position>;
574
+ deposits: Map<PublicKey, Position>;
575
+ refreshedStats: ObligationStats;
576
+ } {
577
+ const getOraclePx = (reserve: KaminoReserve) => reserve.getOracleMarketPrice();
578
+
579
+ const depositStatsOraclePrice = KaminoObligation.calculateObligationDeposits(
580
+ market,
581
+ obligationDeposits,
582
+ collateralExchangeRates,
583
+ elevationGroup,
584
+ getOraclePx
585
+ );
586
+
587
+ const borrowStatsOraclePrice = KaminoObligation.calculateObligationBorrows(
588
+ market,
589
+ obligationBorrows,
590
+ cumulativeBorrowRates,
591
+ elevationGroup,
592
+ getOraclePx
593
+ );
594
+
595
+ const netAccountValueScopeRefreshed = depositStatsOraclePrice.userTotalDeposit.minus(
596
+ borrowStatsOraclePrice.userTotalBorrow
597
+ );
598
+
599
+ // TODO: Fix this?
600
+ const potentialElevationGroupUpdate = 0;
601
+
602
+ return {
603
+ deposits: depositStatsOraclePrice.deposits,
604
+ borrows: borrowStatsOraclePrice.borrows,
605
+ refreshedStats: {
606
+ borrowLimit: depositStatsOraclePrice.borrowLimit,
607
+ borrowLiquidationLimit: depositStatsOraclePrice.borrowLiquidationLimit,
608
+ userTotalBorrow: borrowStatsOraclePrice.userTotalBorrow,
609
+ userTotalBorrowBorrowFactorAdjusted: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted,
610
+ userTotalDeposit: depositStatsOraclePrice.userTotalDeposit,
611
+ liquidationLtv: depositStatsOraclePrice.liquidationLtv,
612
+ borrowUtilization: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted.dividedBy(
613
+ depositStatsOraclePrice.borrowLimit
614
+ ),
615
+ netAccountValue: netAccountValueScopeRefreshed,
616
+ leverage: depositStatsOraclePrice.userTotalDeposit.dividedBy(netAccountValueScopeRefreshed),
617
+ loanToValue: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted.dividedBy(
618
+ depositStatsOraclePrice.userTotalDeposit
619
+ ),
620
+ potentialElevationGroupUpdate,
621
+ },
622
+ };
623
+ }
624
+
625
+ public static calculateObligationDeposits(
626
+ market: KaminoMarket,
627
+ obligationDeposits: ObligationCollateral[],
628
+ collateralExchangeRates: Map<PublicKey, Decimal> | null,
629
+ elevationGroup: number,
630
+ getPx: (reserve: KaminoReserve) => Decimal
631
+ ): DepositStats {
632
+ let userTotalDeposit = new Decimal(0);
633
+ let userTotalCollateralDeposit = new Decimal(0);
634
+ let borrowLimit = new Decimal(0);
635
+ let borrowLiquidationLimit = new Decimal(0);
636
+
637
+ const deposits = new PubkeyHashMap<PublicKey, Position>();
638
+ for (let i = 0; i < obligationDeposits.length; i++) {
639
+ if (!isNotNullPubkey(obligationDeposits[i].depositReserve)) {
640
+ continue;
641
+ }
642
+ const deposit = obligationDeposits[i];
643
+ const reserve = market.getReserveByAddress(deposit.depositReserve);
644
+ if (!reserve) {
645
+ throw new Error(
646
+ `Obligation contains a deposit belonging to reserve: ${deposit.depositReserve} but the reserve was not found on the market. Deposit amount: ${deposit.depositedAmount}`
647
+ );
648
+ }
649
+ const { maxLtv, liquidationLtv } = KaminoObligation.getLtvForReserve(market, reserve, elevationGroup);
650
+
651
+ let exchangeRate: Decimal;
652
+ if (collateralExchangeRates !== null) {
653
+ exchangeRate = collateralExchangeRates.get(reserve.address)!;
654
+ } else {
655
+ exchangeRate = reserve.getCollateralExchangeRate();
656
+ }
657
+ const supplyAmount = new Decimal(deposit.depositedAmount.toString()).div(exchangeRate);
658
+
659
+ const depositValueUsd = supplyAmount.mul(getPx(reserve)).div(reserve.getMintFactor());
660
+
661
+ userTotalDeposit = userTotalDeposit.add(depositValueUsd);
662
+
663
+ if (!maxLtv.eq('0')) {
664
+ userTotalCollateralDeposit = userTotalCollateralDeposit.add(depositValueUsd);
665
+ }
666
+
667
+ borrowLimit = borrowLimit.add(depositValueUsd.mul(maxLtv));
668
+ borrowLiquidationLimit = borrowLiquidationLimit.add(depositValueUsd.mul(liquidationLtv));
669
+
670
+ const position: Position = {
671
+ reserveAddress: reserve.address,
672
+ mintAddress: reserve.getLiquidityMint(),
673
+ amount: supplyAmount,
674
+ marketValueRefreshed: depositValueUsd,
675
+ };
676
+ deposits.set(reserve.address, position);
677
+ }
678
+
679
+ return {
680
+ deposits,
681
+ userTotalDeposit,
682
+ userTotalCollateralDeposit,
683
+ borrowLimit,
684
+ liquidationLtv: valueOrZero(borrowLiquidationLimit.div(userTotalDeposit)),
685
+ borrowLiquidationLimit,
686
+ };
687
+ }
688
+
689
+ public static calculateObligationBorrows(
690
+ market: KaminoMarket,
691
+ obligationBorrows: ObligationLiquidity[],
692
+ cumulativeBorrowRates: Map<PublicKey, Decimal> | null,
693
+ elevationGroup: number,
694
+ getPx: (reserve: KaminoReserve) => Decimal
695
+ ): BorrowStats {
696
+ let userTotalBorrow = new Decimal(0);
697
+ let userTotalBorrowBorrowFactorAdjusted = new Decimal(0);
698
+ let positions = 0;
699
+
700
+ const borrows = new PubkeyHashMap<PublicKey, Position>();
701
+ for (let i = 0; i < obligationBorrows.length; i++) {
702
+ if (!isNotNullPubkey(obligationBorrows[i].borrowReserve)) {
703
+ continue;
704
+ }
705
+ const borrow = obligationBorrows[i];
706
+ const reserve = market.getReserveByAddress(borrow.borrowReserve);
707
+ if (!reserve) {
708
+ throw new Error(
709
+ `Obligation contains a borrow belonging to reserve: ${
710
+ borrow.borrowReserve
711
+ } but the reserve was not found on the market. Borrow amount: ${KaminoObligation.getBorrowAmount(borrow)}`
712
+ );
713
+ }
714
+
715
+ const obligationCumulativeBorrowRate = KaminoObligation.getCumulativeBorrowRate(borrow);
716
+ let cumulativeBorrowRate;
717
+ if (cumulativeBorrowRates !== null) {
718
+ cumulativeBorrowRate = cumulativeBorrowRates.get(reserve.address)!;
719
+ } else {
720
+ cumulativeBorrowRate = reserve.getCumulativeBorrowRate();
721
+ }
722
+
723
+ const borrowAmount = KaminoObligation.getBorrowAmount(borrow)
724
+ .mul(cumulativeBorrowRate)
725
+ .dividedBy(obligationCumulativeBorrowRate);
726
+
727
+ const borrowValueUsd = borrowAmount.mul(getPx(reserve)).dividedBy(reserve.getMintFactor());
728
+
729
+ const borrowFactor = KaminoObligation.getBorrowFactorForReserve(reserve, elevationGroup);
730
+ const borrowValueBorrowFactorAdjustedUsd = borrowValueUsd.mul(borrowFactor);
731
+
732
+ if (!borrowAmount.eq(new Decimal('0'))) {
733
+ positions += 1;
734
+ }
735
+
736
+ userTotalBorrow = userTotalBorrow.plus(borrowValueUsd);
737
+ userTotalBorrowBorrowFactorAdjusted = userTotalBorrowBorrowFactorAdjusted.plus(
738
+ borrowValueBorrowFactorAdjustedUsd
739
+ );
740
+
741
+ const position: Position = {
742
+ reserveAddress: reserve.address,
743
+ mintAddress: reserve.getLiquidityMint(),
744
+ amount: borrowAmount,
745
+ marketValueRefreshed: borrowValueUsd,
746
+ };
747
+ borrows.set(reserve.address, position);
748
+ }
749
+
750
+ return {
751
+ borrows,
752
+ userTotalBorrow,
753
+ userTotalBorrowBorrowFactorAdjusted,
754
+ positions,
755
+ };
756
+ }
757
+
758
+ getMaxLoanLtvGivenElevationGroup(market: KaminoMarket, elevationGroup: number, slot: number): Decimal {
759
+ const getOraclePx = (reserve: KaminoReserve) => reserve.getOracleMarketPrice();
760
+ const { collateralExchangeRates } = KaminoObligation.getRatesForObligation(market, this.state, slot);
761
+
762
+ const { borrowLimit, userTotalDeposit } = KaminoObligation.calculateObligationDeposits(
763
+ market,
764
+ this.state.deposits,
765
+ collateralExchangeRates,
766
+ elevationGroup,
767
+ getOraclePx
768
+ );
769
+
770
+ if (borrowLimit.eq(0) || userTotalDeposit.eq(0)) {
771
+ return new Decimal(0);
772
+ }
773
+
774
+ return borrowLimit.div(userTotalDeposit);
775
+ }
776
+
777
+ /*
778
+ How much of a given token can a user borrow extra given an elevation group,
779
+ regardless of caps and liquidity or assuming infinite liquidity and infinite caps,
780
+ until it hits max LTV.
781
+
782
+ This is purely a function about the borrow power of an obligation,
783
+ not a reserve-specific, caps-specific, liquidity-specific function.
784
+
785
+ * @param market - The KaminoMarket instance.
786
+ * @param liquidityMint - The liquidity mint PublicKey.
787
+ * @param slot - The slot number.
788
+ * @param elevationGroup - The elevation group number (default: this.state.elevationGroup).
789
+ * @returns The borrow power as a Decimal.
790
+ * @throws Error if the reserve is not found.
791
+ */
792
+ getBorrowPower(
793
+ market: KaminoMarket,
794
+ liquidityMint: PublicKey,
795
+ slot: number,
796
+ elevationGroup: number = this.state.elevationGroup
797
+ ): Decimal {
798
+ const reserve = market.getReserveByMint(liquidityMint);
799
+ if (!reserve) {
800
+ throw new Error('Reserve not found');
801
+ }
802
+
803
+ const elevationGroupActivated =
804
+ reserve.state.config.elevationGroups.includes(elevationGroup) && elevationGroup !== 0;
805
+
806
+ const borrowFactor = KaminoObligation.getBorrowFactorForReserve(reserve, elevationGroup);
807
+
808
+ const getOraclePx = (reserve: KaminoReserve) => reserve.getOracleMarketPrice();
809
+ const { collateralExchangeRates, cumulativeBorrowRates } = KaminoObligation.getRatesForObligation(
810
+ market,
811
+ this.state,
812
+ slot
813
+ );
814
+
815
+ const { borrowLimit } = KaminoObligation.calculateObligationDeposits(
816
+ market,
817
+ this.state.deposits,
818
+ collateralExchangeRates,
819
+ elevationGroup,
820
+ getOraclePx
821
+ );
822
+
823
+ const { userTotalBorrowBorrowFactorAdjusted } = KaminoObligation.calculateObligationBorrows(
824
+ market,
825
+ this.state.borrows,
826
+ cumulativeBorrowRates,
827
+ elevationGroup,
828
+ getOraclePx
829
+ );
830
+
831
+ const maxObligationBorrowPower = borrowLimit // adjusted available amount
832
+ .minus(userTotalBorrowBorrowFactorAdjusted)
833
+ .div(borrowFactor)
834
+ .div(reserve.getOracleMarketPrice())
835
+ .mul(reserve.getMintFactor());
836
+
837
+ // If it has any collateral outside emode, then return 0
838
+ for (const [_, value] of this.deposits.entries()) {
839
+ const depositReserve = market.getReserveByAddress(value.reserveAddress);
840
+ if (!depositReserve) {
841
+ throw new Error('Reserve not found');
842
+ }
843
+ if (depositReserve.state.config.disableUsageAsCollOutsideEmode && !elevationGroupActivated) {
844
+ return new Decimal(0);
845
+ }
846
+ }
847
+
848
+ // This is not amazing because it assumes max borrow, which is not true
849
+ let originationFeeRate = reserve.getBorrowFee();
850
+
851
+ // Inclusive fee rate
852
+ originationFeeRate = originationFeeRate.div(originationFeeRate.add(new Decimal(1)));
853
+ const borrowFee = maxObligationBorrowPower.mul(originationFeeRate);
854
+
855
+ const maxBorrowAmount = maxObligationBorrowPower.sub(borrowFee);
856
+
857
+ return Decimal.max(new Decimal(0), maxBorrowAmount);
858
+ }
859
+
860
+ /*
861
+ How much of a given token can a user borrow extra given an elevation group,
862
+ and a specific reserve, until it hits max LTV and given available liquidity and caps.
863
+
864
+ * @param market - The KaminoMarket instance.
865
+ * @param liquidityMint - The liquidity mint PublicKey.
866
+ * @param slot - The slot number.
867
+ * @param elevationGroup - The elevation group number (default: this.state.elevationGroup).
868
+ * @returns The maximum borrow amount as a Decimal.
869
+ * @throws Error if the reserve is not found.
870
+ */
871
+ getMaxBorrowAmountV2(
872
+ market: KaminoMarket,
873
+ liquidityMint: PublicKey,
874
+ slot: number,
875
+ elevationGroup: number = this.state.elevationGroup
876
+ ): Decimal {
877
+ const reserve = market.getReserveByMint(liquidityMint);
878
+ if (!reserve) {
879
+ throw new Error('Reserve not found');
880
+ }
881
+
882
+ const liquidityAvailable = reserve.getLiquidityAvailableForDebtReserveGivenCaps(market, [elevationGroup])[0];
883
+ const maxBorrowAmount = this.getBorrowPower(market, liquidityMint, slot, elevationGroup);
884
+
885
+ if (elevationGroup === this.state.elevationGroup) {
886
+ return Decimal.min(maxBorrowAmount, liquidityAvailable);
887
+ } else {
888
+ const { amount: debtThisReserve } = this.borrows.get(reserve.address) || { amount: new Decimal(0) };
889
+ const liquidityAvailablePostMigration = Decimal.max(0, liquidityAvailable.minus(debtThisReserve));
890
+ return Decimal.min(maxBorrowAmount, liquidityAvailablePostMigration);
891
+ }
892
+ }
893
+
894
+ /*
895
+ Returns true if the loan is eligible for the elevation group, including for the default one.
896
+ * @param market - The KaminoMarket object representing the market.
897
+ * @param slot - The slot number of the loan.
898
+ * @param elevationGroup - The elevation group number.
899
+ * @returns A boolean indicating whether the loan is eligible for elevation.
900
+ */
901
+ isLoanEligibleForElevationGroup(market: KaminoMarket, slot: number, elevationGroup: number): boolean {
902
+ // - isLoanEligibleForEmode(obligation, emode: 0 | number): <boolean, ErrorMessage>
903
+ // - essentially checks if a loan can be migrated or not
904
+ // - [x] due to collateral / debt reserves combination
905
+ // - [x] due to LTV, etc
906
+
907
+ const reserveDeposits: string[] = Array.from(this.deposits.keys()).map((x) => x.toString());
908
+ const reserveBorrows: string[] = Array.from(this.borrows.keys()).map((x) => x.toString());
909
+
910
+ if (reserveBorrows.length > 1) {
911
+ return false;
912
+ }
913
+
914
+ if (elevationGroup > 0) {
915
+ // Elevation group 0 doesn't need to do reserve checks, as all are included by default
916
+ const allElevationGroups = market.getMarketElevationGroupDescriptions();
917
+ const elevationGroupDescription = allElevationGroups[elevationGroup - 1];
918
+
919
+ // Has to be a subset
920
+ const allCollsIncluded = reserveDeposits.every((reserve) =>
921
+ elevationGroupDescription.collateralReserves.includes(reserve)
922
+ );
923
+ const allDebtsIncluded =
924
+ reserveBorrows.length === 0 ||
925
+ (reserveBorrows.length === 1 && elevationGroupDescription.debtReserve === reserveBorrows[0]);
926
+
927
+ if (!allCollsIncluded || !allDebtsIncluded) {
928
+ return false;
929
+ }
930
+ }
931
+
932
+ // Check if the loan can be migrated based on LTV
933
+ const getOraclePx = (reserve: KaminoReserve) => reserve.getOracleMarketPrice();
934
+ const { collateralExchangeRates } = KaminoObligation.getRatesForObligation(market, this.state, slot);
935
+
936
+ const { borrowLimit } = KaminoObligation.calculateObligationDeposits(
937
+ market,
938
+ this.state.deposits,
939
+ collateralExchangeRates,
940
+ elevationGroup,
941
+ getOraclePx
942
+ );
943
+
944
+ const isEligibleBasedOnLtv = this.refreshedStats.userTotalBorrowBorrowFactorAdjusted.lte(borrowLimit);
945
+
946
+ return isEligibleBasedOnLtv;
947
+ }
948
+
949
+ /*
950
+ Returns all elevation groups for a given obligation, except the default one
951
+ * @param market - The KaminoMarket instance.
952
+ * @returns An array of ElevationGroupDescription objects representing the elevation groups for the obligation.
953
+ */
954
+ getElevationGroupsForObligation(market: KaminoMarket): ElevationGroupDescription[] {
955
+ if (this.borrows.size > 1) {
956
+ return [];
957
+ }
958
+
959
+ const collReserves = Array.from(this.deposits.keys());
960
+ if (this.borrows.size === 0) {
961
+ return market.getElevationGroupsForReservesCombination(collReserves);
962
+ } else {
963
+ const debtReserve = Array.from(this.borrows.keys())[0];
964
+ return market.getElevationGroupsForReservesCombination(collReserves, debtReserve);
965
+ }
966
+ }
967
+
968
+ /* Deprecated function, also broken */
969
+ getMaxBorrowAmount(
970
+ market: KaminoMarket,
971
+ liquidityMint: PublicKey,
972
+ slot: number,
973
+ requestElevationGroup: boolean
974
+ ): Decimal {
975
+ const reserve = market.getReserveByMint(liquidityMint);
976
+
977
+ if (!reserve) {
978
+ throw new Error('Reserve not found');
979
+ }
980
+
981
+ const groups = market.state.elevationGroups;
982
+ const emodeGroupsDebtReserve = reserve.state.config.elevationGroups;
983
+ let commonElevationGroups = [...emodeGroupsDebtReserve].filter(
984
+ (item) => item !== 0 && groups[item - 1].debtReserve.equals(reserve.address)
985
+ );
986
+
987
+ for (const [_, value] of this.deposits.entries()) {
988
+ const depositReserve = market.getReserveByAddress(value.reserveAddress);
989
+
990
+ if (!depositReserve) {
991
+ throw new Error('Reserve not found');
992
+ }
993
+
994
+ const depositReserveEmodeGroups = depositReserve.state.config.elevationGroups;
995
+
996
+ commonElevationGroups = commonElevationGroups.filter((item) => depositReserveEmodeGroups.includes(item));
997
+ }
998
+
999
+ let elevationGroup = this.state.elevationGroup;
1000
+ if (commonElevationGroups.length != 0) {
1001
+ const eModeGroupWithMaxLtvAndDebtReserve = commonElevationGroups.reduce((prev, curr) => {
1002
+ const prevGroup = groups.find((group) => group.id === prev);
1003
+ const currGroup = groups.find((group) => group.id === curr);
1004
+ return prevGroup!.ltvPct > currGroup!.ltvPct ? prev : curr;
1005
+ });
1006
+
1007
+ if (requestElevationGroup) {
1008
+ elevationGroup = eModeGroupWithMaxLtvAndDebtReserve;
1009
+ }
1010
+ }
1011
+
1012
+ const elevationGroupActivated =
1013
+ reserve.state.config.elevationGroups.includes(elevationGroup) && elevationGroup !== 0;
1014
+
1015
+ const borrowFactor = KaminoObligation.getBorrowFactorForReserve(reserve, elevationGroup);
1016
+
1017
+ const maxObligationBorrowPower = this.refreshedStats.borrowLimit // adjusted available amount
1018
+ .minus(this.refreshedStats.userTotalBorrowBorrowFactorAdjusted)
1019
+ .div(borrowFactor)
1020
+ .div(reserve.getOracleMarketPrice())
1021
+ .mul(reserve.getMintFactor());
1022
+ const reserveAvailableAmount = reserve.getLiquidityAvailableAmount();
1023
+ let reserveBorrowCapRemained = reserve.stats.reserveBorrowLimit.sub(reserve.getBorrowedAmount());
1024
+
1025
+ this.deposits.forEach((deposit) => {
1026
+ const depositReserve = market.getReserveByAddress(deposit.reserveAddress);
1027
+ if (!depositReserve) {
1028
+ throw new Error('Reserve not found');
1029
+ }
1030
+ if (depositReserve.state.config.disableUsageAsCollOutsideEmode && !elevationGroupActivated) {
1031
+ reserveBorrowCapRemained = new Decimal(0);
1032
+ }
1033
+ });
1034
+
1035
+ let maxBorrowAmount = Decimal.min(maxObligationBorrowPower, reserveAvailableAmount, reserveBorrowCapRemained);
1036
+
1037
+ const debtWithdrawalCap = reserve.getDebtWithdrawalCapCapacity().sub(reserve.getDebtWithdrawalCapCurrent(slot));
1038
+ maxBorrowAmount = reserve.getDebtWithdrawalCapCapacity().gt(0)
1039
+ ? Decimal.min(maxBorrowAmount, debtWithdrawalCap)
1040
+ : maxBorrowAmount;
1041
+
1042
+ let originationFeeRate = reserve.getBorrowFee();
1043
+
1044
+ // Inclusive fee rate
1045
+ originationFeeRate = originationFeeRate.div(originationFeeRate.add(new Decimal(1)));
1046
+ const borrowFee = maxBorrowAmount.mul(originationFeeRate);
1047
+
1048
+ maxBorrowAmount = maxBorrowAmount.sub(borrowFee);
1049
+
1050
+ const utilizationRatioLimit = reserve.state.config.utilizationLimitBlockBorrowingAbove / 100;
1051
+ const currentUtilizationRatio = reserve.calculateUtilizationRatio();
1052
+
1053
+ if (utilizationRatioLimit > 0 && currentUtilizationRatio > utilizationRatioLimit) {
1054
+ return new Decimal(0);
1055
+ } else if (utilizationRatioLimit > 0 && currentUtilizationRatio < utilizationRatioLimit) {
1056
+ const maxBorrowBasedOnUtilization = new Decimal(utilizationRatioLimit - currentUtilizationRatio).mul(
1057
+ reserve.getTotalSupply()
1058
+ );
1059
+ maxBorrowAmount = Decimal.min(maxBorrowAmount, maxBorrowBasedOnUtilization);
1060
+ }
1061
+
1062
+ let borrowLimitDependentOnElevationGroup = new Decimal(U64_MAX);
1063
+
1064
+ if (!elevationGroupActivated) {
1065
+ borrowLimitDependentOnElevationGroup = reserve
1066
+ .getBorrowLimitOutsideElevationGroup()
1067
+ .sub(reserve.getBorrowedAmountOutsideElevationGroup());
1068
+ } else {
1069
+ let maxDebtTakenAgainstCollaterals = new Decimal(U64_MAX);
1070
+ for (const [_, value] of this.deposits.entries()) {
1071
+ const depositReserve = market.getReserveByAddress(value.reserveAddress);
1072
+
1073
+ if (!depositReserve) {
1074
+ throw new Error('Reserve not found');
1075
+ }
1076
+
1077
+ const maxDebtAllowedAgainstCollateral = depositReserve
1078
+ .getBorrowLimitAgainstCollateralInElevationGroup(elevationGroup - 1)
1079
+ .sub(depositReserve.getBorrowedAmountAgainstCollateralInElevationGroup(elevationGroup - 1));
1080
+
1081
+ maxDebtTakenAgainstCollaterals = Decimal.max(
1082
+ new Decimal(0),
1083
+ Decimal.min(maxDebtAllowedAgainstCollateral, maxDebtTakenAgainstCollaterals)
1084
+ );
1085
+ }
1086
+ borrowLimitDependentOnElevationGroup = maxDebtTakenAgainstCollaterals;
1087
+ }
1088
+
1089
+ maxBorrowAmount = Decimal.min(maxBorrowAmount, borrowLimitDependentOnElevationGroup);
1090
+
1091
+ return Decimal.max(new Decimal(0), maxBorrowAmount);
1092
+ }
1093
+
1094
+ getMaxWithdrawAmount(market: KaminoMarket, tokenMint: PublicKey, slot: number): Decimal {
1095
+ const depositReserve = market.getReserveByMint(tokenMint);
1096
+
1097
+ if (!depositReserve) {
1098
+ throw new Error('Reserve not found');
1099
+ }
1100
+
1101
+ const userDepositPosition = this.getDepositByReserve(depositReserve.address);
1102
+
1103
+ if (!userDepositPosition) {
1104
+ throw new Error('Deposit reserve not found');
1105
+ }
1106
+
1107
+ const userDepositPositionAmount = userDepositPosition.amount;
1108
+
1109
+ if (this.refreshedStats.userTotalBorrowBorrowFactorAdjusted.equals(new Decimal(0))) {
1110
+ return new Decimal(userDepositPositionAmount);
1111
+ }
1112
+
1113
+ const { maxLtv: reserveMaxLtv } = KaminoObligation.getLtvForReserve(
1114
+ market,
1115
+ depositReserve,
1116
+ this.state.elevationGroup
1117
+ );
1118
+ // bf adjusted debt value > allowed_borrow_value
1119
+ if (this.refreshedStats.userTotalBorrowBorrowFactorAdjusted.gte(this.refreshedStats.borrowLimit)) {
1120
+ return new Decimal(0);
1121
+ }
1122
+
1123
+ let maxWithdrawValue: Decimal;
1124
+ if (reserveMaxLtv.eq(0)) {
1125
+ maxWithdrawValue = userDepositPositionAmount;
1126
+ } else {
1127
+ // borrowLimit / userTotalDeposit = maxLtv
1128
+ // maxWithdrawValue = userTotalDeposit - userTotalBorrow / maxLtv
1129
+ maxWithdrawValue = this.refreshedStats.borrowLimit
1130
+ .sub(this.refreshedStats.userTotalBorrowBorrowFactorAdjusted)
1131
+ .div(reserveMaxLtv)
1132
+ .mul(0.999); // remove 0.1% to prevent going over max ltv
1133
+ }
1134
+
1135
+ const maxWithdrawAmount = maxWithdrawValue
1136
+ .div(depositReserve.getOracleMarketPrice())
1137
+ .mul(depositReserve.getMintFactor());
1138
+ const reserveAvailableLiquidity = depositReserve.getLiquidityAvailableAmount();
1139
+
1140
+ const withdrawalCapRemained = depositReserve
1141
+ .getDepositWithdrawalCapCapacity()
1142
+ .sub(depositReserve.getDepositWithdrawalCapCurrent(slot));
1143
+ return Decimal.max(
1144
+ 0,
1145
+ Decimal.min(userDepositPositionAmount, maxWithdrawAmount, reserveAvailableLiquidity, withdrawalCapRemained)
1146
+ );
1147
+ }
1148
+
1149
+ /**
1150
+ *
1151
+ * @returns Total borrowed amount for the specified obligation liquidity/borrow asset
1152
+ */
1153
+ static getBorrowAmount(borrow: ObligationLiquidity): Decimal {
1154
+ return new Fraction(borrow.borrowedAmountSf).toDecimal();
1155
+ }
1156
+
1157
+ /**
1158
+ *
1159
+ * @returns Cumulative borrow rate for the specified obligation liquidity/borrow asset
1160
+ */
1161
+ static getCumulativeBorrowRate(borrow: ObligationLiquidity): Decimal {
1162
+ let accSf = new BN(0);
1163
+ for (const value of borrow.cumulativeBorrowRateBsf.value.reverse()) {
1164
+ accSf = accSf.add(value);
1165
+ accSf.shrn(64);
1166
+ }
1167
+ return new Fraction(accSf).toDecimal();
1168
+ }
1169
+
1170
+ public static getRatesForObligation(
1171
+ kaminoMarket: KaminoMarket,
1172
+ obligation: Obligation,
1173
+ slot: number
1174
+ ): {
1175
+ collateralExchangeRates: Map<PublicKey, Decimal>;
1176
+ cumulativeBorrowRates: Map<PublicKey, Decimal>;
1177
+ } {
1178
+ const collateralExchangeRates = KaminoObligation.getCollateralExchangeRatesForObligation(
1179
+ kaminoMarket,
1180
+ obligation,
1181
+ slot
1182
+ );
1183
+ const cumulativeBorrowRates = KaminoObligation.getCumulativeBorrowRatesForObligation(
1184
+ kaminoMarket,
1185
+ obligation,
1186
+ slot
1187
+ );
1188
+
1189
+ return {
1190
+ collateralExchangeRates,
1191
+ cumulativeBorrowRates,
1192
+ };
1193
+ }
1194
+
1195
+ public static addRatesForObligation(
1196
+ kaminoMarket: KaminoMarket,
1197
+ obligation: Obligation,
1198
+ collateralExchangeRates: Map<PublicKey, Decimal>,
1199
+ cumulativeBorrowRates: Map<PublicKey, Decimal>,
1200
+ slot: number
1201
+ ): void {
1202
+ KaminoObligation.addCollateralExchangeRatesForObligation(kaminoMarket, collateralExchangeRates, obligation, slot);
1203
+ KaminoObligation.addCumulativeBorrowRatesForObligation(kaminoMarket, cumulativeBorrowRates, obligation, slot);
1204
+ }
1205
+
1206
+ static getCollateralExchangeRatesForObligation(
1207
+ kaminoMarket: KaminoMarket,
1208
+ obligation: Obligation,
1209
+ slot: number
1210
+ ): Map<PublicKey, Decimal> {
1211
+ const collateralExchangeRates = new PubkeyHashMap<PublicKey, Decimal>();
1212
+ for (let i = 0; i < obligation.deposits.length; i++) {
1213
+ const deposit = obligation.deposits[i];
1214
+ if (isNotNullPubkey(deposit.depositReserve) && !collateralExchangeRates.has(deposit.depositReserve)) {
1215
+ const reserve = kaminoMarket.getReserveByAddress(deposit.depositReserve)!;
1216
+ const collateralExchangeRate = reserve.getEstimatedCollateralExchangeRate(
1217
+ slot,
1218
+ kaminoMarket.state.referralFeeBps
1219
+ );
1220
+ collateralExchangeRates.set(reserve.address, collateralExchangeRate);
1221
+ }
1222
+ }
1223
+ return collateralExchangeRates;
1224
+ }
1225
+
1226
+ static addCollateralExchangeRatesForObligation(
1227
+ kaminoMarket: KaminoMarket,
1228
+ collateralExchangeRates: Map<PublicKey, Decimal>,
1229
+ obligation: Obligation,
1230
+ slot: number
1231
+ ) {
1232
+ for (let i = 0; i < obligation.deposits.length; i++) {
1233
+ const deposit = obligation.deposits[i];
1234
+ if (isNotNullPubkey(deposit.depositReserve) && !collateralExchangeRates.has(deposit.depositReserve)) {
1235
+ const reserve = kaminoMarket.getReserveByAddress(deposit.depositReserve)!;
1236
+ const collateralExchangeRate = reserve.getEstimatedCollateralExchangeRate(
1237
+ slot,
1238
+ kaminoMarket.state.referralFeeBps
1239
+ );
1240
+ collateralExchangeRates.set(reserve.address, collateralExchangeRate);
1241
+ }
1242
+ }
1243
+ }
1244
+
1245
+ static getCumulativeBorrowRatesForObligation(kaminoMarket: KaminoMarket, obligation: Obligation, slot: number) {
1246
+ const cumulativeBorrowRates = new PubkeyHashMap<PublicKey, Decimal>();
1247
+ for (let i = 0; i < obligation.borrows.length; i++) {
1248
+ const borrow = obligation.borrows[i];
1249
+ if (isNotNullPubkey(borrow.borrowReserve) && !cumulativeBorrowRates.has(borrow.borrowReserve)) {
1250
+ const reserve = kaminoMarket.getReserveByAddress(borrow.borrowReserve)!;
1251
+ const cumulativeBorrowRate = reserve.getEstimatedCumulativeBorrowRate(slot, kaminoMarket.state.referralFeeBps);
1252
+ cumulativeBorrowRates.set(reserve.address, cumulativeBorrowRate);
1253
+ }
1254
+ }
1255
+ return cumulativeBorrowRates;
1256
+ }
1257
+
1258
+ static addCumulativeBorrowRatesForObligation(
1259
+ kaminoMarket: KaminoMarket,
1260
+ cumulativeBorrowRates: Map<PublicKey, Decimal>,
1261
+ obligation: Obligation,
1262
+ slot: number
1263
+ ) {
1264
+ for (let i = 0; i < obligation.borrows.length; i++) {
1265
+ const borrow = obligation.borrows[i];
1266
+ if (isNotNullPubkey(borrow.borrowReserve) && !cumulativeBorrowRates.has(borrow.borrowReserve)) {
1267
+ const reserve = kaminoMarket.getReserveByAddress(borrow.borrowReserve)!;
1268
+ const cumulativeBorrowRate = reserve.getEstimatedCumulativeBorrowRate(slot, kaminoMarket.state.referralFeeBps);
1269
+ cumulativeBorrowRates.set(reserve.address, cumulativeBorrowRate);
1270
+ }
1271
+ }
1272
+ }
1273
+
1274
+ /**
1275
+ * Get the borrow factor for a borrow reserve, accounting for the obligation elevation group if it is active
1276
+ * @param reserve
1277
+ * @param elevationGroup
1278
+ */
1279
+ public static getBorrowFactorForReserve(reserve: KaminoReserve, elevationGroup: number): Decimal {
1280
+ const elevationGroupActivated =
1281
+ reserve.state.config.elevationGroups.includes(elevationGroup) && elevationGroup !== 0;
1282
+ if (elevationGroupActivated) {
1283
+ return new Decimal('1');
1284
+ }
1285
+ return new Decimal(reserve.stats.borrowFactor).div('100');
1286
+ }
1287
+
1288
+ /**
1289
+ * Get the loan to value and liquidation loan to value for a collateral reserve as ratios, accounting for the obligation elevation group if it is active
1290
+ * @param market
1291
+ * @param reserve
1292
+ * @param elevationGroup
1293
+ */
1294
+ public static getLtvForReserve(
1295
+ market: KaminoMarket,
1296
+ reserve: KaminoReserve,
1297
+ elevationGroup: number
1298
+ ): { maxLtv: Decimal; liquidationLtv: Decimal } {
1299
+ const elevationGroupActivated =
1300
+ elevationGroup !== 0 && reserve.state.config.elevationGroups.includes(elevationGroup);
1301
+ if (elevationGroupActivated) {
1302
+ const { ltvPct, liquidationThresholdPct } = market.getElevationGroup(elevationGroup);
1303
+ return {
1304
+ maxLtv: new Decimal(ltvPct).div('100'),
1305
+ liquidationLtv: new Decimal(liquidationThresholdPct).div('100'),
1306
+ };
1307
+ } else {
1308
+ const { loanToValue, liquidationThreshold } = reserve.stats;
1309
+ return {
1310
+ maxLtv: new Decimal(loanToValue),
1311
+ liquidationLtv: new Decimal(liquidationThreshold),
1312
+ };
1313
+ }
1314
+ }
1315
+ }