@kamino-finance/klend-sdk 5.0.7 → 5.1.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.
@@ -1,6 +1,6 @@
1
1
  import { PublicKey, SYSVAR_RENT_PUBKEY, SystemProgram, TransactionInstruction, Connection } from '@solana/web3.js';
2
2
  import { KaminoMarket } from '../classes';
3
- import { checkIfAccountExists, referrerStatePda, referrerTokenStatePda, shortUrlPda, userMetadataPda } from '../utils';
3
+ import { PublicKeySet, referrerStatePda, referrerTokenStatePda, shortUrlPda, userMetadataPda } from '../utils';
4
4
  import {
5
5
  PROGRAM_ID,
6
6
  ReferrerState,
@@ -12,9 +12,11 @@ import {
12
12
  export const getInitAllReferrerTokenStateIxns = async ({
13
13
  referrer,
14
14
  kaminoMarket,
15
+ payer = referrer,
15
16
  }: {
16
17
  referrer: PublicKey;
17
18
  kaminoMarket: KaminoMarket;
19
+ payer?: PublicKey;
18
20
  }) => {
19
21
  if (referrer.equals(PublicKey.default)) {
20
22
  throw new Error('Referrer not set');
@@ -25,27 +27,32 @@ export const getInitAllReferrerTokenStateIxns = async ({
25
27
  const initReferrerTokenStateIxns: TransactionInstruction[] = [];
26
28
 
27
29
  const tokenStatesToCreate: [PublicKey, PublicKey][] = [];
28
- for (const reserve of kaminoMarket.reserves.values()) {
29
- const referrerTokenStateAddress = referrerTokenStatePda(referrer, reserve.address, kaminoMarket.programId)[0];
30
-
31
- if (!(await checkIfAccountExists(kaminoMarket.getConnection(), referrerTokenStateAddress))) {
32
- tokenStatesToCreate.push([referrerTokenStateAddress, reserve?.address]);
30
+ const reserves = kaminoMarket.getReserves();
31
+ const referrerTokenStates = reserves.map((reserve) => {
32
+ return referrerTokenStatePda(referrer, reserve.address, kaminoMarket.programId)[0];
33
+ });
34
+ const uniqueReferrerTokenStates = new PublicKeySet<PublicKey>(referrerTokenStates).toArray();
35
+ const accounts = await kaminoMarket.getConnection().getMultipleAccountsInfo(uniqueReferrerTokenStates);
36
+ for (let i = 0; i < uniqueReferrerTokenStates.length; i++) {
37
+ if (!accounts[i]) {
38
+ tokenStatesToCreate.push([uniqueReferrerTokenStates[i], reserves[i].address]);
33
39
  }
34
40
  }
35
41
 
36
42
  tokenStatesToCreate.forEach(([referrerTokenStateAddress, reserveAddress]) => {
37
43
  const initReferrerTokenStateIx = initReferrerTokenState(
38
44
  {
39
- referrer: referrer,
45
+ referrer,
40
46
  },
41
47
  {
42
48
  lendingMarket: kaminoMarket.getAddress(),
43
- payer: referrer,
49
+ payer,
44
50
  reserve: reserveAddress,
45
51
  referrerTokenState: referrerTokenStateAddress,
46
52
  rent: SYSVAR_RENT_PUBKEY,
47
53
  systemProgram: SystemProgram.programId,
48
- }
54
+ },
55
+ kaminoMarket.programId
49
56
  );
50
57
 
51
58
  initReferrerTokenStateIxns.push(initReferrerTokenStateIx);
package/src/utils/ata.ts CHANGED
@@ -98,11 +98,6 @@ export function createAtasIdempotent(
98
98
  return res;
99
99
  }
100
100
 
101
- export const checkIfAccountExists = async (connection: Connection, account: PublicKey): Promise<boolean> => {
102
- const acc = await connection.getAccountInfo(account);
103
- return acc !== null;
104
- };
105
-
106
101
  export function getDepositWsolIxns(owner: PublicKey, ata: PublicKey, amountLamports: Decimal) {
107
102
  const ixns: TransactionInstruction[] = [];
108
103
 
@@ -158,15 +153,14 @@ export async function getTokenAccountBalance(provider: AnchorProvider, tokenAcco
158
153
  export async function getTokenAccountBalanceDecimal(
159
154
  connection: Connection,
160
155
  mint: PublicKey,
161
- owner: PublicKey
156
+ owner: PublicKey,
157
+ tokenProgram: PublicKey = TOKEN_PROGRAM_ID,
162
158
  ): Promise<Decimal> {
163
- const tokenAta = getAssociatedTokenAddress(mint, owner);
164
- const ataExists = await checkIfAccountExists(connection, tokenAta);
165
-
166
- if (!ataExists) {
167
- return new Decimal(0);
168
- } else {
169
- const tokenData = (await connection.getTokenAccountBalance(tokenAta)).value;
170
- return new Decimal(tokenData.uiAmountString!);
159
+ const ata = getAssociatedTokenAddress(mint, owner, true, tokenProgram);
160
+ const accInfo = await connection.getAccountInfo(ata);
161
+ if (accInfo === null) {
162
+ return new Decimal('0');
171
163
  }
164
+ const { value } = await connection.getTokenAccountBalance(ata);
165
+ return new Decimal(value.uiAmountString!);
172
166
  }