@dvmkit/sdk 0.0.0 → 0.1.0-rc.2

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.
Files changed (42) hide show
  1. package/NOTICE +2 -0
  2. package/README.md +38 -2
  3. package/dist/chunk-27V2ILSR.js +291 -0
  4. package/dist/chunk-365P52XQ.js +4121 -0
  5. package/dist/chunk-5GFED3GJ.js +955 -0
  6. package/dist/chunk-6JZIX5WW.js +1155 -0
  7. package/dist/chunk-7IH5SG2A.js +1038 -0
  8. package/dist/chunk-AT6V3SY7.js +102 -0
  9. package/dist/chunk-DCNT4PJS.js +733 -0
  10. package/dist/chunk-DMNLFNTW.js +135 -0
  11. package/dist/chunk-FROTD5XQ.js +70 -0
  12. package/dist/chunk-H25M54MI.js +149 -0
  13. package/dist/chunk-KQAJVVZT.js +712 -0
  14. package/dist/chunk-KXWROQGK.js +74 -0
  15. package/dist/chunk-L4OYF4DQ.js +67 -0
  16. package/dist/chunk-OJ5WFIB2.js +1266 -0
  17. package/dist/chunk-S3XAHZQY.js +63 -0
  18. package/dist/chunk-YG7G4DPZ.js +25 -0
  19. package/dist/credit-ledger-RO4FGSHG.js +28 -0
  20. package/dist/index.d.ts +144 -0
  21. package/dist/index.js +303 -0
  22. package/dist/job-store-6gR4pZRP.d.ts +5350 -0
  23. package/dist/memory-credit-ledger-I2G64DDK.js +9 -0
  24. package/dist/mpp-secret-state-WNAQQ6K4.js +127 -0
  25. package/dist/mpp-setup-MOBWGTWJ.js +30 -0
  26. package/dist/payout-reporter-4TNWRS5F.js +753 -0
  27. package/dist/postgres-consumed-credential-store-VHBT4KEA.js +72 -0
  28. package/dist/postgres-job-store-J5F4GUWU.js +7 -0
  29. package/dist/postgres-kv-store-JFBDP5IP.js +7 -0
  30. package/dist/postgres-replay-store-UJXRT6VO.js +7 -0
  31. package/dist/pricing-4CEB34RM.js +48 -0
  32. package/dist/processed-payment-store-HAA4SFNK.js +11 -0
  33. package/dist/revenue-reporter-GB4WKLDC.js +510 -0
  34. package/dist/server/index.d.ts +4168 -0
  35. package/dist/server/index.js +22716 -0
  36. package/dist/ssrf-DZi-xJyn.d.ts +325 -0
  37. package/dist/tempo-charge-store-6GJEMNUU.js +130 -0
  38. package/dist/tempo-session-store-FTEEGZXA.js +467 -0
  39. package/dist/testing/index.d.ts +135 -0
  40. package/dist/testing/index.js +151 -0
  41. package/dist/x402-35VLYFKZ.js +1272 -0
  42. package/package.json +89 -6
@@ -0,0 +1,102 @@
1
+ // src/sdk/pricing/usd.ts
2
+ var InvalidFxRateError = class extends Error {
3
+ code = "invalid_fx_rate";
4
+ ratePerBtc;
5
+ constructor(ratePerBtc) {
6
+ super(
7
+ `invalid fx rate ${String(ratePerBtc)} \u2014 expected a finite positive number of fiat per BTC`
8
+ );
9
+ this.ratePerBtc = ratePerBtc;
10
+ }
11
+ };
12
+ function fiatToSatsCeil(amount, ratePerBtc) {
13
+ if (!Number.isFinite(ratePerBtc) || ratePerBtc <= 0) {
14
+ throw new InvalidFxRateError(ratePerBtc);
15
+ }
16
+ if (!Number.isFinite(amount) || amount < 0) return 0;
17
+ return Math.ceil(amount * 1e8 / ratePerBtc);
18
+ }
19
+ function satsToFiat(sats, ratePerBtc) {
20
+ if (!Number.isFinite(ratePerBtc) || ratePerBtc <= 0) {
21
+ throw new InvalidFxRateError(ratePerBtc);
22
+ }
23
+ return sats / 1e8 * ratePerBtc;
24
+ }
25
+ var InvalidUsdPriceError = class extends Error {
26
+ constructor(field, value, detail) {
27
+ super(`${field}: ${detail}`);
28
+ this.field = field;
29
+ this.value = value;
30
+ this.name = "InvalidUsdPriceError";
31
+ }
32
+ field;
33
+ value;
34
+ code = "invalid_usd_price";
35
+ };
36
+ function parseUsdPrice(value, field) {
37
+ if (!USD_PRICE_RE.test(value)) {
38
+ throw new InvalidUsdPriceError(
39
+ field,
40
+ value,
41
+ `${JSON.stringify(value)} is not a USD price literal. Use a "$X.XX" string, e.g. "$0.05".`
42
+ );
43
+ }
44
+ const usd = parseFloat(value.slice(1));
45
+ if (!Number.isFinite(usd) || usd < MIN_USD_PRICE) {
46
+ throw new InvalidUsdPriceError(
47
+ field,
48
+ value,
49
+ `${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.`
50
+ );
51
+ }
52
+ if (roundUsd(usd) !== usd) {
53
+ throw new InvalidUsdPriceError(
54
+ field,
55
+ value,
56
+ `${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.`
57
+ );
58
+ }
59
+ if (!Number.isSafeInteger(Math.round(usd * 1e6))) {
60
+ throw new InvalidUsdPriceError(
61
+ field,
62
+ value,
63
+ `${JSON.stringify(value)} is too large to denominate. Use an amount under $9,007,199,254.`
64
+ );
65
+ }
66
+ return usd;
67
+ }
68
+ function roundUsd(usd) {
69
+ return Math.round(usd * 1e4) / 1e4;
70
+ }
71
+ function formatUsd(usd) {
72
+ if (usd === 0) return "$0.00";
73
+ if (usd >= 0.01) return `$${usd.toFixed(2)}`;
74
+ return `$${usd.toFixed(4)}`;
75
+ }
76
+ function formatFiat(amount, currency) {
77
+ const code = currency.toUpperCase();
78
+ const symbol = FIAT_SYMBOLS[currency] ?? "";
79
+ const digits = currency === "jpy" ? 0 : amount >= 0.01 || amount === 0 ? 2 : 4;
80
+ const body = `${amount.toFixed(digits)} ${code}`;
81
+ return symbol ? `${symbol}${body}` : body;
82
+ }
83
+ var USD_PRICE_RE = /^\$\d+(\.\d+)?$/;
84
+ var MIN_USD_PRICE = 1e-4;
85
+ var MIN_USD_PRICE_LITERAL = '"$0.0001"';
86
+ var FIAT_SYMBOLS = {
87
+ usd: "$",
88
+ eur: "\u20AC",
89
+ gbp: "\xA3",
90
+ jpy: "\xA5"
91
+ };
92
+
93
+ export {
94
+ InvalidFxRateError,
95
+ fiatToSatsCeil,
96
+ satsToFiat,
97
+ InvalidUsdPriceError,
98
+ parseUsdPrice,
99
+ roundUsd,
100
+ formatUsd,
101
+ formatFiat
102
+ };