miden_usdcx/xreserve/encoding/
error.rs1use core::fmt;
7
8use miden_protocol::utils::serde::DeserializationError;
9use miden_standards::interop::eth::EthAmountError;
10
11use super::deposit_intent::DepositIntentField;
12use super::domain::CircleDomain;
13
14#[derive(Debug, Clone, PartialEq, Eq)]
15pub enum EncodingError {
16 LimbOutOfField,
17 LimbNotU32,
21 AmountTooLarge,
24 AmountOverCap,
26 ScaleExpTooLarge,
27 BadMagic,
28 BadVersion,
29 ZeroField {
30 field: DepositIntentField,
31 },
32 TruncatedHeader,
33 LengthMismatch,
34 HookDataTooLarge,
35 FieldNotAssetAmount {
40 field: DepositIntentField,
41 },
42 RemoteTokenMismatch,
44 RemoteDomainMismatch {
48 expected: CircleDomain,
49 actual: CircleDomain,
50 },
51 AccountIdOutOfRange,
52 NonCanonicalAccountId,
53 BurnItemsMalformed,
54 JsonSchema(String),
55 BinaryMagic,
56 BinaryLength,
57}
58
59impl fmt::Display for EncodingError {
60 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
61 match self {
62 Self::LimbOutOfField => write!(f, "a u64 limb is not a valid field element"),
63 Self::LimbNotU32 => write!(f, "packed felt exceeds u32 range"),
64 Self::AmountTooLarge => write!(f, "post-scale quotient does not fit a u64"),
65 Self::AmountOverCap => {
66 write!(f, "post-scale quotient exceeds the asset amount maximum")
67 },
68 Self::ScaleExpTooLarge => write!(f, "scale exponent exceeds 18"),
69 Self::BadMagic => write!(f, "deposit intent magic mismatch"),
70 Self::BadVersion => write!(f, "deposit intent version mismatch"),
71 Self::ZeroField { field } => write!(f, "deposit intent field {field:?} is zero"),
72 Self::TruncatedHeader => write!(f, "deposit intent header is shorter than 240 bytes"),
73 Self::LengthMismatch => write!(f, "deposit intent length relation violated"),
74 Self::HookDataTooLarge => write!(f, "hook data exceeds the note storage felt bound"),
75 Self::FieldNotAssetAmount { field } => {
76 write!(f, "deposit intent field {field:?} is not a valid asset amount")
77 },
78 Self::RemoteTokenMismatch => {
79 write!(f, "deposit intent remote token is not the faucet account id")
80 },
81 Self::RemoteDomainMismatch { expected, actual } => {
82 write!(
83 f,
84 "deposit intent remote domain {actual} is not the faucet's configured domain {expected}"
85 )
86 },
87 Self::AccountIdOutOfRange => {
88 write!(f, "bytes set outside the 16-byte account id region")
92 },
93 Self::NonCanonicalAccountId => {
94 write!(f, "bytes do not decode to a canonical account id")
95 },
96 Self::BurnItemsMalformed => write!(f, "burn note items have the wrong length or shape"),
97 Self::JsonSchema(msg) => write!(f, "circle json does not match the schema: {msg}"),
98 Self::BinaryMagic => write!(f, "circle binary decoder magic mismatch"),
99 Self::BinaryLength => write!(f, "circle binary length reconciliation failed"),
100 }
101 }
102}
103
104impl core::error::Error for EncodingError {}
105
106impl From<EncodingError> for DeserializationError {
107 fn from(error: EncodingError) -> Self {
112 Self::InvalidValue(error.to_string())
113 }
114}
115
116impl From<EthAmountError> for EncodingError {
117 fn from(error: EthAmountError) -> Self {
120 match error {
121 EthAmountError::ScaleTooLarge => Self::ScaleExpTooLarge,
122 EthAmountError::ScaledValueDoesNotFitU64 | EthAmountError::Overflow => {
125 Self::AmountTooLarge
126 },
127 EthAmountError::ScaledValueExceedsMaxFungibleAmount => Self::AmountOverCap,
128 }
129 }
130}