This repository has been archived by the owner on May 19, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 11
/
csv.go
105 lines (101 loc) · 3.33 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
package monero
import (
"encoding/csv"
"io"
"log"
"strconv"
"time"
"github.com/fiscafacile/CryptoFiscaFacile/category"
"github.com/fiscafacile/CryptoFiscaFacile/wallet"
"github.com/shopspring/decimal"
)
type CsvTX struct {
BlockHeight string
Epoch time.Time
Date string
Direction string
Amount decimal.Decimal
AtomicAmount decimal.Decimal
Fee decimal.Decimal
TxID string
Label string
SubaddrAccount string
PaymentId string
}
func (xmr *Monero) ParseCSV(reader io.Reader, cat category.Category) (err error) {
const SOURCE = "Monero CSV :"
csvReader := csv.NewReader(reader)
records, err := csvReader.ReadAll()
if err == nil {
alreadyAsked := []string{}
for _, r := range records {
if r[0] != "blockHeight" {
tx := CsvTX{}
tx.BlockHeight = r[0]
epoch, err := strconv.ParseInt(r[1], 10, 64)
if err != nil {
log.Println(SOURCE, "Error Parsing Epoch", r[1])
} else {
tx.Epoch = time.Unix(epoch, 0)
}
tx.Date = r[2]
tx.Direction = r[3]
tx.Amount, err = decimal.NewFromString(r[4])
if err != nil {
log.Println(SOURCE, "Error Parsing Amount", r[4])
}
atomic, err := strconv.ParseInt(r[5], 10, 64)
if err != nil {
log.Println(SOURCE, "Error Parsing AtomicAmount", r[5])
} else {
tx.AtomicAmount = decimal.New(atomic, -12)
}
if r[6] != "" {
tx.Fee, err = decimal.NewFromString(r[6])
if err != nil {
log.Println(SOURCE, "Error Parsing Fee", r[6])
}
}
tx.TxID = r[7]
tx.SubaddrAccount = r[8]
tx.PaymentId = r[9]
xmr.CsvTXs = append(xmr.CsvTXs, tx)
}
}
for _, tx := range xmr.CsvTXs {
// Fixmr TXsByCategory
if tx.Direction == "in" {
t := wallet.TX{Timestamp: tx.Epoch, ID: tx.TxID, Note: SOURCE + " " + tx.BlockHeight + " " + tx.Label}
t.Items = make(map[string]wallet.Currencies)
t.Items["To"] = append(t.Items["To"], wallet.Currency{Code: "XMR", Amount: tx.AtomicAmount})
if !tx.Fee.IsZero() {
t.Items["Fee"] = append(t.Items["Fee"], wallet.Currency{Code: "XMR", Amount: tx.Fee})
}
if is, desc, val, curr := cat.IsTxCashIn(tx.TxID); is {
t.Note += " crypto_purchase " + desc
t.Items["From"] = append(t.Items["From"], wallet.Currency{Code: curr, Amount: val})
xmr.TXsByCategory["CashIn"] = append(xmr.TXsByCategory["CashIn"], t)
} else {
xmr.TXsByCategory["Deposits"] = append(xmr.TXsByCategory["Deposits"], t)
}
} else if tx.Direction == "out" {
t := wallet.TX{Timestamp: tx.Epoch, ID: tx.TxID, Note: SOURCE + " " + tx.BlockHeight + " " + tx.Label}
t.Items = make(map[string]wallet.Currencies)
if !tx.Fee.IsZero() {
t.Items["Fee"] = append(t.Items["Fee"], wallet.Currency{Code: "XMR", Amount: tx.Fee})
}
t.Items["From"] = append(t.Items["From"], wallet.Currency{Code: "XMR", Amount: tx.AtomicAmount})
if is, desc, val, curr := cat.IsTxCashOut(tx.TxID); is {
t.Note += " crypto_payment " + desc
t.Items["To"] = append(t.Items["To"], wallet.Currency{Code: curr, Amount: val})
xmr.TXsByCategory["CashOut"] = append(xmr.TXsByCategory["CashOut"], t)
} else {
xmr.TXsByCategory["Withdrawals"] = append(xmr.TXsByCategory["Withdrawals"], t)
}
} else {
alreadyAsked = wallet.AskForHelp(SOURCE+" : "+tx.Direction, tx, alreadyAsked)
}
}
}
return
}