diff --git a/dex/src/instruction.rs b/dex/src/instruction.rs index f240b851..ba189cd9 100644 --- a/dex/src/instruction.rs +++ b/dex/src/instruction.rs @@ -4,7 +4,10 @@ use crate::matching::{OrderType, Side}; use bytemuck::cast; use serde::{Deserialize, Serialize}; use solana_program::{ + declare_id, + entrypoint::ProgramResult, instruction::{AccountMeta, Instruction}, + program_error::ProgramError, pubkey::Pubkey, sysvar::rent, }; @@ -19,6 +22,8 @@ use proptest::prelude::*; #[cfg(test)] use proptest_derive::Arbitrary; +declare_id!("9xQeWvG816bUx9EPjHmaT23yvVM2ZWbrrpZb9PusVFin"); + pub mod srm_token { use solana_program::declare_id; declare_id!("SRMuApVNdxXokk5GT7XD5cUUgXMBCoAz2LHeuAoKWRt"); @@ -603,6 +608,7 @@ pub fn initialize_market( vault_signer_nonce: u64, pc_dust_threshold: u64, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::InitializeMarket(InitializeMarketInstruction { coin_lot_size, pc_lot_size, @@ -685,6 +691,7 @@ pub fn new_order( limit: u16, max_native_pc_qty_including_fees: NonZeroU64, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::NewOrderV3(NewOrderInstructionV3 { side, limit_price, @@ -731,6 +738,7 @@ pub fn match_orders( pc_fee_receivable_account: &Pubkey, limit: u16, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::MatchOrders(limit).pack(); let accounts: Vec = vec![ AccountMeta::new(*market, false), @@ -757,6 +765,7 @@ pub fn consume_events( pc_fee_receivable_account: &Pubkey, limit: u16, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::ConsumeEvents(limit).pack(); let mut accounts: Vec = open_orders_accounts .iter() @@ -783,6 +792,7 @@ pub fn consume_events_permissioned( consume_events_authority: &Pubkey, limit: u16, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::ConsumeEventsPermissioned(limit).pack(); let mut accounts: Vec = open_orders_accounts .iter() @@ -811,6 +821,7 @@ pub fn cancel_order( side: Side, order_id: u128, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::CancelOrderV2(CancelOrderInstructionV2 { side, order_id }).pack(); let accounts: Vec = vec![ AccountMeta::new(*market, false), @@ -840,6 +851,7 @@ pub fn settle_funds( referrer_pc_wallet: Option<&Pubkey>, vault_signer: &Pubkey, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::SettleFunds.pack(); let mut accounts: Vec = vec![ AccountMeta::new(*market, false), @@ -872,6 +884,7 @@ pub fn cancel_order_by_client_order_id( event_queue: &Pubkey, client_order_id: u64, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::CancelOrderByClientIdV2(client_order_id).pack(); let accounts: Vec = vec![ AccountMeta::new(*market, false), @@ -893,6 +906,7 @@ pub fn disable_market( market: &Pubkey, disable_authority_key: &Pubkey, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::DisableMarket.pack(); let accounts: Vec = vec![ AccountMeta::new(*market, false), @@ -914,6 +928,7 @@ pub fn sweep_fees( vault_signer: &Pubkey, spl_token_program_id: &Pubkey, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::SweepFees.pack(); let accounts: Vec = vec![ AccountMeta::new(*market, false), @@ -937,6 +952,7 @@ pub fn close_open_orders( destination: &Pubkey, market: &Pubkey, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::CloseOpenOrders.pack(); let accounts: Vec = vec![ AccountMeta::new(*open_orders, false), @@ -958,6 +974,7 @@ pub fn init_open_orders( market: &Pubkey, market_authority: Option<&Pubkey>, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::InitOpenOrders.pack(); let mut accounts: Vec = vec![ AccountMeta::new(*open_orders, false), @@ -986,6 +1003,7 @@ pub fn prune( event_q: &Pubkey, limit: u16, ) -> Result { + check_program_account(program_id)?; let data = MarketInstruction::Prune(limit).pack(); let accounts: Vec = vec![ AccountMeta::new(*market, false), @@ -1210,3 +1228,10 @@ mod fuzzing { arbitrary_impl!(NewOrderInstructionV2, NewOrderInstructionU64); arbitrary_impl!(NewOrderInstructionV1, NewOrderInstructionU64); } + +fn check_program_account(program_id: &Pubkey) -> ProgramResult { + if program_id != &ID { + return Err(ProgramError::IncorrectProgramId); + } + Ok(()) +} diff --git a/dex/src/tests.rs b/dex/src/tests.rs index f265b46b..4637c9f3 100644 --- a/dex/src/tests.rs +++ b/dex/src/tests.rs @@ -164,7 +164,7 @@ fn new_spl_token_program<'bump>(bump: &'bump Bump) -> AccountInfo<'bump> { } fn setup_market<'bump, R: Rng>(rng: &mut R, bump: &'bump Bump) -> MarketAccounts<'bump> { - let program_id = random_pubkey(rng, bump); + let program_id = &crate::instruction::ID; let market = new_dex_owned_account(rng, size_of::(), program_id, bump); let bids = new_dex_owned_account(rng, 1 << 23, program_id, bump); let asks = new_dex_owned_account(rng, 1 << 23, program_id, bump); @@ -260,7 +260,7 @@ fn test_new_order() { let accounts = setup_market(&mut rng, &bump); - let dex_program_id = accounts.market.owner; + let dex_program_id = &crate::instruction::ID; let owner = new_sol_account(&mut rng, 1_000_000_000, &bump); let orders_account_buyer =