Skip to content

Commit 163c09d

Browse files
authored
Merge pull request #934 from Jaybee020/Jaybee-Bronze-Submission
Jaybee bronze bonus
2 parents 9492d2a + 02906ae commit 163c09d

File tree

4 files changed

+1207
-0
lines changed

4 files changed

+1207
-0
lines changed

Bronze_Badges/Jaybee020_Bash/app.js

Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
//importing the neccesary modules
2+
const express=require("express")
3+
const app=express()
4+
const server = require('http').createServer(app);
5+
const { Server } = require("socket.io");
6+
const io = new Server(server);
7+
8+
const algosdk=require('algosdk');
9+
10+
//This helps in connecting the client with the algorand network
11+
const algoServer='https://testnet-algorand.api.purestake.io/ps2'
12+
const algoPort='';
13+
const token = {
14+
'X-API-Key': 'Xy8NsXxfJg2cQ2YQ4pax6aLrTcj55jZ9mbsNCM30 '
15+
}
16+
let algoClient = new algosdk.Algodv2(token, algoServer, algoPort);
17+
const CHOICE_ASSET_ID = 21364625;
18+
const choice_per_vote=1;//this constant can be changed
19+
const button_address=""//wallet address of sender
20+
const button_mnemonic=""//25 word pattern of address of sender
21+
const red_address=""//address of candidate 1
22+
const blue_address=""//address of candidate 2
23+
const escrow_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+
let enc=new TextEncoder()
37+
const note=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+
const voter_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+
const params=await algoClient.getTransactionParams().do();//getting the parameter objects for the transaction
44+
let txn=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+
let signedtxn=txn.signTxn(escrow_key)//authenticatiing the transaction with the secret key
46+
await algoClient.sendRawTransaction(signedtxn).do()//sending the signed transaction to the algorand network for confirmation
47+
// Wait for confirmation
48+
let txId = txn.txID().toString();
49+
let confirmedTxn = await waitForConfirmation(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+
}else if (Number(voter_input)==1){
53+
const params=await algoClient.getTransactionParams().do();//getting the parameter objects for the transaction
54+
let txn=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+
let signedtxn=txn.signTxn(escrow_key)//authenticatiing the transaction with the secret key
56+
await algoClient.sendRawTransaction(signedtxn).do()//sending the signed transaction to the algorand network for confirmation
57+
// Wait for confirmation
58+
let txId = txn.txID().toString();
59+
let confirmedTxn = await waitForConfirmation(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
68+
const waitForConfirmation = async function (algodalgoClient, txId, timeout) {
69+
if (algodalgoClient == null || txId == null || timeout < 0) {
70+
throw new Error("Bad arguments");
71+
}
72+
73+
const status = (await algodalgoClient.status().do());
74+
if (status === undefined) {
75+
throw new Error("Unable to get node status");
76+
}
77+
78+
const startround = status["last-round"] + 1;
79+
let currentround = startround;
80+
81+
while (currentround < (startround + timeout)) {
82+
const pendingInfo = await algodalgoClient.pendingTransactionInformation(txId).do();
83+
if (pendingInfo !== undefined) {
84+
if (pendingInfo["confirmed-round"] !== null && pendingInfo["confirmed-round"] > 0) {
85+
//Got the completed Transaction
86+
return pendingInfo;
87+
} else {
88+
if (pendingInfo["pool-error"] != null && pendingInfo["pool-error"].length > 0) {
89+
// If there was a pool error, then the transaction has been rejected!
90+
throw new Error("Transaction " + txId + " rejected - pool error: " + pendingInfo["pool-error"]);
91+
}
92+
}
93+
}
94+
await algodalgoClient.statusAfterBlock(currentround).do();
95+
currentround++;
96+
}
97+
98+
throw new Error("Transaction " + txId + " not confirmed after " + timeout + " rounds!");
99+
}
100+
101+
server.listen("8000",function(){
102+
console.log("Server now listening on port 8000")
103+
})

0 commit comments

Comments
 (0)