@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.
- package/dist/@codegen/kvault/accounts/Reserve.js +1 -1
- package/dist/@codegen/kvault/accounts/VaultState.d.ts +6 -6
- package/dist/@codegen/kvault/accounts/VaultState.d.ts.map +1 -1
- package/dist/@codegen/kvault/accounts/VaultState.js +12 -12
- package/dist/@codegen/kvault/accounts/VaultState.js.map +1 -1
- package/dist/@codegen/kvault/errors/custom.d.ts +9 -1
- package/dist/@codegen/kvault/errors/custom.d.ts.map +1 -1
- package/dist/@codegen/kvault/errors/custom.js +15 -1
- package/dist/@codegen/kvault/errors/custom.js.map +1 -1
- package/dist/@codegen/kvault/types/VaultConfigField.d.ts +26 -0
- package/dist/@codegen/kvault/types/VaultConfigField.d.ts.map +1 -1
- package/dist/@codegen/kvault/types/VaultConfigField.js +49 -1
- package/dist/@codegen/kvault/types/VaultConfigField.js.map +1 -1
- package/dist/@codegen/kvault/types/index.d.ts +2 -2
- package/dist/@codegen/kvault/types/index.d.ts.map +1 -1
- package/dist/@codegen/kvault/types/index.js.map +1 -1
- package/dist/classes/action.d.ts +1 -1
- package/dist/classes/action.d.ts.map +1 -1
- package/dist/classes/action.js +26 -11
- package/dist/classes/action.js.map +1 -1
- package/dist/classes/manager.d.ts +25 -6
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +59 -27
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/market.d.ts +7 -2
- package/dist/classes/market.d.ts.map +1 -1
- package/dist/classes/market.js +12 -6
- package/dist/classes/market.js.map +1 -1
- package/dist/classes/vault.d.ts +22 -3
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +73 -40
- package/dist/classes/vault.js.map +1 -1
- package/dist/classes/vault_types.d.ts +9 -1
- package/dist/classes/vault_types.d.ts.map +1 -1
- package/dist/client/commands/borrow.d.ts.map +1 -1
- package/dist/client/commands/borrow.js +2 -1
- package/dist/client/commands/borrow.js.map +1 -1
- package/dist/client/commands/deposit.d.ts.map +1 -1
- package/dist/client/commands/deposit.js +2 -1
- package/dist/client/commands/deposit.js.map +1 -1
- package/dist/client/commands/repay.d.ts.map +1 -1
- package/dist/client/commands/repay.js +2 -1
- package/dist/client/commands/repay.js.map +1 -1
- package/dist/client/commands/withdraw.d.ts.map +1 -1
- package/dist/client/commands/withdraw.js +2 -1
- package/dist/client/commands/withdraw.js.map +1 -1
- package/dist/leverage/operations.d.ts.map +1 -1
- package/dist/leverage/operations.js +8 -5
- package/dist/leverage/operations.js.map +1 -1
- package/dist/manager/client_kamino_manager.js +10 -3
- package/dist/manager/client_kamino_manager.js.map +1 -1
- package/dist/utils/index.d.ts +1 -0
- package/dist/utils/index.d.ts.map +1 -1
- package/dist/utils/index.js +1 -0
- package/dist/utils/index.js.map +1 -1
- package/dist/utils/managerTypes.d.ts +2 -3
- package/dist/utils/managerTypes.d.ts.map +1 -1
- package/dist/utils/managerTypes.js +37 -34
- package/dist/utils/managerTypes.js.map +1 -1
- package/dist/utils/map.d.ts +2 -0
- package/dist/utils/map.d.ts.map +1 -0
- package/dist/utils/map.js +13 -0
- package/dist/utils/map.js.map +1 -0
- package/dist/utils/oracle.d.ts +2 -1
- package/dist/utils/oracle.d.ts.map +1 -1
- package/dist/utils/oracle.js +4 -4
- package/dist/utils/oracle.js.map +1 -1
- package/dist/utils/vault.js +2 -2
- package/dist/utils/vault.js.map +1 -1
- package/dist/utils/vaultAllocation.d.ts +21 -0
- package/dist/utils/vaultAllocation.d.ts.map +1 -0
- package/dist/utils/vaultAllocation.js +71 -0
- package/dist/utils/vaultAllocation.js.map +1 -0
- package/package.json +2 -2
- package/src/@codegen/kvault/accounts/Reserve.ts +1 -1
- package/src/@codegen/kvault/accounts/VaultState.ts +16 -16
- package/src/@codegen/kvault/errors/custom.ts +14 -0
- package/src/@codegen/kvault/types/VaultConfigField.ts +60 -0
- package/src/@codegen/kvault/types/index.ts +4 -0
- package/src/classes/action.ts +26 -11
- package/src/classes/manager.ts +92 -30
- package/src/classes/market.ts +15 -9
- package/src/classes/vault.ts +125 -42
- package/src/classes/vault_types.ts +11 -1
- package/src/client/commands/borrow.ts +2 -1
- package/src/client/commands/deposit.ts +2 -1
- package/src/client/commands/repay.ts +2 -1
- package/src/client/commands/withdraw.ts +2 -1
- package/src/idl/kvault.json +34 -12
- package/src/leverage/operations.ts +9 -10
- package/src/manager/client_kamino_manager.ts +15 -3
- package/src/utils/index.ts +1 -0
- package/src/utils/managerTypes.ts +29 -28
- package/src/utils/map.ts +8 -0
- package/src/utils/oracle.ts +4 -3
- package/src/utils/vault.ts +2 -2
- 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
|
+
}
|