@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.
- package/dist/classes/manager.d.ts +1 -1
- package/dist/classes/manager.d.ts.map +1 -1
- package/dist/classes/manager.js +3 -3
- package/dist/classes/manager.js.map +1 -1
- package/dist/classes/market.d.ts.map +1 -1
- package/dist/classes/market.js +3 -0
- package/dist/classes/market.js.map +1 -1
- package/dist/classes/reserve.d.ts +7 -1
- package/dist/classes/reserve.d.ts.map +1 -1
- package/dist/classes/reserve.js +145 -255
- package/dist/classes/reserve.js.map +1 -1
- package/dist/classes/types.d.ts +2 -0
- package/dist/classes/types.d.ts.map +1 -1
- package/dist/classes/utils.d.ts +1 -1
- package/dist/classes/utils.d.ts.map +1 -1
- package/dist/classes/utils.js +8 -2
- package/dist/classes/utils.js.map +1 -1
- package/dist/classes/vault.d.ts +1 -1
- package/dist/classes/vault.d.ts.map +1 -1
- package/dist/classes/vault.js +24 -5
- package/dist/classes/vault.js.map +1 -1
- package/dist/client_kamino_manager.d.ts.map +1 -1
- package/dist/client_kamino_manager.js +99 -89
- package/dist/client_kamino_manager.js.map +1 -1
- package/dist/utils/ata.d.ts +15 -2
- package/dist/utils/ata.d.ts.map +1 -1
- package/dist/utils/ata.js +68 -3
- package/dist/utils/ata.js.map +1 -1
- package/dist/utils/constants.d.ts +1 -0
- package/dist/utils/constants.d.ts.map +1 -1
- package/dist/utils/constants.js +2 -1
- package/dist/utils/constants.js.map +1 -1
- package/package.json +1 -1
- package/src/classes/manager.ts +5 -6
- package/src/classes/market.ts +4 -0
- package/src/classes/reserve.ts +178 -319
- package/src/classes/types.ts +3 -0
- package/src/classes/utils.ts +9 -3
- package/src/classes/vault.ts +44 -12
- package/src/client_kamino_manager.ts +225 -92
- package/src/utils/ata.ts +102 -4
- 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 {
|
|
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 {
|
|
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(
|
|
148
|
-
const tokenAccountBalance = await
|
|
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
|
+
};
|