eth-portfolio 0.5.4__cp310-cp310-macosx_11_0_arm64.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 +25 -0
  2. eth_portfolio/_argspec.cpython-310-darwin.so +0 -0
  3. eth_portfolio/_argspec.py +42 -0
  4. eth_portfolio/_cache.py +121 -0
  5. eth_portfolio/_config.cpython-310-darwin.so +0 -0
  6. eth_portfolio/_config.py +4 -0
  7. eth_portfolio/_db/__init__.py +0 -0
  8. eth_portfolio/_db/decorators.py +148 -0
  9. eth_portfolio/_db/entities.py +311 -0
  10. eth_portfolio/_db/utils.py +610 -0
  11. eth_portfolio/_decimal.py +156 -0
  12. eth_portfolio/_decorators.py +84 -0
  13. eth_portfolio/_exceptions.py +67 -0
  14. eth_portfolio/_ledgers/__init__.py +0 -0
  15. eth_portfolio/_ledgers/address.py +925 -0
  16. eth_portfolio/_ledgers/portfolio.py +328 -0
  17. eth_portfolio/_loaders/__init__.py +33 -0
  18. eth_portfolio/_loaders/_nonce.cpython-310-darwin.so +0 -0
  19. eth_portfolio/_loaders/_nonce.py +196 -0
  20. eth_portfolio/_loaders/balances.cpython-310-darwin.so +0 -0
  21. eth_portfolio/_loaders/balances.py +94 -0
  22. eth_portfolio/_loaders/token_transfer.py +217 -0
  23. eth_portfolio/_loaders/transaction.py +241 -0
  24. eth_portfolio/_loaders/utils.cpython-310-darwin.so +0 -0
  25. eth_portfolio/_loaders/utils.py +68 -0
  26. eth_portfolio/_shitcoins.cpython-310-darwin.so +0 -0
  27. eth_portfolio/_shitcoins.py +342 -0
  28. eth_portfolio/_stableish.cpython-310-darwin.so +0 -0
  29. eth_portfolio/_stableish.py +42 -0
  30. eth_portfolio/_submodules.py +73 -0
  31. eth_portfolio/_utils.py +225 -0
  32. eth_portfolio/_ydb/__init__.py +0 -0
  33. eth_portfolio/_ydb/token_transfers.py +146 -0
  34. eth_portfolio/address.py +397 -0
  35. eth_portfolio/buckets.py +212 -0
  36. eth_portfolio/constants.cpython-310-darwin.so +0 -0
  37. eth_portfolio/constants.py +87 -0
  38. eth_portfolio/portfolio.py +661 -0
  39. eth_portfolio/protocols/__init__.py +65 -0
  40. eth_portfolio/protocols/_base.py +107 -0
  41. eth_portfolio/protocols/convex.py +17 -0
  42. eth_portfolio/protocols/dsr.py +51 -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 +57 -0
  46. eth_portfolio/protocols/lending/compound.py +187 -0
  47. eth_portfolio/protocols/lending/liquity.py +110 -0
  48. eth_portfolio/protocols/lending/maker.py +111 -0
  49. eth_portfolio/protocols/lending/unit.py +46 -0
  50. eth_portfolio/protocols/liquity.py +16 -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 +626 -0
  55. eth_portfolio/typing/__init__.py +1419 -0
  56. eth_portfolio/typing/balance/single.py +176 -0
  57. eth_portfolio-0.5.4.dist-info/METADATA +26 -0
  58. eth_portfolio-0.5.4.dist-info/RECORD +83 -0
  59. eth_portfolio-0.5.4.dist-info/WHEEL +6 -0
  60. eth_portfolio-0.5.4.dist-info/entry_points.txt +2 -0
  61. eth_portfolio-0.5.4.dist-info/top_level.txt +3 -0
  62. eth_portfolio__mypyc.cpython-310-darwin.so +0 -0
  63. eth_portfolio_scripts/__init__.py +20 -0
  64. eth_portfolio_scripts/_args.py +26 -0
  65. eth_portfolio_scripts/_logging.py +15 -0
  66. eth_portfolio_scripts/_portfolio.py +209 -0
  67. eth_portfolio_scripts/_utils.py +106 -0
  68. eth_portfolio_scripts/balances.cpython-310-darwin.so +0 -0
  69. eth_portfolio_scripts/balances.py +57 -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-310-darwin.so +0 -0
  74. eth_portfolio_scripts/docker/__init__.py +16 -0
  75. eth_portfolio_scripts/docker/check.cpython-310-darwin.so +0 -0
  76. eth_portfolio_scripts/docker/check.py +67 -0
  77. eth_portfolio_scripts/docker/docker-compose.yaml +61 -0
  78. eth_portfolio_scripts/docker/docker_compose.cpython-310-darwin.so +0 -0
  79. eth_portfolio_scripts/docker/docker_compose.py +100 -0
  80. eth_portfolio_scripts/main.py +119 -0
  81. eth_portfolio_scripts/py.typed +1 -0
  82. eth_portfolio_scripts/victoria/__init__.py +73 -0
  83. eth_portfolio_scripts/victoria/types.py +38 -0
