@kamino-finance/klend-sdk 5.2.12 → 5.2.14

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 (36) hide show
  1. package/dist/classes/manager.d.ts +36 -7
  2. package/dist/classes/manager.d.ts.map +1 -1
  3. package/dist/classes/manager.js +41 -7
  4. package/dist/classes/manager.js.map +1 -1
  5. package/dist/classes/obligation.d.ts +2 -0
  6. package/dist/classes/obligation.d.ts.map +1 -1
  7. package/dist/classes/obligation.js +12 -6
  8. package/dist/classes/obligation.js.map +1 -1
  9. package/dist/classes/types.d.ts +23 -0
  10. package/dist/classes/types.d.ts.map +1 -0
  11. package/dist/classes/types.js +3 -0
  12. package/dist/classes/types.js.map +1 -0
  13. package/dist/classes/vault.d.ts +45 -9
  14. package/dist/classes/vault.d.ts.map +1 -1
  15. package/dist/classes/vault.js +349 -20
  16. package/dist/classes/vault.js.map +1 -1
  17. package/dist/client_kamino_manager.d.ts.map +1 -1
  18. package/dist/client_kamino_manager.js +100 -16
  19. package/dist/client_kamino_manager.js.map +1 -1
  20. package/dist/lending_operations/repay_with_collateral_calcs.d.ts +4 -2
  21. package/dist/lending_operations/repay_with_collateral_calcs.d.ts.map +1 -1
  22. package/dist/lending_operations/repay_with_collateral_calcs.js +45 -52
  23. package/dist/lending_operations/repay_with_collateral_calcs.js.map +1 -1
  24. package/dist/lending_operations/repay_with_collateral_operations.d.ts +7 -0
  25. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  26. package/dist/lending_operations/repay_with_collateral_operations.js +13 -3
  27. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  28. package/package.json +1 -1
  29. package/src/classes/manager.ts +57 -10
  30. package/src/classes/obligation.ts +15 -6
  31. package/src/classes/types.ts +27 -0
  32. package/src/classes/vault.ts +511 -26
  33. package/src/client_kamino_manager.ts +174 -19
  34. package/src/lending_operations/repay_with_collateral_calcs.ts +55 -61
  35. package/src/lending_operations/repay_with_collateral_operations.ts +24 -4
  36. package/src/leverage/operations.ts +1 -1
@@ -65,6 +65,13 @@ import { Data } from '@kamino-finance/kliquidity-sdk';
65
65
  import bs58 from 'bs58';
66
66
  import { getProgramAccounts } from '../utils/rpc';
67
67
  import { VaultConfigFieldKind } from '../idl_codegen_kamino_vault/types';
68
+ import {
69
+ AcceptVaultOwnershipIxs,
70
+ InitVaultIxs,
71
+ SyncVaultLUTIxs,
72
+ UpdateReserveAllocationIxs,
73
+ UpdateVaultConfigIxs,
74
+ } from './types';
68
75
 
69
76
  /**
70
77
  * KaminoManager is a class that provides a high-level interface to interact with the Kamino Lend and Kamino Vault programs, in order to create and manage a market, as well as vaults
@@ -190,9 +197,9 @@ export class KaminoManager {
190
197
  * This method will create a vault with a given config. The config can be changed later on, but it is recommended to set it up correctly from the start
191
198
  * @param vaultConfig - the config object used to create a vault
192
199
  * @returns vault - keypair, should be used to sign the transaction which creates the vault account
193
- * @returns ixns - an array of instructions to create the vault
200
+ * @returns vault: the keypair of the vault, used to sign the initialization transaction; initVaultIxs: a struct with ixs to initialize the vault and its lookup table + populateLUTIxs, a list to populate the lookup table which has to be executed in a separate transaction
194
201
  */
