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,628 @@
1
+ """
2
+ Defines the data classes used to represent the various types of value-transfer actions on the blockchain. These include transactions, internal transfers, and token transfers.
3
+
4
+ The classes are designed to provide a consistent and flexible interface for working with blockchain data. Instance attributes can be fetched with either dot notation or key lookup. Classes are compatible with the standard dictionary interface.
5
+ """
6
+
7
+ from functools import cached_property
8
+ from typing import Any, ClassVar, Literal, TypeVar, Union, final
9
+
10
+ import evmspec
11
+ from brownie import chain
12
+ from dictstruct import DictStruct
13
+ from evmspec.data import Address, BlockHash, TransactionHash, Wei
14
+ from evmspec.structs.trace import reward
15
+ from evmspec.structs.transaction import AccessListEntry
16
+ from hexbytes import HexBytes
17
+ from msgspec import Struct
18
+ from y import Network
19
+ from y._db.log import Log
20
+ from y._decorators import stuck_coro_debugger
21
+ from y.constants import EEE_ADDRESS
22
+ from y.datatypes import Block
23
+
24
+ from eth_portfolio._decimal import Decimal
25
+ from eth_portfolio._utils import _get_price
26
+ from eth_portfolio.structs.modified import ModifiedTrace, _modified_trace_type_map
27
+
28
+
29
+ class _LedgerEntryBase(DictStruct, kw_only=True, frozen=True, omit_defaults=True, repr_omit_defaults=True): # type: ignore [call-arg]
30
+ """
31
+ The :class:`~structs._LedgerEntryBase` class is a base class for ledger entries representing on-chain actions in a blockchain.
32
+
33
+ Provides common attributes for transactions, internal transfers, and token transfers.
34
+
35
+ Extended by specific ledger entry types :class:`~structs.Transaction`, :class:`~structs.InternalTransfer`, and :class:`~structs.TokenTransfer`.
36
+ """
37
+
38
+ @property
39
+ def _evm_object(self) -> evmspec.Transaction | evmspec.FilterTrace | Log:
40
+ """
41
+ The EVM object associated with {cls_name}, exactly as it was received from the RPC.
42
+ """
43
+ return getattr(self, self.entry_type)
44
+
45
+ @property
46
+ def chainid(self) -> Network:
47
+ """
48
+ The network ID where the {cls_name} occurred.
49
+ """
50
+ try:
51
+ return Network(self._evm_object.chainId)
52
+ except AttributeError:
53
+ return Network(chain.id)
54
+
55
+ @property
56
+ def block_number(self) -> Block:
57
+ """
58
+ The block number where the {cls_name} was included.
59
+ """
60
+ return self._evm_object.block
61
+
62
+ price: Decimal | None = None
63
+ """
64
+ The price of the cryptocurrency at the time of the {cls_name}, if known.
65
+ """
66
+
67
+ value_usd: Decimal | None = None
68
+ """
69
+ The USD value of the cryptocurrency transferred in the {cls_name}, if price is known.
70
+ """
71
+
72
+ def __init_subclass__(cls, **kwargs: Any) -> None:
73
+ super().__init_subclass__(**kwargs)
74
+
75
+ # Replace {cls_name} in attribute-level docstrings
76
+ for key, attr in cls.__dict__.items():
77
+ if attr.__doc__ and "{cls_name}" in attr.__doc__:
78
+ attr.__doc__ = attr.__doc__.replace("{cls_name}", cls.__name__)
79
+
80
+
81
+ def _get_init_kwargs(original_struct: Struct) -> dict[str, Any]:
82
+ kwargs = {}
83
+ for key in original_struct.__struct_fields__:
84
+ try:
85
+ kwargs[key] = getattr(original_struct, key)
86
+ except AttributeError:
87
+ continue
88
+ return kwargs
89
+
90
+
91
+ class _TransactionBase(
92
+ _LedgerEntryBase,
93
+ kw_only=True,
94
+ frozen=True,
95
+ array_like=True,
96
+ forbid_unknown_fields=True,
97
+ omit_defaults=True,
98
+ repr_omit_defaults=True,
99
+ dict=True,
100
+ ):
101
+ """
102
+ The :class:`~structs.Transaction` class represents a complete on-chain blockchain transaction.
103
+
104
+ Contains detailed information about a single executed transaction on the blockchain,
105
+ including gas parameters, signature components, and transaction-specific data.
106
+
107
+ Example:
108
+ >>> tx = Transaction(tx=Transaction1559(...))
109
+ >>> tx.chainid
110
+ Network.Mainnet
111
+ >>> tx.type
112
+ 2
113
+ >>> tx.max_fee_per_gas
114
+ 30000000000
115
+ """
116
+
117
+ entry_type: ClassVar[Literal["transaction"]] = "transaction"
118
+ """
119
+ Constant indicating this value transfer is an on-chain transaction entry.
120
+ """
121
+
122
+ @classmethod
123
+ def from_rpc_response(
124
+ cls,
125
+ transaction: evmspec.Transaction,
126
+ *,
127
+ price: Decimal | None = None,
128
+ value_usd: Decimal | None = None,
129
+ ) -> "Transaction":
130
+ return cls(
131
+ transaction=transaction,
132
+ price=price,
133
+ value_usd=value_usd,
134
+ )
135
+
136
+ @property
137
+ def hash(self) -> TransactionHash:
138
+ """
139
+ The unique transaction hash.
140
+ """
141
+ return self.transaction.hash
142
+
143
+ @property
144
+ def block_hash(self) -> BlockHash:
145
+ """
146
+ The hash of the block that includes this Transaction.
147
+ """
148
+ return self.transaction.blockHash
149
+
150
+ @property
151
+ def transaction_index(self) -> int | None:
152
+ """
153
+ The index of the transaction within its block, if applicable.
154
+ """
155
+ return self.transaction.transactionIndex
156
+
157
+ @property
158
+ def nonce(self) -> int:
159
+ """
160
+ The sender's transaction count at the time of this Transaction.
161
+ """
162
+ return self.transaction.nonce
163
+
164
+ @cached_property
165
+ def type(self) -> int | None:
166
+ """
167
+ The transaction type (e.g., 0 for legacy, 1 for EIP-2930, 2 for EIP-1559).
168
+ None for chains that don't specify transaction types.
169
+ """
170
+ if typ := self.transaction.type:
171
+ return int(typ.hex(), 16)
172
+ else:
173
+ return None
174
+
175
+ @property
176
+ def from_address(self) -> Address | None:
177
+ """
178
+ The address from which the transaction was sent, if applicable.
179
+ """
180
+ return self.transaction.sender
181
+
182
+ @property
183
+ def to_address(self) -> Address | None:
184
+ """
185
+ The address to which the transaction was sent, if applicable.
186
+ """
187
+ return self.transaction.to
188
+
189
+ @property
190
+ def value(self) -> Decimal:
191
+ """
192
+ The value/amount of cryptocurrency transferred in the transaction, scaled to a human-readable decimal value.
193
+ """
194
+ return Wei(self.transaction.value).scaled
195
+
196
+ @property
197
+ def gas(self) -> int:
198
+ """
199
+ The maximum amount of gas the sender is willing to use for the Transaction.
200
+ """
201
+ return self.transaction.gas
202
+
203
+ @property
204
+ def gas_price(self) -> int:
205
+ """
206
+ The price per unit of gas the sender is willing to pay (for legacy and EIP-2930 transactions).
207
+ """
208
+ return self.transaction.gasPrice
209
+
210
+ @property
211
+ def max_fee_per_gas(self) -> int | None:
212
+ """
213
+ The maximum total fee per gas the sender is willing to pay (for EIP-1559 transactions only).
214
+ """
215
+ return self.transaction.maxFeePerGas
216
+
217
+ @property
218
+ def max_priority_fee_per_gas(self) -> int | None:
219
+ """
220
+ The maximum priority fee per gas the sender is willing to pay (for EIP-1559 transactions only).
221
+ """
222
+ return self.transaction.maxPriorityFeePerGas
223
+
224
+ @property
225
+ def input(self) -> HexBytes:
226
+ """
227
+ The data payload sent with the Transaction, often used for contract interactions.
228
+ """
229
+ return self.transaction.input
230
+
231
+ @property
232
+ def r(self) -> HexBytes:
233
+ """
234
+ The R component of the Transaction's ECDSA signature.
235
+ """
236
+ return self.transaction.r
237
+
238
+ @property
239
+ def s(self) -> HexBytes:
240
+ """
241
+ The S component of the Transaction's ECDSA signature.
242
+ """
243
+ return self.transaction.s
244
+
245
+ @property
246
+ def v(self) -> int:
247
+ """
248
+ The V component of the Transaction's ECDSA signature, used for replay protection.
249
+ """
250
+ return self.transaction.v
251
+
252
+ @property
253
+ def access_list(self) -> tuple[AccessListEntry, ...] | None:
254
+ """
255
+ List of addresses and storage keys the transaction plans to access (for EIP-2930 and EIP-1559 transactions).
256
+ """
257
+ return self.transaction.accessList
258
+
259
+ @property
260
+ def y_parity(self) -> int | None:
261
+ """
262
+ The Y parity of the transaction signature, used in EIP-2718 typed transactions.
263
+ """
264
+ return self.transaction.yParity
265
+
266
+ @property
267
+ def __db_primary_key__(self) -> dict[str, tuple[int, Address] | int]:
268
+ return {"from_address": (chain.id, self.from_address), "nonce": self.nonce}
269
+
270
+
271
+ @final
272
+ class Transaction(
273
+ _TransactionBase,
274
+ kw_only=True,
275
+ frozen=True,
276
+ array_like=True,
277
+ forbid_unknown_fields=True,
278
+ omit_defaults=True,
279
+ repr_omit_defaults=True,
280
+ dict=True,
281
+ ):
282
+
283
+ transaction: evmspec.Transaction
284
+ """
285
+ The transaction object received by calling eth_getTransactionByHash.
286
+ """
287
+
288
+
289
+ @final
290
+ class TransactionRLP(
291
+ _TransactionBase,
292
+ kw_only=True,
293
+ frozen=True,
294
+ array_like=True,
295
+ forbid_unknown_fields=True,
296
+ omit_defaults=True,
297
+ repr_omit_defaults=True,
298
+ dict=True,
299
+ ):
300
+
301
+ transaction: evmspec.TransactionRLP
302
+ """
303
+ The transaction object received by calling eth_getTransactionByHash.
304
+ """
305
+
306
+
307
+ @final
308
+ class InternalTransfer(
309
+ _LedgerEntryBase,
310
+ kw_only=True,
311
+ frozen=True,
312
+ array_like=True,
313
+ forbid_unknown_fields=True,
314
+ omit_defaults=True,
315
+ repr_omit_defaults=True,
316
+ ):
317
+ """
318
+ The :class:`~structs.InternalTransfer`class represents an internal transfer or call within a blockchain transaction.
319
+
320
+ Captures operations that occur during transaction execution, such as contract-to-contract calls,
321
+ contract creations, or self-destructs. These are not separate on-chain transactions but part of
322
+ the execution of a single transaction.
323
+
324
+ Example:
325
+ >>> internal_tx = InternalTransfer(trace=evmspec.FilterTrace(...), ...)
326
+ >>> internal_tx.type
327
+ 'call'
328
+ >>> internal_tx.trace_address
329
+ '0.1'
330
+ >>> internal_tx.gas_used
331
+ 21000
332
+ """
333
+
334
+ @staticmethod
335
+ @stuck_coro_debugger
336
+ async def from_trace(trace: evmspec.FilterTrace, load_prices: bool) -> "InternalTransfer":
337
+ # sourcery skip: simplify-boolean-comparison
338
+ """
339
+ Asynchronously processes a raw internal transfer dictionary into an InternalTransfer object.
340
+
341
+ This function is the core of the internal transfer processing pipeline. It handles
342
+ various types of transfers, including special cases like block and uncle rewards.
343
+ It also filters out certain transfers (e.g., to Gnosis Safe Singleton) and verifies
344
+ transaction success for non-reward transfers.
345
+
346
+ The function performs several data transformations:
347
+ - Value and gas conversions
348
+ - Optional USD price loading
349
+ - Field standardization
350
+
351
+ Args:
352
+ transfer: A dictionary containing the raw internal transfer data. Expected to have keys such as
353
+ 'type', 'transactionHash', 'blockNumber', 'from', 'to', 'value', 'gas', 'gasUsed', 'traceAddress'.
354
+ load_prices: Flag to determine whether to load USD prices for the transfer value.
355
+
356
+ Returns:
357
+ A processed InternalTransfer object.
358
+
359
+ Example:
360
+ >>> transfer = {"type": "call", "transactionHash": "0x123...", "blockNumber": 15537393, "from": "0xA0b86991c6218b36c1d19D4a2e9Eb0cE3606eB48", "to": "0x7a250d5630B4cF539739dF2C5dAcb4c659F2488D", "value": "0x10", "gas": "0x5208", "gasUsed": "0x5208", "traceAddress": [0]}
361
+ >>> internal_tx = await load_internal_transfer(transfer=transfer, load_prices=True); print(internal_tx)
362
+
363
+ Note:
364
+ - Transfers to the Gnosis Safe Singleton (0xd9Db270c1B5E3Bd161E8c8503c55cEABeE709552) are filtered out
365
+ as they typically don't represent actual value transfers.
366
+ - The `traceAddress` is converted to a string for consistent representation across different scenarios.
367
+ - For block and uncle rewards, `gas` is set to 0 as these are not regular transactions.
368
+ - When loading prices, the `EEE_ADDRESS` constant is used, which represents the native currency of the chain.
369
+
370
+ Integration with eth_portfolio ecosystem:
371
+ - Uses the InternalTransfer struct from eth_portfolio.structs for standardized output.
372
+ - Utilizes utility functions from eth_portfolio._loaders.utils and eth_portfolio._utils.
373
+ - Interacts with the global 'chain' object from the brownie library for chain ID.
374
+ """
375
+
376
+ modified_cls = _modified_trace_type_map[type(trace)]
377
+ modified_trace = modified_cls(**_get_init_kwargs(trace))
378
+
379
+ if load_prices is False:
380
+ return InternalTransfer(trace=modified_trace)
381
+
382
+ price = await _get_price(EEE_ADDRESS, trace.block)
383
+ value_usd = round(trace.action.value.scaled * price, 18)
384
+ return InternalTransfer(trace=modified_trace, price=price, value_usd=value_usd)
385
+
386
+ entry_type: ClassVar[Literal["internal_transfer"]] = "internal_transfer"
387
+ """
388
+ Constant indicating this value transfer is an internal transfer or call entry.
389
+ """
390
+
391
+ @property
392
+ def _evm_object(self) -> ModifiedTrace:
393
+ return self.trace
394
+
395
+ trace: ModifiedTrace
396
+ """
397
+ The raw trace object associated with this internal transfer.
398
+ """
399
+
400
+ @property
401
+ def block_hash(self) -> HexBytes:
402
+ """
403
+ The hash of the block containing the transaction that includes this InternalTransfer.
404
+ """
405
+ return self.trace.blockHash
406
+
407
+ @property
408
+ def transaction_index(self) -> int | None:
409
+ """
410
+ The index of the transaction within its block, if applicable.
411
+ """
412
+ return self.trace.transactionPosition
413
+
414
+ @property
415
+ def hash(self) -> HexBytes | str:
416
+ """
417
+ The unique hash of the transaction containing this internal transfer.
418
+ """
419
+ return (
420
+ f"{self.trace.action.rewardType.name} reward"
421
+ if isinstance(self.trace, reward.Trace)
422
+ else self.trace.transactionHash
423
+ )
424
+
425
+ @property
426
+ def from_address(self) -> Address:
427
+ """
428
+ The address from which the internal transfer was sent, if applicable.
429
+ """
430
+ return self.trace.action.sender
431
+
432
+ @property
433
+ def to_address(self) -> Address | None:
434
+ """
435
+ The address to which the internal transfer was sent, if applicable.
436
+ """
437
+ action = self.trace.action
438
+ # NOTE: for block reward transfers, the recipient is 'author'
439
+ return action.author if isinstance(action, reward.Action) else action.to
440
+
441
+ @property
442
+ def value(self) -> Decimal:
443
+ """
444
+ The value/amount of cryptocurrency transferred in the internal transfer, scaled to a human-readable decimal value.
445
+ """
446
+ return self.trace.action.value.scaled
447
+
448
+ @property
449
+ def type(self) -> Literal["call", "create", "reward", "suicide"]:
450
+ return self.trace.type
451
+
452
+ @property
453
+ def trace_address(self) -> list[int]:
454
+ """
455
+ The path of sub-calls to reach this InternalTransfer within the transaction,
456
+
457
+ Example:
458
+ - The following trace_address
459
+ ```python
460
+ [0, 1, 2]
461
+ ```
462
+ represents the third sub-call of the second sub-call of the first top-level call.
463
+ """
464
+ return self.trace.traceAddress
465
+
466
+ @property
467
+ def gas(self) -> int:
468
+ """
469
+ The amount of gas allocated for this internal operation.
470
+ """
471
+ return 0 if isinstance(self.trace, reward.Trace) else self.trace.action.gas
472
+
473
+ @property
474
+ def gas_used(self) -> int:
475
+ """
476
+ The amount of gas actually consumed by this internal operation, if known.
477
+ """
478
+ return self.trace.result.gasUsed
479
+
480
+ @property
481
+ def subtraces(self) -> int:
482
+ """
483
+ The number of sub-operations spawned by this InternalTransfer.
484
+ """
485
+ return self.trace.subtraces
486
+
487
+ @property
488
+ def call_type(self) -> str | None:
489
+ """
490
+ The type of call made in this InternalTransfer (e.g., "call", "delegatecall", "staticcall").
491
+ """
492
+ return self.trace.action.callType.name
493
+
494
+ @property
495
+ def reward_type(self) -> str | None:
496
+ """
497
+ The type of the reward, for reward transactions.
498
+ """
499
+ return self.trace.action.rewardType.name
500
+
501
+ @property
502
+ def input(self) -> HexBytes:
503
+ """
504
+ The input data for this internal operation, if any.
505
+ """
506
+ return self.trace.action.input
507
+
508
+ @property
509
+ def output(self) -> HexBytes:
510
+ """
511
+ The output data from this internal operation, if any.
512
+ """
513
+ return self.trace.result.output
514
+
515
+ '''
516
+ init: HexBytes | None = UNSET
517
+ """
518
+ The initialization code for contract creation, if this is a create operation.
519
+ """
520
+
521
+ address: HexBytes | None = UNSET
522
+ """
523
+ The address of the account or contract involved in this InternalTransfer.
524
+ """
525
+
526
+ code: HexBytes | None = UNSET
527
+ """
528
+ The code of the contract involved in this InternalTransfer, if applicable.
529
+ """
530
+ '''
531
+
532
+
533
+ @final
534
+ class TokenTransfer(
535
+ _LedgerEntryBase,
536
+ kw_only=True,
537
+ frozen=True,
538
+ array_like=True,
539
+ forbid_unknown_fields=True,
540
+ omit_defaults=True,
541
+ ):
542
+ """
543
+ The :class:`~structs.TokenTransfer` class represents a token transfer event within a blockchain transaction.
544
+
545
+ Captures the movement of ERC20-like tokens between addresses. These are typically
546
+ emitted as events by token contracts and are not separate transactions but part of
547
+ the execution of a transaction interacting with the token contract.
548
+
549
+ Example:
550
+ >>> token_transfer = TokenTransfer(
551
+ ... log=Log(...),
552
+ ... value=Decimal("1000000"), # 1 USDC (assuming 6 decimals)
553
+ ... token="USDC",
554
+ ... )
555
+ >>> token_transfer.token
556
+ 'USDC'
557
+ >>> token_transfer.value
558
+ Decimal('1000000')
559
+ >>> token_transfer.log_index
560
+ 3
561
+ """
562
+
563
+ entry_type: ClassVar[Literal["token_transfer"]] = "token_transfer"
564
+ """
565
+ Constant indicating this value transfer is a token transfer entry.
566
+ """
567
+
568
+ log: Log
569
+ """
570
+ The log associated with this token transfer.
571
+ """
572
+
573
+ @property
574
+ def from_address(self) -> Address:
575
+ return self.log.topic1.as_address
576
+
577
+ @property
578
+ def to_address(self) -> Address:
579
+ return self.log.topic2.as_address
580
+
581
+ @property
582
+ def _evm_object(self) -> Log:
583
+ return self.log
584
+
585
+ transaction_index: int
586
+
587
+ @property
588
+ def log_index(self) -> int:
589
+ """
590
+ The index of this transfer event within the transaction logs.
591
+ Used to uniquely identify the Transfer event associated with this TokenTransfer within the transaction.
592
+ """
593
+ return self.log.logIndex
594
+
595
+ token: str | None
596
+ """
597
+ The symbol of the token being transferred, if known.
598
+ """
599
+
600
+ @property
601
+ def hash(self) -> HexBytes:
602
+ """
603
+ The unique hash of the transaction containing this token transfer.
604
+ """
605
+ return self.log.transactionHash
606
+
607
+ @property
608
+ def token_address(self) -> Address:
609
+ """
610
+ The contract address of the token being transferred.
611
+ """
612
+ return self.log.address
613
+
614
+ value: Decimal
615
+ """
616
+ The amount of tokens transferred, scaled to a human-readable decimal value.
617
+ """
618
+
619
+
620
+ LedgerEntry = Union[Transaction, InternalTransfer, TokenTransfer]
621
+ """
622
+ Type alias representing any type of ledger entry (:class:`~structs.Transaction`, :class:`~structs.InternalTransfer`, or :class:`~structs.TokenTransfer`).
623
+ """
624
+
625
+ _LE = TypeVar("_LE", Transaction, InternalTransfer, TokenTransfer)
626
+ """
627
+ Type variable for generic operations on ledger entries. Can be :class:`~structs.Transaction`, :class:`~structs.InternalTransfer`, or :class:`~structs.TokenTransfer`.
628
+ """