Go SDK: Your First Transaction
This section is a quick start guide for interacting with the Algorand network using Go. This guide will help to install Algorand sandbox, which provides a node for testing and development. This guide will also help to install the Go SDK, create an account and submit your first transaction on Algorand.
Install Sandbox¶
Info
This step is only required if you are not using AlgoKit. If you are using AlgoKit, you can spin up a sandbox using the LocalNet, see AlgoKit getting started guide for more information.
Prerequisites
- Docker Compose (install guide)
- Git (install guide)
Algorand provides a docker instance for setting up a node, which can be used to get started developing quickly. To install and use this instance, follow these instructions.
git clone https://github.com/algorand/sandbox.git
cd sandbox
./sandbox up dev
This will install and start private network. To read more about Algorand networks see Algorand Networks.
More Information about the sandbox and how to use it.
Install Go SDK¶
Algorand provides an SDK for Go.
Prerequisites
- Go programming language (install guide)
From a terminal window, install the Go SDK:
go get -u github.com/algorand/go-algorand-sdk/v2
The GitHub repository contains additional documentation and examples.
See the JavaScript SDK reference documentation for more information on methods.
The SDK is installed and can now interact with the running Algorand Sandbox environment, as configured above.
Create an account¶
In order to interact with the Algorand blockchain, you must have a funded account on the network. To quickly create a test account use the following code.
account := crypto.GenerateAccount()
mn, err := mnemonic.FromPrivateKey(account.PrivateKey)
if err != nil {
log.Fatalf("failed to generate account: %s", err)
}
log.Printf("Address: %s\n", account.Address)
log.Printf("Mnemonic: %s\n", mn)
Warning
Never share your mnemonic passphrase or private keys. Production environments require stringent private key management. For more information on key management in community Wallets, click here. For the open source Algorand Wallet, click here.
Fund the account¶
Before sending transactions to the Algorand network, the account must be funded to cover the minimal transaction fees that exist on Algorand. In this example, we'll be using prefunded accounts available in the Sandbox. To fund an account on Testnet account use the Algorand faucet.
Info
All Algorand accounts require a minimum balance to be registered in the ledger. To read more about Algorand minimum balance see Account Overview
Connect Your Client¶
An Algod client must be instantiated prior to making calls to the API endpoints. You must provide values for <algod-address>
and <algod-token>
. The CLI tools implement the client natively. By default, the algodToken
for each sandbox is set to its aaa...
value and the algodAddress
corresponds to http://localhost:4001
.
// Create a new algod client, configured to connect to out local sandbox
var algodAddress = "http://localhost:4001"
var algodToken = strings.Repeat("a", 64)
algodClient, _ := algod.MakeClient(
algodAddress,
algodToken,
)
// Or, if necessary, pass alternate headers
var algodHeader common.Header
algodHeader.Key = "X-API-Key"
algodHeader.Value = algodToken
algodClientWithHeaders, _ := algod.MakeClientWithHeaders(
algodAddress,
algodToken,
[]*common.Header{&algodHeader},
)
Info
The example code connects to the sandbox Algod client. If you want to connect to a public API client, change the host, port, and token parameters to match the API service. See some service available here
Info
If you are connecting to the Testnet, a dispenser is available here
Check Your Balance¶
Before moving on to the next step, make sure your account has been funded.
acctInfo, err := algodClient.AccountInformation(acct.Address.String()).Do(context.Background())
if err != nil {
log.Fatalf("failed to fetch account info: %s", err)
}
log.Printf("Account balance: %d microAlgos", acctInfo.Amount)
Build First Transaction¶
Transactions are used to interact with the Algorand network. To create a payment transaction use the following code.
sp, err := algodClient.SuggestedParams().Do(context.Background())
if err != nil {
log.Fatalf("failed to get suggested params: %s", err)
}
// payment from account to itself
ptxn, err := transaction.MakePaymentTxn(acct.Address.String(), acct.Address.String(), 100000, nil, "", sp)
if err != nil {
log.Fatalf("failed creating transaction: %s", err)
}
Info
Algorand supports many transaction types. To see what types are supported see Transactions.
Sign First transaction¶
Before the transaction is considered valid, it must be signed by a private key. Use the following code to sign the transaction.
_, sptxn, err := crypto.SignTransaction(acct.PrivateKey, ptxn)
if err != nil {
fmt.Printf("Failed to sign transaction: %s\n", err)
return
}
Info
Algorand provides many ways to sign transactions. To see other ways see Authorization.
Submit transaction¶
The signed transaction can now be broadcast to the network for validation and inclusion in a future block. The waitForConfirmation
SDK method polls the algod
node for the transaction ID to ensure it succeeded.
pendingTxID, err := algodClient.SendRawTransaction(sptxn).Do(context.Background())
if err != nil {
fmt.Printf("failed to send transaction: %s\n", err)
return
}
confirmedTxn, err := transaction.WaitForConfirmation(algodClient, pendingTxID, 4, context.Background())
if err != nil {
fmt.Printf("Error waiting for confirmation on txID: %s\n", pendingTxID)
return
}
fmt.Printf("Confirmed Transaction: %s in Round %d\n", pendingTxID, confirmedTxn.ConfirmedRound)
Viewing the Transaction¶
To view the transaction we submitted to the sandbox Algod, open Lora and choose LocalNet
configuration option, then search for the transaction ID.
To view a transaction submitted to public network like testnet, open Lora or Pera Explorer and paste the transaction ID into the search bar.