Repository navigation
Expand file tree
/
Copy pathproject-page.js
More file actions
257 lines (245 loc) · 19.4 KB
/
Copy pathproject-page.js
File metadata and controls
257 lines (245 loc) · 19.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/*
* Project Page (/project/<address>) — one scrolling page, the same for visitors and operators.
*
* header ← Explore · name · origin · sync state · contract + Copy · external actions
* rows HOME · CONTROL · MARKET · CONNECTIONS · ECONOMY — each only when it says something true and useful
* DETAILS the Registry proofs, Passport technical data and origin evidence (v2-token.js, unchanged trust model)
*
* Authority never changes what the page SAYS, only which actions appear: the current Passport operator gets
* Edit / Transfer / List / Review & adopt; a wallet that can establish the first Passport (on-chain deployer or
* creator-fee recipient) gets SYNC PROJECT. Every fact comes from v2-token.js (read on-chain first) or from the
* server APIs, which re-verify every signed action. Nothing here moves funds: SYNC PROJECT is one free EIP-712
* signature (the Marketplace OperatorClaim) that the server checks against live factory records.
*/
(function () {
'use strict';
const UI = window.SyncNetUI, W = window.SyncNetWallet, Market = window.SyncNetMarket;
const $ = (id) => document.getElementById(id);
const esc = UI.esc, short = UI.short;
const lc = (v) => String(v == null ? '' : v).toLowerCase();
const HUB = 3;
const SYNC_HELP = 'Verify that you control this project. Nothing moves on-chain.';
let P = null; // facts announced by v2-token.js
const S = { market: null, home: null, homeStatus: null, economy: null, busy: false };
const me = () => { const s = W ? W.state() : null; return s && s.connected ? lc(s.account) : ''; };
const operator = () => (P && P.passport ? lc(P.passport.operator) : '');
/** operator | eligible (can establish the first Passport) | visitor */
function role() {
const a = me();
if (!a || !P) return 'visitor';
if (operator() === a) return 'operator';
if (!P.passport && P.claimable && (a === P.deployer || a === P.feeRecipient)) return 'eligible';
return 'visitor';
}
const syncState = () => (P && P.passport ? 'synced' : 'unclaimed');
const status = (msg, tone) => { const el = $('pjStatus'); el.textContent = msg || ''; if (tone) el.dataset.tone = tone; else delete el.dataset.tone; };
const nonce = () => { const a = new Uint8Array(32); crypto.getRandomValues(a); return '0x' + Array.from(a, (b) => b.toString(16).padStart(2, '0')).join(''); };
async function getJson(url) { const r = await fetch(url, { cache: 'no-store' }); const j = await r.json().catch(() => ({})); if (!r.ok) throw Object.assign(new Error(j.error || 'Unavailable right now.'), { status: r.status, body: j }); return j; }
async function postJson(url, body) { const r = await fetch(url, { method: 'POST', headers: { 'content-type': 'application/json' }, body: JSON.stringify(body) }); const j = await r.json().catch(() => ({})); if (!r.ok) throw Object.assign(new Error(j.error || 'The request was refused.'), { status: r.status, body: j }); return j; }
const draftKey = (t) => 'syncnet_home_draft_' + t;
const hasDraft = (t) => { try { return Boolean(localStorage.getItem(draftKey(t))); } catch { return false; } };
// ------------------------------------------------------------------ header
function renderHead() {
const logo = $('pjLogo');
if (logo && P.token) { const tmp = document.createElement('span'); tmp.innerHTML = UI.logoHtml(P.logoUri, P.symbol || P.name); const n = tmp.firstElementChild; n.id = 'pjLogo'; logo.replaceWith(n); }
const verifiedOp = !P.passport && P.registryOperator;
const stateText = verifiedOp ? 'Verified operator · not synced' : UI.LABEL[syncState()];
$('pjSub').innerHTML = `<span id="tokenDescription" class="sn-mono">$${esc(P.symbol)}</span><span class="sep" aria-hidden="true">·</span><span>${esc(P.originLabel)}</span><span class="sep" aria-hidden="true">·</span><span id="pjState">${UI.stateHtml(syncState(), stateText)}</span>`;
$('pjAddress').textContent = P.token;
$('pjCopy').setAttribute('data-copy-text', P.token);
$('pjCopy').setAttribute('aria-label', 'Copy contract address');
$('pjContract').hidden = false;
// Same protected ticker as a canonical SyncNet asset at a DIFFERENT contract (identity = contract address, from
// the reviewed registry; the ticker comparison is confusable-aware). Always visible — never only inside DETAILS.
const imp = P.impostorOf;
const warn = $('pjWarn');
if (imp) {
warn.innerHTML = `<span class="pj-warn-mark" aria-hidden="true">⚠</span><span><strong>Not the canonical $${esc(imp.symbol)} contract.</strong> Same ticker, different contract. The canonical $${esc(imp.symbol)} is <span class="sn-mono">${esc(short(imp.token))}</span>. <a href="/project/${esc(imp.token)}">Open the canonical $${esc(imp.symbol)} →</a></span>`;
warn.hidden = false;
} else { warn.hidden = true; warn.textContent = ''; }
const acts = [];
// On an impostor, trading is still possible but is labelled by contract, never by the protected ticker alone.
if (P.tradeUrl) acts.push(`<a class="sn-btn" href="${esc(P.tradeUrl)}" target="_blank" rel="noreferrer">${imp ? `Open chart for contract ${esc(short(P.token))} on PAR ↗` : 'OPEN CHART ON PAR ↗'}</a>`);
acts.push(`<a href="https://robinhoodchain.blockscout.com/address/${esc(P.token)}" target="_blank" rel="noreferrer">View on explorer ↗</a>`);
$('pjActions').innerHTML = acts.join('');
// The token's own on-chain description (PAR metadata), clearly attributed. Never shown for a ticker impostor, whose
// self-description could impersonate the canonical project.
const desc = $('pjDesc');
if (desc) {
if (P.description && !imp) { desc.innerHTML = `${esc(P.description)}<span class="pj-desc-src">From the token’s on-chain metadata</span>`; desc.hidden = false; }
else { desc.hidden = true; desc.textContent = ''; }
}
}
// ------------------------------------------------------------------ rows
const rowHtml = (key, label, value, action, note, panel) => `<li class="pj-row" data-row="${key}"><span class="pj-row-k" id="row-${key}">${esc(label)}</span><div class="pj-row-v" aria-labelledby="row-${key}">${value}</div><div class="pj-row-a">${action || ''}</div>${note ? `<p class="pj-note">${note}</p>` : ''}${panel || ''}</li>`;
function homeRow() {
const cfg = S.home, st = S.homeStatus;
if (!cfg || !cfg.enabled || !st) return '';
const r = role(), t = P.token;
const site = st.site, ent = st.entitlement, intent = st.openIntent;
const entOk = ent && (ent.status === 'ACTIVE' || ent.status === 'FINALIZED');
const published = site && site.state === 'PUBLISHED';
const byCurrent = published && operator() && lc(site.signer) === operator();
const edit = `/home-editor.html?token=${esc(t)}`;
const open = `<a href="/site/${esc(t)}" target="_blank" rel="noopener">Open home ↗</a>`;
if (st.suspension) return rowHtml('home', 'Home', 'Unavailable<small>SyncNet is not serving this Project Home right now.' + (r === 'operator' ? ' Its activation, history and Project Passport are unchanged.' : '') + '</small>', r === 'operator' ? '<a href="/contact.html#report">Contact →</a>' : '');
if (ent && ent.status === 'INVALIDATED_BY_REORG') {
return rowHtml('home', 'Home', 'Suspended<small>The activation payment left the canonical chain. The home is paused until the same payment is confirmed again.</small>', r === 'operator' ? `<a href="${edit}">Check status →</a>` : '');
}
if (published && entOk && byCurrent) return rowHtml('home', 'Home', 'Live<small>Signed by the current Passport operator.</small>', r === 'operator' ? `<a href="${edit}">Edit →</a>` : open, r === 'operator' ? open : '');
if (published && entOk) {
if (r === 'operator') return rowHtml('home', 'Home', 'Previous operator’s home<small>Published before you became the operator. Its links stay disabled until you review and adopt it.</small>', `<a class="sn-btn primary" href="${edit}&review=1">REVIEW & ADOPT</a>`);
return rowHtml('home', 'Home', 'Awaiting confirmation<small>Published by a previous operator. Links stay disabled until the current operator confirms it.</small>', open);
}
if (r !== 'operator') {
if (r === 'eligible') return rowHtml('home', 'Home', 'Needs Sync<small>A Project Home belongs to the Passport operator. Sync the project first.</small>', '');
return '';
}
if (site && site.state === 'UNPUBLISHED') return rowHtml('home', 'Home', 'Unpublished<small>Your last version is kept and can be published again.</small>', `<a href="${edit}">Edit →</a>`);
if (entOk) return rowHtml('home', 'Home', 'Draft<small>Activated · not published yet.</small>', `<a class="sn-btn primary" href="${edit}">Edit & publish</a>`);
if (intent && intent.observed) return rowHtml('home', 'Home', 'Publishing<small>Payment seen · confirming on Robinhood Chain. You can leave this page.</small>', `<a href="${edit}&step=pay">Check status →</a>`);
if (intent && intent.status === 'OPEN' && Date.parse(intent.expiresAt) > Date.now()) return rowHtml('home', 'Home', 'Needs payment<small>Your quote is locked until ' + esc(new Date(intent.expiresAt).toLocaleTimeString([], { hour: '2-digit', minute: '2-digit' })) + '.</small>', `<a href="${edit}&step=pay">Continue →</a>`);
if (hasDraft(t)) return rowHtml('home', 'Home', 'Draft<small>Saved in this browser. Not published.</small>', `<a href="${edit}">Edit →</a>`);
return rowHtml('home', 'Home', 'No home yet<small>A simple public page for this project, signed by you. Preview is free.</small>', `<a href="${edit}">Create home →</a>`);
}
function controlRow() {
if (P.origin === 'UNAVAILABLE') return rowHtml('control', 'Control', 'Unknown<small>Robinhood Chain could not be read right now.</small>', '');
const r = role();
if (P.passport) {
const since = String(P.passport.operatorSince || '').slice(0, 10);
const who = r === 'operator' ? 'You are the operator' : `Operator <span class="sn-mono">${esc(short(P.passport.operator))}</span>`;
return rowHtml('control', 'Control', `<span id="pjControlState">${UI.stateHtml('synced')}</span><small>${who}${since ? ` · since <span class="sn-mono">${esc(since)}</span>` : ''}</small>`,
r === 'operator' ? `<a href="/marketplace.html#sell=${esc(P.token)}">Transfer →</a>` : '');
}
if (!P.claimable) {
const why = P.origin === 'PONS_V1' ? 'Earlier Pons launches cannot hold a Project Passport.' : 'SyncNet can sync PAR and Pons V2 launches only.';
return rowHtml('control', 'Control', `${UI.stateHtml('unclaimed')}<small>${esc(why)}</small>`, '');
}
const label = P.registryOperator ? 'Verified operator · not synced' : 'Unclaimed';
if (r === 'eligible') {
const basis = me() === P.deployer ? 'the on-chain deployer' : 'the creator-fee recipient';
return rowHtml('control', 'Control', `<span id="pjControlState">${UI.stateHtml('unclaimed', label)}</span><small>Your wallet is ${basis}.</small>`,
`<button class="sn-btn primary" type="button" id="syncProject"${S.market === false ? ' disabled aria-disabled="true"' : ''}>SYNC PROJECT</button>`, esc(S.market === false ? 'Syncing is paused right now. Try again later.' : SYNC_HELP));
}
if (me()) return rowHtml('control', 'Control', `${UI.stateHtml('unclaimed', label)}<small>Only the deployer or the creator-fee recipient wallet can sync this project.</small>`, '');
return rowHtml('control', 'Control', `${UI.stateHtml('unclaimed', label)}<small>Is this your project? Connect the deployer or creator-fee recipient wallet.</small>`, '<button type="button" data-connect>Connect</button>');
}
function marketRow() {
const l = P.listing, r = role();
if (l && l.status === 'ACTIVE') return rowHtml('market', 'Market', `<span class="is-for-sale">For sale</span> · <span class="sn-num">${esc(l.price)} ${esc(l.currency)}</span><small>An operator-signed listing on the SyncNet Marketplace.</small>`, `<a href="/marketplace.html#listing=${esc(l.id)}">View listing →</a>`);
if (l && l.status) return rowHtml('market', 'Market', `Listing · ${esc(String(l.status).replace(/_/g, ' ').toLowerCase())}`, `<a href="/marketplace.html#listing=${esc(l.id)}">View listing →</a>`);
if (r === 'operator') return rowHtml('market', 'Market', 'Not listed<small>Sell control of this project through a two-party Marketplace transfer.</small>', `<a href="/marketplace.html#sell=${esc(P.token)}">List →</a>`);
return '';
}
/** The project's DIRECT markets as read from its factory record (never inferred): quote-asset labels, each linking to
* that asset's own Project Page. A quote that imitates a protected canonical ticker is marked, never passed off. */
function marketLabels() {
return (Array.isArray(P.markets) ? P.markets : []).map((m) => `<a class="pj-asset" href="/project/${esc(m.address)}">${esc(m.symbol)}</a>${m.notCanonical ? ' <span class="sn-flag">not canonical</span>' : ''}`);
}
function connectionsRow() {
const n = P.directMarkets || 0, u = P.usedBy || 0;
if (!n && !u) return '';
const named = marketLabels();
let value = named.length ? `Markets · ${named.join('<span class="sep" aria-hidden="true"> · </span>')}` : n ? `<span class="sn-num">${n}</span> direct market${n === 1 ? '' : 's'}` : '';
const used = u ? `Used by <span class="sn-num">${u}</span> project${u === 1 ? '' : 's'}${u >= HUB ? ' · Network hub' : ''}` : '';
if (!value) { value = used; }
const small = value === used ? '' : used ? `<small>${used}</small>` : '';
const links = [];
// Never offer "connect to $TICKER" on a contract that only imitates a protected ticker.
if (P.origin === 'PAR' && !P.impostorOf) links.push(`<a href="/build.html?with=${esc(P.token)}">Create a project connected to $${esc(P.symbol)} →</a>`);
return rowHtml('connections', 'Connections', value + small, `<a href="/network.html?token=${esc(P.token)}">View network →</a>`, links.join(''));
}
/** Where the project's creator fees go, from the LIVE factory record (PAR only), plus curated-economy facts. */
function feeFlow() {
const assets = (Array.isArray(P.markets) ? P.markets : []).filter((m) => !m.notCanonical).map((m) => esc(m.symbol));
switch (P.feeMode) {
case 'holders': return { value: `Holder rewards · ${[...assets, '$' + esc(P.symbol)].join(' · ')}`, note: 'Creator fees go to PAR’s holder vault. PAR’s distributor pays holders in the traded assets, in rounds. Not guaranteed.' };
case 'burn': return { value: 'Creator fees · buyback & burn', note: 'Sent to PAR’s burn vault. Buyback rounds are run by PAR.' };
case 'floor': return { value: 'Creator fees · price floor', note: 'Sent to PAR’s floor vault.' };
case 'creator': return { value: 'Creator fees · to a wallet', note: `Recipient <span class="sn-mono">${esc(short(P.feeRecipient))}</span>` };
default: return null;
}
}
function economyRow() {
const e = S.economy;
const n = e && e.recognized ? e.recognized.length : 0;
// Curation is meaningful when projects are recognised, or when this operator (the curator) has projects to curate.
const curated = Boolean(e && (n || (role() === 'operator' && (P.usedBy || 0) > 0)));
const fee = feeFlow();
if (!fee && !curated) return '';
const who = curated ? `<span class="sn-num">${n}</span> recognised project${n === 1 ? '' : 's'} · ${e.curator && e.curator.address ? `curated by <span class="sn-mono">${esc(short(e.curator.address))}</span>` : 'no curator yet'}` : '';
const value = fee ? fee.value + `<small>${fee.note}</small>` + (who ? `<small>${who}</small>` : '') : who;
const act = curated || (P.usedBy || 0) > 0 ? `<a href="/economy.html?root=${esc(P.token)}">View economy →</a>` : '';
return rowHtml('economy', 'Economy', value, act);
}
function render() {
if (!P || P.invalid || P.unavailable) return;
renderHead();
$('projectRows').innerHTML = [homeRow(), controlRow(), marketRow(), connectionsRow(), economyRow()].join('');
const b = $('syncProject'); if (b) b.addEventListener('click', syncProject);
document.querySelectorAll('#projectRows [data-connect]').forEach((x) => x.addEventListener('click', () => W.connect().catch((e) => status(e.message, 'bad'))));
}
// ------------------------------------------------------------------ SYNC PROJECT (Marketplace OperatorClaim)
async function syncProject() {
if (S.busy || !P) return;
const account = me();
if (!account) { await W.connect(); return; }
const basis = account === P.deployer ? 'deployer' : account === P.feeRecipient ? 'fee-recipient' : '';
if (!basis || P.passport) { render(); return; }
S.busy = true;
const btn = $('syncProject'); if (btn) btn.disabled = true;
try {
const message = { token: P.token, operator: account, basis, nonce: nonce(), expiry: Math.floor(Date.now() / 1000) + 900 };
status('Sign in your wallet to verify control. This is a free signature, not a transaction.');
let signature;
try { signature = await W.signTyped(Market.typedData('OperatorClaim', message)); }
catch (e) { throw new Error(e && (e.code === 4001 || /reject|denied/i.test(String(e.message))) ? 'You rejected the signature. Nothing was saved.' : 'The wallet could not sign: ' + String((e && e.message) || e).slice(0, 140)); }
const head = document.querySelector('#pjState .sn-state'), ctl = document.querySelector('#pjControlState .sn-state');
[head, ctl].forEach((el) => { if (el) { el.dataset.state = 'syncing'; el.querySelector('.sn-glyph').outerHTML = UI.glyph('syncing'); el.querySelector('span').textContent = 'Syncing'; } });
status('Syncing… SyncNet is checking your wallet against the launch record.');
const r = await postJson('/api/marketplace', { action: 'claim', ...message, signature });
P.passport = { operator: r.passport.operator, operatorSince: r.passport.operatorSince, history: r.passport.history };
await Promise.all([head, ctl].map((el) => UI.animateSync(el)));
render();
status('Synced. Your Project Passport is recorded. Nothing moved on-chain.', 'ok');
loadHome();
} catch (e) {
render();
status(e.message || 'Sync failed.', 'bad');
} finally { S.busy = false; }
}
// ------------------------------------------------------------------ data
async function loadHome() {
try {
const cfg = await getJson('/api/project-home?view=config');
S.home = cfg;
S.homeStatus = cfg.enabled ? await getJson('/api/project-home?view=status&token=' + P.token) : null;
} catch { S.home = null; S.homeStatus = null; }
render();
}
async function load() {
const [mk, eco] = await Promise.all([
getJson('/api/marketplace?view=config').then((j) => j.enabled !== false).catch(() => null),
getJson('/api/economies?view=economy&root=' + P.token).catch(() => null),
]);
S.market = mk; S.economy = eco;
render();
loadHome();
}
function onProject(d) {
if (P || !d) return;
P = d;
if (d.invalid || d.unavailable) return;
P.token = lc(d.token);
render();
load();
if (W) W.onChange(() => render());
}
function init() {
const openDetails = () => { if (location.hash === '#details') { const d = $('projectDetails'); if (d) { d.open = true; d.scrollIntoView({ block: 'start' }); } } };
openDetails(); window.addEventListener('hashchange', openDetails);
window.addEventListener('syncnet:project', (e) => onProject(e.detail));
if (window.__snProject) onProject(window.__snProject);
}
if (document.readyState === 'loading') document.addEventListener('DOMContentLoaded', init); else init();
})();