Skip to content

Commit 7c00254

Browse files
committed
Document @solana/sysvars with TypeDoc
1 parent 65235d4 commit 7c00254

12 files changed

+318
-86
lines changed

packages/rpc-api/src/getEpochSchedule.ts

+7-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
11
type GetEpochScheduleApiResponse = Readonly<{
2-
/** First normal-length epoch, log2(slotsPerEpoch) - log2(MINIMUM_SLOTS_PER_EPOCH) */
2+
/**
3+
* First normal-length epoch after the warmup period,
4+
* log2(slotsPerEpoch) - log2(MINIMUM_SLOTS_PER_EPOCH)
5+
*/
36
firstNormalEpoch: bigint;
4-
/** MINIMUM_SLOTS_PER_EPOCH * (2^(firstNormalEpoch) - 1) */
7+
/**
8+
* The first slot after the warmup period, MINIMUM_SLOTS_PER_EPOCH * (2^(firstNormalEpoch) - 1)
9+
*/
510
firstNormalSlot: bigint;
611
/**
712
* The number of slots before beginning of an epoch to calculate a leader schedule for that

packages/sysvars/src/clock.ts

+26-6
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,30 @@ import { fetchEncodedSysvarAccount, SYSVAR_CLOCK_ADDRESS } from './sysvar';
2020
type SysvarClockSize = 40;
2121

2222
/**
23-
* The `Clock` sysvar.
24-
*
25-
* Information about the network’s clock, ticks, slots, etc.
23+
* Contains data on cluster time, including the current slot, epoch, and estimated wall-clock Unix
24+
* timestamp. It is updated every slot.
2625
*/
2726
export type SysvarClock = Readonly<{
27+
/** The current epoch */
2828
epoch: Epoch;
29+
/**
30+
* The Unix timestamp of the first slot in this epoch.
31+
*
32+
* In the first slot of an epoch, this timestamp is identical to the `unixTimestamp`.
33+
*/
2934
epochStartTimestamp: UnixTimestamp;
35+
/** The most recent epoch for which the leader schedule has already been generated */
3036
leaderScheduleEpoch: Epoch;
37+
/** The current slot */
3138
slot: Slot;
39+
/** The Unix timestamp of this slot */
3240
unixTimestamp: UnixTimestamp;
3341
}>;
3442

43+
/**
44+
* Returns an encoder that you can use to encode a {@link SysvarClock} to a byte array representing
45+
* the `Clock` sysvar's account data.
46+
*/
3547
export function getSysvarClockEncoder(): FixedSizeEncoder<SysvarClock, SysvarClockSize> {
3648
return getStructEncoder([
3749
['slot', getU64Encoder()],
@@ -42,6 +54,10 @@ export function getSysvarClockEncoder(): FixedSizeEncoder<SysvarClock, SysvarClo
4254
]) as FixedSizeEncoder<SysvarClock, SysvarClockSize>;
4355
}
4456

57+
/**
58+
* Returns a decoder that you can use to decode a byte array representing the `Clock` sysvar's
59+
* account data to a {@link SysvarClock}.
60+
*/
4561
export function getSysvarClockDecoder(): FixedSizeDecoder<SysvarClock, SysvarClockSize> {
4662
return getStructDecoder([
4763
['slot', getU64Decoder()],
@@ -52,14 +68,18 @@ export function getSysvarClockDecoder(): FixedSizeDecoder<SysvarClock, SysvarClo
5268
]) as FixedSizeDecoder<SysvarClock, SysvarClockSize>;
5369
}
5470

71+
/**
72+
* Returns a codec that you can use to encode from or decode to {@link SysvarClock}
73+
*
74+
* @see {@link getSysvarClockDecoder}
75+
* @see {@link getSysvarClockEncoder}
76+
*/
5577
export function getSysvarClockCodec(): FixedSizeCodec<SysvarClock, SysvarClock, SysvarClockSize> {
5678
return combineCodec(getSysvarClockEncoder(), getSysvarClockDecoder());
5779
}
5880

5981
/**
60-
* Fetch the `Clock` sysvar.
61-
*
62-
* Information about the network’s clock, ticks, slots, etc.
82+
* Fetches the `Clock` sysvar account using any RPC that supports the {@link GetAccountInfoApi}.
6383
*/
6484
export async function fetchSysvarClock(rpc: Rpc<GetAccountInfoApi>, config?: FetchAccountConfig): Promise<SysvarClock> {
6585
const account = await fetchEncodedSysvarAccount(rpc, SYSVAR_CLOCK_ADDRESS, config);

packages/sysvars/src/epoch-rewards.ts

+37-21
Original file line numberDiff line numberDiff line change
@@ -29,28 +29,43 @@ import { fetchEncodedSysvarAccount, SYSVAR_EPOCH_REWARDS_ADDRESS } from './sysva
2929
type SysvarEpochRewardsSize = 81;
3030

3131
/**
32-
* The `EpochRewards` sysvar.
32+
* Tracks whether the rewards period (including calculation and distribution) is in progress, as
33+
* well as the details needed to resume distribution when starting from a snapshot during the
34+
* rewards period.
3335
*
34-
* Tracks the progress of epoch rewards distribution. It includes:
35-
* - Total rewards for the current epoch, in lamports.
36-
* - Rewards for the current epoch distributed so far, in lamports.
37-
* - Distribution completed block height, i.e. distribution of all staking rewards for the current
38-
* epoch will be completed at this block height.
39-
*
40-
* Note that `EpochRewards` only lasts for a handful of blocks at the start of
41-
* an epoch. When all rewards have been distributed, the sysvar is deleted.
42-
* See https://github.com/anza-xyz/agave/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/docs/src/runtime/sysvars.md?plain=1#L155-L168
36+
* The sysvar is repopulated at the start of the first block of each epoch. Therefore, the sysvar
37+
* contains data about the current epoch until a new epoch begins.
4338
*/
4439
export type SysvarEpochRewards = Readonly<{
40+
/** Whether the rewards period (including calculation and distribution) is active */
4541
active: boolean;
42+
/** The rewards currently distributed for the current epoch, in {@link Lamports} */
4643
distributedRewards: Lamports;
44+
/** The starting block height of the rewards distribution in the current epoch */
4745
distributionStartingBlockHeight: bigint;
46+
/**
47+
* Number of partitions in the rewards distribution in the current epoch, used to generate an
48+
* `EpochRewardsHasher`
49+
*/
4850
numPartitions: bigint;
51+
/**
52+
* The {@link Blockhash} of the parent block of the first block in the epoch, used to seed an
53+
* `EpochRewardsHasher`
54+
*/
4955
parentBlockhash: Blockhash;
56+
/**
57+
* The total rewards points calculated for the current epoch, where points equals the sum of
58+
* (delegated stake * credits observed) for all delegations
59+
*/
5060
totalPoints: bigint;
61+
/** The total rewards for the current epoch, in {@link Lamports} */
5162
totalRewards: Lamports;
5263
}>;
5364

65+
/**
66+
* Returns an encoder that you can use to encode a {@link SysvarEpochRewards} to a byte array
67+
* representing the `EpochRewards` sysvar's account data.
68+
*/
5469
export function getSysvarEpochRewardsEncoder(): FixedSizeEncoder<SysvarEpochRewards, SysvarEpochRewardsSize> {
5570
return getStructEncoder([
5671
['distributionStartingBlockHeight', getU64Encoder()],
@@ -63,6 +78,10 @@ export function getSysvarEpochRewardsEncoder(): FixedSizeEncoder<SysvarEpochRewa
6378
]) as FixedSizeEncoder<SysvarEpochRewards, SysvarEpochRewardsSize>;
6479
}
6580

81+
/**
82+
* Returns a decoder that you can use to decode a byte array representing the `EpochRewards`
83+
* sysvar's account data to a {@link SysvarEpochRewards}.
84+
*/
6685
export function getSysvarEpochRewardsDecoder(): FixedSizeDecoder<SysvarEpochRewards, SysvarEpochRewardsSize> {
6786
return getStructDecoder([
6887
['distributionStartingBlockHeight', getU64Decoder()],
@@ -75,6 +94,12 @@ export function getSysvarEpochRewardsDecoder(): FixedSizeDecoder<SysvarEpochRewa
7594
]) as FixedSizeDecoder<SysvarEpochRewards, SysvarEpochRewardsSize>;
7695
}
7796

97+
/**
98+
* Returns a codec that you can use to encode from or decode to {@link SysvarEpochRewards}
99+
*
100+
* @see {@link getSysvarEpochRewardsDecoder}
101+
* @see {@link getSysvarEpochRewardsEncoder}
102+
*/
78103
export function getSysvarEpochRewardsCodec(): FixedSizeCodec<
79104
SysvarEpochRewards,
80105
SysvarEpochRewards,
@@ -84,17 +109,8 @@ export function getSysvarEpochRewardsCodec(): FixedSizeCodec<
84109
}
85110

86111
/**
87-
* Fetch the `EpochRewards` sysvar.
88-
*
89-
* Tracks the progress of epoch rewards distribution. It includes:
90-
* - Total rewards for the current epoch, in lamports.
91-
* - Rewards for the current epoch distributed so far, in lamports.
92-
* - Distribution completed block height, i.e. distribution of all staking rewards for the current
93-
* epoch will be completed at this block height.
94-
*
95-
* Note that `EpochRewards` only lasts for a handful of blocks at the start of
96-
* an epoch. When all rewards have been distributed, the sysvar is deleted.
97-
* See https://github.com/anza-xyz/agave/blob/e0203f22dc83cb792fa97f91dbe6e924cbd08af1/docs/src/runtime/sysvars.md?plain=1#L155-L168
112+
* Fetch the `EpochRewards` sysvar account using any RPC that supports the
113+
* {@link GetAccountInfoApi}.
98114
*/
99115
export async function fetchSysvarEpochRewards(
100116
rpc: Rpc<GetAccountInfoApi>,

packages/sysvars/src/epoch-schedule.ts

+31-6
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,34 @@ import { fetchEncodedSysvarAccount, SYSVAR_EPOCH_SCHEDULE_ADDRESS } from './sysv
2020
type SysvarEpochScheduleSize = 33;
2121

2222
/**
23-
* The `EpochSchedule` sysvar.
24-
*
25-
* Information about epoch duration.
23+
* Includes the number of slots per epoch, timing of leader schedule selection, and information
24+
* about epoch warm-up time.
2625
*/
2726
export type SysvarEpochSchedule = Readonly<{
27+
/**
28+
* First normal-length epoch after the warmup period,
29+
* log2(slotsPerEpoch) - log2(MINIMUM_SLOTS_PER_EPOCH)
30+
*/
2831
firstNormalEpoch: Epoch;
32+
/**
33+
* The first slot after the warmup period, MINIMUM_SLOTS_PER_EPOCH * (2^(firstNormalEpoch) - 1)
34+
*/
2935
firstNormalSlot: Slot;
36+
/**
37+
* A number of slots before beginning of an epoch to calculate a leader schedule for that
38+
* epoch.
39+
*/
3040
leaderScheduleSlotOffset: bigint;
41+
/** The maximum number of slots in each epoch */
3142
slotsPerEpoch: bigint;
43+
/** Whether epochs start short and grow */
3244
warmup: boolean;
3345
}>;
3446

47+
/**
48+
* Returns an encoder that you can use to encode a {@link SysvarEpochSchedule} to a byte array
49+
* representing the `EpochSchedule` sysvar's account data.
50+
*/
3551
export function getSysvarEpochScheduleEncoder(): FixedSizeEncoder<SysvarEpochSchedule, SysvarEpochScheduleSize> {
3652
return getStructEncoder([
3753
['slotsPerEpoch', getU64Encoder()],
@@ -42,6 +58,10 @@ export function getSysvarEpochScheduleEncoder(): FixedSizeEncoder<SysvarEpochSch
4258
]) as FixedSizeEncoder<SysvarEpochSchedule, SysvarEpochScheduleSize>;
4359
}
4460

61+
/**
62+
* Returns a decoder that you can use to decode a byte array representing the `EpochSchedule`
63+
* sysvar's account data to a {@link SysvarEpochSchedule}.
64+
*/
4565
export function getSysvarEpochScheduleDecoder(): FixedSizeDecoder<SysvarEpochSchedule, SysvarEpochScheduleSize> {
4666
return getStructDecoder([
4767
['slotsPerEpoch', getU64Decoder()],
@@ -52,6 +72,12 @@ export function getSysvarEpochScheduleDecoder(): FixedSizeDecoder<SysvarEpochSch
5272
]) as FixedSizeDecoder<SysvarEpochSchedule, SysvarEpochScheduleSize>;
5373
}
5474

75+
/**
76+
* Returns a codec that you can use to encode from or decode to {@link SysvarEpochSchedule}
77+
*
78+
* @see {@link getSysvarEpochScheduleDecoder}
79+
* @see {@link getSysvarEpochScheduleEncoder}
80+
*/
5581
export function getSysvarEpochScheduleCodec(): FixedSizeCodec<
5682
SysvarEpochSchedule,
5783
SysvarEpochSchedule,
@@ -61,9 +87,8 @@ export function getSysvarEpochScheduleCodec(): FixedSizeCodec<
6187
}
6288

6389
/**
64-
* Fetch the `EpochSchedule` sysvar.
65-
*
66-
* Information about epoch duration.
90+
* Fetches the `EpochSchedule` sysvar account using any RPC that supports the
91+
* {@link GetAccountInfoApi}.
6792
*/
6893
export async function fetchSysvarEpochSchedule(
6994
rpc: Rpc<GetAccountInfoApi>,

packages/sysvars/src/index.ts

+54
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,57 @@
1+
/**
2+
* This package contains types and helpers for fetching and decoding Solana sysvars.
3+
* It can be used standalone, but it is also exported as part of Kit
4+
* [`@solana/kit`](https://github.com/anza-xyz/kit/tree/main/packages/kit).
5+
*
6+
* More information about the available sysvars on Solana can be found in the docs at
7+
* https://docs.solanalabs.com/runtime/sysvars.
8+
*
9+
* All currently available sysvars can be retrieved and/or decoded using this library.
10+
*
11+
* - `Clock`
12+
* - `EpochRewards`
13+
* - `EpochSchedule`
14+
* - `Fees`
15+
* - `LastRestartSlot`
16+
* - `RecentBlockhashes`
17+
* - `Rent`
18+
* - `SlotHashes`
19+
* - `SlotHistory`
20+
* - `StakeHistory`
21+
*
22+
* The `Instructions` sysvar is also supported but does not exist on-chain, therefore has no
23+
* corresponding module or codec.
24+
*
25+
* @example Fetch and decode a sysvar account.
26+
* ```ts
27+
* const clock: SysvarClock = await fetchSysvarClock(rpc);
28+
* ```
29+
*
30+
* @example Fetch and decode a sysvar account manually.
31+
* ```ts
32+
* // Fetch.
33+
* const clock = await fetchEncodedSysvarAccount(rpc, SYSVAR_CLOCK_ADDRESS);
34+
* clock satisfies MaybeEncodedAccount<'SysvarC1ock11111111111111111111111111111111'>;
35+
*
36+
* // Assert.
37+
* assertAccountExists(clock);
38+
* clock satisfies EncodedAccount<'SysvarC1ock11111111111111111111111111111111'>;
39+
*
40+
* // Decode.
41+
* const decodedClock = decodeAccount(clock, getSysvarClockDecoder());
42+
* decodedClock satisfies Account<SysvarClock, 'SysvarC1ock11111111111111111111111111111111'>;
43+
* ```
44+
*
45+
* @example Fetch a JSON-parsed sysvar account.
46+
* ```ts
47+
* const maybeJsonParsedClock = await fetchJsonParsedSysvarAccount(rpc, SYSVAR_CLOCK_ADDRESS);
48+
* maybeJsonParsedClock satisfies
49+
* | MaybeAccount<JsonParsedSysvarAccount, 'SysvarC1ock11111111111111111111111111111111'>
50+
* | MaybeEncodedAccount<'SysvarC1ock11111111111111111111111111111111'>;
51+
* ```
52+
*
53+
* @packageDocumentation
54+
*/
155
export * from './clock';
256
export * from './epoch-rewards';
357
export * from './epoch-schedule';

packages/sysvars/src/last-restart-slot.ts

+20-12
Original file line numberDiff line numberDiff line change
@@ -18,32 +18,45 @@ import { fetchEncodedSysvarAccount, SYSVAR_LAST_RESTART_SLOT_ADDRESS } from './s
1818
type SysvarLastRestartSlotSize = 8;
1919

2020
/**
21-
* The `LastRestartSlot` sysvar.
22-
*
2321
* Information about the last restart slot (hard fork).
2422
*
25-
* The `LastRestartSlot` sysvar provides access to the last restart slot kept in the
26-
* bank fork for the slot on the fork that executes the current transaction.
27-
* In case there was no fork it returns `0`.
23+
* The `LastRestartSlot` sysvar provides access to the last restart slot kept in the bank fork for
24+
* the slot on the fork that executes the current transaction. In case there was no fork it returns
25+
* `0`.
2826
*/
2927
export type SysvarLastRestartSlot = Readonly<{
28+
/** The last restart {@link Slot} */
3029
lastRestartSlot: Slot;
3130
}>;
3231

32+
/**
33+
* Returns an encoder that you can use to encode a {@link SysvarLastRestartSlot} to a byte array
34+
* representing the `LastRestartSlot` sysvar's account data.
35+
*/
3336
export function getSysvarLastRestartSlotEncoder(): FixedSizeEncoder<SysvarLastRestartSlot, SysvarLastRestartSlotSize> {
3437
return getStructEncoder([['lastRestartSlot', getU64Encoder()]]) as FixedSizeEncoder<
3538
SysvarLastRestartSlot,
3639
SysvarLastRestartSlotSize
3740
>;
3841
}
3942

43+
/**
44+
* Returns a decoder that you can use to decode a byte array representing the `LastRestartSlot`
45+
* sysvar's account data to a {@link SysvarLastRestartSlot}.
46+
*/
4047
export function getSysvarLastRestartSlotDecoder(): FixedSizeDecoder<SysvarLastRestartSlot, SysvarLastRestartSlotSize> {
4148
return getStructDecoder([['lastRestartSlot', getU64Decoder()]]) as FixedSizeDecoder<
4249
SysvarLastRestartSlot,
4350
SysvarLastRestartSlotSize
4451
>;
4552
}
4653

54+
/**
55+
* Returns a codec that you can use to encode from or decode to {@link SysvarLastRestartSlot}
56+
*
57+
* @see {@link getSysvarLastRestartSlotDecoder}
58+
* @see {@link getSysvarLastRestartSlotEncoder}
59+
*/
4760
export function getSysvarLastRestartSlotCodec(): FixedSizeCodec<
4861
SysvarLastRestartSlot,
4962
SysvarLastRestartSlot,
@@ -53,13 +66,8 @@ export function getSysvarLastRestartSlotCodec(): FixedSizeCodec<
5366
}
5467

5568
/**
56-
* Fetch the `LastRestartSlot` sysvar.
57-
*
58-
* Information about the last restart slot (hard fork).
59-
*
60-
* The `LastRestartSlot` sysvar provides access to the last restart slot kept in the
61-
* bank fork for the slot on the fork that executes the current transaction.
62-
* In case there was no fork it returns `0`.
69+
* Fetches the `LastRestartSlot` sysvar account using any RPC that supports the
70+
* {@link GetAccountInfoApi}.
6371
*/
6472
export async function fetchSysvarLastRestartSlot(
6573
rpc: Rpc<GetAccountInfoApi>,

0 commit comments

Comments
 (0)