eth-portfolio 1.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.

Potentially problematic release.


This version of eth-portfolio might be problematic. Click here for more details.

Files changed (47) hide show
  1. eth_portfolio/__init__.py +16 -0
  2. eth_portfolio/_argspec.py +42 -0
  3. eth_portfolio/_cache.py +116 -0
  4. eth_portfolio/_config.py +3 -0
  5. eth_portfolio/_db/__init__.py +0 -0
  6. eth_portfolio/_db/decorators.py +147 -0
  7. eth_portfolio/_db/entities.py +204 -0
  8. eth_portfolio/_db/utils.py +595 -0
  9. eth_portfolio/_decimal.py +122 -0
  10. eth_portfolio/_decorators.py +71 -0
  11. eth_portfolio/_exceptions.py +67 -0
  12. eth_portfolio/_ledgers/__init__.py +0 -0
  13. eth_portfolio/_ledgers/address.py +892 -0
  14. eth_portfolio/_ledgers/portfolio.py +327 -0
  15. eth_portfolio/_loaders/__init__.py +33 -0
  16. eth_portfolio/_loaders/balances.py +78 -0
  17. eth_portfolio/_loaders/token_transfer.py +214 -0
  18. eth_portfolio/_loaders/transaction.py +379 -0
  19. eth_portfolio/_loaders/utils.py +59 -0
  20. eth_portfolio/_shitcoins.py +212 -0
  21. eth_portfolio/_utils.py +286 -0
  22. eth_portfolio/_ydb/__init__.py +0 -0
  23. eth_portfolio/_ydb/token_transfers.py +136 -0
  24. eth_portfolio/address.py +382 -0
  25. eth_portfolio/buckets.py +181 -0
  26. eth_portfolio/constants.py +58 -0
  27. eth_portfolio/portfolio.py +629 -0
  28. eth_portfolio/protocols/__init__.py +66 -0
  29. eth_portfolio/protocols/_base.py +107 -0
  30. eth_portfolio/protocols/convex.py +17 -0
  31. eth_portfolio/protocols/dsr.py +31 -0
  32. eth_portfolio/protocols/lending/__init__.py +49 -0
  33. eth_portfolio/protocols/lending/_base.py +57 -0
  34. eth_portfolio/protocols/lending/compound.py +185 -0
  35. eth_portfolio/protocols/lending/liquity.py +110 -0
  36. eth_portfolio/protocols/lending/maker.py +105 -0
  37. eth_portfolio/protocols/lending/unit.py +47 -0
  38. eth_portfolio/protocols/liquity.py +16 -0
  39. eth_portfolio/py.typed +0 -0
  40. eth_portfolio/structs/__init__.py +43 -0
  41. eth_portfolio/structs/modified.py +69 -0
  42. eth_portfolio/structs/structs.py +637 -0
  43. eth_portfolio/typing.py +1460 -0
  44. eth_portfolio-1.1.0.dist-info/METADATA +174 -0
  45. eth_portfolio-1.1.0.dist-info/RECORD +47 -0
  46. eth_portfolio-1.1.0.dist-info/WHEEL +5 -0
  47. eth_portfolio-1.1.0.dist-info/top_level.txt +1 -0
