Last active
June 4, 2019 04:14
-
-
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]
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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