Skip to content

Commit 5f40d96

Browse files
authored
swift: add helper to extract raw message (#313)
* swift: add helper to extract raw message * fix swift-maker example * add --isolated-position example for swift-taker
1 parent d6b482c commit 5f40d96

File tree

6 files changed

+273
-233
lines changed

6 files changed

+273
-233
lines changed

crates/src/swift_order_subscriber.rs

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
//! Swift order subscriber and serialization utilities
12
use std::time::{SystemTime, UNIX_EPOCH};
23

34
use anchor_lang::{AnchorDeserialize, AnchorSerialize, Space};
@@ -59,6 +60,13 @@ pub enum SignedOrderType {
5960
}
6061

6162
impl SignedOrderType {
63+
/// Return the original, hexified message
64+
pub fn raw(&self) -> &Option<String> {
65+
match self {
66+
Self::Authority { inner: _, ref raw } => raw,
67+
Self::Delegated { inner: _, ref raw } => raw,
68+
}
69+
}
6270
pub fn delegated(order: SignedDelegateOrder) -> Self {
6371
Self::Delegated {
6472
inner: order,
@@ -102,7 +110,7 @@ impl SignedOrderType {
102110

103111
buf
104112
}
105-
113+
/// Returns order details
106114
pub fn info(&self, taker_authority: &Pubkey) -> SignedMessageInfo {
107115
match self {
108116
Self::Authority { inner, .. } => SignedMessageInfo {
@@ -131,6 +139,7 @@ struct OrderNotification<'a> {
131139
}
132140

133141
/// Error notification from Websocket
142+
#[allow(unused)]
134143
#[derive(Clone, Debug, Deserialize)]
135144
struct ErrorNotification<'a> {
136145
channel: &'a str,
@@ -575,18 +584,17 @@ pub fn deser_signed_msg_type<'de, D>(deserializer: D) -> Result<SignedOrderType,
575584
where
576585
D: serde::Deserializer<'de>,
577586
{
578-
let payload: &str = serde::Deserialize::deserialize(deserializer)?;
587+
let payload: std::borrow::Cow<String> = serde::Deserialize::deserialize(deserializer)?;
579588
if payload.len() % 2 != 0 {
580589
return Err(serde::de::Error::custom("Hex string length must be even"));
581590
}
582-
583-
// decode expecting the largest possible variant
584-
if (payload.len() / 2) > SignedDelegateOrder::INIT_SPACE + 8 || payload.is_empty() {
591+
if payload.is_empty() {
585592
return Err(serde::de::Error::custom("invalid signed message hex"));
586593
}
587594

595+
// decode expecting the largest possible variant
588596
let mut borsh_buf = [0u8; SignedDelegateOrder::INIT_SPACE + 8];
589-
hex::decode_to_slice(payload, &mut borsh_buf[..payload.len() / 2])
597+
hex::decode_to_slice(payload.as_bytes(), &mut borsh_buf[..payload.len() / 2])
590598
.map_err(serde::de::Error::custom)?;
591599

592600
// this is basically the same as if we derived AnchorDeserialize on `SignedOrderType` _expect_ it does not

0 commit comments

Comments
 (0)