@kamino-finance/klend-sdk 3.2.26 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/classes/action.d.ts +1 -0
- package/dist/classes/action.d.ts.map +1 -0
- package/dist/classes/action.js +1132 -1188
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/curve.d.ts +1 -0
- package/dist/classes/curve.d.ts.map +1 -0
- package/dist/classes/curve.js +1 -1
- package/dist/classes/curve.js.map +1 -1
- package/dist/classes/fraction.d.ts +1 -0
- package/dist/classes/fraction.d.ts.map +1 -0
- package/dist/classes/fraction.js +7 -6
- package/dist/classes/fraction.js.map +1 -1
- package/dist/classes/index.d.ts +1 -0
- package/dist/classes/index.d.ts.map +1 -0
- package/dist/classes/index.js.map +1 -1
- package/dist/classes/jupiterPerps.d.ts +1 -0
- package/dist/classes/jupiterPerps.d.ts.map +1 -0
- package/dist/classes/jupiterPerps.js +7 -18
- package/dist/classes/jupiterPerps.js.map +1 -1
- package/dist/classes/manager.d.ts +1 -0
- package/dist/classes/manager.d.ts.map +1 -0
- package/dist/classes/manager.js +132 -159
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/market.d.ts +2 -1
- package/dist/classes/market.d.ts.map +1 -0
- package/dist/classes/market.js +622 -716
- package/dist/classes/market.js.map +1 -1
- package/dist/classes/obligation.d.ts +1 -0
- package/dist/classes/obligation.d.ts.map +1 -0
- package/dist/classes/obligation.js +60 -62
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/reserve.d.ts +1 -0
- package/dist/classes/reserve.d.ts.map +1 -0
- package/dist/classes/reserve.js +121 -130
- package/dist/classes/reserve.js.map +1 -1
- package/dist/classes/shared.d.ts +2 -1
- package/dist/classes/shared.d.ts.map +1 -0
- package/dist/classes/shared.js.map +1 -1
- package/dist/classes/utils.d.ts +1 -0
- package/dist/classes/utils.d.ts.map +1 -0
- package/dist/classes/utils.js +3 -3
- package/dist/classes/utils.js.map +1 -1
- package/dist/classes/vault.d.ts +1 -0
- package/dist/classes/vault.d.ts.map +1 -0
- package/dist/classes/vault.js +334 -354
- package/dist/classes/vault.js.map +1 -1
- package/dist/client_kamino_manager.d.ts +1 -0
- package/dist/client_kamino_manager.d.ts.map +1 -0
- package/dist/client_kamino_manager.js +323 -326
- package/dist/client_kamino_manager.js.map +1 -1
- package/dist/idl_codegen/accounts/LendingMarket.d.ts +1 -0
- package/dist/idl_codegen/accounts/LendingMarket.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/LendingMarket.js +89 -55
- package/dist/idl_codegen/accounts/LendingMarket.js.map +1 -1
- package/dist/idl_codegen/accounts/Obligation.d.ts +1 -0
- package/dist/idl_codegen/accounts/Obligation.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/Obligation.js +87 -56
- package/dist/idl_codegen/accounts/Obligation.js.map +1 -1
- package/dist/idl_codegen/accounts/ReferrerState.d.ts +1 -0
- package/dist/idl_codegen/accounts/ReferrerState.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/ReferrerState.js +22 -33
- package/dist/idl_codegen/accounts/ReferrerState.js.map +1 -1
- package/dist/idl_codegen/accounts/ReferrerTokenState.d.ts +1 -0
- package/dist/idl_codegen/accounts/ReferrerTokenState.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/ReferrerTokenState.js +35 -37
- package/dist/idl_codegen/accounts/ReferrerTokenState.js.map +1 -1
- package/dist/idl_codegen/accounts/Reserve.d.ts +1 -0
- package/dist/idl_codegen/accounts/Reserve.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/Reserve.js +60 -49
- package/dist/idl_codegen/accounts/Reserve.js.map +1 -1
- package/dist/idl_codegen/accounts/ShortUrl.d.ts +1 -0
- package/dist/idl_codegen/accounts/ShortUrl.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/ShortUrl.js +22 -33
- package/dist/idl_codegen/accounts/ShortUrl.js.map +1 -1
- package/dist/idl_codegen/accounts/UserMetadata.d.ts +1 -0
- package/dist/idl_codegen/accounts/UserMetadata.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/UserMetadata.js +34 -37
- package/dist/idl_codegen/accounts/UserMetadata.js.map +1 -1
- package/dist/idl_codegen/accounts/UserState.d.ts +1 -0
- package/dist/idl_codegen/accounts/UserState.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/UserState.js +77 -48
- package/dist/idl_codegen/accounts/UserState.js.map +1 -1
- package/dist/idl_codegen/accounts/index.d.ts +1 -0
- package/dist/idl_codegen/accounts/index.d.ts.map +1 -0
- package/dist/idl_codegen/accounts/index.js.map +1 -1
- package/dist/idl_codegen/errors/anchor.d.ts +1 -0
- package/dist/idl_codegen/errors/anchor.d.ts.map +1 -0
- package/dist/idl_codegen/errors/anchor.js +270 -216
- package/dist/idl_codegen/errors/anchor.js.map +1 -1
- package/dist/idl_codegen/errors/custom.d.ts +1 -0
- package/dist/idl_codegen/errors/custom.d.ts.map +1 -0
- package/dist/idl_codegen/errors/custom.js +560 -448
- package/dist/idl_codegen/errors/custom.js.map +1 -1
- package/dist/idl_codegen/errors/index.d.ts +1 -0
- package/dist/idl_codegen/errors/index.d.ts.map +1 -0
- package/dist/idl_codegen/errors/index.js.map +1 -1
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.d.ts +1 -0
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.js.map +1 -1
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.d.ts +1 -0
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.js.map +1 -1
- package/dist/idl_codegen/instructions/depositObligationCollateral.d.ts +1 -0
- package/dist/idl_codegen/instructions/depositObligationCollateral.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/depositObligationCollateral.js.map +1 -1
- package/dist/idl_codegen/instructions/depositReserveLiquidity.d.ts +1 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidity.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidity.js.map +1 -1
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.d.ts +1 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.js.map +1 -1
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.d.ts +1 -0
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.js.map +1 -1
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.d.ts +1 -0
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.js.map +1 -1
- package/dist/idl_codegen/instructions/idlMissingTypes.d.ts +1 -0
- package/dist/idl_codegen/instructions/idlMissingTypes.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/idlMissingTypes.js.map +1 -1
- package/dist/idl_codegen/instructions/index.d.ts +1 -0
- package/dist/idl_codegen/instructions/index.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/index.js.map +1 -1
- package/dist/idl_codegen/instructions/initFarmsForReserve.d.ts +1 -0
- package/dist/idl_codegen/instructions/initFarmsForReserve.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/initFarmsForReserve.js.map +1 -1
- package/dist/idl_codegen/instructions/initLendingMarket.d.ts +1 -0
- package/dist/idl_codegen/instructions/initLendingMarket.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/initLendingMarket.js.map +1 -1
- package/dist/idl_codegen/instructions/initObligation.d.ts +1 -0
- package/dist/idl_codegen/instructions/initObligation.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/initObligation.js.map +1 -1
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.d.ts +1 -0
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.js.map +1 -1
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.d.ts +1 -0
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.js.map +1 -1
- package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts +1 -0
- package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/initReferrerTokenState.js.map +1 -1
- package/dist/idl_codegen/instructions/initReserve.d.ts +1 -0
- package/dist/idl_codegen/instructions/initReserve.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/initReserve.js.map +1 -1
- package/dist/idl_codegen/instructions/initUserMetadata.d.ts +1 -0
- package/dist/idl_codegen/instructions/initUserMetadata.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/initUserMetadata.js.map +1 -1
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.d.ts +1 -0
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.js.map +1 -1
- package/dist/idl_codegen/instructions/redeemFees.d.ts +1 -0
- package/dist/idl_codegen/instructions/redeemFees.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/redeemFees.js.map +1 -1
- package/dist/idl_codegen/instructions/redeemReserveCollateral.d.ts +1 -0
- package/dist/idl_codegen/instructions/redeemReserveCollateral.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/redeemReserveCollateral.js.map +1 -1
- package/dist/idl_codegen/instructions/refreshObligation.d.ts +1 -0
- package/dist/idl_codegen/instructions/refreshObligation.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/refreshObligation.js.map +1 -1
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.d.ts +1 -0
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.js.map +1 -1
- package/dist/idl_codegen/instructions/refreshReserve.d.ts +1 -0
- package/dist/idl_codegen/instructions/refreshReserve.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/refreshReserve.js.map +1 -1
- package/dist/idl_codegen/instructions/refreshReservesBatch.d.ts +1 -0
- package/dist/idl_codegen/instructions/refreshReservesBatch.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/refreshReservesBatch.js.map +1 -1
- package/dist/idl_codegen/instructions/repayObligationLiquidity.d.ts +1 -0
- package/dist/idl_codegen/instructions/repayObligationLiquidity.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/repayObligationLiquidity.js.map +1 -1
- package/dist/idl_codegen/instructions/requestElevationGroup.d.ts +1 -0
- package/dist/idl_codegen/instructions/requestElevationGroup.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/requestElevationGroup.js.map +1 -1
- package/dist/idl_codegen/instructions/socializeLoss.d.ts +1 -0
- package/dist/idl_codegen/instructions/socializeLoss.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/socializeLoss.js.map +1 -1
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.d.ts +1 -0
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.js.map +1 -1
- package/dist/idl_codegen/instructions/updateLendingMarket.d.ts +1 -0
- package/dist/idl_codegen/instructions/updateLendingMarket.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/updateLendingMarket.js.map +1 -1
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.d.ts +1 -0
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.js.map +1 -1
- package/dist/idl_codegen/instructions/updateReserveConfig.d.ts +1 -0
- package/dist/idl_codegen/instructions/updateReserveConfig.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/updateReserveConfig.js.map +1 -1
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.d.ts +1 -0
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.js.map +1 -1
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.d.ts +1 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.js.map +1 -1
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.d.ts +1 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js.map +1 -1
- package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts +1 -0
- package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/withdrawProtocolFee.js.map +1 -1
- package/dist/idl_codegen/instructions/withdrawReferrerFees.d.ts +1 -0
- package/dist/idl_codegen/instructions/withdrawReferrerFees.d.ts.map +1 -0
- package/dist/idl_codegen/instructions/withdrawReferrerFees.js.map +1 -1
- package/dist/idl_codegen/programId.d.ts +1 -0
- package/dist/idl_codegen/programId.d.ts.map +1 -0
- package/dist/idl_codegen/programId.js.map +1 -1
- package/dist/idl_codegen/types/AssetTier.d.ts +1 -0
- package/dist/idl_codegen/types/AssetTier.d.ts.map +1 -0
- package/dist/idl_codegen/types/AssetTier.js +12 -18
- package/dist/idl_codegen/types/AssetTier.js.map +1 -1
- package/dist/idl_codegen/types/BigFractionBytes.d.ts +1 -0
- package/dist/idl_codegen/types/BigFractionBytes.d.ts.map +1 -0
- package/dist/idl_codegen/types/BigFractionBytes.js +2 -0
- package/dist/idl_codegen/types/BigFractionBytes.js.map +1 -1
- package/dist/idl_codegen/types/BorrowRateCurve.d.ts +1 -0
- package/dist/idl_codegen/types/BorrowRateCurve.d.ts.map +1 -0
- package/dist/idl_codegen/types/BorrowRateCurve.js +2 -1
- package/dist/idl_codegen/types/BorrowRateCurve.js.map +1 -1
- package/dist/idl_codegen/types/CurvePoint.d.ts +1 -0
- package/dist/idl_codegen/types/CurvePoint.d.ts.map +1 -0
- package/dist/idl_codegen/types/CurvePoint.js +2 -0
- package/dist/idl_codegen/types/CurvePoint.js.map +1 -1
- package/dist/idl_codegen/types/ElevationGroup.d.ts +1 -0
- package/dist/idl_codegen/types/ElevationGroup.d.ts.map +1 -0
- package/dist/idl_codegen/types/ElevationGroup.js +10 -0
- package/dist/idl_codegen/types/ElevationGroup.js.map +1 -1
- package/dist/idl_codegen/types/FeeCalculation.d.ts +1 -0
- package/dist/idl_codegen/types/FeeCalculation.d.ts.map +1 -0
- package/dist/idl_codegen/types/FeeCalculation.js +8 -12
- package/dist/idl_codegen/types/FeeCalculation.js.map +1 -1
- package/dist/idl_codegen/types/InitObligationArgs.d.ts +1 -0
- package/dist/idl_codegen/types/InitObligationArgs.d.ts.map +1 -0
- package/dist/idl_codegen/types/InitObligationArgs.js +2 -0
- package/dist/idl_codegen/types/InitObligationArgs.js.map +1 -1
- package/dist/idl_codegen/types/LastUpdate.d.ts +1 -0
- package/dist/idl_codegen/types/LastUpdate.d.ts.map +1 -0
- package/dist/idl_codegen/types/LastUpdate.js +7 -0
- package/dist/idl_codegen/types/LastUpdate.js.map +1 -1
- package/dist/idl_codegen/types/ObligationCollateral.d.ts +1 -0
- package/dist/idl_codegen/types/ObligationCollateral.d.ts.map +1 -0
- package/dist/idl_codegen/types/ObligationCollateral.js +14 -0
- package/dist/idl_codegen/types/ObligationCollateral.js.map +1 -1
- package/dist/idl_codegen/types/ObligationLiquidity.d.ts +1 -0
- package/dist/idl_codegen/types/ObligationLiquidity.d.ts.map +1 -0
- package/dist/idl_codegen/types/ObligationLiquidity.js +17 -1
- package/dist/idl_codegen/types/ObligationLiquidity.js.map +1 -1
- package/dist/idl_codegen/types/PriceHeuristic.d.ts +1 -0
- package/dist/idl_codegen/types/PriceHeuristic.d.ts.map +1 -0
- package/dist/idl_codegen/types/PriceHeuristic.js +6 -0
- package/dist/idl_codegen/types/PriceHeuristic.js.map +1 -1
- package/dist/idl_codegen/types/PythConfiguration.d.ts +1 -0
- package/dist/idl_codegen/types/PythConfiguration.d.ts.map +1 -0
- package/dist/idl_codegen/types/PythConfiguration.js +2 -0
- package/dist/idl_codegen/types/PythConfiguration.js.map +1 -1
- package/dist/idl_codegen/types/ReserveCollateral.d.ts +1 -0
- package/dist/idl_codegen/types/ReserveCollateral.d.ts.map +1 -0
- package/dist/idl_codegen/types/ReserveCollateral.js +8 -0
- package/dist/idl_codegen/types/ReserveCollateral.js.map +1 -1
- package/dist/idl_codegen/types/ReserveConfig.d.ts +1 -0
- package/dist/idl_codegen/types/ReserveConfig.d.ts.map +1 -0
- package/dist/idl_codegen/types/ReserveConfig.js +78 -5
- package/dist/idl_codegen/types/ReserveConfig.js.map +1 -1
- package/dist/idl_codegen/types/ReserveFarmKind.d.ts +1 -0
- package/dist/idl_codegen/types/ReserveFarmKind.d.ts.map +1 -0
- package/dist/idl_codegen/types/ReserveFarmKind.js +8 -12
- package/dist/idl_codegen/types/ReserveFarmKind.js.map +1 -1
- package/dist/idl_codegen/types/ReserveFees.d.ts +1 -0
- package/dist/idl_codegen/types/ReserveFees.d.ts.map +1 -0
- package/dist/idl_codegen/types/ReserveFees.js +16 -0
- package/dist/idl_codegen/types/ReserveFees.js.map +1 -1
- package/dist/idl_codegen/types/ReserveLiquidity.d.ts +1 -0
- package/dist/idl_codegen/types/ReserveLiquidity.d.ts.map +1 -0
- package/dist/idl_codegen/types/ReserveLiquidity.js +43 -1
- package/dist/idl_codegen/types/ReserveLiquidity.js.map +1 -1
- package/dist/idl_codegen/types/ReserveStatus.d.ts +1 -0
- package/dist/idl_codegen/types/ReserveStatus.d.ts.map +1 -0
- package/dist/idl_codegen/types/ReserveStatus.js +12 -18
- package/dist/idl_codegen/types/ReserveStatus.js.map +1 -1
- package/dist/idl_codegen/types/ScopeConfiguration.d.ts +1 -0
- package/dist/idl_codegen/types/ScopeConfiguration.d.ts.map +1 -0
- package/dist/idl_codegen/types/ScopeConfiguration.js +6 -0
- package/dist/idl_codegen/types/ScopeConfiguration.js.map +1 -1
- package/dist/idl_codegen/types/SwitchboardConfiguration.d.ts +1 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.d.ts.map +1 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.js +3 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.js.map +1 -1
- package/dist/idl_codegen/types/TokenInfo.d.ts +1 -0
- package/dist/idl_codegen/types/TokenInfo.d.ts.map +1 -0
- package/dist/idl_codegen/types/TokenInfo.js +27 -4
- package/dist/idl_codegen/types/TokenInfo.js.map +1 -1
- package/dist/idl_codegen/types/UpdateConfigMode.d.ts +1 -0
- package/dist/idl_codegen/types/UpdateConfigMode.d.ts.map +1 -0
- package/dist/idl_codegen/types/UpdateConfigMode.js +188 -282
- package/dist/idl_codegen/types/UpdateConfigMode.js.map +1 -1
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts +1 -0
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts.map +1 -0
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js +41 -33
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js.map +1 -1
- package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts +1 -0
- package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts.map +1 -0
- package/dist/idl_codegen/types/UpdateLendingMarketMode.js +72 -108
- package/dist/idl_codegen/types/UpdateLendingMarketMode.js.map +1 -1
- package/dist/idl_codegen/types/WithdrawalCaps.d.ts +1 -0
- package/dist/idl_codegen/types/WithdrawalCaps.d.ts.map +1 -0
- package/dist/idl_codegen/types/WithdrawalCaps.js +4 -0
- package/dist/idl_codegen/types/WithdrawalCaps.js.map +1 -1
- package/dist/idl_codegen/types/index.d.ts +1 -0
- package/dist/idl_codegen/types/index.d.ts.map +1 -0
- package/dist/idl_codegen/types/index.js.map +1 -1
- package/dist/idl_codegen/zero_padding/ObligationZP.d.ts +1 -0
- package/dist/idl_codegen/zero_padding/ObligationZP.d.ts.map +1 -0
- package/dist/idl_codegen/zero_padding/ObligationZP.js +83 -54
- package/dist/idl_codegen/zero_padding/ObligationZP.js.map +1 -1
- package/dist/idl_codegen/zero_padding/index.d.ts +1 -0
- package/dist/idl_codegen/zero_padding/index.d.ts.map +1 -0
- package/dist/idl_codegen/zero_padding/index.js.map +1 -1
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.d.ts +1 -0
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.d.ts.map +1 -0
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.js +39 -42
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.js.map +1 -1
- package/dist/idl_codegen_jupiter_perps/accounts/index.d.ts +1 -0
- package/dist/idl_codegen_jupiter_perps/accounts/index.d.ts.map +1 -0
- package/dist/idl_codegen_jupiter_perps/accounts/index.js.map +1 -1
- package/dist/idl_codegen_jupiter_perps/programId.d.ts +1 -0
- package/dist/idl_codegen_jupiter_perps/programId.d.ts.map +1 -0
- package/dist/idl_codegen_jupiter_perps/programId.js.map +1 -1
- package/dist/idl_codegen_jupiter_perps/types/Fees.d.ts +1 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.d.ts.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.js +9 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.js.map +1 -1
- package/dist/idl_codegen_jupiter_perps/types/Limit.d.ts +1 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.d.ts.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.js +3 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.js.map +1 -1
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.d.ts +1 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.d.ts.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.js +3 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.js.map +1 -1
- package/dist/idl_codegen_jupiter_perps/types/index.d.ts +1 -0
- package/dist/idl_codegen_jupiter_perps/types/index.d.ts.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/index.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/Reserve.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/accounts/Reserve.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/accounts/Reserve.js +58 -47
- package/dist/idl_codegen_kamino_vault/accounts/Reserve.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.js +51 -47
- package/dist/idl_codegen_kamino_vault/accounts/VaultState.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/accounts/index.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/accounts/index.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/accounts/index.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/anchor.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/errors/anchor.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/errors/anchor.js +270 -216
- package/dist/idl_codegen_kamino_vault/errors/anchor.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/custom.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/errors/custom.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/errors/custom.js +120 -96
- package/dist/idl_codegen_kamino_vault/errors/custom.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/errors/index.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/errors/index.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/errors/index.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/deposit.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/deposit.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/deposit.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/index.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/index.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/index.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/initVault.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/initVault.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/initVault.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/invest.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/invest.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/invest.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/updateReserveAllocation.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/instructions/withdraw.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/withdraw.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/instructions/withdraw.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/programId.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/programId.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/programId.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.js +2 -0
- package/dist/idl_codegen_kamino_vault/types/BigFractionBytes.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.js +2 -1
- package/dist/idl_codegen_kamino_vault/types/BorrowRateCurve.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/CurvePoint.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/CurvePoint.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/CurvePoint.js +2 -0
- package/dist/idl_codegen_kamino_vault/types/CurvePoint.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/LastUpdate.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/LastUpdate.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/LastUpdate.js +7 -0
- package/dist/idl_codegen_kamino_vault/types/LastUpdate.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.js +6 -0
- package/dist/idl_codegen_kamino_vault/types/PriceHeuristic.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/PythConfiguration.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/PythConfiguration.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/PythConfiguration.js +2 -0
- package/dist/idl_codegen_kamino_vault/types/PythConfiguration.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.js +8 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveCollateral.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveConfig.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveConfig.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveConfig.js +77 -5
- package/dist/idl_codegen_kamino_vault/types/ReserveConfig.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveFees.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveFees.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveFees.js +16 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveFees.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.js +41 -1
- package/dist/idl_codegen_kamino_vault/types/ReserveLiquidity.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.js +6 -0
- package/dist/idl_codegen_kamino_vault/types/ScopeConfiguration.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.js +3 -0
- package/dist/idl_codegen_kamino_vault/types/SwitchboardConfiguration.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/TokenInfo.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/TokenInfo.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/TokenInfo.js +27 -4
- package/dist/idl_codegen_kamino_vault/types/TokenInfo.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/VaultAllocation.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/VaultAllocation.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/VaultAllocation.js +8 -0
- package/dist/idl_codegen_kamino_vault/types/VaultAllocation.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/WithdrawalCaps.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/WithdrawalCaps.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/WithdrawalCaps.js +4 -0
- package/dist/idl_codegen_kamino_vault/types/WithdrawalCaps.js.map +1 -1
- package/dist/idl_codegen_kamino_vault/types/index.d.ts +1 -0
- package/dist/idl_codegen_kamino_vault/types/index.d.ts.map +1 -0
- package/dist/idl_codegen_kamino_vault/types/index.js.map +1 -1
- package/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js.map +1 -1
- package/dist/lending_operations/index.d.ts +1 -0
- package/dist/lending_operations/index.d.ts.map +1 -0
- package/dist/lending_operations/index.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts +1 -0
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -0
- package/dist/lending_operations/repay_with_collateral_calcs.js +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts +1 -0
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -0
- package/dist/lending_operations/repay_with_collateral_operations.js +15 -24
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/leverage/calcs.d.ts +1 -0
- package/dist/leverage/calcs.d.ts.map +1 -0
- package/dist/leverage/calcs.js +119 -132
- package/dist/leverage/calcs.js.map +1 -1
- package/dist/leverage/index.d.ts +1 -0
- package/dist/leverage/index.d.ts.map +1 -0
- package/dist/leverage/index.js.map +1 -1
- package/dist/leverage/instructions.d.ts +1 -0
- package/dist/leverage/instructions.d.ts.map +1 -0
- package/dist/leverage/instructions.js.map +1 -1
- package/dist/leverage/operations.d.ts +1 -0
- package/dist/leverage/operations.d.ts.map +1 -0
- package/dist/leverage/operations.js +75 -84
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/utils.d.ts +1 -0
- package/dist/leverage/utils.d.ts.map +1 -0
- package/dist/leverage/utils.js +73 -88
- package/dist/leverage/utils.js.map +1 -1
- package/dist/lib.d.ts +1 -0
- package/dist/lib.d.ts.map +1 -0
- package/dist/lib.js.map +1 -1
- package/dist/referrals/index.d.ts +1 -0
- package/dist/referrals/index.d.ts.map +1 -0
- package/dist/referrals/index.js.map +1 -1
- package/dist/referrals/instructions.d.ts +1 -0
- package/dist/referrals/instructions.d.ts.map +1 -0
- package/dist/referrals/instructions.js +8 -17
- package/dist/referrals/instructions.js.map +1 -1
- package/dist/referrals/operations.d.ts +2 -1
- package/dist/referrals/operations.d.ts.map +1 -0
- package/dist/referrals/operations.js +119 -143
- package/dist/referrals/operations.js.map +1 -1
- package/dist/utils/ObligationType.d.ts +1 -0
- package/dist/utils/ObligationType.d.ts.map +1 -0
- package/dist/utils/ObligationType.js +16 -7
- package/dist/utils/ObligationType.js.map +1 -1
- package/dist/utils/api.d.ts +1 -0
- package/dist/utils/api.d.ts.map +1 -0
- package/dist/utils/api.js +10 -21
- package/dist/utils/api.js.map +1 -1
- package/dist/utils/ata.d.ts +1 -0
- package/dist/utils/ata.d.ts.map +1 -0
- package/dist/utils/ata.js +25 -38
- package/dist/utils/ata.js.map +1 -1
- package/dist/utils/constants.d.ts +1 -0
- package/dist/utils/constants.d.ts.map +1 -0
- package/dist/utils/constants.js +1 -1
- package/dist/utils/constants.js.map +1 -1
- package/dist/utils/idl.d.ts +1 -0
- package/dist/utils/idl.d.ts.map +1 -0
- package/dist/utils/idl.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/instruction.d.ts +1 -0
- package/dist/utils/instruction.d.ts.map +1 -0
- package/dist/utils/instruction.js +116 -143
- package/dist/utils/instruction.js.map +1 -1
- package/dist/utils/kamino.d.ts +1 -0
- package/dist/utils/kamino.d.ts.map +1 -0
- package/dist/utils/kamino.js +4 -15
- package/dist/utils/kamino.js.map +1 -1
- package/dist/utils/layout.d.ts +1 -0
- package/dist/utils/layout.d.ts.map +1 -0
- package/dist/utils/layout.js.map +1 -1
- package/dist/utils/lookupTable.d.ts +1 -0
- package/dist/utils/lookupTable.d.ts.map +1 -0
- package/dist/utils/lookupTable.js +14 -27
- package/dist/utils/lookupTable.js.map +1 -1
- package/dist/utils/managerTypes.d.ts +1 -0
- package/dist/utils/managerTypes.d.ts.map +1 -0
- package/dist/utils/managerTypes.js +32 -3
- package/dist/utils/managerTypes.js.map +1 -1
- package/dist/utils/oracle.d.ts +2 -1
- package/dist/utils/oracle.d.ts.map +1 -0
- package/dist/utils/oracle.js +71 -85
- package/dist/utils/oracle.js.map +1 -1
- package/dist/utils/pubkey.d.ts +1 -0
- package/dist/utils/pubkey.d.ts.map +1 -0
- package/dist/utils/pubkey.js +7 -9
- package/dist/utils/pubkey.js.map +1 -1
- package/dist/utils/rpc.d.ts +1 -0
- package/dist/utils/rpc.d.ts.map +1 -0
- package/dist/utils/rpc.js +48 -70
- package/dist/utils/rpc.js.map +1 -1
- package/dist/utils/seeds.d.ts +1 -0
- package/dist/utils/seeds.d.ts.map +1 -0
- package/dist/utils/seeds.js.map +1 -1
- package/dist/utils/sendTransactionsUtils.d.ts +1 -0
- package/dist/utils/sendTransactionsUtils.d.ts.map +1 -0
- package/dist/utils/sendTransactionsUtils.js +82 -82
- package/dist/utils/sendTransactionsUtils.js.map +1 -1
- package/dist/utils/slots.d.ts +1 -0
- package/dist/utils/slots.d.ts.map +1 -0
- package/dist/utils/slots.js.map +1 -1
- package/dist/utils/syncNative.d.ts +1 -0
- package/dist/utils/syncNative.d.ts.map +1 -0
- package/dist/utils/syncNative.js.map +1 -1
- package/dist/utils/userMetadata.d.ts +1 -0
- package/dist/utils/userMetadata.d.ts.map +1 -0
- package/dist/utils/userMetadata.js +41 -53
- package/dist/utils/userMetadata.js.map +1 -1
- package/package.json +14 -8
- package/src/classes/action.ts +2884 -0
- package/src/classes/curve.ts +29 -0
- package/src/classes/fraction.ts +65 -0
- package/src/classes/index.ts +10 -0
- package/src/classes/jupiterPerps.ts +14 -0
- package/src/classes/manager.ts +746 -0
- package/src/classes/market.ts +1459 -0
- package/src/classes/obligation.ts +1315 -0
- package/src/classes/reserve.ts +1980 -0
- package/src/classes/shared.ts +57 -0
- package/src/classes/utils.ts +192 -0
- package/src/classes/vault.ts +808 -0
- package/src/client.ts +446 -0
- package/src/client_kamino_manager.ts +791 -0
- package/src/global.d.ts +1 -0
- package/src/idl_codegen/accounts/LendingMarket.ts +364 -0
- package/src/idl_codegen/accounts/Obligation.ts +349 -0
- package/src/idl_codegen/accounts/ReferrerState.ts +97 -0
- package/src/idl_codegen/accounts/ReferrerTokenState.ts +145 -0
- package/src/idl_codegen/accounts/Reserve.ts +248 -0
- package/src/idl_codegen/accounts/ShortUrl.ts +97 -0
- package/src/idl_codegen/accounts/UserMetadata.ts +142 -0
- package/src/idl_codegen/accounts/UserState.ts +301 -0
- package/src/idl_codegen/accounts/index.ts +19 -0
- package/src/idl_codegen/errors/anchor.ts +773 -0
- package/src/idl_codegen/errors/custom.ts +1633 -0
- package/src/idl_codegen/errors/index.ts +62 -0
- package/src/idl_codegen/instructions/borrowObligationLiquidity.ts +82 -0
- package/src/idl_codegen/instructions/deleteReferrerStateAndShortUrl.ts +30 -0
- package/src/idl_codegen/instructions/depositObligationCollateral.ts +62 -0
- package/src/idl_codegen/instructions/depositReserveLiquidity.ts +90 -0
- package/src/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.ts +98 -0
- package/src/idl_codegen/instructions/flashBorrowReserveLiquidity.ts +82 -0
- package/src/idl_codegen/instructions/flashRepayReserveLiquidity.ts +83 -0
- package/src/idl_codegen/instructions/idlMissingTypes.ts +62 -0
- package/src/idl_codegen/instructions/index.ts +138 -0
- package/src/idl_codegen/instructions/initFarmsForReserve.ts +62 -0
- package/src/idl_codegen/instructions/initLendingMarket.ts +50 -0
- package/src/idl_codegen/instructions/initObligation.ts +52 -0
- package/src/idl_codegen/instructions/initObligationFarmsForReserve.ts +60 -0
- package/src/idl_codegen/instructions/initReferrerStateAndShortUrl.ts +50 -0
- package/src/idl_codegen/instructions/initReferrerTokenState.ts +46 -0
- package/src/idl_codegen/instructions/initReserve.ts +74 -0
- package/src/idl_codegen/instructions/initUserMetadata.ts +50 -0
- package/src/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.ts +139 -0
- package/src/idl_codegen/instructions/redeemFees.ts +50 -0
- package/src/idl_codegen/instructions/redeemReserveCollateral.ts +94 -0
- package/src/idl_codegen/instructions/refreshObligation.ts +24 -0
- package/src/idl_codegen/instructions/refreshObligationFarmsForReserve.ts +62 -0
- package/src/idl_codegen/instructions/refreshReserve.ts +40 -0
- package/src/idl_codegen/instructions/refreshReservesBatch.ts +29 -0
- package/src/idl_codegen/instructions/repayObligationLiquidity.ts +64 -0
- package/src/idl_codegen/instructions/requestElevationGroup.ts +40 -0
- package/src/idl_codegen/instructions/socializeLoss.ts +48 -0
- package/src/idl_codegen/instructions/updateEntireReserveConfig.ts +45 -0
- package/src/idl_codegen/instructions/updateLendingMarket.ts +43 -0
- package/src/idl_codegen/instructions/updateLendingMarketOwner.ts +28 -0
- package/src/idl_codegen/instructions/updateReserveConfig.ts +52 -0
- package/src/idl_codegen/instructions/updateSingleReserveConfig.ts +48 -0
- package/src/idl_codegen/instructions/withdrawObligationCollateral.ts +68 -0
- package/src/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.ts +102 -0
- package/src/idl_codegen/instructions/withdrawProtocolFee.ts +62 -0
- package/src/idl_codegen/instructions/withdrawReferrerFees.ts +54 -0
- package/src/idl_codegen/programId.ts +13 -0
- package/src/idl_codegen/types/AssetTier.ts +118 -0
- package/src/idl_codegen/types/BigFractionBytes.ts +67 -0
- package/src/idl_codegen/types/BorrowRateCurve.ts +60 -0
- package/src/idl_codegen/types/CurvePoint.ts +64 -0
- package/src/idl_codegen/types/ElevationGroup.ts +133 -0
- package/src/idl_codegen/types/FeeCalculation.ts +90 -0
- package/src/idl_codegen/types/InitObligationArgs.ts +61 -0
- package/src/idl_codegen/types/LastUpdate.ts +95 -0
- package/src/idl_codegen/types/ObligationCollateral.ts +128 -0
- package/src/idl_codegen/types/ObligationLiquidity.ts +159 -0
- package/src/idl_codegen/types/PriceHeuristic.ts +81 -0
- package/src/idl_codegen/types/PythConfiguration.ts +56 -0
- package/src/idl_codegen/types/ReserveCollateral.ts +104 -0
- package/src/idl_codegen/types/ReserveConfig.ts +467 -0
- package/src/idl_codegen/types/ReserveFarmKind.ts +90 -0
- package/src/idl_codegen/types/ReserveFees.ts +122 -0
- package/src/idl_codegen/types/ReserveLiquidity.ts +286 -0
- package/src/idl_codegen/types/ReserveStatus.ts +120 -0
- package/src/idl_codegen/types/ScopeConfiguration.ts +85 -0
- package/src/idl_codegen/types/SwitchboardConfiguration.ts +67 -0
- package/src/idl_codegen/types/TokenInfo.ts +190 -0
- package/src/idl_codegen/types/UpdateConfigMode.ts +1440 -0
- package/src/idl_codegen/types/UpdateLendingMarketConfigValue.ts +376 -0
- package/src/idl_codegen/types/UpdateLendingMarketMode.ts +570 -0
- package/src/idl_codegen/types/WithdrawalCaps.ts +86 -0
- package/src/idl_codegen/types/index.ts +276 -0
- package/src/idl_codegen/zero_padding/ObligationZP.ts +186 -0
- package/src/idl_codegen/zero_padding/index.ts +1 -0
- package/src/idl_codegen_jupiter_perps/accounts/Pool.ts +155 -0
- package/src/idl_codegen_jupiter_perps/accounts/index.ts +2 -0
- package/src/idl_codegen_jupiter_perps/programId.ts +7 -0
- package/src/idl_codegen_jupiter_perps/types/Fees.ts +130 -0
- package/src/idl_codegen_jupiter_perps/types/Limit.ts +72 -0
- package/src/idl_codegen_jupiter_perps/types/PoolApr.ts +69 -0
- package/src/idl_codegen_jupiter_perps/types/index.ts +6 -0
- package/src/idl_codegen_kamino_vault/accounts/Reserve.ts +221 -0
- package/src/idl_codegen_kamino_vault/accounts/VaultState.ts +214 -0
- package/src/idl_codegen_kamino_vault/accounts/index.ts +4 -0
- package/src/idl_codegen_kamino_vault/errors/anchor.ts +764 -0
- package/src/idl_codegen_kamino_vault/errors/custom.ts +344 -0
- package/src/idl_codegen_kamino_vault/errors/index.ts +49 -0
- package/src/idl_codegen_kamino_vault/instructions/deposit.ts +54 -0
- package/src/idl_codegen_kamino_vault/instructions/index.ts +10 -0
- package/src/idl_codegen_kamino_vault/instructions/initVault.ts +35 -0
- package/src/idl_codegen_kamino_vault/instructions/invest.ts +60 -0
- package/src/idl_codegen_kamino_vault/instructions/updateReserveAllocation.ts +58 -0
- package/src/idl_codegen_kamino_vault/instructions/withdraw.ts +81 -0
- package/src/idl_codegen_kamino_vault/programId.ts +7 -0
- package/src/idl_codegen_kamino_vault/types/BigFractionBytes.ts +61 -0
- package/src/idl_codegen_kamino_vault/types/BorrowRateCurve.ts +55 -0
- package/src/idl_codegen_kamino_vault/types/CurvePoint.ts +61 -0
- package/src/idl_codegen_kamino_vault/types/LastUpdate.ts +90 -0
- package/src/idl_codegen_kamino_vault/types/PriceHeuristic.ts +78 -0
- package/src/idl_codegen_kamino_vault/types/PythConfiguration.ts +56 -0
- package/src/idl_codegen_kamino_vault/types/ReserveCollateral.ts +104 -0
- package/src/idl_codegen_kamino_vault/types/ReserveConfig.ts +426 -0
- package/src/idl_codegen_kamino_vault/types/ReserveFees.ts +118 -0
- package/src/idl_codegen_kamino_vault/types/ReserveLiquidity.ts +268 -0
- package/src/idl_codegen_kamino_vault/types/ScopeConfiguration.ts +85 -0
- package/src/idl_codegen_kamino_vault/types/SwitchboardConfiguration.ts +64 -0
- package/src/idl_codegen_kamino_vault/types/TokenInfo.ts +172 -0
- package/src/idl_codegen_kamino_vault/types/VaultAllocation.ts +115 -0
- package/src/idl_codegen_kamino_vault/types/WithdrawalCaps.ts +86 -0
- package/src/idl_codegen_kamino_vault/types/index.ts +30 -0
- package/src/index.ts +1 -0
- package/src/lending_operations/index.ts +2 -0
- package/src/lending_operations/repay_with_collateral_calcs.ts +71 -0
- package/src/lending_operations/repay_with_collateral_operations.ts +318 -0
- package/src/leverage/calcs.ts +465 -0
- package/src/leverage/index.ts +4 -0
- package/src/leverage/instructions.ts +144 -0
- package/src/leverage/operations.ts +1814 -0
- package/src/leverage/utils.ts +273 -0
- package/src/lib.ts +9 -0
- package/src/referrals/index.ts +2 -0
- package/src/referrals/instructions.ts +114 -0
- package/src/referrals/operations.ts +325 -0
- package/src/utils/ObligationType.ts +171 -0
- package/src/utils/api.ts +59 -0
- package/src/utils/ata.ts +195 -0
- package/src/utils/constants.ts +81 -0
- package/src/utils/idl.ts +4 -0
- package/src/utils/index.ts +16 -0
- package/src/utils/instruction.ts +258 -0
- package/src/utils/kamino.ts +12 -0
- package/src/utils/layout.ts +118 -0
- package/src/utils/lookupTable.ts +55 -0
- package/src/utils/managerTypes.ts +456 -0
- package/src/utils/oracle.ts +337 -0
- package/src/utils/pubkey.ts +261 -0
- package/src/utils/rpc.ts +108 -0
- package/src/utils/seeds.ts +183 -0
- package/src/utils/sendTransactionsUtils.ts +207 -0
- package/src/utils/slots.ts +28 -0
- package/src/utils/syncNative.ts +22 -0
- package/src/utils/userMetadata.ts +401 -0
|
@@ -0,0 +1,1814 @@
|
|
|
1
|
+
import { Connection, LAMPORTS_PER_SOL, PublicKey, TransactionInstruction } from '@solana/web3.js';
|
|
2
|
+
import Decimal from 'decimal.js';
|
|
3
|
+
import { KaminoAction, KaminoMarket, KaminoObligation, lamportsToNumberDecimal as fromLamports } from '../classes';
|
|
4
|
+
import { getFlashLoanInstructions } from './instructions';
|
|
5
|
+
|
|
6
|
+
import { numberToLamportsDecimal as toLamports } from '../classes';
|
|
7
|
+
import {
|
|
8
|
+
LeverageObligation,
|
|
9
|
+
MultiplyObligation,
|
|
10
|
+
ObligationType,
|
|
11
|
+
ObligationTypeTag,
|
|
12
|
+
PublicKeySet,
|
|
13
|
+
U64_MAX,
|
|
14
|
+
WRAPPED_SOL_MINT,
|
|
15
|
+
getAssociatedTokenAddress,
|
|
16
|
+
getAtasWithCreateIxnsIfMissing,
|
|
17
|
+
getComputeBudgetAndPriorityFeeIxns,
|
|
18
|
+
getDepositWsolIxns,
|
|
19
|
+
removeBudgetAndAtaIxns,
|
|
20
|
+
} from '../utils';
|
|
21
|
+
import { calcAdjustAmounts, calcWithdrawAmounts, simulateMintKToken, toJson } from './calcs';
|
|
22
|
+
import { TOKEN_PROGRAM_ID, createCloseAccountInstruction } from '@solana/spl-token';
|
|
23
|
+
import {
|
|
24
|
+
Kamino,
|
|
25
|
+
InstructionsWithLookupTables,
|
|
26
|
+
StrategyWithAddress,
|
|
27
|
+
TokenAmounts,
|
|
28
|
+
} from '@kamino-finance/kliquidity-sdk';
|
|
29
|
+
import { getExpectedTokenBalanceAfterBorrow, getKtokenToTokenSwapper, getTokenToKtokenSwapper } from './utils';
|
|
30
|
+
|
|
31
|
+
export type SwapIxnsProvider = (
|
|
32
|
+
amountInLamports: number,
|
|
33
|
+
amountInMint: PublicKey,
|
|
34
|
+
amountOutMint: PublicKey,
|
|
35
|
+
slippagePct: number,
|
|
36
|
+
amountDebtAtaBalance?: Decimal
|
|
37
|
+
) => Promise<[TransactionInstruction[], PublicKey[]]>;
|
|
38
|
+
|
|
39
|
+
export type PriceAinBProvider = (mintA: PublicKey, mintB: PublicKey) => Promise<Decimal>;
|
|
40
|
+
|
|
41
|
+
export type IsKtokenProvider = (token: PublicKey | string) => Promise<boolean>;
|
|
42
|
+
|
|
43
|
+
export type SwapInputs = {
|
|
44
|
+
inputAmountLamports: number;
|
|
45
|
+
inputMint: PublicKey;
|
|
46
|
+
outputMint: PublicKey;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
export type KaminoDepositSwapOverride = (
|
|
50
|
+
kaminoMarket: KaminoMarket,
|
|
51
|
+
kamino: Kamino,
|
|
52
|
+
depositor: PublicKey,
|
|
53
|
+
amountInMint: PublicKey,
|
|
54
|
+
amountOutMint: PublicKey,
|
|
55
|
+
amountIn: Decimal,
|
|
56
|
+
slippageFactor: Decimal,
|
|
57
|
+
amountDebtAtaBalance: Decimal
|
|
58
|
+
) => Promise<InstructionsWithLookupTables>;
|
|
59
|
+
|
|
60
|
+
export const depositLeverageCalcs = (props: {
|
|
61
|
+
depositAmount: Decimal;
|
|
62
|
+
depositTokenIsCollToken: boolean;
|
|
63
|
+
depositTokenIsSol: boolean;
|
|
64
|
+
priceDebtToColl: Decimal;
|
|
65
|
+
targetLeverage: Decimal;
|
|
66
|
+
slippagePct: Decimal;
|
|
67
|
+
flashLoanFee: Decimal;
|
|
68
|
+
}): {
|
|
69
|
+
flashBorrowInCollToken: Decimal;
|
|
70
|
+
initDepositInSol: Decimal;
|
|
71
|
+
debtTokenToBorrow: Decimal;
|
|
72
|
+
collTokenToDeposit: Decimal;
|
|
73
|
+
swapDebtTokenIn: Decimal;
|
|
74
|
+
swapCollTokenExpectedOut: Decimal;
|
|
75
|
+
} => {
|
|
76
|
+
// Initialize local variables from the props object
|
|
77
|
+
const {
|
|
78
|
+
depositAmount,
|
|
79
|
+
depositTokenIsCollToken,
|
|
80
|
+
depositTokenIsSol,
|
|
81
|
+
priceDebtToColl,
|
|
82
|
+
targetLeverage,
|
|
83
|
+
slippagePct,
|
|
84
|
+
flashLoanFee,
|
|
85
|
+
} = props;
|
|
86
|
+
const slippage = slippagePct.div('100');
|
|
87
|
+
|
|
88
|
+
const initDepositInSol = depositTokenIsSol ? depositAmount : new Decimal(0);
|
|
89
|
+
|
|
90
|
+
// Core logic
|
|
91
|
+
if (depositTokenIsCollToken) {
|
|
92
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
93
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
94
|
+
const finalColl = depositAmount.mul(x).div(x.sub(targetLeverage.sub('1').div(y)));
|
|
95
|
+
const debt = finalColl.sub(depositAmount).mul(x);
|
|
96
|
+
const flashBorrowColl = finalColl.sub(depositAmount).mul(flashLoanFee.add('1'));
|
|
97
|
+
|
|
98
|
+
return {
|
|
99
|
+
flashBorrowInCollToken: flashBorrowColl,
|
|
100
|
+
initDepositInSol,
|
|
101
|
+
debtTokenToBorrow: debt,
|
|
102
|
+
collTokenToDeposit: finalColl,
|
|
103
|
+
swapDebtTokenIn: debt,
|
|
104
|
+
swapCollTokenExpectedOut: finalColl.sub(depositAmount),
|
|
105
|
+
};
|
|
106
|
+
} else {
|
|
107
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
108
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
109
|
+
const finalColl = depositAmount.div(x.sub(targetLeverage.sub('1').div(y)));
|
|
110
|
+
const flashBorrowColl = finalColl.mul(flashLoanFee.add('1'));
|
|
111
|
+
const debt = targetLeverage.sub('1').mul(finalColl).div(y);
|
|
112
|
+
|
|
113
|
+
return {
|
|
114
|
+
flashBorrowInCollToken: flashBorrowColl,
|
|
115
|
+
initDepositInSol,
|
|
116
|
+
debtTokenToBorrow: debt,
|
|
117
|
+
collTokenToDeposit: finalColl,
|
|
118
|
+
swapDebtTokenIn: debt.add(depositAmount),
|
|
119
|
+
swapCollTokenExpectedOut: finalColl,
|
|
120
|
+
};
|
|
121
|
+
}
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
export const depositLeverageKtokenCalcs = async (props: {
|
|
125
|
+
kamino: Kamino;
|
|
126
|
+
strategy: StrategyWithAddress;
|
|
127
|
+
debtTokenMint: PublicKey;
|
|
128
|
+
depositAmount: Decimal;
|
|
129
|
+
depositTokenIsCollToken: boolean;
|
|
130
|
+
depositTokenIsSol: boolean;
|
|
131
|
+
priceDebtToColl: Decimal;
|
|
132
|
+
targetLeverage: Decimal;
|
|
133
|
+
slippagePct: Decimal;
|
|
134
|
+
flashLoanFee: Decimal;
|
|
135
|
+
priceAinB: PriceAinBProvider;
|
|
136
|
+
strategyHoldings?: TokenAmounts;
|
|
137
|
+
}): Promise<{
|
|
138
|
+
flashBorrowInDebtToken: Decimal;
|
|
139
|
+
initDepositInSol: Decimal;
|
|
140
|
+
collTokenToDeposit: Decimal;
|
|
141
|
+
debtTokenToBorrow: Decimal; // debtTokenToBorrow = flashBorrowInDebtToken + flashLoanFee
|
|
142
|
+
requiredCollateral: Decimal;
|
|
143
|
+
singleSidedDeposit: Decimal;
|
|
144
|
+
}> => {
|
|
145
|
+
const {
|
|
146
|
+
kamino,
|
|
147
|
+
strategy,
|
|
148
|
+
debtTokenMint,
|
|
149
|
+
depositAmount,
|
|
150
|
+
depositTokenIsCollToken,
|
|
151
|
+
depositTokenIsSol,
|
|
152
|
+
priceDebtToColl,
|
|
153
|
+
targetLeverage,
|
|
154
|
+
slippagePct,
|
|
155
|
+
flashLoanFee,
|
|
156
|
+
priceAinB,
|
|
157
|
+
strategyHoldings,
|
|
158
|
+
} = props;
|
|
159
|
+
const initDepositInSol = depositTokenIsSol ? depositAmount : new Decimal(0);
|
|
160
|
+
const slippage = slippagePct.div('100');
|
|
161
|
+
|
|
162
|
+
let flashBorrowInDebtToken: Decimal;
|
|
163
|
+
let collTokenToDeposit: Decimal;
|
|
164
|
+
let debtTokenToBorrow: Decimal;
|
|
165
|
+
|
|
166
|
+
if (depositTokenIsCollToken) {
|
|
167
|
+
const x = slippage.add('1').div(priceDebtToColl);
|
|
168
|
+
const y = flashLoanFee.add('1').mul(priceDebtToColl);
|
|
169
|
+
const z = targetLeverage.mul(y).div(targetLeverage.sub(1));
|
|
170
|
+
flashBorrowInDebtToken = depositAmount.div(z.minus(new Decimal(1).div(x)));
|
|
171
|
+
collTokenToDeposit = depositAmount.add(flashBorrowInDebtToken.div(x));
|
|
172
|
+
debtTokenToBorrow = flashBorrowInDebtToken.mul(new Decimal(1).add(flashLoanFee));
|
|
173
|
+
|
|
174
|
+
return {
|
|
175
|
+
flashBorrowInDebtToken,
|
|
176
|
+
initDepositInSol,
|
|
177
|
+
collTokenToDeposit,
|
|
178
|
+
debtTokenToBorrow,
|
|
179
|
+
requiredCollateral: collTokenToDeposit.sub(depositAmount), // Assuming netValue is requiredCollateral, adjust as needed
|
|
180
|
+
singleSidedDeposit: flashBorrowInDebtToken,
|
|
181
|
+
};
|
|
182
|
+
} else {
|
|
183
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
184
|
+
// although we will only swap ~half of the debt token, we account for the slippage on the entire amount as we are working backwards from the minimum collateral and do not know the exact swap proportion in advance
|
|
185
|
+
// This also allows for some variation in the pool ratios between calculation + submitting the tx
|
|
186
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
187
|
+
// Calculate the amount of collateral tokens we will deposit in order to achieve the desired leverage after swapping a portion of the debt token and flash loan fees
|
|
188
|
+
const finalColl = depositAmount.div(x.sub(targetLeverage.sub('1').div(y)));
|
|
189
|
+
// Calculate how many A and B tokens we will need to actually mint the desired amount of ktoken collateral
|
|
190
|
+
// The actual amount of ktokens received may be less than the finalColl due to smart proportional contract logic
|
|
191
|
+
// So we use the actualColl as the amount we will deposit
|
|
192
|
+
const [estimatedA, estimatedB, actualColl] = await simulateMintKToken(
|
|
193
|
+
kamino!,
|
|
194
|
+
strategy!,
|
|
195
|
+
finalColl,
|
|
196
|
+
strategyHoldings
|
|
197
|
+
);
|
|
198
|
+
const pxAinB = await priceAinB(strategy!.strategy.tokenAMint, strategy!.strategy.tokenBMint);
|
|
199
|
+
const isTokenADeposit = strategy.strategy.tokenAMint.equals(debtTokenMint);
|
|
200
|
+
// Calculate the amount we need to flash borrow by combining value of A and B into the debt token
|
|
201
|
+
const singleSidedDepositAmount = isTokenADeposit
|
|
202
|
+
? estimatedA.add(estimatedB.div(pxAinB))
|
|
203
|
+
: estimatedB.add(estimatedA.mul(pxAinB));
|
|
204
|
+
|
|
205
|
+
// Add slippage to the entire amount, add flash loan fee to part we will flash borrow
|
|
206
|
+
flashBorrowInDebtToken = singleSidedDepositAmount
|
|
207
|
+
.div(new Decimal('1').sub(slippage))
|
|
208
|
+
.sub(depositAmount)
|
|
209
|
+
.div(new Decimal('1').sub(flashLoanFee));
|
|
210
|
+
// Deposit the min ktoken amount we calculated at the beginning
|
|
211
|
+
// Any slippage will be left in the user's wallet as ktokens
|
|
212
|
+
collTokenToDeposit = actualColl;
|
|
213
|
+
debtTokenToBorrow = flashBorrowInDebtToken.div(new Decimal('1').sub(flashLoanFee));
|
|
214
|
+
// Add slippage to ensure we try to swap/deposit as much as possible after flash loan fees
|
|
215
|
+
const singleSidedDeposit = singleSidedDepositAmount.div(new Decimal('1').sub(slippage));
|
|
216
|
+
|
|
217
|
+
return {
|
|
218
|
+
flashBorrowInDebtToken,
|
|
219
|
+
initDepositInSol,
|
|
220
|
+
collTokenToDeposit,
|
|
221
|
+
debtTokenToBorrow,
|
|
222
|
+
requiredCollateral: collTokenToDeposit, // Assuming collTokenToDeposit is requiredCollateral, adjust as needed
|
|
223
|
+
singleSidedDeposit,
|
|
224
|
+
};
|
|
225
|
+
}
|
|
226
|
+
};
|
|
227
|
+
|
|
228
|
+
export const getDepositWithLeverageSwapInputs = (props: {
|
|
229
|
+
depositAmount: Decimal;
|
|
230
|
+
priceDebtToColl: Decimal;
|
|
231
|
+
slippagePct: Decimal;
|
|
232
|
+
targetLeverage: Decimal;
|
|
233
|
+
kaminoMarket: KaminoMarket;
|
|
234
|
+
selectedTokenMint: PublicKey;
|
|
235
|
+
debtTokenMint: PublicKey;
|
|
236
|
+
collTokenMint: PublicKey;
|
|
237
|
+
}): {
|
|
238
|
+
swapInputs: SwapInputs;
|
|
239
|
+
} => {
|
|
240
|
+
const {
|
|
241
|
+
depositAmount,
|
|
242
|
+
priceDebtToColl,
|
|
243
|
+
slippagePct,
|
|
244
|
+
targetLeverage,
|
|
245
|
+
kaminoMarket,
|
|
246
|
+
selectedTokenMint,
|
|
247
|
+
debtTokenMint,
|
|
248
|
+
collTokenMint,
|
|
249
|
+
} = props;
|
|
250
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
251
|
+
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
252
|
+
const solTokenReserve = kaminoMarket.getReserveByMint(WRAPPED_SOL_MINT);
|
|
253
|
+
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve!.getLiquidityMint());
|
|
254
|
+
const flashLoanFee = debtReserve?.getFlashLoanFee() || new Decimal(0);
|
|
255
|
+
|
|
256
|
+
const calcs = depositLeverageCalcs({
|
|
257
|
+
depositAmount,
|
|
258
|
+
depositTokenIsCollToken: selectedTokenIsCollToken,
|
|
259
|
+
depositTokenIsSol,
|
|
260
|
+
priceDebtToColl,
|
|
261
|
+
targetLeverage,
|
|
262
|
+
slippagePct,
|
|
263
|
+
flashLoanFee,
|
|
264
|
+
});
|
|
265
|
+
|
|
266
|
+
return {
|
|
267
|
+
swapInputs: {
|
|
268
|
+
inputAmountLamports: toLamports(calcs.swapDebtTokenIn, debtReserve!.state.liquidity.mintDecimals.toNumber())
|
|
269
|
+
.ceil()
|
|
270
|
+
.toNumber(),
|
|
271
|
+
inputMint: debtTokenMint,
|
|
272
|
+
outputMint: collTokenMint,
|
|
273
|
+
},
|
|
274
|
+
};
|
|
275
|
+
};
|
|
276
|
+
|
|
277
|
+
export const getDepositWithLeverageIxns = async (props: {
|
|
278
|
+
connection: Connection;
|
|
279
|
+
budgetAndPriorityFeeIxns?: TransactionInstruction[];
|
|
280
|
+
user: PublicKey;
|
|
281
|
+
amount: Decimal;
|
|
282
|
+
selectedTokenMint: PublicKey;
|
|
283
|
+
collTokenMint: PublicKey;
|
|
284
|
+
debtTokenMint: PublicKey;
|
|
285
|
+
targetLeverage: Decimal;
|
|
286
|
+
kaminoMarket: KaminoMarket;
|
|
287
|
+
slippagePct: Decimal;
|
|
288
|
+
priceDebtToColl: Decimal;
|
|
289
|
+
swapper: SwapIxnsProvider;
|
|
290
|
+
referrer: PublicKey;
|
|
291
|
+
isKtoken: IsKtokenProvider;
|
|
292
|
+
priceAinB: PriceAinBProvider;
|
|
293
|
+
kamino: Kamino | undefined;
|
|
294
|
+
obligationTypeTagOverride: ObligationTypeTag;
|
|
295
|
+
obligation: KaminoObligation | null;
|
|
296
|
+
currentSlot: number;
|
|
297
|
+
getTotalKlendAccountsOnly: boolean;
|
|
298
|
+
scopeFeed?: string;
|
|
299
|
+
}): Promise<{
|
|
300
|
+
ixns: TransactionInstruction[];
|
|
301
|
+
lookupTablesAddresses: PublicKey[];
|
|
302
|
+
swapInputs: SwapInputs;
|
|
303
|
+
totalKlendAccounts: number;
|
|
304
|
+
}> => {
|
|
305
|
+
const {
|
|
306
|
+
connection,
|
|
307
|
+
budgetAndPriorityFeeIxns,
|
|
308
|
+
user,
|
|
309
|
+
amount,
|
|
310
|
+
selectedTokenMint,
|
|
311
|
+
collTokenMint,
|
|
312
|
+
debtTokenMint,
|
|
313
|
+
targetLeverage,
|
|
314
|
+
kaminoMarket,
|
|
315
|
+
slippagePct,
|
|
316
|
+
priceDebtToColl,
|
|
317
|
+
swapper,
|
|
318
|
+
referrer,
|
|
319
|
+
isKtoken,
|
|
320
|
+
priceAinB,
|
|
321
|
+
kamino,
|
|
322
|
+
obligationTypeTagOverride = 1,
|
|
323
|
+
obligation,
|
|
324
|
+
currentSlot,
|
|
325
|
+
getTotalKlendAccountsOnly,
|
|
326
|
+
scopeFeed,
|
|
327
|
+
} = props;
|
|
328
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
329
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
330
|
+
const solTokenReserve = kaminoMarket.getReserveByMint(WRAPPED_SOL_MINT);
|
|
331
|
+
const flashLoanFee = collReserve!.getFlashLoanFee() || new Decimal(0);
|
|
332
|
+
|
|
333
|
+
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
334
|
+
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve!.getLiquidityMint());
|
|
335
|
+
|
|
336
|
+
const collIsKtoken = await isKtoken(collTokenMint);
|
|
337
|
+
const strategy = collIsKtoken ? (await kamino!.getStrategyByKTokenMint(collTokenMint))! : undefined;
|
|
338
|
+
|
|
339
|
+
const calcs = depositLeverageCalcs({
|
|
340
|
+
depositAmount: amount,
|
|
341
|
+
depositTokenIsCollToken: selectedTokenIsCollToken,
|
|
342
|
+
depositTokenIsSol,
|
|
343
|
+
priceDebtToColl,
|
|
344
|
+
targetLeverage,
|
|
345
|
+
slippagePct,
|
|
346
|
+
flashLoanFee,
|
|
347
|
+
});
|
|
348
|
+
let calcsKtoken;
|
|
349
|
+
if (collIsKtoken) {
|
|
350
|
+
calcsKtoken = await depositLeverageKtokenCalcs({
|
|
351
|
+
kamino: kamino!,
|
|
352
|
+
strategy: strategy!,
|
|
353
|
+
debtTokenMint,
|
|
354
|
+
depositAmount: amount,
|
|
355
|
+
depositTokenIsCollToken: selectedTokenIsCollToken,
|
|
356
|
+
depositTokenIsSol,
|
|
357
|
+
priceDebtToColl,
|
|
358
|
+
targetLeverage,
|
|
359
|
+
slippagePct,
|
|
360
|
+
flashLoanFee,
|
|
361
|
+
priceAinB,
|
|
362
|
+
});
|
|
363
|
+
// Rounding to exact number of decimals so this value is passed through in all calcs without rounding inconsistencies
|
|
364
|
+
calcsKtoken.flashBorrowInDebtToken = calcsKtoken.flashBorrowInDebtToken.toDecimalPlaces(
|
|
365
|
+
debtReserve?.state.liquidity.mintDecimals.toNumber()!,
|
|
366
|
+
Decimal.ROUND_CEIL
|
|
367
|
+
);
|
|
368
|
+
calcsKtoken.debtTokenToBorrow = calcsKtoken.debtTokenToBorrow.toDecimalPlaces(
|
|
369
|
+
debtReserve?.state.liquidity.mintDecimals.toNumber()!,
|
|
370
|
+
Decimal.ROUND_CEIL
|
|
371
|
+
);
|
|
372
|
+
calcsKtoken.singleSidedDeposit = calcsKtoken.singleSidedDeposit.toDecimalPlaces(
|
|
373
|
+
debtReserve?.state.liquidity.mintDecimals.toNumber()!,
|
|
374
|
+
Decimal.ROUND_CEIL
|
|
375
|
+
);
|
|
376
|
+
}
|
|
377
|
+
|
|
378
|
+
console.log('Ops Calcs', toJson(!collIsKtoken ? calcs : calcsKtoken));
|
|
379
|
+
console.log(
|
|
380
|
+
'Infos',
|
|
381
|
+
toJson({
|
|
382
|
+
depositTokenIsSol,
|
|
383
|
+
selectedTokenIsCollToken,
|
|
384
|
+
initDepositInSol: calcs.initDepositInSol,
|
|
385
|
+
})
|
|
386
|
+
);
|
|
387
|
+
|
|
388
|
+
// 1. Create atas & budget txns
|
|
389
|
+
let mintsToCreateAtas: PublicKey[] = [];
|
|
390
|
+
let mintsToCreateAtasTokenPrograms: PublicKey[] = [];
|
|
391
|
+
if (collIsKtoken) {
|
|
392
|
+
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
393
|
+
? strategy!.strategy.tokenBMint
|
|
394
|
+
: strategy!.strategy.tokenAMint;
|
|
395
|
+
const secondTokenTokenProgarm = strategy?.strategy.tokenAMint.equals(debtTokenMint)
|
|
396
|
+
? strategy!.strategy.tokenBTokenProgram.equals(PublicKey.default)
|
|
397
|
+
? TOKEN_PROGRAM_ID
|
|
398
|
+
: strategy!.strategy.tokenBTokenProgram
|
|
399
|
+
: strategy!.strategy.tokenATokenProgram.equals(PublicKey.default)
|
|
400
|
+
? TOKEN_PROGRAM_ID
|
|
401
|
+
: strategy!.strategy.tokenATokenProgram;
|
|
402
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve!.getCTokenMint(), secondTokenAta];
|
|
403
|
+
mintsToCreateAtasTokenPrograms = [
|
|
404
|
+
collReserve!.getLiquidityTokenProgram(),
|
|
405
|
+
debtReserve!.getLiquidityTokenProgram(),
|
|
406
|
+
TOKEN_PROGRAM_ID,
|
|
407
|
+
secondTokenTokenProgarm,
|
|
408
|
+
];
|
|
409
|
+
} else {
|
|
410
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve!.getCTokenMint()];
|
|
411
|
+
mintsToCreateAtasTokenPrograms = [
|
|
412
|
+
collReserve!.getLiquidityTokenProgram(),
|
|
413
|
+
debtReserve!.getLiquidityTokenProgram(),
|
|
414
|
+
TOKEN_PROGRAM_ID,
|
|
415
|
+
];
|
|
416
|
+
}
|
|
417
|
+
|
|
418
|
+
const budgetIxns = budgetAndPriorityFeeIxns || getComputeBudgetAndPriorityFeeIxns(3000000);
|
|
419
|
+
const {
|
|
420
|
+
atas: [collTokenAta, debtTokenAta],
|
|
421
|
+
createAtasIxns,
|
|
422
|
+
closeAtasIxns,
|
|
423
|
+
} = await getAtasWithCreateIxnsIfMissing(connection, user, mintsToCreateAtas, mintsToCreateAtasTokenPrograms);
|
|
424
|
+
|
|
425
|
+
// TODO: this needs to work the other way around also
|
|
426
|
+
// TODO: marius test this with shorting leverage and with leverage looping
|
|
427
|
+
const fillWsolAtaIxns: TransactionInstruction[] = [];
|
|
428
|
+
if (depositTokenIsSol) {
|
|
429
|
+
fillWsolAtaIxns.push(
|
|
430
|
+
...getDepositWsolIxns(
|
|
431
|
+
user,
|
|
432
|
+
selectedTokenIsCollToken ? collTokenAta : debtTokenAta,
|
|
433
|
+
toLamports(calcs.initDepositInSol, solTokenReserve!.stats.decimals).ceil()
|
|
434
|
+
)
|
|
435
|
+
);
|
|
436
|
+
}
|
|
437
|
+
|
|
438
|
+
// 1. Flash borrow & repay the collateral amount needed for given leverage
|
|
439
|
+
// if user deposits coll, then we borrow the diff, else we borrow the entire amount
|
|
440
|
+
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
441
|
+
borrowIxnIndex: budgetIxns.length + createAtasIxns.length + fillWsolAtaIxns.length,
|
|
442
|
+
walletPublicKey: user,
|
|
443
|
+
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
444
|
+
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
445
|
+
reserve: !collIsKtoken ? collReserve! : debtReserve!,
|
|
446
|
+
amountLamports: toLamports(
|
|
447
|
+
!collIsKtoken ? calcs.flashBorrowInCollToken : calcsKtoken!.flashBorrowInDebtToken,
|
|
448
|
+
!collIsKtoken ? collReserve!.stats.decimals : debtReserve!.stats.decimals
|
|
449
|
+
),
|
|
450
|
+
destinationAta: !collIsKtoken ? collTokenAta : debtTokenAta,
|
|
451
|
+
referrerAccount: kaminoMarket.programId,
|
|
452
|
+
referrerTokenState: kaminoMarket.programId,
|
|
453
|
+
programId: kaminoMarket.programId,
|
|
454
|
+
});
|
|
455
|
+
|
|
456
|
+
console.log(
|
|
457
|
+
'Borrowing: ',
|
|
458
|
+
toLamports(!collIsKtoken ? calcs.debtTokenToBorrow : calcsKtoken!.debtTokenToBorrow, debtReserve!.stats.decimals)
|
|
459
|
+
.ceil()
|
|
460
|
+
.toString()
|
|
461
|
+
);
|
|
462
|
+
// 3. Deposit initial tokens + borrowed tokens into reserve
|
|
463
|
+
let obligationType: ObligationType;
|
|
464
|
+
if (obligationTypeTagOverride === ObligationTypeTag.Multiply) {
|
|
465
|
+
// multiply
|
|
466
|
+
obligationType = new MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
467
|
+
} else if (obligationTypeTagOverride === ObligationTypeTag.Leverage) {
|
|
468
|
+
// leverage
|
|
469
|
+
obligationType = new LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
470
|
+
} else {
|
|
471
|
+
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
472
|
+
}
|
|
473
|
+
|
|
474
|
+
const scopeRefresh = scopeFeed ? { includeScopeRefresh: true, scopeFeed: scopeFeed } : undefined;
|
|
475
|
+
|
|
476
|
+
const kaminoDepositAndBorrowAction = await KaminoAction.buildDepositAndBorrowTxns(
|
|
477
|
+
kaminoMarket,
|
|
478
|
+
toLamports(!collIsKtoken ? calcs.collTokenToDeposit : calcsKtoken!.collTokenToDeposit, collReserve!.stats.decimals)
|
|
479
|
+
.floor()
|
|
480
|
+
.toString(),
|
|
481
|
+
collTokenMint,
|
|
482
|
+
toLamports(!collIsKtoken ? calcs.debtTokenToBorrow : calcsKtoken!.debtTokenToBorrow, debtReserve!.stats.decimals)
|
|
483
|
+
.ceil()
|
|
484
|
+
.toString(),
|
|
485
|
+
debtTokenMint,
|
|
486
|
+
user,
|
|
487
|
+
obligation ? obligation : obligationType,
|
|
488
|
+
0,
|
|
489
|
+
false,
|
|
490
|
+
true, // emode
|
|
491
|
+
false, // to be checked and created in a setup tx in the UI
|
|
492
|
+
referrer,
|
|
493
|
+
currentSlot,
|
|
494
|
+
scopeRefresh
|
|
495
|
+
);
|
|
496
|
+
|
|
497
|
+
console.log(
|
|
498
|
+
'Expected to swap in',
|
|
499
|
+
!collIsKtoken ? calcs.swapDebtTokenIn.toNumber().toString() : calcsKtoken!.singleSidedDeposit.toNumber().toString(),
|
|
500
|
+
'debt for',
|
|
501
|
+
!collIsKtoken ? calcs.swapCollTokenExpectedOut.toString() : calcsKtoken!.requiredCollateral.toNumber().toString(),
|
|
502
|
+
'coll'
|
|
503
|
+
);
|
|
504
|
+
|
|
505
|
+
const ixns = [
|
|
506
|
+
...budgetIxns,
|
|
507
|
+
...createAtasIxns,
|
|
508
|
+
...fillWsolAtaIxns,
|
|
509
|
+
...[flashBorrowIxn],
|
|
510
|
+
...kaminoDepositAndBorrowAction.setupIxs,
|
|
511
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[0]],
|
|
512
|
+
...kaminoDepositAndBorrowAction.inBetweenIxs,
|
|
513
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[1]],
|
|
514
|
+
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
515
|
+
...[flashRepayIxn],
|
|
516
|
+
...closeAtasIxns,
|
|
517
|
+
];
|
|
518
|
+
|
|
519
|
+
const uniqueAccounts = new PublicKeySet<PublicKey>([]);
|
|
520
|
+
ixns.forEach((ixn) => {
|
|
521
|
+
ixn.keys.forEach((key) => {
|
|
522
|
+
uniqueAccounts.add(key.pubkey);
|
|
523
|
+
});
|
|
524
|
+
});
|
|
525
|
+
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
526
|
+
|
|
527
|
+
// return early to avoid extra swapper calls
|
|
528
|
+
if (getTotalKlendAccountsOnly) {
|
|
529
|
+
return {
|
|
530
|
+
ixns: [],
|
|
531
|
+
lookupTablesAddresses: [],
|
|
532
|
+
swapInputs: { inputAmountLamports: 0, inputMint: PublicKey.default, outputMint: PublicKey.default },
|
|
533
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
534
|
+
};
|
|
535
|
+
}
|
|
536
|
+
|
|
537
|
+
let depositSwapper: SwapIxnsProvider;
|
|
538
|
+
let expectedDebtTokenAtaBalance: Decimal = new Decimal(0); // only needed for kTokens
|
|
539
|
+
|
|
540
|
+
if (!collIsKtoken) {
|
|
541
|
+
depositSwapper = swapper;
|
|
542
|
+
} else {
|
|
543
|
+
if (kamino === undefined) {
|
|
544
|
+
throw Error('Ktoken use as collateral for leverage without Kamino instance');
|
|
545
|
+
}
|
|
546
|
+
depositSwapper = await getTokenToKtokenSwapper(connection, kaminoMarket, kamino, user, swapper, priceAinB, false);
|
|
547
|
+
|
|
548
|
+
let futureBalanceInAta = new Decimal(0);
|
|
549
|
+
if (debtTokenMint.equals(WRAPPED_SOL_MINT)) {
|
|
550
|
+
futureBalanceInAta = futureBalanceInAta.add(
|
|
551
|
+
!collIsKtoken ? calcs.initDepositInSol : calcsKtoken!.initDepositInSol
|
|
552
|
+
);
|
|
553
|
+
}
|
|
554
|
+
futureBalanceInAta = futureBalanceInAta.add(
|
|
555
|
+
!collIsKtoken ? calcs.debtTokenToBorrow : calcsKtoken!.flashBorrowInDebtToken
|
|
556
|
+
);
|
|
557
|
+
expectedDebtTokenAtaBalance = await getExpectedTokenBalanceAfterBorrow(
|
|
558
|
+
connection,
|
|
559
|
+
debtTokenMint,
|
|
560
|
+
user,
|
|
561
|
+
toLamports(futureBalanceInAta.toDecimalPlaces(debtReserve!.stats.decimals), debtReserve!.stats.decimals),
|
|
562
|
+
debtReserve!.state.liquidity.mintDecimals.toNumber()
|
|
563
|
+
);
|
|
564
|
+
}
|
|
565
|
+
|
|
566
|
+
const swapInputs: SwapInputs = {
|
|
567
|
+
inputAmountLamports: toLamports(
|
|
568
|
+
!collIsKtoken ? calcs.swapDebtTokenIn : calcsKtoken!.singleSidedDeposit,
|
|
569
|
+
debtReserve!.stats.decimals
|
|
570
|
+
)
|
|
571
|
+
.ceil()
|
|
572
|
+
.toNumber(),
|
|
573
|
+
inputMint: debtTokenMint,
|
|
574
|
+
outputMint: collTokenMint,
|
|
575
|
+
};
|
|
576
|
+
|
|
577
|
+
const [swapIxns, lookupTablesAddresses] = await depositSwapper(
|
|
578
|
+
swapInputs.inputAmountLamports,
|
|
579
|
+
swapInputs.inputMint,
|
|
580
|
+
swapInputs.outputMint,
|
|
581
|
+
slippagePct.toNumber(),
|
|
582
|
+
expectedDebtTokenAtaBalance
|
|
583
|
+
);
|
|
584
|
+
|
|
585
|
+
if (collIsKtoken) {
|
|
586
|
+
if (strategy?.strategy.strategyLookupTable) {
|
|
587
|
+
lookupTablesAddresses.push(strategy?.strategy.strategyLookupTable!);
|
|
588
|
+
} else {
|
|
589
|
+
console.log('Strategy lookup table not found');
|
|
590
|
+
}
|
|
591
|
+
}
|
|
592
|
+
|
|
593
|
+
const swapInstructions = removeBudgetAndAtaIxns(swapIxns, []);
|
|
594
|
+
|
|
595
|
+
if (!collIsKtoken) {
|
|
596
|
+
return {
|
|
597
|
+
ixns: [
|
|
598
|
+
...budgetIxns,
|
|
599
|
+
...createAtasIxns,
|
|
600
|
+
...fillWsolAtaIxns,
|
|
601
|
+
...[flashBorrowIxn],
|
|
602
|
+
...kaminoDepositAndBorrowAction.setupIxs,
|
|
603
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[0]],
|
|
604
|
+
...kaminoDepositAndBorrowAction.inBetweenIxs,
|
|
605
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[1]],
|
|
606
|
+
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
607
|
+
...swapInstructions,
|
|
608
|
+
...[flashRepayIxn],
|
|
609
|
+
...closeAtasIxns,
|
|
610
|
+
],
|
|
611
|
+
lookupTablesAddresses,
|
|
612
|
+
swapInputs,
|
|
613
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
614
|
+
};
|
|
615
|
+
} else {
|
|
616
|
+
return {
|
|
617
|
+
ixns: [
|
|
618
|
+
...budgetIxns,
|
|
619
|
+
...createAtasIxns,
|
|
620
|
+
...fillWsolAtaIxns,
|
|
621
|
+
...[flashBorrowIxn],
|
|
622
|
+
...swapInstructions,
|
|
623
|
+
...kaminoDepositAndBorrowAction.setupIxs,
|
|
624
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[0]],
|
|
625
|
+
...kaminoDepositAndBorrowAction.inBetweenIxs,
|
|
626
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[1]],
|
|
627
|
+
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
628
|
+
...[flashRepayIxn],
|
|
629
|
+
...closeAtasIxns,
|
|
630
|
+
],
|
|
631
|
+
lookupTablesAddresses,
|
|
632
|
+
swapInputs,
|
|
633
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
634
|
+
};
|
|
635
|
+
}
|
|
636
|
+
};
|
|
637
|
+
|
|
638
|
+
export const getWithdrawWithLeverageSwapInputs = (props: {
|
|
639
|
+
amount: Decimal;
|
|
640
|
+
deposited: Decimal;
|
|
641
|
+
borrowed: Decimal;
|
|
642
|
+
priceCollToDebt: Decimal;
|
|
643
|
+
slippagePct: number;
|
|
644
|
+
isClosingPosition: boolean;
|
|
645
|
+
kaminoMarket: KaminoMarket;
|
|
646
|
+
selectedTokenMint: PublicKey;
|
|
647
|
+
debtTokenMint: PublicKey;
|
|
648
|
+
collTokenMint: PublicKey;
|
|
649
|
+
userObligation: KaminoObligation;
|
|
650
|
+
currentSlot: number;
|
|
651
|
+
}): {
|
|
652
|
+
swapInputs: SwapInputs;
|
|
653
|
+
} => {
|
|
654
|
+
const {
|
|
655
|
+
amount,
|
|
656
|
+
deposited,
|
|
657
|
+
borrowed,
|
|
658
|
+
priceCollToDebt,
|
|
659
|
+
slippagePct,
|
|
660
|
+
isClosingPosition,
|
|
661
|
+
kaminoMarket,
|
|
662
|
+
selectedTokenMint,
|
|
663
|
+
debtTokenMint,
|
|
664
|
+
collTokenMint,
|
|
665
|
+
userObligation,
|
|
666
|
+
currentSlot,
|
|
667
|
+
} = props;
|
|
668
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
669
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
670
|
+
const flashLoanFee = debtReserve?.getFlashLoanFee() || new Decimal(0);
|
|
671
|
+
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
672
|
+
|
|
673
|
+
const { adjustDepositPosition: withdrawAmount, adjustBorrowPosition: initialRepayAmount } = isClosingPosition
|
|
674
|
+
? { adjustDepositPosition: deposited, adjustBorrowPosition: borrowed }
|
|
675
|
+
: calcWithdrawAmounts({
|
|
676
|
+
collTokenMint: collTokenMint,
|
|
677
|
+
priceCollToDebt: new Decimal(priceCollToDebt),
|
|
678
|
+
currentDepositPosition: deposited,
|
|
679
|
+
currentBorrowPosition: borrowed,
|
|
680
|
+
withdrawAmount: new Decimal(amount),
|
|
681
|
+
selectedTokenMint: selectedTokenMint,
|
|
682
|
+
});
|
|
683
|
+
|
|
684
|
+
const irSlippageBpsForDebt = userObligation
|
|
685
|
+
.estimateObligationInterestRate(kaminoMarket, debtReserve!, userObligation.state.borrows[0]!, currentSlot)
|
|
686
|
+
.toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber()!, Decimal.ROUND_CEIL);
|
|
687
|
+
|
|
688
|
+
const repayAmount = initialRepayAmount
|
|
689
|
+
.mul(irSlippageBpsForDebt.add('0.1').div('10_000').add('1'))
|
|
690
|
+
.toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber()!, Decimal.ROUND_CEIL);
|
|
691
|
+
|
|
692
|
+
const swapAmountIfWithdrawingColl = repayAmount
|
|
693
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
694
|
+
.mul(new Decimal(1 + slippagePct / 100))
|
|
695
|
+
.div(priceCollToDebt);
|
|
696
|
+
|
|
697
|
+
const swapAmountIfWithdrawingDebt = withdrawAmount;
|
|
698
|
+
|
|
699
|
+
const collTokenSwapIn = selectedTokenIsCollToken ? swapAmountIfWithdrawingColl : swapAmountIfWithdrawingDebt;
|
|
700
|
+
|
|
701
|
+
return {
|
|
702
|
+
swapInputs: {
|
|
703
|
+
inputAmountLamports: toLamports(collTokenSwapIn, collReserve!.state.liquidity.mintDecimals.toNumber())
|
|
704
|
+
.ceil()
|
|
705
|
+
.toNumber(),
|
|
706
|
+
inputMint: collTokenMint,
|
|
707
|
+
outputMint: debtTokenMint,
|
|
708
|
+
},
|
|
709
|
+
};
|
|
710
|
+
};
|
|
711
|
+
|
|
712
|
+
export const getWithdrawWithLeverageIxns = async (props: {
|
|
713
|
+
connection: Connection;
|
|
714
|
+
budgetAndPriorityFeeIxns: TransactionInstruction[];
|
|
715
|
+
user: PublicKey;
|
|
716
|
+
amount: Decimal;
|
|
717
|
+
deposited: Decimal;
|
|
718
|
+
borrowed: Decimal;
|
|
719
|
+
collTokenMint: PublicKey;
|
|
720
|
+
debtTokenMint: PublicKey;
|
|
721
|
+
priceCollToDebt: Decimal;
|
|
722
|
+
selectedTokenMint: PublicKey;
|
|
723
|
+
isClosingPosition: boolean;
|
|
724
|
+
kaminoMarket: KaminoMarket;
|
|
725
|
+
slippagePct: number;
|
|
726
|
+
swapper: SwapIxnsProvider;
|
|
727
|
+
referrer: PublicKey;
|
|
728
|
+
isKtoken: IsKtokenProvider;
|
|
729
|
+
kamino: Kamino | undefined;
|
|
730
|
+
obligationTypeTagOverride: ObligationTypeTag;
|
|
731
|
+
obligation: KaminoObligation | null;
|
|
732
|
+
currentSlot: number;
|
|
733
|
+
getTotalKlendAccountsOnly: boolean;
|
|
734
|
+
scopeFeed?: string;
|
|
735
|
+
}): Promise<{
|
|
736
|
+
ixns: TransactionInstruction[];
|
|
737
|
+
lookupTablesAddresses: PublicKey[];
|
|
738
|
+
swapInputs: SwapInputs;
|
|
739
|
+
totalKlendAccounts: number;
|
|
740
|
+
}> => {
|
|
741
|
+
const {
|
|
742
|
+
connection,
|
|
743
|
+
budgetAndPriorityFeeIxns,
|
|
744
|
+
user,
|
|
745
|
+
amount,
|
|
746
|
+
deposited,
|
|
747
|
+
borrowed,
|
|
748
|
+
collTokenMint,
|
|
749
|
+
debtTokenMint,
|
|
750
|
+
priceCollToDebt,
|
|
751
|
+
selectedTokenMint,
|
|
752
|
+
isClosingPosition,
|
|
753
|
+
kaminoMarket,
|
|
754
|
+
slippagePct,
|
|
755
|
+
swapper,
|
|
756
|
+
referrer,
|
|
757
|
+
isKtoken,
|
|
758
|
+
kamino,
|
|
759
|
+
obligationTypeTagOverride,
|
|
760
|
+
obligation,
|
|
761
|
+
currentSlot,
|
|
762
|
+
getTotalKlendAccountsOnly,
|
|
763
|
+
scopeFeed,
|
|
764
|
+
} = props;
|
|
765
|
+
|
|
766
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
767
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
768
|
+
const flashLoanFee = debtReserve?.getFlashLoanFee() || new Decimal(0);
|
|
769
|
+
const collIsKtoken = await isKtoken(collTokenMint);
|
|
770
|
+
|
|
771
|
+
const solTokenReserve = kaminoMarket.getReserveByMint(WRAPPED_SOL_MINT);
|
|
772
|
+
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
773
|
+
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve!.getLiquidityMint());
|
|
774
|
+
|
|
775
|
+
let obligationType: ObligationType;
|
|
776
|
+
if (obligationTypeTagOverride == ObligationTypeTag.Multiply) {
|
|
777
|
+
// multiply
|
|
778
|
+
obligationType = new MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
779
|
+
} else if (obligationTypeTagOverride == ObligationTypeTag.Leverage) {
|
|
780
|
+
// leverage
|
|
781
|
+
obligationType = new LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
782
|
+
} else {
|
|
783
|
+
throw Error(
|
|
784
|
+
`Obligation type tag ${obligationTypeTagOverride} not supported for leverage, please use multiply (1) or leverage (3) obligation type`
|
|
785
|
+
);
|
|
786
|
+
}
|
|
787
|
+
|
|
788
|
+
// 1. Calculate coll_amount and debt_amount to repay such that we maintain leverage and we withdraw to
|
|
789
|
+
// the wallet `amountInDepositTokenToWithdrawToWallet` amount of collateral token
|
|
790
|
+
// We need to withdraw withdrawAmountInDepositToken coll tokens
|
|
791
|
+
// and repay repayAmountInBorrowToken debt tokens
|
|
792
|
+
// TODO marius: do the same in useDeposit
|
|
793
|
+
const { adjustDepositPosition: withdrawAmount, adjustBorrowPosition: initialRepayAmount } = isClosingPosition
|
|
794
|
+
? { adjustDepositPosition: deposited, adjustBorrowPosition: borrowed }
|
|
795
|
+
: calcWithdrawAmounts({
|
|
796
|
+
collTokenMint: collTokenMint,
|
|
797
|
+
priceCollToDebt: new Decimal(priceCollToDebt),
|
|
798
|
+
currentDepositPosition: deposited,
|
|
799
|
+
currentBorrowPosition: borrowed,
|
|
800
|
+
withdrawAmount: new Decimal(amount),
|
|
801
|
+
selectedTokenMint: selectedTokenMint,
|
|
802
|
+
});
|
|
803
|
+
|
|
804
|
+
// Add slippage for the accrued interest rate amount
|
|
805
|
+
const userObligation = obligation
|
|
806
|
+
? obligation
|
|
807
|
+
: await kaminoMarket.getObligationByAddress(obligationType.toPda(kaminoMarket.getAddress(), user));
|
|
808
|
+
const irSlippageBpsForDebt = userObligation!
|
|
809
|
+
.estimateObligationInterestRate(kaminoMarket, debtReserve!, userObligation?.state.borrows[0]!, currentSlot)
|
|
810
|
+
.toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber()!, Decimal.ROUND_CEIL);
|
|
811
|
+
// add 0.1 to irSlippageBpsForDebt because we don't want to estimate slightly less than SC and end up not reapying enough
|
|
812
|
+
const repayAmount = initialRepayAmount
|
|
813
|
+
.mul(irSlippageBpsForDebt.add('0.1').div('10_000').add('1'))
|
|
814
|
+
.toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber()!, Decimal.ROUND_CEIL);
|
|
815
|
+
|
|
816
|
+
// 6. Get swap ixns
|
|
817
|
+
// 5. Get swap estimations to understand how much we need to borrow from borrow reserve
|
|
818
|
+
// prevent withdrawing more then deposited if we close position
|
|
819
|
+
const depositTokenWithdrawAmount = !isClosingPosition
|
|
820
|
+
? withdrawAmount.mul(new Decimal(1).plus(flashLoanFee))
|
|
821
|
+
: withdrawAmount;
|
|
822
|
+
|
|
823
|
+
// We are swapping debt token
|
|
824
|
+
// When withdrawing coll, it means we just need to swap enough to pay for the flash borrow
|
|
825
|
+
const swapAmountIfWithdrawingColl = repayAmount
|
|
826
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
827
|
+
.mul(new Decimal(1 + slippagePct / 100))
|
|
828
|
+
.div(priceCollToDebt);
|
|
829
|
+
|
|
830
|
+
// When withdrawing debt, it means we need to swap just the collateral we are withdrwaing
|
|
831
|
+
// enough to cover the debt we are repaying, leaving the remaining in the wallet
|
|
832
|
+
const swapAmountIfWithdrawingDebt = withdrawAmount;
|
|
833
|
+
|
|
834
|
+
const collTokenSwapIn = selectedTokenIsCollToken ? swapAmountIfWithdrawingColl : swapAmountIfWithdrawingDebt;
|
|
835
|
+
const debtTokenExpectedSwapOut = collTokenSwapIn.mul(priceCollToDebt).div(new Decimal(1 + slippagePct / 100));
|
|
836
|
+
|
|
837
|
+
const strategy = collIsKtoken ? await kamino?.getStrategyByKTokenMint(collTokenMint) : undefined;
|
|
838
|
+
|
|
839
|
+
console.log('Expecting to swap', collTokenSwapIn.toString(), 'coll for', debtTokenExpectedSwapOut.toString(), 'debt');
|
|
840
|
+
// 1. Create atas & budget txns & user metadata
|
|
841
|
+
let mintsToCreateAtas: PublicKey[] = [];
|
|
842
|
+
let mintsToCreateAtasTokenPrograms: PublicKey[] = [];
|
|
843
|
+
if (collIsKtoken) {
|
|
844
|
+
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
845
|
+
? strategy!.strategy.tokenBMint
|
|
846
|
+
: strategy!.strategy.tokenAMint;
|
|
847
|
+
const secondTokenTokenProgarm = strategy?.strategy.tokenAMint.equals(debtTokenMint)
|
|
848
|
+
? strategy!.strategy.tokenBTokenProgram.equals(PublicKey.default)
|
|
849
|
+
? TOKEN_PROGRAM_ID
|
|
850
|
+
: strategy!.strategy.tokenBTokenProgram
|
|
851
|
+
: strategy!.strategy.tokenATokenProgram.equals(PublicKey.default)
|
|
852
|
+
? TOKEN_PROGRAM_ID
|
|
853
|
+
: strategy!.strategy.tokenATokenProgram;
|
|
854
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve!.getCTokenMint(), secondTokenAta];
|
|
855
|
+
mintsToCreateAtasTokenPrograms = [
|
|
856
|
+
collReserve!.getLiquidityTokenProgram(),
|
|
857
|
+
debtReserve!.getLiquidityTokenProgram(),
|
|
858
|
+
TOKEN_PROGRAM_ID,
|
|
859
|
+
secondTokenTokenProgarm,
|
|
860
|
+
];
|
|
861
|
+
} else {
|
|
862
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve!.getCTokenMint()];
|
|
863
|
+
mintsToCreateAtasTokenPrograms = [
|
|
864
|
+
collReserve!.getLiquidityTokenProgram(),
|
|
865
|
+
debtReserve!.getLiquidityTokenProgram(),
|
|
866
|
+
TOKEN_PROGRAM_ID,
|
|
867
|
+
];
|
|
868
|
+
}
|
|
869
|
+
|
|
870
|
+
const {
|
|
871
|
+
atas: [, debtTokenAta],
|
|
872
|
+
createAtasIxns,
|
|
873
|
+
closeAtasIxns,
|
|
874
|
+
} = await getAtasWithCreateIxnsIfMissing(connection, user, mintsToCreateAtas, mintsToCreateAtasTokenPrograms);
|
|
875
|
+
|
|
876
|
+
const closeWsolAtaIxns: TransactionInstruction[] = [];
|
|
877
|
+
if (depositTokenIsSol || debtTokenMint.equals(WRAPPED_SOL_MINT)) {
|
|
878
|
+
const wsolAta = getAssociatedTokenAddress(WRAPPED_SOL_MINT, user, false);
|
|
879
|
+
closeWsolAtaIxns.push(createCloseAccountInstruction(wsolAta, user, user, [], TOKEN_PROGRAM_ID));
|
|
880
|
+
}
|
|
881
|
+
closeAtasIxns.push(...closeWsolAtaIxns);
|
|
882
|
+
|
|
883
|
+
const budgetIxns = budgetAndPriorityFeeIxns || getComputeBudgetAndPriorityFeeIxns(3000000);
|
|
884
|
+
|
|
885
|
+
// TODO: marius test this with shorting leverage and with leverage looping
|
|
886
|
+
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
887
|
+
const fillWsolAtaIxns: TransactionInstruction[] = [];
|
|
888
|
+
if (debtTokenMint.equals(WRAPPED_SOL_MINT)) {
|
|
889
|
+
const halfSolBalance = (await connection.getBalance(user)) / LAMPORTS_PER_SOL / 2;
|
|
890
|
+
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
891
|
+
fillWsolAtaIxns.push(
|
|
892
|
+
...getDepositWsolIxns(user, debtTokenAta, toLamports(balanceToWrap, solTokenReserve!.stats.decimals).ceil())
|
|
893
|
+
);
|
|
894
|
+
}
|
|
895
|
+
|
|
896
|
+
// 2. Prepare the flash borrow and flash repay amounts and ixns
|
|
897
|
+
// We borrow exactly how much we need to repay
|
|
898
|
+
// and repay that + flash amount fee
|
|
899
|
+
|
|
900
|
+
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
901
|
+
borrowIxnIndex: budgetIxns.length + createAtasIxns.length + fillWsolAtaIxns.length,
|
|
902
|
+
walletPublicKey: user,
|
|
903
|
+
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
904
|
+
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
905
|
+
reserve: debtReserve!,
|
|
906
|
+
amountLamports: toLamports(repayAmount, debtReserve!.stats.decimals),
|
|
907
|
+
destinationAta: debtTokenAta,
|
|
908
|
+
referrerAccount: kaminoMarket.programId,
|
|
909
|
+
referrerTokenState: kaminoMarket.programId,
|
|
910
|
+
programId: kaminoMarket.programId,
|
|
911
|
+
});
|
|
912
|
+
|
|
913
|
+
// 6. Repay borrowed tokens and Withdraw tokens from reserve that will be swapped to repay flash loan
|
|
914
|
+
const repayAndWithdrawAction = await KaminoAction.buildRepayAndWithdrawTxns(
|
|
915
|
+
kaminoMarket,
|
|
916
|
+
isClosingPosition ? U64_MAX : toLamports(repayAmount, debtReserve!.stats.decimals).floor().toString(),
|
|
917
|
+
debtTokenMint,
|
|
918
|
+
isClosingPosition ? U64_MAX : toLamports(depositTokenWithdrawAmount, collReserve!.stats.decimals).ceil().toString(),
|
|
919
|
+
collTokenMint,
|
|
920
|
+
user,
|
|
921
|
+
currentSlot,
|
|
922
|
+
userObligation ? userObligation : obligationType,
|
|
923
|
+
0,
|
|
924
|
+
false,
|
|
925
|
+
false,
|
|
926
|
+
false, // to be checked and created in a setup tx in the UI (won't be the case for withdraw anyway as this would be created in deposit)
|
|
927
|
+
isClosingPosition,
|
|
928
|
+
referrer,
|
|
929
|
+
{ includeScopeRefresh: true, scopeFeed: scopeFeed! }
|
|
930
|
+
);
|
|
931
|
+
|
|
932
|
+
const klendIxns = [
|
|
933
|
+
...budgetIxns,
|
|
934
|
+
...createAtasIxns,
|
|
935
|
+
...fillWsolAtaIxns,
|
|
936
|
+
...[flashBorrowIxn],
|
|
937
|
+
...repayAndWithdrawAction.setupIxs,
|
|
938
|
+
...[repayAndWithdrawAction.lendingIxs[0]],
|
|
939
|
+
...repayAndWithdrawAction.inBetweenIxs,
|
|
940
|
+
...[repayAndWithdrawAction.lendingIxs[1]],
|
|
941
|
+
...repayAndWithdrawAction.cleanupIxs,
|
|
942
|
+
...[flashRepayIxn],
|
|
943
|
+
...closeAtasIxns,
|
|
944
|
+
];
|
|
945
|
+
|
|
946
|
+
const uniqueAccounts = new PublicKeySet<PublicKey>([]);
|
|
947
|
+
klendIxns.forEach((ixn) => {
|
|
948
|
+
ixn.keys.forEach((key) => {
|
|
949
|
+
uniqueAccounts.add(key.pubkey);
|
|
950
|
+
});
|
|
951
|
+
});
|
|
952
|
+
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
953
|
+
|
|
954
|
+
// return early to avoid extra swapper calls
|
|
955
|
+
if (getTotalKlendAccountsOnly) {
|
|
956
|
+
return {
|
|
957
|
+
ixns: [],
|
|
958
|
+
lookupTablesAddresses: [],
|
|
959
|
+
swapInputs: { inputAmountLamports: 0, inputMint: PublicKey.default, outputMint: PublicKey.default },
|
|
960
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
961
|
+
};
|
|
962
|
+
}
|
|
963
|
+
|
|
964
|
+
let withdrawSwapper: SwapIxnsProvider;
|
|
965
|
+
|
|
966
|
+
if (collIsKtoken) {
|
|
967
|
+
if (kamino === undefined) {
|
|
968
|
+
throw Error('Ktoken use as collateral for leverage without Kamino instance');
|
|
969
|
+
}
|
|
970
|
+
withdrawSwapper = await getKtokenToTokenSwapper(kaminoMarket, kamino, user, swapper);
|
|
971
|
+
} else {
|
|
972
|
+
withdrawSwapper = swapper;
|
|
973
|
+
}
|
|
974
|
+
|
|
975
|
+
const swapInputs: SwapInputs = {
|
|
976
|
+
inputAmountLamports: toLamports(collTokenSwapIn, collReserve!.stats.decimals).ceil().toNumber(),
|
|
977
|
+
inputMint: collTokenMint,
|
|
978
|
+
outputMint: debtTokenMint,
|
|
979
|
+
};
|
|
980
|
+
|
|
981
|
+
const [swapIxns, lookupTablesAddresses] = await withdrawSwapper(
|
|
982
|
+
swapInputs.inputAmountLamports,
|
|
983
|
+
swapInputs.inputMint,
|
|
984
|
+
swapInputs.outputMint,
|
|
985
|
+
slippagePct
|
|
986
|
+
);
|
|
987
|
+
|
|
988
|
+
// TODO MARIUS: remove first instruction that is setBudget ixn
|
|
989
|
+
|
|
990
|
+
if (collIsKtoken) {
|
|
991
|
+
if (strategy?.strategy.strategyLookupTable) {
|
|
992
|
+
lookupTablesAddresses.push(strategy?.strategy.strategyLookupTable!);
|
|
993
|
+
} else {
|
|
994
|
+
console.log('Strategy lookup table not found');
|
|
995
|
+
}
|
|
996
|
+
}
|
|
997
|
+
|
|
998
|
+
const swapInstructions = removeBudgetAndAtaIxns(swapIxns, []);
|
|
999
|
+
|
|
1000
|
+
const ixns = [
|
|
1001
|
+
...budgetIxns,
|
|
1002
|
+
...createAtasIxns,
|
|
1003
|
+
...fillWsolAtaIxns,
|
|
1004
|
+
...[flashBorrowIxn],
|
|
1005
|
+
...repayAndWithdrawAction.setupIxs,
|
|
1006
|
+
...[repayAndWithdrawAction.lendingIxs[0]],
|
|
1007
|
+
...repayAndWithdrawAction.inBetweenIxs,
|
|
1008
|
+
...[repayAndWithdrawAction.lendingIxs[1]],
|
|
1009
|
+
...repayAndWithdrawAction.cleanupIxs,
|
|
1010
|
+
...swapInstructions,
|
|
1011
|
+
...[flashRepayIxn],
|
|
1012
|
+
...closeAtasIxns,
|
|
1013
|
+
];
|
|
1014
|
+
|
|
1015
|
+
// Send ixns and lookup tables
|
|
1016
|
+
return {
|
|
1017
|
+
ixns,
|
|
1018
|
+
lookupTablesAddresses,
|
|
1019
|
+
swapInputs,
|
|
1020
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
1021
|
+
};
|
|
1022
|
+
};
|
|
1023
|
+
|
|
1024
|
+
export const getAdjustLeverageSwapInputs = (props: {
|
|
1025
|
+
deposited: Decimal;
|
|
1026
|
+
borrowed: Decimal;
|
|
1027
|
+
priceCollToDebt: Decimal;
|
|
1028
|
+
priceDebtToColl: Decimal;
|
|
1029
|
+
slippagePct: number;
|
|
1030
|
+
targetLeverage: Decimal;
|
|
1031
|
+
kaminoMarket: KaminoMarket;
|
|
1032
|
+
debtTokenMint: PublicKey;
|
|
1033
|
+
collTokenMint: PublicKey;
|
|
1034
|
+
}): {
|
|
1035
|
+
swapInputs: SwapInputs;
|
|
1036
|
+
} => {
|
|
1037
|
+
const {
|
|
1038
|
+
deposited,
|
|
1039
|
+
borrowed,
|
|
1040
|
+
priceCollToDebt,
|
|
1041
|
+
priceDebtToColl,
|
|
1042
|
+
slippagePct,
|
|
1043
|
+
targetLeverage,
|
|
1044
|
+
kaminoMarket,
|
|
1045
|
+
debtTokenMint,
|
|
1046
|
+
collTokenMint,
|
|
1047
|
+
} = props;
|
|
1048
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1049
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
1050
|
+
const flashLoanFee = debtReserve?.getFlashLoanFee() || new Decimal(0);
|
|
1051
|
+
|
|
1052
|
+
const { adjustDepositPosition, adjustBorrowPosition } = calcAdjustAmounts({
|
|
1053
|
+
currentDepositPosition: deposited,
|
|
1054
|
+
currentBorrowPosition: borrowed,
|
|
1055
|
+
targetLeverage: targetLeverage,
|
|
1056
|
+
priceCollToDebt: priceCollToDebt,
|
|
1057
|
+
flashLoanFee: new Decimal(flashLoanFee),
|
|
1058
|
+
});
|
|
1059
|
+
const isDeposit = adjustDepositPosition.gte(0) && adjustBorrowPosition.gte(0);
|
|
1060
|
+
|
|
1061
|
+
if (isDeposit) {
|
|
1062
|
+
const borrowAmount = adjustDepositPosition
|
|
1063
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
1064
|
+
.mul(new Decimal(1 + slippagePct / 100))
|
|
1065
|
+
.div(priceDebtToColl);
|
|
1066
|
+
|
|
1067
|
+
return {
|
|
1068
|
+
swapInputs: {
|
|
1069
|
+
inputAmountLamports: toLamports(borrowAmount, debtReserve!.state.liquidity.mintDecimals.toNumber())
|
|
1070
|
+
.ceil()
|
|
1071
|
+
.toNumber(),
|
|
1072
|
+
inputMint: debtTokenMint,
|
|
1073
|
+
outputMint: collTokenMint,
|
|
1074
|
+
},
|
|
1075
|
+
};
|
|
1076
|
+
} else {
|
|
1077
|
+
const withdrawAmountWithSlippageAndFlashLoanFee = Decimal.abs(adjustDepositPosition)
|
|
1078
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
1079
|
+
.mul(1 + slippagePct / 100);
|
|
1080
|
+
|
|
1081
|
+
return {
|
|
1082
|
+
swapInputs: {
|
|
1083
|
+
inputAmountLamports: toLamports(
|
|
1084
|
+
withdrawAmountWithSlippageAndFlashLoanFee,
|
|
1085
|
+
collReserve!.state.liquidity.mintDecimals.toNumber()
|
|
1086
|
+
)
|
|
1087
|
+
.ceil()
|
|
1088
|
+
.toNumber(),
|
|
1089
|
+
inputMint: collTokenMint,
|
|
1090
|
+
outputMint: debtTokenMint,
|
|
1091
|
+
},
|
|
1092
|
+
};
|
|
1093
|
+
}
|
|
1094
|
+
};
|
|
1095
|
+
|
|
1096
|
+
export const getAdjustLeverageIxns = async (props: {
|
|
1097
|
+
connection: Connection;
|
|
1098
|
+
budgetAndPriorityFeeIxns: TransactionInstruction[];
|
|
1099
|
+
user: PublicKey;
|
|
1100
|
+
kaminoMarket: KaminoMarket;
|
|
1101
|
+
priceDebtToColl: Decimal;
|
|
1102
|
+
priceCollToDebt: Decimal;
|
|
1103
|
+
targetLeverage: Decimal;
|
|
1104
|
+
slippagePct: number;
|
|
1105
|
+
depositedLamports: Decimal;
|
|
1106
|
+
borrowedLamports: Decimal;
|
|
1107
|
+
collTokenMint: PublicKey;
|
|
1108
|
+
debtTokenMint: PublicKey;
|
|
1109
|
+
swapper: SwapIxnsProvider;
|
|
1110
|
+
referrer: PublicKey;
|
|
1111
|
+
isKtoken: IsKtokenProvider;
|
|
1112
|
+
priceAinB: PriceAinBProvider;
|
|
1113
|
+
kamino: Kamino | undefined;
|
|
1114
|
+
obligationTypeTagOverride: ObligationTypeTag;
|
|
1115
|
+
obligation: KaminoObligation | null;
|
|
1116
|
+
currentSlot: number;
|
|
1117
|
+
getTotalKlendAccountsOnly: boolean;
|
|
1118
|
+
scopeFeed: string | undefined;
|
|
1119
|
+
}) => {
|
|
1120
|
+
const {
|
|
1121
|
+
connection,
|
|
1122
|
+
budgetAndPriorityFeeIxns,
|
|
1123
|
+
user,
|
|
1124
|
+
kaminoMarket,
|
|
1125
|
+
priceDebtToColl,
|
|
1126
|
+
priceCollToDebt,
|
|
1127
|
+
targetLeverage,
|
|
1128
|
+
slippagePct,
|
|
1129
|
+
depositedLamports,
|
|
1130
|
+
borrowedLamports,
|
|
1131
|
+
collTokenMint,
|
|
1132
|
+
debtTokenMint,
|
|
1133
|
+
swapper,
|
|
1134
|
+
referrer,
|
|
1135
|
+
isKtoken,
|
|
1136
|
+
priceAinB,
|
|
1137
|
+
kamino,
|
|
1138
|
+
obligationTypeTagOverride,
|
|
1139
|
+
obligation,
|
|
1140
|
+
currentSlot,
|
|
1141
|
+
getTotalKlendAccountsOnly,
|
|
1142
|
+
scopeFeed,
|
|
1143
|
+
} = props;
|
|
1144
|
+
|
|
1145
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1146
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
1147
|
+
|
|
1148
|
+
const deposited = fromLamports(depositedLamports, collReserve!.stats.decimals);
|
|
1149
|
+
const borrowed = fromLamports(borrowedLamports, debtReserve!.stats.decimals);
|
|
1150
|
+
const userObligation = obligation
|
|
1151
|
+
? obligation
|
|
1152
|
+
: (await kaminoMarket.getUserObligationsByTag(obligationTypeTagOverride, user)).filter(
|
|
1153
|
+
(obligation: KaminoObligation) =>
|
|
1154
|
+
obligation.getBorrowByMint(debtReserve!.getLiquidityMint()) !== undefined &&
|
|
1155
|
+
obligation.getDepositByMint(collReserve!.getLiquidityMint()) !== undefined
|
|
1156
|
+
)[0];
|
|
1157
|
+
const currentLeverage = userObligation!.refreshedStats.leverage;
|
|
1158
|
+
const isDepositViaLeverage = targetLeverage.gte(new Decimal(currentLeverage));
|
|
1159
|
+
|
|
1160
|
+
let flashLoanFee = new Decimal(0);
|
|
1161
|
+
if (isDepositViaLeverage) {
|
|
1162
|
+
flashLoanFee = collReserve!.getFlashLoanFee() || new Decimal(0);
|
|
1163
|
+
} else {
|
|
1164
|
+
flashLoanFee = debtReserve!.getFlashLoanFee() || new Decimal(0);
|
|
1165
|
+
}
|
|
1166
|
+
|
|
1167
|
+
const { adjustDepositPosition, adjustBorrowPosition } = calcAdjustAmounts({
|
|
1168
|
+
currentDepositPosition: deposited,
|
|
1169
|
+
currentBorrowPosition: borrowed,
|
|
1170
|
+
targetLeverage: targetLeverage,
|
|
1171
|
+
priceCollToDebt: priceCollToDebt,
|
|
1172
|
+
flashLoanFee: new Decimal(flashLoanFee),
|
|
1173
|
+
});
|
|
1174
|
+
|
|
1175
|
+
let ixns: TransactionInstruction[] = [];
|
|
1176
|
+
let lookupTablesAddresses: PublicKey[] = [];
|
|
1177
|
+
let swapInputs: SwapInputs;
|
|
1178
|
+
let totalKlendAccounts: number = 0;
|
|
1179
|
+
|
|
1180
|
+
const isDeposit = adjustDepositPosition.gte(0) && adjustBorrowPosition.gte(0);
|
|
1181
|
+
if (isDepositViaLeverage !== isDeposit) {
|
|
1182
|
+
throw new Error('Invalid target leverage');
|
|
1183
|
+
}
|
|
1184
|
+
|
|
1185
|
+
// leverage increased so we need to deposit and borrow more
|
|
1186
|
+
if (isDeposit) {
|
|
1187
|
+
console.log('Increasing leaverage');
|
|
1188
|
+
// TODO: marius why are we not using both adjustDepositPosition & adjustBorrowPosition
|
|
1189
|
+
const res = await getIncreaseLeverageIxns({
|
|
1190
|
+
connection,
|
|
1191
|
+
budgetAndPriorityFeeIxns,
|
|
1192
|
+
user,
|
|
1193
|
+
kaminoMarket,
|
|
1194
|
+
depositAmount: adjustDepositPosition,
|
|
1195
|
+
collTokenMint,
|
|
1196
|
+
debtTokenMint,
|
|
1197
|
+
slippagePct,
|
|
1198
|
+
priceDebtToColl,
|
|
1199
|
+
priceCollToDebt,
|
|
1200
|
+
swapper,
|
|
1201
|
+
referrer,
|
|
1202
|
+
isKtoken,
|
|
1203
|
+
priceAinB,
|
|
1204
|
+
kamino,
|
|
1205
|
+
obligationTypeTagOverride,
|
|
1206
|
+
obligation: userObligation,
|
|
1207
|
+
currentSlot,
|
|
1208
|
+
getTotalKlendAccountsOnly,
|
|
1209
|
+
scopeFeed,
|
|
1210
|
+
});
|
|
1211
|
+
ixns = res.ixns;
|
|
1212
|
+
lookupTablesAddresses = res.lookupTablesAddresses;
|
|
1213
|
+
swapInputs = res.swapInputs;
|
|
1214
|
+
totalKlendAccounts = res.totalKlendAccounts;
|
|
1215
|
+
} else {
|
|
1216
|
+
console.log('Decreasing leverage');
|
|
1217
|
+
const res = await getDecreaseLeverageIxns({
|
|
1218
|
+
connection,
|
|
1219
|
+
budgetAndPriorityFeeIxns,
|
|
1220
|
+
user,
|
|
1221
|
+
kaminoMarket,
|
|
1222
|
+
withdrawAmount: Decimal.abs(adjustDepositPosition),
|
|
1223
|
+
repayAmount: Decimal.abs(adjustBorrowPosition),
|
|
1224
|
+
collTokenMint,
|
|
1225
|
+
debtTokenMint,
|
|
1226
|
+
slippagePct,
|
|
1227
|
+
swapper,
|
|
1228
|
+
referrer,
|
|
1229
|
+
isKtoken,
|
|
1230
|
+
kamino,
|
|
1231
|
+
obligationTypeTagOverride,
|
|
1232
|
+
obligation: userObligation,
|
|
1233
|
+
currentSlot,
|
|
1234
|
+
getTotalKlendAccountsOnly,
|
|
1235
|
+
scopeFeed,
|
|
1236
|
+
});
|
|
1237
|
+
ixns = res.ixns;
|
|
1238
|
+
lookupTablesAddresses = res.lookupTablesAddresses;
|
|
1239
|
+
swapInputs = res.swapInputs;
|
|
1240
|
+
totalKlendAccounts = res.totalKlendAccounts;
|
|
1241
|
+
}
|
|
1242
|
+
|
|
1243
|
+
return {
|
|
1244
|
+
ixns,
|
|
1245
|
+
lookupTablesAddresses,
|
|
1246
|
+
swapInputs,
|
|
1247
|
+
totalKlendAccounts,
|
|
1248
|
+
};
|
|
1249
|
+
};
|
|
1250
|
+
|
|
1251
|
+
/**
|
|
1252
|
+
* Deposit and borrow tokens if leverage increased
|
|
1253
|
+
*/
|
|
1254
|
+
export const getIncreaseLeverageIxns = async (props: {
|
|
1255
|
+
connection: Connection;
|
|
1256
|
+
budgetAndPriorityFeeIxns: TransactionInstruction[];
|
|
1257
|
+
user: PublicKey;
|
|
1258
|
+
kaminoMarket: KaminoMarket;
|
|
1259
|
+
depositAmount: Decimal;
|
|
1260
|
+
collTokenMint: PublicKey;
|
|
1261
|
+
debtTokenMint: PublicKey;
|
|
1262
|
+
slippagePct: number;
|
|
1263
|
+
priceDebtToColl: Decimal;
|
|
1264
|
+
priceCollToDebt: Decimal;
|
|
1265
|
+
swapper: SwapIxnsProvider;
|
|
1266
|
+
referrer: PublicKey;
|
|
1267
|
+
isKtoken: IsKtokenProvider;
|
|
1268
|
+
priceAinB: PriceAinBProvider;
|
|
1269
|
+
kamino: Kamino | undefined;
|
|
1270
|
+
obligationTypeTagOverride: ObligationTypeTag;
|
|
1271
|
+
obligation: KaminoObligation | null;
|
|
1272
|
+
currentSlot: number;
|
|
1273
|
+
getTotalKlendAccountsOnly: boolean;
|
|
1274
|
+
scopeFeed: string | undefined;
|
|
1275
|
+
}) => {
|
|
1276
|
+
const {
|
|
1277
|
+
connection,
|
|
1278
|
+
budgetAndPriorityFeeIxns,
|
|
1279
|
+
user,
|
|
1280
|
+
kaminoMarket,
|
|
1281
|
+
depositAmount,
|
|
1282
|
+
collTokenMint,
|
|
1283
|
+
debtTokenMint,
|
|
1284
|
+
slippagePct,
|
|
1285
|
+
priceDebtToColl,
|
|
1286
|
+
priceCollToDebt,
|
|
1287
|
+
swapper,
|
|
1288
|
+
referrer,
|
|
1289
|
+
isKtoken,
|
|
1290
|
+
priceAinB,
|
|
1291
|
+
kamino,
|
|
1292
|
+
obligationTypeTagOverride = 1,
|
|
1293
|
+
obligation,
|
|
1294
|
+
currentSlot,
|
|
1295
|
+
getTotalKlendAccountsOnly,
|
|
1296
|
+
scopeFeed,
|
|
1297
|
+
} = props;
|
|
1298
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1299
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
1300
|
+
const collIsKtoken = await isKtoken(collTokenMint);
|
|
1301
|
+
|
|
1302
|
+
const flashLoanFee = collReserve!.getFlashLoanFee() || new Decimal(0);
|
|
1303
|
+
|
|
1304
|
+
if (!priceDebtToColl || !priceCollToDebt) {
|
|
1305
|
+
throw new Error('Price is not loaded. Please, reload the page and try again');
|
|
1306
|
+
}
|
|
1307
|
+
|
|
1308
|
+
// TODO: why are we recalculating here again
|
|
1309
|
+
const strategy = collIsKtoken ? await kamino?.getStrategyByKTokenMint(collTokenMint) : undefined;
|
|
1310
|
+
|
|
1311
|
+
// 1. Create atas & budget txns
|
|
1312
|
+
const budgetIxns = budgetAndPriorityFeeIxns || getComputeBudgetAndPriorityFeeIxns(3000000);
|
|
1313
|
+
let mintsToCreateAtas: PublicKey[] = [];
|
|
1314
|
+
let mintsToCreateAtasTokenPrograms: PublicKey[] = [];
|
|
1315
|
+
if (collIsKtoken) {
|
|
1316
|
+
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
1317
|
+
? strategy!.strategy.tokenBMint
|
|
1318
|
+
: strategy!.strategy.tokenAMint;
|
|
1319
|
+
const secondTokenTokenProgarm = strategy?.strategy.tokenAMint.equals(debtTokenMint)
|
|
1320
|
+
? strategy!.strategy.tokenBTokenProgram.equals(PublicKey.default)
|
|
1321
|
+
? TOKEN_PROGRAM_ID
|
|
1322
|
+
: strategy!.strategy.tokenBTokenProgram
|
|
1323
|
+
: strategy!.strategy.tokenATokenProgram.equals(PublicKey.default)
|
|
1324
|
+
? TOKEN_PROGRAM_ID
|
|
1325
|
+
: strategy!.strategy.tokenATokenProgram;
|
|
1326
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve!.getCTokenMint(), secondTokenAta];
|
|
1327
|
+
mintsToCreateAtasTokenPrograms = [
|
|
1328
|
+
collReserve!.getLiquidityTokenProgram(),
|
|
1329
|
+
debtReserve!.getLiquidityTokenProgram(),
|
|
1330
|
+
TOKEN_PROGRAM_ID,
|
|
1331
|
+
secondTokenTokenProgarm,
|
|
1332
|
+
];
|
|
1333
|
+
} else {
|
|
1334
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve!.getCTokenMint()];
|
|
1335
|
+
mintsToCreateAtasTokenPrograms = [
|
|
1336
|
+
collReserve!.getLiquidityTokenProgram(),
|
|
1337
|
+
debtReserve!.getLiquidityTokenProgram(),
|
|
1338
|
+
TOKEN_PROGRAM_ID,
|
|
1339
|
+
];
|
|
1340
|
+
}
|
|
1341
|
+
|
|
1342
|
+
const {
|
|
1343
|
+
atas: [collTokenAta, debtTokenAta],
|
|
1344
|
+
createAtasIxns,
|
|
1345
|
+
closeAtasIxns,
|
|
1346
|
+
} = await getAtasWithCreateIxnsIfMissing(connection, user, mintsToCreateAtas, mintsToCreateAtasTokenPrograms);
|
|
1347
|
+
|
|
1348
|
+
// 2. Create borrow flash loan instruction
|
|
1349
|
+
|
|
1350
|
+
// used if coll is Ktoken and we borrow debt token instead
|
|
1351
|
+
const amountToFashBorrowDebt = depositAmount
|
|
1352
|
+
.div(priceDebtToColl)
|
|
1353
|
+
.mul(new Decimal(1 + slippagePct / 100))
|
|
1354
|
+
.toDecimalPlaces(debtReserve!.stats.decimals, Decimal.ROUND_UP);
|
|
1355
|
+
// .toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber());
|
|
1356
|
+
|
|
1357
|
+
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
1358
|
+
borrowIxnIndex: budgetIxns.length + createAtasIxns.length, // TODO: how about user metadata ixns
|
|
1359
|
+
walletPublicKey: user,
|
|
1360
|
+
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
1361
|
+
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
1362
|
+
reserve: !collIsKtoken ? collReserve! : debtReserve!,
|
|
1363
|
+
amountLamports: toLamports(
|
|
1364
|
+
!collIsKtoken ? depositAmount : amountToFashBorrowDebt,
|
|
1365
|
+
!collIsKtoken ? collReserve!.stats.decimals : debtReserve!.stats.decimals
|
|
1366
|
+
),
|
|
1367
|
+
destinationAta: !collIsKtoken ? collTokenAta : debtTokenAta,
|
|
1368
|
+
referrerAccount: kaminoMarket.programId,
|
|
1369
|
+
referrerTokenState: kaminoMarket.programId,
|
|
1370
|
+
programId: kaminoMarket.programId,
|
|
1371
|
+
});
|
|
1372
|
+
|
|
1373
|
+
// 3. Deposit initial tokens + borrowed tokens into reserve
|
|
1374
|
+
let obligationType: ObligationType;
|
|
1375
|
+
if (obligationTypeTagOverride === ObligationTypeTag.Multiply) {
|
|
1376
|
+
// multiply
|
|
1377
|
+
obligationType = new MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
1378
|
+
} else if (obligationTypeTagOverride === ObligationTypeTag.Leverage) {
|
|
1379
|
+
// leverage
|
|
1380
|
+
obligationType = new LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
1381
|
+
} else {
|
|
1382
|
+
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
1383
|
+
}
|
|
1384
|
+
|
|
1385
|
+
const depositAction = await KaminoAction.buildDepositTxns(
|
|
1386
|
+
kaminoMarket,
|
|
1387
|
+
toLamports(depositAmount, collReserve!.stats.decimals).floor().toString(),
|
|
1388
|
+
collTokenMint,
|
|
1389
|
+
user,
|
|
1390
|
+
obligation ? obligation : obligationType,
|
|
1391
|
+
0,
|
|
1392
|
+
false,
|
|
1393
|
+
false,
|
|
1394
|
+
false, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1395
|
+
referrer,
|
|
1396
|
+
currentSlot,
|
|
1397
|
+
{ includeScopeRefresh: true, scopeFeed: scopeFeed! }
|
|
1398
|
+
);
|
|
1399
|
+
|
|
1400
|
+
// 4. Get swap estimations to understand how much we need to borrow from borrow reserve
|
|
1401
|
+
const borrowAmount = depositAmount
|
|
1402
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
1403
|
+
.mul(new Decimal(1 + slippagePct / 100))
|
|
1404
|
+
.div(priceDebtToColl);
|
|
1405
|
+
|
|
1406
|
+
const _collTokenExpectedSwapOut = depositAmount.mul(new Decimal(1).plus(flashLoanFee));
|
|
1407
|
+
|
|
1408
|
+
// 5. Borrow tokens in borrow token reserve that will be swapped to repay flash loan
|
|
1409
|
+
const borrowAction = await KaminoAction.buildBorrowTxns(
|
|
1410
|
+
kaminoMarket,
|
|
1411
|
+
toLamports(borrowAmount, debtReserve!.stats.decimals).ceil().toString(),
|
|
1412
|
+
debtTokenMint,
|
|
1413
|
+
user,
|
|
1414
|
+
obligation ? obligation : obligationType,
|
|
1415
|
+
0,
|
|
1416
|
+
false,
|
|
1417
|
+
false,
|
|
1418
|
+
false, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1419
|
+
referrer,
|
|
1420
|
+
currentSlot,
|
|
1421
|
+
{ includeScopeRefresh: true, scopeFeed: scopeFeed! }
|
|
1422
|
+
);
|
|
1423
|
+
|
|
1424
|
+
const klendIxns = [
|
|
1425
|
+
...budgetIxns,
|
|
1426
|
+
...createAtasIxns,
|
|
1427
|
+
...[flashBorrowIxn],
|
|
1428
|
+
...depositAction.setupIxs,
|
|
1429
|
+
...depositAction.lendingIxs,
|
|
1430
|
+
...depositAction.cleanupIxs,
|
|
1431
|
+
...borrowAction.setupIxs,
|
|
1432
|
+
...borrowAction.lendingIxs,
|
|
1433
|
+
...borrowAction.cleanupIxs,
|
|
1434
|
+
...[flashRepayIxn],
|
|
1435
|
+
...closeAtasIxns,
|
|
1436
|
+
];
|
|
1437
|
+
|
|
1438
|
+
const uniqueAccounts = new PublicKeySet<PublicKey>([]);
|
|
1439
|
+
klendIxns.forEach((ixn) => {
|
|
1440
|
+
ixn.keys.forEach((key) => {
|
|
1441
|
+
uniqueAccounts.add(key.pubkey);
|
|
1442
|
+
});
|
|
1443
|
+
});
|
|
1444
|
+
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
1445
|
+
|
|
1446
|
+
// return early to avoid extra swapper calls
|
|
1447
|
+
if (getTotalKlendAccountsOnly) {
|
|
1448
|
+
return {
|
|
1449
|
+
ixns: [],
|
|
1450
|
+
lookupTablesAddresses: [],
|
|
1451
|
+
swapInputs: { inputAmountLamports: 0, inputMint: PublicKey.default, outputMint: PublicKey.default },
|
|
1452
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
1453
|
+
};
|
|
1454
|
+
}
|
|
1455
|
+
|
|
1456
|
+
let depositSwapper: SwapIxnsProvider;
|
|
1457
|
+
let expectedDebtTokenAtaBalance = new Decimal(0);
|
|
1458
|
+
|
|
1459
|
+
if (collIsKtoken) {
|
|
1460
|
+
if (kamino === undefined) {
|
|
1461
|
+
throw Error('Ktoken use as collateral for leverage without Kamino instance');
|
|
1462
|
+
}
|
|
1463
|
+
depositSwapper = await getTokenToKtokenSwapper(connection, kaminoMarket, kamino, user, swapper, priceAinB, false);
|
|
1464
|
+
|
|
1465
|
+
expectedDebtTokenAtaBalance = await getExpectedTokenBalanceAfterBorrow(
|
|
1466
|
+
connection,
|
|
1467
|
+
debtTokenMint,
|
|
1468
|
+
user,
|
|
1469
|
+
toLamports(!collIsKtoken ? borrowAmount : amountToFashBorrowDebt, debtReserve!.stats.decimals).floor(),
|
|
1470
|
+
debtReserve!.state.liquidity.mintDecimals.toNumber()
|
|
1471
|
+
);
|
|
1472
|
+
} else {
|
|
1473
|
+
depositSwapper = swapper;
|
|
1474
|
+
}
|
|
1475
|
+
|
|
1476
|
+
const swapInputs: SwapInputs = {
|
|
1477
|
+
inputAmountLamports: toLamports(!collIsKtoken ? borrowAmount : amountToFashBorrowDebt, debtReserve!.stats.decimals)
|
|
1478
|
+
.ceil()
|
|
1479
|
+
.toNumber(),
|
|
1480
|
+
inputMint: debtTokenMint,
|
|
1481
|
+
outputMint: collTokenMint,
|
|
1482
|
+
};
|
|
1483
|
+
|
|
1484
|
+
const [swapIxns, lookupTablesAddresses] = await depositSwapper(
|
|
1485
|
+
swapInputs.inputAmountLamports,
|
|
1486
|
+
swapInputs.inputMint,
|
|
1487
|
+
swapInputs.outputMint,
|
|
1488
|
+
slippagePct,
|
|
1489
|
+
expectedDebtTokenAtaBalance
|
|
1490
|
+
);
|
|
1491
|
+
|
|
1492
|
+
const swapInstructions = removeBudgetAndAtaIxns(swapIxns, []);
|
|
1493
|
+
|
|
1494
|
+
const ixns = !collIsKtoken
|
|
1495
|
+
? [
|
|
1496
|
+
...budgetIxns,
|
|
1497
|
+
...createAtasIxns,
|
|
1498
|
+
...[flashBorrowIxn],
|
|
1499
|
+
...depositAction.setupIxs,
|
|
1500
|
+
...depositAction.lendingIxs,
|
|
1501
|
+
...depositAction.cleanupIxs,
|
|
1502
|
+
...borrowAction.setupIxs,
|
|
1503
|
+
...borrowAction.lendingIxs,
|
|
1504
|
+
...borrowAction.cleanupIxs,
|
|
1505
|
+
...swapInstructions,
|
|
1506
|
+
...[flashRepayIxn],
|
|
1507
|
+
...closeAtasIxns,
|
|
1508
|
+
]
|
|
1509
|
+
: [
|
|
1510
|
+
...budgetIxns,
|
|
1511
|
+
...createAtasIxns,
|
|
1512
|
+
...[flashBorrowIxn],
|
|
1513
|
+
...swapInstructions,
|
|
1514
|
+
...depositAction.setupIxs,
|
|
1515
|
+
...depositAction.lendingIxs,
|
|
1516
|
+
...depositAction.cleanupIxs,
|
|
1517
|
+
...borrowAction.setupIxs,
|
|
1518
|
+
...borrowAction.lendingIxs,
|
|
1519
|
+
...borrowAction.cleanupIxs,
|
|
1520
|
+
...[flashRepayIxn],
|
|
1521
|
+
...closeAtasIxns,
|
|
1522
|
+
];
|
|
1523
|
+
|
|
1524
|
+
ixns.forEach((ixn, i) => {
|
|
1525
|
+
console.log(`ixn ${i + 1}: ${ixn.programId.toString()}`);
|
|
1526
|
+
});
|
|
1527
|
+
|
|
1528
|
+
// Create and send transaction
|
|
1529
|
+
if (collIsKtoken) {
|
|
1530
|
+
if (strategy?.strategy.strategyLookupTable) {
|
|
1531
|
+
lookupTablesAddresses.push(strategy?.strategy.strategyLookupTable!);
|
|
1532
|
+
} else {
|
|
1533
|
+
console.log('Strategy lookup table not found');
|
|
1534
|
+
}
|
|
1535
|
+
}
|
|
1536
|
+
return {
|
|
1537
|
+
ixns,
|
|
1538
|
+
lookupTablesAddresses,
|
|
1539
|
+
swapInputs,
|
|
1540
|
+
totalKlendAccounts,
|
|
1541
|
+
};
|
|
1542
|
+
};
|
|
1543
|
+
|
|
1544
|
+
/**
|
|
1545
|
+
* Withdraw and repay tokens if leverage decreased
|
|
1546
|
+
*/
|
|
1547
|
+
export const getDecreaseLeverageIxns = async (props: {
|
|
1548
|
+
connection: Connection;
|
|
1549
|
+
budgetAndPriorityFeeIxns: TransactionInstruction[];
|
|
1550
|
+
user: PublicKey;
|
|
1551
|
+
kaminoMarket: KaminoMarket;
|
|
1552
|
+
withdrawAmount: Decimal;
|
|
1553
|
+
repayAmount: Decimal;
|
|
1554
|
+
collTokenMint: PublicKey;
|
|
1555
|
+
debtTokenMint: PublicKey;
|
|
1556
|
+
slippagePct: number;
|
|
1557
|
+
swapper: SwapIxnsProvider;
|
|
1558
|
+
referrer: PublicKey;
|
|
1559
|
+
isKtoken: IsKtokenProvider;
|
|
1560
|
+
kamino: Kamino | undefined;
|
|
1561
|
+
obligationTypeTagOverride: ObligationTypeTag;
|
|
1562
|
+
obligation: KaminoObligation | null;
|
|
1563
|
+
currentSlot: number;
|
|
1564
|
+
getTotalKlendAccountsOnly: boolean;
|
|
1565
|
+
scopeFeed: string | undefined;
|
|
1566
|
+
}) => {
|
|
1567
|
+
const {
|
|
1568
|
+
connection,
|
|
1569
|
+
budgetAndPriorityFeeIxns,
|
|
1570
|
+
user,
|
|
1571
|
+
kaminoMarket,
|
|
1572
|
+
withdrawAmount,
|
|
1573
|
+
repayAmount,
|
|
1574
|
+
collTokenMint,
|
|
1575
|
+
debtTokenMint,
|
|
1576
|
+
slippagePct,
|
|
1577
|
+
swapper,
|
|
1578
|
+
referrer,
|
|
1579
|
+
isKtoken,
|
|
1580
|
+
kamino,
|
|
1581
|
+
obligationTypeTagOverride = 1,
|
|
1582
|
+
obligation,
|
|
1583
|
+
currentSlot,
|
|
1584
|
+
getTotalKlendAccountsOnly,
|
|
1585
|
+
scopeFeed,
|
|
1586
|
+
} = props;
|
|
1587
|
+
|
|
1588
|
+
console.log(
|
|
1589
|
+
'getDecreaseLeverageIxns',
|
|
1590
|
+
toJson({ withdrawAmount, repayAmount, collTokenMint, debtTokenMint, slippagePct })
|
|
1591
|
+
);
|
|
1592
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
1593
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
1594
|
+
const collIsKtoken = await isKtoken(collTokenMint);
|
|
1595
|
+
|
|
1596
|
+
const flashLoanFee = debtReserve?.getFlashLoanFee() || new Decimal(0);
|
|
1597
|
+
|
|
1598
|
+
const strategy = collIsKtoken ? await kamino?.getStrategyByKTokenMint(collTokenMint) : undefined;
|
|
1599
|
+
|
|
1600
|
+
// 1. Create atas & budget txns
|
|
1601
|
+
const budgetIxns = budgetAndPriorityFeeIxns || getComputeBudgetAndPriorityFeeIxns(3000000);
|
|
1602
|
+
let mintsToCreateAtas: PublicKey[] = [];
|
|
1603
|
+
let mintsToCreateAtasTokenPrograms: PublicKey[] = [];
|
|
1604
|
+
if (collIsKtoken) {
|
|
1605
|
+
const secondTokenAta = strategy!.strategy.tokenAMint.equals(debtTokenMint)
|
|
1606
|
+
? strategy!.strategy.tokenBMint
|
|
1607
|
+
: strategy!.strategy.tokenAMint;
|
|
1608
|
+
const secondTokenTokenProgarm = strategy?.strategy.tokenAMint.equals(debtTokenMint)
|
|
1609
|
+
? strategy!.strategy.tokenBTokenProgram.equals(PublicKey.default)
|
|
1610
|
+
? TOKEN_PROGRAM_ID
|
|
1611
|
+
: strategy!.strategy.tokenBTokenProgram
|
|
1612
|
+
: strategy!.strategy.tokenATokenProgram.equals(PublicKey.default)
|
|
1613
|
+
? TOKEN_PROGRAM_ID
|
|
1614
|
+
: strategy!.strategy.tokenATokenProgram;
|
|
1615
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve!.getCTokenMint(), secondTokenAta];
|
|
1616
|
+
mintsToCreateAtasTokenPrograms = [
|
|
1617
|
+
collReserve!.getLiquidityTokenProgram(),
|
|
1618
|
+
debtReserve!.getLiquidityTokenProgram(),
|
|
1619
|
+
TOKEN_PROGRAM_ID,
|
|
1620
|
+
secondTokenTokenProgarm,
|
|
1621
|
+
];
|
|
1622
|
+
} else {
|
|
1623
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve!.getCTokenMint()];
|
|
1624
|
+
mintsToCreateAtasTokenPrograms = [
|
|
1625
|
+
collReserve!.getLiquidityTokenProgram(),
|
|
1626
|
+
debtReserve!.getLiquidityTokenProgram(),
|
|
1627
|
+
TOKEN_PROGRAM_ID,
|
|
1628
|
+
];
|
|
1629
|
+
}
|
|
1630
|
+
const {
|
|
1631
|
+
atas: [, debtTokenAta],
|
|
1632
|
+
createAtasIxns,
|
|
1633
|
+
closeAtasIxns,
|
|
1634
|
+
} = await getAtasWithCreateIxnsIfMissing(connection, user, mintsToCreateAtas, mintsToCreateAtasTokenPrograms);
|
|
1635
|
+
|
|
1636
|
+
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
1637
|
+
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
1638
|
+
const closeWsolAtaIxns: TransactionInstruction[] = [];
|
|
1639
|
+
if (debtTokenMint.equals(WRAPPED_SOL_MINT)) {
|
|
1640
|
+
const wsolAta = await getAssociatedTokenAddress(WRAPPED_SOL_MINT, user, false);
|
|
1641
|
+
closeWsolAtaIxns.push(createCloseAccountInstruction(wsolAta, user, user, [], TOKEN_PROGRAM_ID));
|
|
1642
|
+
}
|
|
1643
|
+
closeAtasIxns.push(...closeWsolAtaIxns);
|
|
1644
|
+
|
|
1645
|
+
const fillWsolAtaIxns: TransactionInstruction[] = [];
|
|
1646
|
+
if (debtTokenMint.equals(WRAPPED_SOL_MINT)) {
|
|
1647
|
+
const halfSolBalance = (await connection.getBalance(user)) / LAMPORTS_PER_SOL / 2;
|
|
1648
|
+
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
1649
|
+
fillWsolAtaIxns.push(
|
|
1650
|
+
...getDepositWsolIxns(user, debtTokenAta, toLamports(balanceToWrap, debtReserve!.stats.decimals).ceil())
|
|
1651
|
+
);
|
|
1652
|
+
}
|
|
1653
|
+
|
|
1654
|
+
// 3. Flash borrow & repay amount to repay (debt)
|
|
1655
|
+
const { flashBorrowIxn, flashRepayIxn } = getFlashLoanInstructions({
|
|
1656
|
+
borrowIxnIndex: budgetIxns.length + createAtasIxns.length + fillWsolAtaIxns.length,
|
|
1657
|
+
walletPublicKey: user,
|
|
1658
|
+
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
1659
|
+
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
1660
|
+
reserve: debtReserve!,
|
|
1661
|
+
amountLamports: toLamports(repayAmount, debtReserve!.stats.decimals),
|
|
1662
|
+
destinationAta: debtTokenAta,
|
|
1663
|
+
referrerAccount: kaminoMarket.programId,
|
|
1664
|
+
referrerTokenState: kaminoMarket.programId,
|
|
1665
|
+
programId: kaminoMarket.programId,
|
|
1666
|
+
});
|
|
1667
|
+
|
|
1668
|
+
// 4. Actually do the repay of the flash borrowed amounts
|
|
1669
|
+
let obligationType: ObligationType;
|
|
1670
|
+
if (obligationTypeTagOverride === ObligationTypeTag.Multiply) {
|
|
1671
|
+
// multiply
|
|
1672
|
+
obligationType = new MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
1673
|
+
} else if (obligationTypeTagOverride === ObligationTypeTag.Leverage) {
|
|
1674
|
+
// leverage
|
|
1675
|
+
obligationType = new LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
1676
|
+
} else {
|
|
1677
|
+
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
1678
|
+
}
|
|
1679
|
+
|
|
1680
|
+
const scopeRefresh = scopeFeed ? { includeScopeRefresh: true, scopeFeed: scopeFeed } : undefined;
|
|
1681
|
+
|
|
1682
|
+
const repayAction = await KaminoAction.buildRepayTxns(
|
|
1683
|
+
kaminoMarket,
|
|
1684
|
+
toLamports(repayAmount, debtReserve!.stats.decimals).floor().toString(),
|
|
1685
|
+
debtTokenMint,
|
|
1686
|
+
user,
|
|
1687
|
+
obligation ? obligation : obligationType,
|
|
1688
|
+
currentSlot,
|
|
1689
|
+
undefined,
|
|
1690
|
+
0,
|
|
1691
|
+
false,
|
|
1692
|
+
false,
|
|
1693
|
+
false, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1694
|
+
referrer,
|
|
1695
|
+
scopeRefresh
|
|
1696
|
+
);
|
|
1697
|
+
|
|
1698
|
+
// 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
|
|
1699
|
+
const withdrawAmountWithSlippageAndFlashLoanFee = withdrawAmount
|
|
1700
|
+
.mul(new Decimal(1).plus(flashLoanFee))
|
|
1701
|
+
.mul(1 + slippagePct / 100);
|
|
1702
|
+
|
|
1703
|
+
const withdrawAction = await KaminoAction.buildWithdrawTxns(
|
|
1704
|
+
kaminoMarket,
|
|
1705
|
+
toLamports(withdrawAmountWithSlippageAndFlashLoanFee, collReserve!.stats.decimals).ceil().toString(),
|
|
1706
|
+
collTokenMint,
|
|
1707
|
+
user,
|
|
1708
|
+
obligation ? obligation : obligationType,
|
|
1709
|
+
0,
|
|
1710
|
+
false,
|
|
1711
|
+
false,
|
|
1712
|
+
false, // to be checked and create in a setup tx in the UI (won't be the case for adjust anyway as this would be created in deposit)
|
|
1713
|
+
referrer,
|
|
1714
|
+
currentSlot,
|
|
1715
|
+
{ includeScopeRefresh: true, scopeFeed: scopeFeed! }
|
|
1716
|
+
);
|
|
1717
|
+
|
|
1718
|
+
const klendIxns = [
|
|
1719
|
+
...budgetIxns,
|
|
1720
|
+
...createAtasIxns,
|
|
1721
|
+
...fillWsolAtaIxns,
|
|
1722
|
+
...[flashBorrowIxn],
|
|
1723
|
+
...repayAction.setupIxs,
|
|
1724
|
+
...repayAction.lendingIxs,
|
|
1725
|
+
...repayAction.cleanupIxs,
|
|
1726
|
+
...withdrawAction.setupIxs,
|
|
1727
|
+
...withdrawAction.lendingIxs,
|
|
1728
|
+
...withdrawAction.cleanupIxs,
|
|
1729
|
+
...[flashRepayIxn],
|
|
1730
|
+
...closeAtasIxns,
|
|
1731
|
+
];
|
|
1732
|
+
|
|
1733
|
+
const uniqueAccounts = new PublicKeySet<PublicKey>([]);
|
|
1734
|
+
klendIxns.forEach((ixn) => {
|
|
1735
|
+
ixn.keys.forEach((key) => {
|
|
1736
|
+
uniqueAccounts.add(key.pubkey);
|
|
1737
|
+
});
|
|
1738
|
+
});
|
|
1739
|
+
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
1740
|
+
|
|
1741
|
+
// return early to avoid extra swapper calls
|
|
1742
|
+
if (getTotalKlendAccountsOnly) {
|
|
1743
|
+
return {
|
|
1744
|
+
ixns: [],
|
|
1745
|
+
lookupTablesAddresses: [],
|
|
1746
|
+
swapInputs: { inputAmountLamports: 0, inputMint: PublicKey.default, outputMint: PublicKey.default },
|
|
1747
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
1748
|
+
};
|
|
1749
|
+
}
|
|
1750
|
+
|
|
1751
|
+
let withdrawSwapper: SwapIxnsProvider;
|
|
1752
|
+
|
|
1753
|
+
if (collIsKtoken) {
|
|
1754
|
+
if (kamino === undefined) {
|
|
1755
|
+
throw Error('Ktoken use as collateral for leverage without Kamino instance');
|
|
1756
|
+
}
|
|
1757
|
+
withdrawSwapper = await getKtokenToTokenSwapper(kaminoMarket, kamino, user, swapper);
|
|
1758
|
+
} else {
|
|
1759
|
+
withdrawSwapper = swapper;
|
|
1760
|
+
}
|
|
1761
|
+
|
|
1762
|
+
const swapInputs: SwapInputs = {
|
|
1763
|
+
inputAmountLamports: toLamports(withdrawAmountWithSlippageAndFlashLoanFee, collReserve!.stats.decimals)
|
|
1764
|
+
.ceil()
|
|
1765
|
+
.toNumber(),
|
|
1766
|
+
inputMint: collTokenMint,
|
|
1767
|
+
outputMint: debtTokenMint,
|
|
1768
|
+
};
|
|
1769
|
+
|
|
1770
|
+
// 5. Get swap ixns
|
|
1771
|
+
const [swapIxns, lookupTablesAddresses] = await withdrawSwapper(
|
|
1772
|
+
swapInputs.inputAmountLamports,
|
|
1773
|
+
swapInputs.inputMint,
|
|
1774
|
+
swapInputs.outputMint,
|
|
1775
|
+
slippagePct
|
|
1776
|
+
);
|
|
1777
|
+
|
|
1778
|
+
const swapInstructions = removeBudgetAndAtaIxns(swapIxns, []);
|
|
1779
|
+
|
|
1780
|
+
const ixns = [
|
|
1781
|
+
...budgetIxns,
|
|
1782
|
+
...createAtasIxns,
|
|
1783
|
+
...fillWsolAtaIxns,
|
|
1784
|
+
...[flashBorrowIxn],
|
|
1785
|
+
...repayAction.setupIxs,
|
|
1786
|
+
...repayAction.lendingIxs,
|
|
1787
|
+
...repayAction.cleanupIxs,
|
|
1788
|
+
...withdrawAction.setupIxs,
|
|
1789
|
+
...withdrawAction.lendingIxs,
|
|
1790
|
+
...withdrawAction.cleanupIxs,
|
|
1791
|
+
...swapInstructions,
|
|
1792
|
+
...[flashRepayIxn],
|
|
1793
|
+
...closeAtasIxns,
|
|
1794
|
+
];
|
|
1795
|
+
|
|
1796
|
+
ixns.forEach((ixn, i) => {
|
|
1797
|
+
console.log(`ixn ${i + 1}: ${ixn.programId.toString()}`);
|
|
1798
|
+
});
|
|
1799
|
+
|
|
1800
|
+
if (collIsKtoken) {
|
|
1801
|
+
if (strategy?.strategy.strategyLookupTable) {
|
|
1802
|
+
lookupTablesAddresses.push(strategy?.strategy.strategyLookupTable!);
|
|
1803
|
+
} else {
|
|
1804
|
+
console.log('Strategy lookup table not found');
|
|
1805
|
+
}
|
|
1806
|
+
}
|
|
1807
|
+
// Create and send transaction
|
|
1808
|
+
return {
|
|
1809
|
+
ixns,
|
|
1810
|
+
lookupTablesAddresses,
|
|
1811
|
+
swapInputs,
|
|
1812
|
+
totalKlendAccounts,
|
|
1813
|
+
};
|
|
1814
|
+
};
|