-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathexplorer.js
More file actions
82 lines (69 loc) · 2.8 KB
/
explorer.js
File metadata and controls
82 lines (69 loc) · 2.8 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
import { ethers } from "https://cdn.jsdelivr.net/npm/ethers@5.7.2/dist/ethers.esm.min.js";
// ======================
// Load Configuration
// ======================
let config = null;
let contractABI = null;
async function loadConfig() {
const configResponse = await fetch("public_config.json");
config = await configResponse.json();
const abiResponse = await fetch("contract_abi.json");
contractABI = await abiResponse.json();
}
// ======================
// Global Variables
// ======================
let provider = null;
let contract = null;
const tableBody = document.getElementById("predictions-table").getElementsByTagName("tbody")[0];
// ======================
// Load Predictions
// ======================
async function loadPredictions() {
tableBody.innerHTML = "";
try {
const countBN = await contract.predictionCount();
const total = countBN.toNumber();
const latest = 10;
const start = total > latest ? total - latest : 0;
for (let i = total - 1; i >= start; i--) {
const prediction = await contract.predictions(i);
const id = i;
const ciphertext = prediction[1];
// Shorten the ciphertext preview to 20 characters
const shortCiphertext = ciphertext.length > 20 ? ciphertext.substring(0, 20) + "..." : ciphertext;
const revealTime = prediction[2].toNumber();
const revealedText = prediction[3];
const isRevealed = prediction[4];
const status = isRevealed ? "Revealed" : "Pending";
const plaintext = isRevealed ? revealedText : "Not revealed";
const revealDate = new Date(revealTime * 1000).toLocaleString();
const row = document.createElement("tr");
row.innerHTML = `
<td>${id}</td>
<td>${shortCiphertext}</td>
<td>${plaintext}</td>
<td>${revealDate}</td>
<td>${status}</td>
<td><button class="details-btn" onclick="window.location.href='prediction_detail.html?id=${id}'">Details</button></td>
`;
tableBody.appendChild(row);
}
} catch (error) {
console.error("Error loading predictions:", error);
tableBody.innerHTML = "<tr><td colspan='6'>Error loading predictions</td></tr>";
}
}
// ======================
// Initialization
// ======================
document.getElementById("refresh-btn").addEventListener("click", loadPredictions);
(async function initialize() {
await loadConfig();
// Use the RPC URL from config
provider = new ethers.providers.JsonRpcProvider(config.rpc_url);
// Create the contract instance
contract = new ethers.Contract(config.contract_address, contractABI, provider);
// Load predictions
loadPredictions();
})();