r402_near/lib.rs
1#![cfg_attr(docsrs, feature(doc_cfg))]
2#![cfg_attr(
3 test,
4 allow(
5 unknown_lints,
6 clippy::unused_async_trait_impl,
7 reason = "in-crate mock impls of AFIT traits have no .await"
8 )
9)]
10
11//! NEAR chain support for the x402 payment protocol.
12//!
13//! This crate implements the x402 `"exact"` scheme for NEAR: the buyer signs a
14//! NEP-366 `SignedDelegate` that authorizes one NEP-141 `ft_transfer`, and a
15//! facilitator-sponsored relayer submits the outer transaction.
16//!
17//! # Features
18//!
19//! - **CAIP-2 Addressing**: `near:mainnet` and `near:testnet`
20//! - **NEP-141 Payments**: exact `ft_transfer` of a token (USDC by default)
21//! - **NEP-366 Meta-transactions**: client signs a delegate; the relayer pays gas
22//! - **In-process facilitator**: JSON-RPC verify and settle
23//!
24//! # Feature Flags
25//!
26//! - `server` — server-side price tag generation
27//! - `client` — client-side delegate signing
28//! - `facilitator` — facilitator-side payment verification and settlement
29//! - `telemetry` — `tracing` instrumentation
30
31#[cfg(not(any(feature = "client", feature = "facilitator")))]
32use base64 as _;
33#[cfg(feature = "telemetry")]
34use tracing_core as _;
35
36/// Default gas attached to the delegated `ft_transfer` (30 `TGas`).
37pub const DEFAULT_FT_TRANSFER_GAS: u64 = 30_000_000_000_000;
38
39/// Conservative cap on sponsored gas to protect relayers (100 `TGas`).
40pub const DEFAULT_MAX_SPONSORED_GAS: u64 = 100_000_000_000_000;
41
42/// NEP-141 requires exactly 1 yoctoNEAR attached to `ft_transfer`.
43pub const ONE_YOCTO: u128 = 1;
44
45/// Deterministic timeout mapping (spec §5): `estimatedBlockSeconds = 1`.
46pub const ESTIMATED_BLOCK_SECONDS: u64 = 1;
47
48/// NEAR delegate-action nonce upper-bound multiplier
49/// (`ACCESS_KEY_NONCE_RANGE_MULTIPLIER`).
50pub const NONCE_RANGE_MULTIPLIER: u64 = 1_000_000;
51
52/// Base58 representation of an all-zero 32-byte code hash.
53pub const EMPTY_CONTRACT_CODE_HASH: &str = "11111111111111111111111111111111";
54
55/// NEP-141 transfer method.
56pub const FT_TRANSFER_METHOD: &str = "ft_transfer";
57
58/// Default USDC decimal precision.
59pub const DEFAULT_TOKEN_DECIMALS: u8 = 6;
60
61pub mod chain;
62pub mod exact;
63
64mod networks;
65#[cfg(any(feature = "client", feature = "facilitator"))]
66pub use chain::NearJsonRpc;
67#[cfg(feature = "facilitator")]
68pub use chain::{NearChainProvider, NearRelayer};
69pub use exact::NearExact;
70#[cfg(feature = "client")]
71pub use exact::client::{NearExactClient, NearSigner};
72pub use networks::*;
73
74/// `timeoutBlocks = max(1, ceil(maxTimeoutSeconds / estimatedBlockSeconds))`.
75#[must_use]
76pub const fn timeout_blocks(max_timeout_seconds: u64) -> u64 {
77 let blocks = max_timeout_seconds.div_ceil(ESTIMATED_BLOCK_SECONDS);
78 if blocks == 0 { 1 } else { blocks }
79}