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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
//! Guest-side development kit for Lyquid WASM modules.
//!
//! `lyquid` is the crate Lyquid authors import inside contract crates. It exposes the ABI tags,
//! call context, result and error types, LyteMemory layout constants, HTTP request shapes, guest
//! memory helpers, runtime host imports, and the `method` proc-macro facade. Those pieces line up
//! with the metadata decoded by `lyquor-wasm` and the entry points executed by `lyquor-vm`: method
//! macros generate network, instance, Ethereum-exported, and UPC entry points, while runtime
//! helpers perform host calls from inside the WASM guest.
//!
//! - [Litepaper](https://docs.lyquor.dev/docs/litepaper/arch)
//! - [Tutorial](https://docs.lyquor.dev/docs/tutorial/)
//! - [Lyquor Development Kit Documentation](https://docs.lyquor.dev/docs/ldk/)
pub use hashbrown;
pub use lyquor_primitives;
/// Method metadata categories and WASM custom-section encoding helpers.
/// Guest runtime support for memory, calls, oracle, UPC, and synchronization.
pub use prelude;
pub use alloy_sol_types;
/// HTTP request and response types exposed to Lyquid instance functions.
/// Stable hostnames and endpoint URLs exposed by the Lyquor runtime.
/// Guest pointer and memory-layout helpers.
use ;
use ;
use Error;
/// Lyquid method syntax (attribute macros).
///
/// Lyquid functions are defined with attribute macros. These methods may execute with network or
/// instance context, and can include UPC procedures. Define them as top-level functions in your
/// crate (module-level items, not inside `impl`/`trait` blocks). All functions are exported into a
/// single global namespace keyed by `<category>`, `<group>`, and `<method_name>`.
///
/// ### Constructor (optional)
/// The constructor is invoked atomically once at deployment (or code upgrade). It must be named
/// `constructor`, must not return a value, and must use `#[lyquid::method::network]` with no
/// attribute arguments.
///
/// ```ignore
/// #[lyquid::method::network]
/// fn constructor(ctx: &mut _, greeting: String) {
/// *ctx.network.greeting = greeting.into();
/// }
/// ```
///
/// ### Standard Methods
///
/// #### Network method (defaults to `main` group)
/// ```ignore
/// #[lyquid::method::network]
/// fn set_greeting(ctx: &mut _, greeting: String) -> LyquidResult<bool> {
/// *ctx.network.greeting = greeting.into();
/// Ok(true)
/// }
/// ```
///
/// #### Network method with explicit group
/// ```ignore
/// #[lyquid::method::network(group = node)]
/// fn join(ctx: &mut _, node: NodeID) -> LyquidResult<()> {
/// ctx.network.nodes.push(node);
/// Ok(())
/// }
/// ```
///
/// #### Instance method
/// ```ignore
/// #[lyquid::method::instance]
/// fn get_price(ctx: &_) -> LyquidResult<U256> {
/// Ok(*ctx.instance.price.read())
/// }
/// ```
///
/// #### Ethereum export creator guard
/// ```ignore
/// #[lyquid::method::network(export = eth, eth_guard = creator)]
/// fn setup(ctx: &mut _, value: U256) -> LyquidResult<()> {
/// *ctx.network.value = value;
/// Ok(())
/// }
/// ```
/// `eth_guard = creator` adds a `msg.sender == creator` check to the generated EVM transaction
/// wrapper for mutable `main` or `node` network exports. It does not protect calls that arrive
/// through other Lyquor paths; methods that require runtime authorization should still validate
/// the call context inside the method body.
///
/// ### UPC Methods
///
/// UPC expands into three instance functions using dedicated groups.
///
/// #### 1. UPC callee selection
/// ```ignore
/// #[lyquid::method::instance(upc(prepare))]
/// fn ping(ctx: &_) -> LyquidResult<Vec<NodeID>> {
/// Ok(Vec::from(&ctx.network.nodes[..]))
/// }
/// ```
///
/// #### 2. UPC request handler
/// ```ignore
/// #[lyquid::method::instance(upc(request))]
/// fn ping(ctx: &mut _, msg: String) -> LyquidResult<String> {
/// let from = ctx.from;
/// let id = ctx.id;
/// Ok(format!("pong: {msg} ({from:?}, {id})"))
/// }
/// ```
///
/// #### 3. UPC response aggregator
/// ```ignore
/// #[lyquid::method::instance(upc(response))]
/// fn ping(ctx: &_, response: LyquidResult<String>) -> LyquidResult<Option<String>> {
/// let resp = response?;
/// let from = ctx.from;
/// Ok(Some(format!("from {from:?}: {resp}")))
/// }
/// ```
///
/// ### Notes on Categories and Context
/// - `network` methods are deterministic and can read/write `network` state. They cannot perform
/// nondeterministic operations (UPC, timers, etc.).
/// - `instance` methods are event-driven and can read/write `instance` state and read `network`
/// state, but cannot mutate shared `network` state.
/// - The context parameter must be a reference like `ctx: &mut _` or `ctx: &_`. The concrete
/// context type depends on the method category (network/instance/UPC).
/// - UPC `response` functions are optional. If omitted, UPC behaves like a request-response call
/// that returns the first result.
/// Invocation context supplied by the host for one Lyquid method call.
/// Error type shared by generated Lyquid wrappers and host-facing runtime helpers.
/// Numeric ABI tag for Ethereum-compatible call payloads.
pub const ABI_ETH: u32 = 0x1;
/// Numeric ABI tag for native Lyquor call payloads.
pub const ABI_LYQUOR: u32 = 0x0;
/// Standard result type for Lyquid runtime and generated wrapper operations.
pub type LyquidResult<T> = ;
/// The starting address for stacks used by Lyquid.
pub const LYTESTACK_BASE: usize = 0x30000000;
/// The base address for LyteMemory.
/// Volatile's upper address is below next to this address. Everything from this base to
/// [NETWORK_MEMSIZE_IN_MB] and [INSTANCE_MEMSIZE_IN_MB] are persistent.
pub const LYTEMEM_BASE: usize = 0x80000000;
/// Total size of the memory in megabytes.
pub const LYTEMEM_SIZE_IN_MB: usize = 4096; // 4GB (WASM limit)
/// Size cap for the addressable LyteMemory that is globally viewed (and persisted) by all Lyquid instances.
pub const NETWORK_MEMSIZE_IN_MB: usize = 1024; // 1GB
/// Size cap for the addressable LyteMemory that is locally viewed (and persisted) for one Lyquid instance.
pub const INSTANCE_MEMSIZE_IN_MB: usize = 1024; // 1GB
/// Size cap for the volatile memory that can be used by each function call.
pub const VOLATILE_MEMSIZE_IN_MB: usize = 1024; // 1GB
/// Prefix bytes used for varaiable catalog in versioned state.
pub const VAR_CATALOG_PREFIX: = ;
/// Prefix bytes used for runtime-owned state in versioned state.
pub const INTERNAL_STATE_PREFIX: = ;
/// Prefix bytes used for lite pages in versioned state.
pub const LYTEMEM_PAGE_PREFIX: = ;
/// Exported guest initialization function name.
pub const WASM_INIT_FUNC: &str = "__lyquid_initialize";
/// Exported guest state-variable initialization function name.
pub const WASM_INIT_VAR_FUNC: &str = "__lyquid_initialize_state_variables";
/// Exported guest function name that marks Lyquid state uninitialized; the VM calls it to reset
/// network state when a new Lyquid image is loaded.
pub const WASM_NUKE_STATE_FUNC: &str = "__lyquid_nuke_state";
/// Exported guest volatile-memory allocation function name.
pub const WASM_VOLATILE_ALLOC_FUNC: &str = "__lyquid_volatile_alloc";
/// Exported guest volatile-memory deallocation function name.
pub const WASM_VOLATILE_DEALLOC_FUNC: &str = "__lyquid_volatile_dealloc";
/// WASM global name for the guest stack pointer.
pub const WASM_STACK_POINTER: &str = "__stack_pointer";
/// Prefix for exported network method entry points.
pub const WASM_NETWORK_METHOD_PREFIX: &str = "__lyquid_method_network";
/// Prefix for exported instance method entry points.
pub const WASM_INSTANCE_METHOD_PREFIX: &str = "__lyquid_method_instance";
/// The maximum size of a stack per call.
pub const WASM_CALLSTACK_LIMIT: u32 = 0x100000; // 1M
/// Default stack base address for Lyquid guest modules.
pub const WASM_DEFAULT_STACK_BASE: u32 = 0x100000;
/// Ethereum export metadata for a Lyquid method.
/// Method metadata decoded from Lyquid WASM custom sections.