Skip to content

Commit f4ad94d

Browse files
committed
feat(anchor): add sysvar data aliases
1 parent f774c06 commit f4ad94d

10 files changed

Lines changed: 204 additions & 8 deletions

File tree

CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
All notable changes to the Solana SDK Zig implementation will be documented in this file.
44

5+
### Session 2026-01-15-090
6+
7+
**Date**: 2026-01-15
8+
**Goal**: Add sysvar data aliases
9+
10+
#### Completed Work
11+
1. Added pre-defined SysvarData aliases (Clock/Rent/EpochSchedule/etc.)
12+
2. Added sysvar id-only wrappers
13+
3. Updated docs and examples
14+
15+
#### Test Results
16+
- `./solana-zig/zig build test --summary all`
17+
518
### Session 2026-01-15-089
619

720
**Date**: 2026-01-15

anchor/CHANGELOG.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,19 @@
22

33
All notable changes to sol-anchor-zig will be documented in this file.
44

5+
### Session 2026-01-15-018
6+
7+
**Date**: 2026-01-15
8+
**Goal**: Add sysvar data aliases
9+
10+
#### Completed Work
11+
1. Added pre-defined SysvarData aliases (Clock/Rent/EpochSchedule/etc.)
12+
2. Added sysvar id-only wrappers
13+
3. Updated docs and examples
14+
15+
#### Test Results
16+
- `./solana-zig/zig build test --summary all`
17+
518
### Session 2026-01-15-017
619

720
**Date**: 2026-01-15

