@kamino-finance/klend-sdk 5.10.28-beta.0 → 5.11.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.
Files changed (169) hide show
  1. package/dist/classes/action.d.ts +22 -14
  2. package/dist/classes/action.d.ts.map +1 -1
  3. package/dist/classes/action.js +585 -132
  4. package/dist/classes/action.js.map +1 -1
  5. package/dist/classes/index.d.ts +0 -3
  6. package/dist/classes/index.d.ts.map +1 -1
  7. package/dist/classes/index.js +0 -3
  8. package/dist/classes/index.js.map +1 -1
  9. package/dist/classes/manager.js +1 -1
  10. package/dist/classes/manager.js.map +1 -1
  11. package/dist/classes/obligation.d.ts +1 -6
  12. package/dist/classes/obligation.d.ts.map +1 -1
  13. package/dist/classes/obligation.js +2 -4
  14. package/dist/classes/obligation.js.map +1 -1
  15. package/dist/classes/types.d.ts +1 -13
  16. package/dist/classes/types.d.ts.map +1 -1
  17. package/dist/idl.json +71 -5
  18. package/dist/idl_codegen/accounts/Obligation.d.ts +6 -0
  19. package/dist/idl_codegen/accounts/Obligation.d.ts.map +1 -1
  20. package/dist/idl_codegen/accounts/Obligation.js +8 -1
  21. package/dist/idl_codegen/accounts/Obligation.js.map +1 -1
  22. package/dist/idl_codegen/errors/custom.d.ts +35 -3
  23. package/dist/idl_codegen/errors/custom.d.ts.map +1 -1
  24. package/dist/idl_codegen/errors/custom.js +61 -5
  25. package/dist/idl_codegen/errors/custom.js.map +1 -1
  26. package/dist/idl_codegen/instructions/repayAndWithdrawAndRedeem.d.ts +9 -0
  27. package/dist/idl_codegen/instructions/repayAndWithdrawAndRedeem.d.ts.map +1 -1
  28. package/dist/idl_codegen/instructions/repayAndWithdrawAndRedeem.js +22 -1
  29. package/dist/idl_codegen/instructions/repayAndWithdrawAndRedeem.js.map +1 -1
  30. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js +1 -1
  31. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js.map +1 -1
  32. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateralV2.js +1 -1
  33. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateralV2.js.map +1 -1
  34. package/dist/idl_codegen/zero_padding/ObligationZP.d.ts +2 -0
  35. package/dist/idl_codegen/zero_padding/ObligationZP.d.ts.map +1 -1
  36. package/dist/idl_codegen/zero_padding/ObligationZP.js +6 -1
  37. package/dist/idl_codegen/zero_padding/ObligationZP.js.map +1 -1
  38. package/dist/lending_operations/repay_with_collateral_operations.d.ts +3 -2
  39. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  40. package/dist/lending_operations/repay_with_collateral_operations.js +7 -6
  41. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  42. package/dist/lending_operations/swap_collateral_operations.d.ts +5 -0
  43. package/dist/lending_operations/swap_collateral_operations.d.ts.map +1 -1
  44. package/dist/lending_operations/swap_collateral_operations.js +4 -2
  45. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  46. package/dist/leverage/calcs.d.ts +1 -1
  47. package/dist/leverage/calcs.d.ts.map +1 -1
  48. package/dist/leverage/calcs.js +10 -12
  49. package/dist/leverage/calcs.js.map +1 -1
  50. package/dist/leverage/operations.d.ts +8 -8
  51. package/dist/leverage/operations.d.ts.map +1 -1
  52. package/dist/leverage/operations.js +27 -24
  53. package/dist/leverage/operations.js.map +1 -1
  54. package/dist/leverage/types.d.ts +4 -1
  55. package/dist/leverage/types.d.ts.map +1 -1
  56. package/dist/utils/seeds.d.ts +5 -0
  57. package/dist/utils/seeds.d.ts.map +1 -1
  58. package/dist/utils/seeds.js +10 -1
  59. package/dist/utils/seeds.js.map +1 -1
  60. package/dist/utils/userMetadata.d.ts.map +1 -1
  61. package/dist/utils/userMetadata.js +6 -9
  62. package/dist/utils/userMetadata.js.map +1 -1
  63. package/package.json +4 -4
  64. package/src/classes/action.ts +749 -146
  65. package/src/classes/index.ts +0 -3
  66. package/src/classes/manager.ts +1 -1
  67. package/src/classes/obligation.ts +4 -8
  68. package/src/classes/types.ts +1 -17
  69. package/src/client.ts +7 -3
  70. package/src/idl.json +71 -5
  71. package/src/idl_codegen/accounts/Obligation.ts +12 -1
  72. package/src/idl_codegen/errors/custom.ts +66 -4
  73. package/src/idl_codegen/instructions/repayAndWithdrawAndRedeem.ts +31 -1
  74. package/src/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.ts +1 -1
  75. package/src/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateralV2.ts +1 -1
  76. package/src/idl_codegen/zero_padding/ObligationZP.ts +6 -1
  77. package/src/lending_operations/repay_with_collateral_operations.ts +11 -3
  78. package/src/lending_operations/swap_collateral_operations.ts +11 -0
  79. package/src/leverage/calcs.ts +13 -16
  80. package/src/leverage/operations.ts +38 -11
  81. package/src/leverage/types.ts +4 -1
  82. package/src/utils/seeds.ts +13 -0
  83. package/src/utils/userMetadata.ts +19 -24
  84. package/dist/classes/stakePool.d.ts +0 -8
  85. package/dist/classes/stakePool.d.ts.map +0 -1
  86. package/dist/classes/stakePool.js +0 -18
  87. package/dist/classes/stakePool.js.map +0 -1
  88. package/dist/classes/standardStakePool.d.ts +0 -255
  89. package/dist/classes/standardStakePool.d.ts.map +0 -1
  90. package/dist/classes/standardStakePool.js +0 -275
  91. package/dist/classes/standardStakePool.js.map +0 -1
  92. package/dist/classes/unstakingPool.d.ts +0 -111
  93. package/dist/classes/unstakingPool.d.ts.map +0 -1
  94. package/dist/classes/unstakingPool.js +0 -437
  95. package/dist/classes/unstakingPool.js.map +0 -1
  96. package/dist/idl_codegen_unstaking_pool/accounts/PoolState.d.ts +0 -50
  97. package/dist/idl_codegen_unstaking_pool/accounts/PoolState.d.ts.map +0 -1
  98. package/dist/idl_codegen_unstaking_pool/accounts/PoolState.js +0 -156
  99. package/dist/idl_codegen_unstaking_pool/accounts/PoolState.js.map +0 -1
  100. package/dist/idl_codegen_unstaking_pool/accounts/index.d.ts +0 -3
  101. package/dist/idl_codegen_unstaking_pool/accounts/index.d.ts.map +0 -1
  102. package/dist/idl_codegen_unstaking_pool/accounts/index.js +0 -6
  103. package/dist/idl_codegen_unstaking_pool/accounts/index.js.map +0 -1
  104. package/dist/idl_codegen_unstaking_pool/errors/anchor.d.ts +0 -435
  105. package/dist/idl_codegen_unstaking_pool/errors/anchor.d.ts.map +0 -1
  106. package/dist/idl_codegen_unstaking_pool/errors/anchor.js +0 -767
  107. package/dist/idl_codegen_unstaking_pool/errors/anchor.js.map +0 -1
  108. package/dist/idl_codegen_unstaking_pool/errors/custom.d.ts +0 -163
  109. package/dist/idl_codegen_unstaking_pool/errors/custom.d.ts.map +0 -1
  110. package/dist/idl_codegen_unstaking_pool/errors/custom.js +0 -290
  111. package/dist/idl_codegen_unstaking_pool/errors/custom.js.map +0 -1
  112. package/dist/idl_codegen_unstaking_pool/errors/index.d.ts +0 -6
  113. package/dist/idl_codegen_unstaking_pool/errors/index.d.ts.map +0 -1
  114. package/dist/idl_codegen_unstaking_pool/errors/index.js +0 -75
  115. package/dist/idl_codegen_unstaking_pool/errors/index.js.map +0 -1
  116. package/dist/idl_codegen_unstaking_pool/instructions/burn.d.ts +0 -21
  117. package/dist/idl_codegen_unstaking_pool/instructions/burn.d.ts.map +0 -1
  118. package/dist/idl_codegen_unstaking_pool/instructions/burn.js +0 -73
  119. package/dist/idl_codegen_unstaking_pool/instructions/burn.js.map +0 -1
  120. package/dist/idl_codegen_unstaking_pool/instructions/collect.d.ts +0 -16
  121. package/dist/idl_codegen_unstaking_pool/instructions/collect.d.ts.map +0 -1
  122. package/dist/idl_codegen_unstaking_pool/instructions/collect.js +0 -29
  123. package/dist/idl_codegen_unstaking_pool/instructions/collect.js.map +0 -1
  124. package/dist/idl_codegen_unstaking_pool/instructions/index.d.ts +0 -11
  125. package/dist/idl_codegen_unstaking_pool/instructions/index.d.ts.map +0 -1
  126. package/dist/idl_codegen_unstaking_pool/instructions/index.js +0 -14
  127. package/dist/idl_codegen_unstaking_pool/instructions/index.js.map +0 -1
  128. package/dist/idl_codegen_unstaking_pool/instructions/initializePool.d.ts +0 -15
  129. package/dist/idl_codegen_unstaking_pool/instructions/initializePool.d.ts.map +0 -1
  130. package/dist/idl_codegen_unstaking_pool/instructions/initializePool.js +0 -28
  131. package/dist/idl_codegen_unstaking_pool/instructions/initializePool.js.map +0 -1
  132. package/dist/idl_codegen_unstaking_pool/instructions/mint.d.ts +0 -21
  133. package/dist/idl_codegen_unstaking_pool/instructions/mint.d.ts.map +0 -1
  134. package/dist/idl_codegen_unstaking_pool/instructions/mint.js +0 -77
  135. package/dist/idl_codegen_unstaking_pool/instructions/mint.js.map +0 -1
  136. package/dist/idl_codegen_unstaking_pool/instructions/updatePoolConfig.d.ts +0 -13
  137. package/dist/idl_codegen_unstaking_pool/instructions/updatePoolConfig.d.ts.map +0 -1
  138. package/dist/idl_codegen_unstaking_pool/instructions/updatePoolConfig.js +0 -58
  139. package/dist/idl_codegen_unstaking_pool/instructions/updatePoolConfig.js.map +0 -1
  140. package/dist/idl_codegen_unstaking_pool/programId.d.ts +0 -4
  141. package/dist/idl_codegen_unstaking_pool/programId.d.ts.map +0 -1
  142. package/dist/idl_codegen_unstaking_pool/programId.js +0 -9
  143. package/dist/idl_codegen_unstaking_pool/programId.js.map +0 -1
  144. package/dist/idl_codegen_unstaking_pool/types/PoolConfigField.d.ts +0 -32
  145. package/dist/idl_codegen_unstaking_pool/types/PoolConfigField.d.ts.map +0 -1
  146. package/dist/idl_codegen_unstaking_pool/types/PoolConfigField.js +0 -105
  147. package/dist/idl_codegen_unstaking_pool/types/PoolConfigField.js.map +0 -1
  148. package/dist/idl_codegen_unstaking_pool/types/index.d.ts +0 -5
  149. package/dist/idl_codegen_unstaking_pool/types/index.d.ts.map +0 -1
  150. package/dist/idl_codegen_unstaking_pool/types/index.js +0 -39
  151. package/dist/idl_codegen_unstaking_pool/types/index.js.map +0 -1
  152. package/src/classes/stakePool.ts +0 -21
  153. package/src/classes/standardStakePool.ts +0 -361
  154. package/src/classes/unstakingPool.ts +0 -565
  155. package/src/idl_codegen_unstaking_pool/accounts/PoolState.ts +0 -163
  156. package/src/idl_codegen_unstaking_pool/accounts/index.ts +0 -2
  157. package/src/idl_codegen_unstaking_pool/errors/anchor.ts +0 -764
  158. package/src/idl_codegen_unstaking_pool/errors/custom.ts +0 -288
  159. package/src/idl_codegen_unstaking_pool/errors/index.ts +0 -49
  160. package/src/idl_codegen_unstaking_pool/instructions/burn.ts +0 -60
  161. package/src/idl_codegen_unstaking_pool/instructions/collect.ts +0 -43
  162. package/src/idl_codegen_unstaking_pool/instructions/index.ts +0 -10
  163. package/src/idl_codegen_unstaking_pool/instructions/initializePool.ts +0 -41
  164. package/src/idl_codegen_unstaking_pool/instructions/mint.ts +0 -64
  165. package/src/idl_codegen_unstaking_pool/instructions/updatePoolConfig.ts +0 -40
  166. package/src/idl_codegen_unstaking_pool/programId.ts +0 -7
  167. package/src/idl_codegen_unstaking_pool/types/PoolConfigField.ts +0 -85
  168. package/src/idl_codegen_unstaking_pool/types/index.ts +0 -6
  169. package/src/idl_unstaking_pool.json +0 -456
