hopper-native 0.1.0

Hopper's sovereign raw backend for Solana. Zero-copy account access, direct syscall layer, CPI infrastructure, PDA helpers, and entrypoint glue. no_std, no_alloc, no external runtime dependencies.
Documentation
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
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
//! Alignment-safe wire types for zero-copy account data.
//!
//! Solana account data buffers have alignment 1. Casting a `*const u8`
//! to `*const u64` causes undefined behavior when the pointer is not
//! 8-byte aligned. Every framework that does zero-copy must solve this.
//!
//! Quasar solves it with `PodU64([u8; 8])` -- wrapping arithmetic by
//! default and implicit conversion. Hopper takes a different approach:
//!
//! - **Explicit endianness**: Types are named `LeU64` ("little-endian u64"),
//!   making the wire representation unambiguous at every call site.
//! - **Checked arithmetic by default**: `+`, `-`, `*` return `Option` via
//!   `checked_add` etc. This is safer than wrapping overflow silently
//!   (Quasar's default) and matches Rust's principled stance on UB.
//! - **`const fn` constructors**: `LeU64::new(42)` works in const context,
//!   enabling compile-time constants for discriminators, seeds, etc.
//! - **`Projectable`**: All wire types implement `Projectable`, so you can
//!   use `project::<LeU64>(account, offset, None)` to read them directly
//!   from account data without alignment issues.
//!
//! These types are the foundation for safe zero-copy account structs.
//! Any `#[repr(C)]` struct composed entirely of wire types + `[u8; N]`
//! arrays is alignment-1-safe and can be projected from account data.

use crate::project::Projectable;

// ---- Macro to generate integer wire types ----------------------------

