@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.
package/lib/index.cjs.js CHANGED
@@ -2110,6 +2110,13 @@ const authorized = (property = 'authorized') => {
2110
2110
  const lockup = (property = 'lockup') => {
2111
2111
  return BufferLayout__namespace.struct([BufferLayout__namespace.ns64('unixTimestamp'), BufferLayout__namespace.ns64('epoch'), publicKey('custodian')], property);
2112
2112
  };
2113
+ /**
2114
+ * Layout for a VoteInit object
2115
+ */
2116
+
2117
+ const voteInit = (property = 'voteInit') => {
2118
+ return BufferLayout__namespace.struct([publicKey('nodePubkey'), publicKey('authorizedVoter'), publicKey('authorizedWithdrawer'), BufferLayout__namespace.u8('commission')], property);
2119
+ };
2113
2120
  function getAlloc(type, fields) {
2114
2121
  let alloc = 0;
2115
2122
  type.layout.fields.forEach(item => {
@@ -6939,7 +6946,14 @@ class Connection {
6939
6946
  this._rpcWebSocketIdleTimeout = setTimeout(() => {
6940
6947
  this._rpcWebSocketIdleTimeout = null;
6941
6948
 
6942
- this._rpcWebSocket.close();
6949
+ try {
6950
+ this._rpcWebSocket.close();
6951
+ } catch (err) {
6952
+ // swallow error if socket has already been closed.
6953
+ if (err instanceof Error) {
6954
+ console.log(`Error when closing socket connection: ${err.message}`);
6955
+ }
6956
+ }
6943
6957
  }, 500);
6944
6958
  }
6945
6959
 
@@ -8251,30 +8265,22 @@ class StakeProgram {
8251
8265
  });
8252
8266
  }
8253
8267
  /**
8254
- * Generate a Transaction that splits Stake tokens into another stake account
8268
+ * @internal
8255
8269
  */
8256
8270
 
8257
8271
 
8258
- static split(params) {
8272
+ static splitInstruction(params) {
8259
8273
  const {
8260
8274
  stakePubkey,
8261
8275
  authorizedPubkey,
8262
8276
  splitStakePubkey,
8263
8277
  lamports
8264
8278
  } = params;
8265
- const transaction = new Transaction();
8266
- transaction.add(SystemProgram.createAccount({
8267
- fromPubkey: authorizedPubkey,
8268
- newAccountPubkey: splitStakePubkey,
8269
- lamports: 0,
8270
- space: this.space,
8271
- programId: this.programId
8272
- }));
8273
8279
  const type = STAKE_INSTRUCTION_LAYOUTS.Split;
8274
8280
  const data = encodeData(type, {
8275
8281
  lamports
8276
8282
  });
8277
- return transaction.add({
8283
+ return new TransactionInstruction({
8278
8284
  keys: [{
8279
8285
  pubkey: stakePubkey,
8280
8286
  isSigner: false,
@@ -8292,6 +8298,52 @@ class StakeProgram {
8292
8298
  data
8293
8299
  });
8294
8300
  }
8301
+ /**
8302
+ * Generate a Transaction that splits Stake tokens into another stake account
8303
+ */
8304
+
8305
+
8306
+ static split(params) {
8307
+ const transaction = new Transaction();
8308
+ transaction.add(SystemProgram.createAccount({
8309
+ fromPubkey: params.authorizedPubkey,
8310
+ newAccountPubkey: params.splitStakePubkey,
8311
+ lamports: 0,
8312
+ space: this.space,
8313
+ programId: this.programId
8314
+ }));
8315
+ return transaction.add(this.splitInstruction(params));
8316
+ }
8317
+ /**
8318
+ * Generate a Transaction that splits Stake tokens into another account
8319
+ * derived from a base public key and seed
8320
+ */
8321
+
8322
+
8323
+ static splitWithSeed(params) {
8324
+ const {
8325
+ stakePubkey,
8326
+ authorizedPubkey,
8327
+ splitStakePubkey,
8328
+ basePubkey,
8329
+ seed,
8330
+ lamports
8331
+ } = params;
8332
+ const transaction = new Transaction();
8333
+ transaction.add(SystemProgram.allocate({
8334
+ accountPubkey: splitStakePubkey,
8335
+ basePubkey,
8336
+ seed,
8337
+ space: this.space,
8338
+ programId: this.programId
8339
+ }));
8340
+ return transaction.add(this.splitInstruction({
8341
+ stakePubkey,
8342
+ authorizedPubkey,
8343
+ splitStakePubkey,
8344
+ lamports
8345
+ }));
8346
+ }
8295
8347
  /**
8296
8348
  * Generate a Transaction that merges Stake accounts.
8297
8349
  */
@@ -8750,6 +8802,322 @@ function getPriorVoters({
8750
8802
  return [...buf.slice(idx + 1).map(parsePriorVoters), ...buf.slice(0, idx)];
8751
8803
  }
8752
8804
 
8805
+ /**
8806
+ * Vote account info
8807
+ */
8808
+
8809
+ class VoteInit {
8810
+ /** [0, 100] */
8811
+ constructor(nodePubkey, authorizedVoter, authorizedWithdrawer, commission) {
8812
+ this.nodePubkey = void 0;
8813
+ this.authorizedVoter = void 0;
8814
+ this.authorizedWithdrawer = void 0;
8815
+ this.commission = void 0;
8816
+ this.nodePubkey = nodePubkey;
8817
+ this.authorizedVoter = authorizedVoter;
8818
+ this.authorizedWithdrawer = authorizedWithdrawer;
8819
+ this.commission = commission;
8820
+ }
8821
+
8822
+ }
8823
+ /**
8824
+ * Create vote account transaction params
8825
+ */
8826
+
8827
+ /**
8828
+ * Vote Instruction class
8829
+ */
8830
+ class VoteInstruction {
8831
+ /**
8832
+ * @internal
8833
+ */
8834
+ constructor() {}
8835
+ /**
8836
+ * Decode a vote instruction and retrieve the instruction type.
8837
+ */
8838
+
8839
+
8840
+ static decodeInstructionType(instruction) {
8841
+ this.checkProgramId(instruction.programId);
8842
+ const instructionTypeLayout = BufferLayout__namespace.u32('instruction');
8843
+ const typeIndex = instructionTypeLayout.decode(instruction.data);
8844
+ let type;
8845
+
8846
+ for (const [ixType, layout] of Object.entries(VOTE_INSTRUCTION_LAYOUTS)) {
8847
+ if (layout.index == typeIndex) {
8848
+ type = ixType;
8849
+ break;
8850
+ }
8851
+ }
8852
+
8853
+ if (!type) {
8854
+ throw new Error('Instruction type incorrect; not a VoteInstruction');
8855
+ }
8856
+
8857
+ return type;
8858
+ }
8859
+ /**
8860
+ * Decode an initialize vote instruction and retrieve the instruction params.
8861
+ */
8862
+
8863
+
8864
+ static decodeInitializeAccount(instruction) {
8865
+ this.checkProgramId(instruction.programId);
8866
+ this.checkKeyLength(instruction.keys, 4);
8867
+ const {
8868
+ voteInit
8869
+ } = decodeData(VOTE_INSTRUCTION_LAYOUTS.InitializeAccount, instruction.data);
8870
+ return {
8871
+ votePubkey: instruction.keys[0].pubkey,
8872
+ nodePubkey: instruction.keys[3].pubkey,
8873
+ voteInit: new VoteInit(new PublicKey(voteInit.nodePubkey), new PublicKey(voteInit.authorizedVoter), new PublicKey(voteInit.authorizedWithdrawer), voteInit.commission)
8874
+ };
8875
+ }
8876
+ /**
8877
+ * Decode an authorize instruction and retrieve the instruction params.
8878
+ */
8879
+
8880
+
8881
+ static decodeAuthorize(instruction) {
8882
+ this.checkProgramId(instruction.programId);
8883
+ this.checkKeyLength(instruction.keys, 3);
8884
+ const {
8885
+ newAuthorized,
8886
+ voteAuthorizationType
8887
+ } = decodeData(VOTE_INSTRUCTION_LAYOUTS.Authorize, instruction.data);
8888
+ return {
8889
+ votePubkey: instruction.keys[0].pubkey,
8890
+ authorizedPubkey: instruction.keys[2].pubkey,
8891
+ newAuthorizedPubkey: new PublicKey(newAuthorized),
8892
+ voteAuthorizationType: {
8893
+ index: voteAuthorizationType
8894
+ }
8895
+ };
8896
+ }
8897
+ /**
8898
+ * Decode a withdraw instruction and retrieve the instruction params.
8899
+ */
8900
+
8901
+
8902
+ static decodeWithdraw(instruction) {
8903
+ this.checkProgramId(instruction.programId);
8904
+ this.checkKeyLength(instruction.keys, 3);
8905
+ const {
8906
+ lamports
8907
+ } = decodeData(VOTE_INSTRUCTION_LAYOUTS.Withdraw, instruction.data);
8908
+ return {
8909
+ votePubkey: instruction.keys[0].pubkey,
8910
+ authorizedWithdrawerPubkey: instruction.keys[2].pubkey,
8911
+ lamports,
8912
+ toPubkey: instruction.keys[1].pubkey
8913
+ };
8914
+ }
8915
+ /**
8916
+ * @internal
8917
+ */
8918
+
8919
+
8920
+ static checkProgramId(programId) {
8921
+ if (!programId.equals(VoteProgram.programId)) {
8922
+ throw new Error('invalid instruction; programId is not VoteProgram');
8923
+ }
8924
+ }
8925
+ /**
8926
+ * @internal
8927
+ */
8928
+
8929
+
8930
+ static checkKeyLength(keys, expectedLength) {
8931
+ if (keys.length < expectedLength) {
8932
+ throw new Error(`invalid instruction; found ${keys.length} keys, expected at least ${expectedLength}`);
8933
+ }
8934
+ }
8935
+
8936
+ }
8937
+ /**
8938
+ * An enumeration of valid VoteInstructionType's
8939
+ */
8940
+
8941
+ const VOTE_INSTRUCTION_LAYOUTS = Object.freeze({
8942
+ InitializeAccount: {
8943
+ index: 0,
8944
+ layout: BufferLayout__namespace.struct([BufferLayout__namespace.u32('instruction'), voteInit()])
8945
+ },
8946
+ Authorize: {
8947
+ index: 1,
8948
+ layout: BufferLayout__namespace.struct([BufferLayout__namespace.u32('instruction'), publicKey('newAuthorized'), BufferLayout__namespace.u32('voteAuthorizationType')])
8949
+ },
8950
+ Withdraw: {
8951
+ index: 3,
8952
+ layout: BufferLayout__namespace.struct([BufferLayout__namespace.u32('instruction'), BufferLayout__namespace.ns64('lamports')])
8953
+ }
8954
+ });
8955
+ /**
8956
+ * VoteAuthorize type
8957
+ */
8958
+
8959
+ /**
8960
+ * An enumeration of valid VoteAuthorization layouts.
8961
+ */
8962
+ const VoteAuthorizationLayout = Object.freeze({
8963
+ Voter: {
8964
+ index: 0
8965
+ },
8966
+ Withdrawer: {
8967
+ index: 1
8968
+ }
8969
+ });
8970
+ /**
8971
+ * Factory class for transactions to interact with the Vote program
8972
+ */
8973
+
8974
+ class VoteProgram {
8975
+ /**
8976
+ * @internal
8977
+ */
8978
+ constructor() {}
8979
+ /**
8980
+ * Public key that identifies the Vote program
8981
+ */
8982
+
8983
+
8984
+ /**
8985
+ * Generate an Initialize instruction.
8986
+ */
8987
+ static initializeAccount(params) {
8988
+ const {
8989
+ votePubkey,
8990
+ nodePubkey,
8991
+ voteInit
8992
+ } = params;
8993
+ const type = VOTE_INSTRUCTION_LAYOUTS.InitializeAccount;
8994
+ const data = encodeData(type, {
8995
+ voteInit: {
8996
+ nodePubkey: toBuffer(voteInit.nodePubkey.toBuffer()),
8997
+ authorizedVoter: toBuffer(voteInit.authorizedVoter.toBuffer()),
8998
+ authorizedWithdrawer: toBuffer(voteInit.authorizedWithdrawer.toBuffer()),
8999
+ commission: voteInit.commission
9000
+ }
9001
+ });
9002
+ const instructionData = {
9003
+ keys: [{
9004
+ pubkey: votePubkey,
9005
+ isSigner: false,
9006
+ isWritable: true
9007
+ }, {
9008
+ pubkey: SYSVAR_RENT_PUBKEY,
9009
+ isSigner: false,
9010
+ isWritable: false
9011
+ }, {
9012
+ pubkey: SYSVAR_CLOCK_PUBKEY,
9013
+ isSigner: false,
9014
+ isWritable: false
9015
+ }, {
9016
+ pubkey: nodePubkey,
9017
+ isSigner: true,
9018
+ isWritable: false
9019
+ }],
9020
+ programId: this.programId,
9021
+ data
9022
+ };
9023
+ return new TransactionInstruction(instructionData);
9024
+ }
9025
+ /**
9026
+ * Generate a transaction that creates a new Vote account.
9027
+ */
9028
+
9029
+
9030
+ static createAccount(params) {
9031
+ const transaction = new Transaction();
9032
+ transaction.add(SystemProgram.createAccount({
9033
+ fromPubkey: params.fromPubkey,
9034
+ newAccountPubkey: params.votePubkey,
9035
+ lamports: params.lamports,
9036
+ space: this.space,
9037
+ programId: this.programId
9038
+ }));
9039
+ return transaction.add(this.initializeAccount({
9040
+ votePubkey: params.votePubkey,
9041
+ nodePubkey: params.voteInit.nodePubkey,
9042
+ voteInit: params.voteInit
9043
+ }));
9044
+ }
9045
+ /**
9046
+ * Generate a transaction that authorizes a new Voter or Withdrawer on the Vote account.
9047
+ */
9048
+
9049
+
9050
+ static authorize(params) {
9051
+ const {
9052
+ votePubkey,
9053
+ authorizedPubkey,
9054
+ newAuthorizedPubkey,
9055
+ voteAuthorizationType
9056
+ } = params;
9057
+ const type = VOTE_INSTRUCTION_LAYOUTS.Authorize;
9058
+ const data = encodeData(type, {
9059
+ newAuthorized: toBuffer(newAuthorizedPubkey.toBuffer()),
9060
+ voteAuthorizationType: voteAuthorizationType.index
9061
+ });
9062
+ const keys = [{
9063
+ pubkey: votePubkey,
9064
+ isSigner: false,
9065
+ isWritable: true
9066
+ }, {
9067
+ pubkey: SYSVAR_CLOCK_PUBKEY,
9068
+ isSigner: false,
9069
+ isWritable: false
9070
+ }, {
9071
+ pubkey: authorizedPubkey,
9072
+ isSigner: true,
9073
+ isWritable: false
9074
+ }];
9075
+ return new Transaction().add({
9076
+ keys,
9077
+ programId: this.programId,
9078
+ data
9079
+ });
9080
+ }
9081
+ /**
9082
+ * Generate a transaction to withdraw from a Vote account.
9083
+ */
9084
+
9085
+
9086
+ static withdraw(params) {
9087
+ const {
9088
+ votePubkey,
9089
+ authorizedWithdrawerPubkey,
9090
+ lamports,
9091
+ toPubkey
9092
+ } = params;
9093
+ const type = VOTE_INSTRUCTION_LAYOUTS.Withdraw;
9094
+ const data = encodeData(type, {
9095
+ lamports
9096
+ });
9097
+ const keys = [{
9098
+ pubkey: votePubkey,
9099
+ isSigner: false,
9100
+ isWritable: true
9101
+ }, {
9102
+ pubkey: toPubkey,
9103
+ isSigner: false,
9104
+ isWritable: true
9105
+ }, {
9106
+ pubkey: authorizedWithdrawerPubkey,
9107
+ isSigner: true,
9108
+ isWritable: false
9109
+ }];
9110
+ return new Transaction().add({
9111
+ keys,
9112
+ programId: this.programId,
9113
+ data
9114
+ });
9115
+ }
9116
+
9117
+ }
9118
+ VoteProgram.programId = new PublicKey('Vote111111111111111111111111111111111111111');
9119
+ VoteProgram.space = 3731;
9120
+
8753
9121
  /**
8754
9122
  * Send and confirm a raw transaction
8755
9123
  *
@@ -8861,6 +9229,10 @@ exports.VALIDATOR_INFO_KEY = VALIDATOR_INFO_KEY;
8861
9229
  exports.VOTE_PROGRAM_ID = VOTE_PROGRAM_ID;
8862
9230
  exports.ValidatorInfo = ValidatorInfo;
8863
9231
  exports.VoteAccount = VoteAccount;
9232
+ exports.VoteAuthorizationLayout = VoteAuthorizationLayout;
9233
+ exports.VoteInit = VoteInit;
9234
+ exports.VoteInstruction = VoteInstruction;
9235
+ exports.VoteProgram = VoteProgram;
8864
9236
  exports.clusterApiUrl = clusterApiUrl;
8865
9237
  exports.sendAndConfirmRawTransaction = sendAndConfirmRawTransaction;
8866
9238
  exports.sendAndConfirmTransaction = sendAndConfirmTransaction;