195
- async createVaultIxs(vaultConfig: KaminoVaultConfig): Promise<{ vault: Keypair; ixns: TransactionInstruction[] }> {
202
+ async createVaultIxs(vaultConfig: KaminoVaultConfig): Promise<{ vault: Keypair; initVaultIxs: InitVaultIxs }> {
196
203
  return this._vaultClient.createVaultIxs(vaultConfig);
197
204
  }
198
205
 
@@ -200,12 +207,12 @@ export class KaminoManager {
200
207
  * This method updates the vault reserve allocation cofnig for an exiting vault reserve, or adds a new reserve to the vault if it does not exist.
201
208
  * @param vault - vault to be updated
202
209
  * @param reserveAllocationConfig - new reserve allocation config
203
- * @returns - a list of instructions
210
+ * @returns - a struct of instructions
204
211
  */
205
212
  async updateVaultReserveAllocationIxs(
206
213
  vault: KaminoVault,
207
214
  reserveAllocationConfig: ReserveAllocationConfig
208
- ): Promise<TransactionInstruction> {
215
+ ): Promise<UpdateReserveAllocationIxs> {
209
216
  return this._vaultClient.updateReserveAllocationIxs(vault, reserveAllocationConfig);
210
217
  }
211
218
 
@@ -331,21 +338,28 @@ export class KaminoManager {
331
338
  return this._vaultClient.depositIxs(user, vault, tokenAmount, vaultReservesMap);
332
339
  }
333
340
 
334
- async updateVaultConfigIx(
341
+ /**
342
+ * Update a field of the vault. If the field is a pubkey it will return an extra instruction to add that account into the lookup table
343
+ * @param vault the vault to update
344
+ * @param mode the field to update (based on VaultConfigFieldKind enum)
345
+ * @param value the value to update the field with
346
+ * @returns a struct that contains the instruction to update the field and an optional list of instructions to update the lookup table
347
+ */
348
+ async updateVaultConfigIxs(
335
349
  vault: KaminoVault,
336
350
  mode: VaultConfigFieldKind,
337
351
  value: string
338
- ): Promise<TransactionInstruction> {
339
- return this._vaultClient.updateVaultConfigIx(vault, mode, value);
352
+ ): Promise<UpdateVaultConfigIxs> {
353
+ return this._vaultClient.updateVaultConfigIxs(vault, mode, value);
340
354
  }
341
355
 
342
356
  /**
343
- * This function creates the instruction for the `pendingAdmin` of the vault to accept to become the owner of the vault (step 2/2 of the ownership transfer)
357
+ * This function creates the instruction for the `pendingAdmin` of the vault to accept to become the owner of the vault (step 2/2 of the ownership transfer) and the instructions to sync the vault LUT to the new state
344
358
  * @param vault - vault to change the ownership for
345
359
  * @returns - an instruction to be used to be executed
346
360
  */
347
- async acceptVaultOwnershipIx(vault: KaminoVault): Promise<TransactionInstruction> {
348
- return this._vaultClient.acceptVaultOwnershipIx(vault);
361
+ async acceptVaultOwnershipIxs(vault: KaminoVault): Promise<AcceptVaultOwnershipIxs> {
362
+ return this._vaultClient.acceptVaultOwnershipIxs(vault);
349
363
  }
350
364
 
351
365
  /**
@@ -385,6 +399,30 @@ export class KaminoManager {
385
399
  return this._vaultClient.withdrawPendingFeesIxs(vault, slot);
386
400
  }
387
401
 
402
+ /**
403
+ * This method append the given keys to the lookup table
404
+ * @param payer - the owner of the lookup table
405
+ * @param slot - the LUT into which the keys will be inserted
406
+ * @param keys - the keys to be inserted
407
+ * @returns - list of instructions to insert the keys into the lookup table
408
+ */
409
+ async insertIntoLUT(payer: PublicKey, lut: PublicKey, keys: PublicKey[]): Promise<TransactionInstruction[]> {
410
+ return this._vaultClient.insertIntoLookupTableIxs(payer, lut, keys);
411
+ }
412
+
413
+ /**
414
+ * Sync a vault for lookup table; create and set the LUT for the vault if needed and fill it with all the needed accounts
415
+ * @param vault the vault to sync and set the LUT for if needed
416
+ * @param vaultReserves optional; the state of the reserves in the vault allocation
417
+ * @returns a struct that contains a list of ix to create the LUT and assign it to the vault if needed + a list of ixs to insert all the accounts in the LUT
418
+ */
419
+ async syncVaultLUT(
420
+ vault: KaminoVault,
421
+ vaultReserves?: PubkeyHashMap<PublicKey, KaminoReserve>
422
+ ): Promise<SyncVaultLUTIxs> {
423
+ return this._vaultClient.syncVaultLookupTable(vault, vaultReserves);
424
+ }
425
+
388
426
  /**
389
427
  * This method calculates the token per share value. This will always change based on interest earned from the vault, but calculating it requires a bunch of rpc requests. Caching this for a short duration would be optimal
390
428
  * @param vault - vault to calculate tokensPerShare for
@@ -439,6 +477,15 @@ export class KaminoManager {
439
477
  return this._vaultClient.getVaultFeesPct(vaultState);
440
478
  }
441
479
 
480
+ /**
481
+ * This method returns the vault name
482
+ * @param vault - vault to retrieve the onchain name for
483
+ * @returns - the vault name as string
484
+ */
485
+ getDecodedVaultName(vaultState: VaultState): string {
486
+ return this._vaultClient.decodeVaultName(vaultState.name);
487
+ }
488
+
442
489
  /**
443
490
  * @returns - the KaminoVault client
444
491
  */
@@ -32,6 +32,7 @@ export type Position = {
32
32
  export type ObligationStats = {
33
33
  userTotalDeposit: Decimal;
34
34
  userTotalCollateralDeposit: Decimal;
35
+ userTotalLiquidatableDeposit: Decimal;
35
36
  userTotalBorrow: Decimal;
36
37
  userTotalBorrowBorrowFactorAdjusted: Decimal;
37
38
  borrowLimit: Decimal;
@@ -55,6 +56,7 @@ interface DepositStats {
55
56
  deposits: Map<PublicKey, Position>;
56
57
  userTotalDeposit: Decimal;
57
58
  userTotalCollateralDeposit: Decimal;
59
+ userTotalLiquidatableDeposit: Decimal;
58
60
  borrowLimit: Decimal;
59
61
  liquidationLtv: Decimal;
60
62
  borrowLiquidationLimit: Decimal;
@@ -639,7 +641,7 @@ export class KaminoObligation {
639
641
 
640
642
  newStats.netAccountValue = newStats.userTotalDeposit.minus(newStats.userTotalBorrow);
641
643
  newStats.loanToValue = valueOrZero(
642
- newStats.userTotalBorrowBorrowFactorAdjusted.dividedBy(newStats.userTotalDeposit)
644
+ newStats.userTotalBorrowBorrowFactorAdjusted.dividedBy(newStats.userTotalCollateralDeposit)
643
645
  );
644
646
  newStats.leverage = valueOrZero(newStats.userTotalDeposit.dividedBy(newStats.netAccountValue));
645
647
 
@@ -714,6 +716,7 @@ export class KaminoObligation {
714
716
  userTotalBorrowBorrowFactorAdjusted: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted,
715
717
  userTotalDeposit: depositStatsOraclePrice.userTotalDeposit,
716
718
  userTotalCollateralDeposit: depositStatsOraclePrice.userTotalCollateralDeposit,
719
+ userTotalLiquidatableDeposit: depositStatsOraclePrice.userTotalLiquidatableDeposit,
717
720
  liquidationLtv: depositStatsOraclePrice.liquidationLtv,
718
721
  borrowUtilization: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted.dividedBy(
719
722
  depositStatsOraclePrice.borrowLimit
@@ -721,7 +724,7 @@ export class KaminoObligation {
721
724
  netAccountValue: netAccountValueScopeRefreshed,
722
725
  leverage: depositStatsOraclePrice.userTotalDeposit.dividedBy(netAccountValueScopeRefreshed),
723
726
  loanToValue: borrowStatsOraclePrice.userTotalBorrowBorrowFactorAdjusted.dividedBy(
724
- depositStatsOraclePrice.userTotalDeposit
727
+ depositStatsOraclePrice.userTotalCollateralDeposit
725
728
  ),
726
729
  potentialElevationGroupUpdate,
727
730
  },
@@ -737,6 +740,7 @@ export class KaminoObligation {
737
740
  ): DepositStats {
738
741
  let userTotalDeposit = new Decimal(0);
739
742
  let userTotalCollateralDeposit = new Decimal(0);
743
+ let userTotalLiquidatableDeposit = new Decimal(0);
740
744
  let borrowLimit = new Decimal(0);
741
745
  let borrowLiquidationLimit = new Decimal(0);
742
746
 
@@ -770,6 +774,10 @@ export class KaminoObligation {
770
774
  userTotalCollateralDeposit = userTotalCollateralDeposit.add(depositValueUsd);
771
775
  }
772
776
 
777
+ if (!liquidationLtv.eq('0')) {
778
+ userTotalLiquidatableDeposit = userTotalLiquidatableDeposit.add(depositValueUsd);
779
+ }
780
+
773
781
  borrowLimit = borrowLimit.add(depositValueUsd.mul(maxLtv));
774
782
  borrowLiquidationLimit = borrowLiquidationLimit.add(depositValueUsd.mul(liquidationLtv));
775
783
 
@@ -786,8 +794,9 @@ export class KaminoObligation {
786
794
  deposits,
787
795
  userTotalDeposit,
788
796
  userTotalCollateralDeposit,
797
+ userTotalLiquidatableDeposit,
789
798
  borrowLimit,
790
- liquidationLtv: valueOrZero(borrowLiquidationLimit.div(userTotalDeposit)),
799
+ liquidationLtv: valueOrZero(borrowLiquidationLimit.div(userTotalLiquidatableDeposit)),
791
800
  borrowLiquidationLimit,
792
801
  };
793
802
  }
@@ -865,7 +874,7 @@ export class KaminoObligation {
865
874
  const getOraclePx = (reserve: KaminoReserve) => reserve.getOracleMarketPrice();
866
875
  const { collateralExchangeRates } = KaminoObligation.getRatesForObligation(market, this.state, slot);
867
876
 
868
- const { borrowLimit, userTotalDeposit } = KaminoObligation.calculateObligationDeposits(
877
+ const { borrowLimit, userTotalCollateralDeposit } = KaminoObligation.calculateObligationDeposits(
869
878
  market,
870
879
  this.state.deposits,
871
880
  collateralExchangeRates,
@@ -873,11 +882,11 @@ export class KaminoObligation {
873
882
  getOraclePx
874
883
  );
875
884
 
876
- if (borrowLimit.eq(0) || userTotalDeposit.eq(0)) {
885
+ if (borrowLimit.eq(0) || userTotalCollateralDeposit.eq(0)) {
877
886
  return new Decimal(0);
878
887
  }
879
888
 
880
- return borrowLimit.div(userTotalDeposit);
889
+ return borrowLimit.div(userTotalCollateralDeposit);
881
890
  }
882
891
 
883
892
  /*
@@ -0,0 +1,27 @@
1
+ import { TransactionInstruction } from '@solana/web3.js';
2
+
3
+ /** the populateLUTIxs should be executed in a separate transaction as we cannot create and populate a lookup table in the same tx */
4
+ export type InitVaultIxs = {
5
+ initVaultIxs: TransactionInstruction[];
6
+ populateLUTIxs: TransactionInstruction[];
7
+ };
8
+
9
+ export type AcceptVaultOwnershipIxs = {
10
+ acceptVaultOwnershipIx: TransactionInstruction;
11
+ updateLUTIxs: TransactionInstruction[];
12
+ };
13
+
14
+ export type UpdateReserveAllocationIxs = {
15
+ updateReserveAllocationIx: TransactionInstruction;
16
+ updateLUTIxs: TransactionInstruction[];
17
+ };
18
+
19
+ export type UpdateVaultConfigIxs = {
20
+ updateVaultConfigIx: TransactionInstruction;
21
+ updateLUTIxs: TransactionInstruction[];
22
+ };
23
+
24
+ export type SyncVaultLUTIxs = {
25
+ setupLUTIfNeededIxs: TransactionInstruction[];
26
+ syncLUTIxs: TransactionInstruction[];
27
+ };