@kamino-finance/klend-sdk 7.3.10-beta.0 → 7.3.10-beta.2
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 +21 -91
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +152 -139
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/actionTypes.d.ts +310 -0
- package/dist/classes/actionTypes.d.ts.map +1 -0
- package/dist/classes/actionTypes.js +3 -0
- package/dist/classes/actionTypes.js.map +1 -0
- package/dist/classes/configItems.d.ts +1 -6
- package/dist/classes/configItems.d.ts.map +1 -1
- package/dist/classes/configItems.js +1 -93
- package/dist/classes/configItems.js.map +1 -1
- package/dist/classes/farm_utils.d.ts +1 -1
- package/dist/classes/farm_utils.d.ts.map +1 -1
- package/dist/classes/farm_utils.js +3 -1
- package/dist/classes/farm_utils.js.map +1 -1
- package/dist/classes/market.d.ts +15 -10
- package/dist/classes/market.d.ts.map +1 -1
- package/dist/classes/market.js +51 -28
- package/dist/classes/market.js.map +1 -1
- package/dist/classes/obligation.d.ts +11 -11
- package/dist/classes/obligation.d.ts.map +1 -1
- package/dist/classes/obligation.js +56 -56
- package/dist/classes/obligation.js.map +1 -1
- package/dist/classes/obligationOrder.d.ts.map +1 -1
- package/dist/classes/obligationOrder.js +6 -3
- package/dist/classes/obligationOrder.js.map +1 -1
- package/dist/classes/reserve.d.ts +7 -3
- package/dist/classes/reserve.d.ts.map +1 -1
- package/dist/classes/reserve.js +31 -43
- package/dist/classes/reserve.js.map +1 -1
- package/dist/classes/shared.d.ts +3 -2
- package/dist/classes/shared.d.ts.map +1 -1
- package/dist/classes/vault.d.ts +28 -1
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +103 -6
- package/dist/classes/vault.js.map +1 -1
- package/dist/classes/vault_types.d.ts +2 -1
- package/dist/classes/vault_types.d.ts.map +1 -1
- package/dist/client/commands/borrow.d.ts +1 -1
- package/dist/client/commands/borrow.d.ts.map +1 -1
- package/dist/client/commands/borrow.js +11 -2
- package/dist/client/commands/borrow.js.map +1 -1
- package/dist/client/commands/deposit.d.ts +1 -1
- package/dist/client/commands/deposit.d.ts.map +1 -1
- package/dist/client/commands/deposit.js +11 -2
- package/dist/client/commands/deposit.js.map +1 -1
- package/dist/client/commands/printReserve.d.ts +1 -1
- package/dist/client/commands/printReserve.d.ts.map +1 -1
- package/dist/client/commands/printReserve.js +2 -4
- package/dist/client/commands/printReserve.js.map +1 -1
- package/dist/client/commands/repay.d.ts +1 -1
- package/dist/client/commands/repay.d.ts.map +1 -1
- package/dist/client/commands/repay.js +12 -2
- package/dist/client/commands/repay.js.map +1 -1
- package/dist/client/commands/withdraw.d.ts +1 -1
- package/dist/client/commands/withdraw.d.ts.map +1 -1
- package/dist/client/commands/withdraw.js +11 -2
- package/dist/client/commands/withdraw.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_calcs.js +2 -2
- package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.d.ts +4 -4
- package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/repay_with_collateral_operations.js +43 -10
- package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.d.ts +4 -4
- package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
- package/dist/lending_operations/swap_collateral_operations.js +40 -21
- package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
- package/dist/leverage/operations.d.ts +6 -6
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +167 -52
- package/dist/leverage/operations.js.map +1 -1
- package/dist/leverage/types.d.ts +2 -2
- package/dist/leverage/types.d.ts.map +1 -1
- package/dist/manager/client_kamino_manager.js +0 -2
- package/dist/manager/client_kamino_manager.js.map +1 -1
- package/dist/obligation_orders/price_based.js +5 -3
- package/dist/obligation_orders/price_based.js.map +1 -1
- package/dist/utils/Logger.d.ts +14 -0
- package/dist/utils/Logger.d.ts.map +1 -0
- package/dist/utils/Logger.js +12 -0
- package/dist/utils/Logger.js.map +1 -0
- package/dist/utils/ObligationType.d.ts +33 -1
- package/dist/utils/ObligationType.d.ts.map +1 -1
- package/dist/utils/ObligationType.js +81 -2
- package/dist/utils/ObligationType.js.map +1 -1
- package/dist/utils/api.d.ts +13 -4
- package/dist/utils/api.d.ts.map +1 -1
- package/dist/utils/api.js +33 -31
- package/dist/utils/api.js.map +1 -1
- package/dist/utils/userMetadata.d.ts +2 -2
- package/dist/utils/userMetadata.d.ts.map +1 -1
- package/dist/utils/userMetadata.js +49 -25
- package/dist/utils/userMetadata.js.map +1 -1
- package/dist/utils/validations.d.ts +1 -0
- package/dist/utils/validations.d.ts.map +1 -1
- package/dist/utils/validations.js +5 -0
- package/dist/utils/validations.js.map +1 -1
- package/package.json +1 -1
- package/src/classes/action.ts +346 -372
- package/src/classes/actionTypes.ts +295 -0
- package/src/classes/configItems.ts +1 -99
- package/src/classes/farm_utils.ts +5 -1
- package/src/classes/market.ts +76 -33
- package/src/classes/obligation.ts +62 -59
- package/src/classes/obligationOrder.ts +6 -3
- package/src/classes/reserve.ts +118 -122
- package/src/classes/shared.ts +4 -2
- package/src/classes/vault.ts +160 -7
- package/src/classes/vault_types.ts +2 -1
- package/src/client/client.ts +17 -18
- package/src/client/commands/borrow.ts +10 -9
- package/src/client/commands/deposit.ts +10 -9
- package/src/client/commands/printReserve.ts +2 -4
- package/src/client/commands/repay.ts +11 -10
- package/src/client/commands/withdraw.ts +15 -9
- package/src/lending_operations/repay_with_collateral_calcs.ts +3 -4
- package/src/lending_operations/repay_with_collateral_operations.ts +40 -38
- package/src/lending_operations/swap_collateral_operations.ts +47 -41
- package/src/leverage/operations.ts +168 -129
- package/src/leverage/types.ts +2 -2
- package/src/manager/client_kamino_manager.ts +0 -2
- package/src/obligation_orders/price_based.ts +7 -5
- package/src/utils/Logger.ts +14 -0
- package/src/utils/ObligationType.ts +92 -1
- package/src/utils/api.ts +56 -33
- package/src/utils/userMetadata.ts +64 -30
- package/src/utils/validations.ts +5 -0
|
@@ -337,31 +337,33 @@ export class KaminoObligation {
|
|
|
337
337
|
return this.deposits.get(reserve);
|
|
338
338
|
}
|
|
339
339
|
|
|
340
|
-
|
|
340
|
+
getBorrowsByMint(mint: Address): Position[] {
|
|
341
|
+
const positions: Position[] = [];
|
|
341
342
|
for (const value of this.borrows.values()) {
|
|
342
343
|
if (value.mintAddress === mint) {
|
|
343
|
-
|
|
344
|
+
positions.push(value);
|
|
344
345
|
}
|
|
345
346
|
}
|
|
346
|
-
return
|
|
347
|
+
return positions;
|
|
347
348
|
}
|
|
348
349
|
|
|
349
350
|
getBorrowAmountByReserve(reserve: KaminoReserve): Decimal {
|
|
350
|
-
const amountLamports = this.
|
|
351
|
+
const amountLamports = this.getBorrowByReserve(reserve.address)?.amount ?? new Decimal(0);
|
|
351
352
|
return amountLamports.div(reserve.getMintFactor());
|
|
352
353
|
}
|
|
353
354
|
|
|
354
|
-
|
|
355
|
+
getDepositsByMint(mint: Address): Position[] {
|
|
356
|
+
const positions: Position[] = [];
|
|
355
357
|
for (const value of this.deposits.values()) {
|
|
356
358
|
if (value.mintAddress === mint) {
|
|
357
|
-
|
|
359
|
+
positions.push(value);
|
|
358
360
|
}
|
|
359
361
|
}
|
|
360
|
-
return
|
|
362
|
+
return positions;
|
|
361
363
|
}
|
|
362
364
|
|
|
363
365
|
getDepositAmountByReserve(reserve: KaminoReserve): Decimal {
|
|
364
|
-
const amountLamports = this.
|
|
366
|
+
const amountLamports = this.getDepositByReserve(reserve.address)?.amount ?? new Decimal(0);
|
|
365
367
|
return amountLamports.div(reserve.getMintFactor());
|
|
366
368
|
}
|
|
367
369
|
|
|
@@ -595,8 +597,8 @@ export class KaminoObligation {
|
|
|
595
597
|
amountCollateral?: Decimal;
|
|
596
598
|
amountDebt?: Decimal;
|
|
597
599
|
action: ActionType;
|
|
598
|
-
|
|
599
|
-
|
|
600
|
+
collateralReserveAddress?: Address;
|
|
601
|
+
debtReserveAddress?: Address;
|
|
600
602
|
market: KaminoMarket;
|
|
601
603
|
reserves: Map<Address, KaminoReserve>;
|
|
602
604
|
slot: Slot;
|
|
@@ -606,18 +608,15 @@ export class KaminoObligation {
|
|
|
606
608
|
deposits: Map<Address, Position>;
|
|
607
609
|
borrows: Map<Address, Position>;
|
|
608
610
|
} {
|
|
609
|
-
const { amountCollateral, amountDebt, action,
|
|
611
|
+
const { amountCollateral, amountDebt, action, collateralReserveAddress, debtReserveAddress, market } = params;
|
|
610
612
|
let newStats = { ...this.refreshedStats };
|
|
611
613
|
|
|
612
|
-
const collateralReservePk = mintCollateral ? market.getReserveByMint(mintCollateral)!.address : undefined;
|
|
613
|
-
const debtReservePk = mintDebt ? market.getReserveByMint(mintDebt)!.address : undefined;
|
|
614
|
-
|
|
615
614
|
const additionalReserves: Address[] = [];
|
|
616
|
-
if (
|
|
617
|
-
additionalReserves.push(
|
|
615
|
+
if (collateralReserveAddress !== undefined) {
|
|
616
|
+
additionalReserves.push(collateralReserveAddress);
|
|
618
617
|
}
|
|
619
|
-
if (
|
|
620
|
-
additionalReserves.push(
|
|
618
|
+
if (debtReserveAddress !== undefined) {
|
|
619
|
+
additionalReserves.push(debtReserveAddress);
|
|
621
620
|
}
|
|
622
621
|
|
|
623
622
|
const { collateralExchangeRates } = KaminoObligation.getRatesForObligation(
|
|
@@ -636,8 +635,8 @@ export class KaminoObligation {
|
|
|
636
635
|
// so we have to recalculate the entire position, not just an updated deposit or borrow
|
|
637
636
|
// as both LTVs and borrow factors can change, affecting all calcs
|
|
638
637
|
|
|
639
|
-
const debtReserveCumulativeBorrowRate =
|
|
640
|
-
? market.
|
|
638
|
+
const debtReserveCumulativeBorrowRate = debtReserveAddress
|
|
639
|
+
? market.getExistingReserveByAddress(debtReserveAddress).getCumulativeBorrowRate()
|
|
641
640
|
: undefined;
|
|
642
641
|
|
|
643
642
|
let newObligationDeposits = this.state.deposits;
|
|
@@ -645,13 +644,13 @@ export class KaminoObligation {
|
|
|
645
644
|
|
|
646
645
|
switch (action) {
|
|
647
646
|
case 'deposit': {
|
|
648
|
-
if (amountCollateral === undefined ||
|
|
649
|
-
throw Error('amountCollateral &
|
|
647
|
+
if (amountCollateral === undefined || collateralReserveAddress === undefined) {
|
|
648
|
+
throw Error('amountCollateral & collateralReserveAddress are required for deposit action');
|
|
650
649
|
}
|
|
651
650
|
newObligationDeposits = this.simulateDepositChange(
|
|
652
651
|
this.state.deposits,
|
|
653
652
|
{
|
|
654
|
-
reserveAddress:
|
|
653
|
+
reserveAddress: collateralReserveAddress,
|
|
655
654
|
amountChangeLamports: amountCollateral,
|
|
656
655
|
},
|
|
657
656
|
collateralExchangeRates
|
|
@@ -659,14 +658,14 @@ export class KaminoObligation {
|
|
|
659
658
|
break;
|
|
660
659
|
}
|
|
661
660
|
case 'borrow': {
|
|
662
|
-
if (amountDebt === undefined ||
|
|
663
|
-
throw Error('amountDebt &
|
|
661
|
+
if (amountDebt === undefined || debtReserveAddress === undefined) {
|
|
662
|
+
throw Error('amountDebt & debtReserveAddress are required for borrow action');
|
|
664
663
|
}
|
|
665
664
|
|
|
666
665
|
newObligationBorrows = this.simulateBorrowChange(
|
|
667
666
|
this.state.borrows,
|
|
668
667
|
{
|
|
669
|
-
reserveAddress:
|
|
668
|
+
reserveAddress: debtReserveAddress,
|
|
670
669
|
amountChangeLamports: amountDebt,
|
|
671
670
|
},
|
|
672
671
|
debtReserveCumulativeBorrowRate!
|
|
@@ -674,14 +673,14 @@ export class KaminoObligation {
|
|
|
674
673
|
break;
|
|
675
674
|
}
|
|
676
675
|
case 'repay': {
|
|
677
|
-
if (amountDebt === undefined ||
|
|
678
|
-
throw Error('amountDebt &
|
|
676
|
+
if (amountDebt === undefined || debtReserveAddress === undefined) {
|
|
677
|
+
throw Error('amountDebt & debtReserveAddress are required for repay action');
|
|
679
678
|
}
|
|
680
679
|
|
|
681
680
|
newObligationBorrows = this.simulateBorrowChange(
|
|
682
681
|
this.state.borrows,
|
|
683
682
|
{
|
|
684
|
-
reserveAddress:
|
|
683
|
+
reserveAddress: debtReserveAddress,
|
|
685
684
|
amountChangeLamports: amountDebt.neg(),
|
|
686
685
|
},
|
|
687
686
|
debtReserveCumulativeBorrowRate!
|
|
@@ -691,13 +690,13 @@ export class KaminoObligation {
|
|
|
691
690
|
}
|
|
692
691
|
|
|
693
692
|
case 'withdraw': {
|
|
694
|
-
if (amountCollateral === undefined ||
|
|
695
|
-
throw Error('amountCollateral &
|
|
693
|
+
if (amountCollateral === undefined || collateralReserveAddress === undefined) {
|
|
694
|
+
throw Error('amountCollateral & collateralReserveAddress are required for withdraw action');
|
|
696
695
|
}
|
|
697
696
|
newObligationDeposits = this.simulateDepositChange(
|
|
698
697
|
this.state.deposits,
|
|
699
698
|
{
|
|
700
|
-
reserveAddress:
|
|
699
|
+
reserveAddress: collateralReserveAddress,
|
|
701
700
|
amountChangeLamports: amountCollateral.neg(),
|
|
702
701
|
},
|
|
703
702
|
collateralExchangeRates
|
|
@@ -708,15 +707,17 @@ export class KaminoObligation {
|
|
|
708
707
|
if (
|
|
709
708
|
amountCollateral === undefined ||
|
|
710
709
|
amountDebt === undefined ||
|
|
711
|
-
|
|
712
|
-
|
|
710
|
+
collateralReserveAddress === undefined ||
|
|
711
|
+
debtReserveAddress === undefined
|
|
713
712
|
) {
|
|
714
|
-
throw Error(
|
|
713
|
+
throw Error(
|
|
714
|
+
'amountColl & amountDebt & collateralReserveAddress & debtReserveAddress are required for depositAndBorrow action'
|
|
715
|
+
);
|
|
715
716
|
}
|
|
716
717
|
newObligationDeposits = this.simulateDepositChange(
|
|
717
718
|
this.state.deposits,
|
|
718
719
|
{
|
|
719
|
-
reserveAddress:
|
|
720
|
+
reserveAddress: collateralReserveAddress,
|
|
720
721
|
amountChangeLamports: amountCollateral,
|
|
721
722
|
},
|
|
722
723
|
collateralExchangeRates
|
|
@@ -725,7 +726,7 @@ export class KaminoObligation {
|
|
|
725
726
|
newObligationBorrows = this.simulateBorrowChange(
|
|
726
727
|
this.state.borrows,
|
|
727
728
|
{
|
|
728
|
-
reserveAddress:
|
|
729
|
+
reserveAddress: debtReserveAddress,
|
|
729
730
|
amountChangeLamports: amountDebt,
|
|
730
731
|
},
|
|
731
732
|
debtReserveCumulativeBorrowRate!
|
|
@@ -736,15 +737,17 @@ export class KaminoObligation {
|
|
|
736
737
|
if (
|
|
737
738
|
amountCollateral === undefined ||
|
|
738
739
|
amountDebt === undefined ||
|
|
739
|
-
|
|
740
|
-
|
|
740
|
+
collateralReserveAddress === undefined ||
|
|
741
|
+
debtReserveAddress === undefined
|
|
741
742
|
) {
|
|
742
|
-
throw Error(
|
|
743
|
+
throw Error(
|
|
744
|
+
'amountColl & amountDebt & collateralReserveAddress & debtReserveAddress are required for repayAndWithdraw action'
|
|
745
|
+
);
|
|
743
746
|
}
|
|
744
747
|
newObligationDeposits = this.simulateDepositChange(
|
|
745
748
|
this.state.deposits,
|
|
746
749
|
{
|
|
747
|
-
reserveAddress:
|
|
750
|
+
reserveAddress: collateralReserveAddress,
|
|
748
751
|
amountChangeLamports: amountCollateral.neg(),
|
|
749
752
|
},
|
|
750
753
|
collateralExchangeRates
|
|
@@ -752,7 +755,7 @@ export class KaminoObligation {
|
|
|
752
755
|
newObligationBorrows = this.simulateBorrowChange(
|
|
753
756
|
this.state.borrows,
|
|
754
757
|
{
|
|
755
|
-
reserveAddress:
|
|
758
|
+
reserveAddress: debtReserveAddress,
|
|
756
759
|
amountChangeLamports: amountDebt.neg(),
|
|
757
760
|
},
|
|
758
761
|
debtReserveCumulativeBorrowRate!
|
|
@@ -1186,7 +1189,7 @@ export class KaminoObligation {
|
|
|
1186
1189
|
not a reserve-specific, caps-specific, liquidity-specific function.
|
|
1187
1190
|
|
|
1188
1191
|
* @param market - The KaminoMarket instance.
|
|
1189
|
-
* @param
|
|
1192
|
+
* @param liquidityReserveAddress - The liquidity reserve Address.
|
|
1190
1193
|
* @param slot - The slot number.
|
|
1191
1194
|
* @param elevationGroup - The elevation group number (default: this.state.elevationGroup).
|
|
1192
1195
|
* @returns The borrow power as a Decimal.
|
|
@@ -1194,11 +1197,11 @@ export class KaminoObligation {
|
|
|
1194
1197
|
*/
|
|
1195
1198
|
getBorrowPower(
|
|
1196
1199
|
market: KaminoMarket,
|
|
1197
|
-
|
|
1200
|
+
liquidityReserveAddress: Address,
|
|
1198
1201
|
slot: Slot,
|
|
1199
1202
|
elevationGroup: number = this.state.elevationGroup
|
|
1200
1203
|
): Decimal {
|
|
1201
|
-
const reserve = market.
|
|
1204
|
+
const reserve = market.getReserveByAddress(liquidityReserveAddress);
|
|
1202
1205
|
if (!reserve) {
|
|
1203
1206
|
throw new Error('Reserve not found');
|
|
1204
1207
|
}
|
|
@@ -1265,7 +1268,7 @@ export class KaminoObligation {
|
|
|
1265
1268
|
and a specific reserve, until it hits max LTV and given available liquidity and caps.
|
|
1266
1269
|
|
|
1267
1270
|
* @param market - The KaminoMarket instance.
|
|
1268
|
-
* @param
|
|
1271
|
+
* @param liquidityReserveAddress - The liquidity reserve Address.
|
|
1269
1272
|
* @param slot - The slot number.
|
|
1270
1273
|
* @param elevationGroup - The elevation group number (default: this.state.elevationGroup).
|
|
1271
1274
|
* @returns The maximum borrow amount as a Decimal.
|
|
@@ -1273,11 +1276,11 @@ export class KaminoObligation {
|
|
|
1273
1276
|
*/
|
|
1274
1277
|
getMaxBorrowAmountV2(
|
|
1275
1278
|
market: KaminoMarket,
|
|
1276
|
-
|
|
1279
|
+
liquidityReserveAddress: Address,
|
|
1277
1280
|
slot: Slot,
|
|
1278
1281
|
elevationGroup: number = this.state.elevationGroup
|
|
1279
1282
|
): Decimal {
|
|
1280
|
-
const reserve = market.
|
|
1283
|
+
const reserve = market.getReserveByAddress(liquidityReserveAddress);
|
|
1281
1284
|
if (!reserve) {
|
|
1282
1285
|
throw new Error('Reserve not found');
|
|
1283
1286
|
}
|
|
@@ -1287,7 +1290,7 @@ export class KaminoObligation {
|
|
|
1287
1290
|
[elevationGroup],
|
|
1288
1291
|
Array.from(this.deposits.keys())
|
|
1289
1292
|
)[0];
|
|
1290
|
-
const maxBorrowAmount = this.getBorrowPower(market,
|
|
1293
|
+
const maxBorrowAmount = this.getBorrowPower(market, liquidityReserveAddress, slot, elevationGroup);
|
|
1291
1294
|
|
|
1292
1295
|
if (elevationGroup === this.state.elevationGroup) {
|
|
1293
1296
|
return Decimal.min(maxBorrowAmount, liquidityAvailable);
|
|
@@ -1304,7 +1307,7 @@ export class KaminoObligation {
|
|
|
1304
1307
|
the new borrow power after the deposit, without overriding the obligation itself.
|
|
1305
1308
|
|
|
1306
1309
|
* @param market - The KaminoMarket instance.
|
|
1307
|
-
* @param
|
|
1310
|
+
* @param liquidityReserveAddress - The liquidity reserve Address.
|
|
1308
1311
|
* @param slot - The slot number.
|
|
1309
1312
|
* @param elevationGroup - The elevation group number (default: this.state.elevationGroup).
|
|
1310
1313
|
* @returns The maximum borrow amount as a Decimal.
|
|
@@ -1312,7 +1315,7 @@ export class KaminoObligation {
|
|
|
1312
1315
|
*/
|
|
1313
1316
|
getMaxBorrowAmountV2WithDeposit(
|
|
1314
1317
|
market: KaminoMarket,
|
|
1315
|
-
|
|
1318
|
+
liquidityReserveAddress: Address,
|
|
1316
1319
|
slot: Slot,
|
|
1317
1320
|
elevationGroup: number = this.state.elevationGroup,
|
|
1318
1321
|
depositAmountLamports: Decimal,
|
|
@@ -1326,7 +1329,7 @@ export class KaminoObligation {
|
|
|
1326
1329
|
];
|
|
1327
1330
|
const obligationWithDeposit = this.withPositionChanges(market, slot, depositChanges);
|
|
1328
1331
|
|
|
1329
|
-
return obligationWithDeposit.getMaxBorrowAmountV2(market,
|
|
1332
|
+
return obligationWithDeposit.getMaxBorrowAmountV2(market, liquidityReserveAddress, slot, elevationGroup);
|
|
1330
1333
|
}
|
|
1331
1334
|
|
|
1332
1335
|
/*
|
|
@@ -1406,11 +1409,11 @@ export class KaminoObligation {
|
|
|
1406
1409
|
/* Deprecated function, also broken */
|
|
1407
1410
|
getMaxBorrowAmount(
|
|
1408
1411
|
market: KaminoMarket,
|
|
1409
|
-
|
|
1412
|
+
liquidityReserveAddress: Address,
|
|
1410
1413
|
slot: Slot,
|
|
1411
1414
|
requestElevationGroup: boolean
|
|
1412
1415
|
): Decimal {
|
|
1413
|
-
const reserve = market.
|
|
1416
|
+
const reserve = market.getReserveByAddress(liquidityReserveAddress);
|
|
1414
1417
|
|
|
1415
1418
|
if (!reserve) {
|
|
1416
1419
|
throw new Error('Reserve not found');
|
|
@@ -1525,8 +1528,8 @@ export class KaminoObligation {
|
|
|
1525
1528
|
return Decimal.max(new Decimal(0), maxBorrowAmount);
|
|
1526
1529
|
}
|
|
1527
1530
|
|
|
1528
|
-
getMaxWithdrawAmount(market: KaminoMarket,
|
|
1529
|
-
const depositReserve = market.
|
|
1531
|
+
getMaxWithdrawAmount(market: KaminoMarket, depositReserveAddress: Address, slot: Slot): Decimal {
|
|
1532
|
+
const depositReserve = market.getReserveByAddress(depositReserveAddress);
|
|
1530
1533
|
|
|
1531
1534
|
if (!depositReserve) {
|
|
1532
1535
|
throw new Error('Reserve not found');
|
|
@@ -1585,7 +1588,7 @@ export class KaminoObligation {
|
|
|
1585
1588
|
* the new withdraw power after the repay, without overriding the obligation itself.
|
|
1586
1589
|
*
|
|
1587
1590
|
* @param market - The KaminoMarket instance.
|
|
1588
|
-
* @param
|
|
1591
|
+
* @param depositReserveAddress - The liquidity (deposit) reserve Address.
|
|
1589
1592
|
* @param slot - The slot number.
|
|
1590
1593
|
* @param repayAmountLamports - The amount to repay in lamports (use U64_MAX for full repay).
|
|
1591
1594
|
* @param repayReserveAddress - The reserve address of the borrow being repaid.
|
|
@@ -1594,7 +1597,7 @@ export class KaminoObligation {
|
|
|
1594
1597
|
*/
|
|
1595
1598
|
getMaxWithdrawAmountWithRepay(
|
|
1596
1599
|
market: KaminoMarket,
|
|
1597
|
-
|
|
1600
|
+
depositReserveAddress: Address,
|
|
1598
1601
|
slot: Slot,
|
|
1599
1602
|
repayAmountLamports: Decimal,
|
|
1600
1603
|
repayReserveAddress: Address
|
|
@@ -1615,7 +1618,7 @@ export class KaminoObligation {
|
|
|
1615
1618
|
];
|
|
1616
1619
|
const obligationWithRepay = this.withPositionChanges(market, slot, undefined, borrowChanges);
|
|
1617
1620
|
|
|
1618
|
-
return obligationWithRepay.getMaxWithdrawAmount(market,
|
|
1621
|
+
return obligationWithRepay.getMaxWithdrawAmount(market, depositReserveAddress, slot);
|
|
1619
1622
|
}
|
|
1620
1623
|
|
|
1621
1624
|
getObligationLiquidityByReserve(reserveAddress: Address): ObligationLiquidity {
|
|
@@ -169,6 +169,7 @@ export class DeleverageDebtAmount implements OrderOpportunity {
|
|
|
169
169
|
return {
|
|
170
170
|
mint: singleBorrow.mintAddress,
|
|
171
171
|
amount: Decimal.min(singleBorrow.amount, this.amount),
|
|
172
|
+
reserveAddress: singleBorrow.reserveAddress,
|
|
172
173
|
};
|
|
173
174
|
}
|
|
174
175
|
}
|
|
@@ -204,6 +205,7 @@ export class DeleverageAllDebt implements OrderOpportunity {
|
|
|
204
205
|
return {
|
|
205
206
|
mint: highestValueBorrow.mintAddress,
|
|
206
207
|
amount: highestValueBorrow.amount,
|
|
208
|
+
reserveAddress: highestValueBorrow.reserveAddress,
|
|
207
209
|
};
|
|
208
210
|
}
|
|
209
211
|
}
|
|
@@ -310,7 +312,7 @@ export class KaminoObligationOrder {
|
|
|
310
312
|
return undefined; // condition not met - cannot execute
|
|
311
313
|
}
|
|
312
314
|
const maxRepay = this.opportunity.getMaxRepay(obligation.getBorrows());
|
|
313
|
-
const repayBorrow = obligation.
|
|
315
|
+
const repayBorrow = obligation.getBorrowByReserve(maxRepay.reserveAddress)!;
|
|
314
316
|
const maxRepayValue = tokenAmountToValue(maxRepay, repayBorrow);
|
|
315
317
|
const executionBonusRate = this.calculateExecutionBonusRate(conditionHit, obligation);
|
|
316
318
|
const executionBonusFactor = new Decimal(1).add(executionBonusRate);
|
|
@@ -342,7 +344,7 @@ export class KaminoObligationOrder {
|
|
|
342
344
|
return valueComparison;
|
|
343
345
|
}
|
|
344
346
|
// Just for deterministic selection in case of multiple equally-good deposits: pick the one with lower mint pubkey (mostly for test stability purposes)
|
|
345
|
-
return leftDeposit.
|
|
347
|
+
return leftDeposit.reserveAddress.localeCompare(rightDeposit.reserveAddress);
|
|
346
348
|
})
|
|
347
349
|
.at(-1)!;
|
|
348
350
|
const actualRepayValue = actualWithdrawValue.div(executionBonusFactor);
|
|
@@ -505,7 +507,7 @@ export type AvailableOrderExecution = {
|
|
|
505
507
|
// Internal calculation functions:
|
|
506
508
|
|
|
507
509
|
function tokenAmountToValue(tokenAmount: TokenAmount, position: Position): Decimal {
|
|
508
|
-
if (tokenAmount.
|
|
510
|
+
if (tokenAmount.reserveAddress !== position.reserveAddress) {
|
|
509
511
|
throw new Error(`Value of token amount ${tokenAmount} cannot be computed using data from ${position}`);
|
|
510
512
|
}
|
|
511
513
|
return tokenAmount.amount.mul(position.marketValueRefreshed).div(position.amount);
|
|
@@ -516,6 +518,7 @@ function valueToTokenAmount(value: Decimal, position: Position): TokenAmount {
|
|
|
516
518
|
return {
|
|
517
519
|
amount: roundNearest(fractionalAmount),
|
|
518
520
|
mint: position.mintAddress,
|
|
521
|
+
reserveAddress: position.reserveAddress,
|
|
519
522
|
};
|
|
520
523
|
}
|
|
521
524
|
|