macro_rules! le_integer {
    (
        $(#[$meta:meta])*
        $name:ident, $native:ty, $size:expr, unsigned
    ) => {
        $(#[$meta])*
        #[repr(transparent)]
        #[derive(Clone, Copy, Default, Eq, PartialEq, Hash)]
        pub struct $name([u8; $size]);

        impl $name {
            /// Zero value.
            pub const ZERO: Self = Self([0; $size]);

            /// Maximum representable value.
            pub const MAX: Self = Self(<$native>::MAX.to_le_bytes());

            /// Construct from a native integer (const-safe).
            #[inline(always)]
            pub const fn new(v: $native) -> Self {
                Self(v.to_le_bytes())
            }

            /// Read the native integer value.
            #[inline(always)]
            pub const fn get(self) -> $native {
                <$native>::from_le_bytes(self.0)
            }

            /// Raw little-endian bytes.
            #[inline(always)]
            pub const fn to_le_bytes(self) -> [u8; $size] {
                self.0
            }

            /// Construct from raw little-endian bytes.
            #[inline(always)]
            pub const fn from_le_bytes(bytes: [u8; $size]) -> Self {
                Self(bytes)
            }

            /// Checked addition. Returns `None` on overflow.
            #[inline(always)]
            pub const fn checked_add(self, rhs: Self) -> Option<Self> {
                match self.get().checked_add(rhs.get()) {
                    Some(v) => Some(Self::new(v)),
                    None => None,
                }
            }

            /// Checked subtraction. Returns `None` on underflow.
            #[inline(always)]
            pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
                match self.get().checked_sub(rhs.get()) {
                    Some(v) => Some(Self::new(v)),
                    None => None,
                }
            }

            /// Checked multiplication. Returns `None` on overflow.
            #[inline(always)]
            pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
                match self.get().checked_mul(rhs.get()) {
                    Some(v) => Some(Self::new(v)),
                    None => None,
                }
            }

            /// Checked division. Returns `None` on divide-by-zero.
            #[inline(always)]
            pub const fn checked_div(self, rhs: Self) -> Option<Self> {
                match self.get().checked_div(rhs.get()) {
                    Some(v) => Some(Self::new(v)),
                    None => None,
                }
            }

            /// Saturating addition (clamps at MAX instead of wrapping).
            #[inline(always)]
            pub const fn saturating_add(self, rhs: Self) -> Self {
                Self::new(self.get().saturating_add(rhs.get()))
            }

            /// Saturating subtraction (clamps at 0 instead of wrapping).
            #[inline(always)]
            pub const fn saturating_sub(self, rhs: Self) -> Self {
                Self::new(self.get().saturating_sub(rhs.get()))
            }

            /// Wrapping addition (use explicitly when wrapping is intended).
            #[inline(always)]
            pub const fn wrapping_add(self, rhs: Self) -> Self {
                Self::new(self.get().wrapping_add(rhs.get()))
            }

            /// Wrapping subtraction.
            #[inline(always)]
            pub const fn wrapping_sub(self, rhs: Self) -> Self {
                Self::new(self.get().wrapping_sub(rhs.get()))
            }

            /// Whether the value is zero.
            #[inline(always)]
            pub const fn is_zero(self) -> bool {
                self.get() == 0
            }
        }

        impl From<$native> for $name {
            #[inline(always)]
            fn from(v: $native) -> Self { Self::new(v) }
        }

        impl From<$name> for $native {
            #[inline(always)]
            fn from(v: $name) -> Self { v.get() }
        }

        impl PartialOrd for $name {
            #[inline(always)]
            fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
                Some(self.cmp(other))
            }
        }

        impl Ord for $name {
            #[inline(always)]
            fn cmp(&self, other: &Self) -> core::cmp::Ordering {
                self.get().cmp(&other.get())
            }
        }

        impl core::fmt::Debug for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "{}({})", stringify!($name), self.get())
            }
        }

        impl core::fmt::Display for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "{}", self.get())
            }
        }

        // SAFETY: $name is #[repr(transparent)] over [u8; N].
        // All bit patterns are valid (no padding, no alignment requirement).
        unsafe impl Projectable for $name {}

        $crate::__wire_arith_ops!($name, $native);
    };

    // Signed variant -- same API but with signed native type.
    (
        $(#[$meta:meta])*
        $name:ident, $native:ty, $size:expr, signed
    ) => {
        $(#[$meta])*
        #[repr(transparent)]
        #[derive(Clone, Copy, Default, Eq, PartialEq, Hash)]
        pub struct $name([u8; $size]);

        impl $name {
            /// Zero value.
            pub const ZERO: Self = Self([0; $size]);

            /// Maximum representable value.
            pub const MAX: Self = Self(<$native>::MAX.to_le_bytes());

            /// Minimum representable value.
            pub const MIN: Self = Self(<$native>::MIN.to_le_bytes());

            /// Construct from a native integer (const-safe).
            #[inline(always)]
            pub const fn new(v: $native) -> Self {
                Self(v.to_le_bytes())
            }

            /// Read the native integer value.
            #[inline(always)]
            pub const fn get(self) -> $native {
                <$native>::from_le_bytes(self.0)
            }

            /// Raw little-endian bytes.
            #[inline(always)]
            pub const fn to_le_bytes(self) -> [u8; $size] {
                self.0
            }

            /// Construct from raw little-endian bytes.
            #[inline(always)]
            pub const fn from_le_bytes(bytes: [u8; $size]) -> Self {
                Self(bytes)
            }

            /// Checked addition.
            #[inline(always)]
            pub const fn checked_add(self, rhs: Self) -> Option<Self> {
                match self.get().checked_add(rhs.get()) {
                    Some(v) => Some(Self::new(v)),
                    None => None,
                }
            }

            /// Checked subtraction.
            #[inline(always)]
            pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
                match self.get().checked_sub(rhs.get()) {
                    Some(v) => Some(Self::new(v)),
                    None => None,
                }
            }

            /// Checked multiplication.
            #[inline(always)]
            pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
                match self.get().checked_mul(rhs.get()) {
                    Some(v) => Some(Self::new(v)),
                    None => None,
                }
            }

            /// Checked division.
            #[inline(always)]
            pub const fn checked_div(self, rhs: Self) -> Option<Self> {
                match self.get().checked_div(rhs.get()) {
                    Some(v) => Some(Self::new(v)),
                    None => None,
                }
            }

            /// Saturating addition.
            #[inline(always)]
            pub const fn saturating_add(self, rhs: Self) -> Self {
                Self::new(self.get().saturating_add(rhs.get()))
            }

            /// Saturating subtraction.
            #[inline(always)]
            pub const fn saturating_sub(self, rhs: Self) -> Self {
                Self::new(self.get().saturating_sub(rhs.get()))
            }

            /// Whether the value is zero.
            #[inline(always)]
            pub const fn is_zero(self) -> bool {
                self.get() == 0
            }

            /// Whether the value is negative.
            #[inline(always)]
            pub const fn is_negative(self) -> bool {
                self.get() < 0
            }

            /// Absolute value (wraps on MIN).
            #[inline(always)]
            pub const fn abs(self) -> Self {
                Self::new(self.get().wrapping_abs())
            }
        }

        impl From<$native> for $name {
            #[inline(always)]
            fn from(v: $native) -> Self { Self::new(v) }
        }

        impl From<$name> for $native {
            #[inline(always)]
            fn from(v: $name) -> Self { v.get() }
        }

        impl PartialOrd for $name {
            #[inline(always)]
            fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
                Some(self.cmp(other))
            }
        }

        impl Ord for $name {
            #[inline(always)]
            fn cmp(&self, other: &Self) -> core::cmp::Ordering {
                self.get().cmp(&other.get())
            }
        }

        impl core::fmt::Debug for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "{}({})", stringify!($name), self.get())
            }
        }

        impl core::fmt::Display for $name {
            fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
                write!(f, "{}", self.get())
            }
        }

        unsafe impl Projectable for $name {}

        $crate::__wire_arith_ops!($name, $native);
    };
}

