-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlive.html
More file actions
401 lines (359 loc) · 16.5 KB
/
live.html
File metadata and controls
401 lines (359 loc) · 16.5 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Real-Time objkt.com Events</title>
<style>
body {
font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
background-color: #1a1a1a;
color: #e0e0e0;
margin: 0;
padding: 20px;
line-height: 1.6;
}
#app {
max-width: 800px;
margin: 0 auto;
}
h1 {
color: #ffffff;
text-align: center;
border-bottom: 2px solid #444;
padding-bottom: 10px;
}
#status-bar {
text-align: center;
padding: 10px;
margin-bottom: 20px;
border-radius: 6px;
font-weight: bold;
transition: background-color 0.3s ease;
}
.status-connecting { background-color: #4a3d0b; color: #ffd966; }
.status-connected { background-color: #1e4d2b; color: #a7f3d0; }
.status-disconnected { background-color: #5c2121; color: #fca5a5; }
.status-polling { background-color: #1e3a8a; color: #93c5fd; }
#listings-log {
display: flex;
flex-direction: column;
gap: 15px;
}
.listing-card {
background-color: #2b2b2b;
border: 1px solid #444;
border-radius: 8px;
padding: 15px 20px;
animation: fadeIn 0.5s ease-in-out;
}
.listing-card p {
margin: 5px 0;
}
.listing-card strong {
color: #bb86fc;
}
.listing-card code {
background-color: #3a3a3a;
padding: 2px 5px;
border-radius: 4px;
font-size: 0.9em;
color: #ccc;
}
.listing-card a {
color: #03dac6;
text-decoration: none;
font-weight: bold;
}
.listing-card a:hover {
text-decoration: underline;
}
.error-message {
background-color: #5c2121;
border: 1px solid #dc2626;
border-radius: 8px;
padding: 15px;
margin-bottom: 20px;
color: #fca5a5;
}
.retry-button {
background-color: #1e40af;
color: white;
border: none;
padding: 8px 16px;
border-radius: 4px;
cursor: pointer;
margin-left: 10px;
}
.retry-button:hover {
background-color: #1d4ed8;
}
@keyframes fadeIn {
from { opacity: 0; transform: translateY(-10px); }
to { opacity: 1; transform: translateY(0); }
}
</style>
<script src="https://www.gstatic.com/cast/sdk/libs/caf_receiver/v3/cast_receiver_framework.js"></script>
<!-- Cast Debug Logger -->
<script src="https://www.gstatic.com/cast/sdk/libs/devtools/debug_layer/caf_receiver_logger.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/microsoft-signalr/6.0.1/signalr.min.js"></script>
</head>
<body>
<div id="app">
<h1>Real-Time objkt.com Events</h1>
<div id="status-bar" class="status-connecting">Initializing...</div>
<div id="error-container"></div>
<h2>Listings & Sales</h2>
<div id="listings-log">
<!-- New listings will be added here by JavaScript -->
</div>
<h2 style="margin-top: 40px;">Mints</h2>
<div id="mints-log">
<!-- New mints will be added here by JavaScript -->
</div>
</div>
<script>
document.addEventListener('DOMContentLoaded', () => {
// Check if the receiver is running on a real Chromecast device.
// The Chromecast user agent contains the string 'CrKey'.
if (!/CrKey/.test(navigator.userAgent)) {
document.body.innerHTML = `
<div style="position: absolute; top: 0; left: 0; width: 100%; height: 100%; background-color: #121212; color: #e0e0e0; display: flex; justify-content: center; align-items: center; text-align: center; font-family: sans-serif;">
<div style="max-width: 600px; padding: 20px;">
<h1 style="color: #FF5722;">This is a Chromecast Receiver</h1>
<p style="font-size: 1.2em;">This page is not meant to be opened directly in your browser.</p>
<p>To view this application, you must launch it from the sender page on a real Chromecast device.</p>
<p style="margin-top: 30px; font-size: 1.1em;">
1. Make sure your local server is running.<br/>
2. Open <strong style="color: #FF5722;">sender.html</strong> in your Chrome browser.<br/>
3. Click the Cast icon and select your device.
</p>
</div>
</div>
`;
// Stop the script to prevent errors.
return;
}
const context = cast.framework.CastReceiverContext.getInstance();
const playerManager = context.getPlayerManager();
// Debug Logger
const castDebugLogger = cast.debug.CastDebugLogger.getInstance();
const LOG_TAG = 'ArtCastReceiver';
castDebugLogger.setEnabled(true);
castDebugLogger.loggerLevelByTags = {
[LOG_TAG]: cast.framework.LoggerLevel.DEBUG,
};
const namespace = 'urn:x-cast:com.google.cast.sample.artcast';
context.addCustomMessageListener(namespace, (customEvent) => {
castDebugLogger.info(LOG_TAG, `Message from sender: ${JSON.stringify(customEvent.data)}`);
// You can handle messages from the sender here.
// For example, you could change the contract address to watch.
});
context.start({
statusText: "ArtCast Receiver is ready."
});
context.addEventListener(cast.framework.system.EventType.READY, () => {
castDebugLogger.info(LOG_TAG, 'Receiver is ready.');
// Start the main application logic once the receiver is ready
connect();
});
context.addEventListener(cast.framework.system.EventType.SENDER_CONNECTED, (event) => {
castDebugLogger.info(LOG_TAG, `Sender connected: ${event.senderId}`);
});
context.addEventListener(cast.framework.system.EventType.SENDER_DISCONNECTED, (event) => {
castDebugLogger.info(LOG_TAG, `Sender disconnected: ${event.senderId}`);
// If the last sender disconnects, you might want to stop the app.
if (context.getSenders().length === 0) {
// window.close(); // This will close the receiver app
}
});
});
// --- Configuration ---
const TZKT_API_URL = "https://api.tzkt.io/v1/events";
const OBJKT_MARKETPLACE_CONTRACT = "KT1WvzYHCNBvDSdwafTHv7nJ1dWmZ8GCYuuC";
// Entrypoints to monitor on the marketplace contract
const MARKETPLACE_ENTRYPOINTS = ["ask", "fulfill_ask", "bid", "fulfill_bid", "retract_ask", "retract_bid"];
const MINT_ENTRYPOINT = "mint";
// --- DOM Element References ---
const statusBar = document.getElementById('status-bar');
const listingsLog = document.getElementById('listings-log');
const errorContainer = document.getElementById('error-container');
const mintsLog = document.getElementById('mints-log');
// --- SignalR Logic ---
let connection;
async function connect() {
updateStatus('Connecting...', 'status-connecting');
connection = new signalR.HubConnectionBuilder()
.withUrl(TZKT_API_URL)
.withAutomaticReconnect()
.build();
connection.onreconnecting(error => {
console.warn(`Connection lost due to error "${error}". Reconnecting.`);
updateStatus('Reconnecting...', 'status-connecting');
});
connection.onreconnected(connectionId => {
console.log(`Connection re-established. Connected with connectionId "${connectionId}".`);
updateStatus('Connected to TzKT WebSocket', 'status-connected');
});
connection.onclose(error => {
console.error(`Connection closed. ${error ? `Error: ${error}` : ''}`);
updateStatus('Disconnected. Will attempt to reconnect automatically...', 'status-disconnected');
});
connection.on('operations', (msg) => {
try {
if (msg.type === 1 && msg.data) {
msg.data.forEach(op => {
if (op.type !== "transaction" || op.status !== "applied") {
return;
}
// Marketplace Events (List, Sale, etc.)
if (
op.target?.address === OBJKT_MARKETPLACE_CONTRACT &&
MARKETPLACE_ENTRYPOINTS.includes(op.parameter?.entrypoint)
) {
console.log("New Marketplace Event:", op);
displayMarketplaceEvent(op);
}
// Mint Events
if (op.parameter?.entrypoint === MINT_ENTRYPOINT) {
console.log("New Mint Detected:", op);
displayNewMint(op);
}
});
}
} catch (e) {
console.error("Error processing message:", e, msg);
}
});
try {
await connection.start();
console.log("SignalR Connection successful!");
updateStatus('Connected to TzKT WebSocket', 'status-connected');
// Subscribe to Marketplace
await connection.invoke("SubscribeToOperations", {
address: OBJKT_MARKETPLACE_CONTRACT,
types: "transaction"
});
console.log(`Subscribed to marketplace contract: ${OBJKT_MARKETPLACE_CONTRACT}`);
// Subscribe to Mints
await connection.invoke("SubscribeToOperations", {
entrypoint: MINT_ENTRYPOINT,
types: "transaction"
});
console.log(`Subscribed to all '${MINT_ENTRYPOINT}' operations.`);
} catch (err) {
console.error("SignalR Connection failed: ", err);
updateStatus('Connection failed. Retrying in 5 seconds...', 'status-disconnected');
setTimeout(connect, 5000);
}
}
// --- UI Update Functions ---
function updateStatus(text, className) {
statusBar.textContent = text;
statusBar.className = '';
statusBar.classList.add('status-bar', className);
}
function showError(message, isPermanent = false) {
errorContainer.innerHTML = `
<div class="error-message">
⚠️ ${message}
${!isPermanent ? '<button class="retry-button" onclick="retryConnection()">Retry Connection</button>' : ''}
</div>
`;
}
function clearError() {
errorContainer.innerHTML = '';
}
function retryConnection() {
connect();
}
function displayMarketplaceEvent(op) {
try {
const entrypoint = op.parameter?.entrypoint;
let title = `✅ New Event: ${entrypoint}`;
let details = '';
const seller = op.sender.address;
const timestamp = new Date(op.timestamp).toLocaleString();
const tzktUrl = `https://tzkt.io/${op.hash}`;
let objktUrl = `https://objkt.com/tx/${op.hash}`; // Default to transaction link
switch(entrypoint) {
case 'ask':
title = '✅ New Listing';
const askParams = op.parameter.value;
const priceMutez = parseInt(askParams.price);
const priceTez = priceMutez / 1_000_000;
const fa2Contract = askParams.token.address;
const tokenId = askParams.token.token_id;
objktUrl = `https://objkt.com/asset/${fa2Contract}/${tokenId}`; // More specific link
details = `
<p><strong>Seller:</strong> <code>${seller}</code></p>
<p><strong>Price:</strong> ${priceTez.toLocaleString('en-US', { minimumFractionDigits: 2, maximumFractionDigits: 6 })} tez</p>
`;
break;
case 'fulfill_ask':
title = '🔥 Sale';
details = `
<p><strong>Buyer:</strong> <code>${seller}</code></p>
<p>A listing was fulfilled.</p>
`;
break;
default:
// Generic display for other entrypoints
details = `<p><strong>Sender:</strong> <code>${seller}</code></p>`;
break;
}
const card = document.createElement('div');
card.classList.add('listing-card');
card.innerHTML = `
<p><strong>${title}</strong> <small>${timestamp}</small></p>
${details}
<p>
<a href="${objktUrl}" target="_blank" rel="noopener noreferrer">View on objkt.com</a> |
<a href="${tzktUrl}" target="_blank" rel="noopener noreferrer">View on TzKT</a>
</p>
`;
listingsLog.prepend(card);
if (listingsLog.children.length > 50) {
listingsLog.removeChild(listingsLog.lastChild);
}
} catch (e) {
console.error("Failed to parse and display marketplace event:", e, op);
}
}
function displayNewMint(op) {
try {
const minter = op.sender.address;
const contract = op.target.address;
const timestamp = new Date(op.timestamp).toLocaleString();
const tzktUrl = `https://tzkt.io/${op.hash}`;
let objktUrl = `https://objkt.com/tx/${op.hash}`; // Default to tx link
// Try to find token_id to build a direct asset link
const tokenId = op.parameter?.value?.token_id;
if (tokenId !== undefined) {
objktUrl = `https://objkt.com/asset/${contract}/${tokenId}`;
}
const card = document.createElement('div');
card.classList.add('listing-card');
card.innerHTML = `
<p><strong>🌱 New Mint!</strong> <small>${timestamp}</small></p>
<p><strong>Minter:</strong> <code>${minter}</code></p>
<p><strong>Contract:</strong> <code>${contract}</code></p>
<p>
<a href="${objktUrl}" target="_blank" rel="noopener noreferrer">View on objkt.com</a> |
<a href="${tzktUrl}" target="_blank" rel="noopener noreferrer">View on TzKT</a>
</p>
`;
mintsLog.prepend(card);
if (mintsLog.children.length > 50) {
mintsLog.removeChild(mintsLog.lastChild);
}
} catch (e) {
console.error("Failed to parse and display mint:", e, op);
}
}
// --- Initial Call ---
// We will now call connect() when the cast receiver is ready.
// document.addEventListener('DOMContentLoaded', connect);
</script>
</body>
</html>