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

Samuel Tosin
CoinsBench
Published in
4 min readJan 20, 2022

--

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.

Requirements

This Tutorial is a guide to build a voting application with NodeJS on Algorand using Choice Coin and Javascript algorand SDK.

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’s 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 variable 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 HERE.
  • 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 = 21364625const 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.

5. Wait For Confirmation To Sync Vote From Algorand Blockchain

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

6. Check $Choice Balance After Voting

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 account

7. Run The Full Code

The index.js would look like.

In Conclusion, We just made a voting application with choice coin and javascript algorand SDK using NodeJS.You can check the full code on Github

--

--