@solana/web3.js 1.17.1 → 1.18.1

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.
@@ -4284,6 +4284,107 @@ function parseHost(self) {
4284
4284
  if (host) self.hostname = host;
4285
4285
  }
4286
4286
 
4287
+ const MINIMUM_SLOT_PER_EPOCH = 32; // Returns the number of trailing zeros in the binary representation of self.
4288
+
4289
+ function trailingZeros(n) {
4290
+ let trailingZeros = 0;
4291
+
4292
+ while (n > 1) {
4293
+ n /= 2;
4294
+ trailingZeros++;
4295
+ }
4296
+
4297
+ return trailingZeros;
4298
+ } // Returns the smallest power of two greater than or equal to n
4299
+
4300
+
4301
+ function nextPowerOfTwo(n) {
4302
+ if (n === 0) return 1;
4303
+ n--;
4304
+ n |= n >> 1;
4305
+ n |= n >> 2;
4306
+ n |= n >> 4;
4307
+ n |= n >> 8;
4308
+ n |= n >> 16;
4309
+ n |= n >> 32;
4310
+ return n + 1;
4311
+ }
4312
+ /**
4313
+ * Epoch schedule
4314
+ * (see https://docs.solana.com/terminology#epoch)
4315
+ * Can be retrieved with the {@link connection.getEpochSchedule} method
4316
+ */
4317
+
4318
+
4319
+ class EpochSchedule {
4320
+ /** The maximum number of slots in each epoch */
4321
+
4322
+ /** The number of slots before beginning of an epoch to calculate a leader schedule for that epoch */
4323
+
4324
+ /** Indicates whether epochs start short and grow */
4325
+
4326
+ /** The first epoch with `slotsPerEpoch` slots */
4327
+
4328
+ /** The first slot of `firstNormalEpoch` */
4329
+ constructor(slotsPerEpoch, leaderScheduleSlotOffset, warmup, firstNormalEpoch, firstNormalSlot) {
4330
+ _defineProperty(this, "slotsPerEpoch", void 0);
4331
+
4332
+ _defineProperty(this, "leaderScheduleSlotOffset", void 0);
4333
+
4334
+ _defineProperty(this, "warmup", void 0);
4335
+
4336
+ _defineProperty(this, "firstNormalEpoch", void 0);
4337
+
4338
+ _defineProperty(this, "firstNormalSlot", void 0);
4339
+
4340
+ this.slotsPerEpoch = slotsPerEpoch;
4341
+ this.leaderScheduleSlotOffset = leaderScheduleSlotOffset;
4342
+ this.warmup = warmup;
4343
+ this.firstNormalEpoch = firstNormalEpoch;
4344
+ this.firstNormalSlot = firstNormalSlot;
4345
+ }
4346
+
4347
+ getEpoch(slot) {
4348
+ return this.getEpochAndSlotIndex(slot)[0];
4349
+ }
4350
+
4351
+ getEpochAndSlotIndex(slot) {
4352
+ if (slot < this.firstNormalSlot) {
4353
+ const epoch = trailingZeros(nextPowerOfTwo(slot + MINIMUM_SLOT_PER_EPOCH + 1)) - trailingZeros(MINIMUM_SLOT_PER_EPOCH) - 1;
4354
+ const epochLen = this.getSlotsInEpoch(epoch);
4355
+ const slotIndex = slot - (epochLen - MINIMUM_SLOT_PER_EPOCH);
4356
+ return [epoch, slotIndex];
4357
+ } else {
4358
+ const normalSlotIndex = slot - this.firstNormalSlot;
4359
+ const normalEpochIndex = Math.floor(normalSlotIndex / this.slotsPerEpoch);
4360
+ const epoch = this.firstNormalEpoch + normalEpochIndex;
4361
+ const slotIndex = normalSlotIndex % this.slotsPerEpoch;
4362
+ return [epoch, slotIndex];
4363
+ }
4364
+ }
4365
+
4366
+ getFirstSlotInEpoch(epoch) {
4367
+ if (epoch <= this.firstNormalEpoch) {
4368
+ return (Math.pow(2, epoch) - 1) * MINIMUM_SLOT_PER_EPOCH;
4369
+ } else {
4370
+ return (epoch - this.firstNormalEpoch) * this.slotsPerEpoch + this.firstNormalSlot;
4371
+ }
4372
+ }
4373
+
4374
+ getLastSlotInEpoch(epoch) {
4375
+ return this.getFirstSlotInEpoch(epoch) + this.getSlotsInEpoch(epoch) - 1;
4376
+ }
4377
+
4378
+ getSlotsInEpoch(epoch) {
4379
+ if (epoch < this.firstNormalEpoch) {
4380
+ return Math.pow(2, epoch + trailingZeros(MINIMUM_SLOT_PER_EPOCH));
4381
+ } else {
4382
+ return this.slotsPerEpoch;
4383
+ }
4384
+ }
4385
+
4386
+ }
4387
+
4287
4388
  // TODO: These constants should be removed in favor of reading them out of a
