Create Publication

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Tutorial Thumbnail
Beginner · 30 minutes

Build a decentralized voting app with Choice Coin and Javascript Algorand SDK tutorial Using NodeJs 📨

In this tutorial, you will learn how to build a voting application with Choice Coin and JavaScript Algorand SDK using NodeJs.

Requirements

  • NPM and Node installed, download HERE
  • A Purestake API key: See Tutorial
  • Funded Testnet Accounts: See Tutorial
  • An Integrated Development Environment(IDE) e.g VSCode

Background

This tutorial has been created by Samuel Aspirin and Adetona Fashola.

Choice Coin is an Algorand Standard Asset that powers Decentralized Decisions; a voting and governance software built directly on the Algorand blockchain. Decentralized Decisions enables organizations to make governance decisions in an open and decentralized manner.

Voting Infrastructure

The purpose of Choice Coin is to allow decentralized organizations govern themselves and control digital assets in an equitable fashion. More organizations developing projects in Decentralized Finance (DeFi), Non-Fungible Tokens (NFTs), and blockchain networks need a way to govern. Contrary to centralized systems, which are inherently hierarchical and pyramid like in nature, decentralized systems distribute power and decision making across global networks in a fair fashion. Thus, there is need for a way decentralized organizations can make decisions across distributed ledgers.

Decentralized Decisions is a software designed to meet this need and provide a ready to use decentralized voting application using Choice Coin on the Algorand Network.

The Decentralized Decisions software is open source and available on GitHub.

The main programming language used for Decentralized Decisions development is Python, however the software may be written in other languages too, such as JavaScript.

Steps

1. Set up project folder and install algosdk

Create a new project folder, this can be done in the terminal with :

$ mkdir choice-coin-voting-app

After creating the project folder, enter the directory in your terminal

$ cd choice-coin-voting-app

To install dependencies in the project, you need to start an NPM instances using:

$ npm init -y

Create a new file, I will name mine index.js. This can also be done in the terminal with:

$ touch index.js

In the terminal, npm install both AlgoSDK and Prompt-Sync

$ npm install algosdk prompt-sync

AlgoSDK is the official JavaScript library for communicating with the Algorand network. It is designed for modern browsers and Node.js.
Prompt-Sync module is a function that creates prompting functions, this is the same thing with browsers’ prompt but this works with NodeJS environment.

In index.js file, import both modules.

const algosdk = require('algosdk'); 
const prompt = require('prompt-sync')(); 

2. Configure Purestake API and Create Client

Register for a Purestake developer account and get your API key to interact with Algorand network.

const server = "https://testnet-algorand.api.purestake.io/ps2";
const port = "";
const token = {
  "X-API-Key": "YOUR API KEY", 
};

Create AlgodClient constant to start connection.

const algodClient = new algosdk.Algodv2(token, server, port)

3. Recover Account And Enter Choice Coin Asset ID

  • Create new testnet wallet address from either myAlgoWallet or Algosigner, Copy and save your 25 mnemonic passphrase and do not share with anyone.
  • Fund the wallet address with some testnet ALGOs via the faucet.
  • Opt In to $Choice Coin asset using ID 21364625 using myAlgoWallet or Algosigner
  • Swap the testnet Algos to $choice on Tinyman.

In index.js

const mnemonic = "The mmemonic 25 characters seperated by a whitespace should be imported here"; 

const recoveredAccount = algosdk.mnemonicToSecretKey(mnemonic); 

const ASSET_ID = 21364625

const voting_address = "" 

In the constant voting_address input a newly created voting address wallet different from the one recovered that $choice voting amount can be sent to, make sure $choice is opt-in to receive votes just like earlier.

4. Choosing Voting Option and Sending Vote

Create a voting function and send the voted amount from the recoveredAccount to the voting_address wallet depending on the candidate option being voted.

const chooseVotingOption = async () => {
    const candidateOption = prompt("Press 0 for candidate Zero or Press 1 for candidate One:") 

     const amount = prompt("Please enter Amount to commit to voting:");


    const params =  await algodClient.getTransactionParams().do()
    const encoder = new TextEncoder()

     if (!(candidateOption)) {
         console.log('Please select a valid candidate option');
     } else if (!Number(amount)) {
         console.log("Please Enter A valid Choice token amount to vote")
     }
      else  if (candidateOption == "0") {
            try {
                let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(
                    recoveredAccount.addr,
                    voting_address,
                    undefined,
                    undefined,
                    Number(amount),
                    encoder.encode("Voting with Choice coin"),
                    ASSET_ID,
                    params

                )

        let signedTxn = txn.signTxn(recoveredAccount.sk);
        const response =  await algodClient.sendRawTransaction(signedTxn).do();
            if(response) {
              console.log(`You just voted for candidate Zero,Your voting ID: ${response.txId}`);

                waitForConfirmation(algodClient, response.txId);
            } else {
                console.log('error voting for candidate Zero, try again later')
            }

        }
        catch(error) {
            console.log("error voting for candidate Zero, Try again later");
        }

 } 


     else  if(candidateOption == "1"){
        try {
            let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(
                recoveredAccount.addr,
                voting_address,
                undefined,
                undefined,
                Number(amount),
                encoder.encode("Voting with Choice coin"),
                ASSET_ID,
                params
            )
       let signedTxn = txn.signTxn(recoveredAccount.sk);
       const response =  await algodClient.sendRawTransaction(signedTxn).do();
            if(response) {
               console.log(`You just voted for candidate One,Your voting ID: ${response.txId}`);

                waitForConfirmation(algodClient, response.txId);
            } else {
               console.log('error voting for candidate one, try again later')
            }

        }
        catch(error) {
            console.log("Error voting for candidate One, Try again later");
        }

        }
        }

5. Wait For Confirmation To Sync Vote From Algorand Blockchain

const waitForConfirmation = async function (algodClient, txId) {
    let lastround = (await algodClient.status().do())['last-round'];
     while (true) {
        const pendingInfo = await algodClient.pendingTransactionInformation(txId).do();
        if (pendingInfo['confirmed-round'] !== null && pendingInfo['confirmed-round'] > 0) {
          //Got the completed Transaction
          console.log('Voting confirmed in round ' + pendingInfo['confirmed-round']);
          break;
        }
        lastround++;
        await algodClient.statusAfterBlock(lastround).do();
     }
 };

This function verify when the vote is being confirmed from algorand network.

6. Check $Choice Balance After Voting

const checkBalance = async () => {

    // get the account information
    const accountInfo =  await algodClient.accountInformation(recoveredAccount.addr).do();
    const assets =  accountInfo["assets"];

    // get choice amount from assets
    assets.map(asset => {
        if (asset['asset-id'] === ASSET_ID) {
            const amount = asset["amount"];
            const choiceAmount = amount / 100;
            console.log(
                `Account ${recoveredAccount.addr} has ${choiceAmount} $choice`
              );
              return;
        }  else {
            console.log(`Account ${recoveredAccount.addr} must opt in to Choice Coin Asset ID ${ASSET_ID}`);
          }
     })

  };

Check $Choice balance after voting has ended, if There is no choice asset ID in the account information. the function is stopped using return and a console message is shown to add choice asset ID to the wallet address

7. Run The Full Code

The index.js would look like.

const algosdk = require('algosdk'); 
const prompt = require('prompt-sync')();  

// open a Purestake API and get a unique API KEY
const server = "https://testnet-algorand.api.purestake.io/ps2";
const port = "";
const token = {
  "X-API-Key": "your API key", 
};
const algodClient = new algosdk.Algodv2(token, server, port); 

// create a testnet account with myalgowallet, keep the mmemonic key
const mnemonic = "The mmemonic 25 characters seperated by a whitespace should be imported here";

// get account from mmemonic key
const recoveredAccount = algosdk.mnemonicToSecretKey(mnemonic); 

