-
Notifications
You must be signed in to change notification settings - Fork 514
Expand file tree
/
Copy pathmain.zig
More file actions
65 lines (51 loc) · 2.36 KB
/
main.zig
File metadata and controls
65 lines (51 loc) · 2.36 KB
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
const sol = @import("solana_program_sdk");
const sol_lib = @import("solana_program_library");
const std = @import("std");
const Rent = sol.rent.Rent;
const PublicKey = sol.public_key.PublicKey;
const Account = sol.account.Account;
pub const ProgramError = error{ InvalidInstructionData, InvalidAccountData, Unexpected };
pub const AddressInfo = struct {
name: [32]u8,
house_number: u8,
street: [64]u8,
city: [32]u8,
pub const SIZE = @sizeOf(AddressInfo);
pub fn new(name: [32]u8, house_number: u8, street: [64]u8, city: [32]u8) AddressInfo {
return AddressInfo{ .name = name, .house_number = house_number, .street = street, .city = city };
}
};
export fn entrypoint(input: [*]u8) u64 {
var context = sol.context.Context.load(input) catch return 1;
processInstruction(context.program_id, context.accounts[0..context.num_accounts], context.data) catch |err| return @intFromError(err);
return 0;
}
fn processInstruction(program_id: *PublicKey, accounts: []Account, data: []const u8) ProgramError!void {
if (data.len < AddressInfo.SIZE) return ProgramError.InvalidInstructionData;
if (accounts.len < 3) return ProgramError.InvalidAccountData;
const address_info: AddressInfo = std.mem.bytesToValue(AddressInfo, data[0..AddressInfo.SIZE]);
const address_info_account = accounts[0];
const payer = accounts[1];
const system_program = accounts[2];
if (address_info_account.dataLen() != 0) return ProgramError.InvalidAccountData;
if (!payer.isSigner()) return ProgramError.InvalidAccountData;
if (!PublicKey.equals(system_program.id(), sol_lib.system.id)) return ProgramError.InvalidAccountData;
const space = AddressInfo.SIZE;
const rent = try Rent.get();
const lamports = rent.getMinimumBalance(space);
sol_lib.system.createAccount(.{
.from = payer.info(),
.to = address_info_account.info(),
.lamports = lamports,
.space = space,
.owner_id = program_id.*,
}) catch |e| return switch (e) {
error.InvalidInstructionData => error.InvalidInstructionData,
error.InvalidAccountData => error.InvalidAccountData,
else => error.Unexpected,
};
const bytes = std.mem.asBytes(&address_info);
if (address_info_account.dataLen() < bytes.len)
return error.InvalidAccountData;
@memcpy(address_info_account.data()[0..bytes.len], bytes);
}