@kamino-finance/klend-sdk 7.0.7 → 7.0.9
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 -1
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +15 -15
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/obligation.js +7 -6
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +0 -1
- package/dist/classes/vault.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +36 -32
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +6 -6
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/operations.d.ts +4 -3
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +175 -149
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +1 -0
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/manager/client_kamino_manager.js +5 -4
- package/dist/manager/client_kamino_manager.js.map +1 -1
- package/dist/utils/farmUtils.d.ts.map +1 -1
- package/dist/utils/farmUtils.js +1 -4
- package/dist/utils/farmUtils.js.map +1 -1
- package/package.json +1 -1
- package/src/classes/action.ts +15 -15
- package/src/classes/obligation.ts +9 -9
- package/src/classes/vault.ts +0 -1
- package/src/lending_operations/repay_with_collateral_operations.ts +78 -74
- package/src/lending_operations/swap_collateral_operations.ts +15 -13
- package/src/leverage/operations.ts +350 -318
- package/src/leverage/types.ts +1 -0
- package/src/manager/client_kamino_manager.ts +5 -4
- package/src/utils/farmUtils.ts +1 -4
|
@@ -69,6 +69,8 @@ import { findAssociatedTokenPda, getCloseAccountInstruction } from '@solana-prog
|
|
|
69
69
|
import { LAMPORTS_PER_SOL } from '../utils/consts';
|
|
70
70
|
import { fetchAddressLookupTable } from '@solana-program/address-lookup-table';
|
|
71
71
|
|
|
72
|
+
export const WITHDRAW_SLOT_OFFSET = 150; // Offset for the withdraw slot to underestimate the exchange rate
|
|
73
|
+
|
|
72
74
|
export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
|
|
73
75
|
owner,
|
|
74
76
|
kaminoMarket,
|
|
@@ -129,32 +131,36 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
|
|
|
129
131
|
const obligationType = checkObligationType(obligationTypeTagOverride, collTokenMint, debtTokenMint, kaminoMarket);
|
|
130
132
|
|
|
131
133
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
132
|
-
const klendIxs: LeverageIxsOutput =
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
134
|
+
const klendIxs: LeverageIxsOutput = (
|
|
135
|
+
await buildDepositWithLeverageIxs(
|
|
136
|
+
kaminoMarket,
|
|
137
|
+
debtReserve,
|
|
138
|
+
collReserve,
|
|
139
|
+
owner,
|
|
140
|
+
obligation ? obligation : obligationType,
|
|
141
|
+
referrer,
|
|
142
|
+
currentSlot,
|
|
143
|
+
depositTokenIsSol,
|
|
144
|
+
scopeRefreshConfig,
|
|
145
|
+
calcs,
|
|
146
|
+
budgetAndPriorityFeeIxs,
|
|
147
|
+
[
|
|
148
|
+
{
|
|
149
|
+
preActionIxs: [],
|
|
150
|
+
swapIxs: [],
|
|
151
|
+
lookupTables: [],
|
|
152
|
+
quote: {
|
|
153
|
+
priceAInB: new Decimal(0), // not used
|
|
154
|
+
quoteResponse: undefined,
|
|
155
|
+
},
|
|
156
|
+
},
|
|
157
|
+
],
|
|
158
|
+
strategy,
|
|
159
|
+
collIsKtoken,
|
|
160
|
+
useV2Ixs,
|
|
161
|
+
elevationGroupOverride
|
|
162
|
+
)
|
|
163
|
+
)[0];
|
|
158
164
|
|
|
159
165
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
160
166
|
|
|
@@ -376,42 +382,42 @@ export async function getDepositWithLeverageIxs<QuoteResponse>({
|
|
|
376
382
|
const solTokenReserve = kaminoMarket.getReserveByMint(WRAPPED_SOL_MINT);
|
|
377
383
|
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint === solTokenReserve!.getLiquidityMint();
|
|
378
384
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
budgetAndPriorityFeeIxs,
|
|
393
|
-
{
|
|
394
|
-
preActionIxs: [],
|
|
395
|
-
swapIxs: swap.swapIxs,
|
|
396
|
-
lookupTables: swap.lookupTables,
|
|
397
|
-
quote: swap.quote,
|
|
398
|
-
},
|
|
399
|
-
initialInputs.strategy,
|
|
400
|
-
initialInputs.collIsKtoken,
|
|
401
|
-
useV2Ixs,
|
|
402
|
-
elevationGroupOverride
|
|
403
|
-
);
|
|
404
|
-
|
|
385
|
+
const depositWithLeverageIxs = await buildDepositWithLeverageIxs(
|
|
386
|
+
kaminoMarket,
|
|
387
|
+
debtReserve!,
|
|
388
|
+
collReserve!,
|
|
389
|
+
owner,
|
|
390
|
+
initialInputs.obligation,
|
|
391
|
+
referrer,
|
|
392
|
+
currentSlot,
|
|
393
|
+
depositTokenIsSol,
|
|
394
|
+
scopeRefreshConfig,
|
|
395
|
+
initialInputs.calcs,
|
|
396
|
+
budgetAndPriorityFeeIxs,
|
|
397
|
+
swapsArray.map((swap) => {
|
|
405
398
|
return {
|
|
406
|
-
|
|
407
|
-
|
|
399
|
+
preActionIxs: [],
|
|
400
|
+
swapIxs: swap.swapIxs,
|
|
408
401
|
lookupTables: swap.lookupTables,
|
|
409
|
-
|
|
410
|
-
initialInputs,
|
|
411
|
-
quote: swap.quote.quoteResponse,
|
|
402
|
+
quote: swap.quote,
|
|
412
403
|
};
|
|
413
|
-
})
|
|
404
|
+
}),
|
|
405
|
+
initialInputs.strategy,
|
|
406
|
+
initialInputs.collIsKtoken,
|
|
407
|
+
useV2Ixs,
|
|
408
|
+
elevationGroupOverride
|
|
414
409
|
);
|
|
410
|
+
|
|
411
|
+
return depositWithLeverageIxs.map((depositWithLeverageIxs, index) => {
|
|
412
|
+
return {
|
|
413
|
+
ixs: depositWithLeverageIxs.instructions,
|
|
414
|
+
flashLoanInfo: depositWithLeverageIxs.flashLoanInfo,
|
|
415
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
416
|
+
swapInputs,
|
|
417
|
+
initialInputs,
|
|
418
|
+
quote: swapsArray[index].quote.quoteResponse,
|
|
419
|
+
};
|
|
420
|
+
});
|
|
415
421
|
}
|
|
416
422
|
|
|
417
423
|
async function buildDepositWithLeverageIxs<QuoteResponse>(
|
|
@@ -426,12 +432,12 @@ async function buildDepositWithLeverageIxs<QuoteResponse>(
|
|
|
426
432
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
427
433
|
calcs: DepositLeverageCalcsResult,
|
|
428
434
|
budgetAndPriorityFeeIxs: IInstruction[] | undefined,
|
|
429
|
-
|
|
435
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
430
436
|
strategy: StrategyWithAddress | undefined,
|
|
431
437
|
collIsKtoken: boolean,
|
|
432
438
|
useV2Ixs: boolean,
|
|
433
439
|
elevationGroupOverride?: number
|
|
434
|
-
): Promise<LeverageIxsOutput> {
|
|
440
|
+
): Promise<LeverageIxsOutput[]> {
|
|
435
441
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
436
442
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
437
443
|
const [[collTokenAta], [debtTokenAta]] = await Promise.all([
|
|
@@ -516,29 +522,31 @@ async function buildDepositWithLeverageIxs<QuoteResponse>(
|
|
|
516
522
|
currentSlot
|
|
517
523
|
);
|
|
518
524
|
|
|
519
|
-
|
|
520
|
-
|
|
521
|
-
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
525
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
526
|
+
// 4. Swap
|
|
527
|
+
const { swapIxs } = swapQuoteIxs;
|
|
528
|
+
const swapInstructions = removeBudgetIxs(swapIxs);
|
|
529
|
+
const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
|
|
530
|
+
const flashLoanInfo = {
|
|
531
|
+
flashBorrowReserve: flashBorrowReserve.address,
|
|
532
|
+
flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
|
|
533
|
+
};
|
|
527
534
|
|
|
528
|
-
|
|
529
|
-
|
|
530
|
-
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
535
|
+
return {
|
|
536
|
+
flashLoanInfo,
|
|
537
|
+
instructions: [
|
|
538
|
+
...scopeRefreshIx,
|
|
539
|
+
...budgetIxs,
|
|
540
|
+
...createAtasIxs,
|
|
541
|
+
...fillWsolAtaIxs,
|
|
542
|
+
...[flashBorrowIx],
|
|
543
|
+
...(collIsKtoken ? swapInstructions : []),
|
|
544
|
+
...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
|
|
545
|
+
...(collIsKtoken ? [] : swapInstructions),
|
|
546
|
+
...[flashRepayIx],
|
|
547
|
+
],
|
|
548
|
+
};
|
|
549
|
+
});
|
|
542
550
|
}
|
|
543
551
|
|
|
544
552
|
export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
@@ -594,32 +602,36 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
594
602
|
slippagePct
|
|
595
603
|
);
|
|
596
604
|
|
|
597
|
-
const klendIxs =
|
|
598
|
-
|
|
599
|
-
|
|
600
|
-
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
605
|
+
const klendIxs = (
|
|
606
|
+
await buildWithdrawWithLeverageIxs(
|
|
607
|
+
kaminoMarket,
|
|
608
|
+
debtReserve!,
|
|
609
|
+
collReserve!,
|
|
610
|
+
owner,
|
|
611
|
+
obligation,
|
|
612
|
+
referrer,
|
|
613
|
+
currentSlot,
|
|
614
|
+
isClosingPosition,
|
|
615
|
+
inputTokenIsSol,
|
|
616
|
+
scopeRefreshConfig,
|
|
617
|
+
calcs,
|
|
618
|
+
budgetAndPriorityFeeIxs,
|
|
619
|
+
[
|
|
620
|
+
{
|
|
621
|
+
preActionIxs: [],
|
|
622
|
+
swapIxs: [],
|
|
623
|
+
lookupTables: [],
|
|
624
|
+
quote: {
|
|
625
|
+
priceAInB: new Decimal(0), // not used
|
|
626
|
+
quoteResponse: undefined,
|
|
627
|
+
},
|
|
628
|
+
},
|
|
629
|
+
],
|
|
630
|
+
strategy,
|
|
631
|
+
collIsKtoken,
|
|
632
|
+
useV2Ixs
|
|
633
|
+
)
|
|
634
|
+
)[0];
|
|
623
635
|
|
|
624
636
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
625
637
|
|
|
@@ -753,43 +765,43 @@ export async function getWithdrawWithLeverageIxs<QuoteResponse>({
|
|
|
753
765
|
}
|
|
754
766
|
}
|
|
755
767
|
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
budgetAndPriorityFeeIxs,
|
|
771
|
-
{
|
|
772
|
-
preActionIxs: [],
|
|
773
|
-
swapIxs: swap.swapIxs,
|
|
774
|
-
lookupTables: swap.lookupTables,
|
|
775
|
-
quote: swap.quote,
|
|
776
|
-
},
|
|
777
|
-
initialInputs.strategy,
|
|
778
|
-
initialInputs.collIsKtoken,
|
|
779
|
-
useV2Ixs
|
|
780
|
-
);
|
|
781
|
-
|
|
782
|
-
// Send ixs and lookup tables
|
|
768
|
+
const withdrawWithLeverageIxs = await buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
769
|
+
kaminoMarket,
|
|
770
|
+
debtReserve!,
|
|
771
|
+
collReserve!,
|
|
772
|
+
owner,
|
|
773
|
+
obligation,
|
|
774
|
+
referrer,
|
|
775
|
+
currentSlot,
|
|
776
|
+
isClosingPosition,
|
|
777
|
+
inputTokenIsSol,
|
|
778
|
+
scopeRefreshConfig,
|
|
779
|
+
initialInputs.calcs,
|
|
780
|
+
budgetAndPriorityFeeIxs,
|
|
781
|
+
swapsArray.map((swap) => {
|
|
783
782
|
return {
|
|
784
|
-
|
|
785
|
-
|
|
783
|
+
preActionIxs: [],
|
|
784
|
+
swapIxs: swap.swapIxs,
|
|
786
785
|
lookupTables: swap.lookupTables,
|
|
787
|
-
|
|
788
|
-
initialInputs: initialInputs,
|
|
789
|
-
quote: swap.quote.quoteResponse,
|
|
786
|
+
quote: swap.quote,
|
|
790
787
|
};
|
|
791
|
-
})
|
|
788
|
+
}),
|
|
789
|
+
initialInputs.strategy,
|
|
790
|
+
initialInputs.collIsKtoken,
|
|
791
|
+
useV2Ixs
|
|
792
792
|
);
|
|
793
|
+
|
|
794
|
+
// Send ixs and lookup tables
|
|
795
|
+
return withdrawWithLeverageIxs.map((ixs, index) => {
|
|
796
|
+
return {
|
|
797
|
+
ixs: ixs.instructions,
|
|
798
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
799
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
800
|
+
swapInputs,
|
|
801
|
+
initialInputs: initialInputs,
|
|
802
|
+
quote: swapsArray[index].quote.quoteResponse,
|
|
803
|
+
};
|
|
804
|
+
});
|
|
793
805
|
}
|
|
794
806
|
|
|
795
807
|
export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
@@ -805,11 +817,11 @@ export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
|
805
817
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
806
818
|
calcs: WithdrawLeverageCalcsResult,
|
|
807
819
|
budgetAndPriorityFeeIxs: IInstruction[] | undefined,
|
|
808
|
-
|
|
820
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
809
821
|
strategy: StrategyWithAddress | undefined,
|
|
810
822
|
collIsKtoken: boolean,
|
|
811
823
|
useV2Ixs: boolean
|
|
812
|
-
): Promise<LeverageIxsOutput> {
|
|
824
|
+
): Promise<LeverageIxsOutput[]> {
|
|
813
825
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
814
826
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
815
827
|
const debtTokenAta = await getAssociatedTokenAddress(
|
|
@@ -903,25 +915,27 @@ export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
|
903
915
|
referrer
|
|
904
916
|
);
|
|
905
917
|
|
|
906
|
-
|
|
918
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
919
|
+
const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
|
|
907
920
|
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
919
|
-
|
|
920
|
-
|
|
921
|
-
|
|
922
|
-
|
|
923
|
-
|
|
924
|
-
|
|
921
|
+
return {
|
|
922
|
+
flashLoanInfo: {
|
|
923
|
+
flashLoanFee: debtReserve.getFlashLoanFee(),
|
|
924
|
+
flashBorrowReserve: debtReserve.address,
|
|
925
|
+
},
|
|
926
|
+
instructions: [
|
|
927
|
+
...scopeRefreshIx,
|
|
928
|
+
...budgetIxs,
|
|
929
|
+
...createAtasIxs,
|
|
930
|
+
...fillWsolAtaIxs,
|
|
931
|
+
...[flashBorrowIx],
|
|
932
|
+
...KaminoAction.actionToIxs(repayAndWithdrawAction),
|
|
933
|
+
...swapInstructions,
|
|
934
|
+
...[flashRepayIx],
|
|
935
|
+
...closeWsolAtaIxs,
|
|
936
|
+
],
|
|
937
|
+
};
|
|
938
|
+
});
|
|
925
939
|
}
|
|
926
940
|
|
|
927
941
|
export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
@@ -945,6 +959,7 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
945
959
|
isKtoken,
|
|
946
960
|
quoter,
|
|
947
961
|
useV2Ixs,
|
|
962
|
+
withdrawSlotOffset,
|
|
948
963
|
}: AdjustLeverageSwapInputsProps<QuoteResponse>): Promise<{
|
|
949
964
|
swapInputs: SwapInputs;
|
|
950
965
|
flashLoanInfo: FlashLoanInfo;
|
|
@@ -994,30 +1009,34 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
994
1009
|
);
|
|
995
1010
|
|
|
996
1011
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
997
|
-
const klendIxs: LeverageIxsOutput =
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1015
|
-
|
|
1016
|
-
|
|
1017
|
-
|
|
1018
|
-
|
|
1019
|
-
|
|
1020
|
-
|
|
1012
|
+
const klendIxs: LeverageIxsOutput = (
|
|
1013
|
+
await buildIncreaseLeverageIxs(
|
|
1014
|
+
owner,
|
|
1015
|
+
kaminoMarket,
|
|
1016
|
+
collTokenMint,
|
|
1017
|
+
debtTokenMint,
|
|
1018
|
+
obligation,
|
|
1019
|
+
referrer,
|
|
1020
|
+
currentSlot,
|
|
1021
|
+
calcs,
|
|
1022
|
+
strategy,
|
|
1023
|
+
scopeRefreshConfig,
|
|
1024
|
+
collIsKtoken,
|
|
1025
|
+
[
|
|
1026
|
+
{
|
|
1027
|
+
preActionIxs: [],
|
|
1028
|
+
swapIxs: [],
|
|
1029
|
+
lookupTables: [],
|
|
1030
|
+
quote: {
|
|
1031
|
+
priceAInB: new Decimal(0), // not used
|
|
1032
|
+
quoteResponse: undefined,
|
|
1033
|
+
},
|
|
1034
|
+
},
|
|
1035
|
+
],
|
|
1036
|
+
budgetAndPriorityFeeIxs,
|
|
1037
|
+
useV2Ixs
|
|
1038
|
+
)
|
|
1039
|
+
)[0];
|
|
1021
1040
|
|
|
1022
1041
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
1023
1042
|
|
|
@@ -1103,30 +1122,35 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1103
1122
|
} else {
|
|
1104
1123
|
const calcs = adjustWithdrawLeverageCalcs(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
|
|
1105
1124
|
|
|
1106
|
-
const klendIxs: LeverageIxsOutput =
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1124
|
-
|
|
1125
|
-
|
|
1126
|
-
|
|
1127
|
-
|
|
1128
|
-
|
|
1129
|
-
|
|
1125
|
+
const klendIxs: LeverageIxsOutput = (
|
|
1126
|
+
await buildDecreaseLeverageIxs(
|
|
1127
|
+
owner,
|
|
1128
|
+
kaminoMarket,
|
|
1129
|
+
collTokenMint,
|
|
1130
|
+
debtTokenMint,
|
|
1131
|
+
obligation,
|
|
1132
|
+
referrer,
|
|
1133
|
+
currentSlot,
|
|
1134
|
+
calcs,
|
|
1135
|
+
strategy,
|
|
1136
|
+
scopeRefreshConfig,
|
|
1137
|
+
collIsKtoken,
|
|
1138
|
+
[
|
|
1139
|
+
{
|
|
1140
|
+
preActionIxs: [],
|
|
1141
|
+
swapIxs: [],
|
|
1142
|
+
lookupTables: [],
|
|
1143
|
+
quote: {
|
|
1144
|
+
priceAInB: new Decimal(0), // not used
|
|
1145
|
+
quoteResponse: undefined,
|
|
1146
|
+
},
|
|
1147
|
+
},
|
|
1148
|
+
],
|
|
1149
|
+
budgetAndPriorityFeeIxs,
|
|
1150
|
+
useV2Ixs,
|
|
1151
|
+
withdrawSlotOffset
|
|
1152
|
+
)
|
|
1153
|
+
)[0];
|
|
1130
1154
|
|
|
1131
1155
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
1132
1156
|
|
|
@@ -1213,6 +1237,7 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
|
1213
1237
|
quoter,
|
|
1214
1238
|
swapper,
|
|
1215
1239
|
useV2Ixs,
|
|
1240
|
+
withdrawSlotOffset,
|
|
1216
1241
|
}: AdjustLeverageProps<QuoteResponse>): Promise<Array<AdjustLeverageIxsResponse<QuoteResponse>>> {
|
|
1217
1242
|
const { swapInputs, initialInputs } = await getAdjustLeverageSwapInputs({
|
|
1218
1243
|
owner,
|
|
@@ -1261,39 +1286,39 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
|
1261
1286
|
|
|
1262
1287
|
const swapsArray = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
1263
1288
|
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
|
|
1272
|
-
|
|
1273
|
-
|
|
1274
|
-
|
|
1275
|
-
|
|
1276
|
-
|
|
1277
|
-
initialInputs.collIsKtoken,
|
|
1278
|
-
{
|
|
1279
|
-
preActionIxs: [],
|
|
1280
|
-
swapIxs: swap.swapIxs,
|
|
1281
|
-
lookupTables: swap.lookupTables,
|
|
1282
|
-
quote: swap.quote,
|
|
1283
|
-
},
|
|
1284
|
-
budgetAndPriorityFeeIxs,
|
|
1285
|
-
useV2Ixs
|
|
1286
|
-
);
|
|
1289
|
+
const increaseLeverageIxs = await buildIncreaseLeverageIxs(
|
|
1290
|
+
owner,
|
|
1291
|
+
kaminoMarket,
|
|
1292
|
+
collTokenMint,
|
|
1293
|
+
debtTokenMint,
|
|
1294
|
+
obligation,
|
|
1295
|
+
referrer,
|
|
1296
|
+
currentSlot,
|
|
1297
|
+
initialInputs.calcs,
|
|
1298
|
+
initialInputs.strategy,
|
|
1299
|
+
scopeRefreshConfig,
|
|
1300
|
+
initialInputs.collIsKtoken,
|
|
1301
|
+
swapsArray.map((swap) => {
|
|
1287
1302
|
return {
|
|
1288
|
-
|
|
1289
|
-
|
|
1303
|
+
preActionIxs: [],
|
|
1304
|
+
swapIxs: swap.swapIxs,
|
|
1290
1305
|
lookupTables: swap.lookupTables,
|
|
1291
|
-
|
|
1292
|
-
initialInputs,
|
|
1293
|
-
quote: swap.quote.quoteResponse,
|
|
1306
|
+
quote: swap.quote,
|
|
1294
1307
|
};
|
|
1295
|
-
})
|
|
1308
|
+
}),
|
|
1309
|
+
budgetAndPriorityFeeIxs,
|
|
1310
|
+
useV2Ixs
|
|
1296
1311
|
);
|
|
1312
|
+
return increaseLeverageIxs.map((ixs, index) => {
|
|
1313
|
+
return {
|
|
1314
|
+
ixs: ixs.instructions,
|
|
1315
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
1316
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
1317
|
+
swapInputs,
|
|
1318
|
+
initialInputs,
|
|
1319
|
+
quote: swapsArray[index].quote.quoteResponse,
|
|
1320
|
+
};
|
|
1321
|
+
});
|
|
1297
1322
|
} else {
|
|
1298
1323
|
console.log('Decreasing leverage');
|
|
1299
1324
|
|
|
@@ -1311,40 +1336,41 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
|
1311
1336
|
// 5. Get swap ixs
|
|
1312
1337
|
const swapsArray = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
1313
1338
|
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
|
|
1322
|
-
|
|
1323
|
-
|
|
1324
|
-
|
|
1325
|
-
|
|
1326
|
-
|
|
1327
|
-
initialInputs.collIsKtoken,
|
|
1328
|
-
{
|
|
1329
|
-
preActionIxs: [],
|
|
1330
|
-
swapIxs: swap.swapIxs,
|
|
1331
|
-
lookupTables: swap.lookupTables,
|
|
1332
|
-
quote: swap.quote,
|
|
1333
|
-
},
|
|
1334
|
-
budgetAndPriorityFeeIxs,
|
|
1335
|
-
useV2Ixs
|
|
1336
|
-
);
|
|
1337
|
-
|
|
1339
|
+
const decreaseLeverageIxs = await buildDecreaseLeverageIxs(
|
|
1340
|
+
owner,
|
|
1341
|
+
kaminoMarket,
|
|
1342
|
+
collTokenMint,
|
|
1343
|
+
debtTokenMint,
|
|
1344
|
+
obligation,
|
|
1345
|
+
referrer,
|
|
1346
|
+
currentSlot,
|
|
1347
|
+
initialInputs.calcs,
|
|
1348
|
+
initialInputs.strategy,
|
|
1349
|
+
scopeRefreshConfig,
|
|
1350
|
+
initialInputs.collIsKtoken,
|
|
1351
|
+
swapsArray.map((swap) => {
|
|
1338
1352
|
return {
|
|
1339
|
-
|
|
1340
|
-
|
|
1353
|
+
preActionIxs: [],
|
|
1354
|
+
swapIxs: swap.swapIxs,
|
|
1341
1355
|
lookupTables: swap.lookupTables,
|
|
1342
|
-
|
|
1343
|
-
initialInputs,
|
|
1344
|
-
quote: swap.quote.quoteResponse,
|
|
1356
|
+
quote: swap.quote,
|
|
1345
1357
|
};
|
|
1346
|
-
})
|
|
1358
|
+
}),
|
|
1359
|
+
budgetAndPriorityFeeIxs,
|
|
1360
|
+
useV2Ixs,
|
|
1361
|
+
withdrawSlotOffset
|
|
1347
1362
|
);
|
|
1363
|
+
|
|
1364
|
+
return decreaseLeverageIxs.map((ixs, index) => {
|
|
1365
|
+
return {
|
|
1366
|
+
ixs: ixs.instructions,
|
|
1367
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
1368
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
1369
|
+
swapInputs,
|
|
1370
|
+
initialInputs,
|
|
1371
|
+
quote: swapsArray[index].quote.quoteResponse,
|
|
1372
|
+
};
|
|
1373
|
+
});
|
|
1348
1374
|
}
|
|
1349
1375
|
}
|
|
1350
1376
|
|
|
@@ -1363,10 +1389,10 @@ async function buildIncreaseLeverageIxs<QuoteResponse>(
|
|
|
1363
1389
|
strategy: StrategyWithAddress | undefined,
|
|
1364
1390
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1365
1391
|
collIsKtoken: boolean,
|
|
1366
|
-
|
|
1392
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
1367
1393
|
budgetAndPriorityFeeIxs: IInstruction[] | undefined,
|
|
1368
1394
|
useV2Ixs: boolean
|
|
1369
|
-
): Promise<LeverageIxsOutput> {
|
|
1395
|
+
): Promise<LeverageIxsOutput[]> {
|
|
1370
1396
|
const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
|
|
1371
1397
|
const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
|
|
1372
1398
|
const [debtTokenAta, collTokenAta] = await Promise.all([
|
|
@@ -1440,30 +1466,32 @@ async function buildIncreaseLeverageIxs<QuoteResponse>(
|
|
|
1440
1466
|
currentSlot
|
|
1441
1467
|
);
|
|
1442
1468
|
|
|
1443
|
-
|
|
1444
|
-
|
|
1445
|
-
const ixs = [
|
|
1446
|
-
...scopeRefreshIx,
|
|
1447
|
-
...budgetIxs,
|
|
1448
|
-
...createAtasIxs,
|
|
1449
|
-
...[flashBorrowIx],
|
|
1450
|
-
...(collIsKtoken ? swapInstructions : []),
|
|
1451
|
-
...KaminoAction.actionToIxs(depositAction),
|
|
1452
|
-
...KaminoAction.actionToIxs(borrowAction),
|
|
1453
|
-
...(collIsKtoken ? [] : swapInstructions),
|
|
1454
|
-
...[flashRepayIx],
|
|
1455
|
-
];
|
|
1469
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
1470
|
+
const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
|
|
1456
1471
|
|
|
1457
|
-
|
|
1458
|
-
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1472
|
+
const ixs = [
|
|
1473
|
+
...scopeRefreshIx,
|
|
1474
|
+
...budgetIxs,
|
|
1475
|
+
...createAtasIxs,
|
|
1476
|
+
...[flashBorrowIx],
|
|
1477
|
+
...(collIsKtoken ? swapInstructions : []),
|
|
1478
|
+
...KaminoAction.actionToIxs(depositAction),
|
|
1479
|
+
...KaminoAction.actionToIxs(borrowAction),
|
|
1480
|
+
...(collIsKtoken ? [] : swapInstructions),
|
|
1481
|
+
...[flashRepayIx],
|
|
1482
|
+
];
|
|
1483
|
+
|
|
1484
|
+
const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
|
|
1485
|
+
const res: LeverageIxsOutput = {
|
|
1486
|
+
flashLoanInfo: {
|
|
1487
|
+
flashBorrowReserve: flashBorrowReserve.address,
|
|
1488
|
+
flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
|
|
1489
|
+
},
|
|
1490
|
+
instructions: ixs,
|
|
1491
|
+
};
|
|
1465
1492
|
|
|
1466
|
-
|
|
1493
|
+
return res;
|
|
1494
|
+
});
|
|
1467
1495
|
}
|
|
1468
1496
|
|
|
1469
1497
|
/**
|
|
@@ -1481,10 +1509,11 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
|
|
|
1481
1509
|
strategy: StrategyWithAddress | undefined,
|
|
1482
1510
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1483
1511
|
collIsKtoken: boolean,
|
|
1484
|
-
|
|
1512
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
1485
1513
|
budgetAndPriorityFeeIxs: IInstruction[] | undefined,
|
|
1486
|
-
useV2Ixs: boolean
|
|
1487
|
-
|
|
1514
|
+
useV2Ixs: boolean,
|
|
1515
|
+
withdrawSlotOffset: number = WITHDRAW_SLOT_OFFSET
|
|
1516
|
+
): Promise<LeverageIxsOutput[]> {
|
|
1488
1517
|
const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
|
|
1489
1518
|
const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
|
|
1490
1519
|
const [debtTokenAta] = await findAssociatedTokenPda({
|
|
@@ -1574,6 +1603,7 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
|
|
|
1574
1603
|
referrer
|
|
1575
1604
|
);
|
|
1576
1605
|
|
|
1606
|
+
const withdrawSlot = currentSlot - BigInt(withdrawSlotOffset);
|
|
1577
1607
|
// 6. Withdraw collateral (a little bit more to be able to pay for the slippage on swap)
|
|
1578
1608
|
const withdrawAction = await KaminoAction.buildWithdrawTxns(
|
|
1579
1609
|
kaminoMarket,
|
|
@@ -1588,33 +1618,35 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
|
|
|
1588
1618
|
false,
|
|
1589
1619
|
{ skipInitialization: true, skipLutCreation: true }, // 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)
|
|
1590
1620
|
referrer,
|
|
1591
|
-
|
|
1621
|
+
withdrawSlot
|
|
1592
1622
|
);
|
|
1593
1623
|
|
|
1594
|
-
|
|
1595
|
-
|
|
1596
|
-
const ixs = [
|
|
1597
|
-
...scopeRefreshIx,
|
|
1598
|
-
...budgetIxs,
|
|
1599
|
-
...createAtasIxs,
|
|
1600
|
-
...fillWsolAtaIxs,
|
|
1601
|
-
...[flashBorrowIx],
|
|
1602
|
-
...KaminoAction.actionToIxs(repayAction),
|
|
1603
|
-
...KaminoAction.actionToIxs(withdrawAction),
|
|
1604
|
-
...swapInstructions,
|
|
1605
|
-
...[flashRepayIx],
|
|
1606
|
-
...closeWsolAtaIxs,
|
|
1607
|
-
];
|
|
1624
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
1625
|
+
const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
|
|
1608
1626
|
|
|
1609
|
-
|
|
1610
|
-
|
|
1611
|
-
|
|
1612
|
-
|
|
1613
|
-
|
|
1614
|
-
|
|
1615
|
-
|
|
1627
|
+
const ixs = [
|
|
1628
|
+
...scopeRefreshIx,
|
|
1629
|
+
...budgetIxs,
|
|
1630
|
+
...createAtasIxs,
|
|
1631
|
+
...fillWsolAtaIxs,
|
|
1632
|
+
...[flashBorrowIx],
|
|
1633
|
+
...KaminoAction.actionToIxs(repayAction),
|
|
1634
|
+
...KaminoAction.actionToIxs(withdrawAction),
|
|
1635
|
+
...swapInstructions,
|
|
1636
|
+
...[flashRepayIx],
|
|
1637
|
+
...closeWsolAtaIxs,
|
|
1638
|
+
];
|
|
1616
1639
|
|
|
1617
|
-
|
|
1640
|
+
const res: LeverageIxsOutput = {
|
|
1641
|
+
flashLoanInfo: {
|
|
1642
|
+
flashBorrowReserve: debtReserve!.address,
|
|
1643
|
+
flashLoanFee: debtReserve!.getFlashLoanFee(),
|
|
1644
|
+
},
|
|
1645
|
+
instructions: ixs,
|
|
1646
|
+
};
|
|
1647
|
+
|
|
1648
|
+
return res;
|
|
1649
|
+
});
|
|
1618
1650
|
}
|
|
1619
1651
|
|
|
1620
1652
|
export const getSetupIxs = async (
|