MCPcopy Create free account
hub / github.com/Bonfida/dex-v4 / process

Function process

program/src/processor/initialize_account.rs:87–149  ·  view source on GitHub ↗
(
    program_id: &Pubkey,
    accounts: &[AccountInfo],
    instruction_data: &[u8],
)

Source from the content-addressed store, hash-verified

85}
86
87pub(crate) fn process(
88 program_id: &Pubkey,
89 accounts: &[AccountInfo],
90 instruction_data: &[u8],
91) -> ProgramResult {
92 let accounts = Accounts::parse(program_id, accounts)?;
93
94 let Params { market, max_orders } =
95 try_from_bytes(instruction_data).map_err(|_| ProgramError::InvalidInstructionData)?;
96
97 let market_key_bytes = market.to_bytes();
98 let (user_account_key, user_account_nonce) = Pubkey::find_program_address(
99 &[&market_key_bytes, &accounts.user_owner.key.to_bytes()],
100 program_id,
101 );
102
103 if &user_account_key != accounts.user.key {
104 msg!("Provided an invalid user account for the specified market and owner");
105 return Err(ProgramError::InvalidArgument);
106 }
107
108 if max_orders == &0 {
109 msg!("The minimum number of orders an account should be able to hold is 1");
110 return Err(ProgramError::InvalidArgument);
111 }
112
113 // (USER_ACCOUNT_HEADER_LEN as u64) + max_orders * (Order::LEN as u64);
114 let space = max_orders
115 .checked_mul(Order::LEN as u64)
116 .and_then(|n| n.checked_add(USER_ACCOUNT_HEADER_LEN as u64))
117 .ok_or(DexError::NumericalOverflow)?;
118
119 let lamports = Rent::get()?.minimum_balance(space as usize);
120
121 let allocate_account = create_account(
122 accounts.fee_payer.key,
123 accounts.user.key,
124 lamports,
125 space,
126 program_id,
127 );
128
129 invoke_signed(
130 &allocate_account,
131 &[
132 accounts.system_program.clone(),
133 accounts.fee_payer.clone(),
134 accounts.user.clone(),
135 ],
136 &[&[
137 &market_key_bytes,
138 &accounts.user_owner.key.to_bytes(),
139 &[user_account_nonce],
140 ]],
141 )?;
142
143 let mut user_account_data = accounts.user.data.borrow_mut();
144 let u = UserAccount::from_buffer_unchecked(&mut user_account_data)?;

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected