@solana/web3.js 1.35.0 → 1.37.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.
@@ -17,39 +17,69 @@ export type Lockout = {
17
17
  /**
18
18
  * History of how many credits earned by the end of each epoch
19
19
  */
20
- export type EpochCredits = {
20
+ export type EpochCredits = Readonly<{
21
21
  epoch: number;
22
22
  credits: number;
23
23
  prevCredits: number;
24
- };
24
+ }>;
25
25
 
26
- export type AuthorizedVoter = {
26
+ export type AuthorizedVoter = Readonly<{
27
27
  epoch: number;
28
28
  authorizedVoter: PublicKey;
29
- };
29
+ }>;
30
30
 
31
- export type PriorVoter = {
31
+ type AuthorizedVoterRaw = Readonly<{
32
+ authorizedVoter: Uint8Array;
33
+ epoch: number;
34
+ }>;
35
+
36
+ type PriorVoters = Readonly<{
37
+ buf: PriorVoterRaw[];
38
+ idx: number;
39
+ isEmpty: number;
40
+ }>;
41
+
42
+ export type PriorVoter = Readonly<{
32
43
  authorizedPubkey: PublicKey;
33
44
  epochOfLastAuthorizedSwitch: number;
34
45
  targetEpoch: number;
35
- };
46
+ }>;
36
47
 
37
- export type BlockTimestamp = {
48
+ type PriorVoterRaw = Readonly<{
49
+ authorizedPubkey: Uint8Array;
50
+ epochOfLastAuthorizedSwitch: number;
51
+ targetEpoch: number;
52
+ }>;
53
+
54
+ export type BlockTimestamp = Readonly<{
38
55
  slot: number;
39
- timetamp: number;
40
- };
56
+ timestamp: number;
57
+ }>;
58
+
59
+ type VoteAccountData = Readonly<{
60
+ authorizedVoters: AuthorizedVoterRaw[];
61
+ authorizedWithdrawer: Uint8Array;
62
+ commission: number;
63
+ epochCredits: EpochCredits[];
64
+ lastTimestamp: BlockTimestamp;
65
+ nodePubkey: Uint8Array;
66
+ priorVoters: PriorVoters;
67
+ rootSlot: number;
68
+ rootSlotValid: number;
69
+ votes: Lockout[];
70
+ }>;
41
71
 
42
72
  /**
43
73
  * See https://github.com/solana-labs/solana/blob/8a12ed029cfa38d4a45400916c2463fb82bbec8c/programs/vote_api/src/vote_state.rs#L68-L88
44
74
  *
45
75
  * @internal
46
76
  */
47
- const VoteAccountLayout = BufferLayout.struct([
77
+ const VoteAccountLayout = BufferLayout.struct<VoteAccountData>([
48
78
  Layout.publicKey('nodePubkey'),
49
79
  Layout.publicKey('authorizedWithdrawer'),
50
80
  BufferLayout.u8('commission'),
51
81
  BufferLayout.nu64(), // votes.length
52
- BufferLayout.seq(
82
+ BufferLayout.seq<Lockout>(
53
83
  BufferLayout.struct([
54
84
  BufferLayout.nu64('slot'),
55
85
  BufferLayout.u32('confirmationCount'),
@@ -60,7 +90,7 @@ const VoteAccountLayout = BufferLayout.struct([
60
90
  BufferLayout.u8('rootSlotValid'),
61
91
  BufferLayout.nu64('rootSlot'),
62
92
  BufferLayout.nu64(), // authorizedVoters.length
63
- BufferLayout.seq(
93
+ BufferLayout.seq<AuthorizedVoterRaw>(
64
94
  BufferLayout.struct([
65
95
  BufferLayout.nu64('epoch'),
66
96
  Layout.publicKey('authorizedVoter'),
@@ -68,7 +98,7 @@ const VoteAccountLayout = BufferLayout.struct([
68
98
  BufferLayout.offset(BufferLayout.u32(), -8),
69
99
  'authorizedVoters',
70
100
  ),
71
- BufferLayout.struct(
101
+ BufferLayout.struct<PriorVoters>(
72
102
  [
73
103
  BufferLayout.seq(
74
104
  BufferLayout.struct([
@@ -85,7 +115,7 @@ const VoteAccountLayout = BufferLayout.struct([
85
115
  'priorVoters',
86
116
  ),
87
117
  BufferLayout.nu64(), // epochCredits.length
88
- BufferLayout.seq(
118
+ BufferLayout.seq<EpochCredits>(
89
119
  BufferLayout.struct([
90
120
  BufferLayout.nu64('epoch'),
91
121
  BufferLayout.nu64('credits'),
@@ -94,7 +124,7 @@ const VoteAccountLayout = BufferLayout.struct([
94
124
  BufferLayout.offset(BufferLayout.u32(), -8),
95
125
  'epochCredits',
96
126
  ),
97
- BufferLayout.struct(
127
+ BufferLayout.struct<BlockTimestamp>(
98
128
  [BufferLayout.nu64('slot'), BufferLayout.nu64('timestamp')],
99
129
  'lastTimestamp',
100
130
  ),
@@ -172,7 +202,10 @@ export class VoteAccount {
172
202
  }
173
203
  }
174
204
 
175
- function parseAuthorizedVoter({epoch, authorizedVoter}: AuthorizedVoter) {
205
+ function parseAuthorizedVoter({
206
+ authorizedVoter,
207
+ epoch,
208
+ }: AuthorizedVoterRaw): AuthorizedVoter {
176
209
  return {
177
210
  epoch,
178
211
  authorizedVoter: new PublicKey(authorizedVoter),
@@ -183,7 +216,7 @@ function parsePriorVoters({
183
216
  authorizedPubkey,
184
217
  epochOfLastAuthorizedSwitch,
185
218
  targetEpoch,
186
- }: PriorVoter) {
219
+ }: PriorVoterRaw): PriorVoter {
187
220
  return {
188
221
  authorizedPubkey: new PublicKey(authorizedPubkey),
189
222
  epochOfLastAuthorizedSwitch,
@@ -191,18 +224,13 @@ function parsePriorVoters({
191
224
  };
192
225
  }
193
226
 
194
- function getPriorVoters({
195
- buf,
196
- idx,
197
- isEmpty,
198
- }: {
199
- buf: PriorVoter[];
200
- idx: number;
201
- isEmpty: boolean;
202
- }): PriorVoter[] {
227
+ function getPriorVoters({buf, idx, isEmpty}: PriorVoters): PriorVoter[] {
203
228
  if (isEmpty) {
204
229
  return [];
205
230
  }
206
231
 
207
- return [...buf.slice(idx + 1).map(parsePriorVoters), ...buf.slice(0, idx)];
232
+ return [
233
+ ...buf.slice(idx + 1).map(parsePriorVoters),
234
+ ...buf.slice(0, idx).map(parsePriorVoters),
235
+ ];
208
236
  }
@@ -1,6 +1,11 @@
1
1
  import * as BufferLayout from '@solana/buffer-layout';
2
2
 
3
- import {encodeData, decodeData, InstructionType} from './instruction';
3
+ import {
4
+ encodeData,
5
+ decodeData,
6
+ InstructionType,
7
+ IInstructionInputData,
8
+ } from './instruction';
4
9
  import * as Layout from './layout';
5
10
  import {PublicKey} from './publickey';
6
11
  import {SystemProgram} from './system-program';
@@ -202,23 +207,45 @@ export class VoteInstruction {
202
207
  * An enumeration of valid VoteInstructionType's
203
208
  */
204
209
  export type VoteInstructionType =
205
- | 'Authorize'
206
- | 'InitializeAccount'
207
- | 'Withdraw';
210
+ // FIXME
211
+ // It would be preferable for this type to be `keyof VoteInstructionInputData`
212
+ // but Typedoc does not transpile `keyof` expressions.
213
+ // See https://github.com/TypeStrong/typedoc/issues/1894
214
+ 'Authorize' | 'InitializeAccount' | 'Withdraw';
215
+
216
+ type VoteInstructionInputData = {
217
+ Authorize: IInstructionInputData & {
218
+ newAuthorized: Uint8Array;
219
+ voteAuthorizationType: number;
220
+ };
221
+ InitializeAccount: IInstructionInputData & {
222
+ voteInit: Readonly<{
223
+ authorizedVoter: Uint8Array;
224
+ authorizedWithdrawer: Uint8Array;
225
+ commission: number;
226
+ nodePubkey: Uint8Array;
227
+ }>;
228
+ };
229
+ Withdraw: IInstructionInputData & {
230
+ lamports: number;
231
+ };
232
+ };
208
233
 
209
- const VOTE_INSTRUCTION_LAYOUTS: {
210
- [type in VoteInstructionType]: InstructionType;
211
- } = Object.freeze({
234
+ const VOTE_INSTRUCTION_LAYOUTS = Object.freeze<{
235
+ [Instruction in VoteInstructionType]: InstructionType<
236
+ VoteInstructionInputData[Instruction]
237
+ >;
238
+ }>({
212
239
  InitializeAccount: {
213
240
  index: 0,
214
- layout: BufferLayout.struct([
241
+ layout: BufferLayout.struct<VoteInstructionInputData['InitializeAccount']>([
215
242
  BufferLayout.u32('instruction'),
216
243
  Layout.voteInit(),
217
244
  ]),
218
245
  },
219
246
  Authorize: {
220
247
  index: 1,
221
- layout: BufferLayout.struct([
248
+ layout: BufferLayout.struct<VoteInstructionInputData['Authorize']>([
222
249
  BufferLayout.u32('instruction'),
223
250
  Layout.publicKey('newAuthorized'),
224
251
  BufferLayout.u32('voteAuthorizationType'),
@@ -226,7 +253,7 @@ const VOTE_INSTRUCTION_LAYOUTS: {
226
253
  },
227
254
  Withdraw: {
228
255
  index: 3,
229
- layout: BufferLayout.struct([
256
+ layout: BufferLayout.struct<VoteInstructionInputData['Withdraw']>([
230
257
  BufferLayout.u32('instruction'),
231
258
  BufferLayout.ns64('lamports'),
232
259
  ]),