@kamino-finance/klend-sdk 5.11.14 → 5.11.16

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 (42) hide show
  1. package/dist/classes/manager.d.ts +1 -1
  2. package/dist/classes/manager.d.ts.map +1 -1
  3. package/dist/classes/manager.js +3 -3
  4. package/dist/classes/manager.js.map +1 -1
  5. package/dist/classes/market.d.ts.map +1 -1
  6. package/dist/classes/market.js +3 -0
  7. package/dist/classes/market.js.map +1 -1
  8. package/dist/classes/reserve.d.ts +7 -1
  9. package/dist/classes/reserve.d.ts.map +1 -1
  10. package/dist/classes/reserve.js +145 -255
  11. package/dist/classes/reserve.js.map +1 -1
  12. package/dist/classes/types.d.ts +2 -0
  13. package/dist/classes/types.d.ts.map +1 -1
  14. package/dist/classes/utils.d.ts +1 -1
  15. package/dist/classes/utils.d.ts.map +1 -1
  16. package/dist/classes/utils.js +8 -2
  17. package/dist/classes/utils.js.map +1 -1
  18. package/dist/classes/vault.d.ts +1 -1
  19. package/dist/classes/vault.d.ts.map +1 -1
  20. package/dist/classes/vault.js +24 -5
  21. package/dist/classes/vault.js.map +1 -1
  22. package/dist/client_kamino_manager.d.ts.map +1 -1
  23. package/dist/client_kamino_manager.js +99 -89
  24. package/dist/client_kamino_manager.js.map +1 -1
  25. package/dist/utils/ata.d.ts +15 -2
  26. package/dist/utils/ata.d.ts.map +1 -1
  27. package/dist/utils/ata.js +68 -3
  28. package/dist/utils/ata.js.map +1 -1
  29. package/dist/utils/constants.d.ts +1 -0
  30. package/dist/utils/constants.d.ts.map +1 -1
  31. package/dist/utils/constants.js +2 -1
  32. package/dist/utils/constants.js.map +1 -1
  33. package/package.json +1 -1
  34. package/src/classes/manager.ts +5 -6
  35. package/src/classes/market.ts +4 -0
  36. package/src/classes/reserve.ts +178 -319
  37. package/src/classes/types.ts +3 -0
  38. package/src/classes/utils.ts +9 -3
  39. package/src/classes/vault.ts +44 -12
  40. package/src/client_kamino_manager.ts +225 -92
  41. package/src/utils/ata.ts +102 -4
  42. package/src/utils/constants.ts +2 -0
package/src/utils/ata.ts CHANGED
@@ -1,11 +1,22 @@
1
1
  import {
2
2
  ASSOCIATED_TOKEN_PROGRAM_ID,
3
+ NATIVE_MINT,
3
4
  TOKEN_PROGRAM_ID,
5
+ createAssociatedTokenAccountInstruction,
4
6
  createAssociatedTokenAccountIdempotentInstruction as createAtaIx,
7
+ createCloseAccountInstruction,
8
+ getAssociatedTokenAddressSync,
5
9
  } from '@solana/spl-token';
6
- import { ComputeBudgetProgram, Connection, PublicKey, SystemProgram, TransactionInstruction } from '@solana/web3.js';
10
+ import {
11
+ AccountInfo,
12
+ ComputeBudgetProgram,
13
+ Connection,
14
+ PublicKey,
15
+ SystemProgram,
16
+ TransactionInstruction,
17
+ } from '@solana/web3.js';
7
18
  import Decimal from 'decimal.js';
8
- import { AnchorProvider } from '@coral-xyz/anchor';
19
+ import { collToLamportsDecimal, DECIMALS_SOL } from '@kamino-finance/kliquidity-sdk/dist';
9
20
 
