Skip to content

Commit 60cdaa7

Browse files
authored
Bump Zig to 0.15 (#10)
#### Problem The repo is still on Zig 0.14. #### Summary of changes Update everything, along with tests.
1 parent bbddbdd commit 60cdaa7

File tree

7 files changed

+142
-173
lines changed

7 files changed

+142
-173
lines changed

.github/workflows/main.yml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
name: Build and test library
1+
name: Main
22

33
on:
44
push:
@@ -18,13 +18,13 @@ jobs:
1818
- uses: actions/checkout@v2
1919
- uses: goto-bus-stop/setup-zig@v2
2020
with:
21-
version: 0.14.0
21+
version: 0.15.2
2222
- run: zig build test --summary all
2323
lint:
2424
runs-on: ubuntu-latest
2525
steps:
2626
- uses: actions/checkout@v2
2727
- uses: goto-bus-stop/setup-zig@v2
2828
with:
29-
version: 0.14.0
29+
version: 0.15.2
3030
- run: zig fmt --check .

build.zig

Lines changed: 1 addition & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,71 +1,32 @@
11
const std = @import("std");
22

3-
// Although this function looks imperative, note that its job is to
4-
// declaratively construct a build graph that will be executed by an external
5-
// runner.
63
pub fn build(b: *std.Build) void {
7-
// Standard target options allows the person running `zig build` to choose
8-
// what target to build for. Here we do not override the defaults, which
9-
// means any target is allowed, and the default is native. Other options
10-
// for restricting supported target set are available.
114
const target = b.standardTargetOptions(.{});
12-
13-
// Standard optimization options allow the person running `zig build` to select
14-
// between Debug, ReleaseSafe, ReleaseFast, and ReleaseSmall. Here we do not
15-
// set a preferred release mode, allowing the user to decide how to optimize.
165
const optimize = b.standardOptimizeOption(.{});
176

187
const dep_opts = .{ .target = target, .optimize = optimize };
19-
// Export self as a module
208
const sol_lib_mod = b.addModule("solana_program_library", .{
219
.root_source_file = b.path("src/root.zig"),
2210
.target = target,
2311
.optimize = optimize,
2412
});
2513

26-
const lib = b.addStaticLibrary(.{
27-
.name = "solana_program_library",
28-
// In this case the main source file is merely a path, however, in more
29-
// complicated build scripts, this could be a generated file.
30-
.root_source_file = b.path("src/root.zig"),
31-
.target = target,
32-
.optimize = optimize,
33-
});
34-
3514
// Adding it as a module
3615
const solana_dep = b.dependency("solana_program_sdk", dep_opts);
3716
const solana_mod = solana_dep.module("solana_program_sdk");
38-
lib.root_module.addImport("solana_program_sdk", solana_mod);
3917
sol_lib_mod.addImport("solana_program_sdk", solana_mod);
4018

4119
const bincode_dep = b.dependency("bincode", .{
4220
.target = target,
4321
.optimize = optimize,
4422
});
4523
const bincode_mod = bincode_dep.module("bincode");
46-
lib.root_module.addImport("bincode", bincode_mod);
4724
sol_lib_mod.addImport("bincode", bincode_mod);
4825

49-
// This declares intent for the library to be installed into the standard
50-
// location when the user invokes the "install" step (the default step when
51-
// running `zig build`).
52-
b.installArtifact(lib);
53-
54-
// Creates a step for unit testing. This only builds the test executable
55-
// but does not run it.
5626
const lib_unit_tests = b.addTest(.{
57-
.root_source_file = b.path("src/root.zig"),
58-
.target = target,
59-
.optimize = optimize,
27+
.root_module = sol_lib_mod,
6028
});
61-
lib_unit_tests.root_module.addImport("solana_program_sdk", solana_mod);
62-
lib_unit_tests.root_module.addImport("bincode", bincode_mod);
63-
6429
const run_lib_unit_tests = b.addRunArtifact(lib_unit_tests);
65-
66-
// Similar to creating the run step earlier, this exposes a `test` step to
67-
// the `zig build --help` menu, providing a way for the user to request
68-
// running the unit tests.
6930
const test_step = b.step("test", "Run unit tests");
7031
test_step.dependOn(&run_lib_unit_tests.step);
7132
}

build.zig.zon

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,18 @@
11
.{
22
.fingerprint = 0xa4b85d3595fe4ad7,
33
.name = .solana_program_library,
4-
5-
.version = "0.15.1",
6-
7-
.minimum_zig_version = "0.14.0",
8-
4+
.version = "0.16.0",
5+
.minimum_zig_version = "0.15.0",
96
.dependencies = .{
107
.bincode = .{
11-
.url = "https://github.com/joncinque/bincode-zig/archive/refs/tags/v0.14.0.tar.gz",
12-
.hash = "bincode-0.14.0-LYspxE5uAABpwu-W95yJ9swZ3YhGkgbwaGwVjzNZGNXH",
8+
.url = "https://github.com/joncinque/bincode-zig/archive/refs/tags/v0.15.1.tar.gz",
9+
.hash = "bincode-0.15.1-LYspxORwAAC0ty6JQz_DAmUk2gHiOMmJnYveMUEW939Z",
1310
},
1411
.solana_program_sdk = .{
15-
.url = "https://github.com/joncinque/solana-program-sdk-zig/archive/refs/tags/v0.16.3.tar.gz",
16-
.hash = "solana_program_sdk-0.16.3-wGj9UHnjAADiUN0cEUQGAbMCpDDiNUhKI5xkCuiFNGhr",
12+
.url = "https://github.com/joncinque/solana-program-sdk-zig/archive/refs/tags/v0.17.0.tar.gz",
13+
.hash = "solana_program_sdk-0.17.0-wGj9UHjiAAC5lyL1BQ4MI7wcQYrqSjHrxdvZXDaA8Glb",
1714
},
1815
},
19-
2016
.paths = .{
2117
"build.zig",
2218
"build.zig.zon",

src/associated_token_account.zig

Lines changed: 36 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,12 @@ const std = @import("std");
22
const bincode = @import("bincode");
33
const sol = @import("solana_program_sdk");
44

5+
const Account = sol.account.Account;
6+
const ProgramDerivedAddress = sol.public_key.ProgramDerivedAddress;
7+
const PublicKey = sol.public_key.PublicKey;
8+
59
const AssociatedTokenAccountProgram = @This();
6-
pub const id = sol.PublicKey.comptimeFromBase58("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
10+
pub const id = PublicKey.comptimeFromBase58("ATokenGPvbdGVxr1b2hvZbsiqW5xWH25efTNsLJA8knL");
711

812
pub const Instruction = union(enum(u8)) {
913
/// Creates an associated token account for the given wallet address and token mint
@@ -48,31 +52,31 @@ pub const Instruction = union(enum(u8)) {
4852
recover_nested: void,
4953
};
5054

51-
pub fn getAssociatedTokenAccountAddressWithProgramId(wallet_address: sol.PublicKey, mint_address: sol.PublicKey) !sol.PublicKey {
55+
pub fn getAssociatedTokenAccountAddressWithProgramId(wallet_address: PublicKey, mint_address: PublicKey) !PublicKey {
5256
const pda = try getAssociatedTokenAccountAddressAndBumpSeed(wallet_address, mint_address);
5357
return pda.address;
5458
}
5559

56-
pub fn getAssociatedTokenAccountAddressAndBumpSeed(wallet_address: sol.PublicKey, mint_address: sol.PublicKey, token_program_id: sol.PublicKey) !sol.ProgramDerivedAddress {
57-
return sol.PublicKey.findProgramAddress(.{ wallet_address, token_program_id, mint_address }, id);
60+
pub fn getAssociatedTokenAccountAddressAndBumpSeed(wallet_address: PublicKey, mint_address: PublicKey, token_program_id: PublicKey) !ProgramDerivedAddress {
61+
return PublicKey.findProgramAddress(.{ wallet_address, token_program_id, mint_address }, id);
5862
}
5963

6064
pub fn createAccount(params: struct {
61-
funder: sol.Account.Info,
62-
account: sol.Account.Info,
63-
owner: sol.Account.Info,
64-
mint: sol.Account.Info,
65-
system_program: sol.Account.Info,
66-
token_program: sol.Account.Info,
67-
rent: sol.Account.Info,
65+
funder: Account.Info,
66+
account: Account.Info,
67+
owner: Account.Info,
68+
mint: Account.Info,
69+
system_program: Account.Info,
70+
token_program: Account.Info,
71+
rent: Account.Info,
6872
seeds: []const []const []const u8 = &.{},
6973
}) !void {
7074
var data: [1]u8 = undefined;
7175
_ = try bincode.writeToSlice(&data, AssociatedTokenAccountProgram.Instruction.create, .default);
7276

73-
const instruction = sol.Instruction.from(.{
77+
const instruction = sol.instruction.Instruction.from(.{
7478
.program_id = &id,
75-
.accounts = &[_]sol.Account.Param{
79+
.accounts = &[_]Account.Param{
7680
.{ .id = params.funder.id, .is_writable = true, .is_signer = true },
7781
.{ .id = params.account.id, .is_writable = true, .is_signer = false },
7882
.{ .id = params.owner.id, .is_writable = false, .is_signer = false },
@@ -96,21 +100,21 @@ pub fn createAccount(params: struct {
96100
}
97101

98102
pub fn createIdempotentAccount(params: struct {
99-
funder: sol.Account.Info,
100-
account: sol.Account.Info,
101-
owner: sol.Account.Info,
102-
mint: sol.Account.Info,
103-
system_program: sol.Account.Info,
104-
token_program: sol.Account.Info,
105-
associated_token_program: sol.Account.Info,
103+
funder: Account.Info,
104+
account: Account.Info,
105+
owner: Account.Info,
106+
mint: Account.Info,
107+
system_program: Account.Info,
108+
token_program: Account.Info,
109+
associated_token_program: Account.Info,
106110
seeds: []const []const []const u8 = &.{},
107111
}) !void {
108112
var data: [1]u8 = undefined;
109113
_ = try bincode.writeToSlice(&data, AssociatedTokenAccountProgram.Instruction.create_idempotent, .default);
110114

111-
const instruction = sol.Instruction.from(.{
115+
const instruction = sol.instruction.Instruction.from(.{
112116
.program_id = &id,
113-
.accounts = &[_]sol.Account.Param{
117+
.accounts = &[_]Account.Param{
114118
.{ .id = params.funder.id, .is_writable = true, .is_signer = true },
115119
.{ .id = params.account.id, .is_writable = true, .is_signer = false },
116120
.{ .id = params.owner.id, .is_writable = false, .is_signer = false },
@@ -133,22 +137,22 @@ pub fn createIdempotentAccount(params: struct {
133137
}
134138

135139
pub fn recoverNestedAccount(params: struct {
136-
account: sol.Account.Info,
137-
nested_mint: sol.Account.Info,
138-
destination_associated_account: sol.Account.Info,
139-
owner_associated_account: sol.Account.Info,
140-
owner_mint: sol.Account.Info,
141-
owner: sol.Account.Info,
142-
token_program: sol.Account.Info,
143-
associated_token_program: sol.Account.Info,
140+
account: Account.Info,
141+
nested_mint: Account.Info,
142+
destination_associated_account: Account.Info,
143+
owner_associated_account: Account.Info,
144+
owner_mint: Account.Info,
145+
owner: Account.Info,
146+
token_program: Account.Info,
147+
associated_token_program: Account.Info,
144148
seeds: []const []const []const u8 = &.{},
145149
}) !void {
146150
var data: [1]u8 = undefined;
147151
_ = try bincode.writeToSlice(&data, AssociatedTokenAccountProgram.Instruction.recover_nested, .default);
148152

149-
const instruction = sol.Instruction.from(.{
153+
const instruction = sol.instruction.Instruction.from(.{
150154
.program_id = &id,
151-
.accounts = &[_]sol.Account.Param{
155+
.accounts = &[_]Account.Param{
152156
.{ .id = params.account.id, .is_writable = true, .is_signer = false },
153157
.{ .id = params.nested_mint.id, .is_writable = false, .is_signer = false },
154158
.{ .id = params.destination_associated_account.id, .is_writable = true, .is_signer = false },

src/compute_budget.zig

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2,57 +2,59 @@ const std = @import("std");
22
const sol = @import("solana_program_sdk");
33
const bincode = @import("bincode");
44

5-
const Account = sol.Account;
5+
const Account = sol.account.Account;
6+
const PublicKey = sol.public_key.PublicKey;
7+
const SolInstruction = sol.instruction.Instruction;
68

79
const ComputeBudget = @This();
810

9-
pub const id = sol.PublicKey.comptimeFromBase58("ComputeBudget111111111111111111111111111111");
11+
pub const id = PublicKey.comptimeFromBase58("ComputeBudget111111111111111111111111111111");
1012

11-
pub fn requestHeapFrame(allocator: std.mem.Allocator, bytes: u32) !sol.Instruction {
13+
pub fn requestHeapFrame(allocator: std.mem.Allocator, bytes: u32) !SolInstruction {
1214
const data = try bincode.writeAlloc(allocator, ComputeBudget.Instruction{
1315
.request_heap_frame = .{ .bytes = bytes },
1416
}, .default);
1517

16-
return sol.Instruction.from(.{
18+
return SolInstruction.from(.{
1719
.program_id = &id,
1820
.accounts = &[_]Account.Param{},
19-
.data = data,
21+
.data = data.items,
2022
});
2123
}
2224

23-
pub fn setComputeUnitLimit(allocator: std.mem.Allocator, compute_units: u32) !sol.Instruction {
25+
pub fn setComputeUnitLimit(allocator: std.mem.Allocator, compute_units: u32) !SolInstruction {
2426
const data = try bincode.writeAlloc(allocator, ComputeBudget.Instruction{
2527
.set_compute_unit_limit = .{ .compute_units = compute_units },
2628
}, .default);
2729

28-
return sol.Instruction.from(.{
30+
return SolInstruction.from(.{
2931
.program_id = &id,
3032
.accounts = &[_]Account.Param{},
31-
.data = data,
33+
.data = data.items,
3234
});
3335
}
3436

35-
pub fn setComputeUnitPrice(allocator: std.mem.Allocator, micro_lamports: u64) !sol.Instruction {
37+
pub fn setComputeUnitPrice(allocator: std.mem.Allocator, micro_lamports: u64) !SolInstruction {
3638
const data = try bincode.writeAlloc(allocator, ComputeBudget.Instruction{
3739
.set_compute_unit_price = .{ .micro_lamports = micro_lamports },
3840
}, .default);
3941

40-
return sol.Instruction.from(.{
42+
return SolInstruction.from(.{
4143
.program_id = &id,
4244
.accounts = &[_]Account.Param{},
43-
.data = data,
45+
.data = data.items,
4446
});
4547
}
4648

47-
pub fn setLoadedAccountsDataSizeLimit(allocator: std.mem.Allocator, bytes: u32) !sol.Instruction {
49+
pub fn setLoadedAccountsDataSizeLimit(allocator: std.mem.Allocator, bytes: u32) !SolInstruction {
4850
const data = try bincode.writeAlloc(allocator, ComputeBudget.Instruction{
4951
.set_loaded_accounts_data_size_limit = .{ .bytes = bytes },
5052
}, .default);
5153

52-
return sol.Instruction.from(.{
54+
return SolInstruction.from(.{
5355
.program_id = &id,
5456
.accounts = &[_]Account.Param{},
55-
.data = data,
57+
.data = data.items,
5658
});
5759
}
5860

src/system.zig

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ const std = @import("std");
22
const bincode = @import("bincode");
33
const sol = @import("solana_program_sdk");
44

5-
const Account = sol.Account;
6-
const PublicKey = sol.PublicKey;
5+
const Account = sol.account.Account;
6+
const PublicKey = sol.public_key.PublicKey;
77

88
const SystemProgram = @This();
99
pub const id = PublicKey.comptimeFromBase58("11111111111111111111111111111111");
@@ -25,7 +25,7 @@ pub fn createAccount(params: struct {
2525
},
2626
}, .default);
2727

28-
const instruction = sol.Instruction.from(.{
28+
const instruction = sol.instruction.Instruction.from(.{
2929
.program_id = &id,
3030
.accounts = &[_]Account.Param{
3131
.{ .id = params.from.id, .is_writable = true, .is_signer = true },
@@ -48,7 +48,7 @@ pub fn transfer(params: struct {
4848
.transfer = .{ .lamports = params.lamports },
4949
}, .default);
5050

51-
const instruction = sol.Instruction.from(.{
51+
const instruction = sol.instruction.Instruction.from(.{
5252
.program_id = &id,
5353
.accounts = &[_]Account.Param{
5454
.{ .id = params.from.id, .is_writable = true, .is_signer = true },
@@ -70,7 +70,7 @@ pub fn allocate(params: struct {
7070
.allocate = .{ .space = params.space },
7171
}, .default);
7272

73-
const instruction = sol.Instruction.from(.{
73+
const instruction = sol.instruction.Instruction.from(.{
7474
.program_id = &id,
7575
.accounts = &[_]Account.Param{
7676
.{ .id = params.account.id, .is_writable = true, .is_signer = true },
@@ -91,7 +91,7 @@ pub fn assign(params: struct {
9191
.assign = .{ .owner_id = params.owner_id },
9292
}, .default);
9393

94-
const instruction = sol.Instruction.from(.{
94+
const instruction = sol.instruction.Instruction.from(.{
9595
.program_id = &id,
9696
.accounts = &[_]Account.Param{
9797
.{ .id = params.account.id, .is_writable = true, .is_signer = true },
@@ -246,8 +246,8 @@ pub const Instruction = union(enum(u32)) {
246246
};
247247

248248
test "SystemProgram.Instruction: serialize and deserialize" {
249-
var buffer = std.ArrayList(u8).init(std.testing.allocator);
250-
defer buffer.deinit();
249+
var writer = std.Io.Writer.Allocating.init(std.testing.allocator);
250+
defer writer.deinit();
251251

252252
inline for (.{ bincode.Params.default, bincode.Params.legacy, bincode.Params.standard }) |params| {
253253
inline for (.{
@@ -259,10 +259,12 @@ test "SystemProgram.Instruction: serialize and deserialize" {
259259
},
260260
},
261261
}) |payload| {
262-
try bincode.write(buffer.writer(), payload, params);
263-
var stream = std.io.fixedBufferStream(buffer.items);
264-
try std.testing.expectEqual(payload, try bincode.read(std.testing.allocator, @TypeOf(payload), stream.reader(), params));
265-
buffer.clearRetainingCapacity();
262+
try bincode.write(&writer.writer, payload, params);
263+
var buffer = writer.toArrayList();
264+
defer buffer.deinit(std.testing.allocator);
265+
var reader = std.Io.Reader.fixed(buffer.items);
266+
try std.testing.expectEqual(payload, try bincode.read(std.testing.allocator, @TypeOf(payload), &reader, params));
267+
writer.clearRetainingCapacity();
266268
}
267269
}
268270
}

0 commit comments

Comments
 (0)