@solana/web3.js 1.40.1 → 1.41.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.40.1",
3
+ "version": "1.41.0",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -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';