forked from provinzio/CoinTaxman
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook.py
774 lines (672 loc) · 26.3 KB
/
book.py
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
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
# CoinTaxman
# Copyright (C) 2021 Carsten Docktor <https://github.com/provinzio>
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
import csv
import datetime
import decimal
import logging
import re
from pathlib import Path
from typing import Optional
import config
import misc
import transaction as tr
from core import kraken_asset_map
from price_data import PriceData
log = logging.getLogger(__name__)
class Book:
def __init__(self, price_data: PriceData) -> None:
self.price_data = price_data
self.operations: list[tr.Operation] = []
def __bool__(self) -> bool:
return bool(self.operations)
def append_operation(
self,
operation: str,
utc_time: datetime.datetime,
platform: str,
change: decimal.Decimal,
coin: str,
row: int,
file_path: Path,
) -> None:
try:
Op = getattr(tr, operation)
except AttributeError:
log.warning(
"Could not recognize operation `%s` in %s file `%s:%i`.",
operation,
platform,
file_path,
row,
)
return
o = Op(utc_time, platform, change, coin, row, file_path)
self.operations.append(o)
def _read_binance(self, file_path: Path) -> None:
platform = "binance"
operation_mapping = {
"Distribution": "Airdrop",
"Savings Interest": "CoinLendInterest",
"Savings purchase": "CoinLend",
"Savings Principal redemption": "CoinLendEnd",
"Commission History": "Commission",
"Commission Fee Shared With You": "Commission",
"Launchpool Interest": "StakingInterest",
"Cash Voucher distribution": "Airdrop",
"Super BNB Mining": "StakingInterest",
"Liquid Swap add": "CoinLend",
"Liquid Swap remove": "CoinLendEnd",
}
with open(file_path, encoding="utf8") as f:
reader = csv.reader(f)
# Skip header.
next(reader)
for _utc_time, account, operation, coin, _change, remark in reader:
row = reader.line_num
# Parse data.
utc_time = datetime.datetime.strptime(_utc_time, "%Y-%m-%d %H:%M:%S")
utc_time = utc_time.replace(tzinfo=datetime.timezone.utc)
change = misc.force_decimal(_change)
operation = operation_mapping.get(operation, operation)
if operation in (
"The Easiest Way to Trade",
"Small assets exchange BNB",
"Transaction Related",
"Large OTC trading",
):
operation = "Sell" if change < 0 else "Buy"
change = abs(change)
# Validate data.
assert account == "Spot", (
"Other types than Spot are currently not supported. "
"Please create an Issue or PR."
)
assert operation
assert coin
assert change
# Check for problems.
if remark:
log.warning(
"I may have missed a remark in %s:%i: `%s`.",
file_path,
row,
remark,
)
self.append_operation(
operation, utc_time, platform, change, coin, row, file_path
)
def _read_coinbase(self, file_path: Path) -> None:
platform = "coinbase"
operation_mapping = {
"Receive": "Deposit",
"Send": "Withdraw",
"Coinbase Earn": "Buy",
}
with open(file_path, encoding="utf8") as f:
reader = csv.reader(f)
# Skip header.
try:
assert next(reader) # header line
assert next(reader) == []
assert next(reader) == []
assert next(reader) == []
assert next(reader) == ["Transactions"]
assert next(reader) # user row
assert next(reader) == []
assert next(reader) == [
"Timestamp",
"Transaction Type",
"Asset",
"Quantity Transacted",
"EUR Spot Price at Transaction",
"EUR Subtotal",
"EUR Total (inclusive of fees)",
"EUR Fees",
"Notes",
]
except AssertionError as e:
msg = (
"Unable to read coinbase file: Malformed header. "
f"Skipping {file_path}."
)
e.args += (msg,)
log.exception(e)
return
for (
_utc_time,
operation,
coin,
_change,
_eur_spot,
_eur_subtotal,
_eur_total,
_eur_fee,
remark,
) in reader:
row = reader.line_num
# Parse data.
utc_time = datetime.datetime.strptime(_utc_time, "%Y-%m-%dT%H:%M:%SZ")
utc_time = utc_time.replace(tzinfo=datetime.timezone.utc)
operation = operation_mapping.get(operation, operation)
change = misc.force_decimal(_change)
# Current price from exchange.
eur_spot = misc.force_decimal(_eur_spot)
# Cost without fees.
eur_subtotal = misc.xdecimal(_eur_subtotal)
eur_fee = misc.xdecimal(_eur_fee)
# Validate data.
assert operation
assert coin
assert change
# Save price in our local database for later.
self.price_data.set_price_db(platform, coin, "EUR", utc_time, eur_spot)
if operation == "Convert":
# Parse change + coin from remark, which is
# in format "Converted 0,123 ETH to 0,456 BTC".
match = re.match(
r"^Converted [0-9,\.]+ [A-Z]+ to "
r"(?P<change>[0-9,\.]+) (?P<coin>[A-Z]+)$",
remark,
)
assert match
_convert_change = match.group("change").replace(",", ".")
convert_change = misc.force_decimal(_convert_change)
convert_coin = match.group("coin")
eur_total = misc.force_decimal(_eur_total)
convert_eur_spot = eur_total / convert_change
self.append_operation(
"Sell", utc_time, platform, change, coin, row, file_path
)
self.append_operation(
"Buy",
utc_time,
platform,
convert_change,
convert_coin,
row,
file_path,
)
# Save convert price in local database, too.
self.price_data.set_price_db(
platform, convert_coin, "EUR", utc_time, convert_eur_spot
)
else:
self.append_operation(
operation, utc_time, platform, change, coin, row, file_path
)
if operation == "Sell":
assert isinstance(eur_subtotal, decimal.Decimal)
self.append_operation(
"Buy",
utc_time,
platform,
eur_subtotal,
"EUR",
row,
file_path,
)
elif operation == "Buy":
assert isinstance(eur_subtotal, decimal.Decimal)
self.append_operation(
"Sell",
utc_time,
platform,
eur_subtotal,
"EUR",
row,
file_path,
)
if eur_fee:
self.append_operation(
"Fee", utc_time, platform, eur_fee, "EUR", row, file_path
)
def _read_coinbase_pro(self, file_path: Path) -> None:
platform = "coinbase_pro"
operation_mapping = {
"BUY": "Buy",
"SELL": "Sell",
}
with open(file_path, encoding="utf8") as f:
reader = csv.reader(f)
# Skip header.
next(reader)
for (
portfolio,
trade_id,
product,
operation,
_utc_time,
_size,
size_unit,
_price,
_fee,
total,
price_fee_total_unit,
) in reader:
row = reader.line_num
# Parse data.
utc_time = datetime.datetime.strptime(
_utc_time, "%Y-%m-%dT%H:%M:%S.%fZ"
)
utc_time = utc_time.replace(tzinfo=datetime.timezone.utc)
operation = operation_mapping.get(operation, operation)
size = misc.force_decimal(_size)
price = misc.force_decimal(_price)
fee = misc.xdecimal(_fee)
total_price = size * price
# Unused variables.
del portfolio
del trade_id
del product
del total
# Validate data.
assert operation
assert size
assert size_unit
assert price_fee_total_unit
self.append_operation(
operation, utc_time, platform, size, size_unit, row, file_path
)
if operation == "Sell":
self.append_operation(
"Buy",
utc_time,
platform,
total_price,
price_fee_total_unit,
row,
file_path,
)
elif operation == "Buy":
self.append_operation(
"Sell",
utc_time,
platform,
total_price,
price_fee_total_unit,
row,
file_path,
)
if fee:
self.append_operation(
"Fee",
utc_time,
platform,
fee,
price_fee_total_unit,
row,
file_path,
)
def _read_kraken_trades(self, file_path: Path) -> None:
log.error(
f"{file_path.name}: "
"Looks like this is a Kraken 'Trades' history, "
"but we need the 'Ledgers' history. "
"(See: Wiki - Exchange Kraken)"
)
def _read_kraken_ledgers(self, file_path: Path) -> None:
platform = "kraken"
operation_mapping = {
"spend": "Sell", # Sell ordered via 'Buy Crypto' button
"receive": "Buy", # Buy ordered via 'Buy Crypto' button
"transfer": "Airdrop",
"reward": "StakingInterest",
"deposit": "Deposit",
"withdrawal": "Withdraw",
}
# Need to track state of "duplicate entries"
# for deposits / withdrawals;
# the second deposit and the first withdrawal entry
# need to be skipped.
# dup_state["deposit"] == 0:
# Deposit is broadcast to blockchain
# > Taxable event (is in public trade history)
# dup_state["deposit"] == 1:
# Deposit is credited to Kraken account
# > Skipped
# dup_state["withdrawal"] == 0:
# Withdrawal is requested in Kraken account
# > Skipped
# dup_state["withdrawal"] == 1:
# Withdrawal is broadcast to blockchain
# > Taxable event (is in public trade history)
dup_state = {"deposit": 0, "withdrawal": 0}
dup_skip = {"deposit": 1, "withdrawal": 0}
with open(file_path, encoding="utf8") as f:
reader = csv.reader(f)
# Skip header.
next(reader)
for columns in reader:
num_columns = len(columns)
# Kraken ledgers export format from October 2020 and ongoing
if num_columns == 10:
(
txid,
refid,
_utc_time,
_type,
subtype,
aclass,
_asset,
_amount,
_fee,
balance,
) = columns
# Kraken ledgers export format from September 2020 and before
elif num_columns == 9:
(
txid,
refid,
_utc_time,
_type,
aclass,
_asset,
_amount,
_fee,
balance,
) = columns
else:
raise RuntimeError(
"Unknown Kraken ledgers format: "
"Number of rows do not match known versions."
)
row = reader.line_num
# Skip "duplicate entries" for deposits / withdrawals
if _type in dup_state.keys():
skip = dup_state[_type] == dup_skip[_type]
dup_state[_type] = (dup_state[_type] + 1) % 2
if skip:
continue
# Parse data.
utc_time = datetime.datetime.strptime(_utc_time, "%Y-%m-%d %H:%M:%S")
utc_time = utc_time.replace(tzinfo=datetime.timezone.utc)
change = misc.force_decimal(_amount)
coin = kraken_asset_map.get(_asset, _asset)
fee = misc.force_decimal(_fee)
operation = operation_mapping.get(_type)
if operation is None:
if _type == "trade":
operation = "Sell" if change < 0 else "Buy"
elif _type in ["margin trade", "rollover", "settled"]:
log.error(
f"{file_path}: {row}: Margin trading is "
"currently not supported. "
"Please create an Issue or PR."
)
raise RuntimeError
else:
log.error(
f"{file_path}: {row}: Other order type '{_type}' "
"is currently not supported. "
"Please create an Issue or PR."
)
raise RuntimeError
change = abs(change)
# Validate data.
assert operation
assert coin
assert change
self.append_operation(
operation, utc_time, platform, change, coin, row, file_path
)
if fee != 0:
self.append_operation(
"Fee", utc_time, platform, fee, coin, row, file_path
)
assert dup_state["deposit"] == 0, (
"Orphaned deposit. (Must always come in pairs). " "Is your file corrupted?"
)
assert dup_state["withdrawal"] == 0, (
"Orphaned withdrawal. (Must always come in pairs). "
"Is your file corrupted?"
)
def _read_kraken_ledgers_old(self, file_path: Path) -> None:
self._read_kraken_ledgers(file_path)
def _read_bitpanda_pro_trades(self, file_path: Path) -> None:
"""Reads a trade statement from Bitpanda Pro.
Args:
file_path (Path): Path to Bitpanda trade history.
"""
platform = "bitpanda_pro"
with open(file_path, encoding="utf8") as f:
reader = csv.reader(f)
# skip header
line = next(reader)
line = next(reader)
transaction_file_warn = (
f"{file_path} looks like a Bitpanda transaction file."
" Skipping. Please download the trade history instead."
)
# for transactions, it's currently written "id" (small)
if line[0].startswith("Account id :"):
log.warning(transaction_file_warn)
return
assert line[0].startswith("Account ID:")
line = next(reader)
# empty line - still keep this check in case Bitpanda changes the
# transaction file to match the trade header (casing)
if not line:
log.warning(transaction_file_warn)
return
elif line[0] != "Bitpanda Pro trade history":
log.warning(
f"{file_path} doesn't look like a Bitpanda trade file. Skipping."
)
return
line = next(reader)
assert line == [
"Order ID",
"Trade ID",
"Type",
"Market",
"Amount",
"Amount Currency",
"Price",
"Price Currency",
"Fee",
"Fee Currency",
"Time (UTC)",
]
for (
_order_id,
_trace_id,
operation,
trade_pair,
amount,
amount_currency,
_price,
price_currency,
fee,
fee_currency,
_utc_time,
) in reader:
row = reader.line_num
# trade pair is of form e.g. BTC_EUR
assert [amount_currency, price_currency] == trade_pair.split("_")
# At the time of writing (2021-05-02),
# there were only these two operations
assert operation in ["BUY", "SELL"], "Unsupported operation"
change = misc.force_decimal(amount)
assert change > 0, "Unexpected value for 'Amount' column"
# see _get_price_bitpanda_pro in price_data.py
assert price_currency == "EUR", (
"Only Euro is supported as 'price' currency, "
"since price fetching is not fully implemented yet."
)
# sanity checks
assert (
fee_currency == "BEST"
or (operation == "SELL" and fee_currency == price_currency)
or (operation == "BUY" and fee_currency == amount_currency)
)
# make RFC3339 timestamp ISO 8601 parseable
if _utc_time[-1] == "Z":
_utc_time = _utc_time[:-1] + "+00:00"
# timezone information is already taken care of with this
utc_time = datetime.datetime.fromisoformat(_utc_time)
coin = amount_currency
self.append_operation(
operation.title(), utc_time, platform, change, coin, row, file_path
)
# Save price in our local database for later.
price = misc.force_decimal(_price)
self.price_data.set_price_db(platform, coin, "EUR", utc_time, price)
self.append_operation(
"Fee",
utc_time,
platform,
misc.force_decimal(fee),
fee_currency,
row,
file_path,
)
def detect_exchange(self, file_path: Path) -> Optional[str]:
if file_path.suffix == ".csv":
with open(file_path, encoding="utf8") as f:
reader = csv.reader(f)
header = next(reader, None)
expected_headers = {
"binance": [
"UTC_Time",
"Account",
"Operation",
"Coin",
"Change",
"Remark",
],
"coinbase": [
"You can use this transaction report to inform your "
"likely tax obligations. For US customers, Sells, "
"Converts, and Rewards Income, and Coinbase Earn "
"transactions are taxable events. For final tax "
"obligations, please consult your tax advisor."
],
"coinbase_pro": [
"portfolio",
"trade id",
"product",
"side",
"created at",
"size",
"size unit",
"price",
"fee",
"total",
"price/fee/total unit",
],
"kraken_ledgers_old": [
"txid",
"refid",
"time",
"type",
"aclass",
"asset",
"amount",
"fee",
"balance",
],
"kraken_ledgers": [
"txid",
"refid",
"time",
"type",
"subtype",
"aclass",
"asset",
"amount",
"fee",
"balance",
],
"kraken_trades": [
"txid",
"ordertxid",
"pair",
"time",
"type",
"ordertype",
"price",
"cost",
"fee",
"vol",
"margin",
"misc",
"ledgers",
],
"bitpanda_pro_trades": [
"Disclaimer: All data is without guarantee,"
" errors and changes are reserved."
],
}
for exchange, expected in expected_headers.items():
if header == expected:
return exchange
return None
def read_file(self, file_path: Path) -> None:
"""Import transactions form an account statement.
Detect the exchange of the file. The file will be ignored with a
warning, if the detecting or reading functionality is not implemented.
Args:
file_path (Path): Path to account statment.
"""
assert file_path.is_file()
if exchange := self.detect_exchange(file_path):
try:
read_file = getattr(self, f"_read_{exchange}")
except AttributeError:
log.warning(
f"Unable to read files from the exchange `{exchange}`. "
f"Skipping `{file_path}`."
)
return
log.info("Reading file from exchange %s at %s", exchange, file_path)
read_file(file_path)
else:
log.warning(
f"Unable to detect the exchange of file `{file_path}`. "
"Skipping file."
)
def get_account_statement_paths(self, statements_dir: Path) -> list[Path]:
"""Return file paths of all account statements in `statements_dir`.
Args:
statements_dir (str): Folder in which account statements
will be searched.
Returns:
list[Path]: List of account statement file paths.
"""
file_paths: list[Path] = []
if statements_dir.is_dir():
for file_path in statements_dir.iterdir():
# Ignore .gitkeep and temporary exel files.
filename = file_path.stem
if filename == ".gitkeep" or filename.startswith("~$"):
continue
file_paths.append(file_path)
return file_paths
def read_files(self) -> bool:
"""Read all account statements from the folder specified in the config.
Returns:
bool: Return True if everything went as expected.
"""
paths = self.get_account_statement_paths(config.ACCOUNT_STATMENTS_PATH)
if not paths:
log.warning(
"No account statement files located in %s.",
config.ACCOUNT_STATMENTS_PATH,
)
return False
for file_path in paths:
self.read_file(file_path)
if not bool(self):
log.warning("Unable to import any data.")
return False
return True