mm-eth 0.5.12__py3-none-any.whl → 0.5.13__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,109 @@
|
|
|
1
|
+
import json
|
|
2
|
+
import string
|
|
3
|
+
from collections.abc import Sequence
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
import eth_utils
|
|
7
|
+
import websockets
|
|
8
|
+
from mm_std import DataResult, http_request
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
async def rpc_call(
|
|
12
|
+
node: str,
|
|
13
|
+
method: str,
|
|
14
|
+
params: Sequence[object],
|
|
15
|
+
timeout: float,
|
|
16
|
+
proxy: str | None,
|
|
17
|
+
id_: int = 1,
|
|
18
|
+
) -> DataResult[Any]:
|
|
19
|
+
data = {"jsonrpc": "2.0", "method": method, "params": params, "id": id_}
|
|
20
|
+
if node.startswith("http"):
|
|
21
|
+
return await _http_call(node, data, timeout, proxy)
|
|
22
|
+
return await _ws_call(node, data, timeout)
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
async def _http_call(node: str, data: dict[str, object], timeout: float, proxy: str | None) -> DataResult[Any]:
|
|
26
|
+
res = await http_request(node, method="POST", proxy=proxy, timeout=timeout, json=data)
|
|
27
|
+
if res.is_error():
|
|
28
|
+
return res.to_data_result_err()
|
|
29
|
+
try:
|
|
30
|
+
parsed_body = res.parse_json_body()
|
|
31
|
+
err = parsed_body.get("error", {}).get("message", "")
|
|
32
|
+
if err:
|
|
33
|
+
return res.to_data_result_err(f"service_error: {err}")
|
|
34
|
+
if "result" in parsed_body:
|
|
35
|
+
return res.to_data_result_ok(parsed_body["result"])
|
|
36
|
+
return res.to_data_result_err("unknown_response")
|
|
37
|
+
except Exception as err:
|
|
38
|
+
return res.to_data_result_err(f"exception: {err}")
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
async def _ws_call(node: str, data: dict[str, object], timeout: float) -> DataResult[Any]:
|
|
42
|
+
try:
|
|
43
|
+
async with websockets.connect(node, timeout=timeout) as ws:
|
|
44
|
+
await ws.send(json.dumps(data))
|
|
45
|
+
response = json.loads(await ws.recv())
|
|
46
|
+
|
|
47
|
+
err = response.get("error", {}).get("message", "")
|
|
48
|
+
if err:
|
|
49
|
+
return DataResult(err=f"service_error: {err}", data=response)
|
|
50
|
+
if "result" in response:
|
|
51
|
+
return DataResult(ok=response["result"], data=response)
|
|
52
|
+
return DataResult(err="unknown_response", data=response)
|
|
53
|
+
except TimeoutError:
|
|
54
|
+
return DataResult(err="timeout")
|
|
55
|
+
except Exception as err:
|
|
56
|
+
return DataResult(err=f"exception: {err}")
|
|
57
|
+
|
|
58
|
+
|
|
59
|
+
async def eth_block_number(node: str, timeout: int = 10, proxy: str | None = None) -> DataResult[int]:
|
|
60
|
+
return (await rpc_call(node, "eth_blockNumber", [], timeout, proxy)).map(hex_str_to_int)
|
|
61
|
+
|
|
62
|
+
|
|
63
|
+
async def eth_get_balance(node: str, address: str, timeout: int = 10, proxy: str | None = None) -> DataResult[int]:
|
|
64
|
+
return (await rpc_call(node, "eth_getBalance", [address, "latest"], timeout, proxy)).map(hex_str_to_int)
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+
async def erc20_balance(
|
|
68
|
+
node: str,
|
|
69
|
+
token_address: str,
|
|
70
|
+
user_address: str,
|
|
71
|
+
timeout: float = 7.0,
|
|
72
|
+
proxy: str | None = None,
|
|
73
|
+
) -> DataResult[int]:
|
|
74
|
+
data = "0x70a08231000000000000000000000000" + user_address[2:]
|
|
75
|
+
params = [{"to": token_address, "data": data}, "latest"]
|
|
76
|
+
return (await rpc_call(node, "eth_call", params, timeout, proxy)).map(hex_str_to_int)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
async def erc20_name(node: str, token_address: str, timeout: float = 7.0, proxy: str | None = None) -> DataResult[str]:
|
|
80
|
+
params = [{"to": token_address, "data": "0x06fdde03"}, "latest"]
|
|
81
|
+
return (await rpc_call(node, "eth_call", params, timeout, proxy)).map(_normalize_str)
|
|
82
|
+
|
|
83
|
+
|
|
84
|
+
async def erc20_symbol(node: str, token_address: str, timeout: float = 7.0, proxy: str | None = None) -> DataResult[str]:
|
|
85
|
+
params = [{"to": token_address, "data": "0x95d89b41"}, "latest"]
|
|
86
|
+
return (await rpc_call(node, "eth_call", params, timeout, proxy)).map(_normalize_str)
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
async def erc20_decimals(node: str, token_address: str, timeout: float = 7.0, proxy: str | None = None) -> DataResult[int]:
|
|
90
|
+
params = [{"to": token_address, "data": "0x313ce567"}, "latest"]
|
|
91
|
+
res = await rpc_call(node, "eth_call", params, timeout, proxy)
|
|
92
|
+
if res.is_err():
|
|
93
|
+
return res
|
|
94
|
+
try:
|
|
95
|
+
if res.unwrap() == "0x":
|
|
96
|
+
return DataResult(err="no_decimals", data=res.data)
|
|
97
|
+
value = res.unwrap()
|
|
98
|
+
result = eth_utils.to_int(hexstr=value[0:66]) if len(value) > 66 else eth_utils.to_int(hexstr=value)
|
|
99
|
+
return DataResult(ok=result, data=res.data)
|
|
100
|
+
except Exception as e:
|
|
101
|
+
return DataResult(err=f"exception: {e}", data=res.data)
|
|
102
|
+
|
|
103
|
+
|
|
104
|
+
def hex_str_to_int(value: str) -> int:
|
|
105
|
+
return int(value, 16)
|
|
106
|
+
|
|
107
|
+
|
|
108
|
+
def _normalize_str(value: str) -> str:
|
|
109
|
+
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.13
|
|
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.12
|
|
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=3AzSSA9zQ_sPtGT4TUDUCorFGnDlmQC4WnmbwmUQETY,4260
|
|
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.13.dist-info/METADATA,sha256=AyFH0U7lyIP_seMH_MsCOwQHUsw-85j0jH2eloElwXA,277
|
|
47
|
+
mm_eth-0.5.13.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
|
|
48
|
+
mm_eth-0.5.13.dist-info/entry_points.txt,sha256=aGhpsozl8NIrkuUcX5fSgURCcDhr3ShUdeTSIrJq4oc,46
|
|
49
|
+
mm_eth-0.5.13.dist-info/RECORD,,
|
|
File without changes
|
|
File without changes
|