@kamino-finance/klend-sdk 5.2.13 → 5.3.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 (150) hide show
  1. package/dist/classes/action.d.ts.map +1 -1
  2. package/dist/classes/action.js +1 -2
  3. package/dist/classes/action.js.map +1 -1
  4. package/dist/classes/manager.js +22 -5
  5. package/dist/classes/manager.js.map +1 -1
  6. package/dist/classes/obligation.d.ts +2 -0
  7. package/dist/classes/obligation.d.ts.map +1 -1
  8. package/dist/classes/obligation.js +12 -6
  9. package/dist/classes/obligation.js.map +1 -1
  10. package/dist/classes/reserve.js +14 -14
  11. package/dist/classes/reserve.js.map +1 -1
  12. package/dist/classes/shared.d.ts +2 -2
  13. package/dist/classes/shared.d.ts.map +1 -1
  14. package/dist/classes/types.d.ts +1 -1
  15. package/dist/classes/types.d.ts.map +1 -1
  16. package/dist/classes/vault.d.ts +1 -1
  17. package/dist/classes/vault.d.ts.map +1 -1
  18. package/dist/classes/vault.js +16 -3
  19. package/dist/classes/vault.js.map +1 -1
  20. package/dist/client_kamino_manager.d.ts.map +1 -1
  21. package/dist/client_kamino_manager.js +44 -6
  22. package/dist/client_kamino_manager.js.map +1 -1
  23. package/dist/idl.json +310 -48
  24. package/dist/idl_codegen/accounts/LendingMarket.d.ts +48 -3
  25. package/dist/idl_codegen/accounts/LendingMarket.d.ts.map +1 -1
  26. package/dist/idl_codegen/accounts/LendingMarket.js +35 -8
  27. package/dist/idl_codegen/accounts/LendingMarket.js.map +1 -1
  28. package/dist/idl_codegen/accounts/Obligation.d.ts +30 -0
  29. package/dist/idl_codegen/accounts/Obligation.d.ts.map +1 -1
  30. package/dist/idl_codegen/accounts/Obligation.js +23 -2
  31. package/dist/idl_codegen/accounts/Obligation.js.map +1 -1
  32. package/dist/idl_codegen/accounts/Reserve.js +1 -1
  33. package/dist/idl_codegen/errors/custom.d.ts +33 -1
  34. package/dist/idl_codegen/errors/custom.d.ts.map +1 -1
  35. package/dist/idl_codegen/errors/custom.js +57 -1
  36. package/dist/idl_codegen/errors/custom.js.map +1 -1
  37. package/dist/idl_codegen/instructions/index.d.ts +7 -3
  38. package/dist/idl_codegen/instructions/index.d.ts.map +1 -1
  39. package/dist/idl_codegen/instructions/index.js +7 -3
  40. package/dist/idl_codegen/instructions/index.js.map +1 -1
  41. package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts +2 -5
  42. package/dist/idl_codegen/instructions/initReferrerTokenState.d.ts.map +1 -1
  43. package/dist/idl_codegen/instructions/initReferrerTokenState.js +3 -32
  44. package/dist/idl_codegen/instructions/initReferrerTokenState.js.map +1 -1
  45. package/dist/idl_codegen/instructions/markObligationForDeleveraging.d.ts +12 -0
  46. package/dist/idl_codegen/instructions/markObligationForDeleveraging.d.ts.map +1 -0
  47. package/dist/idl_codegen/instructions/markObligationForDeleveraging.js +47 -0
  48. package/dist/idl_codegen/instructions/markObligationForDeleveraging.js.map +1 -0
  49. package/dist/idl_codegen/instructions/repayAndWithdrawAndRedeem.d.ts +38 -0
  50. package/dist/idl_codegen/instructions/repayAndWithdrawAndRedeem.d.ts.map +1 -0
  51. package/dist/idl_codegen/instructions/repayAndWithdrawAndRedeem.js +159 -0
  52. package/dist/idl_codegen/instructions/repayAndWithdrawAndRedeem.js.map +1 -0
  53. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js +1 -1
  54. package/dist/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.js.map +1 -1
  55. package/dist/idl_codegen/types/ReserveConfig.d.ts +78 -20
  56. package/dist/idl_codegen/types/ReserveConfig.d.ts.map +1 -1
  57. package/dist/idl_codegen/types/ReserveConfig.js +45 -14
  58. package/dist/idl_codegen/types/ReserveConfig.js.map +1 -1
  59. package/dist/idl_codegen/types/ReserveLiquidity.d.ts +16 -16
  60. package/dist/idl_codegen/types/ReserveLiquidity.d.ts.map +1 -1
  61. package/dist/idl_codegen/types/ReserveLiquidity.js +16 -16
  62. package/dist/idl_codegen/types/ReserveLiquidity.js.map +1 -1
  63. package/dist/idl_codegen/types/UpdateConfigMode.d.ts +40 -14
  64. package/dist/idl_codegen/types/UpdateConfigMode.d.ts.map +1 -1
  65. package/dist/idl_codegen/types/UpdateConfigMode.js +71 -23
  66. package/dist/idl_codegen/types/UpdateConfigMode.js.map +1 -1
  67. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts +37 -11
  68. package/dist/idl_codegen/types/UpdateLendingMarketMode.d.ts.map +1 -1
  69. package/dist/idl_codegen/types/UpdateLendingMarketMode.js +64 -16
  70. package/dist/idl_codegen/types/UpdateLendingMarketMode.js.map +1 -1
  71. package/dist/idl_codegen/types/index.d.ts +4 -4
  72. package/dist/idl_codegen/types/index.d.ts.map +1 -1
  73. package/dist/idl_codegen/types/index.js.map +1 -1
  74. package/dist/idl_codegen/zero_padding/ObligationZP.d.ts +4 -0
  75. package/dist/idl_codegen/zero_padding/ObligationZP.d.ts.map +1 -1
  76. package/dist/idl_codegen/zero_padding/ObligationZP.js +10 -0
  77. package/dist/idl_codegen/zero_padding/ObligationZP.js.map +1 -1
  78. package/dist/lending_operations/repay_with_collateral_calcs.d.ts +4 -2
  79. package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
  80. package/dist/lending_operations/repay_with_collateral_calcs.js +45 -52
  81. package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
  82. package/dist/lending_operations/repay_with_collateral_operations.d.ts +7 -0
  83. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  84. package/dist/lending_operations/repay_with_collateral_operations.js +13 -3
  85. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  86. package/dist/pyth/accounts/PriceUpdateV2.d.ts +30 -0
  87. package/dist/pyth/accounts/PriceUpdateV2.d.ts.map +1 -0
  88. package/dist/pyth/accounts/PriceUpdateV2.js +104 -0
  89. package/dist/pyth/accounts/PriceUpdateV2.js.map +1 -0
  90. package/dist/pyth/accounts/index.d.ts +3 -0
  91. package/dist/pyth/accounts/index.d.ts.map +1 -0
  92. package/dist/pyth/accounts/index.js +6 -0
  93. package/dist/pyth/accounts/index.js.map +1 -0
  94. package/dist/pyth/types/PriceFeedMessage.d.ts +58 -0
  95. package/dist/pyth/types/PriceFeedMessage.d.ts.map +1 -0
  96. package/dist/pyth/types/PriceFeedMessage.js +118 -0
  97. package/dist/pyth/types/PriceFeedMessage.js.map +1 -0
  98. package/dist/pyth/types/VerificationLevel.d.ts +45 -0
  99. package/dist/pyth/types/VerificationLevel.d.ts.map +1 -0
  100. package/dist/pyth/types/VerificationLevel.js +105 -0
  101. package/dist/pyth/types/VerificationLevel.js.map +1 -0
  102. package/dist/pyth/types/index.d.ts +7 -0
  103. package/dist/pyth/types/index.d.ts.map +1 -0
  104. package/dist/pyth/types/index.js +31 -0
  105. package/dist/pyth/types/index.js.map +1 -0
  106. package/dist/referrals/instructions.d.ts.map +1 -1
  107. package/dist/referrals/instructions.js +1 -2
  108. package/dist/referrals/instructions.js.map +1 -1
  109. package/dist/utils/managerTypes.d.ts.map +1 -1
  110. package/dist/utils/managerTypes.js +5 -4
  111. package/dist/utils/managerTypes.js.map +1 -1
  112. package/dist/utils/oracle.d.ts.map +1 -1
  113. package/dist/utils/oracle.js +9 -14
  114. package/dist/utils/oracle.js.map +1 -1
  115. package/package.json +3 -4
  116. package/src/classes/action.ts +1 -3
  117. package/src/classes/manager.ts +29 -6
  118. package/src/classes/obligation.ts +15 -6
  119. package/src/classes/reserve.ts +16 -16
  120. package/src/classes/shared.ts +2 -2
  121. package/src/classes/types.ts +16 -17
  122. package/src/classes/vault.ts +18 -4
  123. package/src/client_kamino_manager.ts +61 -6
  124. package/src/idl_codegen/accounts/LendingMarket.ts +76 -13
  125. package/src/idl_codegen/accounts/Obligation.ts +47 -2
  126. package/src/idl_codegen/accounts/Reserve.ts +1 -1
  127. package/src/idl_codegen/errors/custom.ts +56 -0
  128. package/src/idl_codegen/instructions/index.ts +13 -6
  129. package/src/idl_codegen/instructions/initReferrerTokenState.ts +3 -15
  130. package/src/idl_codegen/instructions/markObligationForDeleveraging.ts +40 -0
  131. package/src/idl_codegen/instructions/repayAndWithdrawAndRedeem.ts +177 -0
  132. package/src/idl_codegen/instructions/withdrawObligationCollateralAndRedeemReserveCollateral.ts +1 -1
  133. package/src/idl_codegen/programId.ts +1 -1
  134. package/src/idl_codegen/types/ReserveConfig.ts +100 -28
  135. package/src/idl_codegen/types/ReserveLiquidity.ts +25 -24
  136. package/src/idl_codegen/types/UpdateConfigMode.ts +86 -26
  137. package/src/idl_codegen/types/UpdateLendingMarketMode.ts +77 -17
  138. package/src/idl_codegen/types/index.ts +14 -6
  139. package/src/idl_codegen/zero_padding/ObligationZP.ts +10 -0
  140. package/src/lending_operations/repay_with_collateral_calcs.ts +55 -61
  141. package/src/lending_operations/repay_with_collateral_operations.ts +24 -4
  142. package/src/leverage/operations.ts +1 -1
  143. package/src/pyth/accounts/PriceUpdateV2.ts +110 -0
  144. package/src/pyth/accounts/index.ts +2 -0
  145. package/src/pyth/types/PriceFeedMessage.ts +120 -0
  146. package/src/pyth/types/VerificationLevel.ts +98 -0
  147. package/src/pyth/types/index.ts +9 -0
  148. package/src/referrals/instructions.ts +1 -3
  149. package/src/utils/managerTypes.ts +5 -5
  150. package/src/utils/oracle.ts +9 -15
