Getting Started with Algorand
To set up your own node, visit this GitHub repository for a previous tutorial series:
Scroll down to the section called "Algorand Node Installation"— and make sure to run these commands in an Ubuntu LTS Shell (Download link in bottom resources section). If you chose to run a node, that's wonderful! If not, I will be including sections for those programming without a node.
Now that we have access to an Algorand testnet node, as well as a basic understanding of how to work with different kinds of variables, functions and imports. It's time to get started! This will reaffirm your ability to use functions, methods, and imports— as well as key notation: eg; accessing the name of a customer in a variable named my_dictionary:
const myDictionary = {'customer-name': 'Jerry'};
const customerName = myDictionary['customer-name'];
console.log(customerName);
If you are using your own Algorand testnet node on Windows, you will need to access your algod_token and algod_port from your node's data directory:
- Enter the data directory from the root folder:
cd node/data
- Use the following commands to obtain your node token and port:
cat algod.token
---Logs the token into the terminal, it should look something like:
b94c8e5d7a3f1bbd249e83a1cc5b4ae67d8c2a7e9b5f0c6d8e1a7b4f263859cd
cat algod.net
---Logs the port into the terminal, it should look something like:
127.0.0.1:8080
When programming, you will format this information for variables like so:
const algodToken = 'b94c8e5d7a3f1bbd249e83a1cc5b4ae67d8c2a7e9b5f0c6d8e1a7b4f263859cd';
const algodServer = 'http:127.0.0.1';
const algodPort = 8080;
If you are not using your own Algorand testnet node, you can use these:
(Free service does not require token, and the algodToken variable will be an empty string '', whereas the algodServer will be the algonode testnet cloud link)
const algodToken = '';
const algodServer = 'https://testnet-api.algonode.cloud';
const algodPort = 443;
Note: If you are attempting to run this code outside of the browser in your own IDE, remember to use npm init -y to create a package.json, and add "type": "module" anywhere in your package. Then make sure you install the algorand sdk by using 'npm install algosdk'. Afterwards, you can run the code in your terminal by using node nameOfYourFile.js.
If you get an error along the likes of 'node is not a function', this means you haven't installed node.js, you can install it here:
// Import algosdk
import algosdk from 'algosdk';
// Initialize AlgodClient
const algodToken = ''; // Leave '' for public node service, or enter your node token
const algodServer = 'https://testnet-api.algonode.cloud'; // Use this cloud link, or enter your own host & port
const algodPort = 443;
// Create an instance of the algod client
const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);
// Fetch the node status and print it
algodClient.status().do()
.then(status => {
console.log('Node status:', status);
})
.catch(err => {
console.error('Failed to get node status:', err);
});
First, we import the algosdk library, which includes the AlgodClient necessary for interacting with the Algorand network.
Next, we define variables for our token, server URL, and port number for the node we'll be using. These are set up to handle connections to the Algorand testnet via a public service, but can be adjusted if you are running your own node.
We then create an instance of AlgodClient using the algodToken, algodServer, and algodPort. This instance is assigned to the variable algodClient.
Using the algodClient instance, we access the status method to fetch the current status of the Algorand network. This method returns a promise that resolves to the status information.
Once the promise resolves, the resulting status data is contained within the status variable within the .then() method's callback. Here, we print the entire status object to the console to view its contents.
Try running the code now!
You'll see that you receive a dictionary with several keys and associated values, here is a list of all the keys:
- catchpoint
- catchpoint-acquired-blocks
- catchpoint-processed-accounts
- catchpoint-processed-kvs
- catchpoint-total-accounts
- catchpoint-total-blocks
- catchpoint-total-kvs
- catchpoint-verified-accounts
- catchpoint-verified-kvs
- catchup-time
- last-catchpoint
- last-round
- last-version
- next-version
- next-version-round
- next-version-supported
- stopped-at-unsupported-round
- time-since-last-round
The most popular keys are last-round, so you know which block you are on, and perhaps time-since-last-round, if you should need to know that information. Other keys are primarily used by node running services, like upcoming reti-pool incentives, but otherwise are not especially useful for our purposes.
Referencing the information from Chapter 4, for Objects, try this:
- Create a variable, and name it lastRound
- Assign the value of the key 'last-round' in the status dictionary to this variable you created
- Print your variable!
// Import algosdk
import algosdk from 'algosdk';
// Initialize AlgodClient
const algodToken = ''; // Leave '' for public node service, or enter your node token
const algodServer = 'https://testnet-api.algonode.cloud'; // Use this cloud link, or enter your own host & port
const algodPort = 443;
// Create an instance of the algod client
const algodClient = new algosdk.Algodv2(algodToken, algodServer, algodPort);
// Fetch the node status and print it
algodClient.status().do()
.then(status => {
// Create a variable, and name it lastRound
const lastRound = status['last-round'];
// Print your variable
console.log('Last round:', lastRound);
})
.catch(err => {
console.error('Failed to get node status:', err);
});