@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.
- package/README.md +85 -0
- package/dist/classes/action.d.ts +154 -0
- package/dist/classes/action.js +1505 -0
- package/dist/classes/action.js.map +1 -0
- package/dist/classes/curve.d.ts +15 -0
- package/dist/classes/curve.js +32 -0
- package/dist/classes/curve.js.map +1 -0
- package/dist/classes/fraction.d.ts +23 -0
- package/dist/classes/fraction.js +60 -0
- package/dist/classes/fraction.js.map +1 -0
- package/dist/classes/index.d.ts +8 -0
- package/dist/classes/index.js +25 -0
- package/dist/classes/index.js.map +1 -0
- package/dist/classes/jupiterPerps.d.ts +2 -0
- package/dist/classes/jupiterPerps.js +25 -0
- package/dist/classes/jupiterPerps.js.map +1 -0
- package/dist/classes/market.d.ts +140 -0
- package/dist/classes/market.js +901 -0
- package/dist/classes/market.js.map +1 -0
- package/dist/classes/obligation.d.ts +170 -0
- package/dist/classes/obligation.js +610 -0
- package/dist/classes/obligation.js.map +1 -0
- package/dist/classes/reserve.d.ts +161 -0
- package/dist/classes/reserve.js +474 -0
- package/dist/classes/reserve.js.map +1 -0
- package/dist/classes/shared.d.ts +41 -0
- package/dist/classes/shared.js +10 -0
- package/dist/classes/shared.js.map +1 -0
- package/dist/classes/utils.d.ts +40 -0
- package/dist/classes/utils.js +129 -0
- package/dist/classes/utils.js.map +1 -0
- package/dist/idl.json +4102 -0
- package/dist/idl_codegen/accounts/LendingMarket.d.ts +153 -0
- package/dist/idl_codegen/accounts/LendingMarket.js +204 -0
- package/dist/idl_codegen/accounts/LendingMarket.js.map +1 -0
- package/dist/idl_codegen/accounts/Obligation.d.ts +139 -0
- package/dist/idl_codegen/accounts/Obligation.js +193 -0
- package/dist/idl_codegen/accounts/Obligation.js.map +1 -0
- package/dist/idl_codegen/accounts/ReferrerState.d.ts +22 -0
- package/dist/idl_codegen/accounts/ReferrerState.js +101 -0
- package/dist/idl_codegen/accounts/ReferrerState.js.map +1 -0
- package/dist/idl_codegen/accounts/ReferrerTokenState.d.ts +51 -0
- package/dist/idl_codegen/accounts/ReferrerTokenState.js +126 -0
- package/dist/idl_codegen/accounts/ReferrerTokenState.js.map +1 -0
- package/dist/idl_codegen/accounts/Reserve.d.ts +72 -0
- package/dist/idl_codegen/accounts/Reserve.js +156 -0
- package/dist/idl_codegen/accounts/Reserve.js.map +1 -0
- package/dist/idl_codegen/accounts/ShortUrl.d.ts +22 -0
- package/dist/idl_codegen/accounts/ShortUrl.js +101 -0
- package/dist/idl_codegen/accounts/ShortUrl.js.map +1 -0
- package/dist/idl_codegen/accounts/UserMetadata.d.ts +48 -0
- package/dist/idl_codegen/accounts/UserMetadata.js +126 -0
- package/dist/idl_codegen/accounts/UserMetadata.js.map +1 -0
- package/dist/idl_codegen/accounts/UserState.d.ts +143 -0
- package/dist/idl_codegen/accounts/UserState.js +180 -0
- package/dist/idl_codegen/accounts/UserState.js.map +1 -0
- package/dist/idl_codegen/accounts/index.d.ts +16 -0
- package/dist/idl_codegen/accounts/index.js +20 -0
- package/dist/idl_codegen/accounts/index.js.map +1 -0
- package/dist/idl_codegen/errors/anchor.d.ts +434 -0
- package/dist/idl_codegen/errors/anchor.js +713 -0
- package/dist/idl_codegen/errors/anchor.js.map +1 -0
- package/dist/idl_codegen/errors/custom.d.ts +730 -0
- package/dist/idl_codegen/errors/custom.js +1194 -0
- package/dist/idl_codegen/errors/custom.js.map +1 -0
- package/dist/idl_codegen/errors/index.d.ts +5 -0
- package/dist/idl_codegen/errors/index.js +71 -0
- package/dist/idl_codegen/errors/index.js.map +1 -0
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.d.ts +20 -0
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.js +75 -0
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.d.ts +9 -0
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.js +20 -0
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.js.map +1 -0
- package/dist/idl_codegen/instructions/depositObligationCollateral.d.ts +17 -0
- package/dist/idl_codegen/instructions/depositObligationCollateral.js +64 -0
- package/dist/idl_codegen/instructions/depositObligationCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidity.d.ts +19 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidity.js +74 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.d.ts +21 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.js +80 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.d.ts +20 -0
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.js +75 -0
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.d.ts +21 -0
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.js +75 -0
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/idlMissingTypes.d.ts +18 -0
- package/dist/idl_codegen/instructions/idlMissingTypes.js +62 -0
- package/dist/idl_codegen/instructions/idlMissingTypes.js.map +1 -0
- package/dist/idl_codegen/instructions/index.d.ts +66 -0
- package/dist/idl_codegen/instructions/index.js +70 -0
- package/dist/idl_codegen/instructions/index.js.map +1 -0
- package/dist/idl_codegen/instructions/initFarmsForReserve.d.ts +19 -0
- package/dist/idl_codegen/instructions/initFarmsForReserve.js +63 -0
- package/dist/idl_codegen/instructions/initFarmsForReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/initLendingMarket.d.ts +13 -0
- package/dist/idl_codegen/instructions/initLendingMarket.js +55 -0
- package/dist/idl_codegen/instructions/initLendingMarket.js.map +1 -0
- package/dist/idl_codegen/instructions/initObligation.d.ts +19 -0
- package/dist/idl_codegen/instructions/initObligation.js +55 -0
- package/dist/idl_codegen/instructions/initObligation.js.map +1 -0
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.d.ts +20 -0
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.js +60 -0
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.d.ts +14 -0
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.js +54 -0
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.js.map +1 -0
- package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts +14 -0
- package/dist/idl_codegen/instructions/initReferrerTokenState.js +50 -0
- package/dist/idl_codegen/instructions/initReferrerTokenState.js.map +1 -0
- package/dist/idl_codegen/instructions/initReserve.d.ts +16 -0
- package/dist/idl_codegen/instructions/initReserve.js +47 -0
- package/dist/idl_codegen/instructions/initReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/initUserMetadata.d.ts +14 -0
- package/dist/idl_codegen/instructions/initUserMetadata.js +54 -0
- package/dist/idl_codegen/instructions/initUserMetadata.js.map +1 -0
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.d.ts +27 -0
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.js +102 -0
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/redeemFees.d.ts +10 -0
- package/dist/idl_codegen/instructions/redeemFees.js +33 -0
- package/dist/idl_codegen/instructions/redeemFees.js.map +1 -0
- package/dist/idl_codegen/instructions/redeemReserveCollateral.d.ts +19 -0
- package/dist/idl_codegen/instructions/redeemReserveCollateral.js +78 -0
- package/dist/idl_codegen/instructions/redeemReserveCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/refreshObligation.d.ts +6 -0
- package/dist/idl_codegen/instructions/refreshObligation.js +17 -0
- package/dist/idl_codegen/instructions/refreshObligation.js.map +1 -0
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.d.ts +19 -0
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.js +63 -0
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/refreshReserve.d.ts +10 -0
- package/dist/idl_codegen/instructions/refreshReserve.js +29 -0
- package/dist/idl_codegen/instructions/refreshReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/repayObligationLiquidity.d.ts +17 -0
- package/dist/idl_codegen/instructions/repayObligationLiquidity.js +60 -0
- package/dist/idl_codegen/instructions/repayObligationLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/requestElevationGroup.d.ts +11 -0
- package/dist/idl_codegen/instructions/requestElevationGroup.js +47 -0
- package/dist/idl_codegen/instructions/requestElevationGroup.js.map +1 -0
- package/dist/idl_codegen/instructions/socializeLoss.d.ts +14 -0
- package/dist/idl_codegen/instructions/socializeLoss.js +53 -0
- package/dist/idl_codegen/instructions/socializeLoss.js.map +1 -0
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.d.ts +13 -0
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.js +51 -0
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.js.map +1 -0
- package/dist/idl_codegen/instructions/updateLendingMarket.d.ts +12 -0
- package/dist/idl_codegen/instructions/updateLendingMarket.js +50 -0
- package/dist/idl_codegen/instructions/updateLendingMarket.js.map +1 -0
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.d.ts +6 -0
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.js +21 -0
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.js.map +1 -0
- package/dist/idl_codegen/instructions/updateReserveConfig.d.ts +13 -0
- package/dist/idl_codegen/instructions/updateReserveConfig.js +51 -0
- package/dist/idl_codegen/instructions/updateReserveConfig.js.map +1 -0
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.d.ts +13 -0
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.js +51 -0
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.js.map +1 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.d.ts +18 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.js +69 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.d.ts +21 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js +84 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts +16 -0
- package/dist/idl_codegen/instructions/withdrawProtocolFee.js +59 -0
- package/dist/idl_codegen/instructions/withdrawProtocolFee.js.map +1 -0
- package/dist/idl_codegen/instructions/withdrawReferrerFees.d.ts +12 -0
- package/dist/idl_codegen/instructions/withdrawReferrerFees.js +35 -0
- package/dist/idl_codegen/instructions/withdrawReferrerFees.js.map +1 -0
- package/dist/idl_codegen/programId.d.ts +4 -0
- package/dist/idl_codegen/programId.js +13 -0
- package/dist/idl_codegen/programId.js.map +1 -0
- package/dist/idl_codegen/types/AssetTier.d.ts +44 -0
- package/dist/idl_codegen/types/AssetTier.js +128 -0
- package/dist/idl_codegen/types/AssetTier.js.map +1 -0
- package/dist/idl_codegen/types/BigFractionBytes.d.ts +27 -0
- package/dist/idl_codegen/types/BigFractionBytes.js +73 -0
- package/dist/idl_codegen/types/BigFractionBytes.js.map +1 -0
- package/dist/idl_codegen/types/BorrowRateCurve.d.ts +27 -0
- package/dist/idl_codegen/types/BorrowRateCurve.js +62 -0
- package/dist/idl_codegen/types/BorrowRateCurve.js.map +1 -0
- package/dist/idl_codegen/types/CurvePoint.d.ts +26 -0
- package/dist/idl_codegen/types/CurvePoint.js +66 -0
- package/dist/idl_codegen/types/CurvePoint.js.map +1 -0
- package/dist/idl_codegen/types/ElevationGroup.d.ts +52 -0
- package/dist/idl_codegen/types/ElevationGroup.js +103 -0
- package/dist/idl_codegen/types/ElevationGroup.js.map +1 -0
- package/dist/idl_codegen/types/FeeCalculation.d.ts +31 -0
- package/dist/idl_codegen/types/FeeCalculation.js +102 -0
- package/dist/idl_codegen/types/FeeCalculation.js.map +1 -0
- package/dist/idl_codegen/types/InitObligationArgs.d.ts +26 -0
- package/dist/idl_codegen/types/InitObligationArgs.js +66 -0
- package/dist/idl_codegen/types/InitObligationArgs.js.map +1 -0
- package/dist/idl_codegen/types/LastUpdate.d.ts +47 -0
- package/dist/idl_codegen/types/LastUpdate.js +86 -0
- package/dist/idl_codegen/types/LastUpdate.js.map +1 -0
- package/dist/idl_codegen/types/ObligationCollateral.d.ts +48 -0
- package/dist/idl_codegen/types/ObligationCollateral.js +87 -0
- package/dist/idl_codegen/types/ObligationCollateral.js.map +1 -0
- package/dist/idl_codegen/types/ObligationLiquidity.d.ts +75 -0
- package/dist/idl_codegen/types/ObligationLiquidity.js +107 -0
- package/dist/idl_codegen/types/ObligationLiquidity.js.map +1 -0
- package/dist/idl_codegen/types/PriceHeuristic.d.ts +41 -0
- package/dist/idl_codegen/types/PriceHeuristic.js +75 -0
- package/dist/idl_codegen/types/PriceHeuristic.js.map +1 -0
- package/dist/idl_codegen/types/PythConfiguration.d.ts +25 -0
- package/dist/idl_codegen/types/PythConfiguration.js +62 -0
- package/dist/idl_codegen/types/PythConfiguration.js.map +1 -0
- package/dist/idl_codegen/types/ReserveCollateral.d.ts +53 -0
- package/dist/idl_codegen/types/ReserveCollateral.js +93 -0
- package/dist/idl_codegen/types/ReserveCollateral.js.map +1 -0
- package/dist/idl_codegen/types/ReserveConfig.d.ts +304 -0
- package/dist/idl_codegen/types/ReserveConfig.js +209 -0
- package/dist/idl_codegen/types/ReserveConfig.js.map +1 -0
- package/dist/idl_codegen/types/ReserveFarmKind.d.ts +31 -0
- package/dist/idl_codegen/types/ReserveFarmKind.js +102 -0
- package/dist/idl_codegen/types/ReserveFarmKind.js.map +1 -0
- package/dist/idl_codegen/types/ReserveFees.d.ts +78 -0
- package/dist/idl_codegen/types/ReserveFees.js +86 -0
- package/dist/idl_codegen/types/ReserveFees.js.map +1 -0
- package/dist/idl_codegen/types/ReserveLiquidity.d.ts +173 -0
- package/dist/idl_codegen/types/ReserveLiquidity.js +166 -0
- package/dist/idl_codegen/types/ReserveLiquidity.js.map +1 -0
- package/dist/idl_codegen/types/ReserveStatus.d.ts +44 -0
- package/dist/idl_codegen/types/ReserveStatus.js +128 -0
- package/dist/idl_codegen/types/ReserveStatus.js.map +1 -0
- package/dist/idl_codegen/types/ScopeConfiguration.d.ts +41 -0
- package/dist/idl_codegen/types/ScopeConfiguration.js +76 -0
- package/dist/idl_codegen/types/ScopeConfiguration.js.map +1 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.d.ts +30 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.js +67 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.js.map +1 -0
- package/dist/idl_codegen/types/TokenInfo.d.ts +109 -0
- package/dist/idl_codegen/types/TokenInfo.js +116 -0
- package/dist/idl_codegen/types/TokenInfo.js.map +1 -0
- package/dist/idl_codegen/types/UpdateConfigMode.d.ts +538 -0
- package/dist/idl_codegen/types/UpdateConfigMode.js +1116 -0
- package/dist/idl_codegen/types/UpdateConfigMode.js.map +1 -0
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts +155 -0
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js +273 -0
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js.map +1 -0
- package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts +200 -0
- package/dist/idl_codegen/types/UpdateLendingMarketMode.js +440 -0
- package/dist/idl_codegen/types/UpdateLendingMarketMode.js.map +1 -0
- package/dist/idl_codegen/types/WithdrawalCaps.d.ts +38 -0
- package/dist/idl_codegen/types/WithdrawalCaps.js +86 -0
- package/dist/idl_codegen/types/WithdrawalCaps.js.map +1 -0
- package/dist/idl_codegen/types/index.d.ts +65 -0
- package/dist/idl_codegen/types/index.js +77 -0
- package/dist/idl_codegen/types/index.js.map +1 -0
- package/dist/idl_codegen/zero_padding/ObligationZP.d.ts +53 -0
- package/dist/idl_codegen/zero_padding/ObligationZP.js +140 -0
- package/dist/idl_codegen/zero_padding/ObligationZP.js.map +1 -0
- package/dist/idl_codegen/zero_padding/index.d.ts +1 -0
- package/dist/idl_codegen/zero_padding/index.js +6 -0
- package/dist/idl_codegen/zero_padding/index.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.d.ts +48 -0
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.js +144 -0
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/accounts/index.d.ts +2 -0
- package/dist/idl_codegen_jupiter_perps/accounts/index.js +6 -0
- package/dist/idl_codegen_jupiter_perps/accounts/index.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/programId.d.ts +3 -0
- package/dist/idl_codegen_jupiter_perps/programId.js +9 -0
- package/dist/idl_codegen_jupiter_perps/programId.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.d.ts +62 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.js +115 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.d.ts +32 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.js +75 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.d.ts +32 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.js +75 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/index.d.ts +6 -0
- package/dist/idl_codegen_jupiter_perps/types/index.js +10 -0
- package/dist/idl_codegen_jupiter_perps/types/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/lending_operations/index.d.ts +2 -0
- package/dist/lending_operations/index.js +19 -0
- package/dist/lending_operations/index.js.map +1 -0
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts +5 -0
- package/dist/lending_operations/repay_with_collateral_calcs.js +26 -0
- package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -0
- package/dist/lending_operations/repay_with_collateral_operations.d.ts +47 -0
- package/dist/lending_operations/repay_with_collateral_operations.js +163 -0
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -0
- package/dist/leverage/calcs.d.ts +132 -0
- package/dist/leverage/calcs.js +291 -0
- package/dist/leverage/calcs.js.map +1 -0
- package/dist/leverage/index.d.ts +4 -0
- package/dist/leverage/index.js +21 -0
- package/dist/leverage/index.js.map +1 -0
- package/dist/leverage/instructions.d.ts +43 -0
- package/dist/leverage/instructions.js +103 -0
- package/dist/leverage/instructions.js.map +1 -0
- package/dist/leverage/operations.d.ts +231 -0
- package/dist/leverage/operations.js +1058 -0
- package/dist/leverage/operations.js.map +1 -0
- package/dist/leverage/utils.d.ts +22 -0
- package/dist/leverage/utils.js +144 -0
- package/dist/leverage/utils.js.map +1 -0
- package/dist/lib.d.ts +9 -0
- package/dist/lib.js +26 -0
- package/dist/lib.js.map +1 -0
- package/dist/referrals/index.d.ts +2 -0
- package/dist/referrals/index.js +19 -0
- package/dist/referrals/index.js.map +1 -0
- package/dist/referrals/instructions.d.ts +16 -0
- package/dist/referrals/instructions.js +76 -0
- package/dist/referrals/instructions.js.map +1 -0
- package/dist/referrals/operations.d.ts +97 -0
- package/dist/referrals/operations.js +258 -0
- package/dist/referrals/operations.js.map +1 -0
- package/dist/utils/ObligationType.d.ts +49 -0
- package/dist/utils/ObligationType.js +129 -0
- package/dist/utils/ObligationType.js.map +1 -0
- package/dist/utils/api.d.ts +6 -0
- package/dist/utils/api.js +61 -0
- package/dist/utils/api.js.map +1 -0
- package/dist/utils/ata.d.ts +25 -0
- package/dist/utils/ata.js +155 -0
- package/dist/utils/ata.js.map +1 -0
- package/dist/utils/constants.d.ts +48 -0
- package/dist/utils/constants.js +75 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/idl.d.ts +2 -0
- package/dist/utils/idl.js +9 -0
- package/dist/utils/idl.js.map +1 -0
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.js +31 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/instruction.d.ts +22 -0
- package/dist/utils/instruction.js +202 -0
- package/dist/utils/instruction.js.map +1 -0
- package/dist/utils/kamino.d.ts +3 -0
- package/dist/utils/kamino.js +45 -0
- package/dist/utils/kamino.js.map +1 -0
- package/dist/utils/layout.d.ts +13 -0
- package/dist/utils/layout.js +123 -0
- package/dist/utils/layout.js.map +1 -0
- package/dist/utils/lookupTable.d.ts +5 -0
- package/dist/utils/lookupTable.js +57 -0
- package/dist/utils/lookupTable.js.map +1 -0
- package/dist/utils/oracle.d.ts +51 -0
- package/dist/utils/oracle.js +295 -0
- package/dist/utils/oracle.js.map +1 -0
- package/dist/utils/pubkey.d.ts +74 -0
- package/dist/utils/pubkey.js +229 -0
- package/dist/utils/pubkey.js.map +1 -0
- package/dist/utils/seeds.d.ts +122 -0
- package/dist/utils/seeds.js +153 -0
- package/dist/utils/seeds.js.map +1 -0
- package/dist/utils/slots.d.ts +18 -0
- package/dist/utils/slots.js +32 -0
- package/dist/utils/slots.js.map +1 -0
- package/dist/utils/syncNative.d.ts +10 -0
- package/dist/utils/syncNative.js +46 -0
- package/dist/utils/syncNative.js.map +1 -0
- package/dist/utils/userMetadata.d.ts +15 -0
- package/dist/utils/userMetadata.js +259 -0
- package/dist/utils/userMetadata.js.map +1 -0
- package/package.json +94 -0
|
@@ -0,0 +1,1505 @@
|
|
|
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.KaminoAction = exports.DEPOSITS_LIMIT = exports.BORROWS_LIMIT = exports.POSITION_LIMIT = void 0;
|
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
18
|
+
const bn_js_1 = __importDefault(require("bn.js"));
|
|
19
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
20
|
+
const instructions_1 = require("../idl_codegen/instructions");
|
|
21
|
+
const utils_1 = require("../utils");
|
|
22
|
+
const obligation_1 = require("./obligation");
|
|
23
|
+
const types_1 = require("../idl_codegen/types");
|
|
24
|
+
const farms_sdk_1 = require("@hubbleprotocol/farms-sdk");
|
|
25
|
+
const ObligationType_1 = require("../utils/ObligationType");
|
|
26
|
+
const lib_1 = require("../lib");
|
|
27
|
+
exports.POSITION_LIMIT = 10;
|
|
28
|
+
exports.BORROWS_LIMIT = 5;
|
|
29
|
+
exports.DEPOSITS_LIMIT = 8;
|
|
30
|
+
const SOL_PADDING_FOR_INTEREST = new bn_js_1.default('1000000');
|
|
31
|
+
class KaminoAction {
|
|
32
|
+
constructor(kaminoMarket, owner, obligation, userTokenAccountAddress, userCollateralAccountAddress, mint, positions, amount, depositReserves, borrowReserves, reserveState, currentSlot, hostAta, secondaryMint, additionalTokenAccountAddress, outflowReserveState, outflowAmount, referrer, payer) {
|
|
33
|
+
this.obligation = null;
|
|
34
|
+
/**
|
|
35
|
+
* Null unless the obligation is not passed
|
|
36
|
+
*/
|
|
37
|
+
this.obligationType = null;
|
|
38
|
+
if (obligation instanceof obligation_1.KaminoObligation) {
|
|
39
|
+
this.obligation = obligation;
|
|
40
|
+
}
|
|
41
|
+
else if (obligation !== null) {
|
|
42
|
+
this.obligationType = obligation;
|
|
43
|
+
}
|
|
44
|
+
this.kaminoMarket = kaminoMarket;
|
|
45
|
+
this.owner = owner;
|
|
46
|
+
this.payer = payer !== null && payer !== void 0 ? payer : owner;
|
|
47
|
+
this.amount = new bn_js_1.default(amount);
|
|
48
|
+
this.mint = mint;
|
|
49
|
+
this.positions = positions;
|
|
50
|
+
this.hostAta = hostAta;
|
|
51
|
+
this.userTokenAccountAddress = userTokenAccountAddress;
|
|
52
|
+
this.userCollateralAccountAddress = userCollateralAccountAddress;
|
|
53
|
+
this.setupIxs = [];
|
|
54
|
+
this.setupIxsLabels = [];
|
|
55
|
+
this.inBetweenIxs = [];
|
|
56
|
+
this.inBetweenIxsLabels = [];
|
|
57
|
+
this.lendingIxs = [];
|
|
58
|
+
this.lendingIxsLabels = [];
|
|
59
|
+
this.cleanupIxs = [];
|
|
60
|
+
this.cleanupIxsLabels = [];
|
|
61
|
+
this.preTxnIxs = [];
|
|
62
|
+
this.preTxnIxsLabels = [];
|
|
63
|
+
this.postTxnIxs = [];
|
|
64
|
+
this.postTxnIxsLabels = [];
|
|
65
|
+
this.refreshFarmsCleanupTxnIxs = [];
|
|
66
|
+
this.refreshFarmsCleanupTxnIxsLabels = [];
|
|
67
|
+
this.depositReserves = depositReserves;
|
|
68
|
+
this.borrowReserves = borrowReserves;
|
|
69
|
+
this.additionalTokenAccountAddress = additionalTokenAccountAddress;
|
|
70
|
+
this.secondaryMint = secondaryMint;
|
|
71
|
+
this.reserve = reserveState;
|
|
72
|
+
this.outflowReserve = outflowReserveState;
|
|
73
|
+
this.outflowAmount = outflowAmount ? new bn_js_1.default(outflowAmount) : undefined;
|
|
74
|
+
this.preLoadedDepositReservesSameTx = [];
|
|
75
|
+
this.preLoadedBorrowReservesSameTx = [];
|
|
76
|
+
this.referrer = referrer ? referrer : web3_js_1.PublicKey.default;
|
|
77
|
+
this.currentSlot = currentSlot;
|
|
78
|
+
}
|
|
79
|
+
static initialize(action, amount, mint, owner, kaminoMarket, obligation, referrer = web3_js_1.PublicKey.default, currentSlot = 0, hostAta, payer) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
const reserve = kaminoMarket.getReserveByMint(mint);
|
|
82
|
+
if (reserve === undefined) {
|
|
83
|
+
throw new Error(`Reserve ${mint} not found in market ${kaminoMarket.getAddress().toBase58()}`);
|
|
84
|
+
}
|
|
85
|
+
const { userTokenAccountAddress, userCollateralAccountAddress } = yield KaminoAction.getUserAccountAddresses(payer !== null && payer !== void 0 ? payer : owner, reserve.state);
|
|
86
|
+
const { kaminoObligation, depositReserves, borrowReserves, distinctReserveCount } = yield KaminoAction.loadObligation(action, kaminoMarket, owner, reserve.address, obligation);
|
|
87
|
+
const [_, userMetadata] = yield kaminoMarket.getUserMetadata(owner);
|
|
88
|
+
if (userMetadata) {
|
|
89
|
+
referrer = userMetadata.referrer;
|
|
90
|
+
}
|
|
91
|
+
const referrerKey = kaminoObligation ? kaminoObligation.state.referrer : referrer;
|
|
92
|
+
return new KaminoAction(kaminoMarket, owner, kaminoObligation || obligation, userTokenAccountAddress, userCollateralAccountAddress, mint, distinctReserveCount, amount, depositReserves, borrowReserves, reserve, currentSlot, hostAta, undefined, undefined, undefined, undefined, referrerKey, payer);
|
|
93
|
+
});
|
|
94
|
+
}
|
|
95
|
+
static getUserAccountAddresses(owner, reserve) {
|
|
96
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
97
|
+
const userTokenAccountAddress = yield spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, reserve.liquidity.mintPubkey, owner, true);
|
|
98
|
+
const userCollateralAccountAddress = yield spl_token_1.Token.getAssociatedTokenAddress(spl_token_1.ASSOCIATED_TOKEN_PROGRAM_ID, spl_token_1.TOKEN_PROGRAM_ID, reserve.collateral.mintPubkey, owner, true);
|
|
99
|
+
return { userTokenAccountAddress, userCollateralAccountAddress };
|
|
100
|
+
});
|
|
101
|
+
}
|
|
102
|
+
static loadObligation(action, kaminoMarket, owner, reserve, obligation, outflowReserve) {
|
|
103
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
104
|
+
let kaminoObligation;
|
|
105
|
+
const depositReserves = [];
|
|
106
|
+
const borrowReserves = [];
|
|
107
|
+
if (obligation instanceof obligation_1.KaminoObligation) {
|
|
108
|
+
kaminoObligation = obligation;
|
|
109
|
+
}
|
|
110
|
+
else {
|
|
111
|
+
const obligationAddress = obligation.toPda(kaminoMarket.getAddress(), owner);
|
|
112
|
+
kaminoObligation = yield obligation_1.KaminoObligation.load(kaminoMarket, obligationAddress);
|
|
113
|
+
}
|
|
114
|
+
if (kaminoObligation !== null) {
|
|
115
|
+
depositReserves.push(...[...kaminoObligation.deposits.keys()]);
|
|
116
|
+
borrowReserves.push(...[...kaminoObligation.borrows.keys()]);
|
|
117
|
+
}
|
|
118
|
+
if (!outflowReserve && action === 'depositAndBorrow') {
|
|
119
|
+
throw new Error(`Outflow reserve has not been set for depositAndBorrow`);
|
|
120
|
+
}
|
|
121
|
+
// Union of addresses
|
|
122
|
+
const distinctReserveCount = [
|
|
123
|
+
...new Set([
|
|
124
|
+
...borrowReserves.map((e) => e.toBase58()),
|
|
125
|
+
...(action === 'borrow' ? [reserve.toBase58()] : []),
|
|
126
|
+
...(action === 'depositAndBorrow' ? [reserve.toBase58()] : []),
|
|
127
|
+
]),
|
|
128
|
+
].length +
|
|
129
|
+
[
|
|
130
|
+
...new Set([
|
|
131
|
+
...depositReserves.map((e) => e.toBase58()),
|
|
132
|
+
...(action === 'deposit' ? [reserve.toBase58()] : []),
|
|
133
|
+
...(action === 'depositAndBorrow' ? [outflowReserve.toBase58()] : []),
|
|
134
|
+
]),
|
|
135
|
+
].length;
|
|
136
|
+
if (distinctReserveCount > exports.POSITION_LIMIT) {
|
|
137
|
+
throw Error(`Obligation already has max number of positions: ${exports.POSITION_LIMIT}`);
|
|
138
|
+
}
|
|
139
|
+
return {
|
|
140
|
+
kaminoObligation,
|
|
141
|
+
depositReserves,
|
|
142
|
+
borrowReserves,
|
|
143
|
+
distinctReserveCount,
|
|
144
|
+
};
|
|
145
|
+
});
|
|
146
|
+
}
|
|
147
|
+
static buildRefreshObligationTxns(kaminoMarket, payer, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
148
|
+
currentSlot = 0) {
|
|
149
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
150
|
+
// placeholder for action initialization
|
|
151
|
+
const firstReserve = obligation.state.deposits[0].depositReserve;
|
|
152
|
+
const firstKaminoReserve = kaminoMarket.getReserveByAddress(firstReserve);
|
|
153
|
+
if (!firstKaminoReserve) {
|
|
154
|
+
throw new Error(`Reserve ${firstReserve.toBase58()} not found`);
|
|
155
|
+
}
|
|
156
|
+
const axn = yield KaminoAction.initialize('refreshObligation', '0', firstKaminoReserve === null || firstKaminoReserve === void 0 ? void 0 : firstKaminoReserve.getLiquidityMint(), obligation.state.owner, kaminoMarket, obligation, kaminoMarket.programId, currentSlot);
|
|
157
|
+
if (extraComputeBudget > 0) {
|
|
158
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
159
|
+
}
|
|
160
|
+
axn.addRefreshObligation(payer);
|
|
161
|
+
return axn;
|
|
162
|
+
});
|
|
163
|
+
}
|
|
164
|
+
static buildDepositTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
165
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas,
|
|
166
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
|
|
167
|
+
referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
|
|
168
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
169
|
+
const axn = yield KaminoAction.initialize('deposit', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
|
|
170
|
+
const addInitObligationForFarm = true;
|
|
171
|
+
if (extraComputeBudget > 0) {
|
|
172
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
173
|
+
}
|
|
174
|
+
yield axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm);
|
|
175
|
+
axn.addDepositIx();
|
|
176
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
177
|
+
return axn;
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
static buildBorrowTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
181
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas,
|
|
182
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
|
|
183
|
+
referrer = web3_js_1.PublicKey.default, currentSlot = 0, hostAta) {
|
|
184
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
185
|
+
const axn = yield KaminoAction.initialize('borrow', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot, hostAta);
|
|
186
|
+
const addInitObligationForFarm = true;
|
|
187
|
+
if (extraComputeBudget > 0) {
|
|
188
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
189
|
+
}
|
|
190
|
+
yield axn.addSupportIxs('borrow', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm);
|
|
191
|
+
axn.addBorrowIx();
|
|
192
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
193
|
+
return axn;
|
|
194
|
+
});
|
|
195
|
+
}
|
|
196
|
+
static buildDepositReserveLiquidityTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
197
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas
|
|
198
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
|
|
199
|
+
referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
|
|
200
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
201
|
+
const axn = yield KaminoAction.initialize('mint', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
|
|
202
|
+
const addInitObligationForFarm = true;
|
|
203
|
+
if (extraComputeBudget > 0) {
|
|
204
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
205
|
+
}
|
|
206
|
+
yield axn.addSupportIxs('mint', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm);
|
|
207
|
+
axn.addDepositReserveLiquidityIx();
|
|
208
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
209
|
+
return axn;
|
|
210
|
+
});
|
|
211
|
+
}
|
|
212
|
+
static buildRedeemReserveCollateralTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
213
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas
|
|
214
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
|
|
215
|
+
referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
|
|
216
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
217
|
+
const axn = yield KaminoAction.initialize('redeem', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
|
|
218
|
+
const addInitObligationForFarm = true;
|
|
219
|
+
if (extraComputeBudget > 0) {
|
|
220
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
221
|
+
}
|
|
222
|
+
yield axn.addSupportIxs('redeem', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm);
|
|
223
|
+
axn.addRedeemReserveCollateralIx();
|
|
224
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
225
|
+
return axn;
|
|
226
|
+
});
|
|
227
|
+
}
|
|
228
|
+
static buildDepositObligationCollateralTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
229
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas
|
|
230
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
|
|
231
|
+
referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
|
|
232
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
233
|
+
const axn = yield KaminoAction.initialize('depositCollateral', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
|
|
234
|
+
const addInitObligationForFarm = true;
|
|
235
|
+
if (extraComputeBudget > 0) {
|
|
236
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
237
|
+
}
|
|
238
|
+
yield axn.addSupportIxs('depositCollateral', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm);
|
|
239
|
+
axn.addDepositObligationCollateralIx();
|
|
240
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
241
|
+
return axn;
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
static buildDepositAndBorrowTxns(kaminoMarket, depositAmount, depositMint, borrowAmount, borrowMint, payer, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
245
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas,
|
|
246
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
|
|
247
|
+
referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
|
|
248
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
249
|
+
const axn = yield KaminoAction.initializeMultiTokenAction(kaminoMarket, 'depositAndBorrow', depositAmount, depositMint, borrowMint, payer, payer, obligation, borrowAmount, referrer, currentSlot);
|
|
250
|
+
const addInitObligationForFarmForDeposit = true;
|
|
251
|
+
const addInitObligationForFarmForBorrow = false;
|
|
252
|
+
const twoTokenAction = true;
|
|
253
|
+
if (extraComputeBudget > 0) {
|
|
254
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
255
|
+
}
|
|
256
|
+
yield axn.addSupportIxs('deposit', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForDeposit, twoTokenAction);
|
|
257
|
+
yield axn.addDepositAndBorrowIx();
|
|
258
|
+
yield axn.addInBetweenIxs('depositAndBorrow', includeAtaIxns, requestElevationGroup, addInitObligationForFarmForBorrow);
|
|
259
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
260
|
+
return axn;
|
|
261
|
+
});
|
|
262
|
+
}
|
|
263
|
+
static buildRepayAndWithdrawTxns(kaminoMarket, repayAmount, repayMint, withdrawAmount, withdrawMint, payer, currentSlot, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
264
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas,
|
|
265
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata,
|
|
266
|
+
isClosingPosition = false, referrer = web3_js_1.PublicKey.default) {
|
|
267
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
268
|
+
const axn = yield KaminoAction.initializeMultiTokenAction(kaminoMarket, 'repayAndWithdraw', repayAmount, repayMint, withdrawMint, payer, payer, obligation, withdrawAmount, referrer, currentSlot);
|
|
269
|
+
const addInitObligationForFarmForRepay = true;
|
|
270
|
+
const addInitObligationForFarmForWithdraw = false;
|
|
271
|
+
const twoTokenAction = true;
|
|
272
|
+
if (extraComputeBudget > 0) {
|
|
273
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
274
|
+
}
|
|
275
|
+
yield axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarmForRepay, twoTokenAction);
|
|
276
|
+
yield axn.addRepayAndWithdrawIxs();
|
|
277
|
+
yield axn.addInBetweenIxs('repayAndWithdraw', includeAtaIxns, requestElevationGroup, addInitObligationForFarmForWithdraw, isClosingPosition);
|
|
278
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
279
|
+
return axn;
|
|
280
|
+
});
|
|
281
|
+
}
|
|
282
|
+
static buildWithdrawTxns(kaminoMarket, amount, mint, owner, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
283
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas,
|
|
284
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
|
|
285
|
+
referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
|
|
286
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
287
|
+
const axn = yield KaminoAction.initialize('withdraw', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot);
|
|
288
|
+
const addInitObligationForFarm = true;
|
|
289
|
+
if (extraComputeBudget > 0) {
|
|
290
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
291
|
+
}
|
|
292
|
+
yield axn.addSupportIxs('withdraw', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm);
|
|
293
|
+
yield axn.addWithdrawIx();
|
|
294
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
295
|
+
return axn;
|
|
296
|
+
});
|
|
297
|
+
}
|
|
298
|
+
/**
|
|
299
|
+
*
|
|
300
|
+
* @param kaminoMarket
|
|
301
|
+
* @param amount
|
|
302
|
+
* @param mint
|
|
303
|
+
* @param owner
|
|
304
|
+
* @param obligation - obligation to repay or the PDA seeds
|
|
305
|
+
* @param currentSlot
|
|
306
|
+
* @param payer - if not set then owner is used
|
|
307
|
+
* @param extraComputeBudget - if > 0 then adds the ixn
|
|
308
|
+
* @param includeAtaIxns - if true it includes create and close wsol and token atas
|
|
309
|
+
* @param requestElevationGroup
|
|
310
|
+
* @param includeUserMetadata - if true it includes user metadata
|
|
311
|
+
* @param referrer
|
|
312
|
+
*/
|
|
313
|
+
static buildRepayTxns(kaminoMarket, amount, mint, owner, obligation, currentSlot, payer = undefined, extraComputeBudget = 1000000, includeAtaIxns = true, requestElevationGroup = false, includeUserMetadata = true, referrer = web3_js_1.PublicKey.default) {
|
|
314
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
315
|
+
const axn = yield KaminoAction.initialize('repay', amount, mint, owner, kaminoMarket, obligation, referrer, currentSlot, undefined, payer);
|
|
316
|
+
const addInitObligationForFarm = true;
|
|
317
|
+
if (extraComputeBudget > 0) {
|
|
318
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
319
|
+
}
|
|
320
|
+
yield axn.addSupportIxs('repay', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm);
|
|
321
|
+
yield axn.addRepayIx();
|
|
322
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
323
|
+
return axn;
|
|
324
|
+
});
|
|
325
|
+
}
|
|
326
|
+
static buildLiquidateTxns(kaminoMarket, amount, minCollateralReceiveAmount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, extraComputeBudget = 1000000, // if > 0 then adds the ixn
|
|
327
|
+
includeAtaIxns = true, // if true it includes create and close wsol and token atas, and creates all other token atas if they don't exist
|
|
328
|
+
requestElevationGroup = false, includeUserMetadata = true, // if true it includes user metadata
|
|
329
|
+
referrer = web3_js_1.PublicKey.default, maxAllowedLtvOverridePercent = 0, currentSlot = 0) {
|
|
330
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
331
|
+
const axn = yield KaminoAction.initializeMultiTokenAction(kaminoMarket, 'liquidate', amount, repayTokenMint, withdrawTokenMint, liquidator, obligationOwner, obligation, minCollateralReceiveAmount, referrer, currentSlot);
|
|
332
|
+
const addInitObligationForFarm = true;
|
|
333
|
+
if (extraComputeBudget > 0) {
|
|
334
|
+
axn.addComputeBudgetIxn(extraComputeBudget);
|
|
335
|
+
}
|
|
336
|
+
yield axn.addSupportIxs('liquidate', includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm);
|
|
337
|
+
yield axn.addLiquidateIx(maxAllowedLtvOverridePercent);
|
|
338
|
+
axn.addRefreshFarmsCleanupTxnIxsToCleanupIxs();
|
|
339
|
+
return axn;
|
|
340
|
+
});
|
|
341
|
+
}
|
|
342
|
+
static buildWithdrawReferrerFeeTxns(owner, tokenMint, kaminoMarket, currentSlot = 0) {
|
|
343
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
344
|
+
const { axn, createAtasIxns } = yield KaminoAction.initializeWithdrawReferrerFees(tokenMint, owner, kaminoMarket, currentSlot);
|
|
345
|
+
axn.preTxnIxs.push(...createAtasIxns);
|
|
346
|
+
axn.preTxnIxsLabels.push(`createAtasIxs[${axn.userTokenAccountAddress.toString()}]`);
|
|
347
|
+
axn.addRefreshReserveIxs([axn.reserve.address]);
|
|
348
|
+
axn.addWithdrawReferrerFeesIxs();
|
|
349
|
+
return axn;
|
|
350
|
+
});
|
|
351
|
+
}
|
|
352
|
+
getTransactions() {
|
|
353
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
354
|
+
const txns = {
|
|
355
|
+
preLendingTxn: null,
|
|
356
|
+
lendingTxn: null,
|
|
357
|
+
postLendingTxn: null,
|
|
358
|
+
};
|
|
359
|
+
if (this.preTxnIxs.length) {
|
|
360
|
+
txns.preLendingTxn = new web3_js_1.Transaction({
|
|
361
|
+
feePayer: this.owner,
|
|
362
|
+
recentBlockhash: (yield this.kaminoMarket.getConnection().getLatestBlockhash()).blockhash,
|
|
363
|
+
}).add(...this.preTxnIxs);
|
|
364
|
+
}
|
|
365
|
+
if (this.lendingIxs.length === 2) {
|
|
366
|
+
txns.lendingTxn = new web3_js_1.Transaction({
|
|
367
|
+
feePayer: this.owner,
|
|
368
|
+
recentBlockhash: (yield this.kaminoMarket.getConnection().getLatestBlockhash()).blockhash,
|
|
369
|
+
}).add(...this.setupIxs, ...[this.lendingIxs[0]], ...this.inBetweenIxs, ...[this.lendingIxs[1]], ...this.cleanupIxs);
|
|
370
|
+
}
|
|
371
|
+
else {
|
|
372
|
+
txns.lendingTxn = new web3_js_1.Transaction({
|
|
373
|
+
feePayer: this.owner,
|
|
374
|
+
recentBlockhash: (yield this.kaminoMarket.getConnection().getLatestBlockhash()).blockhash,
|
|
375
|
+
}).add(...this.setupIxs, ...this.lendingIxs, ...this.cleanupIxs);
|
|
376
|
+
}
|
|
377
|
+
if (this.postTxnIxs.length) {
|
|
378
|
+
txns.postLendingTxn = new web3_js_1.Transaction({
|
|
379
|
+
feePayer: this.owner,
|
|
380
|
+
recentBlockhash: (yield this.kaminoMarket.getConnection().getLatestBlockhash()).blockhash,
|
|
381
|
+
}).add(...this.postTxnIxs);
|
|
382
|
+
}
|
|
383
|
+
return txns;
|
|
384
|
+
});
|
|
385
|
+
}
|
|
386
|
+
sendTransactions(sendTransaction) {
|
|
387
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
388
|
+
const txns = yield this.getTransactions();
|
|
389
|
+
yield this.sendSingleTransaction(txns.preLendingTxn, sendTransaction);
|
|
390
|
+
const signature = yield this.sendSingleTransaction(txns.lendingTxn, sendTransaction);
|
|
391
|
+
yield this.sendSingleTransaction(txns.postLendingTxn, sendTransaction);
|
|
392
|
+
return signature;
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
sendSingleTransaction(txn, sendTransaction) {
|
|
396
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
397
|
+
if (!txn)
|
|
398
|
+
return '';
|
|
399
|
+
const signature = yield sendTransaction(txn, this.kaminoMarket.getConnection());
|
|
400
|
+
yield this.kaminoMarket.getConnection().confirmTransaction(signature);
|
|
401
|
+
return signature;
|
|
402
|
+
});
|
|
403
|
+
}
|
|
404
|
+
simulateTransactions(sendTransaction) {
|
|
405
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
406
|
+
const txns = yield this.getTransactions();
|
|
407
|
+
yield this.simulateSingleTransaction(txns.preLendingTxn, sendTransaction);
|
|
408
|
+
const signature = yield this.simulateSingleTransaction(txns.lendingTxn, sendTransaction);
|
|
409
|
+
yield this.simulateSingleTransaction(txns.postLendingTxn, sendTransaction);
|
|
410
|
+
return signature;
|
|
411
|
+
});
|
|
412
|
+
}
|
|
413
|
+
simulateSingleTransaction(txn, sendTransaction) {
|
|
414
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
415
|
+
if (!txn)
|
|
416
|
+
return '';
|
|
417
|
+
return yield sendTransaction(txn, this.kaminoMarket.getConnection());
|
|
418
|
+
});
|
|
419
|
+
}
|
|
420
|
+
addDepositIx() {
|
|
421
|
+
this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateral`);
|
|
422
|
+
this.lendingIxs.push((0, instructions_1.depositReserveLiquidityAndObligationCollateral)({
|
|
423
|
+
liquidityAmount: this.amount,
|
|
424
|
+
}, {
|
|
425
|
+
owner: this.owner,
|
|
426
|
+
obligation: this.getObligationPda(),
|
|
427
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
428
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
429
|
+
reserve: this.reserve.address,
|
|
430
|
+
reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
|
|
431
|
+
reserveCollateralMint: this.reserve.getCTokenMint(),
|
|
432
|
+
reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault,
|
|
433
|
+
userSourceLiquidity: this.userTokenAccountAddress,
|
|
434
|
+
placeholderUserDestinationCollateral: this.kaminoMarket.programId,
|
|
435
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
436
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
437
|
+
}, this.kaminoMarket.programId));
|
|
438
|
+
}
|
|
439
|
+
addDepositReserveLiquidityIx() {
|
|
440
|
+
this.lendingIxsLabels.push(`depositReserveLiquidity`);
|
|
441
|
+
this.lendingIxs.push((0, instructions_1.depositReserveLiquidity)({
|
|
442
|
+
liquidityAmount: this.amount,
|
|
443
|
+
}, {
|
|
444
|
+
owner: this.owner,
|
|
445
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
446
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
447
|
+
reserve: this.reserve.address,
|
|
448
|
+
reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
|
|
449
|
+
reserveCollateralMint: this.reserve.getCTokenMint(),
|
|
450
|
+
userSourceLiquidity: this.userTokenAccountAddress,
|
|
451
|
+
userDestinationCollateral: this.userCollateralAccountAddress,
|
|
452
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
453
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
454
|
+
}, this.kaminoMarket.programId));
|
|
455
|
+
}
|
|
456
|
+
addRedeemReserveCollateralIx() {
|
|
457
|
+
this.lendingIxsLabels.push(`redeemReserveCollateral`);
|
|
458
|
+
this.lendingIxs.push((0, instructions_1.redeemReserveCollateral)({
|
|
459
|
+
collateralAmount: this.amount,
|
|
460
|
+
}, {
|
|
461
|
+
owner: this.owner,
|
|
462
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
463
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
464
|
+
reserve: this.reserve.address,
|
|
465
|
+
reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
|
|
466
|
+
reserveCollateralMint: this.reserve.getCTokenMint(),
|
|
467
|
+
userSourceCollateral: this.userCollateralAccountAddress,
|
|
468
|
+
userDestinationLiquidity: this.userTokenAccountAddress,
|
|
469
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
470
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
471
|
+
}, this.kaminoMarket.programId));
|
|
472
|
+
}
|
|
473
|
+
addDepositObligationCollateralIx() {
|
|
474
|
+
this.lendingIxsLabels.push(`depositObligationCollateral`);
|
|
475
|
+
this.lendingIxs.push((0, instructions_1.depositObligationCollateral)({
|
|
476
|
+
collateralAmount: this.amount,
|
|
477
|
+
}, {
|
|
478
|
+
owner: this.owner,
|
|
479
|
+
obligation: this.getObligationPda(),
|
|
480
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
481
|
+
depositReserve: this.reserve.address,
|
|
482
|
+
reserveDestinationCollateral: this.reserve.state.collateral.supplyVault,
|
|
483
|
+
userSourceCollateral: this.userCollateralAccountAddress,
|
|
484
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
485
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
486
|
+
}, this.kaminoMarket.programId));
|
|
487
|
+
}
|
|
488
|
+
addBorrowIx() {
|
|
489
|
+
this.lendingIxsLabels.push(`borrowObligationLiquidity`);
|
|
490
|
+
this.lendingIxs.push((0, instructions_1.borrowObligationLiquidity)({
|
|
491
|
+
liquidityAmount: this.amount,
|
|
492
|
+
}, {
|
|
493
|
+
owner: this.owner,
|
|
494
|
+
obligation: this.getObligationPda(),
|
|
495
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
496
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
497
|
+
borrowReserve: this.reserve.address,
|
|
498
|
+
reserveSourceLiquidity: this.reserve.state.liquidity.supplyVault,
|
|
499
|
+
userDestinationLiquidity: this.userTokenAccountAddress,
|
|
500
|
+
borrowReserveLiquidityFeeReceiver: this.reserve.state.liquidity.feeVault,
|
|
501
|
+
referrerTokenState: (0, utils_1.referrerTokenStatePda)(this.referrer, this.reserve.address, this.kaminoMarket.programId)[0],
|
|
502
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
503
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
504
|
+
}, this.kaminoMarket.programId));
|
|
505
|
+
}
|
|
506
|
+
addDepositAndBorrowIx() {
|
|
507
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
508
|
+
this.lendingIxsLabels.push(`depositReserveLiquidityAndObligationCollateral`);
|
|
509
|
+
this.lendingIxsLabels.push(`borrowObligationLiquidity`);
|
|
510
|
+
this.lendingIxs.push((0, instructions_1.depositReserveLiquidityAndObligationCollateral)({
|
|
511
|
+
liquidityAmount: this.amount,
|
|
512
|
+
}, {
|
|
513
|
+
owner: this.owner,
|
|
514
|
+
obligation: this.getObligationPda(),
|
|
515
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
516
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
517
|
+
reserve: this.reserve.address,
|
|
518
|
+
reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
|
|
519
|
+
reserveCollateralMint: this.reserve.getCTokenMint(),
|
|
520
|
+
reserveDestinationDepositCollateral: this.reserve.state.collateral.supplyVault,
|
|
521
|
+
userSourceLiquidity: this.userTokenAccountAddress,
|
|
522
|
+
placeholderUserDestinationCollateral: this.kaminoMarket.programId,
|
|
523
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
524
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
525
|
+
}, this.kaminoMarket.programId));
|
|
526
|
+
if (!this.outflowReserve) {
|
|
527
|
+
throw new Error(`outflowReserve not set`);
|
|
528
|
+
}
|
|
529
|
+
if (!this.additionalTokenAccountAddress) {
|
|
530
|
+
throw new Error(`additionalTokenAccountAddress not set`);
|
|
531
|
+
}
|
|
532
|
+
if (!this.outflowAmount) {
|
|
533
|
+
throw new Error(`outflowAmount not set`);
|
|
534
|
+
}
|
|
535
|
+
this.lendingIxs.push((0, instructions_1.borrowObligationLiquidity)({
|
|
536
|
+
liquidityAmount: this.outflowAmount,
|
|
537
|
+
}, {
|
|
538
|
+
owner: this.owner,
|
|
539
|
+
obligation: this.getObligationPda(),
|
|
540
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
541
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
542
|
+
borrowReserve: this.outflowReserve.address,
|
|
543
|
+
reserveSourceLiquidity: this.outflowReserve.state.liquidity.supplyVault,
|
|
544
|
+
userDestinationLiquidity: this.additionalTokenAccountAddress,
|
|
545
|
+
borrowReserveLiquidityFeeReceiver: this.outflowReserve.state.liquidity.feeVault,
|
|
546
|
+
referrerTokenState: (0, utils_1.referrerTokenStatePda)(this.referrer, this.outflowReserve.address, this.kaminoMarket.programId)[0],
|
|
547
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
548
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
549
|
+
}, this.kaminoMarket.programId));
|
|
550
|
+
});
|
|
551
|
+
}
|
|
552
|
+
addRepayAndWithdrawIxs() {
|
|
553
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
554
|
+
this.lendingIxsLabels.push(`repayObligationLiquidity(reserve=${this.reserve.address})(obligation=${this.getObligationPda()})`);
|
|
555
|
+
this.lendingIxsLabels.push(`withdrawObligationCollateralAndRedeemReserveCollateral`);
|
|
556
|
+
this.lendingIxs.push((0, instructions_1.repayObligationLiquidity)({
|
|
557
|
+
liquidityAmount: this.amount,
|
|
558
|
+
}, {
|
|
559
|
+
owner: this.owner,
|
|
560
|
+
obligation: this.getObligationPda(),
|
|
561
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
562
|
+
repayReserve: this.reserve.address,
|
|
563
|
+
userSourceLiquidity: this.userTokenAccountAddress,
|
|
564
|
+
reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
|
|
565
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
566
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
567
|
+
}, this.kaminoMarket.programId));
|
|
568
|
+
if (!this.outflowReserve) {
|
|
569
|
+
throw new Error(`outflowReserve not set`);
|
|
570
|
+
}
|
|
571
|
+
if (!this.additionalTokenAccountAddress) {
|
|
572
|
+
throw new Error(`additionalTokenAccountAddress not set`);
|
|
573
|
+
}
|
|
574
|
+
if (!this.outflowAmount) {
|
|
575
|
+
throw new Error(`outflowAmount not set`);
|
|
576
|
+
}
|
|
577
|
+
const collateralExchangeRate = this.outflowReserve.getEstimatedCollateralExchangeRate(this.currentSlot, this.kaminoMarket.state.referralFeeBps);
|
|
578
|
+
this.lendingIxs.push((0, instructions_1.withdrawObligationCollateralAndRedeemReserveCollateral)({
|
|
579
|
+
collateralAmount: this.outflowAmount.eq(new bn_js_1.default(utils_1.U64_MAX))
|
|
580
|
+
? this.outflowAmount
|
|
581
|
+
: new bn_js_1.default(new decimal_js_1.default(this.outflowAmount.toString()).mul(collateralExchangeRate).ceil().toString()),
|
|
582
|
+
}, {
|
|
583
|
+
owner: this.owner,
|
|
584
|
+
obligation: this.getObligationPda(),
|
|
585
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
586
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
587
|
+
withdrawReserve: this.outflowReserve.address,
|
|
588
|
+
reserveCollateralMint: this.outflowReserve.getCTokenMint(),
|
|
589
|
+
reserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
|
|
590
|
+
reserveSourceCollateral: this.outflowReserve.state.collateral.supplyVault,
|
|
591
|
+
userDestinationLiquidity: this.additionalTokenAccountAddress,
|
|
592
|
+
placeholderUserDestinationCollateral: this.kaminoMarket.programId,
|
|
593
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
594
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
595
|
+
}, this.kaminoMarket.programId));
|
|
596
|
+
});
|
|
597
|
+
}
|
|
598
|
+
addWithdrawIx() {
|
|
599
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
600
|
+
const collateralExchangeRate = this.reserve.getEstimatedCollateralExchangeRate(this.currentSlot, this.kaminoMarket.state.referralFeeBps);
|
|
601
|
+
const collateralAmount = this.amount.eq(new bn_js_1.default(utils_1.U64_MAX))
|
|
602
|
+
? this.amount
|
|
603
|
+
: new bn_js_1.default(new decimal_js_1.default(this.amount.toString()).mul(collateralExchangeRate).ceil().toString());
|
|
604
|
+
this.lendingIxsLabels.push(`withdrawObligationCollateralAndRedeemReserveCollateral`);
|
|
605
|
+
this.lendingIxs.push((0, instructions_1.withdrawObligationCollateralAndRedeemReserveCollateral)({
|
|
606
|
+
collateralAmount,
|
|
607
|
+
}, {
|
|
608
|
+
owner: this.owner,
|
|
609
|
+
obligation: this.getObligationPda(),
|
|
610
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
611
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
612
|
+
withdrawReserve: this.reserve.address,
|
|
613
|
+
reserveCollateralMint: this.reserve.getCTokenMint(),
|
|
614
|
+
reserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
|
|
615
|
+
reserveSourceCollateral: this.reserve.state.collateral.supplyVault,
|
|
616
|
+
userDestinationLiquidity: this.userTokenAccountAddress,
|
|
617
|
+
placeholderUserDestinationCollateral: this.kaminoMarket.programId,
|
|
618
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
619
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
620
|
+
}, this.kaminoMarket.programId));
|
|
621
|
+
});
|
|
622
|
+
}
|
|
623
|
+
addRepayIx() {
|
|
624
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
625
|
+
this.lendingIxsLabels.push(`repayObligationLiquidity(reserve=${this.reserve.address})(obligation=${this.getObligationPda()})`);
|
|
626
|
+
this.lendingIxs.push((0, instructions_1.repayObligationLiquidity)({
|
|
627
|
+
liquidityAmount: this.amount,
|
|
628
|
+
}, {
|
|
629
|
+
owner: this.payer,
|
|
630
|
+
obligation: this.getObligationPda(),
|
|
631
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
632
|
+
repayReserve: this.reserve.address,
|
|
633
|
+
userSourceLiquidity: this.userTokenAccountAddress,
|
|
634
|
+
reserveDestinationLiquidity: this.reserve.state.liquidity.supplyVault,
|
|
635
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
636
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
637
|
+
}, this.kaminoMarket.programId));
|
|
638
|
+
});
|
|
639
|
+
}
|
|
640
|
+
addLiquidateIx(maxAllowedLtvOverridePercent = 0) {
|
|
641
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
642
|
+
this.lendingIxsLabels.push(`liquidateObligationAndRedeemReserveCollateral`);
|
|
643
|
+
if (!this.outflowReserve) {
|
|
644
|
+
throw Error(`Withdraw reserve during liquidation is not defined`);
|
|
645
|
+
}
|
|
646
|
+
if (!this.additionalTokenAccountAddress) {
|
|
647
|
+
throw Error(`Liquidating token account address is not defined`);
|
|
648
|
+
}
|
|
649
|
+
this.lendingIxs.push((0, instructions_1.liquidateObligationAndRedeemReserveCollateral)({
|
|
650
|
+
liquidityAmount: this.amount,
|
|
651
|
+
// TODO: Configure this when updating liquidator with new interface
|
|
652
|
+
minAcceptableReceivedCollateralAmount: this.outflowAmount || new bn_js_1.default(0),
|
|
653
|
+
// TODO: remove this before going live
|
|
654
|
+
maxAllowedLtvOverridePercent: new bn_js_1.default(maxAllowedLtvOverridePercent),
|
|
655
|
+
}, {
|
|
656
|
+
liquidator: this.owner,
|
|
657
|
+
obligation: this.getObligationPda(),
|
|
658
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
659
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
660
|
+
repayReserve: this.reserve.address,
|
|
661
|
+
repayReserveLiquiditySupply: this.reserve.state.liquidity.supplyVault,
|
|
662
|
+
withdrawReserve: this.outflowReserve.address,
|
|
663
|
+
withdrawReserveCollateralMint: this.outflowReserve.getCTokenMint(),
|
|
664
|
+
withdrawReserveCollateralSupply: this.outflowReserve.state.collateral.supplyVault,
|
|
665
|
+
withdrawReserveLiquiditySupply: this.outflowReserve.state.liquidity.supplyVault,
|
|
666
|
+
userSourceLiquidity: this.additionalTokenAccountAddress,
|
|
667
|
+
userDestinationCollateral: this.userCollateralAccountAddress,
|
|
668
|
+
userDestinationLiquidity: this.userTokenAccountAddress,
|
|
669
|
+
withdrawReserveLiquidityFeeReceiver: this.outflowReserve.state.liquidity.feeVault,
|
|
670
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
671
|
+
instructionSysvarAccount: web3_js_1.SYSVAR_INSTRUCTIONS_PUBKEY,
|
|
672
|
+
}, this.kaminoMarket.programId));
|
|
673
|
+
});
|
|
674
|
+
}
|
|
675
|
+
addInBetweenIxs(action, includeAtaIxns, requestElevationGroup, addInitObligationForFarm, isClosingPosition = false) {
|
|
676
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
677
|
+
yield this.addSupportIxsWithoutInitObligation(action, includeAtaIxns, false, requestElevationGroup, addInitObligationForFarm, isClosingPosition);
|
|
678
|
+
});
|
|
679
|
+
}
|
|
680
|
+
addRefreshObligation(crank) {
|
|
681
|
+
const uniqueReserveAddresses = new utils_1.PublicKeySet(this.depositReserves.concat(this.borrowReserves)).toArray();
|
|
682
|
+
const addAllToSetupIxns = true;
|
|
683
|
+
// Union of addresses
|
|
684
|
+
const allReservesExcludingCurrent = [...uniqueReserveAddresses];
|
|
685
|
+
this.addRefreshReserveIxs(allReservesExcludingCurrent, addAllToSetupIxns);
|
|
686
|
+
this.addRefreshFarmsForReserve(this.depositReserves.map((r) => this.kaminoMarket.getReserveByAddress(r)), addAllToSetupIxns, types_1.ReserveFarmKind.Collateral, crank);
|
|
687
|
+
this.addRefreshFarmsForReserve(this.borrowReserves.map((r) => this.kaminoMarket.getReserveByAddress(r)), addAllToSetupIxns, types_1.ReserveFarmKind.Debt, crank);
|
|
688
|
+
this.addRefreshObligationIx(addAllToSetupIxns, false);
|
|
689
|
+
}
|
|
690
|
+
addSupportIxsWithoutInitObligation(action, includeAtaIxns, addToSetupIxs = true, requestElevationGroup = false, addInitObligationForFarm = false, isClosingPosition = false, twoTokenAction = false) {
|
|
691
|
+
var _a;
|
|
692
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
693
|
+
// TODO: why are we not doing this first?
|
|
694
|
+
if (includeAtaIxns) {
|
|
695
|
+
yield this.addAtaIxs(action);
|
|
696
|
+
}
|
|
697
|
+
if ([
|
|
698
|
+
'depositCollateral',
|
|
699
|
+
'deposit',
|
|
700
|
+
'withdraw',
|
|
701
|
+
'borrow',
|
|
702
|
+
'liquidate',
|
|
703
|
+
'repay',
|
|
704
|
+
'depositAndBorrow',
|
|
705
|
+
'repayAndWithdraw',
|
|
706
|
+
'refreshObligation',
|
|
707
|
+
].includes(action)) {
|
|
708
|
+
// The support ixns in order are:
|
|
709
|
+
// 0. Init obligation ixn
|
|
710
|
+
// 0. Token Ata ixns
|
|
711
|
+
// 0. Init obligation for farm
|
|
712
|
+
// 1. Ixns to refresh the reserves of the obligation not related to the current action
|
|
713
|
+
// 2. Ixn to refresh the reserve of the current action
|
|
714
|
+
// 3. Ixn to refresh the obligation
|
|
715
|
+
// 4. Ixn to refresh the `debt` farm of the obligation
|
|
716
|
+
// 5. Ixn to refresh the `collateral` farm of the obligation
|
|
717
|
+
// 6. The instruction itself
|
|
718
|
+
// 7. Ixn to refresh the `debt` farm of the obligation
|
|
719
|
+
// 8. Ixn to refresh the `collateral` farm of the obligation
|
|
720
|
+
let currentReserves = [];
|
|
721
|
+
if (action === 'liquidate' || action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
|
|
722
|
+
if (!this.outflowReserve) {
|
|
723
|
+
throw new Error('outflowReserve is undefined');
|
|
724
|
+
}
|
|
725
|
+
if (action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
|
|
726
|
+
currentReserves = [this.reserve, this.outflowReserve];
|
|
727
|
+
if (this.obligation) {
|
|
728
|
+
if (action === 'depositAndBorrow') {
|
|
729
|
+
const deposit = this.obligation.state.deposits.find((deposit) => deposit.depositReserve.equals(this.reserve.address));
|
|
730
|
+
if (!deposit) {
|
|
731
|
+
this.preLoadedDepositReservesSameTx.push(this.reserve.address);
|
|
732
|
+
}
|
|
733
|
+
}
|
|
734
|
+
else {
|
|
735
|
+
const borrow = this.obligation.state.borrows.find((borrow) => borrow.borrowReserve.equals(this.reserve.address));
|
|
736
|
+
if (!borrow) {
|
|
737
|
+
throw Error(`Unable to find obligation borrow to repay for ${this.obligation.state.owner.toBase58()}`);
|
|
738
|
+
}
|
|
739
|
+
const cumulativeBorrowRateObligation = obligation_1.KaminoObligation.getCumulativeBorrowRate(borrow);
|
|
740
|
+
const cumulativeBorrowRateReserve = this.reserve.getEstimatedCumulativeBorrowRate(this.currentSlot);
|
|
741
|
+
const fullRepay = obligation_1.KaminoObligation.getBorrowAmount(borrow)
|
|
742
|
+
.mul(cumulativeBorrowRateReserve)
|
|
743
|
+
.div(cumulativeBorrowRateObligation);
|
|
744
|
+
const amountDecimal = new decimal_js_1.default(this.amount.toString());
|
|
745
|
+
if (fullRepay.lte(amountDecimal)) {
|
|
746
|
+
this.preLoadedBorrowReservesSameTx.push(this.reserve.address);
|
|
747
|
+
}
|
|
748
|
+
}
|
|
749
|
+
}
|
|
750
|
+
else {
|
|
751
|
+
// Obligation doesn't exist yet, so we have to preload the deposit reserve
|
|
752
|
+
this.preLoadedDepositReservesSameTx.push(this.reserve.address);
|
|
753
|
+
}
|
|
754
|
+
}
|
|
755
|
+
else if (action === 'liquidate' && !this.outflowReserve.address.equals(this.reserve.address)) {
|
|
756
|
+
currentReserves = [this.outflowReserve, this.reserve];
|
|
757
|
+
}
|
|
758
|
+
else {
|
|
759
|
+
currentReserves = [this.reserve];
|
|
760
|
+
}
|
|
761
|
+
}
|
|
762
|
+
else {
|
|
763
|
+
currentReserves = [this.reserve];
|
|
764
|
+
}
|
|
765
|
+
const uniqueReserveAddresses = new utils_1.PublicKeySet(this.depositReserves.concat(this.borrowReserves));
|
|
766
|
+
const currentReserveAddresses = new utils_1.PublicKeySet(currentReserves.map((reserve) => reserve.address));
|
|
767
|
+
// Union of addresses
|
|
768
|
+
const allReservesExcludingCurrent = [...uniqueReserveAddresses.toArray()].filter((address) => !currentReserveAddresses.contains(address));
|
|
769
|
+
this.addRefreshReserveIxs(allReservesExcludingCurrent, addToSetupIxs);
|
|
770
|
+
if (addInitObligationForFarm) {
|
|
771
|
+
if (action === 'liquidate') {
|
|
772
|
+
yield this.addInitObligationForFarm(this.reserve, types_1.ReserveFarmKind.Debt, addToSetupIxs);
|
|
773
|
+
yield this.addInitObligationForFarm(this.outflowReserve, types_1.ReserveFarmKind.Collateral, addToSetupIxs);
|
|
774
|
+
}
|
|
775
|
+
else if (action === 'depositAndBorrow' ||
|
|
776
|
+
action === 'depositCollateral' ||
|
|
777
|
+
action === 'withdraw' ||
|
|
778
|
+
action === 'deposit') {
|
|
779
|
+
yield this.addInitObligationForFarm(this.reserve, types_1.ReserveFarmKind.Collateral, addToSetupIxs);
|
|
780
|
+
if (this.outflowReserve) {
|
|
781
|
+
yield this.addInitObligationForFarm(this.outflowReserve, types_1.ReserveFarmKind.Debt, addToSetupIxs);
|
|
782
|
+
}
|
|
783
|
+
}
|
|
784
|
+
else if (action === 'repayAndWithdraw' || action === 'borrow' || action === 'repay') {
|
|
785
|
+
// todo - probably don't need to add both debt and collateral for everything here
|
|
786
|
+
yield this.addInitObligationForFarm(this.reserve, types_1.ReserveFarmKind.Debt, addToSetupIxs);
|
|
787
|
+
if (this.outflowReserve) {
|
|
788
|
+
yield this.addInitObligationForFarm(this.outflowReserve, types_1.ReserveFarmKind.Collateral, addToSetupIxs);
|
|
789
|
+
}
|
|
790
|
+
}
|
|
791
|
+
else {
|
|
792
|
+
yield this.addInitObligationForFarm(this.reserve, types_1.ReserveFarmKind.Collateral, addToSetupIxs);
|
|
793
|
+
yield this.addInitObligationForFarm(this.reserve, types_1.ReserveFarmKind.Debt, addToSetupIxs);
|
|
794
|
+
if (this.outflowReserve) {
|
|
795
|
+
yield this.addInitObligationForFarm(this.outflowReserve, types_1.ReserveFarmKind.Collateral, addToSetupIxs);
|
|
796
|
+
yield this.addInitObligationForFarm(this.outflowReserve, types_1.ReserveFarmKind.Debt, addToSetupIxs);
|
|
797
|
+
}
|
|
798
|
+
}
|
|
799
|
+
}
|
|
800
|
+
this.addRefreshReserveIxs(currentReserveAddresses.toArray(), addToSetupIxs);
|
|
801
|
+
if (action === 'repayAndWithdraw' && !addToSetupIxs && isClosingPosition) {
|
|
802
|
+
// addToSetupIxs === addInBetween (same thing)
|
|
803
|
+
// If this is a repay and withdraw, and it's not the first action, and it's closing a position
|
|
804
|
+
// we don't need to include the repay reserve in the refresh obligation
|
|
805
|
+
// I am ashamed of this code, we need to rewrite this entire thing
|
|
806
|
+
this.addRefreshObligationIx(addToSetupIxs, true);
|
|
807
|
+
}
|
|
808
|
+
else {
|
|
809
|
+
this.addRefreshObligationIx(addToSetupIxs, false);
|
|
810
|
+
}
|
|
811
|
+
if (addToSetupIxs) {
|
|
812
|
+
// If this is an setup ixn (therefore not an in-between), it means it's either a one off action
|
|
813
|
+
// or the first of a two-token-action
|
|
814
|
+
if (action === 'liquidate') {
|
|
815
|
+
this.addRefreshFarmsForReserve([this.outflowReserve], addToSetupIxs, types_1.ReserveFarmKind.Collateral);
|
|
816
|
+
this.addRefreshFarmsForReserve([this.reserve], addToSetupIxs, types_1.ReserveFarmKind.Debt);
|
|
817
|
+
}
|
|
818
|
+
else if (action === 'depositAndBorrow' ||
|
|
819
|
+
action === 'depositCollateral' ||
|
|
820
|
+
action === 'withdraw' ||
|
|
821
|
+
action === 'deposit') {
|
|
822
|
+
this.addRefreshFarmsForReserve(currentReserves, addToSetupIxs, types_1.ReserveFarmKind.Collateral, undefined, twoTokenAction);
|
|
823
|
+
}
|
|
824
|
+
else if (action === 'repayAndWithdraw' || action === 'borrow' || action === 'repay') {
|
|
825
|
+
this.addRefreshFarmsForReserve(currentReserves, addToSetupIxs, types_1.ReserveFarmKind.Debt, undefined, twoTokenAction);
|
|
826
|
+
}
|
|
827
|
+
else {
|
|
828
|
+
throw new Error(`Could not decide on refresh farm for action ${action}`);
|
|
829
|
+
}
|
|
830
|
+
}
|
|
831
|
+
else {
|
|
832
|
+
// If this is an inbetween, it means it's part of a two-token-action
|
|
833
|
+
// so we skip the refresh farm obligation of the first reserve as that operation already happened
|
|
834
|
+
// add added to 'setup' ixns
|
|
835
|
+
if (action === 'depositAndBorrow') {
|
|
836
|
+
this.addRefreshFarmsForReserve([this.outflowReserve], addToSetupIxs, types_1.ReserveFarmKind.Debt);
|
|
837
|
+
}
|
|
838
|
+
else if (action === 'repayAndWithdraw') {
|
|
839
|
+
this.addRefreshFarmsForReserve([this.outflowReserve], addToSetupIxs, types_1.ReserveFarmKind.Collateral);
|
|
840
|
+
}
|
|
841
|
+
else {
|
|
842
|
+
throw new Error(`Could not decide on refresh farm for action ${action}`);
|
|
843
|
+
}
|
|
844
|
+
}
|
|
845
|
+
if (action === 'depositAndBorrow' && requestElevationGroup) {
|
|
846
|
+
const groupsColl = this.reserve.state.config.elevationGroups;
|
|
847
|
+
const groupsDebt = this.outflowReserve.state.config.elevationGroups;
|
|
848
|
+
const groups = this.kaminoMarket.state.elevationGroups;
|
|
849
|
+
const commonElevationGroups = [...groupsColl].filter((item) => groupsDebt.includes(item) && item !== 0);
|
|
850
|
+
console.log('Groups of coll reserve', groupsColl, 'Groups of debt reserve', groupsDebt, 'Common groups', commonElevationGroups);
|
|
851
|
+
if (commonElevationGroups.length === 0) {
|
|
852
|
+
console.log('No common elevation groups found, staying with default');
|
|
853
|
+
}
|
|
854
|
+
else {
|
|
855
|
+
const eModeGroupWithMaxLtv = commonElevationGroups.reduce((prev, curr) => {
|
|
856
|
+
const prevGroup = groups.find((group) => group.id === prev);
|
|
857
|
+
const currGroup = groups.find((group) => group.id === curr);
|
|
858
|
+
return prevGroup.ltvPct > currGroup.ltvPct ? prev : curr;
|
|
859
|
+
});
|
|
860
|
+
const eModeGroup = groups.find((group) => group.id === eModeGroupWithMaxLtv).id;
|
|
861
|
+
console.log('Setting eModeGroup to', eModeGroup);
|
|
862
|
+
if (eModeGroup !== 0 && eModeGroup !== ((_a = this.obligation) === null || _a === void 0 ? void 0 : _a.state.elevationGroup)) {
|
|
863
|
+
this.addRequestElevationIx(eModeGroup, false);
|
|
864
|
+
this.addRefreshReserveIxs(allReservesExcludingCurrent, addToSetupIxs);
|
|
865
|
+
this.addRefreshReserveIxs(currentReserveAddresses.toArray(), addToSetupIxs);
|
|
866
|
+
this.addRefreshObligationIx(addToSetupIxs);
|
|
867
|
+
}
|
|
868
|
+
}
|
|
869
|
+
}
|
|
870
|
+
}
|
|
871
|
+
});
|
|
872
|
+
}
|
|
873
|
+
addSupportIxs(action, includeAtaIxns, requestElevationGroup, includeUserMetadata, addInitObligationForFarm, twoTokenAction = false) {
|
|
874
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
875
|
+
if (!['mint', 'redeem'].includes(action)) {
|
|
876
|
+
const [, ownerUserMetadata] = yield this.kaminoMarket.getUserMetadata(this.owner);
|
|
877
|
+
if (!ownerUserMetadata && includeUserMetadata) {
|
|
878
|
+
yield this.addInitUserMetadataIxs();
|
|
879
|
+
}
|
|
880
|
+
yield this.addInitReferrerTokenStateIxs();
|
|
881
|
+
yield this.addInitObligationIxs();
|
|
882
|
+
}
|
|
883
|
+
yield this.addSupportIxsWithoutInitObligation(action, includeAtaIxns, true, requestElevationGroup, addInitObligationForFarm, false, twoTokenAction);
|
|
884
|
+
});
|
|
885
|
+
}
|
|
886
|
+
static optionalAccount(pubkey, programId = lib_1.PROGRAM_ID) {
|
|
887
|
+
if ((0, utils_1.isNotNullPubkey)(pubkey)) {
|
|
888
|
+
return pubkey;
|
|
889
|
+
}
|
|
890
|
+
else {
|
|
891
|
+
return programId;
|
|
892
|
+
}
|
|
893
|
+
}
|
|
894
|
+
addRefreshReserveIxs(reserves, addToSetupIxs = true) {
|
|
895
|
+
reserves.forEach((reserveAddress) => {
|
|
896
|
+
const foundReserve = this.kaminoMarket.getReserveByAddress(reserveAddress);
|
|
897
|
+
if (!foundReserve) {
|
|
898
|
+
throw new Error(`Could not find reserve ${reserveAddress} in reserves`);
|
|
899
|
+
}
|
|
900
|
+
const { state } = foundReserve;
|
|
901
|
+
const refreshReserveIx = (0, instructions_1.refreshReserve)({
|
|
902
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
903
|
+
reserve: reserveAddress,
|
|
904
|
+
pythOracle: KaminoAction.optionalAccount(state.config.tokenInfo.pythConfiguration.price, this.kaminoMarket.programId),
|
|
905
|
+
switchboardPriceOracle: KaminoAction.optionalAccount(state.config.tokenInfo.switchboardConfiguration.priceAggregator, this.kaminoMarket.programId),
|
|
906
|
+
switchboardTwapOracle: KaminoAction.optionalAccount(state.config.tokenInfo.switchboardConfiguration.twapAggregator, this.kaminoMarket.programId),
|
|
907
|
+
scopePrices: KaminoAction.optionalAccount(state.config.tokenInfo.scopeConfiguration.priceFeed, this.kaminoMarket.programId),
|
|
908
|
+
}, this.kaminoMarket.programId);
|
|
909
|
+
if (addToSetupIxs) {
|
|
910
|
+
this.setupIxs.push(refreshReserveIx);
|
|
911
|
+
this.setupIxsLabels.push(`RefreshReserve[${reserveAddress}]`);
|
|
912
|
+
}
|
|
913
|
+
else {
|
|
914
|
+
this.inBetweenIxs.push(refreshReserveIx);
|
|
915
|
+
this.inBetweenIxsLabels.push(`RefreshReserve[${reserveAddress}]`);
|
|
916
|
+
}
|
|
917
|
+
});
|
|
918
|
+
}
|
|
919
|
+
static getRefreshAllReserves(kaminoMarket, reserves) {
|
|
920
|
+
return reserves.map((reserveAddress) => {
|
|
921
|
+
const foundReserve = kaminoMarket.getReserveByAddress(reserveAddress);
|
|
922
|
+
if (!foundReserve) {
|
|
923
|
+
throw new Error(`Could not find reserve ${reserveAddress} in reserves`);
|
|
924
|
+
}
|
|
925
|
+
const { state } = foundReserve;
|
|
926
|
+
return (0, instructions_1.refreshReserve)({
|
|
927
|
+
reserve: reserveAddress,
|
|
928
|
+
lendingMarket: state.lendingMarket,
|
|
929
|
+
pythOracle: this.optionalAccount(state.config.tokenInfo.pythConfiguration.price, kaminoMarket.programId),
|
|
930
|
+
switchboardPriceOracle: this.optionalAccount(state.config.tokenInfo.switchboardConfiguration.priceAggregator, kaminoMarket.programId),
|
|
931
|
+
switchboardTwapOracle: this.optionalAccount(state.config.tokenInfo.switchboardConfiguration.twapAggregator, kaminoMarket.programId),
|
|
932
|
+
scopePrices: this.optionalAccount(state.config.tokenInfo.scopeConfiguration.priceFeed, kaminoMarket.programId),
|
|
933
|
+
}, kaminoMarket.programId);
|
|
934
|
+
});
|
|
935
|
+
}
|
|
936
|
+
addRefreshObligationIx(addToSetupIxs = true, skipBorrowObligations = false) {
|
|
937
|
+
const marketAddress = this.kaminoMarket.getAddress();
|
|
938
|
+
const obligationPda = this.getObligationPda();
|
|
939
|
+
const refreshObligationIx = (0, instructions_1.refreshObligation)({
|
|
940
|
+
lendingMarket: marketAddress,
|
|
941
|
+
obligation: obligationPda,
|
|
942
|
+
}, this.kaminoMarket.programId);
|
|
943
|
+
const depositReservesList = this.depositReserves;
|
|
944
|
+
// check if there's any member in the preloadedDepositReserves that is not in the depositReserves
|
|
945
|
+
// if so, add it to the depositReserves
|
|
946
|
+
for (let i = 0; i < this.preLoadedDepositReservesSameTx.length; i++) {
|
|
947
|
+
const preloadedDepositReserve = this.preLoadedDepositReservesSameTx[i];
|
|
948
|
+
// Check if the depositReserves array contains the current preloadedDepositReserve
|
|
949
|
+
const found = this.depositReserves.some((depositReserve) => {
|
|
950
|
+
return depositReserve.equals(preloadedDepositReserve);
|
|
951
|
+
});
|
|
952
|
+
// If not found, push the current preloadedDepositReserve to the depositReserves array
|
|
953
|
+
if (!found) {
|
|
954
|
+
depositReservesList.push(this.preLoadedDepositReservesSameTx[i]);
|
|
955
|
+
}
|
|
956
|
+
}
|
|
957
|
+
const depositReserveAccountMetas = depositReservesList.map((reserve) => {
|
|
958
|
+
return { pubkey: reserve, isSigner: false, isWritable: true };
|
|
959
|
+
});
|
|
960
|
+
const preloadedBorrowReservesString = this.preLoadedBorrowReservesSameTx.map((reserve) => reserve.toString());
|
|
961
|
+
const borrowReservesList = this.borrowReserves.filter((reserve) => !preloadedBorrowReservesString.includes(reserve.toString()));
|
|
962
|
+
const borrowReserveAccountMetas = borrowReservesList.map((reserve) => {
|
|
963
|
+
return { pubkey: reserve, isSigner: false, isWritable: true };
|
|
964
|
+
});
|
|
965
|
+
const borrowReservesReferrerTokenStates = borrowReservesList
|
|
966
|
+
.map((reserve) => {
|
|
967
|
+
if (this.referrer.equals(web3_js_1.PublicKey.default)) {
|
|
968
|
+
return { pubkey: this.kaminoMarket.programId, isSigner: false, isWritable: true };
|
|
969
|
+
}
|
|
970
|
+
const referrerTokenStateAddress = (0, utils_1.referrerTokenStatePda)(this.referrer, reserve, this.kaminoMarket.programId)[0];
|
|
971
|
+
return { pubkey: referrerTokenStateAddress, isSigner: false, isWritable: true };
|
|
972
|
+
})
|
|
973
|
+
.filter((x) => !x.pubkey.equals(this.kaminoMarket.programId));
|
|
974
|
+
refreshObligationIx.keys = refreshObligationIx.keys.concat([
|
|
975
|
+
...depositReserveAccountMetas,
|
|
976
|
+
...(skipBorrowObligations ? [] : [...borrowReserveAccountMetas, ...borrowReservesReferrerTokenStates]),
|
|
977
|
+
]);
|
|
978
|
+
if (addToSetupIxs) {
|
|
979
|
+
this.setupIxs.push(refreshObligationIx);
|
|
980
|
+
this.setupIxsLabels.push(`RefreshObligation[${obligationPda.toString()}]`);
|
|
981
|
+
}
|
|
982
|
+
else {
|
|
983
|
+
this.inBetweenIxs.push(refreshObligationIx);
|
|
984
|
+
this.inBetweenIxsLabels.push(`RefreshObligation[${obligationPda.toString()}]`);
|
|
985
|
+
}
|
|
986
|
+
}
|
|
987
|
+
addRequestElevationIx(elevationGroup, addToSetupIxs) {
|
|
988
|
+
const obligationPda = this.getObligationPda();
|
|
989
|
+
const args = {
|
|
990
|
+
elevationGroup,
|
|
991
|
+
};
|
|
992
|
+
const accounts = {
|
|
993
|
+
owner: this.owner,
|
|
994
|
+
obligation: obligationPda,
|
|
995
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
996
|
+
};
|
|
997
|
+
const requestElevationGroupIx = (0, instructions_1.requestElevationGroup)(args, accounts, this.kaminoMarket.programId);
|
|
998
|
+
const depositReservesList = this.depositReserves;
|
|
999
|
+
// check if there's any member in the preloadedDepositReserves that is not in the depositReserves
|
|
1000
|
+
// if so, add it to the depositReserves
|
|
1001
|
+
for (let i = 0; i < this.preLoadedDepositReservesSameTx.length; i++) {
|
|
1002
|
+
const preloadedDepositReserveString = this.preLoadedDepositReservesSameTx[i].toString();
|
|
1003
|
+
// Check if the depositReserves array contains the current preloadedDepositReserve
|
|
1004
|
+
const found = this.depositReserves.some((depositReserve) => {
|
|
1005
|
+
return depositReserve.toString() === preloadedDepositReserveString;
|
|
1006
|
+
});
|
|
1007
|
+
// If not found, push the current preloadedDepositReserve to the depositReserves array
|
|
1008
|
+
if (!found) {
|
|
1009
|
+
depositReservesList.push(this.preLoadedDepositReservesSameTx[i]);
|
|
1010
|
+
}
|
|
1011
|
+
}
|
|
1012
|
+
const depositReserveAccountMetas = depositReservesList.map((reserve) => {
|
|
1013
|
+
return { pubkey: reserve, isSigner: false, isWritable: true };
|
|
1014
|
+
});
|
|
1015
|
+
const preloadedBorrowReservesString = this.preLoadedBorrowReservesSameTx.map((reserve) => reserve.toString());
|
|
1016
|
+
const borrowReservesList = this.borrowReserves.filter((reserve) => !preloadedBorrowReservesString.includes(reserve.toString()));
|
|
1017
|
+
const borrowReserveAccountMetas = borrowReservesList.map((reserve) => {
|
|
1018
|
+
return { pubkey: reserve, isSigner: false, isWritable: true };
|
|
1019
|
+
});
|
|
1020
|
+
const borrowReservesReferrerTokenStates = borrowReservesList
|
|
1021
|
+
.map((reserve) => {
|
|
1022
|
+
if (this.referrer.equals(web3_js_1.PublicKey.default)) {
|
|
1023
|
+
return { pubkey: this.kaminoMarket.programId, isSigner: false, isWritable: true };
|
|
1024
|
+
}
|
|
1025
|
+
const reserveState = this.kaminoMarket.getReserveByAddress(reserve);
|
|
1026
|
+
const referrerTokenStateAddress = (0, utils_1.referrerTokenStatePda)(this.referrer, reserveState.address, this.kaminoMarket.programId)[0];
|
|
1027
|
+
return { pubkey: referrerTokenStateAddress, isSigner: false, isWritable: true };
|
|
1028
|
+
})
|
|
1029
|
+
.filter((x) => !x.pubkey.equals(this.kaminoMarket.programId));
|
|
1030
|
+
requestElevationGroupIx.keys = requestElevationGroupIx.keys.concat([
|
|
1031
|
+
...depositReserveAccountMetas,
|
|
1032
|
+
...borrowReserveAccountMetas,
|
|
1033
|
+
...borrowReservesReferrerTokenStates,
|
|
1034
|
+
]);
|
|
1035
|
+
if (addToSetupIxs) {
|
|
1036
|
+
this.setupIxs.push(requestElevationGroupIx);
|
|
1037
|
+
this.setupIxsLabels.push(`RequestElevation[${obligationPda}], elevation_group:${elevationGroup}`);
|
|
1038
|
+
}
|
|
1039
|
+
else {
|
|
1040
|
+
this.inBetweenIxs.push(requestElevationGroupIx);
|
|
1041
|
+
this.inBetweenIxsLabels.push(`RequestElevation[${obligationPda}], elevation_group:${elevationGroup}`);
|
|
1042
|
+
}
|
|
1043
|
+
}
|
|
1044
|
+
addRefreshFarmsForReserve(reserves, addToSetupIxs = true, mode, crank = this.payer, twoTokenAction = false) {
|
|
1045
|
+
const BASE_SEED_USER_STATE = Buffer.from('user');
|
|
1046
|
+
const getPda = (farm) => web3_js_1.PublicKey.findProgramAddressSync([BASE_SEED_USER_STATE, farm.toBytes(), this.getObligationPda().toBytes()], farms_sdk_1.farmsId)[0];
|
|
1047
|
+
const farms = [];
|
|
1048
|
+
for (const kaminoReserve of reserves) {
|
|
1049
|
+
if (mode === types_1.ReserveFarmKind.Collateral && !kaminoReserve.state.farmCollateral.equals(web3_js_1.PublicKey.default)) {
|
|
1050
|
+
farms.push([
|
|
1051
|
+
types_1.ReserveFarmKind.Collateral,
|
|
1052
|
+
kaminoReserve.state.farmCollateral,
|
|
1053
|
+
getPda(kaminoReserve.state.farmCollateral),
|
|
1054
|
+
kaminoReserve,
|
|
1055
|
+
]);
|
|
1056
|
+
}
|
|
1057
|
+
if (mode === types_1.ReserveFarmKind.Debt && !kaminoReserve.state.farmDebt.equals(web3_js_1.PublicKey.default)) {
|
|
1058
|
+
farms.push([
|
|
1059
|
+
types_1.ReserveFarmKind.Debt,
|
|
1060
|
+
kaminoReserve.state.farmDebt,
|
|
1061
|
+
getPda(kaminoReserve.state.farmDebt),
|
|
1062
|
+
kaminoReserve,
|
|
1063
|
+
]);
|
|
1064
|
+
}
|
|
1065
|
+
}
|
|
1066
|
+
farms.forEach((arg) => {
|
|
1067
|
+
const args = { mode: arg[0].discriminator };
|
|
1068
|
+
const accounts = {
|
|
1069
|
+
crank,
|
|
1070
|
+
obligation: this.getObligationPda(),
|
|
1071
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
1072
|
+
reserve: arg[3].address,
|
|
1073
|
+
reserveFarmState: arg[1],
|
|
1074
|
+
obligationFarmUserState: arg[2],
|
|
1075
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
1076
|
+
farmsProgram: farms_sdk_1.farmsId,
|
|
1077
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
1078
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1079
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
1080
|
+
};
|
|
1081
|
+
const refreshFarmForObligationix = (0, instructions_1.refreshObligationFarmsForReserve)(args, accounts, this.kaminoMarket.programId);
|
|
1082
|
+
if (addToSetupIxs) {
|
|
1083
|
+
this.setupIxs.push(refreshFarmForObligationix);
|
|
1084
|
+
this.setupIxsLabels.push(`RefreshFarmForObligation[${arg[0].kind}, res=${arg[3].address.toString()}, obl=${this.getObligationPda().toString()}]`);
|
|
1085
|
+
if (twoTokenAction) {
|
|
1086
|
+
// If two token action, this refresh needs to be the first inbetween ix
|
|
1087
|
+
this.inBetweenIxs.push(refreshFarmForObligationix);
|
|
1088
|
+
this.inBetweenIxsLabels.push(`RefreshFarmForObligation[${arg[0].kind}, res=${arg[3].address.toString()}, obl=${this.getObligationPda().toString()}]`);
|
|
1089
|
+
}
|
|
1090
|
+
else {
|
|
1091
|
+
this.refreshFarmsCleanupTxnIxs.push(refreshFarmForObligationix);
|
|
1092
|
+
this.refreshFarmsCleanupTxnIxsLabels.push(`RefreshFarmForObligation[${arg[0].kind}, res=${arg[3].address.toString()}, obl=${this.getObligationPda().toString()}]`);
|
|
1093
|
+
}
|
|
1094
|
+
}
|
|
1095
|
+
else {
|
|
1096
|
+
this.inBetweenIxs.push(refreshFarmForObligationix);
|
|
1097
|
+
this.inBetweenIxsLabels.push(`RefreshFarmForObligation[${arg[0].kind}, res=${arg[3].address.toString()}, obl=${this.getObligationPda().toString()}]`);
|
|
1098
|
+
this.refreshFarmsCleanupTxnIxs.push(refreshFarmForObligationix);
|
|
1099
|
+
this.refreshFarmsCleanupTxnIxsLabels.push(`RefreshFarmForObligation[${arg[0].kind}, res=${arg[3].address.toString()}, obl=${this.getObligationPda().toString()}]`);
|
|
1100
|
+
}
|
|
1101
|
+
});
|
|
1102
|
+
}
|
|
1103
|
+
addRefreshFarmsCleanupTxnIxsToCleanupIxs() {
|
|
1104
|
+
this.cleanupIxs.splice(this.cleanupIxs.length - 1, 0, ...this.refreshFarmsCleanupTxnIxs);
|
|
1105
|
+
this.cleanupIxsLabels.splice(this.cleanupIxsLabels.length - 1, 0, ...this.refreshFarmsCleanupTxnIxsLabels);
|
|
1106
|
+
}
|
|
1107
|
+
addInitObligationForFarm(reserve, mode, addToSetupIxs = true) {
|
|
1108
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1109
|
+
const BASE_SEED_USER_STATE = Buffer.from('user');
|
|
1110
|
+
const getPda = (farm) => web3_js_1.PublicKey.findProgramAddressSync([BASE_SEED_USER_STATE, farm.toBytes(), this.getObligationPda().toBytes()], farms_sdk_1.farmsId)[0];
|
|
1111
|
+
const farms = [];
|
|
1112
|
+
if (mode === types_1.ReserveFarmKind.Collateral && (0, utils_1.isNotNullPubkey)(reserve.state.farmCollateral)) {
|
|
1113
|
+
const pda = getPda(reserve.state.farmCollateral);
|
|
1114
|
+
const account = yield this.kaminoMarket.getConnection().getAccountInfo(pda);
|
|
1115
|
+
if (!account) {
|
|
1116
|
+
farms.push([types_1.ReserveFarmKind.Collateral.discriminator, reserve.state.farmCollateral, pda]);
|
|
1117
|
+
}
|
|
1118
|
+
}
|
|
1119
|
+
if (mode === types_1.ReserveFarmKind.Debt && (0, utils_1.isNotNullPubkey)(reserve.state.farmDebt)) {
|
|
1120
|
+
const pda = getPda(reserve.state.farmDebt);
|
|
1121
|
+
const account = yield this.kaminoMarket.getConnection().getAccountInfo(pda);
|
|
1122
|
+
if (!account) {
|
|
1123
|
+
farms.push([types_1.ReserveFarmKind.Debt.discriminator, reserve.state.farmDebt, getPda(reserve.state.farmDebt)]);
|
|
1124
|
+
}
|
|
1125
|
+
}
|
|
1126
|
+
farms.forEach((arg) => {
|
|
1127
|
+
const args = { mode: arg[0] };
|
|
1128
|
+
const accounts = {
|
|
1129
|
+
owner: this.obligation ? this.obligation.state.owner : this.owner,
|
|
1130
|
+
payer: this.owner,
|
|
1131
|
+
obligation: this.getObligationPda(),
|
|
1132
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
1133
|
+
reserve: reserve.address,
|
|
1134
|
+
reserveFarmState: arg[1],
|
|
1135
|
+
obligationFarm: arg[2],
|
|
1136
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
1137
|
+
farmsProgram: farms_sdk_1.farmsId,
|
|
1138
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
1139
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1140
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
1141
|
+
};
|
|
1142
|
+
const initObligationForFarm = (0, instructions_1.initObligationFarmsForReserve)(args, accounts, this.kaminoMarket.programId);
|
|
1143
|
+
if (addToSetupIxs) {
|
|
1144
|
+
this.setupIxs.push(initObligationForFarm);
|
|
1145
|
+
this.setupIxsLabels.push(`InitObligationForFarm[${reserve.address.toString()}, ${this.getObligationPda().toString()}]`);
|
|
1146
|
+
}
|
|
1147
|
+
else {
|
|
1148
|
+
this.inBetweenIxs.push(initObligationForFarm);
|
|
1149
|
+
this.inBetweenIxsLabels.push(`InitObligationForFarm[${reserve.address.toString()}, ${this.getObligationPda().toString()}]`);
|
|
1150
|
+
}
|
|
1151
|
+
});
|
|
1152
|
+
});
|
|
1153
|
+
}
|
|
1154
|
+
addInitObligationIxs() {
|
|
1155
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1156
|
+
if (!this.obligation) {
|
|
1157
|
+
const obligationPda = this.getObligationPda();
|
|
1158
|
+
const [userMetadataAddress, _bump] = (0, utils_1.userMetadataPda)(this.owner, this.kaminoMarket.programId);
|
|
1159
|
+
const initObligationIx = (0, instructions_1.initObligation)({
|
|
1160
|
+
args: {
|
|
1161
|
+
tag: this.obligationType.toArgs().tag,
|
|
1162
|
+
id: this.obligationType.toArgs().id,
|
|
1163
|
+
},
|
|
1164
|
+
}, {
|
|
1165
|
+
obligationOwner: this.owner,
|
|
1166
|
+
feePayer: this.payer,
|
|
1167
|
+
obligation: obligationPda,
|
|
1168
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
1169
|
+
seed1Account: this.obligationType.toArgs().seed1,
|
|
1170
|
+
seed2Account: this.obligationType.toArgs().seed2,
|
|
1171
|
+
ownerUserMetadata: userMetadataAddress,
|
|
1172
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
1173
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1174
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
1175
|
+
}, this.kaminoMarket.programId);
|
|
1176
|
+
this.setupIxs.push(initObligationIx);
|
|
1177
|
+
this.setupIxsLabels.push(`InitObligation[${obligationPda.toString()}]`);
|
|
1178
|
+
}
|
|
1179
|
+
});
|
|
1180
|
+
}
|
|
1181
|
+
addInitUserMetadataIxs() {
|
|
1182
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1183
|
+
const [createLutIx, lookupTableAddress] = yield (0, utils_1.createLookupTableIx)(this.kaminoMarket.getConnection(), this.owner);
|
|
1184
|
+
this.setupIxs.push(createLutIx);
|
|
1185
|
+
this.setupIxsLabels.push(`createUserLutIx[${lookupTableAddress.toString()}]`);
|
|
1186
|
+
const [userMetadataAddress, _bump] = (0, utils_1.userMetadataPda)(this.owner, this.kaminoMarket.programId);
|
|
1187
|
+
const referrerUserMetadataAddress = this.referrer.equals(web3_js_1.PublicKey.default)
|
|
1188
|
+
? this.kaminoMarket.programId
|
|
1189
|
+
: (0, utils_1.userMetadataPda)(this.referrer, this.kaminoMarket.programId)[0];
|
|
1190
|
+
const initUserMetadataIx = (0, instructions_1.initUserMetadata)({
|
|
1191
|
+
userLookupTable: lookupTableAddress,
|
|
1192
|
+
}, {
|
|
1193
|
+
owner: this.owner,
|
|
1194
|
+
feePayer: this.payer,
|
|
1195
|
+
userMetadata: userMetadataAddress,
|
|
1196
|
+
referrerUserMetadata: referrerUserMetadataAddress,
|
|
1197
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
1198
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
1199
|
+
}, this.kaminoMarket.programId);
|
|
1200
|
+
this.setupIxs.push(initUserMetadataIx);
|
|
1201
|
+
this.setupIxsLabels.push(`initUserMetadata[${userMetadataAddress.toString()}]`);
|
|
1202
|
+
});
|
|
1203
|
+
}
|
|
1204
|
+
addInitReferrerTokenStateIxs(reservesArr = []) {
|
|
1205
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1206
|
+
if (this.referrer.equals(web3_js_1.PublicKey.default)) {
|
|
1207
|
+
return;
|
|
1208
|
+
}
|
|
1209
|
+
const reserves = reservesArr.length !== 0 ? reservesArr : [...new Set([this.reserve, this.outflowReserve])];
|
|
1210
|
+
const tokenStatesToCreate = [];
|
|
1211
|
+
for (const reserve of reserves) {
|
|
1212
|
+
if (!reserve) {
|
|
1213
|
+
continue;
|
|
1214
|
+
}
|
|
1215
|
+
const referrerTokenStateAddress = (0, utils_1.referrerTokenStatePda)(this.referrer, reserve.address, this.kaminoMarket.programId)[0];
|
|
1216
|
+
if (!(yield (0, utils_1.checkIfAccountExists)(this.kaminoMarket.getConnection(), referrerTokenStateAddress))) {
|
|
1217
|
+
tokenStatesToCreate.push([referrerTokenStateAddress, reserve === null || reserve === void 0 ? void 0 : reserve.address]);
|
|
1218
|
+
}
|
|
1219
|
+
}
|
|
1220
|
+
tokenStatesToCreate.forEach(([referrerTokenStateAddress, reserveAddress]) => {
|
|
1221
|
+
const initreferrerTokenStateIx = (0, instructions_1.initReferrerTokenState)({
|
|
1222
|
+
referrer: this.referrer,
|
|
1223
|
+
}, {
|
|
1224
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
1225
|
+
payer: this.owner,
|
|
1226
|
+
reserve: reserveAddress,
|
|
1227
|
+
referrerTokenState: referrerTokenStateAddress,
|
|
1228
|
+
rent: web3_js_1.SYSVAR_RENT_PUBKEY,
|
|
1229
|
+
systemProgram: web3_js_1.SystemProgram.programId,
|
|
1230
|
+
}, this.kaminoMarket.programId);
|
|
1231
|
+
this.setupIxs.unshift(initreferrerTokenStateIx);
|
|
1232
|
+
this.setupIxsLabels.unshift(`InitReferrerTokenState[${referrerTokenStateAddress.toString()} res=${reserveAddress}]`);
|
|
1233
|
+
});
|
|
1234
|
+
});
|
|
1235
|
+
}
|
|
1236
|
+
addWithdrawReferrerFeesIxs() {
|
|
1237
|
+
const referrerTokenStateAddress = (0, utils_1.referrerTokenStatePda)(this.owner, this.reserve.address, this.kaminoMarket.programId)[0];
|
|
1238
|
+
const withdrawReferrerFeesIx = (0, instructions_1.withdrawReferrerFees)({
|
|
1239
|
+
referrer: this.owner,
|
|
1240
|
+
lendingMarket: this.kaminoMarket.getAddress(),
|
|
1241
|
+
reserve: this.reserve.address,
|
|
1242
|
+
referrerTokenState: referrerTokenStateAddress,
|
|
1243
|
+
reserveSupplyLiquidity: this.reserve.state.liquidity.supplyVault,
|
|
1244
|
+
referrerTokenAccount: this.userTokenAccountAddress,
|
|
1245
|
+
lendingMarketAuthority: this.kaminoMarket.getLendingMarketAuthority(),
|
|
1246
|
+
tokenProgram: spl_token_1.TOKEN_PROGRAM_ID,
|
|
1247
|
+
}, this.kaminoMarket.programId);
|
|
1248
|
+
this.lendingIxs.push(withdrawReferrerFeesIx);
|
|
1249
|
+
this.lendingIxsLabels.push(`WithdrawReferrerFeesIx[${this.owner.toString()}]`);
|
|
1250
|
+
}
|
|
1251
|
+
addComputeBudgetIxn(units) {
|
|
1252
|
+
this.setupIxs.push((0, utils_1.buildComputeBudgetIx)(units));
|
|
1253
|
+
this.setupIxsLabels.push(`AddComputeBudget[${units}]`);
|
|
1254
|
+
}
|
|
1255
|
+
addAtaIxs(action) {
|
|
1256
|
+
var _a, _b;
|
|
1257
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1258
|
+
if (this.mint.equals(utils_1.WRAPPED_SOL_MINT) || ((_a = this.secondaryMint) === null || _a === void 0 ? void 0 : _a.equals(utils_1.WRAPPED_SOL_MINT))) {
|
|
1259
|
+
yield this.updateWSOLAccount(action);
|
|
1260
|
+
}
|
|
1261
|
+
if ((action === 'withdraw' || action === 'borrow' || action === 'redeem') && !this.mint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
1262
|
+
const userTokenAccountInfo = yield this.kaminoMarket.getConnection().getAccountInfo(this.userTokenAccountAddress);
|
|
1263
|
+
if (!userTokenAccountInfo) {
|
|
1264
|
+
const [, createUserTokenAccountIx] = yield (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.reserve.getLiquidityMint(), this.owner, this.userTokenAccountAddress);
|
|
1265
|
+
if (this.positions === exports.POSITION_LIMIT && this.hostAta) {
|
|
1266
|
+
this.preTxnIxs.push(createUserTokenAccountIx);
|
|
1267
|
+
this.preTxnIxsLabels.push(`CreateLiquidityUserAta[${this.owner}]`);
|
|
1268
|
+
}
|
|
1269
|
+
else {
|
|
1270
|
+
this.setupIxs.unshift(createUserTokenAccountIx);
|
|
1271
|
+
this.setupIxsLabels.unshift(`CreateLiquidityUserAta[${this.owner}]`);
|
|
1272
|
+
}
|
|
1273
|
+
}
|
|
1274
|
+
}
|
|
1275
|
+
if (action === 'liquidate') {
|
|
1276
|
+
const userTokenAccountInfo = yield this.kaminoMarket.getConnection().getAccountInfo(this.userTokenAccountAddress);
|
|
1277
|
+
if (!this.outflowReserve) {
|
|
1278
|
+
throw new Error(`Outflow reserve state not found ${this.mint}`);
|
|
1279
|
+
}
|
|
1280
|
+
if (!userTokenAccountInfo) {
|
|
1281
|
+
const [, createUserTokenAccountIx] = yield (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getLiquidityMint(), this.owner, this.userTokenAccountAddress);
|
|
1282
|
+
if (this.positions === exports.POSITION_LIMIT && this.mint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
1283
|
+
this.preTxnIxs.push(createUserTokenAccountIx);
|
|
1284
|
+
this.preTxnIxsLabels.push(`CreateUserAta[${this.userTokenAccountAddress.toBase58()}]`);
|
|
1285
|
+
}
|
|
1286
|
+
else {
|
|
1287
|
+
this.setupIxs.unshift(createUserTokenAccountIx);
|
|
1288
|
+
this.setupIxsLabels.unshift(`CreateUserAta[${this.userTokenAccountAddress.toBase58()}]`);
|
|
1289
|
+
}
|
|
1290
|
+
}
|
|
1291
|
+
const userCollateralAccountInfo = yield this.kaminoMarket
|
|
1292
|
+
.getConnection()
|
|
1293
|
+
.getAccountInfo(this.userCollateralAccountAddress);
|
|
1294
|
+
if (!userCollateralAccountInfo) {
|
|
1295
|
+
const [, createUserCollateralAccountIx] = yield (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getCTokenMint(), this.owner, this.userCollateralAccountAddress);
|
|
1296
|
+
if (this.positions === exports.POSITION_LIMIT && this.mint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
1297
|
+
this.preTxnIxs.push(createUserCollateralAccountIx);
|
|
1298
|
+
this.preTxnIxsLabels.push(`CreateCollateralUserAta[${this.userCollateralAccountAddress.toString()}]`);
|
|
1299
|
+
}
|
|
1300
|
+
else {
|
|
1301
|
+
this.setupIxs.unshift(createUserCollateralAccountIx);
|
|
1302
|
+
this.setupIxsLabels.unshift(`CreateCollateralUserAta[${this.userCollateralAccountAddress.toString()}]`);
|
|
1303
|
+
}
|
|
1304
|
+
}
|
|
1305
|
+
if (!this.additionalTokenAccountAddress) {
|
|
1306
|
+
throw new Error(`Additional token account address not found ${this.mint}`);
|
|
1307
|
+
}
|
|
1308
|
+
}
|
|
1309
|
+
if (action === 'depositAndBorrow' ||
|
|
1310
|
+
(action === 'repayAndWithdraw' && !((_b = this.secondaryMint) === null || _b === void 0 ? void 0 : _b.equals(utils_1.WRAPPED_SOL_MINT)))) {
|
|
1311
|
+
if (!this.additionalTokenAccountAddress) {
|
|
1312
|
+
throw new Error(`Additional token account address not found ${this.secondaryMint}`);
|
|
1313
|
+
}
|
|
1314
|
+
if (!this.outflowReserve) {
|
|
1315
|
+
throw new Error(`Outflow reserve state not found ${this.mint}`);
|
|
1316
|
+
}
|
|
1317
|
+
const additionalUserTokenAccountInfo = yield this.kaminoMarket
|
|
1318
|
+
.getConnection()
|
|
1319
|
+
.getAccountInfo(this.additionalTokenAccountAddress);
|
|
1320
|
+
if (!additionalUserTokenAccountInfo) {
|
|
1321
|
+
const [, createUserTokenAccountIx] = yield (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.outflowReserve.getLiquidityMint(), this.owner, this.additionalTokenAccountAddress);
|
|
1322
|
+
this.setupIxs.unshift(createUserTokenAccountIx);
|
|
1323
|
+
this.setupIxsLabels.unshift(`CreateAdditionalUserTokenAta[${this.owner}]`);
|
|
1324
|
+
}
|
|
1325
|
+
}
|
|
1326
|
+
if (action === 'withdraw' || action === 'mint' || action === 'deposit' || action === 'repayAndWithdraw') {
|
|
1327
|
+
const userTokenAccountInfo = yield this.kaminoMarket.getConnection().getAccountInfo(this.userTokenAccountAddress);
|
|
1328
|
+
// TODO: Might need to remove this
|
|
1329
|
+
if (!userTokenAccountInfo) {
|
|
1330
|
+
const [, createUserTokenAccountIx] = yield (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, this.reserve.getLiquidityMint(), this.owner, this.userTokenAccountAddress);
|
|
1331
|
+
this.preTxnIxs.push(createUserTokenAccountIx);
|
|
1332
|
+
this.preTxnIxsLabels.push(`CreateUserAta[${this.userTokenAccountAddress.toBase58()}]`);
|
|
1333
|
+
}
|
|
1334
|
+
}
|
|
1335
|
+
if (action === 'mint') {
|
|
1336
|
+
const userCollateralAccountInfo = yield this.kaminoMarket
|
|
1337
|
+
.getConnection()
|
|
1338
|
+
.getAccountInfo(this.userCollateralAccountAddress);
|
|
1339
|
+
if (!userCollateralAccountInfo) {
|
|
1340
|
+
const collateralMintPubkey = this.reserve.getCTokenMint();
|
|
1341
|
+
const [, createUserCollateralAccountIx] = yield (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, collateralMintPubkey, this.owner, this.userCollateralAccountAddress);
|
|
1342
|
+
if (this.positions === exports.POSITION_LIMIT && this.mint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
1343
|
+
this.preTxnIxs.push(createUserCollateralAccountIx);
|
|
1344
|
+
this.preTxnIxsLabels.push(`CreateCollateralUserAta[${this.userCollateralAccountAddress.toString()}]`);
|
|
1345
|
+
}
|
|
1346
|
+
else {
|
|
1347
|
+
this.setupIxs.unshift(createUserCollateralAccountIx);
|
|
1348
|
+
this.setupIxsLabels.unshift(`CreateCollateralUserAta[${this.userCollateralAccountAddress.toString()}]`);
|
|
1349
|
+
}
|
|
1350
|
+
}
|
|
1351
|
+
}
|
|
1352
|
+
});
|
|
1353
|
+
}
|
|
1354
|
+
updateWSOLAccount(action) {
|
|
1355
|
+
var _a, _b;
|
|
1356
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1357
|
+
const preIxs = [];
|
|
1358
|
+
const postIxs = [];
|
|
1359
|
+
const preIxsLabels = [];
|
|
1360
|
+
const postIxsLabels = [];
|
|
1361
|
+
if (action === 'depositAndBorrow' || action === 'repayAndWithdraw') {
|
|
1362
|
+
return;
|
|
1363
|
+
}
|
|
1364
|
+
let safeRepay = new bn_js_1.default(this.amount);
|
|
1365
|
+
if (this.obligation && action === 'repay' && this.amount.eq(new bn_js_1.default(utils_1.U64_MAX))) {
|
|
1366
|
+
const borrow = this.obligation.state.borrows.find((borrow) => borrow.borrowReserve.toString() === this.reserve.address.toString());
|
|
1367
|
+
if (!borrow) {
|
|
1368
|
+
throw Error(`Unable to find obligation borrow to repay for ${this.obligation.state.owner.toBase58()}`);
|
|
1369
|
+
}
|
|
1370
|
+
const cumulativeBorrowRateObligation = obligation_1.KaminoObligation.getCumulativeBorrowRate(borrow);
|
|
1371
|
+
const cumulativeBorrowRateReserve = this.reserve.getEstimatedCumulativeBorrowRate(this.currentSlot);
|
|
1372
|
+
// TODO: shouldn't this calc be added to all other stuff as well?
|
|
1373
|
+
safeRepay = new bn_js_1.default(Math.floor(obligation_1.KaminoObligation.getBorrowAmount(borrow)
|
|
1374
|
+
.mul(cumulativeBorrowRateReserve)
|
|
1375
|
+
.div(cumulativeBorrowRateObligation)
|
|
1376
|
+
.add(new decimal_js_1.default(SOL_PADDING_FOR_INTEREST.toString()))
|
|
1377
|
+
.toNumber()).toString());
|
|
1378
|
+
}
|
|
1379
|
+
let userTokenAccountAddress = this.userTokenAccountAddress;
|
|
1380
|
+
if ((_a = this.secondaryMint) === null || _a === void 0 ? void 0 : _a.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
1381
|
+
if (!this.additionalTokenAccountAddress) {
|
|
1382
|
+
throw new Error(`Additional token account address not found ${this.secondaryMint}`);
|
|
1383
|
+
}
|
|
1384
|
+
userTokenAccountAddress = this.additionalTokenAccountAddress;
|
|
1385
|
+
}
|
|
1386
|
+
const userWSOLAccountInfo = yield this.kaminoMarket.getConnection().getAccountInfo(userTokenAccountAddress);
|
|
1387
|
+
const rentExempt = yield spl_token_1.Token.getMinBalanceRentForExemptAccount(this.kaminoMarket.getConnection());
|
|
1388
|
+
// Add rent exemption lamports for WSOL accounts that need to be pre-funded for inflow/send transactions
|
|
1389
|
+
const sendAction = action === 'deposit' ||
|
|
1390
|
+
action === 'repay' ||
|
|
1391
|
+
action === 'mint' ||
|
|
1392
|
+
(action === 'liquidate' && ((_b = this.secondaryMint) === null || _b === void 0 ? void 0 : _b.equals(utils_1.WRAPPED_SOL_MINT))); // only sync WSOL amount if liquidator repays SOL which is secondaryMint
|
|
1393
|
+
const transferLamportsIx = web3_js_1.SystemProgram.transfer({
|
|
1394
|
+
fromPubkey: this.owner,
|
|
1395
|
+
toPubkey: userTokenAccountAddress,
|
|
1396
|
+
lamports: (userWSOLAccountInfo ? 0 : rentExempt) + (sendAction ? parseInt(safeRepay.toString(), 10) : 0),
|
|
1397
|
+
});
|
|
1398
|
+
preIxs.push(transferLamportsIx);
|
|
1399
|
+
preIxsLabels.push(`TransferLamportsToUserAtaSOL[${userTokenAccountAddress}]`);
|
|
1400
|
+
const closeWSOLAccountIx = spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, userTokenAccountAddress, this.owner, this.owner, []);
|
|
1401
|
+
const syncIx = (0, utils_1.syncNative)(userTokenAccountAddress);
|
|
1402
|
+
if (userWSOLAccountInfo) {
|
|
1403
|
+
if (sendAction) {
|
|
1404
|
+
preIxs.push(syncIx);
|
|
1405
|
+
preIxsLabels.push(`SyncUserAtaSOL[${userTokenAccountAddress}]`);
|
|
1406
|
+
}
|
|
1407
|
+
else {
|
|
1408
|
+
postIxs.push(closeWSOLAccountIx);
|
|
1409
|
+
postIxsLabels.push(`CloseUserAtaSOL[${userTokenAccountAddress}]`);
|
|
1410
|
+
}
|
|
1411
|
+
}
|
|
1412
|
+
else {
|
|
1413
|
+
const [, createUserWSOLAccountIx] = yield (0, utils_1.createAssociatedTokenAccountIdempotentInstruction)(this.owner, spl_token_1.NATIVE_MINT, this.owner, userTokenAccountAddress);
|
|
1414
|
+
preIxs.push(createUserWSOLAccountIx);
|
|
1415
|
+
preIxsLabels.push(`CreateUserAtaSOL[${userTokenAccountAddress}]`);
|
|
1416
|
+
preIxs.push(syncIx);
|
|
1417
|
+
preIxsLabels.push(`SyncUserAtaSOL[${userTokenAccountAddress}]`);
|
|
1418
|
+
postIxs.push(closeWSOLAccountIx);
|
|
1419
|
+
postIxsLabels.push(`CloseUserAtaSOL[${userTokenAccountAddress}]`);
|
|
1420
|
+
}
|
|
1421
|
+
// TODO: Consider for liquidations and other types of actions if we have to split up some ixs in 2-3 txs
|
|
1422
|
+
// if (this.positions && this.positions >= POSITION_LIMIT) {
|
|
1423
|
+
// this.preTxnIxs.push(...preIxs);
|
|
1424
|
+
// this.preTxnIxsLabels.push(...preIxsLabels);
|
|
1425
|
+
// this.postTxnIxs.push(...postIxs);
|
|
1426
|
+
// this.postTxnIxsLabels.push(...postIxsLabels);
|
|
1427
|
+
// } else {
|
|
1428
|
+
// }
|
|
1429
|
+
this.setupIxs.unshift(...preIxs);
|
|
1430
|
+
this.setupIxsLabels.unshift(...preIxsLabels);
|
|
1431
|
+
this.cleanupIxs.push(...postIxs);
|
|
1432
|
+
this.cleanupIxsLabels.push(...postIxsLabels);
|
|
1433
|
+
});
|
|
1434
|
+
}
|
|
1435
|
+
static initializeMultiTokenAction(kaminoMarket, action, inflowAmount, inflowTokenMint, outflowTokenMint, payer, obligationOwner, obligation, outflowAmount, referrer = web3_js_1.PublicKey.default, currentSlot = 0) {
|
|
1436
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1437
|
+
const inflowReserve = kaminoMarket.getReserveByMint(inflowTokenMint);
|
|
1438
|
+
const outflowReserve = kaminoMarket.getReserveByMint(outflowTokenMint);
|
|
1439
|
+
if (!outflowReserve || !inflowReserve) {
|
|
1440
|
+
throw new Error('reserve states are not fetched');
|
|
1441
|
+
}
|
|
1442
|
+
const { userTokenAccountAddress: userOutflowTokenAccountAddress, userCollateralAccountAddress: userOutflowCollateralAccountAddress, } = yield KaminoAction.getUserAccountAddresses(payer, outflowReserve.state);
|
|
1443
|
+
const { userTokenAccountAddress: userInflowTokenAccountAddress, userCollateralAccountAddress: userInflowCollateralAccountAddress, } = yield KaminoAction.getUserAccountAddresses(payer, inflowReserve.state);
|
|
1444
|
+
const { kaminoObligation, depositReserves, borrowReserves, distinctReserveCount } = yield KaminoAction.loadObligation(action, kaminoMarket, obligationOwner, inflowReserve.address, obligation, outflowReserve.address);
|
|
1445
|
+
const [_, userMetadata] = yield kaminoMarket.getUserMetadata(payer);
|
|
1446
|
+
if (userMetadata) {
|
|
1447
|
+
referrer = userMetadata.referrer;
|
|
1448
|
+
}
|
|
1449
|
+
const referrerKey = kaminoObligation ? kaminoObligation.state.referrer : referrer;
|
|
1450
|
+
let userTokenAccountAddress;
|
|
1451
|
+
let userCollateralAccountAddress;
|
|
1452
|
+
let additionalUserTokenAccountAddress;
|
|
1453
|
+
let secondaryMint;
|
|
1454
|
+
let primaryMint;
|
|
1455
|
+
if (action === 'liquidate') {
|
|
1456
|
+
userTokenAccountAddress = userOutflowTokenAccountAddress;
|
|
1457
|
+
userCollateralAccountAddress = userOutflowCollateralAccountAddress;
|
|
1458
|
+
additionalUserTokenAccountAddress = userInflowTokenAccountAddress;
|
|
1459
|
+
primaryMint = outflowTokenMint;
|
|
1460
|
+
secondaryMint = inflowTokenMint;
|
|
1461
|
+
}
|
|
1462
|
+
else if (action === 'depositAndBorrow') {
|
|
1463
|
+
userTokenAccountAddress = userInflowTokenAccountAddress;
|
|
1464
|
+
userCollateralAccountAddress = userInflowCollateralAccountAddress;
|
|
1465
|
+
additionalUserTokenAccountAddress = userOutflowTokenAccountAddress;
|
|
1466
|
+
primaryMint = inflowTokenMint;
|
|
1467
|
+
secondaryMint = outflowTokenMint;
|
|
1468
|
+
}
|
|
1469
|
+
else if (action === 'repayAndWithdraw') {
|
|
1470
|
+
primaryMint = inflowTokenMint;
|
|
1471
|
+
secondaryMint = outflowTokenMint;
|
|
1472
|
+
userTokenAccountAddress = userInflowTokenAccountAddress;
|
|
1473
|
+
userCollateralAccountAddress = userOutflowCollateralAccountAddress;
|
|
1474
|
+
additionalUserTokenAccountAddress = userOutflowTokenAccountAddress;
|
|
1475
|
+
}
|
|
1476
|
+
else {
|
|
1477
|
+
throw new Error('Invalid action');
|
|
1478
|
+
}
|
|
1479
|
+
return new KaminoAction(kaminoMarket, payer, kaminoObligation || obligation, userTokenAccountAddress, userCollateralAccountAddress, primaryMint, distinctReserveCount, inflowAmount, depositReserves, borrowReserves, inflowReserve, currentSlot, undefined, secondaryMint, additionalUserTokenAccountAddress, outflowReserve, outflowAmount, referrerKey);
|
|
1480
|
+
});
|
|
1481
|
+
}
|
|
1482
|
+
static initializeWithdrawReferrerFees(mint, owner, kaminoMarket, currentSlot = 0, hostAta) {
|
|
1483
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
1484
|
+
const reserve = kaminoMarket.getReserveByMint(mint);
|
|
1485
|
+
if (reserve === undefined) {
|
|
1486
|
+
throw new Error(`Reserve ${mint} not found in market ${kaminoMarket.getAddress().toBase58()}`);
|
|
1487
|
+
}
|
|
1488
|
+
const { atas, createAtasIxns } = yield (0, utils_1.getAtasWithCreateIxnsIfMissing)(kaminoMarket.getConnection(), owner, [
|
|
1489
|
+
reserve.getLiquidityMint(),
|
|
1490
|
+
]);
|
|
1491
|
+
const userTokenAccountAddress = atas[0];
|
|
1492
|
+
return {
|
|
1493
|
+
axn: new KaminoAction(kaminoMarket, owner, new ObligationType_1.VanillaObligation(kaminoMarket.programId), userTokenAccountAddress, web3_js_1.PublicKey.default, mint, 0, new bn_js_1.default(0), [], [], reserve, currentSlot, hostAta, undefined, undefined, undefined, undefined, undefined),
|
|
1494
|
+
createAtasIxns,
|
|
1495
|
+
};
|
|
1496
|
+
});
|
|
1497
|
+
}
|
|
1498
|
+
getObligationPda() {
|
|
1499
|
+
return this.obligation
|
|
1500
|
+
? this.obligation.obligationAddress
|
|
1501
|
+
: this.obligationType.toPda(this.kaminoMarket.getAddress(), this.owner);
|
|
1502
|
+
}
|
|
1503
|
+
}
|
|
1504
|
+
exports.KaminoAction = KaminoAction;
|
|
1505
|
+
//# sourceMappingURL=action.js.map
|