wasabi-solana-ts 1.2.4 → 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.
@@ -0,0 +1,2 @@
1
+ export * from './TokenMintCache';
2
+ export * from './BaseAccountCache';
@@ -0,0 +1,18 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./TokenMintCache"), exports);
18
+ __exportStar(require("./BaseAccountCache"), exports);
package/dist/index.d.ts CHANGED
@@ -11,6 +11,7 @@ export * from './bundle-builder/index.js';
11
11
  export * from './market-deployer/index.js';
12
12
  import { WasabiSolana } from './idl/wasabi_solana.js';
13
13
  import * as idl from './idl/wasabi_solana.json';
14
+ export * from './cache/index.js';
14
15
  export { idl };
15
16
  import { Program } from '@coral-xyz/anchor';
16
17
  import { OpenPositionCleanupAccounts, OpenPositionSetupAccounts, OpenPositionSetupArgs } from './instructions/openPosition.js';
package/dist/index.js CHANGED
@@ -40,6 +40,7 @@ __exportStar(require("./bundle-builder/index.js"), exports);
40
40
  __exportStar(require("./market-deployer/index.js"), exports);
41
41
  const idl = __importStar(require("./idl/wasabi_solana.json"));
42
42
  exports.idl = idl;
43
+ __exportStar(require("./cache/index.js"), exports);
43
44
  const closeLongPosition_js_1 = require("./instructions/closeLongPosition.js");
44
45
  const openLongPosition_js_1 = require("./instructions/openLongPosition.js");
45
46
  const openShortPosition_js_1 = require("./instructions/openShortPosition.js");
@@ -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;
@@ -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.getAccountInfo(ata);
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.getAccountInfo(ownerWrappedSolAta);
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.getAccountInfo(ownerWrappedSolAta);
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.getAccountInfo(userAta);
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.getAccountInfo(ownerPaymentAta);
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.getAccountInfo(ownerPayoutAta);
534
+ const ownerPayoutAtaInfo = await getTokenAccountCached(config.program.provider.connection, ownerPayoutAta);
533
535
  const setupIx = [];
534
536
  const cleanupIx = [];
535
537
  if (!ownerPayoutAtaInfo) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "wasabi-solana-ts",
3
- "version": "1.2.4",
3
+ "version": "1.2.6",
4
4
  "description": "Typescript library for the Wasabi program",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",