@kamino-finance/klend-sdk 7.1.1 → 7.1.3

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 (175) hide show
  1. package/dist/@codegen/unstaking_pool/accounts/PoolState.d.ts +53 -0
  2. package/dist/@codegen/unstaking_pool/accounts/PoolState.d.ts.map +1 -0
  3. package/dist/@codegen/unstaking_pool/accounts/PoolState.js +167 -0
  4. package/dist/@codegen/unstaking_pool/accounts/PoolState.js.map +1 -0
  5. package/dist/@codegen/unstaking_pool/accounts/UnstakeTicket.d.ts +41 -0
  6. package/dist/@codegen/unstaking_pool/accounts/UnstakeTicket.d.ts.map +1 -0
  7. package/dist/@codegen/unstaking_pool/accounts/UnstakeTicket.js +143 -0
  8. package/dist/@codegen/unstaking_pool/accounts/UnstakeTicket.js.map +1 -0
  9. package/dist/@codegen/unstaking_pool/accounts/index.d.ts +5 -0
  10. package/dist/@codegen/unstaking_pool/accounts/index.d.ts.map +1 -0
  11. package/dist/@codegen/unstaking_pool/accounts/index.js +8 -0
  12. package/dist/@codegen/unstaking_pool/accounts/index.js.map +1 -0
  13. package/dist/@codegen/unstaking_pool/errors/anchor.d.ts +435 -0
  14. package/dist/@codegen/unstaking_pool/errors/anchor.d.ts.map +1 -0
  15. package/dist/@codegen/unstaking_pool/errors/anchor.js +767 -0
  16. package/dist/@codegen/unstaking_pool/errors/anchor.js.map +1 -0
  17. package/dist/@codegen/unstaking_pool/errors/custom.d.ts +259 -0
  18. package/dist/@codegen/unstaking_pool/errors/custom.d.ts.map +1 -0
  19. package/dist/@codegen/unstaking_pool/errors/custom.js +458 -0
  20. package/dist/@codegen/unstaking_pool/errors/custom.js.map +1 -0
  21. package/dist/@codegen/unstaking_pool/errors/index.d.ts +6 -0
  22. package/dist/@codegen/unstaking_pool/errors/index.d.ts.map +1 -0
  23. package/dist/@codegen/unstaking_pool/errors/index.js +86 -0
  24. package/dist/@codegen/unstaking_pool/errors/index.js.map +1 -0
  25. package/dist/@codegen/unstaking_pool/instructions/burn.d.ts +21 -0
  26. package/dist/@codegen/unstaking_pool/instructions/burn.d.ts.map +1 -0
  27. package/dist/@codegen/unstaking_pool/instructions/burn.js +67 -0
  28. package/dist/@codegen/unstaking_pool/instructions/burn.js.map +1 -0
  29. package/dist/@codegen/unstaking_pool/instructions/changeTicketAuthority.d.ts +8 -0
  30. package/dist/@codegen/unstaking_pool/instructions/changeTicketAuthority.d.ts.map +1 -0
  31. package/dist/@codegen/unstaking_pool/instructions/changeTicketAuthority.js +16 -0
  32. package/dist/@codegen/unstaking_pool/instructions/changeTicketAuthority.js.map +1 -0
  33. package/dist/@codegen/unstaking_pool/instructions/collect.d.ts +16 -0
  34. package/dist/@codegen/unstaking_pool/instructions/collect.d.ts.map +1 -0
  35. package/dist/@codegen/unstaking_pool/instructions/collect.js +24 -0
  36. package/dist/@codegen/unstaking_pool/instructions/collect.js.map +1 -0
  37. package/dist/@codegen/unstaking_pool/instructions/index.d.ts +17 -0
  38. package/dist/@codegen/unstaking_pool/instructions/index.d.ts.map +1 -0
  39. package/dist/@codegen/unstaking_pool/instructions/index.js +20 -0
  40. package/dist/@codegen/unstaking_pool/instructions/index.js.map +1 -0
  41. package/dist/@codegen/unstaking_pool/instructions/initializePool.d.ts +14 -0
  42. package/dist/@codegen/unstaking_pool/instructions/initializePool.d.ts.map +1 -0
  43. package/dist/@codegen/unstaking_pool/instructions/initializePool.js +22 -0
  44. package/dist/@codegen/unstaking_pool/instructions/initializePool.js.map +1 -0
  45. package/dist/@codegen/unstaking_pool/instructions/mint.d.ts +24 -0
  46. package/dist/@codegen/unstaking_pool/instructions/mint.d.ts.map +1 -0
  47. package/dist/@codegen/unstaking_pool/instructions/mint.js +74 -0
  48. package/dist/@codegen/unstaking_pool/instructions/mint.js.map +1 -0
  49. package/dist/@codegen/unstaking_pool/instructions/splitTicket.d.ts +15 -0
  50. package/dist/@codegen/unstaking_pool/instructions/splitTicket.d.ts.map +1 -0
  51. package/dist/@codegen/unstaking_pool/instructions/splitTicket.js +62 -0
  52. package/dist/@codegen/unstaking_pool/instructions/splitTicket.js.map +1 -0
  53. package/dist/@codegen/unstaking_pool/instructions/updateAdmin.d.ts +7 -0
  54. package/dist/@codegen/unstaking_pool/instructions/updateAdmin.d.ts.map +1 -0
  55. package/dist/@codegen/unstaking_pool/instructions/updateAdmin.js +19 -0
  56. package/dist/@codegen/unstaking_pool/instructions/updateAdmin.js.map +1 -0
  57. package/dist/@codegen/unstaking_pool/instructions/updatePoolConfig.d.ts +13 -0
  58. package/dist/@codegen/unstaking_pool/instructions/updatePoolConfig.d.ts.map +1 -0
  59. package/dist/@codegen/unstaking_pool/instructions/updatePoolConfig.js +60 -0
  60. package/dist/@codegen/unstaking_pool/instructions/updatePoolConfig.js.map +1 -0
  61. package/dist/@codegen/unstaking_pool/programId.d.ts +4 -0
  62. package/dist/@codegen/unstaking_pool/programId.d.ts.map +1 -0
  63. package/dist/@codegen/unstaking_pool/programId.js +9 -0
  64. package/dist/@codegen/unstaking_pool/programId.js.map +1 -0
  65. package/dist/@codegen/unstaking_pool/types/PoolConfigField.d.ts +45 -0
  66. package/dist/@codegen/unstaking_pool/types/PoolConfigField.d.ts.map +1 -0
  67. package/dist/@codegen/unstaking_pool/types/PoolConfigField.js +132 -0
  68. package/dist/@codegen/unstaking_pool/types/PoolConfigField.js.map +1 -0
  69. package/dist/@codegen/unstaking_pool/types/index.d.ts +5 -0
  70. package/dist/@codegen/unstaking_pool/types/index.d.ts.map +1 -0
  71. package/dist/@codegen/unstaking_pool/types/index.js +39 -0
  72. package/dist/@codegen/unstaking_pool/types/index.js.map +1 -0
  73. package/dist/@codegen/unstaking_pool/utils/borshAddress.d.ts +4 -0
  74. package/dist/@codegen/unstaking_pool/utils/borshAddress.d.ts.map +1 -0
  75. package/dist/@codegen/unstaking_pool/utils/borshAddress.js +30 -0
  76. package/dist/@codegen/unstaking_pool/utils/borshAddress.js.map +1 -0
  77. package/dist/@codegen/unstaking_pool/utils/index.d.ts +2 -0
  78. package/dist/@codegen/unstaking_pool/utils/index.d.ts.map +1 -0
  79. package/dist/@codegen/unstaking_pool/utils/index.js +18 -0
  80. package/dist/@codegen/unstaking_pool/utils/index.js.map +1 -0
  81. package/dist/classes/action.d.ts.map +1 -1
  82. package/dist/classes/action.js +5 -5
  83. package/dist/classes/action.js.map +1 -1
  84. package/dist/classes/index.d.ts +5 -0
  85. package/dist/classes/index.d.ts.map +1 -1
  86. package/dist/classes/index.js +5 -0
  87. package/dist/classes/index.js.map +1 -1
  88. package/dist/classes/manager.d.ts.map +1 -1
  89. package/dist/classes/manager.js +2 -3
  90. package/dist/classes/manager.js.map +1 -1
  91. package/dist/classes/stakePool.d.ts +8 -0
  92. package/dist/classes/stakePool.d.ts.map +1 -0
  93. package/dist/classes/stakePool.js +18 -0
  94. package/dist/classes/stakePool.js.map +1 -0
  95. package/dist/classes/standardStakePool.d.ts +76 -0
  96. package/dist/classes/standardStakePool.d.ts.map +1 -0
  97. package/dist/classes/standardStakePool.js +400 -0
  98. package/dist/classes/standardStakePool.js.map +1 -0
  99. package/dist/classes/unstakingPool.d.ts +115 -0
  100. package/dist/classes/unstakingPool.d.ts.map +1 -0
  101. package/dist/classes/unstakingPool.js +372 -0
  102. package/dist/classes/unstakingPool.js.map +1 -0
  103. package/dist/classes/unstakingPoolTypes.d.ts +10 -0
  104. package/dist/classes/unstakingPoolTypes.d.ts.map +1 -0
  105. package/dist/classes/unstakingPoolTypes.js +3 -0
  106. package/dist/classes/unstakingPoolTypes.js.map +1 -0
  107. package/dist/classes/vault.d.ts +0 -9
  108. package/dist/classes/vault.d.ts.map +1 -1
  109. package/dist/classes/vault.js +6 -41
  110. package/dist/classes/vault.js.map +1 -1
  111. package/dist/lending_operations/repay_with_collateral_operations.d.ts.map +1 -1
  112. package/dist/lending_operations/repay_with_collateral_operations.js +0 -2
  113. package/dist/lending_operations/repay_with_collateral_operations.js.map +1 -1
  114. package/dist/lending_operations/swap_collateral_operations.js +0 -1
  115. package/dist/lending_operations/swap_collateral_operations.js.map +1 -1
  116. package/dist/leverage/calcs.d.ts +2 -27
  117. package/dist/leverage/calcs.d.ts.map +1 -1
  118. package/dist/leverage/calcs.js +6 -136
  119. package/dist/leverage/calcs.js.map +1 -1
  120. package/dist/leverage/operations.d.ts +8 -9
  121. package/dist/leverage/operations.d.ts.map +1 -1
  122. package/dist/leverage/operations.js +72 -228
  123. package/dist/leverage/operations.js.map +1 -1
  124. package/dist/leverage/types.d.ts +0 -19
  125. package/dist/leverage/types.d.ts.map +1 -1
  126. package/dist/leverage/utils.d.ts +2 -19
  127. package/dist/leverage/utils.d.ts.map +1 -1
  128. package/dist/leverage/utils.js +0 -164
  129. package/dist/leverage/utils.js.map +1 -1
  130. package/dist/lib.d.ts +1 -0
  131. package/dist/lib.d.ts.map +1 -1
  132. package/dist/lib.js +3 -1
  133. package/dist/lib.js.map +1 -1
  134. package/dist/utils/lookupTable.d.ts +9 -0
  135. package/dist/utils/lookupTable.d.ts.map +1 -1
  136. package/dist/utils/lookupTable.js +37 -0
  137. package/dist/utils/lookupTable.js.map +1 -1
  138. package/package.json +4 -2
  139. package/src/@codegen/unstaking_pool/accounts/PoolState.ts +188 -0
  140. package/src/@codegen/unstaking_pool/accounts/UnstakeTicket.ts +156 -0
  141. package/src/@codegen/unstaking_pool/accounts/index.ts +4 -0
  142. package/src/@codegen/unstaking_pool/errors/anchor.ts +773 -0
  143. package/src/@codegen/unstaking_pool/errors/custom.ts +477 -0
  144. package/src/@codegen/unstaking_pool/errors/index.ts +68 -0
  145. package/src/@codegen/unstaking_pool/instructions/burn.ts +70 -0
  146. package/src/@codegen/unstaking_pool/instructions/changeTicketAuthority.ts +37 -0
  147. package/src/@codegen/unstaking_pool/instructions/collect.ts +53 -0
  148. package/src/@codegen/unstaking_pool/instructions/index.ts +19 -0
  149. package/src/@codegen/unstaking_pool/instructions/initializePool.ts +49 -0
  150. package/src/@codegen/unstaking_pool/instructions/mint.ts +80 -0
  151. package/src/@codegen/unstaking_pool/instructions/splitTicket.ts +59 -0
  152. package/src/@codegen/unstaking_pool/instructions/updateAdmin.ts +39 -0
  153. package/src/@codegen/unstaking_pool/instructions/updatePoolConfig.ts +58 -0
  154. package/src/@codegen/unstaking_pool/programId.ts +9 -0
  155. package/src/@codegen/unstaking_pool/types/PoolConfigField.ts +121 -0
  156. package/src/@codegen/unstaking_pool/types/index.ts +12 -0
  157. package/src/@codegen/unstaking_pool/utils/borshAddress.ts +43 -0
  158. package/src/@codegen/unstaking_pool/utils/index.ts +1 -0
  159. package/src/classes/action.ts +10 -5
  160. package/src/classes/index.ts +5 -0
  161. package/src/classes/manager.ts +3 -4
  162. package/src/classes/stakePool.ts +21 -0
  163. package/src/classes/standardStakePool.ts +487 -0
  164. package/src/classes/unstakingPool.ts +504 -0
  165. package/src/classes/unstakingPoolTypes.ts +12 -0
  166. package/src/classes/vault.ts +43 -56
  167. package/src/idl/unstaking_pool.json +662 -0
  168. package/src/lending_operations/repay_with_collateral_operations.ts +0 -2
  169. package/src/lending_operations/swap_collateral_operations.ts +0 -1
  170. package/src/leverage/calcs.ts +2 -201
  171. package/src/leverage/operations.ts +45 -377
  172. package/src/leverage/types.ts +0 -20
  173. package/src/leverage/utils.ts +3 -320
  174. package/src/lib.ts +1 -0
  175. package/src/utils/lookupTable.ts +48 -0
@@ -1,7 +1,6 @@
1
1
  import { Account, Address, Instruction, Option, Slot, TransactionSigner } from '@solana/kit';
2
2
  import Decimal from 'decimal.js';
3
3
  import { KaminoMarket, KaminoObligation } from '../classes';
4
- import { Kamino, StrategyWithAddress } from '@kamino-finance/kliquidity-sdk';
5
4
  import { ObligationType, ObligationTypeTag, ScopePriceRefreshConfig } from '../utils';
6
5
  import { AddressLookupTable } from '@solana-program/address-lookup-table';
7
6
 
@@ -30,8 +29,6 @@ export type SwapIxs<QuoteResponse> = {
30
29
 
31
30
  export type PriceAinBProvider = (mintA: Address, mintB: Address) => Promise<Decimal>;
32
31
 
33
- export type IsKtokenProvider = (token: Address) => Promise<boolean>;
34
-
35
32
  export type FlashLoanInfo = {
36
33
  flashBorrowReserve: Address;
37
34
  flashLoanFee: Decimal;
@@ -47,7 +44,6 @@ export type SwapInputs = {
47
44
  minOutAmountLamports?: Decimal;
48
45
  inputMint: Address;
49
46
  outputMint: Address;
50
- amountDebtAtaBalance: Decimal | undefined;
51
47
  };
52
48
 
53
49
  export type BaseLeverageIxsResponse<QuoteResponse> = {
@@ -63,9 +59,7 @@ export type LeverageInitialInputs<LeverageCalcsResult, QuoteResponse> = {
63
59
  swapQuote: SwapQuote<QuoteResponse>;
64
60
  currentSlot: Slot;
65
61
  klendAccounts: Array<Address>;
66
- collIsKtoken: boolean;
67
62
  obligation: KaminoObligation | ObligationType | undefined;
68
- strategy: StrategyWithAddress | undefined;
69
63
  };
70
64
 
71
65
  export interface BaseLeverageSwapInputsProps<QuoteResponse> {
@@ -77,10 +71,8 @@ export interface BaseLeverageSwapInputsProps<QuoteResponse> {
77
71
  currentSlot: Slot;
78
72
  slippagePct: Decimal;
79
73
  budgetAndPriorityFeeIxs?: Instruction[];
80
- kamino: Kamino | undefined;
81
74
  scopeRefreshConfig?: ScopePriceRefreshConfig;
82
75
  quoteBufferBps: Decimal;
83
- isKtoken: IsKtokenProvider;
84
76
  quoter: SwapQuoteProvider<QuoteResponse>;
85
77
  useV2Ixs: boolean;
86
78
  }
@@ -94,9 +86,7 @@ export type DepositLeverageInitialInputs<QuoteResponse> = {
94
86
  swapQuote: SwapQuote<QuoteResponse>;
95
87
  currentSlot: Slot;
96
88
  klendAccounts: Array<Address>;
97
- collIsKtoken: boolean;
98
89
  obligation: KaminoObligation | ObligationType | undefined;
99
- strategy: StrategyWithAddress | undefined;
100
90
  };
101
91
 
102
92
  export interface DepositWithLeverageSwapInputsProps<QuoteResponse> extends BaseLeverageSwapInputsProps<QuoteResponse> {
@@ -106,7 +96,6 @@ export interface DepositWithLeverageSwapInputsProps<QuoteResponse> extends BaseL
106
96
  priceDebtToColl: Decimal;
107
97
  targetLeverage: Decimal;
108
98
  selectedTokenMint: Address;
109
- priceAinB: PriceAinBProvider;
110
99
  // currently only used to disable requesting elevation group when this value is 0
111
100
  // to be implemented properly in the future
112
101
  elevationGroupOverride?: number;
@@ -123,9 +112,6 @@ export type DepositLeverageCalcsResult = {
123
112
  collTokenToDeposit: Decimal;
124
113
  swapDebtTokenIn: Decimal;
125
114
  swapCollTokenExpectedOut: Decimal;
126
- flashBorrowInDebtTokenKtokenOnly: Decimal;
127
- singleSidedDepositKtokenOnly: Decimal;
128
- requiredCollateralKtokenOnly: Decimal;
129
115
  };
130
116
 
131
117
  export type WithdrawLeverageIxsResponse<QuoteResponse> = BaseLeverageIxsResponse<QuoteResponse> & {
@@ -137,9 +123,7 @@ export type WithdrawLeverageInitialInputs<QuoteResponse> = {
137
123
  swapQuote: SwapQuote<QuoteResponse>;
138
124
  currentSlot: Slot;
139
125
  klendAccounts: Array<Address>;
140
- collIsKtoken: boolean;
141
126
  obligation: KaminoObligation | ObligationType | undefined;
142
- strategy: StrategyWithAddress | undefined;
143
127
  };
144
128
 
145
129
  export interface WithdrawWithLeverageSwapInputsProps<QuoteResponse> extends BaseLeverageSwapInputsProps<QuoteResponse> {
@@ -176,9 +160,7 @@ export type AdjustLeverageInitialInputs<QuoteResponse> = {
176
160
  currentSlot: Slot;
177
161
  klendAccounts: Array<Address>;
178
162
  isDeposit: boolean;
179
- collIsKtoken: boolean;
180
163
  obligation: KaminoObligation | ObligationType | undefined;
181
- strategy: StrategyWithAddress | undefined;
182
164
  };
183
165
 
184
166
  export interface AdjustLeverageSwapInputsProps<QuoteResponse> extends BaseLeverageSwapInputsProps<QuoteResponse> {
@@ -188,7 +170,6 @@ export interface AdjustLeverageSwapInputsProps<QuoteResponse> extends BaseLevera
188
170
  targetLeverage: Decimal;
189
171
  priceCollToDebt: Decimal;
190
172
  priceDebtToColl: Decimal;
191
- priceAinB: PriceAinBProvider;
192
173
  withdrawSlotOffset?: number;
193
174
  }
194
175
 
@@ -201,6 +182,5 @@ export type AdjustLeverageCalcsResult = {
201
182
  adjustBorrowPosition: Decimal;
202
183
  amountToFlashBorrowDebt: Decimal;
203
184
  borrowAmount: Decimal;
204
- expectedDebtTokenAtaBalance: Decimal;
205
185
  withdrawAmountWithSlippageAndFlashLoanFee: Decimal;
206
186
  };
@@ -1,325 +1,8 @@
1
- import { Kamino, StrategyWithAddress } from '@kamino-finance/kliquidity-sdk';
2
- import { KaminoMarket, KaminoReserve, lamportsToNumberDecimal } from '../classes';
3
- import {
4
- Address,
5
- Instruction,
6
- GetAccountInfoApi,
7
- Rpc,
8
- GetTokenAccountBalanceApi,
9
- TransactionSigner,
10
- } from '@solana/kit';
1
+ import { KaminoReserve, lamportsToNumberDecimal } from '../classes';
2
+ import { Address, GetAccountInfoApi, Rpc, GetTokenAccountBalanceApi } from '@solana/kit';
11
3
  import Decimal from 'decimal.js';
12
- import { getLookupTableAccounts, getTokenAccountBalanceDecimal } from '../utils';
13
- import { numberToLamportsDecimal } from '../classes/utils';
4
+ import { getTokenAccountBalanceDecimal } from '../utils';
14
5
  import BN from 'bn.js';
15
- import { PriceAinBProvider, SwapInputs, SwapQuote, SwapIxs, SwapIxsProvider } from './types';
16
-
17
- export interface KaminoSwapperIxBuilder {
18
- (
19
- input: DepositAmountsForSwap,
20
- tokenAMint: Address,
21
- tokenBMint: Address,
22
- owner: TransactionSigner,
23
- slippage: Decimal,
24
- allKeys: Address[]
25
- ): Promise<[Instruction[], Address[]]>;
26
- }
27
-
28
- export interface DepositAmountsForSwap {
29
- requiredAAmountToDeposit: Decimal;
30
- requiredBAmountToDeposit: Decimal;
31
- tokenAToSwapAmount: Decimal;
32
- tokenBToSwapAmount: Decimal;
33
- }
34
-
35
- export async function getTokenToKtokenSwapper<QuoteResponse>(
36
- kaminoMarket: KaminoMarket,
37
- kamino: Kamino,
38
- depositor: TransactionSigner,
39
- slippagePct: Decimal,
40
- swapper: SwapIxsProvider<QuoteResponse>,
41
- priceAinB: PriceAinBProvider,
42
- includeAtaIxs: boolean = true
43
- ): Promise<SwapIxsProvider<QuoteResponse>> {
44
- return async (
45
- inputs: SwapInputs,
46
- klendAccounts: Array<Address>,
47
- quote: SwapQuote<QuoteResponse>
48
- ): Promise<Array<SwapIxs<QuoteResponse>>> => {
49
- const slippageBps = new Decimal(slippagePct).mul('100');
50
- const mintInDecimals = kaminoMarket.getExistingReserveByMint(inputs.inputMint).getMintDecimals();
51
- const amountIn = lamportsToNumberDecimal(inputs.inputAmountLamports, mintInDecimals);
52
- console.debug('Depositing token', inputs.inputMint, ' for ', inputs.outputMint, 'ktoken');
53
- if (inputs.amountDebtAtaBalance === undefined) {
54
- throw Error('Amount in debt ATA balance is undefined for leverage ktoken deposit');
55
- }
56
-
57
- const ixWithLookup = (await getKtokenDepositIxs(
58
- kaminoMarket.getRpc(),
59
- kamino,
60
- depositor,
61
- inputs.inputMint,
62
- inputs.outputMint,
63
- amountIn,
64
- slippageBps,
65
- inputs.amountDebtAtaBalance,
66
- swapper,
67
- priceAinB,
68
- includeAtaIxs,
69
- klendAccounts,
70
- quote
71
- ))!;
72
-
73
- const luts = await getLookupTableAccounts(kaminoMarket.getRpc(), ixWithLookup.lookupTablesAddresses);
74
-
75
- return [
76
- {
77
- preActionIxs: [],
78
- swapIxs: ixWithLookup.instructions,
79
- lookupTables: luts,
80
- // TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
81
- quote: {
82
- priceAInB: new Decimal(0),
83
- quoteResponse: undefined,
84
- },
85
- },
86
- ];
87
- };
88
- }
89
-
90
- export async function getKtokenDepositIxs<QuoteResponse>(
91
- rpc: Rpc<GetAccountInfoApi & GetTokenAccountBalanceApi>,
92
- kamino: Kamino,
93
- depositor: TransactionSigner,
94
- depositTokenMint: Address,
95
- ktokenMint: Address,
96
- amountToDeposit: Decimal,
97
- slippageBps: Decimal,
98
- amountExpectedDepositAtaBalance: Decimal,
99
- swapper: SwapIxsProvider<QuoteResponse>,
100
- priceAinB: PriceAinBProvider,
101
- includeAtaIxs: boolean = true,
102
- klendAccounts: Array<Address>,
103
- quote: SwapQuote<QuoteResponse>
104
- ) {
105
- const kaminoStrategy = await kamino.getStrategyByKTokenMint(ktokenMint);
106
- const tokenAMint = kaminoStrategy?.strategy.tokenAMint!;
107
- const tokenBMint = kaminoStrategy?.strategy.tokenBMint!;
108
- const priceAinBDecimal = await priceAinB(tokenAMint, tokenBMint);
109
-
110
- if (tokenAMint === depositTokenMint) {
111
- const bBalance = await getTokenAccountBalanceDecimal(rpc, tokenBMint, depositor.address);
112
- const tokensBalances = { a: amountExpectedDepositAtaBalance, b: bBalance };
113
- console.log('amountToDeposit', amountToDeposit);
114
- return await kamino.singleSidedDepositTokenA(
115
- kaminoStrategy!,
116
- amountToDeposit,
117
- depositor,
118
- slippageBps,
119
- undefined,
120
- swapProviderToKaminoSwapProvider(swapper, klendAccounts, quote),
121
- tokensBalances,
122
- priceAinBDecimal,
123
- includeAtaIxs
124
- );
125
- } else if (tokenBMint === depositTokenMint) {
126
- const aBalance = await getTokenAccountBalanceDecimal(rpc, tokenAMint, depositor.address);
127
- const tokensBalances = { a: aBalance, b: amountExpectedDepositAtaBalance };
128
- return await kamino.singleSidedDepositTokenB(
129
- kaminoStrategy!,
130
- amountToDeposit,
131
- depositor,
132
- slippageBps,
133
- undefined,
134
- swapProviderToKaminoSwapProvider(swapper, klendAccounts, quote),
135
- tokensBalances,
136
- priceAinBDecimal,
137
- includeAtaIxs
138
- );
139
- } else {
140
- throw Error('Deposit token is neither A nor B in the strategy');
141
- }
142
- }
143
-
144
- export async function getKtokenToTokenSwapper<QuoteResponse>(
145
- kaminoMarket: KaminoMarket,
146
- kamino: Kamino,
147
- depositor: TransactionSigner,
148
- swapper: SwapIxsProvider<QuoteResponse>
149
- ): Promise<SwapIxsProvider<QuoteResponse>> {
150
- return async (inputs: SwapInputs, klendAccounts: Array<Address>, quote: SwapQuote<QuoteResponse>) => {
151
- const amountInDecimals = kaminoMarket.getExistingReserveByMint(inputs.inputMint).getMintDecimals();
152
- const amountToWithdraw = lamportsToNumberDecimal(inputs.inputAmountLamports, amountInDecimals);
153
- const kaminoStrategy = await kamino.getStrategyByKTokenMint(inputs.inputMint);
154
-
155
- console.log('Withdrawing ktoken', inputs.inputMint.toString(), ' for ', inputs.outputMint.toString(), 'token');
156
-
157
- const ixWithdraw = (await getKtokenWithdrawIxs(kamino, depositor, kaminoStrategy!, amountToWithdraw))!;
158
-
159
- const [estimatedAOut, estimatedBOut] = await getKtokenWithdrawEstimatesAndPrice(
160
- kamino,
161
- kaminoStrategy!,
162
- amountToWithdraw
163
- );
164
-
165
- if (inputs.outputMint === kaminoStrategy!.strategy.tokenAMint!) {
166
- const swapArray = await swapper(
167
- {
168
- inputAmountLamports: estimatedBOut,
169
- inputMint: kaminoStrategy!.strategy.tokenBMint!,
170
- outputMint: kaminoStrategy!.strategy.tokenAMint!,
171
- amountDebtAtaBalance: new Decimal(0),
172
- },
173
- klendAccounts,
174
- quote
175
- );
176
- // TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
177
- const swap = swapArray[0];
178
-
179
- return [
180
- {
181
- preActionIxs: [],
182
- swapIxs: [...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swap.swapIxs],
183
- lookupTables: swap.lookupTables,
184
- quote: swap.quote,
185
- },
186
- ];
187
- } else if (inputs.outputMint === kaminoStrategy!.strategy.tokenBMint) {
188
- const swapArray = await swapper(
189
- {
190
- inputAmountLamports: estimatedAOut,
191
- inputMint: kaminoStrategy!.strategy.tokenAMint!,
192
- outputMint: kaminoStrategy!.strategy.tokenBMint!,
193
- amountDebtAtaBalance: new Decimal(0),
194
- },
195
- klendAccounts,
196
- quote
197
- );
198
- // TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
199
- const swap = swapArray[0];
200
-
201
- return [
202
- {
203
- preActionIxs: [],
204
- swapIxs: [...ixWithdraw.prerequisiteIxs, ixWithdraw.withdrawIx, ...swap.swapIxs],
205
- lookupTables: swap.lookupTables,
206
- quote: swap.quote,
207
- },
208
- ];
209
- } else {
210
- throw Error('Deposit token is neither A nor B in the strategy');
211
- }
212
- };
213
- }
214
-
215
- export async function getKtokenWithdrawIxs(
216
- kamino: Kamino,
217
- withdrawer: TransactionSigner,
218
- kaminoStrategy: StrategyWithAddress,
219
- amountToWithdraw: Decimal
220
- ) {
221
- return await kamino.withdrawShares(kaminoStrategy!, amountToWithdraw, withdrawer);
222
- }
223
-
224
- export async function getKtokenWithdrawEstimatesAndPrice(
225
- kamino: Kamino,
226
- kaminoStrategy: StrategyWithAddress,
227
- amountToWithdraw: Decimal
228
- ) {
229
- const sharesData = await kamino.getStrategyShareData(kaminoStrategy);
230
- const withdrawPct = amountToWithdraw
231
- .div(
232
- lamportsToNumberDecimal(
233
- new Decimal(kaminoStrategy.strategy.sharesIssued.toString()),
234
- kaminoStrategy.strategy.sharesMintDecimals.toNumber()
235
- )
236
- )
237
- .toDecimalPlaces(18);
238
-
239
- const withdrawFee = new Decimal(10_000).sub(new Decimal(kaminoStrategy.strategy.withdrawFee.toString()));
240
-
241
- // TODO: Mihai/Marius improve - currently subtracting due to decimal accuracy issues compared to yvaults SC
242
- // for both A and B op: .sub(0.000002)
243
-
244
- const estimatedAOut = sharesData.balance.computedHoldings.invested.a
245
- .add(sharesData.balance.computedHoldings.available.a)
246
- .mul(withdrawPct)
247
- .toDecimalPlaces(kaminoStrategy.strategy.tokenAMintDecimals.toNumber(), 1)
248
- .sub(0.000002)
249
- .mul(withdrawFee)
250
- .div(10_000)
251
- .toDecimalPlaces(kaminoStrategy.strategy.tokenAMintDecimals.toNumber());
252
-
253
- const estimatedAOutDecimal = numberToLamportsDecimal(
254
- estimatedAOut,
255
- kaminoStrategy.strategy.tokenAMintDecimals.toNumber()
256
- ).floor();
257
-
258
- const estimatedBOut = sharesData.balance.computedHoldings.invested.b
259
- .add(sharesData.balance.computedHoldings.available.b)
260
- .mul(withdrawPct)
261
- .toDecimalPlaces(kaminoStrategy.strategy.tokenBMintDecimals.toNumber(), 1)
262
- .sub(0.000002)
263
- .mul(withdrawFee)
264
- .div(10_000)
265
- .toDecimalPlaces(kaminoStrategy.strategy.tokenAMintDecimals.toNumber());
266
-
267
- const estimatedBOutDecimal = numberToLamportsDecimal(
268
- estimatedBOut,
269
- kaminoStrategy.strategy.tokenBMintDecimals.toNumber()
270
- ).floor();
271
-
272
- console.log('a-out', estimatedAOutDecimal.toString());
273
- console.log('b-out', estimatedBOut.toString());
274
- return [estimatedAOutDecimal, estimatedBOutDecimal];
275
- }
276
-
277
- export function swapProviderToKaminoSwapProvider<QuoteResponse>(
278
- swapper: SwapIxsProvider<QuoteResponse>,
279
- klendAccounts: Array<Address>,
280
- swapQuote: SwapQuote<QuoteResponse>
281
- ): KaminoSwapperIxBuilder {
282
- return async (
283
- input: DepositAmountsForSwap,
284
- tokenAMint: Address,
285
- tokenBMint: Address,
286
- _owner: TransactionSigner,
287
- _slippage: Decimal,
288
- _allKeys: Address[]
289
- ): Promise<[Instruction[], Address[]]> => {
290
- if (input.tokenBToSwapAmount.lt(0)) {
291
- const swapperIxsArray = await swapper(
292
- {
293
- inputAmountLamports: input.tokenBToSwapAmount.abs(),
294
- inputMint: tokenBMint,
295
- outputMint: tokenAMint,
296
- amountDebtAtaBalance: undefined,
297
- },
298
- klendAccounts,
299
- swapQuote
300
- );
301
- // TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
302
- const swapperIxs = swapperIxsArray[0];
303
- return [swapperIxs.swapIxs, swapperIxs.lookupTables.map((lt) => lt.address)];
304
- } else if (input.tokenAToSwapAmount.lt(0)) {
305
- const swapperIxsArray = await swapper(
306
- {
307
- inputAmountLamports: input.tokenAToSwapAmount.abs(),
308
- inputMint: tokenAMint,
309
- outputMint: tokenBMint,
310
- amountDebtAtaBalance: undefined,
311
- },
312
- klendAccounts,
313
- swapQuote
314
- );
315
- // TODO: Ktoken only supports one swap at a time for now (to be updated if we enable ktokens)
316
- const swapperIxs = swapperIxsArray[0];
317
- return [swapperIxs.swapIxs, swapperIxs.lookupTables.map((lt) => lt.address)];
318
- } else {
319
- throw Error('Nothing to swap');
320
- }
321
- };
322
- }
323
6
 
324
7
  export const getExpectedTokenBalanceAfterBorrow = async (
325
8
  rpc: Rpc<GetAccountInfoApi & GetTokenAccountBalanceApi>,
package/src/lib.ts CHANGED
@@ -8,6 +8,7 @@ export * from './@codegen/kvault/instructions';
8
8
  export * from './@codegen/kvault/accounts/VaultState';
9
9
  export * from './@codegen/kvault/types';
10
10
  export { PROGRAM_ID as KVAULTS_PROGRAM_ID } from './@codegen/kvault/programId';
11
+ export { PROGRAM_ID as UNSTAKING_POOL_ID } from './@codegen/unstaking_pool/programId';
11
12
 
12
13
  export * from './classes';
13
14
  export * from './utils';
@@ -7,6 +7,8 @@ import {
7
7
  getDeactivateLookupTableInstruction,
8
8
  getExtendLookupTableInstruction,
9
9
  } from '@solana-program/address-lookup-table';
10
+ import { DEFAULT_PUBLIC_KEY } from './pubkey';
11
+ import { SYSTEM_PROGRAM_ADDRESS } from '@solana-program/system';
10
12
 
11
13
  export async function printAddressLookupTable(rpc: Rpc<GetAccountInfoApi>, lookupTablePk: Address): Promise<void> {
12
14
  const lookupTableAccount = (await fetchAddressLookupTable(rpc, lookupTablePk)).data;
@@ -114,3 +116,49 @@ export async function getAccountsInLut(rpc: Rpc<GetAccountInfoApi>, lookupTable:
114
116
  const lutState = await fetchAddressLookupTable(rpc, lookupTable);
115
117
  return lutState.data.addresses;
116
118
  }
119
+ /**
120
+ * This method inserts the missing keys from the provided keys into an existent lookup table
121
+ * @param authority - payer wallet pubkey
122
+ * @param lookupTable - lookup table to insert the keys into
123
+ * @param keys - keys to insert into the lookup table
124
+ * @param [accountsInLut] - the existent accounts in the lookup table. Optional. If provided, the function will not fetch the accounts in the lookup table
125
+ * @returns - an array of instructions to insert the missing keys into the lookup table
126
+ */
127
+ export async function insertIntoLookupTableIxs(
128
+ rpc: Rpc<GetAccountInfoApi>,
129
+ authority: TransactionSigner,
130
+ lookupTable: Address,
131
+ keys: Address[],
132
+ accountsInLut?: Address[]
133
+ ): Promise<Instruction[]> {
134
+ let lutContentsList = accountsInLut;
135
+ if (!accountsInLut) {
136
+ lutContentsList = await getAccountsInLut(rpc, lookupTable);
137
+ } else {
138
+ lutContentsList = accountsInLut;
139
+ }
140
+
141
+ const lutContents = new Set<Address>(lutContentsList);
142
+
143
+ const missingAccounts = keys.filter((key) => !lutContents.has(key) && key !== DEFAULT_PUBLIC_KEY);
144
+ // deduplicate missing accounts and remove default accounts and convert it back to an array
145
+ const missingAccountsList = [...new Set<Address>(missingAccounts)];
146
+
147
+ const chunkSize = 20;
148
+ const ixs: Instruction[] = [];
149
+
150
+ for (let i = 0; i < missingAccountsList.length; i += chunkSize) {
151
+ const chunk = missingAccountsList.slice(i, i + chunkSize);
152
+ ixs.push(
153
+ getExtendLookupTableInstruction({
154
+ payer: authority,
155
+ authority,
156
+ address: lookupTable,
157
+ systemProgram: SYSTEM_PROGRAM_ADDRESS,
158
+ addresses: chunk,
159
+ })
160
+ );
161
+ }
162
+
163
+ return ixs;
164
+ }