Tutorials
No Results
Tutorial Image

Beginner · 15 minutes or less

Create an Account on TestNet using Go

This tutorial demonstrates the steps involved in creating a basic Standalone Algorand Account using the Go SDK and funding it using the Algorand Testnet Faucet.

Requirements

Background

Many tutorials will need accounts to perform transactions on. Code samples will typically call a restore account function from a mnemonic code. Copy off the account addresses and mnemonics generated from this tutorial for later use. To learn more about Accounts on Algorand visit the Accounts Overview page in the docs.

1. Generate an Algorand keypair

Import go-algorand-sdk crypto and mnemonic libraries.

import (
    "fmt"

    "github.com/algorand/go-algorand-sdk/crypto"
    "github.com/algorand/go-algorand-sdk/mnemonic"
)

Standalone Account

Use the GenerateAccount() method to generate the keypairs.

    account1 := crypto.GenerateAccount()
    account2 := crypto.GenerateAccount()
    account3 := crypto.GenerateAccount()

2. Retrieve the private key mnemonic

Use the mnemonic.FromPrivateKey method to get the mnemonic phrase from the newly generated account. This will subsequently provide the ability to restore an account from a mnemonic phrase.

Standlone Account Derivation

    address1 := account1.Address.String()
    address2 := account2.Address.String()
    address3 := account3.Address.String()

    mnemonic1, err := mnemonic.FromPrivateKey(account1.PrivateKey)
    if err != nil {
        return
    }
    mnemonic2, err := mnemonic.FromPrivateKey(account2.PrivateKey)
    if err != nil {
        return
    }
    mnemonic3, err := mnemonic.FromPrivateKey(account3.PrivateKey)
    if err != nil {
        return
    }
    fmt.Printf("1 : \"%s\"\n", address1)
    fmt.Printf("2 : \"%s\"\n", address2)
    fmt.Printf("3 : \"%s\"\n", address3)
    fmt.Printf("")
    fmt.Printf("Copy off accounts above and add TestNet Algo funds using the TestNet Dispenser at https://bank.testnet.algorand.network/\n")
    fmt.Printf("Copy off the following mnemonic code for use in subsequent tutorials\n")
    fmt.Printf("\n")
    fmt.Printf("mnemonic1 := \"%s\"\n", mnemonic1)
    fmt.Printf("mnemonic2 := \"%s\"\n", mnemonic2)
    fmt.Printf("mnemonic3 := \"%s\"\n", mnemonic3)

3. Send Algos to the new account

Your output from the above step should look similar to this:

1 : "5SUWZX3DT2DFCP7Z4N67TL3GUY6M6XR6IMHSLR4IVLYYLRIPVONTNRNC5U"
2 : "HDYCZJ6GUP3N3K6TZDU4LPMIQ4KLSZ62S2QAAAO6S5VP6KQNOPYBUQPOYQ"
3 : "C4DZBRTW745777ZK7QLV7ASQG3CRSAWFHG7OFIO7PWJ5UILL7IBJV5XDV4"
Copy off accounts above and add TestNet Algo funds using the TestNet Dispenser at https://bank.testnet.algorand.network/
Copy off the following mnemonic code for use in subsequent tutorials.

mnemonic1 := "order shadow purity struggle return husband neglect actress globe robot noble fatigue picnic curtain little table wheel among inject clean satisfy ecology defy able century"
mnemonic2 := "hair layer army devote alcohol walk repair arctic catalog warrior catch mechanic cost liberty crisp local maze abuse angry group kingdom one ethics abstract soul"
mnemonic3 := "dizzy thing process apple glow pact elephant future mask pizza install behave pottery poem casual yard west oblige skull shaft horror young seven absorb tide"

Let’s send Algos to the new account by visiting the TestNet faucet.

Enter the Algorand Public Address in the textbox, complete the recaptcha, and click “Dispense”. If you receive a 200 status code, your account should now be funded with 100 Algos.

4. Check your balance

You can check your balance with any block explorer connected to TestNet.

You can also connect to a node through the Go SDK client. Make sure to first retrieve an IP address and access token through one of the methods described in the Workspace Setup. Also make sure to replace the placeholder values with your own token, server and port values.

Once you have an address and token. Instantiate a client and call the AccountInformation method to check the account’s balance.

    const algodAddress = "http://localhost:4001"
    const algodToken = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"

    var txHeaders = append([]*algod.Header{}, &algod.Header{"Content-Type", "application/json"})

    // Initialize an algodClient
    algodClient, err := algod.MakeClient(algodAddress, algodToken)
    if err != nil {
        return
    }
    act, err := algodClient.AccountInformation(account1.Address.String(), txHeaders...)
    if err != nil {
        fmt.Printf("failed to get account information: %s\n", err)
        return
    }
    fmt.Print("Account 1: ", act)

5. Optional - Import account into mobile wallet

The mnemonic you just generated is compatible with Algorand’s mobile wallet. Tap the “Recover from passphrase” button to import this account into your mobile wallet. Your balance should now update.

Note: Any account on TestNet exists in MainNet, and vice versa. However, the same account on each will have different assets and funds, etc. Currently, transacting with TestNet accounts using Algorand Mobile Wallet is not available.

6. Completed Code

Here is the completed code to create 3 accounts. If debugging this code, you can set a breakpoint on the initialize an algodClient statement and fund the accounts before continuing.

