@dvmkit/dvm-cli 0.1.4 → 0.2.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 +14 -0
- package/dist/{_esm-M2HUAA3B.js → _esm-MN4BUYV6.js} +56 -54
- package/dist/{accounts-S747UW3A.js → accounts-2WZE4N5F.js} +5 -5
- package/dist/actions-LR65XILR.js +244 -0
- package/dist/{ccip-XZ5F5J6R.js → ccip-JXYJSXTW.js} +3 -3
- package/dist/{chains-PINFXYUE.js → chains-LJ7XK6H3.js} +8 -8
- package/dist/{chunk-YZUBKHHA.js → chunk-2VVVWBQA.js} +471 -471
- package/dist/chunk-3SWX5BQP.js +66 -0
- package/dist/chunk-4JXFXP4Y.js +757 -0
- package/dist/{chunk-GIDSTROG.js → chunk-4NWEJTAW.js} +160 -158
- package/dist/{chunk-UXQCP26E.js → chunk-65EY7CT4.js} +2 -2
- package/dist/{chunk-6UGZAFIR.js → chunk-6CC6JGEU.js} +348 -18
- package/dist/{chunk-PHZHPRQE.js → chunk-6O27TEXD.js} +173 -173
- package/dist/{chunk-HPRIQEJS.js → chunk-7OK7MBT6.js} +19 -19
- package/dist/{chunk-7UPZZGC5.js → chunk-BECPI5H5.js} +193 -193
- package/dist/chunk-CJTE5TS3.js +1298 -0
- package/dist/{chunk-RWBYCM47.js → chunk-ECEM3ZAS.js} +10 -10
- package/dist/{chunk-ACNNU5RY.js → chunk-ECK465AY.js} +694 -565
- package/dist/{chunk-XGABM4H5.js → chunk-EGY4KDBW.js} +3822 -2012
- package/dist/{chunk-SLDVIXCS.js → chunk-EVQERRCP.js} +12 -12
- package/dist/{chunk-4AQ4G7L5.js → chunk-EZHT2RLJ.js} +383 -383
- package/dist/{chunk-IC6OXFYW.js → chunk-F55NNH2F.js} +63 -63
- package/dist/chunk-G5C6E6PJ.js +9063 -0
- package/dist/{chunk-6AJ7H2VX.js → chunk-GZWKCIYN.js} +213 -1281
- package/dist/{chunk-ZGXCK43G.js → chunk-LH3QPVN6.js} +8499 -8498
- package/dist/{chunk-MMJR6N3J.js → chunk-MLDUKBU3.js} +1368 -1368
- package/dist/chunk-N3UJT2NJ.js +3 -0
- package/dist/chunk-N7MGIK2U.js +61408 -0
- package/dist/{chunk-BZNZWRBB.js → chunk-OWJZYYMG.js} +11 -15
- package/dist/chunk-ZJYO7OLO.js +514 -0
- package/dist/{client-D3XFLWOK.js → client-OWIOFBKS.js} +18 -16
- package/dist/{dist-7MMV3EZA.js → dist-2G3QBGID.js} +11 -10
- package/dist/dvm.js +27046 -4414
- package/dist/esm-RQLHAH2K.js +5183 -0
- package/dist/internal-JDG52DJH.js +1230 -0
- package/dist/payout-reporter-3UB5WRCV-XTVZFIAO.js +17 -0
- package/dist/{pocket-refill-GDYSTECR.js → pocket-refill-QJMMD6QN.js} +29 -30
- package/dist/revenue-reporter-JIKUPXOK-TYUBTF4P.js +11 -0
- package/dist/{secp256k1-4YWQEMGF.js → secp256k1-JLZEQ2H7.js} +2 -2
- package/dist/tempo-lifecycle-SQL3KLEZ-AATA2SKI.js +28 -0
- package/dist/{tempo-wallet-5FEOI4CF.js → tempo-wallet-QOLEIPCV-SWCXRBDS.js} +2 -4
- package/dist/{x402-VNFA3DBY.js → x402-FTG2GRAQ-UGAUMUL6.js} +13 -24
- package/package.json +2 -10
- package/skills/dvm/SKILL.md +2 -1
- package/dist/channel-store-GOV7TEDN.js +0 -62
- package/dist/chunk-3PPOU55E.js +0 -312
- package/dist/chunk-3TBULV3F.js +0 -551
- package/dist/chunk-5BUV73FO.js +0 -2351
- package/dist/chunk-EY5K4BM3.js +0 -1780
- package/dist/chunk-G3RLVJF5.js +0 -19
- package/dist/chunk-KC4UPK44.js +0 -113
- package/dist/chunk-MTBCEXJ6.js +0 -4602
- package/dist/chunk-ONQMRWY3.js +0 -83
- package/dist/chunk-TO47XRK7.js +0 -24890
- package/dist/chunk-U5TNSUYW.js +0 -1757
- package/dist/chunk-UU6QMSTQ.js +0 -171
- package/dist/chunk-ZELLQI7F.js +0 -248
- package/dist/lifecycle-5RLM5JMN.js +0 -29
- package/dist/tempo-lifecycle-DHZPQTSB.js +0 -31
- package/dist/wallet-G3VOUCL4.js +0 -30
|
@@ -2,17 +2,320 @@
|
|
|
2
2
|
import { createRequire as __dvmCreateRequire } from "node:module";
|
|
3
3
|
const require = __dvmCreateRequire(import.meta.url);
|
|
4
4
|
import {
|
|
5
|
+
BTC_RATE_FILE,
|
|
5
6
|
CREDITS_FILE,
|
|
6
7
|
DvmError,
|
|
7
8
|
ensureConfigDir,
|
|
9
|
+
ensureConfigSubdir,
|
|
8
10
|
require_proper_lockfile
|
|
9
|
-
} from "./chunk-
|
|
11
|
+
} from "./chunk-EGY4KDBW.js";
|
|
10
12
|
import {
|
|
11
13
|
__toESM
|
|
12
14
|
} from "./chunk-IYHTITNZ.js";
|
|
13
15
|
|
|
14
|
-
//
|
|
16
|
+
// node_modules/@dvmkit/sdk/dist/chunk-AT6V3SY7.js
|
|
17
|
+
var InvalidFxRateError = class extends Error {
|
|
18
|
+
code = "invalid_fx_rate";
|
|
19
|
+
ratePerBtc;
|
|
20
|
+
constructor(ratePerBtc) {
|
|
21
|
+
super(
|
|
22
|
+
`invalid fx rate ${String(ratePerBtc)} \u2014 expected a finite positive number of fiat per BTC`
|
|
23
|
+
);
|
|
24
|
+
this.ratePerBtc = ratePerBtc;
|
|
25
|
+
}
|
|
26
|
+
};
|
|
27
|
+
function fiatToSatsCeil(amount, ratePerBtc) {
|
|
28
|
+
if (!Number.isFinite(ratePerBtc) || ratePerBtc <= 0) {
|
|
29
|
+
throw new InvalidFxRateError(ratePerBtc);
|
|
30
|
+
}
|
|
31
|
+
if (!Number.isFinite(amount) || amount < 0) return 0;
|
|
32
|
+
return Math.ceil(amount * 1e8 / ratePerBtc);
|
|
33
|
+
}
|
|
34
|
+
function satsToFiat(sats, ratePerBtc) {
|
|
35
|
+
if (!Number.isFinite(ratePerBtc) || ratePerBtc <= 0) {
|
|
36
|
+
throw new InvalidFxRateError(ratePerBtc);
|
|
37
|
+
}
|
|
38
|
+
return sats / 1e8 * ratePerBtc;
|
|
39
|
+
}
|
|
40
|
+
var InvalidUsdPriceError = class extends Error {
|
|
41
|
+
constructor(field, value, detail) {
|
|
42
|
+
super(`${field}: ${detail}`);
|
|
43
|
+
this.field = field;
|
|
44
|
+
this.value = value;
|
|
45
|
+
this.name = "InvalidUsdPriceError";
|
|
46
|
+
}
|
|
47
|
+
field;
|
|
48
|
+
value;
|
|
49
|
+
code = "invalid_usd_price";
|
|
50
|
+
};
|
|
51
|
+
function parseUsdPrice(value, field) {
|
|
52
|
+
if (!USD_PRICE_RE.test(value)) {
|
|
53
|
+
throw new InvalidUsdPriceError(
|
|
54
|
+
field,
|
|
55
|
+
value,
|
|
56
|
+
`${JSON.stringify(value)} is not a USD price literal. Use a "$X.XX" string, e.g. "$0.05".`
|
|
57
|
+
);
|
|
58
|
+
}
|
|
59
|
+
const usd = parseFloat(value.slice(1));
|
|
60
|
+
if (!Number.isFinite(usd) || usd < MIN_USD_PRICE) {
|
|
61
|
+
throw new InvalidUsdPriceError(
|
|
62
|
+
field,
|
|
63
|
+
value,
|
|
64
|
+
`${JSON.stringify(value)} is below ${MIN_USD_PRICE_LITERAL}, the smallest price the wire can carry \u2014 every price surface rounds to four decimals, so this would advertise as $0 and then charge anyway. Charge at least ${MIN_USD_PRICE_LITERAL}, or omit the price for a free capability.`
|
|
65
|
+
);
|
|
66
|
+
}
|
|
67
|
+
if (roundUsd(usd) !== usd) {
|
|
68
|
+
throw new InvalidUsdPriceError(
|
|
69
|
+
field,
|
|
70
|
+
value,
|
|
71
|
+
`${JSON.stringify(value)} carries more than four decimal places. Every price surface rounds to four, so this would advertise ${formatUsd(roundUsd(usd))} and charge something else. Use at most four decimals.`
|
|
72
|
+
);
|
|
73
|
+
}
|
|
74
|
+
if (!Number.isSafeInteger(Math.round(usd * 1e6))) {
|
|
75
|
+
throw new InvalidUsdPriceError(
|
|
76
|
+
field,
|
|
77
|
+
value,
|
|
78
|
+
`${JSON.stringify(value)} is too large to denominate. Use an amount under $9,007,199,254.`
|
|
79
|
+
);
|
|
80
|
+
}
|
|
81
|
+
return usd;
|
|
82
|
+
}
|
|
83
|
+
function roundUsd(usd) {
|
|
84
|
+
return Math.round(usd * 1e4) / 1e4;
|
|
85
|
+
}
|
|
86
|
+
function formatUsd(usd) {
|
|
87
|
+
if (usd === 0) return "$0.00";
|
|
88
|
+
if (usd >= 0.01) return `$${usd.toFixed(2)}`;
|
|
89
|
+
return `$${usd.toFixed(4)}`;
|
|
90
|
+
}
|
|
91
|
+
function formatFiat(amount, currency) {
|
|
92
|
+
const code = currency.toUpperCase();
|
|
93
|
+
const symbol = FIAT_SYMBOLS[currency] ?? "";
|
|
94
|
+
const digits = currency === "jpy" ? 0 : amount >= 0.01 || amount === 0 ? 2 : 4;
|
|
95
|
+
const body = `${amount.toFixed(digits)} ${code}`;
|
|
96
|
+
return symbol ? `${symbol}${body}` : body;
|
|
97
|
+
}
|
|
98
|
+
var USD_PRICE_RE = /^\$\d+(\.\d+)?$/;
|
|
99
|
+
var MIN_USD_PRICE = 1e-4;
|
|
100
|
+
var MIN_USD_PRICE_LITERAL = '"$0.0001"';
|
|
101
|
+
var FIAT_SYMBOLS = {
|
|
102
|
+
usd: "$",
|
|
103
|
+
eur: "\u20AC",
|
|
104
|
+
gbp: "\xA3",
|
|
105
|
+
jpy: "\xA5"
|
|
106
|
+
};
|
|
107
|
+
|
|
108
|
+
// node_modules/@dvmkit/sdk/dist/chunk-5URG56JJ.js
|
|
109
|
+
import { existsSync, readFileSync, writeFileSync } from "fs";
|
|
110
|
+
import { dirname } from "path";
|
|
111
|
+
var IMPLICIT_CREDIT_TTL_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
112
|
+
var COINGECKO_SIMPLE_PRICE_URL = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin,usd-coin&vs_currencies=usd";
|
|
113
|
+
var COINGECKO_BTC_USD_URL = "https://api.coingecko.com/api/v3/simple/price?ids=bitcoin&vs_currencies=usd";
|
|
114
|
+
var DEFAULT_TIMEOUT_MS = 5e3;
|
|
115
|
+
var DEFAULT_USER_AGENT = "dvmkit-platform/1.0 (+https://dvmkit.com)";
|
|
116
|
+
async function fetchCoinGeckoSpot(opts) {
|
|
117
|
+
const body = await fetchCoinGeckoBody(opts, COINGECKO_SIMPLE_PRICE_URL);
|
|
118
|
+
const btc = body.bitcoin?.usd;
|
|
119
|
+
const usdc = body["usd-coin"]?.usd;
|
|
120
|
+
if (!isPositiveNumber(btc)) {
|
|
121
|
+
throw new Error("coingecko response missing bitcoin.usd");
|
|
122
|
+
}
|
|
123
|
+
if (!isPositiveNumber(usdc)) {
|
|
124
|
+
throw new Error("coingecko response missing usd-coin.usd");
|
|
125
|
+
}
|
|
126
|
+
return { btc_usd: btc, usdc_usd: usdc, as_of: (/* @__PURE__ */ new Date()).toISOString() };
|
|
127
|
+
}
|
|
128
|
+
async function fetchCoinGeckoBtcUsd(opts) {
|
|
129
|
+
const body = await fetchCoinGeckoBody(opts, COINGECKO_BTC_USD_URL);
|
|
130
|
+
const btc = body.bitcoin?.usd;
|
|
131
|
+
if (!isPositiveNumber(btc)) {
|
|
132
|
+
throw new Error("coingecko response missing bitcoin.usd");
|
|
133
|
+
}
|
|
134
|
+
return { btc_usd: btc, as_of: (/* @__PURE__ */ new Date()).toISOString() };
|
|
135
|
+
}
|
|
136
|
+
async function fetchCoinGeckoBody(opts, defaultUrl) {
|
|
137
|
+
const url = opts?.url ?? defaultUrl;
|
|
138
|
+
const fetchImpl = opts?.fetchImpl ?? fetch;
|
|
139
|
+
const timeoutMs = opts?.timeoutMs ?? DEFAULT_TIMEOUT_MS;
|
|
140
|
+
const userAgent = opts?.userAgent ?? DEFAULT_USER_AGENT;
|
|
141
|
+
const res = await fetchImpl(url, {
|
|
142
|
+
headers: { "User-Agent": userAgent, Accept: "application/json" },
|
|
143
|
+
signal: AbortSignal.timeout(timeoutMs)
|
|
144
|
+
});
|
|
145
|
+
if (!res.ok) {
|
|
146
|
+
throw new Error(`coingecko returned HTTP ${res.status.toString()}`);
|
|
147
|
+
}
|
|
148
|
+
return await res.json();
|
|
149
|
+
}
|
|
150
|
+
function isPositiveNumber(v) {
|
|
151
|
+
return typeof v === "number" && Number.isFinite(v) && v > 0;
|
|
152
|
+
}
|
|
153
|
+
function msatsToUsd(msats, rate) {
|
|
154
|
+
return msats / 1e11 * rate;
|
|
155
|
+
}
|
|
156
|
+
function usdToMsats(usd, rate) {
|
|
157
|
+
return Math.round(usd / rate * 1e11);
|
|
158
|
+
}
|
|
159
|
+
function msatsToUsdCents(msats, btcPriceUsd) {
|
|
160
|
+
const usd = msatsToUsd(msats, btcPriceUsd);
|
|
161
|
+
return Math.round(usd * 100);
|
|
162
|
+
}
|
|
163
|
+
function msatsToUsdc(msats, btcUsdRate) {
|
|
164
|
+
const usd = msatsToUsd(msats, btcUsdRate);
|
|
165
|
+
return Math.ceil(usd * 1e6);
|
|
166
|
+
}
|
|
167
|
+
function usdcToMsats(usdc, btcUsdRate) {
|
|
168
|
+
return usdToMsats(usdc, btcUsdRate);
|
|
169
|
+
}
|
|
170
|
+
function formatSats(msats) {
|
|
171
|
+
if (msats === 0) return "0 sats";
|
|
172
|
+
if (msats < 1e3) return `${msats.toLocaleString("en-US")} msats`;
|
|
173
|
+
if (msats % 1e3 === 0) return `${(msats / 1e3).toLocaleString("en-US")} sats`;
|
|
174
|
+
return `${Math.round(msats / 1e3 * 100) / 100} sats`;
|
|
175
|
+
}
|
|
176
|
+
function formatAmount(msats, rate, opts) {
|
|
177
|
+
const usd = msatsToUsd(msats, rate);
|
|
178
|
+
const base = `~$${formatUsd2(usd)} (${formatSats(msats)})`;
|
|
179
|
+
return opts?.anchor === false ? base : `${base} \u2014 ${costAnchor(usd)}`;
|
|
180
|
+
}
|
|
181
|
+
function formatPaymentAmount(msats, fiat, opts) {
|
|
182
|
+
if (!fiat || fiat.amount <= 0) return formatAmountBestEffort(msats, opts);
|
|
183
|
+
const base = `${formatFiat(fiat.amount, fiat.currency)} (${formatSats(msats)})`;
|
|
184
|
+
return opts?.anchor === false ? base : `${base} \u2014 ${costAnchor(fiat.amount, fiat.currency)}`;
|
|
185
|
+
}
|
|
186
|
+
var COST_ANCHOR_BANDS = {
|
|
187
|
+
usd: [0.01, 0.1, 1, 10],
|
|
188
|
+
eur: [0.01, 0.1, 1, 10],
|
|
189
|
+
gbp: [0.01, 0.1, 1, 10],
|
|
190
|
+
jpy: [1, 10, 100, 1500]
|
|
191
|
+
};
|
|
192
|
+
function costAnchor(amount, currency = "usd") {
|
|
193
|
+
const bands = COST_ANCHOR_BANDS[currency] ?? COST_ANCHOR_BANDS.usd;
|
|
194
|
+
if (amount < bands[0]) return "fraction of a cent";
|
|
195
|
+
if (amount <= bands[1]) return "comparable to an API call";
|
|
196
|
+
if (amount <= bands[2]) return "comparable to a ChatGPT query";
|
|
197
|
+
if (amount <= bands[3]) return "comparable to a coffee";
|
|
198
|
+
return "significant purchase \u2014 review carefully";
|
|
199
|
+
}
|
|
200
|
+
function monetaryJson(msats, rate) {
|
|
201
|
+
const usd = msatsToUsd(msats, rate);
|
|
202
|
+
return {
|
|
203
|
+
msats,
|
|
204
|
+
usd: Math.round(usd * 100) / 100,
|
|
205
|
+
anchor: costAnchor(usd),
|
|
206
|
+
display: formatAmount(msats, rate)
|
|
207
|
+
};
|
|
208
|
+
}
|
|
209
|
+
function formatUsd2(usd) {
|
|
210
|
+
if (usd === 0) return "0.00";
|
|
211
|
+
if (usd >= 0.01) return usd.toFixed(2);
|
|
212
|
+
const digits = Math.max(2, -Math.floor(Math.log10(usd)) + 1);
|
|
213
|
+
return usd.toFixed(digits);
|
|
214
|
+
}
|
|
215
|
+
function formatUsdcMicrounits(microunits) {
|
|
216
|
+
const value = typeof microunits === "bigint" ? microunits : BigInt(microunits);
|
|
217
|
+
const whole = value / 1000000n;
|
|
218
|
+
const fraction = (value % 1000000n).toString().padStart(6, "0").replace(/0+$/, "");
|
|
219
|
+
return `${whole}.${fraction.padEnd(2, "0") || "00"}`;
|
|
220
|
+
}
|
|
221
|
+
function parseBudget(budget, rate) {
|
|
222
|
+
const value = assertValidBudget(budget);
|
|
223
|
+
return budget.startsWith("$") ? usdToMsats(value, rate) : value;
|
|
224
|
+
}
|
|
225
|
+
function assertValidBudget(budget) {
|
|
226
|
+
const value = budget.startsWith("$") ? parseFloat(budget.slice(1)) : parseInt(budget, 10);
|
|
227
|
+
if (isNaN(value)) throw new Error(`Invalid budget: ${budget}`);
|
|
228
|
+
return value;
|
|
229
|
+
}
|
|
230
|
+
async function fetchBtcUsdRate() {
|
|
231
|
+
if (memRate && Date.now() - memTime < CACHE_TTL) {
|
|
232
|
+
return memRate;
|
|
233
|
+
}
|
|
234
|
+
const disk = readDiskCache();
|
|
235
|
+
if (disk) {
|
|
236
|
+
memRate = disk;
|
|
237
|
+
memTime = Date.now();
|
|
238
|
+
return disk;
|
|
239
|
+
}
|
|
240
|
+
const rate = await fetchFromApi();
|
|
241
|
+
memRate = rate;
|
|
242
|
+
memTime = Date.now();
|
|
243
|
+
writeDiskCache(rate);
|
|
244
|
+
return rate;
|
|
245
|
+
}
|
|
246
|
+
function getLastKnownBtcRate() {
|
|
247
|
+
if (memRate && memTime > 0) return { rate: memRate, cachedAt: memTime };
|
|
248
|
+
return readDiskCacheStale();
|
|
249
|
+
}
|
|
250
|
+
function formatStaleness(cachedAt, now = Date.now()) {
|
|
251
|
+
const ageMs = Math.max(0, now - cachedAt);
|
|
252
|
+
const seconds = Math.floor(ageMs / 1e3);
|
|
253
|
+
if (seconds < 60) return `${seconds}s ago`;
|
|
254
|
+
const minutes = Math.floor(seconds / 60);
|
|
255
|
+
if (minutes < 60) return `${minutes} min ago`;
|
|
256
|
+
const hours = Math.floor(minutes / 60);
|
|
257
|
+
if (hours < 24) return `${hours} hour${hours === 1 ? "" : "s"} ago`;
|
|
258
|
+
const days = Math.floor(hours / 24);
|
|
259
|
+
return `${days} day${days === 1 ? "" : "s"} ago`;
|
|
260
|
+
}
|
|
261
|
+
function formatAmountBestEffort(msats, opts) {
|
|
262
|
+
const m = msats ?? 0;
|
|
263
|
+
const cached = getLastKnownBtcRate();
|
|
264
|
+
if (cached === null) return `${Math.round(m / 1e3)} sats`;
|
|
265
|
+
const base = formatAmount(m, cached.rate, opts);
|
|
266
|
+
if (Date.now() - cached.cachedAt < CACHE_TTL) return base;
|
|
267
|
+
return `${base} (rate cached ${formatStaleness(cached.cachedAt)})`;
|
|
268
|
+
}
|
|
269
|
+
var CACHE_TTL = 5 * 60 * 1e3;
|
|
270
|
+
var CACHE_FILE = BTC_RATE_FILE;
|
|
271
|
+
var memRate = null;
|
|
272
|
+
var memTime = 0;
|
|
273
|
+
function readDiskCache() {
|
|
274
|
+
try {
|
|
275
|
+
if (!existsSync(CACHE_FILE)) return null;
|
|
276
|
+
const data = JSON.parse(readFileSync(CACHE_FILE, "utf-8"));
|
|
277
|
+
if (Date.now() - data.timestamp >= CACHE_TTL) return null;
|
|
278
|
+
if (!isPlausibleBtcUsdRate(data.rate)) return null;
|
|
279
|
+
return data.rate;
|
|
280
|
+
} catch {
|
|
281
|
+
return null;
|
|
282
|
+
}
|
|
283
|
+
}
|
|
284
|
+
function readDiskCacheStale() {
|
|
285
|
+
try {
|
|
286
|
+
if (!existsSync(CACHE_FILE)) return null;
|
|
287
|
+
const data = JSON.parse(readFileSync(CACHE_FILE, "utf-8"));
|
|
288
|
+
if (!isPlausibleBtcUsdRate(data.rate)) return null;
|
|
289
|
+
return { rate: data.rate, cachedAt: data.timestamp };
|
|
290
|
+
} catch {
|
|
291
|
+
return null;
|
|
292
|
+
}
|
|
293
|
+
}
|
|
294
|
+
function writeDiskCache(rate) {
|
|
295
|
+
try {
|
|
296
|
+
ensureConfigSubdir(dirname(CACHE_FILE));
|
|
297
|
+
writeFileSync(CACHE_FILE, JSON.stringify({ rate, timestamp: Date.now() }));
|
|
298
|
+
} catch {
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
var MAX_PLAUSIBLE_BTC_USD = 1e7;
|
|
302
|
+
function isPlausibleBtcUsdRate(rate) {
|
|
303
|
+
return typeof rate === "number" && Number.isFinite(rate) && rate > 0 && rate < MAX_PLAUSIBLE_BTC_USD;
|
|
304
|
+
}
|
|
305
|
+
async function fetchFromApi() {
|
|
306
|
+
const spot = await fetchCoinGeckoBtcUsd();
|
|
307
|
+
const rate = spot.btc_usd;
|
|
308
|
+
if (!isPlausibleBtcUsdRate(rate)) {
|
|
309
|
+
throw new Error(`implausible BTC/USD rate from upstream: ${String(rate)}`);
|
|
310
|
+
}
|
|
311
|
+
return rate;
|
|
312
|
+
}
|
|
313
|
+
|
|
314
|
+
// node_modules/@dvmkit/sdk/dist/chunk-KSQEFVFJ.js
|
|
315
|
+
var import_proper_lockfile = __toESM(require_proper_lockfile(), 1);
|
|
15
316
|
import { writeSync } from "fs";
|
|
317
|
+
import { existsSync as existsSync2, readFileSync as readFileSync2, renameSync, unlinkSync, writeFileSync as writeFileSync2 } from "fs";
|
|
318
|
+
import { dirname as dirname2 } from "path";
|
|
16
319
|
var _traceId = null;
|
|
17
320
|
function setOutputTraceId(id) {
|
|
18
321
|
_traceId = id;
|
|
@@ -46,16 +349,14 @@ function writeAllSync(fd, text) {
|
|
|
46
349
|
}
|
|
47
350
|
}
|
|
48
351
|
}
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
// src/lib/transport/types.ts
|
|
352
|
+
var RAIL_FLAGS = ["cashu", "tempo", "x402"];
|
|
353
|
+
var ADVERTISED_AMOUNT_TOLERANCE_BPS = 1000n;
|
|
354
|
+
function maxAdvertisedMicro(localMicro) {
|
|
355
|
+
return localMicro * (10000n + ADVERTISED_AMOUNT_TOLERANCE_BPS) / 10000n;
|
|
356
|
+
}
|
|
357
|
+
function withinAdvertisedTolerance(advertisedMicro, localMicro) {
|
|
358
|
+
return advertisedMicro * 10000n <= localMicro * (10000n + ADVERTISED_AMOUNT_TOLERANCE_BPS);
|
|
359
|
+
}
|
|
59
360
|
var SUPPORTED_PROTOCOL_VERSION = 1;
|
|
60
361
|
function parseCreditTerms(raw2) {
|
|
61
362
|
if (!raw2 || typeof raw2 !== "object" || Array.isArray(raw2)) return void 0;
|
|
@@ -133,8 +434,6 @@ function parseCreditX402Terms(raw2) {
|
|
|
133
434
|
}))
|
|
134
435
|
};
|
|
135
436
|
}
|
|
136
|
-
|
|
137
|
-
// src/lib/credit-store.ts
|
|
138
437
|
function withCreditLock(fn, opts = {}) {
|
|
139
438
|
if (lockDepth > 0) {
|
|
140
439
|
lockDepth += 1;
|
|
@@ -883,12 +1182,12 @@ var MAX_SETTLED_DRAIN_EVIDENCE = 8;
|
|
|
883
1182
|
var MAX_LOST_IMPLICIT_CREDITS = 8;
|
|
884
1183
|
var MAX_DATE_MS = 864e13;
|
|
885
1184
|
function loadStore() {
|
|
886
|
-
if (!
|
|
1185
|
+
if (!existsSync2(CREDITS_FILE)) {
|
|
887
1186
|
return { credits: [], implicitCredits: [], pendingFundings: [], fundingMenuSnapshots: [] };
|
|
888
1187
|
}
|
|
889
1188
|
let raw2;
|
|
890
1189
|
try {
|
|
891
|
-
raw2 =
|
|
1190
|
+
raw2 = readFileSync2(CREDITS_FILE, "utf-8");
|
|
892
1191
|
} catch (cause) {
|
|
893
1192
|
throw unreadableStore(cause);
|
|
894
1193
|
}
|
|
@@ -1309,7 +1608,7 @@ function capDrainEvidence(evidence, endpoint) {
|
|
|
1309
1608
|
function saveStore(store) {
|
|
1310
1609
|
ensureConfigDir();
|
|
1311
1610
|
const tmp = `${CREDITS_FILE}.${String(process.pid)}.tmp`;
|
|
1312
|
-
|
|
1611
|
+
writeFileSync2(tmp, JSON.stringify(store, null, 2) + "\n", { mode: 384 });
|
|
1313
1612
|
try {
|
|
1314
1613
|
renameSync(tmp, CREDITS_FILE);
|
|
1315
1614
|
} catch (err) {
|
|
@@ -1396,7 +1695,7 @@ function acquireCreditLock(opts) {
|
|
|
1396
1695
|
lastError = err;
|
|
1397
1696
|
if (err.code !== "ELOCKED") {
|
|
1398
1697
|
const detail = err instanceof Error ? err.message : String(err);
|
|
1399
|
-
const configDir =
|
|
1698
|
+
const configDir = dirname2(lockPath);
|
|
1400
1699
|
throw new DvmError(
|
|
1401
1700
|
"credit_store_lock_failed",
|
|
1402
1701
|
`Could not create the prepaid-credit lock at ${lockPath}: ${detail}`,
|
|
@@ -1439,12 +1738,43 @@ function sleepSync(ms) {
|
|
|
1439
1738
|
}
|
|
1440
1739
|
|
|
1441
1740
|
export {
|
|
1741
|
+
fiatToSatsCeil,
|
|
1742
|
+
satsToFiat,
|
|
1743
|
+
InvalidUsdPriceError,
|
|
1744
|
+
parseUsdPrice,
|
|
1745
|
+
roundUsd,
|
|
1746
|
+
formatFiat,
|
|
1747
|
+
IMPLICIT_CREDIT_TTL_MS,
|
|
1748
|
+
COINGECKO_SIMPLE_PRICE_URL,
|
|
1749
|
+
fetchCoinGeckoSpot,
|
|
1750
|
+
fetchCoinGeckoBtcUsd,
|
|
1751
|
+
msatsToUsd,
|
|
1752
|
+
usdToMsats,
|
|
1753
|
+
msatsToUsdCents,
|
|
1754
|
+
msatsToUsdc,
|
|
1755
|
+
usdcToMsats,
|
|
1756
|
+
formatSats,
|
|
1757
|
+
formatAmount,
|
|
1758
|
+
formatPaymentAmount,
|
|
1759
|
+
costAnchor,
|
|
1760
|
+
monetaryJson,
|
|
1761
|
+
formatUsd2 as formatUsd,
|
|
1762
|
+
formatUsdcMicrounits,
|
|
1763
|
+
parseBudget,
|
|
1764
|
+
assertValidBudget,
|
|
1765
|
+
fetchBtcUsdRate,
|
|
1766
|
+
getLastKnownBtcRate,
|
|
1767
|
+
formatStaleness,
|
|
1768
|
+
formatAmountBestEffort,
|
|
1442
1769
|
setOutputTraceId,
|
|
1443
1770
|
getOutputTraceId,
|
|
1444
1771
|
json,
|
|
1445
1772
|
info,
|
|
1446
1773
|
raw,
|
|
1447
1774
|
progress,
|
|
1775
|
+
RAIL_FLAGS,
|
|
1776
|
+
maxAdvertisedMicro,
|
|
1777
|
+
withinAdvertisedTolerance,
|
|
1448
1778
|
SUPPORTED_PROTOCOL_VERSION,
|
|
1449
1779
|
parseCreditTerms,
|
|
1450
1780
|
parseCreditMenu,
|