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
use std::{
    cell::{Ref, RefMut},
    ops::Deref,
};

use phoenix::program::{
    assert_with_msg, checkers::TokenAccountInfo, get_discriminant, MarketHeader,
};
use solana_program::{account_info::AccountInfo, msg, program_error::ProgramError, pubkey::Pubkey};
use spl_associated_token_account::get_associated_token_address;

use crate::{get_seat_manager_seeds, seat_manager::SeatManager};

pub struct MarketAccount<'a, 'info> {
    pub account: &'a AccountInfo<'info>,
}

impl<'a, 'info> MarketAccount<'a, 'info> {
    pub fn new_with_checked_discriminant(
        account: &'a AccountInfo<'info>,
    ) -> Result<Self, ProgramError> {
        assert_with_msg(
            *account.owner == phoenix::id(),
            ProgramError::InvalidAccountData,
            "Market account must be owned by the Phoenix program",
        )?;

        assert_with_msg(
            !account.data_is_empty(),
            ProgramError::InvalidAccountData,
            "Market account must not be empty",
        )?;
        let data = account.try_borrow_data()?;
        assert_with_msg(
            u64::from_le_bytes(data[..8].try_into().map_err(|_| {
                msg!("Failed to deserialize u64");
                ProgramError::InvalidAccountData
            })?) == get_discriminant::<MarketHeader>()?,
            ProgramError::InvalidAccountData,
            "Market account discriminant mismatch",
        )?;
        Ok(Self { account })
    }

    pub fn new(account: &'a AccountInfo<'info>) -> Result<Self, ProgramError> {
        assert_with_msg(
            *account.owner == phoenix::id(),
            ProgramError::InvalidAccountData,
            "Market account must be owned by the Phoenix program",
        )?;

        assert_with_msg(
            !account.data_is_empty(),
            ProgramError::InvalidAccountData,
            "Market account must not be empty",
        )?;
        Ok(Self { account })
    }
}

impl<'a, 'info> Deref for MarketAccount<'a, 'info> {
    type Target = AccountInfo<'info>;

    fn deref(&self) -> &Self::Target {
        self.account
    }
}

pub struct SeatManagerAccount<'a, 'info> {
    pub account: &'a AccountInfo<'info>,
    pub seeds: Vec<Vec<u8>>,
}

impl<'a, 'info> SeatManagerAccount<'a, 'info> {
    pub fn new(account: &'a AccountInfo<'info>) -> Result<Self, ProgramError> {
        let data = account.try_borrow_data()?;
        let seat_manager = SeatManager::load(&data)?;
        let market = seat_manager.market;
        // Assert that the seat manager address is correct
        let seeds = get_seat_manager_seeds(&market, account.key, &crate::id())?;
        Ok(Self { account, seeds })
    }

    pub fn new_with_market(
        account: &'a AccountInfo<'info>,
        market: &Pubkey,
    ) -> Result<Self, ProgramError> {
        // Assert that the seat manager address is correct
        let seeds = get_seat_manager_seeds(market, account.key, &crate::id())?;
        if !account.data_is_empty() {
            let data = account.try_borrow_data()?;
            let seat_manager = SeatManager::load(&data)?;
            if seat_manager.market != *market {
                msg!("Seat manager does not belong to market");
                return Err(ProgramError::InvalidAccountData);
            }
        }
        Ok(Self { account, seeds })
    }

    pub fn load(&self) -> Result<Ref<'_, SeatManager>, ProgramError> {
        let data = self.account.try_borrow_data()?;
        Ok(Ref::map(data, |data| {
            return SeatManager::load(data).unwrap();
        }))
    }

    pub fn load_mut(&self) -> Result<RefMut<'_, SeatManager>, ProgramError> {
        let data = self.account.try_borrow_mut_data()?;
        Ok(RefMut::map(data, |data| {
            return SeatManager::load_mut(data).unwrap();
        }))
    }
}

impl<'a, 'info> Deref for SeatManagerAccount<'a, 'info> {
    type Target = AccountInfo<'info>;

    fn deref(&self) -> &Self::Target {
        self.account
    }
}

pub struct AssociatedTokenAccount<'a, 'info> {
    pub account: &'a AccountInfo<'info>,
    pub is_initialized: bool,
    pub has_expected_owner: bool,
}

impl<'a, 'info> AssociatedTokenAccount<'a, 'info> {
    pub fn new(
        account: &'a AccountInfo<'info>,
        mint: &Pubkey,
        owner: &Pubkey,
    ) -> Result<Self, ProgramError> {
        let is_initialized = !account.data_is_empty();
        let has_expected_owner = if is_initialized {
            // The owner key is found at offset 32 of the token account
            &account.try_borrow_data()?[32..64] == owner.as_ref()
        } else {
            // The owner will match if the account is empty as it will be initialized in this transaction
            true
        };
        assert_with_msg(
            get_associated_token_address(owner, mint) == *account.key,
            ProgramError::InvalidArgument,
            "Associated token account address is incorrect",
        )?;
        Ok(Self {
            account,
            is_initialized,
            has_expected_owner,
        })
    }
}

impl<'a, 'info> AsRef<AccountInfo<'info>> for AssociatedTokenAccount<'a, 'info> {
    fn as_ref(&self) -> &AccountInfo<'info> {
        self.account
    }
}

impl<'a, 'info> Deref for AssociatedTokenAccount<'a, 'info> {
    type Target = AccountInfo<'info>;

    fn deref(&self) -> &Self::Target {
        self.account
    }
}

pub struct BackupTokenAccount<'a, 'info> {
    pub account: &'a AccountInfo<'info>,
    pub is_supplied: bool,
}

impl<'a, 'info> BackupTokenAccount<'a, 'info> {
    pub fn new(
        account: &'a AccountInfo<'info>,
        mint: &Pubkey,
        owner: &Pubkey,
    ) -> Result<Self, ProgramError> {
        let is_supplied = *account.owner == spl_token::id();
        if is_supplied {
            TokenAccountInfo::new_with_owner(account, mint, owner)?;
        }
        Ok(Self {
            account,
            is_supplied,
        })
    }
}

impl<'a, 'info> AsRef<AccountInfo<'info>> for BackupTokenAccount<'a, 'info> {
    fn as_ref(&self) -> &AccountInfo<'info> {
        self.account
    }
}

impl<'a, 'info> Deref for BackupTokenAccount<'a, 'info> {
    type Target = AccountInfo<'info>;

    fn deref(&self) -> &Self::Target {
        self.account
    }
}