eth-portfolio 0.5.8__cp311-cp311-musllinux_1_2_x86_64.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 (83) hide show
  1. eth_portfolio/__init__.py +24 -0
  2. eth_portfolio/_argspec.cpython-311-x86_64-linux-musl.so +0 -0
  3. eth_portfolio/_argspec.py +43 -0
  4. eth_portfolio/_cache.py +119 -0
  5. eth_portfolio/_config.cpython-311-x86_64-linux-musl.so +0 -0
  6. eth_portfolio/_config.py +4 -0
  7. eth_portfolio/_db/__init__.py +0 -0
  8. eth_portfolio/_db/decorators.py +147 -0
  9. eth_portfolio/_db/entities.py +311 -0
  10. eth_portfolio/_db/utils.py +619 -0
  11. eth_portfolio/_decimal.py +154 -0
  12. eth_portfolio/_decorators.py +84 -0
  13. eth_portfolio/_exceptions.py +65 -0
  14. eth_portfolio/_ledgers/__init__.py +0 -0
  15. eth_portfolio/_ledgers/address.py +917 -0
  16. eth_portfolio/_ledgers/portfolio.py +328 -0
  17. eth_portfolio/_loaders/__init__.py +33 -0
  18. eth_portfolio/_loaders/_nonce.cpython-311-x86_64-linux-musl.so +0 -0
  19. eth_portfolio/_loaders/_nonce.py +193 -0
  20. eth_portfolio/_loaders/balances.cpython-311-x86_64-linux-musl.so +0 -0
  21. eth_portfolio/_loaders/balances.py +95 -0
  22. eth_portfolio/_loaders/token_transfer.py +215 -0
  23. eth_portfolio/_loaders/transaction.py +240 -0
  24. eth_portfolio/_loaders/utils.cpython-311-x86_64-linux-musl.so +0 -0
  25. eth_portfolio/_loaders/utils.py +67 -0
  26. eth_portfolio/_shitcoins.cpython-311-x86_64-linux-musl.so +0 -0
  27. eth_portfolio/_shitcoins.py +342 -0
  28. eth_portfolio/_stableish.cpython-311-x86_64-linux-musl.so +0 -0
  29. eth_portfolio/_stableish.py +42 -0
  30. eth_portfolio/_submodules.py +72 -0
  31. eth_portfolio/_utils.py +229 -0
  32. eth_portfolio/_ydb/__init__.py +0 -0
  33. eth_portfolio/_ydb/token_transfers.py +144 -0
  34. eth_portfolio/address.py +396 -0
  35. eth_portfolio/buckets.py +212 -0
  36. eth_portfolio/constants.cpython-311-x86_64-linux-musl.so +0 -0
  37. eth_portfolio/constants.py +87 -0
  38. eth_portfolio/portfolio.py +669 -0
  39. eth_portfolio/protocols/__init__.py +64 -0
  40. eth_portfolio/protocols/_base.py +107 -0
  41. eth_portfolio/protocols/convex.py +17 -0
  42. eth_portfolio/protocols/dsr.py +50 -0
  43. eth_portfolio/protocols/lending/README.md +6 -0
  44. eth_portfolio/protocols/lending/__init__.py +50 -0
  45. eth_portfolio/protocols/lending/_base.py +56 -0
  46. eth_portfolio/protocols/lending/compound.py +186 -0
  47. eth_portfolio/protocols/lending/liquity.py +108 -0
  48. eth_portfolio/protocols/lending/maker.py +110 -0
  49. eth_portfolio/protocols/lending/unit.py +44 -0
  50. eth_portfolio/protocols/liquity.py +17 -0
  51. eth_portfolio/py.typed +0 -0
  52. eth_portfolio/structs/__init__.py +43 -0
  53. eth_portfolio/structs/modified.py +69 -0
  54. eth_portfolio/structs/structs.py +628 -0
  55. eth_portfolio/typing/__init__.py +1418 -0
  56. eth_portfolio/typing/balance/single.py +176 -0
  57. eth_portfolio-0.5.8.dist-info/METADATA +28 -0
  58. eth_portfolio-0.5.8.dist-info/RECORD +83 -0
  59. eth_portfolio-0.5.8.dist-info/WHEEL +5 -0
  60. eth_portfolio-0.5.8.dist-info/entry_points.txt +2 -0
  61. eth_portfolio-0.5.8.dist-info/top_level.txt +3 -0
  62. eth_portfolio__mypyc.cpython-311-x86_64-linux-musl.so +0 -0
  63. eth_portfolio_scripts/__init__.py +17 -0
  64. eth_portfolio_scripts/_args.py +26 -0
  65. eth_portfolio_scripts/_logging.py +14 -0
  66. eth_portfolio_scripts/_portfolio.py +209 -0
  67. eth_portfolio_scripts/_utils.py +106 -0
  68. eth_portfolio_scripts/balances.cpython-311-x86_64-linux-musl.so +0 -0
  69. eth_portfolio_scripts/balances.py +56 -0
  70. eth_portfolio_scripts/docker/.grafana/dashboards/Portfolio/Balances.json +1962 -0
  71. eth_portfolio_scripts/docker/.grafana/dashboards/dashboards.yaml +10 -0
  72. eth_portfolio_scripts/docker/.grafana/datasources/datasources.yml +11 -0
  73. eth_portfolio_scripts/docker/__init__.cpython-311-x86_64-linux-musl.so +0 -0
  74. eth_portfolio_scripts/docker/__init__.py +16 -0
  75. eth_portfolio_scripts/docker/check.cpython-311-x86_64-linux-musl.so +0 -0
  76. eth_portfolio_scripts/docker/check.py +66 -0
  77. eth_portfolio_scripts/docker/docker-compose.yaml +61 -0
  78. eth_portfolio_scripts/docker/docker_compose.cpython-311-x86_64-linux-musl.so +0 -0
  79. eth_portfolio_scripts/docker/docker_compose.py +97 -0
  80. eth_portfolio_scripts/main.py +118 -0
  81. eth_portfolio_scripts/py.typed +1 -0
  82. eth_portfolio_scripts/victoria/__init__.py +72 -0
  83. eth_portfolio_scripts/victoria/types.py +38 -0
