forked from BittyTax/BittyTax
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransactions.py
470 lines (394 loc) · 18.4 KB
/
transactions.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
# -*- coding: utf-8 -*-
# (c) Nano Nano Ltd 2019
import sys
import copy
from colorama import Fore, Style
from tqdm import tqdm
from .config import config
from .record import TransactionRecord
class TransactionHistory(object):
def __init__(self, transaction_records, value_asset):
self.value_asset = value_asset
self.transactions = []
if config.debug:
print("%ssplit transaction records" % Fore.CYAN)
for tr in tqdm(transaction_records,
unit='tr',
desc="%ssplit transaction records%s" % (Fore.CYAN, Fore.GREEN),
disable=bool(config.debug or not sys.stdout.isatty())):
if config.debug:
print("%ssplit: TR %s" % (Fore.MAGENTA, tr))
self.get_all_values(tr)
# Attribute the fee value (allowable cost) to the buy, the sell or both
if tr.fee and tr.fee.disposal and tr.fee.proceeds:
if tr.buy and tr.buy.acquisition and tr.sell and tr.sell.disposal:
if tr.buy.asset in config.fiat_list:
tr.sell.fee_value = tr.fee.proceeds
tr.sell.fee_fixed = tr.fee.proceeds_fixed
elif tr.sell.asset in config.fiat_list:
tr.buy.fee_value = tr.fee.proceeds
tr.buy.fee_fixed = tr.fee.proceeds_fixed
else:
# Crypto-to-crypto trades
if config.trade_allowable_cost_type == config.TRADE_ALLOWABLE_COST_BUY:
tr.buy.fee_value = tr.fee.proceeds
tr.buy.fee_fixed = tr.fee.proceeds_fixed
elif config.trade_allowable_cost_type == config.TRADE_ALLOWABLE_COST_SELL:
tr.sell.fee_value = tr.fee.proceeds
tr.sell.fee_fixed = tr.fee.proceeds_fixed
else:
# Split fee between both
tr.buy.fee_value = tr.fee.proceeds / 2
tr.buy.fee_fixed = tr.fee.proceeds_fixed
tr.sell.fee_value = tr.fee.proceeds - tr.buy.fee_value
tr.sell.fee_fixed = tr.fee.proceeds_fixed
elif tr.buy and tr.buy.acquisition:
tr.buy.fee_value = tr.fee.proceeds
tr.buy.fee_fixed = tr.fee.proceeds_fixed
elif tr.sell and tr.sell.disposal:
tr.sell.fee_value = tr.fee.proceeds
tr.sell.fee_fixed = tr.fee.proceeds_fixed
else:
# Special case for transfer fees
if config.transfer_fee_allowable_cost:
tr.fee.fee_value = tr.fee.proceeds
tr.fee.fee_fixed = tr.fee.proceeds_fixed
if tr.buy and (tr.buy.quantity or tr.buy.fee_value) and \
tr.buy.asset not in config.fiat_list:
tr.buy.set_tid()
self.transactions.append(tr.buy)
if config.debug:
print("%ssplit: %s" % (Fore.GREEN, tr.buy))
if tr.sell and (tr.sell.quantity or tr.sell.fee_value) and \
tr.sell.asset not in config.fiat_list:
tr.sell.set_tid()
self.transactions.append(tr.sell)
if config.debug:
print("%ssplit: %s" % (Fore.GREEN, tr.sell))
if tr.fee and tr.fee.quantity and tr.fee.asset not in config.fiat_list:
tr.fee.set_tid()
self.transactions.append(tr.fee)
if config.debug:
print("%ssplit: %s" % (Fore.GREEN, tr.fee))
if config.debug:
print("%ssplit: total transactions=%d" % (Fore.CYAN, len(self.transactions)))
def get_all_values(self, tr):
if tr.buy and tr.buy.acquisition and tr.buy.cost is None:
if tr.sell:
(tr.buy.cost,
tr.buy.cost_fixed) = self.which_asset_value(tr)
else:
(tr.buy.cost,
tr.buy.cost_fixed) = self.value_asset.get_value(tr.buy.asset,
tr.buy.timestamp,
tr.buy.quantity)
if tr.sell and tr.sell.disposal and tr.sell.proceeds is None:
if tr.buy:
tr.sell.proceeds = tr.buy.cost
tr.sell.proceeds_fixed = tr.buy.cost_fixed
else:
(tr.sell.proceeds,
tr.sell.proceeds_fixed) = self.value_asset.get_value(tr.sell.asset,
tr.sell.timestamp,
tr.sell.quantity)
if tr.fee and tr.fee.disposal and tr.fee.proceeds is None:
if tr.fee.asset not in config.fiat_list:
if tr.buy and tr.buy.asset == tr.fee.asset:
if tr.buy.cost and tr.buy.quantity:
price = tr.buy.cost / tr.buy.quantity
tr.fee.proceeds = tr.fee.quantity * price
tr.fee.proceeds_fixed = tr.buy.cost_fixed
else:
(tr.fee.proceeds,
tr.fee.proceeds_fixed) = self.value_asset.get_value(tr.fee.asset,
tr.fee.timestamp,
tr.fee.quantity)
elif tr.sell and tr.sell.asset == tr.fee.asset:
if tr.sell.proceeds and tr.sell.quantity:
price = tr.sell.proceeds / tr.sell.quantity
tr.fee.proceeds = tr.fee.quantity * price
tr.fee.proceeds_fixed = tr.sell.proceeds_fixed
else:
(tr.fee.proceeds,
tr.fee.proceeds_fixed) = self.value_asset.get_value(tr.fee.asset,
tr.fee.timestamp,
tr.fee.quantity)
else:
# Must be a 3rd cryptoasset
(tr.fee.proceeds,
tr.fee.proceeds_fixed) = self.value_asset.get_value(tr.fee.asset,
tr.fee.timestamp,
tr.fee.quantity)
else:
# Fee paid in fiat
(tr.fee.proceeds,
tr.fee.proceeds_fixed) = self.value_asset.get_value(tr.fee.asset,
tr.fee.timestamp,
tr.fee.quantity)
def which_asset_value(self, tr):
if config.trade_asset_type == config.TRADE_ASSET_TYPE_BUY:
if tr.buy.cost is None:
value, fixed = self.value_asset.get_value(tr.buy.asset,
tr.buy.timestamp,
tr.buy.quantity)
else:
value, fixed = tr.buy.cost, tr.buy.cost_fixed
elif config.trade_asset_type == config.TRADE_ASSET_TYPE_SELL:
if tr.sell.proceeds is None:
value, fixed = self.value_asset.get_value(tr.sell.asset,
tr.sell.timestamp,
tr.sell.quantity)
else:
value, fixed = tr.sell.proceeds, tr.sell.proceeds_fixed
else:
pos_sell_asset = pos_buy_asset = len(config.asset_priority) + 1
if tr.sell.asset in config.asset_priority:
pos_sell_asset = config.asset_priority.index(tr.sell.asset)
if tr.buy.asset in config.asset_priority:
pos_buy_asset = config.asset_priority.index(tr.buy.asset)
if pos_sell_asset <= pos_buy_asset:
if tr.sell.proceeds is None:
value, fixed = self.value_asset.get_value(tr.sell.asset,
tr.sell.timestamp,
tr.sell.quantity)
else:
value, fixed = tr.sell.proceeds, tr.sell.proceeds_fixed
else:
if tr.buy.cost is None:
value, fixed = self.value_asset.get_value(tr.buy.asset,
tr.buy.timestamp,
tr.buy.quantity)
else:
value, fixed = tr.buy.cost, tr.buy.cost_fixed
return value, fixed
class TransactionBase(object):
def __init__(self, t_type, asset, quantity):
self.tid = None
self.t_record = None
self.t_type = t_type
self.asset = asset
self.quantity = quantity
self.fee_value = None
self.fee_fixed = True
self.wallet = None
self.timestamp = None
self.note = None
self.matched = False
self.pooled = []
def set_tid(self):
self.tid = self.t_record.set_tid()
def _format_tid(self):
return "%s.%s" % (self.tid[0], self.tid[1])
def _format_quantity(self):
if self.quantity is None:
return ''
return '{:0,f}'.format(self.quantity.normalize())
def _format_asset(self):
if sys.version_info[0] < 3:
return self.asset.decode('utf8')
return self.asset
def _format_wallet(self):
if sys.version_info[0] < 3:
return self.wallet.decode('utf8')
return self.wallet
def _format_note(self):
if self.note:
if sys.version_info[0] < 3:
return "'%s' " % self.note.decode('utf8')
return "'%s' " % self.note
return ''
def _format_pooled(self, bold=False):
if self.pooled:
return " %s(%s)%s" % (
Style.BRIGHT if bold else '',
len(self.pooled),
Style.NORMAL if bold else '')
return ''
def _format_fee(self):
if self.fee_value is not None:
return " + fee=%s%s %s" % (
'' if self.fee_fixed else '~',
config.sym() + '{:0,.2f}'.format(self.fee_value),
config.ccy)
return ''
def _format_timestamp(self):
if self.timestamp.microsecond:
return self.timestamp.strftime('%Y-%m-%dT%H:%M:%S.%f %Z')
return self.timestamp.strftime('%Y-%m-%dT%H:%M:%S %Z')
def __eq__(self, other):
return (self.asset, self.timestamp) == (other.asset, other.timestamp)
def __ne__(self, other):
return not self == other
def __lt__(self, other):
return (self.asset, self.timestamp) < (other.asset, other.timestamp)
def __deepcopy__(self, memo):
cls = self.__class__
result = cls.__new__(cls)
memo[id(self)] = result
for k, v in self.__dict__.items():
if k == 't_record':
# Keep reference to the transaction record
setattr(result, k, v)
else:
setattr(result, k, copy.deepcopy(v, memo))
return result
class Buy(TransactionBase):
TYPE_DEPOSIT = TransactionRecord.TYPE_DEPOSIT
TYPE_MINING = TransactionRecord.TYPE_MINING
TYPE_STAKING = TransactionRecord.TYPE_STAKING
TYPE_INTEREST = TransactionRecord.TYPE_INTEREST
TYPE_DIVIDEND = TransactionRecord.TYPE_DIVIDEND
TYPE_INCOME = TransactionRecord.TYPE_INCOME
TYPE_GIFT_RECEIVED = TransactionRecord.TYPE_GIFT_RECEIVED
TYPE_AIRDROP = TransactionRecord.TYPE_AIRDROP
TYPE_TRADE = TransactionRecord.TYPE_TRADE
ACQUISITION_TYPES = {TYPE_MINING, TYPE_STAKING, TYPE_INTEREST, TYPE_DIVIDEND,
TYPE_INCOME, TYPE_GIFT_RECEIVED, TYPE_AIRDROP, TYPE_TRADE}
def __init__(self, t_type, buy_quantity, buy_asset, buy_value):
super(Buy, self).__init__(t_type, buy_asset, buy_quantity)
self.acquisition = bool(self.t_type in self.ACQUISITION_TYPES)
self.cost = None
self.cost_fixed = False
if self.acquisition and buy_value is not None:
self.cost = buy_value
self.cost_fixed = True
def __iadd__(self, other):
if not self.pooled:
self.pooled.append(copy.deepcopy(self))
# Pool buys
if self.asset != other.asset:
raise Exception
self.quantity += other.quantity
self.cost += other.cost
if self.fee_value is not None and other.fee_value is not None:
self.fee_value += other.fee_value
elif self.fee_value is None and other.fee_value is not None:
self.fee_value = other.fee_value
if other.timestamp < self.timestamp:
# Keep timestamp of earliest transaction
self.timestamp = other.timestamp
if other.wallet != self.wallet:
self.wallet = "<pooled>"
if other.cost_fixed != self.cost_fixed:
self.cost_fixed = False
if other.fee_fixed != self.fee_fixed:
self.fee_fixed = False
if other.note != self.note:
self.note = "<pooled>"
self.pooled.append(other)
return self
def split_buy(self, sell_quantity):
remainder = copy.deepcopy(self)
self.cost = self.cost * (sell_quantity / self.quantity)
if self.fee_value:
self.fee_value = self.fee_value * (sell_quantity / self.quantity)
self.quantity = sell_quantity
self.set_tid()
remainder.cost = remainder.cost - self.cost
if self.fee_value:
remainder.fee_value = remainder.fee_value - self.fee_value
remainder.quantity = remainder.quantity - sell_quantity
remainder.set_tid()
return remainder
def _format_cost(self):
if self.cost is not None:
return " (%s%s %s)" % (
'=' if self.cost_fixed else '~',
config.sym() + '{:0,.2f}'.format(self.cost),
config.ccy)
return ''
def __str__(self, pooled_bold=False, quantity_bold=False):
return "%s%s %s%s %s %s%s%s%s '%s' %s %s[TID:%s]%s" % (
type(self).__name__.upper(),
'*' if not self.acquisition else '',
self.t_type,
Style.BRIGHT if quantity_bold else '',
self._format_quantity(),
self._format_asset(),
Style.NORMAL if quantity_bold else '',
self._format_cost(),
self._format_fee(),
self._format_wallet(),
self._format_timestamp(),
self._format_note(),
self._format_tid(),
self._format_pooled(pooled_bold))
class Sell(TransactionBase):
TYPE_WITHDRAWAL = TransactionRecord.TYPE_WITHDRAWAL
TYPE_SPEND = TransactionRecord.TYPE_SPEND
TYPE_GIFT_SENT = TransactionRecord.TYPE_GIFT_SENT
TYPE_GIFT_SPOUSE = TransactionRecord.TYPE_GIFT_SPOUSE
TYPE_CHARITY_SENT = TransactionRecord.TYPE_CHARITY_SENT
TYPE_LOST = TransactionRecord.TYPE_LOST
TYPE_TRADE = TransactionRecord.TYPE_TRADE
DISPOSAL_TYPES = {TYPE_SPEND, TYPE_GIFT_SENT, TYPE_GIFT_SPOUSE, TYPE_CHARITY_SENT,
TYPE_LOST, TYPE_TRADE}
def __init__(self, t_type, sell_quantity, sell_asset, sell_value):
super(Sell, self).__init__(t_type, sell_asset, sell_quantity)
self.disposal = bool(self.t_type in self.DISPOSAL_TYPES)
self.proceeds = None
self.proceeds_fixed = False
if self.disposal and sell_value is not None:
self.proceeds = sell_value
self.proceeds_fixed = True
def __iadd__(self, other):
if not self.pooled:
self.pooled.append(copy.deepcopy(self))
# Pool sells
if self.asset != other.asset:
raise Exception
self.quantity += other.quantity
self.proceeds += other.proceeds
if self.fee_value is not None and other.fee_value is not None:
self.fee_value += other.fee_value
elif self.fee_value is None and other.fee_value is not None:
self.fee_value = other.fee_value
if other.timestamp > self.timestamp:
# Keep timestamp of latest transaction
self.timestamp = other.timestamp
if other.wallet != self.wallet:
self.wallet = "<pooled>"
if other.proceeds_fixed != self.proceeds_fixed:
self.proceeds_fixed = False
if other.fee_fixed != self.fee_fixed:
self.fee_fixed = False
if other.note != self.note:
self.note = "<pooled>"
self.pooled.append(other)
return self
def split_sell(self, buy_quantity):
remainder = copy.deepcopy(self)
self.proceeds = self.proceeds * (buy_quantity / self.quantity)
if self.fee_value:
self.fee_value = self.fee_value * (buy_quantity / self.quantity)
self.quantity = buy_quantity
self.set_tid()
remainder.proceeds = remainder.proceeds - self.proceeds
if self.fee_value:
remainder.fee_value = remainder.fee_value - self.fee_value
remainder.quantity = remainder.quantity - buy_quantity
remainder.set_tid()
return remainder
def _format_proceeds(self):
if self.proceeds is not None:
return " (%s%s %s)" % (
'=' if self.proceeds_fixed else '~',
config.sym() + '{:0,.2f}'.format(self.proceeds),
config.ccy)
return ''
def __str__(self, pooled_bold=False, quantity_bold=False):
return "%s%s %s%s %s %s%s%s%s '%s' %s %s[TID:%s]%s" % (
type(self).__name__.upper(),
'*' if not self.disposal else '',
self.t_type,
Style.BRIGHT if quantity_bold else '',
self._format_quantity(),
self._format_asset(),
Style.NORMAL if quantity_bold else '',
self._format_proceeds(),
self._format_fee(),
self._format_wallet(),
self._format_timestamp(),
self._format_note(),
self._format_tid(),
self._format_pooled(pooled_bold))