// choice coin asset ID
const ASSET_ID = 21364625

const voting_address = "input a voting address wallet you can send choice to, make sure choice is opt-in to receive votes"

// Input 1 to vote for candidate one and 0 to vote for candidate Zero

const chooseVotingOption = async () => {
    const candidateOption = prompt("Press 0 for candidate Zero or Press 1 for candidate One:") 
     const amount = prompt("Please enter Amount to commit to voting:");


    const params =  await algodClient.getTransactionParams().do(); 
    const encoder = new TextEncoder();  

    // if there is no valid option
    if (!(candidateOption)) {
        console.log('Please select a valid candidate option');
    } else if (!Number(amount)) {
        console.log("Please Enter A valid Choice token amount to vote")
    }
    else  if (candidateOption == "0") {
            try {
                let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(
                    recoveredAccount.addr,
                    voting_address,
                    undefined,
                    undefined,
                    Number(amount),
                    encoder.encode("Voting with Choice coin"),
                    ASSET_ID,
                    params

                )

            let signedTxn = txn.signTxn(recoveredAccount.sk);
            const response =  await algodClient.sendRawTransaction(signedTxn).do();
            if(response) {
                console.log(`You just voted for candidate Zero,Your voting ID: ${response.txId}`);
                <!-- wait for confirmation-->
                waitForConfirmation(algodClient, response.txId);
            } else {
                console.log('error voting for candidate Zero, try again later')
            }

        } catch(error) {
            console.log("error voting for candidate Zero, Try again later");
        }

 } else  if(candidateOption == "1"){
    try {
        let txn = algosdk.makeAssetTransferTxnWithSuggestedParams(
            recoveredAccount.addr,
            voting_address,
            undefined,
            undefined,
            Number(amount),
            encoder.encode("Voting with Choice coin"),
            ASSET_ID,
            params
        )
        let signedTxn = txn.signTxn(recoveredAccount.sk);
        const response =  await algodClient.sendRawTransaction(signedTxn).do();
        if(response) {
            console.log(`You just voted for candidate One,Your voting ID: ${response.txId}`);
            <!-- wait for confirmation-->
            waitForConfirmation(algodClient, response.txId);
        } else {
            console.log('error voting for candidate one, try again later')
        }

    } catch(error) {
        console.log("Error voting for candidate One, Try again later");
    }
    }
}

chooseVotingOption();

// verification function
const waitForConfirmation = async function (algodClient, txId) {
    let lastround = (await algodClient.status().do())['last-round'];
     while (true) {
        const pendingInfo = await algodClient.pendingTransactionInformation(txId).do();
        if (pendingInfo['confirmed-round'] !== null && pendingInfo['confirmed-round'] > 0) {
          console.log('Voting confirmed in round ' + pendingInfo['confirmed-round']);
          break;
        }
        lastround++;
        await algodClient.statusAfterBlock(lastround).do();
     }
 };


// check account balance
const checkBalance = async () => {
    const accountInfo =  await algodClient.accountInformation(recoveredAccount.addr).do();
    const assets =  accountInfo["assets"];

    // get choice amount from assets
    assets.map(asset => {
        if (asset['asset-id'] === ASSET_ID) {
            const amount = asset["amount"];
            const choiceAmount = amount / 100;
            console.log(
                `Account ${recoveredAccount.addr} has ${choiceAmount} $choice`
              );
              return;
        }  else {
            console.log(`Account ${recoveredAccount.addr} must opt in to Choice Coin Asset ID ${ASSET_ID}`);
         }
    })
};

checkBalance();

Do determine the winner, you can check the output of the checkBalance function which prints the number of $choice each account has. This number represents the total number of votes for each option. If you want, you can write a quick function to determine who received more votes.

In conclusion, We made a voting application with Choice Coin and Javascript Algorand SDK using NodeJS.You can check the full code on GitHub

(Image source: Unsplash @Arnaud Jaegers)