@alleyboss/micropay-solana-x402-paywall 3.2.2 → 3.3.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.
- package/README.md +30 -0
- package/dist/agent/index.cjs +2 -2
- package/dist/agent/index.d.cts +1 -1
- package/dist/agent/index.d.ts +1 -1
- package/dist/agent/index.js +2 -2
- package/dist/client/index.cjs +220 -0
- package/dist/client/index.d.cts +101 -1
- package/dist/client/index.d.ts +101 -1
- package/dist/client/index.js +218 -2
- package/dist/index.cjs +278 -154
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +276 -156
- package/dist/next/index.cjs +58 -23
- package/dist/next/index.js +58 -23
- package/dist/pricing/index.cjs +54 -38
- package/dist/pricing/index.d.cts +8 -9
- package/dist/pricing/index.d.ts +8 -9
- package/dist/pricing/index.js +54 -38
- package/package.json +1 -1
package/dist/client/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { decodePaymentRequiredHeader, encodePaymentSignatureHeader } from '@x402/core/http';
|
|
2
2
|
import { PublicKey, Transaction, SystemProgram, LAMPORTS_PER_SOL } from '@solana/web3.js';
|
|
3
|
-
import { useState, useCallback, useEffect } from 'react';
|
|
3
|
+
import { useState, useCallback, useEffect, useRef } from 'react';
|
|
4
4
|
|
|
5
5
|
// src/client/types.ts
|
|
6
6
|
var TOKEN_MINTS = {
|
|
@@ -239,4 +239,220 @@ function usePaywallResource({
|
|
|
239
239
|
};
|
|
240
240
|
}
|
|
241
241
|
|
|
242
|
-
|
|
242
|
+
// src/pricing/index.ts
|
|
243
|
+
var priceCache = null;
|
|
244
|
+
var currentConfig = {};
|
|
245
|
+
var PROVIDERS = [
|
|
246
|
+
{
|
|
247
|
+
name: "coincap",
|
|
248
|
+
url: "https://api.coincap.io/v2/assets/solana",
|
|
249
|
+
parse: (data) => parseFloat(data.data?.priceUsd || "0")
|
|
250
|
+
},
|
|
251
|
+
{
|
|
252
|
+
name: "binance",
|
|
253
|
+
url: "https://api.binance.com/api/v3/ticker/price?symbol=SOLUSDT",
|
|
254
|
+
parse: (data) => parseFloat(data.price || "0")
|
|
255
|
+
},
|
|
256
|
+
{
|
|
257
|
+
name: "coingecko",
|
|
258
|
+
url: "https://api.coingecko.com/api/v3/simple/price?ids=solana&vs_currencies=usd",
|
|
259
|
+
parse: (data) => data.solana?.usd || 0
|
|
260
|
+
},
|
|
261
|
+
{
|
|
262
|
+
name: "kraken",
|
|
263
|
+
url: "https://api.kraken.com/0/public/Ticker?pair=SOLUSD",
|
|
264
|
+
parse: (data) => parseFloat(data.result?.SOLUSD?.c?.[0] || "0")
|
|
265
|
+
}
|
|
266
|
+
];
|
|
267
|
+
async function fetchFromProvider(provider, timeout) {
|
|
268
|
+
const controller = new AbortController();
|
|
269
|
+
const timeoutId = setTimeout(() => controller.abort(), timeout);
|
|
270
|
+
try {
|
|
271
|
+
const response = await fetch(provider.url, {
|
|
272
|
+
headers: { "Accept": "application/json" },
|
|
273
|
+
signal: controller.signal
|
|
274
|
+
});
|
|
275
|
+
if (!response.ok) {
|
|
276
|
+
throw new Error(`HTTP ${response.status}`);
|
|
277
|
+
}
|
|
278
|
+
const data = await response.json();
|
|
279
|
+
const price = provider.parse(data);
|
|
280
|
+
if (!price || price <= 0) {
|
|
281
|
+
throw new Error("Invalid price");
|
|
282
|
+
}
|
|
283
|
+
return { price, source: provider.name };
|
|
284
|
+
} finally {
|
|
285
|
+
clearTimeout(timeoutId);
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
async function fetchPriceParallel(timeout) {
|
|
289
|
+
const promises = PROVIDERS.map(
|
|
290
|
+
(provider) => fetchFromProvider(provider, timeout).catch(() => null)
|
|
291
|
+
);
|
|
292
|
+
const results = await Promise.all(promises);
|
|
293
|
+
const validResult = results.find((r) => r !== null);
|
|
294
|
+
if (validResult) {
|
|
295
|
+
return validResult;
|
|
296
|
+
}
|
|
297
|
+
throw new Error("All providers failed");
|
|
298
|
+
}
|
|
299
|
+
async function fetchPriceSequential(timeout) {
|
|
300
|
+
for (const provider of PROVIDERS) {
|
|
301
|
+
try {
|
|
302
|
+
return await fetchFromProvider(provider, timeout);
|
|
303
|
+
} catch {
|
|
304
|
+
continue;
|
|
305
|
+
}
|
|
306
|
+
}
|
|
307
|
+
throw new Error("All providers failed");
|
|
308
|
+
}
|
|
309
|
+
async function getSolPrice() {
|
|
310
|
+
const cacheTTL = currentConfig.cacheTTL ?? 6e4;
|
|
311
|
+
const timeout = currentConfig.timeout ?? 3e3;
|
|
312
|
+
const useParallel = currentConfig.parallelFetch ?? true;
|
|
313
|
+
const now = Date.now();
|
|
314
|
+
if (priceCache && now - priceCache.timestamp < cacheTTL) {
|
|
315
|
+
return priceCache.data;
|
|
316
|
+
}
|
|
317
|
+
if (currentConfig.customProvider) {
|
|
318
|
+
try {
|
|
319
|
+
const price = await currentConfig.customProvider();
|
|
320
|
+
if (price > 0) {
|
|
321
|
+
const data = {
|
|
322
|
+
solPrice: price,
|
|
323
|
+
fetchedAt: /* @__PURE__ */ new Date(),
|
|
324
|
+
source: "custom"
|
|
325
|
+
};
|
|
326
|
+
priceCache = { data, timestamp: now };
|
|
327
|
+
return data;
|
|
328
|
+
}
|
|
329
|
+
} catch {
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
try {
|
|
333
|
+
const result = useParallel ? await fetchPriceParallel(timeout) : await fetchPriceSequential(timeout);
|
|
334
|
+
const data = {
|
|
335
|
+
solPrice: result.price,
|
|
336
|
+
fetchedAt: /* @__PURE__ */ new Date(),
|
|
337
|
+
source: result.source
|
|
338
|
+
};
|
|
339
|
+
priceCache = { data, timestamp: now };
|
|
340
|
+
return data;
|
|
341
|
+
} catch {
|
|
342
|
+
if (priceCache) {
|
|
343
|
+
return {
|
|
344
|
+
...priceCache.data,
|
|
345
|
+
source: `${priceCache.data.source} (stale)`
|
|
346
|
+
};
|
|
347
|
+
}
|
|
348
|
+
throw new Error(
|
|
349
|
+
"Failed to fetch SOL price from all providers. Configure a custom provider or ensure network connectivity."
|
|
350
|
+
);
|
|
351
|
+
}
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
// src/client/hooks.ts
|
|
355
|
+
function usePricing(refreshIntervalMs = 6e4) {
|
|
356
|
+
const [priceData, setPriceData] = useState(null);
|
|
357
|
+
const [isLoading, setIsLoading] = useState(true);
|
|
358
|
+
const [error, setError] = useState(null);
|
|
359
|
+
const intervalRef = useRef(null);
|
|
360
|
+
const fetchPrice = useCallback(async () => {
|
|
361
|
+
try {
|
|
362
|
+
setIsLoading(true);
|
|
363
|
+
setError(null);
|
|
364
|
+
const data = await getSolPrice();
|
|
365
|
+
setPriceData(data);
|
|
366
|
+
} catch (err) {
|
|
367
|
+
setError(err instanceof Error ? err.message : "Failed to fetch price");
|
|
368
|
+
} finally {
|
|
369
|
+
setIsLoading(false);
|
|
370
|
+
}
|
|
371
|
+
}, []);
|
|
372
|
+
useEffect(() => {
|
|
373
|
+
fetchPrice();
|
|
374
|
+
if (refreshIntervalMs > 0) {
|
|
375
|
+
intervalRef.current = setInterval(fetchPrice, refreshIntervalMs);
|
|
376
|
+
}
|
|
377
|
+
return () => {
|
|
378
|
+
if (intervalRef.current) {
|
|
379
|
+
clearInterval(intervalRef.current);
|
|
380
|
+
}
|
|
381
|
+
};
|
|
382
|
+
}, [fetchPrice, refreshIntervalMs]);
|
|
383
|
+
return {
|
|
384
|
+
solPrice: priceData?.solPrice ?? null,
|
|
385
|
+
source: priceData?.source ?? null,
|
|
386
|
+
fetchedAt: priceData?.fetchedAt ?? null,
|
|
387
|
+
isLoading,
|
|
388
|
+
error,
|
|
389
|
+
refresh: fetchPrice
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
function useLamportsToUsd(lamports) {
|
|
393
|
+
const { solPrice, isLoading } = usePricing();
|
|
394
|
+
if (isLoading || !solPrice || lamports === null) {
|
|
395
|
+
return { usd: null, formatted: null, isLoading };
|
|
396
|
+
}
|
|
397
|
+
const lamportsBigInt = typeof lamports === "number" ? BigInt(lamports) : lamports;
|
|
398
|
+
const sol = Number(lamportsBigInt) / 1e9;
|
|
399
|
+
const usd = sol * solPrice;
|
|
400
|
+
return {
|
|
401
|
+
usd,
|
|
402
|
+
formatted: `$${usd.toFixed(2)}`,
|
|
403
|
+
isLoading: false
|
|
404
|
+
};
|
|
405
|
+
}
|
|
406
|
+
function useMicropay() {
|
|
407
|
+
const [status, setStatus] = useState("idle");
|
|
408
|
+
const [error, setError] = useState(null);
|
|
409
|
+
const [signature, setSignature] = useState(null);
|
|
410
|
+
const reset = useCallback(() => {
|
|
411
|
+
setStatus("idle");
|
|
412
|
+
setError(null);
|
|
413
|
+
setSignature(null);
|
|
414
|
+
}, []);
|
|
415
|
+
const pay = useCallback(async (_options) => {
|
|
416
|
+
setStatus("pending");
|
|
417
|
+
setError(null);
|
|
418
|
+
try {
|
|
419
|
+
throw new Error(
|
|
420
|
+
"useMicropay requires implementation of onSign/onSend callbacks. See documentation for wallet adapter integration."
|
|
421
|
+
);
|
|
422
|
+
} catch (err) {
|
|
423
|
+
const errorMessage = err instanceof Error ? err.message : "Payment failed";
|
|
424
|
+
setError(errorMessage);
|
|
425
|
+
setStatus("error");
|
|
426
|
+
return { success: false, error: errorMessage };
|
|
427
|
+
}
|
|
428
|
+
}, []);
|
|
429
|
+
return {
|
|
430
|
+
status,
|
|
431
|
+
error,
|
|
432
|
+
signature,
|
|
433
|
+
pay,
|
|
434
|
+
reset
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
function useFormatPrice(lamports) {
|
|
438
|
+
const { solPrice, isLoading } = usePricing();
|
|
439
|
+
if (isLoading || !solPrice || lamports === null) {
|
|
440
|
+
return {
|
|
441
|
+
sol: null,
|
|
442
|
+
usd: null,
|
|
443
|
+
formatted: "Loading...",
|
|
444
|
+
isLoading
|
|
445
|
+
};
|
|
446
|
+
}
|
|
447
|
+
const lamportsBigInt = typeof lamports === "number" ? BigInt(lamports) : lamports;
|
|
448
|
+
const sol = Number(lamportsBigInt) / 1e9;
|
|
449
|
+
const usd = sol * solPrice;
|
|
450
|
+
return {
|
|
451
|
+
sol,
|
|
452
|
+
usd,
|
|
453
|
+
formatted: `${sol.toFixed(4)} SOL (~$${usd.toFixed(2)})`,
|
|
454
|
+
isLoading: false
|
|
455
|
+
};
|
|
456
|
+
}
|
|
457
|
+
|
|
458
|
+
export { buildSolanaPayUrl, createPaymentFlow, createPaymentReference, createX402AuthorizationHeader, sendSolanaPayment, useFormatPrice, useLamportsToUsd, useMicropay, usePaywallResource, usePricing };
|