Skip to content

Commit 851c533

Browse files
committed
solana: rename close fast market order processor fn
clean up instruction serde
1 parent 21d9efc commit 851c533

File tree

2 files changed

+22
-43
lines changed

2 files changed

+22
-43
lines changed

solana/programs/matching-engine/src/fallback/processor/close_fast_market_order.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ impl CloseFastMarketOrder<'_> {
3737
}
3838
}
3939

40-
pub fn close_fast_market_order(accounts: &[AccountInfo]) -> Result<()> {
40+
pub fn process(accounts: &[AccountInfo]) -> Result<()> {
4141
super::helpers::require_min_account_infos_len(accounts, 2)?;
4242

4343
// We need to check the refund recipient account against what we know as the

solana/programs/matching-engine/src/fallback/processor/process_instruction.rs

Lines changed: 21 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,8 @@ use wormhole_svm_definitions::make_anchor_discriminator;
33

44
use crate::ID;
55

6-
use super::close_fast_market_order::close_fast_market_order;
76
use super::execute_order::handle_execute_order_shim;
8-
use super::initialize_fast_market_order::{self, InitializeFastMarketOrderData};
7+
use super::initialize_fast_market_order::InitializeFastMarketOrderData;
98
use super::place_initial_offer::{place_initial_offer_cctp_shim, PlaceInitialOfferCctpShimData};
109
use super::prepare_order_response::{
1110
prepare_order_response_cctp_shim, PrepareOrderResponseCctpShimData,
@@ -29,6 +28,7 @@ impl<'ix> FallbackMatchingEngineInstruction<'ix> {
2928
pub enum FallbackMatchingEngineInstruction<'ix> {
3029
InitializeFastMarketOrder(&'ix InitializeFastMarketOrderData),
3130
CloseFastMarketOrder,
31+
// TODO: Replace with u64.
3232
PlaceInitialOfferCctpShim(&'ix PlaceInitialOfferCctpShimData),
3333
ExecuteOrderCctpShim,
3434
PrepareOrderResponseCctpShim(PrepareOrderResponseCctpShimData),
@@ -48,10 +48,10 @@ pub fn process_instruction(
4848

4949
match instruction {
5050
FallbackMatchingEngineInstruction::InitializeFastMarketOrder(data) => {
51-
initialize_fast_market_order::process(accounts, data)
51+
super::initialize_fast_market_order::process(accounts, data)
5252
}
5353
FallbackMatchingEngineInstruction::CloseFastMarketOrder => {
54-
close_fast_market_order(accounts)
54+
super::close_fast_market_order::process(accounts)
5555
}
5656
FallbackMatchingEngineInstruction::PlaceInitialOfferCctpShim(data) => {
5757
place_initial_offer_cctp_shim(accounts, data)
@@ -73,15 +73,14 @@ impl<'ix> FallbackMatchingEngineInstruction<'ix> {
7373

7474
match instruction_data[..SELECTOR_SIZE].try_into().unwrap() {
7575
FallbackMatchingEngineInstruction::PLACE_INITIAL_OFFER_CCTP_SHIM_SELECTOR => {
76-
Some(Self::PlaceInitialOfferCctpShim(
77-
PlaceInitialOfferCctpShimData::from_bytes(&instruction_data[SELECTOR_SIZE..])
78-
.unwrap(),
79-
))
76+
bytemuck::try_from_bytes(&instruction_data[SELECTOR_SIZE..])
77+
.ok()
78+
.map(Self::PlaceInitialOfferCctpShim)
8079
}
8180
FallbackMatchingEngineInstruction::INITIALIZE_FAST_MARKET_ORDER_SELECTOR => {
82-
Some(Self::InitializeFastMarketOrder(bytemuck::from_bytes(
83-
&instruction_data[SELECTOR_SIZE..],
84-
)))
81+
bytemuck::try_from_bytes(&instruction_data[SELECTOR_SIZE..])
82+
.ok()
83+
.map(Self::InitializeFastMarketOrder)
8584
}
8685
FallbackMatchingEngineInstruction::CLOSE_FAST_MARKET_ORDER_SELECTOR => {
8786
Some(Self::CloseFastMarketOrder)
@@ -90,6 +89,7 @@ impl<'ix> FallbackMatchingEngineInstruction<'ix> {
9089
Some(Self::ExecuteOrderCctpShim)
9190
}
9291
FallbackMatchingEngineInstruction::PREPARE_ORDER_RESPONSE_CCTP_SHIM_SELECTOR => {
92+
// TODO: Fix this
9393
Some(Self::PrepareOrderResponseCctpShim(
9494
PrepareOrderResponseCctpShimData::from_bytes(
9595
&instruction_data[SELECTOR_SIZE..],
@@ -107,7 +107,7 @@ impl FallbackMatchingEngineInstruction<'_> {
107107
match self {
108108
Self::InitializeFastMarketOrder(data) => {
109109
let mut out = Vec::with_capacity(
110-
std::mem::size_of::<InitializeFastMarketOrderData>().saturating_add(8),
110+
SELECTOR_SIZE + std::mem::size_of::<InitializeFastMarketOrderData>(),
111111
);
112112

113113
out.extend_from_slice(
@@ -118,53 +118,32 @@ impl FallbackMatchingEngineInstruction<'_> {
118118
out
119119
}
120120
Self::PlaceInitialOfferCctpShim(data) => {
121-
// Calculate the total capacity needed
122-
let data_slice = bytemuck::bytes_of(*data);
123-
let total_capacity = 8_usize.saturating_add(data_slice.len()); // 8 for the selector, plus the data length
124-
125-
// Create a vector with the calculated capacity
126-
let mut out = Vec::with_capacity(total_capacity);
121+
let mut out = Vec::with_capacity(SELECTOR_SIZE + std::mem::size_of::<u64>());
127122

128-
// Add the selector
129123
out.extend_from_slice(
130124
&FallbackMatchingEngineInstruction::PLACE_INITIAL_OFFER_CCTP_SHIM_SELECTOR,
131125
);
132-
out.extend_from_slice(data_slice);
126+
out.extend_from_slice(bytemuck::bytes_of(*data));
133127

134128
out
135129
}
136130
Self::ExecuteOrderCctpShim => {
137-
let total_capacity = 8; // 8 for the selector (no data)
138-
139-
let mut out = Vec::with_capacity(total_capacity);
140-
141-
out.extend_from_slice(
142-
&FallbackMatchingEngineInstruction::EXECUTE_ORDER_CCTP_SHIM_SELECTOR,
143-
);
144-
145-
out
131+
FallbackMatchingEngineInstruction::EXECUTE_ORDER_CCTP_SHIM_SELECTOR.to_vec()
146132
}
147133
Self::CloseFastMarketOrder => {
148-
let total_capacity = 8; // 8 for the selector (no data)
149-
150-
let mut out = Vec::with_capacity(total_capacity);
151-
152-
out.extend_from_slice(
153-
&FallbackMatchingEngineInstruction::CLOSE_FAST_MARKET_ORDER_SELECTOR,
154-
);
155-
156-
out
134+
FallbackMatchingEngineInstruction::CLOSE_FAST_MARKET_ORDER_SELECTOR.to_vec()
157135
}
158136
Self::PrepareOrderResponseCctpShim(data) => {
159-
let data_slice = data.try_to_vec().unwrap();
160-
let total_capacity = 8_usize.saturating_add(data_slice.len()); // 8 for the selector, plus the data length
137+
// Use a temporary vector, which will be consumed by the output vector when it is
138+
// extended.
139+
let tmp_data = data.try_to_vec().unwrap();
161140

162-
let mut out = Vec::with_capacity(total_capacity);
141+
let mut out = Vec::with_capacity(tmp_data.len().saturating_add(SELECTOR_SIZE));
163142

164143
out.extend_from_slice(
165144
&FallbackMatchingEngineInstruction::PREPARE_ORDER_RESPONSE_CCTP_SHIM_SELECTOR,
166145
);
167-
out.extend_from_slice(&data_slice);
146+
out.extend(tmp_data);
168147

169148
out
170149
}

0 commit comments

Comments
 (0)