@solana/web3.js 1.40.0 → 1.41.1

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.40.0",
3
+ "version": "1.41.1",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -50,8 +50,8 @@
50
50
  "pretty": "prettier --check '{,{src,test}/**/}*.{j,t}s'",
51
51
  "pretty:fix": "prettier --write '{,{src,test}/**/}*.{j,t}s'",
52
52
  "re": "semantic-release --repository-url git@github.com:solana-labs/solana-web3.js.git",
53
- "test": "mocha -r ts-node/register './test/**/*.test.ts'",
54
- "test:cover": "TS_NODE_COMPILER_OPTIONS='{\"module\": \"commonjs\" }' nyc --reporter=lcov mocha -r ts-node/register './test/**/*.test.ts'",
53
+ "test": "cross-env TS_NODE_COMPILER_OPTIONS='{ \"module\": \"commonjs\" }' ts-mocha --require esm './test/**/*.test.ts'",
54
+ "test:cover": "nyc --reporter=lcov npm run test",
55
55
  "test:live": "TEST_LIVE=1 npm run test",
56
56
  "test:live-with-test-validator": "start-server-and-test 'solana-test-validator --reset --quiet' http://localhost:8899/health test:live"
57
57
  },
@@ -117,7 +117,6 @@
117
117
  "npm-run-all": "^4.1.5",
118
118
  "nyc": "^15.1.0",
119
119
  "prettier": "^2.3.0",
120
- "puppeteer": "^12.0.0",
121
120
  "rimraf": "3.0.2",
122
121
  "rollup": "2.60.2",
123
122
  "rollup-plugin-dts": "^4.0.0",
@@ -126,6 +125,7 @@
126
125
  "semantic-release": "^18.0.0",
127
126
  "sinon": "^12.0.0",
128
127
  "start-server-and-test": "^1.12.0",
128
+ "ts-mocha": "^9.0.2",
129
129
  "ts-node": "^10.0.0",
130
130
  "tslib": "^2.1.0",
131
131
  "typedoc": "^0.22.2",