10
21
  /**
11
22
  * Create an idempotent create ATA instruction
@@ -144,8 +155,8 @@ export function removeBudgetAndAtaIxns(ixns: TransactionInstruction[], mints: st
144
155
  });
145
156
  }
146
157
 
147
- export async function getTokenAccountBalance(provider: AnchorProvider, tokenAccount: PublicKey): Promise<number> {
148
- const tokenAccountBalance = await provider.connection.getTokenAccountBalance(tokenAccount);
158
+ export async function getTokenAccountBalance(connection: Connection, tokenAccount: PublicKey): Promise<number> {
159
+ const tokenAccountBalance = await connection.getTokenAccountBalance(tokenAccount);
149
160
 
150
161
  return Number(tokenAccountBalance.value.amount).valueOf();
151
162
  }
@@ -165,3 +176,90 @@ export async function getTokenAccountBalanceDecimal(
165
176
  const { value } = await connection.getTokenAccountBalance(ata);
166
177
  return new Decimal(value.uiAmountString!);
167
178
  }
179
+
180
+ export type CreateWsolAtaIxs = {
181
+ wsolAta: PublicKey;
182
+ createAtaIxs: TransactionInstruction[];
183
+ closeAtaIxs: TransactionInstruction[];
184
+ };
185
+
186
+ /**
187
+ * Creates a wSOL ata if missing and syncs the balance. If the ata exists and it has more or equal no wrapping happens
188
+ * @param connection - Solana RPC connection (read)
189
+ * @param amount min amount to have in the wSOL ata. If the ata exists and it has more or equal no wrapping happens
190
+ * @param owner - owner of the ata
191
+ * @returns wsolAta: the keypair of the ata, used to sign the initialization transaction; createAtaIxs: a list with ixs to initialize the ata and wrap SOL if needed; closeAtaIxs: a list with ixs to close the ata
192
+ */
193
+ export const createWsolAtaIfMissing = async (
194
+ connection: Connection,
195
+ amount: Decimal,
196
+ owner: PublicKey
197
+ ): Promise<CreateWsolAtaIxs> => {
198
+ const createIxns: TransactionInstruction[] = [];
199
+ const closeIxns: TransactionInstruction[] = [];
200
+
201
+ const wsolAta: PublicKey = getAssociatedTokenAddressSync(NATIVE_MINT, owner, true, TOKEN_PROGRAM_ID);
202
+
203
+ const solDeposit = amount;
204
+ const wsolAtaAccountInfo: AccountInfo<Buffer> | null = await connection.getAccountInfo(wsolAta);
205
+
206
+ // This checks if we need to create it
207
+ if (isWsolInfoInvalid(wsolAtaAccountInfo)) {
208
+ createIxns.push(createAssociatedTokenAccountInstruction(owner, wsolAta, owner, NATIVE_MINT, TOKEN_PROGRAM_ID));
209
+ }
210
+
211
+ let wsolExistingBalanceLamports = new Decimal(0);
212
+ try {
213
+ if (wsolAtaAccountInfo != null) {
214
+ const uiAmount = (await getTokenAccountBalanceDecimal(connection, NATIVE_MINT, owner)).toNumber();
215
+ wsolExistingBalanceLamports = collToLamportsDecimal(new Decimal(uiAmount), DECIMALS_SOL);
216
+ }
217
+ } catch (err) {
218
+ console.log('Err Token Balance', err);
219
+ }
220
+
221
+ if (solDeposit !== null && solDeposit.gt(wsolExistingBalanceLamports)) {
222
+ createIxns.push(
223
+ SystemProgram.transfer({
224
+ fromPubkey: owner,
225
+ toPubkey: wsolAta,
226
+ lamports: BigInt(solDeposit.sub(wsolExistingBalanceLamports).floor().toString()),
227
+ })
228
+ );
229
+ }
230
+
231
+ if (createIxns.length > 0) {
232
+ // Primitive way of wrapping SOL
233
+ createIxns.push(
234
+ new TransactionInstruction({
235
+ keys: [
236
+ {
237
+ pubkey: wsolAta,
238
+ isSigner: false,
239
+ isWritable: true,
240
+ },
241
+ ],
242
+ data: Buffer.from(new Uint8Array([17])),
243
+ programId: TOKEN_PROGRAM_ID,
244
+ })
245
+ );
246
+ }
247
+
248
+ closeIxns.push(createCloseAccountInstruction(wsolAta, owner, owner, [], TOKEN_PROGRAM_ID));
249
+
250
+ return {
251
+ wsolAta,
252
+ createAtaIxs: createIxns,
253
+ closeAtaIxs: closeIxns,
254
+ };
255
+ };
256
+
257
+ export const isWsolInfoInvalid = (wsolAtaAccountInfo: any): boolean => {
258
+ const res =
259
+ wsolAtaAccountInfo === null ||
260
+ (wsolAtaAccountInfo !== null &&
261
+ wsolAtaAccountInfo.data.length === 0 &&
262
+ wsolAtaAccountInfo.owner.eq(PublicKey.default));
263
+
264
+ return res;
265
+ };
@@ -92,3 +92,5 @@ export const SOL_PADDING_FOR_INTEREST = new BN('1000000');
92
92
  */
93
93
  export const MIN_INITIAL_DEPOSIT = 100_000;
94
94
  export const MIN_VAULT_INITIAL_DEPOSIT = 1_000_000_000;
95
+
96
+ export const VAULT_INITIAL_DEPOSIT = 1000;