Golang Quickstart
A quick start guide for building applications with Sonr using the Go programming language.
This guide provides a walkthrough for setting up a Go project to interact with the Sonr network. You will learn how to create a wallet, query the blockchain, and send transactions using the Sonr Go SDK.
Prerequisites
- A local Sonr network running. See the Installation Guide for instructions.
- Go version 1.18 or higher.
1. Project Setup
Initialize a Go Module
Create a new directory for your project and initialize a Go module:
mkdir sonr-go-quickstart
cd sonr-go-quickstart
go mod init github.com/your-username/sonr-go-quickstart
Add the Sonr SDK Dependency
Add the Sonr SDK to your project's dependencies:
go get github.com/sonr-io/sonr
2. Creating a Wallet
Let's create a new Go application that generates a Sonr wallet.
Create the Main File
Create a new file named main.go
.
Implement Wallet Creation
Add the following code to main.go
to create a new wallet and print its details:
package main
import (
"context"
"fmt"
"github.com/sonr-io/sonr/x/sonr/pkgs/sdk"
)
func main() {
fmt.Println("Creating a new Sonr wallet...")
sonr, err := sdk.NewSonr("http://localhost:1317", "")
if err != nil {
panic(err)
}
wallet, err := sonr.CreateWallet()
if err != nil {
panic(err)
}
fmt.Printf("Wallet created!\n")
fmt.Printf("Address: %s\n", wallet.Address)
fmt.Printf("Mnemonic: %s\n", wallet.Mnemonic)
}
Important: In a real application, you must store the mnemonic securely. Never expose it in your source code.
Run the Application
Execute the application from your terminal:
go run main.go
You will see the address and mnemonic of your new wallet printed to the console.
3. Querying the Blockchain
Now, let's query the blockchain for an account's balance.
Get Account Balance
Modify your main.go
file to query an account balance. For this example, we'll query the balance of the localnet validator, which is pre-funded.
// ... inside your main function
fmt.Println("Querying account balance...")
// Replace with the actual validator address from your localnet
validatorAddress := "snr1..._validator_address_..."
balance, err := sonr.GetAccountBalance(context.Background(), validatorAddress)
if err != nil {
panic(err)
}
fmt.Printf("Balance for %s: %v\n", validatorAddress, balance)
4. Sending a Transaction
Finally, let's send a transaction between two wallets.
Implement Transaction Sending
You will need two wallets for this step. You can create them using the CreateWallet
method. Make sure to fund the sending wallet from the localnet faucet.
// ... inside your main function
wallet1, _ := sonr.CreateWallet() // Fund this from the faucet
wallet2, _ := sonr.CreateWallet()
fmt.Printf("Sending 1 SNR from %s to %s\n", wallet1.Address, wallet2.Address)
result, err := wallet1.Send(wallet2.Address, "1000000usnr")
if err != nil {
panic(err)
}
fmt.Printf("Transaction successful! TxHash: %s\n", result.TxHash)
Next Steps
This quickstart has covered the basics of using the Sonr Go SDK. You are now ready to explore more advanced features:
- Working with DIDs: Create and manage decentralized identities.
- Using UCANs: Implement capability-based authorization in your applications.
- Interacting with Smart Contracts: Build services that leverage on-chain logic.