@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.iife.js CHANGED
@@ -17960,6 +17960,107 @@ var solanaWeb3 = (function (exports) {
17960
17960
  callback(null, response);
17961
17961
  };
17962
17962
 
17963
+ const MINIMUM_SLOT_PER_EPOCH = 32; // Returns the number of trailing zeros in the binary representation of self.
17964
+
17965
+ function trailingZeros(n) {
17966
+ let trailingZeros = 0;
17967
+
17968
+ while (n > 1) {
17969
+ n /= 2;
17970
+ trailingZeros++;
17971
+ }
17972
+
17973
+ return trailingZeros;
17974
+ } // Returns the smallest power of two greater than or equal to n
17975
+
17976
+
17977
+ function nextPowerOfTwo(n) {
17978
+ if (n === 0) return 1;
17979
+ n--;
17980
+ n |= n >> 1;
17981
+ n |= n >> 2;
17982
+ n |= n >> 4;
17983
+ n |= n >> 8;
17984
+ n |= n >> 16;
17985
+ n |= n >> 32;
17986
+ return n + 1;
17987
+ }
17988
+ /**
17989
+ * Epoch schedule
17990
+ * (see https://docs.solana.com/terminology#epoch)
17991
+ * Can be retrieved with the {@link connection.getEpochSchedule} method
17992
+ */
17993
+
17994
+
17995
+ class EpochSchedule {
17996
+ /** The maximum number of slots in each epoch */
17997
+
17998
+ /** The number of slots before beginning of an epoch to calculate a leader schedule for that epoch */
17999
+
18000
+ /** Indicates whether epochs start short and grow */
18001
+
18002
+ /** The first epoch with `slotsPerEpoch` slots */
18003
+
18004
+ /** The first slot of `firstNormalEpoch` */
18005
+ constructor(slotsPerEpoch, leaderScheduleSlotOffset, warmup, firstNormalEpoch, firstNormalSlot) {
18006
+ _defineProperty(this, "slotsPerEpoch", void 0);
18007
+
18008
+ _defineProperty(this, "leaderScheduleSlotOffset", void 0);
18009
+
18010
+ _defineProperty(this, "warmup", void 0);
18011
+
18012
+ _defineProperty(this, "firstNormalEpoch", void 0);
18013
+
18014
+ _defineProperty(this, "firstNormalSlot", void 0);
18015
+
18016
+ this.slotsPerEpoch = slotsPerEpoch;
18017
+ this.leaderScheduleSlotOffset = leaderScheduleSlotOffset;
18018
+ this.warmup = warmup;
18019
+ this.firstNormalEpoch = firstNormalEpoch;
18020
+ this.firstNormalSlot = firstNormalSlot;
18021
+ }
18022
+
18023
+ getEpoch(slot) {
18024
+ return this.getEpochAndSlotIndex(slot)[0];
18025
+ }
18026
+
18027
+ getEpochAndSlotIndex(slot) {
18028
+ if (slot < this.firstNormalSlot) {
18029
+ const epoch = trailingZeros(nextPowerOfTwo(slot + MINIMUM_SLOT_PER_EPOCH + 1)) - trailingZeros(MINIMUM_SLOT_PER_EPOCH) - 1;
18030
+ const epochLen = this.getSlotsInEpoch(epoch);
18031
+ const slotIndex = slot - (epochLen - MINIMUM_SLOT_PER_EPOCH);
18032
+ return [epoch, slotIndex];
18033
+ } else {
18034
+ const normalSlotIndex = slot - this.firstNormalSlot;
18035
+ const normalEpochIndex = Math.floor(normalSlotIndex / this.slotsPerEpoch);
18036
+ const epoch = this.firstNormalEpoch + normalEpochIndex;
18037
+ const slotIndex = normalSlotIndex % this.slotsPerEpoch;
18038
+ return [epoch, slotIndex];
18039
+ }
18040
+ }
18041
+
18042
+ getFirstSlotInEpoch(epoch) {
18043
+ if (epoch <= this.firstNormalEpoch) {
18044
+ return (Math.pow(2, epoch) - 1) * MINIMUM_SLOT_PER_EPOCH;
18045
+ } else {
18046
+ return (epoch - this.firstNormalEpoch) * this.slotsPerEpoch + this.firstNormalSlot;
18047
+ }
18048
+ }
18049
+
18050
+ getLastSlotInEpoch(epoch) {
18051
+ return this.getFirstSlotInEpoch(epoch) + this.getSlotsInEpoch(epoch) - 1;
18052
+ }
18053
+
18054
+ getSlotsInEpoch(epoch) {
18055
+ if (epoch < this.firstNormalEpoch) {
18056
+ return Math.pow(2, epoch + trailingZeros(MINIMUM_SLOT_PER_EPOCH));
18057
+ } else {
18058
+ return this.slotsPerEpoch;
18059
+ }
18060
+ }
18061
+
18062
+ }
18063
+
17963
18064
  // TODO: These constants should be removed in favor of reading them out of a
17964
18065
  // Syscall account
17965
18066
 
@@ -18125,11 +18226,6 @@ var solanaWeb3 = (function (exports) {
18125
18226
  blockHeight: optional(number()),
18126
18227
  transactionCount: optional(number())
18127
18228
  });
18128
- /**
18129
- * Epoch schedule
18130
- * (see https://docs.solana.com/terminology#epoch)
18131
- */
18132
-
18133
18229
  const GetEpochScheduleResult = type({
18134
18230
  slotsPerEpoch: number(),
18135
18231
  leaderScheduleSlotOffset: number(),
@@ -19656,7 +19752,8 @@ var solanaWeb3 = (function (exports) {
19656
19752
  throw new Error('failed to get epoch schedule: ' + res.error.message);
19657
19753
  }
19658
19754
 
19659
- return res.result;
19755
+ const epochSchedule = res.result;
19756
+ return new EpochSchedule(epochSchedule.slotsPerEpoch, epochSchedule.leaderScheduleSlotOffset, epochSchedule.warmup, epochSchedule.firstNormalEpoch, epochSchedule.firstNormalSlot);
19660
19757
  }
19661
19758
  /**
19662
19759
  * Fetch the leader schedule for the current epoch
@@ -20072,12 +20169,23 @@ var solanaWeb3 = (function (exports) {
20072
20169
  });
20073
20170
  }
20074
20171
  /**
20075
- * Request an allocation of lamports to the specified account
20172
+ * Request an allocation of lamports to the specified address
20173
+ *
20174
+ * ```typescript
20175
+ * import { Connection, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
20176
+ *
20177
+ * (async () => {
20178
+ * const connection = new Connection("https://api.testnet.solana.com", "confirmed");
20179
+ * const myAddress = new PublicKey("2nr1bHFT86W9tGnyvmYW4vcHKsQB3sVQfnddasz4kExM");
20180
+ * const signature = await connection.requestAirdrop(myAddress, LAMPORTS_PER_SOL);
20181
+ * await connection.confirmTransaction(signature);
20182
+ * })();
20183
+ * ```
20076
20184
  */
20077
20185
 
20078
20186
 
20079
- async requestAirdrop(to, amount) {
20080
- const unsafeRes = await this._rpcRequest('requestAirdrop', [to.toBase58(), amount]);
20187
+ async requestAirdrop(to, lamports) {
20188
+ const unsafeRes = await this._rpcRequest('requestAirdrop', [to.toBase58(), lamports]);
20081
20189
  const res = create(unsafeRes, RequestAirdropRpcResult);
20082
20190
 
20083
20191
  if ('error' in res) {
@@ -28980,6 +29088,7 @@ var solanaWeb3 = (function (exports) {
28980
29088
  exports.BpfLoader = BpfLoader;
28981
29089
  exports.Connection = Connection;
28982
29090
  exports.Enum = Enum;
29091
+ exports.EpochSchedule = EpochSchedule;
28983
29092
  exports.FeeCalculatorLayout = FeeCalculatorLayout;
28984
29093
  exports.Keypair = Keypair;
28985
29094
  exports.LAMPORTS_PER_SOL = LAMPORTS_PER_SOL;