use alloy_primitives::U256;
use serde::{Deserialize, Serialize};
use std::sync::LazyLock;
use x402_types::lit_str;
use x402_types::scheme::ExtensionKey;
use x402_types::timestamp::UnixTimestamp;
#[cfg(feature = "client")]
use alloy_sol_types::sol;
use crate::chain::{ChecksummedAddress, EOASignature};
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Eip2612GasSponsoring {
pub info: Eip2612GasSponsoringInfo,
}
impl Eip2612GasSponsoring {
pub fn server() -> Eip2612GasSponsoringServer {
Eip2612GasSponsoringServer::default()
}
}
impl ExtensionKey for Eip2612GasSponsoring {
const EXTENSION_KEY: &'static str = "eip2612GasSponsoring";
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Eip2612GasSponsoringServer {
pub info: Eip2612GasSponsoringServerInfo,
pub schema: serde_json::Value,
}
impl ExtensionKey for Eip2612GasSponsoringServer {
const EXTENSION_KEY: &'static str = Eip2612GasSponsoring::EXTENSION_KEY;
}
impl Default for Eip2612GasSponsoringServer {
fn default() -> Self {
Self {
info: Eip2612GasSponsoringServerInfo::default(),
schema: SCHEMA.clone(),
}
}
}
lit_str!(Eip2612GasSponsoringV1, "1");
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Eip2612GasSponsoringServerInfo {
pub description: String,
pub version: Eip2612GasSponsoringV1,
}
impl Default for Eip2612GasSponsoringServerInfo {
fn default() -> Self {
Self {
description:
"The facilitator accepts EIP-2612 gasless Permit to `Permit2` canonical contract."
.to_string(),
version: Eip2612GasSponsoringV1,
}
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct Eip2612GasSponsoringInfo {
pub from: ChecksummedAddress,
pub asset: ChecksummedAddress,
pub spender: ChecksummedAddress,
#[serde(with = "crate::decimal_u256")]
pub amount: U256,
#[serde(with = "crate::decimal_u256")]
pub nonce: U256,
pub deadline: UnixTimestamp,
pub signature: EOASignature,
pub version: Eip2612GasSponsoringV1,
}
#[cfg(feature = "client")]
sol! {
#[allow(missing_docs)]
#[derive(Debug)]
struct Permit {
address owner;
address spender;
uint256 value;
uint256 nonce;
uint256 deadline;
}
}
static SCHEMA: LazyLock<serde_json::Value> = LazyLock::new(|| {
serde_json::json!({
"$schema": "https://json-schema.org/draft/2020-12/schema",
"type": "object",
"properties": {
"from": {
"type": "string",
"pattern": "^0x[a-fA-F0-9]{40}$",
"description": "The address of the sender.",
},
"asset": {
"type": "string",
"pattern": "^0x[a-fA-F0-9]{40}$",
"description": "The address of the ERC-20 token contract.",
},
"spender": {
"type": "string",
"pattern": "^0x[a-fA-F0-9]{40}$",
"description": "The address of the spender (Canonical Permit2).",
},
"amount": {
"type": "string",
"pattern": "^[0-9]+$",
"description": "The amount to approve (uint256). Typically MaxUint.",
},
"nonce": {
"type": "string",
"pattern": "^[0-9]+$",
"description": "The current nonce of the sender.",
},
"deadline": {
"type": "string",
"pattern": "^[0-9]+$",
"description": "The timestamp at which the signature expires.",
},
"signature": {
"type": "string",
"pattern": "^0x[a-fA-F0-9]+$",
"description": "The 65-byte concatenated signature (r, s, v) as a hex string.",
},
"version": {
"type": "string",
"pattern": "^[0-9]+(\\.[0-9]+)*$",
"description": "Schema version identifier.",
},
},
"required": [
"from",
"asset",
"spender",
"amount",
"nonce",
"deadline",
"signature",
"version",
],
})
});