@kamino-finance/klend-sdk 2.10.6
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +85 -0
- package/dist/classes/action.d.ts +154 -0
- package/dist/classes/action.js +1505 -0
- package/dist/classes/action.js.map +1 -0
- package/dist/classes/curve.d.ts +15 -0
- package/dist/classes/curve.js +32 -0
- package/dist/classes/curve.js.map +1 -0
- package/dist/classes/fraction.d.ts +23 -0
- package/dist/classes/fraction.js +60 -0
- package/dist/classes/fraction.js.map +1 -0
- package/dist/classes/index.d.ts +8 -0
- package/dist/classes/index.js +25 -0
- package/dist/classes/index.js.map +1 -0
- package/dist/classes/jupiterPerps.d.ts +2 -0
- package/dist/classes/jupiterPerps.js +25 -0
- package/dist/classes/jupiterPerps.js.map +1 -0
- package/dist/classes/market.d.ts +140 -0
- package/dist/classes/market.js +901 -0
- package/dist/classes/market.js.map +1 -0
- package/dist/classes/obligation.d.ts +170 -0
- package/dist/classes/obligation.js +610 -0
- package/dist/classes/obligation.js.map +1 -0
- package/dist/classes/reserve.d.ts +161 -0
- package/dist/classes/reserve.js +474 -0
- package/dist/classes/reserve.js.map +1 -0
- package/dist/classes/shared.d.ts +41 -0
- package/dist/classes/shared.js +10 -0
- package/dist/classes/shared.js.map +1 -0
- package/dist/classes/utils.d.ts +40 -0
- package/dist/classes/utils.js +129 -0
- package/dist/classes/utils.js.map +1 -0
- package/dist/idl.json +4102 -0
- package/dist/idl_codegen/accounts/LendingMarket.d.ts +153 -0
- package/dist/idl_codegen/accounts/LendingMarket.js +204 -0
- package/dist/idl_codegen/accounts/LendingMarket.js.map +1 -0
- package/dist/idl_codegen/accounts/Obligation.d.ts +139 -0
- package/dist/idl_codegen/accounts/Obligation.js +193 -0
- package/dist/idl_codegen/accounts/Obligation.js.map +1 -0
- package/dist/idl_codegen/accounts/ReferrerState.d.ts +22 -0
- package/dist/idl_codegen/accounts/ReferrerState.js +101 -0
- package/dist/idl_codegen/accounts/ReferrerState.js.map +1 -0
- package/dist/idl_codegen/accounts/ReferrerTokenState.d.ts +51 -0
- package/dist/idl_codegen/accounts/ReferrerTokenState.js +126 -0
- package/dist/idl_codegen/accounts/ReferrerTokenState.js.map +1 -0
- package/dist/idl_codegen/accounts/Reserve.d.ts +72 -0
- package/dist/idl_codegen/accounts/Reserve.js +156 -0
- package/dist/idl_codegen/accounts/Reserve.js.map +1 -0
- package/dist/idl_codegen/accounts/ShortUrl.d.ts +22 -0
- package/dist/idl_codegen/accounts/ShortUrl.js +101 -0
- package/dist/idl_codegen/accounts/ShortUrl.js.map +1 -0
- package/dist/idl_codegen/accounts/UserMetadata.d.ts +48 -0
- package/dist/idl_codegen/accounts/UserMetadata.js +126 -0
- package/dist/idl_codegen/accounts/UserMetadata.js.map +1 -0
- package/dist/idl_codegen/accounts/UserState.d.ts +143 -0
- package/dist/idl_codegen/accounts/UserState.js +180 -0
- package/dist/idl_codegen/accounts/UserState.js.map +1 -0
- package/dist/idl_codegen/accounts/index.d.ts +16 -0
- package/dist/idl_codegen/accounts/index.js +20 -0
- package/dist/idl_codegen/accounts/index.js.map +1 -0
- package/dist/idl_codegen/errors/anchor.d.ts +434 -0
- package/dist/idl_codegen/errors/anchor.js +713 -0
- package/dist/idl_codegen/errors/anchor.js.map +1 -0
- package/dist/idl_codegen/errors/custom.d.ts +730 -0
- package/dist/idl_codegen/errors/custom.js +1194 -0
- package/dist/idl_codegen/errors/custom.js.map +1 -0
- package/dist/idl_codegen/errors/index.d.ts +5 -0
- package/dist/idl_codegen/errors/index.js +71 -0
- package/dist/idl_codegen/errors/index.js.map +1 -0
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.d.ts +20 -0
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.js +75 -0
- package/dist/idl_codegen/instructions/borrowObligationLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.d.ts +9 -0
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.js +20 -0
- package/dist/idl_codegen/instructions/deleteReferrerStateAndShortUrl.js.map +1 -0
- package/dist/idl_codegen/instructions/depositObligationCollateral.d.ts +17 -0
- package/dist/idl_codegen/instructions/depositObligationCollateral.js +64 -0
- package/dist/idl_codegen/instructions/depositObligationCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidity.d.ts +19 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidity.js +74 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.d.ts +21 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.js +80 -0
- package/dist/idl_codegen/instructions/depositReserveLiquidityAndObligationCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.d.ts +20 -0
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.js +75 -0
- package/dist/idl_codegen/instructions/flashBorrowReserveLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.d.ts +21 -0
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.js +75 -0
- package/dist/idl_codegen/instructions/flashRepayReserveLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/idlMissingTypes.d.ts +18 -0
- package/dist/idl_codegen/instructions/idlMissingTypes.js +62 -0
- package/dist/idl_codegen/instructions/idlMissingTypes.js.map +1 -0
- package/dist/idl_codegen/instructions/index.d.ts +66 -0
- package/dist/idl_codegen/instructions/index.js +70 -0
- package/dist/idl_codegen/instructions/index.js.map +1 -0
- package/dist/idl_codegen/instructions/initFarmsForReserve.d.ts +19 -0
- package/dist/idl_codegen/instructions/initFarmsForReserve.js +63 -0
- package/dist/idl_codegen/instructions/initFarmsForReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/initLendingMarket.d.ts +13 -0
- package/dist/idl_codegen/instructions/initLendingMarket.js +55 -0
- package/dist/idl_codegen/instructions/initLendingMarket.js.map +1 -0
- package/dist/idl_codegen/instructions/initObligation.d.ts +19 -0
- package/dist/idl_codegen/instructions/initObligation.js +55 -0
- package/dist/idl_codegen/instructions/initObligation.js.map +1 -0
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.d.ts +20 -0
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.js +60 -0
- package/dist/idl_codegen/instructions/initObligationFarmsForReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.d.ts +14 -0
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.js +54 -0
- package/dist/idl_codegen/instructions/initReferrerStateAndShortUrl.js.map +1 -0
- package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts +14 -0
- package/dist/idl_codegen/instructions/initReferrerTokenState.js +50 -0
- package/dist/idl_codegen/instructions/initReferrerTokenState.js.map +1 -0
- package/dist/idl_codegen/instructions/initReserve.d.ts +16 -0
- package/dist/idl_codegen/instructions/initReserve.js +47 -0
- package/dist/idl_codegen/instructions/initReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/initUserMetadata.d.ts +14 -0
- package/dist/idl_codegen/instructions/initUserMetadata.js +54 -0
- package/dist/idl_codegen/instructions/initUserMetadata.js.map +1 -0
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.d.ts +27 -0
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.js +102 -0
- package/dist/idl_codegen/instructions/liquidateObligationAndRedeemReserveCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/redeemFees.d.ts +10 -0
- package/dist/idl_codegen/instructions/redeemFees.js +33 -0
- package/dist/idl_codegen/instructions/redeemFees.js.map +1 -0
- package/dist/idl_codegen/instructions/redeemReserveCollateral.d.ts +19 -0
- package/dist/idl_codegen/instructions/redeemReserveCollateral.js +78 -0
- package/dist/idl_codegen/instructions/redeemReserveCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/refreshObligation.d.ts +6 -0
- package/dist/idl_codegen/instructions/refreshObligation.js +17 -0
- package/dist/idl_codegen/instructions/refreshObligation.js.map +1 -0
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.d.ts +19 -0
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.js +63 -0
- package/dist/idl_codegen/instructions/refreshObligationFarmsForReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/refreshReserve.d.ts +10 -0
- package/dist/idl_codegen/instructions/refreshReserve.js +29 -0
- package/dist/idl_codegen/instructions/refreshReserve.js.map +1 -0
- package/dist/idl_codegen/instructions/repayObligationLiquidity.d.ts +17 -0
- package/dist/idl_codegen/instructions/repayObligationLiquidity.js +60 -0
- package/dist/idl_codegen/instructions/repayObligationLiquidity.js.map +1 -0
- package/dist/idl_codegen/instructions/requestElevationGroup.d.ts +11 -0
- package/dist/idl_codegen/instructions/requestElevationGroup.js +47 -0
- package/dist/idl_codegen/instructions/requestElevationGroup.js.map +1 -0
- package/dist/idl_codegen/instructions/socializeLoss.d.ts +14 -0
- package/dist/idl_codegen/instructions/socializeLoss.js +53 -0
- package/dist/idl_codegen/instructions/socializeLoss.js.map +1 -0
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.d.ts +13 -0
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.js +51 -0
- package/dist/idl_codegen/instructions/updateEntireReserveConfig.js.map +1 -0
- package/dist/idl_codegen/instructions/updateLendingMarket.d.ts +12 -0
- package/dist/idl_codegen/instructions/updateLendingMarket.js +50 -0
- package/dist/idl_codegen/instructions/updateLendingMarket.js.map +1 -0
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.d.ts +6 -0
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.js +21 -0
- package/dist/idl_codegen/instructions/updateLendingMarketOwner.js.map +1 -0
- package/dist/idl_codegen/instructions/updateReserveConfig.d.ts +13 -0
- package/dist/idl_codegen/instructions/updateReserveConfig.js +51 -0
- package/dist/idl_codegen/instructions/updateReserveConfig.js.map +1 -0
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.d.ts +13 -0
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.js +51 -0
- package/dist/idl_codegen/instructions/updateSingleReserveConfig.js.map +1 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.d.ts +18 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.js +69 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.d.ts +21 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js +84 -0
- package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js.map +1 -0
- package/dist/idl_codegen/instructions/withdrawProtocolFee.d.ts +16 -0
- package/dist/idl_codegen/instructions/withdrawProtocolFee.js +59 -0
- package/dist/idl_codegen/instructions/withdrawProtocolFee.js.map +1 -0
- package/dist/idl_codegen/instructions/withdrawReferrerFees.d.ts +12 -0
- package/dist/idl_codegen/instructions/withdrawReferrerFees.js +35 -0
- package/dist/idl_codegen/instructions/withdrawReferrerFees.js.map +1 -0
- package/dist/idl_codegen/programId.d.ts +4 -0
- package/dist/idl_codegen/programId.js +13 -0
- package/dist/idl_codegen/programId.js.map +1 -0
- package/dist/idl_codegen/types/AssetTier.d.ts +44 -0
- package/dist/idl_codegen/types/AssetTier.js +128 -0
- package/dist/idl_codegen/types/AssetTier.js.map +1 -0
- package/dist/idl_codegen/types/BigFractionBytes.d.ts +27 -0
- package/dist/idl_codegen/types/BigFractionBytes.js +73 -0
- package/dist/idl_codegen/types/BigFractionBytes.js.map +1 -0
- package/dist/idl_codegen/types/BorrowRateCurve.d.ts +27 -0
- package/dist/idl_codegen/types/BorrowRateCurve.js +62 -0
- package/dist/idl_codegen/types/BorrowRateCurve.js.map +1 -0
- package/dist/idl_codegen/types/CurvePoint.d.ts +26 -0
- package/dist/idl_codegen/types/CurvePoint.js +66 -0
- package/dist/idl_codegen/types/CurvePoint.js.map +1 -0
- package/dist/idl_codegen/types/ElevationGroup.d.ts +52 -0
- package/dist/idl_codegen/types/ElevationGroup.js +103 -0
- package/dist/idl_codegen/types/ElevationGroup.js.map +1 -0
- package/dist/idl_codegen/types/FeeCalculation.d.ts +31 -0
- package/dist/idl_codegen/types/FeeCalculation.js +102 -0
- package/dist/idl_codegen/types/FeeCalculation.js.map +1 -0
- package/dist/idl_codegen/types/InitObligationArgs.d.ts +26 -0
- package/dist/idl_codegen/types/InitObligationArgs.js +66 -0
- package/dist/idl_codegen/types/InitObligationArgs.js.map +1 -0
- package/dist/idl_codegen/types/LastUpdate.d.ts +47 -0
- package/dist/idl_codegen/types/LastUpdate.js +86 -0
- package/dist/idl_codegen/types/LastUpdate.js.map +1 -0
- package/dist/idl_codegen/types/ObligationCollateral.d.ts +48 -0
- package/dist/idl_codegen/types/ObligationCollateral.js +87 -0
- package/dist/idl_codegen/types/ObligationCollateral.js.map +1 -0
- package/dist/idl_codegen/types/ObligationLiquidity.d.ts +75 -0
- package/dist/idl_codegen/types/ObligationLiquidity.js +107 -0
- package/dist/idl_codegen/types/ObligationLiquidity.js.map +1 -0
- package/dist/idl_codegen/types/PriceHeuristic.d.ts +41 -0
- package/dist/idl_codegen/types/PriceHeuristic.js +75 -0
- package/dist/idl_codegen/types/PriceHeuristic.js.map +1 -0
- package/dist/idl_codegen/types/PythConfiguration.d.ts +25 -0
- package/dist/idl_codegen/types/PythConfiguration.js +62 -0
- package/dist/idl_codegen/types/PythConfiguration.js.map +1 -0
- package/dist/idl_codegen/types/ReserveCollateral.d.ts +53 -0
- package/dist/idl_codegen/types/ReserveCollateral.js +93 -0
- package/dist/idl_codegen/types/ReserveCollateral.js.map +1 -0
- package/dist/idl_codegen/types/ReserveConfig.d.ts +304 -0
- package/dist/idl_codegen/types/ReserveConfig.js +209 -0
- package/dist/idl_codegen/types/ReserveConfig.js.map +1 -0
- package/dist/idl_codegen/types/ReserveFarmKind.d.ts +31 -0
- package/dist/idl_codegen/types/ReserveFarmKind.js +102 -0
- package/dist/idl_codegen/types/ReserveFarmKind.js.map +1 -0
- package/dist/idl_codegen/types/ReserveFees.d.ts +78 -0
- package/dist/idl_codegen/types/ReserveFees.js +86 -0
- package/dist/idl_codegen/types/ReserveFees.js.map +1 -0
- package/dist/idl_codegen/types/ReserveLiquidity.d.ts +173 -0
- package/dist/idl_codegen/types/ReserveLiquidity.js +166 -0
- package/dist/idl_codegen/types/ReserveLiquidity.js.map +1 -0
- package/dist/idl_codegen/types/ReserveStatus.d.ts +44 -0
- package/dist/idl_codegen/types/ReserveStatus.js +128 -0
- package/dist/idl_codegen/types/ReserveStatus.js.map +1 -0
- package/dist/idl_codegen/types/ScopeConfiguration.d.ts +41 -0
- package/dist/idl_codegen/types/ScopeConfiguration.js +76 -0
- package/dist/idl_codegen/types/ScopeConfiguration.js.map +1 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.d.ts +30 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.js +67 -0
- package/dist/idl_codegen/types/SwitchboardConfiguration.js.map +1 -0
- package/dist/idl_codegen/types/TokenInfo.d.ts +109 -0
- package/dist/idl_codegen/types/TokenInfo.js +116 -0
- package/dist/idl_codegen/types/TokenInfo.js.map +1 -0
- package/dist/idl_codegen/types/UpdateConfigMode.d.ts +538 -0
- package/dist/idl_codegen/types/UpdateConfigMode.js +1116 -0
- package/dist/idl_codegen/types/UpdateConfigMode.js.map +1 -0
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.d.ts +155 -0
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js +273 -0
- package/dist/idl_codegen/types/UpdateLendingMarketConfigValue.js.map +1 -0
- package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts +200 -0
- package/dist/idl_codegen/types/UpdateLendingMarketMode.js +440 -0
- package/dist/idl_codegen/types/UpdateLendingMarketMode.js.map +1 -0
- package/dist/idl_codegen/types/WithdrawalCaps.d.ts +38 -0
- package/dist/idl_codegen/types/WithdrawalCaps.js +86 -0
- package/dist/idl_codegen/types/WithdrawalCaps.js.map +1 -0
- package/dist/idl_codegen/types/index.d.ts +65 -0
- package/dist/idl_codegen/types/index.js +77 -0
- package/dist/idl_codegen/types/index.js.map +1 -0
- package/dist/idl_codegen/zero_padding/ObligationZP.d.ts +53 -0
- package/dist/idl_codegen/zero_padding/ObligationZP.js +140 -0
- package/dist/idl_codegen/zero_padding/ObligationZP.js.map +1 -0
- package/dist/idl_codegen/zero_padding/index.d.ts +1 -0
- package/dist/idl_codegen/zero_padding/index.js +6 -0
- package/dist/idl_codegen/zero_padding/index.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.d.ts +48 -0
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.js +144 -0
- package/dist/idl_codegen_jupiter_perps/accounts/Pool.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/accounts/index.d.ts +2 -0
- package/dist/idl_codegen_jupiter_perps/accounts/index.js +6 -0
- package/dist/idl_codegen_jupiter_perps/accounts/index.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/programId.d.ts +3 -0
- package/dist/idl_codegen_jupiter_perps/programId.js +9 -0
- package/dist/idl_codegen_jupiter_perps/programId.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.d.ts +62 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.js +115 -0
- package/dist/idl_codegen_jupiter_perps/types/Fees.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.d.ts +32 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.js +75 -0
- package/dist/idl_codegen_jupiter_perps/types/Limit.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.d.ts +32 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.js +75 -0
- package/dist/idl_codegen_jupiter_perps/types/PoolApr.js.map +1 -0
- package/dist/idl_codegen_jupiter_perps/types/index.d.ts +6 -0
- package/dist/idl_codegen_jupiter_perps/types/index.js +10 -0
- package/dist/idl_codegen_jupiter_perps/types/index.js.map +1 -0
- package/dist/index.d.ts +1 -0
- package/dist/index.js +18 -0
- package/dist/index.js.map +1 -0
- package/dist/lending_operations/index.d.ts +2 -0
- package/dist/lending_operations/index.js +19 -0
- package/dist/lending_operations/index.js.map +1 -0
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts +5 -0
- package/dist/lending_operations/repay_with_collateral_calcs.js +26 -0
- package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -0
- package/dist/lending_operations/repay_with_collateral_operations.d.ts +47 -0
- package/dist/lending_operations/repay_with_collateral_operations.js +163 -0
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -0
- package/dist/leverage/calcs.d.ts +132 -0
- package/dist/leverage/calcs.js +291 -0
- package/dist/leverage/calcs.js.map +1 -0
- package/dist/leverage/index.d.ts +4 -0
- package/dist/leverage/index.js +21 -0
- package/dist/leverage/index.js.map +1 -0
- package/dist/leverage/instructions.d.ts +43 -0
- package/dist/leverage/instructions.js +103 -0
- package/dist/leverage/instructions.js.map +1 -0
- package/dist/leverage/operations.d.ts +231 -0
- package/dist/leverage/operations.js +1058 -0
- package/dist/leverage/operations.js.map +1 -0
- package/dist/leverage/utils.d.ts +22 -0
- package/dist/leverage/utils.js +144 -0
- package/dist/leverage/utils.js.map +1 -0
- package/dist/lib.d.ts +9 -0
- package/dist/lib.js +26 -0
- package/dist/lib.js.map +1 -0
- package/dist/referrals/index.d.ts +2 -0
- package/dist/referrals/index.js +19 -0
- package/dist/referrals/index.js.map +1 -0
- package/dist/referrals/instructions.d.ts +16 -0
- package/dist/referrals/instructions.js +76 -0
- package/dist/referrals/instructions.js.map +1 -0
- package/dist/referrals/operations.d.ts +97 -0
- package/dist/referrals/operations.js +258 -0
- package/dist/referrals/operations.js.map +1 -0
- package/dist/utils/ObligationType.d.ts +49 -0
- package/dist/utils/ObligationType.js +129 -0
- package/dist/utils/ObligationType.js.map +1 -0
- package/dist/utils/api.d.ts +6 -0
- package/dist/utils/api.js +61 -0
- package/dist/utils/api.js.map +1 -0
- package/dist/utils/ata.d.ts +25 -0
- package/dist/utils/ata.js +155 -0
- package/dist/utils/ata.js.map +1 -0
- package/dist/utils/constants.d.ts +48 -0
- package/dist/utils/constants.js +75 -0
- package/dist/utils/constants.js.map +1 -0
- package/dist/utils/idl.d.ts +2 -0
- package/dist/utils/idl.js +9 -0
- package/dist/utils/idl.js.map +1 -0
- package/dist/utils/index.d.ts +14 -0
- package/dist/utils/index.js +31 -0
- package/dist/utils/index.js.map +1 -0
- package/dist/utils/instruction.d.ts +22 -0
- package/dist/utils/instruction.js +202 -0
- package/dist/utils/instruction.js.map +1 -0
- package/dist/utils/kamino.d.ts +3 -0
- package/dist/utils/kamino.js +45 -0
- package/dist/utils/kamino.js.map +1 -0
- package/dist/utils/layout.d.ts +13 -0
- package/dist/utils/layout.js +123 -0
- package/dist/utils/layout.js.map +1 -0
- package/dist/utils/lookupTable.d.ts +5 -0
- package/dist/utils/lookupTable.js +57 -0
- package/dist/utils/lookupTable.js.map +1 -0
- package/dist/utils/oracle.d.ts +51 -0
- package/dist/utils/oracle.js +295 -0
- package/dist/utils/oracle.js.map +1 -0
- package/dist/utils/pubkey.d.ts +74 -0
- package/dist/utils/pubkey.js +229 -0
- package/dist/utils/pubkey.js.map +1 -0
- package/dist/utils/seeds.d.ts +122 -0
- package/dist/utils/seeds.js +153 -0
- package/dist/utils/seeds.js.map +1 -0
- package/dist/utils/slots.d.ts +18 -0
- package/dist/utils/slots.js +32 -0
- package/dist/utils/slots.js.map +1 -0
- package/dist/utils/syncNative.d.ts +10 -0
- package/dist/utils/syncNative.js +46 -0
- package/dist/utils/syncNative.js.map +1 -0
- package/dist/utils/userMetadata.d.ts +15 -0
- package/dist/utils/userMetadata.js +259 -0
- package/dist/utils/userMetadata.js.map +1 -0
- package/package.json +94 -0
|
@@ -0,0 +1,1058 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.getDecreaseLeverageIxns = exports.getIncreaseLeverageIxns = exports.getAdjustLeverageIxns = exports.getAdjustLeverageSwapInputs = exports.getWithdrawWithLeverageIxns = exports.getWithdrawWithLeverageSwapInputs = exports.getDepositWithLeverageIxns = exports.getDepositWithLeverageSwapInputs = exports.depositLeverageKtokenCalcs = exports.depositLeverageCalcs = void 0;
|
|
16
|
+
const web3_js_1 = require("@solana/web3.js");
|
|
17
|
+
const decimal_js_1 = __importDefault(require("decimal.js"));
|
|
18
|
+
const classes_1 = require("../classes");
|
|
19
|
+
const instructions_1 = require("./instructions");
|
|
20
|
+
const classes_2 = require("../classes");
|
|
21
|
+
const utils_1 = require("../utils");
|
|
22
|
+
const calcs_1 = require("./calcs");
|
|
23
|
+
const spl_token_1 = require("@solana/spl-token");
|
|
24
|
+
const utils_2 = require("./utils");
|
|
25
|
+
const depositLeverageCalcs = (props) => {
|
|
26
|
+
// Initialize local variables from the props object
|
|
27
|
+
const { depositAmount, depositTokenIsCollToken, depositTokenIsSol, priceDebtToColl, targetLeverage, slippagePct, flashLoanFee, } = props;
|
|
28
|
+
const slippage = slippagePct.div('100');
|
|
29
|
+
const initDepositInSol = depositTokenIsSol ? depositAmount : new decimal_js_1.default(0);
|
|
30
|
+
// Core logic
|
|
31
|
+
if (depositTokenIsCollToken) {
|
|
32
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
33
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
34
|
+
const finalColl = depositAmount.mul(x).div(x.sub(targetLeverage.sub('1').div(y)));
|
|
35
|
+
const debt = finalColl.sub(depositAmount).mul(x);
|
|
36
|
+
const flashBorrowColl = finalColl.sub(depositAmount).mul(flashLoanFee.add('1'));
|
|
37
|
+
return {
|
|
38
|
+
flashBorrowInCollToken: flashBorrowColl,
|
|
39
|
+
initDepositInSol,
|
|
40
|
+
debtTokenToBorrow: debt,
|
|
41
|
+
collTokenToDeposit: finalColl,
|
|
42
|
+
swapDebtTokenIn: debt,
|
|
43
|
+
swapCollTokenExpectedOut: finalColl.sub(depositAmount),
|
|
44
|
+
};
|
|
45
|
+
}
|
|
46
|
+
else {
|
|
47
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
48
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
49
|
+
const finalColl = depositAmount.div(x.sub(targetLeverage.sub('1').div(y)));
|
|
50
|
+
const flashBorrowColl = finalColl.mul(flashLoanFee.add('1'));
|
|
51
|
+
const debt = targetLeverage.sub('1').mul(finalColl).div(y);
|
|
52
|
+
return {
|
|
53
|
+
flashBorrowInCollToken: flashBorrowColl,
|
|
54
|
+
initDepositInSol,
|
|
55
|
+
debtTokenToBorrow: debt,
|
|
56
|
+
collTokenToDeposit: finalColl,
|
|
57
|
+
swapDebtTokenIn: debt.add(depositAmount),
|
|
58
|
+
swapCollTokenExpectedOut: finalColl,
|
|
59
|
+
};
|
|
60
|
+
}
|
|
61
|
+
};
|
|
62
|
+
exports.depositLeverageCalcs = depositLeverageCalcs;
|
|
63
|
+
const depositLeverageKtokenCalcs = (props) => __awaiter(void 0, void 0, void 0, function* () {
|
|
64
|
+
const { kamino, strategy, debtTokenMint, depositAmount, depositTokenIsCollToken, depositTokenIsSol, priceDebtToColl, targetLeverage, slippagePct, flashLoanFee, priceAinB, strategyHoldings, } = props;
|
|
65
|
+
const initDepositInSol = depositTokenIsSol ? depositAmount : new decimal_js_1.default(0);
|
|
66
|
+
const slippage = slippagePct.div('100');
|
|
67
|
+
let flashBorrowInDebtToken;
|
|
68
|
+
let collTokenToDeposit;
|
|
69
|
+
let debtTokenToBorrow;
|
|
70
|
+
if (depositTokenIsCollToken) {
|
|
71
|
+
const x = slippage.add('1').div(priceDebtToColl);
|
|
72
|
+
const y = flashLoanFee.add('1').mul(priceDebtToColl);
|
|
73
|
+
const z = targetLeverage.mul(y).div(targetLeverage.sub(1));
|
|
74
|
+
flashBorrowInDebtToken = depositAmount.div(z.minus(new decimal_js_1.default(1).div(x)));
|
|
75
|
+
collTokenToDeposit = depositAmount.add(flashBorrowInDebtToken.div(x));
|
|
76
|
+
debtTokenToBorrow = flashBorrowInDebtToken.mul(new decimal_js_1.default(1).add(flashLoanFee));
|
|
77
|
+
return {
|
|
78
|
+
flashBorrowInDebtToken,
|
|
79
|
+
initDepositInSol,
|
|
80
|
+
collTokenToDeposit,
|
|
81
|
+
debtTokenToBorrow,
|
|
82
|
+
requiredCollateral: collTokenToDeposit.sub(depositAmount),
|
|
83
|
+
singleSidedDeposit: flashBorrowInDebtToken,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
else {
|
|
87
|
+
const y = targetLeverage.mul(priceDebtToColl);
|
|
88
|
+
// 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
|
|
89
|
+
// This also allows for some variation in the pool ratios between calculation + submitting the tx
|
|
90
|
+
const x = flashLoanFee.add('1').mul(slippage.add('1')).div(priceDebtToColl);
|
|
91
|
+
// 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
|
|
92
|
+
const finalColl = depositAmount.div(x.sub(targetLeverage.sub('1').div(y)));
|
|
93
|
+
// Calculate how many A and B tokens we will need to actually mint the desired amount of ktoken collateral
|
|
94
|
+
// The actual amount of ktokens received may be less than the finalColl due to smart proportional contract logic
|
|
95
|
+
// So we use the actualColl as the amount we will deposit
|
|
96
|
+
const [estimatedA, estimatedB, actualColl] = yield (0, calcs_1.simulateMintKToken)(kamino, strategy, finalColl, strategyHoldings);
|
|
97
|
+
const pxAinB = yield priceAinB(strategy.strategy.tokenAMint, strategy.strategy.tokenBMint);
|
|
98
|
+
const isTokenADeposit = strategy.strategy.tokenAMint.equals(debtTokenMint);
|
|
99
|
+
// Calculate the amount we need to flash borrow by combining value of A and B into the debt token
|
|
100
|
+
const singleSidedDepositAmount = isTokenADeposit
|
|
101
|
+
? estimatedA.add(estimatedB.div(pxAinB))
|
|
102
|
+
: estimatedB.add(estimatedA.mul(pxAinB));
|
|
103
|
+
// Add slippage to the entire amount, add flash loan fee to part we will flash borrow
|
|
104
|
+
flashBorrowInDebtToken = singleSidedDepositAmount
|
|
105
|
+
.div(new decimal_js_1.default('1').sub(slippage))
|
|
106
|
+
.sub(depositAmount)
|
|
107
|
+
.div(new decimal_js_1.default('1').sub(flashLoanFee));
|
|
108
|
+
// Deposit the min ktoken amount we calculated at the beginning
|
|
109
|
+
// Any slippage will be left in the user's wallet as ktokens
|
|
110
|
+
collTokenToDeposit = actualColl;
|
|
111
|
+
debtTokenToBorrow = flashBorrowInDebtToken.div(new decimal_js_1.default('1').sub(flashLoanFee));
|
|
112
|
+
// Add slippage to ensure we try to swap/deposit as much as possible after flash loan fees
|
|
113
|
+
const singleSidedDeposit = singleSidedDepositAmount.div(new decimal_js_1.default('1').sub(slippage));
|
|
114
|
+
return {
|
|
115
|
+
flashBorrowInDebtToken,
|
|
116
|
+
initDepositInSol,
|
|
117
|
+
collTokenToDeposit,
|
|
118
|
+
debtTokenToBorrow,
|
|
119
|
+
requiredCollateral: collTokenToDeposit,
|
|
120
|
+
singleSidedDeposit,
|
|
121
|
+
};
|
|
122
|
+
}
|
|
123
|
+
});
|
|
124
|
+
exports.depositLeverageKtokenCalcs = depositLeverageKtokenCalcs;
|
|
125
|
+
const getDepositWithLeverageSwapInputs = (props) => {
|
|
126
|
+
const { depositAmount, priceDebtToColl, slippagePct, targetLeverage, kaminoMarket, selectedTokenMint, debtTokenMint, collTokenMint, } = props;
|
|
127
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
128
|
+
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
129
|
+
const solTokenReserve = kaminoMarket.getReserveByMint(utils_1.WRAPPED_SOL_MINT);
|
|
130
|
+
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve.getLiquidityMint());
|
|
131
|
+
const flashLoanFee = (debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
132
|
+
const calcs = (0, exports.depositLeverageCalcs)({
|
|
133
|
+
depositAmount,
|
|
134
|
+
depositTokenIsCollToken: selectedTokenIsCollToken,
|
|
135
|
+
depositTokenIsSol,
|
|
136
|
+
priceDebtToColl,
|
|
137
|
+
targetLeverage,
|
|
138
|
+
slippagePct,
|
|
139
|
+
flashLoanFee,
|
|
140
|
+
});
|
|
141
|
+
return {
|
|
142
|
+
swapInputs: {
|
|
143
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(calcs.swapDebtTokenIn, debtReserve.state.liquidity.mintDecimals.toNumber())
|
|
144
|
+
.ceil()
|
|
145
|
+
.toNumber(),
|
|
146
|
+
inputMint: debtTokenMint,
|
|
147
|
+
outputMint: collTokenMint,
|
|
148
|
+
},
|
|
149
|
+
};
|
|
150
|
+
};
|
|
151
|
+
exports.getDepositWithLeverageSwapInputs = getDepositWithLeverageSwapInputs;
|
|
152
|
+
const getDepositWithLeverageIxns = (props) => __awaiter(void 0, void 0, void 0, function* () {
|
|
153
|
+
const { connection, budgetAndPriorityFeeIxns, user, amount, selectedTokenMint, collTokenMint, debtTokenMint, targetLeverage, kaminoMarket, slippagePct, priceDebtToColl, swapper, referrer, isKtoken, priceAinB, kamino, obligationTypeTagOverride = 1, obligation, currentSlot, getTotalKlendAccountsOnly, } = props;
|
|
154
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
155
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
156
|
+
const solTokenReserve = kaminoMarket.getReserveByMint(utils_1.WRAPPED_SOL_MINT);
|
|
157
|
+
const flashLoanFee = (collReserve === null || collReserve === void 0 ? void 0 : collReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
158
|
+
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
159
|
+
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve.getLiquidityMint());
|
|
160
|
+
const collIsKtoken = yield isKtoken(collTokenMint);
|
|
161
|
+
const strategy = collIsKtoken ? (yield kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
|
|
162
|
+
const calcs = (0, exports.depositLeverageCalcs)({
|
|
163
|
+
depositAmount: amount,
|
|
164
|
+
depositTokenIsCollToken: selectedTokenIsCollToken,
|
|
165
|
+
depositTokenIsSol,
|
|
166
|
+
priceDebtToColl,
|
|
167
|
+
targetLeverage,
|
|
168
|
+
slippagePct,
|
|
169
|
+
flashLoanFee,
|
|
170
|
+
});
|
|
171
|
+
let calcsKtoken;
|
|
172
|
+
if (collIsKtoken) {
|
|
173
|
+
calcsKtoken = yield (0, exports.depositLeverageKtokenCalcs)({
|
|
174
|
+
kamino: kamino,
|
|
175
|
+
strategy: strategy,
|
|
176
|
+
debtTokenMint,
|
|
177
|
+
depositAmount: amount,
|
|
178
|
+
depositTokenIsCollToken: selectedTokenIsCollToken,
|
|
179
|
+
depositTokenIsSol,
|
|
180
|
+
priceDebtToColl,
|
|
181
|
+
targetLeverage,
|
|
182
|
+
slippagePct,
|
|
183
|
+
flashLoanFee,
|
|
184
|
+
priceAinB,
|
|
185
|
+
});
|
|
186
|
+
// Rounding to exact number of decimals so this value is passed through in all calcs without rounding inconsistencies
|
|
187
|
+
calcsKtoken.flashBorrowInDebtToken = calcsKtoken.flashBorrowInDebtToken.toDecimalPlaces(debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.state.liquidity.mintDecimals.toNumber(), decimal_js_1.default.ROUND_CEIL);
|
|
188
|
+
calcsKtoken.debtTokenToBorrow = calcsKtoken.debtTokenToBorrow.toDecimalPlaces(debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.state.liquidity.mintDecimals.toNumber(), decimal_js_1.default.ROUND_CEIL);
|
|
189
|
+
calcsKtoken.singleSidedDeposit = calcsKtoken.singleSidedDeposit.toDecimalPlaces(debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.state.liquidity.mintDecimals.toNumber(), decimal_js_1.default.ROUND_CEIL);
|
|
190
|
+
}
|
|
191
|
+
console.log('Ops Calcs', (0, calcs_1.toJson)(!collIsKtoken ? calcs : calcsKtoken));
|
|
192
|
+
console.log('Infos', (0, calcs_1.toJson)({
|
|
193
|
+
depositTokenIsSol,
|
|
194
|
+
selectedTokenIsCollToken,
|
|
195
|
+
initDepositInSol: calcs.initDepositInSol,
|
|
196
|
+
}));
|
|
197
|
+
// // 1. Create atas & budget txns
|
|
198
|
+
let mintsToCreateAtas = [];
|
|
199
|
+
if (collIsKtoken) {
|
|
200
|
+
const secondTokenAta = strategy.strategy.tokenAMint.equals(debtTokenMint)
|
|
201
|
+
? strategy.strategy.tokenBMint
|
|
202
|
+
: strategy.strategy.tokenAMint;
|
|
203
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve.getCTokenMint(), secondTokenAta];
|
|
204
|
+
}
|
|
205
|
+
else {
|
|
206
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve.getCTokenMint()];
|
|
207
|
+
}
|
|
208
|
+
const budgetIxns = budgetAndPriorityFeeIxns || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
209
|
+
const { atas: [collTokenAta, debtTokenAta], createAtasIxns, closeAtasIxns, } = yield (0, utils_1.getAtasWithCreateIxnsIfMissing)(connection, user, mintsToCreateAtas);
|
|
210
|
+
// TODO: this needs to work the other way around also
|
|
211
|
+
// TODO: marius test this with shorting leverage and with leverage looping
|
|
212
|
+
const fillWsolAtaIxns = [];
|
|
213
|
+
if (depositTokenIsSol) {
|
|
214
|
+
fillWsolAtaIxns.push(...(0, utils_1.getDepositWsolIxns)(user, selectedTokenIsCollToken ? collTokenAta : debtTokenAta, (0, classes_2.numberToLamportsDecimal)(calcs.initDepositInSol, solTokenReserve.stats.decimals).ceil()));
|
|
215
|
+
}
|
|
216
|
+
// 1. Flash borrow & repay the collateral amount needed for given leverage
|
|
217
|
+
// if user deposits coll, then we borrow the diff, else we borrow the entire amount
|
|
218
|
+
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
219
|
+
borrowIxnIndex: budgetIxns.length + createAtasIxns.length + fillWsolAtaIxns.length,
|
|
220
|
+
walletPublicKey: user,
|
|
221
|
+
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
222
|
+
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
223
|
+
reserve: !collIsKtoken ? collReserve : debtReserve,
|
|
224
|
+
amountLamports: (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.flashBorrowInCollToken : calcsKtoken.flashBorrowInDebtToken, !collIsKtoken ? collReserve.stats.decimals : debtReserve.stats.decimals),
|
|
225
|
+
destinationAta: !collIsKtoken ? collTokenAta : debtTokenAta,
|
|
226
|
+
referrerAccount: kaminoMarket.programId,
|
|
227
|
+
referrerTokenState: kaminoMarket.programId,
|
|
228
|
+
programId: kaminoMarket.programId,
|
|
229
|
+
});
|
|
230
|
+
console.log('Borrowing: ', (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.debtTokenToBorrow : calcsKtoken.debtTokenToBorrow, debtReserve.stats.decimals)
|
|
231
|
+
.ceil()
|
|
232
|
+
.toString());
|
|
233
|
+
// 3. Deposit initial tokens + borrowed tokens into reserve
|
|
234
|
+
let obligationType;
|
|
235
|
+
if (obligationTypeTagOverride === utils_1.ObligationTypeTag.Multiply) {
|
|
236
|
+
// multiply
|
|
237
|
+
obligationType = new utils_1.MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
238
|
+
}
|
|
239
|
+
else if (obligationTypeTagOverride === utils_1.ObligationTypeTag.Leverage) {
|
|
240
|
+
// leverage
|
|
241
|
+
obligationType = new utils_1.LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
242
|
+
}
|
|
243
|
+
else {
|
|
244
|
+
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
245
|
+
}
|
|
246
|
+
const kaminoDepositAndBorrowAction = yield classes_1.KaminoAction.buildDepositAndBorrowTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.collTokenToDeposit : calcsKtoken.collTokenToDeposit, collReserve.stats.decimals)
|
|
247
|
+
.floor()
|
|
248
|
+
.toString(), collTokenMint, (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.debtTokenToBorrow : calcsKtoken.debtTokenToBorrow, debtReserve.stats.decimals)
|
|
249
|
+
.ceil()
|
|
250
|
+
.toString(), debtTokenMint, user, obligation ? obligation : obligationType, 0, false, true, // emode
|
|
251
|
+
false, // to be checked and created in a setup tx in the UI
|
|
252
|
+
referrer, currentSlot);
|
|
253
|
+
console.log('Expected to swap in', !collIsKtoken ? calcs.swapDebtTokenIn.toNumber().toString() : calcsKtoken.singleSidedDeposit.toNumber().toString(), 'debt for', !collIsKtoken ? calcs.swapCollTokenExpectedOut.toString() : calcsKtoken.requiredCollateral.toNumber().toString(), 'coll');
|
|
254
|
+
const ixns = [
|
|
255
|
+
...budgetIxns,
|
|
256
|
+
...createAtasIxns,
|
|
257
|
+
...fillWsolAtaIxns,
|
|
258
|
+
...[flashBorrowIxn],
|
|
259
|
+
...kaminoDepositAndBorrowAction.setupIxs,
|
|
260
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[0]],
|
|
261
|
+
...kaminoDepositAndBorrowAction.inBetweenIxs,
|
|
262
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[1]],
|
|
263
|
+
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
264
|
+
...[flashRepayIxn],
|
|
265
|
+
...closeAtasIxns,
|
|
266
|
+
];
|
|
267
|
+
const uniqueAccounts = new utils_1.PublicKeySet([]);
|
|
268
|
+
ixns.forEach((ixn) => {
|
|
269
|
+
ixn.keys.forEach((key) => {
|
|
270
|
+
uniqueAccounts.add(key.pubkey);
|
|
271
|
+
});
|
|
272
|
+
});
|
|
273
|
+
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
274
|
+
// return early to avoid extra swapper calls
|
|
275
|
+
if (getTotalKlendAccountsOnly) {
|
|
276
|
+
return {
|
|
277
|
+
ixns: [],
|
|
278
|
+
lookupTablesAddresses: [],
|
|
279
|
+
swapInputs: { inputAmountLamports: 0, inputMint: web3_js_1.PublicKey.default, outputMint: web3_js_1.PublicKey.default },
|
|
280
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
281
|
+
};
|
|
282
|
+
}
|
|
283
|
+
let depositSwapper;
|
|
284
|
+
let expectedDebtTokenAtaBalance = new decimal_js_1.default(0); // only needed for kTokens
|
|
285
|
+
if (!collIsKtoken) {
|
|
286
|
+
depositSwapper = swapper;
|
|
287
|
+
}
|
|
288
|
+
else {
|
|
289
|
+
if (kamino === undefined) {
|
|
290
|
+
throw Error('Ktoken use as collateral for leverage without Kamino instance');
|
|
291
|
+
}
|
|
292
|
+
depositSwapper = yield (0, utils_2.getTokenToKtokenSwapper)(connection, kaminoMarket, kamino, user, swapper, priceAinB, false);
|
|
293
|
+
let futureBalanceInAta = new decimal_js_1.default(0);
|
|
294
|
+
if (debtTokenMint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
295
|
+
futureBalanceInAta = futureBalanceInAta.add(!collIsKtoken ? calcs.initDepositInSol : calcsKtoken.initDepositInSol);
|
|
296
|
+
}
|
|
297
|
+
futureBalanceInAta = futureBalanceInAta.add(!collIsKtoken ? calcs.debtTokenToBorrow : calcsKtoken.flashBorrowInDebtToken);
|
|
298
|
+
expectedDebtTokenAtaBalance = yield (0, utils_2.getExpectedTokenBalanceAfterBorrow)(connection, debtTokenMint, user, (0, classes_2.numberToLamportsDecimal)(futureBalanceInAta.toDecimalPlaces(debtReserve.stats.decimals), debtReserve.stats.decimals), debtReserve.state.liquidity.mintDecimals.toNumber());
|
|
299
|
+
}
|
|
300
|
+
const swapInputs = {
|
|
301
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? calcs.swapDebtTokenIn : calcsKtoken.singleSidedDeposit, debtReserve.stats.decimals)
|
|
302
|
+
.ceil()
|
|
303
|
+
.toNumber(),
|
|
304
|
+
inputMint: debtTokenMint,
|
|
305
|
+
outputMint: collTokenMint,
|
|
306
|
+
};
|
|
307
|
+
const [swapIxns, lookupTablesAddresses] = yield depositSwapper(swapInputs.inputAmountLamports, swapInputs.inputMint, swapInputs.outputMint, slippagePct.toNumber(), expectedDebtTokenAtaBalance);
|
|
308
|
+
if (collIsKtoken) {
|
|
309
|
+
if (strategy === null || strategy === void 0 ? void 0 : strategy.strategy.strategyLookupTable) {
|
|
310
|
+
lookupTablesAddresses.push(strategy === null || strategy === void 0 ? void 0 : strategy.strategy.strategyLookupTable);
|
|
311
|
+
}
|
|
312
|
+
else {
|
|
313
|
+
console.log('Strategy lookup table not found');
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapIxns, []);
|
|
317
|
+
if (!collIsKtoken) {
|
|
318
|
+
return {
|
|
319
|
+
ixns: [
|
|
320
|
+
...budgetIxns,
|
|
321
|
+
...createAtasIxns,
|
|
322
|
+
...fillWsolAtaIxns,
|
|
323
|
+
...[flashBorrowIxn],
|
|
324
|
+
...kaminoDepositAndBorrowAction.setupIxs,
|
|
325
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[0]],
|
|
326
|
+
...kaminoDepositAndBorrowAction.inBetweenIxs,
|
|
327
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[1]],
|
|
328
|
+
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
329
|
+
...swapInstructions,
|
|
330
|
+
...[flashRepayIxn],
|
|
331
|
+
...closeAtasIxns,
|
|
332
|
+
],
|
|
333
|
+
lookupTablesAddresses,
|
|
334
|
+
swapInputs,
|
|
335
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
336
|
+
};
|
|
337
|
+
}
|
|
338
|
+
else {
|
|
339
|
+
return {
|
|
340
|
+
ixns: [
|
|
341
|
+
...budgetIxns,
|
|
342
|
+
...createAtasIxns,
|
|
343
|
+
...fillWsolAtaIxns,
|
|
344
|
+
...[flashBorrowIxn],
|
|
345
|
+
...swapInstructions,
|
|
346
|
+
...kaminoDepositAndBorrowAction.setupIxs,
|
|
347
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[0]],
|
|
348
|
+
...kaminoDepositAndBorrowAction.inBetweenIxs,
|
|
349
|
+
...[kaminoDepositAndBorrowAction.lendingIxs[1]],
|
|
350
|
+
...kaminoDepositAndBorrowAction.cleanupIxs,
|
|
351
|
+
...[flashRepayIxn],
|
|
352
|
+
...closeAtasIxns,
|
|
353
|
+
],
|
|
354
|
+
lookupTablesAddresses,
|
|
355
|
+
swapInputs,
|
|
356
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
357
|
+
};
|
|
358
|
+
}
|
|
359
|
+
});
|
|
360
|
+
exports.getDepositWithLeverageIxns = getDepositWithLeverageIxns;
|
|
361
|
+
const getWithdrawWithLeverageSwapInputs = (props) => {
|
|
362
|
+
const { amount, deposited, borrowed, priceCollToDebt, slippagePct, isClosingPosition, kaminoMarket, selectedTokenMint, debtTokenMint, collTokenMint, userObligation, currentSlot, } = props;
|
|
363
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
364
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
365
|
+
const flashLoanFee = (debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
366
|
+
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
367
|
+
const { adjustDepositPosition: withdrawAmount, adjustBorrowPosition: initialRepayAmount } = isClosingPosition
|
|
368
|
+
? { adjustDepositPosition: deposited, adjustBorrowPosition: borrowed }
|
|
369
|
+
: (0, calcs_1.calcWithdrawAmounts)({
|
|
370
|
+
collTokenMint: collTokenMint,
|
|
371
|
+
priceCollToDebt: new decimal_js_1.default(priceCollToDebt),
|
|
372
|
+
currentDepositPosition: deposited,
|
|
373
|
+
currentBorrowPosition: borrowed,
|
|
374
|
+
withdrawAmount: new decimal_js_1.default(amount),
|
|
375
|
+
selectedTokenMint: selectedTokenMint,
|
|
376
|
+
});
|
|
377
|
+
const irSlippageBpsForDebt = userObligation
|
|
378
|
+
.estimateObligationInterestRate(debtReserve, userObligation.state.borrows[0], currentSlot)
|
|
379
|
+
.toDecimalPlaces(debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.state.liquidity.mintDecimals.toNumber(), decimal_js_1.default.ROUND_CEIL);
|
|
380
|
+
const repayAmount = initialRepayAmount
|
|
381
|
+
.mul(irSlippageBpsForDebt.add('0.1').div('10_000').add('1'))
|
|
382
|
+
.toDecimalPlaces(debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.state.liquidity.mintDecimals.toNumber(), decimal_js_1.default.ROUND_CEIL);
|
|
383
|
+
const swapAmountIfWithdrawingColl = repayAmount
|
|
384
|
+
.mul(new decimal_js_1.default(1).plus(flashLoanFee))
|
|
385
|
+
.mul(new decimal_js_1.default(1 + slippagePct / 100))
|
|
386
|
+
.div(priceCollToDebt);
|
|
387
|
+
const swapAmountIfWithdrawingDebt = withdrawAmount;
|
|
388
|
+
const collTokenSwapIn = selectedTokenIsCollToken ? swapAmountIfWithdrawingColl : swapAmountIfWithdrawingDebt;
|
|
389
|
+
return {
|
|
390
|
+
swapInputs: {
|
|
391
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(collTokenSwapIn, collReserve.state.liquidity.mintDecimals.toNumber())
|
|
392
|
+
.ceil()
|
|
393
|
+
.toNumber(),
|
|
394
|
+
inputMint: collTokenMint,
|
|
395
|
+
outputMint: debtTokenMint,
|
|
396
|
+
},
|
|
397
|
+
};
|
|
398
|
+
};
|
|
399
|
+
exports.getWithdrawWithLeverageSwapInputs = getWithdrawWithLeverageSwapInputs;
|
|
400
|
+
const getWithdrawWithLeverageIxns = (props) => __awaiter(void 0, void 0, void 0, function* () {
|
|
401
|
+
const { connection, budgetAndPriorityFeeIxns, user, amount, deposited, borrowed, collTokenMint, debtTokenMint, priceCollToDebt, selectedTokenMint, isClosingPosition, kaminoMarket, slippagePct, swapper, referrer, isKtoken, kamino, obligationTypeTagOverride, obligation, currentSlot, getTotalKlendAccountsOnly, } = props;
|
|
402
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
403
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
404
|
+
const flashLoanFee = (debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
405
|
+
const collIsKtoken = yield isKtoken(collTokenMint);
|
|
406
|
+
const solTokenReserve = kaminoMarket.getReserveByMint(utils_1.WRAPPED_SOL_MINT);
|
|
407
|
+
const selectedTokenIsCollToken = selectedTokenMint.equals(collTokenMint);
|
|
408
|
+
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve.getLiquidityMint());
|
|
409
|
+
let obligationType;
|
|
410
|
+
if (obligationTypeTagOverride == utils_1.ObligationTypeTag.Multiply) {
|
|
411
|
+
// multiply
|
|
412
|
+
obligationType = new utils_1.MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
413
|
+
}
|
|
414
|
+
else if (obligationTypeTagOverride == utils_1.ObligationTypeTag.Leverage) {
|
|
415
|
+
// leverage
|
|
416
|
+
obligationType = new utils_1.LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
417
|
+
}
|
|
418
|
+
else {
|
|
419
|
+
throw Error(`Obligation type tag ${obligationTypeTagOverride} not supported for leverage, please use multiply (1) or leverage (3) obligation type`);
|
|
420
|
+
}
|
|
421
|
+
// 1. Calculate coll_amount and debt_amount to repay such that we maintain leverage and we withdraw to
|
|
422
|
+
// the wallet `amountInDepositTokenToWithdrawToWallet` amount of collateral token
|
|
423
|
+
// We need to withdraw withdrawAmountInDepositToken coll tokens
|
|
424
|
+
// and repay repayAmountInBorrowToken debt tokens
|
|
425
|
+
// TODO marius: do the same in useDeposit
|
|
426
|
+
const { adjustDepositPosition: withdrawAmount, adjustBorrowPosition: initialRepayAmount } = isClosingPosition
|
|
427
|
+
? { adjustDepositPosition: deposited, adjustBorrowPosition: borrowed }
|
|
428
|
+
: (0, calcs_1.calcWithdrawAmounts)({
|
|
429
|
+
collTokenMint: collTokenMint,
|
|
430
|
+
priceCollToDebt: new decimal_js_1.default(priceCollToDebt),
|
|
431
|
+
currentDepositPosition: deposited,
|
|
432
|
+
currentBorrowPosition: borrowed,
|
|
433
|
+
withdrawAmount: new decimal_js_1.default(amount),
|
|
434
|
+
selectedTokenMint: selectedTokenMint,
|
|
435
|
+
});
|
|
436
|
+
// Add slippage for the accrued interest rate amount
|
|
437
|
+
const userObligation = obligation
|
|
438
|
+
? obligation
|
|
439
|
+
: yield kaminoMarket.getObligationByAddress(obligationType.toPda(kaminoMarket.getAddress(), user));
|
|
440
|
+
const irSlippageBpsForDebt = userObligation
|
|
441
|
+
.estimateObligationInterestRate(debtReserve, userObligation === null || userObligation === void 0 ? void 0 : userObligation.state.borrows[0], currentSlot)
|
|
442
|
+
.toDecimalPlaces(debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.state.liquidity.mintDecimals.toNumber(), decimal_js_1.default.ROUND_CEIL);
|
|
443
|
+
// add 0.1 to irSlippageBpsForDebt because we don't want to estimate slightly less than SC and end up not reapying enough
|
|
444
|
+
const repayAmount = initialRepayAmount
|
|
445
|
+
.mul(irSlippageBpsForDebt.add('0.1').div('10_000').add('1'))
|
|
446
|
+
.toDecimalPlaces(debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.state.liquidity.mintDecimals.toNumber(), decimal_js_1.default.ROUND_CEIL);
|
|
447
|
+
// 6. Get swap ixns
|
|
448
|
+
// 5. Get swap estimations to understand how much we need to borrow from borrow reserve
|
|
449
|
+
// prevent withdrawing more then deposited if we close position
|
|
450
|
+
const depositTokenWithdrawAmount = !isClosingPosition
|
|
451
|
+
? withdrawAmount.mul(new decimal_js_1.default(1).plus(flashLoanFee))
|
|
452
|
+
: withdrawAmount;
|
|
453
|
+
// We are swapping debt token
|
|
454
|
+
// When withdrawing coll, it means we just need to swap enough to pay for the flash borrow
|
|
455
|
+
const swapAmountIfWithdrawingColl = repayAmount
|
|
456
|
+
.mul(new decimal_js_1.default(1).plus(flashLoanFee))
|
|
457
|
+
.mul(new decimal_js_1.default(1 + slippagePct / 100))
|
|
458
|
+
.div(priceCollToDebt);
|
|
459
|
+
// When withdrawing debt, it means we need to swap just the collateral we are withdrwaing
|
|
460
|
+
// enough to cover the debt we are repaying, leaving the remaining in the wallet
|
|
461
|
+
const swapAmountIfWithdrawingDebt = withdrawAmount;
|
|
462
|
+
const collTokenSwapIn = selectedTokenIsCollToken ? swapAmountIfWithdrawingColl : swapAmountIfWithdrawingDebt;
|
|
463
|
+
const debtTokenExpectedSwapOut = collTokenSwapIn.mul(priceCollToDebt).div(new decimal_js_1.default(1 + slippagePct / 100));
|
|
464
|
+
const strategy = collIsKtoken ? yield (kamino === null || kamino === void 0 ? void 0 : kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
|
|
465
|
+
console.log('Expecting to swap', collTokenSwapIn.toString(), 'coll for', debtTokenExpectedSwapOut.toString(), 'debt');
|
|
466
|
+
// 1. Create atas & budget txns & user metadata
|
|
467
|
+
let mintsToCreateAtas = [];
|
|
468
|
+
if (collIsKtoken) {
|
|
469
|
+
const secondTokenAta = (strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenAMint.equals(debtTokenMint))
|
|
470
|
+
? strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenBMint
|
|
471
|
+
: strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenAMint;
|
|
472
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve.getCTokenMint(), secondTokenAta];
|
|
473
|
+
}
|
|
474
|
+
else {
|
|
475
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve.getCTokenMint()];
|
|
476
|
+
}
|
|
477
|
+
const { atas: [, debtTokenAta], createAtasIxns, closeAtasIxns, } = yield (0, utils_1.getAtasWithCreateIxnsIfMissing)(connection, user, mintsToCreateAtas);
|
|
478
|
+
const closeWsolAtaIxns = [];
|
|
479
|
+
if (depositTokenIsSol || debtTokenMint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
480
|
+
const wsolAta = yield (0, utils_1.getAssociatedTokenAddress)(utils_1.WRAPPED_SOL_MINT, user, false);
|
|
481
|
+
closeWsolAtaIxns.push(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, wsolAta, user, user, []));
|
|
482
|
+
}
|
|
483
|
+
closeAtasIxns.push(...closeWsolAtaIxns);
|
|
484
|
+
const budgetIxns = budgetAndPriorityFeeIxns || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
485
|
+
// TODO: marius test this with shorting leverage and with leverage looping
|
|
486
|
+
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
487
|
+
const fillWsolAtaIxns = [];
|
|
488
|
+
if (debtTokenMint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
489
|
+
const halfSolBalance = (yield connection.getBalance(user)) / web3_js_1.LAMPORTS_PER_SOL / 2;
|
|
490
|
+
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
491
|
+
fillWsolAtaIxns.push(...(0, utils_1.getDepositWsolIxns)(user, debtTokenAta, (0, classes_2.numberToLamportsDecimal)(balanceToWrap, solTokenReserve.stats.decimals).ceil()));
|
|
492
|
+
}
|
|
493
|
+
// 2. Prepare the flash borrow and flash repay amounts and ixns
|
|
494
|
+
// We borrow exactly how much we need to repay
|
|
495
|
+
// and repay that + flash amount fee
|
|
496
|
+
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
497
|
+
borrowIxnIndex: budgetIxns.length + createAtasIxns.length + fillWsolAtaIxns.length,
|
|
498
|
+
walletPublicKey: user,
|
|
499
|
+
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
500
|
+
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
501
|
+
reserve: debtReserve,
|
|
502
|
+
amountLamports: (0, classes_2.numberToLamportsDecimal)(repayAmount, debtReserve.stats.decimals),
|
|
503
|
+
destinationAta: debtTokenAta,
|
|
504
|
+
referrerAccount: kaminoMarket.programId,
|
|
505
|
+
referrerTokenState: kaminoMarket.programId,
|
|
506
|
+
programId: kaminoMarket.programId,
|
|
507
|
+
});
|
|
508
|
+
// 6. Repay borrowed tokens and Withdraw tokens from reserve that will be swapped to repay flash loan
|
|
509
|
+
const repayAndWithdrawAction = yield classes_1.KaminoAction.buildRepayAndWithdrawTxns(kaminoMarket, isClosingPosition ? utils_1.U64_MAX : (0, classes_2.numberToLamportsDecimal)(repayAmount, debtReserve.stats.decimals).floor().toString(), debtTokenMint, isClosingPosition ? utils_1.U64_MAX : (0, classes_2.numberToLamportsDecimal)(depositTokenWithdrawAmount, collReserve.stats.decimals).ceil().toString(), collTokenMint, user, currentSlot, userObligation ? userObligation : obligationType, 0, false, false, 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)
|
|
510
|
+
isClosingPosition, referrer);
|
|
511
|
+
const klendIxns = [
|
|
512
|
+
...budgetIxns,
|
|
513
|
+
...createAtasIxns,
|
|
514
|
+
...fillWsolAtaIxns,
|
|
515
|
+
...[flashBorrowIxn],
|
|
516
|
+
...repayAndWithdrawAction.setupIxs,
|
|
517
|
+
...[repayAndWithdrawAction.lendingIxs[0]],
|
|
518
|
+
...repayAndWithdrawAction.inBetweenIxs,
|
|
519
|
+
...[repayAndWithdrawAction.lendingIxs[1]],
|
|
520
|
+
...repayAndWithdrawAction.cleanupIxs,
|
|
521
|
+
...[flashRepayIxn],
|
|
522
|
+
...closeAtasIxns,
|
|
523
|
+
];
|
|
524
|
+
const uniqueAccounts = new utils_1.PublicKeySet([]);
|
|
525
|
+
klendIxns.forEach((ixn) => {
|
|
526
|
+
ixn.keys.forEach((key) => {
|
|
527
|
+
uniqueAccounts.add(key.pubkey);
|
|
528
|
+
});
|
|
529
|
+
});
|
|
530
|
+
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
531
|
+
// return early to avoid extra swapper calls
|
|
532
|
+
if (getTotalKlendAccountsOnly) {
|
|
533
|
+
return {
|
|
534
|
+
ixns: [],
|
|
535
|
+
lookupTablesAddresses: [],
|
|
536
|
+
swapInputs: { inputAmountLamports: 0, inputMint: web3_js_1.PublicKey.default, outputMint: web3_js_1.PublicKey.default },
|
|
537
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
538
|
+
};
|
|
539
|
+
}
|
|
540
|
+
let withdrawSwapper;
|
|
541
|
+
if (collIsKtoken) {
|
|
542
|
+
if (kamino === undefined) {
|
|
543
|
+
throw Error('Ktoken use as collateral for leverage without Kamino instance');
|
|
544
|
+
}
|
|
545
|
+
withdrawSwapper = yield (0, utils_2.getKtokenToTokenSwapper)(kaminoMarket, kamino, user, swapper);
|
|
546
|
+
}
|
|
547
|
+
else {
|
|
548
|
+
withdrawSwapper = swapper;
|
|
549
|
+
}
|
|
550
|
+
const swapInputs = {
|
|
551
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(collTokenSwapIn, collReserve.stats.decimals).ceil().toNumber(),
|
|
552
|
+
inputMint: collTokenMint,
|
|
553
|
+
outputMint: debtTokenMint,
|
|
554
|
+
};
|
|
555
|
+
const [swapIxns, lookupTablesAddresses] = yield withdrawSwapper(swapInputs.inputAmountLamports, swapInputs.inputMint, swapInputs.outputMint, slippagePct);
|
|
556
|
+
// TODO MARIUS: remove first instruction that is setBudget ixn
|
|
557
|
+
if (collIsKtoken) {
|
|
558
|
+
if (strategy === null || strategy === void 0 ? void 0 : strategy.strategy.strategyLookupTable) {
|
|
559
|
+
lookupTablesAddresses.push(strategy === null || strategy === void 0 ? void 0 : strategy.strategy.strategyLookupTable);
|
|
560
|
+
}
|
|
561
|
+
else {
|
|
562
|
+
console.log('Strategy lookup table not found');
|
|
563
|
+
}
|
|
564
|
+
}
|
|
565
|
+
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapIxns, []);
|
|
566
|
+
const ixns = [
|
|
567
|
+
...budgetIxns,
|
|
568
|
+
...createAtasIxns,
|
|
569
|
+
...fillWsolAtaIxns,
|
|
570
|
+
...[flashBorrowIxn],
|
|
571
|
+
...repayAndWithdrawAction.setupIxs,
|
|
572
|
+
...[repayAndWithdrawAction.lendingIxs[0]],
|
|
573
|
+
...repayAndWithdrawAction.inBetweenIxs,
|
|
574
|
+
...[repayAndWithdrawAction.lendingIxs[1]],
|
|
575
|
+
...repayAndWithdrawAction.cleanupIxs,
|
|
576
|
+
...swapInstructions,
|
|
577
|
+
...[flashRepayIxn],
|
|
578
|
+
...closeAtasIxns,
|
|
579
|
+
];
|
|
580
|
+
// Send ixns and lookup tables
|
|
581
|
+
return {
|
|
582
|
+
ixns,
|
|
583
|
+
lookupTablesAddresses,
|
|
584
|
+
swapInputs,
|
|
585
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
586
|
+
};
|
|
587
|
+
});
|
|
588
|
+
exports.getWithdrawWithLeverageIxns = getWithdrawWithLeverageIxns;
|
|
589
|
+
const getAdjustLeverageSwapInputs = (props) => {
|
|
590
|
+
const { deposited, borrowed, priceCollToDebt, priceDebtToColl, slippagePct, targetLeverage, kaminoMarket, debtTokenMint, collTokenMint, } = props;
|
|
591
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
592
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
593
|
+
const flashLoanFee = (debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
594
|
+
const { adjustDepositPosition, adjustBorrowPosition } = (0, calcs_1.calcAdjustAmounts)({
|
|
595
|
+
currentDepositPosition: deposited,
|
|
596
|
+
currentBorrowPosition: borrowed,
|
|
597
|
+
targetLeverage: targetLeverage,
|
|
598
|
+
priceCollToDebt: priceCollToDebt,
|
|
599
|
+
flashLoanFee: new decimal_js_1.default(flashLoanFee),
|
|
600
|
+
});
|
|
601
|
+
const isDeposit = adjustDepositPosition.gte(0) && adjustBorrowPosition.gte(0);
|
|
602
|
+
if (isDeposit) {
|
|
603
|
+
const borrowAmount = adjustDepositPosition
|
|
604
|
+
.mul(new decimal_js_1.default(1).plus(flashLoanFee))
|
|
605
|
+
.mul(new decimal_js_1.default(1 + slippagePct / 100))
|
|
606
|
+
.div(priceDebtToColl);
|
|
607
|
+
return {
|
|
608
|
+
swapInputs: {
|
|
609
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(borrowAmount, debtReserve.state.liquidity.mintDecimals.toNumber())
|
|
610
|
+
.ceil()
|
|
611
|
+
.toNumber(),
|
|
612
|
+
inputMint: debtTokenMint,
|
|
613
|
+
outputMint: collTokenMint,
|
|
614
|
+
},
|
|
615
|
+
};
|
|
616
|
+
}
|
|
617
|
+
else {
|
|
618
|
+
const withdrawAmountWithSlippageAndFlashLoanFee = decimal_js_1.default.abs(adjustDepositPosition)
|
|
619
|
+
.mul(new decimal_js_1.default(1).plus(flashLoanFee))
|
|
620
|
+
.mul(1 + slippagePct / 100);
|
|
621
|
+
return {
|
|
622
|
+
swapInputs: {
|
|
623
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(withdrawAmountWithSlippageAndFlashLoanFee, collReserve.state.liquidity.mintDecimals.toNumber())
|
|
624
|
+
.ceil()
|
|
625
|
+
.toNumber(),
|
|
626
|
+
inputMint: collTokenMint,
|
|
627
|
+
outputMint: debtTokenMint,
|
|
628
|
+
},
|
|
629
|
+
};
|
|
630
|
+
}
|
|
631
|
+
};
|
|
632
|
+
exports.getAdjustLeverageSwapInputs = getAdjustLeverageSwapInputs;
|
|
633
|
+
const getAdjustLeverageIxns = (props) => __awaiter(void 0, void 0, void 0, function* () {
|
|
634
|
+
const { connection, budgetAndPriorityFeeIxns, user, kaminoMarket, priceDebtToColl, priceCollToDebt, targetLeverage, slippagePct, depositedLamports, borrowedLamports, collTokenMint, debtTokenMint, swapper, referrer, isKtoken, priceAinB, kamino, obligationTypeTagOverride, obligation, currentSlot, getTotalKlendAccountsOnly, } = props;
|
|
635
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
636
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
637
|
+
const deposited = (0, classes_1.lamportsToNumberDecimal)(depositedLamports, collReserve.stats.decimals);
|
|
638
|
+
const borrowed = (0, classes_1.lamportsToNumberDecimal)(borrowedLamports, debtReserve.stats.decimals);
|
|
639
|
+
const userObligation = obligation
|
|
640
|
+
? obligation
|
|
641
|
+
: (yield kaminoMarket.getUserObligationsByTag(obligationTypeTagOverride, user)).filter((obligation) => obligation.getBorrowByMint(debtReserve.getLiquidityMint()) !== undefined &&
|
|
642
|
+
obligation.getDepositByMint(collReserve.getLiquidityMint()) !== undefined)[0];
|
|
643
|
+
const currentLeverage = userObligation.refreshedStats.leverage;
|
|
644
|
+
const isDepositViaLeverage = targetLeverage.gte(new decimal_js_1.default(currentLeverage));
|
|
645
|
+
let flashLoanFee = new decimal_js_1.default(0);
|
|
646
|
+
if (isDepositViaLeverage) {
|
|
647
|
+
flashLoanFee = (collReserve === null || collReserve === void 0 ? void 0 : collReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
648
|
+
}
|
|
649
|
+
else {
|
|
650
|
+
flashLoanFee = (debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
651
|
+
}
|
|
652
|
+
const { adjustDepositPosition, adjustBorrowPosition } = (0, calcs_1.calcAdjustAmounts)({
|
|
653
|
+
currentDepositPosition: deposited,
|
|
654
|
+
currentBorrowPosition: borrowed,
|
|
655
|
+
targetLeverage: targetLeverage,
|
|
656
|
+
priceCollToDebt: priceCollToDebt,
|
|
657
|
+
flashLoanFee: new decimal_js_1.default(flashLoanFee),
|
|
658
|
+
});
|
|
659
|
+
let ixns = [];
|
|
660
|
+
let lookupTablesAddresses = [];
|
|
661
|
+
let swapInputs;
|
|
662
|
+
let totalKlendAccounts = 0;
|
|
663
|
+
const isDeposit = adjustDepositPosition.gte(0) && adjustBorrowPosition.gte(0);
|
|
664
|
+
if (isDepositViaLeverage !== isDeposit) {
|
|
665
|
+
throw new Error('Invalid target leverage');
|
|
666
|
+
}
|
|
667
|
+
// leverage increased so we need to deposit and borrow more
|
|
668
|
+
if (isDeposit) {
|
|
669
|
+
console.log('Increasing leaverage');
|
|
670
|
+
// TODO: marius why are we not using both adjustDepositPosition & adjustBorrowPosition
|
|
671
|
+
const res = yield (0, exports.getIncreaseLeverageIxns)({
|
|
672
|
+
connection,
|
|
673
|
+
budgetAndPriorityFeeIxns,
|
|
674
|
+
user,
|
|
675
|
+
kaminoMarket,
|
|
676
|
+
depositAmount: adjustDepositPosition,
|
|
677
|
+
collTokenMint,
|
|
678
|
+
debtTokenMint,
|
|
679
|
+
slippagePct,
|
|
680
|
+
priceDebtToColl,
|
|
681
|
+
priceCollToDebt,
|
|
682
|
+
swapper,
|
|
683
|
+
referrer,
|
|
684
|
+
isKtoken,
|
|
685
|
+
priceAinB,
|
|
686
|
+
kamino,
|
|
687
|
+
obligationTypeTagOverride,
|
|
688
|
+
obligation: userObligation,
|
|
689
|
+
currentSlot,
|
|
690
|
+
getTotalKlendAccountsOnly,
|
|
691
|
+
});
|
|
692
|
+
ixns = res.ixns;
|
|
693
|
+
lookupTablesAddresses = res.lookupTablesAddresses;
|
|
694
|
+
swapInputs = res.swapInputs;
|
|
695
|
+
totalKlendAccounts = res.totalKlendAccounts;
|
|
696
|
+
}
|
|
697
|
+
else {
|
|
698
|
+
console.log('Decreasing leverage');
|
|
699
|
+
const res = yield (0, exports.getDecreaseLeverageIxns)({
|
|
700
|
+
connection,
|
|
701
|
+
budgetAndPriorityFeeIxns,
|
|
702
|
+
user,
|
|
703
|
+
kaminoMarket,
|
|
704
|
+
withdrawAmount: decimal_js_1.default.abs(adjustDepositPosition),
|
|
705
|
+
repayAmount: decimal_js_1.default.abs(adjustBorrowPosition),
|
|
706
|
+
collTokenMint,
|
|
707
|
+
debtTokenMint,
|
|
708
|
+
slippagePct,
|
|
709
|
+
swapper,
|
|
710
|
+
referrer,
|
|
711
|
+
isKtoken,
|
|
712
|
+
kamino,
|
|
713
|
+
obligationTypeTagOverride,
|
|
714
|
+
obligation: userObligation,
|
|
715
|
+
currentSlot,
|
|
716
|
+
getTotalKlendAccountsOnly,
|
|
717
|
+
});
|
|
718
|
+
ixns = res.ixns;
|
|
719
|
+
lookupTablesAddresses = res.lookupTablesAddresses;
|
|
720
|
+
swapInputs = res.swapInputs;
|
|
721
|
+
totalKlendAccounts = res.totalKlendAccounts;
|
|
722
|
+
}
|
|
723
|
+
return {
|
|
724
|
+
ixns,
|
|
725
|
+
lookupTablesAddresses,
|
|
726
|
+
swapInputs,
|
|
727
|
+
totalKlendAccounts,
|
|
728
|
+
};
|
|
729
|
+
});
|
|
730
|
+
exports.getAdjustLeverageIxns = getAdjustLeverageIxns;
|
|
731
|
+
/**
|
|
732
|
+
* Deposit and borrow tokens if leverage increased
|
|
733
|
+
*/
|
|
734
|
+
const getIncreaseLeverageIxns = (props) => __awaiter(void 0, void 0, void 0, function* () {
|
|
735
|
+
const { connection, budgetAndPriorityFeeIxns, user, kaminoMarket, depositAmount, collTokenMint, debtTokenMint, slippagePct, priceDebtToColl, priceCollToDebt, swapper, referrer, isKtoken, priceAinB, kamino, obligationTypeTagOverride = 1, obligation, currentSlot, getTotalKlendAccountsOnly, } = props;
|
|
736
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
737
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
738
|
+
const collIsKtoken = yield isKtoken(collTokenMint);
|
|
739
|
+
const flashLoanFee = (collReserve === null || collReserve === void 0 ? void 0 : collReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
740
|
+
if (!priceDebtToColl || !priceCollToDebt) {
|
|
741
|
+
throw new Error('Price is not loaded. Please, reload the page and try again');
|
|
742
|
+
}
|
|
743
|
+
// TODO: why are we recalculating here again
|
|
744
|
+
const strategy = collIsKtoken ? yield (kamino === null || kamino === void 0 ? void 0 : kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
|
|
745
|
+
// 1. Create atas & budget txns
|
|
746
|
+
const budgetIxns = budgetAndPriorityFeeIxns || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
747
|
+
let mintsToCreateAtas = [];
|
|
748
|
+
if (collIsKtoken) {
|
|
749
|
+
const secondTokenAta = (strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenAMint.equals(debtTokenMint))
|
|
750
|
+
? strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenBMint
|
|
751
|
+
: strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenAMint;
|
|
752
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve.getCTokenMint(), secondTokenAta];
|
|
753
|
+
}
|
|
754
|
+
else {
|
|
755
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve.getCTokenMint()];
|
|
756
|
+
}
|
|
757
|
+
const { atas: [collTokenAta, debtTokenAta], createAtasIxns, closeAtasIxns, } = yield (0, utils_1.getAtasWithCreateIxnsIfMissing)(connection, user, mintsToCreateAtas);
|
|
758
|
+
// 2. Create borrow flash loan instruction
|
|
759
|
+
// used if coll is Ktoken and we borrow debt token instead
|
|
760
|
+
const amountToFashBorrowDebt = depositAmount
|
|
761
|
+
.div(priceDebtToColl)
|
|
762
|
+
.mul(new decimal_js_1.default(1 + slippagePct / 100))
|
|
763
|
+
.toDecimalPlaces(debtReserve.stats.decimals, decimal_js_1.default.ROUND_UP);
|
|
764
|
+
// .toDecimalPlaces(debtReserve?.state.liquidity.mintDecimals.toNumber());
|
|
765
|
+
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
766
|
+
borrowIxnIndex: budgetIxns.length + createAtasIxns.length,
|
|
767
|
+
walletPublicKey: user,
|
|
768
|
+
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
769
|
+
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
770
|
+
reserve: !collIsKtoken ? collReserve : debtReserve,
|
|
771
|
+
amountLamports: (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? depositAmount : amountToFashBorrowDebt, !collIsKtoken ? collReserve.stats.decimals : debtReserve.stats.decimals),
|
|
772
|
+
destinationAta: !collIsKtoken ? collTokenAta : debtTokenAta,
|
|
773
|
+
referrerAccount: kaminoMarket.programId,
|
|
774
|
+
referrerTokenState: kaminoMarket.programId,
|
|
775
|
+
programId: kaminoMarket.programId,
|
|
776
|
+
});
|
|
777
|
+
// 3. Deposit initial tokens + borrowed tokens into reserve
|
|
778
|
+
let obligationType;
|
|
779
|
+
if (obligationTypeTagOverride === utils_1.ObligationTypeTag.Multiply) {
|
|
780
|
+
// multiply
|
|
781
|
+
obligationType = new utils_1.MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
782
|
+
}
|
|
783
|
+
else if (obligationTypeTagOverride === utils_1.ObligationTypeTag.Leverage) {
|
|
784
|
+
// leverage
|
|
785
|
+
obligationType = new utils_1.LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
786
|
+
}
|
|
787
|
+
else {
|
|
788
|
+
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
789
|
+
}
|
|
790
|
+
const depositAction = yield classes_1.KaminoAction.buildDepositTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(depositAmount, collReserve.stats.decimals).floor().toString(), collTokenMint, user, obligation ? obligation : obligationType, 0, false, false, 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)
|
|
791
|
+
referrer, currentSlot);
|
|
792
|
+
// 4. Get swap estimations to understand how much we need to borrow from borrow reserve
|
|
793
|
+
const borrowAmount = depositAmount
|
|
794
|
+
.mul(new decimal_js_1.default(1).plus(flashLoanFee))
|
|
795
|
+
.mul(new decimal_js_1.default(1 + slippagePct / 100))
|
|
796
|
+
.div(priceDebtToColl);
|
|
797
|
+
const _collTokenExpectedSwapOut = depositAmount.mul(new decimal_js_1.default(1).plus(flashLoanFee));
|
|
798
|
+
// 5. Borrow tokens in borrow token reserve that will be swapped to repay flash loan
|
|
799
|
+
const borrowAction = yield classes_1.KaminoAction.buildBorrowTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(borrowAmount, debtReserve.stats.decimals).ceil().toString(), debtTokenMint, user, obligation ? obligation : obligationType, 0, false, false, 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)
|
|
800
|
+
referrer, currentSlot, debtTokenAta);
|
|
801
|
+
const klendIxns = [
|
|
802
|
+
...budgetIxns,
|
|
803
|
+
...createAtasIxns,
|
|
804
|
+
...[flashBorrowIxn],
|
|
805
|
+
...depositAction.setupIxs,
|
|
806
|
+
...depositAction.lendingIxs,
|
|
807
|
+
...depositAction.cleanupIxs,
|
|
808
|
+
...borrowAction.setupIxs,
|
|
809
|
+
...borrowAction.lendingIxs,
|
|
810
|
+
...borrowAction.cleanupIxs,
|
|
811
|
+
...[flashRepayIxn],
|
|
812
|
+
...closeAtasIxns,
|
|
813
|
+
];
|
|
814
|
+
const uniqueAccounts = new utils_1.PublicKeySet([]);
|
|
815
|
+
klendIxns.forEach((ixn) => {
|
|
816
|
+
ixn.keys.forEach((key) => {
|
|
817
|
+
uniqueAccounts.add(key.pubkey);
|
|
818
|
+
});
|
|
819
|
+
});
|
|
820
|
+
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
821
|
+
// return early to avoid extra swapper calls
|
|
822
|
+
if (getTotalKlendAccountsOnly) {
|
|
823
|
+
return {
|
|
824
|
+
ixns: [],
|
|
825
|
+
lookupTablesAddresses: [],
|
|
826
|
+
swapInputs: { inputAmountLamports: 0, inputMint: web3_js_1.PublicKey.default, outputMint: web3_js_1.PublicKey.default },
|
|
827
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
828
|
+
};
|
|
829
|
+
}
|
|
830
|
+
let depositSwapper;
|
|
831
|
+
let expectedDebtTokenAtaBalance = new decimal_js_1.default(0);
|
|
832
|
+
if (collIsKtoken) {
|
|
833
|
+
if (kamino === undefined) {
|
|
834
|
+
throw Error('Ktoken use as collateral for leverage without Kamino instance');
|
|
835
|
+
}
|
|
836
|
+
depositSwapper = yield (0, utils_2.getTokenToKtokenSwapper)(connection, kaminoMarket, kamino, user, swapper, priceAinB, false);
|
|
837
|
+
expectedDebtTokenAtaBalance = yield (0, utils_2.getExpectedTokenBalanceAfterBorrow)(connection, debtTokenMint, user, (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? borrowAmount : amountToFashBorrowDebt, debtReserve.stats.decimals).floor(), debtReserve.state.liquidity.mintDecimals.toNumber());
|
|
838
|
+
}
|
|
839
|
+
else {
|
|
840
|
+
depositSwapper = swapper;
|
|
841
|
+
}
|
|
842
|
+
const swapInputs = {
|
|
843
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(!collIsKtoken ? borrowAmount : amountToFashBorrowDebt, debtReserve.stats.decimals)
|
|
844
|
+
.ceil()
|
|
845
|
+
.toNumber(),
|
|
846
|
+
inputMint: debtTokenMint,
|
|
847
|
+
outputMint: collTokenMint,
|
|
848
|
+
};
|
|
849
|
+
const [swapIxns, lookupTablesAddresses] = yield depositSwapper(swapInputs.inputAmountLamports, swapInputs.inputMint, swapInputs.outputMint, slippagePct, expectedDebtTokenAtaBalance);
|
|
850
|
+
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapIxns, []);
|
|
851
|
+
const ixns = !collIsKtoken
|
|
852
|
+
? [
|
|
853
|
+
...budgetIxns,
|
|
854
|
+
...createAtasIxns,
|
|
855
|
+
...[flashBorrowIxn],
|
|
856
|
+
...depositAction.setupIxs,
|
|
857
|
+
...depositAction.lendingIxs,
|
|
858
|
+
...depositAction.cleanupIxs,
|
|
859
|
+
...borrowAction.setupIxs,
|
|
860
|
+
...borrowAction.lendingIxs,
|
|
861
|
+
...borrowAction.cleanupIxs,
|
|
862
|
+
...swapInstructions,
|
|
863
|
+
...[flashRepayIxn],
|
|
864
|
+
...closeAtasIxns,
|
|
865
|
+
]
|
|
866
|
+
: [
|
|
867
|
+
...budgetIxns,
|
|
868
|
+
...createAtasIxns,
|
|
869
|
+
...[flashBorrowIxn],
|
|
870
|
+
...swapInstructions,
|
|
871
|
+
...depositAction.setupIxs,
|
|
872
|
+
...depositAction.lendingIxs,
|
|
873
|
+
...depositAction.cleanupIxs,
|
|
874
|
+
...borrowAction.setupIxs,
|
|
875
|
+
...borrowAction.lendingIxs,
|
|
876
|
+
...borrowAction.cleanupIxs,
|
|
877
|
+
...[flashRepayIxn],
|
|
878
|
+
...closeAtasIxns,
|
|
879
|
+
];
|
|
880
|
+
ixns.forEach((ixn, i) => {
|
|
881
|
+
console.log(`ixn ${i + 1}: ${ixn.programId.toString()}`);
|
|
882
|
+
});
|
|
883
|
+
// Create and send transaction
|
|
884
|
+
if (collIsKtoken) {
|
|
885
|
+
if (strategy === null || strategy === void 0 ? void 0 : strategy.strategy.strategyLookupTable) {
|
|
886
|
+
lookupTablesAddresses.push(strategy === null || strategy === void 0 ? void 0 : strategy.strategy.strategyLookupTable);
|
|
887
|
+
}
|
|
888
|
+
else {
|
|
889
|
+
console.log('Strategy lookup table not found');
|
|
890
|
+
}
|
|
891
|
+
}
|
|
892
|
+
return {
|
|
893
|
+
ixns,
|
|
894
|
+
lookupTablesAddresses,
|
|
895
|
+
swapInputs,
|
|
896
|
+
totalKlendAccounts,
|
|
897
|
+
};
|
|
898
|
+
});
|
|
899
|
+
exports.getIncreaseLeverageIxns = getIncreaseLeverageIxns;
|
|
900
|
+
/**
|
|
901
|
+
* Withdraw and repay tokens if leverage decreased
|
|
902
|
+
*/
|
|
903
|
+
const getDecreaseLeverageIxns = (props) => __awaiter(void 0, void 0, void 0, function* () {
|
|
904
|
+
const { connection, budgetAndPriorityFeeIxns, user, kaminoMarket, withdrawAmount, repayAmount, collTokenMint, debtTokenMint, slippagePct, swapper, referrer, isKtoken, kamino, obligationTypeTagOverride = 1, obligation, currentSlot, getTotalKlendAccountsOnly, } = props;
|
|
905
|
+
console.log('getDecreaseLeverageIxns', (0, calcs_1.toJson)({ withdrawAmount, repayAmount, collTokenMint, debtTokenMint, slippagePct }));
|
|
906
|
+
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
907
|
+
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
908
|
+
const collIsKtoken = yield isKtoken(collTokenMint);
|
|
909
|
+
const flashLoanFee = (debtReserve === null || debtReserve === void 0 ? void 0 : debtReserve.getFlashLoanFee()) || new decimal_js_1.default(0);
|
|
910
|
+
const strategy = collIsKtoken ? yield (kamino === null || kamino === void 0 ? void 0 : kamino.getStrategyByKTokenMint(collTokenMint)) : undefined;
|
|
911
|
+
// 1. Create atas & budget txns
|
|
912
|
+
const budgetIxns = budgetAndPriorityFeeIxns || (0, utils_1.getComputeBudgetAndPriorityFeeIxns)(3000000);
|
|
913
|
+
let mintsToCreateAtas = [];
|
|
914
|
+
if (collIsKtoken) {
|
|
915
|
+
const secondTokenAta = (strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenAMint.equals(debtTokenMint))
|
|
916
|
+
? strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenBMint
|
|
917
|
+
: strategy === null || strategy === void 0 ? void 0 : strategy.strategy.tokenAMint;
|
|
918
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve.getCTokenMint(), secondTokenAta];
|
|
919
|
+
}
|
|
920
|
+
else {
|
|
921
|
+
mintsToCreateAtas = [collTokenMint, debtTokenMint, collReserve.getCTokenMint()];
|
|
922
|
+
}
|
|
923
|
+
const { atas: [, debtTokenAta], createAtasIxns, closeAtasIxns, } = yield (0, utils_1.getAtasWithCreateIxnsIfMissing)(connection, user, mintsToCreateAtas);
|
|
924
|
+
// TODO: Mihai/Marius check if we can improve this logic and not convert any SOL
|
|
925
|
+
// This is here so that we have enough wsol to repay in case the kAB swapped to sol after estimates is not enough
|
|
926
|
+
const closeWsolAtaIxns = [];
|
|
927
|
+
if (debtTokenMint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
928
|
+
const wsolAta = yield (0, utils_1.getAssociatedTokenAddress)(utils_1.WRAPPED_SOL_MINT, user, false);
|
|
929
|
+
closeWsolAtaIxns.push(spl_token_1.Token.createCloseAccountInstruction(spl_token_1.TOKEN_PROGRAM_ID, wsolAta, user, user, []));
|
|
930
|
+
}
|
|
931
|
+
closeAtasIxns.push(...closeWsolAtaIxns);
|
|
932
|
+
const fillWsolAtaIxns = [];
|
|
933
|
+
if (debtTokenMint.equals(utils_1.WRAPPED_SOL_MINT)) {
|
|
934
|
+
const halfSolBalance = (yield connection.getBalance(user)) / web3_js_1.LAMPORTS_PER_SOL / 2;
|
|
935
|
+
const balanceToWrap = halfSolBalance < 0.1 ? halfSolBalance : 0.1;
|
|
936
|
+
fillWsolAtaIxns.push(...(0, utils_1.getDepositWsolIxns)(user, debtTokenAta, (0, classes_2.numberToLamportsDecimal)(balanceToWrap, debtReserve.stats.decimals).ceil()));
|
|
937
|
+
}
|
|
938
|
+
// 3. Flash borrow & repay amount to repay (debt)
|
|
939
|
+
const { flashBorrowIxn, flashRepayIxn } = (0, instructions_1.getFlashLoanInstructions)({
|
|
940
|
+
borrowIxnIndex: budgetIxns.length + createAtasIxns.length + fillWsolAtaIxns.length,
|
|
941
|
+
walletPublicKey: user,
|
|
942
|
+
lendingMarketAuthority: kaminoMarket.getLendingMarketAuthority(),
|
|
943
|
+
lendingMarketAddress: kaminoMarket.getAddress(),
|
|
944
|
+
reserve: debtReserve,
|
|
945
|
+
amountLamports: (0, classes_2.numberToLamportsDecimal)(repayAmount, debtReserve.stats.decimals),
|
|
946
|
+
destinationAta: debtTokenAta,
|
|
947
|
+
referrerAccount: kaminoMarket.programId,
|
|
948
|
+
referrerTokenState: kaminoMarket.programId,
|
|
949
|
+
programId: kaminoMarket.programId,
|
|
950
|
+
});
|
|
951
|
+
// 4. Actually do the repay of the flash borrowed amounts
|
|
952
|
+
let obligationType;
|
|
953
|
+
if (obligationTypeTagOverride === utils_1.ObligationTypeTag.Multiply) {
|
|
954
|
+
// multiply
|
|
955
|
+
obligationType = new utils_1.MultiplyObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
956
|
+
}
|
|
957
|
+
else if (obligationTypeTagOverride === utils_1.ObligationTypeTag.Leverage) {
|
|
958
|
+
// leverage
|
|
959
|
+
obligationType = new utils_1.LeverageObligation(collTokenMint, debtTokenMint, kaminoMarket.programId);
|
|
960
|
+
}
|
|
961
|
+
else {
|
|
962
|
+
throw Error('Obligation type tag not supported for leverage, please use 1 - multiply or 3 - leverage');
|
|
963
|
+
}
|
|
964
|
+
const repayAction = yield classes_1.KaminoAction.buildRepayTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(repayAmount, debtReserve.stats.decimals).floor().toString(), debtTokenMint, user, obligation ? obligation : obligationType, currentSlot, undefined, 0, false, false, 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)
|
|
965
|
+
referrer);
|
|
966
|
+
// 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
|
|
967
|
+
const withdrawAmountWithSlippageAndFlashLoanFee = withdrawAmount
|
|
968
|
+
.mul(new decimal_js_1.default(1).plus(flashLoanFee))
|
|
969
|
+
.mul(1 + slippagePct / 100);
|
|
970
|
+
const _debtTokenExpectedSwapOut = repayAmount.mul(new decimal_js_1.default(1).plus(flashLoanFee));
|
|
971
|
+
const withdrawAction = yield classes_1.KaminoAction.buildWithdrawTxns(kaminoMarket, (0, classes_2.numberToLamportsDecimal)(withdrawAmountWithSlippageAndFlashLoanFee, collReserve.stats.decimals).ceil().toString(), collTokenMint, user, obligation ? obligation : obligationType, 0, false, false, 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)
|
|
972
|
+
referrer, currentSlot);
|
|
973
|
+
const klendIxns = [
|
|
974
|
+
...budgetIxns,
|
|
975
|
+
...createAtasIxns,
|
|
976
|
+
...fillWsolAtaIxns,
|
|
977
|
+
...[flashBorrowIxn],
|
|
978
|
+
...repayAction.setupIxs,
|
|
979
|
+
...repayAction.lendingIxs,
|
|
980
|
+
...repayAction.cleanupIxs,
|
|
981
|
+
...withdrawAction.setupIxs,
|
|
982
|
+
...withdrawAction.lendingIxs,
|
|
983
|
+
...withdrawAction.cleanupIxs,
|
|
984
|
+
...[flashRepayIxn],
|
|
985
|
+
...closeAtasIxns,
|
|
986
|
+
];
|
|
987
|
+
const uniqueAccounts = new utils_1.PublicKeySet([]);
|
|
988
|
+
klendIxns.forEach((ixn) => {
|
|
989
|
+
ixn.keys.forEach((key) => {
|
|
990
|
+
uniqueAccounts.add(key.pubkey);
|
|
991
|
+
});
|
|
992
|
+
});
|
|
993
|
+
const totalKlendAccounts = uniqueAccounts.toArray().length;
|
|
994
|
+
// return early to avoid extra swapper calls
|
|
995
|
+
if (getTotalKlendAccountsOnly) {
|
|
996
|
+
return {
|
|
997
|
+
ixns: [],
|
|
998
|
+
lookupTablesAddresses: [],
|
|
999
|
+
swapInputs: { inputAmountLamports: 0, inputMint: web3_js_1.PublicKey.default, outputMint: web3_js_1.PublicKey.default },
|
|
1000
|
+
totalKlendAccounts: totalKlendAccounts,
|
|
1001
|
+
};
|
|
1002
|
+
}
|
|
1003
|
+
let withdrawSwapper;
|
|
1004
|
+
if (collIsKtoken) {
|
|
1005
|
+
if (kamino === undefined) {
|
|
1006
|
+
throw Error('Ktoken use as collateral for leverage without Kamino instance');
|
|
1007
|
+
}
|
|
1008
|
+
withdrawSwapper = yield (0, utils_2.getKtokenToTokenSwapper)(kaminoMarket, kamino, user, swapper);
|
|
1009
|
+
}
|
|
1010
|
+
else {
|
|
1011
|
+
withdrawSwapper = swapper;
|
|
1012
|
+
}
|
|
1013
|
+
const swapInputs = {
|
|
1014
|
+
inputAmountLamports: (0, classes_2.numberToLamportsDecimal)(withdrawAmountWithSlippageAndFlashLoanFee, collReserve.stats.decimals)
|
|
1015
|
+
.ceil()
|
|
1016
|
+
.toNumber(),
|
|
1017
|
+
inputMint: collTokenMint,
|
|
1018
|
+
outputMint: debtTokenMint,
|
|
1019
|
+
};
|
|
1020
|
+
// 5. Get swap ixns
|
|
1021
|
+
const [swapIxns, lookupTablesAddresses] = yield withdrawSwapper(swapInputs.inputAmountLamports, swapInputs.inputMint, swapInputs.outputMint, slippagePct);
|
|
1022
|
+
const swapInstructions = (0, utils_1.removeBudgetAndAtaIxns)(swapIxns, []);
|
|
1023
|
+
const ixns = [
|
|
1024
|
+
...budgetIxns,
|
|
1025
|
+
...createAtasIxns,
|
|
1026
|
+
...fillWsolAtaIxns,
|
|
1027
|
+
...[flashBorrowIxn],
|
|
1028
|
+
...repayAction.setupIxs,
|
|
1029
|
+
...repayAction.lendingIxs,
|
|
1030
|
+
...repayAction.cleanupIxs,
|
|
1031
|
+
...withdrawAction.setupIxs,
|
|
1032
|
+
...withdrawAction.lendingIxs,
|
|
1033
|
+
...withdrawAction.cleanupIxs,
|
|
1034
|
+
...swapInstructions,
|
|
1035
|
+
...[flashRepayIxn],
|
|
1036
|
+
...closeAtasIxns,
|
|
1037
|
+
];
|
|
1038
|
+
ixns.forEach((ixn, i) => {
|
|
1039
|
+
console.log(`ixn ${i + 1}: ${ixn.programId.toString()}`);
|
|
1040
|
+
});
|
|
1041
|
+
if (collIsKtoken) {
|
|
1042
|
+
if (strategy === null || strategy === void 0 ? void 0 : strategy.strategy.strategyLookupTable) {
|
|
1043
|
+
lookupTablesAddresses.push(strategy === null || strategy === void 0 ? void 0 : strategy.strategy.strategyLookupTable);
|
|
1044
|
+
}
|
|
1045
|
+
else {
|
|
1046
|
+
console.log('Strategy lookup table not found');
|
|
1047
|
+
}
|
|
1048
|
+
}
|
|
1049
|
+
// Create and send transaction
|
|
1050
|
+
return {
|
|
1051
|
+
ixns,
|
|
1052
|
+
lookupTablesAddresses,
|
|
1053
|
+
swapInputs,
|
|
1054
|
+
totalKlendAccounts,
|
|
1055
|
+
};
|
|
1056
|
+
});
|
|
1057
|
+
exports.getDecreaseLeverageIxns = getDecreaseLeverageIxns;
|
|
1058
|
+
//# sourceMappingURL=operations.js.map
|