/// Internal: emit arithmetic operator impls for a wire integer type.
///
/// Mirrors Rust's native integer behavior: panic on overflow in debug,
/// wrap in release. Programs that need explicit semantics should use the
/// `checked_*`, `saturating_*`, or `wrapping_*` inherent methods.
#[doc(hidden)]
#[macro_export]
macro_rules! __wire_arith_ops {
    ($name:ident, $native:ty) => {
        impl core::ops::Add for $name {
            type Output = Self;
            #[inline(always)]
            fn add(self, rhs: Self) -> Self {
                Self::new(self.get() + rhs.get())
            }
        }
        impl core::ops::Sub for $name {
            type Output = Self;
            #[inline(always)]
            fn sub(self, rhs: Self) -> Self {
                Self::new(self.get() - rhs.get())
            }
        }
        impl core::ops::Mul for $name {
            type Output = Self;
            #[inline(always)]
            fn mul(self, rhs: Self) -> Self {
                Self::new(self.get() * rhs.get())
            }
        }
        impl core::ops::Div for $name {
            type Output = Self;
            #[inline(always)]
            fn div(self, rhs: Self) -> Self {
                Self::new(self.get() / rhs.get())
            }
        }
        impl core::ops::Rem for $name {
            type Output = Self;
            #[inline(always)]
            fn rem(self, rhs: Self) -> Self {
                Self::new(self.get() % rhs.get())
            }
        }
        impl core::ops::Add<$native> for $name {
            type Output = Self;
            #[inline(always)]
            fn add(self, rhs: $native) -> Self {
                Self::new(self.get() + rhs)
            }
        }
        impl core::ops::Sub<$native> for $name {
            type Output = Self;
            #[inline(always)]
            fn sub(self, rhs: $native) -> Self {
                Self::new(self.get() - rhs)
            }
        }
        impl core::ops::Mul<$native> for $name {
            type Output = Self;
            #[inline(always)]
            fn mul(self, rhs: $native) -> Self {
                Self::new(self.get() * rhs)
            }
        }
        impl core::ops::Div<$native> for $name {
            type Output = Self;
            #[inline(always)]
            fn div(self, rhs: $native) -> Self {
                Self::new(self.get() / rhs)
            }
        }
        impl core::ops::Rem<$native> for $name {
            type Output = Self;
            #[inline(always)]
            fn rem(self, rhs: $native) -> Self {
                Self::new(self.get() % rhs)
            }
        }
        impl core::ops::AddAssign for $name {
            #[inline(always)]
            fn add_assign(&mut self, rhs: Self) {
                *self = *self + rhs;
            }
        }
        impl core::ops::SubAssign for $name {
            #[inline(always)]
            fn sub_assign(&mut self, rhs: Self) {
                *self = *self - rhs;
            }
        }
        impl core::ops::MulAssign for $name {
            #[inline(always)]
            fn mul_assign(&mut self, rhs: Self) {
                *self = *self * rhs;
            }
        }
        impl core::ops::DivAssign for $name {
            #[inline(always)]
            fn div_assign(&mut self, rhs: Self) {
                *self = *self / rhs;
            }
        }
        impl core::ops::RemAssign for $name {
            #[inline(always)]
            fn rem_assign(&mut self, rhs: Self) {
                *self = *self % rhs;
            }
        }
        impl core::ops::AddAssign<$native> for $name {
            #[inline(always)]
            fn add_assign(&mut self, rhs: $native) {
                *self = *self + rhs;
            }
        }
        impl core::ops::SubAssign<$native> for $name {
            #[inline(always)]
            fn sub_assign(&mut self, rhs: $native) {
                *self = *self - rhs;
            }
        }
        impl core::ops::MulAssign<$native> for $name {
            #[inline(always)]
            fn mul_assign(&mut self, rhs: $native) {
                *self = *self * rhs;
            }
        }
        impl core::ops::DivAssign<$native> for $name {
            #[inline(always)]
            fn div_assign(&mut self, rhs: $native) {
                *self = *self / rhs;
            }
        }
        impl core::ops::RemAssign<$native> for $name {
            #[inline(always)]
            fn rem_assign(&mut self, rhs: $native) {
                *self = *self % rhs;
            }
        }
        impl PartialEq<$native> for $name {
            #[inline(always)]
            fn eq(&self, other: &$native) -> bool {
                self.get() == *other
            }
        }
        impl PartialOrd<$native> for $name {
            #[inline(always)]
            fn partial_cmp(&self, other: &$native) -> Option<core::cmp::Ordering> {
                Some(self.get().cmp(other))
            }
        }
    };
}

