Skip to content

Commit

Permalink
More fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
m30m committed Mar 27, 2024
1 parent e783af6 commit a987791
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 15 deletions.
2 changes: 1 addition & 1 deletion auction-server/migrations/20240326063340_bids.up.sql
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ CREATE TABLE bid
creation_time TIMESTAMP NOT NULL,
permission_key BYTEA NOT NULL,
chain_id TEXT NOT NULL,
target_contract BYTEA NOT NULL CHECK (LENGTH(permission_key) = 20),
target_contract BYTEA NOT NULL CHECK (LENGTH(target_contract) = 20),
target_calldata BYTEA NOT NULL,
bid_amount NUMERIC(78, 0) NOT NULL,
status bid_status NOT NULL,
Expand Down
45 changes: 31 additions & 14 deletions auction-server/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,18 @@ use {
Deserialize,
Serialize,
},
sqlx::types::{
time::{
OffsetDateTime,
PrimitiveDateTime,
sqlx::{
database::HasArguments,
encode::IsNull,
types::{
time::{
OffsetDateTime,
PrimitiveDateTime,
},
BigDecimal,
},
BigDecimal,
Postgres,
TypeInfo,
},
std::{
collections::HashMap,
Expand Down Expand Up @@ -171,13 +177,24 @@ pub enum BidStatus {
Lost,
}

impl BidStatus {
pub fn status_name(&self) -> String {
match self {
BidStatus::Pending => "pending".to_string(),
BidStatus::Submitted(_) => "submitted".to_string(),
BidStatus::Lost => "lost".to_string(),
}
impl sqlx::Encode<'_, sqlx::Postgres> for BidStatus {
fn encode_by_ref(&self, buf: &mut <Postgres as HasArguments<'_>>::ArgumentBuffer) -> IsNull {
let result = match self {
BidStatus::Pending => "pending",
BidStatus::Submitted(_) => "submitted",
BidStatus::Lost => "lost",
};
<&str as sqlx::Encode<sqlx::Postgres>>::encode(result, buf)
}
}

impl sqlx::Type<sqlx::Postgres> for BidStatus {
fn type_info() -> sqlx::postgres::PgTypeInfo {
sqlx::postgres::PgTypeInfo::with_name("bid_status")
}

fn compatible(ty: &sqlx::postgres::PgTypeInfo) -> bool {
ty.name() == "bid_status"
}
}

Expand Down Expand Up @@ -274,7 +291,7 @@ impl Store {
&bid.target_contract.to_fixed_bytes(),
bid.target_calldata.to_vec(),
BigDecimal::from_str(&bid.bid_amount.to_string()).unwrap(),
bid.status.status_name() as _,
bid.status as _,
)
.execute(&self.db)
.await.map_err(|e| {
Expand All @@ -301,7 +318,7 @@ impl Store {
let now = OffsetDateTime::now_utc();
sqlx::query!(
"UPDATE bid SET status = $1, removal_time = $2 WHERE id = $3 AND removal_time IS NULL",
update.bid_status.status_name() as _,
update.bid_status as _,
PrimitiveDateTime::new(now.date(), now.time()),
update.id
)
Expand Down

0 comments on commit a987791

Please sign in to comment.