@@ -53,6 +53,7 @@ import {
53
53
  PerformanceFeeBps,
54
54
  } from './idl_codegen_kamino_vault/types/VaultConfigField';
55
55
  import { getAccountOwner } from './utils/rpc';
56
+ import { Farms, FarmState, getUserStatePDA } from '@kamino-finance/farms-sdk';
56
57
 
57
58
  dotenv.config({
58
59
  path: `.env${process.env.ENV ? '.' + process.env.ENV : ''}`,
@@ -519,6 +520,56 @@ async function main() {
519
520
  mode === 'execute' && console.log('Pending fees withdrawn:', withdrawPendingFeesSig);
520
521
  });
521
522
 
523
+ commands
524
+ .command('stake')
525
+ .requiredOption('--vault <string>', 'Vault address')
526
+ .requiredOption('--farm <string>', 'Farm address')
527
+ .requiredOption('--amount <string>', 'The number of kTokens to stake')
528
+ .requiredOption(
529
+ `--mode <string>`,
530
+ 'simulate - to print txn simulation, inspect - to get txn simulation in explorer, execute - execute txn, multisig - to get bs58 txn for multisig usage'
531
+ )
532
+ .option(`--staging`, 'If true, will use the staging programs')
533
+ .option(`--multisig <string>`, 'If using multisig mode this is required, otherwise will be ignored')
534
+ .action(async ({ vault, farm, amount, mode, staging, multisig }) => {
535
+ const env = initializeClient(mode === 'multisig', staging);
536
+ const vaultAddress = new PublicKey(vault);
537
+ const farmAddress = new PublicKey(farm);
538
+
539
+ const farmClient = new Farms(env.connection);
540
+
541
+ const farmState = await FarmState.fetch(env.connection, farmAddress);
542
+ const vaultState = await new KaminoVault(vaultAddress, undefined, env.kVaultProgramId).getState(env.connection);
543
+
544
+ const scopePricesArg = farmState!.scopePrices.equals(PublicKey.default)
545
+ ? farmClient.getProgramID()
546
+ : farmState!.scopePrices;
547
+
548
+ const ixns: TransactionInstruction[] = [];
549
+ const userState = getUserStatePDA(farmClient.getProgramID(), farmAddress, env.provider.publicKey);
550
+ if (!userState) {
551
+ const createUserIx = await farmClient.createNewUserIx(env.provider.publicKey, farmAddress);
552
+ ixns.push(createUserIx);
553
+ }
554
+
555
+ // todo: fix in farms sdk to make this not async
556
+ const stakeIx = await farmClient.stakeIx(
557
+ env.payer.publicKey,
558
+ farmAddress,
559
+ new Decimal(amount),
560
+ vaultState.sharesMint,
561
+ scopePricesArg
562
+ );
563
+ ixns.push(stakeIx);
564
+
565
+ if (mode === 'multisig' && !multisig) {
566
+ throw new Error('If using multisig mode, multisig is required');
567
+ }
568
+ const withdrawPendingFeesSig = await processTxn(env.client, env.payer, ixns, mode, 2500, []);
569
+
570
+ mode === 'execute' && console.log('Pending fees withdrawn:', withdrawPendingFeesSig);
571
+ });
572
+
522
573
  commands
523
574
  .command('update-vault-reserve-allocation')
524
575
  .requiredOption('--vault <string>', 'Vault address')
@@ -661,7 +712,7 @@ async function main() {
661
712
  for (let i = 0; i < instructions.length; i++) {
662
713
  const txInstructions: TransactionInstruction[] = [];
663
714
  txInstructions.push(instructions[i]);
664
- const investReserveSig = await processTxn(env.client, env.payer, txInstructions, mode, 2500, [], 400000);
715
+ const investReserveSig = await processTxn(env.client, env.payer, txInstructions, mode, 2500, [], 800000);
665
716
 
666
717
  mode === 'execute' && console.log('Reserve invested:', investReserveSig);
667
718
  }
@@ -703,7 +754,7 @@ async function main() {
703
754
  kaminoVault,
704
755
  reserveWithAddress
705
756
  );
706
- const investReserveSig = await processTxn(env.client, env.payer, instructions, mode, 2500, [], 400_000);
757
+ const investReserveSig = await processTxn(env.client, env.payer, instructions, mode, 2500, [], 800_000);
707
758
 
708
759
  mode === 'execute' && console.log('Reserve invested:', investReserveSig);
709
760
  });
