SC CODE: Function InitializePrivate() Uint64
10 IF init() == 0 THEN GOTO 30
20 RETURN 1
30 STORE("var_header_name", "ws.js")
31 STORE("var_header_description", "")
32 STORE("var_header_icon", "")
33 STORE("dURL", "")
34 STORE("docType", "TELA-JS-1")
35 STORE("subDir", "/")
36 STORE("fileCheckC", "2d7b1452f42652c46c23651d4a38d8e57ffc806489b6e85b475ad81277d5c300")
37 STORE("fileCheckS", "27fd0926edc1547d9ae807fd644fc0d6bcb7dc1aa809917fe2a7485e982ddb0a")
100 RETURN 0
End Function
Function init() Uint64
10 IF EXISTS("owner") == 0 THEN GOTO 30
20 RETURN 1
30 STORE("owner", address())
50 STORE("docVersion", "1.0.0")
60 STORE("hash", HEX(TXID()))
70 STORE("likes", 0)
80 STORE("dislikes", 0)
100 RETURN 0
End Function
Function address() String
10 DIM s as String
20 LET s = SIGNER()
30 IF IS_ADDRESS_VALID(s) THEN GOTO 50
40 RETURN "anon"
50 RETURN ADDRESS_STRING(s)
End Function
Function Rate(r Uint64) Uint64
10 DIM addr as String
15 LET addr = address()
16 IF r < 100 && EXISTS(addr) == 0 && addr != "anon" THEN GOTO 30
20 RETURN 1
30 STORE(addr, ""+r+"_"+BLOCK_HEIGHT())
40 IF r < 50 THEN GOTO 70
50 STORE("likes", LOAD("likes")+1)
60 RETURN 0
70 STORE("dislikes", LOAD("dislikes")+1)
100 RETURN 0
End Function
/*
// ws.js - WebSocket manager (wallet-friendly disconnect)
if (!window.wsManager) {
window.wsManager = {
socket: null,
intervalId: null,
applicationData: {
id: "6631df0da874d1f3e9431b5e81a363d507b1f47ff3703c100de2f9c27ed224c9",
name: "AS's Playground Live",
description: "Basic WS connection parts for TELA application",
url: "http://localhost:" + location.port,
},
// Toggle connection
toggleConnection: function () {
if (this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING)) {
this.disconnect();
} else {
this.connect();
}
},
// Connect
connect: function () {
if (this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING)) {
console.log("WebSocket already active");
return;
}
this.socket = new WebSocket("ws://localhost:44326/xswd");
const connectButton = document.getElementById("connectButton");
this.socket.addEventListener("open", () => {
console.log("WebSocket connected");
this.updateStatus("Waiting for wallet acceptance...", "text-xs text-gray-600 dark:text-gray-400");
// Send app data once
this.sendData(this.applicationData);
if (connectButton) connectButton.innerText = "Disconnect";
});
this.socket.addEventListener("message", (event) => {
const response = JSON.parse(event.data);
console.log("Message received:", response);
// Authorization handling
if (response.accepted && response.message) {
this.updateStatus("Authorized", "text-xs text-green-600 dark:text-green-400");
// Start interval only after authorization
if (!this.intervalId) {
this.intervalId = setInterval(() => {
this.sendDEROGetInfoRequest();
attemptEPOCHfun?.();
}, 1000);
}
getEPOCHmaxHashes?.();
return;
}
const responseId = response.id ? String(response.id).replace(/^"|"$/g, "") : null;
switch (responseId) {
case "nodeStatusChannel":
if (response.result) updateData?.(response.result);
break;
case "maxEPOCHhashChannel":
handleEPOCHmaxHashesResponse?.(response.result);
break;
case "AttemptEPOCHchannel":
handleEPOCHresponse?.(response.result);
break;
default:
console.warn("Unhandled response ID:", responseId);
}
});
this.socket.addEventListener("error", (event) => {
console.error("WebSocket error:", event);
this.cleanupAfterDisconnect();
this.updateStatus("Check Cyberdeck", "text-xs text-red-600 dark:text-red-400");
});
this.socket.addEventListener("close", (event) => {
console.log("WebSocket closed:", event.code, event.reason);
this.cleanupAfterDisconnect();
});
},
// Disconnect (wallet-friendly)
disconnect: function () {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
// Notify wallet/server first
this.sendData({ jsonrpc: "2.0", method: "Disconnect", id: "clientDisconnect" });
// Close WebSocket
this.socket.close();
} else {
this.cleanupAfterDisconnect();
}
},
// Cleanup function (interval + UI + socket reference)
cleanupAfterDisconnect: function () {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
this.updateStatus("Not connected", "text-xs text-gray-600 dark:text-gray-400");
const connectButton = document.getElementById("connectButton");
if (connectButton) connectButton.innerText = "Connect";
this.socket = null;
},
// Send JSON data safely
sendData: function (data) {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
try {
this.socket.send(JSON.stringify(data));
console.log("Data sent:", data);
} catch (err) {
console.error("Failed to send:", err);
}
} else {
console.log("WebSocket not open. State:", this.socket?.readyState);
}
},
// Periodic DERO.GetInfo request
sendDEROGetInfoRequest: function () {
this.sendData({
jsonrpc: "2.0",
method: "DERO.GetInfo",
id: "nodeStatusChannel",
});
},
// Update status UI
updateStatus: function (text, css) {
const wsStatus = document.getElementById("wsStatus");
if (wsStatus) {
wsStatus.innerText = text;
wsStatus.className = css;
}
},
};
console.log("wsManager initialized globally");
}
// Global function for navbar button
function connectWebSocket() {
window.wsManager.toggleConnection();
}
*/ |
| SC Arguments: [Name:SC_ACTION Type:uint64 Value:'1' Name:SC_CODE Type:string Value:'Function InitializePrivate() Uint64
10 IF init() == 0 THEN GOTO 30
20 RETURN 1
30 STORE("var_header_name", "ws.js")
31 STORE("var_header_description", "")
32 STORE("var_header_icon", "")
33 STORE("dURL", "")
34 STORE("docType", "TELA-JS-1")
35 STORE("subDir", "/")
36 STORE("fileCheckC", "2d7b1452f42652c46c23651d4a38d8e57ffc806489b6e85b475ad81277d5c300")
37 STORE("fileCheckS", "27fd0926edc1547d9ae807fd644fc0d6bcb7dc1aa809917fe2a7485e982ddb0a")
100 RETURN 0
End Function
Function init() Uint64
10 IF EXISTS("owner") == 0 THEN GOTO 30
20 RETURN 1
30 STORE("owner", address())
50 STORE("docVersion", "1.0.0")
60 STORE("hash", HEX(TXID()))
70 STORE("likes", 0)
80 STORE("dislikes", 0)
100 RETURN 0
End Function
Function address() String
10 DIM s as String
20 LET s = SIGNER()
30 IF IS_ADDRESS_VALID(s) THEN GOTO 50
40 RETURN "anon"
50 RETURN ADDRESS_STRING(s)
End Function
Function Rate(r Uint64) Uint64
10 DIM addr as String
15 LET addr = address()
16 IF r < 100 && EXISTS(addr) == 0 && addr != "anon" THEN GOTO 30
20 RETURN 1
30 STORE(addr, ""+r+"_"+BLOCK_HEIGHT())
40 IF r < 50 THEN GOTO 70
50 STORE("likes", LOAD("likes")+1)
60 RETURN 0
70 STORE("dislikes", LOAD("dislikes")+1)
100 RETURN 0
End Function
/*
// ws.js - WebSocket manager (wallet-friendly disconnect)
if (!window.wsManager) {
window.wsManager = {
socket: null,
intervalId: null,
applicationData: {
id: "6631df0da874d1f3e9431b5e81a363d507b1f47ff3703c100de2f9c27ed224c9",
name: "AS's Playground Live",
description: "Basic WS connection parts for TELA application",
url: "http://localhost:" + location.port,
},
// Toggle connection
toggleConnection: function () {
if (this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING)) {
this.disconnect();
} else {
this.connect();
}
},
// Connect
connect: function () {
if (this.socket && (this.socket.readyState === WebSocket.OPEN || this.socket.readyState === WebSocket.CONNECTING)) {
console.log("WebSocket already active");
return;
}
this.socket = new WebSocket("ws://localhost:44326/xswd");
const connectButton = document.getElementById("connectButton");
this.socket.addEventListener("open", () => {
console.log("WebSocket connected");
this.updateStatus("Waiting for wallet acceptance...", "text-xs text-gray-600 dark:text-gray-400");
// Send app data once
this.sendData(this.applicationData);
if (connectButton) connectButton.innerText = "Disconnect";
});
this.socket.addEventListener("message", (event) => {
const response = JSON.parse(event.data);
console.log("Message received:", response);
// Authorization handling
if (response.accepted && response.message) {
this.updateStatus("Authorized", "text-xs text-green-600 dark:text-green-400");
// Start interval only after authorization
if (!this.intervalId) {
this.intervalId = setInterval(() => {
this.sendDEROGetInfoRequest();
attemptEPOCHfun?.();
}, 1000);
}
getEPOCHmaxHashes?.();
return;
}
const responseId = response.id ? String(response.id).replace(/^"|"$/g, "") : null;
switch (responseId) {
case "nodeStatusChannel":
if (response.result) updateData?.(response.result);
break;
case "maxEPOCHhashChannel":
handleEPOCHmaxHashesResponse?.(response.result);
break;
case "AttemptEPOCHchannel":
handleEPOCHresponse?.(response.result);
break;
default:
console.warn("Unhandled response ID:", responseId);
}
});
this.socket.addEventListener("error", (event) => {
console.error("WebSocket error:", event);
this.cleanupAfterDisconnect();
this.updateStatus("Check Cyberdeck", "text-xs text-red-600 dark:text-red-400");
});
this.socket.addEventListener("close", (event) => {
console.log("WebSocket closed:", event.code, event.reason);
this.cleanupAfterDisconnect();
});
},
// Disconnect (wallet-friendly)
disconnect: function () {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
// Notify wallet/server first
this.sendData({ jsonrpc: "2.0", method: "Disconnect", id: "clientDisconnect" });
// Close WebSocket
this.socket.close();
} else {
this.cleanupAfterDisconnect();
}
},
// Cleanup function (interval + UI + socket reference)
cleanupAfterDisconnect: function () {
if (this.intervalId) {
clearInterval(this.intervalId);
this.intervalId = null;
}
this.updateStatus("Not connected", "text-xs text-gray-600 dark:text-gray-400");
const connectButton = document.getElementById("connectButton");
if (connectButton) connectButton.innerText = "Connect";
this.socket = null;
},
// Send JSON data safely
sendData: function (data) {
if (this.socket && this.socket.readyState === WebSocket.OPEN) {
try {
this.socket.send(JSON.stringify(data));
console.log("Data sent:", data);
} catch (err) {
console.error("Failed to send:", err);
}
} else {
console.log("WebSocket not open. State:", this.socket?.readyState);
}
},
// Periodic DERO.GetInfo request
sendDEROGetInfoRequest: function () {
this.sendData({
jsonrpc: "2.0",
method: "DERO.GetInfo",
id: "nodeStatusChannel",
});
},
// Update status UI
updateStatus: function (text, css) {
const wsStatus = document.getElementById("wsStatus");
if (wsStatus) {
wsStatus.innerText = text;
wsStatus.className = css;
}
},
};
console.log("wsManager initialized globally");
}
// Global function for navbar button
function connectWebSocket() {
window.wsManager.toggleConnection();
}
*/'] |