Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 10 additions & 4 deletions program-test/pubkey/main.zig
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
const sol = @import("solana_program_sdk");

export fn entrypoint(input: [*]u8) u64 {
const context = sol.Context.load(input) catch return 1;
sol.print("Hello zig program {s}", .{context.program_id});
return 0;
fn processInstruction(program_id: *sol.PublicKey, accounts: []sol.Account, data: []const u8) sol.ProgramError!void {
_ = accounts;
_ = data;
sol.print("Hello zig program {s}", .{program_id});
return;
}

// Declare the program entrypoint
comptime {
sol.entrypoint(processInstruction);
}
2 changes: 1 addition & 1 deletion program-test/tests/pubkey.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ fn program_test() -> ProgramTest {
#[tokio::test]
async fn call() {
let pt = program_test();
let mut context = pt.start_with_context().await;
let context = pt.start_with_context().await;
let blockhash = context.banks_client.get_latest_blockhash().await.unwrap();
let transaction = Transaction::new_signed_with_payer(
&[Instruction {
Expand Down
22 changes: 22 additions & 0 deletions src/entrypoint.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const PublicKey = @import("public_key.zig").PublicKey;
const Account = @import("account.zig").Account;
const ProgramError = @import("error.zig").ProgramError;
const Context = @import("context.zig").Context;

const processInstruction = fn (program_id: *PublicKey, accounts: []Account, data: []const u8) ProgramError!void;

pub fn declareEntrypoint(comptime process_instruction: processInstruction) void {
const S = struct {
pub export fn entrypoint(input: [*]u8) callconv(.C) u64 {
var context = Context.load(input) catch return 1;
process_instruction(context.program_id, context.accounts[0..context.num_accounts], context.data) catch |err| return @intFromError(err);
return 0;
}
};
_ = &S.entrypoint;
}

/// Helper macro-like function for simple entrypoint declaration
pub inline fn entrypoint(comptime process_instruction: processInstruction) void {
declareEntrypoint(process_instruction);
}
6 changes: 6 additions & 0 deletions src/error.zig
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
pub const ProgramError = error{
AlreadyInUse,
InvalidAccountType,
Uninitialized,
IncorrectSize,
};
2 changes: 2 additions & 0 deletions src/root.zig
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ pub usingnamespace @import("clock.zig");
pub usingnamespace @import("rent.zig");
pub usingnamespace @import("log.zig");
pub usingnamespace @import("hash.zig");
pub usingnamespace @import("error.zig");
pub usingnamespace @import("entrypoint.zig");

pub const blake3 = @import("blake3.zig");
//pub const system_program = @import("system_program.zig");
Expand Down