@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.
package/lib/index.cjs.js CHANGED
@@ -2380,6 +2380,107 @@ class AgentManager {
2380
2380
 
2381
2381
  }
2382
2382
 
2383
+ const MINIMUM_SLOT_PER_EPOCH = 32; // Returns the number of trailing zeros in the binary representation of self.
2384
+
2385
+ function trailingZeros(n) {
2386
+ let trailingZeros = 0;
2387
+
2388
+ while (n > 1) {
2389
+ n /= 2;
2390
+ trailingZeros++;
2391
+ }
2392
+
2393
+ return trailingZeros;
2394
+ } // Returns the smallest power of two greater than or equal to n
2395
+
2396
+
2397
+ function nextPowerOfTwo(n) {
2398
+ if (n === 0) return 1;
2399
+ n--;
2400
+ n |= n >> 1;
2401
+ n |= n >> 2;
2402
+ n |= n >> 4;
2403
+ n |= n >> 8;
2404
+ n |= n >> 16;
2405
+ n |= n >> 32;
2406
+ return n + 1;
2407
+ }
2408
+ /**
2409
+ * Epoch schedule
2410
+ * (see https://docs.solana.com/terminology#epoch)
2411
+ * Can be retrieved with the {@link connection.getEpochSchedule} method
2412
+ */
2413
+
2414
+
2415
+ class EpochSchedule {
2416
+ /** The maximum number of slots in each epoch */
2417
+
2418
+ /** The number of slots before beginning of an epoch to calculate a leader schedule for that epoch */
2419
+
2420
+ /** Indicates whether epochs start short and grow */
2421
+
2422
+ /** The first epoch with `slotsPerEpoch` slots */
2423
+
2424
+ /** The first slot of `firstNormalEpoch` */
2425
+ constructor(slotsPerEpoch, leaderScheduleSlotOffset, warmup, firstNormalEpoch, firstNormalSlot) {
2426
+ _defineProperty__default['default'](this, "slotsPerEpoch", void 0);
2427
+
2428
+ _defineProperty__default['default'](this, "leaderScheduleSlotOffset", void 0);
2429
+
2430
+ _defineProperty__default['default'](this, "warmup", void 0);
2431
+
2432
+ _defineProperty__default['default'](this, "firstNormalEpoch", void 0);
2433
+
2434
+ _defineProperty__default['default'](this, "firstNormalSlot", void 0);
2435
+
2436
+ this.slotsPerEpoch = slotsPerEpoch;
2437
+ this.leaderScheduleSlotOffset = leaderScheduleSlotOffset;
2438
+ this.warmup = warmup;
2439
+ this.firstNormalEpoch = firstNormalEpoch;
2440
+ this.firstNormalSlot = firstNormalSlot;
2441
+ }
2442
+
2443
+ getEpoch(slot) {
2444
+ return this.getEpochAndSlotIndex(slot)[0];
2445
+ }
2446
+
2447
+ getEpochAndSlotIndex(slot) {
2448
+ if (slot < this.firstNormalSlot) {
2449
+ const epoch = trailingZeros(nextPowerOfTwo(slot + MINIMUM_SLOT_PER_EPOCH + 1)) - trailingZeros(MINIMUM_SLOT_PER_EPOCH) - 1;
2450
+ const epochLen = this.getSlotsInEpoch(epoch);
2451
+ const slotIndex = slot - (epochLen - MINIMUM_SLOT_PER_EPOCH);
2452
+ return [epoch, slotIndex];
2453
+ } else {
2454
+ const normalSlotIndex = slot - this.firstNormalSlot;
2455
+ const normalEpochIndex = Math.floor(normalSlotIndex / this.slotsPerEpoch);
2456
+ const epoch = this.firstNormalEpoch + normalEpochIndex;
2457
+ const slotIndex = normalSlotIndex % this.slotsPerEpoch;
2458
+ return [epoch, slotIndex];
2459
+ }
2460
+ }
2461
+
2462
+ getFirstSlotInEpoch(epoch) {
2463
+ if (epoch <= this.firstNormalEpoch) {
2464
+ return (Math.pow(2, epoch) - 1) * MINIMUM_SLOT_PER_EPOCH;
2465
+ } else {
2466
+ return (epoch - this.firstNormalEpoch) * this.slotsPerEpoch + this.firstNormalSlot;
2467
+ }
2468
+ }
2469
+
2470
+ getLastSlotInEpoch(epoch) {
2471
+ return this.getFirstSlotInEpoch(epoch) + this.getSlotsInEpoch(epoch) - 1;
2472
+ }
2473
+
2474
+ getSlotsInEpoch(epoch) {
2475
+ if (epoch < this.firstNormalEpoch) {
2476
+ return Math.pow(2, epoch + trailingZeros(MINIMUM_SLOT_PER_EPOCH));
2477
+ } else {
2478
+ return this.slotsPerEpoch;
2479
+ }
2480
+ }
2481
+
2482
+ }
2483
+
2383
2484
  // TODO: These constants should be removed in favor of reading them out of a
