Skip to content

Commit

Permalink
chore: use MintBtcOnchainState instead of paid flag
Browse files Browse the repository at this point in the history
fixes #310
  • Loading branch information
ngutech21 committed Aug 1, 2024
1 parent 793bae2 commit 9e9cae6
Show file tree
Hide file tree
Showing 9 changed files with 109 additions and 46 deletions.
44 changes: 41 additions & 3 deletions moksha-core/src/primitives.rs
Original file line number Diff line number Diff line change
Expand Up @@ -261,15 +261,15 @@ pub struct BtcOnchainMintQuote {
pub unit: CurrencyUnit,
pub amount: u64,
pub expiry: u64,
pub paid: bool,
pub state: MintBtcOnchainState,
}

impl From<BtcOnchainMintQuote> for PostMintQuoteBtcOnchainResponse {
fn from(quote: BtcOnchainMintQuote) -> Self {
Self {
quote: quote.quote_id.to_string(),
address: quote.address,
paid: quote.paid,
state: quote.state,
expiry: quote.expiry,
}
}
Expand Down Expand Up @@ -297,10 +297,48 @@ pub struct PostMintQuoteBtcOnchainRequest {
pub struct PostMintQuoteBtcOnchainResponse {
pub quote: String,
pub address: String,
pub paid: bool,
pub state: MintBtcOnchainState,
pub expiry: u64,
}

#[derive(Deserialize, Serialize, Debug, PartialEq, Eq, Clone, ToSchema)]
#[serde(rename_all = "UPPERCASE")]
pub enum MintBtcOnchainState {
/// initial state. No payment received from the wallet yet
Unpaid,

Pending,

Paid,

Issued,
}

impl Display for MintBtcOnchainState {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
MintBtcOnchainState::Unpaid => write!(f, "UNPAID"),
MintBtcOnchainState::Pending => write!(f, "PENDING"),
MintBtcOnchainState::Paid => write!(f, "PAID"),
MintBtcOnchainState::Issued => write!(f, "ISSUED"),
}
}
}

impl FromStr for MintBtcOnchainState {
type Err = ();

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"UNPAID" => Ok(MintBtcOnchainState::Unpaid),
"PENDING" => Ok(MintBtcOnchainState::Pending),
"PAID" => Ok(MintBtcOnchainState::Paid),
"ISSUED" => Ok(MintBtcOnchainState::Issued),
_ => Err(()),
}
}
}

#[derive(Deserialize, Serialize, Debug, Clone, ToSchema)]
pub struct PostMintBtcOnchainRequest {
pub quote: String,
Expand Down

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

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

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

This file was deleted.

15 changes: 15 additions & 0 deletions moksha-mint/migrations/20240801124852_btconchain_mint_state.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
-- Add migration script here
ALTER TABLE onchain_mint_quotes
ADD COLUMN state TEXT;

UPDATE onchain_mint_quotes
SET state = CASE
WHEN paid = true THEN 'PAID'
WHEN paid = false THEN 'UNPAID'
END;

ALTER TABLE onchain_mint_quotes
DROP COLUMN paid;

ALTER TABLE onchain_mint_quotes
ALTER COLUMN state SET NOT NULL;
14 changes: 7 additions & 7 deletions moksha-mint/src/database/postgres.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use moksha_core::{
dhke,
primitives::{
Bolt11MeltQuote, Bolt11MintQuote, BtcOnchainMeltQuote, BtcOnchainMintQuote, CurrencyUnit,
MeltBtcOnchainState,
MeltBtcOnchainState, MintBtcOnchainState,
},
proof::{Proof, Proofs},
};
Expand Down Expand Up @@ -295,14 +295,14 @@ impl Database for PostgresDB {
key: &Uuid,
) -> Result<BtcOnchainMintQuote, MokshaMintError> {
let quote: BtcOnchainMintQuote = sqlx::query!(
"SELECT id, address, amount, expiry, paid FROM onchain_mint_quotes WHERE id = $1",
"SELECT id, address, amount, expiry, state FROM onchain_mint_quotes WHERE id = $1",
key
)
.map(|row| BtcOnchainMintQuote {
quote_id: row.id,
address: row.address,
expiry: row.expiry as u64,
paid: row.paid,
state: MintBtcOnchainState::from_str(&row.state).expect("invalid state in mint quote"),
amount: row.amount as u64,
unit: CurrencyUnit::Sat,
})
Expand All @@ -319,12 +319,12 @@ impl Database for PostgresDB {
quote: &BtcOnchainMintQuote,
) -> Result<(), MokshaMintError> {
sqlx::query!(
"INSERT INTO onchain_mint_quotes (id, address, amount, expiry, paid) VALUES ($1, $2, $3, $4, $5)",
"INSERT INTO onchain_mint_quotes (id, address, amount, expiry, state) VALUES ($1, $2, $3, $4, $5)",
quote.quote_id,
quote.address,
quote.amount as i64,
quote.expiry as i64,
quote.paid,
quote.state.to_string(),
)
.execute(&mut **tx)
.await?;
Expand All @@ -338,8 +338,8 @@ impl Database for PostgresDB {
quote: &BtcOnchainMintQuote,
) -> Result<(), MokshaMintError> {
sqlx::query!(
"UPDATE onchain_mint_quotes SET paid = $1 WHERE id = $2",
quote.paid,
"UPDATE onchain_mint_quotes SET state = $1 WHERE id = $2",
quote.state.to_string(),
quote.quote_id
)
.execute(&mut **tx)
Expand Down
20 changes: 13 additions & 7 deletions moksha-mint/src/routes/btconchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@ use axum::{
Json,
};
use moksha_core::primitives::{
BtcOnchainMeltQuote, BtcOnchainMintQuote, CurrencyUnit, MeltBtcOnchainState, PaymentMethod,
PostMeltBtcOnchainRequest, PostMeltBtcOnchainResponse, PostMeltQuoteBtcOnchainRequest,
PostMeltQuoteBtcOnchainResponse, PostMintBtcOnchainRequest, PostMintBtcOnchainResponse,
PostMintQuoteBtcOnchainRequest, PostMintQuoteBtcOnchainResponse,
BtcOnchainMeltQuote, BtcOnchainMintQuote, CurrencyUnit, MeltBtcOnchainState,
MintBtcOnchainState, PaymentMethod, PostMeltBtcOnchainRequest, PostMeltBtcOnchainResponse,
PostMeltQuoteBtcOnchainRequest, PostMeltQuoteBtcOnchainResponse, PostMintBtcOnchainRequest,
PostMintBtcOnchainResponse, PostMintQuoteBtcOnchainRequest, PostMintQuoteBtcOnchainResponse,
};
use tracing::{info, instrument};
use uuid::Uuid;
Expand Down Expand Up @@ -63,7 +63,7 @@ pub async fn post_mint_quote_btconchain(
unit: request.unit,
amount: request.amount,
expiry: quote_onchain_expiry(),
paid: false,
state: MintBtcOnchainState::Unpaid,
};

let mut tx = mint.db.begin_tx().await?;
Expand Down Expand Up @@ -109,7 +109,13 @@ pub async fn get_mint_quote_btconchain(
.is_paid(&quote.address, quote.amount, min_confs)
.await?;

Ok(Json(BtcOnchainMintQuote { paid, ..quote }.into()))
// FIXME compute correct state
let state = match paid {
true => MintBtcOnchainState::Paid,
false => MintBtcOnchainState::Unpaid,
};

Ok(Json(BtcOnchainMintQuote { state, ..quote }.into()))
}

#[utoipa::path(
Expand Down Expand Up @@ -146,7 +152,7 @@ pub async fn post_mint_btconchain(
.update_onchain_mint_quote(
&mut tx,
&BtcOnchainMintQuote {
paid: true,
state: MintBtcOnchainState::Issued,
..old_quote.clone()
},
)
Expand Down
18 changes: 11 additions & 7 deletions moksha-wallet/src/wallet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,10 @@ use moksha_core::{
dhke::Dhke,
keyset::KeysetId,
primitives::{
CurrencyUnit, MeltBtcOnchainState, MintInfoResponse, PaymentMethod, PostMeltBolt11Response,
PostMeltBtcOnchainResponse, PostMeltQuoteBolt11Response, PostMeltQuoteBtcOnchainResponse,
PostMintQuoteBolt11Response, PostMintQuoteBtcOnchainResponse,
CurrencyUnit, MeltBtcOnchainState, MintBtcOnchainState, MintInfoResponse, PaymentMethod,
PostMeltBolt11Response, PostMeltBtcOnchainResponse, PostMeltQuoteBolt11Response,
PostMeltQuoteBtcOnchainResponse, PostMintQuoteBolt11Response,
PostMintQuoteBtcOnchainResponse,
},
proof::{Proof, Proofs},
token::TokenV3,
Expand Down Expand Up @@ -160,10 +161,13 @@ where
}

PaymentMethod::BtcOnchain => {
self.client
.get_mint_quote_onchain(mint_url, quote)
.await?
.paid
matches!(
self.client
.get_mint_quote_onchain(mint_url, quote)
.await?
.state,
MintBtcOnchainState::Paid | MintBtcOnchainState::Issued
)
}
})
}
Expand Down

0 comments on commit 9e9cae6

Please sign in to comment.