-
Notifications
You must be signed in to change notification settings - Fork 5
/
account.zig
executable file
·102 lines (87 loc) · 3.25 KB
/
account.zig
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const std = @import("std");
const sol = @import("sol.zig");
pub const Account = struct {
/// A Solana account sliced from what is provided as inputs to the BPF virtual machine.
pub const Data = extern struct {
duplicate_index: u8,
is_signer: bool,
is_writable: bool,
is_executable: bool,
_: [4]u8,
id: sol.PublicKey,
owner_id: sol.PublicKey,
lamports: u64,
data_len: usize,
comptime {
std.debug.assert(@offsetOf(Account.Data, "duplicate_index") == 0);
std.debug.assert(@offsetOf(Account.Data, "is_signer") == 0 + 1);
std.debug.assert(@offsetOf(Account.Data, "is_writable") == 0 + 1 + 1);
std.debug.assert(@offsetOf(Account.Data, "is_executable") == 0 + 1 + 1 + 1);
std.debug.assert(@offsetOf(Account.Data, "_") == 0 + 1 + 1 + 1 + 1);
std.debug.assert(@offsetOf(Account.Data, "id") == 0 + 1 + 1 + 1 + 1 + 4);
std.debug.assert(@offsetOf(Account.Data, "owner_id") == 0 + 1 + 1 + 1 + 1 + 4 + 32);
std.debug.assert(@offsetOf(Account.Data, "lamports") == 0 + 1 + 1 + 1 + 1 + 4 + 32 + 32);
std.debug.assert(@offsetOf(Account.Data, "data_len") == 0 + 1 + 1 + 1 + 1 + 4 + 32 + 32 + 8);
std.debug.assert(@sizeOf(Account.Data) == 1 + 1 + 1 + 1 + 4 + 32 + 32 + 8 + 8);
}
};
/// Metadata representing a Solana acconut.
pub const Param = extern struct {
id: *const sol.PublicKey,
is_writable: bool,
is_signer: bool,
};
pub const Info = extern struct {
id: *const sol.PublicKey,
lamports: *u64,
data_len: u64,
data: [*]u8,
owner_id: *const sol.PublicKey,
rent_epoch: u64,
is_signer: bool,
is_writable: bool,
is_executable: bool,
};
ptr: *Account.Data,
len: usize,
pub fn id(self: Account) sol.PublicKey {
return self.ptr.id;
}
pub fn lamports(self: Account) *u64 {
return &self.ptr.lamports;
}
pub fn ownerId(self: Account) sol.PublicKey {
return self.ptr.owner_id;
}
pub fn data(self: Account) []u8 {
const data_ptr = @intToPtr([*]u8, @ptrToInt(self.ptr)) + @sizeOf(Account.Data);
return data_ptr[0..self.ptr.data_len];
}
pub fn isWritable(self: Account) bool {
return self.ptr.is_writable;
}
pub fn isExecutable(self: Account) bool {
return self.ptr.is_executable;
}
pub fn isSigner(self: Account) bool {
return self.ptr.is_signer;
}
pub fn dataLen(self: Account) usize {
return self.ptr.data_len;
}
pub fn info(self: Account) Account.Info {
const data_ptr = @intToPtr([*]u8, @ptrToInt(self.ptr)) + @sizeOf(Account.Data);
const rent_epoch = @intToPtr(*u64, @ptrToInt(self.ptr) + self.len - @sizeOf(u64));
return .{
.id = &self.ptr.id,
.lamports = &self.ptr.lamports,
.data_len = self.ptr.data_len,
.data = data_ptr,
.owner_id = &self.ptr.owner_id,
.rent_epoch = rent_epoch.*,
.is_signer = self.ptr.is_signer,
.is_writable = self.ptr.is_writable,
.is_executable = self.ptr.is_executable,
};
}
};