forked from ethereum/web3.py
/
main.py
379 lines (334 loc) · 9.67 KB
/
main.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
import decimal
from eth_abi.codec import (
ABICodec,
)
from eth_utils import (
add_0x_prefix,
apply_to_return_value,
from_wei,
is_address,
is_checksum_address,
keccak as eth_utils_keccak,
remove_0x_prefix,
to_bytes,
to_checksum_address,
to_int,
to_text,
to_wei,
)
from functools import (
wraps,
)
from hexbytes import (
HexBytes,
)
from typing import (
Any,
Dict,
List,
Optional,
Sequence,
Type,
TYPE_CHECKING,
Union,
cast,
)
from eth_typing import (
AnyAddress,
ChecksumAddress,
HexStr,
Primitives,
)
from eth_typing.abi import TypeStr
from eth_utils import (
combomethod,
)
from ens import ENS
from web3._utils.abi import (
build_default_registry,
build_strict_registry,
map_abi_data,
)
from web3._utils.decorators import (
deprecated_for,
)
from web3._utils.empty import (
empty,
)
from web3._utils.encoding import (
hex_encode_abi_type,
to_hex,
to_json,
)
from web3._utils.rpc_abi import (
RPC,
)
from web3._utils.module import (
attach_modules,
)
from web3._utils.normalizers import (
abi_ens_resolver,
)
from web3.eth import (
Eth,
)
from web3.geth import (
Geth,
GethAdmin,
GethMiner,
GethPersonal,
GethTxPool,
)
from web3.iban import (
Iban,
)
from web3.manager import (
RequestManager as DefaultRequestManager,
)
from web3.module import (
Module,
)
from web3.net import (
AsyncNet,
Net,
)
from web3.parity import (
Parity,
ParityPersonal,
)
from web3.providers import (
BaseProvider,
)
from web3.providers.eth_tester import (
EthereumTesterProvider,
)
from web3.providers.ipc import (
IPCProvider,
)
from web3.providers.async_rpc import (
AsyncHTTPProvider,
)
from web3.providers.rpc import (
HTTPProvider,
)
from web3.providers.websocket import (
WebsocketProvider,
)
from web3.testing import (
Testing,
)
from web3.types import ( # noqa: F401
Middleware,
MiddlewareOnion,
Wei,
)
from web3.version import (
Version,
)
if TYPE_CHECKING:
from web3.pm import PM # noqa: F401
def get_default_modules() -> Dict[str, Union[Type[Module], Sequence[Any]]]:
return {
"eth": Eth,
"net": Net,
"version": Version,
"parity": (Parity, {
"personal": ParityPersonal,
}),
"geth": (Geth, {
"admin": GethAdmin,
"miner": GethMiner,
"personal": GethPersonal,
"txpool": GethTxPool,
}),
"testing": Testing,
}
class Web3:
# Providers
HTTPProvider = HTTPProvider
IPCProvider = IPCProvider
EthereumTesterProvider = EthereumTesterProvider
WebsocketProvider = WebsocketProvider
AsyncHTTPProvider = AsyncHTTPProvider
# Managers
RequestManager = DefaultRequestManager
# Iban
Iban = Iban
# Encoding and Decoding
@staticmethod
@wraps(to_bytes)
def toBytes(
primitive: Primitives = None, hexstr: HexStr = None, text: str = None
) -> bytes:
return to_bytes(primitive, hexstr, text)
@staticmethod
@wraps(to_int)
def toInt(
primitive: Primitives = None, hexstr: HexStr = None, text: str = None
) -> int:
return to_int(primitive, hexstr, text)
@staticmethod
@wraps(to_hex)
def toHex(
primitive: Primitives = None, hexstr: HexStr = None, text: str = None
) -> HexStr:
return to_hex(primitive, hexstr, text)
@staticmethod
@wraps(to_text)
def toText(
primitive: Primitives = None, hexstr: HexStr = None, text: str = None
) -> str:
return to_text(primitive, hexstr, text)
@staticmethod
@wraps(to_json)
def toJSON(obj: Dict[Any, Any]) -> str:
return to_json(obj)
# Currency Utility
@staticmethod
@wraps(to_wei)
def toWei(number: Union[int, float, str, decimal.Decimal], unit: str) -> Wei:
return cast(Wei, to_wei(number, unit))
@staticmethod
@wraps(from_wei)
def fromWei(number: int, unit: str) -> Union[int, decimal.Decimal]:
return from_wei(number, unit)
# Address Utility
@staticmethod
@wraps(is_address)
def isAddress(value: Any) -> bool:
return is_address(value)
@staticmethod
@wraps(is_checksum_address)
def isChecksumAddress(value: Any) -> bool:
return is_checksum_address(value)
@staticmethod
@wraps(to_checksum_address)
def toChecksumAddress(value: Union[AnyAddress, str, bytes]) -> ChecksumAddress:
return to_checksum_address(value)
# mypy Types
eth: Eth
parity: Parity
geth: Geth
net: Net
async_net: AsyncNet
def __init__(
self,
provider: Optional[BaseProvider] = None,
middlewares: Optional[Sequence[Any]] = None,
modules: Optional[Dict[str, Union[Type[Module], Sequence[Any]]]] = None,
external_modules: Optional[Dict[str, Union[Type[Module], Sequence[Any]]]] = None,
ens: ENS = cast(ENS, empty)
) -> None:
self.manager = self.RequestManager(self, provider, middlewares)
# this codec gets used in the module initialization,
# so it needs to come before attach_modules
self.codec = ABICodec(build_default_registry())
if modules is None:
modules = get_default_modules()
attach_modules(self, modules)
if external_modules is not None:
attach_modules(self, external_modules)
self.ens = ens
@property
def middleware_onion(self) -> MiddlewareOnion:
return self.manager.middleware_onion
@property
def provider(self) -> BaseProvider:
return self.manager.provider
@provider.setter
def provider(self, provider: BaseProvider) -> None:
self.manager.provider = provider
@property
def clientVersion(self) -> str:
return self.manager.request_blocking(RPC.web3_clientVersion, [])
@property
def api(self) -> str:
from web3 import __version__
return __version__
@staticmethod
@deprecated_for("keccak")
@apply_to_return_value(HexBytes)
def sha3(primitive: Optional[Primitives] = None, text: Optional[str] = None,
hexstr: Optional[HexStr] = None) -> bytes:
return Web3.keccak(primitive, text, hexstr)
@staticmethod
@apply_to_return_value(HexBytes)
def keccak(primitive: Optional[Primitives] = None, text: Optional[str] = None,
hexstr: Optional[HexStr] = None) -> bytes:
if isinstance(primitive, (bytes, int, type(None))):
input_bytes = to_bytes(primitive, hexstr=hexstr, text=text)
return eth_utils_keccak(input_bytes)
raise TypeError(
"You called keccak with first arg %r and keywords %r. You must call it with one of "
"these approaches: keccak(text='txt'), keccak(hexstr='0x747874'), "
"keccak(b'\\x74\\x78\\x74'), or keccak(0x747874)." % (
primitive,
{'text': text, 'hexstr': hexstr}
)
)
@combomethod
@deprecated_for("solidityKeccak")
def soliditySha3(cls, abi_types: List[TypeStr], values: List[Any]) -> bytes:
return cls.solidityKeccak(abi_types, values)
@combomethod
def solidityKeccak(cls, abi_types: List[TypeStr], values: List[Any]) -> bytes:
"""
Executes keccak256 exactly as Solidity does.
Takes list of abi_types as inputs -- `[uint24, int8[], bool]`
and list of corresponding values -- `[20, [-1, 5, 0], True]`
"""
if len(abi_types) != len(values):
raise ValueError(
"Length mismatch between provided abi types and values. Got "
"{0} types and {1} values.".format(len(abi_types), len(values))
)
if isinstance(cls, type):
w3 = None
else:
w3 = cls
normalized_values = map_abi_data([abi_ens_resolver(w3)], abi_types, values)
hex_string = add_0x_prefix(HexStr(''.join(
remove_0x_prefix(hex_encode_abi_type(abi_type, value))
for abi_type, value
in zip(abi_types, normalized_values)
)))
return cls.keccak(hexstr=hex_string)
def attach_modules(
self,
modules: Optional[Dict[str, Union[Type[Module], Sequence[Any]]]]
) -> None:
"""
Attach modules to the `Web3` instance. Modules should inherit from the `web3.module.Module`
class.
"""
attach_modules(self, modules)
def isConnected(self) -> bool:
return self.provider.isConnected()
def is_encodable(self, _type: TypeStr, value: Any) -> bool:
return self.codec.is_encodable(_type, value)
@property
def ens(self) -> ENS:
if self._ens is cast(ENS, empty):
return ENS.fromWeb3(self)
else:
return self._ens
@ens.setter
def ens(self, new_ens: ENS) -> None:
self._ens = new_ens
@property
def pm(self) -> "PM":
if hasattr(self, '_pm'):
# ignored b/c property is dynamically set via enable_unstable_package_management_api
return self._pm # type: ignore
else:
raise AttributeError(
"The Package Management feature is disabled by default until "
"its API stabilizes. To use these features, please enable them by running "
"`w3.enable_unstable_package_management_api()` and try again."
)
def enable_unstable_package_management_api(self) -> None:
from web3.pm import PM # noqa: F811
if not hasattr(self, '_pm'):
attach_modules(self, {'_pm': (PM,)})
def enable_strict_bytes_type_checking(self) -> None:
self.codec = ABICodec(build_strict_registry())