@@ -1163,7 +1214,7 @@ async function processTxn(
1163
1214
  if (simulation.value.logs && simulation.value.logs.length > 0) {
1164
1215
  console.log('Simulation: \n' + simulation.value.logs);
1165
1216
  } else {
1166
- console.log('Simulation failed: \n' + simulation);
1217
+ console.log('Simulation failed: \n' + simulation.value.err);
1167
1218
  }
1168
1219
  } else if (mode === 'inspect') {
1169
1220
  console.log(
@@ -1180,7 +1231,7 @@ async function processTxn(
1180
1231
  function createAddExtraComputeUnitFeeTransaction(units: number, microLamports: number): TransactionInstruction[] {
1181
1232
  const ixns: TransactionInstruction[] = [];
1182
1233
  ixns.push(ComputeBudgetProgram.setComputeUnitLimit({ units }));
1183
- ixns.push(ComputeBudgetProgram.setComputeUnitPrice({ microLamports }));
1234
+ ixns.push(ComputeBudgetProgram.setComputeUnitPrice({ microLamports: new Decimal(microLamports).floor().toNumber() }));
1184
1235
  return ixns;
1185
1236
  }
1186
1237
 
@@ -1233,12 +1284,14 @@ function parseReserveConfigFromFile(farmConfigFromFile: any): ReserveConfig {
1233
1284
  deleveragingMarginCallPeriodSecs: new BN(farmConfigFromFile.deleveragingMarginCallPeriodSecs),
1234
1285
  borrowFactorPct: new BN(farmConfigFromFile.borrowFactorPct),
1235
1286
  elevationGroups: farmConfigFromFile.elevationGroups,
1236
- deleveragingThresholdSlotsPerBps: new BN(farmConfigFromFile.deleveragingThresholdSlotsPerBps),
1287
+ deleveragingThresholdSecsPerBps: new BN(farmConfigFromFile.deleveragingThresholdSecsPerBps),
1237
1288
  disableUsageAsCollOutsideEmode: farmConfigFromFile.disableUsageAsCollOutsideEmode,
1238
1289
  utilizationLimitBlockBorrowingAbove: farmConfigFromFile.utilizationLimitBlockBorrowingAbove,
1239
1290
  hostFixedInterestRateBps: farmConfigFromFile.hostFixedInterestRateBps,
1291
+ autodeleverageEnabled: farmConfigFromFile.autodeleverageEnabled,
1240
1292
  borrowLimitOutsideElevationGroup: new BN(farmConfigFromFile.borrowLimitOutsideElevationGroup),
1241
1293
  borrowLimitAgainstThisCollateralInElevationGroup: parseReserveBorrowLimitAgainstCollInEmode(farmConfigFromFile),
1294
+ deleveragingBonusIncreaseBpsPerDay: new BN(farmConfigFromFile.deleveragingBonusIncreaseBpsPerDay),
1242
1295
  reserved1: Array(2).fill(0),
1243
1296
  reserved2: Array(2).fill(0),
1244
1297
  reserved3: Array(8).fill(0),
@@ -1351,13 +1404,15 @@ function parseReserveConfigToFile(reserveConfig: ReserveConfig) {
1351
1404
  deleveragingMarginCallPeriodSecs: reserveConfig.deleveragingMarginCallPeriodSecs.toString(),
1352
1405
  borrowFactorPct: reserveConfig.borrowFactorPct.toString(),
1353
1406
  elevationGroups: reserveConfig.elevationGroups,
1354
- deleveragingThresholdSlotsPerBps: reserveConfig.deleveragingThresholdSlotsPerBps.toString(),
1407
+ deleveragingThresholdSecsPerBps: reserveConfig.deleveragingThresholdSecsPerBps.toString(),
1355
1408
  disableUsageAsCollOutsideEmode: reserveConfig.disableUsageAsCollOutsideEmode,
1356
1409
  utilizationLimitBlockBorrowingAbove: reserveConfig.utilizationLimitBlockBorrowingAbove,
1357
1410
  hostFixedInterestRateBps: reserveConfig.hostFixedInterestRateBps,
1411
+ autodeleverageEnabled: reserveConfig.autodeleverageEnabled,
1358
1412
  borrowLimitOutsideElevationGroup: reserveConfig.borrowLimitOutsideElevationGroup.toString(),
1359
1413
  borrowLimitAgainstThisCollateralInElevationGroup:
1360
1414
  reserveConfig.borrowLimitAgainstThisCollateralInElevationGroup.map((entry) => entry.toString()),
1415
+ deleveragingBonusIncreaseBpsPerDay: reserveConfig.deleveragingBonusIncreaseBpsPerDay.toString(),
1361
1416
  reserved1: Array(2).fill(0),
1362
1417
  reserved2: Array(2).fill(0),
1363
1418
  reserved3: Array(8).fill(0),
@@ -21,6 +21,13 @@ export interface LendingMarketFields {
21
21
  /** Referral fee for the lending market, as bps out of the total protocol fee */
22
22
  referralFeeBps: number
23
23
  emergencyMode: number
24
+ /**
25
+ * Whether the obligations on this market should be subject to auto-deleveraging after deposit
26
+ * or borrow limit is crossed.
27
+ * Besides this flag, the particular reserve's flag also needs to be enabled (logical `AND`).
28
+ * **NOTE:** the manual "target LTV" deleveraging (enabled by the risk council for individual
29
+ * obligations) is NOT affected by this flag.
30
+ */
24
31
  autodeleverageEnabled: number
25
32
  borrowDisabled: number
26
33
  /**
@@ -50,9 +57,17 @@ export interface LendingMarketFields {
50
57
  elevationGroupPadding: Array<BN>
51
58
  /** Min net value accepted to be found in a position after any lending action in an obligation (scaled by quote currency decimals) */
52
59
  minNetValueInObligationSf: BN
53
- minValueSkipLiquidationLtvBfChecks: BN
60
+ /** Minimum value to enforce smallest ltv priority checks on the collateral reserves on liquidation */
61
+ minValueSkipLiquidationLtvChecks: BN
54
62
  /** Market name, zero-padded. */
55
63
  name: Array<number>
64
+ /** Minimum value to enforce highest borrow factor priority checks on the debt reserves on liquidation */
65
+ minValueSkipLiquidationBfChecks: BN
66
+ /**
67
+ * Time (in seconds) that must pass before liquidation is allowed on an obligation that has
68
+ * been individually marked for auto-deleveraging (by the risk council).
69
+ */
70
+ individualAutodeleverageMarginCallPeriodSecs: BN
56
71
  padding1: Array<BN>
57
72
  }
58
73
 
@@ -73,6 +88,13 @@ export interface LendingMarketJSON {
73
88
  /** Referral fee for the lending market, as bps out of the total protocol fee */
74
89
  referralFeeBps: number
75
90
  emergencyMode: number
91
+ /**
92
+ * Whether the obligations on this market should be subject to auto-deleveraging after deposit
93
+ * or borrow limit is crossed.
94
+ * Besides this flag, the particular reserve's flag also needs to be enabled (logical `AND`).
95
+ * **NOTE:** the manual "target LTV" deleveraging (enabled by the risk council for individual
96
+ * obligations) is NOT affected by this flag.
97
+ */
76
98
  autodeleverageEnabled: number
77
99
  borrowDisabled: number
78
100
  /**
@@ -102,9 +124,17 @@ export interface LendingMarketJSON {
102
124
  elevationGroupPadding: Array<string>
103
125
  /** Min net value accepted to be found in a position after any lending action in an obligation (scaled by quote currency decimals) */
104
126
  minNetValueInObligationSf: string
105
- minValueSkipLiquidationLtvBfChecks: string
127
+ /** Minimum value to enforce smallest ltv priority checks on the collateral reserves on liquidation */
128
+ minValueSkipLiquidationLtvChecks: string
106
129
  /** Market name, zero-padded. */
107
130
  name: Array<number>
131
+ /** Minimum value to enforce highest borrow factor priority checks on the debt reserves on liquidation */
132
+ minValueSkipLiquidationBfChecks: string
133
+ /**
134
+ * Time (in seconds) that must pass before liquidation is allowed on an obligation that has
135
+ * been individually marked for auto-deleveraging (by the risk council).
136
+ */
137
+ individualAutodeleverageMarginCallPeriodSecs: string
108
138
  padding1: Array<string>
109
139
  }
110
140
 
@@ -125,6 +155,13 @@ export class LendingMarket {
125
155
  /** Referral fee for the lending market, as bps out of the total protocol fee */
126
156
  readonly referralFeeBps: number
127
157
  readonly emergencyMode: number
158
+ /**
159
+ * Whether the obligations on this market should be subject to auto-deleveraging after deposit
160
+ * or borrow limit is crossed.
161
+ * Besides this flag, the particular reserve's flag also needs to be enabled (logical `AND`).
162
+ * **NOTE:** the manual "target LTV" deleveraging (enabled by the risk council for individual
163
+ * obligations) is NOT affected by this flag.
164
+ */
128
165
  readonly autodeleverageEnabled: number
129
166
  readonly borrowDisabled: number
130
167
  /**
@@ -154,9 +191,17 @@ export class LendingMarket {
154
191
  readonly elevationGroupPadding: Array<BN>
155
192
  /** Min net value accepted to be found in a position after any lending action in an obligation (scaled by quote currency decimals) */
156
193
  readonly minNetValueInObligationSf: BN
157
- readonly minValueSkipLiquidationLtvBfChecks: BN
194
+ /** Minimum value to enforce smallest ltv priority checks on the collateral reserves on liquidation */
195
+ readonly minValueSkipLiquidationLtvChecks: BN
158
196
  /** Market name, zero-padded. */
159
197
  readonly name: Array<number>
198
+ /** Minimum value to enforce highest borrow factor priority checks on the debt reserves on liquidation */
199
+ readonly minValueSkipLiquidationBfChecks: BN
200
+ /**
201
+ * Time (in seconds) that must pass before liquidation is allowed on an obligation that has
202
+ * been individually marked for auto-deleveraging (by the risk council).
203
+ */
204
+ readonly individualAutodeleverageMarginCallPeriodSecs: BN
160
205
  readonly padding1: Array<BN>
161
206
 
162
207
  static readonly discriminator = Buffer.from([
@@ -185,9 +230,11 @@ export class LendingMarket {
185
230
  borsh.array(types.ElevationGroup.layout(), 32, "elevationGroups"),
186
231
  borsh.array(borsh.u64(), 90, "elevationGroupPadding"),
187
232
  borsh.u128("minNetValueInObligationSf"),
188
- borsh.u64("minValueSkipLiquidationLtvBfChecks"),
233
+ borsh.u64("minValueSkipLiquidationLtvChecks"),
189
234
  borsh.array(borsh.u8(), 32, "name"),
190
- borsh.array(borsh.u64(), 173, "padding1"),
235
+ borsh.u64("minValueSkipLiquidationBfChecks"),
236
+ borsh.u64("individualAutodeleverageMarginCallPeriodSecs"),
237
+ borsh.array(borsh.u64(), 171, "padding1"),
191
238
  ])
192
239
 
193
240
  constructor(fields: LendingMarketFields) {
@@ -217,9 +264,13 @@ export class LendingMarket {
217
264
  )
218
265
  this.elevationGroupPadding = fields.elevationGroupPadding
219
266
  this.minNetValueInObligationSf = fields.minNetValueInObligationSf
220
- this.minValueSkipLiquidationLtvBfChecks =
221
- fields.minValueSkipLiquidationLtvBfChecks
267
+ this.minValueSkipLiquidationLtvChecks =
268
+ fields.minValueSkipLiquidationLtvChecks
222
269
  this.name = fields.name
270
+ this.minValueSkipLiquidationBfChecks =
271
+ fields.minValueSkipLiquidationBfChecks
272
+ this.individualAutodeleverageMarginCallPeriodSecs =
273
+ fields.individualAutodeleverageMarginCallPeriodSecs
223
274
  this.padding1 = fields.padding1
224
275
  }
225
276
 
@@ -293,9 +344,11 @@ export class LendingMarket {
293
344
  ),
294
345
  elevationGroupPadding: dec.elevationGroupPadding,
295
346
  minNetValueInObligationSf: dec.minNetValueInObligationSf,
296
- minValueSkipLiquidationLtvBfChecks:
297
- dec.minValueSkipLiquidationLtvBfChecks,
347
+ minValueSkipLiquidationLtvChecks: dec.minValueSkipLiquidationLtvChecks,
298
348
  name: dec.name,
349
+ minValueSkipLiquidationBfChecks: dec.minValueSkipLiquidationBfChecks,
350
+ individualAutodeleverageMarginCallPeriodSecs:
351
+ dec.individualAutodeleverageMarginCallPeriodSecs,
299
352
  padding1: dec.padding1,
300
353
  })
301
354
  }
@@ -327,9 +380,13 @@ export class LendingMarket {
327
380
  item.toString()
328
381
  ),
329
382
  minNetValueInObligationSf: this.minNetValueInObligationSf.toString(),
330
- minValueSkipLiquidationLtvBfChecks:
331
- this.minValueSkipLiquidationLtvBfChecks.toString(),
383
+ minValueSkipLiquidationLtvChecks:
384
+ this.minValueSkipLiquidationLtvChecks.toString(),
332
385
  name: this.name,
386
+ minValueSkipLiquidationBfChecks:
387
+ this.minValueSkipLiquidationBfChecks.toString(),
388
+ individualAutodeleverageMarginCallPeriodSecs:
389
+ this.individualAutodeleverageMarginCallPeriodSecs.toString(),
333
390
  padding1: this.padding1.map((item) => item.toString()),
334
391
  }
335
392
  }
@@ -365,10 +422,16 @@ export class LendingMarket {
365
422
  (item) => new BN(item)
366
423
  ),
367
424
  minNetValueInObligationSf: new BN(obj.minNetValueInObligationSf),
368
- minValueSkipLiquidationLtvBfChecks: new BN(
369
- obj.minValueSkipLiquidationLtvBfChecks
425
+ minValueSkipLiquidationLtvChecks: new BN(
426
+ obj.minValueSkipLiquidationLtvChecks
370
427
  ),
371
428
  name: obj.name,
429
+ minValueSkipLiquidationBfChecks: new BN(
430
+ obj.minValueSkipLiquidationBfChecks
431
+ ),
432
+ individualAutodeleverageMarginCallPeriodSecs: new BN(
433
+ obj.individualAutodeleverageMarginCallPeriodSecs
434
+ ),
372
435
  padding1: obj.padding1.map((item) => new BN(item)),
373
436
  })
374
437
  }