// ---- Unsigned wire types ---------------------------------------------

le_integer! {
    /// 64-bit unsigned little-endian integer. Alignment 1.
    ///
    /// The workhorse type for token amounts, lamport balances, timestamps,
    /// and most on-chain numeric fields. Use this instead of `u64` in any
    /// `#[repr(C)]` struct that will be projected from account data.
    LeU64, u64, 8, unsigned
}

le_integer! {
    /// 32-bit unsigned little-endian integer. Alignment 1.
    LeU32, u32, 4, unsigned
}

le_integer! {
    /// 16-bit unsigned little-endian integer. Alignment 1.
    LeU16, u16, 2, unsigned
}

// ---- Signed wire types -----------------------------------------------

le_integer! {
    /// 64-bit signed little-endian integer. Alignment 1.
    ///
    /// Used for timestamps (unix_timestamp is i64), deltas, and any
    /// signed arithmetic in account data.
    LeI64, i64, 8, signed
}

le_integer! {
    /// 32-bit signed little-endian integer. Alignment 1.
    LeI32, i32, 4, signed
}

le_integer! {
    /// 16-bit signed little-endian integer. Alignment 1.
    LeI16, i16, 2, signed
}

// ---- LeBool ----------------------------------------------------------

/// Boolean wire type. Alignment 1.
///
/// Stored as a single byte: 0 = false, nonzero = true.
/// `is_valid()` returns true only for 0 or 1, catching
/// corrupted data that other frameworks would silently accept.
#[repr(transparent)]
#[derive(Clone, Copy, Default, Eq, PartialEq, Hash)]
pub struct LeBool(u8);

impl LeBool {
    /// Canonical true value.
    pub const TRUE: Self = Self(1);

    /// Canonical false value.
    pub const FALSE: Self = Self(0);

    /// Construct from a Rust bool.
    #[inline(always)]
    pub const fn new(v: bool) -> Self {
        Self(v as u8)
    }

