3

I recently started using the Go SDK to interact with the horizon testnet.

I am able to read data from the account key/value store using the func (*Account) GetData method:

stellarAcc, err := client.LoadAccount(accountId)
if err != nil {
   log.Println("Stellar AccountId not found")
   return
}
res, _ := stellarAcc.GetData(key)
s := string(res[:5])
log.Printf("Value: %s", s)

I can't find in the documentation any method to set a new key/value pair. Is it possible with the current state of the SDK?

2

I now understand that you have to build the transaction yourself using build. Example if anyone else gets stuck:

import (
    build "github.com/stellar/go/build"
    "github.com/stellar/go/clients/horizon"
)

var b = []byte("Hello, goodbye, etc!")

tx, err := build.Transaction(
    build.SourceAccount{AddressOrSeed: accountId},
    build.TestNetwork,
    build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},
    build.SetData("key", b),
)
if err != nil {
     panic(err)
}
txe, err := tx.Sign("xxx")
if err != nil {
    panic(err)
}

txeB64, err := txe.Base64()

if err != nil {
    panic(err)
}

fmt.Printf("tx base64: %s", txeB64)

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

fmt.Println("transaction posted in ledger:", resp.Ledger)
0

You need to build the transaction and include the SetData operation.

Sample code:

import "github.com/stellar/go/build"

tx, err := build.Transaction(
    build.SourceAccount{AddressOrSeed: accountId},
    build.TestNetwork,
    build.AutoSequence{SequenceProvider: horizon.DefaultTestNetClient},
    build.SetData("key", []byte("value")),  // data value is a []byte
)

Note that when you read the data from horizon it will be in base64 format and will need to be decoded to give the original "value" string.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.