@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/module.flow.js CHANGED
@@ -136,6 +136,50 @@ declare module "@solana/web3.js" {
136
136
  declare export type Blockhash = string;
137
137
  declare export var BPF_LOADER_DEPRECATED_PROGRAM_ID: PublicKey;
138
138
 
139
+ /**
140
+ * Epoch schedule
141
+ * (see https://docs.solana.com/terminology#epoch)
142
+ * Can be retrieved with the {@link connection.getEpochSchedule} method
143
+ */
144
+ declare export class EpochSchedule {
145
+ /**
146
+ * The maximum number of slots in each epoch
147
+ */
148
+ slotsPerEpoch: number;
149
+
150
+ /**
151
+ * The number of slots before beginning of an epoch to calculate a leader schedule for that epoch
152
+ */
153
+ leaderScheduleSlotOffset: number;
154
+
155
+ /**
156
+ * Indicates whether epochs start short and grow
157
+ */
158
+ warmup: boolean;
159
+
160
+ /**
161
+ * The first epoch with `slotsPerEpoch` slots
162
+ */
163
+ firstNormalEpoch: number;
164
+
165
+ /**
166
+ * The first slot of `firstNormalEpoch`
167
+ */
168
+ firstNormalSlot: number;
169
+ constructor(
170
+ slotsPerEpoch: number,
171
+ leaderScheduleSlotOffset: number,
172
+ warmup: boolean,
173
+ firstNormalEpoch: number,
174
+ firstNormalSlot: number
175
+ ): this;
176
+ getEpoch(slot: number): number;
177
+ getEpochAndSlotIndex(slot: number): [number, number];
178
+ getFirstSlotInEpoch(epoch: number): number;
179
+ getLastSlotInEpoch(epoch: number): number;
180
+ getSlotsInEpoch(epoch: number): number;
181
+ }
182
+
139
183
  /**
140
184
  * Calculator for transaction fees.
141
185
  */
@@ -875,38 +919,6 @@ declare module "@solana/web3.js" {
875
919
  ...
876
920
  };
877
921
 
878
- /**
879
- * Epoch schedule
880
- * (see https://docs.solana.com/terminology#epoch)
881
- */
882
- declare export type EpochSchedule = {
883
- /**
884
- * The maximum number of slots in each epoch
885
- */
886
- slotsPerEpoch: number,
887
-
888
- /**
889
- * The number of slots before beginning of an epoch to calculate a leader schedule for that epoch
890
- */
891
- leaderScheduleSlotOffset: number,
892
-
893
- /**
894
- * Indicates whether epochs start short and grow
895
- */
896
- warmup: boolean,
897
-
898
- /**
899
- * The first epoch with `slotsPerEpoch` slots
900
- */
901
- firstNormalEpoch: number,
902
-
903
- /**
904
- * The first slot of `firstNormalEpoch`
905
- */
906
- firstNormalSlot: number,
907
- ...
908
- };
909
-
910
922
  /**
911
923
  * Leader schedule
912
924
  * (see https://docs.solana.com/terminology#leader-schedule)
@@ -2440,11 +2452,22 @@ feeCalculator: FeeCalculator,...
2440
2452
  ): Promise<NonceAccount | null>;
2441
2453
 
2442
2454
  /**
2443
- * Request an allocation of lamports to the specified account
2455
+ * Request an allocation of lamports to the specified address
2456
+ *
2457
+ * ```typescript
2458
+ * import { Connection, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
2459
+ *
2460
+ * (async () => {
2461
+ * const connection = new Connection("https://api.testnet.solana.com", "confirmed");
2462
+ * const myAddress = new PublicKey("2nr1bHFT86W9tGnyvmYW4vcHKsQB3sVQfnddasz4kExM");
2463
+ * const signature = await connection.requestAirdrop(myAddress, LAMPORTS_PER_SOL);
2464
+ * await connection.confirmTransaction(signature);
2465
+ * })();
2466
+ * ```
2444
2467
  */
2445
2468
  requestAirdrop(
2446
2469
  to: PublicKey,
2447
- amount: number
2470
+ lamports: number
2448
2471
  ): Promise<TransactionSignature>;
2449
2472
 
2450
2473
  /**
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@solana/web3.js",
3
- "version": "1.17.1",
3
+ "version": "1.18.1",
4
4
  "description": "Solana Javascript API",
5
5
  "keywords": [
6
6
  "api",
@@ -102,7 +102,7 @@
102
102
  "nyc": "^15.1.0",
103
103
  "prettier": "^2.3.0",
104
104
  "rimraf": "3.0.2",
105
- "rollup": "2.50.6",
105
+ "rollup": "2.51.0",
106
106
  "rollup-plugin-dts": "^3.0.1",
107
107
  "rollup-plugin-node-polyfills": "^0.2.1",
108
108
  "rollup-plugin-terser": "^7.0.2",
package/src/connection.ts CHANGED
@@ -27,6 +27,7 @@ import RpcClient from 'jayson/lib/client/browser';
27
27
  import {IWSRequestParams} from 'rpc-websockets/dist/lib/client';
28
28
 
29
29
  import {AgentManager} from './agent-manager';
30
+ import {EpochSchedule} from './epoch-schedule';
30
31
  import {NonceAccount} from './nonce-account';
31
32
  import {PublicKey} from './publickey';
32
33
  import {Signer} from './keypair';
@@ -373,23 +374,6 @@ const GetEpochInfoResult = pick({
373
374
  transactionCount: optional(number()),
374
375
  });
375
376
 
376
- /**
377
- * Epoch schedule
378
- * (see https://docs.solana.com/terminology#epoch)
379
- */
380
- export type EpochSchedule = {
381
- /** The maximum number of slots in each epoch */
382
- slotsPerEpoch: number;
383
- /** The number of slots before beginning of an epoch to calculate a leader schedule for that epoch */
384
- leaderScheduleSlotOffset: number;
385
- /** Indicates whether epochs start short and grow */
386
- warmup: boolean;
387
- /** The first epoch with `slotsPerEpoch` slots */
388
- firstNormalEpoch: number;
389
- /** The first slot of `firstNormalEpoch` */
390
- firstNormalSlot: number;
391
- };
392
-
393
377
  const GetEpochScheduleResult = pick({
394
378
  slotsPerEpoch: number(),
395
379
  leaderScheduleSlotOffset: number(),
@@ -2788,7 +2772,14 @@ export class Connection {
2788
2772
  if ('error' in res) {
2789
2773
  throw new Error('failed to get epoch schedule: ' + res.error.message);
2790
2774
  }
2791
- return res.result;
2775
+ const epochSchedule = res.result;
2776
+ return new EpochSchedule(
2777
+ epochSchedule.slotsPerEpoch,
2778
+ epochSchedule.leaderScheduleSlotOffset,
2779
+ epochSchedule.warmup,
2780
+ epochSchedule.firstNormalEpoch,
2781
+ epochSchedule.firstNormalSlot,
2782
+ );
2792
2783
  }
2793
2784
 
2794
2785
  /**
@@ -3257,15 +3248,26 @@ export class Connection {
3257
3248
  }
3258
3249
 
3259
3250
  /**
3260
- * Request an allocation of lamports to the specified account
3251
+ * Request an allocation of lamports to the specified address
3252
+ *
3253
+ * ```typescript
3254
+ * import { Connection, PublicKey, LAMPORTS_PER_SOL } from "@solana/web3.js";
3255
+ *
3256
+ * (async () => {
3257
+ * const connection = new Connection("https://api.testnet.solana.com", "confirmed");
3258
+ * const myAddress = new PublicKey("2nr1bHFT86W9tGnyvmYW4vcHKsQB3sVQfnddasz4kExM");
3259
+ * const signature = await connection.requestAirdrop(myAddress, LAMPORTS_PER_SOL);
3260
+ * await connection.confirmTransaction(signature);
3261
+ * })();
3262
+ * ```
3261
3263
  */
3262
3264
  async requestAirdrop(
3263
3265
  to: PublicKey,
3264
- amount: number,
3266
+ lamports: number,
3265
3267
  ): Promise<TransactionSignature> {
3266
3268
  const unsafeRes = await this._rpcRequest('requestAirdrop', [
3267
3269
  to.toBase58(),
3268
- amount,
3270
+ lamports,
3269
3271
  ]);
3270
3272
  const res = create(unsafeRes, RequestAirdropRpcResult);
3271
3273
  if ('error' in res) {
@@ -0,0 +1,102 @@
1
+ const MINIMUM_SLOT_PER_EPOCH = 32;
2
+
3
+ // Returns the number of trailing zeros in the binary representation of self.
4
+ function trailingZeros(n: number) {
5
+ let trailingZeros = 0;
6
+ while (n > 1) {
7
+ n /= 2;
8
+ trailingZeros++;
9
+ }
10
+ return trailingZeros;
11
+ }
12
+
13
+ // Returns the smallest power of two greater than or equal to n
14
+ function nextPowerOfTwo(n: number) {
15
+ if (n === 0) return 1;
16
+ n--;
17
+ n |= n >> 1;
18
+ n |= n >> 2;
19
+ n |= n >> 4;
20
+ n |= n >> 8;
21
+ n |= n >> 16;
22
+ n |= n >> 32;
23
+ return n + 1;
24
+ }
25
+
26
+ /**
27
+ * Epoch schedule
28
+ * (see https://docs.solana.com/terminology#epoch)
29
+ * Can be retrieved with the {@link connection.getEpochSchedule} method
30
+ */
31
+ export class EpochSchedule {
32
+ /** The maximum number of slots in each epoch */
33
+ public slotsPerEpoch: number;
34
+ /** The number of slots before beginning of an epoch to calculate a leader schedule for that epoch */
35
+ public leaderScheduleSlotOffset: number;
36
+ /** Indicates whether epochs start short and grow */
37
+ public warmup: boolean;
38
+ /** The first epoch with `slotsPerEpoch` slots */
39
+ public firstNormalEpoch: number;
40
+ /** The first slot of `firstNormalEpoch` */
41
+ public firstNormalSlot: number;
42
+
43
+ constructor(
44
+ slotsPerEpoch: number,
45
+ leaderScheduleSlotOffset: number,
46
+ warmup: boolean,
47
+ firstNormalEpoch: number,
48
+ firstNormalSlot: number,
49
+ ) {
50
+ this.slotsPerEpoch = slotsPerEpoch;
51
+ this.leaderScheduleSlotOffset = leaderScheduleSlotOffset;
52
+ this.warmup = warmup;
53
+ this.firstNormalEpoch = firstNormalEpoch;
54
+ this.firstNormalSlot = firstNormalSlot;
55
+ }
56
+
57
+ getEpoch(slot: number): number {
58
+ return this.getEpochAndSlotIndex(slot)[0];
59
+ }
60
+
61
+ getEpochAndSlotIndex(slot: number): [number, number] {
62
+ if (slot < this.firstNormalSlot) {
63
+ const epoch =
64
+ trailingZeros(nextPowerOfTwo(slot + MINIMUM_SLOT_PER_EPOCH + 1)) -
65
+ trailingZeros(MINIMUM_SLOT_PER_EPOCH) -
66
+ 1;
67
+
68
+ const epochLen = this.getSlotsInEpoch(epoch);
69
+ const slotIndex = slot - (epochLen - MINIMUM_SLOT_PER_EPOCH);
70
+ return [epoch, slotIndex];
71
+ } else {
72
+ const normalSlotIndex = slot - this.firstNormalSlot;
73
+ const normalEpochIndex = Math.floor(normalSlotIndex / this.slotsPerEpoch);
74
+ const epoch = this.firstNormalEpoch + normalEpochIndex;
75
+ const slotIndex = normalSlotIndex % this.slotsPerEpoch;
76
+ return [epoch, slotIndex];
77
+ }
78
+ }
79
+
80
+ getFirstSlotInEpoch(epoch: number): number {
81
+ if (epoch <= this.firstNormalEpoch) {
82
+ return (Math.pow(2, epoch) - 1) * MINIMUM_SLOT_PER_EPOCH;
83
+ } else {
84
+ return (
85
+ (epoch - this.firstNormalEpoch) * this.slotsPerEpoch +
86
+ this.firstNormalSlot
87
+ );
88
+ }
89
+ }
90
+
91
+ getLastSlotInEpoch(epoch: number): number {
92
+ return this.getFirstSlotInEpoch(epoch) + this.getSlotsInEpoch(epoch) - 1;
93
+ }
94
+
95
+ getSlotsInEpoch(epoch: number) {
96
+ if (epoch < this.firstNormalEpoch) {
97
+ return Math.pow(2, epoch + trailingZeros(MINIMUM_SLOT_PER_EPOCH));
98
+ } else {
99
+ return this.slotsPerEpoch;
100
+ }
101
+ }
102
+ }
package/src/index.ts CHANGED
@@ -3,6 +3,7 @@ export * from './blockhash';
3
3
  export * from './bpf-loader-deprecated';
4
4
  export * from './bpf-loader';
5
5
  export * from './connection';
6
+ export * from './epoch-schedule';
6
7
  export * from './fee-calculator';
7
8
  export * from './keypair';
8
9
  export * from './loader';