-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
179 lines (147 loc) · 4.03 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
package main
import (
"bytes"
"context"
"flag"
"fmt"
"io"
"io/ioutil"
"log"
"os"
"os/exec"
"runtime"
"strings"
"text/template"
"time"
"github.com/fox-one/mixin-sdk-go"
"github.com/manifoldco/promptui"
"github.com/shopspring/decimal"
)
type Config struct {
AccessToken string
ClientID string
ClientSecret string
AssetID string
OpponentID string
StartTime time.Time
EndTime time.Time
OutputPath string
FormatTemplate *template.Template
}
func main() {
cfg := initConfig()
snapshots := getSnapshots(context.Background(), cfg)
if len(snapshots) == 0 {
log.Println("no records")
os.Exit(0)
}
writer := new(bytes.Buffer)
ids := make([]string, len(snapshots))
asm := map[string][]*mixin.Snapshot{}
for i, s := range snapshots {
asm[s.AssetID] = append(asm[s.AssetID], s)
ids[i] = fmt.Sprintf("'%s'", s.SnapshotID)
tpl := new(bytes.Buffer)
fatalIfErr(cfg.FormatTemplate.Execute(tpl, s))
fmt.Fprintf(writer, "%s\n", tpl.String())
}
fmt.Fprintf(writer, "\nids: (%s)\n\n", strings.Join(ids, ", "))
for asset, ss := range asm {
count := len(ss)
var total decimal.Decimal
for _, s := range ss {
total = total.Add(s.Amount)
}
fmt.Fprintf(writer, "asset: %s -> (count: %d, total: %s)\n", asset, count, total)
}
if cfg.OutputPath != "" {
err := ioutil.WriteFile(cfg.OutputPath, writer.Bytes(), 0644)
fatalIfErr(err)
} else {
io.Copy(os.Stdout, writer)
}
}
func getSnapshots(ctx context.Context, cfg *Config) []*mixin.Snapshot {
var result []*mixin.Snapshot
limit := 500
offset := cfg.StartTime
for {
ss, err := mixin.ReadSnapshots(ctx, cfg.AccessToken, cfg.AssetID, offset, "ASC", limit)
fatalIfErr(err)
for _, s := range ss {
if !cfg.EndTime.IsZero() && s.CreatedAt.After(cfg.EndTime) {
break
}
if s.OpponentID == cfg.OpponentID {
result = append(result, s)
}
}
if len(ss) < limit {
break
}
offset = ss[len(ss)-1].CreatedAt
}
return result
}
func initConfig() *Config {
cfg := &Config{}
var startTime, endTime, format string
flag.StringVar(&cfg.AccessToken, "token", "", "Access token")
flag.StringVar(&cfg.ClientID, "client", "", "Mixin client id")
flag.StringVar(&cfg.ClientSecret, "secret", "", "Mixin client secret")
flag.StringVar(&cfg.AssetID, "asset", "", "Asset id")
flag.StringVar(&cfg.OpponentID, "opponent", "", "Opponent id")
flag.StringVar(&startTime, "start", "", "Start time, RFC3339 format")
flag.StringVar(&endTime, "end", "", "End time, RFC3339 format")
flag.StringVar(&cfg.OutputPath, "output", "", "Output file path")
flag.StringVar(&format, "format", "id: {{ .SnapshotID }} -> (asset: {{ .AssetID }}, amount: {{ .Amount }})", "Snapshot format")
flag.Parse()
if cfg.AccessToken == "" && (cfg.ClientID == "" || cfg.ClientSecret == "") {
log.Fatalln("you must set one of token and (client+secret)")
}
if startTime != "" {
cfg.StartTime = mustParseTime(startTime)
}
if endTime != "" {
cfg.EndTime = mustParseTime(endTime)
}
if cfg.AccessToken == "" {
scope := "SNAPSHOTS:READ+PROFILE:READ"
err := openBrowser(fmt.Sprintf("https://mixin-oauth.fox.one?client_id=%s&scope=%s", cfg.ClientID, scope))
fatalIfErr(err)
prompt := promptui.Prompt{
Label: "OAuth Code",
}
code, err := prompt.Run()
fatalIfErr(err)
cfg.AccessToken, _, err = mixin.AuthorizeToken(context.Background(), cfg.ClientID, cfg.ClientSecret, code, "")
fatalIfErr(err)
fmt.Printf("\ntoken: %s\n\n", cfg.AccessToken)
}
cfg.FormatTemplate = template.Must(template.New("").Parse(format))
return cfg
}
func mustParseTime(v string) time.Time {
t, err := time.Parse(time.RFC3339, v)
fatalIfErr(err)
return t
}
func fatalIfErr(err error) {
if err != nil {
log.Fatal(err)
}
}
func openBrowser(url string) error {
var err error
switch runtime.GOOS {
case "linux":
err = exec.Command("xdg-open", url).Start()
case "windows":
err = exec.Command("rundll32", "url.dll,FileProtocolHandler", url).Start()
case "darwin":
err = exec.Command("open", url).Start()
default:
err = fmt.Errorf("unsupported platform")
}
return err
}