@@ -0,0 +1,189 @@
1
+ import * as BufferLayout from '@solana/buffer-layout';
2
+
3
+ import {
4
+ encodeData,
5
+ decodeData,
6
+ InstructionType,
7
+ IInstructionInputData,
8
+ } from './instruction';
9
+ import {PublicKey} from './publickey';
10
+ import {TransactionInstruction} from './transaction';
11
+
12
+ /**
13
+ * Compute Budget Instruction class
14
+ */
15
+ export class ComputeBudgetInstruction {
16
+ /**
17
+ * @internal
18
+ */
19
+ constructor() {}
20
+
21
+ /**
22
+ * Decode a compute budget instruction and retrieve the instruction type.
23
+ */
24
+ static decodeInstructionType(
25
+ instruction: TransactionInstruction,
26
+ ): ComputeBudgetInstructionType {
27
+ this.checkProgramId(instruction.programId);
28
+
29
+ const instructionTypeLayout = BufferLayout.u8('instruction');
30
+ const typeIndex = instructionTypeLayout.decode(instruction.data);
31
+
32
+ let type: ComputeBudgetInstructionType | undefined;
33
+ for (const [ixType, layout] of Object.entries(
34
+ COMPUTE_BUDGET_INSTRUCTION_LAYOUTS,
35
+ )) {
36
+ if (layout.index == typeIndex) {
37
+ type = ixType as ComputeBudgetInstructionType;
38
+ break;
39
+ }
40
+ }
41
+
42
+ if (!type) {
43
+ throw new Error(
44
+ 'Instruction type incorrect; not a ComputeBudgetInstruction',
45
+ );
46
+ }
47
+
48
+ return type;
49
+ }
50
+
51
+ /**
52
+ * Decode request units compute budget instruction and retrieve the instruction params.
53
+ */
54
+ static decodeRequestUnits(
55
+ instruction: TransactionInstruction,
56
+ ): RequestUnitsParams {
57
+ this.checkProgramId(instruction.programId);
58
+ const {units, additionalFee} = decodeData(
59
+ COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.RequestUnits,
60
+ instruction.data,
61
+ );
62
+ return {units, additionalFee};
63
+ }
64
+
65
+ /**
66
+ * Decode request heap frame compute budget instruction and retrieve the instruction params.
67
+ */
68
+ static decodeRequestHeapFrame(
69
+ instruction: TransactionInstruction,
70
+ ): RequestHeapFrameParams {
71
+ this.checkProgramId(instruction.programId);
72
+ const {bytes} = decodeData(
73
+ COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.RequestHeapFrame,
74
+ instruction.data,
75
+ );
76
+ return {bytes};
77
+ }
78
+
79
+ /**
80
+ * @internal
81
+ */
82
+ static checkProgramId(programId: PublicKey) {
83
+ if (!programId.equals(ComputeBudgetProgram.programId)) {
84
+ throw new Error(
85
+ 'invalid instruction; programId is not ComputeBudgetProgram',
86
+ );
87
+ }
88
+ }
89
+ }
90
+
91
+ /**
92
+ * An enumeration of valid ComputeBudgetInstructionType's
93
+ */
94
+ export type ComputeBudgetInstructionType =
95
+ // FIXME
96
+ // It would be preferable for this type to be `keyof ComputeBudgetInstructionInputData`
97
+ // but Typedoc does not transpile `keyof` expressions.
98
+ // See https://github.com/TypeStrong/typedoc/issues/1894
99
+ 'RequestUnits' | 'RequestHeapFrame';
100
+
101
+ type ComputeBudgetInstructionInputData = {
102
+ RequestUnits: IInstructionInputData & Readonly<RequestUnitsParams>;
103
+ RequestHeapFrame: IInstructionInputData & Readonly<RequestHeapFrameParams>;
104
+ };
105
+
106
+ /**
107
+ * Request units instruction params
108
+ */
109
+ export interface RequestUnitsParams {
110
+ /** Units to request for transaction-wide compute */
111
+ units: number;
112
+
113
+ /** Additional fee to pay */
114
+ additionalFee: number;
115
+ }
116
+
117
+ /**
118
+ * Request heap frame instruction params
119
+ */
120
+ export type RequestHeapFrameParams = {
121
+ /** Requested transaction-wide program heap size in bytes. Must be multiple of 1024. Applies to each program, including CPIs. */
122
+ bytes: number;
123
+ };
124
+
125
+ /**
126
+ * An enumeration of valid ComputeBudget InstructionType's
127
+ * @internal
128
+ */
129
+ export const COMPUTE_BUDGET_INSTRUCTION_LAYOUTS = Object.freeze<{
130
+ [Instruction in ComputeBudgetInstructionType]: InstructionType<
131
+ ComputeBudgetInstructionInputData[Instruction]
132
+ >;
133
+ }>({
134
+ RequestUnits: {
135
+ index: 0,
136
+ layout: BufferLayout.struct<
137
+ ComputeBudgetInstructionInputData['RequestUnits']
138
+ >([
139
+ BufferLayout.u8('instruction'),
140
+ BufferLayout.u32('units'),
141
+ BufferLayout.u32('additionalFee'),
142
+ ]),
143
+ },
144
+ RequestHeapFrame: {
145
+ index: 1,
146
+ layout: BufferLayout.struct<
147
+ ComputeBudgetInstructionInputData['RequestHeapFrame']
148
+ >([BufferLayout.u8('instruction'), BufferLayout.u32('bytes')]),
149
+ },
150
+ });
151
+
152
+ /**
153
+ * Factory class for transaction instructions to interact with the Compute Budget program
154
+ */
155
+ export class ComputeBudgetProgram {
156
+ /**
157
+ * @internal
158
+ */
159
+ constructor() {}
160
+
161
+ /**
162
+ * Public key that identifies the Compute Budget program
163
+ */
164
+ static programId: PublicKey = new PublicKey(
165
+ 'ComputeBudget111111111111111111111111111111',
166
+ );
167
+
168
+ static requestUnits(params: RequestUnitsParams): TransactionInstruction {
169
+ const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.RequestUnits;
170
+ const data = encodeData(type, params);
171
+ return new TransactionInstruction({
172
+ keys: [],
173
+ programId: this.programId,
174
+ data,
175
+ });
176
+ }
177
+
178
+ static requestHeapFrame(
179
+ params: RequestHeapFrameParams,
180
+ ): TransactionInstruction {
181
+ const type = COMPUTE_BUDGET_INSTRUCTION_LAYOUTS.RequestHeapFrame;
182
+ const data = encodeData(type, params);
183
+ return new TransactionInstruction({
184
+ keys: [],
185
+ programId: this.programId,
186
+ data,
187
+ });
188
+ }
189
+ }
package/src/index.ts CHANGED
@@ -2,6 +2,7 @@ export * from './account';
2
2
  export * from './blockhash';
3
3
  export * from './bpf-loader-deprecated';
4
4
  export * from './bpf-loader';
5
+ export * from './compute-budget';
5
6
  export * from './connection';
6
7
  export * from './epoch-schedule';
7
8
  export * from './ed25519-program';
package/src/loader.ts CHANGED
@@ -2,7 +2,7 @@ import {Buffer} from 'buffer';
2
2
  import * as BufferLayout from '@solana/buffer-layout';
3
3
 
4
4
  import {PublicKey} from './publickey';
5
- import {Transaction, PACKET_DATA_SIZE} from './transaction';
5
+ import {Transaction} from './transaction';
6
6
  import {SYSVAR_RENT_PUBKEY} from './sysvar';
7
7
  import {sendAndConfirmTransaction} from './util/send-and-confirm-transaction';
8
8
  import {sleep} from './util/sleep';
@@ -10,6 +10,7 @@ import type {Connection} from './connection';
10
10
  import type {Signer} from './keypair';
11
11
  import {SystemProgram} from './system-program';
12
12
  import {IInstructionInputData} from './instruction';
13
+ import {PACKET_DATA_SIZE} from './transaction-constants';
13
14
 
14
15
  // Keep program chunks under PACKET_DATA_SIZE, leaving enough room for the
15
16
  // rest of the Transaction fields
package/src/message.ts CHANGED
@@ -5,7 +5,7 @@ import * as BufferLayout from '@solana/buffer-layout';
5
5
  import {PublicKey} from './publickey';
6
6
  import type {Blockhash} from './blockhash';
7
7
  import * as Layout from './layout';
8
- import {PACKET_DATA_SIZE} from './transaction';
8
+ import {PACKET_DATA_SIZE} from './transaction-constants';
9
9
  import * as shortvec from './util/shortvec-encoding';
10
10
  import {toBuffer} from './util/to-buffer';
11
11
 
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Maximum over-the-wire size of a Transaction
3
+ *
4
+ * 1280 is IPv6 minimum MTU
5
+ * 40 bytes is the size of the IPv6 header
6
+ * 8 bytes is the size of the fragment header
7
+ */
8
+ export const PACKET_DATA_SIZE = 1280 - 40 - 8;
9
+
10
+ export const SIGNATURE_LENGTH_IN_BYTES = 64;
@@ -2,6 +2,10 @@ import nacl from 'tweetnacl';
2
2
  import bs58 from 'bs58';
3
3
  import {Buffer} from 'buffer';
4
4
 
5
+ import {
6
+ PACKET_DATA_SIZE,
7
+ SIGNATURE_LENGTH_IN_BYTES,
8
+ } from './transaction-constants';
5
9
  import {Connection} from './connection';
6
10
  import {Message} from './message';
7
11
  import {PublicKey} from './publickey';
@@ -19,21 +23,8 @@ export type TransactionSignature = string;
19
23
 
20
24
  /**
21
25
  * Default (empty) signature
22
- *
23
- * Signatures are 64 bytes in length
24
- */
25
- const DEFAULT_SIGNATURE = Buffer.alloc(64).fill(0);
26
-
27
- /**
28
- * Maximum over-the-wire size of a Transaction
29
- *
30
- * 1280 is IPv6 minimum MTU
31
- * 40 bytes is the size of the IPv6 header
32
- * 8 bytes is the size of the fragment header
33
26
  */
34
- export const PACKET_DATA_SIZE = 1280 - 40 - 8;
35
-
36
- const SIGNATURE_LENGTH = 64;
27
+ const DEFAULT_SIGNATURE = Buffer.alloc(SIGNATURE_LENGTH_IN_BYTES).fill(0);
37
28
 
38
29
  /**
39
30
  * Account metadata used to define instructions
@@ -747,8 +738,8 @@ export class Transaction {
747
738
  const signatureCount = shortvec.decodeLength(byteArray);
748
739
  let signatures = [];
749
740
  for (let i = 0; i < signatureCount; i++) {
750
- const signature = byteArray.slice(0, SIGNATURE_LENGTH);
751
- byteArray = byteArray.slice(SIGNATURE_LENGTH);
741
+ const signature = byteArray.slice(0, SIGNATURE_LENGTH_IN_BYTES);
742
+ byteArray = byteArray.slice(SIGNATURE_LENGTH_IN_BYTES);
752
743
  signatures.push(bs58.encode(Buffer.from(signature)));
753
744
  }
754
745