Skip to content

Instantly share code, notes, and snippets.

@matheusb-comp
Created September 11, 2018 00:37
Show Gist options
  • Save matheusb-comp/b9e885606a1724719851cf36b77b8c62 to your computer and use it in GitHub Desktop.
Save matheusb-comp/b9e885606a1724719851cf36b77b8c62 to your computer and use it in GitHub Desktop.
Stellar CSV
// TODO: Fix the end-case scenario where the FINAL_OP_PT is the last operation the Pool did
package main
import (
"os"
"fmt"
"log"
"time"
"strconv"
"net/http"
"encoding/json"
// "github.com/stellar/go/clients/horizon"
)
const FINAL_OP_PT = 82481733068517476
type Link struct {
Href string `json:"href"`
}
type Operations struct {
Links struct {
Self Link `json:"self"`
Next Link `json:"next"`
Prev Link `json:"prev"`
} `json:"_links"`
Embedded struct {
Records []Operation `json:"records"`
} `json:"_embedded"`
}
type Operation struct {
ID string `json:"id"`
Type string `json:"type"`
PagingToken string `json:"paging_token"`
CreatedAt time.Time `json:"created_at"`
From string `json:"from"`
To string `json:"to"`
Amount string `json:"amount"`
}
func main() {
f, err := os.OpenFile("ops.csv", os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0666)
if err != nil {
log.Fatalln("Failed to open the file: ", err)
}
defer f.Close()
// client := horizon.DefaultPublicNetClient
opURL := "https://horizon.stellar.org/accounts/GCCD6AJOYZCUAQLX32ZJF2MKFFAUJ53PVCFQI3RHWKL3V47QYE2BNAUT/operations?limit=200&order=asc&cursor=82479826102943745"
for opURL != "" {
fmt.Println("getting ", opURL)
resp, err := http.Get(opURL)
if err != nil {
log.Fatalln("Failed to GET ", opURL, ": ", err)
}
var ops Operations
if err = json.NewDecoder(resp.Body).Decode(&ops); err != nil {
log.Fatalln("Failed to decode response JSON: ", err)
}
for _, o := range ops.Embedded.Records {
fmt.Println("Reading Operation #", o.PagingToken, "...")
pt, err := strconv.ParseInt(o.PagingToken, 10, 64)
if err != nil {
log.Fatalln("Failed to convert paging token ", o.PagingToken, " to Int64:", err)
}
if pt > FINAL_OP_PT {
fmt.Println("Done!")
if err = f.Close(); err != nil {
log.Fatalln("Failed to close the file: ", err)
}
return
}
if _, err = f.Write([]byte(fmt.Sprintf("%s,%s\n", o.To, o.Amount))); err != nil {
log.Fatalln("Failed to write to the file: ", err)
}
}
opURL = ops.Links.Next.Href
}
log.Fatalln("Failed getting the new URL")
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment