@alleyboss/micropay-solana-x402-paywall 3.2.2 → 3.3.1
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 +45 -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 +229 -5
- package/dist/client/index.d.cts +101 -1
- package/dist/client/index.d.ts +101 -1
- package/dist/client/index.js +227 -7
- package/dist/index.cjs +287 -159
- package/dist/index.d.cts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +285 -161
- package/dist/next/index.cjs +91 -28
- package/dist/next/index.js +91 -28
- 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 +2 -2
package/dist/pricing/index.js
CHANGED
|
@@ -4,12 +4,10 @@ function lamportsToSol(lamports) {
|
|
|
4
4
|
}
|
|
5
5
|
|
|
6
6
|
// src/pricing/index.ts
|
|
7
|
-
var
|
|
8
|
-
var
|
|
9
|
-
var lastProviderIndex = -1;
|
|
7
|
+
var priceCache = null;
|
|
8
|
+
var currentConfig = {};
|
|
10
9
|
function configurePricing(newConfig) {
|
|
11
|
-
|
|
12
|
-
cachedPrice = null;
|
|
10
|
+
currentConfig = { ...currentConfig, ...newConfig };
|
|
13
11
|
}
|
|
14
12
|
var PROVIDERS = [
|
|
15
13
|
{
|
|
@@ -49,56 +47,75 @@ async function fetchFromProvider(provider, timeout) {
|
|
|
49
47
|
if (!price || price <= 0) {
|
|
50
48
|
throw new Error("Invalid price");
|
|
51
49
|
}
|
|
52
|
-
return price;
|
|
50
|
+
return { price, source: provider.name };
|
|
53
51
|
} finally {
|
|
54
52
|
clearTimeout(timeoutId);
|
|
55
53
|
}
|
|
56
54
|
}
|
|
55
|
+
async function fetchPriceParallel(timeout) {
|
|
56
|
+
const promises = PROVIDERS.map(
|
|
57
|
+
(provider) => fetchFromProvider(provider, timeout).catch(() => null)
|
|
58
|
+
);
|
|
59
|
+
const results = await Promise.all(promises);
|
|
60
|
+
const validResult = results.find((r) => r !== null);
|
|
61
|
+
if (validResult) {
|
|
62
|
+
return validResult;
|
|
63
|
+
}
|
|
64
|
+
throw new Error("All providers failed");
|
|
65
|
+
}
|
|
66
|
+
async function fetchPriceSequential(timeout) {
|
|
67
|
+
for (const provider of PROVIDERS) {
|
|
68
|
+
try {
|
|
69
|
+
return await fetchFromProvider(provider, timeout);
|
|
70
|
+
} catch {
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
throw new Error("All providers failed");
|
|
75
|
+
}
|
|
57
76
|
async function getSolPrice() {
|
|
58
|
-
const cacheTTL =
|
|
59
|
-
const timeout =
|
|
60
|
-
|
|
61
|
-
|
|
77
|
+
const cacheTTL = currentConfig.cacheTTL ?? 6e4;
|
|
78
|
+
const timeout = currentConfig.timeout ?? 3e3;
|
|
79
|
+
const useParallel = currentConfig.parallelFetch ?? true;
|
|
80
|
+
const now = Date.now();
|
|
81
|
+
if (priceCache && now - priceCache.timestamp < cacheTTL) {
|
|
82
|
+
return priceCache.data;
|
|
62
83
|
}
|
|
63
|
-
if (
|
|
84
|
+
if (currentConfig.customProvider) {
|
|
64
85
|
try {
|
|
65
|
-
const price = await
|
|
86
|
+
const price = await currentConfig.customProvider();
|
|
66
87
|
if (price > 0) {
|
|
67
|
-
|
|
88
|
+
const data = {
|
|
68
89
|
solPrice: price,
|
|
69
90
|
fetchedAt: /* @__PURE__ */ new Date(),
|
|
70
91
|
source: "custom"
|
|
71
92
|
};
|
|
72
|
-
|
|
93
|
+
priceCache = { data, timestamp: now };
|
|
94
|
+
return data;
|
|
73
95
|
}
|
|
74
96
|
} catch {
|
|
75
97
|
}
|
|
76
98
|
}
|
|
77
|
-
|
|
78
|
-
const
|
|
79
|
-
const
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
99
|
+
try {
|
|
100
|
+
const result = useParallel ? await fetchPriceParallel(timeout) : await fetchPriceSequential(timeout);
|
|
101
|
+
const data = {
|
|
102
|
+
solPrice: result.price,
|
|
103
|
+
fetchedAt: /* @__PURE__ */ new Date(),
|
|
104
|
+
source: result.source
|
|
105
|
+
};
|
|
106
|
+
priceCache = { data, timestamp: now };
|
|
107
|
+
return data;
|
|
108
|
+
} catch {
|
|
109
|
+
if (priceCache) {
|
|
110
|
+
return {
|
|
111
|
+
...priceCache.data,
|
|
112
|
+
source: `${priceCache.data.source} (stale)`
|
|
87
113
|
};
|
|
88
|
-
return cachedPrice;
|
|
89
|
-
} catch {
|
|
90
|
-
continue;
|
|
91
114
|
}
|
|
115
|
+
throw new Error(
|
|
116
|
+
"Failed to fetch SOL price from all providers. Configure a custom provider or ensure network connectivity."
|
|
117
|
+
);
|
|
92
118
|
}
|
|
93
|
-
if (cachedPrice) {
|
|
94
|
-
return {
|
|
95
|
-
...cachedPrice,
|
|
96
|
-
source: `${cachedPrice.source} (stale)`
|
|
97
|
-
};
|
|
98
|
-
}
|
|
99
|
-
throw new Error(
|
|
100
|
-
"Failed to fetch SOL price from all providers. Configure a custom provider or ensure network connectivity."
|
|
101
|
-
);
|
|
102
119
|
}
|
|
103
120
|
async function lamportsToUsd(lamports) {
|
|
104
121
|
const { solPrice } = await getSolPrice();
|
|
@@ -126,8 +143,7 @@ function formatPriceSync(lamports, solPrice) {
|
|
|
126
143
|
};
|
|
127
144
|
}
|
|
128
145
|
function clearPriceCache() {
|
|
129
|
-
|
|
130
|
-
lastProviderIndex = -1;
|
|
146
|
+
priceCache = null;
|
|
131
147
|
}
|
|
132
148
|
function getProviders() {
|
|
133
149
|
return PROVIDERS.map((p) => ({ name: p.name, url: p.url }));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alleyboss/micropay-solana-x402-paywall",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.3.1",
|
|
4
4
|
"description": "Production-ready Solana micropayments library wrapper for official x402 SDK",
|
|
5
5
|
"author": "AlleyBoss",
|
|
6
6
|
"license": "MIT",
|
|
@@ -135,4 +135,4 @@
|
|
|
135
135
|
"engines": {
|
|
136
136
|
"node": ">=18"
|
|
137
137
|
}
|
|
138
|
-
}
|
|
138
|
+
}
|