@@ -43,8 +43,18 @@ export interface ObligationFields {
43
43
  referrer: PublicKey
44
44
  /** Marked = 1 if borrowing disabled, 0 = borrowing enabled */
45
45
  borrowingDisabled: number
46
+ /**
47
+ * A target LTV set by the risk council when marking this obligation for deleveraging.
48
+ * Only effective when `deleveraging_margin_call_started_slot != 0`.
49
+ */
50
+ autodeleverageTargetLtvPct: number
46
51
  reserved: Array<number>
47
52
  highestBorrowFactorPct: BN
53
+ /**
54
+ * A timestamp at which the risk council most-recently marked this obligation for deleveraging.
55
+ * Zero if not currently subject to deleveraging.
56
+ */
57
+ autodeleverageMarginCallStartedTimestamp: BN
48
58
  padding3: Array<BN>
49
59
  }
50
60
 
@@ -87,8 +97,18 @@ export interface ObligationJSON {
87
97
  referrer: string
88
98
  /** Marked = 1 if borrowing disabled, 0 = borrowing enabled */
89
99
  borrowingDisabled: number
100
+ /**
101
+ * A target LTV set by the risk council when marking this obligation for deleveraging.
102
+ * Only effective when `deleveraging_margin_call_started_slot != 0`.
103
+ */
104
+ autodeleverageTargetLtvPct: number
90
105
  reserved: Array<number>
91
106
  highestBorrowFactorPct: string
107
+ /**
108
+ * A timestamp at which the risk council most-recently marked this obligation for deleveraging.
109
+ * Zero if not currently subject to deleveraging.
110
+ */
111
+ autodeleverageMarginCallStartedTimestamp: string
92
112
  padding3: Array<string>
93
113
  }
