@solana/web3.js 1.91.9 → 1.92.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.
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.91.9",
3
+ "version": "1.92.0",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
package/src/connection.ts CHANGED
@@ -5782,10 +5782,13 @@ export class Connection {
5782
5782
  console.error(res.error.message, logTrace);
5783
5783
  }
5784
5784
  }
5785
- throw new SendTransactionError(
5786
- 'failed to simulate transaction: ' + res.error.message,
5787
- logs,
5788
- );
5785
+
5786
+ throw new SendTransactionError({
5787
+ action: 'simulate',
5788
+ signature: '',
5789
+ transactionMessage: res.error.message,
5790
+ transactionLogs: logs,
5791
+ });
5789
5792
  }
5790
5793
  return res.result;
5791
5794
  }
@@ -5916,14 +5919,17 @@ export class Connection {
5916
5919
  const unsafeRes = await this._rpcRequest('sendTransaction', args);
5917
5920
  const res = create(unsafeRes, SendTransactionRpcResult);
5918
5921
  if ('error' in res) {
5919
- let logs;
5922
+ let logs = undefined;
5920
5923
  if ('data' in res.error) {
5921
5924
  logs = res.error.data.logs;
5922
5925
  }
5923
- throw new SendTransactionError(
5924
- 'failed to send transaction: ' + res.error.message,
5925
- logs,
5926
- );
5926
+
5927
+ throw new SendTransactionError({
5928
+ action: skipPreflight ? 'send' : 'simulate',
5929
+ signature: '',
5930
+ transactionMessage: res.error.message,
5931
+ transactionLogs: logs,
5932
+ });
5927
5933
  }
5928
5934
  return res.result;
5929
5935
  }
package/src/errors.ts CHANGED
@@ -1,10 +1,77 @@
1
+ import {Connection} from './connection';
2
+ import {TransactionSignature} from './transaction';
3
+
1
4
  export class SendTransactionError extends Error {
2
- logs: string[] | undefined;
5
+ private signature: TransactionSignature;
6
+ private transactionMessage: string;
7
+ private transactionLogs?: string[];
8
+ private resolvedLogs: string[] | Promise<string[]> | undefined;
9
+
10
+ constructor({
11
+ action,
12
+ signature,
13
+ transactionMessage,
14
+ transactionLogs,
15
+ }: {
16
+ action: 'send' | 'simulate';
17
+ signature: TransactionSignature;
18
+ transactionMessage: string;
19
+ transactionLogs?: string[];
20
+ }) {
21
+ let message: string;
3
22
 
4
- constructor(message: string, logs?: string[]) {
23
+ switch (action) {
24
+ case 'send':
25
+ message =
26
+ `Transaction ${signature} resulted in an error. \n` +
27
+ `${transactionMessage}. ` +
28
+ (transactionLogs
29
+ ? `Logs: \n${JSON.stringify(transactionLogs.slice(-10), null, 2)}. `
30
+ : '') +
31
+ '\nCatch the SendTransactionError and call `getLogs()` on it for full details.';
32
+ break;
33
+ case 'simulate':
34
+ message =
35
+ `Simulation failed. \nMessage: ${transactionMessage}. \n` +
36
+ (transactionLogs
37
+ ? `Logs: \n${JSON.stringify(transactionLogs.slice(-10), null, 2)}. `
38
+ : '') +
39
+ '\nCatch the SendTransactionError and call `getLogs()` on it for full details.';
40
+ break;
41
+ default:
42
+ message = 'Unknown action';
43
+ }
5
44
  super(message);
6
45
 
7
- this.logs = logs;
46
+ this.signature = signature;
47
+ this.transactionMessage = transactionMessage;
48
+ this.transactionLogs = transactionLogs;
49
+ this.resolvedLogs = transactionLogs ? transactionLogs : undefined;
50
+ }
51
+
52
+ get transactionError(): {message: string; logs?: string[]} {
53
+ return {message: this.transactionMessage, logs: this.transactionLogs};
54
+ }
55
+
56
+ async getLogs(connection: Connection): Promise<string[]> {
57
+ if (this.resolvedLogs === undefined) {
58
+ this.resolvedLogs = new Promise((resolve, reject) => {
59
+ connection
60
+ .getTransaction(this.signature)
61
+ .then(tx => {
62
+ if (tx && tx.meta && tx.meta.logMessages) {
63
+ const logs = tx.meta.logMessages;
64
+ this.resolvedLogs = logs;
65
+ this.transactionLogs = logs;
66
+ resolve(logs);
67
+ } else {
68
+ reject(new Error('Log messages not found'));
69
+ }
70
+ })
71
+ .catch(reject);
72
+ });
73
+ }
74
+ return await this.resolvedLogs;
8
75
  }
9
76
  }
10
77
 
@@ -8,6 +8,7 @@ import {
8
8
  } from '../connection';
9
9
  import type {TransactionSignature} from '../transaction';
10
10
  import type {ConfirmOptions} from '../connection';
11
+ import {SendTransactionError} from '../errors';
11
12
 
12
13
  /**
13
14
  * Send and confirm a raw transaction
@@ -93,6 +94,13 @@ export async function sendAndConfirmRawTransaction(
93
94
  const status = (await confirmationPromise).value;
94
95
 
95
96
  if (status.err) {
97
+ if (signature != null) {
98
+ throw new SendTransactionError({
99
+ action: sendOptions?.skipPreflight ? 'send' : 'simulate',
100
+ signature: signature,
101
+ transactionMessage: `Status: (${JSON.stringify(status)})`,
102
+ });
103
+ }
96
104
  throw new Error(
97
105
  `Raw transaction ${signature} failed (${JSON.stringify(status)})`,
98
106
  );
@@ -3,6 +3,7 @@ import {Transaction} from '../transaction';
3
3
  import type {ConfirmOptions} from '../connection';
4
4
  import type {Signer} from '../keypair';
5
5
  import type {TransactionSignature} from '../transaction';
6
+ import {SendTransactionError} from '../errors';
6
7
 
7
8
  /**
8
9
  * Sign, send and confirm a transaction.
@@ -89,6 +90,13 @@ export async function sendAndConfirmTransaction(
89
90
  }
90
91
 
91
92
  if (status.err) {
93
+ if (signature != null) {
94
+ throw new SendTransactionError({
95
+ action: 'send',
96
+ signature: signature,
97
+ transactionMessage: `Status: (${JSON.stringify(status)})`,
98
+ });
99
+ }
92
100
  throw new Error(
93
101
  `Transaction ${signature} failed (${JSON.stringify(status)})`,
94
102
  );