@@ -0,0 +1,892 @@
1
+ """
2
+ This module defines the :class:`~eth_portfolio.AddressLedgerBase`, :class:`~eth_portfolio.TransactionsList`,
3
+ :class:`~eth_portfolio.AddressTransactionsLedger`, :class:`~eth_portfolio.InternalTransfersList`,
4
+ :class:`~eth_portfolio.AddressInternalTransfersLedger`, :class:`~eth_portfolio.TokenTransfersList`,
5
+ and :class:`~eth_portfolio.AddressTokenTransfersLedger` classes. These classes manage and interact with ledger entries
6
+ such as transactions, internal transfers, and token transfers associated with Ethereum addresses within the `eth-portfolio` system.
7
+
8
+ These classes leverage the `a_sync` library to support both synchronous and asynchronous operations, allowing efficient data gathering
9
+ and processing without blocking, thus improving the overall responsiveness and performance of portfolio operations.
10
+ """
11
+
12
+ from abc import ABCMeta, abstractmethod
13
+ from asyncio import Lock, Queue, create_task, gather, sleep
14
+ from collections import defaultdict
15
+ from functools import partial
16
+ from http import HTTPStatus
17
+ from itertools import product
18
+ from logging import getLogger
19
+ from typing import (
20
+ TYPE_CHECKING,
21
+ AsyncGenerator,
22
+ AsyncIterator,
23
+ Callable,
24
+ Generic,
25
+ List,
26
+ NoReturn,
27
+ Optional,
28
+ Tuple,
29
+ Type,
30
+ TypeVar,
31
+ Union,
32
+ )
33
+
34
+ import a_sync
35
+ import dank_mids
36
+ import eth_retry
37
+ from aiohttp import ClientResponseError
38
+ from async_lru import alru_cache
39
+ from brownie import chain
40
+ from dank_mids.eth import TraceFilterParams
41
+ from eth_typing import ChecksumAddress, HexStr
42
+ from evmspec import FilterTrace
43
+ from evmspec.structs.receipt import Status
44
+ from evmspec.structs.trace import call, reward
45
+ from pandas import DataFrame # type: ignore
46
+ from tqdm import tqdm
47
+ from y import ERC20, Network
48
+ from y._decorators import stuck_coro_debugger
49
+ from y.datatypes import Block
50
+ from y.utils.events import BATCH_SIZE
51
+
52
+ from eth_portfolio import _exceptions, _loaders
53
+ from eth_portfolio._cache import cache_to_disk
54
+ from eth_portfolio._decorators import set_end_block_if_none
55
+ from eth_portfolio._loaders.transaction import get_nonce_at_block, load_transaction
56
+ from eth_portfolio._utils import PandableList, _AiterMixin, get_buffered_chain_height
57
+ from eth_portfolio._ydb.token_transfers import TokenTransfers
58
+ from eth_portfolio.structs import InternalTransfer, TokenTransfer, Transaction
59
+
60
+ if TYPE_CHECKING:
61
+ from eth_portfolio.address import PortfolioAddress
62
+
63
+ logger = getLogger(__name__)
64
+
65
+
66
+ T = TypeVar("T")
67
+
68
+ _LedgerEntryList = TypeVar(
69
+ "_LedgerEntryList", "TransactionsList", "InternalTransfersList", "TokenTransfersList"
70
+ )
71
+ PandableLedgerEntryList = Union["TransactionsList", "InternalTransfersList", "TokenTransfersList"]
72
+
73
+
74
+ class AddressLedgerBase(
75
+ a_sync.ASyncGenericBase, _AiterMixin[T], Generic[_LedgerEntryList, T], metaclass=ABCMeta
76
+ ):
77
+ """
78
+ Abstract base class for address ledgers in the eth-portfolio system.
79
+ """
80
+
81
+ __slots__ = (
82
+ "address",
83
+ "asynchronous",
84
+ "cached_from",
85
+ "cached_thru",
86
+ "load_prices",
87
+ "objects",
88
+ "portfolio_address",
89
+ "_lock",
90
+ )
91
+
92
+ def __init__(self, portfolio_address: "PortfolioAddress") -> None:
93
+ """
94
+ Initializes the AddressLedgerBase instance.
95
+
96
+ Args:
97
+ portfolio_address: The :class:`~eth_portfolio.address.PortfolioAddress` this ledger belongs to.
98
+ """
99
+
100
+ # TODO replace the following line with an abc implementation.
101
+ # assert isinstance(portfolio_address, PortfolioAddress), f"address must be a PortfolioAddress. try passing in PortfolioAddress({portfolio_address}) instead."
102
+
103
+ super().__init__()
104
+
105
+ self.portfolio_address = portfolio_address
106
+ """
107
+ The portfolio address this ledger belongs to.
108
+ """
109
+
110
+ self.address = self.portfolio_address.address
111
+ """
112
+ The Ethereum address being managed.
113
+ """
114
+
115
+ self.asynchronous = self.portfolio_address.asynchronous
116
+ """
117
+ Flag indicating if the operations are asynchronous.
118
+ """
119
+
120
+ self.load_prices = self.portfolio_address.load_prices
121
+ """
122
+ Indicates if price loading is enabled.
123
+ """
124
+
125
+ self.objects: _LedgerEntryList = self._list_type()
126
+ """
127
+ _LedgerEntryList: List of ledger entries.
128
+ """
129
+
130
+ # NOTE: The following two properties will both be ints once the cache has contents
131
+ self.cached_from: int = None # type: ignore
132
+ """
133
+ The block from which all entries for this ledger have been loaded into memory.
134
+ """
135
+
136
+ self.cached_thru: int = None # type: ignore
137
+ """
138
+ The block through which all entries for this ledger have been loaded into memory.
139
+ """
140
+
141
+ self._lock = Lock()
142
+ """
143
+ Lock: Lock for synchronizing access to ledger entries.
144
+ """
145
+
146
+ def __hash__(self) -> int:
147
+ """
148
+ Returns the hash of the address.
149
+
150
+ Returns:
151
+ The hash value.
152
+ """
153
+ return hash(self.address)
154
+
155
+ def __repr__(self) -> str:
156
+ return f"<{type(self).__name__} for {self.address} at {hex(id(self))}>"
157
+
158
+ @property
159
+ @abstractmethod
160
+ def _list_type(self) -> Type[_LedgerEntryList]:
161
+ """
162
+ Type of list used to store ledger entries.
163
+ """
164
+ ...
165
+
166
+ @property
167
+ def _start_block(self) -> int:
168
+ """
169
+ Returns the starting block for the portfolio address.
170
+
171
+ Returns:
172
+ The starting block number.
173
+ """
174
+ return self.portfolio_address._start_block
175
+
176
+ async def _get_and_yield(self, start_block: Block, end_block: Block) -> AsyncGenerator[T, None]:
177
+ """
178
+ Yields ledger entries between the specified blocks.
179
+
180
+ Args:
181
+ start_block: The starting block number.
182
+ end_block: The ending block number.
183
+
184
+ Yields:
185
+ AsyncGenerator[T, None]: An async generator of ledger entries.
186
+ """
187
+ num_yielded = 0
188
+
189
+ async def unblock_loop() -> None:
190
+ """
191
+ Let the event loop run at least once for every 100
192
+ objects yielded so it doesn't get too congested.
193
+ """
194
+ nonlocal num_yielded
195
+ num_yielded += 1
196
+ if num_yielded % 100 == 0:
197
+ await sleep(0)
198
+
199
+ if self.objects and end_block and self.objects[-1].block_number > end_block:
200
+ for ledger_entry in self.objects:
201
+ block = ledger_entry.block_number
202
+ if block < start_block:
203
+ continue
204
+ elif block > end_block:
205
+ return
206
+ yield ledger_entry
207
+ await unblock_loop()
208
+
209
+ yielded = set()
210
+ for ledger_entry in self.objects:
211
+ block = ledger_entry.block_number
212
+ if block < start_block:
213
+ continue
214
+ elif end_block and block > end_block:
215
+ break
216
+ yield ledger_entry
217
+ yielded.add(ledger_entry)
218
+ await unblock_loop()
219
+ async for ledger_entry in self._get_new_objects(start_block, end_block): # type: ignore [assignment, misc]
220
+ if ledger_entry not in yielded:
221
+ yield ledger_entry
222
+ yielded.add(ledger_entry)
223
+ await unblock_loop()
224
+ for ledger_entry in self.objects:
225
+ block = ledger_entry.block_number
226
+ if block < start_block:
227
+ continue
228
+ elif end_block and block > end_block:
229
+ break
230
+ if ledger_entry not in yielded:
231
+ yield ledger_entry
232
+ yielded.add(ledger_entry)
233
+ await unblock_loop()
234
+
235
+ @set_end_block_if_none
236
+ @stuck_coro_debugger
237
+ async def get(self, start_block: Block, end_block: Block) -> _LedgerEntryList:
238
+ """
239
+ Retrieves ledger entries between the specified blocks.
240
+
241
+ Args:
242
+ start_block: The starting block number.
243
+ end_block: The ending block number.
244
+
245
+ Returns:
246
+ _LedgerEntryList: The list of ledger entries.
247
+
248
+ Examples:
249
+ >>> entries = await ledger.get(12000000, 12345678)
250
+ """
251
+ return self._list_type([ledger_entry async for ledger_entry in self[start_block:end_block]])
252
+
253
+ @stuck_coro_debugger
254
+ async def new(self) -> _LedgerEntryList:
255
+ """
256
+ Retrieves new ledger entries since the last cached block.
257
+
258
+ Returns:
259
+ _LedgerEntryList: The list of new ledger entries.
260
+
261
+ Examples:
262
+ >>> new_entries = await ledger.new()
263
+ """
264
+ start_block = 0 if self.cached_thru is None else self.cached_thru + 1
265
+ end_block = await get_buffered_chain_height()
266
+ return self[start_block, end_block] # type: ignore [index, return-value]
267
+
268
+ async def sent(
269
+ self, start_block: Optional[Block] = None, end_block: Optional[Block] = None
270
+ ) -> AsyncIterator[T]:
271
+ address = self.portfolio_address.address
272
+ async for obj in self[start_block:end_block]:
273
+ if obj.from_address == address:
274
+ yield obj
275
+
276
+ async def received(
277
+ self, start_block: Optional[Block] = None, end_block: Optional[Block] = None
278
+ ) -> AsyncIterator[T]:
279
+ address = self.portfolio_address.address
280
+ async for obj in self[start_block:end_block]:
281
+ if obj.from_address != address:
282
+ yield obj
283
+
284
+ @set_end_block_if_none
285
+ @stuck_coro_debugger
286
+ async def _get_new_objects(self, start_block: Block, end_block: Block) -> AsyncIterator[T]:
287
+ """
288
+ Retrieves new ledger entries between the specified blocks.
289
+
290
+ Args:
291
+ start_block: The starting block number.
292
+ end_block: The ending block number.
293
+
294
+ Yields:
295
+ AsyncIterator[T]: An async iterator of new ledger entries.
296
+ """
297
+ async with self._lock:
298
+ async for ledger_entry in self._load_new_objects(start_block, end_block):
299
+ yield ledger_entry
300
+
301
+ @abstractmethod
302
+ async def _load_new_objects(self, start_block: Block, end_block: Block) -> AsyncIterator[T]:
303
+ """
304
+ Abstract method to load new ledger entries between the specified blocks.
305
+
306
+ Args:
307
+ start_block: The starting block number.
308
+ end_block: The ending block number.
309
+
310
+ Yields:
311
+ AsyncIterator[T]: An async iterator of new ledger entries.
312
+ """
313
+ yield # type: ignore [misc]
314
+
315
+ def _check_blocks_against_cache(
316
+ self, start_block: Block, end_block: Block
317
+ ) -> Tuple[Block, Block]:
318
+ """
319
+ Checks the specified block range against the cached block range.
320
+
321
+ Args:
322
+ start_block: The starting block number.
323
+ end_block: The ending block number.
324
+
325
+ Returns:
326
+ Tuple: The adjusted block range.
327
+
328
+ Raises:
329
+ ValueError: If the start block is after the end block.
330
+ _exceptions.BlockRangeIsCached: If the block range is already cached.
331
+ _exceptions.BlockRangeOutOfBounds: If the block range is out of bounds.
332
+ """
333
+ if start_block > end_block:
334
+ raise ValueError(f"Start block {start_block} is after end block {end_block}")
335
+
336
+ # There is no cache
337
+ elif self.cached_from is None or self.cached_thru is None:
338
+ return start_block, end_block
339
+
340
+ # Range is cached
341
+ elif start_block >= self.cached_from and end_block <= self.cached_thru:
342
+ raise _exceptions.BlockRangeIsCached()
343
+
344
+ # Beginning of range is cached
345
+ elif (
346
+ start_block >= self.cached_from
347
+ and start_block < self.cached_thru
348
+ and end_block > self.cached_thru
349
+ ):
350
+ return self.cached_thru + 1, end_block
351
+
352
+ # End of range is cached
353
+ elif (
354
+ start_block < self.cached_from
355
+ and end_block >= self.cached_from
356
+ and end_block < self.cached_thru
357
+ ):
358
+ return start_block, self.cached_from - 1
359
+
360
+ # Beginning and end both outside bounds of cache to high side
361
+ elif start_block > self.cached_thru:
362
+ return self.cached_thru + 1, end_block
363
+
364
+ # Beginning and end both outside bounds of cache to low side
365
+ elif end_block < self.cached_from:
366
+ return start_block, self.cached_from - 1
367
+
368
+ # Beginning and end both outside bounds of cache, split
369
+ elif start_block < self.cached_from and end_block > self.cached_thru:
370
+ raise _exceptions.BlockRangeOutOfBounds(start_block, end_block, self)
371
+
372
+ raise NotImplementedError(
373
+ f"This is a work in progress and we still need code for this specific case. Feel free to create an issue on our github if you need this.\n\nstart_block: {start_block} end_block: {end_block} cached_from: {self.cached_from} cached_thru: {self.cached_thru}"
374
+ )
375
+
376
+
377
+ class TransactionsList(PandableList[Transaction]):
378
+ """
379
+ A list subclass for transactions that can convert to a :class:`DataFrame`.
380
+ """
381
+
382
+ def _df(self) -> DataFrame:
383
+ """
384
+ Converts the list of transactions to a DataFrame.
385
+
386
+ Returns:
387
+ DataFrame: The transactions as a DataFrame.
388
+ """
389
+ df = DataFrame(self)
390
+ if len(df) > 0:
391
+ df.chainId = df.chainId.apply(int)
392
+ df.blockNumber = df.blockNumber.apply(int)
393
+ df.transactionIndex = df.transactionIndex.apply(int)
394
+ df.nonce = df.nonce.apply(int)
395
+ df.gas = df.gas.apply(int)
396
+ df.gasPrice = df.gasPrice.apply(int)
397
+ return df
398
+
399
+
400
+ Nonce = int
401
+
402
+
403
+ class AddressTransactionsLedger(AddressLedgerBase[TransactionsList, Transaction]):
404
+ """
405
+ A ledger for managing transaction entries.
406
+ """
407
+
408
+ _list_type = TransactionsList
409
+ __slots__ = ("cached_thru_nonce", "_queue", "_ready", "_num_workers", "_workers")
410
+
411
+ def __init__(self, portfolio_address: "PortfolioAddress", num_workers: int = 1000):
412
+ """
413
+ Initializes the AddressTransactionsLedger instance.
414
+
415
+ Args:
416
+ portfolio_address: The :class:`~eth_portfolio.address.PortfolioAddress` this ledger belongs to.
417
+ """
418
+ super().__init__(portfolio_address)
419
+ self.cached_thru_nonce = -1
420
+ """
421
+ The nonce through which all transactions have been loaded into memory.
422
+ """
423
+ self._queue = Queue()
424
+ self._ready = Queue()
425
+ self._num_workers = num_workers
426
+ self._workers = []
427
+
428
+ def __del__(self) -> None:
429
+ self.__stop_workers()
430
+
431
+ @set_end_block_if_none
432
+ @stuck_coro_debugger
433
+ async def _load_new_objects(self, _: Block, end_block: Block) -> AsyncIterator[Transaction]: # type: ignore [override]
434
+ """
435
+ Loads new transaction entries between the specified blocks.
436
+
437
+ Args:
438
+ _: The starting block number (unused).
439
+ end_block: The ending block number.
440
+
441
+ Yields:
442
+ AsyncIterator[Transaction]: An async iterator of transaction entries.
443
+ """
444
+ if self.cached_thru and end_block < self.cached_thru:
445
+ return
446
+ end_block_nonce: int = await get_nonce_at_block(self.address, end_block)
447
+ if nonces := tuple(range(self.cached_thru_nonce + 1, end_block_nonce + 1)):
448
+ for i, nonce in enumerate(nonces):
449
+ self._queue.put_nowait(nonce)
450
+
451
+ # Keep the event loop relatively unblocked
452
+ # and let the rpc start doing work asap
453
+ if i % 1000:
454
+ await sleep(0)
455
+
456
+ len_nonces = len(nonces)
457
+ del nonces
458
+
459
+ self._ensure_workers(min(len_nonces, self._num_workers))
460
+
461
+ transactions = []
462
+ transaction: Optional[Transaction]
463
+ for _ in tqdm(range(len_nonces), desc=f"Transactions {self.address}"):
464
+ nonce, transaction = await self._ready.get()
465
+ if transaction:
466
+ if isinstance(transaction, Exception):
467
+ raise transaction
468
+ transactions.append(transaction)
469
+ yield transaction
470
+ elif nonce == 0 and self.cached_thru_nonce == -1:
471
+ # Gnosis safes
472
+ self.cached_thru_nonce = 0
473
+ else:
474
+ # NOTE Are we sure this is the correct way to handle this scenario? Are we sure it will ever even occur with the new gnosis handling?
475
+ logger.warning("No transaction with nonce %s for %s", nonce, self.address)
476
+
477
+ self.__stop_workers()
478
+
479
+ if transactions:
480
+ self.objects.extend(transactions)
481
+ if self.objects:
482
+ self.objects.sort(key=lambda t: t.nonce)
483
+ self.cached_thru_nonce = self.objects[-1].nonce
484
+
485
+ if self.cached_from is None:
486
+ self.cached_from = 0
487
+ if self.cached_thru is None or end_block > self.cached_thru:
488
+ self.cached_thru = end_block
489
+
490
+ def _ensure_workers(self, num_workers: int) -> None:
491
+ len_workers = len(self._workers)
492
+ if len_workers < num_workers:
493
+ worker_fn = self.__worker_fn
494
+ address = self.address
495
+ load_prices = self.load_prices
496
+ queue_get = stuck_coro_debugger(self._queue.get)
497
+ put_ready = self._ready.put_nowait
498
+
499
+ self._workers.extend(
500
+ create_task(
501
+ coro=worker_fn(address, load_prices, queue_get, put_ready),
502
+ name=f"AddressTransactionsLedger worker {i} for {address}",
503
+ )
504
+ for i in range(num_workers - len_workers)
505
+ )
506
+
507
+ @staticmethod
508
+ async def __worker_fn(
509
+ address: ChecksumAddress,
510
+ load_prices: bool,
511
+ queue_get: Callable[[], Nonce],
512
+ put_ready: Callable[[Nonce, Optional[Transaction]], None],
513
+ ) -> NoReturn:
514
+ try:
515
+ while True:
516
+ nonce = await queue_get()
517
+ try:
518
+ put_ready(await load_transaction(address, nonce, load_prices))
519
+ except Exception as e:
520
+ put_ready((nonce, e))
521
+ except Exception as e:
522
+ logger.error(f"%s in %s __worker_coro", type(e), self)
523
+ logger.exception(e)
524
+ raise
525
+
526
+ def __stop_workers(self) -> None:
527
+ logger.info("stopping workers for %s", self)
528
+ workers = self._workers
529
+ pop_next = workers.pop
530
+ for _ in range(len(workers)):
531
+ pop_next().cancel()
532
+
533
+
534
+ class InternalTransfersList(PandableList[InternalTransfer]):
535
+ """
536
+ A list subclass for internal transfer entries that can convert to a :class:`DataFrame`.
537
+ """
538
+
539
+
540
+ @a_sync.Semaphore(128, __name__ + ".trace_filter")
541
+ @stuck_coro_debugger
542
+ @eth_retry.auto_retry
543
+ async def trace_filter(fromBlock: HexStr, toBlock: HexStr, **kwargs) -> List[FilterTrace]:
544
+ return await __trace_filter(fromBlock, toBlock, **kwargs)
545
+
546
+
547
+ async def __trace_filter(fromBlock: HexStr, toBlock: HexStr, **kwargs) -> List[FilterTrace]:
548
+ try:
549
+ return await dank_mids.eth.trace_filter(
550
+ {"fromBlock": fromBlock, "toBlock": toBlock, **kwargs}
551
+ )
552
+ except ClientResponseError as e:
553
+ if e.status != HTTPStatus.SERVICE_UNAVAILABLE or toBlock == fromBlock:
554
+ raise
555
+ except TypeError as e:
556
+ # This is some intermittent error I need to debug in dank_mids, I think it occurs when we get rate limited
557
+ if str(e) != "a bytes-like object is required, not 'NoneType'":
558
+ raise
559
+ await sleep(0.5)
560
+ # remove this logger when I know there are no looping issues
561
+ logger.info("call failed, trying again")
562
+
563
+ from_block = int(fromBlock, 16)
564
+ range_size = int(toBlock, 16) - from_block + 1
565
+ chunk_size = range_size // 2
566
+ halfway = from_block + chunk_size
567
+
568
+ results = await gather(
569
+ __trace_filter(fromBlock=fromBlock, toBlock=HexStr(hex(halfway)), **kwargs),
570
+ __trace_filter(fromBlock=HexStr(hex(halfway + 1)), toBlock=toBlock, **kwargs),
571
+ )
572
+ return results[0] + results[1]
573
+
574
+
575
+ @alru_cache(maxsize=None)
576
+ async def get_transaction_status(txhash: str) -> Status:
577
+ """
578
+ Retrieves the status for a transaction.
579
+
580
+ This function is cached to disk to reduce resource usage.
581
+
582
+ Args:
583
+ txhash: The hash of the transaction.
584
+
585
+ Returns:
586
+ The status of the transaction.
587
+ """
588
+ return await dank_mids.eth.get_transaction_status(txhash)
589
+
590
+
591
+ _trace_semaphores = defaultdict(lambda: a_sync.Semaphore(16, __name__ + ".trace_semaphore"))
592
+
593
+
594
+ @cache_to_disk
595
+ @eth_retry.auto_retry
596
+ async def get_traces(filter_params: TraceFilterParams) -> List[FilterTrace]:
597
+ """
598
+ Retrieves traces from the web3 provider using the given parameters.
599
+
600
+ This function is cached to disk to reduce resource usage.
601
+
602
+ Args:
603
+ filter_params: The parameters for the trace filter.
604
+
605
+ Returns:
606
+ The list of traces.
607
+ """
608
+ if chain.id == Network.Polygon:
609
+ logger.warning(
610
+ "polygon doesnt support trace_filter method, must develop alternate solution"
611
+ )
612
+ return []
613
+ semaphore_key = tuple(
614
+ sorted(tuple(filter_params.get(x, ("",))) for x in ("toAddress", "fromAddress"))
615
+ )
616
+ async with _trace_semaphores[semaphore_key]:
617
+ return await _check_traces(await trace_filter(**filter_params))
618
+
619
+
620
+ @stuck_coro_debugger
621
+ @eth_retry.auto_retry
622
+ async def _check_traces(traces: List[FilterTrace]) -> List[FilterTrace]:
623
+ good_traces = []
624
+ append = good_traces.append
625
+
626
+ check_status_tasks = a_sync.TaskMapping(get_transaction_status)
627
+
628
+ for i, trace in enumerate(traces):
629
+ # Make sure we don't block up the event loop
630
+ if i % 500:
631
+ await sleep(0)
632
+
633
+ if "error" in trace:
634
+ continue
635
+
636
+ # NOTE: Not sure why these appear, but I've yet to come across an internal transfer
637
+ # that actually transmitted value to the singleton even though they appear to.
638
+ if (
639
+ isinstance(trace, call.Trace)
640
+ and trace.action.to == "0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552"
641
+ ): # Gnosis Safe Singleton 1.3.0
642
+ continue
643
+
644
+ if not isinstance(trace, reward.Trace):
645
+ # NOTE: We don't need to confirm block rewards came from a successful transaction, because they don't come from a transaction
646
+ check_status_tasks[trace.transactionHash]
647
+
648
+ append(trace)
649
+
650
+ # NOTE: We don't need to confirm block rewards came from a successful transaction, because they don't come from a transaction
651
+ return [
652
+ trace
653
+ for trace in good_traces
654
+ if isinstance(trace, reward.Trace)
655
+ or await check_status_tasks[trace.transactionHash] == Status.success
656
+ ]
657
+
658
+
659
+ BlockRange = Tuple[Block, Block]
660
+
661
+
662
+ def _get_block_ranges(start_block: Block, end_block: Block) -> List[BlockRange]:
663
+ return [(i, i + BATCH_SIZE - 1) for i in range(start_block, end_block, BATCH_SIZE)]
664
+
665
+
666
+ class AddressInternalTransfersLedger(AddressLedgerBase[InternalTransfersList, InternalTransfer]):
667
+ """
668
+ A ledger for managing internal transfer entries.
669
+ """
670
+
671
+ _list_type = InternalTransfersList
672
+
673
+ @set_end_block_if_none
674
+ @stuck_coro_debugger
675
+ async def _load_new_objects(
676
+ self, start_block: Block, end_block: Block
677
+ ) -> AsyncIterator[InternalTransfer]:
678
+ """
679
+ Loads new internal transfer entries between the specified blocks.
680
+
681
+ Args:
682
+ start_block: The starting block number.
683
+ end_block: The ending block number.
684
+
685
+ Yields:
686
+ AsyncIterator[InternalTransfer]: An async iterator of internal transfer entries.
687
+ """
688
+ if start_block == 0:
689
+ start_block = 1
690
+
691
+ try:
692
+ start_block, end_block = self._check_blocks_against_cache(start_block, end_block)
693
+ except _exceptions.BlockRangeIsCached:
694
+ return
695
+ except _exceptions.BlockRangeOutOfBounds as e:
696
+ await e.load_remaining()
697
+ return
698
+
699
+ # TODO: figure out where this float comes from and raise a TypeError there
700
+ if isinstance(start_block, float) and int(start_block) == start_block:
701
+ start_block = int(start_block)
702
+ if isinstance(end_block, float) and int(end_block) == end_block:
703
+ end_block = int(end_block)
704
+
705
+ block_ranges = _get_block_ranges(start_block, end_block)
706
+
707
+ trace_filter_coros = [
708
+ get_traces({direction: [self.address], "fromBlock": hex(start), "toBlock": hex(end)})
709
+ for direction, (start, end) in product(["toAddress", "fromAddress"], block_ranges)
710
+ ]
711
+
712
+ # NOTE: We only want tqdm progress bar when there is work to do
713
+ if len(block_ranges) == 1:
714
+ generator_function = a_sync.as_completed
715
+ else:
716
+ generator_function = partial( # type: ignore [assignment]
717
+ a_sync.as_completed, tqdm=True, desc=f"Trace Filters {self.address}"
718
+ )
719
+
720
+ traces = []
721
+ async for chunk in generator_function(trace_filter_coros, aiter=True):
722
+ traces.extend(chunk)
723
+
724
+ if traces:
725
+ internal_transfers = []
726
+ append_transfer = internal_transfers.append
727
+ load = InternalTransfer.from_trace
728
+ tqdm_desc = f"Internal Transfers {self.address}"
729
+
730
+ if self.load_prices:
731
+ tasks = []
732
+ while traces:
733
+ tasks.extend(
734
+ create_task(load(trace, load_prices=True)) for trace in traces[:1000]
735
+ )
736
+ traces = traces[1000:]
737
+ # let the tasks start sending calls to your node now
738
+ # without waiting for all tasks to be created
739
+ await sleep(0)
740
+
741
+ done = 0
742
+ async for internal_transfer in a_sync.as_completed(
743
+ tasks, aiter=True, tqdm=True, desc=tqdm_desc
744
+ ):
745
+ if internal_transfer is not None:
746
+ append_transfer(internal_transfer)
747
+ yield internal_transfer
748
+
749
+ done += 1
750
+ if done % 100 == 0:
751
+ await sleep(0)
752
+
753
+ else:
754
+ pop_next_trace = traces.pop
755
+ for i in tqdm(tuple(range(len(traces))), desc=tqdm_desc):
756
+ internal_transfer = await load(pop_next_trace(), load_prices=False)
757
+ if internal_transfer is not None:
758
+ append_transfer(internal_transfer)
759
+ yield internal_transfer
760
+
761
+ if i % 100 == 0:
762
+ await sleep(0)
763
+
764
+ if internal_transfers:
765
+ self.objects.extend(internal_transfers)
766
+
767
+ self.objects.sort(key=lambda t: (t.block_number, t.transaction_index))
768
+
769
+ if self.cached_from is None or start_block < self.cached_from:
770
+ self.cached_from = start_block
771
+ if self.cached_thru is None or end_block > self.cached_thru:
772
+ self.cached_thru = end_block
773
+
774
+
775
+ _yield_tokens_semaphore = a_sync.Semaphore(
776
+ 10, name="eth_portfolio._ledgers.address._yield_tokens_semaphore"
777
+ )
778
+
779
+
780
+ class TokenTransfersList(PandableList[TokenTransfer]):
781
+ """
782
+ A list subclass for token transfer entries that can convert to a :class:`DataFrame`.
783
+ """
784
+
785
+
786
+ class AddressTokenTransfersLedger(AddressLedgerBase[TokenTransfersList, TokenTransfer]):
787
+ """
788
+ A ledger for managing token transfer entries.
789
+ """
790
+
791
+ _list_type = TokenTransfersList
792
+ __slots__ = ("_transfers",)
793
+
794
+ def __init__(self, portfolio_address: "PortfolioAddress"):
795
+ """
796
+ Initializes the AddressTokenTransfersLedger instance.
797
+
798
+ Args:
799
+ portfolio_address: The :class:`~eth_portfolio.address.PortfolioAddress` this ledger belongs to.
800
+ """
801
+ super().__init__(portfolio_address)
802
+ self._transfers = TokenTransfers(
803
+ self.address, self.portfolio_address._start_block, load_prices=self.load_prices
804
+ )
805
+ """
806
+ TokenTransfers: Instance for handling token transfer operations.
807
+ """
808
+
809
+ @stuck_coro_debugger
810
+ async def list_tokens_at_block(self, block: Optional[int] = None) -> List[ERC20]:
811
+ """
812
+ Lists the tokens held at a specific block.
813
+
814
+ Args:
815
+ block (Optional[int], optional): The block number. Defaults to None.
816
+
817
+ Returns:
818
+ List[ERC20]: The list of ERC20 tokens.
819
+
820
+ Examples:
821
+ >>> tokens = await ledger.list_tokens_at_block(12345678)
822
+ """
823
+ return [token async for token in self._yield_tokens_at_block(block)]
824
+
825
+ async def _yield_tokens_at_block(self, block: Optional[int] = None) -> AsyncIterator[ERC20]:
826
+ """
827
+ Yields the tokens held at a specific block.
828
+
829
+ Args:
830
+ block (Optional[int], optional): The block number. Defaults to None.
831
+
832
+ Yields:
833
+ AsyncIterator[ERC20]: An async iterator of ERC20 tokens.
834
+ """
835
+ async with _yield_tokens_semaphore:
836
+ yielded = set()
837
+ async for transfer in self[:block]:
838
+ address = transfer.token_address
839
+ if address not in yielded:
840
+ yielded.add(address)
841
+ yield ERC20(address, asynchronous=self.asynchronous)
842
+
843
+ @set_end_block_if_none
844
+ @stuck_coro_debugger
845
+ async def _load_new_objects(self, start_block: Block, end_block: Block) -> AsyncIterator[TokenTransfer]: # type: ignore [override]
846
+ """
847
+ Loads new token transfer entries between the specified blocks.
848
+
849
+ Args:
850
+ start_block: The starting block number.
851
+ end_block: The ending block number.
852
+
853
+ Yields:
854
+ AsyncIterator[TokenTransfer]: An async iterator of token transfer entries.
855
+ """
856
+ try:
857
+ start_block, end_block = self._check_blocks_against_cache(start_block, end_block)
858
+ except _exceptions.BlockRangeIsCached:
859
+ return
860
+ except _exceptions.BlockRangeOutOfBounds as e:
861
+ await e.load_remaining()
862
+ return
863
+
864
+ if tasks := [
865
+ task
866
+ async for task in self._transfers.yield_thru_block(end_block)
867
+ if start_block <= task.block # type: ignore [attr-defined]
868
+ ]:
869
+ token_transfers = []
870
+ append_token_transfer = token_transfers.append
871
+ done = 0
872
+ async for token_transfer in a_sync.as_completed(
873
+ tasks, aiter=True, tqdm=True, desc=f"Token Transfers {self.address}"
874
+ ):
875
+ if token_transfer:
876
+ append_token_transfer(token_transfer)
877
+ yield token_transfer
878
+
879
+ # Don't let the event loop get congested
880
+ done += 1
881
+ if done % 100 == 0:
882
+ await sleep(0)
883
+
884
+ if token_transfers:
885
+ self.objects.extend(token_transfers)
886
+
887
+ self.objects.sort(key=lambda t: (t.block_number, t.transaction_index, t.log_index))
888
+
889
+ if self.cached_from is None or start_block < self.cached_from:
890
+ self.cached_from = start_block
891
+ if self.cached_thru is None or end_block > self.cached_thru:
892
+ self.cached_thru = end_block