Skip to main content

percli_program/instructions/
update_matcher.rs

1use anchor_lang::prelude::*;
2
3use crate::error::PercolatorError;
4use crate::instructions::events;
5use crate::state::{header_from_account_data, write_header, MARKET_ACCOUNT_SIZE};
6
7#[derive(Accounts)]
8pub struct UpdateMatcher<'info> {
9    pub authority: Signer<'info>,
10
11    /// CHECK: Validated via owner, discriminator, and size.
12    #[account(
13        mut,
14        owner = crate::ID @ PercolatorError::AccountNotFound,
15        constraint = market.data_len() >= MARKET_ACCOUNT_SIZE @ PercolatorError::AccountNotFound,
16    )]
17    pub market: UncheckedAccount<'info>,
18}
19
20pub fn handler(ctx: Context<UpdateMatcher>, new_matcher: Pubkey) -> Result<()> {
21    let market = &ctx.accounts.market;
22    let mut data = market.try_borrow_mut_data()?;
23
24    require!(
25        &data[0..8] == b"percmrkt",
26        PercolatorError::AccountNotFound
27    );
28
29    let mut header = header_from_account_data(&data)?;
30    require!(
31        header.authority == ctx.accounts.authority.key(),
32        PercolatorError::Unauthorized
33    );
34
35    let old_matcher = header.matcher;
36    header.matcher = new_matcher;
37    write_header(&mut data, &header);
38
39    emit!(events::MatcherUpdated {
40        authority: ctx.accounts.authority.key(),
41        old_matcher,
42        new_matcher,
43    });
44
45    Ok(())
46}