anchor/README.md

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,18 @@ fn addMemo(ctx: anchor.Context(Accounts)) !void {
365365
}
366366
```
367367

368+
## Sysvar Data Wrappers
369+
370+
Use `SysvarData` aliases to parse common sysvars directly.
371+
372+
```zig
373+
const Accounts = struct {
374+
clock: anchor.ClockData,
375+
rent: anchor.RentData,
376+
epoch_schedule: anchor.EpochScheduleData,
377+
};
378+
```
379+
368380
## Stake Wrappers + CPI Helpers
369381

370382
Use `anchor.StakeAccount` to parse stake state and `anchor.stake` CPI helpers

anchor/ROADMAP.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# sol-anchor-zig Roadmap
22

3+
## ✅ v3.2.68 - Sysvar Data Aliases
4+
5+
- [x] Add common SysvarData aliases (Clock/Rent/EpochSchedule/etc.)
6+
- [x] Add sysvar id-only wrappers
7+
- [x] Update docs and examples
8+
39
## ✅ v3.2.67 - Transfer Checked Sugar
410

511
- [x] Add `transferCheckedWithMint` helper for token CPI

anchor/src/root.zig

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -559,6 +559,24 @@ pub const Sysvar = sysvar_account.Sysvar;
559559
/// Sysvar account wrapper with data parsing.
560560
pub const SysvarData = sysvar_account.SysvarData;
561561

562+
/// Sysvar data wrappers
563+
pub const ClockData = sysvar_account.ClockData;
564+
pub const RentData = sysvar_account.RentData;
565+
pub const EpochScheduleData = sysvar_account.EpochScheduleData;
566+
pub const SlotHashesData = sysvar_account.SlotHashesData;
567+
pub const SlotHistoryData = sysvar_account.SlotHistoryData;
568+
pub const EpochRewardsData = sysvar_account.EpochRewardsData;
569+
pub const LastRestartSlotData = sysvar_account.LastRestartSlotData;
570+
571+
/// Sysvar id-only wrappers
572+
pub const ClockSysvar = sysvar_account.ClockSysvar;
573+
pub const RentSysvar = sysvar_account.RentSysvar;
574+
pub const EpochScheduleSysvar = sysvar_account.EpochScheduleSysvar;
575+
pub const SlotHashesSysvar = sysvar_account.SlotHashesSysvar;
576+
pub const SlotHistorySysvar = sysvar_account.SlotHistorySysvar;
577+
pub const EpochRewardsSysvar = sysvar_account.EpochRewardsSysvar;
578+
pub const LastRestartSlotSysvar = sysvar_account.LastRestartSlotSysvar;
579+
562580
// ============================================================================
563581
// Context Module
564582
// ============================================================================
@@ -947,6 +965,20 @@ test "anchor module exports" {
947965
_ = Context;
948966
_ = Sysvar;
949967
_ = SysvarData;
968+
_ = ClockData;
969+
_ = RentData;
970+
_ = EpochScheduleData;
971+
_ = SlotHashesData;
972+
_ = SlotHistoryData;
973+
_ = EpochRewardsData;
974+
_ = LastRestartSlotData;
975+
_ = ClockSysvar;
976+
_ = RentSysvar;
977+
_ = EpochScheduleSysvar;
978+
_ = SlotHashesSysvar;
979+
_ = SlotHistorySysvar;
980+
_ = EpochRewardsSysvar;
981+
_ = LastRestartSlotSysvar;
950982
_ = token;
951983
_ = associated_token;
952984
_ = memo;

anchor/src/sysvar_account.zig

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,22 @@ pub fn SysvarId(comptime sysvar_id: PublicKey) type {
1818
pub const Instructions = SysvarId(sol.INSTRUCTIONS_ID);
1919
pub const StakeHistory = SysvarId(sol.STAKE_HISTORY_ID);
2020

21+
pub const ClockData = SysvarData(sol.clock.Clock);
22+
pub const RentData = SysvarData(sol.rent.Rent.Data);
23+
pub const EpochScheduleData = SysvarData(sol.epoch_schedule.EpochSchedule);
24+
pub const SlotHashesData = SysvarData(sol.slot_hashes.SlotHashes);
25+
pub const SlotHistoryData = SysvarData(sol.slot_history.SlotHistory);
26+
pub const EpochRewardsData = SysvarData(sol.epoch_rewards.EpochRewards);
27+
pub const LastRestartSlotData = SysvarData(sol.last_restart_slot.LastRestartSlot);
28+
29+
pub const ClockSysvar = SysvarId(sol.sysvar_id.CLOCK);
30+
pub const RentSysvar = SysvarId(sol.sysvar_id.RENT);
31+
pub const EpochScheduleSysvar = SysvarId(sol.sysvar_id.EPOCH_SCHEDULE);
32+
pub const SlotHashesSysvar = SysvarId(sol.sysvar_id.SLOT_HASHES);
33+
pub const SlotHistorySysvar = SysvarId(sol.sysvar_id.SLOT_HISTORY);
34+
pub const EpochRewardsSysvar = SysvarId(sol.sysvar_id.EPOCH_REWARDS);
35+
pub const LastRestartSlotSysvar = SysvarId(sol.sysvar_id.LAST_RESTART_SLOT);
36+
2137
/// Sysvar account wrapper with address validation.
2238
pub fn Sysvar(comptime SysvarType: type) type {
2339
if (!@hasDecl(SysvarType, "id")) {

anchor/src/typed_dsl.zig

Lines changed: 60 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -164,27 +164,39 @@ pub fn SysvarAccount(comptime sysvar_id: PublicKey) type {
164164

165165
/// Rent Sysvar marker.
166166
/// Usage: `.rent = RentSysvar`
167-
pub const RentSysvar = SysvarAccount(PublicKey.comptimeFromBase58("SysvarRent111111111111111111111111111111111"));
167+
pub const RentSysvar = SysvarAccount(sol.sysvar_id.RENT);
168168

169169
/// Clock Sysvar marker.
170170
/// Usage: `.clock = ClockSysvar`
171-
pub const ClockSysvar = SysvarAccount(PublicKey.comptimeFromBase58("SysvarC1ock11111111111111111111111111111111"));
171+
pub const ClockSysvar = SysvarAccount(sol.sysvar_id.CLOCK);
172172

173173
/// Epoch Schedule Sysvar marker.
174174
/// Usage: `.epoch_schedule = EpochScheduleSysvar`
175-
pub const EpochScheduleSysvar = SysvarAccount(PublicKey.comptimeFromBase58("SysvarEpochScheworLNPkL1MWVrwsLE4D8F2auJV1"));
175+
pub const EpochScheduleSysvar = SysvarAccount(sol.sysvar_id.EPOCH_SCHEDULE);
176176

177177
/// Slot Hashes Sysvar marker.
178178
/// Usage: `.slot_hashes = SlotHashesSysvar`
179-
pub const SlotHashesSysvar = SysvarAccount(PublicKey.comptimeFromBase58("SysvarS1otHashes111111111111111111111111111"));
179+
pub const SlotHashesSysvar = SysvarAccount(sol.sysvar_id.SLOT_HASHES);
180+
181+
/// Slot History Sysvar marker.
182+
/// Usage: `.slot_history = SlotHistorySysvar`
183+
pub const SlotHistorySysvar = SysvarAccount(sol.sysvar_id.SLOT_HISTORY);
180184

181185
/// Stake History Sysvar marker.
182186
/// Usage: `.stake_history = StakeHistorySysvar`
183-
pub const StakeHistorySysvar = SysvarAccount(PublicKey.comptimeFromBase58("SysvarStakeHistory1111111111111111111111111"));
187+
pub const StakeHistorySysvar = SysvarAccount(sol.sysvar_id.STAKE_HISTORY);
188+
189+
/// Epoch Rewards Sysvar marker.
190+
/// Usage: `.epoch_rewards = EpochRewardsSysvar`
191+
pub const EpochRewardsSysvar = SysvarAccount(sol.sysvar_id.EPOCH_REWARDS);
192+
193+
/// Last Restart Slot Sysvar marker.
194+
/// Usage: `.last_restart_slot = LastRestartSlotSysvar`
195+
pub const LastRestartSlotSysvar = SysvarAccount(sol.sysvar_id.LAST_RESTART_SLOT);
184196

185197
/// Instructions Sysvar marker.
186198
/// Usage: `.instructions = InstructionsSysvar`
187-
pub const InstructionsSysvar = SysvarAccount(sol.instructions_sysvar.ID);
199+
pub const InstructionsSysvar = SysvarAccount(sol.sysvar_id.INSTRUCTIONS);
188200

189201
// ============================================================================
190202
// Data Account with Type-Safe Config
@@ -1262,6 +1274,41 @@ test "predefined sysvar markers" {
12621274
&ClockSysvar.ID.toBase58String(),
12631275
);
12641276

1277+
// Epoch Schedule Sysvar
1278+
try std.testing.expect(EpochScheduleSysvar.IS_SYSVAR == true);
1279+
try std.testing.expectEqualStrings(
1280+
"SysvarEpochSchedu1e111111111111111111111111",
1281+
&EpochScheduleSysvar.ID.toBase58String(),
1282+
);
1283+
1284+
// Slot Hashes Sysvar
1285+
try std.testing.expect(SlotHashesSysvar.IS_SYSVAR == true);
1286+
try std.testing.expectEqualStrings(
1287+
"SysvarS1otHashes111111111111111111111111111",
1288+
&SlotHashesSysvar.ID.toBase58String(),
1289+
);
1290+
1291+
// Slot History Sysvar
1292+
try std.testing.expect(SlotHistorySysvar.IS_SYSVAR == true);
1293+
try std.testing.expectEqualStrings(
1294+
"SysvarS1otHistory11111111111111111111111111",
1295+
&SlotHistorySysvar.ID.toBase58String(),
1296+
);
1297+
1298+
// Epoch Rewards Sysvar
1299+
try std.testing.expect(EpochRewardsSysvar.IS_SYSVAR == true);
1300+
try std.testing.expectEqualStrings(
1301+
"SysvarEpochRewards1111111111111111111111111",
1302+
&EpochRewardsSysvar.ID.toBase58String(),
1303+
);
1304+
1305+
// Last Restart Slot Sysvar
1306+
try std.testing.expect(LastRestartSlotSysvar.IS_SYSVAR == true);
1307+
try std.testing.expectEqualStrings(
1308+
"SysvarLastRestartS1ot1111111111111111111111",
1309+
&LastRestartSlotSysvar.ID.toBase58String(),
1310+
);
1311+
12651312
// Instructions Sysvar
12661313
try std.testing.expect(InstructionsSysvar.IS_SYSVAR == true);
12671314
try std.testing.expect(InstructionsSysvar.ID.equals(sol.instructions_sysvar.ID));
@@ -1275,10 +1322,16 @@ test "Accounts with predefined programs" {
12751322
.memo_program = MemoProgram,
12761323
.stake_program = StakeProgram,
12771324
.rent = RentSysvar,
1325+
.clock = ClockSysvar,
1326+
.epoch_schedule = EpochScheduleSysvar,
1327+
.slot_hashes = SlotHashesSysvar,
1328+
.slot_history = SlotHistorySysvar,
1329+
.epoch_rewards = EpochRewardsSysvar,
1330+
.last_restart_slot = LastRestartSlotSysvar,
12781331
});
12791332

12801333
const fields = @typeInfo(TestAccounts).@"struct".fields;
1281-
try std.testing.expect(fields.len == 6);
1334+
try std.testing.expect(fields.len == 12);
12821335
}
12831336

12841337
test "isEventFieldWrapper" {
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
# Story: v3.2.68 Sysvar Data Aliases
2+
3+
> Add pre-defined SysvarData wrappers for common sysvars.
4+
5+
## Goals
6+
7+
- Provide `ClockData`, `RentData`, `EpochScheduleData`, etc. for easy sysvar parsing.
8+
- Provide sysvar ID-only wrappers for common sysvars.
9+
10+
## Acceptance Criteria
11+
12+
### anchor/src/sysvar_account.zig
13+
14+
- [x] Add SysvarData aliases for Clock/Rent/EpochSchedule/SlotHashes/SlotHistory/EpochRewards/LastRestartSlot.
15+
- [x] Add SysvarId aliases for common sysvars.
16+
17+
### anchor/src/root.zig
18+
19+
- [x] Export sysvar data aliases and id-only wrappers.
20+
21+
### anchor/src/typed_dsl.zig
22+
23+
- [x] Extend predefined sysvar markers to include epoch schedule, slot hashes, slot history, epoch rewards, last restart slot.
24+
25+
### Documentation
26+
27+
- [x] Update README and compatibility docs.
28+
- [x] Add sysvar data example.
29+
- [x] Update ROADMAP and CHANGELOG.
30+
31+
## Completion Status
32+
33+
- Start date: 2026-01-15
34+
- Completion date: 2026-01-15
35+
- Status: ✅ Completed

docs/ANCHOR_COMPATIBILITY.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ const sol = anchor.sdk;
3131
- Memo CPI helpers (`anchor.memo`)
3232
- Stake CPI helpers and StakeAccount wrapper (`anchor.stake`, `anchor.StakeAccount`)
3333
- ATA init/payer semantics via `anchor.typed.ATA` + `anchor.associated_token`
34+
- Sysvar data wrappers (`ClockData`, `RentData`, `EpochScheduleData`, etc.)
3435

3536
## Not Implemented Yet
3637

@@ -72,7 +73,7 @@ const sol = anchor.sdk;
7273
| Stake account wrapper | `StakeAccount` | `anchor.StakeAccount(.{})` ||
7374
| Stake CPI helpers | `anchor_spl::stake::*` | `anchor.stake.*` ||
7475
| ATA init | `associated_token::create` | `anchor.typed.ATA` + `anchor.associated_token` ||
75-
| Sysvar parsing | `Sysvar<'info, T>` | `anchor.SysvarData(T)` ||
76+
| Sysvar parsing | `Sysvar<'info, T>` | `anchor.SysvarData(T)` / `ClockData` / `RentData` ||
7677

7778
## Example
7879

examples/anchor_sysvar_data.zig

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//! Example: SysvarData wrappers.
2+
3+
const anchor = @import("sol_anchor_zig");
4+
5+
const Accounts = struct {
6+
clock: anchor.ClockData,
7+
rent: anchor.RentData,
8+
epoch_schedule: anchor.EpochScheduleData,
9+
};
10+
11+
pub fn readSysvars(ctx: anchor.Context(Accounts)) !void {
12+
_ = ctx.accounts.clock.data;
13+
_ = ctx.accounts.rent.data;
14+
_ = ctx.accounts.epoch_schedule.data;
15+
}

0 commit comments

Comments
 (0)