forked from fiscafacile/CryptoFiscaFacile
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
524 lines (521 loc) · 16.6 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
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
package main
import (
"fmt"
"log"
"os"
"time"
"github.com/fiscafacile/CryptoFiscaFacile/binance"
"github.com/fiscafacile/CryptoFiscaFacile/bitfinex"
"github.com/fiscafacile/CryptoFiscaFacile/bitstamp"
"github.com/fiscafacile/CryptoFiscaFacile/bittrex"
"github.com/fiscafacile/CryptoFiscaFacile/blockchain"
"github.com/fiscafacile/CryptoFiscaFacile/blockstream"
"github.com/fiscafacile/CryptoFiscaFacile/btc"
"github.com/fiscafacile/CryptoFiscaFacile/category"
"github.com/fiscafacile/CryptoFiscaFacile/cfg"
"github.com/fiscafacile/CryptoFiscaFacile/coinbase"
"github.com/fiscafacile/CryptoFiscaFacile/cryptocom"
"github.com/fiscafacile/CryptoFiscaFacile/etherscan"
"github.com/fiscafacile/CryptoFiscaFacile/hitbtc"
"github.com/fiscafacile/CryptoFiscaFacile/kraken"
"github.com/fiscafacile/CryptoFiscaFacile/ledgerlive"
"github.com/fiscafacile/CryptoFiscaFacile/localbitcoin"
"github.com/fiscafacile/CryptoFiscaFacile/mycelium"
"github.com/fiscafacile/CryptoFiscaFacile/poloniex"
"github.com/fiscafacile/CryptoFiscaFacile/revolut"
"github.com/fiscafacile/CryptoFiscaFacile/source"
"github.com/fiscafacile/CryptoFiscaFacile/wallet"
"github.com/shopspring/decimal"
)
func main() {
// Configuration
config, err := cfg.LoadConfig()
if err != nil {
log.Fatal(err)
}
if config.Options.LogFile != "" {
f, err := os.OpenFile(config.Options.LogFile, os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if err != nil {
log.Fatalf("error opening file: %v", err)
}
defer f.Close()
log.SetOutput(f)
}
if config.Tools.CoinAPI.Key != "" {
wallet.CoinAPISetKey(config.Tools.CoinAPI.Key)
}
if config.Tools.CoinLayer.Key != "" {
wallet.CoinLayerSetKey(config.Tools.CoinLayer.Key)
}
categ := category.New()
if config.Options.TxsCategory != "" {
recordFile, err := os.Open(config.Options.TxsCategory)
if err != nil {
log.Fatal("Error opening Transactions CSV Category file:", err)
}
categ.ParseCSVCategory(recordFile)
}
loc, err := time.LoadLocation(config.Options.Location)
if err != nil {
log.Fatal("Error parsing Location:", err)
}
// Launch APIs access in go routines
btc := btc.New()
btc.AddListAddresses(config.Blockchains.BTC.Addresses)
for _, file := range config.Blockchains.BTC.CSV {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Bitcoin CSV Addresses file:", err)
}
err = btc.ParseCSVAddresses(recordFile)
if err != nil {
log.Fatal("")
}
}
blkst := blockstream.New()
if len(config.Blockchains.BTC.CSV)+len(config.Blockchains.BTC.Addresses) > 0 {
go blkst.GetAllTXs(btc, *categ)
}
ethsc := etherscan.New()
ethsc.AddListAddresses(config.Blockchains.ETH.Addresses)
for _, file := range config.Blockchains.ETH.CSV {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Ethereum CSV Addresses file:", err)
}
err = ethsc.ParseCSVAddresses(recordFile)
if err != nil {
log.Fatal("")
}
}
if len(config.Blockchains.ETH.CSV)+len(config.Blockchains.ETH.Addresses) > 0 {
ethsc.NewAPI(config.Tools.EtherScan.Key, config.Options.Debug)
go ethsc.GetAPITXs(*categ)
}
b := binance.New()
if config.Exchanges.Binance.API.Key != "" && config.Exchanges.Binance.API.Secret != "" {
b.NewAPI(config.Exchanges.Binance.API.Key, config.Exchanges.Binance.API.Secret, config.Options.Debug)
fmt.Print("Début de récupération des TXs par l'API Binance (attention ce processus peut être long la première fois)...")
go b.GetAPIAllTXs(loc)
}
bs := bitstamp.New()
if config.Exchanges.Bitstamp.API.Key != "" && config.Exchanges.Bitstamp.API.Secret != "" {
bs.NewAPI(config.Exchanges.Bitstamp.API.Key, config.Exchanges.Bitstamp.API.Secret, config.Options.Debug)
go bs.GetAPIAllTXs()
}
btrx := bittrex.New()
if config.Exchanges.Bittrex.API.Key != "" && config.Exchanges.Bittrex.API.Secret != "" {
btrx.NewAPI(config.Exchanges.Bittrex.API.Key, config.Exchanges.Bittrex.API.Secret, config.Options.Debug)
go btrx.GetAPIAllTXs()
}
cdc := cryptocom.New()
if config.Exchanges.CdcEx.API.Key != "" && config.Exchanges.CdcEx.API.Secret != "" {
cdc.NewExchangeAPI(config.Exchanges.CdcEx.API.Key, config.Exchanges.CdcEx.API.Secret, config.Options.Debug)
fmt.Print("Début de récupération des TXs par l'API CdC Exchange (attention ce processus peut être long la première fois)")
go cdc.GetAPIExchangeTXs(loc)
}
hb := hitbtc.New()
if config.Exchanges.HitBTC.API.Key != "" && config.Exchanges.HitBTC.API.Secret != "" {
hb.NewAPI(config.Exchanges.HitBTC.API.Key, config.Exchanges.HitBTC.API.Secret, config.Options.Debug)
go hb.GetAPIAllTXs()
}
kr := kraken.New()
if config.Exchanges.Kraken.API.Key != "" && config.Exchanges.Kraken.API.Secret != "" {
kr.NewAPI(config.Exchanges.Kraken.API.Key, config.Exchanges.Kraken.API.Secret, config.Options.Debug)
go kr.GetAPIAllTXs()
}
// Now parse local files
bc := blockchain.New()
if config.Blockchains.BTG.JSON != "" {
jsonFile, err := os.Open(config.Blockchains.BTG.JSON)
if err != nil {
log.Fatal("Error opening Bitcoin Gold JSON Transactions file:", err)
}
err = bc.ParseTXsJSON(jsonFile, "BTG")
if err != nil {
log.Fatal("Error parsing Bitcoin Gold JSON Transactions file:", err)
}
}
for _, file := range config.Exchanges.Binance.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Binance CSV file:", err)
}
err = b.ParseCSV(recordFile, config.Options.BinanceExtended)
if err != nil {
log.Fatal("Error parsing Binance CSV file:", err)
}
}
bf := bitfinex.New()
for _, file := range config.Exchanges.Bitfinex.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Bitfinex CSV file:", err)
}
err = bf.ParseCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Bitfinex CSV file:", err)
}
}
for _, file := range config.Exchanges.Bitstamp.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Bitstamp CSV file:", err)
}
err = bs.ParseCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Bitstamp CSV file:", err)
}
}
for _, file := range config.Exchanges.Bittrex.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Bittrex CSV file:", err)
}
err = btrx.ParseCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Bittrex CSV file:", err)
}
}
cb := coinbase.New()
for _, file := range config.Exchanges.Coinbase.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Coinbase CSV file:", err)
}
err = cb.ParseCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Coinbase CSV file:", err)
}
}
for _, file := range config.Exchanges.CdcApp.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Crypto.com CSV file:", err)
}
err = cdc.ParseCSVAppCrypto(recordFile, *categ)
if err != nil {
log.Fatal("Error parsing Crypto.com CSV file:", err)
}
}
if config.Exchanges.CdcEx.JSON != "" {
recordFile, err := os.Open(config.Exchanges.CdcEx.JSON)
if err != nil {
log.Fatal("Error opening Crypto.com Exchange ExportJS JSON file:", err)
}
err = cdc.ParseJSONExchangeExportJS(recordFile)
if err != nil {
log.Fatal("Error parsing Crypto.com Exchange ExportJS JSON file:", err)
}
}
for _, file := range config.Exchanges.CdcApp.CSV.Transfers {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Crypto.com Exchange Deposit/Withdrawal CSV file:", err)
}
err = cdc.ParseCSVExchangeTransfer(recordFile)
if err != nil {
log.Fatal("Error parsing Crypto.com Exchange Deposit/Withdrawal CSV file:", err)
}
}
for _, file := range config.Exchanges.CdcApp.CSV.Staking {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Crypto.com Exchange Stake CSV file:", err)
}
err = cdc.ParseCSVExchangeStake(recordFile)
if err != nil {
log.Fatal("Error parsing Crypto.com Exchange Stake CSV file:", err)
}
}
for _, file := range config.Exchanges.CdcApp.CSV.Supercharger {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Crypto.com Exchange Supercharger CSV file:", err)
}
err = cdc.ParseCSVExchangeSupercharger(recordFile)
if err != nil {
log.Fatal("Error parsing Crypto.com Exchange Supercharger CSV file:", err)
}
}
for _, file := range config.Exchanges.HitBTC.CSV.Trades {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening HitBTC Trades CSV file:", err)
}
err = hb.ParseCSVTrades(recordFile)
if err != nil {
log.Fatal("Error parsing HitBTC Trades CSV file:", err)
}
}
for _, file := range config.Exchanges.HitBTC.CSV.Transfers {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening HitBTC Transactions CSV file:", err)
}
err = hb.ParseCSVTransactions(recordFile)
if err != nil {
log.Fatal("Error parsing HitBTC Transactions CSV file:", err)
}
}
for _, file := range config.Exchanges.Kraken.CSV.Transfers {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Kraken CSV file:", err)
}
err = kr.ParseCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Kraken CSV file:", err)
}
}
ll := ledgerlive.New()
for _, file := range config.Wallets.LedgerLive.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening LedgerLive CSV file:", err)
}
err = ll.ParseCSV(recordFile, *categ)
if err != nil {
log.Fatal("Error parsing LedgerLive CSV file:", err)
}
}
lb := localbitcoin.New()
for _, file := range config.Exchanges.LocalBitcoins.CSV.Trades {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Local Bitcoin Trade CSV file:", err)
}
err = lb.ParseTradeCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Local Bitcoin Trade CSV file:", err)
}
}
for _, file := range config.Exchanges.LocalBitcoins.CSV.Transfers {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Local Bitcoin Transfer CSV file:", err)
}
err = lb.ParseTransferCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Local Bitcoin Transfer CSV file:", err)
}
}
mc := mycelium.New()
for _, file := range config.Wallets.MyCelium.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening MyCelium CSV file:", err)
}
err = mc.ParseCSV(recordFile)
if err != nil {
log.Fatal("Error parsing MyCelium CSV file:", err)
}
}
pl := poloniex.New()
for _, file := range config.Exchanges.Poloniex.CSV.Deposits {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Poloniex Deposits CSV file:", err)
}
err = pl.ParseDepositsCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Poloniex Deposits CSV file:", err)
}
}
for _, file := range config.Exchanges.Poloniex.CSV.Distributions {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Poloniex Distributions CSV file:", err)
}
err = pl.ParseDistributionsCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Poloniex Distributions CSV file:", err)
}
}
for _, file := range config.Exchanges.Poloniex.CSV.Trades {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Poloniex Trades CSV file:", err)
}
err = pl.ParseTradesCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Poloniex Trades CSV file:", err)
}
}
for _, file := range config.Exchanges.Poloniex.CSV.Withdrawals {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Poloniex Withdrawals CSV file:", err)
}
err = pl.ParseWithdrawalsCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Poloniex Withdrawals CSV file:", err)
}
}
revo := revolut.New()
for _, file := range config.Exchanges.Revolut.CSV.All {
recordFile, err := os.Open(file)
if err != nil {
log.Fatal("Error opening Revolut CSV file:", err)
}
err = revo.ParseCSV(recordFile)
if err != nil {
log.Fatal("Error parsing Revolut CSV file:", err)
}
}
// Wait for API access to finish
if config.Exchanges.Bitstamp.API.Key != "" && config.Exchanges.Bitstamp.API.Secret != "" {
err := bs.WaitFinish()
if err != nil {
log.Fatal("Error getting BiTstamp API TXs:", err)
}
}
if config.Exchanges.CdcEx.API.Key != "" && config.Exchanges.CdcEx.API.Secret != "" {
err := cdc.WaitFinish()
if err != nil {
log.Fatal("Error getting Crypto.com Exchange API TXs:", err)
}
}
if config.Exchanges.Binance.API.Key != "" && config.Exchanges.Binance.API.Secret != "" {
err := b.WaitFinish()
if err != nil {
log.Fatal("Error getting Binance API TXs:", err)
}
}
if config.Exchanges.HitBTC.API.Key != "" && config.Exchanges.HitBTC.API.Secret != "" {
err := hb.WaitFinish()
if err != nil {
log.Fatal("Error getting HitBTC API TXs:", err)
}
}
if len(config.Blockchains.ETH.CSV)+len(config.Blockchains.ETH.Addresses) > 0 {
err := ethsc.WaitFinish()
if err != nil {
log.Fatal("Error parsing Ethereum CSV file:", err)
}
}
if config.Exchanges.Bittrex.API.Key != "" && config.Exchanges.Bittrex.API.Secret != "" {
err := btrx.WaitFinish()
if err != nil {
log.Fatal("Error getting Bittrex API TXs:", err)
}
}
if config.Exchanges.Kraken.API.Key != "" && config.Exchanges.Kraken.API.Secret != "" {
err := kr.WaitFinish()
if err != nil {
log.Fatal("Error getting Kraken API TXs:", err)
}
}
if len(config.Blockchains.BTC.CSV)+len(config.Blockchains.BTC.Addresses) > 0 {
err := blkst.WaitFinish()
if err != nil {
log.Fatal("Error parsing Bitcoin CSV file:", err)
}
if config.Options.Bcd {
blkst.DetectBCD(btc)
}
if config.Options.Bch {
blkst.DetectBCH(btc)
}
if config.Options.Btg {
blkst.DetectBTG(btc)
}
if config.Options.Lbtc {
blkst.DetectLBTC(btc)
}
}
if config.Options.Export3916 {
sources := make(source.Sources)
sources.Add(b.Sources)
sources.Add(bf.Sources)
sources.Add(bs.Sources)
sources.Add(btrx.Sources)
sources.Add(cb.Sources)
sources.Add(cdc.Sources)
sources.Add(hb.Sources)
sources.Add(kr.Sources)
sources.Add(lb.Sources)
sources.Add(pl.Sources)
sources.Add(revo.Sources)
err = sources.ToXlsx("3916.xlsx", loc)
if err != nil {
log.Fatal(err)
}
}
// create Global Wallet up to Date
global := make(wallet.TXsByCategory)
global.Add(b.TXsByCategory)
global.Add(bf.TXsByCategory)
global.Add(bs.TXsByCategory)
global.Add(btrx.TXsByCategory)
global.Add(cb.TXsByCategory)
global.Add(cdc.TXsByCategory)
global.Add(hb.TXsByCategory)
global.Add(kr.TXsByCategory)
global.Add(ll.TXsByCategory)
global.Add(lb.TXsByCategory)
global.Add(mc.TXsByCategory)
global.Add(pl.TXsByCategory)
global.Add(revo.TXsByCategory)
global.Add(ethsc.TXsByCategory)
global.Add(btc.TXsByCategory)
global.Add(bc.TXsByCategory)
fmt.Print("Merging Deposits with Withdrawals into Transfers...")
global.FindTransfers()
fmt.Println("Finished")
if config.Options.ExportStock {
global.StockToXlsx("stock.xlsx")
}
var totalCommercialRebates, totalInterests, totalReferrals decimal.Decimal
if config.Options.Export2086 || config.Options.Display2086 {
fmt.Print("Look for CashIn and CashOut...")
totalCommercialRebates, totalInterests, totalReferrals = global.FindCashInOut(config.Options.Native)
fmt.Println("Finished")
}
global.SortTXsByDate(true)
if config.Options.Stats {
global.PrintStats(config.Options.Native, totalCommercialRebates, totalInterests, totalReferrals)
}
if config.Options.Check {
global.CheckConsistency(loc)
}
// Debug
if config.Options.TxsDisplay != "" {
if config.Options.TxsDisplay == "Alls" {
global.Println(config.Options.CurrencyFilter)
} else {
global[config.Options.TxsDisplay].Println("Category "+config.Options.TxsDisplay, config.Options.CurrencyFilter)
}
}
// Construct global wallet up to date
filterDate, err := time.ParseInLocation("2006-01-02T15:04:05", config.Options.Date, loc)
if err != nil {
log.Fatal("Error parsing Date:", err)
}
globalWallet := global.GetWallets(filterDate, false, !config.Options.Exact)
globalWallet.Println("Global Crypto", "")
globalWalletTotalValue, err := globalWallet.CalculateTotalValue(config.Options.Native)
if err != nil {
log.Fatal("Error Calculating Global Wallet:", err)
} else {
globalWalletTotalValue.Amount = globalWalletTotalValue.Amount.RoundBank(0)
fmt.Print("Total Value : ")
globalWalletTotalValue.Println("")
}
if config.Options.Export2086 || config.Options.Display2086 {
var c2086 Cerfa2086
fmt.Print("Début du calcul pour le 2086...")
err = c2086.CalculatePVMV(global, config.Options.Native, loc)
fmt.Println("Fini")
if err != nil {
log.Fatal(err)
}
if config.Options.Display2086 {
c2086.Println()
}
if config.Options.Export2086 {
c2086.ToXlsx("2086.xlsx", config.Options.Native)
}
}
os.Exit(0)
}