flop-guild 0.2.11 → 0.2.12

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/dist/minerpage.js CHANGED
@@ -24,18 +24,6 @@ function view() {
24
24
  return undefined;
25
25
  return { shown: lastComplete ?? latest, latest, partial: !latest.complete };
26
26
  }
27
- /**
28
- * For the /miner page: if the explorer read times out or fails, retry twice, 3 s apart.
29
- * Returns undefined when there is still no data; the page then shows "unavailable", never 0 miners.
30
- */
31
- export async function demandWithRetry(sleep = (ms) => new Promise((r) => setTimeout(r, ms)), load = demand) {
32
- let v = await load(undefined, 20_000).catch(() => undefined);
33
- for (let i = 0; i < 2 && !v; i++) {
34
- await sleep(3_000);
35
- v = await load(undefined, 8_000).catch(() => undefined);
36
- }
37
- return v;
38
- }
39
27
  /** Cached demand: refresh after 10 minutes (2 after a partial read); a partial read never replaces a complete one. */
40
28
  export async function demand(network = process.env.MINER_NETWORK || activeNetwork(), waitMs = 25_000) {
41
29
  const age = Date.now() - lastAttemptAt;
@@ -91,8 +79,8 @@ function chart(byHour) {
91
79
  }
92
80
  export const NA = "n/a (explorer unavailable)";
93
81
  /** Without a miner count, every row that depends on it is n/a; only the rent is known. */
94
- function calcRows(o, known = true) {
95
- const k = (v) => (known ? v : NA);
82
+ function calcRows(o, known = true, missing = NA) {
83
+ const k = (v) => (known ? v : missing);
96
84
  const rows = [
97
85
  ["out-rent", "Total rent", usd(o.totalRent)],
98
86
  ["out-share", "Your share of the miner pool (estimate)", k(pct(o.share))],
@@ -100,7 +88,7 @@ function calcRows(o, known = true) {
100
88
  ["out-liquid", "Liquid FLOP at launch (estimate)", k(flop(o.liquidFlop))],
101
89
  ["out-be-liquid", "Break-even FLOP price, liquid FLOP only", k(price(o.breakEvenLiquid))],
102
90
  ["out-be-all", "Break-even FLOP price, if all airdrop FLOP unlocked", k(price(o.breakEvenAll))],
103
- ["out-net", "Liquid value minus rent, at your FLOP price", known ? (o.net === null ? "enter a FLOP price" : usd(o.net)) : NA],
91
+ ["out-net", "Liquid value minus rent, at your FLOP price", known ? (o.net === null ? "enter a FLOP price" : usd(o.net)) : missing],
104
92
  ];
105
93
  return rows.map(([id, label, v]) => `<tr><th scope="row">${label}</th><td id="${id}">${v}</td></tr>`).join("");
106
94
  }
@@ -109,31 +97,49 @@ export const MINER_STYLE = `
109
97
  .prose table{margin:.6rem 0}.prose td{text-align:right;font-variant-numeric:tabular-nums}.prose th{font-weight:400;color:var(--muted)}
110
98
  #miner-count{font-family:var(--display)}
111
99
  .note{font-size:.88rem}`;
112
- export function minerPage(v, err) {
100
+ export const LOADING = "loading";
101
+ const utc = (t) => new Date(t).toISOString().slice(0, 16).replace("T", " ");
102
+ const RETRY = '<p id="retry-wrap"><a class="btn" id="retry" href="/miner" role="button">Retry</a></p>';
103
+ /** The demand block: chart and numbers when there is data, otherwise the loading or unavailable message. */
104
+ export function demandSection(v, state, err) {
113
105
  const d = v?.shown;
114
- const known = !!d; // no explorer data: show "unavailable", never compute with 0 miners
115
- const minerCount = d?.minerCount ?? 0;
116
- const utc = (t) => new Date(t).toISOString().slice(0, 16).replace("T", " ");
106
+ if (!d) {
107
+ return state === "loading"
108
+ ? `<p class="note">Loading demand data from the explorer. The calculator works meanwhile.</p>
109
+ <noscript><p>This part loads with JavaScript. Without it, reload the page in a minute.</p></noscript>`
110
+ : `<p>The explorer is unavailable${err ? ` (${esc(err)})` : " (no answer after 3 tries)"}. Demand and the miner count cannot be shown right now.</p>\n${RETRY}`;
111
+ }
117
112
  const partialHtml = v?.partial
118
113
  ? `<p><strong>${v.shown.complete ? PARTIAL_NOTE : "Partial data, no complete read yet"}</strong> (latest read ${utc(v.latest.fetchedAt)} UTC stopped early: ${esc(v.latest.note ?? "incomplete")}; ${v.latest.pagesRead} pages, ${v.latest.sessions.toLocaleString("en-US")} sessions).</p>`
119
114
  : "";
120
- const readInfo = d
121
- ? ` <span class="note">(${d.sessions.toLocaleString("en-US")} sessions read since ${utc(d.windowStart)} UTC, ${d.pagesRead} explorer pages, ${d.complete ? "complete read" : "partial read"})</span>`
122
- : "";
123
- const out = minerCalc({ ...CALC_DEFAULTS, minerCount });
124
- const demandHtml = d
125
- ? `<p>${d.sessions.toLocaleString("en-US")} sessions opened on ${esc(d.network)} between ${new Date(d.windowStart).toISOString().slice(0, 16).replace("T", " ")} and ${new Date(d.windowEnd).toISOString().slice(0, 16).replace("T", " ")} UTC, about <strong>${d.perHour.toFixed(1)} sessions per hour</strong>.</p>
115
+ return `<p>${d.sessions.toLocaleString("en-US")} sessions opened on ${esc(d.network)} between ${utc(d.windowStart)} and ${utc(d.windowEnd)} UTC, about <strong>${d.perHour.toFixed(1)} sessions per hour</strong>.</p>
126
116
  ${partialHtml}
127
117
  ${chart(d.byHour)}
128
118
  <p>Busiest UTC hours: ${d.busiest.map((b) => `<strong>${hh(b.hour)}</strong> (${b.sessions.toLocaleString("en-US")})`).join(", ")}.</p>
129
- <p class="note">Source: ${esc(d.explorer)}/api/compute-channels, bucketed by the hour each session opened. Updated ${new Date(d.fetchedAt).toISOString().slice(11, 16)} UTC, refreshed every 10 minutes.</p>`
130
- : `<p>The explorer is unavailable${err ? ` (${esc(err)})` : " (no answer after 3 tries)"}. Demand and the miner count cannot be shown right now.</p>
131
- <p><a class="btn" href="/miner" role="button">Retry</a></p>`;
119
+ <p class="note">Source: ${esc(d.explorer)}/api/compute-channels, bucketed by the hour each session opened. Updated ${new Date(d.fetchedAt).toISOString().slice(11, 16)} UTC, refreshed every 10 minutes.</p>`;
120
+ }
121
+ export function readInfoHtml(v) {
122
+ const d = v?.shown;
123
+ return d ? ` <span class="note">(${d.sessions.toLocaleString("en-US")} sessions read since ${utc(d.windowStart)} UTC, ${d.pagesRead} explorer pages, ${d.complete ? "complete read" : "partial read"})</span>` : "";
124
+ }
125
+ /**
126
+ * /miner. With cached data it renders complete. Without, it renders at once in the "loading" state and
127
+ * /miner.js fetches /api/miner-stats in the background (3 tries, 3 s apart), then fills in or shows
128
+ * "unavailable" with a Retry button. A missing miner count is never treated as 0.
129
+ */
130
+ export function minerPage(v, err, state = v?.shown ? "ready" : "unavailable") {
131
+ const d = v?.shown;
132
+ const known = !!d;
133
+ const minerCount = d?.minerCount ?? 0;
134
+ const out = minerCalc({ ...CALC_DEFAULTS, minerCount });
135
+ const countText = known ? minerCount.toLocaleString("en-US") : state === "loading" ? LOADING : "unavailable";
132
136
  return `<h1>Miner economics</h1>
133
137
  <p>What miners are paid for: how much inference demand there is, by hour, and what renting a GPU costs against an estimated airdrop share.</p>
134
138
  <h2>Demand by UTC hour, last 7 days</h2>
135
139
  <p><strong>${esc(testnetLine())}</strong></p>
136
- ${demandHtml}
140
+ <div id="demand" data-state="${known ? "ready" : state}" aria-live="polite">
141
+ ${demandSection(v, state, err)}
142
+ </div>
137
143
  <h2>Rent vs airdrop calculator</h2>
138
144
  <form id="calc" class="grid" data-miners="${known ? minerCount : ""}">
139
145
  <label>Rent, $ per hour<input id="in-rent" type="number" step="0.01" min="0" value="${CALC_DEFAULTS.rentPerHour}"></label>
@@ -145,13 +151,13 @@ ${demandHtml}
145
151
  <label>Miner pool, FLOP<input id="in-pool" type="number" step="1" min="0" value="${CALC_DEFAULTS.poolFlop}"></label>
146
152
  <label>Liquid share, %<input id="in-liquid" type="number" step="1" min="0" max="100" value="${CALC_DEFAULTS.liquidShare * 100}"></label>
147
153
  </form>
148
- <p>Miners active in the last 7 days (live from the explorer): <strong id="miner-count">${known ? minerCount.toLocaleString("en-US") : "unavailable"}</strong>${readInfo}</p>
149
- ${known ? "" : '<p><a class="btn" href="/miner" role="button">Retry</a></p>'}
150
- <table><tbody>${calcRows(out, known)}</tbody></table>
154
+ <p>Miners active in the last 7 days (live from the explorer): <strong id="miner-count">${countText}</strong><span id="read-info">${readInfoHtml(v)}</span></p>
155
+ ${!known && state === "unavailable" ? RETRY : ""}
156
+ <table><tbody>${calcRows(out, known, state === "loading" ? "waiting for the miner count" : NA)}</tbody></table>
151
157
  <p class="note">Share estimate = your weight / (miner count × average weight) × (hours per day / 24). The share assumes the average miner runs 24 hours a day.</p>
152
158
  <p class="note">The real share is your verified compute divided by all verified compute, which nobody can know in advance. No FLOP token or price exists yet.</p>`;
153
159
  }
154
- /** Browser script: the same minerCalc as the server, plus input wiring. */
160
+ /** Browser script: the same minerCalc as the server, input wiring, and the background data fetch. */
155
161
  export function minerScript() {
156
162
  return `"use strict";
157
163
  ${minerCalc.toString()}
@@ -159,23 +165,24 @@ ${minerCalc.toString()}
159
165
  var f = document.getElementById("calc");
160
166
  if (!f) return;
161
167
  var raw = f.getAttribute("data-miners");
162
- var known = raw !== "" && raw !== null; // empty: explorer unavailable, never treat as 0 miners
168
+ var known = raw !== "" && raw !== null; // empty: no explorer data yet, never treat as 0 miners
163
169
  var miners = known ? Number(raw) || 0 : 0;
164
170
  var NA = "n/a (explorer unavailable)";
165
- function v(id) { var x = document.getElementById(id).value; return x === "" ? undefined : Number(x); }
171
+ var WAITING = "waiting for the miner count";
172
+ var missing = WAITING;
173
+ var DEPENDENT = ["out-share", "out-airdrop", "out-liquid", "out-be-liquid", "out-be-all", "out-net"];
174
+ function $(id) { return document.getElementById(id); }
175
+ function v(id) { var x = $(id).value; return x === "" ? undefined : Number(x); }
166
176
  function usd(n) { return "$" + n.toLocaleString("en-US", { minimumFractionDigits: 2, maximumFractionDigits: 2 }); }
167
177
  function fl(n) { return Math.round(n).toLocaleString("en-US") + " FLOP"; }
168
178
  function pr(n) { return n === null ? "n/a" : "$" + (n < 0.01 ? n.toPrecision(3) : n.toFixed(4)); }
169
- function set(id, t) { document.getElementById(id).textContent = t; }
179
+ function set(id, t) { $(id).textContent = t; }
170
180
  function run() {
171
181
  var o = minerCalc({ rentPerHour: v("in-rent") || 0, hoursPerDay: v("in-hours") || 0, days: v("in-days") || 0,
172
182
  gpuWeight: v("in-weight") || 0, avgWeight: v("in-avg") || 0, minerCount: miners,
173
183
  poolFlop: v("in-pool") || 0, liquidShare: (v("in-liquid") || 0) / 100, flopPrice: v("in-price") });
174
184
  set("out-rent", usd(o.totalRent));
175
- if (!known) {
176
- ["out-share", "out-airdrop", "out-liquid", "out-be-liquid", "out-be-all", "out-net"].forEach(function (id) { set(id, NA); });
177
- return;
178
- }
185
+ if (!known) { DEPENDENT.forEach(function (id) { set(id, missing); }); return; }
179
186
  set("out-share", (o.share * 100).toPrecision(3) + "%");
180
187
  set("out-airdrop", fl(o.airdropFlop));
181
188
  set("out-liquid", fl(o.liquidFlop));
@@ -186,6 +193,64 @@ ${minerCalc.toString()}
186
193
  f.addEventListener("submit", function (e) { e.preventDefault(); });
187
194
  f.addEventListener("input", run);
188
195
  run();
196
+
197
+ // Background load: /api/miner-stats, 3 tries 3 s apart, then "unavailable" with a Retry button.
198
+ var box = $("demand");
199
+ if (!box || box.getAttribute("data-state") === "ready") return;
200
+ var timer = null;
201
+ function wireRetry() {
202
+ var b = $("retry");
203
+ if (b) b.addEventListener("click", function (e) { e.preventDefault(); load(0); });
204
+ }
205
+ function loading() {
206
+ box.setAttribute("data-state", "loading");
207
+ box.innerHTML = '<p class="note">Loading demand data from the explorer. The calculator works meanwhile.</p>';
208
+ set("miner-count", "loading");
209
+ missing = WAITING; run();
210
+ }
211
+ function unavailable(why) {
212
+ box.setAttribute("data-state", "unavailable");
213
+ box.innerHTML = "";
214
+ var p = document.createElement("p");
215
+ p.textContent = "The explorer is unavailable (" + why + "). Demand and the miner count cannot be shown right now.";
216
+ box.appendChild(p);
217
+ var w = document.createElement("p"); w.id = "retry-wrap";
218
+ var a = document.createElement("a"); a.className = "btn"; a.id = "retry"; a.href = "/miner"; a.setAttribute("role", "button"); a.textContent = "Retry";
219
+ w.appendChild(a); box.appendChild(w);
220
+ set("miner-count", "unavailable");
221
+ missing = NA; run();
222
+ wireRetry();
223
+ }
224
+ function ready(j) {
225
+ box.innerHTML = j.fragments.demand;
226
+ box.setAttribute("data-state", "ready");
227
+ $("read-info").innerHTML = j.fragments.readInfo;
228
+ miners = Number(j.demand.minerCount) || 0;
229
+ known = true;
230
+ f.setAttribute("data-miners", String(miners));
231
+ set("miner-count", miners.toLocaleString("en-US"));
232
+ run();
233
+ }
234
+ function load(attempt) {
235
+ clearTimeout(timer);
236
+ if (attempt === 0) loading();
237
+ var ctl = window.AbortController ? new AbortController() : null;
238
+ var stop = ctl ? setTimeout(function () { ctl.abort(); }, 30000) : null;
239
+ fetch("/api/miner-stats", { cache: "no-store", credentials: "same-origin", signal: ctl ? ctl.signal : undefined })
240
+ .then(function (r) { return r.json().then(function (j) { return { ok: r.ok, j: j }; }); })
241
+ .then(function (x) {
242
+ clearTimeout(stop);
243
+ if (x.ok && x.j.demand && x.j.fragments) return ready(x.j);
244
+ throw new Error(x.j.error || "no data yet");
245
+ })
246
+ .catch(function (e) {
247
+ clearTimeout(stop);
248
+ if (attempt < 2) timer = setTimeout(function () { load(attempt + 1); }, 3000);
249
+ else unavailable(e && e.name === "AbortError" ? "no answer within 30 s" : (e && e.message) || "no answer");
250
+ });
251
+ }
252
+ wireRetry();
253
+ load(0);
189
254
  })();
190
255
  `;
191
256
  }
package/dist/server.js CHANGED
@@ -406,17 +406,19 @@ export function startServer(port, host = "0.0.0.0") {
406
406
  const mp = await import("./minerpage.js");
407
407
  if (path === "/miner.js")
408
408
  return send(res, 200, "text/javascript; charset=utf-8", mp.minerScript());
409
- const v = path === "/miner" ? await mp.demandWithRetry() : await mp.demand();
409
+ // /miner never waits for the explorer: it renders from the cache (or "loading") and the page script
410
+ // fetches /api/miner-stats in the background. The API waits up to 20 s per try.
411
+ const v = path === "/miner" ? await mp.demand(undefined, 0) : await mp.demand(undefined, 20_000);
410
412
  const d = v?.shown;
411
413
  const fresh = { "cache-control": "public, max-age=60" };
412
414
  if (path === "/api/miner-stats") {
413
415
  const { CALC_DEFAULTS, minerCalc } = await import("./econ.js");
414
416
  const body = d
415
- ? { demand: d, partial: v.partial, latestRead: v.partial ? v.latest : undefined, defaults: CALC_DEFAULTS, defaultResult: minerCalc({ ...CALC_DEFAULTS, minerCount: d.minerCount }), disclaimer: mp.MINER_FOOTER }
417
+ ? { demand: d, partial: v.partial, latestRead: v.partial ? v.latest : undefined, defaults: CALC_DEFAULTS, defaultResult: minerCalc({ ...CALC_DEFAULTS, minerCount: d.minerCount }), fragments: { demand: mp.demandSection(v, "ready"), readInfo: mp.readInfoHtml(v) }, disclaimer: mp.MINER_FOOTER }
416
418
  : { error: mp.demandError() ?? "loading, try again in a minute", disclaimer: mp.MINER_FOOTER };
417
- return send(res, d ? 200 : 503, "application/json", JSON.stringify(body, null, 2), fresh);
419
+ return send(res, d ? 200 : 503, "application/json", JSON.stringify(body, null, 2), d ? fresh : { "cache-control": "no-store" });
418
420
  }
419
- return send(res, 200, "text/html; charset=utf-8", page("Miner economics", mp.minerPage(v, mp.demandError()), { footerNote: mp.MINER_FOOTER, style: mp.MINER_STYLE, script: "/miner.js", cards: true }), fresh);
421
+ return send(res, 200, "text/html; charset=utf-8", page("Miner economics", mp.minerPage(v, mp.demandError(), v?.shown ? "ready" : "loading"), { footerNote: mp.MINER_FOOTER, style: mp.MINER_STYLE, script: "/miner.js", cards: true }), fresh);
420
422
  }
421
423
  if (path === "/") {
422
424
  const body = render(readFileSync(join(SITE, "index.html"), "utf8"))
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "flop-guild",
3
- "version": "0.2.11",
3
+ "version": "0.2.12",
4
4
  "description": "Technocore guild kit: Ed25519 did:key signing CLI for technocore.chat rooms, plus an agent identity scaffold.",
5
5
  "type": "module",
6
6
  "license": "Apache-2.0",