Skip to content

创建文章

We are looking for publications that demonstrate building dApps or smart contracts!
See the full list of Gitcoin bounties that are eligible for rewards.

Your First Transaction

This section is a quick start guide for sending your first transaction on the Algorand TestNet network using the Go programming language. This guide installs the Go SDK, creates an account and submits a payment transaction. This guide also installs Algorand Sandbox, which provides required infrastructure for development and testing.

Install Algorand Sandbox

Algorand Sandbox is developer-focused tool for quickly spinning up the Algorand infrastructure portion of your development environment. It uses Docker to provide an algod instance for connecting to the network of your choosing and an indexer instance for querying blockchain data. APIs are exposed by both instances for client access provided within the SDK. Read more about Algorand networks, their capabilities and intended use.

Prerequisites

From a terminal window, install Algorand Sandbox connected to TestNet:

git clone https://github.com/algorand/sandbox.git
cd sandbox
./sandbox up testnet

Warning

The Algorand Sandbox installation may take a few minutes to complete in order to catch up to the current round on TestNet. To learn more about fast catchup, see Sync Node Network using Fast Catchup.

Install Go SDK

Algorand provides an SDK for Go.

Prerequisites

From a terminal window, install the Go SDK:

go get -u github.com/algorand/go-algorand-sdk/v2

The SDK is installed and can now interact with the running Algorand Sandbox environment, as configured above.

Create account

In order to interact with the Algorand blockchain, you must have a funded account on the network. To quickly create an account on Algorand TestNet create a new file yourFirstTransaction.go and insert the following code:

newAccount := crypto.GenerateAccount()
passphrase, err := mnemonic.FromPrivateKey(newAccount.PrivateKey)

if err != nil {
    fmt.Printf("Error creating transaction: %s\n", err)
} else {
    fmt.Printf("My address: %s\n", newAccount.Address)
    fmt.Printf("My passphrase: %s\n", passphrase)
}
Snippet Source

Note

Lines 17 and 35 contain TODO: comments about inserting additional code. As you proceed with this guide, ensure the line numbers remain in sync.

Tip

Make sure to save the generated address and passphrase in a secure location, as they will be used later on.

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 account

The code below prompts to fund the newly generated account. Before sending transactions to the Algorand network, the account must be funded to cover the minimal transaction fees that exist on Algorand. To fund the account use the Algorand TestNet 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

Instantiate client

You must instantiate a client prior to making calls to the API endpoints. The Go SDK implements the client natively using the following code:

// 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},
)
Snippet Source

Info

This guide provides values for algodAddress and algodToken as specified by Algorand Sandbox. If you want to connect to a third-party service provider, see Purestake or AlgoExplorer Developer API and adjust these values accordingly.

Check account 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)
Snippet Source

Build transaction

Communication with the Algorand network is performed using transactions. Create a payment transaction sending 1 ALGO from your account to the TestNet faucet address:

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)
}
Snippet Source

Info

Algorand supports many transaction types. To see what types are supported see Transactions.

Sign 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
}
Snippet Source

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)
Snippet Source

Viewing the Transaction

To view the transaction, open the Algorand Blockchain Explorer or Goal Seeker and paste the transaction ID into the search bar or simply click on the funded transaction link on the dispenser page.)