Skip to content

Instantly share code, notes, and snippets.

@soichisumi
Last active June 4, 2019 04:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save soichisumi/6f86bfb1eb8b79c757b5bcea8616a30d to your computer and use it in GitHub Desktop.
Save soichisumi/6f86bfb1eb8b79c757b5bcea8616a30d to your computer and use it in GitHub Desktop.
an example of stellar/go/horizonclient and conversion of Embedded.Records / [Stellar] [Go]
package main
import (
"encoding/json"
"fmt"
"github.com/stellar/go/clients/horizonclient"
"github.com/stellar/go/protocols/horizon/operations"
)
func Parse(src interface{}, dst interface{}) error {
b, err := json.Marshal(src)
if err != nil {
return err
}
return json.Unmarshal(b, dst)
}
func main() {
ops, err := horizonclient.DefaultTestNetClient.Operations(horizonclient.OperationRequest{
IncludeFailed: true,
Limit: 200,
Order: horizonclient.OrderDesc,
ForLedger: 659251,
})
if err != nil {
fmt.Printf("err: %+v\n", err)
}
println("yo")
for _, v := range ops.Embedded.Records {
fmt.Println("record. type:" + v.GetType())
switch v.GetType() {
case "create_account":
var op operations.CreateAccount
err := Parse(v, &op)
if err != nil {
fmt.Printf("err: %+v\n", err)
continue
}
fmt.Printf("create account. source: %s\n", op.SourceAccount)
case "payment":
var op operations.Payment
err := Parse(v, &op)
if err != nil {
fmt.Printf("err: %+v\n", err)
continue
}
fmt.Printf("payment. from: %s\n to: %s\n, amount: %s, as: %+v", op.From, op.To, op.Amount, op.Asset)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment