Skip to content

Commit

Permalink
Use simpler SQL queries
Browse files Browse the repository at this point in the history
  • Loading branch information
squadgazzz committed Jul 15, 2024
1 parent 5342eb1 commit 1dd1956
Show file tree
Hide file tree
Showing 9 changed files with 138 additions and 350 deletions.
2 changes: 0 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ secp256k1 = "0.27.0"
serde = { version = "1.0.203", features = ["derive"] }
serde_json = "1.0.117"
serde_with = "3.8.1"
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio", "tls-native-tls", "bigdecimal", "chrono", "postgres", "macros", "json"] }
sqlx = { version = "0.7", default-features = false, features = ["runtime-tokio", "tls-native-tls", "bigdecimal", "chrono", "postgres", "macros"] }
strum = { version = "0.26.2", features = ["derive"] }
tempfile = "3.10.1"
time = { version = "0.3.36", features = ["macros"] }
Expand Down
2 changes: 0 additions & 2 deletions crates/database/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@ chrono = { workspace = true, features = ["clock"] }
const_format = "0.2.32"
futures = { workspace = true }
hex = { workspace = true }
serde = { workspace = true }
serde_json = { workspace = true }
sqlx = { workspace = true }
strum = { workspace = true }

Expand Down
57 changes: 0 additions & 57 deletions crates/database/src/byte_array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,44 +67,10 @@ impl<const N: usize> Encode<'_, Postgres> for ByteArray<N> {
}
}

impl<'de, const N: usize> serde::Deserialize<'de> for ByteArray<N> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where
D: serde::Deserializer<'de>,
{
struct ByteArrayVisitor<const N: usize>;

impl<'de, const N: usize> serde::de::Visitor<'de> for ByteArrayVisitor<N> {
type Value = ByteArray<N>;

fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
formatter.write_str("a hex string with a '\\x' prefix")
}

fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
where
E: serde::de::Error,
{
let text = v.strip_prefix("\\x").ok_or_else(|| {
serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)
})?;
let mut bytes = [0u8; N];
hex::decode_to_slice(text, &mut bytes).map_err(|_| {
serde::de::Error::invalid_value(serde::de::Unexpected::Str(v), &self)
})?;
Ok(ByteArray(bytes))
}
}

deserializer.deserialize_str(ByteArrayVisitor)
}
}

#[cfg(test)]
mod tests {
use {
super::*,
serde_json::json,
sqlx::{Executor, PgPool, Row},
};

Expand Down Expand Up @@ -148,27 +114,4 @@ mod tests {
.await;
assert!(result.is_err());
}

#[test]
fn test_deserialize_byte_array() {
// Valid deserialization
let json_value = json!("\\x010203");
let byte_array: ByteArray<3> = serde_json::from_value(json_value).unwrap();
assert_eq!(byte_array, ByteArray([1, 2, 3]));

// Invalid deserialization: wrong prefix
let json_value = json!("010203");
let result: Result<ByteArray<3>, _> = serde_json::from_value(json_value);
assert!(result.is_err());

// Invalid deserialization: wrong length
let json_value = json!("\\x0102");
let result: Result<ByteArray<3>, _> = serde_json::from_value(json_value);
assert!(result.is_err());

// Invalid deserialization: non-hex characters
let json_value = json!("\\x01g203");
let result: Result<ByteArray<3>, _> = serde_json::from_value(json_value);
assert!(result.is_err());
}
}
5 changes: 2 additions & 3 deletions crates/database/src/fee_policies.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {
sqlx::{PgConnection, QueryBuilder},
};

#[derive(Debug, Clone, PartialEq, sqlx::FromRow, serde::Deserialize)]
#[derive(Debug, Clone, PartialEq, sqlx::FromRow)]
pub struct FeePolicy {
pub auction_id: AuctionId,
pub order_uid: OrderUid,
Expand All @@ -15,8 +15,7 @@ pub struct FeePolicy {
pub price_improvement_max_volume_factor: Option<f64>,
}

#[derive(Debug, Clone, PartialEq, sqlx::Type, serde::Deserialize)]
#[serde(rename_all = "lowercase")]
#[derive(Debug, Clone, PartialEq, sqlx::Type)]
#[sqlx(type_name = "PolicyKind", rename_all = "lowercase")]
pub enum FeePolicyKind {
Surplus,
Expand Down
Loading

0 comments on commit 1dd1956

Please sign in to comment.