2384
2485
  // Syscall account
2385
2486
 
@@ -2545,11 +2646,6 @@ const GetEpochInfoResult = superstruct.type({
2545
2646
  blockHeight: superstruct.optional(superstruct.number()),
2546
2647
  transactionCount: superstruct.optional(superstruct.number())
2547
2648
  });
2548
- /**
2549
- * Epoch schedule
2550
- * (see https://docs.solana.com/terminology#epoch)
2551
- */
2552
-
2553
2649
  const GetEpochScheduleResult = superstruct.type({
2554
2650
  slotsPerEpoch: superstruct.number(),
2555
2651
  leaderScheduleSlotOffset: superstruct.number(),
@@ -4082,7 +4178,8 @@ class Connection {
4082
4178
  throw new Error('failed to get epoch schedule: ' + res.error.message);
4083
4179
  }
4084
4180
 
4085
- return res.result;
4181
+ const epochSchedule = res.result;
4182
+ return new EpochSchedule(epochSchedule.slotsPerEpoch, epochSchedule.leaderScheduleSlotOffset, epochSchedule.warmup, epochSchedule.firstNormalEpoch, epochSchedule.firstNormalSlot);
4086
4183
  }
4087
4184
  /**
4088
4185
  * Fetch the leader schedule for the current epoch
@@ -4498,12 +4595,23 @@ class Connection {
4498
4595
  });
4499
4596
  }
4500
4597
  /**
4501
- * Request an allocation of lamports to the specified account
4598
+ * Request an allocation of lamports to the specified address
4599
+ *
4600
+ * ```typescript
4601
+ * import { Connection, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
4602
+ *
4603
+ * (async () => {
4604
+ * const connection = new Connection("https://api.testnet.solana.com", "confirmed");
4605
+ * const myAddress = new PublicKey("2nr1bHFT86W9tGnyvmYW4vcHKsQB3sVQfnddasz4kExM");
4606
+ * const signature = await connection.requestAirdrop(myAddress, LAMPORTS_PER_SOL);
4607
+ * await connection.confirmTransaction(signature);
4608
+ * })();
4609
+ * ```
4502
4610
  */
4503
4611
 
4504
4612
 
4505
- async requestAirdrop(to, amount) {
4506
- const unsafeRes = await this._rpcRequest('requestAirdrop', [to.toBase58(), amount]);
4613
+ async requestAirdrop(to, lamports) {
4614
+ const unsafeRes = await this._rpcRequest('requestAirdrop', [to.toBase58(), lamports]);
4507
4615
  const res = superstruct.create(unsafeRes, RequestAirdropRpcResult);
4508
4616
 
4509
4617
  if ('error' in res) {
@@ -6555,6 +6663,7 @@ exports.BPF_LOADER_PROGRAM_ID = BPF_LOADER_PROGRAM_ID;
6555
6663
  exports.BpfLoader = BpfLoader;
6556
6664
  exports.Connection = Connection;
6557
6665
  exports.Enum = Enum;
6666
+ exports.EpochSchedule = EpochSchedule;
6558
6667
  exports.FeeCalculatorLayout = FeeCalculatorLayout;
6559
6668
  exports.Keypair = Keypair;
6560
6669
  exports.LAMPORTS_PER_SOL = LAMPORTS_PER_SOL;