Skip to content

Instantly share code, notes, and snippets.

@nikkolasg
Created July 21, 2022 15:27
Show Gist options
  • Save nikkolasg/8bf903fbdfb8676614f2c985f04122e0 to your computer and use it in GitHub Desktop.
Save nikkolasg/8bf903fbdfb8676614f2c985f04122e0 to your computer and use it in GitHub Desktop.
get drand round number and timestamp in CSV format
package main
import (
"context"
"encoding/csv"
"encoding/hex"
"fmt"
"os"
"strconv"
"time"
"github.com/drand/drand/chain"
"github.com/drand/drand/client/http"
)
func main() {
chainHash := "8990e7a9aaed2ffed73dbd7092123d6f289930540d7651336225dc172e51b2ce"
hash, err := hex.DecodeString(chainHash)
if err != nil {
panic(fmt.Errorf("decoding chain hash: %w", err))
}
client, err := http.New("https://api.drand.sh/", hash, nil)
if err != nil {
panic(err)
}
info, err := client.Info(context.Background())
if err != nil {
panic(err)
}
records := []string{"round", "ts"}
currentRound := chain.CurrentRound(time.Now().Unix(), info.Period, info.GenesisTime)
w := csv.NewWriter(os.Stdout)
w.Write(records)
for n := 0; n < 10; n++ {
round := currentRound - uint64(n)
roundTime := chain.TimeOfRound(info.Period, info.GenesisTime, round)
records = []string{strconv.Itoa(int(round)), strconv.Itoa(int(roundTime))}
if err := w.Write(records); err != nil {
panic(err)
}
}
w.Flush()
if err := w.Error(); err != nil {
panic(err)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment