use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Protocol {
X402,
}
impl std::fmt::Display for Protocol {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Protocol::X402 => write!(f, "x402"),
}
}
}
#[derive(Debug, Clone)]
pub struct PaymentInfo {
pub amount: String,
pub network: String,
pub token: String,
}
#[derive(Debug, Clone)]
pub struct PayResult {
pub protocol: Protocol,
pub status: u16,
pub body: String,
pub payment: Option<PaymentInfo>,
}
#[derive(Debug, Clone)]
pub struct Service {
pub protocol: Protocol,
pub name: String,
pub url: String,
pub description: String,
pub price: String,
pub network: String,
pub tags: Vec<String>,
}
#[derive(Debug, Clone)]
pub struct DiscoverResult {
pub services: Vec<Service>,
pub total: u64,
pub limit: u64,
pub offset: u64,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PaymentRequirements {
pub scheme: String,
pub network: String,
#[serde(alias = "maxAmountRequired")]
pub amount: String,
pub asset: String,
#[serde(alias = "payTo")]
pub pay_to: String,
#[serde(default = "default_timeout")]
pub max_timeout_seconds: u64,
#[serde(default, skip_serializing_if = "is_json_null")]
pub extra: serde_json::Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub description: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub resource: Option<String>,
}
fn default_timeout() -> u64 {
30
}
fn is_json_null(value: &serde_json::Value) -> bool {
value.is_null()
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct X402Response {
#[serde(default)]
pub x402_version: Option<u32>,
pub accepts: Vec<PaymentRequirements>,
#[serde(default)]
pub resource: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(untagged)]
pub enum PaymentPayload {
V1(PaymentPayloadV1),
V2(PaymentPayloadV2),
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PaymentPayloadV1 {
pub x402_version: u32,
pub scheme: String,
pub network: String,
pub payload: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct PaymentPayloadV2 {
pub x402_version: u32,
pub accepted: PaymentRequirements,
pub resource: Option<serde_json::Value>,
pub payload: serde_json::Value,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Eip3009Payload {
pub signature: String,
pub authorization: Eip3009Authorization,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Eip3009Authorization {
pub from: String,
pub to: String,
pub value: String,
pub valid_after: String,
pub valid_before: String,
pub nonce: String,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DiscoveredService {
pub resource: String,
#[serde(default)]
pub r#type: Option<String>,
#[serde(default)]
pub x402_version: Option<u32>,
#[serde(default)]
pub accepts: Vec<PaymentRequirements>,
#[serde(default)]
pub metadata: Option<ServiceMetadata>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ServiceMetadata {
pub description: Option<String>,
#[serde(default)]
pub input: Option<serde_json::Value>,
#[serde(default)]
pub output: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DiscoveryResponse {
pub items: Vec<DiscoveredService>,
#[serde(default)]
pub pagination: Option<Pagination>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pagination {
pub limit: u64,
pub offset: u64,
pub total: u64,
}
#[derive(Debug, Clone, Serialize)]
pub struct MoonPayDepositRequest {
pub name: String,
pub wallet: String,
pub chain: String,
pub token: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct MoonPayDepositResponse {
pub id: String,
pub destination_wallet: String,
pub destination_chain: String,
pub customer_token: String,
pub deposit_url: String,
pub wallets: Vec<DepositWallet>,
pub instructions: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct DepositWallet {
pub address: String,
pub chain: String,
pub qr_code: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct MoonPayBalanceRequest {
pub wallet: String,
pub chain: String,
}
#[derive(Debug, Clone, Deserialize)]
pub struct MoonPayBalanceResponse {
pub items: Vec<TokenBalance>,
}
#[derive(Debug, Clone, Deserialize)]
pub struct TokenBalance {
pub address: String,
pub name: String,
pub symbol: String,
pub chain: String,
pub decimals: u32,
pub balance: BalanceInfo,
}
#[derive(Debug, Clone, Deserialize)]
pub struct BalanceInfo {
pub amount: f64,
pub value: f64,
pub price: f64,
}
#[derive(Debug, Clone)]
pub struct FundResult {
pub deposit_id: String,
pub deposit_url: String,
pub wallets: Vec<(String, String)>,
pub instructions: String,
}