@@ -9,6 +9,3 @@ export * from './jupiterPerps';
9
9
  export * from './manager';
10
10
  export * from './vault';
11
11
  export * from './fraction';
12
- export * from './standardStakePool';
13
- export * from './stakePool';
14
- export * from './unstakingPool';
@@ -197,7 +197,7 @@ export class KaminoManager {
197
197
  reserveAccount.publicKey,
198
198
  params.assetConfig.getReserveConfig(),
199
199
  undefined,
200
- true
200
+ false
201
201
  );
202
202
 
203
203
  const txnIxns: TransactionInstruction[][] = [];
@@ -105,7 +105,6 @@ export class KaminoObligation {
105
105
  ) {
106
106
  this.obligationAddress = obligationAddress;
107
107
  this.state = obligation;
108
-
109
108
  const { borrows, deposits, refreshedStats } = this.calculatePositions(
110
109
  market,
111
110
  obligation.deposits,
@@ -785,23 +784,20 @@ export class KaminoObligation {
785
784
  return new Decimal(0);
786
785
  };
787
786
 
788
- static getOraclePx = (reserve: KaminoReserve) => {
789
- return reserve.getOracleMarketPrice();
790
- };
791
-
792
- calculatePositions(
787
+ private calculatePositions(
793
788
  market: KaminoMarket,
794
789
  obligationDeposits: ObligationCollateral[],
795
790
  obligationBorrows: ObligationLiquidity[],
796
791
  elevationGroup: number,
797
792
  collateralExchangeRates: Map<PublicKey, Decimal>,
798
- cumulativeBorrowRates: Map<PublicKey, Decimal> | null,
799
- getOraclePx: (reserve: KaminoReserve) => Decimal = KaminoObligation.getOraclePx
793
+ cumulativeBorrowRates: Map<PublicKey, Decimal> | null
800
794
  ): {
801
795
  borrows: Map<PublicKey, Position>;
802
796
  deposits: Map<PublicKey, Position>;
803
797
  refreshedStats: ObligationStats;
804
798
  } {
799
+ const getOraclePx = (reserve: KaminoReserve) => reserve.getOracleMarketPrice();
800
+
805
801
  const depositStatsOraclePrice = KaminoObligation.calculateObligationDeposits(
806
802
  market,
807
803
  obligationDeposits,
@@ -1,4 +1,4 @@
1
- import { Keypair, TransactionInstruction } from '@solana/web3.js';
1
+ import { TransactionInstruction } from '@solana/web3.js';
2
2
  import Decimal from 'decimal.js/decimal';
3
3
 
4
4
  /** the populateLUTIxs should be executed in a separate transaction as we cannot create and populate a lookup table in the same tx */
@@ -54,19 +54,3 @@ export type ReserveAllocationOverview = {
54
54
  tokenAllocationCap: Decimal;
55
55
  ctokenAllocation: Decimal;
56
56
  };
57
-
58
- // Unstaking pool types
59
- export type InitPoolIxs = {
60
- initPoolIxs: TransactionInstruction[];
61
- populateLUTIxs: TransactionInstruction[];
62
- };
63
-
64
- export type UpdatePoolConfigIxs = {
65
- updatePoolConfigIx: TransactionInstruction;
66
- updateLUTIxs: TransactionInstruction[];
67
- };
68
-
69
- export type MintIxs = {
70
- mintIx: TransactionInstruction;
71
- additionalSigners: Keypair[];
72
- };
package/src/client.ts CHANGED
@@ -308,7 +308,8 @@ async function deposit(connection: Connection, wallet: Keypair, token: string, d
308
308
  depositAmount,
309
309
  kaminoMarket.getReserveBySymbol(token)!.getLiquidityMint(),
310
310
  wallet.publicKey,
311
- new VanillaObligation(STAGING_LENDING_MARKET)
311
+ new VanillaObligation(STAGING_LENDING_MARKET),
312
+ true
312
313
  );
313
314
  console.log('User obligation', kaminoAction.obligation!.obligationAddress.toString());
314
315
 
@@ -328,7 +329,8 @@ async function withdraw(connection: Connection, wallet: Keypair, token: string,
328
329
  depositAmount,
329
330
  kaminoMarket.getReserveBySymbol(token)!.getLiquidityMint(),
330
331
  wallet.publicKey,
331
- new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET))
332
+ new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET)),
333
+ true
332
334
  );
333
335
  console.log('User obligation', kaminoAction.obligation!.obligationAddress.toString());
334
336
 
@@ -348,7 +350,8 @@ async function borrow(connection: Connection, wallet: Keypair, token: string, bo
348
350
  borrowAmount,
349
351
  kaminoMarket.getReserveBySymbol(token)!.getLiquidityMint(),
350
352
  wallet.publicKey,
351
- new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET))
353
+ new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET)),
354
+ true
352
355
  );
