forked from ethereum/web3.py
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtypes.py
474 lines (381 loc) · 10.7 KB
/
types.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
from typing import (
TYPE_CHECKING,
Any,
Callable,
Coroutine,
Dict,
List,
NewType,
Optional,
Sequence,
Type,
TypeVar,
Union,
)
from eth_typing import (
Address,
BlockNumber,
ChecksumAddress,
Hash32,
HexStr,
)
from hexbytes import (
HexBytes,
)
from web3._utils.compat import (
Literal,
TypedDict,
)
from web3._utils.function_identifiers import (
FallbackFn,
ReceiveFn,
)
from web3.datastructures import (
NamedElementOnion,
)
if TYPE_CHECKING:
from web3.contract.async_contract import AsyncContractFunction # noqa: F401
from web3.contract.contract import ContractFunction # noqa: F401
from web3.main import ( # noqa: F401
AsyncWeb3,
Web3,
)
TReturn = TypeVar("TReturn")
TParams = TypeVar("TParams")
TValue = TypeVar("TValue")
BlockParams = Literal["latest", "earliest", "pending", "safe", "finalized"]
BlockIdentifier = Union[BlockParams, BlockNumber, Hash32, HexStr, HexBytes, int]
LatestBlockParam = Literal["latest"]
FunctionIdentifier = Union[str, Type[FallbackFn], Type[ReceiveFn]]
# bytes, hexbytes, or hexstr representing a 32 byte hash
_Hash32 = Union[Hash32, HexBytes, HexStr]
EnodeURI = NewType("EnodeURI", str)
ENS = NewType("ENS", str)
Nonce = NewType("Nonce", int)
RPCEndpoint = NewType("RPCEndpoint", str)
Timestamp = NewType("Timestamp", int)
Wei = NewType("Wei", int)
Gwei = NewType("Gwei", int)
Formatters = Dict[RPCEndpoint, Callable[..., Any]]
class AccessListEntry(TypedDict):
address: HexStr
storageKeys: Sequence[HexStr]
AccessList = NewType("AccessList", Sequence[AccessListEntry])
# todo: move these to eth_typing once web3 is type hinted
class ABIEventParams(TypedDict, total=False):
indexed: bool
name: str
type: str
class ABIEvent(TypedDict, total=False):
anonymous: bool
inputs: Sequence["ABIEventParams"]
name: str
type: Literal["event"]
class ABIFunctionComponents(TypedDict, total=False):
# better typed as Sequence['ABIFunctionComponents'], but recursion isnt possible yet
# https://github.com/python/mypy/issues/731
components: Sequence[Any]
name: str
type: str
class ABIFunctionParams(TypedDict, total=False):
components: Sequence["ABIFunctionComponents"]
name: str
type: str
class ABIFunction(TypedDict, total=False):
constant: bool
inputs: Sequence["ABIFunctionParams"]
name: str
outputs: Sequence["ABIFunctionParams"]
payable: bool
stateMutability: Literal["pure", "view", "nonpayable", "payable"]
type: Literal["function", "constructor", "fallback", "receive"]
ABIElement = Union[ABIFunction, ABIEvent]
ABI = Sequence[Union[ABIFunction, ABIEvent]]
class EventData(TypedDict):
address: ChecksumAddress
args: Dict[str, Any]
blockHash: HexBytes
blockNumber: int
event: str
logIndex: int
transactionHash: HexBytes
transactionIndex: int
class RPCError(TypedDict):
code: int
message: str
data: Optional[str]
class RPCResponse(TypedDict, total=False):
error: Union[RPCError, str]
id: int
jsonrpc: Literal["2.0"]
result: Any
Middleware = Callable[[Callable[[RPCEndpoint, Any], RPCResponse], "Web3"], Any]
AsyncMiddlewareCoroutine = Callable[
[RPCEndpoint, Any], Coroutine[Any, Any, RPCResponse]
]
AsyncMiddleware = Callable[
[Callable[[RPCEndpoint, Any], RPCResponse], "AsyncWeb3"], Any
]
MiddlewareOnion = NamedElementOnion[str, Middleware]
AsyncMiddlewareOnion = NamedElementOnion[str, AsyncMiddleware]
class FormattersDict(TypedDict, total=False):
error_formatters: Optional[Formatters]
request_formatters: Optional[Formatters]
result_formatters: Optional[Formatters]
class FilterParams(TypedDict, total=False):
address: Union[Address, ChecksumAddress, List[Address], List[ChecksumAddress]]
blockHash: HexBytes
fromBlock: BlockIdentifier
toBlock: BlockIdentifier
topics: Sequence[Optional[Union[_Hash32, Sequence[_Hash32]]]]
class FeeHistory(TypedDict):
baseFeePerGas: List[Wei]
gasUsedRatio: List[float]
oldestBlock: BlockNumber
reward: List[List[Wei]]
class LogReceipt(TypedDict):
address: ChecksumAddress
blockHash: HexBytes
blockNumber: BlockNumber
data: HexStr
logIndex: int
payload: HexBytes
removed: bool
topic: HexBytes
topics: Sequence[HexBytes]
transactionHash: HexBytes
transactionIndex: int
# syntax b/c "from" keyword not allowed w/ class construction
TxData = TypedDict(
"TxData",
{
"accessList": AccessList,
"blockHash": HexBytes,
"blockNumber": BlockNumber,
"chainId": int,
"data": Union[bytes, HexStr],
"from": ChecksumAddress,
"gas": int,
"gasPrice": Wei,
"maxFeePerGas": Wei,
"maxPriorityFeePerGas": Wei,
"hash": HexBytes,
"input": HexStr,
"nonce": Nonce,
"r": HexBytes,
"s": HexBytes,
"to": ChecksumAddress,
"transactionIndex": int,
"type": Union[int, HexStr],
"v": int,
"value": Wei,
},
total=False,
)
# syntax b/c "from" keyword not allowed w/ class construction
TxParams = TypedDict(
"TxParams",
{
"chainId": int,
"data": Union[bytes, HexStr],
# addr or ens
"from": Union[Address, ChecksumAddress, str],
"gas": int,
# legacy pricing
"gasPrice": Wei,
# dynamic fee pricing
"maxFeePerGas": Union[str, Wei],
"maxPriorityFeePerGas": Union[str, Wei],
"nonce": Nonce,
# addr or ens
"to": Union[Address, ChecksumAddress, str],
"type": Union[int, HexStr],
"value": Wei,
},
total=False,
)
WithdrawalData = TypedDict(
"WithdrawalData",
{
"index": int,
"validator_index": int,
"address": ChecksumAddress,
"amount": Gwei,
},
)
CallOverrideParams = TypedDict(
"CallOverrideParams",
{
"balance": Optional[Wei],
"nonce": Optional[int],
"code": Optional[Union[bytes, HexStr]],
"state": Optional[Dict[HexStr, HexStr]],
"stateDiff": Optional[Dict[HexStr, HexStr]],
},
total=False,
)
CallOverride = Dict[ChecksumAddress, CallOverrideParams]
GasPriceStrategy = Union[
Callable[["Web3", TxParams], Wei], Callable[["AsyncWeb3", TxParams], Wei]
]
# syntax b/c "from" keyword not allowed w/ class construction
TxReceipt = TypedDict(
"TxReceipt",
{
"blockHash": HexBytes,
"blockNumber": BlockNumber,
"contractAddress": Optional[ChecksumAddress],
"cumulativeGasUsed": int,
"effectiveGasPrice": Wei,
"gasUsed": int,
"from": ChecksumAddress,
"logs": List[LogReceipt],
"logsBloom": HexBytes,
"root": HexStr,
"status": int,
"to": ChecksumAddress,
"transactionHash": HexBytes,
"transactionIndex": int,
},
)
class SignedTx(TypedDict, total=False):
raw: bytes
tx: TxParams
class StorageProof(TypedDict):
key: HexStr
proof: Sequence[HexStr]
value: HexBytes
class MerkleProof(TypedDict):
address: ChecksumAddress
accountProof: Sequence[HexStr]
balance: int
codeHash: HexBytes
nonce: Nonce
storageHash: HexBytes
storageProof: Sequence[StorageProof]
class Protocol(TypedDict):
difficulty: int
head: HexStr
network: int
version: int
class NodeInfo(TypedDict):
enode: EnodeURI
id: HexStr
ip: str
listenAddr: str
name: str
ports: Dict[str, int]
protocols: Dict[str, Protocol]
class Peer(TypedDict, total=False):
caps: Sequence[str]
id: HexStr
name: str
network: Dict[str, str]
protocols: Dict[str, Protocol]
class SyncStatus(TypedDict):
currentBlock: int
highestBlock: int
knownStates: int
pulledStates: int
startingBlock: int
class BlockData(TypedDict, total=False):
baseFeePerGas: Wei
difficulty: int
extraData: HexBytes
gasLimit: int
gasUsed: int
hash: HexBytes
logsBloom: HexBytes
miner: ChecksumAddress
mixHash: HexBytes
nonce: HexBytes
number: BlockNumber
parentHash: HexBytes
receiptsRoot: HexBytes
sha3Uncles: HexBytes
size: int
stateRoot: HexBytes
timestamp: Timestamp
totalDifficulty: int
transactions: Union[Sequence[HexBytes], Sequence[TxData]]
transactionsRoot: HexBytes
uncles: Sequence[HexBytes]
withdrawals: Sequence[WithdrawalData]
withdrawalsRoot: HexBytes
# geth_poa_middleware replaces extraData w/ proofOfAuthorityData
proofOfAuthorityData: HexBytes
class Uncle(TypedDict):
author: ChecksumAddress
difficulty: HexStr
extraData: HexStr
gasLimit: HexStr
gasUsed: HexStr
hash: HexBytes
logsBloom: HexStr
miner: HexBytes
mixHash: HexBytes
nonce: HexStr
number: HexStr
parentHash: HexBytes
receiptsRoot: HexBytes
sealFields: Sequence[HexStr]
sha3Uncles: HexBytes
size: int
stateRoot: HexBytes
timestamp: Timestamp
totalDifficulty: HexStr
transactions: Sequence[HexBytes]
transactionsRoot: HexBytes
uncles: Sequence[HexBytes]
#
# txpool types
#
# syntax b/c "from" keyword not allowed w/ class construction
PendingTx = TypedDict(
"PendingTx",
{
"blockHash": HexBytes,
"blockNumber": None,
"from": ChecksumAddress,
"gas": HexBytes,
"maxFeePerGas": HexBytes,
"maxPriorityFeePerGas": HexBytes,
"gasPrice": HexBytes,
"hash": HexBytes,
"input": HexBytes,
"nonce": HexBytes,
"to": ChecksumAddress,
"transactionIndex": None,
"value": HexBytes,
},
total=False,
)
class TxPoolContent(TypedDict, total=False):
pending: Dict[ChecksumAddress, Dict[Nonce, List[PendingTx]]]
queued: Dict[ChecksumAddress, Dict[Nonce, List[PendingTx]]]
class TxPoolInspect(TypedDict, total=False):
pending: Dict[ChecksumAddress, Dict[Nonce, str]]
queued: Dict[ChecksumAddress, Dict[Nonce, str]]
class TxPoolStatus(TypedDict, total=False):
pending: int
queued: int
#
# web3.geth types
#
class GethWallet(TypedDict):
accounts: Sequence[Dict[str, str]]
status: str
url: str
# Contract types
TContractFn = TypeVar("TContractFn", "ContractFunction", "AsyncContractFunction")
# Tracing types
BlockTrace = NewType("BlockTrace", Dict[str, Any])
FilterTrace = NewType("FilterTrace", Dict[str, Any])
TraceMode = Sequence[Literal["trace", "vmTrace", "stateDiff"]]
class TraceFilterParams(TypedDict, total=False):
after: int
count: int
fromAddress: Sequence[Union[Address, ChecksumAddress, ENS]]
fromBlock: BlockIdentifier
toAddress: Sequence[Union[Address, ChecksumAddress, ENS]]
toBlock: BlockIdentifier