wasabi-solana-ts 1.2.5 → 1.2.6
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/utils/utils.d.ts +0 -1
- package/dist/utils/utils.js +22 -20
- package/package.json +1 -1
package/dist/utils/utils.d.ts
CHANGED
|
@@ -26,7 +26,6 @@ export declare function getPermission(program: Program<WasabiSolana>, admin: Pub
|
|
|
26
26
|
export declare function uiAmountToAmount(uiAmount: number, decimals: number): BN;
|
|
27
27
|
export declare function amountToUiAmount(amount: BN, decimals: number): number;
|
|
28
28
|
export declare function getTokenProgram(connection: Connection, mint: PublicKey, mintCache?: TokenMintCache): Promise<PublicKey | null>;
|
|
29
|
-
export declare function getTokenProgramAndDecimals(connection: Connection, mint: PublicKey): Promise<[PublicKey, number] | null>;
|
|
30
29
|
export declare const PDA: {
|
|
31
30
|
getLongPool(collateral: PublicKey, currency: PublicKey): PublicKey;
|
|
32
31
|
getShortPool(collateral: PublicKey, currency: PublicKey): PublicKey;
|
package/dist/utils/utils.js
CHANGED
|
@@ -1,11 +1,13 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
2
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
6
|
exports.PDA = exports.SEED_PREFIX = exports.METADATA_PROGRAM_ID = exports.WASABI_PROGRAM_ID = exports.SOL_MINT = void 0;
|
|
4
7
|
exports.getPermission = getPermission;
|
|
5
8
|
exports.uiAmountToAmount = uiAmountToAmount;
|
|
6
9
|
exports.amountToUiAmount = amountToUiAmount;
|
|
7
10
|
exports.getTokenProgram = getTokenProgram;
|
|
8
|
-
exports.getTokenProgramAndDecimals = getTokenProgramAndDecimals;
|
|
9
11
|
exports.getMintInfo = getMintInfo;
|
|
10
12
|
exports.getMetaplexMetadata = getMetaplexMetadata;
|
|
11
13
|
exports.getMaxWithdraw = getMaxWithdraw;
|
|
@@ -37,6 +39,7 @@ const anchor_1 = require("@coral-xyz/anchor");
|
|
|
37
39
|
const spl_token_1 = require("@solana/spl-token");
|
|
38
40
|
const js_1 = require("@metaplex-foundation/js");
|
|
39
41
|
const TokenMintCache_1 = require("../cache/TokenMintCache");
|
|
42
|
+
const node_cache_1 = __importDefault(require("node-cache"));
|
|
40
43
|
exports.SOL_MINT = new web3_js_1.PublicKey('So11111111111111111111111111111111111111111');
|
|
41
44
|
exports.WASABI_PROGRAM_ID = new web3_js_1.PublicKey('spicyTHtbmarmUxwFSHYpA8G4uP2nRNq38RReMpoZ9c');
|
|
42
45
|
exports.METADATA_PROGRAM_ID = new web3_js_1.PublicKey('metaqbxxUerdq28cj1RbAWkYQm3ybzjb6a8bt518x1s');
|
|
@@ -61,6 +64,18 @@ function findProgramAddress(seeds, programId) {
|
|
|
61
64
|
const [publicKey] = web3_js_1.PublicKey.findProgramAddressSync(seeds, programId);
|
|
62
65
|
return publicKey;
|
|
63
66
|
}
|
|
67
|
+
const tokenAccountCache = new node_cache_1.default({ stdTTL: 5 * 60 }); // 5 minutes TTL
|
|
68
|
+
const getTokenAccountCached = async (connection, address) => {
|
|
69
|
+
const cached = tokenAccountCache.get(address.toString());
|
|
70
|
+
if (cached) {
|
|
71
|
+
return cached;
|
|
72
|
+
}
|
|
73
|
+
const accountInfo = await connection.getAccountInfo(address);
|
|
74
|
+
if (accountInfo) {
|
|
75
|
+
tokenAccountCache.set(address.toString(), accountInfo);
|
|
76
|
+
}
|
|
77
|
+
return accountInfo;
|
|
78
|
+
};
|
|
64
79
|
async function getPermission(program, admin) {
|
|
65
80
|
let permission;
|
|
66
81
|
const superAdmin = exports.PDA.getSuperAdmin();
|
|
@@ -86,19 +101,6 @@ async function getTokenProgram(connection, mint, mintCache) {
|
|
|
86
101
|
const mintInfo = await mintCache.getAccount(mint);
|
|
87
102
|
return mintInfo.program;
|
|
88
103
|
}
|
|
89
|
-
async function getTokenProgramAndDecimals(connection, mint) {
|
|
90
|
-
const mintInfo = await connection.getAccountInfo(mint);
|
|
91
|
-
if (!mintInfo) {
|
|
92
|
-
return null;
|
|
93
|
-
}
|
|
94
|
-
if (mintInfo.owner.equals(spl_token_1.TOKEN_PROGRAM_ID) || mintInfo.owner.equals(spl_token_1.TOKEN_2022_PROGRAM_ID)) {
|
|
95
|
-
const mintDecimals = spl_token_1.MintLayout.decode(mintInfo.data).decimals;
|
|
96
|
-
return [mintInfo.owner, mintDecimals];
|
|
97
|
-
}
|
|
98
|
-
else {
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
104
|
exports.PDA = {
|
|
103
105
|
getLongPool(collateral, currency) {
|
|
104
106
|
return findProgramAddress([
|
|
@@ -326,7 +328,7 @@ function isNativeMint(mint) {
|
|
|
326
328
|
async function createAtaIfNeeded(connection, owner, mint, ata, tokenProgram, payer) {
|
|
327
329
|
if (isNativeMint(mint))
|
|
328
330
|
return null;
|
|
329
|
-
const account = await connection
|
|
331
|
+
const account = await getTokenAccountCached(connection, ata);
|
|
330
332
|
if (!account) {
|
|
331
333
|
return (0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payer ? payer : owner, ata, owner, mint, tokenProgram);
|
|
332
334
|
}
|
|
@@ -339,7 +341,7 @@ async function createWrapSolInstruction(connection, owner, amount, includeCleanu
|
|
|
339
341
|
? [spl_token_1.NATIVE_MINT_2022, spl_token_1.TOKEN_2022_PROGRAM_ID]
|
|
340
342
|
: [spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID];
|
|
341
343
|
const ownerWrappedSolAta = (0, spl_token_1.getAssociatedTokenAddressSync)(nativeMint, owner, false, tokenProgram);
|
|
342
|
-
const ownerWrappedSolAccount = await connection
|
|
344
|
+
const ownerWrappedSolAccount = await getTokenAccountCached(connection, ownerWrappedSolAta);
|
|
343
345
|
if (!ownerWrappedSolAccount) {
|
|
344
346
|
setupIx.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(owner, ownerWrappedSolAta, owner, nativeMint, tokenProgram));
|
|
345
347
|
if (includeCleanupIx) {
|
|
@@ -364,7 +366,7 @@ async function createUnwrapSolInstructionWithPayer(connection, payer, owner, use
|
|
|
364
366
|
? [spl_token_1.NATIVE_MINT_2022, spl_token_1.TOKEN_2022_PROGRAM_ID]
|
|
365
367
|
: [spl_token_1.NATIVE_MINT, spl_token_1.TOKEN_PROGRAM_ID];
|
|
366
368
|
const ownerWrappedSolAta = (0, spl_token_1.getAssociatedTokenAddressSync)(nativeMint, owner, false, tokenProgram);
|
|
367
|
-
const ownerWrappedSolAccount = await connection
|
|
369
|
+
const ownerWrappedSolAccount = await getTokenAccountCached(connection, ownerWrappedSolAta);
|
|
368
370
|
if (!ownerWrappedSolAccount) {
|
|
369
371
|
setupIx.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(payer, ownerWrappedSolAta, owner, nativeMint, tokenProgram));
|
|
370
372
|
}
|
|
@@ -401,7 +403,7 @@ async function handleMint(connection, mint, options) {
|
|
|
401
403
|
}
|
|
402
404
|
if (options.owner) {
|
|
403
405
|
const userAta = (0, spl_token_1.getAssociatedTokenAddressSync)(mint, options.owner, true, tokenProgram);
|
|
404
|
-
const userTokenAccount = await connection
|
|
406
|
+
const userTokenAccount = await getTokenAccountCached(connection, userAta);
|
|
405
407
|
if (!userTokenAccount) {
|
|
406
408
|
instructions.setupIx.push((0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(options.owner, userAta, options.owner, mint, tokenProgram));
|
|
407
409
|
}
|
|
@@ -501,7 +503,7 @@ async function handleOpenTokenAccounts({ program, owner, downPayment, fee, mintC
|
|
|
501
503
|
setupIx.push((0, spl_token_1.createSyncNativeInstruction)(ownerPaymentAta, paymentTokenProgram));
|
|
502
504
|
cleanupIx.push((0, spl_token_1.createCloseAccountInstruction)(ownerPaymentAta, owner, owner, [], paymentTokenProgram));
|
|
503
505
|
}
|
|
504
|
-
const ownerPaymentAtaInfo = await program.provider.connection
|
|
506
|
+
const ownerPaymentAtaInfo = await getTokenAccountCached(program.provider.connection, ownerPaymentAta);
|
|
505
507
|
if (!ownerPaymentAtaInfo) {
|
|
506
508
|
setupIx = [
|
|
507
509
|
(0, spl_token_1.createAssociatedTokenAccountIdempotentInstruction)(owner, ownerPaymentAta, owner, paymentMint, paymentTokenProgram),
|
|
@@ -529,7 +531,7 @@ async function handleCloseTokenAccounts(config, poolAccount) {
|
|
|
529
531
|
const payoutTokenProgram = poolAccount.isLongPool ? currencyTokenProgram : collateralTokenProgram;
|
|
530
532
|
const payoutIsSol = payoutMint.equals(spl_token_1.NATIVE_MINT);
|
|
531
533
|
const ownerPayoutAta = (0, spl_token_1.getAssociatedTokenAddressSync)(payoutMint, config.owner, false, payoutTokenProgram);
|
|
532
|
-
const ownerPayoutAtaInfo = await config.program.provider.connection
|
|
534
|
+
const ownerPayoutAtaInfo = await getTokenAccountCached(config.program.provider.connection, ownerPayoutAta);
|
|
533
535
|
const setupIx = [];
|
|
534
536
|
const cleanupIx = [];
|
|
535
537
|
if (!ownerPayoutAtaInfo) {
|