@@ -0,0 +1,669 @@
1
+ """
2
+ This module defines the :class:`~eth_portfolio.Portfolio` and :class:`~eth_portfolio.PortfolioWallets` classes, which are used to manage and interact with a collection of :class:`~eth_portfolio.address.PortfolioAddress` objects.
3
+ It also includes the :class:`~eth_portfolio.PortfolioLedger` class for handling transactions, internal transfers, and token transfers associated with the portfolio.
4
+ The :class:`~eth_portfolio.Portfolio` class leverages the `a_sync` library to support both synchronous and asynchronous operations. This allows it to efficiently gather data, perform portfolio-related tasks, and handle network calls without blocking, thus improving the overall responsiveness and performance of portfolio operations.
5
+
6
+ This file is part of a larger system that includes modules for handling portfolio addresses, ledger entries, and other related tasks.
7
+ """
8
+
9
+ import logging
10
+ from collections.abc import AsyncIterator, Iterable, Iterator
11
+ from functools import wraps
12
+ from typing import Any
13
+
14
+ import a_sync
15
+ from a_sync import igather
16
+ from a_sync.a_sync import ASyncFunction
17
+ from brownie import web3
18
+ from checksum_dict import ChecksumAddressDict
19
+ from pandas import DataFrame, concat
20
+ from web3 import Web3
21
+ from y.datatypes import Address, Block
22
+
23
+ from eth_portfolio import _argspec
24
+ from eth_portfolio._decorators import set_end_block_if_none
25
+ from eth_portfolio._ledgers.address import PandableLedgerEntryList
26
+ from eth_portfolio._ledgers.portfolio import (
27
+ PortfolioInternalTransfersLedger,
28
+ PortfolioLedgerBase,
29
+ PortfolioTokenTransfersLedger,
30
+ PortfolioTransactionsLedger,
31
+ )
32
+ from eth_portfolio._utils import _LedgeredBase
33
+ from eth_portfolio.address import PortfolioAddress
34
+ from eth_portfolio.constants import ADDRESSES
35
+ from eth_portfolio.structs import LedgerEntry
36
+ from eth_portfolio.typing import Addresses, PortfolioBalances
37
+
38
+ logger = logging.getLogger(__name__)
39
+
40
+
41
+ class PortfolioWallets(Iterable[PortfolioAddress], dict[Address, PortfolioAddress]): # type: ignore [metaclass]
42
+ """
43
+ A container that holds all :class:`~eth_portfolio.address.PortfolioAddress` objects for a specific :class:`~eth_portfolio.Portfolio`.
44
+
45
+ Works like a ``Dict[Address, PortfolioAddress]`` except when you iterate you get the values instead of the keys.
46
+ You should not initialize these. They are created automatically during :class:`~eth_portfolio.Portfolio` initialization.
47
+
48
+ Attributes:
49
+ _wallets: A checksummed dictionary of :class:`~eth_portfolio.address.PortfolioAddress` objects.
50
+ """
51
+
52
+ _wallets: ChecksumAddressDict[PortfolioAddress]
53
+
54
+ def __init__(
55
+ self,
56
+ addresses: Iterable[Address],
57
+ start_block: Block,
58
+ load_prices: bool,
59
+ num_workers_transactions: int,
60
+ asynchronous: bool,
61
+ ) -> None:
62
+ """
63
+ Initialize a PortfolioWallets instance.
64
+
65
+ Args:
66
+ asynchronous: Whether the parent portfolio is asynchronous.
67
+ addresses: An iterable of addresses to be included in the portfolio.
68
+ """
69
+ self._wallets: ChecksumAddressDict[PortfolioAddress] = ChecksumAddressDict()
70
+ """
71
+ A checksummed dictionary of :class:`~eth_portfolio.address.PortfolioAddress` objects.
72
+
73
+ Type:
74
+ ChecksumAddressDict[PortfolioAddress]
75
+ """
76
+
77
+ for address in addresses:
78
+ self._wallets[address] = PortfolioAddress(
79
+ address,
80
+ start_block,
81
+ load_prices,
82
+ num_workers_transactions=num_workers_transactions,
83
+ asynchronous=asynchronous,
84
+ )
85
+
86
+ def __repr__(self) -> str:
87
+ """
88
+ Return a string representation of the PortfolioWallets instance.
89
+
90
+ Returns:
91
+ String representation of the instance.
92
+ """
93
+ return f"<{type(self).__name__} wallets={list(self._wallets.values())}>"
94
+
95
+ def __contains__(self, address: Address | PortfolioAddress) -> bool:
96
+ """
97
+ Check if an address is in the portfolio wallets.
98
+
99
+ Args:
100
+ address: The address to check.
101
+
102
+ Returns:
103
+ True if the address is in the wallets, False otherwise.
104
+ """
105
+ return address in self._wallets
106
+
107
+ def __getitem__(self, address: Address) -> PortfolioAddress:
108
+ """
109
+ Get the :class:`~eth_portfolio.address.PortfolioAddress` object for a given address.
110
+
111
+ Args:
112
+ address: The address to look up.
113
+
114
+ Returns:
115
+ PortfolioAddress: The :class:`~eth_portfolio.address.PortfolioAddress` object.
116
+ """
117
+ return self._wallets[address]
118
+
119
+ def __iter__(self) -> Iterator[PortfolioAddress]:
120
+ """
121
+ Iterate over the :class:`~eth_portfolio.address.PortfolioAddress` objects in the wallets.
122
+
123
+ Returns:
124
+ Iterator[PortfolioAddress]: An iterator over :class:`~eth_portfolio.address.PortfolioAddress` objects.
125
+ """
126
+ yield from self._wallets.values()
127
+
128
+ def __len__(self) -> int:
129
+ """
130
+ Get the number of :class:`~eth_portfolio.address.PortfolioAddress` objects in the wallets.
131
+
132
+ Returns:
133
+ The number of :class:`~eth_portfolio.address.PortfolioAddress` objects.
134
+ """
135
+ return len(self._wallets)
136
+
137
+ def __bool__(self) -> bool:
138
+ """
139
+ Check if the wallets contain any addresses.
140
+
141
+ Returns:
142
+ True if there are addresses in the wallets, False otherwise.
143
+ """
144
+ return bool(self._wallets)
145
+
146
+ def keys(self) -> Iterable[Address]:
147
+ """
148
+ Get the keys (addresses) of the wallets.
149
+
150
+ Returns:
151
+ Iterable[Address]: An iterable of addresses.
152
+ """
153
+ return self._wallets.keys()
154
+
155
+ def values(self) -> Iterable[PortfolioAddress]:
156
+ """
157
+ Get the values (:class:`~eth_portfolio.address.PortfolioAddress` objects) of the wallets.
158
+
159
+ Returns:
160
+ Iterable[PortfolioAddress]: An iterable of :class:`~eth_portfolio.address.PortfolioAddress` objects.
161
+ """
162
+ return self._wallets.values()
163
+
164
+ def items(self) -> Iterable[tuple[Address, PortfolioAddress]]:
165
+ """
166
+ Get the items (address, :class:`~eth_portfolio.address.PortfolioAddress` pairs) of the wallets.
167
+
168
+ Returns:
169
+ Iterable[Tuple[Address, PortfolioAddress]]: An iterable of (address, :class:`~eth_portfolio.address.PortfolioAddress`) tuples.
170
+ """
171
+ return self._wallets.items()
172
+
173
+
174
+ _DEFAULT_LABEL = "your portfolio"
175
+
176
+
177
+ class Portfolio(a_sync.ASyncGenericBase):
178
+ """
179
+ Used to export information about a group of :class:`~eth_portfolio.address.PortfolioAddress` objects.
180
+
181
+ - Has all attributes of a :class:`~eth_portfolio.address.PortfolioAddress`.
182
+ - All calls to `function(*args, **kwargs)` will return `{address: PortfolioAddress(Address).function(*args, **kwargs)}`
183
+ """
184
+
185
+ label: str = _DEFAULT_LABEL
186
+ """
187
+ A label for the portfolio. Defaults to "your portfolio"
188
+ """
189
+
190
+ load_prices: bool = True
191
+ """
192
+ Whether to load prices. Defaults to True.
193
+ """
194
+
195
+ asynchronous: bool = False
196
+ """
197
+ Whether to use asynchronous operations. Defaults to False.
198
+ """
199
+
200
+ _start_block = 0
201
+ """
202
+ The starting block number. Defaults to 0.
203
+ """
204
+
205
+ def __init__(
206
+ self,
207
+ addresses: Addresses,
208
+ start_block: int = 0,
209
+ label: str = _DEFAULT_LABEL,
210
+ load_prices: bool = True,
211
+ num_workers_transactions: int = 1000,
212
+ asynchronous: bool = False,
213
+ ) -> None:
214
+ """
215
+ Initialize a Portfolio instance.
216
+
217
+ Args:
218
+ addresses: The addresses to include in the portfolio.
219
+ Examples:
220
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
221
+ >>> print(portfolio)
222
+ """
223
+ if not isinstance(start_block, int):
224
+ raise TypeError(f"`start_block` must be an integer, not {type(start_block)}")
225
+ if start_block < 0:
226
+ raise ValueError("`start_block` must be >= 0")
227
+ super().__init__()
228
+
229
+ if start_block:
230
+ self._start_block = start_block
231
+
232
+ assert isinstance(label, str), f"`label` must be a string, you passed {type(label)}"
233
+ if label != _DEFAULT_LABEL:
234
+ self.label = label
235
+
236
+ if load_prices is False:
237
+ self.load_prices = False
238
+ elif load_prices is not True:
239
+ raise TypeError(f"`load_prices` must be a boolean, you passed {type(load_prices)}")
240
+
241
+ if asynchronous is True:
242
+ self.asynchronous = True
243
+ elif asynchronous is not False:
244
+ raise TypeError(f"`asynchronous` must be a boolean, you passed {type(asynchronous)}")
245
+
246
+ if isinstance(addresses, str):
247
+ addresses = [addresses]
248
+ elif not isinstance(addresses, Iterable):
249
+ raise TypeError(f"`addresses` must be an iterable, not {type(addresses)}")
250
+
251
+ self.addresses = PortfolioWallets(
252
+ addresses,
253
+ start_block,
254
+ load_prices,
255
+ num_workers_transactions,
256
+ self.asynchronous,
257
+ )
258
+ """
259
+ A container for the :class:`~eth_portfolio.Portfolio`'s :class:`~eth_portfolio.address.PortfolioAddress` objects.
260
+
261
+ Type:
262
+ :class:`~eth_portfolio.PortfolioWallets`
263
+
264
+ Works like a ``Dict[Address, PortfolioAddress]`` except you get the values when you iterate instead of the keys.
265
+ """
266
+
267
+ self.ledger = PortfolioLedger(self)
268
+ """
269
+ A container for all of your fun taxable events.
270
+
271
+ Type:
272
+ :class:`~eth_portfolio.PortfolioLedger`
273
+ """
274
+
275
+ self.w3: Web3 = web3
276
+ """
277
+ The `Web3` object which will be used to call your rpc for all read operations.
278
+
279
+ Type:
280
+ :class:`~web3.Web3`
281
+ """
282
+
283
+ def __repr__(self) -> str:
284
+ # NOTE: should we show addresses? for now we won't
285
+ # string = f"{type(self).__name__}(addresses={list(map(str, self.addresses))}"
286
+ string = f"{type(self).__name__}(label={self.label}"
287
+ if self._start_block:
288
+ string += f", start_block={self._start_block}"
289
+ return string + f", load_prices={self.load_prices}, asynchronous={self.asynchronous})"
290
+
291
+ def __repr__(self) -> str:
292
+ # NOTE: should we show addresses? for now we won't
293
+ # string = f"{type(self).__name__}(addresses={list(map(str, self.addresses))}"
294
+ string = f"{type(self).__name__}(label={self.label}"
295
+ if self._start_block:
296
+ string += f", start_block={self._start_block}"
297
+ return string + f", load_prices={self.load_prices}, asynchronous={self.asynchronous})"
298
+
299
+ def __getitem__(self, key: Address) -> PortfolioAddress:
300
+ """
301
+ Get a :class:`~eth_portfolio.address.PortfolioAddress` by its key.
302
+
303
+ Args:
304
+ key: The address key.
305
+
306
+ Returns:
307
+ PortfolioAddress: The :class:`~eth_portfolio.address.PortfolioAddress` object.
308
+
309
+ Example:
310
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
311
+ >>> address = portfolio["0xAddress1"]
312
+ >>> print(address)
313
+ """
314
+ return self.addresses[key]
315
+
316
+ def __iter__(self) -> Iterator[PortfolioAddress]:
317
+ """
318
+ Iterate over the :class:`~eth_portfolio.address.PortfolioAddress` objects.
319
+
320
+ Returns:
321
+ Iterator[PortfolioAddress]: An iterator over :class:`~eth_portfolio.address.PortfolioAddress` objects.
322
+
323
+ Example:
324
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
325
+ >>> for address in portfolio:
326
+ ... print(address)
327
+ """
328
+ yield from self.addresses
329
+
330
+ @property
331
+ def transactions(self) -> PortfolioTransactionsLedger:
332
+ """
333
+ A container for all transactions to or from any of your :class:`~eth_portfolio.address.PortfolioAddress`.
334
+
335
+ Returns:
336
+ PortfolioTransactionsLedger: The :class:`~eth_portfolio._ledgers.portfolio.PortfolioTransactionsLedger` object.
337
+
338
+ Example:
339
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
340
+ >>> transactions = portfolio.transactions
341
+ >>> print(transactions)
342
+ """
343
+ return self.ledger.transactions
344
+
345
+ @property
346
+ def internal_transfers(self) -> PortfolioInternalTransfersLedger:
347
+ """
348
+ A container for all internal transfers to or from any of your :class:`~eth_portfolio.address.PortfolioAddress`.
349
+
350
+ Returns:
351
+ PortfolioInternalTransfersLedger: The :class:`~eth_portfolio._ledgers.portfolio.PortfolioInternalTransfersLedger` object.
352
+
353
+ Example:
354
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
355
+ >>> internal_transfers = portfolio.internal_transfers
356
+ >>> print(internal_transfers)
357
+ """
358
+ return self.ledger.internal_transfers
359
+
360
+ @property
361
+ def token_transfers(self) -> PortfolioTokenTransfersLedger:
362
+ """
363
+ A container for all token transfers to or from any of your :class:`~eth_portfolio.address.PortfolioAddress`.
364
+
365
+ Returns:
366
+ PortfolioTokenTransfersLedger: The :class:`~eth_portfolio._ledgers.portfolio.PortfolioTokenTransfersLedger` object.
367
+
368
+ Example:
369
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
370
+ >>> token_transfers = portfolio.token_transfers
371
+ >>> print(token_transfers)
372
+ """
373
+ return self.ledger.token_transfers
374
+
375
+ async def describe(self, block: int) -> PortfolioBalances:
376
+ """
377
+ Returns a full snapshot of your portfolio at a given block.
378
+
379
+ Args:
380
+ block: The block number.
381
+
382
+ Returns:
383
+ PortfolioBalances: A snapshot of the portfolio balances.
384
+
385
+ Example:
386
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
387
+ >>> balances = await portfolio.describe(block=1200000)
388
+ >>> print(balances)
389
+ """
390
+ assert block
391
+ return PortfolioBalances(
392
+ await a_sync.gather(
393
+ {address.address: address.describe(block, sync=False) for address in self}
394
+ ),
395
+ block=block,
396
+ )
397
+
398
+ async def sent(
399
+ self,
400
+ start_block: Block | None = None,
401
+ end_block: Block | None = None,
402
+ *,
403
+ mem_cache: bool = True,
404
+ ) -> AsyncIterator[LedgerEntry]:
405
+ async for obj in self.ledger.sent(start_block, end_block, mem_cache=mem_cache):
406
+ yield obj
407
+
408
+ async def received(
409
+ self,
410
+ start_block: Block | None = None,
411
+ end_block: Block | None = None,
412
+ *,
413
+ mem_cache: bool = True,
414
+ ) -> AsyncIterator[LedgerEntry]:
415
+ async for obj in self.ledger.received(start_block, end_block, mem_cache=mem_cache):
416
+ yield obj
417
+
418
+
419
+ async_functions = {
420
+ name: obj for name, obj in PortfolioAddress.__dict__.items() if isinstance(obj, ASyncFunction)
421
+ }
422
+ for func_name, func in async_functions.items():
423
+ if not callable(getattr(PortfolioAddress, func_name)):
424
+ raise RuntimeError(
425
+ f"A PortfolioAddress object should not have a non-callable attribute suffixed with '_async'"
426
+ )
427
+
428
+ @a_sync.a_sync(default=func.default)
429
+ @wraps(func)
430
+ async def imported_func(
431
+ self: Portfolio, *args: Any, **kwargs: Any
432
+ ) -> _argspec.get_return_type(getattr(PortfolioAddress, func_name)):
433
+ """
434
+ Import an asynchronous function from :class:`~eth_portfolio.address.PortfolioAddress` to :class:`~eth_portfolio.Portfolio`.
435
+
436
+ Args:
437
+ self: The :class:`~eth_portfolio.Portfolio` instance.
438
+ args: Positional arguments for the function.
439
+ kwargs: Keyword arguments for the function.
440
+
441
+ Returns:
442
+ Any: The return type of the function.
443
+
444
+ Example:
445
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
446
+ >>> result = await portfolio.some_async_function(*args, **kwargs)
447
+ >>> print(result)
448
+ """
449
+ return await a_sync.gather(
450
+ {address: func(address, *args, **kwargs, sync=False) for address in self}
451
+ )
452
+
453
+ setattr(Portfolio, func_name, imported_func)
454
+ logger.debug("Ported %s from PortfolioAddress to Portfolio", func_name)
455
+
456
+
457
+ def _get_missing_cols_from_KeyError(e: KeyError) -> list[str]:
458
+ """
459
+ Extract missing column names from a KeyError.
460
+
461
+ Args:
462
+ e: The KeyError exception.
463
+
464
+ Returns:
465
+ A list of missing column names.
466
+ """
467
+ split = str(e).split("'")
468
+ return [split[i * 2 + 1] for i in range(len(split) // 2)]
469
+
470
+
471
+ class PortfolioLedger(_LedgeredBase[PortfolioLedgerBase]):
472
+ """
473
+ A container for all transactions, internal transfers, and token transfers to or from any of the wallets in your :class:`~eth_portfolio.Portfolio`.
474
+ """
475
+
476
+ def __init__(self, portfolio: "Portfolio") -> None:
477
+ """
478
+ Initialize a PortfolioLedger instance.
479
+
480
+ Args:
481
+ portfolio: The :class:`~eth_portfolio.Portfolio` instance to which this ledger belongs.
482
+ """
483
+ super().__init__(portfolio._start_block)
484
+ self.portfolio = portfolio
485
+ """
486
+ The :class:`~eth_portfolio.Portfolio` containing the wallets this ledger will pertain to.
487
+ """
488
+ self.transactions = PortfolioTransactionsLedger(portfolio)
489
+ """
490
+ A container for all transactions to or from any of your :class:`~eth_portfolio.address.PortfolioAddress`.
491
+ """
492
+ self.internal_transfers = PortfolioInternalTransfersLedger(portfolio)
493
+ """
494
+ A container for all internal transfers to or from any of your :class:`~eth_portfolio.address.PortfolioAddress`.
495
+ """
496
+ self.token_transfers = PortfolioTokenTransfersLedger(portfolio)
497
+ """
498
+ A container for all token transfers to or from any of your :class:`~eth_portfolio.address.PortfolioAddress`.
499
+ """
500
+ self.asynchronous = portfolio.asynchronous
501
+ """
502
+ True if default mode is async, False if sync.
503
+ """
504
+
505
+ async def all_entries(
506
+ self, start_block: Block, end_block: Block
507
+ ) -> dict[PortfolioAddress, dict[str, PandableLedgerEntryList]]:
508
+ """
509
+ Returns a mapping containing all transactions, internal transfers, and token transfers to or from each wallet in your portfolio.
510
+
511
+ Args:
512
+ start_block: The starting block number.
513
+ end_block: The ending block number.
514
+
515
+ Returns:
516
+ Dict[PortfolioAddress, Dict[str, PandableLedgerEntryList]]: A dictionary mapping :class:`~eth_portfolio.address.PortfolioAddress` to their ledger entries.
517
+
518
+ Example:
519
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
520
+ >>> entries = await portfolio.ledger.all_entries(start_block=1000000, end_block=1100000)
521
+ >>> print(entries)
522
+ """
523
+ return await a_sync.gather(
524
+ {address: address.all(start_block, end_block, sync=False) for address in self.portfolio}
525
+ )
526
+
527
+ @set_end_block_if_none
528
+ async def df(self, start_block: Block, end_block: Block, full: bool = False) -> DataFrame:
529
+ """
530
+ Returns a DataFrame containing all transactions, internal transfers, and token transfers to or from any wallet in your portfolio.
531
+
532
+ Args:
533
+ start_block: The starting block number.
534
+ end_block: The ending block number.
535
+ full (optional): Whether to include all columns or a subset. Defaults to False.
536
+
537
+ Returns:
538
+ DataFrame: A DataFrame with the ledger entries.
539
+
540
+ Example:
541
+ >>> portfolio = Portfolio(addresses=["0xAddress1", "0xAddress2"])
542
+ >>> df = await portfolio.ledger.df(start_block=1000000, end_block=1100000)
543
+ >>> print(df)
544
+ """
545
+ df = concat(
546
+ await igather(ledger.df(start_block, end_block, sync=False) for ledger in self._ledgers)
547
+ )
548
+
549
+ # Reorder columns
550
+ while True:
551
+ try:
552
+ if full:
553
+ df = df[
554
+ [
555
+ "chainId",
556
+ "blockNumber",
557
+ "blockHash",
558
+ "transactionIndex",
559
+ "hash",
560
+ "log_index",
561
+ "nonce",
562
+ "from",
563
+ "to",
564
+ "token",
565
+ "token_address",
566
+ "value",
567
+ "price",
568
+ "value_usd",
569
+ "gas",
570
+ "gasPrice",
571
+ "gasUsed",
572
+ "maxFeePerGas",
573
+ "maxPriorityFeePerGas",
574
+ "type",
575
+ "callType",
576
+ "traceAddress",
577
+ "subtraces",
578
+ "output",
579
+ "error",
580
+ "result",
581
+ "address",
582
+ "code",
583
+ "init",
584
+ "r",
585
+ "s",
586
+ "v",
587
+ "input",
588
+ ]
589
+ ]
590
+ else:
591
+ df = df[
592
+ [
593
+ "chainId",
594
+ "blockNumber",
595
+ "hash",
596
+ "from",
597
+ "to",
598
+ "token",
599
+ "token_address",
600
+ "value",
601
+ "price",
602
+ "value_usd",
603
+ "gas",
604
+ "gasPrice",
605
+ "gasUsed",
606
+ "maxFeePerGas",
607
+ "maxPriorityFeePerGas",
608
+ "type",
609
+ "callType",
610
+ "traceAddress",
611
+ "subtraces",
612
+ "output",
613
+ "error",
614
+ "result",
615
+ "address",
616
+ ]
617
+ ]
618
+ df = df[df["value"] != 0]
619
+ break
620
+ except KeyError as e:
621
+ for column in _get_missing_cols_from_KeyError(e):
622
+ df[column] = None
623
+
624
+ sort_cols = (
625
+ ["blockNumber", "transactionIndex", "log_index"] if full else ["blockNumber", "hash"]
626
+ )
627
+
628
+ try:
629
+ return df.sort_values(sort_cols).reset_index(drop=True)
630
+ except KeyError:
631
+ logger.error(df)
632
+ logger.error(df.columns)
633
+ raise
634
+
635
+ async def sent(
636
+ self,
637
+ start_block: Block | None = None,
638
+ end_block: Block | None = None,
639
+ *,
640
+ mem_cache: bool = True,
641
+ ) -> AsyncIterator[LedgerEntry]:
642
+ portfolio_addresses = set(self.portfolio.addresses.keys())
643
+ async for obj in self._get_and_yield(start_block or 0, end_block, mem_cache):
644
+ if (
645
+ obj.value
646
+ and obj.from_address in portfolio_addresses
647
+ and obj.to_address not in portfolio_addresses
648
+ ):
649
+ yield obj
650
+
651
+ async def received(
652
+ self,
653
+ start_block: Block | None = None,
654
+ end_block: Block | None = None,
655
+ *,
656
+ mem_cache: bool = True,
657
+ ) -> AsyncIterator[LedgerEntry]:
658
+ portfolio_addresses = set(self.portfolio.addresses.keys())
659
+ async for obj in self._get_and_yield(start_block or 0, end_block, mem_cache):
660
+ if (
661
+ obj.value
662
+ and obj.to_address in portfolio_addresses
663
+ and obj.from_address not in portfolio_addresses
664
+ ):
665
+ yield obj
666
+
667
+
668
+ # Use this var for a convenient way to set up your portfolio using env vars.
669
+ portfolio = Portfolio(ADDRESSES)