This repository has been archived by the owner on Nov 23, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
csv.go
155 lines (123 loc) · 3.11 KB
/
csv.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
package services
import (
"bytes"
"encoding/csv"
"fmt"
"github.com/everstake/teztracker/models"
"github.com/jszwec/csvutil"
)
const (
limit = 250000
frontHost = "https://teztracker.everstake.one/en/mainnet/tx/%s"
)
var header = []string{"block level", "timestamp", "operation", "coin", "in", "out", "from", "to", "fee", "reward", "loss", "status", "link"}
func (t *TezTracker) GetAccountReport(accountID string, from, to int64, operations []string) (resp []byte, err error) {
var buf bytes.Buffer
//Check that account is baker
isBaker, _, err := t.repoProvider.GetBaker().Find(accountID)
if err != nil {
return nil, err
}
var bakingReq, endorsingReq, assetsReq bool
for i := range operations {
if operations[i] == "assets" {
assetsReq = true
}
//For baker check that baking\endorsing\assets operations required
if isBaker {
switch operations[i] {
case "baking":
bakingReq = true
case "endorsement":
endorsingReq = true
}
}
}
report, err := t.repoProvider.GetAccount().GetReport(accountID, models.ReportFilter{
From: from,
To: to,
Operations: operations,
EndorsingReq: endorsingReq,
AssetsReq: assetsReq,
Limit: limit,
})
if err != nil {
return nil, err
}
var bakingReport []models.ExtendReport
if bakingReq {
bakingReport, err = t.repoProvider.GetAccount().GetBakingReport(accountID, models.ReportFilter{
From: from,
To: to,
Operations: operations,
Limit: limit,
})
if err != nil {
return nil, err
}
}
writer := csv.NewWriter(&buf)
writer.UseCRLF = true
writer.Comma = ';'
encoder := csvutil.NewEncoder(writer)
var j int
var record interface{}
for i := 0; i < len(report); {
//Merge sort
if j < len(bakingReport) && report[i].BlockLevel <= bakingReport[j].BlockLevel {
record = bakingReport[j]
j++
} else {
//TODO get front host from env
if report[i].OperationGroupHash.Valid {
report[i].Link = fmt.Sprintf(frontHost, report[i].OperationGroupHash.String)
}
report[i].In = report[i].Amount
if report[i].Source == accountID {
report[i].Out = report[i].Amount
}
record = report[i]
if !isBaker {
record = report[i].OperationReport
}
i++
}
err = encoder.Encode(record)
if err != nil {
return nil, err
}
}
writer.Flush()
return buf.Bytes(), nil
}
func (t *TezTracker) GetAssetReport(assetID string, from, to int64, operations []string) (resp []byte, err error) {
repo := t.repoProvider.GetAssets()
token, err := repo.GetTokenInfo(assetID)
if err != nil {
return nil, err
}
report, err := repo.GetAssetReport(token.ID, models.ReportFilter{
From: from,
To: to,
Limit: limit,
})
if err != nil {
return nil, err
}
var buf bytes.Buffer
writer := csv.NewWriter(&buf)
writer.UseCRLF = true
writer.Comma = ';'
encoder := csvutil.NewEncoder(writer)
for i := range report {
if report[i].OperationGroupHash.Valid {
report[i].Link = fmt.Sprintf(frontHost, report[i].OperationGroupHash.String)
}
err = encoder.Encode(report[i])
if err != nil {
return nil, err
}
}
writer.Flush()
return buf.Bytes(), nil
}