@kamino-finance/klend-sdk 5.3.0 → 5.4.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.
@@ -495,20 +495,18 @@ export class KaminoManager {
495
495
 
496
496
  /**
497
497
  * Get all vaults
498
- * @param useOptimisedRPCCall - if set to true, it will use the optimized getProgramAccounts RPC call, which is more efficient but doesn't work in web environments
499
498
  * @returns an array of all vaults
500
499
  */
501
- async getAllVaults(useOptimisedRPCCall: boolean = true): Promise<KaminoVault[]> {
502
- return this._vaultClient.getAllVaults(useOptimisedRPCCall);
500
+ async getAllVaults(): Promise<KaminoVault[]> {
501
+ return this._vaultClient.getAllVaults();
503
502
  }
504
503
 
505
504
  /**
506
505
  * Get all vaults for owner
507
506
  * @param owner the pubkey of the vaults owner
508
- * @param useOptimisedRPCCall - if set to true, it will use the optimized getProgramAccounts RPC call, which is more efficient but doesn't work in web environments
509
507
  * @returns an array of all vaults owned by a given pubkey
510
508
  */
511
- async getAllVaultsForOwner(owner: PublicKey, useOptimisedRPCCall: boolean = true): Promise<KaminoVault[]> {
509
+ async getAllVaultsForOwner(owner: PublicKey): Promise<KaminoVault[]> {
512
510
  const filters = [
513
511
  {
514
512
  dataSize: VaultState.layout.span + 8,
@@ -527,16 +525,15 @@ export class KaminoManager {
527
525
  },
528
526
  ];
529
527
 
530
- let kaminoVaults: GetProgramAccountsResponse = [];
531
-
532
- if (useOptimisedRPCCall) {
533
- kaminoVaults = await getProgramAccounts(this._connection, this._kaminoVaultProgramId, {
528
+ const kaminoVaults: GetProgramAccountsResponse = await getProgramAccounts(
529
+ this._connection,
530
+ this._kaminoVaultProgramId,
531
+ VaultState.layout.span + 8,
532
+ {
534
533
  commitment: this._connection.commitment ?? 'processed',
535
534
  filters,
536
- });
537
- } else {
538
- kaminoVaults = await this._connection.getProgramAccounts(this._kaminoVaultProgramId, { filters });
539
- }
535
+ }
536
+ );
540
537
 
541
538
  return kaminoVaults.map((kaminoVault) => {
542
539
  if (kaminoVault.account === null) {
@@ -1,4 +1,4 @@
1
- import { AccountInfo, Connection, GetProgramAccountsResponse, PublicKey } from '@solana/web3.js';
1
+ import { AccountInfo, Connection, PublicKey } from '@solana/web3.js';
2
2
  import { KaminoObligation } from './obligation';
3
3
  import { KaminoReserve } from './reserve';
4
4
  import { LendingMarket, Obligation, UserMetadata, ReferrerTokenState, Reserve } from '../idl_codegen/accounts';
@@ -531,9 +531,8 @@ export class KaminoMarket {
531
531
  * This function will likely require an RPC capable of returning more than the default 100k rows in a single scan
532
532
  *
533
533
  * @param tag
534
- * @param useOptimisedRPCCall - use the optimised RPC call (compressed) to get all obligations
535
534
  */
536
- async getAllObligationsForMarket(tag?: number, useOptimisedRPCCall: boolean = true): Promise<KaminoObligation[]> {
535
+ async getAllObligationsForMarket(tag?: number): Promise<KaminoObligation[]> {
537
536
  const filters = [
538
537
  {
539
538
  dataSize: Obligation.layout.span + 8,
@@ -558,26 +557,14 @@ export class KaminoMarket {
558
557
  const collateralExchangeRates = new PubkeyHashMap<PublicKey, Decimal>();
559
558
  const cumulativeBorrowRates = new PubkeyHashMap<PublicKey, Decimal>();
560
559
 
561
- let obligations: GetProgramAccountsResponse = [];
562
- let slot = 0;
563
- const slotPromise = this.connection.getSlot();
564
-
565
- if (useOptimisedRPCCall) {
566
- [slot, obligations] = await Promise.all([
567
- slotPromise,
568
- getProgramAccounts(this.connection, this.programId, {
569
- commitment: this.connection.commitment ?? 'processed',
570
- filters,
571
- dataSlice: { offset: 0, length: ObligationZP.layout.span + 8 }, // truncate the padding
572
- }),
573
- ]);
574
- } else {
575
- const obligationsPromise = this.connection.getProgramAccounts(this.programId, {
560
+ const [slot, obligations] = await Promise.all([
561
+ this.connection.getSlot(),
562
+ getProgramAccounts(this.connection, this.programId, ObligationZP.layout.span + 8, {
563
+ commitment: this.connection.commitment ?? 'processed',
576
564
  filters,
577
565
  dataSlice: { offset: 0, length: ObligationZP.layout.span + 8 }, // truncate the padding
578
- });
579
- [slot, obligations] = await Promise.all([slotPromise, obligationsPromise]);
580
- }
566
+ }),
567
+ ]);
581
568
 
582
569
  return obligations.map((obligation) => {
583
570
  if (obligation.account === null) {
@@ -1349,16 +1349,14 @@ export class KaminoVaultClient {
1349
1349
  * @param vaultsOverride - a list of vaults to get the tokens per share for; if provided with state it will not fetch the state again
1350
1350
  * @param vaultReservesMap - optional parameter; a hashmap from pubkey to reserve state. If provided the function will be significantly faster as it will not have to fetch the reserves
1351
1351
  * @param slot - current slot, used to estimate the interest earned in the different reserves with allocation from the vault
1352
- * @param useOptimisedRPCCall - if set to true, it will use the optimized getProgramAccounts RPC call, which is more efficient but doesn't work in web environments
1353
1352
  * @returns - token per share value
1354
1353
  */
1355
1354
  async getTokensPerShareAllVaults(
1356
1355
  slot: number,
1357
1356
  vaultsOverride?: Array<KaminoVault>,
1358
- vaultReservesMap?: PubkeyHashMap<PublicKey, KaminoReserve>,
1359
- useOptimisedRPCCall: boolean = true
1357
+ vaultReservesMap?: PubkeyHashMap<PublicKey, KaminoReserve>
1360
1358
  ): Promise<PubkeyHashMap<PublicKey, Decimal>> {
1361
- const vaults = vaultsOverride ? vaultsOverride : await this.getAllVaults(useOptimisedRPCCall);
1359
+ const vaults = vaultsOverride ? vaultsOverride : await this.getAllVaults();
1362
1360
  const vaultTokensPerShare = new PubkeyHashMap<PublicKey, Decimal>();
1363
1361
  for (const vault of vaults) {
1364
1362
  const tokensPerShare = await this.getTokensPerShareSingleVault(vault, slot, vaultReservesMap);
@@ -1370,10 +1368,9 @@ export class KaminoVaultClient {
1370
1368
 
1371
1369
  /**
1372
1370
  * Get all vaults
1373
- * @param useOptimisedRPCCall - if set to true, it will use the optimized getProgramAccounts RPC call, which is more efficient but doesn't work in web environments
1374
1371
  * @returns an array of all vaults
1375
1372
  */
1376
- async getAllVaults(useOptimisedRPCCall: boolean = true): Promise<KaminoVault[]> {
1373
+ async getAllVaults(): Promise<KaminoVault[]> {
1377
1374
  const filters = [
1378
1375
  {
1379
1376
  dataSize: VaultState.layout.span + 8,
@@ -1386,16 +1383,15 @@ export class KaminoVaultClient {
1386
1383
  },
1387
1384
  ];
1388
1385
 
1389
- let kaminoVaults: GetProgramAccountsResponse = [];
1390
-
1391
- if (useOptimisedRPCCall) {
1392
- kaminoVaults = await getProgramAccounts(this._connection, this._kaminoVaultProgramId, {
1386
+ const kaminoVaults: GetProgramAccountsResponse = await getProgramAccounts(
1387
+ this._connection,
1388
+ this._kaminoVaultProgramId,
1389
+ VaultState.layout.span + 8,
1390
+ {
1393
1391
  commitment: this._connection.commitment ?? 'processed',
1394
1392
  filters,
1395
- });
1396
- } else {
1397
- kaminoVaults = await this._connection.getProgramAccounts(this._kaminoVaultProgramId, { filters });
1398
- }
1393
+ }
1394
+ );
1399
1395
 
1400
1396
  return kaminoVaults.map((kaminoVault) => {
1401
1397
  if (kaminoVault.account === null) {
@@ -922,7 +922,7 @@ async function main() {
922
922
  const env = initializeClient(false, false);
923
923
  const kaminoManager = new KaminoManager(env.connection, env.kLendProgramId, env.kVaultProgramId);
924
924
 
925
- const allVaults = await kaminoManager.getAllVaults(true);
925
+ const allVaults = await kaminoManager.getAllVaults();
926
926
  console.log('all vaults', allVaults);
927
927
  });
928
928
 
package/src/utils/rpc.ts CHANGED
@@ -9,11 +9,12 @@ import {
9
9
  } from '@solana/web3.js';
10
10
  import { Buffer } from 'buffer';
11
11
  import axios from 'axios';
12
- import { init, decompress } from '@bokuweb/zstd-wasm';
13
12
  import { v4 as uuidv4 } from 'uuid';
13
+ import { ZSTDDecoder } from 'zstddec';
14
14
 
15
+ const decoder = new ZSTDDecoder();
15
16
  (async () => {
16
- await init();
17
+ await decoder.init();
17
18
  })();
18
19
 
19
20
  /**
@@ -21,11 +22,13 @@ import { v4 as uuidv4 } from 'uuid';
21
22
  * Uses axios instead of node-fetch to work around a bug in node-fetch that causes subsequent requests with different encoding to fail
22
23
  * @param connection
23
24
  * @param programId
25
+ * @param structSize - the size of the decompressed account data struct
24
26
  * @param configOrCommitment
25
27
  */
26
28
  export async function getProgramAccounts(
27
29
  connection: Connection,
28
30
  programId: PublicKey,
31
+ structSize: number,
29
32
  configOrCommitment?: GetProgramAccountsConfig | Commitment
30
33
  ): Promise<GetProgramAccountsResponse> {
31
34
  const programIdStr = programId.toBase58();
@@ -65,7 +68,7 @@ export async function getProgramAccounts(
65
68
 
66
69
  const res = unsafeRes as RpcResult;
67
70
  const deser = res.result.map(async (account) => ({
68
- account: await deserializeAccountInfo(account.account),
71
+ account: await deserializeAccountInfo(account.account, structSize),
69
72
  pubkey: new PublicKey(account.pubkey),
70
73
  }));
71
74
  const x = await Promise.all(deser);
@@ -80,8 +83,8 @@ export async function getAccountOwner(connection: Connection, address: PublicKey
80
83
  return acc.owner;
81
84
  }
82
85
 
83
- async function deserializeAccountInfo(accountInfo: AccountInfo<string[]>): Promise<AccountInfo<Buffer>> {
84
- const data = decompress(Buffer.from(accountInfo.data[0], 'base64'));
86
+ async function deserializeAccountInfo(accountInfo: AccountInfo<string[]>, size: number): Promise<AccountInfo<Buffer>> {
87
+ const data = decoder.decode(Buffer.from(accountInfo.data[0], 'base64'), size);
85
88
  return {
86
89
  owner: accountInfo.owner,
87
90
  lamports: accountInfo.lamports,