wasabi-solana-ts 1.2.5 → 1.2.7

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.
@@ -25,6 +25,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  exports.getFailingSwapProgram = exports.matchError = exports.parseErrorLogs = exports.parseSimulationError = exports.parseError = exports.parseSendTransactionError = exports.SimulationError = void 0;
27
27
  const jupiter_1 = require("./jupiter");
28
+ const titan_1 = require("./titan");
28
29
  const web3_js_1 = require("@solana/web3.js");
29
30
  const spl_token_1 = require("@solana/spl-token");
30
31
  const idl = __importStar(require("../idl/wasabi_solana.json"));
@@ -52,6 +53,10 @@ const jupiterProgramId = 'JUP6LkbZbjS1jKKwapdHNy74zcZ3tLUZoi5QNyVTaV4';
52
53
  const jupiterExpectedErrors = [
53
54
  6001 // SlippageToleranceExceeded
54
55
  ];
56
+ const titanProgramId = 'T1TANpTeScyeqVzzgNViGDNrkQ6qHz9KrSBS4aNXvGT';
57
+ const titanExpectedErrors = [
58
+ 6008 // LessThanMinimumAmountOut
59
+ ];
55
60
  //@ts-ignore
56
61
  const raydiumErrors = [
57
62
  6028, // Invalid first tick array
@@ -81,6 +86,18 @@ const jupiterErrorIndex = jupiter_1.IDL.errors.reduce((acc, error) => {
81
86
  const findJupiterError = (code) => {
82
87
  return jupiterErrorIndex[code];
83
88
  };
89
+ const titanErrorIndex = titan_1.IDL.errors.reduce((acc, error) => {
90
+ const expected = titanExpectedErrors.includes(error.code);
91
+ acc[error.code] = {
92
+ ...error,
93
+ expected,
94
+ program: 'Titan'
95
+ };
96
+ return acc;
97
+ }, {});
98
+ const findTitanError = (code) => {
99
+ return titanErrorIndex[code];
100
+ };
84
101
  const parseSendTransactionError = (error, transaction) => {
85
102
  const message = error.transactionError.message;
86
103
  // Parse error messages like: "Transaction simulation failed: Error processing Instruction 3: custom program error: 0x1771"
@@ -108,6 +125,9 @@ const parseError = (instructionNumber, errorCode, transaction) => {
108
125
  else if (programId === jupiterProgramId) {
109
126
  return findJupiterError(errorCode);
110
127
  }
128
+ else if (programId === titanProgramId) {
129
+ return findTitanError(errorCode);
130
+ }
111
131
  return undefined;
112
132
  };
113
133
  exports.parseError = parseError;
@@ -153,14 +173,19 @@ const parseErrorLogs = (logs) => {
153
173
  }
154
174
  const match = log.match(/^Program ([a-zA-Z0-9]+) failed: (?:.*?): (0x[0-9a-fA-F]+)$/);
155
175
  if (match) {
156
- if (match[1].localeCompare(jupiterProgramId) === 0) {
157
- return findJupiterError(parseInt(match[2]));
176
+ const [_, failingProgramId, errorHex] = match;
177
+ const errorCode = parseInt(errorHex);
178
+ if (failingProgramId.localeCompare(jupiterProgramId) === 0) {
179
+ return findJupiterError(errorCode);
180
+ }
181
+ else if (failingProgramId.localeCompare(titanProgramId) === 0) {
182
+ return findTitanError(errorCode);
158
183
  }
159
- else if (match[1].localeCompare(wasabiProgramId) === 0) {
160
- return findWasabiError(parseInt(match[2]));
184
+ else if (failingProgramId.localeCompare(wasabiProgramId) === 0) {
185
+ return findWasabiError(errorCode);
161
186
  }
162
- else if (match[1].localeCompare(web3_js_1.SystemProgram.programId.toBase58())) {
163
- return parseSystemError(parseInt(match[2]), web3_js_1.SystemProgram.programId.toBase58());
187
+ else if (failingProgramId.localeCompare(web3_js_1.SystemProgram.programId.toBase58())) {
188
+ return parseSystemError(errorCode, web3_js_1.SystemProgram.programId.toBase58());
164
189
  }
165
190
  }
166
191
  }
@@ -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.5",
3
+ "version": "1.2.7",
4
4
  "description": "Typescript library for the Wasabi program",
5
5
  "main": "dist/index.js",
6
6
  "types": "dist/index.d.ts",