@kamino-finance/klend-sdk 6.0.5-beta.1 → 6.1.0-beta.0
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/market.d.ts +1 -7
- package/dist/classes/market.d.ts.map +1 -1
- package/dist/classes/market.js +6 -26
- package/dist/classes/market.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 +4 -4
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/operations.d.ts +1 -1
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +169 -145
- package/dist/leverage/operations.js.map +1 -1
- package/package.json +1 -1
- package/src/classes/market.ts +6 -30
- package/src/lending_operations/repay_with_collateral_operations.ts +76 -72
- package/src/lending_operations/swap_collateral_operations.ts +13 -11
- package/src/leverage/operations.ts +340 -316
|
@@ -131,32 +131,36 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
|
|
|
131
131
|
const obligationType = checkObligationType(obligationTypeTagOverride, collTokenMint, debtTokenMint, kaminoMarket);
|
|
132
132
|
|
|
133
133
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
134
|
-
const klendIxs: LeverageIxsOutput =
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
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];
|
|
160
164
|
|
|
161
165
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
162
166
|
|
|
@@ -378,42 +382,42 @@ export async function getDepositWithLeverageIxs<QuoteResponse>({
|
|
|
378
382
|
const solTokenReserve = kaminoMarket.getReserveByMint(NATIVE_MINT);
|
|
379
383
|
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve!.getLiquidityMint());
|
|
380
384
|
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
budgetAndPriorityFeeIxs,
|
|
395
|
-
{
|
|
396
|
-
preActionIxs: [],
|
|
397
|
-
swapIxs: swap.swapIxs,
|
|
398
|
-
lookupTables: swap.lookupTables,
|
|
399
|
-
quote: swap.quote,
|
|
400
|
-
},
|
|
401
|
-
initialInputs.strategy,
|
|
402
|
-
initialInputs.collIsKtoken,
|
|
403
|
-
useV2Ixs,
|
|
404
|
-
elevationGroupOverride
|
|
405
|
-
);
|
|
406
|
-
|
|
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) => {
|
|
407
398
|
return {
|
|
408
|
-
|
|
409
|
-
|
|
399
|
+
preActionIxs: [],
|
|
400
|
+
swapIxs: swap.swapIxs,
|
|
410
401
|
lookupTables: swap.lookupTables,
|
|
411
|
-
|
|
412
|
-
initialInputs,
|
|
413
|
-
quote: swap.quote.quoteResponse,
|
|
402
|
+
quote: swap.quote,
|
|
414
403
|
};
|
|
415
|
-
})
|
|
404
|
+
}),
|
|
405
|
+
initialInputs.strategy,
|
|
406
|
+
initialInputs.collIsKtoken,
|
|
407
|
+
useV2Ixs,
|
|
408
|
+
elevationGroupOverride
|
|
416
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
|
+
});
|
|
417
421
|
}
|
|
418
422
|
|
|
419
423
|
async function buildDepositWithLeverageIxs<QuoteResponse>(
|
|
@@ -428,12 +432,12 @@ async function buildDepositWithLeverageIxs<QuoteResponse>(
|
|
|
428
432
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
429
433
|
calcs: DepositLeverageCalcsResult,
|
|
430
434
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
431
|
-
|
|
435
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
432
436
|
strategy: StrategyWithAddress | undefined,
|
|
433
437
|
collIsKtoken: boolean,
|
|
434
438
|
useV2Ixs: boolean,
|
|
435
439
|
elevationGroupOverride?: number
|
|
436
|
-
): Promise<LeverageIxsOutput> {
|
|
440
|
+
): Promise<LeverageIxsOutput[]> {
|
|
437
441
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
438
442
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
439
443
|
const collTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -519,29 +523,31 @@ async function buildDepositWithLeverageIxs<QuoteResponse>(
|
|
|
519
523
|
currentSlot
|
|
520
524
|
);
|
|
521
525
|
|
|
522
|
-
|
|
523
|
-
|
|
524
|
-
|
|
525
|
-
|
|
526
|
-
|
|
527
|
-
|
|
528
|
-
|
|
529
|
-
|
|
526
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
527
|
+
// 4. Swap
|
|
528
|
+
const { swapIxs } = swapQuoteIxs;
|
|
529
|
+
const swapInstructions = removeBudgetIxs(swapIxs);
|
|
530
|
+
const flashBorrowReserve = !collIsKtoken ? collReserve : debtReserve;
|
|
531
|
+
const flashLoanInfo = {
|
|
532
|
+
flashBorrowReserve: flashBorrowReserve.address,
|
|
533
|
+
flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
|
|
534
|
+
};
|
|
530
535
|
|
|
531
|
-
|
|
532
|
-
|
|
533
|
-
|
|
534
|
-
|
|
535
|
-
|
|
536
|
-
|
|
537
|
-
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
542
|
-
|
|
543
|
-
|
|
544
|
-
|
|
536
|
+
return {
|
|
537
|
+
flashLoanInfo,
|
|
538
|
+
instructions: [
|
|
539
|
+
...scopeRefreshIx,
|
|
540
|
+
...budgetIxs,
|
|
541
|
+
...createAtasIxs,
|
|
542
|
+
...fillWsolAtaIxs,
|
|
543
|
+
...[flashBorrowIx],
|
|
544
|
+
...(collIsKtoken ? swapInstructions : []),
|
|
545
|
+
...KaminoAction.actionToIxs(kaminoDepositAndBorrowAction),
|
|
546
|
+
...(collIsKtoken ? [] : swapInstructions),
|
|
547
|
+
...[flashRepayIx],
|
|
548
|
+
],
|
|
549
|
+
};
|
|
550
|
+
});
|
|
545
551
|
}
|
|
546
552
|
|
|
547
553
|
export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
@@ -597,32 +603,36 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
597
603
|
slippagePct
|
|
598
604
|
);
|
|
599
605
|
|
|
600
|
-
const klendIxs =
|
|
601
|
-
|
|
602
|
-
|
|
603
|
-
|
|
604
|
-
|
|
605
|
-
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
609
|
-
|
|
610
|
-
|
|
611
|
-
|
|
612
|
-
|
|
613
|
-
|
|
614
|
-
|
|
615
|
-
|
|
616
|
-
|
|
617
|
-
|
|
618
|
-
|
|
619
|
-
|
|
620
|
-
|
|
621
|
-
|
|
622
|
-
|
|
623
|
-
|
|
624
|
-
|
|
625
|
-
|
|
606
|
+
const klendIxs = (
|
|
607
|
+
await buildWithdrawWithLeverageIxs(
|
|
608
|
+
kaminoMarket,
|
|
609
|
+
debtReserve!,
|
|
610
|
+
collReserve!,
|
|
611
|
+
owner,
|
|
612
|
+
obligation,
|
|
613
|
+
referrer,
|
|
614
|
+
currentSlot,
|
|
615
|
+
isClosingPosition,
|
|
616
|
+
inputTokenIsSol,
|
|
617
|
+
scopeRefreshConfig,
|
|
618
|
+
calcs,
|
|
619
|
+
budgetAndPriorityFeeIxs,
|
|
620
|
+
[
|
|
621
|
+
{
|
|
622
|
+
preActionIxs: [],
|
|
623
|
+
swapIxs: [],
|
|
624
|
+
lookupTables: [],
|
|
625
|
+
quote: {
|
|
626
|
+
priceAInB: new Decimal(0), // not used
|
|
627
|
+
quoteResponse: undefined,
|
|
628
|
+
},
|
|
629
|
+
},
|
|
630
|
+
],
|
|
631
|
+
strategy,
|
|
632
|
+
collIsKtoken,
|
|
633
|
+
useV2Ixs
|
|
634
|
+
)
|
|
635
|
+
)[0];
|
|
626
636
|
|
|
627
637
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
628
638
|
|
|
@@ -756,43 +766,43 @@ export async function getWithdrawWithLeverageIxs<QuoteResponse>({
|
|
|
756
766
|
}
|
|
757
767
|
}
|
|
758
768
|
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
budgetAndPriorityFeeIxs,
|
|
774
|
-
{
|
|
775
|
-
preActionIxs: [],
|
|
776
|
-
swapIxs: swap.swapIxs,
|
|
777
|
-
lookupTables: swap.lookupTables,
|
|
778
|
-
quote: swap.quote,
|
|
779
|
-
},
|
|
780
|
-
initialInputs.strategy,
|
|
781
|
-
initialInputs.collIsKtoken,
|
|
782
|
-
useV2Ixs
|
|
783
|
-
);
|
|
784
|
-
|
|
785
|
-
// Send ixs and lookup tables
|
|
769
|
+
const withdrawWithLeverageIxs = await buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
770
|
+
kaminoMarket,
|
|
771
|
+
debtReserve!,
|
|
772
|
+
collReserve!,
|
|
773
|
+
owner,
|
|
774
|
+
obligation,
|
|
775
|
+
referrer,
|
|
776
|
+
currentSlot,
|
|
777
|
+
isClosingPosition,
|
|
778
|
+
inputTokenIsSol,
|
|
779
|
+
scopeRefreshConfig,
|
|
780
|
+
initialInputs.calcs,
|
|
781
|
+
budgetAndPriorityFeeIxs,
|
|
782
|
+
swapsArray.map((swap) => {
|
|
786
783
|
return {
|
|
787
|
-
|
|
788
|
-
|
|
784
|
+
preActionIxs: [],
|
|
785
|
+
swapIxs: swap.swapIxs,
|
|
789
786
|
lookupTables: swap.lookupTables,
|
|
790
|
-
|
|
791
|
-
initialInputs: initialInputs,
|
|
792
|
-
quote: swap.quote.quoteResponse,
|
|
787
|
+
quote: swap.quote,
|
|
793
788
|
};
|
|
794
|
-
})
|
|
789
|
+
}),
|
|
790
|
+
initialInputs.strategy,
|
|
791
|
+
initialInputs.collIsKtoken,
|
|
792
|
+
useV2Ixs
|
|
795
793
|
);
|
|
794
|
+
|
|
795
|
+
// Send ixs and lookup tables
|
|
796
|
+
return withdrawWithLeverageIxs.map((ixs, index) => {
|
|
797
|
+
return {
|
|
798
|
+
ixs: ixs.instructions,
|
|
799
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
800
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
801
|
+
swapInputs,
|
|
802
|
+
initialInputs: initialInputs,
|
|
803
|
+
quote: swapsArray[index].quote.quoteResponse,
|
|
804
|
+
};
|
|
805
|
+
});
|
|
796
806
|
}
|
|
797
807
|
|
|
798
808
|
export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
@@ -808,11 +818,11 @@ export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
|
808
818
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
809
819
|
calcs: WithdrawLeverageCalcsResult,
|
|
810
820
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
811
|
-
|
|
821
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
812
822
|
strategy: StrategyWithAddress | undefined,
|
|
813
823
|
collIsKtoken: boolean,
|
|
814
824
|
useV2Ixs: boolean
|
|
815
|
-
): Promise<LeverageIxsOutput> {
|
|
825
|
+
): Promise<LeverageIxsOutput[]> {
|
|
816
826
|
const collTokenMint = collReserve.getLiquidityMint();
|
|
817
827
|
const debtTokenMint = debtReserve.getLiquidityMint();
|
|
818
828
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -897,25 +907,27 @@ export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
|
897
907
|
referrer
|
|
898
908
|
);
|
|
899
909
|
|
|
900
|
-
|
|
910
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
911
|
+
const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
|
|
901
912
|
|
|
902
|
-
|
|
903
|
-
|
|
904
|
-
|
|
905
|
-
|
|
906
|
-
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
911
|
-
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
|
|
916
|
-
|
|
917
|
-
|
|
918
|
-
|
|
913
|
+
return {
|
|
914
|
+
flashLoanInfo: {
|
|
915
|
+
flashLoanFee: debtReserve.getFlashLoanFee(),
|
|
916
|
+
flashBorrowReserve: debtReserve.address,
|
|
917
|
+
},
|
|
918
|
+
instructions: [
|
|
919
|
+
...scopeRefreshIx,
|
|
920
|
+
...budgetIxs,
|
|
921
|
+
...createAtasIxs,
|
|
922
|
+
...fillWsolAtaIxs,
|
|
923
|
+
...[flashBorrowIx],
|
|
924
|
+
...KaminoAction.actionToIxs(repayAndWithdrawAction),
|
|
925
|
+
...swapInstructions,
|
|
926
|
+
...[flashRepayIx],
|
|
927
|
+
...closeWsolAtaIxs,
|
|
928
|
+
],
|
|
929
|
+
};
|
|
930
|
+
});
|
|
919
931
|
}
|
|
920
932
|
|
|
921
933
|
export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
@@ -988,30 +1000,34 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
988
1000
|
);
|
|
989
1001
|
|
|
990
1002
|
// Build the repay & withdraw collateral tx to get the number of accounts
|
|
991
|
-
const klendIxs: LeverageIxsOutput =
|
|
992
|
-
|
|
993
|
-
|
|
994
|
-
|
|
995
|
-
|
|
996
|
-
|
|
997
|
-
|
|
998
|
-
|
|
999
|
-
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1004
|
-
|
|
1005
|
-
|
|
1006
|
-
|
|
1007
|
-
|
|
1008
|
-
|
|
1009
|
-
|
|
1010
|
-
|
|
1011
|
-
|
|
1012
|
-
|
|
1013
|
-
|
|
1014
|
-
|
|
1003
|
+
const klendIxs: LeverageIxsOutput = (
|
|
1004
|
+
await buildIncreaseLeverageIxs(
|
|
1005
|
+
owner,
|
|
1006
|
+
kaminoMarket,
|
|
1007
|
+
collTokenMint,
|
|
1008
|
+
debtTokenMint,
|
|
1009
|
+
obligation,
|
|
1010
|
+
referrer,
|
|
1011
|
+
currentSlot,
|
|
1012
|
+
calcs,
|
|
1013
|
+
strategy,
|
|
1014
|
+
scopeRefreshConfig,
|
|
1015
|
+
collIsKtoken,
|
|
1016
|
+
[
|
|
1017
|
+
{
|
|
1018
|
+
preActionIxs: [],
|
|
1019
|
+
swapIxs: [],
|
|
1020
|
+
lookupTables: [],
|
|
1021
|
+
quote: {
|
|
1022
|
+
priceAInB: new Decimal(0), // not used
|
|
1023
|
+
quoteResponse: undefined,
|
|
1024
|
+
},
|
|
1025
|
+
},
|
|
1026
|
+
],
|
|
1027
|
+
budgetAndPriorityFeeIxs,
|
|
1028
|
+
useV2Ixs
|
|
1029
|
+
)
|
|
1030
|
+
)[0];
|
|
1015
1031
|
|
|
1016
1032
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
1017
1033
|
|
|
@@ -1097,30 +1113,34 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1097
1113
|
} else {
|
|
1098
1114
|
const calcs = adjustWithdrawLeverageCalcs(adjustDepositPosition, adjustBorrowPosition, flashLoanFee, slippagePct);
|
|
1099
1115
|
|
|
1100
|
-
const klendIxs: LeverageIxsOutput =
|
|
1101
|
-
|
|
1102
|
-
|
|
1103
|
-
|
|
1104
|
-
|
|
1105
|
-
|
|
1106
|
-
|
|
1107
|
-
|
|
1108
|
-
|
|
1109
|
-
|
|
1110
|
-
|
|
1111
|
-
|
|
1112
|
-
|
|
1113
|
-
|
|
1114
|
-
|
|
1115
|
-
|
|
1116
|
-
|
|
1117
|
-
|
|
1118
|
-
|
|
1119
|
-
|
|
1120
|
-
|
|
1121
|
-
|
|
1122
|
-
|
|
1123
|
-
|
|
1116
|
+
const klendIxs: LeverageIxsOutput = (
|
|
1117
|
+
await buildDecreaseLeverageIxs(
|
|
1118
|
+
owner,
|
|
1119
|
+
kaminoMarket,
|
|
1120
|
+
collTokenMint,
|
|
1121
|
+
debtTokenMint,
|
|
1122
|
+
obligation,
|
|
1123
|
+
referrer,
|
|
1124
|
+
currentSlot,
|
|
1125
|
+
calcs,
|
|
1126
|
+
strategy,
|
|
1127
|
+
scopeRefreshConfig,
|
|
1128
|
+
collIsKtoken,
|
|
1129
|
+
[
|
|
1130
|
+
{
|
|
1131
|
+
preActionIxs: [],
|
|
1132
|
+
swapIxs: [],
|
|
1133
|
+
lookupTables: [],
|
|
1134
|
+
quote: {
|
|
1135
|
+
priceAInB: new Decimal(0), // not used
|
|
1136
|
+
quoteResponse: undefined,
|
|
1137
|
+
},
|
|
1138
|
+
},
|
|
1139
|
+
],
|
|
1140
|
+
budgetAndPriorityFeeIxs,
|
|
1141
|
+
useV2Ixs
|
|
1142
|
+
)
|
|
1143
|
+
)[0];
|
|
1124
1144
|
|
|
1125
1145
|
const uniqueKlendAccounts = uniqueAccountsWithProgramIds(klendIxs.instructions);
|
|
1126
1146
|
|
|
@@ -1255,39 +1275,39 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
|
1255
1275
|
|
|
1256
1276
|
const swapsArray = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
1257
1277
|
|
|
1258
|
-
|
|
1259
|
-
|
|
1260
|
-
|
|
1261
|
-
|
|
1262
|
-
|
|
1263
|
-
|
|
1264
|
-
|
|
1265
|
-
|
|
1266
|
-
|
|
1267
|
-
|
|
1268
|
-
|
|
1269
|
-
|
|
1270
|
-
|
|
1271
|
-
initialInputs.collIsKtoken,
|
|
1272
|
-
{
|
|
1273
|
-
preActionIxs: [],
|
|
1274
|
-
swapIxs: swap.swapIxs,
|
|
1275
|
-
lookupTables: swap.lookupTables,
|
|
1276
|
-
quote: swap.quote,
|
|
1277
|
-
},
|
|
1278
|
-
budgetAndPriorityFeeIxs,
|
|
1279
|
-
useV2Ixs
|
|
1280
|
-
);
|
|
1278
|
+
const increaseLeverageIxs = await buildIncreaseLeverageIxs(
|
|
1279
|
+
owner,
|
|
1280
|
+
kaminoMarket,
|
|
1281
|
+
collTokenMint,
|
|
1282
|
+
debtTokenMint,
|
|
1283
|
+
obligation,
|
|
1284
|
+
referrer,
|
|
1285
|
+
currentSlot,
|
|
1286
|
+
initialInputs.calcs,
|
|
1287
|
+
initialInputs.strategy,
|
|
1288
|
+
scopeRefreshConfig,
|
|
1289
|
+
initialInputs.collIsKtoken,
|
|
1290
|
+
swapsArray.map((swap) => {
|
|
1281
1291
|
return {
|
|
1282
|
-
|
|
1283
|
-
|
|
1292
|
+
preActionIxs: [],
|
|
1293
|
+
swapIxs: swap.swapIxs,
|
|
1284
1294
|
lookupTables: swap.lookupTables,
|
|
1285
|
-
|
|
1286
|
-
initialInputs,
|
|
1287
|
-
quote: swap.quote.quoteResponse,
|
|
1295
|
+
quote: swap.quote,
|
|
1288
1296
|
};
|
|
1289
|
-
})
|
|
1297
|
+
}),
|
|
1298
|
+
budgetAndPriorityFeeIxs,
|
|
1299
|
+
useV2Ixs
|
|
1290
1300
|
);
|
|
1301
|
+
return increaseLeverageIxs.map((ixs, index) => {
|
|
1302
|
+
return {
|
|
1303
|
+
ixs: ixs.instructions,
|
|
1304
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
1305
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
1306
|
+
swapInputs,
|
|
1307
|
+
initialInputs,
|
|
1308
|
+
quote: swapsArray[index].quote.quoteResponse,
|
|
1309
|
+
};
|
|
1310
|
+
});
|
|
1291
1311
|
} else {
|
|
1292
1312
|
console.log('Decreasing leverage');
|
|
1293
1313
|
|
|
@@ -1305,40 +1325,40 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
|
1305
1325
|
// 5. Get swap ixs
|
|
1306
1326
|
const swapsArray = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
1307
1327
|
|
|
1308
|
-
|
|
1309
|
-
|
|
1310
|
-
|
|
1311
|
-
|
|
1312
|
-
|
|
1313
|
-
|
|
1314
|
-
|
|
1315
|
-
|
|
1316
|
-
|
|
1317
|
-
|
|
1318
|
-
|
|
1319
|
-
|
|
1320
|
-
|
|
1321
|
-
initialInputs.collIsKtoken,
|
|
1322
|
-
{
|
|
1323
|
-
preActionIxs: [],
|
|
1324
|
-
swapIxs: swap.swapIxs,
|
|
1325
|
-
lookupTables: swap.lookupTables,
|
|
1326
|
-
quote: swap.quote,
|
|
1327
|
-
},
|
|
1328
|
-
budgetAndPriorityFeeIxs,
|
|
1329
|
-
useV2Ixs
|
|
1330
|
-
);
|
|
1331
|
-
|
|
1328
|
+
const decreaseLeverageIxs = await buildDecreaseLeverageIxs(
|
|
1329
|
+
owner,
|
|
1330
|
+
kaminoMarket,
|
|
1331
|
+
collTokenMint,
|
|
1332
|
+
debtTokenMint,
|
|
1333
|
+
obligation,
|
|
1334
|
+
referrer,
|
|
1335
|
+
currentSlot,
|
|
1336
|
+
initialInputs.calcs,
|
|
1337
|
+
initialInputs.strategy,
|
|
1338
|
+
scopeRefreshConfig,
|
|
1339
|
+
initialInputs.collIsKtoken,
|
|
1340
|
+
swapsArray.map((swap) => {
|
|
1332
1341
|
return {
|
|
1333
|
-
|
|
1334
|
-
|
|
1342
|
+
preActionIxs: [],
|
|
1343
|
+
swapIxs: swap.swapIxs,
|
|
1335
1344
|
lookupTables: swap.lookupTables,
|
|
1336
|
-
|
|
1337
|
-
initialInputs,
|
|
1338
|
-
quote: swap.quote.quoteResponse,
|
|
1345
|
+
quote: swap.quote,
|
|
1339
1346
|
};
|
|
1340
|
-
})
|
|
1347
|
+
}),
|
|
1348
|
+
budgetAndPriorityFeeIxs,
|
|
1349
|
+
useV2Ixs
|
|
1341
1350
|
);
|
|
1351
|
+
|
|
1352
|
+
return decreaseLeverageIxs.map((ixs, index) => {
|
|
1353
|
+
return {
|
|
1354
|
+
ixs: ixs.instructions,
|
|
1355
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
1356
|
+
lookupTables: swapsArray[index].lookupTables,
|
|
1357
|
+
swapInputs,
|
|
1358
|
+
initialInputs,
|
|
1359
|
+
quote: swapsArray[index].quote.quoteResponse,
|
|
1360
|
+
};
|
|
1361
|
+
});
|
|
1342
1362
|
}
|
|
1343
1363
|
}
|
|
1344
1364
|
|
|
@@ -1357,10 +1377,10 @@ async function buildIncreaseLeverageIxs<QuoteResponse>(
|
|
|
1357
1377
|
strategy: StrategyWithAddress | undefined,
|
|
1358
1378
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1359
1379
|
collIsKtoken: boolean,
|
|
1360
|
-
|
|
1380
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
1361
1381
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
1362
1382
|
useV2Ixs: boolean
|
|
1363
|
-
): Promise<LeverageIxsOutput> {
|
|
1383
|
+
): Promise<LeverageIxsOutput[]> {
|
|
1364
1384
|
const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
|
|
1365
1385
|
const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
|
|
1366
1386
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -1442,30 +1462,32 @@ async function buildIncreaseLeverageIxs<QuoteResponse>(
|
|
|
1442
1462
|
currentSlot
|
|
1443
1463
|
);
|
|
1444
1464
|
|
|
1445
|
-
|
|
1446
|
-
|
|
1447
|
-
const ixs = [
|
|
1448
|
-
...scopeRefreshIx,
|
|
1449
|
-
...budgetIxs,
|
|
1450
|
-
...createAtasIxs,
|
|
1451
|
-
...[flashBorrowIx],
|
|
1452
|
-
...(collIsKtoken ? swapInstructions : []),
|
|
1453
|
-
...KaminoAction.actionToIxs(depositAction),
|
|
1454
|
-
...KaminoAction.actionToIxs(borrowAction),
|
|
1455
|
-
...(collIsKtoken ? [] : swapInstructions),
|
|
1456
|
-
...[flashRepayIx],
|
|
1457
|
-
];
|
|
1465
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
1466
|
+
const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
|
|
1458
1467
|
|
|
1459
|
-
|
|
1460
|
-
|
|
1461
|
-
|
|
1462
|
-
|
|
1463
|
-
|
|
1464
|
-
|
|
1465
|
-
|
|
1466
|
-
|
|
1468
|
+
const ixs = [
|
|
1469
|
+
...scopeRefreshIx,
|
|
1470
|
+
...budgetIxs,
|
|
1471
|
+
...createAtasIxs,
|
|
1472
|
+
...[flashBorrowIx],
|
|
1473
|
+
...(collIsKtoken ? swapInstructions : []),
|
|
1474
|
+
...KaminoAction.actionToIxs(depositAction),
|
|
1475
|
+
...KaminoAction.actionToIxs(borrowAction),
|
|
1476
|
+
...(collIsKtoken ? [] : swapInstructions),
|
|
1477
|
+
...[flashRepayIx],
|
|
1478
|
+
];
|
|
1467
1479
|
|
|
1468
|
-
|
|
1480
|
+
const flashBorrowReserve = !collIsKtoken ? collReserve! : debtReserve!;
|
|
1481
|
+
const res: LeverageIxsOutput = {
|
|
1482
|
+
flashLoanInfo: {
|
|
1483
|
+
flashBorrowReserve: flashBorrowReserve.address,
|
|
1484
|
+
flashLoanFee: flashBorrowReserve.getFlashLoanFee(),
|
|
1485
|
+
},
|
|
1486
|
+
instructions: ixs,
|
|
1487
|
+
};
|
|
1488
|
+
|
|
1489
|
+
return res;
|
|
1490
|
+
});
|
|
1469
1491
|
}
|
|
1470
1492
|
|
|
1471
1493
|
/**
|
|
@@ -1483,10 +1505,10 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
|
|
|
1483
1505
|
strategy: StrategyWithAddress | undefined,
|
|
1484
1506
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1485
1507
|
collIsKtoken: boolean,
|
|
1486
|
-
|
|
1508
|
+
swapQuoteIxsArray: SwapIxs<QuoteResponse>[],
|
|
1487
1509
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
1488
1510
|
useV2Ixs: boolean
|
|
1489
|
-
): Promise<LeverageIxsOutput> {
|
|
1511
|
+
): Promise<LeverageIxsOutput[]> {
|
|
1490
1512
|
const collReserve = kaminoMarket.getExistingReserveByMint(collTokenMint);
|
|
1491
1513
|
const debtReserve = kaminoMarket.getExistingReserveByMint(debtTokenMint);
|
|
1492
1514
|
const debtTokenAta = getAssociatedTokenAddressSync(
|
|
@@ -1578,30 +1600,32 @@ async function buildDecreaseLeverageIxs<QuoteResponse>(
|
|
|
1578
1600
|
currentSlot
|
|
1579
1601
|
);
|
|
1580
1602
|
|
|
1581
|
-
|
|
1582
|
-
|
|
1583
|
-
const ixs = [
|
|
1584
|
-
...scopeRefreshIx,
|
|
1585
|
-
...budgetIxs,
|
|
1586
|
-
...createAtasIxs,
|
|
1587
|
-
...fillWsolAtaIxs,
|
|
1588
|
-
...[flashBorrowIx],
|
|
1589
|
-
...KaminoAction.actionToIxs(repayAction),
|
|
1590
|
-
...KaminoAction.actionToIxs(withdrawAction),
|
|
1591
|
-
...swapInstructions,
|
|
1592
|
-
...[flashRepayIx],
|
|
1593
|
-
...closeWsolAtaIxs,
|
|
1594
|
-
];
|
|
1603
|
+
return swapQuoteIxsArray.map((swapQuoteIxs) => {
|
|
1604
|
+
const swapInstructions = removeBudgetIxs(swapQuoteIxs.swapIxs);
|
|
1595
1605
|
|
|
1596
|
-
|
|
1597
|
-
|
|
1598
|
-
|
|
1599
|
-
|
|
1600
|
-
|
|
1601
|
-
|
|
1602
|
-
|
|
1606
|
+
const ixs = [
|
|
1607
|
+
...scopeRefreshIx,
|
|
1608
|
+
...budgetIxs,
|
|
1609
|
+
...createAtasIxs,
|
|
1610
|
+
...fillWsolAtaIxs,
|
|
1611
|
+
...[flashBorrowIx],
|
|
1612
|
+
...KaminoAction.actionToIxs(repayAction),
|
|
1613
|
+
...KaminoAction.actionToIxs(withdrawAction),
|
|
1614
|
+
...swapInstructions,
|
|
1615
|
+
...[flashRepayIx],
|
|
1616
|
+
...closeWsolAtaIxs,
|
|
1617
|
+
];
|
|
1603
1618
|
|
|
1604
|
-
|
|
1619
|
+
const res: LeverageIxsOutput = {
|
|
1620
|
+
flashLoanInfo: {
|
|
1621
|
+
flashBorrowReserve: debtReserve!.address,
|
|
1622
|
+
flashLoanFee: debtReserve!.getFlashLoanFee(),
|
|
1623
|
+
},
|
|
1624
|
+
instructions: ixs,
|
|
1625
|
+
};
|
|
1626
|
+
|
|
1627
|
+
return res;
|
|
1628
|
+
});
|
|
1605
1629
|
}
|
|
1606
1630
|
|
|
1607
1631
|
export const getSetupIxs = async (
|