@@ -0,0 +1,225 @@
1
+ import logging
2
+ import sqlite3
3
+ from abc import abstractmethod
4
+ from datetime import datetime
5
+ from functools import cached_property
6
+ from typing import (
7
+ TYPE_CHECKING,
8
+ AsyncGenerator,
9
+ AsyncIterator,
10
+ Final,
11
+ Generic,
12
+ List,
13
+ Optional,
14
+ Tuple,
15
+ TypeVar,
16
+ Union,
17
+ )
18
+
19
+ import dank_mids
20
+ from a_sync import ASyncGenericBase, ASyncIterable, ASyncIterator, as_yielded
21
+ from brownie import chain
22
+ from brownie.exceptions import ContractNotFound
23
+ from faster_async_lru import alru_cache
24
+ from faster_eth_abi.exceptions import InsufficientDataBytes
25
+ from eth_typing import ChecksumAddress
26
+ from pandas import DataFrame # type: ignore
27
+ from y import ERC20, Contract, Network
28
+ from y.constants import CHAINID, NETWORK_NAME
29
+ from y.datatypes import AddressOrContract, Block
30
+ from y.exceptions import (
31
+ CantFetchParam,
32
+ ContractNotVerified,
33
+ NodeNotSynced,
34
+ NonStandardERC20,
35
+ PriceError,
36
+ reraise_excs_with_extra_context,
37
+ yPriceMagicError,
38
+ )
39
+ from y.prices.magic import get_price
40
+
41
+ from eth_portfolio import _config, _decimal
42
+ from eth_portfolio.typing import _T
43
+
44
+ if TYPE_CHECKING:
45
+ from eth_portfolio.structs import LedgerEntry
46
+
47
+
48
+ logger: Final = logging.getLogger(__name__)
49
+
50
+ NON_STANDARD_ERC721: Final[List[ChecksumAddress]] = {
51
+ Network.Mainnet: ["0xb47e3cd837dDF8e4c57F05d70Ab865de6e193BBB"], # CryptoPunks
52
+ }.get(CHAINID, [])
53
+
54
+ SUPPRESS_ERROR_LOGS: Final[List[ChecksumAddress]] = {
55
+ # put tokens here when you don't expect them to price successfully and do not want to see the associated error logs
56
+ }.get(CHAINID, [])
57
+ """Put tokens here when you don't expect them to price successfully and do not want to see the associated error logs."""
58
+
59
+
60
+ async def get_buffered_chain_height() -> int:
61
+ """Returns an int equal to the current height of the chain minus `_config.REORG_BUFFER`."""
62
+ return await dank_mids.eth.get_block_number() - _config.REORG_BUFFER
63
+
64
+
65
+ class PandableList(List[_T]):
66
+ @cached_property
67
+ def df(self) -> DataFrame:
68
+ return self._df()
69
+
70
+ def _df(self) -> DataFrame:
71
+ """Override this method if you need to manipulate your dataframe before returning it."""
72
+ return DataFrame(self)
73
+
74
+
75
+ class Decimal(_decimal.Decimal):
76
+ """
77
+ I'm in the process of moving from floats to decimals, this will help be as I buidl.
78
+ """
79
+
80
+ # TODO i forget why I had this lol, should I delete?
81
+
82
+
83
+ async def _describe_err(token: AddressOrContract, block: Optional[Block]) -> str:
84
+ """
85
+ Assembles a string used to provide as much useful information as possible in PriceError messages
86
+ """
87
+ try:
88
+ symbol = await ERC20(token, asynchronous=True).symbol
89
+ except NonStandardERC20:
90
+ symbol = None
91
+
92
+ if block is None:
93
+ if symbol:
94
+ return f"{symbol} {token} on {NETWORK_NAME}"
95
+
96
+ return f"malformed token {token} on {NETWORK_NAME}"
97
+
98
+ if symbol:
99
+ return f"{symbol} {token} on {NETWORK_NAME} at {block}"
100
+
101
+ return f"malformed token {token} on {NETWORK_NAME} at {block}"
102
+
103
+
104
+ _to_raise: Final = (
105
+ OSError,
106
+ FileNotFoundError,
107
+ NodeNotSynced,
108
+ NotImplementedError,
109
+ sqlite3.OperationalError,
110
+ InsufficientDataBytes,
111
+ UnboundLocalError,
112
+ RuntimeError,
113
+ )
114
+
115
+
116
+ async def _get_price(token: AddressOrContract, block: Optional[int] = None) -> _decimal.Decimal:
117
+ token = str(token)
118
+ with reraise_excs_with_extra_context(token, block):
119
+ try:
120
+ if await is_erc721(token):
121
+ return _decimal.Decimal(0)
122
+ maybe_float = await get_price(token, block, silent=True, sync=False)
123
+ dprice = _decimal.Decimal(maybe_float)
124
+ return round(dprice, 18)
125
+ except CantFetchParam as e:
126
+ logger.warning("CantFetchParam %s", e)
127
+ except yPriceMagicError as e:
128
+ # Raise these exceptions
129
+ if isinstance(e.exception, _to_raise) and not isinstance(e.exception, RecursionError):
130
+ raise e.exception
131
+ # The exceptions below are acceptable enough
132
+ elif isinstance(e.exception, NonStandardERC20):
133
+ # Can't get symbol for handling like other excs
134
+ logger.warning(f"NonStandardERC20 while fetching price for {token}")
135
+ elif isinstance(e.exception, PriceError):
136
+ if token not in SUPPRESS_ERROR_LOGS:
137
+ logger.warning(
138
+ f"PriceError while fetching price for {await _describe_err(token, block)}"
139
+ )
140
+ else:
141
+ logger.warning(f"{e} while fetching price for {await _describe_err(token, block)}")
142
+ logger.warning(e, exc_info=True)
143
+ return _decimal.Decimal(0)
144
+
145
+
146
+ @alru_cache(maxsize=None)
147
+ async def is_erc721(token: ChecksumAddress) -> bool:
148
+ # This can probably be improved
149
+ try:
150
+ contract = await Contract.coroutine(token)
151
+ except (ContractNotFound, ContractNotVerified):
152
+ return False
153
+ attrs = "setApprovalForAll", "getApproved", "isApprovedForAll"
154
+ if all(hasattr(contract, attr) for attr in attrs):
155
+ return True
156
+ elif contract.address in NON_STANDARD_ERC721:
157
+ return True
158
+ return False
159
+
160
+
161
+ def _unpack_indicies(indicies: Union[Block, Tuple[Block, Block]]) -> Tuple[Block, Block]:
162
+ """Unpacks indicies and returns a tuple (start_block, end_block)."""
163
+ if isinstance(indicies, tuple):
164
+ start_block, end_block = indicies
165
+ else:
166
+ start_block = 0
167
+ end_block = indicies
168
+ return start_block, end_block
169
+
170
+
171
+ class _AiterMixin(ASyncIterable[_T]):
172
+ def __aiter__(self) -> AsyncIterator[_T]:
173
+ return self[self._start_block : chain.height].__aiter__()
174
+
175
+ def __getitem__(self, slice: slice) -> ASyncIterator[_T]:
176
+ if slice.start is not None and not isinstance(slice.start, (int, datetime)):
177
+ raise TypeError(f"start must be int or datetime. you passed {slice.start}")
178
+ if slice.stop and not isinstance(slice.stop, (int, datetime)):
179
+ raise TypeError(f"start must be int or datetime. you passed {slice.start}")
180
+ if slice.step is not None:
181
+ raise ValueError("You cannot use a step here.")
182
+ return ASyncIterator(self._get_and_yield(slice.start or 0, slice.stop, True)) # type: ignore [truthy-function]
183
+
184
+ def yield_forever(self, mem_cache: bool = True) -> ASyncIterator[_T]:
185
+ return ASyncIterator(self._get_and_yield(self._start_block or 0, None, mem_cache))
186
+
187
+ @abstractmethod
188
+ async def _get_and_yield(
189
+ self, start_block: Block, end_block: Block, mem_cache: bool
190
+ ) -> AsyncGenerator[_T, None]:
191
+ yield
192
+
193
+ @property
194
+ @abstractmethod
195
+ def _start_block(self) -> int: ...
196
+
197
+
198
+ _LT = TypeVar("_LT")
199
+
200
+
201
+ class _LedgeredBase(ASyncGenericBase, _AiterMixin["LedgerEntry"], Generic[_LT]):
202
+ """A mixin class for things with ledgers"""
203
+
204
+ transactions: _LT
205
+ internal_transfers: _LT
206
+ token_transfers: _LT
207
+
208
+ def __init__(self, start_block: Block) -> None:
209
+ self.start_block = start_block
210
+ super().__init__()
211
+
212
+ @property
213
+ def _start_block(self) -> Block:
214
+ # in the middle of refactoring this
215
+ return self.start_block
216
+
217
+ @cached_property
218
+ def _ledgers(self) -> Tuple[_LT, _LT, _LT]:
219
+ """A tuple with the 3 ledgers (transactions, internal transfers, token transfers)"""
220
+ return self.transactions, self.internal_transfers, self.token_transfers
221
+
222
+ def _get_and_yield(
223
+ self, start_block: Block, end_block: Block, mem_cache: bool
224
+ ) -> AsyncGenerator["LedgerEntry", None]:
225
+ return as_yielded(*(ledger._get_and_yield(start_block, end_block, mem_cache) for ledger in self._ledgers)) # type: ignore [return-value,index]
File without changes
@@ -0,0 +1,146 @@
1
+ from abc import abstractmethod
2
+ from asyncio import Task, create_task, sleep
3
+ from logging import DEBUG, getLogger
4
+ from typing import Any, Final, List
5
+ from collections.abc import AsyncIterator
6
+
7
+ import dank_mids
8
+ import evmspec
9
+ import y._db.log
10
+ from a_sync import ASyncIterable, ASyncIterator, as_yielded
11
+ from brownie import chain
12
+ from eth_typing import BlockNumber, ChecksumAddress
13
+ from faster_eth_abi import encode
14
+ from faster_eth_utils import encode_hex
15
+ from y.utils.events import ProcessedEvents
16
+
17
+ from eth_portfolio._loaders import load_token_transfer
18
+ from eth_portfolio._shitcoins import SHITCOINS
19
+ from eth_portfolio.constants import TRANSFER_SIGS
20
+ from eth_portfolio.structs import TokenTransfer
21
+
22
+
23
+ logger: Final = getLogger(__name__)
24
+ _logger_is_enabled_for: Final = logger.isEnabledFor
25
+ _logger_log: Final = logger._log
26
+
27
+
28
+ def encode_address(address: Any) -> bytes:
29
+ return encode_hex(encode(["address"], [str(address)]))
30
+
31
+
32
+ class _TokenTransfers(ProcessedEvents["Task[TokenTransfer]"]):
33
+ """A helper mixin that contains all logic for fetching token transfers for a particular wallet address"""
34
+
35
+ __slots__ = "address", "_load_prices"
36
+
37
+ def __init__(
38
+ self,
39
+ address: ChecksumAddress,
40
+ from_block: BlockNumber,
41
+ load_prices: bool = False,
42
+ ) -> None:
43
+ self.address: Final = address
44
+ self._load_prices: Final = load_prices
45
+ super().__init__(topics=self._topics, from_block=from_block)
46
+
47
+ def __repr__(self) -> str:
48
+ return f"{self.__class__.__name__}(address={self.address})"
49
+
50
+ @property
51
+ @abstractmethod
52
+ def _topics(self) -> list: ...
53
+
54
+ @ASyncIterator.wrap # type: ignore [call-overload]
55
+ async def yield_thru_block(self, block: BlockNumber) -> AsyncIterator["Task[TokenTransfer]"]:
56
+ if not _logger_is_enabled_for(DEBUG):
57
+ async for task in self._objects_thru(block=block):
58
+ yield task
59
+ return
60
+
61
+ _logger_log(DEBUG, "%s yielding all objects thru block %s", (self, block))
62
+ async for task in self._objects_thru(block=block):
63
+ _logger_log(
64
+ DEBUG,
65
+ "yielding %s at block %s [thru: %s, lock: %s]",
66
+ (task, task.block, block, self._lock.value),
67
+ )
68
+ yield task
69
+ _logger_log(DEBUG, "%s yield thru %s complete", (self, block))
70
+
71
+ async def _extend(self, objs: list[evmspec.Log]) -> None:
72
+ shitcoins = SHITCOINS.get(chain.id, set())
73
+ append_loader_task = self._objects.append
74
+ done = 0
75
+ for log in objs:
76
+ if log.address in shitcoins:
77
+ continue
78
+ # save i/o
79
+ array_encodable_log = y._db.log.Log(**log)
80
+ task = create_task(load_token_transfer(array_encodable_log, self._load_prices))
81
+ task.block = log.block # type: ignore [attr-defined]
82
+ append_loader_task(task)
83
+ done += 1
84
+ # Make sure the event loop doesn't get blocked
85
+ if done % 100 == 0:
86
+ await sleep(0)
87
+
88
+ def _get_block_for_obj(self, task: "Task[TokenTransfer]") -> int:
89
+ return task.block # type: ignore [attr-defined]
90
+
91
+ def _process_event(self, task: "Task[TokenTransfer]") -> "Task[TokenTransfer]":
92
+ return task
93
+
94
+ def _done_callback(self, task: Task) -> None:
95
+ if e := task.exception():
96
+ self._exc = e
97
+ logger.exception(e)
98
+ raise e
99
+
100
+
101
+ class InboundTokenTransfers(_TokenTransfers):
102
+ """A container that fetches and iterates over all inbound token transfers for a particular wallet address"""
103
+
104
+ @property
105
+ def _topics(self) -> list:
106
+ return [TRANSFER_SIGS, None, encode_address(self.address)]
107
+
108
+
109
+ class OutboundTokenTransfers(_TokenTransfers):
110
+ """A container that fetches and iterates over all outbound token transfers for a particular wallet address"""
111
+
112
+ @property
113
+ def _topics(self) -> list:
114
+ return [TRANSFER_SIGS, encode_address(self.address)]
115
+
116
+
117
+ class TokenTransfers(ASyncIterable[TokenTransfer]):
118
+ """
119
+ A container that fetches and iterates over all token transfers for a particular wallet address.
120
+ NOTE: These do not come back in chronologcal order.
121
+ """
122
+
123
+ def __init__(
124
+ self,
125
+ address: ChecksumAddress,
126
+ from_block: BlockNumber,
127
+ load_prices: bool = False,
128
+ ) -> None:
129
+ self.transfers_in: Final = InboundTokenTransfers(
130
+ address, from_block, load_prices=load_prices
131
+ )
132
+ self.transfers_out: Final = OutboundTokenTransfers(
133
+ address, from_block, load_prices=load_prices
134
+ )
135
+
136
+ async def __aiter__(self) -> AsyncIterator["Task[TokenTransfer]"]:
137
+ async for transfer in self.__yield_thru_block(await dank_mids.eth.block_number):
138
+ yield transfer
139
+
140
+ def yield_thru_block(self, block: BlockNumber) -> ASyncIterator["Task[TokenTransfer]"]:
141
+ return ASyncIterator(self.__yield_thru_block(block))
142
+
143
+ def __yield_thru_block(self, block: BlockNumber) -> AsyncIterator["Task[TokenTransfer]"]:
144
+ return as_yielded(
145
+ self.transfers_in.yield_thru_block(block), self.transfers_out.yield_thru_block(block)
146
+ )