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