@solana/web3.js 1.32.2 → 1.33.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/module.flow.js CHANGED
@@ -4082,6 +4082,121 @@ lastValidBlockHeight: number,...
4082
4082
  buffer: Buffer | Uint8Array | Array<number>
4083
4083
  ): VoteAccount;
4084
4084
  }
4085
+
4086
+ /**
4087
+ * Vote account info
4088
+ */
4089
+ declare export class VoteInit {
4090
+ nodePubkey: PublicKey;
4091
+ authorizedVoter: PublicKey;
4092
+ authorizedWithdrawer: PublicKey;
4093
+ commission: number;
4094
+ constructor(
4095
+ nodePubkey: PublicKey,
4096
+ authorizedVoter: PublicKey,
4097
+ authorizedWithdrawer: PublicKey,
4098
+ commission: number
4099
+ ): this;
4100
+ }
4101
+
4102
+ /**
4103
+ * Create vote account transaction params
4104
+ */
4105
+ declare export type CreateVoteAccountParams = {
4106
+ fromPubkey: PublicKey,
4107
+ votePubkey: PublicKey,
4108
+ voteInit: VoteInit,
4109
+ lamports: number,
4110
+ ...
4111
+ };
4112
+
4113
+ /**
4114
+ * InitializeAccount instruction params
4115
+ */
4116
+ declare export type InitializeAccountParams = {
4117
+ votePubkey: PublicKey,
4118
+ nodePubkey: PublicKey,
4119
+ voteInit: VoteInit,
4120
+ ...
4121
+ };
4122
+
4123
+ /**
4124
+ * Withdraw from vote account transaction params
4125
+ */
4126
+ declare export type WithdrawFromVoteAccountParams = {
4127
+ votePubkey: PublicKey,
4128
+ authorizedWithdrawerPubkey: PublicKey,
4129
+ lamports: number,
4130
+ toPubkey: PublicKey,
4131
+ ...
4132
+ };
4133
+
4134
+ /**
4135
+ * Vote Instruction class
4136
+ */
4137
+ declare export class VoteInstruction {
4138
+ /**
4139
+ * Decode a vote instruction and retrieve the instruction type.
4140
+ */
4141
+ static decodeInstructionType(
4142
+ instruction: TransactionInstruction
4143
+ ): VoteInstructionType;
4144
+
4145
+ /**
4146
+ * Decode an initialize vote instruction and retrieve the instruction params.
4147
+ */
4148
+ static decodeInitializeAccount(
4149
+ instruction: TransactionInstruction
4150
+ ): InitializeAccountParams;
4151
+
4152
+ /**
4153
+ * Decode a withdraw instruction and retrieve the instruction params.
4154
+ */
4155
+ static decodeWithdraw(
4156
+ instruction: TransactionInstruction
4157
+ ): WithdrawFromVoteAccountParams;
4158
+ }
4159
+
4160
+ /**
4161
+ * An enumeration of valid VoteInstructionType's
4162
+ */
4163
+ declare export type VoteInstructionType = "InitializeAccount" | "Withdraw";
4164
+
4165
+ /**
4166
+ * Factory class for transactions to interact with the Vote program
4167
+ */
4168
+ declare export class VoteProgram {
4169
+ /**
4170
+ * Public key that identifies the Vote program
4171
+ */
4172
+ static programId: PublicKey;
4173
+
4174
+ /**
4175
+ * Max space of a Vote account
4176
+ *
4177
+ * This is generated from the solana-vote-program VoteState struct as
4178
+ * `VoteState::size_of()`:
4179
+ * https://docs.rs/solana-vote-program/1.9.5/solana_vote_program/vote_state/struct.VoteState.html#method.size_of
4180
+ */
4181
+ static space: number;
4182
+
4183
+ /**
4184
+ * Generate an Initialize instruction.
4185
+ */
4186
+ static initializeAccount(
4187
+ params: InitializeAccountParams
4188
+ ): TransactionInstruction;
4189
+
4190
+ /**
4191
+ * Generate a transaction that creates a new Vote account.
4192
+ */
4193
+ static createAccount(params: CreateVoteAccountParams): Transaction;
4194
+
4195
+ /**
4196
+ * Generate a transaction to withdraw from a Vote account.
4197
+ */
4198
+ static withdraw(params: WithdrawFromVoteAccountParams): Transaction;
4199
+ }
4085
4200
  declare export var SYSVAR_CLOCK_PUBKEY: PublicKey;
