Algorand developer: Migrate to v2 endpoint plus beginners’ guide.

This post will walk you though how to upgrade to the latest version of Algod client as an existing Algorand developer if you have deployed an application using the algod v1Client as well, it is ideal for quick set up if you are new to the system. Before now, Algorand developers operate using the algod client API – v1 contained in the documentation found here. Later in June this year(2020), Algorand released a new version of the client – v2Client owing to the new improvements. Therefore, existing developers are strongly advised to upgrade their applications to using the v2Client API. 

Alongside the algod v2Client, the indexer was introduced thus as a faucet to enables developers zip through data easily on the Algorand Blockchain. Although it wasn’t available in the first version of the Algod client, but it marked a great development in the latest version. However, the changes in v2 does not in any way affect the kmd – Key Management Daemon. The Indexer was developed though part of the chain, but as a standalone, distinct from the main protocol without compromising the scalability purpose of the whole system. Read more about the Indexer. Upgrading requires that you update your preferred SDK to be compatible with the recent version of the client which is contained as the parent class in the SDK – ALGODSK. This tutorial and solution will focus on two of the four development/programming languages available to Algorand developers – Python and Javascript.

Note: I am using DELL computer, window 10 Pro, hence some of the commands used in the documentation might give undesired result. But if you run window OS and follow this guide, it should be sufficient for your need.

Python developers

For existing python developers, type the following code in the CLI: (If you don’t already have the algorand python sdk installed, run the same command. It should install the latest algosdk on your machine.

pip install py-algorand-sdk
64a1c06799863f8d04df881b2795624e630c6156580781398e7caa6507bb5728.png

I already have it installed on my computer, so it showed “Requirement already satisfied” . In your case, it should return a success message.

ecc52bb613d3bdb20d2ff9ccab7d7063b57b519a90591e7af32b4c90e2ebb170.png

To confirm if algosdk is installed on your machine, run tthe following command.

pip3 list "py-algorand-sdk"
030e46d0555c73ec3a75f56815e9cb3e919b4adb780cc31a0b5d79e21a8a9125.png

From the image, I ran same code contained in the reference documentationand I it says ‘grep’ is not recognized as an internal or external command, operable program or batch file. This is a type of error you’re likely to encounter in the process. Now run pip3 list “py-algorand-sdk” . It returns a list of python-related sdks of which you should see py-algorand-sdk. New developers should ensure you have python and pip already installed before attempting to install the algosdk.

Note: In  the earlier version of the py-algorand-sdk, the changes had re-positioned both the algod v1client and kmd to be accessible in version 1.2.1 except for the Indexer, v2Client and kmd are supported by version 1.3.0 .

Using the library

Using version 1.3.0 requires a slight change in the way the library is imported. 

v1
#importing algosdk in previous version

from algosdk import algod

#conneecting to the client requires two parameters if you're running a node or using
#the sandbox or three parameters if you are using a third party service.

#using second option
algod_token = 'abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd'
algod_address = '<rest-api-here(visit https://purestake.com)>'
headers = {X-API-Key: algod_token}

connect_algod_client = algod.AlgodClient(algod_token, algod_address, headers)

Remember the Indexer wasn’t implemented in v1

v2
#importing algosdk and Indexer in version 1.3.0

from algosdk.v2Client import algos
from algosdk.v2Client import indexer

#conneecting to the client requires two parameters if you're running a node or using
#the sandbox or three parameters if you are using a third party service.

#using second option
algod_token = 'abcdefabcdefabcdefabcdefabcdefabcdefabcdefabcd'
algod_address = '<rest-api-here(visit https://purestake.com)>'
headers = {X-API-Key: algod_token}

connect_algod_client = algod.AlgodClient(algod_token, algod_address, headers)

indexer_client = indexer.IndexerClient(indexer_token, indexer_address)

JavaScrip developers

From the CLI, run the following command. (New to the environment? run the same command to install JS-algorand-sdk.

npm install algosdk
39e9d9142504b5fed58e399296c915451d549f67584fcc1a56e24e76c3475ccd.png

After successful installation, a success message as seen below should pop up.

2fd3ea13a71111f36eabe308c62f6ab83cec7fd0a1955a5c2513a00af728b73c.png

Check that the installation is successful. 

npm list algosdk

If successfully installed, the terminal will display this.

f0bb4082b5b2bd79d38666839a5fd9738b53b8666b8af45aa98163f3ae17a4a8.png

using the library

//Importing the algosdk in v2 remains same as in previous version.
const algosdk = require('algosdk');

//Specify the token, server amd port to establish connecttion
const algodToken = 'abcdeffedcbaabcdeffedcbaabcdeffedcbaabcdef';
const algodServer = "http://localhost";
const algodPort = 4001;

//Create an instance of the Algod client
let algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);

//create an instance of the indexer.
let indexer_Client = new algosdk.Indexer(indexerToken, indexerServer, indexerPort); // new

If you use a third party API such as Purestake, you may need to log on to the user dashboard to get hold of the newly released API. Most time, your token remain unchanged. Ensure you are making use of the right URL.

If you are a beginner, to run the above javascript code, follow this tutorial which also make use the Visual Studio Code. At a point you will need to set up and update the configuration file, you may experience a slight difficulty getting it right. An alternative option that might be of help is to use the following configuration setting provided you have node.js installed.

1.From the top left corner, click on the column to see a drop down. Click on ‘Add Configuration’.

949a19117a7befde2114b2aa51321b485b7bc175ebfa7d2062274e13549ba3e0.png

2. From list of menu in the drop-down, select ‘Node.js: Attach’ 

07922f8c838f60efd19fddec264d7991f400af37956a1bf11ec11cf5a6f8cffe.png

3. The launch.json configuration file should look pretty much like this. To run the code, click on the green triangular shape from the left. It opens a terminal and executes the current file you are working with.

4. Some times, it may not execute automatically, what you need to do is type `node <followed by the name of the file you are working with>`.

That should run perfectly if all things being equal.


There are handful of resources, solutions and tutorials available at your use on Algorand developer website.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *