@kamino-finance/klend-sdk 7.3.10-beta.0 → 7.3.10-beta.1

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.
Files changed (131) hide show
  1. package/dist/classes/action.d.ts +21 -91
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +152 -139
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/actionTypes.d.ts +310 -0
  6. package/dist/classes/actionTypes.d.ts.map +1 -0
  7. package/dist/classes/actionTypes.js +3 -0
  8. package/dist/classes/actionTypes.js.map +1 -0
  9. package/dist/classes/configItems.d.ts +1 -6
  10. package/dist/classes/configItems.d.ts.map +1 -1
  11. package/dist/classes/configItems.js +1 -93
  12. package/dist/classes/configItems.js.map +1 -1
  13. package/dist/classes/farm_utils.d.ts +1 -1
  14. package/dist/classes/farm_utils.d.ts.map +1 -1
  15. package/dist/classes/farm_utils.js +3 -1
  16. package/dist/classes/farm_utils.js.map +1 -1
  17. package/dist/classes/market.d.ts +15 -10
  18. package/dist/classes/market.d.ts.map +1 -1
  19. package/dist/classes/market.js +49 -26
  20. package/dist/classes/market.js.map +1 -1
  21. package/dist/classes/obligation.d.ts +9 -9
  22. package/dist/classes/obligation.d.ts.map +1 -1
  23. package/dist/classes/obligation.js +48 -50
  24. package/dist/classes/obligation.js.map +1 -1
  25. package/dist/classes/obligationOrder.d.ts.map +1 -1
  26. package/dist/classes/obligationOrder.js +6 -3
  27. package/dist/classes/obligationOrder.js.map +1 -1
  28. package/dist/classes/reserve.d.ts +7 -3
  29. package/dist/classes/reserve.d.ts.map +1 -1
  30. package/dist/classes/reserve.js +31 -43
  31. package/dist/classes/reserve.js.map +1 -1
  32. package/dist/classes/shared.d.ts +3 -2
  33. package/dist/classes/shared.d.ts.map +1 -1
  34. package/dist/classes/vault.d.ts +28 -1
  35. package/dist/classes/vault.d.ts.map +1 -1
  36. package/dist/classes/vault.js +103 -6
  37. package/dist/classes/vault.js.map +1 -1
  38. package/dist/classes/vault_types.d.ts +2 -1
  39. package/dist/classes/vault_types.d.ts.map +1 -1
  40. package/dist/client/commands/borrow.d.ts +1 -1
  41. package/dist/client/commands/borrow.d.ts.map +1 -1
  42. package/dist/client/commands/borrow.js +11 -2
  43. package/dist/client/commands/borrow.js.map +1 -1
  44. package/dist/client/commands/deposit.d.ts +1 -1
  45. package/dist/client/commands/deposit.d.ts.map +1 -1
  46. package/dist/client/commands/deposit.js +11 -2
  47. package/dist/client/commands/deposit.js.map +1 -1
  48. package/dist/client/commands/printReserve.d.ts +1 -1
  49. package/dist/client/commands/printReserve.d.ts.map +1 -1
  50. package/dist/client/commands/printReserve.js +2 -4
  51. package/dist/client/commands/printReserve.js.map +1 -1
  52. package/dist/client/commands/repay.d.ts +1 -1
  53. package/dist/client/commands/repay.d.ts.map +1 -1
  54. package/dist/client/commands/repay.js +12 -2
  55. package/dist/client/commands/repay.js.map +1 -1
  56. package/dist/client/commands/withdraw.d.ts +1 -1
  57. package/dist/client/commands/withdraw.d.ts.map +1 -1
  58. package/dist/client/commands/withdraw.js +11 -2
  59. package/dist/client/commands/withdraw.js.map +1 -1
  60. package/dist/lending_operations/repay_with_collateral_calcs.d.ts +1 -1
  61. package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
  62. package/dist/lending_operations/repay_with_collateral_calcs.js +2 -2
  63. package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
  64. package/dist/lending_operations/repay_with_collateral_operations.d.ts +4 -4
  65. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  66. package/dist/lending_operations/repay_with_collateral_operations.js +43 -10
  67. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  68. package/dist/lending_operations/swap_collateral_operations.d.ts +4 -4
  69. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  70. package/dist/lending_operations/swap_collateral_operations.js +40 -21
  71. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  72. package/dist/leverage/operations.d.ts +6 -6
  73. package/dist/leverage/operations.d.ts.map +1 -1
  74. package/dist/leverage/operations.js +167 -52
  75. package/dist/leverage/operations.js.map +1 -1
  76. package/dist/leverage/types.d.ts +2 -2
  77. package/dist/leverage/types.d.ts.map +1 -1
  78. package/dist/manager/client_kamino_manager.js +0 -2
  79. package/dist/manager/client_kamino_manager.js.map +1 -1
  80. package/dist/obligation_orders/price_based.js +5 -3
  81. package/dist/obligation_orders/price_based.js.map +1 -1
  82. package/dist/utils/Logger.d.ts +14 -0
  83. package/dist/utils/Logger.d.ts.map +1 -0
  84. package/dist/utils/Logger.js +12 -0
  85. package/dist/utils/Logger.js.map +1 -0
  86. package/dist/utils/ObligationType.d.ts +33 -1
  87. package/dist/utils/ObligationType.d.ts.map +1 -1
  88. package/dist/utils/ObligationType.js +81 -2
  89. package/dist/utils/ObligationType.js.map +1 -1
  90. package/dist/utils/api.d.ts +13 -4
  91. package/dist/utils/api.d.ts.map +1 -1
  92. package/dist/utils/api.js +33 -31
  93. package/dist/utils/api.js.map +1 -1
  94. package/dist/utils/userMetadata.d.ts +2 -2
  95. package/dist/utils/userMetadata.d.ts.map +1 -1
  96. package/dist/utils/userMetadata.js +49 -25
  97. package/dist/utils/userMetadata.js.map +1 -1
  98. package/dist/utils/validations.d.ts +1 -0
  99. package/dist/utils/validations.d.ts.map +1 -1
  100. package/dist/utils/validations.js +5 -0
  101. package/dist/utils/validations.js.map +1 -1
  102. package/package.json +1 -1
  103. package/src/classes/action.ts +346 -372
  104. package/src/classes/actionTypes.ts +295 -0
  105. package/src/classes/configItems.ts +1 -99
  106. package/src/classes/farm_utils.ts +5 -1
  107. package/src/classes/market.ts +74 -31
  108. package/src/classes/obligation.ts +54 -53
  109. package/src/classes/obligationOrder.ts +6 -3
  110. package/src/classes/reserve.ts +118 -122
  111. package/src/classes/shared.ts +4 -2
  112. package/src/classes/vault.ts +160 -7
  113. package/src/classes/vault_types.ts +2 -1
  114. package/src/client/client.ts +17 -18
  115. package/src/client/commands/borrow.ts +10 -9
  116. package/src/client/commands/deposit.ts +10 -9
  117. package/src/client/commands/printReserve.ts +2 -4
  118. package/src/client/commands/repay.ts +11 -10
  119. package/src/client/commands/withdraw.ts +15 -9
  120. package/src/lending_operations/repay_with_collateral_calcs.ts +3 -4
  121. package/src/lending_operations/repay_with_collateral_operations.ts +40 -38
  122. package/src/lending_operations/swap_collateral_operations.ts +47 -41
  123. package/src/leverage/operations.ts +168 -129
  124. package/src/leverage/types.ts +2 -2
  125. package/src/manager/client_kamino_manager.ts +0 -2
  126. package/src/obligation_orders/price_based.ts +7 -5
  127. package/src/utils/Logger.ts +14 -0
  128. package/src/utils/ObligationType.ts +92 -1
  129. package/src/utils/api.ts +56 -33
  130. package/src/utils/userMetadata.ts +64 -30
  131. package/src/utils/validations.ts +5 -0
@@ -347,7 +347,7 @@ export class KaminoObligation {
347
347
  }
348
348
 
349
349
  getBorrowAmountByReserve(reserve: KaminoReserve): Decimal {
350
- const amountLamports = this.getBorrowByMint(reserve.getLiquidityMint())?.amount ?? new Decimal(0);
350
+ const amountLamports = this.getBorrowByReserve(reserve.address)?.amount ?? new Decimal(0);
351
351
  return amountLamports.div(reserve.getMintFactor());
352
352
  }
353
353
 
@@ -361,7 +361,7 @@ export class KaminoObligation {
361
361
  }
362
362
 
363
363
  getDepositAmountByReserve(reserve: KaminoReserve): Decimal {
364
- const amountLamports = this.getDepositByMint(reserve.getLiquidityMint())?.amount ?? new Decimal(0);
364
+ const amountLamports = this.getDepositByReserve(reserve.address)?.amount ?? new Decimal(0);
365
365
  return amountLamports.div(reserve.getMintFactor());
366
366
  }
367
367
 
@@ -595,8 +595,8 @@ export class KaminoObligation {
595
595
  amountCollateral?: Decimal;
596
596
  amountDebt?: Decimal;
597
597
  action: ActionType;
598
- mintCollateral?: Address;
599
- mintDebt?: Address;
598
+ collateralReserveAddress?: Address;
599
+ debtReserveAddress?: Address;
600
600
  market: KaminoMarket;
601
601
  reserves: Map<Address, KaminoReserve>;
602
602
  slot: Slot;
@@ -606,18 +606,15 @@ export class KaminoObligation {
606
606
  deposits: Map<Address, Position>;
607
607
  borrows: Map<Address, Position>;
608
608
  } {
609
- const { amountCollateral, amountDebt, action, mintCollateral, mintDebt, market } = params;
609
+ const { amountCollateral, amountDebt, action, collateralReserveAddress, debtReserveAddress, market } = params;
610
610
  let newStats = { ...this.refreshedStats };
611
611
 
612
- const collateralReservePk = mintCollateral ? market.getReserveByMint(mintCollateral)!.address : undefined;
613
- const debtReservePk = mintDebt ? market.getReserveByMint(mintDebt)!.address : undefined;
614
-
615
612
  const additionalReserves: Address[] = [];
616
- if (collateralReservePk !== undefined) {
617
- additionalReserves.push(collateralReservePk);
613
+ if (collateralReserveAddress !== undefined) {
614
+ additionalReserves.push(collateralReserveAddress);
618
615
  }
619
- if (debtReservePk !== undefined) {
620
- additionalReserves.push(debtReservePk);
616
+ if (debtReserveAddress !== undefined) {
617
+ additionalReserves.push(debtReserveAddress);
621
618
  }
622
619
 
623
620
  const { collateralExchangeRates } = KaminoObligation.getRatesForObligation(
@@ -636,8 +633,8 @@ export class KaminoObligation {
636
633
  // so we have to recalculate the entire position, not just an updated deposit or borrow
637
634
  // as both LTVs and borrow factors can change, affecting all calcs
638
635
 
639
- const debtReserveCumulativeBorrowRate = mintDebt
640
- ? market.getReserveByMint(mintDebt)!.getCumulativeBorrowRate()
636
+ const debtReserveCumulativeBorrowRate = debtReserveAddress
637
+ ? market.getExistingReserveByAddress(debtReserveAddress).getCumulativeBorrowRate()
641
638
  : undefined;
642
639
 
643
640
  let newObligationDeposits = this.state.deposits;
@@ -645,13 +642,13 @@ export class KaminoObligation {
645
642
 
646
643
  switch (action) {
647
644
  case 'deposit': {
648
- if (amountCollateral === undefined || mintCollateral === undefined) {
649
- throw Error('amountCollateral & mintCollateral are required for deposit action');
645
+ if (amountCollateral === undefined || collateralReserveAddress === undefined) {
646
+ throw Error('amountCollateral & collateralReserveAddress are required for deposit action');
650
647
  }
651
648
  newObligationDeposits = this.simulateDepositChange(
652
649
  this.state.deposits,
653
650
  {
654
- reserveAddress: collateralReservePk!,
651
+ reserveAddress: collateralReserveAddress,
655
652
  amountChangeLamports: amountCollateral,
656
653
  },
657
654
  collateralExchangeRates
@@ -659,14 +656,14 @@ export class KaminoObligation {
659
656
  break;
660
657
  }
661
658
  case 'borrow': {
662
- if (amountDebt === undefined || mintDebt === undefined) {
663
- throw Error('amountDebt & mintDebt are required for borrow action');
659
+ if (amountDebt === undefined || debtReserveAddress === undefined) {
660
+ throw Error('amountDebt & debtReserveAddress are required for borrow action');
664
661
  }
665
662
 
666
663
  newObligationBorrows = this.simulateBorrowChange(
667
664
  this.state.borrows,
668
665
  {
669
- reserveAddress: debtReservePk!,
666
+ reserveAddress: debtReserveAddress,
670
667
  amountChangeLamports: amountDebt,
671
668
  },
672
669
  debtReserveCumulativeBorrowRate!
@@ -674,14 +671,14 @@ export class KaminoObligation {
674
671
  break;
675
672
  }
676
673
  case 'repay': {
677
- if (amountDebt === undefined || mintDebt === undefined) {
678
- throw Error('amountDebt & mintDebt are required for repay action');
674
+ if (amountDebt === undefined || debtReserveAddress === undefined) {
675
+ throw Error('amountDebt & debtReserveAddress are required for repay action');
679
676
  }
680
677
 
681
678
  newObligationBorrows = this.simulateBorrowChange(
682
679
  this.state.borrows,
683
680
  {
684
- reserveAddress: debtReservePk!,
681
+ reserveAddress: debtReserveAddress,
685
682
  amountChangeLamports: amountDebt.neg(),
686
683
  },
687
684
  debtReserveCumulativeBorrowRate!
@@ -691,13 +688,13 @@ export class KaminoObligation {
691
688
  }
692
689
 
693
690
  case 'withdraw': {
694
- if (amountCollateral === undefined || mintCollateral === undefined) {
695
- throw Error('amountCollateral & mintCollateral are required for withdraw action');
691
+ if (amountCollateral === undefined || collateralReserveAddress === undefined) {
692
+ throw Error('amountCollateral & collateralReserveAddress are required for withdraw action');
696
693
  }
697
694
  newObligationDeposits = this.simulateDepositChange(
698
695
  this.state.deposits,
699
696
  {
700
- reserveAddress: collateralReservePk!,
697
+ reserveAddress: collateralReserveAddress,
701
698
  amountChangeLamports: amountCollateral.neg(),
702
699
  },
703
700
  collateralExchangeRates
@@ -708,15 +705,17 @@ export class KaminoObligation {
708
705
  if (
709
706
  amountCollateral === undefined ||
710
707
  amountDebt === undefined ||
711
- mintCollateral === undefined ||
712
- mintDebt === undefined
708
+ collateralReserveAddress === undefined ||
709
+ debtReserveAddress === undefined
713
710
  ) {
714
- throw Error('amountColl & amountDebt & mintCollateral & mintDebt are required for depositAndBorrow action');
711
+ throw Error(
712
+ 'amountColl & amountDebt & collateralReserveAddress & debtReserveAddress are required for depositAndBorrow action'
713
+ );
715
714
  }
716
715
  newObligationDeposits = this.simulateDepositChange(
717
716
  this.state.deposits,
718
717
  {
719
- reserveAddress: collateralReservePk!,
718
+ reserveAddress: collateralReserveAddress,
720
719
  amountChangeLamports: amountCollateral,
721
720
  },
722
721
  collateralExchangeRates
@@ -725,7 +724,7 @@ export class KaminoObligation {
725
724
  newObligationBorrows = this.simulateBorrowChange(
726
725
  this.state.borrows,
727
726
  {
728
- reserveAddress: debtReservePk!,
727
+ reserveAddress: debtReserveAddress,
729
728
  amountChangeLamports: amountDebt,
730
729
  },
731
730
  debtReserveCumulativeBorrowRate!
@@ -736,15 +735,17 @@ export class KaminoObligation {
736
735
  if (
737
736
  amountCollateral === undefined ||
738
737
  amountDebt === undefined ||
739
- mintCollateral === undefined ||
740
- mintDebt === undefined
738
+ collateralReserveAddress === undefined ||
739
+ debtReserveAddress === undefined
741
740
  ) {
742
- throw Error('amountColl & amountDebt & mintCollateral & mintDebt are required for repayAndWithdraw action');
741
+ throw Error(
742
+ 'amountColl & amountDebt & collateralReserveAddress & debtReserveAddress are required for repayAndWithdraw action'
743
+ );
743
744
  }
744
745
  newObligationDeposits = this.simulateDepositChange(
745
746
  this.state.deposits,
746
747
  {
747
- reserveAddress: collateralReservePk!,
748
+ reserveAddress: collateralReserveAddress,
748
749
  amountChangeLamports: amountCollateral.neg(),
749
750
  },
750
751
  collateralExchangeRates
@@ -752,7 +753,7 @@ export class KaminoObligation {
752
753
  newObligationBorrows = this.simulateBorrowChange(
753
754
  this.state.borrows,
754
755
  {
755
- reserveAddress: debtReservePk!,
756
+ reserveAddress: debtReserveAddress,
756
757
  amountChangeLamports: amountDebt.neg(),
757
758
  },
758
759
  debtReserveCumulativeBorrowRate!
@@ -1186,7 +1187,7 @@ export class KaminoObligation {
1186
1187
  not a reserve-specific, caps-specific, liquidity-specific function.
1187
1188
 
1188
1189
  * @param market - The KaminoMarket instance.
1189
- * @param liquidityMint - The liquidity mint Address.
1190
+ * @param liquidityReserveAddress - The liquidity reserve Address.
1190
1191
  * @param slot - The slot number.
1191
1192
  * @param elevationGroup - The elevation group number (default: this.state.elevationGroup).
1192
1193
  * @returns The borrow power as a Decimal.
@@ -1194,11 +1195,11 @@ export class KaminoObligation {
1194
1195
  */
1195
1196
  getBorrowPower(
1196
1197
  market: KaminoMarket,
1197
- liquidityMint: Address,
1198
+ liquidityReserveAddress: Address,
1198
1199
  slot: Slot,
1199
1200
  elevationGroup: number = this.state.elevationGroup
1200
1201
  ): Decimal {
1201
- const reserve = market.getReserveByMint(liquidityMint);
1202
+ const reserve = market.getReserveByAddress(liquidityReserveAddress);
1202
1203
  if (!reserve) {
1203
1204
  throw new Error('Reserve not found');
1204
1205
  }
@@ -1265,7 +1266,7 @@ export class KaminoObligation {
1265
1266
  and a specific reserve, until it hits max LTV and given available liquidity and caps.
1266
1267
 
1267
1268
  * @param market - The KaminoMarket instance.
1268
- * @param liquidityMint - The liquidity mint Address.
1269
+ * @param liquidityReserveAddress - The liquidity reserve Address.
1269
1270
  * @param slot - The slot number.
1270
1271
  * @param elevationGroup - The elevation group number (default: this.state.elevationGroup).
1271
1272
  * @returns The maximum borrow amount as a Decimal.
@@ -1273,11 +1274,11 @@ export class KaminoObligation {
1273
1274
  */
1274
1275
  getMaxBorrowAmountV2(
1275
1276
  market: KaminoMarket,
1276
- liquidityMint: Address,
1277
+ liquidityReserveAddress: Address,
1277
1278
  slot: Slot,
1278
1279
  elevationGroup: number = this.state.elevationGroup
1279
1280
  ): Decimal {
1280
- const reserve = market.getReserveByMint(liquidityMint);
1281
+ const reserve = market.getReserveByAddress(liquidityReserveAddress);
1281
1282
  if (!reserve) {
1282
1283
  throw new Error('Reserve not found');
1283
1284
  }
@@ -1287,7 +1288,7 @@ export class KaminoObligation {
1287
1288
  [elevationGroup],
1288
1289
  Array.from(this.deposits.keys())
1289
1290
  )[0];
1290
- const maxBorrowAmount = this.getBorrowPower(market, liquidityMint, slot, elevationGroup);
1291
+ const maxBorrowAmount = this.getBorrowPower(market, liquidityReserveAddress, slot, elevationGroup);
1291
1292
 
1292
1293
  if (elevationGroup === this.state.elevationGroup) {
1293
1294
  return Decimal.min(maxBorrowAmount, liquidityAvailable);
@@ -1304,7 +1305,7 @@ export class KaminoObligation {
1304
1305
  the new borrow power after the deposit, without overriding the obligation itself.
1305
1306
 
1306
1307
  * @param market - The KaminoMarket instance.
1307
- * @param liquidityMint - The liquidity mint Address.
1308
+ * @param liquidityReserveAddress - The liquidity reserve Address.
1308
1309
  * @param slot - The slot number.
1309
1310
  * @param elevationGroup - The elevation group number (default: this.state.elevationGroup).
1310
1311
  * @returns The maximum borrow amount as a Decimal.
@@ -1312,7 +1313,7 @@ export class KaminoObligation {
1312
1313
  */
1313
1314
  getMaxBorrowAmountV2WithDeposit(
1314
1315
  market: KaminoMarket,
1315
- liquidityMint: Address,
1316
+ liquidityReserveAddress: Address,
1316
1317
  slot: Slot,
1317
1318
  elevationGroup: number = this.state.elevationGroup,
1318
1319
  depositAmountLamports: Decimal,
@@ -1326,7 +1327,7 @@ export class KaminoObligation {
1326
1327
  ];
1327
1328
  const obligationWithDeposit = this.withPositionChanges(market, slot, depositChanges);
1328
1329
 
1329
- return obligationWithDeposit.getMaxBorrowAmountV2(market, liquidityMint, slot, elevationGroup);
1330
+ return obligationWithDeposit.getMaxBorrowAmountV2(market, liquidityReserveAddress, slot, elevationGroup);
1330
1331
  }
1331
1332
 
1332
1333
  /*
@@ -1406,11 +1407,11 @@ export class KaminoObligation {
1406
1407
  /* Deprecated function, also broken */
1407
1408
  getMaxBorrowAmount(
1408
1409
  market: KaminoMarket,
1409
- liquidityMint: Address,
1410
+ liquidityReserveAddress: Address,
1410
1411
  slot: Slot,
1411
1412
  requestElevationGroup: boolean
1412
1413
  ): Decimal {
1413
- const reserve = market.getReserveByMint(liquidityMint);
1414
+ const reserve = market.getReserveByAddress(liquidityReserveAddress);
1414
1415
 
1415
1416
  if (!reserve) {
1416
1417
  throw new Error('Reserve not found');
@@ -1525,8 +1526,8 @@ export class KaminoObligation {
1525
1526
  return Decimal.max(new Decimal(0), maxBorrowAmount);
1526
1527
  }
1527
1528
 
1528
- getMaxWithdrawAmount(market: KaminoMarket, tokenMint: Address, slot: Slot): Decimal {
1529
- const depositReserve = market.getReserveByMint(tokenMint);
1529
+ getMaxWithdrawAmount(market: KaminoMarket, depositReserveAddress: Address, slot: Slot): Decimal {
1530
+ const depositReserve = market.getReserveByAddress(depositReserveAddress);
1530
1531
 
1531
1532
  if (!depositReserve) {
1532
1533
  throw new Error('Reserve not found');
@@ -1585,7 +1586,7 @@ export class KaminoObligation {
1585
1586
  * the new withdraw power after the repay, without overriding the obligation itself.
1586
1587
  *
1587
1588
  * @param market - The KaminoMarket instance.
1588
- * @param tokenMint - The liquidity mint Address.
1589
+ * @param depositReserveAddress - The liquidity (deposit) reserve Address.
1589
1590
  * @param slot - The slot number.
1590
1591
  * @param repayAmountLamports - The amount to repay in lamports (use U64_MAX for full repay).
1591
1592
  * @param repayReserveAddress - The reserve address of the borrow being repaid.
@@ -1594,7 +1595,7 @@ export class KaminoObligation {
1594
1595
  */
1595
1596
  getMaxWithdrawAmountWithRepay(
1596
1597
  market: KaminoMarket,
1597
- tokenMint: Address,
1598
+ depositReserveAddress: Address,
1598
1599
  slot: Slot,
1599
1600
  repayAmountLamports: Decimal,
1600
1601
  repayReserveAddress: Address
@@ -1615,7 +1616,7 @@ export class KaminoObligation {
1615
1616
  ];
1616
1617
  const obligationWithRepay = this.withPositionChanges(market, slot, undefined, borrowChanges);
1617
1618
 
1618
- return obligationWithRepay.getMaxWithdrawAmount(market, tokenMint, slot);
1619
+ return obligationWithRepay.getMaxWithdrawAmount(market, depositReserveAddress, slot);
1619
1620
  }
1620
1621
 
1621
1622
  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.getBorrowByMint(maxRepay.mint)!;
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.mintAddress.localeCompare(rightDeposit.mintAddress);
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.mint !== position.mintAddress) {
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