@kamino-finance/klend-sdk 6.0.5 → 7.0.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 (97) hide show
  1. package/dist/@codegen/kvault/accounts/Reserve.js +1 -1
  2. package/dist/@codegen/kvault/accounts/VaultState.d.ts +6 -6
  3. package/dist/@codegen/kvault/accounts/VaultState.d.ts.map +1 -1
  4. package/dist/@codegen/kvault/accounts/VaultState.js +12 -12
  5. package/dist/@codegen/kvault/accounts/VaultState.js.map +1 -1
  6. package/dist/@codegen/kvault/errors/custom.d.ts +9 -1
  7. package/dist/@codegen/kvault/errors/custom.d.ts.map +1 -1
  8. package/dist/@codegen/kvault/errors/custom.js +15 -1
  9. package/dist/@codegen/kvault/errors/custom.js.map +1 -1
  10. package/dist/@codegen/kvault/types/VaultConfigField.d.ts +26 -0
  11. package/dist/@codegen/kvault/types/VaultConfigField.d.ts.map +1 -1
  12. package/dist/@codegen/kvault/types/VaultConfigField.js +49 -1
  13. package/dist/@codegen/kvault/types/VaultConfigField.js.map +1 -1
  14. package/dist/@codegen/kvault/types/index.d.ts +2 -2
  15. package/dist/@codegen/kvault/types/index.d.ts.map +1 -1
  16. package/dist/@codegen/kvault/types/index.js.map +1 -1
  17. package/dist/classes/action.d.ts +1 -1
  18. package/dist/classes/action.d.ts.map +1 -1
  19. package/dist/classes/action.js +26 -11
  20. package/dist/classes/action.js.map +1 -1
  21. package/dist/classes/manager.d.ts +25 -6
  22. package/dist/classes/manager.d.ts.map +1 -1
  23. package/dist/classes/manager.js +59 -27
  24. package/dist/classes/manager.js.map +1 -1
  25. package/dist/classes/market.d.ts +7 -2
  26. package/dist/classes/market.d.ts.map +1 -1
  27. package/dist/classes/market.js +12 -6
  28. package/dist/classes/market.js.map +1 -1
  29. package/dist/classes/vault.d.ts +22 -3
  30. package/dist/classes/vault.d.ts.map +1 -1
  31. package/dist/classes/vault.js +73 -40
  32. package/dist/classes/vault.js.map +1 -1
  33. package/dist/classes/vault_types.d.ts +9 -1
  34. package/dist/classes/vault_types.d.ts.map +1 -1
  35. package/dist/client/commands/borrow.d.ts.map +1 -1
  36. package/dist/client/commands/borrow.js +2 -1
  37. package/dist/client/commands/borrow.js.map +1 -1
  38. package/dist/client/commands/deposit.d.ts.map +1 -1
  39. package/dist/client/commands/deposit.js +2 -1
  40. package/dist/client/commands/deposit.js.map +1 -1
  41. package/dist/client/commands/repay.d.ts.map +1 -1
  42. package/dist/client/commands/repay.js +2 -1
  43. package/dist/client/commands/repay.js.map +1 -1
  44. package/dist/client/commands/withdraw.d.ts.map +1 -1
  45. package/dist/client/commands/withdraw.js +2 -1
  46. package/dist/client/commands/withdraw.js.map +1 -1
  47. package/dist/leverage/operations.d.ts.map +1 -1
  48. package/dist/leverage/operations.js +8 -5
  49. package/dist/leverage/operations.js.map +1 -1
  50. package/dist/manager/client_kamino_manager.js +10 -3
  51. package/dist/manager/client_kamino_manager.js.map +1 -1
  52. package/dist/utils/index.d.ts +1 -0
  53. package/dist/utils/index.d.ts.map +1 -1
  54. package/dist/utils/index.js +1 -0
  55. package/dist/utils/index.js.map +1 -1
  56. package/dist/utils/managerTypes.d.ts +2 -3
  57. package/dist/utils/managerTypes.d.ts.map +1 -1
  58. package/dist/utils/managerTypes.js +37 -34
  59. package/dist/utils/managerTypes.js.map +1 -1
  60. package/dist/utils/map.d.ts +2 -0
  61. package/dist/utils/map.d.ts.map +1 -0
  62. package/dist/utils/map.js +13 -0
  63. package/dist/utils/map.js.map +1 -0
  64. package/dist/utils/oracle.d.ts +2 -1
  65. package/dist/utils/oracle.d.ts.map +1 -1
  66. package/dist/utils/oracle.js +4 -4
  67. package/dist/utils/oracle.js.map +1 -1
  68. package/dist/utils/vault.js +2 -2
  69. package/dist/utils/vault.js.map +1 -1
  70. package/dist/utils/vaultAllocation.d.ts +21 -0
  71. package/dist/utils/vaultAllocation.d.ts.map +1 -0
  72. package/dist/utils/vaultAllocation.js +71 -0
  73. package/dist/utils/vaultAllocation.js.map +1 -0
  74. package/package.json +2 -2
  75. package/src/@codegen/kvault/accounts/Reserve.ts +1 -1
  76. package/src/@codegen/kvault/accounts/VaultState.ts +16 -16
  77. package/src/@codegen/kvault/errors/custom.ts +14 -0
  78. package/src/@codegen/kvault/types/VaultConfigField.ts +60 -0
  79. package/src/@codegen/kvault/types/index.ts +4 -0
  80. package/src/classes/action.ts +26 -11
  81. package/src/classes/manager.ts +92 -30
  82. package/src/classes/market.ts +15 -9
  83. package/src/classes/vault.ts +125 -42
  84. package/src/classes/vault_types.ts +11 -1
  85. package/src/client/commands/borrow.ts +2 -1
  86. package/src/client/commands/deposit.ts +2 -1
  87. package/src/client/commands/repay.ts +2 -1
  88. package/src/client/commands/withdraw.ts +2 -1
  89. package/src/idl/kvault.json +34 -12
  90. package/src/leverage/operations.ts +9 -10
  91. package/src/manager/client_kamino_manager.ts +15 -3
  92. package/src/utils/index.ts +1 -0
  93. package/src/utils/managerTypes.ts +29 -28
  94. package/src/utils/map.ts +8 -0
  95. package/src/utils/oracle.ts +4 -3
  96. package/src/utils/vault.ts +2 -2
  97. package/src/utils/vaultAllocation.ts +93 -0