4086
4201
  declare export var SYSVAR_EPOCH_SCHEDULE_PUBKEY: PublicKey;
4087
4202
  declare export var SYSVAR_INSTRUCTIONS_PUBKEY: PublicKey;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.32.2",
3
+ "version": "1.33.0",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
package/src/index.ts CHANGED
@@ -17,6 +17,7 @@ export * from './secp256k1-program';
17
17
  export * from './transaction';
18
18
  export * from './validator-info';
19
19
  export * from './vote-account';
20
+ export * from './vote-program';
20
21
  export * from './sysvar';
21
22
  export * from './errors';
22
23
  export * from './util/borsh-schema';
package/src/layout.ts CHANGED
@@ -79,6 +79,21 @@ export const lockup = (property: string = 'lockup') => {
79
79
  );
80
80
  };
81
81
 
82
+ /**
83
+ * Layout for a VoteInit object
84
+ */
85
+ export const voteInit = (property: string = 'voteInit') => {
86
+ return BufferLayout.struct(
87
+ [
88
+ publicKey('nodePubkey'),
89
+ publicKey('authorizedVoter'),
90
+ publicKey('authorizedWithdrawer'),
91
+ BufferLayout.u8('commission'),
92
+ ],
93
+ property,
94
+ );
95
+ };
96
+
82
97
  export function getAlloc(type: any, fields: any): number {
83
98
  let alloc = 0;
84
99
  type.layout.fields.forEach((item: any) => {
@@ -0,0 +1,290 @@
1
+ import * as BufferLayout from '@solana/buffer-layout';
2
+
3
+ import {encodeData, decodeData, InstructionType} from './instruction';
4
+ import * as Layout from './layout';
5
+ import {PublicKey} from './publickey';
6
+ import {SystemProgram} from './system-program';
7
+ import {SYSVAR_CLOCK_PUBKEY, SYSVAR_RENT_PUBKEY} from './sysvar';
8
+ import {Transaction, TransactionInstruction} from './transaction';
9
+ import {toBuffer} from './util/to-buffer';
10
+
11
+ /**
12
+ * Vote account info
13
+ */
14
+ export class VoteInit {
15
+ nodePubkey: PublicKey;
16
+ authorizedVoter: PublicKey;
17
+ authorizedWithdrawer: PublicKey;
18
+ commission: number; /** [0, 100] */
19
+
20
+ constructor(
21
+ nodePubkey: PublicKey,
22
+ authorizedVoter: PublicKey,
23
+ authorizedWithdrawer: PublicKey,
24
+ commission: number,
25
+ ) {
26
+ this.nodePubkey = nodePubkey;
27
+ this.authorizedVoter = authorizedVoter;
28
+ this.authorizedWithdrawer = authorizedWithdrawer;
29
+ this.commission = commission;
30
+ }
31
+ }
32
+
33
+ /**
34
+ * Create vote account transaction params
35
+ */
36
+ export type CreateVoteAccountParams = {
37
+ fromPubkey: PublicKey;
38
+ votePubkey: PublicKey;
39
+ voteInit: VoteInit;
40
+ lamports: number;
41
+ };
42
+
43
+ /**
44
+ * InitializeAccount instruction params
45
+ */
46
+ export type InitializeAccountParams = {
47
+ votePubkey: PublicKey;
48
+ nodePubkey: PublicKey;
49
+ voteInit: VoteInit;
50
+ };
51
+
52
+ /**
53
+ * Withdraw from vote account transaction params
54
+ */
55
+ export type WithdrawFromVoteAccountParams = {
56
+ votePubkey: PublicKey;
57
+ authorizedWithdrawerPubkey: PublicKey;
58
+ lamports: number;
59
+ toPubkey: PublicKey;
60
+ };
61
+
62
+ /**
63
+ * Vote Instruction class
64
+ */
65
+ export class VoteInstruction {
66
+ /**
67
+ * @internal
68
+ */
69
+ constructor() {}
70
+
71
+ /**
72
+ * Decode a vote instruction and retrieve the instruction type.
73
+ */
74
+ static decodeInstructionType(
75
+ instruction: TransactionInstruction,
76
+ ): VoteInstructionType {
77
+ this.checkProgramId(instruction.programId);
78
+
79
+ const instructionTypeLayout = BufferLayout.u32('instruction');
80
+ const typeIndex = instructionTypeLayout.decode(instruction.data);
81
+
82
+ let type: VoteInstructionType | undefined;
83
+ for (const [ixType, layout] of Object.entries(VOTE_INSTRUCTION_LAYOUTS)) {
84
+ if (layout.index == typeIndex) {
85
+ type = ixType as VoteInstructionType;
86
+ break;
87
+ }
88
+ }
89
+
90
+ if (!type) {
91
+ throw new Error('Instruction type incorrect; not a VoteInstruction');
92
+ }
93
+
94
+ return type;
95
+ }
96
+
97
+ /**
98
+ * Decode an initialize vote instruction and retrieve the instruction params.
99
+ */
100
+ static decodeInitializeAccount(
101
+ instruction: TransactionInstruction,
102
+ ): InitializeAccountParams {
103
+ this.checkProgramId(instruction.programId);
104
+ this.checkKeyLength(instruction.keys, 4);
105
+
106
+ const {voteInit} = decodeData(
107
+ VOTE_INSTRUCTION_LAYOUTS.InitializeAccount,
108
+ instruction.data,
109
+ );
110
+
111
+ return {
112
+ votePubkey: instruction.keys[0].pubkey,
113
+ nodePubkey: instruction.keys[3].pubkey,
114
+ voteInit: new VoteInit(
115
+ new PublicKey(voteInit.nodePubkey),
116
+ new PublicKey(voteInit.authorizedVoter),
117
+ new PublicKey(voteInit.authorizedWithdrawer),
118
+ voteInit.commission,
119
+ ),
120
+ };
121
+ }
122
+
123
+ /**
124
+ * Decode a withdraw instruction and retrieve the instruction params.
125
+ */
126
+ static decodeWithdraw(
127
+ instruction: TransactionInstruction,
128
+ ): WithdrawFromVoteAccountParams {
129
+ this.checkProgramId(instruction.programId);
130
+ this.checkKeyLength(instruction.keys, 3);
131
+
132
+ const {lamports} = decodeData(
133
+ VOTE_INSTRUCTION_LAYOUTS.Withdraw,
134
+ instruction.data,
135
+ );
136
+
137
+ return {
138
+ votePubkey: instruction.keys[0].pubkey,
139
+ authorizedWithdrawerPubkey: instruction.keys[2].pubkey,
140
+ lamports,
141
+ toPubkey: instruction.keys[1].pubkey,
142
+ };
143
+ }
144
+
145
+ /**
146
+ * @internal
147
+ */
148
+ static checkProgramId(programId: PublicKey) {
149
+ if (!programId.equals(VoteProgram.programId)) {
150
+ throw new Error('invalid instruction; programId is not VoteProgram');
151
+ }
152
+ }
153
+
154
+ /**
155
+ * @internal
156
+ */
157
+ static checkKeyLength(keys: Array<any>, expectedLength: number) {
158
+ if (keys.length < expectedLength) {
159
+ throw new Error(
160
+ `invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`,
161
+ );
162
+ }
163
+ }
164
+ }
165
+
166
+ /**
167
+ * An enumeration of valid VoteInstructionType's
168
+ */
169
+ export type VoteInstructionType = 'InitializeAccount' | 'Withdraw';
170
+
171
+ const VOTE_INSTRUCTION_LAYOUTS: {
172
+ [type in VoteInstructionType]: InstructionType;
173
+ } = Object.freeze({
174
+ InitializeAccount: {
175
+ index: 0,
176
+ layout: BufferLayout.struct([
177
+ BufferLayout.u32('instruction'),
178
+ Layout.voteInit(),
179
+ ]),
180
+ },
181
+ Withdraw: {
182
+ index: 3,
183
+ layout: BufferLayout.struct([
184
+ BufferLayout.u32('instruction'),
185
+ BufferLayout.ns64('lamports'),
186
+ ]),
187
+ },
188
+ });
189
+
190
+ /**
191
+ * Factory class for transactions to interact with the Vote program
192
+ */
193
+ export class VoteProgram {
194
+ /**
195
+ * @internal
196
+ */
197
+ constructor() {}
198
+
199
+ /**
200
+ * Public key that identifies the Vote program
201
+ */
202
+ static programId: PublicKey = new PublicKey(
203
+ 'Vote111111111111111111111111111111111111111',
204
+ );
205
+
206
+ /**
207
+ * Max space of a Vote account
208
+ *
209
+ * This is generated from the solana-vote-program VoteState struct as
210
+ * `VoteState::size_of()`:
211
+ * https://docs.rs/solana-vote-program/1.9.5/solana_vote_program/vote_state/struct.VoteState.html#method.size_of
212
+ */
213
+ static space: number = 3731;
214
+
215
+ /**
216
+ * Generate an Initialize instruction.
217
+ */
218
+ static initializeAccount(
219
+ params: InitializeAccountParams,
220
+ ): TransactionInstruction {
221
+ const {votePubkey, nodePubkey, voteInit} = params;
222
+ const type = VOTE_INSTRUCTION_LAYOUTS.InitializeAccount;
223
+ const data = encodeData(type, {
224
+ voteInit: {
225
+ nodePubkey: toBuffer(voteInit.nodePubkey.toBuffer()),
226
+ authorizedVoter: toBuffer(voteInit.authorizedVoter.toBuffer()),
227
+ authorizedWithdrawer: toBuffer(
228
+ voteInit.authorizedWithdrawer.toBuffer(),
229
+ ),
230
+ commission: voteInit.commission,
231
+ },
232
+ });
233
+ const instructionData = {
234
+ keys: [
235
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
236
+ {pubkey: SYSVAR_RENT_PUBKEY, isSigner: false, isWritable: false},
237
+ {pubkey: SYSVAR_CLOCK_PUBKEY, isSigner: false, isWritable: false},
238
+ {pubkey: nodePubkey, isSigner: true, isWritable: false},
239
+ ],
240
+ programId: this.programId,
241
+ data,
242
+ };
243
+ return new TransactionInstruction(instructionData);
244
+ }
245
+
246
+ /**
247
+ * Generate a transaction that creates a new Vote account.
248
+ */
249
+ static createAccount(params: CreateVoteAccountParams): Transaction {
250
+ const transaction = new Transaction();
251
+ transaction.add(
252
+ SystemProgram.createAccount({
253
+ fromPubkey: params.fromPubkey,
254
+ newAccountPubkey: params.votePubkey,
255
+ lamports: params.lamports,
256
+ space: this.space,
257
+ programId: this.programId,
258
+ }),
259
+ );
260
+
261
+ return transaction.add(
262
+ this.initializeAccount({
263
+ votePubkey: params.votePubkey,
264
+ nodePubkey: params.voteInit.nodePubkey,
265
+ voteInit: params.voteInit,
266
+ }),
267
+ );
268
+ }
269
+
270
+ /**
271
+ * Generate a transaction to withdraw from a Vote account.
272
+ */
273
+ static withdraw(params: WithdrawFromVoteAccountParams): Transaction {
274
+ const {votePubkey, authorizedWithdrawerPubkey, lamports, toPubkey} = params;
275
+ const type = VOTE_INSTRUCTION_LAYOUTS.Withdraw;
276
+ const data = encodeData(type, {lamports});
277
+
278
+ const keys = [
279
+ {pubkey: votePubkey, isSigner: false, isWritable: true},
280
+ {pubkey: toPubkey, isSigner: false, isWritable: true},
281
+ {pubkey: authorizedWithdrawerPubkey, isSigner: true, isWritable: false},
282
+ ];
283
+
284
+ return new Transaction().add({
285
+ keys,
286
+ programId: this.programId,
287
+ data,
288
+ });
289
+ }
290
+ }