This repository has been archived by the owner on Jul 31, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 83
/
parse.go
120 lines (99 loc) · 2.35 KB
/
parse.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
package main
import (
"encoding/csv"
"fmt"
"io"
"strconv"
"strings"
"time"
"github.com/regen-network/regen-ledger/app"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type Record struct {
Address sdk.AccAddress
TotalAmount Dec
StartTime time.Time
NumMonthlyDistributions int
}
const (
// Based on 365.2425 days/year
SecondsPerYear = 31556952
SecondsPerMonth = 2629746
)
var (
OneYear, OneMonth time.Duration
)
func init() {
var err error
OneYear, err = time.ParseDuration(fmt.Sprintf("%ds", SecondsPerYear))
if err != nil {
panic(err)
}
OneMonth, err = time.ParseDuration(fmt.Sprintf("%ds", SecondsPerMonth))
if err != nil {
panic(err)
}
}
func ParseAccountsCsv(rdr io.Reader, genesisTime time.Time, errorsAsWarnings bool) ([]Record, error) {
csvRdr := csv.NewReader(rdr)
lines, err := csvRdr.ReadAll()
if err != nil {
return nil, err
}
records := make([]Record, 0, len(lines))
for i, line := range lines {
record, err := parseLine(line, genesisTime)
if err != nil {
if errorsAsWarnings {
fmt.Printf("WARNING: Error on line %d: %v\n", i, err)
continue
} else {
return nil, err
}
}
records = append(records, record)
}
return records, nil
}
func parseLine(line []string, genesisTime time.Time) (Record, error) {
addr, err := sdk.GetFromBech32(line[0], app.Bech32PrefixAccAddr)
if err != nil {
return Record{}, err
}
amount, err := NewDecFromString(line[1])
if err != nil {
return Record{}, err
}
var startTime time.Time
startTimeStr := strings.TrimSpace(line[2])
switch startTimeStr {
case "MAINNET":
startTime = genesisTime
case "MAINNET+1YEAR":
startTime = genesisTime.Add(OneYear)
default:
startTime, err = time.Parse("2006-01-02", line[2])
if err != nil {
return Record{}, err
}
}
numDist, err := strconv.Atoi(line[3])
if err != nil {
return Record{}, err
}
if numDist < 1 {
return Record{}, fmt.Errorf("expected a positive integer, got %d", numDist)
}
return Record{
Address: addr,
TotalAmount: amount,
StartTime: startTime,
NumMonthlyDistributions: numDist,
}, nil
}
func (r Record) Equal(o Record) bool {
return r.StartTime.Equal(o.StartTime) &&
r.TotalAmount.IsEqual(o.TotalAmount) &&
r.Address.Equals(o.Address) &&
r.NumMonthlyDistributions == o.NumMonthlyDistributions
}