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
use borsh::{BorshDeserialize, BorshSerialize};
use solana_program::{
account_info::{next_account_info, AccountInfo},
entrypoint::ProgramResult,
msg,
program_error::ProgramError,
program_pack::Pack,
pubkey::Pubkey,
};
use crate::{
error::AoError,
state::{EventQueue, EventQueueHeader, MarketState, EVENT_QUEUE_HEADER_LEN},
utils::{check_account_key, check_account_owner, check_signer},
};
#[derive(BorshDeserialize, BorshSerialize, Clone)]
pub struct Params {
pub number_of_entries_to_consume: u64,
}
struct Accounts<'a, 'b: 'a> {
market: &'a AccountInfo<'b>,
event_queue: &'a AccountInfo<'b>,
authority: &'a AccountInfo<'b>,
reward_target: &'a AccountInfo<'b>,
msrm_token_account: &'a AccountInfo<'b>,
msrm_token_account_owner: &'a AccountInfo<'b>,
}
impl<'a, 'b: 'a> Accounts<'a, 'b> {
pub fn parse(
program_id: &Pubkey,
accounts: &'a [AccountInfo<'b>],
) -> Result<Self, ProgramError> {
let mut accounts_iter = accounts.iter();
let a = Self {
market: next_account_info(&mut accounts_iter)?,
event_queue: next_account_info(&mut accounts_iter)?,
authority: next_account_info(&mut accounts_iter)?,
reward_target: next_account_info(&mut accounts_iter)?,
msrm_token_account: next_account_info(&mut accounts_iter)?,
msrm_token_account_owner: next_account_info(&mut accounts_iter)?,
};
check_account_owner(a.market, program_id).unwrap();
check_account_owner(a.event_queue, program_id).unwrap();
check_signer(a.authority).unwrap();
check_signer(a.msrm_token_account_owner).unwrap();
#[cfg(not(feature = "permissionless-crank"))]
{
check_account_owner(a.msrm_token_account, &spl_token::ID).unwrap();
let token_account =
spl_token::state::Account::unpack(&a.msrm_token_account.data.borrow()).unwrap();
if &token_account.owner != a.msrm_token_account_owner.key {
msg!("Invalid token account owner");
return Err(ProgramError::InvalidArgument);
}
if token_account.mint != super::msrm_token::ID || token_account.amount == 0 {
msg!("Invalid token account provided");
return Err(ProgramError::InvalidArgument);
}
}
Ok(a)
}
}
pub(crate) fn process(
program_id: &Pubkey,
accounts: &[AccountInfo],
params: Params,
) -> ProgramResult {
let accounts = Accounts::parse(program_id, accounts)?;
let mut market_state = {
let mut market_data: &[u8] = &accounts.market.data.borrow();
MarketState::deserialize(&mut market_data)
.unwrap()
.check()?
};
check_account_key(accounts.event_queue, &market_state.event_queue).unwrap();
check_account_key(accounts.authority, &market_state.caller_authority).unwrap();
if &market_state.event_queue != accounts.event_queue.key {
msg!("Invalid event queue for current market");
return Err(ProgramError::InvalidArgument);
}
let header = {
let mut event_queue_data: &[u8] =
&accounts.event_queue.data.borrow()[0..EVENT_QUEUE_HEADER_LEN];
EventQueueHeader::deserialize(&mut event_queue_data).unwrap()
};
let mut event_queue = EventQueue::new_safe(
header,
accounts.event_queue,
market_state.callback_info_len as usize,
)?;
let capped_number_of_entries_consumed = std::cmp::min(
event_queue.header.count,
params.number_of_entries_to_consume,
);
let reward = (market_state.fee_budget * capped_number_of_entries_consumed)
.checked_div(event_queue.header.count)
.ok_or(AoError::NoOperations)
.unwrap();
market_state.fee_budget -= reward;
**accounts.market.try_borrow_mut_lamports().unwrap() = accounts.market.lamports() - reward;
**accounts.reward_target.try_borrow_mut_lamports().unwrap() =
accounts.reward_target.lamports() + reward;
let mut market_state_data: &mut [u8] = &mut accounts.market.data.borrow_mut();
market_state.serialize(&mut market_state_data).unwrap();
event_queue.pop_n(params.number_of_entries_to_consume);
let mut event_queue_data: &mut [u8] = &mut accounts.event_queue.data.borrow_mut();
event_queue.header.serialize(&mut event_queue_data).unwrap();
msg!(
"Number of events consumed: {:?}",
capped_number_of_entries_consumed
);
Ok(())
}