feat: mint return error codes

This commit is contained in:
thesimplekid
2024-09-05 22:05:27 +01:00
parent 5556080049
commit bf5b953738
2 changed files with 97 additions and 56 deletions

View File

@@ -228,32 +228,64 @@ pub enum ErrorCode {
TokenNotVerified,
/// Lightning Error
LightningError,
/// Unbalanced Error
TransactionUnbalanced,
/// Unknown error code
Unknown(u16),
}
impl ErrorCode {
/// Error code from u16
pub fn from_code(code: u16) -> Self {
match code {
10002 => Self::BlindedMessageAlreadySigned,
10003 => Self::TokenNotVerified,
11001 => Self::TokenAlreadySpent,
11002 => Self::TransactionUnbalanced,
11005 => Self::UnitUnsupported,
11006 => Self::InsufficientFee,
11007 => Self::FeeOverPaid,
12001 => Self::KeysetNotFound,
12002 => Self::KeysetInactive,
20000 => Self::LightningError,
20001 => Self::QuoteNotPaid,
20002 => Self::TokensAlreadyIssued,
20003 => Self::MintingDisabled,
20005 => Self::QuotePending,
20006 => Self::InvoiceAlreadyPaid,
_ => Self::Unknown(code),
}
}
/// Error code to u16
pub fn to_code(&self) -> u16 {
match self {
Self::BlindedMessageAlreadySigned => 10002,
Self::TokenNotVerified => 10003,
Self::TokenAlreadySpent => 11001,
Self::TransactionUnbalanced => 11002,
Self::UnitUnsupported => 11005,
Self::InsufficientFee => 11006,
Self::FeeOverPaid => 11007,
Self::KeysetNotFound => 12001,
Self::KeysetInactive => 12002,
Self::LightningError => 20000,
Self::QuoteNotPaid => 20001,
Self::TokensAlreadyIssued => 20002,
Self::MintingDisabled => 20003,
Self::QuotePending => 20005,
Self::InvoiceAlreadyPaid => 20006,
Self::Unknown(code) => *code,
}
}
}
impl Serialize for ErrorCode {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
serializer.serialize_u16(match self {
Self::TokenAlreadySpent => 11001,
Self::LightningError => 20003,
Self::QuoteNotPaid => 20001,
Self::KeysetNotFound => 12001,
Self::KeysetInactive => 12002,
Self::FeeOverPaid => 11003,
Self::InsufficientFee => 11002,
Self::BlindedMessageAlreadySigned => 10002,
Self::UnitUnsupported => 11005,
Self::TokensAlreadyIssued => 20002,
Self::MintingDisabled => 20003,
Self::QuotePending => 20005,
Self::InvoiceAlreadyPaid => 20006,
Self::TokenNotVerified => 10003,
Self::Unknown(code) => *code,
})
serializer.serialize_u16(self.to_code())
}
}
@@ -264,47 +296,12 @@ impl<'de> Deserialize<'de> for ErrorCode {
{
let code = u16::deserialize(deserializer)?;
let error_code = match code {
10002 => Self::BlindedMessageAlreadySigned,
10003 => Self::TokenNotVerified,
11001 => Self::TokenAlreadySpent,
11002 => Self::InsufficientFee,
11003 => Self::FeeOverPaid,
11005 => Self::UnitUnsupported,
12001 => Self::KeysetNotFound,
12002 => Self::KeysetInactive,
20000 => Self::LightningError,
20001 => Self::QuoteNotPaid,
20002 => Self::TokensAlreadyIssued,
20003 => Self::MintingDisabled,
20005 => Self::QuotePending,
20006 => Self::InvoiceAlreadyPaid,
c => Self::Unknown(c),
};
Ok(error_code)
Ok(ErrorCode::from_code(code))
}
}
impl fmt::Display for ErrorCode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let code = match self {
Self::TokenAlreadySpent => 11001,
Self::QuoteNotPaid => 20001,
Self::KeysetNotFound => 12001,
Self::KeysetInactive => 12002,
Self::FeeOverPaid => 11003,
Self::InsufficientFee => 11002,
Self::BlindedMessageAlreadySigned => 10002,
Self::TokenNotVerified => 10003,
Self::UnitUnsupported => 11005,
Self::LightningError => 20003,
Self::TokensAlreadyIssued => 20002,
Self::MintingDisabled => 20003,
Self::QuotePending => 20005,
Self::InvoiceAlreadyPaid => 20006,
Self::Unknown(code) => *code,
};
write!(f, "{}", code)
write!(f, "{}", self.to_code())
}
}

View File

@@ -24,10 +24,10 @@ pub enum Error {
#[error("Amount Overflow")]
AmountOverflow,
/// Not engough inputs provided
#[error("Inputs: `{0}`, Outputs: `{0}`, Fee: `{0}`")]
#[error("Inputs: `{0}`, Outputs: `{0}`, Expected Fee: `{0}`")]
InsufficientInputs(u64, u64, u64),
/// Transaction unbalanced
#[error("Inputs: `{0}`, Outputs: `{0}`, Fee: `{0}`")]
#[error("Inputs: `{0}`, Outputs: `{0}`, Expected Fee: `{0}`")]
TransactionUnbalanced(u64, u64, u64),
/// Duplicate proofs provided
#[error("Duplicate proofs")]
@@ -135,6 +135,50 @@ impl From<Error> for ErrorResponse {
error: Some(err.to_string()),
detail: None,
},
Error::TransactionUnbalanced(_inputs_amount, _output_amouns, _expected_fee) => {
ErrorResponse {
code: ErrorCode::TransactionUnbalanced,
error: Some(err.to_string()),
detail: None,
}
}
Error::InsufficientInputs(_inputs_amount, _output_amount, _expected_fee) => {
ErrorResponse {
code: ErrorCode::InsufficientFee,
error: Some(err.to_string()),
detail: None,
}
}
Error::MintingDisabled => ErrorResponse {
code: ErrorCode::MintingDisabled,
error: Some(err.to_string()),
detail: None,
},
Error::InactiveKeyset => ErrorResponse {
code: ErrorCode::KeysetInactive,
error: Some(err.to_string()),
detail: None,
},
Error::UnknownKeySet => ErrorResponse {
code: ErrorCode::KeysetNotFound,
error: Some(err.to_string()),
detail: None,
},
Error::UnpaidQuote => ErrorResponse {
code: ErrorCode::QuoteNotPaid,
error: Some(err.to_string()),
detail: None,
},
Error::PendingQuote => ErrorResponse {
code: ErrorCode::QuotePending,
error: Some(err.to_string()),
detail: None,
},
Error::IssuedQuote => ErrorResponse {
code: ErrorCode::TokensAlreadyIssued,
error: Some(err.to_string()),
detail: None,
},
_ => ErrorResponse {
code: ErrorCode::Unknown(9999),
error: Some(err.to_string()),