Skip to content

Commit 6071b5c

Browse files
feat: add zig-version of CPI program (#5)
* feat: add zig-version of CPI program * actualize dep * use tag instead of commit sha * add forgotten cpi program to CI step
1 parent b68c6f6 commit 6071b5c

File tree

9 files changed

+104
-11
lines changed

9 files changed

+104
-11
lines changed

.github/workflows/main.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ jobs:
1313
name: Run tests against Zig implementations
1414
strategy:
1515
matrix:
16-
program: [helloworld, transfer-lamports]
16+
program: [helloworld, transfer-lamports, cpi]
1717
fail-fast: false
1818
runs-on: ubuntu-latest
1919
steps:

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,3 +180,4 @@ the address and `invoke_signed` to CPI to the system program.
180180
| Language | CU Usage |
181181
| --- | --- |
182182
| Rust | 3662 |
183+
| Zig | 3141 |

cpi/zig/build.zig

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
const std = @import("std");
2+
const solana = @import("solana-program-sdk");
3+
4+
pub fn build(b: *std.Build) !void {
5+
const target = b.resolveTargetQuery(solana.sbf_target);
6+
const optimize = .ReleaseFast;
7+
8+
const dep_opts = .{ .target = target, .optimize = optimize };
9+
10+
const solana_lib_dep = b.dependency("solana-program-library", dep_opts);
11+
const solana_lib_mod = solana_lib_dep.module("solana-program-library");
12+
13+
const program = b.addSharedLibrary(.{
14+
.name = "solana_program_rosetta_cpi",
15+
.root_source_file = b.path("main.zig"),
16+
.target = target,
17+
.optimize = optimize,
18+
});
19+
20+
program.root_module.addImport("solana-program-library", solana_lib_mod);
21+
22+
_ = solana.buildProgram(b, program, target, optimize);
23+
24+
b.installArtifact(program);
25+
}

cpi/zig/build.zig.zon

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
.{
2+
.name = "solana-program-rosetta-cpi-zig",
3+
// This is a [Semantic Version](https://semver.org/).
4+
// In a future version of Zig it will be used for package deduplication.
5+
.version = "0.13.0",
6+
7+
// This field is optional.
8+
// This is currently advisory only; Zig does not yet do anything
9+
// with this value.
10+
.minimum_zig_version = "0.13.0",
11+
12+
// This field is optional.
13+
// Each dependency must either provide a `url` and `hash`, or a `path`.
14+
// `zig build --fetch` can be used to fetch all dependencies of a package, recursively.
15+
// Once all dependencies are fetched, `zig build` no longer requires
16+
// internet connectivity.
17+
.dependencies = .{
18+
.base58 = .{
19+
.url = "https://github.com/joncinque/base58-zig/archive/refs/tags/v0.13.3.tar.gz",
20+
.hash = "1220fd067bf167b9062cc29ccf715ff97643c2d3f8958beea863b6036876bb71bcb8",
21+
},
22+
.@"solana-program-sdk" = .{
23+
.url = "https://github.com/joncinque/solana-program-sdk-zig/archive/refs/tags/v0.13.1.tar.gz",
24+
.hash = "122030336f1257e3c0aa64243f5243f554b903c6b9ef3a91d48bfbe896c0c7d9b13b",
25+
},
26+
.@"solana-program-library" = .{
27+
.url = "https://github.com/joncinque/solana-program-library-zig/archive/refs/tags/v0.13.1.tar.gz",
28+
.hash = "12208555f3e41bfba0ac89c4a7c6884595c78aa5d4ff4f99869c7ae0e396fd762448",
29+
},
30+
},
31+
32+
// Specifies the set of files and directories that are included in this package.
33+
// Only files and directories listed here are included in the `hash` that
34+
// is computed for this package.
35+
// Paths are relative to the build root. Use the empty string (`""`) to refer to
36+
// the build root itself.
37+
// A directory listed here means that all files within, recursively, are included.
38+
.paths = .{
39+
// For example...
40+
"build.zig",
41+
"build.zig.zon",
42+
"src",
43+
"../../LICENSE",
44+
"../../README.md",
45+
},
46+
}

cpi/zig/main.zig

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
const sol = @import("solana-program-sdk");
2+
const sol_lib = @import("solana-program-library");
3+
4+
const system_ix = sol_lib.system;
5+
const SIZE = 42;
6+
7+
export fn entrypoint(input: [*]u8) u64 {
8+
const context = sol.Context.load(input) catch return 1;
9+
const accounts = context.loadRawAccounts(sol.allocator) catch return 1;
10+
defer accounts.deinit();
11+
12+
const allocated = accounts.items[0];
13+
14+
const expected_allocated_key = sol.PublicKey.createProgramAddress(
15+
&.{ "You pass butter", &.{context.data[0]} },
16+
context.program_id.*,
17+
) catch return 1;
18+
19+
// allocated key does not match the derived address
20+
if (!allocated.id().equals(expected_allocated_key)) return 1;
21+
22+
// Invoke the system program to allocate account data
23+
system_ix.allocate(
24+
sol.allocator,
25+
allocated.info(),
26+
SIZE,
27+
.{ .seeds = &.{&.{ "You pass butter", &.{context.data[0]} }} },
28+
) catch return 1;
29+
30+
return 0;
31+
}

helloworld/zig/build.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const std = @import("std");
22
const solana = @import("solana-program-sdk");
3-
const base58 = @import("base58");
43

54
pub fn build(b: *std.Build) !void {
65
const target = b.resolveTargetQuery(solana.sbf_target);

helloworld/zig/build.zig.zon

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
.url = "https://github.com/joncinque/base58-zig/archive/refs/tags/v0.13.3.tar.gz",
2020
.hash = "1220fd067bf167b9062cc29ccf715ff97643c2d3f8958beea863b6036876bb71bcb8",
2121
},
22-
.clap = .{
23-
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz",
24-
.hash = "122062d301a203d003547b414237229b09a7980095061697349f8bef41be9c30266b",
25-
},
2622
.@"solana-program-sdk" = .{
2723
.url = "https://github.com/joncinque/solana-program-sdk-zig/archive/refs/tags/v0.13.1.tar.gz",
2824
.hash = "122030336f1257e3c0aa64243f5243f554b903c6b9ef3a91d48bfbe896c0c7d9b13b",

transfer-lamports/zig/build.zig

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
const std = @import("std");
22
const solana = @import("solana-program-sdk");
3-
const base58 = @import("base58");
43

54
pub fn build(b: *std.Build) !void {
65
const target = b.resolveTargetQuery(solana.sbf_target);

transfer-lamports/zig/build.zig.zon

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,10 +19,6 @@
1919
.url = "https://github.com/joncinque/base58-zig/archive/refs/tags/v0.13.3.tar.gz",
2020
.hash = "1220fd067bf167b9062cc29ccf715ff97643c2d3f8958beea863b6036876bb71bcb8",
2121
},
22-
.clap = .{
23-
.url = "https://github.com/Hejsil/zig-clap/archive/refs/tags/0.9.1.tar.gz",
24-
.hash = "122062d301a203d003547b414237229b09a7980095061697349f8bef41be9c30266b",
25-
},
2622
.@"solana-program-sdk" = .{
2723
.url = "https://github.com/joncinque/solana-program-sdk-zig/archive/refs/tags/v0.13.1.tar.gz",
2824
.hash = "122030336f1257e3c0aa64243f5243f554b903c6b9ef3a91d48bfbe896c0c7d9b13b",

0 commit comments

Comments
 (0)