Skip to main content

X402Challenge

Struct X402Challenge 

Source
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: String

Payment scheme, e.g. "exact" (the canonical x402 scheme).

§asset: String

Settlement asset symbol, e.g. "USDC".

§network: Option<String>

Settlement network, e.g. "base-sepolia". Optional — informational.

§amount_atomic: u128

Quoted 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: u32

Asset 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

Source

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.

Source

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.

Source

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.

Source

pub fn summary(&self) -> String

A short human-readable summary of the quote, for alerts + audit reasons.

Trait Implementations§

Source§

impl Clone for X402Challenge

Source§

fn clone(&self) -> X402Challenge

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for X402Challenge

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for X402Challenge

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Eq for X402Challenge

Source§

impl PartialEq for X402Challenge

Source§

fn eq(&self, other: &X402Challenge) -> bool

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Inequality operator !=. Read more
Source§

impl Serialize for X402Challenge

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl StructuralPartialEq for X402Challenge

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,

Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Checks if this value is equivalent to the given key. Read more
Source§

impl<Q, K> Equivalent<K> for Q
where Q: Eq + ?Sized, K: Borrow<Q> + ?Sized,

Source§

fn equivalent(&self, key: &K) -> bool

Compare self to key and return true if they are equal.
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more