353
356
  console.log('User obligation', kaminoAction.obligation!.obligationAddress.toString());
354
357
 
@@ -369,6 +372,7 @@ async function repay(connection: Connection, wallet: Keypair, token: string, bor
369
372
  kaminoMarket.getReserveBySymbol(token)!.getLiquidityMint(),
370
373
  wallet.publicKey,
371
374
  new VanillaObligation(new PublicKey(STAGING_LENDING_MARKET)),
375
+ true,
372
376
  await connection.getSlot()
373
377
  );
374
378
  console.log('User obligation', kaminoAction.obligation!.obligationAddress.toString());
package/src/idl.json CHANGED
@@ -1521,7 +1521,7 @@
1521
1521
  "accounts": [
1522
1522
  {
1523
1523
  "name": "owner",
1524
- "isMut": false,
1524
+ "isMut": true,
1525
1525
  "isSigner": true
1526
1526
  },
1527
1527
  {
@@ -1591,6 +1591,45 @@
1591
1591
  "isSigner": false
1592
1592
  }
1593
1593
  ]
1594
+ },
1595
+ {
1596
+ "name": "collateralFarmsAccounts",
1597
+ "accounts": [
1598
+ {
1599
+ "name": "obligationFarmUserState",
1600
+ "isMut": true,
1601
+ "isSigner": false,
1602
+ "isOptional": true
1603
+ },
1604
+ {
1605
+ "name": "reserveFarmState",
1606
+ "isMut": true,
1607
+ "isSigner": false,
1608
+ "isOptional": true
1609
+ }
1610
+ ]
1611
+ },
1612
+ {
1613
+ "name": "debtFarmsAccounts",
1614
+ "accounts": [
1615
+ {
1616
+ "name": "obligationFarmUserState",
1617
+ "isMut": true,
1618
+ "isSigner": false,
1619
+ "isOptional": true
1620
+ },
1621
+ {
1622
+ "name": "reserveFarmState",
1623
+ "isMut": true,
1624
+ "isSigner": false,
1625
+ "isOptional": true
1626
+ }
1627
+ ]
1628
+ },
1629
+ {
1630
+ "name": "farmsProgram",
1631
+ "isMut": false,
1632
+ "isSigner": false
1594
1633
  }
1595
1634
  ],
1596
1635
  "args": [
@@ -1985,7 +2024,7 @@
1985
2024
  "accounts": [
1986
2025
  {
1987
2026
  "name": "owner",
1988
- "isMut": false,
2027
+ "isMut": true,
1989
2028
  "isSigner": true
1990
2029
  },
1991
2030
  {
@@ -2070,7 +2109,7 @@
2070
2109
  "accounts": [
2071
2110
  {
2072
2111
  "name": "owner",
2073
- "isMut": false,
2112
+ "isMut": true,
2074
2113
  "isSigner": true
2075
2114
  },
2076
2115
  {
@@ -3435,12 +3474,19 @@
3435
3474
  ],
3436
3475
  "type": "u8"
3437
3476
  },
3477
+ {
3478
+ "name": "lowestReserveDepositMaxLtvPct",
3479
+ "docs": [
3480
+ "The lowest max LTV found amongst the collateral deposits"
3481
+ ],
3482
+ "type": "u8"
3483
+ },
3438
3484
  {
3439
3485
  "name": "reserved",
3440
3486
  "type": {
3441
3487
  "array": [
3442
3488
  "u8",
3443
- 6
3489
+ 5
3444
3490
  ]
3445
3491
  }
3446
3492
  },
@@ -5692,10 +5738,30 @@
5692
5738
  {
5693
5739
  "code": 6115,
5694
5740
  "name": "ObligationCurrentlyMarkedForDeleveraging",
5695
- "msg": "Obligation is currently marked for deleveraging"
5741
+ "msg": "Obligation is already marked for deleveraging"
5696
5742
  },
5697
5743
  {
5698
5744
  "code": 6116,
5745
+ "name": "MaximumWithdrawValueZero",
5746
+ "msg": "Maximum withdraw value is zero"
5747
+ },
5748
+ {
5749
+ "code": 6117,
5750
+ "name": "ZeroMaxLtvAssetsInDeposits",
5751
+ "msg": "No max LTV 0 assets allowed in deposits for repay and withdraw"
5752
+ },
5753
+ {
5754
+ "code": 6118,
5755
+ "name": "MinLtvAssetsPriority",
5756
+ "msg": "The operation must prioritize the collateral with the lowest LTV"
5757
+ },
5758
+ {
5759
+ "code": 6119,
5760
+ "name": "WorseLTVThanUnhealthyLTV",
5761
+ "msg": "Cannot get the obligation liquidatable"
5762
+ },
5763
+ {
5764
+ "code": 6120,
5699
5765
  "name": "FarmAccountsMissing",
5700
5766
  "msg": "Farm accounts to refresh are missing"
5701
5767
  }
@@ -48,6 +48,8 @@ export interface ObligationFields {
48
48
  * Only effective when `deleveraging_margin_call_started_slot != 0`.
49
49
  */
50
50
  autodeleverageTargetLtvPct: number
51
+ /** The lowest max LTV found amongst the collateral deposits */
52
+ lowestReserveDepositMaxLtvPct: number
51
53
  reserved: Array<number>
52
54
  highestBorrowFactorPct: BN
53
55
  /**
@@ -102,6 +104,8 @@ export interface ObligationJSON {
102
104
  * Only effective when `deleveraging_margin_call_started_slot != 0`.
103
105
  */
104
106
  autodeleverageTargetLtvPct: number
107
+ /** The lowest max LTV found amongst the collateral deposits */
108
+ lowestReserveDepositMaxLtvPct: number
105
109
  reserved: Array<number>
106
110
  highestBorrowFactorPct: string
107
111
  /**
@@ -157,6 +161,8 @@ export class Obligation {
157
161
  * Only effective when `deleveraging_margin_call_started_slot != 0`.
158
162
  */
159
163
  readonly autodeleverageTargetLtvPct: number
164
+ /** The lowest max LTV found amongst the collateral deposits */
165
+ readonly lowestReserveDepositMaxLtvPct: number
160
166
  readonly reserved: Array<number>
161
167
  readonly highestBorrowFactorPct: BN
162
168
  /**
@@ -191,7 +197,8 @@ export class Obligation {
191
197
  borsh.publicKey("referrer"),
192
198
  borsh.u8("borrowingDisabled"),
193
199
  borsh.u8("autodeleverageTargetLtvPct"),
194
- borsh.array(borsh.u8(), 6, "reserved"),
200
+ borsh.u8("lowestReserveDepositMaxLtvPct"),
201
+ borsh.array(borsh.u8(), 5, "reserved"),
195
202
  borsh.u64("highestBorrowFactorPct"),
196
203
  borsh.u64("autodeleverageMarginCallStartedTimestamp"),
197
204
  borsh.array(borsh.u64(), 125, "padding3"),
@@ -224,6 +231,7 @@ export class Obligation {
224
231
  this.referrer = fields.referrer
225
232
  this.borrowingDisabled = fields.borrowingDisabled
226
233
  this.autodeleverageTargetLtvPct = fields.autodeleverageTargetLtvPct
234
+ this.lowestReserveDepositMaxLtvPct = fields.lowestReserveDepositMaxLtvPct
227
235
  this.reserved = fields.reserved
228
236
  this.highestBorrowFactorPct = fields.highestBorrowFactorPct
229
237
  this.autodeleverageMarginCallStartedTimestamp =
@@ -304,6 +312,7 @@ export class Obligation {
304
312
  referrer: dec.referrer,
305
313
  borrowingDisabled: dec.borrowingDisabled,
306
314
  autodeleverageTargetLtvPct: dec.autodeleverageTargetLtvPct,
315
+ lowestReserveDepositMaxLtvPct: dec.lowestReserveDepositMaxLtvPct,
307
316
  reserved: dec.reserved,
308
317
  highestBorrowFactorPct: dec.highestBorrowFactorPct,
309
318
  autodeleverageMarginCallStartedTimestamp:
@@ -336,6 +345,7 @@ export class Obligation {
336
345
  referrer: this.referrer.toString(),
337
346
  borrowingDisabled: this.borrowingDisabled,
338
347
  autodeleverageTargetLtvPct: this.autodeleverageTargetLtvPct,
348
+ lowestReserveDepositMaxLtvPct: this.lowestReserveDepositMaxLtvPct,
339
349
  reserved: this.reserved,
340
350
  highestBorrowFactorPct: this.highestBorrowFactorPct.toString(),
341
351
  autodeleverageMarginCallStartedTimestamp:
@@ -374,6 +384,7 @@ export class Obligation {
374
384
  referrer: new PublicKey(obj.referrer),
375
385
  borrowingDisabled: obj.borrowingDisabled,
376
386
  autodeleverageTargetLtvPct: obj.autodeleverageTargetLtvPct,
387
+ lowestReserveDepositMaxLtvPct: obj.lowestReserveDepositMaxLtvPct,
377
388
  reserved: obj.reserved,
378
389
  highestBorrowFactorPct: new BN(obj.highestBorrowFactorPct),
379
390
  autodeleverageMarginCallStartedTimestamp: new BN(
@@ -115,6 +115,10 @@ export type CustomError =
115
115
  | ObligationsMustMatch
116
116
  | LendingMarketsMustMatch
117
117
  | ObligationCurrentlyMarkedForDeleveraging
118
+ | MaximumWithdrawValueZero
119
+ | ZeroMaxLtvAssetsInDeposits
120
+ | MinLtvAssetsPriority
121
+ | WorseLTVThanUnhealthyLTV
118
122
  | FarmAccountsMissing
119
123
 
120
124
  export class InvalidMarketAuthority extends Error {
@@ -1443,21 +1447,71 @@ export class ObligationCurrentlyMarkedForDeleveraging extends Error {
1443
1447
  static readonly code = 6115
1444
1448
  readonly code = 6115
1445
1449
  readonly name = "ObligationCurrentlyMarkedForDeleveraging"
1446
- readonly msg = "Obligation is currently marked for deleveraging"
1450
+ readonly msg = "Obligation is already marked for deleveraging"
1447
1451
 
1448
1452
  constructor(readonly logs?: string[]) {
1449
- super("6115: Obligation is currently marked for deleveraging")
1453
+ super("6115: Obligation is already marked for deleveraging")
1450
1454
  }
1451
1455
  }
1452
1456
 
1453
- export class FarmAccountsMissing extends Error {
1457
+ export class MaximumWithdrawValueZero extends Error {
1454
1458
  static readonly code = 6116
1455
1459
  readonly code = 6116
1460
+ readonly name = "MaximumWithdrawValueZero"
1461
+ readonly msg = "Maximum withdraw value is zero"
1462
+
1463
+ constructor(readonly logs?: string[]) {
1464
+ super("6116: Maximum withdraw value is zero")
1465
+ }
1466
+ }
1467
+
1468
+ export class ZeroMaxLtvAssetsInDeposits extends Error {
1469
+ static readonly code = 6117
1470
+ readonly code = 6117
1471
+ readonly name = "ZeroMaxLtvAssetsInDeposits"
1472
+ readonly msg =
1473
+ "No max LTV 0 assets allowed in deposits for repay and withdraw"
1474
+
1475
+ constructor(readonly logs?: string[]) {
1476
+ super(
1477
+ "6117: No max LTV 0 assets allowed in deposits for repay and withdraw"
1478
+ )
1479
+ }
1480
+ }
1481
+
1482
+ export class MinLtvAssetsPriority extends Error {
1483
+ static readonly code = 6118
1484
+ readonly code = 6118
1485
+ readonly name = "MinLtvAssetsPriority"
1486
+ readonly msg =
1487
+ "The operation must prioritize the collateral with the lowest LTV"
1488
+
1489
+ constructor(readonly logs?: string[]) {
1490
+ super(
1491
+ "6118: The operation must prioritize the collateral with the lowest LTV"
1492
+ )
1493
+ }
1494
+ }
1495
+
1496
+ export class WorseLTVThanUnhealthyLTV extends Error {
1497
+ static readonly code = 6119
1498
+ readonly code = 6119
1499
+ readonly name = "WorseLTVThanUnhealthyLTV"
1500
+ readonly msg = "Cannot get the obligation liquidatable"
1501
+
1502
+ constructor(readonly logs?: string[]) {
1503
+ super("6119: Cannot get the obligation liquidatable")
1504
+ }
1505
+ }
1506
+
1507
+ export class FarmAccountsMissing extends Error {
1508
+ static readonly code = 6120
1509
+ readonly code = 6120
1456
1510
  readonly name = "FarmAccountsMissing"
1457
1511
  readonly msg = "Farm accounts to refresh are missing"
1458
1512
 
1459
1513
  constructor(readonly logs?: string[]) {
1460
- super("6116: Farm accounts to refresh are missing")
1514
+ super("6120: Farm accounts to refresh are missing")
1461
1515
  }
1462
1516
  }
1463
1517
 
@@ -1696,6 +1750,14 @@ export function fromCode(code: number, logs?: string[]): CustomError | null {
1696
1750
  case 6115:
1697
1751
  return new ObligationCurrentlyMarkedForDeleveraging(logs)
1698
1752
  case 6116:
1753
+ return new MaximumWithdrawValueZero(logs)
1754
+ case 6117:
1755
+ return new ZeroMaxLtvAssetsInDeposits(logs)
1756
+ case 6118:
1757
+ return new MinLtvAssetsPriority(logs)
1758
+ case 6119:
1759
+ return new WorseLTVThanUnhealthyLTV(logs)
1760
+ case 6120:
1699
1761
  return new FarmAccountsMissing(logs)
1700
1762
  }
1701
1763
 
@@ -37,6 +37,15 @@ export interface RepayAndWithdrawAndRedeemAccounts {
37
37
  liquidityTokenProgram: PublicKey
38
38
  instructionSysvarAccount: PublicKey
39
39
  }
40
+ collateralFarmsAccounts: {
41
+ obligationFarmUserState: PublicKey
42
+ reserveFarmState: PublicKey
43
+ }
44
+ debtFarmsAccounts: {
45
+ obligationFarmUserState: PublicKey
46
+ reserveFarmState: PublicKey
47
+ }
48
+ farmsProgram: PublicKey
40
49
  }
41
50
 
42
51
  export const layout = borsh.struct([
@@ -94,7 +103,7 @@ export function repayAndWithdrawAndRedeem(
94
103
  {
95
104
  pubkey: accounts.withdrawAccounts.owner,
96
105
  isSigner: true,
97
- isWritable: false,
106
+ isWritable: true,
98
107
  },
99
108
  {
100
109
  pubkey: accounts.withdrawAccounts.obligation,
@@ -161,6 +170,27 @@ export function repayAndWithdrawAndRedeem(
161
170
  isSigner: false,
162
171
  isWritable: false,
163
172
  },
173
+ {
174
+ pubkey: accounts.collateralFarmsAccounts.obligationFarmUserState,
175
+ isSigner: false,
176
+ isWritable: true,
177
+ },
178
+ {
179
+ pubkey: accounts.collateralFarmsAccounts.reserveFarmState,
180
+ isSigner: false,
181
+ isWritable: true,
182
+ },
183
+ {
184
+ pubkey: accounts.debtFarmsAccounts.obligationFarmUserState,
185
+ isSigner: false,
186
+ isWritable: true,
187
+ },
188
+ {
189
+ pubkey: accounts.debtFarmsAccounts.reserveFarmState,
190
+ isSigner: false,
191
+ isWritable: true,
192
+ },
193
+ { pubkey: accounts.farmsProgram, isSigner: false, isWritable: false },
164
194
  ]
165
195
  const identifier = Buffer.from([2, 54, 152, 3, 148, 96, 109, 218])
166
196
  const buffer = Buffer.alloc(1000)
@@ -33,7 +33,7 @@ export function withdrawObligationCollateralAndRedeemReserveCollateral(
33
33
  programId: PublicKey = PROGRAM_ID
34
34
  ) {
35
35
  const keys: Array<AccountMeta> = [
36
- { pubkey: accounts.owner, isSigner: true, isWritable: false },
36
+ { pubkey: accounts.owner, isSigner: true, isWritable: true },
37
37
  { pubkey: accounts.obligation, isSigner: false, isWritable: true },
38
38
  { pubkey: accounts.lendingMarket, isSigner: false, isWritable: false },
39
39
  {
@@ -43,7 +43,7 @@ export function withdrawObligationCollateralAndRedeemReserveCollateralV2(
43
43
  {
44
44
  pubkey: accounts.withdrawAccounts.owner,
45
45
  isSigner: true,
46
- isWritable: false,
46
+ isWritable: true,
47
47
  },
48
48
  {
49
49
  pubkey: accounts.withdrawAccounts.obligation,
@@ -50,6 +50,8 @@ export class ObligationZP {
50
50
  readonly borrowingDisabled: number
51
51
  /** A target LTV set by the risk council when marking this obligation for deleveraging. Only effective when `deleveraging_margin_call_started_slot != 0`. */
52
52
  readonly autodeleverageTargetLtvPct: number
53
+ /** The lowest max LTV found amongst the collateral deposits */
54
+ readonly lowestReserveDepositMaxLtvPct: number
53
55
  readonly reserved: Array<BN>;
54
56
  readonly highestBorrowFactorPct: BN
55
57
  /** A timestamp at which the risk council most-recently marked this obligation for deleveraging. Zero if not currently subject to deleveraging. */
@@ -77,7 +79,8 @@ export class ObligationZP {
77
79
  borsh.publicKey("referrer"),
78
80
  borsh.u8("borrowingDisabled"),
79
81
  borsh.u8("autodeleverageTargetLtvPct"),
80
- borsh.array(borsh.u8(), 6, "reserved"),
82
+ borsh.u8("lowestReserveDepositMaxLtvPct"),
83
+ borsh.array(borsh.u8(), 5, "reserved"),
81
84
  borsh.u64("highestBorrowFactorPct"),
82
85
  borsh.u64("autodeleverageMarginCallStartedTimestamp"),
83
86
  ])
@@ -109,6 +112,7 @@ export class ObligationZP {
109
112
  this.referrer = fields.referrer
110
113
  this.borrowingDisabled = fields.borrowingDisabled
111
114
  this.autodeleverageTargetLtvPct = fields.autodeleverageTargetLtvPct
115
+ this.lowestReserveDepositMaxLtvPct = fields.lowestReserveDepositMaxLtvPct
112
116
  this.reserved = fields.reserved.map((num) => new BN(num))
113
117
  this.highestBorrowFactorPct = fields.highestBorrowFactorPct
114
118
  this.autodeleverageMarginCallStartedTimestamp = fields.autodeleverageMarginCallStartedTimestamp
@@ -187,6 +191,7 @@ export class ObligationZP {
187
191
  referrer: dec.referrer,
188
192
  borrowingDisabled: dec.borrowingDisabled,
189
193
  autodeleverageTargetLtvPct: dec.autodeleverageTargetLtvPct,
194
+ lowestReserveDepositMaxLtvPct: dec.lowestReserveDepositMaxLtvPct,
190
195
  reserved: dec.reserved,
191
196
  highestBorrowFactorPct: dec.highestBorrowFactorPct,
192
197
  autodeleverageMarginCallStartedTimestamp: dec.autodeleverageMarginCallStartedTimestamp,
@@ -52,6 +52,7 @@ interface RepayWithCollSwapInputsProps<QuoteResponse> {
52
52
  isClosingPosition: boolean;
53
53
  budgetAndPriorityFeeIxs?: TransactionInstruction[];
54
54
  scopeRefresh?: ScopeRefresh;
55
+ useV2Ixs: boolean;
55
56
  quoter: SwapQuoteProvider<QuoteResponse>;
56
57
  }
57
58
 
@@ -67,6 +68,7 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
67
68
  isClosingPosition,
68
69
  budgetAndPriorityFeeIxs,
69
70
  scopeRefresh,
71
+ useV2Ixs,
70
72
  }: RepayWithCollSwapInputsProps<QuoteResponse>): Promise<{
71
73
  swapInputs: SwapInputs;
72
74
  initialInputs: RepayWithCollInitialInputs<QuoteResponse>;
@@ -132,7 +134,8 @@ export async function getRepayWithCollSwapInputs<QuoteResponse>({
132
134
  },
133
135
  isClosingPosition,
134
136
  repayAmountLamports,
135
- inputAmountLamports
137
+ inputAmountLamports,
138
+ useV2Ixs
136
139
  );
137
140
  const uniqueKlendAccounts = uniqueAccounts(klendIxs);
138
141
 
@@ -189,6 +192,7 @@ export async function getRepayWithCollIxs<QuoteResponse>({
189
192
  swapper,
190
193
  referrer,
191
194
  scopeRefresh,
195
+ useV2Ixs,
192
196
  logger = console.log,
193
197
  }: RepayWithCollIxsProps<QuoteResponse>): Promise<RepayWithCollIxsResponse<QuoteResponse>> {
194
198
  const { swapInputs, initialInputs } = await getRepayWithCollSwapInputs({
@@ -203,6 +207,7 @@ export async function getRepayWithCollIxs<QuoteResponse>({
203
207
  isClosingPosition,
204
208
  budgetAndPriorityFeeIxs,
205
209
  scopeRefresh,
210
+ useV2Ixs,
206
211
  });
207
212
  const { debtRepayAmountLamports, flashRepayAmountLamports, maxCollateralWithdrawLamports, swapQuote } = initialInputs;
208
213
  const { inputAmountLamports: collSwapInLamports } = swapInputs;
@@ -243,7 +248,8 @@ export async function getRepayWithCollIxs<QuoteResponse>({
243
248
  swapResponse,
244
249
  isClosingPosition,
245
250
  debtRepayAmountLamports,
246
- swapInputs.inputAmountLamports
251
+ swapInputs.inputAmountLamports,
252
+ useV2Ixs
247
253
  );
248
254
 
249
255
  return {
@@ -266,7 +272,8 @@ async function buildRepayWithCollateralIxs(
266
272
  swapQuoteIxs: SwapIxs,
267
273
  isClosingPosition: boolean,
268
274
  debtRepayAmountLamports: Decimal,
269
- collWithdrawLamports: Decimal
275
+ collWithdrawLamports: Decimal,
276
+ useV2Ixs: boolean
270
277
  ): Promise<TransactionInstruction[]> {
271
278
  // 1. Create atas & budget txns
272
279
  const budgetIxns = budgetAndPriorityFeeIxs || getComputeBudgetAndPriorityFeeIxns(1_400_000);
@@ -306,6 +313,7 @@ async function buildRepayWithCollateralIxs(
306
313
  obligation.state.owner,
307
314
  currentSlot,
308
315
  obligation,
316
+ useV2Ixs,
309
317
  0,
310
318
  false,
311
319
  requestElevationGroup,
@@ -63,6 +63,7 @@ export interface SwapCollIxnsInputs<QuoteResponse> {
63
63
  currentSlot: number;
64
64
  budgetAndPriorityFeeIxns?: TransactionInstruction[];
65
65
  scopeRefresh?: ScopeRefresh;
66
+ useV2Ixs: boolean;
66
67
  quoter: SwapQuoteProvider<QuoteResponse>;
67
68
  swapper: SwapIxsProvider<QuoteResponse>;
68
69
  logger?: (msg: string, ...extra: any[]) => void;
@@ -82,6 +83,11 @@ export interface SwapCollIxnsOutputs<QuoteResponse> {
82
83
  */
83
84
  lookupTables: AddressLookupTableAccount[];
84
85
 
86
+ /**
87
+ * Whether the swap is using V2 instructions.
88
+ */
89
+ useV2Ixs: boolean;
90
+
85
91
  /**
86
92
  * Informational-only details of the token amounts/fees/rates that were used during construction of `ixs`.
87
93
  */
@@ -168,6 +174,7 @@ export async function getSwapCollIxns<QuoteResponse>(
168
174
  return {
169
175
  ixs: listIxns(klendIxns, externalSwapIxns.ixns),
170
176
  lookupTables: externalSwapIxns.luts,
177
+ useV2Ixs: context.useV2Ixs,
171
178
  simulationDetails: {
172
179
  flashLoan: {
173
180
  targetCollFlashBorrowedAmount: klendIxns.simulationDetails.targetCollFlashBorrowedAmount,
@@ -198,6 +205,7 @@ type SwapCollContext<QuoteResponse> = {
198
205
  swapper: SwapIxsProvider<QuoteResponse>;
199
206
  referrer: PublicKey;
200
207
  currentSlot: number;
208
+ useV2Ixs: boolean;
201
209
  scopeRefresh: ScopeRefresh | undefined;
202
210
  logger: (msg: string, ...extra: any[]) => void;
203
211
  };
@@ -230,6 +238,7 @@ function extractArgsAndContext<QuoteResponse>(
230
238
  referrer: inputs.referrer,
231
239
  scopeRefresh: inputs.scopeRefresh,
232
240
  currentSlot: inputs.currentSlot,
241
+ useV2Ixs: inputs.useV2Ixs,
233
242
  },
234
243
  ];
235
244
  }
@@ -371,6 +380,7 @@ async function getDepositTargetCollIxns(
371
380
  context.targetCollReserve.getLiquidityMint(),
372
381
  context.obligation.state.owner,
373
382
  context.obligation,
383
+ context.useV2Ixs,
374
384
  0, // no extra compute budget
375
385
  false, // we do not need ATA ixns here (we construct and close them ourselves)
376
386
  removesElevationGroup, // we may need to (temporarily) remove the elevation group; the same or a different one will be set on withdraw, if requested
@@ -419,6 +429,7 @@ async function getWithdrawSourceCollIxns(
419
429
  context.sourceCollReserve.getLiquidityMint(),
420
430
  context.obligation.state.owner,
421
431
  context.obligation,
432
+ context.useV2Ixs,
422
433
  0, // no extra compute budget
423
434
  false, // we do not need ATA ixns here (we construct and close them ourselves)
424
435
  requestedElevationGroup !== undefined, // the `elevationGroupIdToRequestAfterWithdraw()` has already decided on this