Demonstration of Algorand with JavaScript SDK

Introduction

This is my second attempt on Algorand. In my previous work I was using the CLI tool 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.

Note that this demonstration is done with Algod v2 API, released in June this year.

It is assumed that you have basic knowledge of and skill in Node JS. We will provide all the scripts with explanation.

Source

Reference is made from some articles.

  • Algorand Documentation: Your First Transaction (link)
  • 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. Prepare environment
  2. Create accounts
  3. Obtain test Algos from TestNet Faucet
  4. Access Algorand TestNet and check balance
  5. Sends Algos to another account

We are building several js scripts to perform these tasks. And during the demonstration we will highlight certain areas.

Step 1: Prepare Environment

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

Prepare a new directory and initialize a Node environment.

Then load the Algorand SDK for Javascript

npm install algosdk -s

Upon completion, we will see the modules are loaded in . Our environment is ready for use.

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.

The public key is not used directly in real life. Instead, an address is computed from the public key, and the result is a 58-char long string recognizable. This is the address facing the world.

The private key is always represented in mnemonic. Besides a readable representation, it also serves as a backup mechanism. In Algorand, the private key is transformed into a 25-word mnemonic. With this we can always construct back the private key. The mnemonic should be kept secure in real life.

More detailed information about account can be seen in Algorand documentation.

The SDK comes with all the tools for account creation, and is independent of which environment we are working on.

Here is the script we are using to create an account for our demonstration. Do it twice and log down both the address and mnemonic.

The result of the function algosdk.generateAccount() contains two parts: the address (addr) and the secret key (sk). And we use another function algosdk.secretKeyToMnemonic() to convert the secret key to a 25-word mnemonic.

One can generate as many accounts as needed. In our case we do it twice, and write down both the address and mnemonic of both accounts for our demonstration.

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.

Here we paste address of account_1 to obtain 100 Algos.

After we see the Code 200 success message with a transaction ID, the Algos are now sent to account_1.

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.

This is for account_1. We see 100 Algos deposited. We also see the transaction when the faucet sends out the 100 Test Algos (Note the transaction ID).

For comparison, we also check account_2. As expected we see no balance yet.

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.

What you need is to make registration (if not yet). You will get your API key. This API key is needed when you access the TestNet through this API service.

And you keep scrolling down and see the API gateway. We are using this later.

Check Balance

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

Some points to note in this script.

In order to access Algorand TestNet through the API service provider, we need to create a wrapper. You can see it as a client accessing the service specified. As a result it is named client. The information we need is,

  • API Gateway or server: https://testnet-algorand.api.purestake.io/ps2. Note that we are using API v2.
  • Keep an empty port.
  • API Key (token): which is defined as an object. Use your own API key obtained from the API service provider.

With this, client is created through algosdk.Algodv2(). We now can use client to access the Algorand TestNet. The function we call is accountInformation() with a given address. The result is an object (account1_info and account2_info). Inside the object we can obtain the balance from the key amount.

You may see that we only need the address (not the private key or mnemonic) to check balance. In theory the balance of an account is open information and anyone can view the balance of any given address. You may remember that we check the balance using Algorand Explorer which is supposed to be accessed by anyone (Step 3).

Here is the result we get after running this script. We see in account 1 we have 100 Algos (shown in microAlgos, 100 Algos = 100 x 10⁶ microAlgos), and none in account 2. Similar to what we observe in Algorand Explorer (Step 3).

For information, here is the complete result of accountInformation(), and you can locate the amount.

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.

Here is the script we are using, and we will walk through those steps.

Script Walk-through

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

It is logical that one is spending one’s “money” with one’s approval. In the blockchain world, this sending is implemented by a transaction requiring the sender’s signature. As a result we need the sender’s secret key. Secret key is retrieved by providing the mnemonic and using algosdk.mnemonicToSecretKey(). The result is now saved in account1. And we can obtain the secret key in account1.sk later in the signing process (d).

Note we do not need the recipient’s secret key (you never have it). What we need is just the account address of the recipient, which is something made public.

(b) Obtain latest transaction parameters from Algorand TestNet (line 14)

If we take a glimpse on (c), certain information in a transaction relies on some latest information from Algorand TestNet. We are using client.getTransactionParams().do() to read this information and store the result object in params.

Here is a sample output after using this function.

(c) Construct a transaction (line 15–25)

Now we are ready to construct a transaction. As we see from the code, we need to specify those items required in a transaction.

  • from and to: sender and recipient account address (You can use the actual address for sender. Since we have account1 object holding it, we use account1.addr instead.)
  • 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.

You can learn more about transactions here.

Now we have the transaction object. We can move to the signing step by the sender.

(d) Sign the transaction with sender’s secret key (line 26)

Sender signs the transaction with sender’s own secret key. The function to sign a transaction is algosdk.signTransaction() with a transaction the secret key. Note that this signing does not involve any interaction with TestNet, and therefore we do not use client for this process. The result is stored in signedTxn object.

(e) Submit the transaction to Algorand TestNet (line 27)

Now the signed transaction signedTxn is ready for sending as raw transaction to Algorand TestNet. The function to send this transaction is client.sendRawTransaction(). An object is received and in this script we simply show the transaction ID.

Here is the result.

The process is complete. After Algorand TestNet has completed processing this transaction, the balance of both accounts get updated.

Check Balance

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

The transfer is done. We see account_2 now has 10 Algos, while account_1 is 10 Algos less than the fee (1000 microAlgos).

Observe Both Accounts in Algorand Explorer

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

For account_1, we also see the transaction we just made sending 10 Algos, by cross referencing the TxID (3KK…).

And in account_2, we see the balance with the same transaction information.

This completes our demonstration.

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.

Visit http://www.ledgertech.biz/kcarticles.html for all my works. Reach me on https://www.linkedin.com/in/ktam1/ or follow me @kctheservant in Twitter.

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