first commit
This commit is contained in:
commit
9b4db62b83
6
.gitignore
vendored
Normal file
6
.gitignore
vendored
Normal file
@ -0,0 +1,6 @@
|
||||
# binaries
|
||||
dficsv
|
||||
*.exe
|
||||
|
||||
# csv files
|
||||
*.csv
|
5
go.mod
Normal file
5
go.mod
Normal file
@ -0,0 +1,5 @@
|
||||
module github.com/thunderstorm99/dficsv
|
||||
|
||||
go 1.16
|
||||
|
||||
require git.nils.zone/nils/prettify v0.0.4
|
16
go.sum
Normal file
16
go.sum
Normal file
@ -0,0 +1,16 @@
|
||||
git.nils.zone/nils/prettify v0.0.4 h1:y014Ejp0yhFAc9vu5U0nJRFxtXN6k6eWW6LiUkwn4NQ=
|
||||
git.nils.zone/nils/prettify v0.0.4/go.mod h1:q4ydEhSvXuywHe5U6uSEoEeS9f2Upz/n8z0MJylLz5c=
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2 h1:ZBbLwSJqkHBuFDA6DUhhse0IGJ7T5bemHyNILUjvOq4=
|
||||
github.com/TylerBrock/colorjson v0.0.0-20200706003622-8a50f05110d2/go.mod h1:VSw57q4QFiWDbRnjdX8Cb3Ow0SFncRw+bA/ofY6Q83w=
|
||||
github.com/fatih/color v1.9.0 h1:8xPHl4/q1VyqGIPif1F+1V3Y3lSmrq01EabUW3CoW5s=
|
||||
github.com/fatih/color v1.9.0/go.mod h1:eQcE1qtQxscV5RaZvpXrrb8Drkc3/DdQ+uUYCNjL+zU=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e h1:0aewS5NTyxftZHSnFaJmWE5oCCrj4DyEXkAiMa1iZJM=
|
||||
github.com/hokaccha/go-prettyjson v0.0.0-20190818114111-108c894c2c0e/go.mod h1:pFlLw2CfqZiIBOx6BuCeRLCrfxBJipTY0nIOF/VbGcI=
|
||||
github.com/mattn/go-colorable v0.1.4 h1:snbPLB8fVfU9iwbbo30TPtbLRzwWu6aJS6Xh4eaaviA=
|
||||
github.com/mattn/go-colorable v0.1.4/go.mod h1:U0ppj6V5qS13XJ6of8GYAs25YV2eR4EVcfRqFIhoBtE=
|
||||
github.com/mattn/go-isatty v0.0.8/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
|
||||
github.com/mattn/go-isatty v0.0.11 h1:FxPOTFNqGkuDUGi3H/qkUbQO4ZiBa2brKq5r0l8TGeM=
|
||||
github.com/mattn/go-isatty v0.0.11/go.mod h1:PhnuNfih5lzO57/f3n+odYbM4JtupLOxQOAqxQCu2WE=
|
||||
golang.org/x/sys v0.0.0-20190222072716-a9d3bda3a223/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037 h1:YyJpGZS1sBuBCzLAR1VEpK193GlqGZbnPFnPV/5Rsb4=
|
||||
golang.org/x/sys v0.0.0-20191026070338-33540a1f6037/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
153
main.go
Normal file
153
main.go
Normal file
@ -0,0 +1,153 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"flag"
|
||||
"fmt"
|
||||
"log"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"git.nils.zone/nils/prettify"
|
||||
)
|
||||
|
||||
type lineEntry struct {
|
||||
BlockHeight int
|
||||
BlockHash string
|
||||
Type string
|
||||
PoolID int
|
||||
Time time.Time
|
||||
Amounts map[string]float64
|
||||
}
|
||||
|
||||
func main() {
|
||||
// define flags
|
||||
csvFile := flag.String("csvFile", "", "specifies the path the csv file containing the transaction log")
|
||||
flag.Parse()
|
||||
if *csvFile == "" {
|
||||
log.Fatal("please specify a csvFile using the flag -csvFile")
|
||||
}
|
||||
|
||||
// start of program
|
||||
b, err := readCSV(*csvFile)
|
||||
if err != nil {
|
||||
log.Fatalf("error occurred when reading csv, error is: %s", err)
|
||||
}
|
||||
entries, err := unmarshalCSV(b)
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
var rewards []lineEntry
|
||||
for i := range entries {
|
||||
if entries[i].Type == "Rewards" {
|
||||
rewards = append(rewards, entries[i])
|
||||
}
|
||||
}
|
||||
|
||||
// daily
|
||||
listRewards("2006-01-02", rewards)
|
||||
|
||||
// monthly
|
||||
listRewards("2006-01", rewards)
|
||||
|
||||
// yearly
|
||||
listRewards("2006", rewards)
|
||||
}
|
||||
|
||||
func listRewards(format string, entries []lineEntry) {
|
||||
var str string
|
||||
switch format {
|
||||
case "2006-01-02":
|
||||
str = "daily"
|
||||
case "2006-01":
|
||||
str = "monthly"
|
||||
case "2006":
|
||||
str = "yearly"
|
||||
}
|
||||
rewards := make(map[string]float64)
|
||||
log.Printf("listing %s Rewards", str)
|
||||
for i := range entries {
|
||||
rewardsString := entries[i].Time.Format(format)
|
||||
rewards[rewardsString] += entries[i].Amounts["DFI"]
|
||||
}
|
||||
prettify.Print(rewards)
|
||||
}
|
||||
|
||||
func readCSV(filename string) ([]byte, error) {
|
||||
b, err := os.ReadFile(filename)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return b, nil
|
||||
}
|
||||
|
||||
func unmarshalCSV(b []byte) ([]lineEntry, error) {
|
||||
// output variable
|
||||
var e []lineEntry
|
||||
|
||||
// read all lines and do magic on each line
|
||||
lines := strings.Split(string(b), "\n")
|
||||
// for i := range lines {
|
||||
for i := 1; i < len(lines); i++ {
|
||||
// create variable for current line
|
||||
var l lineEntry
|
||||
l.Amounts = make(map[string]float64)
|
||||
|
||||
// Remove " from lines
|
||||
line := strings.ReplaceAll(lines[i], `"`, ``)
|
||||
|
||||
// split line by comma
|
||||
entries := strings.Split(line, ",")
|
||||
|
||||
// convert blockHeight to int
|
||||
height, err := strconv.Atoi(entries[0])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't convert blockHeight in line %d to int, error is: %s", i+1, err)
|
||||
}
|
||||
|
||||
// get time from CSV
|
||||
dateTime, err := time.Parse("02/01/2006 / 03:04 pm", entries[2])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't convert time in line %d to time.time, error is: %s", i+1, err)
|
||||
}
|
||||
|
||||
// convert poolID to int
|
||||
if entries[5] == "" {
|
||||
// empty row
|
||||
entries[5] = "0"
|
||||
}
|
||||
poolID, err := strconv.Atoi(entries[5])
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("can't convert poolID in line %d to int, error is: %s", i+1, err)
|
||||
}
|
||||
|
||||
// Split for amount and currency
|
||||
amounts := strings.Split(strings.Join(entries[6:], " "), " ")
|
||||
for i := range amounts {
|
||||
// Split amounts and currency
|
||||
ac := strings.Split(amounts[i], "@")
|
||||
amountString := ac[0]
|
||||
currency := ac[1]
|
||||
// Parse string to float
|
||||
amount, err := strconv.ParseFloat(amountString, 64)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
// put rewards into map
|
||||
l.Amounts[currency] = amount
|
||||
}
|
||||
|
||||
// fill l with values
|
||||
l.BlockHeight = height // entries[0]
|
||||
l.BlockHash = entries[1] // entries[1]
|
||||
l.Type = entries[4] // entries[2]
|
||||
l.PoolID = poolID // entries[3]
|
||||
l.Time = dateTime
|
||||
|
||||
e = append(e, l)
|
||||
}
|
||||
return e, nil
|
||||
}
|
Loading…
Reference in New Issue
Block a user