-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
29e0bdb
commit b95e5c3
Showing
8 changed files
with
139 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
-- Add down migration script here |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
CREATE TYPE direction_enum AS ENUM ('BuyCents', 'SellCents'); | ||
|
||
CREATE TABLE stablesats_quote ( | ||
id UUID PRIMARY KEY DEFAULT gen_random_uuid(), | ||
direction direction_enum NOT NULL, | ||
immediate_execution BOOLEAN NOT NULL, | ||
sat_amount BIGINT NOT NULL, | ||
cent_amount BIGINT NOT NULL, | ||
expires_at TIMESTAMPTZ NOT NULL | ||
); | ||
|
||
CREATE TABLE stablesats_quote_events ( | ||
id UUID REFERENCES stablesats_quote(id) NOT NULL, | ||
sequence INT NOT NULL, | ||
event_type VARCHAR NOT NULL, | ||
event JSONB NOT NULL, | ||
recorded_at TIMESTAMPTZ NOT NULL DEFAULT NOW(), | ||
UNIQUE(id, sequence) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum QuoteError { | ||
#[error("WalletError - Sqlx: {0}")] | ||
Sqlx(#[from] sqlx::Error), | ||
#[error("WalletError - EntityError: {0}")] | ||
EntityError(#[from] crate::entity::EntityError), | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,7 @@ | ||
mod entity; | ||
mod error; | ||
mod repo; | ||
|
||
pub use entity::*; | ||
pub use error::*; | ||
pub use repo::*; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
use rust_decimal::prelude::ToPrimitive; | ||
use sqlx::{Pool, Postgres}; | ||
use tracing::instrument; | ||
|
||
use crate::entity::*; | ||
|
||
use super::{pg, NewQuote, Quote, QuoteError, QuoteEvent, QuoteId}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub struct Quotes { | ||
pool: Pool<Postgres>, | ||
} | ||
|
||
impl Quotes { | ||
pub fn new(pool: &Pool<Postgres>) -> Self { | ||
Self { pool: pool.clone() } | ||
} | ||
|
||
#[instrument(name = "quotes.create", skip(self))] | ||
pub async fn create(&self, quote: NewQuote) -> Result<Quote, QuoteError> { | ||
let mut tx = self.pool.begin().await?; | ||
sqlx::query!( | ||
r#" | ||
INSERT INTO stablesats_quote (id, direction, immediate_execution, sat_amount, cent_amount, expires_at) | ||
VALUES ($1, $2, $3, $4, $5, $6) | ||
"#, | ||
quote.id as QuoteId, | ||
pg::PgDirection::from(quote.direction.clone()) as pg::PgDirection, | ||
quote.immediate_execution, | ||
quote.sat_amount.amount().to_i64(), | ||
quote.cent_amount.amount().to_i64(), | ||
quote.expires_at | ||
) | ||
.execute(&mut *tx) | ||
.await?; | ||
let res = Quote { | ||
id: quote.id, | ||
direction: quote.direction.clone(), | ||
immediate_execution: quote.immediate_execution, | ||
sat_amount: quote.sat_amount.clone(), | ||
cent_amount: quote.cent_amount.clone(), | ||
events: quote.clone().initial_events(), | ||
}; | ||
|
||
EntityEvents::<QuoteEvent>::persist( | ||
"stablesats_quote_events", | ||
&mut tx, | ||
quote.initial_events().new_serialized_events(res.id), | ||
) | ||
.await?; | ||
|
||
tx.commit().await?; | ||
Ok(res) | ||
} | ||
} |