Getting Started with Algorand

To set up your own node, visit this GitHub repository for a previous tutorial series: https://github.com/atsoc1993/Algorand_Discord_Bots_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.

If you are trying to run this code natively on your computer, and not in this browser: Make sure you have GO installed: https://go.dev/dl/

Initialize your project by using the following command in the terminal:
'go mod init algolearn'
Ensure that the name of the file ends with '.go', and that you've installed the go algorand SDK afterwards using the following command in the IDE terminal: go get github.com/algorand/go-algorand-sdk/... You can copy and paste the code below into your yourFileName.go file, and use 'go run yourFileName.go' in the terminal to run it! */
package main
import (
    "context"
    "fmt"
    "os"

    "github.com/algorand/go-algorand-sdk/client/v2/algod"
)

func main() {
    // Variables for Algorand testnet node access
    algodToken := "" // Leave empty for public node service, or enter your node token
    algodServer := "https://testnet-api.algonode.cloud"

    // Create an algod client
    algodClient, err := algod.MakeClient(algodServer, algodToken)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to make algod client: %s\n", err)
        return
    }

    // Fetch the node status and print it
    status, err := algodClient.Status().Do(context.Background())
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to get node status: %s\n", err)
        return
    }

    fmt.Printf("Node status: %+v\n", status)
}

First, we import the necessary Go packages, including context for API call management, fmt for formatted output, and os for system-level operations. We also import algod from the Algorand SDK, which allows us to interact with the Algorand blockchain.

Next, we set up variables for the token and server URL for the Algorand testnet node. These are initialized to work with a public node service, but can be customized for a private node by specifying a token.

We then attempt to create an algodClient using the MakeClient function, passing in our server URL and token. This function returns an algodClient object and an error. If an error occurs during client creation, it is handled immediately by printing an error message and exiting the function.

Using the created algodClient, we proceed to fetch the current node status. This is done by calling the Status method followed by Do, passing in a context to manage the request. If an error occurs, it is caught and printed, similar to the client creation step.

If the status fetch is successful, we print the complete status object to the console to inspect its contents. Additionally, we access and print the LastRound information from the status object, which represents the most recent round of the Algorand consensus protocol.

Print statement formatting:

  • %+v: This format specifier in fmt.Printf is used for printing structs or composite values in Go. The + flag adds field names to the output, making it very useful for debugging by showing both the field names and their values. In your code, it's used to display the complete status of the Algorand node, providing a detailed look at all its properties.
  • %d: This is used to format integers. In your code, it prints the value of lastRound, which is an integer representing the latest round number of the blockchain network. This specifier ensures that the number is printed as a base-10 decimal.

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 Structs, 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!
package main

import (
    "context"
    "fmt"
    "os"

    "github.com/algorand/go-algorand-sdk/client/v2/algod"
)

func main() {
    // Variables for Algorand testnet node access
    algodToken := "" // Leave empty for public node service, or enter your node token
    algodServer := "https://testnet-api.algonode.cloud"

    // Create an algod client
    algodClient, err := algod.MakeClient(algodServer, algodToken)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to make algod client: %s\n", err)
        return
    }

    // Fetch the node status and print it
    status, err := algodClient.Status().Do(context.Background())
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to get node status: %s\n", err)
        return
    }

    // CREATE LAST ROUND VARIABLE AND PRINT THE LAST ROUND HERE

}

Reveal Answer:

package main

import (
    "context"
    "fmt"
    "os"
    "github.com/algorand/go-algorand-sdk/client/v2/algod"
)

func main() {
    // Variables for Algorand testnet node access
    algodToken := "" // Leave empty for public node service, or enter your node token
    algodServer := "https://testnet-api.algonode.cloud"

    // Create an algod client
    algodClient, err := algod.MakeClient(algodServer, algodToken)
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to make algod client: %s\n", err)
        return
    }

    // Fetch the node status and print it
    status, err := algodClient.Status().Do(context.Background())
    if err != nil {
        fmt.Fprintf(os.Stderr, "Failed to get node status: %s\n", err)
        return
    }

    // Access last round status information
    lastRound := status.LastRound
    fmt.Printf("Last round: %d\n", lastRound)
}

Quiz

Question 1

What is the correct way to create an Algorand client in Go?





Question 2

Which package needs to be imported to use the Algorand SDK in Go?





Question 3

How do you fetch the status of an Algorand node in Go?

algodClient, err := algod.MakeClient(algodServer, algodToken)
if err != nil {
    fmt.Fprintf(os.Stderr, "Failed to make algod client: %s
", err)
    return
}




Question 4

How do you access the last round from the status response in Go?

status, err := algodClient.Status().Do(context.Background())
if err != nil {
    fmt.Fprintf(os.Stderr, "Failed to get node status: %s
", err)
    return
}




Code Editor