94
114
 
@@ -132,8 +152,18 @@ export class Obligation {
132
152
  readonly referrer: PublicKey
133
153
  /** Marked = 1 if borrowing disabled, 0 = borrowing enabled */
134
154
  readonly borrowingDisabled: number
155
+ /**
156
+ * A target LTV set by the risk council when marking this obligation for deleveraging.
157
+ * Only effective when `deleveraging_margin_call_started_slot != 0`.
158
+ */
159
+ readonly autodeleverageTargetLtvPct: number
135
160
  readonly reserved: Array<number>
136
161
  readonly highestBorrowFactorPct: BN
162
+ /**
163
+ * A timestamp at which the risk council most-recently marked this obligation for deleveraging.
164
+ * Zero if not currently subject to deleveraging.
165
+ */
166
+ readonly autodeleverageMarginCallStartedTimestamp: BN
137
167
  readonly padding3: Array<BN>
138
168
 
139
169
  static readonly discriminator = Buffer.from([
@@ -160,9 +190,11 @@ export class Obligation {
160
190
  borsh.u8("hasDebt"),
161
191
  borsh.publicKey("referrer"),
162
192
  borsh.u8("borrowingDisabled"),
163
- borsh.array(borsh.u8(), 7, "reserved"),
193
+ borsh.u8("autodeleverageTargetLtvPct"),
194
+ borsh.array(borsh.u8(), 6, "reserved"),
164
195
  borsh.u64("highestBorrowFactorPct"),
165
- borsh.array(borsh.u64(), 126, "padding3"),
196
+ borsh.u64("autodeleverageMarginCallStartedTimestamp"),
197
+ borsh.array(borsh.u64(), 125, "padding3"),
166
198
  ])
167
199
 
168
200
  constructor(fields: ObligationFields) {
@@ -191,8 +223,11 @@ export class Obligation {
191
223
  this.hasDebt = fields.hasDebt
192
224
  this.referrer = fields.referrer
193
225
  this.borrowingDisabled = fields.borrowingDisabled
226
+ this.autodeleverageTargetLtvPct = fields.autodeleverageTargetLtvPct
194
227
  this.reserved = fields.reserved
195
228
  this.highestBorrowFactorPct = fields.highestBorrowFactorPct
229
+ this.autodeleverageMarginCallStartedTimestamp =
230
+ fields.autodeleverageMarginCallStartedTimestamp
196
231
  this.padding3 = fields.padding3
197
232
  }
198
233
 
@@ -268,8 +303,11 @@ export class Obligation {
268
303
  hasDebt: dec.hasDebt,
269
304
  referrer: dec.referrer,
270
305
  borrowingDisabled: dec.borrowingDisabled,
306
+ autodeleverageTargetLtvPct: dec.autodeleverageTargetLtvPct,
271
307
  reserved: dec.reserved,
272
308
  highestBorrowFactorPct: dec.highestBorrowFactorPct,
309
+ autodeleverageMarginCallStartedTimestamp:
310
+ dec.autodeleverageMarginCallStartedTimestamp,
273
311
  padding3: dec.padding3,
274
312
  })
275
313
  }
@@ -297,8 +335,11 @@ export class Obligation {
297
335
  hasDebt: this.hasDebt,
298
336
  referrer: this.referrer.toString(),
299
337
  borrowingDisabled: this.borrowingDisabled,
338
+ autodeleverageTargetLtvPct: this.autodeleverageTargetLtvPct,
300
339
  reserved: this.reserved,
301
340
  highestBorrowFactorPct: this.highestBorrowFactorPct.toString(),
341
+ autodeleverageMarginCallStartedTimestamp:
342
+ this.autodeleverageMarginCallStartedTimestamp.toString(),
302
343
  padding3: this.padding3.map((item) => item.toString()),
303
344
  }
304
345
  }
