@solana/web3.js 1.32.1 → 1.35.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.
@@ -2068,6 +2068,13 @@ const authorized = (property = 'authorized') => {
2068
2068
  const lockup = (property = 'lockup') => {
2069
2069
  return BufferLayout.struct([BufferLayout.ns64('unixTimestamp'), BufferLayout.ns64('epoch'), publicKey('custodian')], property);
2070
2070
  };
2071
+ /**
2072
+ * Layout for a VoteInit object
2073
+ */
2074
+
2075
+ const voteInit = (property = 'voteInit') => {
2076
+ return BufferLayout.struct([publicKey('nodePubkey'), publicKey('authorizedVoter'), publicKey('authorizedWithdrawer'), BufferLayout.u8('commission')], property);
2077
+ };
2071
2078
  function getAlloc(type, fields) {
2072
2079
  let alloc = 0;
2073
2080
  type.layout.fields.forEach(item => {
@@ -7403,7 +7410,14 @@ class Connection {
7403
7410
  this._rpcWebSocketIdleTimeout = setTimeout(() => {
7404
7411
  this._rpcWebSocketIdleTimeout = null;
7405
7412
 
7406
- this._rpcWebSocket.close();
7413
+ try {
7414
+ this._rpcWebSocket.close();
7415
+ } catch (err) {
7416
+ // swallow error if socket has already been closed.
7417
+ if (err instanceof Error) {
7418
+ console.log(`Error when closing socket connection: ${err.message}`);
7419
+ }
7420
+ }
7407
7421
  }, 500);
7408
7422
  }
7409
7423
 
@@ -8715,30 +8729,22 @@ class StakeProgram {
8715
8729
  });
8716
8730
  }
8717
8731
  /**
8718
- * Generate a Transaction that splits Stake tokens into another stake account
8732
+ * @internal
8719
8733
  */
8720
8734
 
8721
8735
 
8722
- static split(params) {
8736
+ static splitInstruction(params) {
8723
8737
  const {
8724
8738
  stakePubkey,
8725
8739
  authorizedPubkey,
8726
8740
  splitStakePubkey,
8727
8741
  lamports
8728
8742
  } = params;
8729
- const transaction = new Transaction();
8730
- transaction.add(SystemProgram.createAccount({
8731
- fromPubkey: authorizedPubkey,
8732
- newAccountPubkey: splitStakePubkey,
8733
- lamports: 0,
8734
- space: this.space,
8735
- programId: this.programId
8736
- }));
8737
8743
  const type = STAKE_INSTRUCTION_LAYOUTS.Split;
8738
8744
  const data = encodeData(type, {
8739
8745
  lamports
8740
8746
  });
8741
- return transaction.add({
8747
+ return new TransactionInstruction({
8742
8748
  keys: [{
8743
8749
  pubkey: stakePubkey,
8744
8750
  isSigner: false,
@@ -8756,6 +8762,52 @@ class StakeProgram {
8756
8762
  data
8757
8763
  });
8758
8764
  }
8765
+ /**
8766
+ * Generate a Transaction that splits Stake tokens into another stake account
8767
+ */
8768
+
8769
+
8770
+ static split(params) {
8771
+ const transaction = new Transaction();
8772
+ transaction.add(SystemProgram.createAccount({
8773
+ fromPubkey: params.authorizedPubkey,
8774
+ newAccountPubkey: params.splitStakePubkey,
8775
+ lamports: 0,
8776
+ space: this.space,
8777
+ programId: this.programId
8778
+ }));
8779
+ return transaction.add(this.splitInstruction(params));
8780
+ }
8781
+ /**
8782
+ * Generate a Transaction that splits Stake tokens into another account
8783
+ * derived from a base public key and seed
8784
+ */
8785
+
8786
+
8787
+ static splitWithSeed(params) {
8788
+ const {
8789
+ stakePubkey,
8790
+ authorizedPubkey,
8791
+ splitStakePubkey,
8792
+ basePubkey,
8793
+ seed,
8794
+ lamports
8795
+ } = params;
8796
+ const transaction = new Transaction();
8797
+ transaction.add(SystemProgram.allocate({
8798
+ accountPubkey: splitStakePubkey,
8799
+ basePubkey,
8800
+ seed,
8801
+ space: this.space,
8802
+ programId: this.programId
8803
+ }));
8804
+ return transaction.add(this.splitInstruction({
8805
+ stakePubkey,
8806
+ authorizedPubkey,
8807
+ splitStakePubkey,
8808
+ lamports
8809
+ }));
8810
+ }
8759
8811
  /**
8760
8812
  * Generate a Transaction that merges Stake accounts.
8761
8813
  */
@@ -9214,6 +9266,322 @@ function getPriorVoters({
9214
9266
  return [...buf.slice(idx + 1).map(parsePriorVoters), ...buf.slice(0, idx)];
9215
9267
  }
9216
9268
 
9269
+ /**
9270
+ * Vote account info
9271
+ */
9272
+
9273
+ class VoteInit {
9274
+ /** [0, 100] */
9275
+ constructor(nodePubkey, authorizedVoter, authorizedWithdrawer, commission) {
9276
+ this.nodePubkey = void 0;
9277
+ this.authorizedVoter = void 0;
9278
+ this.authorizedWithdrawer = void 0;
9279
+ this.commission = void 0;
9280
+ this.nodePubkey = nodePubkey;
9281
+ this.authorizedVoter = authorizedVoter;
9282
+ this.authorizedWithdrawer = authorizedWithdrawer;
9283
+ this.commission = commission;
9284
+ }
9285
+
9286
+ }
9287
+ /**
9288
+ * Create vote account transaction params
9289
+ */
9290
+
9291
+ /**
9292
+ * Vote Instruction class
9293
+ */
9294
+ class VoteInstruction {
9295
+ /**
9296
+ * @internal
9297
+ */
9298
+ constructor() {}
9299
+ /**
9300
+ * Decode a vote instruction and retrieve the instruction type.
9301
+ */
9302
+
9303
+
9304
+ static decodeInstructionType(instruction) {
9305
+ this.checkProgramId(instruction.programId);
9306
+ const instructionTypeLayout = BufferLayout.u32('instruction');
9307
+ const typeIndex = instructionTypeLayout.decode(instruction.data);
9308
+ let type;
9309
+
9310
+ for (const [ixType, layout] of Object.entries(VOTE_INSTRUCTION_LAYOUTS)) {
9311
+ if (layout.index == typeIndex) {
9312
+ type = ixType;
9313
+ break;
9314
+ }
9315
+ }
9316
+
9317
+ if (!type) {
9318
+ throw new Error('Instruction type incorrect; not a VoteInstruction');
9319
+ }
9320
+
9321
+ return type;
9322
+ }
9323
+ /**
9324
+ * Decode an initialize vote instruction and retrieve the instruction params.
9325
+ */
9326
+
9327
+
9328
+ static decodeInitializeAccount(instruction) {
9329
+ this.checkProgramId(instruction.programId);
9330
+ this.checkKeyLength(instruction.keys, 4);
9331
+ const {
9332
+ voteInit
9333
+ } = decodeData(VOTE_INSTRUCTION_LAYOUTS.InitializeAccount, instruction.data);
9334
+ return {
9335
+ votePubkey: instruction.keys[0].pubkey,
9336
+ nodePubkey: instruction.keys[3].pubkey,
9337
+ voteInit: new VoteInit(new PublicKey(voteInit.nodePubkey), new PublicKey(voteInit.authorizedVoter), new PublicKey(voteInit.authorizedWithdrawer), voteInit.commission)
9338
+ };
9339
+ }
9340
+ /**
9341
+ * Decode an authorize instruction and retrieve the instruction params.
9342
+ */
9343
+
9344
+
9345
+ static decodeAuthorize(instruction) {
9346
+ this.checkProgramId(instruction.programId);
9347
+ this.checkKeyLength(instruction.keys, 3);
9348
+ const {
9349
+ newAuthorized,
9350
+ voteAuthorizationType
9351
+ } = decodeData(VOTE_INSTRUCTION_LAYOUTS.Authorize, instruction.data);
9352
+ return {
9353
+ votePubkey: instruction.keys[0].pubkey,
9354
+ authorizedPubkey: instruction.keys[2].pubkey,
9355
+ newAuthorizedPubkey: new PublicKey(newAuthorized),
9356
+ voteAuthorizationType: {
9357
+ index: voteAuthorizationType
9358
+ }
9359
+ };
9360
+ }
9361
+ /**
9362
+ * Decode a withdraw instruction and retrieve the instruction params.
9363
+ */
9364
+
9365
+
9366
+ static decodeWithdraw(instruction) {
9367
+ this.checkProgramId(instruction.programId);
9368
+ this.checkKeyLength(instruction.keys, 3);
9369
+ const {
9370
+ lamports
9371
+ } = decodeData(VOTE_INSTRUCTION_LAYOUTS.Withdraw, instruction.data);
9372
+ return {
9373
+ votePubkey: instruction.keys[0].pubkey,
9374
+ authorizedWithdrawerPubkey: instruction.keys[2].pubkey,
9375
+ lamports,
9376
+ toPubkey: instruction.keys[1].pubkey
9377
+ };
9378
+ }
9379
+ /**
9380
+ * @internal
9381
+ */
9382
+
9383
+
9384
+ static checkProgramId(programId) {
9385
+ if (!programId.equals(VoteProgram.programId)) {
9386
+ throw new Error('invalid instruction; programId is not VoteProgram');
9387
+ }
9388
+ }
9389
+ /**
9390
+ * @internal
9391
+ */
9392
+
9393
+
9394
+ static checkKeyLength(keys, expectedLength) {
9395
+ if (keys.length < expectedLength) {
9396
+ throw new Error(`invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`);
9397
+ }
9398
+ }
9399
+
9400
+ }
9401
+ /**
9402
+ * An enumeration of valid VoteInstructionType's
9403
+ */
9404
+
9405
+ const VOTE_INSTRUCTION_LAYOUTS = Object.freeze({
9406
+ InitializeAccount: {
9407
+ index: 0,
9408
+ layout: BufferLayout.struct([BufferLayout.u32('instruction'), voteInit()])
9409
+ },
9410
+ Authorize: {
9411
+ index: 1,
9412
+ layout: BufferLayout.struct([BufferLayout.u32('instruction'), publicKey('newAuthorized'), BufferLayout.u32('voteAuthorizationType')])
9413
+ },
9414
+ Withdraw: {
9415
+ index: 3,
9416
+ layout: BufferLayout.struct([BufferLayout.u32('instruction'), BufferLayout.ns64('lamports')])
9417
+ }
9418
+ });
9419
+ /**
9420
+ * VoteAuthorize type
9421
+ */
9422
+
9423
+ /**
9424
+ * An enumeration of valid VoteAuthorization layouts.
9425
+ */
9426
+ const VoteAuthorizationLayout = Object.freeze({
9427
+ Voter: {
9428
+ index: 0
9429
+ },
9430
+ Withdrawer: {
9431
+ index: 1
9432
+ }
9433
+ });
9434
+ /**
9435
+ * Factory class for transactions to interact with the Vote program
9436
+ */
9437
+
9438
+ class VoteProgram {
9439
+ /**
9440
+ * @internal
9441
+ */
9442
+ constructor() {}
9443
+ /**
9444
+ * Public key that identifies the Vote program
9445
+ */
9446
+
9447
+
9448
+ /**
9449
+ * Generate an Initialize instruction.
9450
+ */
9451
+ static initializeAccount(params) {
9452
+ const {
9453
+ votePubkey,
9454
+ nodePubkey,
9455
+ voteInit
9456
+ } = params;
9457
+ const type = VOTE_INSTRUCTION_LAYOUTS.InitializeAccount;
9458
+ const data = encodeData(type, {
9459
+ voteInit: {
9460
+ nodePubkey: toBuffer(voteInit.nodePubkey.toBuffer()),
9461
+ authorizedVoter: toBuffer(voteInit.authorizedVoter.toBuffer()),
9462
+ authorizedWithdrawer: toBuffer(voteInit.authorizedWithdrawer.toBuffer()),
9463
+ commission: voteInit.commission
9464
+ }
9465
+ });
9466
+ const instructionData = {
9467
+ keys: [{
9468
+ pubkey: votePubkey,
9469
+ isSigner: false,
9470
+ isWritable: true
9471
+ }, {
9472
+ pubkey: SYSVAR_RENT_PUBKEY,
9473
+ isSigner: false,
9474
+ isWritable: false
9475
+ }, {
9476
+ pubkey: SYSVAR_CLOCK_PUBKEY,
9477
+ isSigner: false,
9478
+ isWritable: false
9479
+ }, {
9480
+ pubkey: nodePubkey,
9481
+ isSigner: true,
9482
+ isWritable: false
9483
+ }],
9484
+ programId: this.programId,
9485
+ data
9486
+ };
9487
+ return new TransactionInstruction(instructionData);
9488
+ }
9489
+ /**
9490
+ * Generate a transaction that creates a new Vote account.
9491
+ */
9492
+
9493
+
9494
+ static createAccount(params) {
9495
+ const transaction = new Transaction();
9496
+ transaction.add(SystemProgram.createAccount({
9497
+ fromPubkey: params.fromPubkey,
9498
+ newAccountPubkey: params.votePubkey,
9499
+ lamports: params.lamports,
9500
+ space: this.space,
9501
+ programId: this.programId
9502
+ }));
9503
+ return transaction.add(this.initializeAccount({
9504
+ votePubkey: params.votePubkey,
9505
+ nodePubkey: params.voteInit.nodePubkey,
9506
+ voteInit: params.voteInit
9507
+ }));
9508
+ }
9509
+ /**
9510
+ * Generate a transaction that authorizes a new Voter or Withdrawer on the Vote account.
9511
+ */
9512
+
9513
+
9514
+ static authorize(params) {
9515
+ const {
9516
+ votePubkey,
9517
+ authorizedPubkey,
9518
+ newAuthorizedPubkey,
9519
+ voteAuthorizationType
9520
+ } = params;
9521
+ const type = VOTE_INSTRUCTION_LAYOUTS.Authorize;
9522
+ const data = encodeData(type, {
9523
+ newAuthorized: toBuffer(newAuthorizedPubkey.toBuffer()),
9524
+ voteAuthorizationType: voteAuthorizationType.index
9525
+ });
9526
+ const keys = [{
9527
+ pubkey: votePubkey,
9528
+ isSigner: false,
9529
+ isWritable: true
9530
+ }, {
9531
+ pubkey: SYSVAR_CLOCK_PUBKEY,
9532
+ isSigner: false,
9533
+ isWritable: false
9534
+ }, {
9535
+ pubkey: authorizedPubkey,
9536
+ isSigner: true,
9537
+ isWritable: false
9538
+ }];
9539
+ return new Transaction().add({
9540
+ keys,
9541
+ programId: this.programId,
9542
+ data
9543
+ });
9544
+ }
9545
+ /**
9546
+ * Generate a transaction to withdraw from a Vote account.
9547
+ */
9548
+
9549
+
9550
+ static withdraw(params) {
9551
+ const {
9552
+ votePubkey,
9553
+ authorizedWithdrawerPubkey,
9554
+ lamports,
9555
+ toPubkey
9556
+ } = params;
9557
+ const type = VOTE_INSTRUCTION_LAYOUTS.Withdraw;
9558
+ const data = encodeData(type, {
9559
+ lamports
9560
+ });
9561
+ const keys = [{
9562
+ pubkey: votePubkey,
9563
+ isSigner: false,
9564
+ isWritable: true
9565
+ }, {
9566
+ pubkey: toPubkey,
9567
+ isSigner: false,
9568
+ isWritable: true
9569
+ }, {
9570
+ pubkey: authorizedWithdrawerPubkey,
9571
+ isSigner: true,
9572
+ isWritable: false
9573
+ }];
9574
+ return new Transaction().add({
9575
+ keys,
9576
+ programId: this.programId,
9577
+ data
9578
+ });
9579
+ }
9580
+
9581
+ }
9582
+ VoteProgram.programId = new PublicKey('Vote111111111111111111111111111111111111111');
9583
+ VoteProgram.space = 3731;
9584
+
9217
9585
  /**
9218
9586
  * Send and confirm a raw transaction
9219
9587
  *
@@ -9277,5 +9645,5 @@ function clusterApiUrl(cluster, tls) {
9277
9645
 
9278
9646
  const LAMPORTS_PER_SOL = 1000000000;
9279
9647
 
9280
- export { Account, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, Loader, Lockup, MAX_SEED_LENGTH, Message, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PublicKey, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionInstruction, VALIDATOR_INFO_KEY, VOTE_PROGRAM_ID, ValidatorInfo, VoteAccount, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
9648
+ export { Account, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, Connection, Ed25519Program, Enum, EpochSchedule, FeeCalculatorLayout, Keypair, LAMPORTS_PER_SOL, Loader, Lockup, MAX_SEED_LENGTH, Message, NONCE_ACCOUNT_LENGTH, NonceAccount, PACKET_DATA_SIZE, PublicKey, SOLANA_SCHEMA, STAKE_CONFIG_ID, STAKE_INSTRUCTION_LAYOUTS, SYSTEM_INSTRUCTION_LAYOUTS, SYSVAR_CLOCK_PUBKEY, SYSVAR_EPOCH_SCHEDULE_PUBKEY, SYSVAR_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_SLOT_HASHES_PUBKEY, SYSVAR_SLOT_HISTORY_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, SendTransactionError, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionInstruction, VALIDATOR_INFO_KEY, VOTE_PROGRAM_ID, ValidatorInfo, VoteAccount, VoteAuthorizationLayout, VoteInit, VoteInstruction, VoteProgram, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
9281
9649
  //# sourceMappingURL=index.browser.esm.js.map