1
1
use super :: helpers:: check_account_length;
2
2
use super :: helpers:: create_account_reliably;
3
+ use super :: helpers:: create_token_account_reliably;
4
+ use crate :: state:: MessageProtocol ;
3
5
use crate :: state:: {
4
6
Auction , AuctionConfig , AuctionInfo , AuctionStatus , Custodian ,
5
- FastMarketOrder as FastMarketOrderState , MessageProtocol , RouterEndpoint ,
7
+ FastMarketOrder as FastMarketOrderState , RouterEndpoint ,
6
8
} ;
7
9
use crate :: ID as PROGRAM_ID ;
8
10
use anchor_lang:: prelude:: * ;
@@ -25,29 +27,38 @@ pub struct PlaceInitialOfferCctpShimData {
25
27
}
26
28
27
29
impl PlaceInitialOfferCctpShimData {
28
- pub fn new ( offer_price : u64 ) -> Self {
29
- Self { offer_price }
30
- }
31
-
32
30
pub fn from_bytes ( data : & [ u8 ] ) -> Option < & Self > {
33
31
bytemuck:: try_from_bytes :: < Self > ( data) . ok ( )
34
32
}
35
33
}
36
34
37
35
#[ derive( Debug , Clone , PartialEq , Eq , Copy ) ]
38
36
pub struct PlaceInitialOfferCctpShimAccounts < ' ix > {
37
+ /// The signer account
39
38
pub signer : & ' ix Pubkey ,
39
+ /// The transfer authority account
40
40
pub transfer_authority : & ' ix Pubkey ,
41
+ /// The custodian account
41
42
pub custodian : & ' ix Pubkey ,
43
+ /// The auction config account
42
44
pub auction_config : & ' ix Pubkey ,
45
+ /// The from endpoint account
43
46
pub from_endpoint : & ' ix Pubkey ,
47
+ /// The to endpoint account
44
48
pub to_endpoint : & ' ix Pubkey ,
45
- pub fast_market_order : & ' ix Pubkey , // Needs initalising. Seeds are [FastMarketOrderState::SEED_PREFIX, auction_address.as_ref()]
46
- pub auction : & ' ix Pubkey , // Needs initalising
49
+ /// The fast market order account, which will be initialised. Seeds are [FastMarketOrderState::SEED_PREFIX, auction_address.as_ref()]
50
+ pub fast_market_order : & ' ix Pubkey ,
51
+ /// The auction account, which will be initialised
52
+ pub auction : & ' ix Pubkey ,
53
+ /// The offer token account
47
54
pub offer_token : & ' ix Pubkey ,
55
+ /// The auction custody token account
48
56
pub auction_custody_token : & ' ix Pubkey ,
57
+ /// The usdc token account
49
58
pub usdc : & ' ix Pubkey ,
59
+ /// The system program account
50
60
pub system_program : & ' ix Pubkey ,
61
+ /// The token program account
51
62
pub token_program : & ' ix Pubkey ,
52
63
}
53
64
@@ -119,8 +130,7 @@ impl VaaMessageBodyHeader {
119
130
}
120
131
}
121
132
122
- /// This function creates both the message body and the payload.
123
- /// This is all done here just because it's (supposedly?) cheaper in the solana vm.
133
+ /// This function creates both the message body for the fast market order, including the payload.
124
134
pub fn message_body ( & self , fast_market_order : & FastMarketOrderState ) -> Vec < u8 > {
125
135
let mut message_body = vec ! [ ] ;
126
136
message_body. extend_from_slice ( & self . vaa_time . to_be_bytes ( ) ) ;
@@ -129,44 +139,48 @@ impl VaaMessageBodyHeader {
129
139
message_body. extend_from_slice ( & self . emitter_address ) ;
130
140
message_body. extend_from_slice ( & self . sequence . to_be_bytes ( ) ) ;
131
141
message_body. extend_from_slice ( & [ self . consistency_level ] ) ;
132
- let mut payload = vec ! [ ] ;
133
- payload. push ( 11_u8 ) ;
134
- payload. extend_from_slice ( & fast_market_order. amount_in . to_be_bytes ( ) ) ;
135
- payload. extend_from_slice ( & fast_market_order. min_amount_out . to_be_bytes ( ) ) ;
136
- payload. extend_from_slice ( & fast_market_order. target_chain . to_be_bytes ( ) ) ;
137
- payload. extend_from_slice ( & fast_market_order. redeemer ) ;
138
- payload. extend_from_slice ( & fast_market_order. sender ) ;
139
- payload. extend_from_slice ( & fast_market_order. refund_address ) ;
140
- payload. extend_from_slice ( & fast_market_order. max_fee . to_be_bytes ( ) ) ;
141
- payload. extend_from_slice ( & fast_market_order. init_auction_fee . to_be_bytes ( ) ) ;
142
- payload. extend_from_slice ( & fast_market_order. deadline . to_be_bytes ( ) ) ;
143
- payload. extend_from_slice ( & fast_market_order. redeemer_message_length . to_be_bytes ( ) ) ;
142
+ message_body. push ( 11_u8 ) ;
143
+ message_body. extend_from_slice ( & fast_market_order. amount_in . to_be_bytes ( ) ) ;
144
+ message_body. extend_from_slice ( & fast_market_order. min_amount_out . to_be_bytes ( ) ) ;
145
+ message_body. extend_from_slice ( & fast_market_order. target_chain . to_be_bytes ( ) ) ;
146
+ message_body. extend_from_slice ( & fast_market_order. redeemer ) ;
147
+ message_body. extend_from_slice ( & fast_market_order. sender ) ;
148
+ message_body. extend_from_slice ( & fast_market_order. refund_address ) ;
149
+ message_body. extend_from_slice ( & fast_market_order. max_fee . to_be_bytes ( ) ) ;
150
+ message_body. extend_from_slice ( & fast_market_order. init_auction_fee . to_be_bytes ( ) ) ;
151
+ message_body. extend_from_slice ( & fast_market_order. deadline . to_be_bytes ( ) ) ;
152
+ message_body. extend_from_slice ( & fast_market_order. redeemer_message_length . to_be_bytes ( ) ) ;
144
153
if fast_market_order. redeemer_message_length > 0 {
145
- payload . extend_from_slice (
154
+ message_body . extend_from_slice (
146
155
& fast_market_order. redeemer_message
147
156
[ ..usize:: from ( fast_market_order. redeemer_message_length ) ] ,
148
157
) ;
149
158
}
150
- message_body. extend_from_slice ( & payload) ;
151
159
message_body
152
160
}
153
161
162
+ /// This function creates the hash of the message body for the fast market order.
163
+ /// This is used to create the digest.
154
164
pub fn message_hash ( & self , fast_market_order : & FastMarketOrderState ) -> keccak:: Hash {
155
165
keccak:: hashv ( & [ self . message_body ( fast_market_order) . as_ref ( ) ] )
156
166
}
157
167
168
+ /// The digest is the hash of the message hash.
158
169
pub fn digest ( & self , fast_market_order : & FastMarketOrderState ) -> keccak:: Hash {
159
170
keccak:: hashv ( & [ self . message_hash ( fast_market_order) . as_ref ( ) ] )
160
171
}
161
172
173
+ /// This function returns the vaa time.
162
174
pub fn vaa_time ( & self ) -> u32 {
163
175
self . vaa_time
164
176
}
165
177
178
+ /// This function returns the sequence number of the fast market order.
166
179
pub fn sequence ( & self ) -> u64 {
167
180
self . sequence
168
181
}
169
182
183
+ /// This function returns the emitter chain of the fast market order.
170
184
pub fn emitter_chain ( & self ) -> u16 {
171
185
self . emitter_chain
172
186
}
@@ -176,8 +190,7 @@ pub fn place_initial_offer_cctp_shim(
176
190
accounts : & [ AccountInfo ] ,
177
191
data : & PlaceInitialOfferCctpShimData ,
178
192
) -> Result < ( ) > {
179
- // Check account owners
180
- let program_id = & crate :: ID ; // Your program ID
193
+ let program_id = & PROGRAM_ID ; // Your program ID
181
194
182
195
// Check all accounts are valid
183
196
check_account_length ( accounts, 11 ) ?;
@@ -204,8 +217,8 @@ pub fn place_initial_offer_cctp_shim(
204
217
. map_err ( |e : Error | e. with_account_name ( "fast_market_order_account" ) ) ;
205
218
}
206
219
207
- let fast_market_order_zero_copy =
208
- FastMarketOrderState :: try_deserialize ( & mut & fast_market_order_account . data . borrow ( ) [ .. ] ) ?;
220
+ let fast_market_order_data = & fast_market_order_account . data . borrow ( ) [ .. ] ;
221
+ let fast_market_order_zero_copy = FastMarketOrderState :: try_read ( fast_market_order_data ) ?;
209
222
210
223
let vaa_time = fast_market_order_zero_copy. vaa_timestamp ;
211
224
let sequence = fast_market_order_zero_copy. vaa_sequence ;
@@ -303,16 +316,16 @@ pub fn place_initial_offer_cctp_shim(
303
316
return Err ( MatchingEngineError :: SameEndpoint . into ( ) ) ;
304
317
}
305
318
306
- // Check that the to endpoint protocol is cctp or local
319
+ // Check that the to endpoint is a valid protocol
307
320
match to_endpoint_account. protocol {
308
321
MessageProtocol :: Cctp { .. } | MessageProtocol :: Local { .. } => ( ) ,
309
322
_ => return Err ( MatchingEngineError :: InvalidEndpoint . into ( ) ) ,
310
323
}
311
324
312
- // Check that the from endpoint protocol is cctp or local
313
- match from_endpoint_account. protocol {
314
- MessageProtocol :: Cctp { .. } | MessageProtocol :: Local { .. } => ( ) ,
315
- _ => return Err ( MatchingEngineError :: InvalidEndpoint . into ( ) ) ,
325
+ // Check that the vaa emitter address equals the from_endpoints encoded address
326
+ if from_endpoint_account. address != fast_market_order_zero_copy . vaa_emitter_address {
327
+ msg ! ( "Vaa emitter address is not equal to the from_endpoints encoded address" ) ;
328
+ return Err ( MatchingEngineError :: InvalidSourceRouter . into ( ) ) ;
316
329
}
317
330
318
331
// Check that to endpoint chain is equal to the fast_market_order target_chain
@@ -349,8 +362,6 @@ pub fn place_initial_offer_cctp_shim(
349
362
350
363
// Begin of initialisation of auction custody token account
351
364
// ------------------------------------------------------------------------------------------------
352
- let auction_custody_token_space = spl_token:: state:: Account :: LEN ;
353
-
354
365
let ( auction_custody_token_pda, auction_custody_token_bump) = Pubkey :: find_program_address (
355
366
& [
356
367
crate :: AUCTION_CUSTODY_TOKEN_SEED_PREFIX ,
@@ -373,25 +384,17 @@ pub fn place_initial_offer_cctp_shim(
373
384
& [ auction_custody_token_bump] ,
374
385
] ;
375
386
let auction_custody_token_signer_seeds = & [ & auction_custody_token_seeds[ ..] ] ;
376
- create_account_reliably (
387
+
388
+ create_token_account_reliably (
377
389
& signer. key ( ) ,
378
390
& auction_custody_token_pda,
391
+ & auction_account. key ( ) ,
392
+ & usdc. key ( ) ,
393
+ spl_token:: state:: Account :: LEN ,
379
394
auction_custody_token. lamports ( ) ,
380
- auction_custody_token_space,
381
395
accounts,
382
- & spl_token:: ID ,
383
396
auction_custody_token_signer_seeds,
384
397
) ?;
385
- // Initialise the token account
386
- let init_token_account_ix = spl_token:: instruction:: initialize_account3 (
387
- & spl_token:: ID ,
388
- & auction_custody_token_pda,
389
- & usdc. key ( ) ,
390
- & auction_account. key ( ) ,
391
- )
392
- . unwrap ( ) ;
393
-
394
- solana_program:: program:: invoke ( & init_token_account_ix, accounts) . unwrap ( ) ;
395
398
396
399
// ------------------------------------------------------------------------------------------------
397
400
// End of initialisation of auction custody token account
@@ -494,8 +497,7 @@ pub fn place_initial_offer_cctp_shim(
494
497
& offer_price. to_be_bytes ( ) ,
495
498
& [ transfer_authority_bump] ,
496
499
] ] ,
497
- )
498
- . map_err ( |_| MatchingEngineError :: TokenTransferFailed ) ?;
500
+ ) ?;
499
501
// ------------------------------------------------------------------------------------------------
500
502
// End of token transfer from offer token to auction custody token
501
503
Ok ( ( ) )
0 commit comments