pub struct X402Challenge {
pub scheme: String,
pub asset: String,
pub network: Option<String>,
pub amount_atomic: u128,
pub decimals: u32,
pub pay_to: Option<String>,
pub resource: Option<String>,
}Expand description
A parsed x402 402 Payment Required challenge — the price quote an agent
must clear before a paywalled call succeeds.
Built from the JSON an x402 server returns ({ "x402Version", "accepts": [ requirements… ], "error"? }) via X402Challenge::from_body, or from a
single requirements object via X402Challenge::from_requirements. Field
names track the x402 PaymentRequirements shape; the amount is kept in the
asset’s smallest (atomic) unit, exactly as quoted, so no precision is lost
before normalization.
Fields§
§scheme: StringPayment scheme, e.g. "exact" (the canonical x402 scheme).
asset: StringSettlement asset symbol, e.g. "USDC".
network: Option<String>Settlement network, e.g. "base-sepolia". Optional — informational.
amount_atomic: u128Quoted amount in the asset’s atomic unit (e.g. 6-decimal USDC: 10_000
== $0.01). Kept exact; normalization to cents happens in
X402Challenge::to_cost_event.
decimals: u32Asset decimals used to interpret amount_atomic (USDC/USDT/PYUSD = 6,
DAI = 18).
pay_to: Option<String>Destination address the payment would settle to. Optional; recorded, not validated.
resource: Option<String>The paywalled resource URL the 402 guards. Optional.
Implementations§
Source§impl X402Challenge
impl X402Challenge
Sourcepub fn from_body(body: &Value) -> Option<Self>
pub fn from_body(body: &Value) -> Option<Self>
Parse the full body of an x402 402 response
({ "x402Version", "accepts": [ … ], "error"? }), taking the first
entry of accepts (x402 lists a server’s acceptable payment options
most-preferred-first). Returns None if the body has no usable
requirements object.
Sourcepub fn from_requirements(req: &Value) -> Option<Self>
pub fn from_requirements(req: &Value) -> Option<Self>
Parse a single x402 PaymentRequirements object.
Reads scheme (default "exact"), network, payTo, resource, the
atomic amount (maxAmountRequired | amount, string or number), and the
asset symbol + decimals (from extra.name/extra.decimals, or the
top-level asset/symbol/decimals). Missing decimals are inferred
from the known-stablecoin table. Returns None when no amount can be
read — an unquoted challenge is not a price.
Sourcepub fn to_cost_event(&self) -> Option<X402CostEvent>
pub fn to_cost_event(&self) -> Option<X402CostEvent>
Normalize the quoted price to a X402CostEvent in cents — the common
budget unit.
Only assets with a known 1:1 USD peg (the stablecoins in
[is_usd_pegged]) can be priced offline. For those,
cents = ⌈amount_atomic · 100 / 10^decimals⌉: integer math, rounded
up, so a sub-cent quote is never charged as free (which would let a
stream of dust payments slip the budget). Any other asset returns None
— see the module’s deny-by-default note.
Trait Implementations§
Source§impl Clone for X402Challenge
impl Clone for X402Challenge
Source§fn clone(&self) -> X402Challenge
fn clone(&self) -> X402Challenge
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreSource§impl Debug for X402Challenge
impl Debug for X402Challenge
Source§impl<'de> Deserialize<'de> for X402Challenge
impl<'de> Deserialize<'de> for X402Challenge
Source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
impl Eq for X402Challenge
Source§impl PartialEq for X402Challenge
impl PartialEq for X402Challenge
Source§impl Serialize for X402Challenge
impl Serialize for X402Challenge
impl StructuralPartialEq for X402Challenge
Auto Trait Implementations§
impl Freeze for X402Challenge
impl RefUnwindSafe for X402Challenge
impl Send for X402Challenge
impl Sync for X402Challenge
impl Unpin for X402Challenge
impl UnsafeUnpin for X402Challenge
impl UnwindSafe for X402Challenge
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> DeserializeOwned for Twhere
T: for<'de> Deserialize<'de>,
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§impl<Q, K> Equivalent<K> for Q
impl<Q, K> Equivalent<K> for Q
Source§fn equivalent(&self, key: &K) -> bool
fn equivalent(&self, key: &K) -> bool
key and return true if they are equal.