@kamino-finance/klend-sdk 5.14.1 → 5.14.3
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.map +1 -1
- package/dist/classes/market.js +1 -1
- package/dist/classes/market.js.map +1 -1
- package/dist/client_kamino_manager.d.ts.map +1 -1
- package/dist/client_kamino_manager.js +3 -2
- package/dist/client_kamino_manager.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts +2 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +13 -9
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.d.ts +1 -1
- package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +34 -30
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/operations.d.ts +4 -4
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +91 -55
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +8 -6
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/leverage/utils.d.ts.map +1 -1
- package/dist/leverage/utils.js +40 -19
- package/dist/leverage/utils.js.map +1 -1
- package/dist/utils/instruction.d.ts +3 -1
- package/dist/utils/instruction.d.ts.map +1 -1
- package/dist/utils/instruction.js +17 -1
- package/dist/utils/instruction.js.map +1 -1
- package/dist/utils/managerTypes.d.ts.map +1 -1
- package/dist/utils/managerTypes.js +9 -3
- package/dist/utils/managerTypes.js.map +1 -1
- package/package.json +1 -1
- package/src/classes/market.ts +6 -1
- package/src/client_kamino_manager.ts +6 -2
- package/src/lending_operations/repay_with_collateral_operations.ts +34 -26
- package/src/lending_operations/swap_collateral_operations.ts +43 -36
- package/src/leverage/operations.ts +175 -147
- package/src/leverage/types.ts +8 -6
- package/src/leverage/utils.ts +41 -20
- package/src/utils/instruction.ts +28 -0
- package/src/utils/managerTypes.ts +9 -3
|
@@ -147,6 +147,10 @@ export async function getDepositWithLeverageSwapInputs<QuoteResponse>({
|
|
|
147
147
|
preActionIxs: [],
|
|
148
148
|
swapIxs: [],
|
|
149
149
|
lookupTables: [],
|
|
150
|
+
quote: {
|
|
151
|
+
priceAInB: new Decimal(0), // not used
|
|
152
|
+
quoteResponse: undefined,
|
|
153
|
+
},
|
|
150
154
|
},
|
|
151
155
|
strategy,
|
|
152
156
|
collIsKtoken,
|
|
@@ -317,7 +321,7 @@ export async function getDepositWithLeverageIxs<QuoteResponse>({
|
|
|
317
321
|
swapper,
|
|
318
322
|
elevationGroupOverride,
|
|
319
323
|
useV2Ixs,
|
|
320
|
-
}: DepositWithLeverageProps<QuoteResponse>): Promise<DepositLeverageIxsResponse<QuoteResponse
|
|
324
|
+
}: DepositWithLeverageProps<QuoteResponse>): Promise<Array<DepositLeverageIxsResponse<QuoteResponse>>> {
|
|
321
325
|
const { swapInputs, initialInputs } = await getDepositWithLeverageSwapInputs({
|
|
322
326
|
owner,
|
|
323
327
|
kaminoMarket,
|
|
@@ -353,11 +357,7 @@ export async function getDepositWithLeverageIxs<QuoteResponse>({
|
|
|
353
357
|
depositSwapper = await getTokenToKtokenSwapper(kaminoMarket, kamino, owner, slippagePct, swapper, priceAinB, false);
|
|
354
358
|
}
|
|
355
359
|
|
|
356
|
-
const
|
|
357
|
-
swapInputs,
|
|
358
|
-
initialInputs.klendAccounts,
|
|
359
|
-
initialInputs.swapQuote
|
|
360
|
-
);
|
|
360
|
+
const swapsArray = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
361
361
|
|
|
362
362
|
if (initialInputs.collIsKtoken) {
|
|
363
363
|
if (initialInputs.strategy!.strategy.strategyLookupTable) {
|
|
@@ -365,7 +365,9 @@ export async function getDepositWithLeverageIxs<QuoteResponse>({
|
|
|
365
365
|
kaminoMarket.getConnection(),
|
|
366
366
|
initialInputs.strategy!.strategy.strategyLookupTable!
|
|
367
367
|
);
|
|
368
|
-
|
|
368
|
+
swapsArray.forEach((swap) => {
|
|
369
|
+
swap.lookupTables.push(strategyLut!);
|
|
370
|
+
});
|
|
369
371
|
} else {
|
|
370
372
|
console.log('Strategy lookup table not found');
|
|
371
373
|
}
|
|
@@ -376,39 +378,45 @@ export async function getDepositWithLeverageIxs<QuoteResponse>({
|
|
|
376
378
|
const solTokenReserve = kaminoMarket.getReserveByMint(NATIVE_MINT);
|
|
377
379
|
const depositTokenIsSol = !solTokenReserve ? false : selectedTokenMint.equals(solTokenReserve!.getLiquidityMint());
|
|
378
380
|
|
|
379
|
-
|
|
380
|
-
|
|
381
|
-
|
|
382
|
-
|
|
383
|
-
|
|
384
|
-
|
|
385
|
-
|
|
386
|
-
|
|
387
|
-
|
|
388
|
-
|
|
389
|
-
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
|
|
395
|
-
|
|
396
|
-
|
|
397
|
-
|
|
398
|
-
|
|
399
|
-
|
|
400
|
-
|
|
381
|
+
return Promise.all(
|
|
382
|
+
swapsArray.map(async (swap) => {
|
|
383
|
+
const ixs: LeverageIxsOutput = await buildDepositWithLeverageIxs(
|
|
384
|
+
kaminoMarket,
|
|
385
|
+
debtReserve!,
|
|
386
|
+
collReserve!,
|
|
387
|
+
owner,
|
|
388
|
+
initialInputs.obligation,
|
|
389
|
+
referrer,
|
|
390
|
+
currentSlot,
|
|
391
|
+
depositTokenIsSol,
|
|
392
|
+
scopeRefreshConfig,
|
|
393
|
+
initialInputs.calcs,
|
|
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
|
+
);
|
|
401
406
|
|
|
402
|
-
|
|
403
|
-
|
|
404
|
-
|
|
405
|
-
|
|
406
|
-
|
|
407
|
-
|
|
408
|
-
|
|
407
|
+
return {
|
|
408
|
+
ixs: ixs.instructions,
|
|
409
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
410
|
+
lookupTables: swap.lookupTables,
|
|
411
|
+
swapInputs,
|
|
412
|
+
initialInputs,
|
|
413
|
+
quote: swap.quote.quoteResponse,
|
|
414
|
+
};
|
|
415
|
+
})
|
|
416
|
+
);
|
|
409
417
|
}
|
|
410
418
|
|
|
411
|
-
async function buildDepositWithLeverageIxs(
|
|
419
|
+
async function buildDepositWithLeverageIxs<QuoteResponse>(
|
|
412
420
|
market: KaminoMarket,
|
|
413
421
|
debtReserve: KaminoReserve,
|
|
414
422
|
collReserve: KaminoReserve,
|
|
@@ -420,7 +428,7 @@ async function buildDepositWithLeverageIxs(
|
|
|
420
428
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
421
429
|
calcs: DepositLeverageCalcsResult,
|
|
422
430
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
423
|
-
swapQuoteIxs: SwapIxs
|
|
431
|
+
swapQuoteIxs: SwapIxs<QuoteResponse>,
|
|
424
432
|
strategy: StrategyWithAddress | undefined,
|
|
425
433
|
collIsKtoken: boolean,
|
|
426
434
|
useV2Ixs: boolean,
|
|
@@ -606,6 +614,10 @@ export async function getWithdrawWithLeverageSwapInputs<QuoteResponse>({
|
|
|
606
614
|
preActionIxs: [],
|
|
607
615
|
swapIxs: [],
|
|
608
616
|
lookupTables: [],
|
|
617
|
+
quote: {
|
|
618
|
+
priceAInB: new Decimal(0), // not used
|
|
619
|
+
quoteResponse: undefined,
|
|
620
|
+
},
|
|
609
621
|
},
|
|
610
622
|
strategy,
|
|
611
623
|
collIsKtoken,
|
|
@@ -688,7 +700,7 @@ export async function getWithdrawWithLeverageIxs<QuoteResponse>({
|
|
|
688
700
|
quoter,
|
|
689
701
|
swapper,
|
|
690
702
|
useV2Ixs,
|
|
691
|
-
}: WithdrawWithLeverageProps<QuoteResponse>): Promise<WithdrawLeverageIxsResponse<QuoteResponse
|
|
703
|
+
}: WithdrawWithLeverageProps<QuoteResponse>): Promise<Array<WithdrawLeverageIxsResponse<QuoteResponse>>> {
|
|
692
704
|
const collReserve = kaminoMarket.getReserveByMint(collTokenMint);
|
|
693
705
|
const debtReserve = kaminoMarket.getReserveByMint(debtTokenMint);
|
|
694
706
|
|
|
@@ -728,11 +740,7 @@ export async function getWithdrawWithLeverageIxs<QuoteResponse>({
|
|
|
728
740
|
withdrawSwapper = swapper;
|
|
729
741
|
}
|
|
730
742
|
|
|
731
|
-
const
|
|
732
|
-
swapInputs,
|
|
733
|
-
initialInputs.klendAccounts,
|
|
734
|
-
initialInputs.swapQuote
|
|
735
|
-
);
|
|
743
|
+
const swapsArray = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
736
744
|
|
|
737
745
|
if (initialInputs.collIsKtoken) {
|
|
738
746
|
if (initialInputs.strategy!.strategy.strategyLookupTable) {
|
|
@@ -740,46 +748,54 @@ export async function getWithdrawWithLeverageIxs<QuoteResponse>({
|
|
|
740
748
|
kaminoMarket.getConnection(),
|
|
741
749
|
initialInputs.strategy!.strategy.strategyLookupTable!
|
|
742
750
|
);
|
|
743
|
-
|
|
751
|
+
swapsArray.forEach((swap) => {
|
|
752
|
+
swap.lookupTables.push(strategyLut!);
|
|
753
|
+
});
|
|
744
754
|
} else {
|
|
745
755
|
console.log('Strategy lookup table not found');
|
|
746
756
|
}
|
|
747
757
|
}
|
|
748
758
|
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
756
|
-
|
|
757
|
-
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
762
|
-
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
|
|
766
|
-
|
|
767
|
-
|
|
768
|
-
|
|
769
|
-
|
|
770
|
-
|
|
759
|
+
return Promise.all(
|
|
760
|
+
swapsArray.map(async (swap) => {
|
|
761
|
+
const ixs: LeverageIxsOutput = await buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
762
|
+
kaminoMarket,
|
|
763
|
+
debtReserve!,
|
|
764
|
+
collReserve!,
|
|
765
|
+
owner,
|
|
766
|
+
obligation,
|
|
767
|
+
referrer,
|
|
768
|
+
currentSlot,
|
|
769
|
+
isClosingPosition,
|
|
770
|
+
inputTokenIsSol,
|
|
771
|
+
scopeRefreshConfig,
|
|
772
|
+
initialInputs.calcs,
|
|
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
|
+
);
|
|
771
784
|
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
785
|
+
// Send ixs and lookup tables
|
|
786
|
+
return {
|
|
787
|
+
ixs: ixs.instructions,
|
|
788
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
789
|
+
lookupTables: swap.lookupTables,
|
|
790
|
+
swapInputs,
|
|
791
|
+
initialInputs: initialInputs,
|
|
792
|
+
quote: swap.quote.quoteResponse,
|
|
793
|
+
};
|
|
794
|
+
})
|
|
795
|
+
);
|
|
780
796
|
}
|
|
781
797
|
|
|
782
|
-
export async function buildWithdrawWithLeverageIxs(
|
|
798
|
+
export async function buildWithdrawWithLeverageIxs<QuoteResponse>(
|
|
783
799
|
market: KaminoMarket,
|
|
784
800
|
debtReserve: KaminoReserve,
|
|
785
801
|
collReserve: KaminoReserve,
|
|
@@ -792,7 +808,7 @@ export async function buildWithdrawWithLeverageIxs(
|
|
|
792
808
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
793
809
|
calcs: WithdrawLeverageCalcsResult,
|
|
794
810
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
795
|
-
swapQuoteIxs: SwapIxs
|
|
811
|
+
swapQuoteIxs: SwapIxs<QuoteResponse>,
|
|
796
812
|
strategy: StrategyWithAddress | undefined,
|
|
797
813
|
collIsKtoken: boolean,
|
|
798
814
|
useV2Ixs: boolean
|
|
@@ -988,6 +1004,10 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
988
1004
|
preActionIxs: [],
|
|
989
1005
|
swapIxs: [],
|
|
990
1006
|
lookupTables: [],
|
|
1007
|
+
quote: {
|
|
1008
|
+
priceAInB: new Decimal(0), // not used
|
|
1009
|
+
quoteResponse: undefined,
|
|
1010
|
+
},
|
|
991
1011
|
},
|
|
992
1012
|
budgetAndPriorityFeeIxs,
|
|
993
1013
|
useV2Ixs
|
|
@@ -1093,6 +1113,10 @@ export async function getAdjustLeverageSwapInputs<QuoteResponse>({
|
|
|
1093
1113
|
preActionIxs: [],
|
|
1094
1114
|
swapIxs: [],
|
|
1095
1115
|
lookupTables: [],
|
|
1116
|
+
quote: {
|
|
1117
|
+
priceAInB: new Decimal(0), // not used
|
|
1118
|
+
quoteResponse: undefined,
|
|
1119
|
+
},
|
|
1096
1120
|
},
|
|
1097
1121
|
budgetAndPriorityFeeIxs,
|
|
1098
1122
|
useV2Ixs
|
|
@@ -1183,7 +1207,7 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
|
1183
1207
|
quoter,
|
|
1184
1208
|
swapper,
|
|
1185
1209
|
useV2Ixs,
|
|
1186
|
-
}: AdjustLeverageProps<QuoteResponse>): Promise<AdjustLeverageIxsResponse<QuoteResponse
|
|
1210
|
+
}: AdjustLeverageProps<QuoteResponse>): Promise<Array<AdjustLeverageIxsResponse<QuoteResponse>>> {
|
|
1187
1211
|
const { swapInputs, initialInputs } = await getAdjustLeverageSwapInputs({
|
|
1188
1212
|
owner,
|
|
1189
1213
|
kaminoMarket,
|
|
@@ -1229,39 +1253,41 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
|
1229
1253
|
depositSwapper = swapper;
|
|
1230
1254
|
}
|
|
1231
1255
|
|
|
1232
|
-
const
|
|
1233
|
-
|
|
1234
|
-
|
|
1235
|
-
|
|
1256
|
+
const swapsArray = await depositSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
1257
|
+
|
|
1258
|
+
return Promise.all(
|
|
1259
|
+
swapsArray.map(async (swap) => {
|
|
1260
|
+
const ixs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
|
|
1261
|
+
owner,
|
|
1262
|
+
kaminoMarket,
|
|
1263
|
+
collTokenMint,
|
|
1264
|
+
debtTokenMint,
|
|
1265
|
+
obligation,
|
|
1266
|
+
referrer,
|
|
1267
|
+
currentSlot,
|
|
1268
|
+
initialInputs.calcs,
|
|
1269
|
+
initialInputs.strategy,
|
|
1270
|
+
scopeRefreshConfig,
|
|
1271
|
+
initialInputs.collIsKtoken,
|
|
1272
|
+
{
|
|
1273
|
+
preActionIxs: [],
|
|
1274
|
+
swapIxs: swap.swapIxs,
|
|
1275
|
+
lookupTables: swap.lookupTables,
|
|
1276
|
+
quote: swap.quote,
|
|
1277
|
+
},
|
|
1278
|
+
budgetAndPriorityFeeIxs,
|
|
1279
|
+
useV2Ixs
|
|
1280
|
+
);
|
|
1281
|
+
return {
|
|
1282
|
+
ixs: ixs.instructions,
|
|
1283
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
1284
|
+
lookupTables: swap.lookupTables,
|
|
1285
|
+
swapInputs,
|
|
1286
|
+
initialInputs,
|
|
1287
|
+
quote: swap.quote.quoteResponse,
|
|
1288
|
+
};
|
|
1289
|
+
})
|
|
1236
1290
|
);
|
|
1237
|
-
|
|
1238
|
-
const ixs: LeverageIxsOutput = await buildIncreaseLeverageIxs(
|
|
1239
|
-
owner,
|
|
1240
|
-
kaminoMarket,
|
|
1241
|
-
collTokenMint,
|
|
1242
|
-
debtTokenMint,
|
|
1243
|
-
obligation,
|
|
1244
|
-
referrer,
|
|
1245
|
-
currentSlot,
|
|
1246
|
-
initialInputs.calcs,
|
|
1247
|
-
initialInputs.strategy,
|
|
1248
|
-
scopeRefreshConfig,
|
|
1249
|
-
initialInputs.collIsKtoken,
|
|
1250
|
-
{
|
|
1251
|
-
preActionIxs: [],
|
|
1252
|
-
swapIxs,
|
|
1253
|
-
lookupTables,
|
|
1254
|
-
},
|
|
1255
|
-
budgetAndPriorityFeeIxs,
|
|
1256
|
-
useV2Ixs
|
|
1257
|
-
);
|
|
1258
|
-
return {
|
|
1259
|
-
ixs: ixs.instructions,
|
|
1260
|
-
flashLoanInfo: ixs.flashLoanInfo,
|
|
1261
|
-
lookupTables,
|
|
1262
|
-
swapInputs,
|
|
1263
|
-
initialInputs,
|
|
1264
|
-
};
|
|
1265
1291
|
} else {
|
|
1266
1292
|
console.log('Decreasing leverage');
|
|
1267
1293
|
|
|
@@ -1277,47 +1303,49 @@ export async function getAdjustLeverageIxs<QuoteResponse>({
|
|
|
1277
1303
|
}
|
|
1278
1304
|
|
|
1279
1305
|
// 5. Get swap ixs
|
|
1280
|
-
const
|
|
1281
|
-
|
|
1282
|
-
|
|
1283
|
-
|
|
1284
|
-
|
|
1285
|
-
|
|
1286
|
-
|
|
1287
|
-
|
|
1288
|
-
|
|
1289
|
-
|
|
1290
|
-
|
|
1291
|
-
|
|
1292
|
-
|
|
1293
|
-
|
|
1294
|
-
|
|
1295
|
-
|
|
1296
|
-
|
|
1297
|
-
|
|
1298
|
-
|
|
1299
|
-
|
|
1300
|
-
|
|
1301
|
-
|
|
1302
|
-
|
|
1303
|
-
|
|
1304
|
-
|
|
1306
|
+
const swapsArray = await withdrawSwapper(swapInputs, initialInputs.klendAccounts, initialInputs.swapQuote);
|
|
1307
|
+
|
|
1308
|
+
return Promise.all(
|
|
1309
|
+
swapsArray.map(async (swap) => {
|
|
1310
|
+
const ixs: LeverageIxsOutput = await buildDecreaseLeverageIxs(
|
|
1311
|
+
owner,
|
|
1312
|
+
kaminoMarket,
|
|
1313
|
+
collTokenMint,
|
|
1314
|
+
debtTokenMint,
|
|
1315
|
+
obligation,
|
|
1316
|
+
referrer,
|
|
1317
|
+
currentSlot,
|
|
1318
|
+
initialInputs.calcs,
|
|
1319
|
+
initialInputs.strategy,
|
|
1320
|
+
scopeRefreshConfig,
|
|
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
|
+
|
|
1332
|
+
return {
|
|
1333
|
+
ixs: ixs.instructions,
|
|
1334
|
+
flashLoanInfo: ixs.flashLoanInfo,
|
|
1335
|
+
lookupTables: swap.lookupTables,
|
|
1336
|
+
swapInputs,
|
|
1337
|
+
initialInputs,
|
|
1338
|
+
quote: swap.quote.quoteResponse,
|
|
1339
|
+
};
|
|
1340
|
+
})
|
|
1305
1341
|
);
|
|
1306
|
-
|
|
1307
|
-
return {
|
|
1308
|
-
ixs: ixs.instructions,
|
|
1309
|
-
flashLoanInfo: ixs.flashLoanInfo,
|
|
1310
|
-
lookupTables,
|
|
1311
|
-
swapInputs,
|
|
1312
|
-
initialInputs,
|
|
1313
|
-
};
|
|
1314
1342
|
}
|
|
1315
1343
|
}
|
|
1316
1344
|
|
|
1317
1345
|
/**
|
|
1318
1346
|
* Deposit and borrow tokens if leverage increased
|
|
1319
1347
|
*/
|
|
1320
|
-
async function buildIncreaseLeverageIxs(
|
|
1348
|
+
async function buildIncreaseLeverageIxs<QuoteResponse>(
|
|
1321
1349
|
owner: PublicKey,
|
|
1322
1350
|
kaminoMarket: KaminoMarket,
|
|
1323
1351
|
collTokenMint: PublicKey,
|
|
@@ -1329,7 +1357,7 @@ async function buildIncreaseLeverageIxs(
|
|
|
1329
1357
|
strategy: StrategyWithAddress | undefined,
|
|
1330
1358
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1331
1359
|
collIsKtoken: boolean,
|
|
1332
|
-
swapQuoteIxs: SwapIxs
|
|
1360
|
+
swapQuoteIxs: SwapIxs<QuoteResponse>,
|
|
1333
1361
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
1334
1362
|
useV2Ixs: boolean
|
|
1335
1363
|
): Promise<LeverageIxsOutput> {
|
|
@@ -1443,7 +1471,7 @@ async function buildIncreaseLeverageIxs(
|
|
|
1443
1471
|
/**
|
|
1444
1472
|
* Withdraw and repay tokens if leverage decreased
|
|
1445
1473
|
*/
|
|
1446
|
-
async function buildDecreaseLeverageIxs(
|
|
1474
|
+
async function buildDecreaseLeverageIxs<QuoteResponse>(
|
|
1447
1475
|
owner: PublicKey,
|
|
1448
1476
|
kaminoMarket: KaminoMarket,
|
|
1449
1477
|
collTokenMint: PublicKey,
|
|
@@ -1455,7 +1483,7 @@ async function buildDecreaseLeverageIxs(
|
|
|
1455
1483
|
strategy: StrategyWithAddress | undefined,
|
|
1456
1484
|
scopeRefreshConfig: ScopePriceRefreshConfig | undefined,
|
|
1457
1485
|
collIsKtoken: boolean,
|
|
1458
|
-
swapQuoteIxs: SwapIxs
|
|
1486
|
+
swapQuoteIxs: SwapIxs<QuoteResponse>,
|
|
1459
1487
|
budgetAndPriorityFeeIxs: TransactionInstruction[] | undefined,
|
|
1460
1488
|
useV2Ixs: boolean
|
|
1461
1489
|
): Promise<LeverageIxsOutput> {
|
package/src/leverage/types.ts
CHANGED
|
@@ -13,17 +13,18 @@ export type SwapIxsProvider<QuoteResponse> = (
|
|
|
13
13
|
inputs: SwapInputs,
|
|
14
14
|
klendAccounts: Array<PublicKey>,
|
|
15
15
|
quote: SwapQuote<QuoteResponse>
|
|
16
|
-
) => Promise<SwapIxs
|
|
16
|
+
) => Promise<Array<SwapIxs<QuoteResponse>>>;
|
|
17
17
|
|
|
18
18
|
export type SwapQuote<QuoteResponse> = {
|
|
19
19
|
priceAInB: Decimal;
|
|
20
20
|
quoteResponse?: QuoteResponse;
|
|
21
21
|
};
|
|
22
22
|
|
|
23
|
-
export type SwapIxs = {
|
|
23
|
+
export type SwapIxs<QuoteResponse> = {
|
|
24
24
|
preActionIxs: TransactionInstruction[];
|
|
25
25
|
swapIxs: TransactionInstruction[];
|
|
26
26
|
lookupTables: AddressLookupTableAccount[];
|
|
27
|
+
quote: SwapQuote<QuoteResponse>;
|
|
27
28
|
};
|
|
28
29
|
|
|
29
30
|
export type PriceAinBProvider = (mintA: PublicKey, mintB: PublicKey) => Promise<Decimal>;
|
|
@@ -48,11 +49,12 @@ export type SwapInputs = {
|
|
|
48
49
|
amountDebtAtaBalance: Decimal | undefined;
|
|
49
50
|
};
|
|
50
51
|
|
|
51
|
-
export type BaseLeverageIxsResponse = {
|
|
52
|
+
export type BaseLeverageIxsResponse<QuoteResponse> = {
|
|
52
53
|
ixs: TransactionInstruction[];
|
|
53
54
|
lookupTables: AddressLookupTableAccount[];
|
|
54
55
|
swapInputs: SwapInputs;
|
|
55
56
|
flashLoanInfo: FlashLoanInfo;
|
|
57
|
+
quote?: QuoteResponse;
|
|
56
58
|
};
|
|
57
59
|
|
|
58
60
|
export type LeverageInitialInputs<LeverageCalcsResult, QuoteResponse> = {
|
|
@@ -82,7 +84,7 @@ export interface BaseLeverageSwapInputsProps<QuoteResponse> {
|
|
|
82
84
|
useV2Ixs: boolean;
|
|
83
85
|
}
|
|
84
86
|
|
|
85
|
-
export type DepositLeverageIxsResponse<QuoteResponse> = BaseLeverageIxsResponse & {
|
|
87
|
+
export type DepositLeverageIxsResponse<QuoteResponse> = BaseLeverageIxsResponse<QuoteResponse> & {
|
|
86
88
|
initialInputs: LeverageInitialInputs<DepositLeverageCalcsResult, QuoteResponse>;
|
|
87
89
|
};
|
|
88
90
|
|
|
@@ -125,7 +127,7 @@ export type DepositLeverageCalcsResult = {
|
|
|
125
127
|
requiredCollateralKtokenOnly: Decimal;
|
|
126
128
|
};
|
|
127
129
|
|
|
128
|
-
export type WithdrawLeverageIxsResponse<QuoteResponse> = BaseLeverageIxsResponse & {
|
|
130
|
+
export type WithdrawLeverageIxsResponse<QuoteResponse> = BaseLeverageIxsResponse<QuoteResponse> & {
|
|
129
131
|
initialInputs: LeverageInitialInputs<WithdrawLeverageCalcsResult, QuoteResponse>;
|
|
130
132
|
};
|
|
131
133
|
|
|
@@ -161,7 +163,7 @@ export type WithdrawLeverageCalcsResult = {
|
|
|
161
163
|
debtTokenExpectedSwapOut: Decimal;
|
|
162
164
|
};
|
|
163
165
|
|
|
164
|
-
export type AdjustLeverageIxsResponse<QuoteResponse> = BaseLeverageIxsResponse & {
|
|
166
|
+
export type AdjustLeverageIxsResponse<QuoteResponse> = BaseLeverageIxsResponse<QuoteResponse> & {
|
|
165
167
|
initialInputs: LeverageInitialInputs<AdjustLeverageCalcsResult, QuoteResponse> & {
|
|
166
168
|
isDeposit: boolean;
|
|
167
169
|
};
|
package/src/leverage/utils.ts
CHANGED
|
@@ -38,7 +38,7 @@ export async function getTokenToKtokenSwapper<QuoteResponse>(
|
|
|
38
38
|
inputs: SwapInputs,
|
|
39
39
|
klendAccounts: Array<PublicKey>,
|
|
40
40
|
quote: SwapQuote<QuoteResponse>
|
|
41
|
-
): Promise<SwapIxs
|
|
41
|
+
): Promise<Array<SwapIxs<QuoteResponse>>> => {
|
|
42
42
|
const slippageBps = new Decimal(slippagePct).mul('100');
|
|
43
43
|
const mintInDecimals = kaminoMarket.getExistingReserveByMint(inputs.inputMint).getMintDecimals();
|
|
44
44
|
const amountIn = lamportsToNumberDecimal(inputs.inputAmountLamports, mintInDecimals);
|
|
@@ -65,11 +65,18 @@ export async function getTokenToKtokenSwapper<QuoteResponse>(
|
|
|
65
65
|
|
|
66
66
|
const luts = await getLookupTableAccounts(kaminoMarket.getConnection(), ixWithLookup.lookupTablesAddresses);
|
|
67
67
|
|
|
68
|
-
return
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
68
|
+
return [
|
|
69
|
+
{
|
|
70
|
+
preActionIxs: [],
|
|
71
|
+
swapIxs: ixWithLookup.instructions,
|
|
72
|
+
lookupTables: luts,
|
|
73
|
+
// TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
|
|
74
|
+
quote: {
|
|
75
|
+
priceAInB: new Decimal(0),
|
|
76
|
+
quoteResponse: undefined,
|
|
77
|
+
},
|
|
78
|
+
},
|
|
79
|
+
];
|
|
73
80
|
};
|
|
74
81
|
}
|
|
75
82
|
|
|
@@ -149,7 +156,7 @@ export async function getKtokenToTokenSwapper<QuoteResponse>(
|
|
|
149
156
|
);
|
|
150
157
|
|
|
151
158
|
if (inputs.outputMint.equals(kaminoStrategy!.strategy.tokenAMint!)) {
|
|
152
|
-
const
|
|
159
|
+
const swapArray = await swapper(
|
|
153
160
|
{
|
|
154
161
|
inputAmountLamports: estimatedBOut,
|
|
155
162
|
inputMint: kaminoStrategy!.strategy.tokenBMint!,
|
|
@@ -159,14 +166,19 @@ export async function getKtokenToTokenSwapper<QuoteResponse>(
|
|
|
159
166
|
klendAccounts,
|
|
160
167
|
quote
|
|
161
168
|
);
|
|
169
|
+
// TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
|
|
170
|
+
const swap = swapArray[0];
|
|
162
171
|
|
|
163
|
-
return
|
|
164
|
-
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
172
|
+
return [
|
|
173
|
+
{
|
|
174
|
+
preActionIxs: [],
|
|
175
|
+
swapIxs: [...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swap.swapIxs],
|
|
176
|
+
lookupTables: swap.lookupTables,
|
|
177
|
+
quote: swap.quote,
|
|
178
|
+
},
|
|
179
|
+
];
|
|
168
180
|
} else if (inputs.outputMint.equals(kaminoStrategy!.strategy.tokenBMint!)) {
|
|
169
|
-
const
|
|
181
|
+
const swapArray = await swapper(
|
|
170
182
|
{
|
|
171
183
|
inputAmountLamports: estimatedAOut,
|
|
172
184
|
inputMint: kaminoStrategy!.strategy.tokenAMint!,
|
|
@@ -176,12 +188,17 @@ export async function getKtokenToTokenSwapper<QuoteResponse>(
|
|
|
176
188
|
klendAccounts,
|
|
177
189
|
quote
|
|
178
190
|
);
|
|
191
|
+
// TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
|
|
192
|
+
const swap = swapArray[0];
|
|
179
193
|
|
|
180
|
-
return
|
|
181
|
-
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
194
|
+
return [
|
|
195
|
+
{
|
|
196
|
+
preActionIxs: [],
|
|
197
|
+
swapIxs: [...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swap.swapIxs],
|
|
198
|
+
lookupTables: swap.lookupTables,
|
|
199
|
+
quote: swap.quote,
|
|
200
|
+
},
|
|
201
|
+
];
|
|
185
202
|
} else {
|
|
186
203
|
throw Error('Deposit token is neither A nor B in the strategy');
|
|
187
204
|
}
|
|
@@ -264,7 +281,7 @@ export function swapProviderToKaminoSwapProvider<QuoteResponse>(
|
|
|
264
281
|
_allKeys: PublicKey[]
|
|
265
282
|
): Promise<[TransactionInstruction[], PublicKey[]]> => {
|
|
266
283
|
if (input.tokenBToSwapAmount.lt(0)) {
|
|
267
|
-
const
|
|
284
|
+
const swapperIxsArray = await swapper(
|
|
268
285
|
{
|
|
269
286
|
inputAmountLamports: input.tokenBToSwapAmount.abs(),
|
|
270
287
|
inputMint: tokenBMint,
|
|
@@ -274,9 +291,11 @@ export function swapProviderToKaminoSwapProvider<QuoteResponse>(
|
|
|
274
291
|
klendAccounts,
|
|
275
292
|
swapQuote
|
|
276
293
|
);
|
|
294
|
+
// TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
|
|
295
|
+
const swapperIxs = swapperIxsArray[0];
|
|
277
296
|
return [swapperIxs.swapIxs, swapperIxs.lookupTables.map((lt) => lt.key)];
|
|
278
297
|
} else if (input.tokenAToSwapAmount.lt(0)) {
|
|
279
|
-
const
|
|
298
|
+
const swapperIxsArray = await swapper(
|
|
280
299
|
{
|
|
281
300
|
inputAmountLamports: input.tokenAToSwapAmount.abs(),
|
|
282
301
|
inputMint: tokenAMint,
|
|
@@ -286,6 +305,8 @@ export function swapProviderToKaminoSwapProvider<QuoteResponse>(
|
|
|
286
305
|
klendAccounts,
|
|
287
306
|
swapQuote
|
|
288
307
|
);
|
|
308
|
+
// TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
|
|
309
|
+
const swapperIxs = swapperIxsArray[0];
|
|
289
310
|
return [swapperIxs.swapIxs, swapperIxs.lookupTables.map((lt) => lt.key)];
|
|
290
311
|
} else {
|
|
291
312
|
throw Error('Nothing to swap');
|
package/src/utils/instruction.ts
CHANGED
|
@@ -2,6 +2,7 @@ import Decimal from 'decimal.js';
|
|
|
2
2
|
|
|
3
3
|
import {
|
|
4
4
|
AddressLookupTableAccount,
|
|
5
|
+
Blockhash,
|
|
5
6
|
Commitment,
|
|
6
7
|
ComputeBudgetProgram,
|
|
7
8
|
Connection,
|
|
@@ -209,6 +210,33 @@ export const buildVersionedTransaction = async (
|
|
|
209
210
|
return new VersionedTransaction(messageV0);
|
|
210
211
|
};
|
|
211
212
|
|
|
213
|
+
export async function getLookupTableAccountsFromAddresses(
|
|
214
|
+
connection: Connection,
|
|
215
|
+
addresses: PublicKey[]
|
|
216
|
+
): Promise<AddressLookupTableAccount[]> {
|
|
217
|
+
const lookupTablesAccounts = await Promise.all(
|
|
218
|
+
addresses.map((address) => {
|
|
219
|
+
return getLookupTableAccount(connection, address);
|
|
220
|
+
})
|
|
221
|
+
);
|
|
222
|
+
return lookupTablesAccounts.filter((account) => account !== null) as AddressLookupTableAccount[];
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
export const buildVersionedTransactionSync = (
|
|
226
|
+
payer: PublicKey,
|
|
227
|
+
instructions: TransactionInstruction[],
|
|
228
|
+
blockhash: Blockhash,
|
|
229
|
+
lookupTables: AddressLookupTableAccount[] = [],
|
|
230
|
+
): VersionedTransaction => {
|
|
231
|
+
const messageV0 = new TransactionMessage({
|
|
232
|
+
payerKey: payer,
|
|
233
|
+
recentBlockhash: blockhash,
|
|
234
|
+
instructions,
|
|
235
|
+
}).compileToV0Message(lookupTables.filter(notEmpty));
|
|
236
|
+
|
|
237
|
+
return new VersionedTransaction(messageV0);
|
|
238
|
+
}
|
|
239
|
+
|
|
212
240
|
export const getLookupTableAccount = async (connection: Connection, address: PublicKey) => {
|
|
213
241
|
return connection.getAddressLookupTable(address).then((res) => res.value);
|
|
214
242
|
};
|