Skip to content

Commit e844aff

Browse files
committed
solana: clean up fast market order
1 parent b5b34b9 commit e844aff

File tree

1 file changed

+46
-48
lines changed

1 file changed

+46
-48
lines changed

solana/programs/matching-engine/src/state/fast_market_order.rs

Lines changed: 46 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,52 +1,57 @@
1-
use anchor_lang::prelude::*;
2-
use anchor_lang::Discriminator;
1+
use anchor_lang::{prelude::*, Discriminator};
32
use solana_program::keccak;
43

5-
/// An account that represents a fast market order vaa. It is created by the signer of the transaction, and owned by the matching engine program.
6-
/// The of the account is able to close this account and redeem the lamports deposited into the account (for rent)
4+
/// An account that represents a fast market order VAA. It is created by the
5+
/// payer of the transaction. This payer is the only authority that can close
6+
/// this account and receive its rent.
77
#[account(zero_copy)]
88
#[derive(Debug)]
99
#[repr(C)]
1010
pub struct FastMarketOrder {
11-
/// The amount of tokens sent from the source chain via the fast transfer
11+
/// The amount of tokens sent from the source chain via the fast transfer.
1212
pub amount_in: u64,
13-
/// The minimum amount of tokens to be received on the target chain via the fast transfer
13+
/// The minimum amount of tokens to be received on the target chain via the
14+
/// fast transfer.
1415
pub min_amount_out: u64,
15-
/// The deadline of the auction
16+
/// The deadline of the auction.
1617
pub deadline: u32,
17-
/// The target chain (represented as a wormhole chain id)
18+
/// The target chain (represented as a Wormhole chain ID).
1819
pub target_chain: u16,
19-
/// The length of the redeemer message
20+
/// The length of the redeemer message.
2021
pub redeemer_message_length: u16,
21-
/// The redeemer of the fast transfer (on the destination chain)
22+
/// The redeemer of the fast transfer (on the destination chain).
2223
pub redeemer: [u8; 32],
23-
/// The sender of the fast transfer (on the source chain)
24+
/// The sender of the fast transfer (on the source chain).
2425
pub sender: [u8; 32],
25-
/// The refund address of the fast transfer
26+
/// The refund address of the fast transfer.
2627
pub refund_address: [u8; 32],
27-
/// The maximum fee of the fast transfer
28+
/// The maximum fee of the fast transfer.
2829
pub max_fee: u64,
29-
/// The initial auction fee of the fast transfer
30+
/// The initial auction fee of the fast transfer.
3031
pub init_auction_fee: u64,
31-
/// The redeemer message of the fast transfer
32-
/// NOTE: This value is based on the max redeemer length of 500 bytes that is specified in the token router program. If this changes in the future, this value must be updated.
32+
/// The redeemer message of the fast transfer.
33+
///
34+
/// NOTE: This value is based on the max redeemer length of 500 bytes that
35+
/// is specified in the token router program. If this changes in the future,
36+
/// this value must be updated.
3337
pub redeemer_message: [u8; 512],
34-
/// The refund recipient for the creator of the fast market order account
38+
/// The refund recipient for the creator of the fast market order account.
3539
pub close_account_refund_recipient: Pubkey,
3640
/// The emitter address of the fast transfer
3741
pub vaa_emitter_address: [u8; 32],
38-
/// The sequence of the fast transfer vaa
42+
/// The sequence of the fast transfer VAA.
3943
pub vaa_sequence: u64,
40-
/// The timestamp of the fast transfer vaa
44+
/// The timestamp of the fast transfer VAA.
4145
pub vaa_timestamp: u32,
42-
/// The vaa nonce, which is not used and can be set to 0.
46+
/// The VAA nonce, which is not used and can be set to 0.
4347
// TODO: Can be taken out.
4448
pub vaa_nonce: u32,
45-
/// The source chain of the fast transfer vaa (represented as a wormhole chain id)
49+
/// The source chain of the fast transfer VAA. (represented as a Wormhole
50+
/// chain ID).
4651
pub vaa_emitter_chain: u16,
47-
/// The consistency level of the fast transfer vaa
52+
/// The consistency level of the fast transfer VAA.
4853
pub vaa_consistency_level: u8,
49-
/// Not used, but required for bytemuck serialisation
54+
/// Not used, but required for bytemuck serialization.
5055
_padding: [u8; 5],
5156
}
5257

@@ -72,6 +77,8 @@ pub struct FastMarketOrderParams {
7277
}
7378

7479
impl FastMarketOrder {
80+
pub const SEED_PREFIX: &'static [u8] = b"fast_market_order";
81+
7582
pub fn new(params: FastMarketOrderParams) -> Self {
7683
Self {
7784
amount_in: params.amount_in,
@@ -96,16 +103,6 @@ impl FastMarketOrder {
96103
}
97104
}
98105

99-
pub const SEED_PREFIX: &'static [u8] = b"fast_market_order";
100-
101-
/// Convert the fast market order to a vec of bytes (without the discriminator)
102-
pub fn to_vec(&self) -> Vec<u8> {
103-
let payload_slice = bytemuck::bytes_of(self);
104-
let mut payload = Vec::with_capacity(payload_slice.len());
105-
payload.extend_from_slice(payload_slice);
106-
payload
107-
}
108-
109106
/// Creates an payload as expected in a fast market order vaa
110107
pub fn payload(&self) -> Vec<u8> {
111108
let mut payload = vec![];
@@ -128,22 +125,23 @@ impl FastMarketOrder {
128125
payload
129126
}
130127

131-
/// A double hash of the serialised fast market order. Used for seeds and verification.
128+
/// A double hash of the serialised fast market order. Used for seeds and
129+
/// verification.
130+
/// TODO: Change return type to keccak::Hash
132131
pub fn digest(&self) -> [u8; 32] {
133-
let message_hash = keccak::hashv(&[
134-
self.vaa_timestamp.to_be_bytes().as_ref(),
135-
self.vaa_nonce.to_be_bytes().as_ref(),
136-
self.vaa_emitter_chain.to_be_bytes().as_ref(),
137-
&self.vaa_emitter_address,
138-
&self.vaa_sequence.to_be_bytes(),
139-
&[self.vaa_consistency_level],
140-
self.payload().as_ref(),
141-
]);
142-
// Digest is the hash of the message
143-
keccak::hashv(&[message_hash.as_ref()])
144-
.as_ref()
145-
.try_into()
146-
.unwrap()
132+
wormhole_svm_definitions::compute_keccak_digest(
133+
keccak::hashv(&[
134+
&self.vaa_timestamp.to_be_bytes(),
135+
&self.vaa_nonce.to_be_bytes(),
136+
&self.vaa_emitter_chain.to_be_bytes(),
137+
&self.vaa_emitter_address,
138+
&self.vaa_sequence.to_be_bytes(),
139+
&[self.vaa_consistency_level],
140+
&self.payload(),
141+
]),
142+
None,
143+
)
144+
.0
147145
}
148146

149147
/// Read from an account info

0 commit comments

Comments
 (0)