@@ -0,0 +1,93 @@
1
+ import Decimal from 'decimal.js';
2
+ import { Address } from '@solana/kit';
3
+
4
+ export interface ReserveAllocationOverview {
5
+ targetWeight: Decimal;
6
+ tokenAllocationCap: Decimal;
7
+ ctokenAllocation: Decimal;
8
+ }
9
+
10
+ export interface VaultAllocationResult {
11
+ targetUnallocatedAmount: Decimal;
12
+ targetReservesAllocation: Map<Address, Decimal>;
13
+ }
14
+
15
+ const ZERO = new Decimal(0);
16
+ const ONE = new Decimal(1);
17
+
18
+ /**
19
+ * Computes the allocation of vault funds across reserves based on weights and caps
20
+ * @param vaultAUM - Total AUM of the vault
21
+ * @param vaultUnallocatedWeight - Weight for unallocated funds
22
+ * @param vaultUnallocatedCap - Maximum amount that can remain unallocated
23
+ * @param initialVaultAllocations - Map of reserve addresses to their allocation configurations
24
+ * @returns Object containing target unallocated amount and target allocations per reserve
25
+ */
26
+ export function computeReservesAllocation(
27
+ vaultAUM: Decimal,
28
+ vaultUnallocatedWeight: Decimal,
29
+ vaultUnallocatedCap: Decimal,
30
+ initialVaultAllocations: Map<Address, ReserveAllocationOverview>
31
+ ): VaultAllocationResult {
32
+ let totalAllocation = new Decimal(0);
33
+ const allReserves = Array.from(initialVaultAllocations.keys());
34
+ const expectedHoldingsDistribution = new Map<Address, Decimal>();
35
+
36
+ // Initialize reserve allocations and calculate total weight
37
+ allReserves.forEach((reserve) => {
38
+ expectedHoldingsDistribution.set(reserve, ZERO);
39
+ totalAllocation = totalAllocation.add(initialVaultAllocations.get(reserve)!.targetWeight);
40
+ });
41
+
42
+ let totalLeftToInvest = vaultAUM;
43
+ const totalAllocationsIncludingUnallocated = totalAllocation.add(vaultUnallocatedWeight);
44
+
45
+ // Calculate initial unallocated amount
46
+ let unallocatedAllocation = totalLeftToInvest.mul(vaultUnallocatedWeight).div(totalAllocationsIncludingUnallocated);
47
+ if (unallocatedAllocation.gt(vaultUnallocatedCap)) {
48
+ unallocatedAllocation = vaultUnallocatedCap;
49
+ }
50
+ totalLeftToInvest = totalLeftToInvest.sub(unallocatedAllocation);
51
+
52
+ let currentAllocationSum = totalAllocation;
53
+
54
+ // Iteratively allocate funds to reserves based on weights and caps
55
+ while (totalLeftToInvest.gt(ONE) && currentAllocationSum.gt(ZERO)) {
56
+ const totalLeftover = totalLeftToInvest;
57
+
58
+ for (const reserve of allReserves) {
59
+ const reserveWithWeight = initialVaultAllocations.get(reserve);
60
+ if (!reserveWithWeight) continue;
61
+
62
+ const targetAllocation = reserveWithWeight.targetWeight.mul(totalLeftover).div(currentAllocationSum);
63
+ const reserveCap = reserveWithWeight.tokenAllocationCap;
64
+ let amountToInvest = Decimal.min(targetAllocation, totalLeftToInvest);
65
+
66
+ // Handle reserve caps
67
+ if (reserveCap.gt(ZERO)) {
68
+ const currentReserveAllocation = expectedHoldingsDistribution.get(reserve)!;
69
+ const remainingCapacity = reserveCap.sub(currentReserveAllocation);
70
+ amountToInvest = Decimal.min(amountToInvest, remainingCapacity);
71
+ } else if (reserveCap.eq(ZERO)) {
72
+ // Zero cap means no investment allowed
73
+ amountToInvest = ZERO;
74
+ }
75
+
76
+ totalLeftToInvest = totalLeftToInvest.sub(amountToInvest);
77
+
78
+ // Check if reserve is now capped and should be removed from future allocations
79
+ const newReserveAllocation = expectedHoldingsDistribution.get(reserve)!.add(amountToInvest);
80
+ if (reserveCap.eq(ZERO) || (reserveCap.gt(ZERO) && newReserveAllocation.gte(reserveCap))) {
81
+ currentAllocationSum = currentAllocationSum.sub(reserveWithWeight.targetWeight);
82
+ }
83
+
84
+ // Update reserve allocation
85
+ expectedHoldingsDistribution.set(reserve, newReserveAllocation);
86
+ }
87
+ }
88
+
89
+ return {
90
+ targetUnallocatedAmount: unallocatedAllocation.add(totalLeftToInvest),
91
+ targetReservesAllocation: expectedHoldingsDistribution,
92
+ };
93
+ }