Note: If you see an amount of 0 from the account_information call, repeat step 3 to fund each account.

package main

import (
    "fmt"
    json "encoding/json"
    "github.com/algorand/go-algorand-sdk/crypto"
    "github.com/algorand/go-algorand-sdk/mnemonic"
    "github.com/algorand/go-algorand-sdk/client/algod"
)

const algodAddress = "http://localhost:4001"
const algodToken = "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"
var txHeaders = append([]*algod.Header{}, &algod.Header{"Content-Type", "application/json"})

// PrettyPrint prints Go structs
func PrettyPrint(data interface{}) {
    var p []byte
    //    var err := error
    p, err := json.MarshalIndent(data, "", "\t")
    if err != nil {
        fmt.Println(err)
        return
    }
    fmt.Printf("%s \n", p)
}

func main() {
    account1 := crypto.GenerateAccount()
    account2 := crypto.GenerateAccount()
    account3 := crypto.GenerateAccount()
    address1 := account1.Address.String()
    address2 := account2.Address.String()
    address3 := account3.Address.String()

    mnemonic1, err := mnemonic.FromPrivateKey(account1.PrivateKey)
    if err != nil {
        return
    }
    mnemonic2, err := mnemonic.FromPrivateKey(account2.PrivateKey)
    if err != nil {
        return
    }
    mnemonic3, err := mnemonic.FromPrivateKey(account3.PrivateKey)
    if err != nil {
        return
    }
    fmt.Printf("1 : \"%s\"\n", address1)
    fmt.Printf("2 : \"%s\"\n", address2)
    fmt.Printf("3 : \"%s\"\n", address3)
    fmt.Printf("")
    fmt.Printf("Copy off accounts above and add TestNet Algo funds using the TestNet Dispenser at https://bank.testnet.algorand.network/\n")
    fmt.Printf("Copy off the following mnemonic code for future tutorial use\n")
    fmt.Printf("\n")
    fmt.Printf("mnemonic1 := \"%s\"\n", mnemonic1)
    fmt.Printf("mnemonic2 := \"%s\"\n", mnemonic2)
    fmt.Printf("mnemonic3 := \"%s\"\n", mnemonic3)

    // Initialize an algodClient
    algodClient, err := algod.MakeClient(algodAddress, algodToken)
    if err != nil {
        return
    }
    act, err := algodClient.AccountInformation(account1.Address.String(), txHeaders...)
    if err != nil {
        fmt.Printf("failed to get account information: %s\n", err)
        return
    }
    fmt.Print("Account 1: ")
    PrettyPrint(act)
    act, err = algodClient.AccountInformation(account2.Address.String(), txHeaders...)
    if err != nil {
        fmt.Printf("failed to get account information: %s\n", err)
        return
    }   
    fmt.Print("Account 2: ")
    PrettyPrint(act)
    act, err = algodClient.AccountInformation(account3.Address.String(), txHeaders...)
    if err != nil {
        fmt.Printf("failed to get account information: %s\n", err)
        return
    }
    fmt.Print("Account 3: ")
    PrettyPrint(act)
}

Your terminal output should look similar to this…

1 : "6RYS2DWSSAYVW5PSOB7WO35ETWVVP4BYKK2NLW4KHGUTHIA63AG3Z7GZIM"
2 : "TVIGQH4QAUQR6FBYFLKVPYUSAVYAKWF7TUL5FM3XVX47755ZHNRMCKISRI"
3 : "R6VFJ6E4XXSE5VX4FBMREU67AQETJHKH34OV3HXL4VBIXSUX4UHEHA65QQ"
Copy off accounts above and add TestNet Algo funds using the TestNet Dispenser at https://bank.testnet.algorand.network/
Copy off the following mnemonic code for future tutorial use

mnemonic1 := "scale damage enter shoot enrich traffic sunny evidence recycle budget hello sorry abstract acoustic foot enlist awful blame arch illness cash victory divide absorb ozone"
mnemonic2 := "culture person industry summer asthma wood police truth design dinner virtual unfold dismiss flavor gospel emerge artwork theory group slide welcome immense direct abstract helmet"
mnemonic3 := "method trumpet novel turkey super chat orchard resist unit evidence grow drift faint apology position easily frog armor cover anger list angle arrange absent project"
Account 1: {
    "round": 5985502,
    "address": "6RYS2DWSSAYVW5PSOB7WO35ETWVVP4BYKK2NLW4KHGUTHIA63AG3Z7GZIM",
    "amount": 100000000,
    "pendingrewards": 0,
    "amountwithoutpendingrewards": 100000000,
    "rewards": 0,
    "status": "Offline"
 } 
 Account 2: {
    "round": 5985502,
    "address": "TVIGQH4QAUQR6FBYFLKVPYUSAVYAKWF7TUL5FM3XVX47755ZHNRMCKISRI",
    "amount": 100000000,
    "pendingrewards": 0,
    "amountwithoutpendingrewards": 100000000,
    "rewards": 0,
    "status": "Offline"
 } 
 Account 3: {
    "round": 5985502,
    "address": "R6VFJ6E4XXSE5VX4FBMREU67AQETJHKH34OV3HXL4VBIXSUX4UHEHA65QQ",
    "amount": 100000000,
    "pendingrewards": 0,
    "amountwithoutpendingrewards": 100000000,
    "rewards": 0,
    "status": "Offline"
 } 

accounts

standalone accounts

testnet

go

v1 api

April 11, 2020