4288
4389
  // Syscall account
4289
4390
 
@@ -4449,11 +4550,6 @@ const GetEpochInfoResult = type({
4449
4550
  blockHeight: optional(number()),
4450
4551
  transactionCount: optional(number())
4451
4552
  });
4452
- /**
4453
- * Epoch schedule
4454
- * (see https://docs.solana.com/terminology#epoch)
4455
- */
4456
-
4457
4553
  const GetEpochScheduleResult = type({
4458
4554
  slotsPerEpoch: number(),
4459
4555
  leaderScheduleSlotOffset: number(),
@@ -5980,7 +6076,8 @@ class Connection {
5980
6076
  throw new Error('failed to get epoch schedule: ' + res.error.message);
5981
6077
  }
5982
6078
 
5983
- return res.result;
6079
+ const epochSchedule = res.result;
6080
+ return new EpochSchedule(epochSchedule.slotsPerEpoch, epochSchedule.leaderScheduleSlotOffset, epochSchedule.warmup, epochSchedule.firstNormalEpoch, epochSchedule.firstNormalSlot);
5984
6081
  }
5985
6082
  /**
5986
6083
  * Fetch the leader schedule for the current epoch
@@ -6396,12 +6493,23 @@ class Connection {
6396
6493
  });
6397
6494
  }
6398
6495
  /**
6399
- * Request an allocation of lamports to the specified account
6496
+ * Request an allocation of lamports to the specified address
6497
+ *
6498
+ * ```typescript
6499
+ * import { Connection, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
6500
+ *
6501
+ * (async () => {
6502
+ * const connection = new Connection("https://api.testnet.solana.com", "confirmed");
6503
+ * const myAddress = new PublicKey("2nr1bHFT86W9tGnyvmYW4vcHKsQB3sVQfnddasz4kExM");
6504
+ * const signature = await connection.requestAirdrop(myAddress, LAMPORTS_PER_SOL);
6505
+ * await connection.confirmTransaction(signature);
6506
+ * })();
6507
+ * ```
6400
6508
  */
6401
6509
 
6402
6510
 
6403
- async requestAirdrop(to, amount) {
6404
- const unsafeRes = await this._rpcRequest('requestAirdrop', [to.toBase58(), amount]);
6511
+ async requestAirdrop(to, lamports) {
6512
+ const unsafeRes = await this._rpcRequest('requestAirdrop', [to.toBase58(), lamports]);
6405
6513
  const res = create(unsafeRes, RequestAirdropRpcResult);
6406
6514
 
6407
6515
  if ('error' in res) {
@@ -8445,5 +8553,5 @@ function clusterApiUrl(cluster, tls) {
8445
8553
 
8446
8554
  const LAMPORTS_PER_SOL = 1000000000;
8447
8555
 
8448
- export { Account, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, Connection, Enum, 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_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionInstruction, VALIDATOR_INFO_KEY, VOTE_PROGRAM_ID, ValidatorInfo, VoteAccount, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
8556
+ export { Account, Authorized, BLOCKHASH_CACHE_TIMEOUT_MS, BPF_LOADER_DEPRECATED_PROGRAM_ID, BPF_LOADER_PROGRAM_ID, BpfLoader, Connection, 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_INSTRUCTIONS_PUBKEY, SYSVAR_RECENT_BLOCKHASHES_PUBKEY, SYSVAR_RENT_PUBKEY, SYSVAR_REWARDS_PUBKEY, SYSVAR_STAKE_HISTORY_PUBKEY, Secp256k1Program, StakeAuthorizationLayout, StakeInstruction, StakeProgram, Struct, SystemInstruction, SystemProgram, Transaction, TransactionInstruction, VALIDATOR_INFO_KEY, VOTE_PROGRAM_ID, ValidatorInfo, VoteAccount, clusterApiUrl, sendAndConfirmRawTransaction, sendAndConfirmTransaction };
8449
8557
  //# sourceMappingURL=index.browser.esm.js.map