Skip to main content

Module lens

Module lens 

Source
Expand description

Cross-program account lenses – read foreign fields by offset.

When Program A wants to read a field from Program B’s account, every existing framework requires importing Program B’s full type definition at compile time. This creates tight coupling between programs.

Hopper lenses solve this: read specific fields from foreign account data by byte offset and type, no compile-time dependency required. This enables composability patterns that were previously impossible without shared crate dependencies.

§Safety

Lenses bypass type-level layout guarantees. The caller must know the correct offset and type for the target field. Incorrect offsets will read garbage data (but never cause UB, since all reads go through bounds-checked accessors).

§Usage

use hopper_native::lens;

// Read a 32-byte address at offset 10 from a foreign program's account
// (skip 10-byte Hopper header: disc + version + layout_id).
let authority = lens::read_address(oracle_account, 10)?;

// Read a u64 price at offset 42.
let price = lens::read_le_u64(oracle_account, 42)?;

// Read a typed struct at an offset.
let data: &MyPodType = lens::read_field::<MyPodType>(account, 10)?;

Functions§

field_eq
Compare a field in account data against an expected value without copying.
read_address
Read a 32-byte address from account data.
read_bool
Read a boolean from account data (0 = false, nonzero = true).
read_bytes
Read a byte slice from account data.
read_field
Read a Projectable field from account data at the given byte offset.
read_field_pod
Read a Pod field from account data at the given byte offset.
read_le_u16
Read a little-endian u16 from account data.
read_le_u32
Read a little-endian u32 from account data.
read_le_u64
Read a little-endian u64 from account data.
read_u8
Read a single byte from account data.