Stellar Cryptocurrency Tutorial: Making a payment with Go on the Testnet

Enter the Testnet(s)

Plan of Action

Accounts

// stellar_payment.gopackage main
import (
"log"
"github.com/stellar/go/keypair"
)
func main() {
sourcePair, err := keypair.Random()
if err != nil {
log.Fatal(err)
}
destinationPair, err := keypair.Random()
if err != nil {
log.Fatal(err)
}
}
log.Println(sourcePair.Address())

Load ’em up

func fillAccounts(addresses [2]string) {
for _, address := range addresses {
friendBotResp, err := http.Get("https://horizon-testnet.stellar.org/friendbot?addr=" + address)
if err != nil {
log.Fatal(err)
}
defer friendBotResp.Body.Close()
}
}
"github.com/stellar/go/clients/horizon"
"net/http"
addresses := [2]string{sourcePair.Address(), destinationPair.Address()}

fillAccounts(addresses)
func logBalances(addresses [2]string) {
for _, address := range addresses {
account, err := horizon.DefaultTestNetClient.LoadAccount(address)
if err != nil {
log.Fatal(err)
}
log.Println("Balances for address:", address)
for _, balance := range account.Balances {
log.Println(balance)
}
}
}
logBalances(addresses)
func sendLumens(amount string, sourceSeed string, destinationAddress string) {
tx, err := build.Transaction(
build.SourceAccount{sourceSeed},
build.TestNetwork,
build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},
build.Payment(
build.Destination{AddressOrSeed: destinationAddress},
build.NativeAmount{Amount: amount},
),
)

if err != nil {
panic(err)
}

txe, err := tx.Sign(sourceSeed)
if err != nil {
panic(err)
}

txeB64, err := txe.Base64()
if err != nil {
panic(err)
}

resp, err := horizon.DefaultTestNetClient.SubmitTransaction(txeB64)
if err != nil {
panic(err)
}

log.Println("Successfully sent", amount, "lumens to", destinationAddress,". Hash:", resp.Hash)
}
"github.com/stellar/go/build"
sendLumens("100", sourcePair.Seed(), destinationPair.Address())
logBalances(addresses)

--

--

I help developers with easy-to-understand technical articles encompassing System Design, Algorithms, Cutting-Edge tech, and misc.

Love podcasts or audiobooks? Learn on the go with our new app.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store