-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsleet_hello.zig
More file actions
77 lines (66 loc) · 2.28 KB
/
sleet_hello.zig
File metadata and controls
77 lines (66 loc) · 2.28 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
66
67
68
69
70
71
72
73
74
75
76
77
const std = @import("std");
// Using WASM allocator for memory management
var allocator = std.heap.wasm_allocator;
// Import NEAR runtime functions
extern fn input(register_id: u64) void;
extern fn read_register(register_id: u64, ptr: u64) void;
extern fn register_len(register_id: u64) u64;
extern fn value_return(value_len: u64, value_ptr: u64) void;
extern fn log_utf8(len: u64, ptr: u64) void;
extern fn storage_write(key_len: u64, key_ptr: u64, value_len: u64, value_ptr: u64, register_id: u64) u64;
extern fn storage_read(key_len: u64, key_ptr: u64, register_id: u64) u64;
const SCRATCH_REGISTER = 0xffffffff;
const GREETING_KEY = "greeting";
const DEFAULT_GREETING = "Hello from Sleet!";
// Helper functions
fn log(str: []const u8) void {
log_utf8(str.len, @intFromPtr(str.ptr));
}
fn valueReturn(value: []const u8) void {
value_return(value.len, @intFromPtr(value.ptr));
}
fn readRegisterAlloc(register_id: u64) []const u8 {
const len: usize = @truncate(register_len(register_id));
const bytes = allocator.alloc(u8, len + 1) catch {
log("Failed to allocate memory");
return "";
};
read_register(register_id, @intFromPtr(bytes.ptr));
return bytes[0..len];
}
fn readInputAlloc() []const u8 {
input(SCRATCH_REGISTER);
return readRegisterAlloc(SCRATCH_REGISTER);
}
fn readStorageAlloc(key: []const u8) ?[]const u8 {
const res = storage_read(key.len, @intFromPtr(key.ptr), SCRATCH_REGISTER);
return switch (res) {
0 => null,
1 => readRegisterAlloc(SCRATCH_REGISTER),
else => null,
};
}
fn storageWrite(key: []const u8, value: []const u8) bool {
const res = storage_write(key.len, @intFromPtr(key.ptr), value.len, @intFromPtr(value.ptr), SCRATCH_REGISTER);
return switch (res) {
0 => false,
1 => true,
else => false,
};
}
// Contract methods
export fn get_greeting() void {
const greeting = readStorageAlloc(GREETING_KEY) orelse DEFAULT_GREETING;
log(greeting);
valueReturn(greeting);
}
export fn set_greeting() void {
const new_greeting = readInputAlloc();
if (new_greeting.len == 0) {
log("Empty greeting not allowed");
return;
}
_ = storageWrite(GREETING_KEY, new_greeting);
log("Greeting updated successfully");
valueReturn(new_greeting);
}