-
-
Notifications
You must be signed in to change notification settings - Fork 523
/
decoder.py
789 lines (724 loc) · 38.9 KB
/
decoder.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
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
import logging
from typing import TYPE_CHECKING, Any, Callable, Literal, Optional
from rotkehlchen.accounting.structures.balance import Balance
from rotkehlchen.accounting.structures.evm_event import EvmProduct
from rotkehlchen.accounting.structures.types import HistoryEventSubType, HistoryEventType
from rotkehlchen.assets.asset import Asset
from rotkehlchen.chain.ethereum.modules.aave.constants import CPT_AAVE_V1, CPT_AAVE_V2
from rotkehlchen.chain.ethereum.utils import asset_normalized_value
from rotkehlchen.chain.evm.constants import ETH_SPECIAL_ADDRESS, ZERO_ADDRESS
from rotkehlchen.chain.evm.decoding.interfaces import (
DecoderInterface,
ReloadablePoolsAndGaugesDecoderMixin,
)
from rotkehlchen.chain.evm.decoding.structures import (
DEFAULT_DECODING_OUTPUT,
FAILED_ENRICHMENT_OUTPUT,
DecoderContext,
DecodingOutput,
EnricherContext,
TransferEnrichmentOutput,
)
from rotkehlchen.chain.evm.decoding.types import CounterpartyDetails
from rotkehlchen.chain.evm.decoding.utils import maybe_reshuffle_events
from rotkehlchen.chain.evm.structures import EvmTxReceiptLog
from rotkehlchen.chain.evm.types import string_to_evm_address
from rotkehlchen.constants.assets import A_ETH
from rotkehlchen.constants.resolver import evm_address_to_identifier
from rotkehlchen.errors.asset import UnknownAsset, WrongAssetType
from rotkehlchen.logging import RotkehlchenLogsAdapter
from rotkehlchen.types import CacheType, ChainID, ChecksumEvmAddress, EvmTokenKind, EvmTransaction
from rotkehlchen.utils.misc import hex_or_bytes_to_address, hex_or_bytes_to_int
from .constants import CPT_CURVE
from .curve_cache import query_curve_data, read_curve_pools_and_gauges, save_curve_data_to_cache
if TYPE_CHECKING:
from rotkehlchen.accounting.structures.evm_event import EvmEvent
from rotkehlchen.chain.ethereum.node_inquirer import EthereumInquirer
from rotkehlchen.chain.evm.decoding.base import BaseDecoderTools
from rotkehlchen.user_messages import MessagesAggregator
ADD_LIQUIDITY_EVENTS = {
b'B?d\x95\xa0\x8f\xc6RB\\\xf4\xed\r\x1f\x9e7\xe5q\xd9\xb9R\x9b\x1c\x1c#\xcc\xe7\x80\xb2\xe7\xdf\r', # ADD_LIQUIDITY # noqa: E501
b'&\xf5Z\x85\x08\x1d$\x97N\x85\xc6\xc0\x00E\xd0\xf0E9\x91\xe9Xs\xf5+\xff\r!\xaf@y\xa7h', # ADD_LIQUIDITY_2_ASSETS # noqa: E501
b'?\x19\x15w^\x0c\x9a8\xa5z{\xb7\xf1\xf9\x00_Ho\xb9\x04\xe1\xf8J\xa2\x156MVs\x19\xa5\x8d', # ADD_LIQUIDITY_4_ASSETS # noqa: E501
}
REMOVE_LIQUIDITY_IMBALANCE = {
b'\xb9d\xb7/s\xf5\xef[\xf0\xfd\xc5Y\xb2\xfa\xb9\xa7\xb1*9\xe4x\x17\xa5G\xf1\xf0\xae\xe4\x7f\xeb\xd6\x02',
b'+U\x087\x8d~\x19\xe0\xd5\xfa3\x84\x19\x03G1AlO[!\x9a\x107\x99V\xf7d1\x7f\xd4~',
}
REMOVE_LIQUIDITY_EVENTS = {
*REMOVE_LIQUIDITY_IMBALANCE,
b'|68T\xcc\xf7\x96#A\x1f\x89\x95\xb3b\xbc\xe5\xed\xdf\xf1\x8c\x92~\xdco]\xbb\xb5\xe0X\x19\xa8,', # REMOVE_LIQUIDITY # noqa: E501,
b'\x9e\x96\xdd;\x99z*%~\xecM\xf9\xbbn\xafbn m\xf5\xf5C\xbd\x966\x82\xd1C0\x0b\xe3\x10', # REMOVE_ONE # noqa: E501
b"Z\xd0V\xf2\xe2\x8a\x8c\xec# \x15@k\x846h\xc1\xe3l\xdaY\x81'\xec;\x8cY\xb8\xc7's\xa0", # REMOVE_LIQUIDITY # noqa: E501,
b'\xa4\x9dL\xf0&V\xae\xbf\x8cw\x1fZ\x85\x85c\x8a*\x15\xeel\x97\xcfr\x05\xd4 \x8e\xd7\xc1\xdf%-', # REMOVE_LIQUIDITY_3_ASSETS # noqa: E501
b'\x98x\xca7^\x10o*C\xc3\xb5\x99\xfcbEh\x13\x1cL\x9aK\xa6j\x14V7\x15v;\xe9\xd5\x9d', # REMOVE_LIQUIDITY_4_ASSETS # noqa: E501
}
# Deposit contracts are retrieved from the links below Deposit<pool>:
# https://curve.readthedocs.io/ref-addresses.html#base-pools
# https://curve.readthedocs.io/ref-addresses.html#metapools
#
# The duplicates were found via Etherscan:
# https://etherscan.io/find-similar-contracts?a=0x35796DAc54f144DFBAD1441Ec7C32313A7c29F39
CURVE_DEPOSIT_CONTRACTS = {
string_to_evm_address('0x094d12e5b541784701FD8d65F11fc0598FBC6332'), # curve usdn
string_to_evm_address('0x35796DAc54f144DFBAD1441Ec7C32313A7c29F39'), # curve usdn duplicate
string_to_evm_address('0xB0a0716841F2Fc03fbA72A891B8Bb13584F52F2d'), # curve ust
string_to_evm_address('0x3c8cAee4E09296800f8D29A68Fa3837e2dae4940'), # curve usdp
string_to_evm_address('0xF1f85a74AD6c64315F85af52d3d46bF715236ADc'), # curve usdk
string_to_evm_address('0x6600e98b71dabfD4A8Cac03b302B0189Adb86Afb'), # curve usdk duplicate
string_to_evm_address('0xBE175115BF33E12348ff77CcfEE4726866A0Fbd5'), # curve rsv
string_to_evm_address('0x803A2B40c5a9BB2B86DD630B274Fa2A9202874C2'), # curve musd
string_to_evm_address('0x78CF256256C8089d68Cde634Cf7cDEFb39286470'), # curve musd duplicate
string_to_evm_address('0x1de7f0866e2c4adAC7b457c58Cc25c8688CDa1f2'), # curve linkusd
string_to_evm_address('0xF6bDc2619FFDA72c537Cd9605e0A274Dc48cB1C9'), # curve linkusd duplicate # noqa: E501
string_to_evm_address('0x09672362833d8f703D5395ef3252D4Bfa51c15ca'), # curve husd
string_to_evm_address('0x0a53FaDa2d943057C47A301D25a4D9b3B8e01e8E'), # curve husd duplicate
string_to_evm_address('0x64448B78561690B70E17CBE8029a3e5c1bB7136e'), # curve gusd
string_to_evm_address('0x0aE274c98c0415C0651AF8cF52b010136E4a0082'), # curve gusd duplicate
string_to_evm_address('0x61E10659fe3aa93d036d099405224E4Ac24996d0'), # curve dusd
string_to_evm_address('0xbBC81d23Ea2c3ec7e56D39296F0cbB648873a5d3'), # curve y
string_to_evm_address('0xb6c057591E073249F2D9D88Ba59a46CFC9B59EdB'), # curve busd
string_to_evm_address('0xeB21209ae4C2c9FF2a86ACA31E123764A3B6Bc06'), # curve compound
string_to_evm_address('0xA50cCc70b6a011CffDdf45057E39679379187287'), # curve pax
string_to_evm_address('0xFCBa3E75865d2d561BE8D220616520c171F12851'), # curve susd v2
string_to_evm_address('0x331aF2E331bd619DefAa5DAc6c038f53FCF9F785'), # curve zap
string_to_evm_address('0xac795D2c97e60DF6a99ff1c814727302fD747a80'), # curve usdt
string_to_evm_address('0xC45b2EEe6e09cA176Ca3bB5f7eEe7C47bF93c756'), # curve bbtc
string_to_evm_address('0xd5BCf53e2C81e1991570f33Fa881c49EEa570C8D'), # curve obtc
string_to_evm_address('0x11F419AdAbbFF8d595E7d5b223eee3863Bb3902C'), # curve pbtc
string_to_evm_address('0xaa82ca713D94bBA7A89CEAB55314F9EfFEdDc78c'), # curve tbtc
string_to_evm_address('0xA79828DF1850E8a3A3064576f380D90aECDD3359'), # curve 3pool
string_to_evm_address('0x08780fb7E580e492c1935bEe4fA5920b94AA95Da'), # curve fraxusdc
string_to_evm_address('0x7abdbaf29929e7f8621b757d2a7c04d78d633834'), # curve sbtc
string_to_evm_address('0xA2d40Edbf76C6C0701BA8899e2d059798eBa628e'), # curve sbtc2
}
GAUGE_DEPOSIT = b'\xe1\xff\xfc\xc4\x92=\x04\xb5Y\xf4\xd2\x9a\x8b\xfcl\xda\x04\xeb[\r<F\x07Q\xc2@,\\\\\xc9\x10\x9c' # noqa: E501
GAUGE_WITHDRAW = b'\x88N\xda\xd9\xceo\xa2D\r\x8aT\xcc\x124\x90\xeb\x96\xd2v\x84y\xd4\x9f\xf9\xc76a%\xa9BCd' # noqa: E501
GAUGE_VOTE = b'E\xca\x9aL\x8d\x01\x19\xeb2\x9eX\r(\xfeh\x9eHN\x1b\xe20\xda\x807\xad\xe9T}-%\xcc\x91' # noqa: E501
GAUGE_CONTROLLER = string_to_evm_address('0x2F50D538606Fa9EDD2B11E2446BEb18C9D5846bB')
TOKEN_EXCHANGE = b'\x8b>\x96\xf2\xb8\x89\xfaw\x1cS\xc9\x81\xb4\r\xaf\x00_c\xf67\xf1\x86\x9fppR\xd1Z=\xd9q@' # noqa: E501
TOKEN_EXCHANGE_UNDERLYING = b'\xd0\x13\xca#\xe7ze\x00<,e\x9cTB\xc0\x0c\x80Sq\xb7\xfc\x1e\xbdL lA\xd1Sk\xd9\x0b' # noqa: E501
EXCHANGE_MULTIPLE = b'\x14\xb5a\x17\x8a\xe0\xf3h\xf4\x0f\xaf\xd0H\\Oq)\xeaq\xcd\xc0\x0bL\xe1\xe5\x94\x0f\x9b\xc6Y\xc8\xb2' # noqa: E501
CURVE_SWAP_ROUTER = string_to_evm_address('0x99a58482BD75cbab83b27EC03CA68fF489b5788f')
AAVE_POOLS = {
string_to_evm_address('0xDeBF20617708857ebe4F679508E7b7863a8A8EeE'), # aDAI + aUSDC + aUSDT
string_to_evm_address('0xEB16Ae0052ed37f479f7fe63849198Df1765a733'), # aDAI + aSUSD
}
logger = logging.getLogger(__name__)
log = RotkehlchenLogsAdapter(logger)
def _read_curve_asset(
asset_address: Optional[ChecksumEvmAddress],
chain_id: ChainID,
) -> Optional[Asset]:
"""
A thin wrapper that turns asset address into an asset object.
Object returned here is a pure Asset (not a resolved CryptoAsset) since it is meant only for
comparison with other assets. And to compare with other assets there is no need to resolve.
"""
if asset_address is None:
return None
if asset_address == ETH_SPECIAL_ADDRESS:
return A_ETH
return Asset(evm_address_to_identifier(
address=asset_address,
chain_id=chain_id,
token_type=EvmTokenKind.ERC20,
))
class CurveDecoder(DecoderInterface, ReloadablePoolsAndGaugesDecoderMixin):
def __init__(
self,
ethereum_inquirer: 'EthereumInquirer', # pylint: disable=unused-argument
base_tools: 'BaseDecoderTools',
msg_aggregator: 'MessagesAggregator',
) -> None:
super().__init__(
evm_inquirer=ethereum_inquirer,
base_tools=base_tools,
msg_aggregator=msg_aggregator,
)
ReloadablePoolsAndGaugesDecoderMixin.__init__(
self,
evm_inquirer=ethereum_inquirer,
cache_type_to_check_for_freshness=CacheType.CURVE_LP_TOKENS,
query_data_method=query_curve_data,
save_data_to_cache_method=save_curve_data_to_cache,
read_data_from_cache_method=read_curve_pools_and_gauges,
)
self.ethereum = ethereum_inquirer
@property
def pools(self) -> dict[ChecksumEvmAddress, list[ChecksumEvmAddress]]:
assert isinstance(self.cache_data[0], dict), 'CurveDecoder cache_data[0] is not a dict'
return self.cache_data[0]
def _decode_curve_remove_events(
self,
transaction: EvmTransaction,
tx_log: EvmTxReceiptLog,
decoded_events: list['EvmEvent'],
user_or_contract_address: ChecksumEvmAddress,
) -> DecodingOutput:
"""Decode information related to withdrawing assets from curve pools"""
withdrawal_events: list[EvmEvent] = []
return_event: Optional[EvmEvent] = None
for event in decoded_events:
try:
crypto_asset = event.asset.resolve_to_crypto_asset()
except (UnknownAsset, WrongAssetType):
self.notify_user(event=event, counterparty=CPT_CURVE)
continue
if ( # Withdraw eth
event.event_type == HistoryEventType.RECEIVE and
event.event_subtype == HistoryEventSubType.NONE and
event.asset == A_ETH and
event.location_label == user_or_contract_address
):
event.event_type = HistoryEventType.WITHDRAWAL
event.event_subtype = HistoryEventSubType.REMOVE_ASSET
event.counterparty = CPT_CURVE
event.notes = f'Remove {event.balance.amount} {crypto_asset.symbol} from the curve pool' # noqa: E501
withdrawal_events.append(event)
elif ( # Withdraw send wrapped
event.event_type == HistoryEventType.SPEND and
event.event_subtype == HistoryEventSubType.NONE and
event.location_label == transaction.from_address and
(
user_or_contract_address == event.location_label or
user_or_contract_address in CURVE_DEPOSIT_CONTRACTS or
tx_log.topics[0] in REMOVE_LIQUIDITY_IMBALANCE
)
):
event.event_type = HistoryEventType.SPEND
event.event_subtype = HistoryEventSubType.RETURN_WRAPPED
event.counterparty = CPT_CURVE
event.notes = f'Return {event.balance.amount} {crypto_asset.symbol}'
return_event = event
elif ( # Withdraw receive asset
tx_log.address in self.pools and
transaction.from_address == user_or_contract_address
):
notes = f'Remove {event.balance.amount} {crypto_asset.symbol} from {tx_log.address} curve pool' # noqa: E501
if ( # Raw event
event.event_type == HistoryEventType.RECEIVE and
event.event_subtype == HistoryEventSubType.NONE and
event.location_label == user_or_contract_address
):
event.event_type = HistoryEventType.WITHDRAWAL
event.event_subtype = HistoryEventSubType.REMOVE_ASSET
event.counterparty = CPT_CURVE
event.notes = notes
withdrawal_events.append(event)
elif ( # Is already decoded by aave
tx_log.address in AAVE_POOLS and
event.event_type == HistoryEventType.WITHDRAWAL and
event.event_subtype == HistoryEventSubType.REMOVE_ASSET and
event.location_label == tx_log.address and
event.counterparty in (CPT_AAVE_V1, CPT_AAVE_V2)
):
event.location_label = user_or_contract_address
event.notes = notes
event.counterparty = CPT_CURVE
withdrawal_events.append(event)
# Two conditions below handle withdrawals from metapools via zap contracts. In this
# case there are 2 RemoveLiquidity events emitted. One for withdrawing from the
# metapool and one for withdrawing from the underlying pool. This means that this
# decoding method is executed twice. Between 2 RemoveLiquidity events an extra transfer
# can occur, and we have to handle it together with the events that were decoded in the
# first run (i.e. those that already have proper event type and counterparty set) in
# order to have extra_data properly populated for accounting. Same happens for deposits
# in another method below.
elif (
event.event_type == HistoryEventType.WITHDRAWAL and
event.event_subtype == HistoryEventSubType.REMOVE_ASSET and
event.counterparty == CPT_CURVE
):
withdrawal_events.append(event)
elif (
event.event_type == HistoryEventType.SPEND and
event.event_subtype == HistoryEventSubType.RETURN_WRAPPED and
event.counterparty == CPT_CURVE
):
return_event = event
# Make sure that the order is the following:
# 1. Return pool token event
# 2. Withdrawal 1
# 3. Withdrawal 2
# etc.
if return_event is None or len(withdrawal_events) == 0:
# for deposit zap contracts, this is handled during post decoding
if user_or_contract_address in CURVE_DEPOSIT_CONTRACTS and return_event is not None:
return DecodingOutput(matched_counterparty=CPT_CURVE)
log.error(
f'Expected to see a return pool token event and '
f'withdrawal events for a curve pool, but have not found them. '
f'Tx_hash: {transaction.tx_hash.hex()} '
f'User address: {user_or_contract_address}',
)
return DEFAULT_DECODING_OUTPUT
self._sort_events(
action_type='removal',
return_or_receive_event=return_event,
withdrawal_or_deposit_events=withdrawal_events,
all_events=decoded_events,
)
return DEFAULT_DECODING_OUTPUT
def _decode_curve_deposit_events(
self,
transaction: EvmTransaction,
tx_log: EvmTxReceiptLog,
decoded_events: list['EvmEvent'],
user_or_contract_address: ChecksumEvmAddress,
) -> DecodingOutput:
"""Decode information related to depositing assets in curve pools"""
deposit_events: list[EvmEvent] = []
receive_event: Optional[EvmEvent] = None
for event in decoded_events:
try:
crypto_asset = event.asset.resolve_to_crypto_asset()
except (UnknownAsset, WrongAssetType):
self.notify_user(event=event, counterparty=CPT_CURVE)
continue
if ( # Deposit ETH
event.event_type == HistoryEventType.SPEND and
event.event_subtype == HistoryEventSubType.NONE and
event.asset == A_ETH and
event.location_label == user_or_contract_address
):
event.event_type = HistoryEventType.DEPOSIT
event.event_subtype = HistoryEventSubType.DEPOSIT_ASSET
event.counterparty = CPT_CURVE
event.notes = f'Deposit {event.balance.amount} {crypto_asset.symbol} in curve pool'
deposit_events.append(event)
elif ( # Deposit receive pool token
event.event_type == HistoryEventType.RECEIVE and
event.event_subtype == HistoryEventSubType.NONE and
event.location_label == user_or_contract_address and
tx_log.address in self.pools
):
event.event_type = HistoryEventType.RECEIVE
event.event_subtype = HistoryEventSubType.RECEIVE_WRAPPED
event.counterparty = CPT_CURVE
event.notes = f'Receive {event.balance.amount} {crypto_asset.symbol} after depositing in curve pool {tx_log.address}' # noqa: E501
receive_event = event
elif ( # deposit give asset
event.event_type == HistoryEventType.SPEND and
event.event_subtype == HistoryEventSubType.NONE and
(
event.location_label == user_or_contract_address or
user_or_contract_address in CURVE_DEPOSIT_CONTRACTS
) and
tx_log.address in self.pools
):
event.event_type = HistoryEventType.DEPOSIT
event.event_subtype = HistoryEventSubType.DEPOSIT_ASSET
event.counterparty = CPT_CURVE
event.notes = f'Deposit {event.balance.amount} {crypto_asset.symbol} in curve pool {tx_log.address}' # noqa: E501
deposit_events.append(event)
elif (
event.event_type == HistoryEventType.DEPOSIT and
event.event_subtype == HistoryEventSubType.DEPOSIT_ASSET and
event.counterparty == CPT_CURVE
):
deposit_events.append(event)
elif (
event.event_type == HistoryEventType.RECEIVE and
event.event_subtype == HistoryEventSubType.RECEIVE_WRAPPED and
event.counterparty == CPT_CURVE
):
receive_event = event
# Make sure that the order is the following:
# 1. Receive pool token event
# 2. Deposit 1
# 3. Deposit 2
# etc.
if receive_event is None or len(deposit_events) == 0:
# for deposit zap contracts, this is handled during post decoding
if user_or_contract_address in CURVE_DEPOSIT_CONTRACTS and len(deposit_events) > 0:
return DecodingOutput(matched_counterparty=CPT_CURVE)
log.error(
f'Expected to see a receive pool token event and deposit '
f'events for a curve pool, but have not found them. '
f'Tx_hash: {transaction.tx_hash.hex()} '
f'User address: {user_or_contract_address}',
)
return DEFAULT_DECODING_OUTPUT
self._sort_events(
action_type='addition',
return_or_receive_event=receive_event,
withdrawal_or_deposit_events=deposit_events,
all_events=decoded_events,
)
return DEFAULT_DECODING_OUTPUT
def _decode_curve_trades(self, context: DecoderContext) -> DecodingOutput:
"""Decode curve trades made via single pools or curve swap router
First determine:
- `spender_address`
- `receiver_address`
- `sold_token_address`
- `bought_token_address`
- `raw_sold_amount`
- `raw_bought_amount`
Then create assets if `sold_token_address` and `bought_token_address` were found.
Then match and label events.
Then reshuffle events to make sure that spend and receive are consecutive.
Note that `sold_token_address` and `bought_token_address` are not always found (e.g.
when pool for some reason is not present in our cache). If tokens that were swapped are
detected then we use them when iterating over `decoded_events` list and matching transfers.
If they are not detected then conditions when matching transfer events are a bit broader.
"""
# These are nullable because in case a curve pool is not stored in our cache or if it
# is a swap in a metapool (TOKEN_EXCHANGE_UNDERLYING) we will skip token check.
sold_token_address: Optional[ChecksumEvmAddress] = None
bought_token_address: Optional[ChecksumEvmAddress] = None
swapping_contract: ChecksumEvmAddress
if context.tx_log.topics[0] in (TOKEN_EXCHANGE, TOKEN_EXCHANGE_UNDERLYING):
pool_address = context.tx_log.address
swapping_contract = pool_address
# When a single pool is used, spender and receiver is always the same
spender_address = receiver_address = hex_or_bytes_to_address(context.tx_log.topics[1])
sold_token_id = hex_or_bytes_to_int(context.tx_log.data[:32])
raw_sold_amount = hex_or_bytes_to_int(context.tx_log.data[32:64])
bought_token_id = hex_or_bytes_to_int(context.tx_log.data[64:96])
raw_bought_amount = hex_or_bytes_to_int(context.tx_log.data[96:128])
if (
context.tx_log.topics[0] == TOKEN_EXCHANGE and
pool_address in self.pools and
len(self.pools[pool_address]) > max(sold_token_id, bought_token_id) # Make sure that tokens of the pool are cached # noqa: E501
):
sold_token_address = self.pools[pool_address][sold_token_id]
bought_token_address = self.pools[pool_address][bought_token_id]
else: # EXCHANGE_MULTIPLE
swapping_contract = CURVE_SWAP_ROUTER
spender_address = hex_or_bytes_to_address(context.tx_log.topics[1])
receiver_address = hex_or_bytes_to_address(context.tx_log.topics[2])
raw_sold_amount = hex_or_bytes_to_int(context.tx_log.data[-64:-32])
raw_bought_amount = hex_or_bytes_to_int(context.tx_log.data[-32:])
# Curve swap router logs route (a list of addresses) that was used. Route consists of
# 9 elements. Consider X a number of pools that was used. Then the structure can be
# described in the following way:
# At 0 index: Address of the sold token (token that goes in the router)
# From 1 to X indices: Addresses of pools that were used
# At X + 1 index: Address of the bought token (token that comes from the router)
# From X + 2 to 8 indices: Unused elements (zero addresses)
# Here we read only addresses of token in and token out.
sold_token_address = hex_or_bytes_to_address(context.tx_log.data[:32])
for i in range(1, 9): # Starting from 1 because at 0 is `sold_token_address`
address = hex_or_bytes_to_address(context.tx_log.data[32 * i:32 * (i + 1)])
if address == ZERO_ADDRESS:
break
bought_token_address = address
sold_asset = _read_curve_asset(sold_token_address, self.evm_inquirer.chain_id)
bought_asset = _read_curve_asset(bought_token_address, self.evm_inquirer.chain_id)
spend_event: Optional[EvmEvent] = None
receive_event: Optional[EvmEvent] = None
for event in context.decoded_events:
if event.address != swapping_contract:
continue
crypto_asset = event.asset.resolve_to_crypto_asset()
if (
event.location_label == spender_address and
event.event_type == HistoryEventType.SPEND and
event.balance.amount == asset_normalized_value(amount=raw_sold_amount, asset=crypto_asset) and # noqa: E501
(sold_asset is None or event.asset == sold_asset)
):
event.event_type = HistoryEventType.TRADE
event.event_subtype = HistoryEventSubType.SPEND
event.notes = f'Swap {event.balance.amount} {crypto_asset.symbol} in curve'
event.counterparty = CPT_CURVE
spend_event = event
elif (
event.location_label == receiver_address and
event.event_type == HistoryEventType.RECEIVE and
event.balance.amount == asset_normalized_value(amount=raw_bought_amount, asset=crypto_asset) and # noqa: E501
(bought_asset is None or event.asset == bought_asset)
):
event.event_type = HistoryEventType.TRADE
event.event_subtype = HistoryEventSubType.RECEIVE
event.notes = f'Receive {event.balance.amount} {crypto_asset.symbol} as the result of a swap in curve' # noqa: E501
event.counterparty = CPT_CURVE
receive_event = event
if spend_event is not None and receive_event is not None:
# Just to make sure that spend and receive events are consecutive
maybe_reshuffle_events(ordered_events=[spend_event, receive_event], events_list=context.decoded_events) # noqa: E501
else:
log.debug(
f'Did not find spend and receive events for a curve swap. '
f'{context.transaction.tx_hash.hex()}. Probably some aggregator was used and '
f'decoding needs to happen in the aggregator-specific decoder.',
)
return DEFAULT_DECODING_OUTPUT
def _decode_pool_events(self, context: DecoderContext) -> DecodingOutput:
if context.tx_log.topics[0] in REMOVE_LIQUIDITY_EVENTS:
# it can either be the user or a deposit zap contract
user_or_contract_address = hex_or_bytes_to_address(context.tx_log.topics[1])
return self._decode_curve_remove_events(
tx_log=context.tx_log,
transaction=context.transaction,
decoded_events=context.decoded_events,
user_or_contract_address=user_or_contract_address,
)
if context.tx_log.topics[0] in ADD_LIQUIDITY_EVENTS:
# it can either be the user or a deposit zap contract
user_or_contract_address = hex_or_bytes_to_address(context.tx_log.topics[1])
return self._decode_curve_deposit_events(
transaction=context.transaction,
tx_log=context.tx_log,
decoded_events=context.decoded_events,
user_or_contract_address=user_or_contract_address,
)
if context.tx_log.topics[0] in (
TOKEN_EXCHANGE,
TOKEN_EXCHANGE_UNDERLYING,
EXCHANGE_MULTIPLE,
):
return self._decode_curve_trades(context=context)
return DEFAULT_DECODING_OUTPUT
def _decode_curve_gauge_votes(self, context: DecoderContext) -> DecodingOutput:
if context.tx_log.topics[0] != GAUGE_VOTE:
return DEFAULT_DECODING_OUTPUT
user_address = hex_or_bytes_to_address(context.tx_log.data[32:64])
if not self.base.is_tracked(user_address):
return DEFAULT_DECODING_OUTPUT
user_note = ''
if user_address != context.transaction.from_address:
user_note = f' from {user_address}'
gauge_address = hex_or_bytes_to_address(context.tx_log.data[64:96])
vote_weight = hex_or_bytes_to_int(context.tx_log.data[96:128])
if vote_weight == 0:
verb = 'Reset vote'
weight_note = ''
else:
verb = 'Vote'
weight_note = f' with {vote_weight / 100}% voting power'
event = self.base.make_event_from_transaction(
transaction=context.transaction,
tx_log=context.tx_log,
event_type=HistoryEventType.INFORMATIONAL,
event_subtype=HistoryEventSubType.GOVERNANCE,
asset=A_ETH,
balance=Balance(),
location_label=context.transaction.from_address,
notes=f'{verb}{user_note} for {gauge_address} curve gauge{weight_note}',
address=gauge_address,
counterparty=CPT_CURVE,
product=EvmProduct.GAUGE,
)
return DecodingOutput(event=event, refresh_balances=False)
def _decode_gauge_events(self, context: DecoderContext) -> DecodingOutput:
if context.tx_log.topics[0] not in (GAUGE_DEPOSIT, GAUGE_WITHDRAW):
return DEFAULT_DECODING_OUTPUT
provider = hex_or_bytes_to_address(context.tx_log.topics[1])
gauge_address = context.tx_log.address
raw_amount = hex_or_bytes_to_int(context.tx_log.data)
found_event_modifying_balances = False
for event in context.decoded_events:
crypto_asset = event.asset.resolve_to_crypto_asset()
if (
event.location_label == provider and
event.address == gauge_address and
event.balance.amount == asset_normalized_value(amount=raw_amount, asset=crypto_asset) # noqa: E501
):
event.counterparty = CPT_CURVE
event.product = EvmProduct.GAUGE
found_event_modifying_balances = True
if context.tx_log.topics[0] == GAUGE_DEPOSIT:
event.event_type = HistoryEventType.DEPOSIT
event.event_subtype = HistoryEventSubType.DEPOSIT_ASSET
event.notes = f'Deposit {event.balance.amount} {crypto_asset.symbol} into {gauge_address} curve gauge' # noqa: E501
else: # Withdraw
event.event_type = HistoryEventType.WITHDRAWAL
event.event_subtype = HistoryEventSubType.REMOVE_ASSET
event.notes = f'Withdraw {event.balance.amount} {crypto_asset.symbol} from {gauge_address} curve gauge' # noqa: E501
return DecodingOutput(refresh_balances=found_event_modifying_balances)
def _handle_post_decoding(
self,
transaction: EvmTransaction,
decoded_events: list['EvmEvent'],
all_logs: list[EvmTxReceiptLog], # pylint: disable=unused-argument
) -> list['EvmEvent']:
"""This method handles post decoding for curve events made through the deposit contracts.
This is required because certain events are not available when `AddLiquidity` or `RemoveLiquidity` occurs.
An example of such is when liquidity is removed, the transfer event of the token removed comes after the
`RemoveLiquidity` event.
""" # noqa: E501
for tx_log in all_logs:
if (
tx_log.topics[0] in ADD_LIQUIDITY_EVENTS and
hex_or_bytes_to_address(tx_log.topics[1]) in CURVE_DEPOSIT_CONTRACTS
):
self._handle_zap_contracts_liquidity_addition(
transaction=transaction,
decoded_events=decoded_events,
)
break # We only need to handle zap contracts once
if (
tx_log.topics[0] in REMOVE_LIQUIDITY_EVENTS and
hex_or_bytes_to_address(tx_log.topics[1]) in CURVE_DEPOSIT_CONTRACTS
):
self._handle_zap_contracts_liquidity_removal(
pool_address=tx_log.address,
transaction=transaction,
decoded_events=decoded_events,
)
break # We only need to handle zap contracts once
return decoded_events # noop. just return the decoded events.
def _handle_zap_contracts_liquidity_addition(
self,
transaction: EvmTransaction,
decoded_events: list['EvmEvent'],
) -> None:
"""
This method completes the decoding of liquidity addition made through
Curve zap contracts such as e.g. Curve Deposit USDN, etc.
"""
receive_event_idx, deposit_events = None, []
for idx, event in enumerate(decoded_events):
if (
event.event_type == HistoryEventType.DEPOSIT and
event.event_subtype == HistoryEventSubType.DEPOSIT_ASSET and
event.counterparty == CPT_CURVE
):
deposit_events.append(event)
# search for the event of the wrapped token received after deposit
elif (
event.event_type == HistoryEventType.RECEIVE and
event.event_subtype == HistoryEventSubType.NONE and
event.location_label == transaction.from_address and
(
event.address in CURVE_DEPOSIT_CONTRACTS or
event.address == ZERO_ADDRESS or
event.address in self.pools
)
):
receive_event_idx = idx
crypto_asset = event.asset.resolve_to_crypto_asset()
event.event_type = HistoryEventType.RECEIVE
event.event_subtype = HistoryEventSubType.RECEIVE_WRAPPED
event.counterparty = CPT_CURVE
event.notes = f'Receive {event.balance.amount} {crypto_asset.symbol} after depositing in a curve pool' # noqa: E501
if receive_event_idx is not None and len(deposit_events) > 0:
self._sort_events(
action_type='addition',
return_or_receive_event=decoded_events[receive_event_idx],
withdrawal_or_deposit_events=deposit_events,
all_events=decoded_events,
)
return
log.error(
f'Expected to see a receive pool token event and deposit events for a curve pool, '
f'but have not found them. Tx_hash: {transaction.tx_hash.hex()}',
)
def _handle_zap_contracts_liquidity_removal(
self,
pool_address: ChecksumEvmAddress,
transaction: EvmTransaction,
decoded_events: list['EvmEvent'],
) -> None:
"""
This method completes the decoding of liquidity removal made through
Curve zap contracts such as e.g. Curve Deposit USDN, etc.
"""
return_event_idx, withdrawal_events = None, []
for idx, event in enumerate(decoded_events):
if (
event.event_type == HistoryEventType.SPEND and
event.event_subtype == HistoryEventSubType.RETURN_WRAPPED and
event.counterparty == CPT_CURVE and
event.extra_data is None
):
return_event_idx = idx
elif ( # find receive events from the deposit contract.
event.event_type == HistoryEventType.RECEIVE and
event.event_subtype == HistoryEventSubType.NONE and
event.location_label == transaction.from_address and
(
event.address in CURVE_DEPOSIT_CONTRACTS or
event.address in self.pools
)
):
crypto_asset = event.asset.resolve_to_crypto_asset()
event.event_type = HistoryEventType.WITHDRAWAL
event.event_subtype = HistoryEventSubType.REMOVE_ASSET
event.counterparty = CPT_CURVE
event.notes = f'Remove {event.balance.amount} {crypto_asset.symbol} from {pool_address} curve pool' # noqa: E501
withdrawal_events.append(event)
if return_event_idx is not None and len(withdrawal_events) > 0:
self._sort_events(
action_type='removal',
return_or_receive_event=decoded_events[return_event_idx],
withdrawal_or_deposit_events=withdrawal_events,
all_events=decoded_events,
)
return
log.error(
f'Expected to see a return pool token event and withdrawal events '
f'for a curve pool, but have not found them. '
f'Tx_hash: {transaction.tx_hash.hex()}',
)
def _maybe_enrich_curve_transfers(self, context: EnricherContext) -> TransferEnrichmentOutput:
"""
May raise:
- UnknownAsset
- WrongAssetType
"""
source_address = hex_or_bytes_to_address(context.tx_log.topics[1])
if (
context.event.event_type == HistoryEventType.RECEIVE and
context.event.event_subtype == HistoryEventSubType.NONE and
source_address in self.gauges
):
crypto_asset = context.event.asset.resolve_to_crypto_asset()
context.event.event_subtype = HistoryEventSubType.REWARD
context.event.notes = f'Receive {context.event.balance.amount} {crypto_asset.symbol} rewards from {source_address} curve gauge' # noqa: E501
context.event.counterparty = CPT_CURVE
return TransferEnrichmentOutput(matched_counterparty=CPT_CURVE)
return FAILED_ENRICHMENT_OUTPUT
@staticmethod
def _sort_events(
action_type: Literal['addition', 'removal'],
return_or_receive_event: 'EvmEvent',
withdrawal_or_deposit_events: list['EvmEvent'],
all_events: list['EvmEvent'],
) -> list['EvmEvent']:
"""
This method sorts the events in the expected order and adds
extra info to the in/out event depending on the action_type.
"""
if action_type == 'addition':
return_or_receive_event.extra_data = {
'deposit_events_num': len(withdrawal_or_deposit_events),
}
else: # can only be removal
return_or_receive_event.extra_data = {
'withdrawal_events_num': len(withdrawal_or_deposit_events),
}
maybe_reshuffle_events(
ordered_events=[return_or_receive_event] + withdrawal_or_deposit_events,
events_list=all_events,
)
return all_events
# -- DecoderInterface methods
@staticmethod
def possible_products() -> dict[str, list[EvmProduct]]:
return {
CPT_CURVE: [EvmProduct.GAUGE],
}
def addresses_to_decoders(self) -> dict[ChecksumEvmAddress, tuple[Any, ...]]:
mapping: dict[ChecksumEvmAddress, tuple[Any, ...]] = {
address: (self._decode_pool_events,)
for address in self.pools
}
mapping.update({ # addresses of pools and gauges don't intersect, so combining like this is fine # noqa: E501
gauge_address: (self._decode_gauge_events,)
for gauge_address in self.gauges
})
mapping[CURVE_SWAP_ROUTER] = (self._decode_pool_events,)
mapping[GAUGE_CONTROLLER] = (self._decode_curve_gauge_votes,)
return mapping
def post_decoding_rules(self) -> dict[str, list[tuple[int, Callable]]]:
return {CPT_CURVE: [(0, self._handle_post_decoding)]}
def enricher_rules(self) -> list[Callable]:
return [
self._maybe_enrich_curve_transfers,
]
@staticmethod
def counterparties() -> tuple[CounterpartyDetails, ...]:
return (CounterpartyDetails(identifier=CPT_CURVE, label='Curve.fi', image='curve.png'),)