    /// Read as a Rust bool (0 = false, anything else = true).
    #[inline(always)]
    pub const fn get(self) -> bool {
        self.0 != 0
    }

    /// Raw byte value.
    #[inline(always)]
    pub const fn raw(self) -> u8 {
        self.0
    }

    /// Whether the byte is strictly 0 or 1 (canonical representation).
    ///
    /// Non-canonical values (2..=255) are technically "true" but may
    /// indicate data corruption or an incompatible writer.
    #[inline(always)]
    pub const fn is_canonical(self) -> bool {
        self.0 == 0 || self.0 == 1
    }
}

impl From<bool> for LeBool {
    #[inline(always)]
    fn from(v: bool) -> Self {
        Self::new(v)
    }
}

impl From<LeBool> for bool {
    #[inline(always)]
    fn from(v: LeBool) -> Self {
        v.get()
    }
}

impl core::fmt::Debug for LeBool {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "LeBool({})", self.get())
    }
}

impl core::fmt::Display for LeBool {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.get())
    }
}

// SAFETY: LeBool is #[repr(transparent)] over u8. All bit patterns valid.
unsafe impl Projectable for LeBool {}

// ---- LeU128 ----------------------------------------------------------

/// 128-bit unsigned little-endian integer. Alignment 1.
///
/// Useful for large amounts (e.g., total supply tracking) where u64
/// would overflow. Stored as 16 bytes in account data.
#[repr(transparent)]
#[derive(Clone, Copy, Default, Eq, PartialEq, Hash)]
pub struct LeU128([u8; 16]);

impl LeU128 {
    pub const ZERO: Self = Self([0; 16]);
    pub const MAX: Self = Self(u128::MAX.to_le_bytes());

    #[inline(always)]
    pub const fn new(v: u128) -> Self {
        Self(v.to_le_bytes())
    }

    #[inline(always)]
    pub const fn get(self) -> u128 {
        u128::from_le_bytes(self.0)
    }

    #[inline(always)]
    pub const fn to_le_bytes(self) -> [u8; 16] {
        self.0
    }

    #[inline(always)]
    pub const fn checked_add(self, rhs: Self) -> Option<Self> {
        match self.get().checked_add(rhs.get()) {
            Some(v) => Some(Self::new(v)),
            None => None,
        }
    }

    #[inline(always)]
    pub const fn checked_sub(self, rhs: Self) -> Option<Self> {
        match self.get().checked_sub(rhs.get()) {
            Some(v) => Some(Self::new(v)),
            None => None,
        }
    }

    #[inline(always)]
    pub const fn checked_mul(self, rhs: Self) -> Option<Self> {
        match self.get().checked_mul(rhs.get()) {
            Some(v) => Some(Self::new(v)),
            None => None,
        }
    }

    #[inline(always)]
    pub const fn saturating_add(self, rhs: Self) -> Self {
        Self::new(self.get().saturating_add(rhs.get()))
    }

    #[inline(always)]
    pub const fn saturating_sub(self, rhs: Self) -> Self {
        Self::new(self.get().saturating_sub(rhs.get()))
    }

    #[inline(always)]
    pub const fn is_zero(self) -> bool {
        self.get() == 0
    }
}

impl From<u128> for LeU128 {
    #[inline(always)]
    fn from(v: u128) -> Self {
        Self::new(v)
    }
}

impl From<LeU128> for u128 {
    #[inline(always)]
    fn from(v: LeU128) -> Self {
        v.get()
    }
}

impl PartialOrd for LeU128 {
    #[inline(always)]
    fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

impl Ord for LeU128 {
    #[inline(always)]
    fn cmp(&self, other: &Self) -> core::cmp::Ordering {
        self.get().cmp(&other.get())
    }
}

impl core::fmt::Debug for LeU128 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "LeU128({})", self.get())
    }
}

impl core::fmt::Display for LeU128 {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        write!(f, "{}", self.get())
    }
}

unsafe impl Projectable for LeU128 {}

__wire_arith_ops!(LeU128, u128);