/
accountant.py
146 lines (135 loc) · 6.95 KB
/
accountant.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
from collections import defaultdict
from typing import TYPE_CHECKING, Dict, List, cast
from rotkehlchen.accounting.mixins.event import AccountingEventType
from rotkehlchen.accounting.structures.base import HistoryBaseEntry, get_tx_event_type_identifier
from rotkehlchen.accounting.structures.types import HistoryEventSubType, HistoryEventType
from rotkehlchen.chain.ethereum.accounting.interfaces import ModuleAccountantInterface
from rotkehlchen.chain.ethereum.accounting.structures import TxEventSettings, TxMultitakeTreatment
from rotkehlchen.constants import ZERO
from rotkehlchen.constants.assets import A_DAI
from rotkehlchen.fval import FVal
from rotkehlchen.types import ChecksumEthAddress
from .constants import CPT_DSR, CPT_MIGRATION, CPT_VAULT
if TYPE_CHECKING:
from rotkehlchen.accounting.pot import AccountingPot
class MakerdaoAccountant(ModuleAccountantInterface):
def reset(self) -> None:
self.vault_balances: Dict[str, FVal] = defaultdict(FVal)
self.dsr_balances: Dict[ChecksumEthAddress, FVal] = defaultdict(FVal)
def _process_vault_dai_generation(
self,
pot: 'AccountingPot', # pylint: disable=unused-argument
event: HistoryBaseEntry,
other_events: List[HistoryBaseEntry], # pylint: disable=unused-argument
) -> None:
cdp_id = event.extra_data['cdp_id'] # type: ignore # this event should have extra data
self.vault_balances[cdp_id] += event.balance.amount
def _process_vault_dai_payback(
self,
pot: 'AccountingPot', # pylint: disable=unused-argument
event: HistoryBaseEntry,
other_events: List[HistoryBaseEntry], # pylint: disable=unused-argument
) -> None:
cdp_id = event.extra_data['cdp_id'] # type: ignore # this event should have extra_data
self.vault_balances[cdp_id] -= event.balance.amount
if self.vault_balances[cdp_id] < ZERO:
loss = -1 * self.vault_balances[cdp_id]
pot.add_spend(
event_type=AccountingEventType.TRANSACTION_EVENT,
notes=f'Lost {loss} DAI as debt during payback to CDP {cdp_id}',
location=event.location,
timestamp=event.get_timestamp_in_sec(),
asset=A_DAI,
amount=loss,
taxable=True,
extra_data={'tx_hash': event.serialized_event_identifier},
)
self.vault_balances[cdp_id] = ZERO
def _process_dsr_deposit(
self,
pot: 'AccountingPot', # pylint: disable=unused-argument
event: HistoryBaseEntry,
other_events: List[HistoryBaseEntry], # pylint: disable=unused-argument
) -> None:
address = cast(ChecksumEthAddress, event.location_label) # should always exist
self.dsr_balances[address] += event.balance.amount
def _process_dsr_withdraw(
self,
pot: 'AccountingPot', # pylint: disable=unused-argument
event: HistoryBaseEntry,
other_events: List[HistoryBaseEntry], # pylint: disable=unused-argument
) -> None:
address = cast(ChecksumEthAddress, event.location_label) # should always exist
self.dsr_balances[address] -= event.balance.amount
if self.dsr_balances[address] < ZERO:
profit = -1 * self.dsr_balances[address]
pot.add_acquisition(
event_type=AccountingEventType.TRANSACTION_EVENT,
notes=f'Gained {profit} DAI from Makerdao DSR',
location=event.location,
timestamp=event.get_timestamp_in_sec(),
asset=A_DAI,
amount=profit,
taxable=True,
extra_data={'tx_hash': event.serialized_event_identifier},
)
self.dsr_balances[address] = ZERO
def event_settings(self, pot: 'AccountingPot') -> Dict[str, TxEventSettings]: # pylint: disable=unused-argument # noqa: E501
"""Being defined at function call time is fine since this function is called only once"""
# TODO: How can we count here loss from debt and gain from DSR? We need to keep state
return { # vault collateral deposit
get_tx_event_type_identifier(HistoryEventType.DEPOSIT, HistoryEventSubType.DEPOSIT_ASSET, CPT_VAULT): TxEventSettings( # noqa: E501
taxable=False,
count_entire_amount_spend=False,
count_cost_basis_pnl=False,
method='spend',
take=1,
), # vault collateral withdraw
get_tx_event_type_identifier(HistoryEventType.WITHDRAWAL, HistoryEventSubType.REMOVE_ASSET, CPT_VAULT): TxEventSettings( # noqa: E501
taxable=False,
count_entire_amount_spend=False,
count_cost_basis_pnl=False,
method='acquisition',
take=1,
), # payback DAI to vault
get_tx_event_type_identifier(HistoryEventType.SPEND, HistoryEventSubType.PAYBACK_DEBT, CPT_VAULT): TxEventSettings( # noqa: E501
taxable=False,
count_entire_amount_spend=False,
count_cost_basis_pnl=False,
method='spend',
take=1,
accountant_cb=self._process_vault_dai_payback,
), # generate DAI from vault
get_tx_event_type_identifier(HistoryEventType.WITHDRAWAL, HistoryEventSubType.GENERATE_DEBT, CPT_VAULT): TxEventSettings( # noqa: E501
taxable=False,
count_entire_amount_spend=False,
count_cost_basis_pnl=False,
method='acquisition',
take=1,
accountant_cb=self._process_vault_dai_generation,
), # Deposit DAI in the DSR
get_tx_event_type_identifier(HistoryEventType.DEPOSIT, HistoryEventSubType.DEPOSIT_ASSET, CPT_DSR): TxEventSettings( # noqa: E501
taxable=False,
count_entire_amount_spend=False,
count_cost_basis_pnl=False,
method='spend',
take=1,
accountant_cb=self._process_dsr_deposit,
), # Withdraw DAI from the DSR
get_tx_event_type_identifier(HistoryEventType.WITHDRAWAL, HistoryEventSubType.REMOVE_ASSET, CPT_DSR): TxEventSettings( # noqa: E501
taxable=False,
count_entire_amount_spend=False,
count_cost_basis_pnl=False,
method='acquisition',
take=1,
accountant_cb=self._process_dsr_withdraw,
), # Migrate SAI to DAI
get_tx_event_type_identifier(HistoryEventType.MIGRATE, HistoryEventSubType.SPEND, CPT_MIGRATION): TxEventSettings( # noqa: E501
taxable=False,
count_entire_amount_spend=False,
count_cost_basis_pnl=False,
method='spend',
take=2,
multitake_treatment=TxMultitakeTreatment.SWAP,
),
}