mm-eth 0.5.12__py3-none-any.whl → 0.5.15__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.
mm_eth/rpc_async.py
ADDED
|
@@ -0,0 +1,170 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import string
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
import ens.utils
|
|
7
|
+
import eth_utils
|
|
8
|
+
import websockets
|
|
9
|
+
from mm_std import DataResult, http_request
|
|
10
|
+
|
|
11
|
+
DEFAULT_TIMEOUT = 7.0
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
async def rpc_call(
|
|
15
|
+
node: str,
|
|
16
|
+
method: str,
|
|
17
|
+
params: Sequence[object],
|
|
18
|
+
timeout: float,
|
|
19
|
+
proxy: str | None,
|
|
20
|
+
id_: int = 1,
|
|
21
|
+
) -> DataResult[Any]:
|
|
22
|
+
data = {"jsonrpc": "2.0", "method": method, "params": params, "id": id_}
|
|
23
|
+
if node.startswith("http"):
|
|
24
|
+
return await _http_call(node, data, timeout, proxy)
|
|
25
|
+
return await _ws_call(node, data, timeout)
|
|
26
|
+
|
|
27
|
+
|
|
28
|
+
async def _http_call(node: str, data: dict[str, object], timeout: float, proxy: str | None) -> DataResult[Any]:
|
|
29
|
+
res = await http_request(node, method="POST", proxy=proxy, timeout=timeout, json=data)
|
|
30
|
+
if res.is_error():
|
|
31
|
+
return res.to_data_result_err()
|
|
32
|
+
try:
|
|
33
|
+
parsed_body = res.parse_json_body()
|
|
34
|
+
err = parsed_body.get("error", {}).get("message", "")
|
|
35
|
+
if err:
|
|
36
|
+
return res.to_data_result_err(f"service_error: {err}")
|
|
37
|
+
if "result" in parsed_body:
|
|
38
|
+
return res.to_data_result_ok(parsed_body["result"])
|
|
39
|
+
return res.to_data_result_err("unknown_response")
|
|
40
|
+
except Exception as err:
|
|
41
|
+
return res.to_data_result_err(f"exception: {err}")
|
|
42
|
+
|
|
43
|
+
|
|
44
|
+
async def _ws_call(node: str, data: dict[str, object], timeout: float) -> DataResult[Any]:
|
|
45
|
+
try:
|
|
46
|
+
async with websockets.connect(node, timeout=timeout) as ws:
|
|
47
|
+
await ws.send(json.dumps(data))
|
|
48
|
+
response = json.loads(await ws.recv())
|
|
49
|
+
|
|
50
|
+
err = response.get("error", {}).get("message", "")
|
|
51
|
+
if err:
|
|
52
|
+
return DataResult(err=f"service_error: {err}", data=response)
|
|
53
|
+
if "result" in response:
|
|
54
|
+
return DataResult(ok=response["result"], data=response)
|
|
55
|
+
return DataResult(err="unknown_response", data=response)
|
|
56
|
+
except TimeoutError:
|
|
57
|
+
return DataResult(err="timeout")
|
|
58
|
+
except Exception as err:
|
|
59
|
+
return DataResult(err=f"exception: {err}")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
async def eth_block_number(node: str, timeout: float = DEFAULT_TIMEOUT, proxy: str | None = None) -> DataResult[int]:
|
|
63
|
+
return (await rpc_call(node, "eth_blockNumber", [], timeout, proxy)).map(_hex_str_to_int)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
async def eth_get_balance(node: str, address: str, timeout: float = DEFAULT_TIMEOUT, proxy: str | None = None) -> DataResult[int]:
|
|
67
|
+
return (await rpc_call(node, "eth_getBalance", [address, "latest"], timeout, proxy)).map(_hex_str_to_int)
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
async def erc20_balance(
|
|
71
|
+
node: str, token_address: str, user_address: str, timeout: float = DEFAULT_TIMEOUT, proxy: str | None = None
|
|
72
|
+
) -> DataResult[int]:
|
|
73
|
+
data = "0x70a08231000000000000000000000000" + user_address[2:]
|
|
74
|
+
params = [{"to": token_address, "data": data}, "latest"]
|
|
75
|
+
return (await rpc_call(node, "eth_call", params, timeout, proxy)).map(_hex_str_to_int)
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
async def erc20_name(
|
|
79
|
+
node: str, token_address: str, timeout: float = DEFAULT_TIMEOUT, proxy: str | None = None
|
|
80
|
+
) -> DataResult[str]:
|
|
81
|
+
params = [{"to": token_address, "data": "0x06fdde03"}, "latest"]
|
|
82
|
+
return (await rpc_call(node, "eth_call", params, timeout, proxy)).map(_normalize_str)
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
async def erc20_symbol(
|
|
86
|
+
node: str, token_address: str, timeout: float = DEFAULT_TIMEOUT, proxy: str | None = None
|
|
87
|
+
) -> DataResult[str]:
|
|
88
|
+
params = [{"to": token_address, "data": "0x95d89b41"}, "latest"]
|
|
89
|
+
return (await rpc_call(node, "eth_call", params, timeout, proxy)).map(_normalize_str)
|
|
90
|
+
|
|
91
|
+
|
|
92
|
+
async def erc20_decimals(
|
|
93
|
+
node: str, token_address: str, timeout: float = DEFAULT_TIMEOUT, proxy: str | None = None
|
|
94
|
+
) -> DataResult[int]:
|
|
95
|
+
params = [{"to": token_address, "data": "0x313ce567"}, "latest"]
|
|
96
|
+
res = await rpc_call(node, "eth_call", params, timeout, proxy)
|
|
97
|
+
if res.is_err():
|
|
98
|
+
return res
|
|
99
|
+
try:
|
|
100
|
+
if res.unwrap() == "0x":
|
|
101
|
+
return DataResult(err="no_decimals", data=res.data)
|
|
102
|
+
value = res.unwrap()
|
|
103
|
+
result = eth_utils.to_int(hexstr=value[0:66]) if len(value) > 66 else eth_utils.to_int(hexstr=value)
|
|
104
|
+
return DataResult(ok=result, data=res.data)
|
|
105
|
+
except Exception as e:
|
|
106
|
+
return DataResult(err=f"exception: {e}", data=res.data)
|
|
107
|
+
|
|
108
|
+
|
|
109
|
+
ENS_REGISTRY_ADDRESS: str = "0x00000000000C2E074eC69A0dFb2997BA6C7d2e1e"
|
|
110
|
+
FUNC_SELECTOR_RESOLVER: str = "0x0178b8bf" # resolver(bytes32)
|
|
111
|
+
FUNC_SELECTOR_NAME: str = "0x691f3431" # name(bytes32)
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
async def ens_name(node: str, address: str, timeout: float = DEFAULT_TIMEOUT, proxy: str | None = None) -> DataResult[str | None]:
|
|
115
|
+
checksum_addr = eth_utils.to_checksum_address(address)
|
|
116
|
+
reverse_name = checksum_addr.lower()[2:] + ".addr.reverse"
|
|
117
|
+
name_hash_hex = ens.utils.normal_name_to_hash(reverse_name).hex()
|
|
118
|
+
|
|
119
|
+
resolver_data = FUNC_SELECTOR_RESOLVER + name_hash_hex
|
|
120
|
+
|
|
121
|
+
resolver_params = [{"to": ENS_REGISTRY_ADDRESS, "data": resolver_data}, "latest"]
|
|
122
|
+
|
|
123
|
+
resolver_res = await rpc_call(node, method="eth_call", params=resolver_params, timeout=timeout, proxy=proxy)
|
|
124
|
+
if resolver_res.is_err():
|
|
125
|
+
return resolver_res
|
|
126
|
+
|
|
127
|
+
if resolver_res.is_ok() and len(resolver_res.unwrap()) != 66:
|
|
128
|
+
return DataResult(ok=None, data={"revolver_response": resolver_res.dict()})
|
|
129
|
+
|
|
130
|
+
resolver_address = eth_utils.to_checksum_address("0x" + resolver_res.unwrap()[-40:])
|
|
131
|
+
|
|
132
|
+
name_data: str = FUNC_SELECTOR_NAME + name_hash_hex
|
|
133
|
+
name_params = [{"to": resolver_address, "data": name_data}, "latest"]
|
|
134
|
+
|
|
135
|
+
name_res: DataResult[str] = await rpc_call(node, "eth_call", name_params, timeout=timeout, proxy=proxy)
|
|
136
|
+
|
|
137
|
+
if name_res.is_err():
|
|
138
|
+
return DataResult(
|
|
139
|
+
err=name_res.unwrap_err(), data={"resolver_response": resolver_res.dict(), "name_response": name_res.dict()}
|
|
140
|
+
)
|
|
141
|
+
|
|
142
|
+
if name_res.unwrap() == "0x":
|
|
143
|
+
return DataResult(
|
|
144
|
+
ok=None,
|
|
145
|
+
data={"resolver_response": resolver_res.dict(), "name_response": name_res.dict()},
|
|
146
|
+
ok_is_none=True,
|
|
147
|
+
)
|
|
148
|
+
|
|
149
|
+
try:
|
|
150
|
+
hex_data = name_res.unwrap()
|
|
151
|
+
length_hex = hex_data[66:130]
|
|
152
|
+
str_len = int(length_hex, 16) * 2
|
|
153
|
+
name_hex = hex_data[130 : 130 + str_len]
|
|
154
|
+
return DataResult(
|
|
155
|
+
ok=bytes.fromhex(name_hex).decode("utf-8"),
|
|
156
|
+
data={"resolver_response": resolver_res.dict(), "name_response": name_res.dict()},
|
|
157
|
+
)
|
|
158
|
+
except Exception as e:
|
|
159
|
+
return DataResult(
|
|
160
|
+
err="exception",
|
|
161
|
+
data={"resolver_response": resolver_res.dict(), "name_response": name_res.dict(), "exception": str(e)},
|
|
162
|
+
)
|
|
163
|
+
|
|
164
|
+
|
|
165
|
+
def _hex_str_to_int(value: str) -> int:
|
|
166
|
+
return int(value, 16)
|
|
167
|
+
|
|
168
|
+
|
|
169
|
+
def _normalize_str(value: str) -> str:
|
|
170
|
+
return "".join(filter(lambda x: x in string.printable, eth_utils.to_text(hexstr=value))).strip()
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: mm-eth
|
|
3
|
-
Version: 0.5.
|
|
3
|
+
Version: 0.5.15
|
|
4
4
|
Requires-Python: >=3.12
|
|
5
5
|
Requires-Dist: aiohttp-socks~=0.10.1
|
|
6
|
-
Requires-Dist: mm-crypto-utils>=0.2.
|
|
7
|
-
Requires-Dist: mm-std~=0.3.
|
|
6
|
+
Requires-Dist: mm-crypto-utils>=0.2.13
|
|
7
|
+
Requires-Dist: mm-std~=0.3.29
|
|
8
8
|
Requires-Dist: typer>=0.15.2
|
|
9
9
|
Requires-Dist: web3~=7.10.0
|
|
10
10
|
Requires-Dist: websocket-client~=1.8.0
|
|
@@ -11,6 +11,7 @@ mm_eth/ethernodes.py,sha256=V4VVbC6Nr9jhwT7blxtLugXC5KfXqE8n-kP0VvGHbqo,3070
|
|
|
11
11
|
mm_eth/json_encoder.py,sha256=S4oD-qfTVztMb4sRpY1puhBQwOBofTyQXWszmdXk4og,433
|
|
12
12
|
mm_eth/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
13
13
|
mm_eth/rpc.py,sha256=k0eHxo_Dp6G0fHQ_rD-QbwOJz5ngC6kxBjl5NEHnyw8,13832
|
|
14
|
+
mm_eth/rpc_async.py,sha256=4bGMSygj_L782KSHtIpmPu9s3f-tJ3H5qzm0Q-V8op4,6678
|
|
14
15
|
mm_eth/solc.py,sha256=dYRvT8PjZlLDZhNsc_-0790Eug_ZwU2G-iBfIdGj6wQ,1071
|
|
15
16
|
mm_eth/tx.py,sha256=efSoMCoWkenbGdHo1_LX66_Edz1HvED5-J_i3wrHwMw,4051
|
|
16
17
|
mm_eth/utils.py,sha256=FytG3U6h80mnUaKP8W2mPZ77EuOp4U7pVbPuoKI3wW4,8215
|
|
@@ -42,7 +43,7 @@ mm_eth/cli/cmd/wallet/private_key_cmd.py,sha256=Fv_2OLog1h32pIP7PJITwl_pHdy3BXva
|
|
|
42
43
|
mm_eth/cli/examples/balances.toml,sha256=i_ALpiEcf8-0TFiUg1cgJhxxfHYeBl9x0b3tnUWjswU,421
|
|
43
44
|
mm_eth/cli/examples/call_contract.toml,sha256=ZQWK-409V_vLIZ2bsRD5RCWPPzShPz2KJTTRQY4YaGw,248
|
|
44
45
|
mm_eth/cli/examples/transfer.toml,sha256=8mWuphDquoSDJw-hb9VJqtonjmv3kJ5Ip8jJ4t5YnfM,1810
|
|
45
|
-
mm_eth-0.5.
|
|
46
|
-
mm_eth-0.5.
|
|
47
|
-
mm_eth-0.5.
|
|
48
|
-
mm_eth-0.5.
|
|
46
|
+
mm_eth-0.5.15.dist-info/METADATA,sha256=jg5LHl1UakJEHjV9e6oY3zK2DeK3vBAjGxazDJdOfVg,277
|
|
47
|
+
mm_eth-0.5.15.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
48
|
+
mm_eth-0.5.15.dist-info/entry_points.txt,sha256=aGhpsozl8NIrkuUcX5fSgURCcDhr3ShUdeTSIrJq4oc,46
|
|
49
|
+
mm_eth-0.5.15.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|