Candy machine NFT observer
by Wuzhong Zhu
HTML
<h1>Candy machine NFT observer</h1>
<p>This is A small piece of code that monitors <a href= "https://solana.com/">Solana</a> network for newly minted NFT.</p>
<div id="controls">
<label>Enter the address of your Solana WebSocket endpoint:</label></br>
<input type="text" id="url" placeholder="wss://xxxxx.com/xxxxx"/></br>
<button type="button" id="openWS" onclick="openConnection();">connect</button></br>
<button type="button" id="startListening" onclick="init();">start</button></br>
<button type="button" id="closeWS" onclick="closeConnection();">disconnect</button></br>
</div>
<div id="info">
<p>
<strong>Status: </strong>
<span id="connectionstatus">Disconnected</span>
</p>
</div>
JavaScript
var socket
var websocketurl
async function openConnection() {
try{
if (!window.WebSocket) {
//Check if browser support WS
lert("Your browser does not support the WebSocket API!");
}
else {
websocketurl = document.getElementById("url").value;
var connstatus = document.getElementById("connectionstatus");
var infodiv = document.getElementById("info");
connstatus.innerHTML = "Connecting...";
infodiv.innerHTML += "<p>Connecting to: " + websocketurl + "</p>";
//Initialize websocket
socket = new WebSocket(websocketurl);
socket.onopen = function() {
connstatus.innerHTML = "Connected!";
infodiv.innerHTML += "<p>Connected to: " + websocketurl + "</p>";
};
socket.onclose = function() {
connstatus.innerHTML = "Disconnected!";
infodiv.innerHTML += "<p>Disconnected from: " + websocketurl + "</p>";
};
//message received
socket.onmessage = function(message) {
try{
data = JSON.parse(message.data);
infodiv.innerHTML += "<p>New token: <a href ='https://explorer.solana.com/tx/" + data.params.result.value.signature + "' target='_blank'>"+ data.params.result.value.signature+"</a></p>";
console.log(data)
console.log(data.params.result.value.signature)
}catch(error){
console.log(error)
}
};
socket.onerror = function(error) {
console.log(error)
};
}
}catch(error){
console.log(error)
}
}
async function init() {
if (socket) {
var message = JSON.stringify({
"jsonrpc": "2.0",
"id": 1,
"method": "logsSubscribe",
"params": [
{
"mentions": [...