@@ -332,8 +373,12 @@ export class Obligation {
332
373
  hasDebt: obj.hasDebt,
333
374
  referrer: new PublicKey(obj.referrer),
334
375
  borrowingDisabled: obj.borrowingDisabled,
376
+ autodeleverageTargetLtvPct: obj.autodeleverageTargetLtvPct,
335
377
  reserved: obj.reserved,
336
378
  highestBorrowFactorPct: new BN(obj.highestBorrowFactorPct),
379
+ autodeleverageMarginCallStartedTimestamp: new BN(
380
+ obj.autodeleverageMarginCallStartedTimestamp
381
+ ),
337
382
  padding3: obj.padding3.map((item) => new BN(item)),
338
383
  })
339
384
  }
@@ -99,7 +99,7 @@ export class Reserve {
99
99
  types.ReserveCollateral.layout("collateral"),
100
100
  borsh.array(borsh.u64(), 150, "reserveCollateralPadding"),
101
101
  types.ReserveConfig.layout("config"),
102
- borsh.array(borsh.u64(), 117, "configPadding"),
102
+ borsh.array(borsh.u64(), 116, "configPadding"),
103
103
  borsh.u64("borrowedAmountOutsideElevationGroup"),
104
104
  borsh.array(
105
105
  borsh.u64(),
@@ -111,6 +111,10 @@ export type CustomError =
111
111
  | InvalidTokenAccount
112
112
  | DepositDisabledOutsideElevationGroup
113
113
  | CannotCalculateReferralAmountDueToSlotsMismatch
114
+ | ObligationOwnersMustMatch
115
+ | ObligationsMustMatch
116
+ | LendingMarketsMustMatch
117
+ | ObligationAlreadyMarkedForDeleveraging
114
118
 
115
119
  export class InvalidMarketAuthority extends Error {
116
120
  static readonly code = 6000
@@ -1401,6 +1405,50 @@ export class CannotCalculateReferralAmountDueToSlotsMismatch extends Error {
1401
1405
  }
1402
1406
  }
1403
1407
 
1408
+ export class ObligationOwnersMustMatch extends Error {
1409
+ static readonly code = 6112
1410
+ readonly code = 6112
1411
+ readonly name = "ObligationOwnersMustMatch"
1412
+ readonly msg = "Obligation owners must match"
1413
+
1414
+ constructor(readonly logs?: string[]) {
1415
+ super("6112: Obligation owners must match")
1416
+ }
1417
+ }
1418
+
1419
+ export class ObligationsMustMatch extends Error {
1420
+ static readonly code = 6113
1421
+ readonly code = 6113
1422
+ readonly name = "ObligationsMustMatch"
1423
+ readonly msg = "Obligations must match"
1424
+
1425
+ constructor(readonly logs?: string[]) {
1426
+ super("6113: Obligations must match")
1427
+ }
1428
+ }
1429
+
1430
+ export class LendingMarketsMustMatch extends Error {
1431
+ static readonly code = 6114
1432
+ readonly code = 6114
1433
+ readonly name = "LendingMarketsMustMatch"
1434
+ readonly msg = "Lending markets must match"
1435
+
1436
+ constructor(readonly logs?: string[]) {
1437
+ super("6114: Lending markets must match")
1438
+ }
1439
+ }
1440
+
1441
+ export class ObligationAlreadyMarkedForDeleveraging extends Error {
1442
+ static readonly code = 6115
1443
+ readonly code = 6115
1444
+ readonly name = "ObligationAlreadyMarkedForDeleveraging"
1445
+ readonly msg = "Obligation is already marked for deleveraging"
1446
+
1447
+ constructor(readonly logs?: string[]) {
1448
+ super("6115: Obligation is already marked for deleveraging")
1449
+ }
1450
+ }
1451
+
1404
1452
  export function fromCode(code: number, logs?: string[]): CustomError | null {
1405
1453
  switch (code) {
1406
1454
  case 6000:
@@ -1627,6 +1675,14 @@ export function fromCode(code: number, logs?: string[]): CustomError | null {
1627
1675
  return new DepositDisabledOutsideElevationGroup(logs)
1628
1676
  case 6111:
1629
1677
  return new CannotCalculateReferralAmountDueToSlotsMismatch(logs)
1678
+ case 6112:
1679
+ return new ObligationOwnersMustMatch(logs)
1680
+ case 6113:
1681
+ return new ObligationsMustMatch(logs)
1682
+ case 6114:
1683
+ return new LendingMarketsMustMatch(logs)
1684
+ case 6115:
1685
+ return new ObligationAlreadyMarkedForDeleveraging(logs)
1630
1686
  }
1631
1687
 
1632
1688
  return null
@@ -24,13 +24,18 @@ export type {
24
24
  } from "./updateReserveConfig"
25
25
  export { redeemFees } from "./redeemFees"
26
26
  export type { RedeemFeesAccounts } from "./redeemFees"
27
- export { socializeLoss } from "./socializeLoss"
28
- export type { SocializeLossArgs, SocializeLossAccounts } from "./socializeLoss"
29
27
  export { withdrawProtocolFee } from "./withdrawProtocolFee"
30
28
  export type {
31
29
  WithdrawProtocolFeeArgs,
32
30
  WithdrawProtocolFeeAccounts,
33
31
  } from "./withdrawProtocolFee"
32
+ export { socializeLoss } from "./socializeLoss"
33
+ export type { SocializeLossArgs, SocializeLossAccounts } from "./socializeLoss"
34
+ export { markObligationForDeleveraging } from "./markObligationForDeleveraging"
35
+ export type {
36
+ MarkObligationForDeleveragingArgs,
37
+ MarkObligationForDeleveragingAccounts,
38
+ } from "./markObligationForDeleveraging"
34
39
  export { refreshReserve } from "./refreshReserve"
35
40
  export type { RefreshReserveAccounts } from "./refreshReserve"
36
41
  export { refreshReservesBatch } from "./refreshReservesBatch"
@@ -82,6 +87,11 @@ export type {
82
87
  RepayObligationLiquidityArgs,
83
88
  RepayObligationLiquidityAccounts,
84
89
  } from "./repayObligationLiquidity"
90
+ export { repayAndWithdrawAndRedeem } from "./repayAndWithdrawAndRedeem"
91
+ export type {
92
+ RepayAndWithdrawAndRedeemArgs,
93
+ RepayAndWithdrawAndRedeemAccounts,
94
+ } from "./repayAndWithdrawAndRedeem"
85
95
  export { depositReserveLiquidityAndObligationCollateral } from "./depositReserveLiquidityAndObligationCollateral"
86
96
  export type {
87
97
  DepositReserveLiquidityAndObligationCollateralArgs,
@@ -113,10 +123,7 @@ export type {
113
123
  RequestElevationGroupAccounts,
114
124
  } from "./requestElevationGroup"
115
125
  export { initReferrerTokenState } from "./initReferrerTokenState"
116
- export type {
117
- InitReferrerTokenStateArgs,
118
- InitReferrerTokenStateAccounts,
119
- } from "./initReferrerTokenState"
126
+ export type { InitReferrerTokenStateAccounts } from "./initReferrerTokenState"
120
127
  export { initUserMetadata } from "./initUserMetadata"
121
128
  export type {
122
129
  InitUserMetadataArgs,
@@ -4,23 +4,17 @@ import * as borsh from "@coral-xyz/borsh" // eslint-disable-line @typescript-esl
4
4
  import * as types from "../types" // eslint-disable-line @typescript-eslint/no-unused-vars
5
5
  import { PROGRAM_ID } from "../programId"
6
6
 
7
- export interface InitReferrerTokenStateArgs {
8
- referrer: PublicKey
9
- }
10
-
11
7
  export interface InitReferrerTokenStateAccounts {
12
8
  payer: PublicKey
13
9
  lendingMarket: PublicKey
14
10
  reserve: PublicKey
11
+ referrer: PublicKey
15
12
  referrerTokenState: PublicKey
16
13
  rent: PublicKey
17
14
  systemProgram: PublicKey
18
15
  }
19
16
 
20
- export const layout = borsh.struct([borsh.publicKey("referrer")])
21
-
22
17
  export function initReferrerTokenState(
23
- args: InitReferrerTokenStateArgs,
24
18
  accounts: InitReferrerTokenStateAccounts,
25
19
  programId: PublicKey = PROGRAM_ID
26
20
  ) {
@@ -28,19 +22,13 @@ export function initReferrerTokenState(
28
22
  { pubkey: accounts.payer, isSigner: true, isWritable: true },
29
23
  { pubkey: accounts.lendingMarket, isSigner: false, isWritable: false },
30
24
  { pubkey: accounts.reserve, isSigner: false, isWritable: false },
25
+ { pubkey: accounts.referrer, isSigner: false, isWritable: false },
31
26
  { pubkey: accounts.referrerTokenState, isSigner: false, isWritable: true },
32
27
  { pubkey: accounts.rent, isSigner: false, isWritable: false },
33
28
  { pubkey: accounts.systemProgram, isSigner: false, isWritable: false },
34
29
  ]
35
30
  const identifier = Buffer.from([116, 45, 66, 148, 58, 13, 218, 115])
36
- const buffer = Buffer.alloc(1000)
37
- const len = layout.encode(
38
- {
39
- referrer: args.referrer,
40
- },
41
- buffer
42
- )
43
- const data = Buffer.concat([identifier, buffer]).slice(0, 8 + len)
31
+ const data = identifier
44
32
  const ix = new TransactionInstruction({ keys, programId, data })
45
33
  return ix
46
34
  }
@@ -0,0 +1,40 @@
1
+ import { TransactionInstruction, PublicKey, AccountMeta } from "@solana/web3.js" // eslint-disable-line @typescript-eslint/no-unused-vars
2
+ import BN from "bn.js" // eslint-disable-line @typescript-eslint/no-unused-vars
3
+ import * as borsh from "@coral-xyz/borsh" // eslint-disable-line @typescript-eslint/no-unused-vars
4
+ import * as types from "../types" // eslint-disable-line @typescript-eslint/no-unused-vars
5
+ import { PROGRAM_ID } from "../programId"
6
+
7
+ export interface MarkObligationForDeleveragingArgs {
8
+ autodeleverageTargetLtvPct: number
9
+ }
10
+
11
+ export interface MarkObligationForDeleveragingAccounts {
12
+ riskCouncil: PublicKey
13
+ obligation: PublicKey
14
+ lendingMarket: PublicKey
15
+ }
16
+
17
+ export const layout = borsh.struct([borsh.u8("autodeleverageTargetLtvPct")])
18
+
19
+ export function markObligationForDeleveraging(
20
+ args: MarkObligationForDeleveragingArgs,
21
+ accounts: MarkObligationForDeleveragingAccounts,
22
+ programId: PublicKey = PROGRAM_ID
23
+ ) {
24
+ const keys: Array<AccountMeta> = [
25
+ { pubkey: accounts.riskCouncil, isSigner: true, isWritable: false },
26
+ { pubkey: accounts.obligation, isSigner: false, isWritable: true },
27
+ { pubkey: accounts.lendingMarket, isSigner: false, isWritable: false },
28
+ ]
29
+ const identifier = Buffer.from([164, 35, 182, 19, 0, 116, 243, 127])
30
+ const buffer = Buffer.alloc(1000)
31
+ const len = layout.encode(
32
+ {
33
+ autodeleverageTargetLtvPct: args.autodeleverageTargetLtvPct,
34
+ },
35
+ buffer
36
+ )
37
+ const data = Buffer.concat([identifier, buffer]).slice(0, 8 + len)
38
+ const ix = new TransactionInstruction({ keys, programId, data })
39
+ return ix
40
+ }