x402-mantle-sdk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,322 @@
1
+ /**
2
+ * Constants and Configuration
3
+ *
4
+ * Centralized configuration for networks and tokens
5
+ * Supports both preset networks (mainnet/testnet) and custom configurations
6
+ */
7
+ /** Supported network identifiers */
8
+ type NetworkId = 'mantle' | 'mantle-sepolia' | 'mantle-testnet';
9
+ /** Network environment */
10
+ type NetworkEnvironment = 'mainnet' | 'testnet';
11
+ /** Network configuration */
12
+ interface NetworkConfig {
13
+ chainId: number;
14
+ rpcUrl: string;
15
+ name: string;
16
+ environment: NetworkEnvironment;
17
+ nativeCurrency: {
18
+ name: string;
19
+ symbol: string;
20
+ decimals: number;
21
+ };
22
+ blockExplorer: string;
23
+ }
24
+ /** Token configuration */
25
+ interface TokenConfig {
26
+ address: string;
27
+ decimals: number;
28
+ symbol: string;
29
+ }
30
+ /** Custom network configuration (user-provided) */
31
+ interface CustomNetworkConfig {
32
+ chainId: number;
33
+ rpcUrl: string;
34
+ name?: string;
35
+ environment?: NetworkEnvironment;
36
+ blockExplorer?: string;
37
+ }
38
+ /** Custom token configuration (user-provided) */
39
+ interface CustomTokenConfig {
40
+ [symbol: string]: {
41
+ address: string;
42
+ decimals: number;
43
+ };
44
+ }
45
+ /** Network configurations */
46
+ declare const NETWORKS: Record<NetworkId, NetworkConfig>;
47
+ /** Token addresses by network */
48
+ declare const TOKENS: Record<NetworkId, Record<string, TokenConfig>>;
49
+ /** Treasury contract address (collects 0.5% platform fee) */
50
+ declare const TREASURY_ADDRESS = "0xB27705342ACE73736AE490540Ea031cc06C3eF49";
51
+ /** Platform fee in basis points (50 = 0.5%) */
52
+ declare const PLATFORM_FEE_BPS = 50n;
53
+ /** Treasury admin address */
54
+ declare const TREASURY_ADMIN = "0xDacCF30F8BB2aEb8D76E68E03033629809ed08E1";
55
+ /**
56
+ * Register a custom network configuration
57
+ *
58
+ * @example
59
+ * ```typescript
60
+ * registerCustomNetwork('my-network', {
61
+ * chainId: 12345,
62
+ * rpcUrl: 'https://my-rpc.example.com',
63
+ * name: 'My Custom Network',
64
+ * environment: 'testnet'
65
+ * })
66
+ * ```
67
+ */
68
+ declare function registerCustomNetwork(networkId: string, config: CustomNetworkConfig): void;
69
+ /**
70
+ * Register custom tokens for a network
71
+ *
72
+ * @example
73
+ * ```typescript
74
+ * registerCustomTokens('mantle', {
75
+ * 'MYTOKEN': { address: '0x...', decimals: 18 }
76
+ * })
77
+ * ```
78
+ */
79
+ declare function registerCustomTokens(networkId: string, tokens: CustomTokenConfig): void;
80
+ /**
81
+ * Check if network is testnet
82
+ */
83
+ declare function isTestnet(network: string): boolean;
84
+ /**
85
+ * Check if network is mainnet
86
+ */
87
+ declare function isMainnet(network: string): boolean;
88
+ /**
89
+ * Get network configuration (supports preset and custom networks)
90
+ */
91
+ declare function getNetworkConfig(network: string): NetworkConfig;
92
+ /**
93
+ * Get token configuration (supports preset and custom tokens)
94
+ */
95
+ declare function getTokenConfig(token: string, network: string): TokenConfig | null;
96
+ /**
97
+ * Get chain ID for network
98
+ */
99
+ declare function getChainId(network: string): number;
100
+ /**
101
+ * Get all available networks (preset + custom)
102
+ */
103
+ declare function getAvailableNetworks(): string[];
104
+ /**
105
+ * Get networks by environment
106
+ */
107
+ declare function getNetworksByEnvironment(env: NetworkEnvironment): string[];
108
+
109
+ /**
110
+ * Type definitions for x402 client SDK
111
+ */
112
+
113
+ /** Payment request from HTTP 402 response */
114
+ interface PaymentRequest {
115
+ amount: string;
116
+ token: string;
117
+ network: string;
118
+ chainId?: number;
119
+ recipient: string;
120
+ }
121
+ /** Payment response after successful payment */
122
+ interface PaymentResponse {
123
+ transactionHash: string;
124
+ timestamp?: string;
125
+ }
126
+ /** x402 Client options */
127
+ interface X402ClientOptions {
128
+ /** Wallet provider (MetaMask, WalletConnect, etc.) */
129
+ walletProvider?: WalletProvider;
130
+ /** Use testnet (mantle-sepolia) */
131
+ testnet?: boolean;
132
+ /** Network identifier (e.g., 'mantle', 'mantle-sepolia') */
133
+ network?: string;
134
+ /** Custom network configuration */
135
+ customNetwork?: CustomNetworkConfig;
136
+ /** Custom token configurations */
137
+ customTokens?: CustomTokenConfig;
138
+ /** Custom payment modal handler */
139
+ paymentModal?: (request: PaymentRequest) => Promise<PaymentResponse | null>;
140
+ /** Auto-retry after payment (default: true) */
141
+ autoRetry?: boolean;
142
+ /** Auto-switch network if needed (default: true) */
143
+ autoSwitchNetwork?: boolean;
144
+ }
145
+ /** EIP-1193 provider request arguments */
146
+ interface EIP1193RequestArgs {
147
+ method: string;
148
+ params?: readonly unknown[] | object;
149
+ }
150
+ /** EIP-1193 compatible provider */
151
+ interface EIP1193Provider {
152
+ request(args: EIP1193RequestArgs): Promise<unknown>;
153
+ on?(event: string, listener: (...args: unknown[]) => void): void;
154
+ removeListener?(event: string, listener: (...args: unknown[]) => void): void;
155
+ isMetaMask?: boolean;
156
+ }
157
+ /** Network parameters for wallet_addEthereumChain */
158
+ interface AddEthereumChainParameter {
159
+ chainId: string;
160
+ chainName: string;
161
+ nativeCurrency: {
162
+ name: string;
163
+ symbol: string;
164
+ decimals: number;
165
+ };
166
+ rpcUrls: string[];
167
+ blockExplorerUrls?: string[];
168
+ }
169
+ /** Wallet provider interface */
170
+ interface WalletProvider {
171
+ /** Check if wallet is available */
172
+ isAvailable(): boolean;
173
+ /** Connect wallet and return address */
174
+ connect(): Promise<string>;
175
+ /** Get current account */
176
+ getAccount(): Promise<string | null>;
177
+ /** Get current chain ID */
178
+ getChainId(): Promise<number | null>;
179
+ /** Switch network */
180
+ switchNetwork(chainId: number): Promise<void>;
181
+ /** Add and switch to network */
182
+ addNetwork(params: AddEthereumChainParameter): Promise<void>;
183
+ /** Send transaction */
184
+ sendTransaction(tx: TransactionRequest): Promise<string>;
185
+ /** Sign message (optional) */
186
+ signMessage?(message: string): Promise<string>;
187
+ }
188
+ /** Transaction request */
189
+ interface TransactionRequest {
190
+ to: string;
191
+ value?: string;
192
+ data?: string;
193
+ gasLimit?: string;
194
+ gasPrice?: string;
195
+ }
196
+
197
+ /**
198
+ * x402 Client
199
+ *
200
+ * Main client that intercepts HTTP 402 responses and handles payment flow
201
+ */
202
+
203
+ /**
204
+ * x402 Client class
205
+ *
206
+ * Provides automatic HTTP 402 handling for fetch requests
207
+ */
208
+ declare class X402Client {
209
+ private options;
210
+ private wallet;
211
+ constructor(options?: X402ClientOptions);
212
+ /**
213
+ * Initialize wallet connection
214
+ */
215
+ initialize(): Promise<void>;
216
+ /**
217
+ * Fetch with automatic 402 handling
218
+ */
219
+ fetch(input: RequestInfo | URL, init?: RequestInit): Promise<Response>;
220
+ }
221
+ /**
222
+ * Create x402 client instance
223
+ */
224
+ declare function x402Client(options?: X402ClientOptions): X402Client;
225
+ /**
226
+ * Enhanced fetch with automatic 402 handling
227
+ *
228
+ * @example
229
+ * ```typescript
230
+ * const response = await x402Fetch('https://api.example.com/data')
231
+ * ```
232
+ */
233
+ declare function x402Fetch(input: RequestInfo | URL, init?: RequestInit, options?: X402ClientOptions): Promise<Response>;
234
+
235
+ /**
236
+ * Payment Modal
237
+ *
238
+ * Default payment modal implementation using vanilla JS
239
+ */
240
+
241
+ /**
242
+ * Create payment modal
243
+ *
244
+ * Uses vanilla JS modal that works in any browser environment
245
+ */
246
+ declare function createPaymentModal(request: PaymentRequest): Promise<PaymentResponse | null>;
247
+
248
+ /**
249
+ * Vanilla JS Payment Modal
250
+ *
251
+ * Creates a payment modal using vanilla JS/DOM
252
+ */
253
+
254
+ /**
255
+ * Create vanilla JS payment modal
256
+ */
257
+ declare function createVanillaPaymentModal(request: PaymentRequest): Promise<PaymentResponse | null>;
258
+
259
+ /**
260
+ * Wallet Connection & Management
261
+ *
262
+ * Handles web3 wallet connections (MetaMask, WalletConnect, etc.)
263
+ * Supports network switching and custom network addition
264
+ */
265
+
266
+ /**
267
+ * MetaMask/EIP-1193 wallet provider
268
+ */
269
+ declare class MetaMaskProvider implements WalletProvider {
270
+ private ethereum;
271
+ constructor();
272
+ isAvailable(): boolean;
273
+ connect(): Promise<string>;
274
+ getAccount(): Promise<string | null>;
275
+ getChainId(): Promise<number | null>;
276
+ switchNetwork(chainId: number): Promise<void>;
277
+ addNetwork(params: AddEthereumChainParameter): Promise<void>;
278
+ sendTransaction(tx: TransactionRequest): Promise<string>;
279
+ signMessage(message: string): Promise<string>;
280
+ }
281
+ /**
282
+ * Auto-detect wallet provider
283
+ */
284
+ declare function detectWalletProvider(): WalletProvider | null;
285
+ /**
286
+ * Connect wallet (auto-detect or use provided)
287
+ */
288
+ declare function connectWallet(provider?: WalletProvider): Promise<string>;
289
+ /**
290
+ * Ensure wallet is on the correct network
291
+ *
292
+ * @param wallet - Wallet provider
293
+ * @param network - Network identifier (e.g., 'mantle', 'mantle-sepolia')
294
+ * @param autoAdd - Auto-add network if not found (default: true)
295
+ */
296
+ declare function ensureNetwork(wallet: WalletProvider, network: string, autoAdd?: boolean): Promise<void>;
297
+ /**
298
+ * Get network config for adding to wallet
299
+ */
300
+ declare function getAddNetworkParams(network: string): AddEthereumChainParameter;
301
+
302
+ /**
303
+ * Payment Processing
304
+ *
305
+ * Handles direct blockchain payments with automatic fee splitting
306
+ * Automatically splits 0.5% platform fee to Treasury
307
+ */
308
+
309
+ /**
310
+ * Process payment for an x402 request
311
+ *
312
+ * Automatically splits payment:
313
+ * - 99.5% to merchant
314
+ * - 0.5% to Treasury
315
+ *
316
+ * @param request - Payment request from HTTP 402 response
317
+ * @param wallet - Wallet provider to use for transaction
318
+ * @returns Payment response with transaction hash
319
+ */
320
+ declare function processPayment(request: PaymentRequest, wallet: WalletProvider): Promise<PaymentResponse>;
321
+
322
+ export { type AddEthereumChainParameter, type CustomNetworkConfig, type CustomTokenConfig, type EIP1193Provider, MetaMaskProvider, NETWORKS, type NetworkConfig, type NetworkEnvironment, type NetworkId, PLATFORM_FEE_BPS, type PaymentRequest, type PaymentResponse, TOKENS, TREASURY_ADDRESS, TREASURY_ADMIN, type TokenConfig, type TransactionRequest, type WalletProvider, X402Client, type X402ClientOptions, connectWallet, createPaymentModal, createVanillaPaymentModal, detectWalletProvider, ensureNetwork, getAddNetworkParams, getAvailableNetworks, getChainId, getNetworkConfig, getNetworksByEnvironment, getTokenConfig, isMainnet, isTestnet, processPayment, registerCustomNetwork, registerCustomTokens, x402Client, x402Fetch };