@kamino-finance/klend-sdk 2.10.6

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 (369) hide show
  1. package/README.md +85 -0
  2. package/dist/classes/action.d.ts +154 -0
  3. package/dist/classes/action.js +1505 -0
  4. package/dist/classes/action.js.map +1 -0
  5. package/dist/classes/curve.d.ts +15 -0
  6. package/dist/classes/curve.js +32 -0
  7. package/dist/classes/curve.js.map +1 -0
  8. package/dist/classes/fraction.d.ts +23 -0
  9. package/dist/classes/fraction.js +60 -0
  10. package/dist/classes/fraction.js.map +1 -0
  11. package/dist/classes/index.d.ts +8 -0
  12. package/dist/classes/index.js +25 -0
  13. package/dist/classes/index.js.map +1 -0
  14. package/dist/classes/jupiterPerps.d.ts +2 -0
  15. package/dist/classes/jupiterPerps.js +25 -0
  16. package/dist/classes/jupiterPerps.js.map +1 -0
  17. package/dist/classes/market.d.ts +140 -0
  18. package/dist/classes/market.js +901 -0
  19. package/dist/classes/market.js.map +1 -0
  20. package/dist/classes/obligation.d.ts +170 -0
  21. package/dist/classes/obligation.js +610 -0
  22. package/dist/classes/obligation.js.map +1 -0
  23. package/dist/classes/reserve.d.ts +161 -0
  24. package/dist/classes/reserve.js +474 -0
  25. package/dist/classes/reserve.js.map +1 -0
  26. package/dist/classes/shared.d.ts +41 -0
  27. package/dist/classes/shared.js +10 -0
  28. package/dist/classes/shared.js.map +1 -0
  29. package/dist/classes/utils.d.ts +40 -0
  30. package/dist/classes/utils.js +129 -0
  31. package/dist/classes/utils.js.map +1 -0
  32. package/dist/idl.json +4102 -0
  33. package/dist/idl_codegen/accounts/LendingMarket.d.ts +153 -0
  34. package/dist/idl_codegen/accounts/LendingMarket.js +204 -0
  35. package/dist/idl_codegen/accounts/LendingMarket.js.map +1 -0
  36. package/dist/idl_codegen/accounts/Obligation.d.ts +139 -0
  37. package/dist/idl_codegen/accounts/Obligation.js +193 -0
  38. package/dist/idl_codegen/accounts/Obligation.js.map +1 -0
  39. package/dist/idl_codegen/accounts/ReferrerState.d.ts +22 -0
  40. package/dist/idl_codegen/accounts/ReferrerState.js +101 -0
  41. package/dist/idl_codegen/accounts/ReferrerState.js.map +1 -0
  42. package/dist/idl_codegen/accounts/ReferrerTokenState.d.ts +51 -0
  43. package/dist/idl_codegen/accounts/ReferrerTokenState.js +126 -0
  44. package/dist/idl_codegen/accounts/ReferrerTokenState.js.map +1 -0
  45. package/dist/idl_codegen/accounts/Reserve.d.ts +72 -0
  46. package/dist/idl_codegen/accounts/Reserve.js +156 -0
  47. package/dist/idl_codegen/accounts/Reserve.js.map +1 -0
  48. package/dist/idl_codegen/accounts/ShortUrl.d.ts +22 -0
  49. package/dist/idl_codegen/accounts/ShortUrl.js +101 -0
  50. package/dist/idl_codegen/accounts/ShortUrl.js.map +1 -0
  51. package/dist/idl_codegen/accounts/UserMetadata.d.ts +48 -0
  52. package/dist/idl_codegen/accounts/UserMetadata.js +126 -0
  53. package/dist/idl_codegen/accounts/UserMetadata.js.map +1 -0
  54. package/dist/idl_codegen/accounts/UserState.d.ts +143 -0
  55. package/dist/idl_codegen/accounts/UserState.js +180 -0
  56. package/dist/idl_codegen/accounts/UserState.js.map +1 -0
  57. package/dist/idl_codegen/accounts/index.d.ts +16 -0
  58. package/dist/idl_codegen/accounts/index.js +20 -0
  59. package/dist/idl_codegen/accounts/index.js.map +1 -0
  60. package/dist/idl_codegen/errors/anchor.d.ts +434 -0
  61. package/dist/idl_codegen/errors/anchor.js +713 -0
  62. package/dist/idl_codegen/errors/anchor.js.map +1 -0
  63. package/dist/idl_codegen/errors/custom.d.ts +730 -0
  64. package/dist/idl_codegen/errors/custom.js +1194 -0
  65. package/dist/idl_codegen/errors/custom.js.map +1 -0
  66. package/dist/idl_codegen/errors/index.d.ts +5 -0
  67. package/dist/idl_codegen/errors/index.js +71 -0
  68. package/dist/idl_codegen/errors/index.js.map +1 -0
  69. package/dist/idl_codegen/instructions/borrowObligationLiquidity.d.ts +20 -0
  70. package/dist/idl_codegen/instructions/borrowObligationLiquidity.js +75 -0
  71. package/dist/idl_codegen/instructions/borrowObligationLiquidity.js.map +1 -0
  72. package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.d.ts +9 -0
  73. package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.js +20 -0
  74. package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.js.map +1 -0
  75. package/dist/idl_codegen/instructions/depositObligationCollateral.d.ts +17 -0
  76. package/dist/idl_codegen/instructions/depositObligationCollateral.js +64 -0
  77. package/dist/idl_codegen/instructions/depositObligationCollateral.js.map +1 -0
  78. package/dist/idl_codegen/instructions/depositReserveLiquidity.d.ts +19 -0
  79. package/dist/idl_codegen/instructions/depositReserveLiquidity.js +74 -0
  80. package/dist/idl_codegen/instructions/depositReserveLiquidity.js.map +1 -0
  81. package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.d.ts +21 -0
  82. package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.js +80 -0
  83. package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.js.map +1 -0
  84. package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.d.ts +20 -0
  85. package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.js +75 -0
  86. package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.js.map +1 -0
  87. package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.d.ts +21 -0
  88. package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.js +75 -0
  89. package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.js.map +1 -0
  90. package/dist/idl_codegen/instructions/idlMissingTypes.d.ts +18 -0
  91. package/dist/idl_codegen/instructions/idlMissingTypes.js +62 -0
  92. package/dist/idl_codegen/instructions/idlMissingTypes.js.map +1 -0
  93. package/dist/idl_codegen/instructions/index.d.ts +66 -0
  94. package/dist/idl_codegen/instructions/index.js +70 -0
  95. package/dist/idl_codegen/instructions/index.js.map +1 -0
  96. package/dist/idl_codegen/instructions/initFarmsForReserve.d.ts +19 -0
  97. package/dist/idl_codegen/instructions/initFarmsForReserve.js +63 -0
  98. package/dist/idl_codegen/instructions/initFarmsForReserve.js.map +1 -0
  99. package/dist/idl_codegen/instructions/initLendingMarket.d.ts +13 -0
  100. package/dist/idl_codegen/instructions/initLendingMarket.js +55 -0
  101. package/dist/idl_codegen/instructions/initLendingMarket.js.map +1 -0
  102. package/dist/idl_codegen/instructions/initObligation.d.ts +19 -0
  103. package/dist/idl_codegen/instructions/initObligation.js +55 -0
  104. package/dist/idl_codegen/instructions/initObligation.js.map +1 -0
  105. package/dist/idl_codegen/instructions/initObligationFarmsForReserve.d.ts +20 -0
  106. package/dist/idl_codegen/instructions/initObligationFarmsForReserve.js +60 -0
  107. package/dist/idl_codegen/instructions/initObligationFarmsForReserve.js.map +1 -0
  108. package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.d.ts +14 -0
  109. package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.js +54 -0
  110. package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.js.map +1 -0
  111. package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts +14 -0
  112. package/dist/idl_codegen/instructions/initReferrerTokenState.js +50 -0
  113. package/dist/idl_codegen/instructions/initReferrerTokenState.js.map +1 -0
  114. package/dist/idl_codegen/instructions/initReserve.d.ts +16 -0
  115. package/dist/idl_codegen/instructions/initReserve.js +47 -0
  116. package/dist/idl_codegen/instructions/initReserve.js.map +1 -0
  117. package/dist/idl_codegen/instructions/initUserMetadata.d.ts +14 -0
  118. package/dist/idl_codegen/instructions/initUserMetadata.js +54 -0
  119. package/dist/idl_codegen/instructions/initUserMetadata.js.map +1 -0
  120. package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.d.ts +27 -0
  121. package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.js +102 -0
  122. package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.js.map +1 -0
  123. package/dist/idl_codegen/instructions/redeemFees.d.ts +10 -0
  124. package/dist/idl_codegen/instructions/redeemFees.js +33 -0
  125. package/dist/idl_codegen/instructions/redeemFees.js.map +1 -0
  126. package/dist/idl_codegen/instructions/redeemReserveCollateral.d.ts +19 -0
  127. package/dist/idl_codegen/instructions/redeemReserveCollateral.js +78 -0
  128. package/dist/idl_codegen/instructions/redeemReserveCollateral.js.map +1 -0
  129. package/dist/idl_codegen/instructions/refreshObligation.d.ts +6 -0
  130. package/dist/idl_codegen/instructions/refreshObligation.js +17 -0
  131. package/dist/idl_codegen/instructions/refreshObligation.js.map +1 -0
  132. package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.d.ts +19 -0
  133. package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.js +63 -0
  134. package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.js.map +1 -0
  135. package/dist/idl_codegen/instructions/refreshReserve.d.ts +10 -0
  136. package/dist/idl_codegen/instructions/refreshReserve.js +29 -0
  137. package/dist/idl_codegen/instructions/refreshReserve.js.map +1 -0
  138. package/dist/idl_codegen/instructions/repayObligationLiquidity.d.ts +17 -0
  139. package/dist/idl_codegen/instructions/repayObligationLiquidity.js +60 -0
  140. package/dist/idl_codegen/instructions/repayObligationLiquidity.js.map +1 -0
  141. package/dist/idl_codegen/instructions/requestElevationGroup.d.ts +11 -0
  142. package/dist/idl_codegen/instructions/requestElevationGroup.js +47 -0
  143. package/dist/idl_codegen/instructions/requestElevationGroup.js.map +1 -0
  144. package/dist/idl_codegen/instructions/socializeLoss.d.ts +14 -0
  145. package/dist/idl_codegen/instructions/socializeLoss.js +53 -0
  146. package/dist/idl_codegen/instructions/socializeLoss.js.map +1 -0
  147. package/dist/idl_codegen/instructions/updateEntireReserveConfig.d.ts +13 -0
  148. package/dist/idl_codegen/instructions/updateEntireReserveConfig.js +51 -0
  149. package/dist/idl_codegen/instructions/updateEntireReserveConfig.js.map +1 -0
  150. package/dist/idl_codegen/instructions/updateLendingMarket.d.ts +12 -0
  151. package/dist/idl_codegen/instructions/updateLendingMarket.js +50 -0
  152. package/dist/idl_codegen/instructions/updateLendingMarket.js.map +1 -0
  153. package/dist/idl_codegen/instructions/updateLendingMarketOwner.d.ts +6 -0
  154. package/dist/idl_codegen/instructions/updateLendingMarketOwner.js +21 -0
  155. package/dist/idl_codegen/instructions/updateLendingMarketOwner.js.map +1 -0
  156. package/dist/idl_codegen/instructions/updateReserveConfig.d.ts +13 -0
  157. package/dist/idl_codegen/instructions/updateReserveConfig.js +51 -0
  158. package/dist/idl_codegen/instructions/updateReserveConfig.js.map +1 -0
  159. package/dist/idl_codegen/instructions/updateSingleReserveConfig.d.ts +13 -0
  160. package/dist/idl_codegen/instructions/updateSingleReserveConfig.js +51 -0
  161. package/dist/idl_codegen/instructions/updateSingleReserveConfig.js.map +1 -0
  162. package/dist/idl_codegen/instructions/withdrawObligationCollateral.d.ts +18 -0
  163. package/dist/idl_codegen/instructions/withdrawObligationCollateral.js +69 -0
  164. package/dist/idl_codegen/instructions/withdrawObligationCollateral.js.map +1 -0
  165. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.d.ts +21 -0
  166. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js +84 -0
  167. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js.map +1 -0
  168. package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts +16 -0
  169. package/dist/idl_codegen/instructions/withdrawProtocolFee.js +59 -0
  170. package/dist/idl_codegen/instructions/withdrawProtocolFee.js.map +1 -0
  171. package/dist/idl_codegen/instructions/withdrawReferrerFees.d.ts +12 -0
  172. package/dist/idl_codegen/instructions/withdrawReferrerFees.js +35 -0
  173. package/dist/idl_codegen/instructions/withdrawReferrerFees.js.map +1 -0
  174. package/dist/idl_codegen/programId.d.ts +4 -0
  175. package/dist/idl_codegen/programId.js +13 -0
  176. package/dist/idl_codegen/programId.js.map +1 -0
  177. package/dist/idl_codegen/types/AssetTier.d.ts +44 -0
  178. package/dist/idl_codegen/types/AssetTier.js +128 -0
  179. package/dist/idl_codegen/types/AssetTier.js.map +1 -0
  180. package/dist/idl_codegen/types/BigFractionBytes.d.ts +27 -0
  181. package/dist/idl_codegen/types/BigFractionBytes.js +73 -0
  182. package/dist/idl_codegen/types/BigFractionBytes.js.map +1 -0
  183. package/dist/idl_codegen/types/BorrowRateCurve.d.ts +27 -0
  184. package/dist/idl_codegen/types/BorrowRateCurve.js +62 -0
  185. package/dist/idl_codegen/types/BorrowRateCurve.js.map +1 -0
  186. package/dist/idl_codegen/types/CurvePoint.d.ts +26 -0
  187. package/dist/idl_codegen/types/CurvePoint.js +66 -0
  188. package/dist/idl_codegen/types/CurvePoint.js.map +1 -0
  189. package/dist/idl_codegen/types/ElevationGroup.d.ts +52 -0
  190. package/dist/idl_codegen/types/ElevationGroup.js +103 -0
  191. package/dist/idl_codegen/types/ElevationGroup.js.map +1 -0
  192. package/dist/idl_codegen/types/FeeCalculation.d.ts +31 -0
  193. package/dist/idl_codegen/types/FeeCalculation.js +102 -0
  194. package/dist/idl_codegen/types/FeeCalculation.js.map +1 -0
  195. package/dist/idl_codegen/types/InitObligationArgs.d.ts +26 -0
  196. package/dist/idl_codegen/types/InitObligationArgs.js +66 -0
  197. package/dist/idl_codegen/types/InitObligationArgs.js.map +1 -0
  198. package/dist/idl_codegen/types/LastUpdate.d.ts +47 -0
  199. package/dist/idl_codegen/types/LastUpdate.js +86 -0
  200. package/dist/idl_codegen/types/LastUpdate.js.map +1 -0
  201. package/dist/idl_codegen/types/ObligationCollateral.d.ts +48 -0
  202. package/dist/idl_codegen/types/ObligationCollateral.js +87 -0
  203. package/dist/idl_codegen/types/ObligationCollateral.js.map +1 -0
  204. package/dist/idl_codegen/types/ObligationLiquidity.d.ts +75 -0
  205. package/dist/idl_codegen/types/ObligationLiquidity.js +107 -0
  206. package/dist/idl_codegen/types/ObligationLiquidity.js.map +1 -0
  207. package/dist/idl_codegen/types/PriceHeuristic.d.ts +41 -0
  208. package/dist/idl_codegen/types/PriceHeuristic.js +75 -0
  209. package/dist/idl_codegen/types/PriceHeuristic.js.map +1 -0
  210. package/dist/idl_codegen/types/PythConfiguration.d.ts +25 -0
  211. package/dist/idl_codegen/types/PythConfiguration.js +62 -0
  212. package/dist/idl_codegen/types/PythConfiguration.js.map +1 -0
  213. package/dist/idl_codegen/types/ReserveCollateral.d.ts +53 -0
  214. package/dist/idl_codegen/types/ReserveCollateral.js +93 -0
  215. package/dist/idl_codegen/types/ReserveCollateral.js.map +1 -0
  216. package/dist/idl_codegen/types/ReserveConfig.d.ts +304 -0
  217. package/dist/idl_codegen/types/ReserveConfig.js +209 -0
  218. package/dist/idl_codegen/types/ReserveConfig.js.map +1 -0
  219. package/dist/idl_codegen/types/ReserveFarmKind.d.ts +31 -0
  220. package/dist/idl_codegen/types/ReserveFarmKind.js +102 -0
  221. package/dist/idl_codegen/types/ReserveFarmKind.js.map +1 -0
  222. package/dist/idl_codegen/types/ReserveFees.d.ts +78 -0
  223. package/dist/idl_codegen/types/ReserveFees.js +86 -0
  224. package/dist/idl_codegen/types/ReserveFees.js.map +1 -0
  225. package/dist/idl_codegen/types/ReserveLiquidity.d.ts +173 -0
  226. package/dist/idl_codegen/types/ReserveLiquidity.js +166 -0
  227. package/dist/idl_codegen/types/ReserveLiquidity.js.map +1 -0
  228. package/dist/idl_codegen/types/ReserveStatus.d.ts +44 -0
  229. package/dist/idl_codegen/types/ReserveStatus.js +128 -0
  230. package/dist/idl_codegen/types/ReserveStatus.js.map +1 -0
  231. package/dist/idl_codegen/types/ScopeConfiguration.d.ts +41 -0
  232. package/dist/idl_codegen/types/ScopeConfiguration.js +76 -0
  233. package/dist/idl_codegen/types/ScopeConfiguration.js.map +1 -0
  234. package/dist/idl_codegen/types/SwitchboardConfiguration.d.ts +30 -0
  235. package/dist/idl_codegen/types/SwitchboardConfiguration.js +67 -0
  236. package/dist/idl_codegen/types/SwitchboardConfiguration.js.map +1 -0
  237. package/dist/idl_codegen/types/TokenInfo.d.ts +109 -0
  238. package/dist/idl_codegen/types/TokenInfo.js +116 -0
  239. package/dist/idl_codegen/types/TokenInfo.js.map +1 -0
  240. package/dist/idl_codegen/types/UpdateConfigMode.d.ts +538 -0
  241. package/dist/idl_codegen/types/UpdateConfigMode.js +1116 -0
  242. package/dist/idl_codegen/types/UpdateConfigMode.js.map +1 -0
  243. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts +155 -0
  244. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js +273 -0
  245. package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js.map +1 -0
  246. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts +200 -0
  247. package/dist/idl_codegen/types/UpdateLendingMarketMode.js +440 -0
  248. package/dist/idl_codegen/types/UpdateLendingMarketMode.js.map +1 -0
  249. package/dist/idl_codegen/types/WithdrawalCaps.d.ts +38 -0
  250. package/dist/idl_codegen/types/WithdrawalCaps.js +86 -0
  251. package/dist/idl_codegen/types/WithdrawalCaps.js.map +1 -0
  252. package/dist/idl_codegen/types/index.d.ts +65 -0
  253. package/dist/idl_codegen/types/index.js +77 -0
  254. package/dist/idl_codegen/types/index.js.map +1 -0
  255. package/dist/idl_codegen/zero_padding/ObligationZP.d.ts +53 -0
  256. package/dist/idl_codegen/zero_padding/ObligationZP.js +140 -0
  257. package/dist/idl_codegen/zero_padding/ObligationZP.js.map +1 -0
  258. package/dist/idl_codegen/zero_padding/index.d.ts +1 -0
  259. package/dist/idl_codegen/zero_padding/index.js +6 -0
  260. package/dist/idl_codegen/zero_padding/index.js.map +1 -0
  261. package/dist/idl_codegen_jupiter_perps/accounts/Pool.d.ts +48 -0
  262. package/dist/idl_codegen_jupiter_perps/accounts/Pool.js +144 -0
  263. package/dist/idl_codegen_jupiter_perps/accounts/Pool.js.map +1 -0
  264. package/dist/idl_codegen_jupiter_perps/accounts/index.d.ts +2 -0
  265. package/dist/idl_codegen_jupiter_perps/accounts/index.js +6 -0
  266. package/dist/idl_codegen_jupiter_perps/accounts/index.js.map +1 -0
  267. package/dist/idl_codegen_jupiter_perps/programId.d.ts +3 -0
  268. package/dist/idl_codegen_jupiter_perps/programId.js +9 -0
  269. package/dist/idl_codegen_jupiter_perps/programId.js.map +1 -0
  270. package/dist/idl_codegen_jupiter_perps/types/Fees.d.ts +62 -0
  271. package/dist/idl_codegen_jupiter_perps/types/Fees.js +115 -0
  272. package/dist/idl_codegen_jupiter_perps/types/Fees.js.map +1 -0
  273. package/dist/idl_codegen_jupiter_perps/types/Limit.d.ts +32 -0
  274. package/dist/idl_codegen_jupiter_perps/types/Limit.js +75 -0
  275. package/dist/idl_codegen_jupiter_perps/types/Limit.js.map +1 -0
  276. package/dist/idl_codegen_jupiter_perps/types/PoolApr.d.ts +32 -0
  277. package/dist/idl_codegen_jupiter_perps/types/PoolApr.js +75 -0
  278. package/dist/idl_codegen_jupiter_perps/types/PoolApr.js.map +1 -0
  279. package/dist/idl_codegen_jupiter_perps/types/index.d.ts +6 -0
  280. package/dist/idl_codegen_jupiter_perps/types/index.js +10 -0
  281. package/dist/idl_codegen_jupiter_perps/types/index.js.map +1 -0
  282. package/dist/index.d.ts +1 -0
  283. package/dist/index.js +18 -0
  284. package/dist/index.js.map +1 -0
  285. package/dist/lending_operations/index.d.ts +2 -0
  286. package/dist/lending_operations/index.js +19 -0
  287. package/dist/lending_operations/index.js.map +1 -0
  288. package/dist/lending_operations/repay_with_collateral_calcs.d.ts +5 -0
  289. package/dist/lending_operations/repay_with_collateral_calcs.js +26 -0
  290. package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -0
  291. package/dist/lending_operations/repay_with_collateral_operations.d.ts +47 -0
  292. package/dist/lending_operations/repay_with_collateral_operations.js +163 -0
  293. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -0
  294. package/dist/leverage/calcs.d.ts +132 -0
  295. package/dist/leverage/calcs.js +291 -0
  296. package/dist/leverage/calcs.js.map +1 -0
  297. package/dist/leverage/index.d.ts +4 -0
  298. package/dist/leverage/index.js +21 -0
  299. package/dist/leverage/index.js.map +1 -0
  300. package/dist/leverage/instructions.d.ts +43 -0
  301. package/dist/leverage/instructions.js +103 -0
  302. package/dist/leverage/instructions.js.map +1 -0
  303. package/dist/leverage/operations.d.ts +231 -0
  304. package/dist/leverage/operations.js +1058 -0
  305. package/dist/leverage/operations.js.map +1 -0
  306. package/dist/leverage/utils.d.ts +22 -0
  307. package/dist/leverage/utils.js +144 -0
  308. package/dist/leverage/utils.js.map +1 -0
  309. package/dist/lib.d.ts +9 -0
  310. package/dist/lib.js +26 -0
  311. package/dist/lib.js.map +1 -0
  312. package/dist/referrals/index.d.ts +2 -0
  313. package/dist/referrals/index.js +19 -0
  314. package/dist/referrals/index.js.map +1 -0
  315. package/dist/referrals/instructions.d.ts +16 -0
  316. package/dist/referrals/instructions.js +76 -0
  317. package/dist/referrals/instructions.js.map +1 -0
  318. package/dist/referrals/operations.d.ts +97 -0
  319. package/dist/referrals/operations.js +258 -0
  320. package/dist/referrals/operations.js.map +1 -0
  321. package/dist/utils/ObligationType.d.ts +49 -0
  322. package/dist/utils/ObligationType.js +129 -0
  323. package/dist/utils/ObligationType.js.map +1 -0
  324. package/dist/utils/api.d.ts +6 -0
  325. package/dist/utils/api.js +61 -0
  326. package/dist/utils/api.js.map +1 -0
  327. package/dist/utils/ata.d.ts +25 -0
  328. package/dist/utils/ata.js +155 -0
  329. package/dist/utils/ata.js.map +1 -0
  330. package/dist/utils/constants.d.ts +48 -0
  331. package/dist/utils/constants.js +75 -0
  332. package/dist/utils/constants.js.map +1 -0
  333. package/dist/utils/idl.d.ts +2 -0
  334. package/dist/utils/idl.js +9 -0
  335. package/dist/utils/idl.js.map +1 -0
  336. package/dist/utils/index.d.ts +14 -0
  337. package/dist/utils/index.js +31 -0
  338. package/dist/utils/index.js.map +1 -0
  339. package/dist/utils/instruction.d.ts +22 -0
  340. package/dist/utils/instruction.js +202 -0
  341. package/dist/utils/instruction.js.map +1 -0
  342. package/dist/utils/kamino.d.ts +3 -0
  343. package/dist/utils/kamino.js +45 -0
  344. package/dist/utils/kamino.js.map +1 -0
  345. package/dist/utils/layout.d.ts +13 -0
  346. package/dist/utils/layout.js +123 -0
  347. package/dist/utils/layout.js.map +1 -0
  348. package/dist/utils/lookupTable.d.ts +5 -0
  349. package/dist/utils/lookupTable.js +57 -0
  350. package/dist/utils/lookupTable.js.map +1 -0
  351. package/dist/utils/oracle.d.ts +51 -0
  352. package/dist/utils/oracle.js +295 -0
  353. package/dist/utils/oracle.js.map +1 -0
  354. package/dist/utils/pubkey.d.ts +74 -0
  355. package/dist/utils/pubkey.js +229 -0
  356. package/dist/utils/pubkey.js.map +1 -0
  357. package/dist/utils/seeds.d.ts +122 -0
  358. package/dist/utils/seeds.js +153 -0
  359. package/dist/utils/seeds.js.map +1 -0
  360. package/dist/utils/slots.d.ts +18 -0
  361. package/dist/utils/slots.js +32 -0
  362. package/dist/utils/slots.js.map +1 -0
  363. package/dist/utils/syncNative.d.ts +10 -0
  364. package/dist/utils/syncNative.js +46 -0
  365. package/dist/utils/syncNative.js.map +1 -0
  366. package/dist/utils/userMetadata.d.ts +15 -0
  367. package/dist/utils/userMetadata.js +259 -0
  368. package/dist/utils/userMetadata.js.map +1 -0
  369. package/package.json +94 -0
