mirror of
https://github.com/aljazceru/cdk.git
synced 2026-02-02 19:55:56 +01:00
fix: check mint and melt quotes are within settings
This commit is contained in:
@@ -62,6 +62,24 @@ pub enum Error {
|
||||
/// Sig all cannot be used in melt
|
||||
#[error("Sig all cannot be used in melt")]
|
||||
SigAllUsedInMelt,
|
||||
/// Minting is disabled
|
||||
#[error("Minting is disabled")]
|
||||
MintingDisabled,
|
||||
/// Minting request is over mint limit
|
||||
#[error("Mint request is over mint limit")]
|
||||
MintOverLimit,
|
||||
/// Mint request is uver mint limit
|
||||
#[error("Mint request is under mint limit")]
|
||||
MintUnderLimit,
|
||||
/// Melting is disabled
|
||||
#[error("Minting is disabled")]
|
||||
MeltingDisabled,
|
||||
/// Melting request is over mint limit
|
||||
#[error("Mint request is over mint limit")]
|
||||
MeltOverLimit,
|
||||
/// Melt request is uver mint limit
|
||||
#[error("Mint request is under mint limit")]
|
||||
MeltUnderLimit,
|
||||
/// Cashu Error
|
||||
#[error(transparent)]
|
||||
Cashu(#[from] crate::error::Error),
|
||||
|
||||
@@ -201,6 +201,33 @@ impl Mint {
|
||||
expiry: u64,
|
||||
ln_lookup: String,
|
||||
) -> Result<MintQuote, Error> {
|
||||
let nut04 = &self.mint_info.nuts.nut04;
|
||||
|
||||
if nut04.disabled {
|
||||
return Err(Error::MintingDisabled);
|
||||
}
|
||||
|
||||
match nut04.get_settings(&unit, &PaymentMethod::Bolt11) {
|
||||
Some(settings) => {
|
||||
if settings
|
||||
.max_amount
|
||||
.map_or(false, |max_amount| amount > max_amount)
|
||||
{
|
||||
return Err(Error::MintOverLimit);
|
||||
}
|
||||
|
||||
if settings
|
||||
.min_amount
|
||||
.map_or(false, |min_amount| amount < min_amount)
|
||||
{
|
||||
return Err(Error::MintUnderLimit);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return Err(Error::UnsupportedUnit);
|
||||
}
|
||||
}
|
||||
|
||||
let quote = MintQuote::new(mint_url, request, unit, amount, expiry, ln_lookup.clone());
|
||||
tracing::debug!(
|
||||
"New mint quote {} for {} {} with request id {}",
|
||||
@@ -320,6 +347,33 @@ impl Mint {
|
||||
expiry: u64,
|
||||
request_lookup_id: String,
|
||||
) -> Result<MeltQuote, Error> {
|
||||
let nut05 = &self.mint_info.nuts.nut05;
|
||||
|
||||
if nut05.disabled {
|
||||
return Err(Error::MeltingDisabled);
|
||||
}
|
||||
|
||||
match nut05.get_settings(&unit, &PaymentMethod::Bolt11) {
|
||||
Some(settings) => {
|
||||
if settings
|
||||
.max_amount
|
||||
.map_or(false, |max_amount| amount > max_amount)
|
||||
{
|
||||
return Err(Error::MeltOverLimit);
|
||||
}
|
||||
|
||||
if settings
|
||||
.min_amount
|
||||
.map_or(false, |min_amount| amount < min_amount)
|
||||
{
|
||||
return Err(Error::MeltUnderLimit);
|
||||
}
|
||||
}
|
||||
None => {
|
||||
return Err(Error::UnsupportedUnit);
|
||||
}
|
||||
}
|
||||
|
||||
let quote = MeltQuote::new(
|
||||
request,
|
||||
unit,
|
||||
|
||||
@@ -222,6 +222,21 @@ impl Settings {
|
||||
pub fn new(methods: Vec<MintMethodSettings>, disabled: bool) -> Self {
|
||||
Self { methods, disabled }
|
||||
}
|
||||
|
||||
/// Get [`MintMethodSettings`] for unit method pair
|
||||
pub fn get_settings(
|
||||
&self,
|
||||
unit: &CurrencyUnit,
|
||||
method: &PaymentMethod,
|
||||
) -> Option<MintMethodSettings> {
|
||||
for method_settings in self.methods.iter() {
|
||||
if method_settings.method.eq(method) && method_settings.unit.eq(unit) {
|
||||
return Some(method_settings.clone());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
impl Default for Settings {
|
||||
|
||||
@@ -257,6 +257,21 @@ impl Settings {
|
||||
pub fn new(methods: Vec<MeltMethodSettings>, disabled: bool) -> Self {
|
||||
Self { methods, disabled }
|
||||
}
|
||||
|
||||
/// Get [`MeltMethodSettings`] for unit method pair
|
||||
pub fn get_settings(
|
||||
&self,
|
||||
unit: &CurrencyUnit,
|
||||
method: &PaymentMethod,
|
||||
) -> Option<MeltMethodSettings> {
|
||||
for method_settings in self.methods.iter() {
|
||||
if method_settings.method.eq(method) && method_settings.unit.eq(unit) {
|
||||
return Some(method_settings.clone());
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
/// Melt Settings
|
||||
|
||||
Reference in New Issue
Block a user