Skip to main content

hopper_native/
return_data.rs

1//! CPI return data retrieval and typed deserialization.
2//!
3//! The Solana runtime supports return data from CPI calls (up to 1024 bytes).
4//! No framework provides a typed wrapper that combines invoke + deserialize
5//! in one step. Hopper does.
6
7use crate::address::Address;
8use crate::error::ProgramError;
9use crate::project::Projectable;
10
11#[cfg(feature = "cpi")]
12use crate::instruction::{InstructionView, Signer};
13
14/// Maximum return data size (1 KiB), matching Solana runtime limit.
15pub const MAX_RETURN_DATA: usize = 1024;
16
17/// Return data from a previous CPI call.
18pub struct ReturnData {
19    /// Buffer holding the return data (stack-allocated).
20    buf: [u8; MAX_RETURN_DATA],
21    /// Actual length of the return data.
22    len: usize,
23    /// Program ID that set the return data.
24    program_id: Address,
25}
26
27impl ReturnData {
28    /// Get the return data bytes.
29    #[inline(always)]
30    pub fn data(&self) -> &[u8] {
31        &self.buf[..self.len]
32    }
33
34    /// Get the program that set the return data.
35    #[inline(always)]
36    pub fn program_id(&self) -> &Address {
37        &self.program_id
38    }
39
40    /// Length of the return data.
41    #[inline(always)]
42    pub fn len(&self) -> usize {
43        self.len
44    }
45
46    /// Whether the return data is empty.
47    #[inline(always)]
48    pub fn is_empty(&self) -> bool {
49        self.len == 0
50    }
51
52    /// Interpret the return data as a `Projectable` type.
53    ///
54    /// Returns `Err(AccountDataTooSmall)` if the return data is smaller
55    /// than `size_of::<T>()`.
56    #[inline]
57    pub fn as_type<T: Projectable>(&self) -> Result<&T, ProgramError> {
58        let size = core::mem::size_of::<T>();
59        if self.len < size {
60            return Err(ProgramError::AccountDataTooSmall);
61        }
62
63        let align = core::mem::align_of::<T>();
64        let ptr = self.buf.as_ptr();
65        if align > 1 && (ptr as usize) % align != 0 {
66            return Err(ProgramError::InvalidAccountData);
67        }
68
69        Ok(unsafe { &*(ptr as *const T) })
70    }
71
72    /// Read a u64 from the first 8 bytes of return data.
73    #[inline]
74    pub fn as_u64(&self) -> Result<u64, ProgramError> {
75        if self.len < 8 {
76            return Err(ProgramError::AccountDataTooSmall);
77        }
78        let mut bytes = [0u8; 8];
79        bytes.copy_from_slice(&self.buf[..8]);
80        Ok(u64::from_le_bytes(bytes))
81    }
82
83    /// Read a u32 from the first 4 bytes of return data.
84    #[inline]
85    pub fn as_u32(&self) -> Result<u32, ProgramError> {
86        if self.len < 4 {
87            return Err(ProgramError::AccountDataTooSmall);
88        }
89        let mut bytes = [0u8; 4];
90        bytes.copy_from_slice(&self.buf[..4]);
91        Ok(u32::from_le_bytes(bytes))
92    }
93}
94
95/// Retrieve return data from the most recent CPI call.
96///
97/// Returns `None` if no return data was set (length == 0).
98#[inline]
99pub fn get_return_data() -> Option<ReturnData> {
100    #[allow(unused_mut)]
101    let mut rd = ReturnData {
102        buf: [0u8; MAX_RETURN_DATA],
103        len: 0,
104        program_id: Address::default(),
105    };
106
107    #[cfg(target_os = "solana")]
108    {
109        let actual_len = unsafe {
110            crate::syscalls::sol_get_return_data(
111                rd.buf.as_mut_ptr(),
112                MAX_RETURN_DATA as u64,
113                rd.program_id.0.as_mut_ptr(),
114            )
115        };
116        rd.len = (actual_len as usize).min(MAX_RETURN_DATA);
117    }
118
119    #[cfg(not(target_os = "solana"))]
120    {
121        // Off-chain: no return data available.
122    }
123
124    if rd.len == 0 {
125        None
126    } else {
127        Some(rd)
128    }
129}
130
131/// Invoke a CPI and immediately read back typed return data.
132///
133/// Combines `invoke_signed` + `get_return_data` + `as_type::<T>()` into
134/// a single operation. This is the cleanest way to call a program that
135/// returns structured data.
136///
137/// # Example
138///
139/// ```ignore
140/// let oracle_price: &PriceData = invoke_and_read::<PriceData, 2>(
141///     &instruction,
142///     &[&oracle_program, &price_feed],
143///     &[],
144/// )?;
145/// ```
146#[cfg(feature = "cpi")]
147#[inline]
148pub fn invoke_and_read<'a, T: Projectable, const ACCOUNTS: usize>(
149    instruction: &InstructionView,
150    account_views: &[&crate::account_view::AccountView; ACCOUNTS],
151    signers_seeds: &[Signer],
152) -> Result<ReturnData, ProgramError> {
153    crate::cpi::invoke_signed::<ACCOUNTS>(instruction, account_views, signers_seeds)?;
154
155    get_return_data().ok_or(ProgramError::InvalidAccountData)
156}