You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
constchoice_per_vote=1;//this constant can be changed
19
+
constbutton_address=""//wallet address of sender
20
+
constbutton_mnemonic=""//25 word pattern of address of sender
21
+
constred_address=""//address of candidate 1
22
+
constblue_address=""//address of candidate 2
23
+
constescrow_key=algosdk.mnemonicToSecretKey(button_mnemonic)['sk'];//generating the secret key from the 25 word pattern
24
+
app.use(express.static(__dirname+"/public"))//setting the public directory you want to use
25
+
26
+
app.get("/",function(req,res){
27
+
res.render("vote.ejs")
28
+
})
29
+
30
+
31
+
//the socet io module for real time communication between backend and frontend
32
+
io.on("connection",(socket)=>{
33
+
console.log("Websocket connected")
34
+
//waits for vote event from front end to occur and receives data from this event
35
+
socket.on('vote',async(data)=>{
36
+
letenc=newTextEncoder()
37
+
constnote=enc.encode("Voting using Choice Coin")
38
+
//red address maps to 0 and blue address maps to 1 in this data sent from frontend
39
+
constvoter_input=data.voted_for
40
+
console.log(voter_input)
41
+
//if else statements that sends to selected addresses
42
+
if(Number(voter_input)==0){
43
+
constparams=awaitalgoClient.getTransactionParams().do();//getting the parameter objects for the transaction
44
+
lettxn=algosdk.makeAssetTransferTxnWithSuggestedParams(button_address,red_address,undefined,undefined,choice_per_vote,note,CHOICE_ASSET_ID,params)//Sending the specified asset to the address zero
45
+
letsignedtxn=txn.signTxn(escrow_key)//authenticatiing the transaction with the secret key
46
+
awaitalgoClient.sendRawTransaction(signedtxn).do()//sending the signed transaction to the algorand network for confirmation
47
+
// Wait for confirmation
48
+
lettxId=txn.txID().toString();
49
+
letconfirmedTxn=awaitwaitForConfirmation(algoClient,txId,4);//awaiting results from the blockchain
50
+
socket.emit("voted",`Voted for red with txID ${txId}`)
51
+
console.log("Signed transaction with txID: %s",txId);
52
+
}elseif(Number(voter_input)==1){
53
+
constparams=awaitalgoClient.getTransactionParams().do();//getting the parameter objects for the transaction
54
+
lettxn=algosdk.makeAssetTransferTxnWithSuggestedParams(button_address,blue_address,undefined,undefined,choice_per_vote,note,CHOICE_ASSET_ID,params)//Sending the specified asset to the address zero
55
+
letsignedtxn=txn.signTxn(escrow_key)//authenticatiing the transaction with the secret key
56
+
awaitalgoClient.sendRawTransaction(signedtxn).do()//sending the signed transaction to the algorand network for confirmation
57
+
// Wait for confirmation
58
+
lettxId=txn.txID().toString();
59
+
letconfirmedTxn=awaitwaitForConfirmation(algoClient,txId,4);//awaiting results from the blockchain
60
+
socket.emit("voted",`Voted for blue with txID ${txId}`)
61
+
console.log("Signed transaction with txID: %s",txId);
62
+
}
63
+
})
64
+
})
65
+
66
+
67
+
//Function to await confirmation results from blockchain
0 commit comments