Demonstration of Algorand with JavaScript SDK

Introduction

This is my second attempt on Algorand. In my previous work I was using the CLI tool goal to access a private network setup. In this one I write scripts in JavaScript, send transactions to the Algorand TestNet. We will walk through all the steps, from preparing the environment, account creation, funding accounts with faucet, and Algos transfer between accounts.

Source

Reference is made from some articles.

  • Algorand Tutorial: Create an Account on TestNet using JavaScript (link)
  • PureStake Developer API Services (link)

Demonstration

Our demonstration is done in the following tasks

  1. Create accounts
  2. Obtain test Algos from TestNet Faucet
  3. Access Algorand TestNet and check balance
  4. Sends Algos to another account

Step 1: Prepare Environment

It is assumed that nodejs and npm are installed properly in our environment.

Image for post
Image for post
Image for post
Image for post
npm install algosdk -s

Step 2: Create Accounts

Algorand is also an account-based blockchain platform. The root of an account is a key-pair (public key and private key), generated from a random seed. Each key is a 32-byte array.

Image for post
Image for post
Image for post
Image for post

Step 3: Obtain Test Algos from TestNet Faucet

Let’s do some explanation first. Algorand runs a Mainnet, where real Algos are used for transaction. Like other blockchain platforms it also provides a TestNet where we can test whatever we like with test Algos. Test Algos are available from a TestNet Faucet, and in this demonstration we will obtain some.

TestNet Faucet

What we need is to access the faucet (https://bank.testnet.algorand.network/) and paste our account address there. Each time the faucet sends out 100 Algos to the account.

Image for post
Image for post
Image for post
Image for post

Check Balance with Algorand Explorer

For sure we can use our environment to check the balance. As of now let’s first check the balance of both accounts using Algorand explorers. There are several Algorand explorers, and here we use goalseeker. Don’t forget to change it to TestNet before checking accounts.

Image for post
Image for post
Image for post
Image for post

Step 4: Access Algorand TestNet and Check Balance

Get access to Algorand TestNet through API Service

Unless you run an Algorand node joining the TestNet, the only way you access the Algorand TestNet is through a third party service. Here are the API services available, and we are using PureStake.

Image for post
Image for post

Check Balance

Now we can prepare our script for checking balance. For simplicity we will check both accounts with one script.

  • Keep an empty port.
  • API Key (token): which is defined as an object. Use your own API key obtained from the API service provider.
Image for post
Image for post
Image for post
Image for post

Step 5: Send Algos to Another Account

Algorand follows the same pattern as other blockchain platforms. The transfer of native currency Algos is done through a transaction signed by the sender and sent to the Algorand TestNet. After the transaction is included in a block the transaction is considered processed.

Script Walk-through

(a) Prepare signing key for sender (line 10–11)

Image for post
Image for post
  • fee: unit fee (microAlgos per byte) for this transaction, with minimum 1000 microAlgos.
  • amount: amount in microAlgos the sender is sending
  • firstRound, lastRound, genesisID and genesisHash: all obtained from (b).
  • note: any arbitrary information for this transaction and we leave it empty here.
Image for post
Image for post

Check Balance

We use the script in Step 4 to check the balance of both accounts.

Image for post
Image for post

Observe Both Accounts in Algorand Explorer

We can use Algorand Explorer (Step 3) to inspect the two accounts again.

Image for post
Image for post
Image for post
Image for post

Summary

We have demonstrated how to use JavaScript SDK to interact with Algorand TestNet. We simply show how accounts are created and Algos are transferred between accounts. With this pattern we can do some more complicated transactions such as creation of tokens (Algorand Standard Asset, ASA). I hope this is good enough for those who are new to this platform and ready to build something interesting in Algorand.

Written by

Happy to share what I learn on blockchain. Visit http://www.ledgertech.biz/kcarticles.html for my works. or reach me on https://www.linkedin.com/in/ktam1/.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store