@@ -0,0 +1,610 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ var __importDefault = (this && this.__importDefault) || function (mod) {
12
+ return (mod && mod.__esModule) ? mod : { "default": mod };
13
+ };
14
+ Object.defineProperty(exports, "__esModule", { value: true });
15
+ exports.KaminoObligation = void 0;
16
+ const decimal_js_1 = __importDefault(require("decimal.js"));
17
+ const accounts_1 = require("../idl_codegen/accounts");
18
+ const bn_js_1 = __importDefault(require("bn.js"));
19
+ const fraction_1 = require("./fraction");
20
+ const utils_1 = require("./utils");
21
+ const utils_2 = require("../utils");
22
+ class KaminoObligation {
23
+ /**
24
+ * Initialise a new Obligation from the deserialized state
25
+ * @param market
26
+ * @param obligationAddress
27
+ * @param obligation
28
+ * @param collateralExchangeRates - rates from the market by reserve address, will be calculated if not provided
29
+ * @param cumulativeBorrowRates - rates from the market by reserve address, will be calculated if not provided
30
+ */
31
+ constructor(market, obligationAddress, obligation, collateralExchangeRates, cumulativeBorrowRates) {
32
+ this.estimateObligationInterestRate = (reserve, borrow, currentSlot) => {
33
+ const estimatedCumulativeBorrowRate = reserve.getEstimatedCumulativeBorrowRate(currentSlot);
34
+ const currentCumulativeBorrowRate = KaminoObligation.getCumulativeBorrowRate(borrow);
35
+ if (estimatedCumulativeBorrowRate.gt(currentCumulativeBorrowRate)) {
36
+ return estimatedCumulativeBorrowRate.div(currentCumulativeBorrowRate);
37
+ }
38
+ return new decimal_js_1.default(0);
39
+ };
40
+ this.obligationAddress = obligationAddress;
41
+ this.state = obligation;
42
+ const { borrows, deposits, refreshedStats } = this.calculatePositions(market, obligation, collateralExchangeRates, cumulativeBorrowRates);
43
+ this.deposits = deposits;
44
+ this.borrows = borrows;
45
+ this.refreshedStats = refreshedStats;
46
+ this.obligationTag = obligation.tag.toNumber();
47
+ }
48
+ static load(kaminoMarket, obligationAddress) {
49
+ return __awaiter(this, void 0, void 0, function* () {
50
+ const res = yield kaminoMarket.getConnection().getAccountInfoAndContext(obligationAddress);
51
+ if (res.value === null) {
52
+ return null;
53
+ }
54
+ const accInfo = res.value;
55
+ if (!accInfo.owner.equals(kaminoMarket.programId)) {
56
+ throw new Error("account doesn't belong to this program");
57
+ }
58
+ const obligation = accounts_1.Obligation.decode(accInfo.data);
59
+ if (obligation === null) {
60
+ return null;
61
+ }
62
+ const { collateralExchangeRates, cumulativeBorrowRates } = KaminoObligation.getRatesForObligation(kaminoMarket, obligation, res.context.slot);
63
+ return new KaminoObligation(kaminoMarket, obligationAddress, obligation, collateralExchangeRates, cumulativeBorrowRates);
64
+ });
65
+ }
66
+ static loadAll(kaminoMarket, obligationAddresses, slot) {
67
+ return __awaiter(this, void 0, void 0, function* () {
68
+ let currentSlot = slot;
69
+ let obligations;
70
+ if (!currentSlot) {
71
+ [currentSlot, obligations] = yield Promise.all([
72
+ kaminoMarket.getConnection().getSlot(),
73
+ accounts_1.Obligation.fetchMultiple(kaminoMarket.getConnection(), obligationAddresses, kaminoMarket.programId),
74
+ ]);
75
+ }
76
+ else {
77
+ obligations = yield accounts_1.Obligation.fetchMultiple(kaminoMarket.getConnection(), obligationAddresses, kaminoMarket.programId);
78
+ }
79
+ const cumulativeBorrowRates = new utils_2.PubkeyHashMap();
80
+ const collateralExchangeRates = new utils_2.PubkeyHashMap();
81
+ for (const obligation of obligations) {
82
+ if (obligation !== null) {
83
+ KaminoObligation.addRatesForObligation(kaminoMarket, obligation, collateralExchangeRates, cumulativeBorrowRates, currentSlot);
84
+ }
85
+ }
86
+ return obligations.map((obligation, i) => {
87
+ if (obligation === null) {
88
+ return null;
89
+ }
90
+ return new KaminoObligation(kaminoMarket, obligationAddresses[i], obligation, collateralExchangeRates, cumulativeBorrowRates);
91
+ });
92
+ });
93
+ }
94
+ /**
95
+ * @returns the obligation borrows as a list
96
+ */
97
+ getBorrows() {
98
+ return [...this.borrows.values()];
99
+ }
100
+ /**
101
+ * @returns the obligation borrows as a list
102
+ */
103
+ getDeposits() {
104
+ return [...this.deposits.values()];
105
+ }
106
+ /**
107
+ * @returns the total deposited value of the obligation (sum of all deposits)
108
+ */
109
+ getDepositedValue() {
110
+ return new fraction_1.Fraction(this.state.depositedValueSf).toDecimal();
111
+ }
112
+ /**
113
+ * @returns the total borrowed value of the obligation (sum of all borrows -- no borrow factor)
114
+ */
115
+ getBorrowedMarketValue() {
116
+ return new fraction_1.Fraction(this.state.borrowedAssetsMarketValueSf).toDecimal();
117
+ }
118
+ /**
119
+ * @returns the total borrowed value of the obligation (sum of all borrows -- with borrow factor weighting)
120
+ */
121
+ getBorrowedMarketValueBFAdjusted() {
122
+ return new fraction_1.Fraction(this.state.borrowFactorAdjustedDebtValueSf).toDecimal();
123
+ }
124
+ /**
125
+ * @returns total borrow power of the obligation, relative to max LTV of each asset's reserve
126
+ */
127
+ getAllowedBorrowValue() {
128
+ return new fraction_1.Fraction(this.state.allowedBorrowValueSf).toDecimal();
129
+ }
130
+ /**
131
+ * @returns the borrow value at which the obligation gets liquidatable
132
+ * (relative to the liquidation threshold of each asset's reserve)
133
+ */
134
+ getUnhealthyBorrowValue() {
135
+ return new fraction_1.Fraction(this.state.unhealthyBorrowValueSf).toDecimal();
136
+ }
137
+ /**
138
+ *
139
+ * @returns Market value of the deposit in the specified obligation collateral/deposit asset (USD)
140
+ */
141
+ getDepositMarketValue(deposit) {
142
+ return new fraction_1.Fraction(deposit.marketValueSf).toDecimal();
143
+ }
144
+ getBorrowByReserve(reserve) {
145
+ return this.borrows.get(reserve);
146
+ }
147
+ getDepositByReserve(reserve) {
148
+ return this.deposits.get(reserve);
149
+ }
150
+ getBorrowByMint(mint) {
151
+ for (const value of this.borrows.values()) {
152
+ if (value.mintAddress.equals(mint)) {
153
+ return value;
154
+ }
155
+ }
156
+ return undefined;
157
+ }
158
+ getDepositByMint(mint) {
159
+ for (const value of this.deposits.values()) {
160
+ if (value.mintAddress.equals(mint)) {
161
+ return value;
162
+ }
163
+ }
164
+ return undefined;
165
+ }
166
+ /**
167
+ *
168
+ * @returns Market value of the borrow in the specified obligation liquidity/borrow asset (USD) (no borrow factor weighting)
169
+ */
170
+ getBorrowMarketValue(borrow) {
171
+ return new fraction_1.Fraction(borrow.marketValueSf).toDecimal();
172
+ }
173
+ /**
174
+ *
175
+ * @returns Market value of the borrow in the specified obligation liquidity/borrow asset (USD) (with borrow factor weighting)
176
+ */
177
+ getBorrowMarketValueBFAdjusted(borrow) {
178
+ return new fraction_1.Fraction(borrow.borrowFactorAdjustedMarketValueSf).toDecimal();
179
+ }
180
+ /**
181
+ * Calculate the current ratio of borrowed value to deposited value
182
+ */
183
+ loanToValue() {
184
+ if (this.refreshedStats.userTotalDeposit.eq(0)) {
185
+ return new decimal_js_1.default(0);
186
+ }
187
+ return this.refreshedStats.userTotalBorrowBorrowFactorAdjusted.div(this.refreshedStats.userTotalDeposit);
188
+ }
189
+ /**
190
+ * @returns the total number of positions (deposits + borrows)
191
+ */
192
+ getNumberOfPositions() {
193
+ return this.deposits.size + this.borrows.size;
194
+ }
195
+ getNetAccountValue() {
196
+ return this.refreshedStats.netAccountValue;
197
+ }
198
+ /**
199
+ * @returns the potential elevation groups the obligation qualifies for
200
+ */
201
+ getElevationGroups(kaminoMarket) {
202
+ const reserves = new utils_2.PubkeyHashMap();
203
+ for (const deposit of this.state.deposits.values()) {
204
+ if ((0, utils_2.isNotNullPubkey)(deposit.depositReserve) && !reserves.has(deposit.depositReserve)) {
205
+ reserves.set(deposit.depositReserve, kaminoMarket.getReserveByAddress(deposit.depositReserve));
206
+ }
207
+ }
208
+ for (const borrow of this.state.borrows.values()) {
209
+ if ((0, utils_2.isNotNullPubkey)(borrow.borrowReserve) && !reserves.has(borrow.borrowReserve)) {
210
+ reserves.set(borrow.borrowReserve, kaminoMarket.getReserveByAddress(borrow.borrowReserve));
211
+ }
212
+ }
213
+ return KaminoObligation.getElevationGroupsForReserves([...reserves.values()]);
214
+ }
215
+ static getElevationGroupsForReserves(reserves) {
216
+ const elevationGroupsCounts = new Map();
217
+ for (const reserve of reserves) {
218
+ for (const elevationGroup of reserve.state.config.elevationGroups) {
219
+ if (elevationGroup !== 0) {
220
+ const count = elevationGroupsCounts.get(elevationGroup);
221
+ if (count) {
222
+ elevationGroupsCounts.set(elevationGroup, count + 1);
223
+ }
224
+ else {
225
+ elevationGroupsCounts.set(elevationGroup, 1);
226
+ }
227
+ }
228
+ }
229
+ }
230
+ const activeElevationGroups = new Array();
231
+ for (const [group, count] of elevationGroupsCounts.entries()) {
232
+ if (count === reserves.length) {
233
+ activeElevationGroups.push(group);
234
+ }
235
+ }
236
+ return activeElevationGroups;
237
+ }
238
+ calculateSimulatedBorrow(oldStats, oldBorrows, borrowAmount, mint, reserves) {
239
+ const newStats = Object.assign({}, oldStats);
240
+ const newBorrows = new utils_2.PubkeyHashMap([...oldBorrows.entries()]);
241
+ let borrowPosition = undefined;
242
+ for (const oldBorrow of oldBorrows.values()) {
243
+ if (oldBorrow.mintAddress.equals(mint)) {
244
+ borrowPosition = oldBorrow;
245
+ }
246
+ }
247
+ let reserve = undefined;
248
+ for (const kaminoReserve of reserves.values()) {
249
+ if (kaminoReserve.getLiquidityMint().equals(mint)) {
250
+ reserve = kaminoReserve;
251
+ }
252
+ }
253
+ if (!reserve) {
254
+ throw new Error(`No reserve found for mint ${mint}`);
255
+ }
256
+ if (!borrowPosition) {
257
+ borrowPosition = {
258
+ reserveAddress: reserve.address,
259
+ mintAddress: mint,
260
+ amount: new decimal_js_1.default(0),
261
+ marketValueRefreshed: new decimal_js_1.default(0),
262
+ };
263
+ }
264
+ if (!reserve.state.config.elevationGroups.includes(this.state.elevationGroup)) {
265
+ throw new Error(`User would have to downgrade the elevation group in order to be able to borrow from this reserve`);
266
+ }
267
+ const borrowFactor = this.state.elevationGroup !== 0 ? new decimal_js_1.default(1) : new decimal_js_1.default(reserve.stats.borrowFactor).div(100);
268
+ const borrowValueUSD = borrowAmount.mul(reserve.getOracleMarketPrice()).dividedBy(reserve.getMintFactor());
269
+ const borrowValueBorrowFactorAdjustedUSD = borrowValueUSD.mul(borrowFactor);
270
+ newStats.userTotalBorrow = (0, utils_1.positiveOrZero)(newStats.userTotalBorrow.plus(borrowValueUSD));
271
+ newStats.userTotalBorrowBorrowFactorAdjusted = (0, utils_1.positiveOrZero)(newStats.userTotalBorrowBorrowFactorAdjusted.plus(borrowValueBorrowFactorAdjustedUSD));
272
+ borrowPosition.amount = (0, utils_1.positiveOrZero)(borrowPosition.amount.plus(borrowAmount));
273
+ borrowPosition.mintAddress = mint;
274
+ borrowPosition.marketValueRefreshed = (0, utils_1.positiveOrZero)(borrowPosition.marketValueRefreshed.plus(borrowValueUSD));
275
+ newBorrows.set(borrowPosition.reserveAddress, borrowPosition);
276
+ return {
277
+ borrows: newBorrows,
278
+ stats: newStats,
279
+ };
280
+ }
281
+ calculateSimulatedDeposit(oldStats, oldDeposits, amount, mint, reserves, market) {
282
+ const newStats = Object.assign({}, oldStats);
283
+ const newDeposits = new utils_2.PubkeyHashMap([...oldDeposits.entries()]);
284
+ let depositPosition = undefined;
285
+ for (const oldDeposit of oldDeposits.values()) {
286
+ if (oldDeposit.mintAddress.equals(mint)) {
287
+ depositPosition = oldDeposit;
288
+ }
289
+ }
290
+ let reserve = undefined;
291
+ for (const kaminoReserve of reserves.values()) {
292
+ if (kaminoReserve.getLiquidityMint().equals(mint)) {
293
+ reserve = kaminoReserve;
294
+ }
295
+ }
296
+ if (!reserve) {
297
+ throw new Error(`No reserve found for mint ${mint}`);
298
+ }
299
+ if (!depositPosition) {
300
+ depositPosition = {
301
+ reserveAddress: reserve.address,
302
+ mintAddress: mint,
303
+ amount: new decimal_js_1.default(0),
304
+ marketValueRefreshed: new decimal_js_1.default(0),
305
+ };
306
+ }
307
+ let loanToValue = reserve.stats.loanToValuePct;
308
+ let liqThreshold = reserve.stats.liquidationThreshold;
309
+ if (this.state.elevationGroup !== 0) {
310
+ loanToValue = market.getElevationGroup(this.state.elevationGroup).ltvPct / 100;
311
+ liqThreshold = market.getElevationGroup(this.state.elevationGroup).liquidationThresholdPct / 100;
312
+ }
313
+ if (!reserve.state.config.elevationGroups.includes(this.state.elevationGroup)) {
314
+ throw new Error(`User would have to downgrade the elevation group in order to be able to deposit in this reserve`);
315
+ }
316
+ const supplyAmount = amount; //.mul(reserve.getCollateralExchangeRate()).floor();
317
+ const supplyAmountMultiplierUSD = supplyAmount
318
+ .mul(reserve.getOracleMarketPrice())
319
+ .dividedBy('1'.concat(Array(reserve.stats.decimals + 1).join('0')));
320
+ newStats.userTotalDeposit = (0, utils_1.positiveOrZero)(newStats.userTotalDeposit.plus(supplyAmountMultiplierUSD));
321
+ newStats.borrowLimit = (0, utils_1.positiveOrZero)(newStats.borrowLimit.plus(supplyAmountMultiplierUSD.mul(loanToValue)));
322
+ newStats.borrowLiquidationLimit = (0, utils_1.positiveOrZero)(newStats.borrowLiquidationLimit.plus(supplyAmountMultiplierUSD.mul(liqThreshold)));
323
+ newStats.liquidationLtv = (0, utils_1.valueOrZero)(newStats.borrowLiquidationLimit.div(newStats.userTotalDeposit));
324
+ depositPosition.amount = (0, utils_1.positiveOrZero)(depositPosition.amount.plus(amount));
325
+ depositPosition.mintAddress = mint;
326
+ depositPosition.marketValueRefreshed = (0, utils_1.positiveOrZero)(depositPosition.marketValueRefreshed.plus(supplyAmount.mul(reserve.getOracleMarketPrice()).dividedBy(reserve.getMintFactor())));
327
+ newDeposits.set(depositPosition.reserveAddress, depositPosition);
328
+ return {
329
+ deposits: newDeposits,
330
+ stats: newStats,
331
+ };
332
+ }
333
+ /**
334
+ * Calculate the newly modified stats of the obligation
335
+ */
336
+ // TODO: Elevation group problems
337
+ // TODO: Shall we set up position limits?
338
+ getSimulatedObligationStats(amount, action, mint, market, reserves) {
339
+ let newStats = Object.assign({}, this.refreshedStats);
340
+ let newDeposits = new utils_2.PubkeyHashMap([...this.deposits.entries()]);
341
+ let newBorrows = new utils_2.PubkeyHashMap([...this.borrows.entries()]);
342
+ switch (action) {
343
+ case 'deposit': {
344
+ const { stats, deposits } = this.calculateSimulatedDeposit(this.refreshedStats, this.deposits, amount, mint, reserves, market);
345
+ newStats = stats;
346
+ newDeposits = deposits;
347
+ break;
348
+ }
349
+ case 'borrow': {
350
+ const { stats, borrows } = this.calculateSimulatedBorrow(this.refreshedStats, this.borrows, amount, mint, reserves);
351
+ newStats = stats;
352
+ newBorrows = borrows;
353
+ break;
354
+ }
355
+ case 'repay': {
356
+ const { stats, borrows } = this.calculateSimulatedBorrow(this.refreshedStats, this.borrows, new decimal_js_1.default(amount).neg(), mint, reserves);
357
+ newStats = stats;
358
+ newBorrows = borrows;
359
+ break;
360
+ }
361
+ case 'withdraw': {
362
+ const { stats, deposits } = this.calculateSimulatedDeposit(this.refreshedStats, this.deposits, new decimal_js_1.default(amount).neg(), mint, reserves, market);
363
+ newStats = stats;
364
+ newDeposits = deposits;
365
+ break;
366
+ }
367
+ case 'depositAndBorrow': {
368
+ const { stats: statsAfterDeposit, deposits } = this.calculateSimulatedDeposit(this.refreshedStats, this.deposits, amount, mint, reserves, market);
369
+ const { stats, borrows } = this.calculateSimulatedBorrow(statsAfterDeposit, this.borrows, amount, mint, reserves);
370
+ newStats = stats;
371
+ newDeposits = deposits;
372
+ newBorrows = borrows;
373
+ break;
374
+ }
375
+ case 'repayAndWithdraw': {
376
+ const { stats: statsAfterRepay, borrows } = this.calculateSimulatedBorrow(this.refreshedStats, this.borrows, new decimal_js_1.default(amount).neg(), mint, reserves);
377
+ const { stats: statsAfterWithdraw, deposits } = this.calculateSimulatedDeposit(statsAfterRepay, this.deposits, amount, mint, reserves, market);
378
+ newStats = statsAfterWithdraw;
379
+ newDeposits = deposits;
380
+ newBorrows = borrows;
381
+ break;
382
+ }
383
+ default: {
384
+ throw Error(`Invalid action type ${action} for getSimulatedObligationStats`);
385
+ }
386
+ }
387
+ newStats.netAccountValue = newStats.userTotalDeposit.minus(newStats.userTotalBorrow);
388
+ newStats.loanToValue = (0, utils_1.valueOrZero)(newStats.userTotalBorrowBorrowFactorAdjusted.dividedBy(newStats.userTotalDeposit));
389
+ newStats.leverage = (0, utils_1.valueOrZero)(newStats.userTotalDeposit.dividedBy(newStats.netAccountValue));
390
+ return {
391
+ stats: newStats,
392
+ deposits: newDeposits,
393
+ borrows: newBorrows,
394
+ };
395
+ }
396
+ calculateDeposits(market, obligation, collateralExchangeRates, getPx) {
397
+ return KaminoObligation.calculateObligationDeposits(market, obligation, collateralExchangeRates, getPx);
398
+ }
399
+ calculateBorrows(market, obligation, cumulativeBorrowRates, getPx) {
400
+ return KaminoObligation.calculateObligationBorrows(market, obligation, cumulativeBorrowRates, getPx);
401
+ }
402
+ calculatePositions(market, obligation, collateralExchangeRates, cumulativeBorrowRates) {
403
+ const commonElevationGroups = this.getElevationGroups(market);
404
+ const getOraclePx = (reserve) => reserve.getOracleMarketPrice();
405
+ const depositStatsOraclePrice = this.calculateDeposits(market, obligation, collateralExchangeRates, getOraclePx);
406
+ const borrowStatsOraclePrice = this.calculateBorrows(market, obligation, cumulativeBorrowRates, getOraclePx);
407
+ const netAccountValueScopeRefreshed = depositStatsOraclePrice.userTotalDeposit.minus(borrowStatsOraclePrice.userTotalBorrow);
408
+ return {
409
+ deposits: depositStatsOraclePrice.deposits,
410
+ borrows: borrowStatsOraclePrice.borrows,
411
+ refreshedStats: {
412
+ borrowLimit: depositStatsOraclePrice.borrowLimit,
413
+ borrowLiquidationLimit: depositStatsOraclePrice.borrowLiquidationLimit,
414
+ userTotalBorrow: borrowStatsOraclePrice.userTotalBorrow,
415
+ userTotalBorrowBorrowFactorAdjusted: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted,
416
+ userTotalDeposit: depositStatsOraclePrice.userTotalDeposit,
417
+ liquidationLtv: depositStatsOraclePrice.liquidationLtv,
418
+ borrowUtilization: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted.dividedBy(depositStatsOraclePrice.borrowLimit),
419
+ netAccountValue: netAccountValueScopeRefreshed,
420
+ leverage: depositStatsOraclePrice.userTotalDeposit.dividedBy(netAccountValueScopeRefreshed),
421
+ loanToValue: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted.dividedBy(depositStatsOraclePrice.userTotalDeposit),
422
+ potentialElevationGroupUpdate: commonElevationGroups,
423
+ },
424
+ };
425
+ }
426
+ static calculateObligationDeposits(market, obligation, collateralExchangeRates, getPx) {
427
+ let userTotalDeposit = new decimal_js_1.default(0);
428
+ let userTotalCollateralDeposit = new decimal_js_1.default(0);
429
+ let borrowLimit = new decimal_js_1.default(0);
430
+ let borrowLiquidationLimit = new decimal_js_1.default(0);
431
+ const deposits = new utils_2.PubkeyHashMap();
432
+ for (let i = 0; i < obligation.deposits.length; i++) {
433
+ if (!(0, utils_2.isNotNullPubkey)(obligation.deposits[i].depositReserve)) {
434
+ continue;
435
+ }
436
+ const deposit = obligation.deposits[i];
437
+ const reserve = market.getReserveByAddress(deposit.depositReserve);
438
+ if (!reserve) {
439
+ throw new Error(`Obligation contains a deposit belonging to reserve: ${deposit.depositReserve} but the reserve was not found on the market. Deposit amount: ${deposit.depositedAmount}`);
440
+ }
441
+ let loanToValue = reserve.stats.loanToValuePct;
442
+ let liqThreshold = reserve.stats.liquidationThreshold;
443
+ if (obligation.elevationGroup !== 0) {
444
+ loanToValue = market.state.elevationGroups[obligation.elevationGroup - 1].ltvPct / 100;
445
+ liqThreshold = market.state.elevationGroups[obligation.elevationGroup - 1].liquidationThresholdPct / 100;
446
+ }
447
+ let exchangeRate;
448
+ if (collateralExchangeRates !== null) {
449
+ exchangeRate = collateralExchangeRates.get(reserve.address);
450
+ }
451
+ else {
452
+ exchangeRate = reserve.getCollateralExchangeRate();
453
+ }
454
+ const supplyAmount = new decimal_js_1.default(deposit.depositedAmount.toString()).div(exchangeRate);
455
+ const depositValueUsd = supplyAmount.mul(getPx(reserve)).div(reserve.getMintFactor());
456
+ userTotalDeposit = userTotalDeposit.add(depositValueUsd);
457
+ if (loanToValue !== 0) {
458
+ userTotalCollateralDeposit = userTotalCollateralDeposit.add(depositValueUsd);
459
+ }
460
+ borrowLimit = borrowLimit.add(depositValueUsd.mul(loanToValue));
461
+ borrowLiquidationLimit = borrowLiquidationLimit.add(depositValueUsd.mul(liqThreshold));
462
+ const position = {
463
+ reserveAddress: reserve.address,
464
+ mintAddress: reserve.getLiquidityMint(),
465
+ amount: supplyAmount,
466
+ marketValueRefreshed: depositValueUsd,
467
+ };
468
+ deposits.set(reserve.address, position);
469
+ }
470
+ return {
471
+ deposits,
472
+ userTotalDeposit,
473
+ userTotalCollateralDeposit,
474
+ borrowLimit,
475
+ liquidationLtv: (0, utils_1.valueOrZero)(borrowLiquidationLimit.div(userTotalDeposit)),
476
+ borrowLiquidationLimit,
477
+ };
478
+ }
479
+ static calculateObligationBorrows(market, obligation, cumulativeBorrowRates, getPx) {
480
+ let userTotalBorrow = new decimal_js_1.default(0);
481
+ let userTotalBorrowBorrowFactorAdjusted = new decimal_js_1.default(0);
482
+ let positions = 0;
483
+ const borrows = new utils_2.PubkeyHashMap();
484
+ for (let i = 0; i < obligation.borrows.length; i++) {
485
+ if (!(0, utils_2.isNotNullPubkey)(obligation.borrows[i].borrowReserve)) {
486
+ continue;
487
+ }
488
+ const borrow = obligation.borrows[i];
489
+ const reserve = market.getReserveByAddress(borrow.borrowReserve);
490
+ if (!reserve) {
491
+ throw new Error(`Obligation contains a borrow belonging to reserve: ${borrow.borrowReserve} but the reserve was not found on the market. Borrow amount: ${KaminoObligation.getBorrowAmount(borrow)}`);
492
+ }
493
+ const obligationCumulativeBorrowRate = KaminoObligation.getCumulativeBorrowRate(borrow);
494
+ let cumulativeBorrowRate;
495
+ if (cumulativeBorrowRates !== null) {
496
+ cumulativeBorrowRate = cumulativeBorrowRates.get(reserve.address);
497
+ }
498
+ else {
499
+ cumulativeBorrowRate = reserve.getCumulativeBorrowRate();
500
+ }
501
+ const borrowAmount = KaminoObligation.getBorrowAmount(borrow)
502
+ .mul(cumulativeBorrowRate)
503
+ .dividedBy(obligationCumulativeBorrowRate);
504
+ const borrowValueUsd = borrowAmount.mul(getPx(reserve)).dividedBy(reserve.getMintFactor());
505
+ const borrowFactor = KaminoObligation.getBorrowFactorForReserve(reserve, obligation.elevationGroup);
506
+ const borrowValueBorrowFactorAdjustedUsd = borrowValueUsd.mul(borrowFactor);
507
+ if (!borrowAmount.eq(new decimal_js_1.default('0'))) {
508
+ positions += 1;
509
+ }
510
+ userTotalBorrow = userTotalBorrow.plus(borrowValueUsd);
511
+ userTotalBorrowBorrowFactorAdjusted = userTotalBorrowBorrowFactorAdjusted.plus(borrowValueBorrowFactorAdjustedUsd);
512
+ const position = {
513
+ reserveAddress: reserve.address,
514
+ mintAddress: reserve.getLiquidityMint(),
515
+ amount: borrowAmount,
516
+ marketValueRefreshed: borrowValueUsd,
517
+ };
518
+ borrows.set(reserve.address, position);
519
+ }
520
+ return {
521
+ borrows,
522
+ userTotalBorrow,
523
+ userTotalBorrowBorrowFactorAdjusted,
524
+ positions,
525
+ };
526
+ }
527
+ /**
528
+ *
529
+ * @returns Total borrowed amount for the specified obligation liquidity/borrow asset
530
+ */
531
+ static getBorrowAmount(borrow) {
532
+ return new fraction_1.Fraction(borrow.borrowedAmountSf).toDecimal();
533
+ }
534
+ /**
535
+ *
536
+ * @returns Cumulative borrow rate for the specified obligation liquidity/borrow asset
537
+ */
538
+ static getCumulativeBorrowRate(borrow) {
539
+ let accSf = new bn_js_1.default(0);
540
+ for (const value of borrow.cumulativeBorrowRateBsf.value.reverse()) {
541
+ accSf = accSf.add(value);
542
+ accSf.shrn(64);
543
+ }
544
+ return new fraction_1.Fraction(accSf).toDecimal();
545
+ }
546
+ static getRatesForObligation(kaminoMarket, obligation, slot) {
547
+ const collateralExchangeRates = KaminoObligation.getCollateralExchangeRatesForObligation(kaminoMarket, obligation, slot);
548
+ const cumulativeBorrowRates = KaminoObligation.getCumulativeBorrowRatesForObligation(kaminoMarket, obligation, slot);
549
+ return {
550
+ collateralExchangeRates,
551
+ cumulativeBorrowRates,
552
+ };
553
+ }
554
+ static addRatesForObligation(kaminoMarket, obligation, collateralExchangeRates, cumulativeBorrowRates, slot) {
555
+ KaminoObligation.addCollateralExchangeRatesForObligation(kaminoMarket, collateralExchangeRates, obligation, slot);
556
+ KaminoObligation.addCumulativeBorrowRatesForObligation(kaminoMarket, cumulativeBorrowRates, obligation, slot);
557
+ }
558
+ static getCollateralExchangeRatesForObligation(kaminoMarket, obligation, slot) {
559
+ const collateralExchangeRates = new utils_2.PubkeyHashMap();
560
+ for (let i = 0; i < obligation.deposits.length; i++) {
561
+ const deposit = obligation.deposits[i];
562
+ if ((0, utils_2.isNotNullPubkey)(deposit.depositReserve) && !collateralExchangeRates.has(deposit.depositReserve)) {
563
+ const reserve = kaminoMarket.getReserveByAddress(deposit.depositReserve);
564
+ const collateralExchangeRate = reserve.getEstimatedCollateralExchangeRate(slot, kaminoMarket.state.referralFeeBps);
565
+ collateralExchangeRates.set(reserve.address, collateralExchangeRate);
566
+ }
567
+ }
568
+ return collateralExchangeRates;
569
+ }
570
+ static addCollateralExchangeRatesForObligation(kaminoMarket, collateralExchangeRates, obligation, slot) {
571
+ for (let i = 0; i < obligation.deposits.length; i++) {
572
+ const deposit = obligation.deposits[i];
573
+ if ((0, utils_2.isNotNullPubkey)(deposit.depositReserve) && !collateralExchangeRates.has(deposit.depositReserve)) {
574
+ const reserve = kaminoMarket.getReserveByAddress(deposit.depositReserve);
575
+ const collateralExchangeRate = reserve.getEstimatedCollateralExchangeRate(slot, kaminoMarket.state.referralFeeBps);
576
+ collateralExchangeRates.set(reserve.address, collateralExchangeRate);
577
+ }
578
+ }
579
+ }
580
+ static getCumulativeBorrowRatesForObligation(kaminoMarket, obligation, slot) {
581
+ const cumulativeBorrowRates = new utils_2.PubkeyHashMap();
582
+ for (let i = 0; i < obligation.borrows.length; i++) {
583
+ const borrow = obligation.borrows[i];
584
+ if ((0, utils_2.isNotNullPubkey)(borrow.borrowReserve) && !cumulativeBorrowRates.has(borrow.borrowReserve)) {
585
+ const reserve = kaminoMarket.getReserveByAddress(borrow.borrowReserve);
586
+ const cumulativeBorrowRate = reserve.getEstimatedCumulativeBorrowRate(slot);
587
+ cumulativeBorrowRates.set(reserve.address, cumulativeBorrowRate);
588
+ }
589
+ }
590
+ return cumulativeBorrowRates;
591
+ }
592
+ static addCumulativeBorrowRatesForObligation(kaminoMarket, cumulativeBorrowRates, obligation, slot) {
593
+ for (let i = 0; i < obligation.borrows.length; i++) {
594
+ const borrow = obligation.borrows[i];
595
+ if ((0, utils_2.isNotNullPubkey)(borrow.borrowReserve) && !cumulativeBorrowRates.has(borrow.borrowReserve)) {
596
+ const reserve = kaminoMarket.getReserveByAddress(borrow.borrowReserve);
597
+ const cumulativeBorrowRate = reserve.getEstimatedCumulativeBorrowRate(slot);
598
+ cumulativeBorrowRates.set(reserve.address, cumulativeBorrowRate);
599
+ }
600
+ }
601
+ }
602
+ static getBorrowFactorForReserve(reserve, elevationGroup) {
603
+ if (elevationGroup !== 0) {
604
+ return new decimal_js_1.default(1);
605
+ }
606
+ return new decimal_js_1.default(reserve.stats.borrowFactor).div(100);
607
+ }
608
+ }
609
+ exports.KaminoObligation = KaminoObligation;
610
+ //# sourceMappingURL=obligation.js.map