hopper_core/accounts/
program_account.rs1use hopper_runtime::error::ProgramError;
7use hopper_runtime::{AccountView, Address};
8
9use crate::account::{FixedLayout, Pod, VerifiedAccount};
10use crate::check;
11
12pub struct ProgramAccount<'a, T: Pod + FixedLayout> {
18 view: &'a AccountView,
19 _marker: core::marker::PhantomData<T>,
20}
21
22impl<'a, T: Pod + FixedLayout> ProgramAccount<'a, T> {
23 #[inline]
25 pub fn from_account(
26 account: &'a AccountView,
27 expected_owner: &Address,
28 ) -> Result<Self, ProgramError> {
29 check::check_owner(account, expected_owner)?;
30 let data = account.try_borrow()?;
31 check::check_size(&data, core::mem::size_of::<T>())?;
32 Ok(Self {
33 view: account,
34 _marker: core::marker::PhantomData,
35 })
36 }
37
38 #[inline]
40 pub fn from_account_unchecked(account: &'a AccountView) -> Self {
41 Self {
42 view: account,
43 _marker: core::marker::PhantomData,
44 }
45 }
46
47 #[inline]
53 pub fn read(&self) -> Result<VerifiedAccount<'a, T>, ProgramError> {
54 let data = self.view.try_borrow()?;
55 VerifiedAccount::from_ref(data)
56 }
57
58 #[inline(always)]
60 pub fn address(&self) -> &Address {
61 self.view.address()
62 }
63
64 #[inline(always)]
70 pub unsafe fn owner(&self) -> &Address {
71 unsafe { self.view.owner() }
73 }
74
75 #[inline(always)]
77 pub fn to_account_view(&self) -> &'a AccountView {
78 self.view
79 }
80}