mm-btc 0.3.0__py3-none-any.whl → 0.4.1__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_btc/blockstream.py CHANGED
@@ -1,7 +1,6 @@
1
1
  from collections.abc import Sequence
2
2
 
3
- from mm_std import Err, HResponse, Ok, Result, hr
4
- from mm_std.random_ import random_str_choice
3
+ from mm_std import HttpResponse, Result, http_request, random_str_choice
5
4
  from pydantic import BaseModel
6
5
 
7
6
  MAINNET_BASE_URL = "https://blockstream.info/api"
@@ -55,53 +54,49 @@ class Utxo(BaseModel):
55
54
 
56
55
 
57
56
  class BlockstreamClient:
58
- def __init__(self, testnet: bool = False, timeout: int = 10, proxies: Proxy = None, attempts: int = 1) -> None:
57
+ def __init__(self, testnet: bool = False, timeout: float = 5, proxies: Proxy = None, attempts: int = 1) -> None:
59
58
  self.testnet = testnet
60
59
  self.timeout = timeout
61
60
  self.proxies = proxies
62
61
  self.attempts = attempts
63
62
  self.base_url = TESTNET_BASE_URL if testnet else MAINNET_BASE_URL
64
63
 
65
- def get_address(self, address: str) -> Result[Address]:
66
- result: Result[Address] = Err("not started yet")
67
- data = None
64
+ async def get_address(self, address: str) -> Result[Address]:
65
+ result: Result[Address] = Result.err("not started yet")
68
66
  for _ in range(self.attempts):
67
+ res = await self._request(f"/address/{address}")
69
68
  try:
70
- res = self._request(f"/address/{address}")
71
- data = res.to_dict()
72
- if res.code == 400:
73
- return Err("400 Bad Request", data=data)
74
- return Ok(Address(**res.json), data=data)
75
- except Exception as err:
76
- result = Err(err, data=data)
69
+ if res.status_code == 400:
70
+ return res.to_err_result("400 Bad Request")
71
+ return res.to_ok_result(Address(**res.parse_json_body()))
72
+ except Exception as e:
73
+ result = res.to_err_result(e)
77
74
  return result
78
75
 
79
- def get_confirmed_balance(self, address: str) -> Result[int]:
80
- return self.get_address(address).and_then(lambda a: Ok(a.chain_stats.funded_txo_sum - a.chain_stats.spent_txo_sum))
76
+ async def get_confirmed_balance(self, address: str) -> Result[int]:
77
+ return (await self.get_address(address)).and_then(
78
+ lambda a: Result.ok(a.chain_stats.funded_txo_sum - a.chain_stats.spent_txo_sum)
79
+ )
81
80
 
82
- def get_utxo(self, address: str) -> Result[list[Utxo]]:
83
- result: Result[list[Utxo]] = Err("not started yet")
84
- data = None
81
+ async def get_utxo(self, address: str) -> Result[list[Utxo]]:
82
+ result: Result[list[Utxo]] = Result.err("not started yet")
85
83
  for _ in range(self.attempts):
84
+ res = await self._request(f"/address/{address}/utxo")
86
85
  try:
87
- res = self._request(f"/address/{address}/utxo")
88
- data = res.to_dict()
89
- return Ok([Utxo(**out) for out in res.json], data=data)
90
- except Exception as err:
91
- result = Err(err, data=data)
86
+ return res.to_ok_result([Utxo(**out) for out in res.parse_json_body()])
87
+ except Exception as e:
88
+ result = res.to_err_result(e)
92
89
  return result
93
90
 
94
- def get_mempool(self) -> Result[Mempool]:
95
- result: Result[Mempool] = Err("not started yet")
96
- data = None
91
+ async def get_mempool(self) -> Result[Mempool]:
92
+ result: Result[Mempool] = Result.err("not started yet")
97
93
  for _ in range(self.attempts):
94
+ res = await self._request("/mempool")
98
95
  try:
99
- res = self._request("/mempool")
100
- data = res.to_dict()
101
- return Ok(Mempool(**res.json), data=data)
102
- except Exception as err:
103
- result = Err(err, data=data)
96
+ return res.to_ok_result(Mempool(**res.parse_json_body()))
97
+ except Exception as e:
98
+ result = res.to_err_result(e)
104
99
  return result
105
100
 
106
- def _request(self, url: str) -> HResponse:
107
- return hr(f"{self.base_url}{url}", timeout=self.timeout, proxy=random_str_choice(self.proxies))
101
+ async def _request(self, url: str) -> HttpResponse:
102
+ return await http_request(f"{self.base_url}{url}", timeout=self.timeout, proxy=random_str_choice(self.proxies))
mm_btc/cli/cli.py CHANGED
@@ -1,3 +1,4 @@
1
+ import asyncio
1
2
  import importlib.metadata
2
3
  from pathlib import Path
3
4
  from typing import Annotated
@@ -44,7 +45,7 @@ def mnemonic_command( # nosec B107:hardcoded_password_default
44
45
  @app.command(name="a", hidden=True)
45
46
  def address_command(address: str) -> None:
46
47
  """Get address info from Blockstream API"""
47
- address_cmd.run(address)
48
+ asyncio.run(address_cmd.run(address))
48
49
 
49
50
 
50
51
  @app.command("create-tx")
@@ -62,7 +63,7 @@ def decode_tx_command(tx_hex: str, testnet: Annotated[bool, typer.Option("--test
62
63
  @app.command("utxo")
63
64
  def utxo_command(address: str) -> None:
64
65
  """Get UTXOs from Blockstream API"""
65
- utxo_cmd.run(address)
66
+ asyncio.run(utxo_cmd.run(address))
66
67
 
67
68
 
68
69
  def version_callback(value: bool) -> None:
@@ -4,7 +4,7 @@ from mm_btc.blockstream import BlockstreamClient
4
4
  from mm_btc.wallet import is_testnet_address
5
5
 
6
6
 
7
- def run(address: str) -> None:
7
+ async def run(address: str) -> None:
8
8
  client = BlockstreamClient(testnet=is_testnet_address(address))
9
- res = client.get_address(address)
10
- print_json(res)
9
+ res = await client.get_address(address)
10
+ print_json(res.ok_or_error())
@@ -4,8 +4,7 @@ from mm_btc.blockstream import BlockstreamClient
4
4
  from mm_btc.wallet import is_testnet_address
5
5
 
6
6
 
7
- def run(address: str) -> None:
7
+ async def run(address: str) -> None:
8
8
  client = BlockstreamClient(testnet=is_testnet_address(address))
9
- res = client.get_utxo(address)
10
-
11
- print_json(res.ok_or_err())
9
+ res = await client.get_utxo(address)
10
+ print_json(res.ok_or_error())
@@ -0,0 +1,10 @@
1
+ Metadata-Version: 2.4
2
+ Name: mm-btc
3
+ Version: 0.4.1
4
+ Requires-Python: >=3.12
5
+ Requires-Dist: bitcoinlib~=0.7.3
6
+ Requires-Dist: bit~=0.8.0
7
+ Requires-Dist: hdwallet~=3.4.0
8
+ Requires-Dist: mm-crypto-utils>=0.3.4
9
+ Requires-Dist: mnemonic~=0.21
10
+ Requires-Dist: typer~=0.15.2
@@ -1,17 +1,17 @@
1
1
  mm_btc/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
- mm_btc/blockstream.py,sha256=hnZ4pSPUbXCoPA0zPaEazwibDvy-WXHCXk0hrix-grA,3347
2
+ mm_btc/blockstream.py,sha256=FfQ3QYx9jsEIYVQDttyrYHjWn9nZSt0g2RVJ0BP2nW4,3323
3
3
  mm_btc/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
4
4
  mm_btc/tx.py,sha256=7KTMNuL1n8rRj_245BV1bH9M2_PctNvlvPsLEsRTYx4,245
5
5
  mm_btc/wallet.py,sha256=_pGTuAf6Y9KzaWTTSg_tn6WEoRsGqhxJuPof0_xyrmM,2333
6
6
  mm_btc/cli/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
7
- mm_btc/cli/cli.py,sha256=-UQ_StfyHS0p5gSD7bWWsibjJh3FJDSaWkVnlRUvZds,2615
7
+ mm_btc/cli/cli.py,sha256=peUio3WAa2oXomZvq-87QAbuTP37CPNoq5RAG7ax3ns,2656
8
8
  mm_btc/cli/cmd/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
9
- mm_btc/cli/cmd/address_cmd.py,sha256=fipX9FiQ2EgCEq8nVN6ELnDMoR5KTIX7bpAaRK3t_yg,284
9
+ mm_btc/cli/cmd/address_cmd.py,sha256=PCjOEpZbMqBKpaAJnDHzYSqRnQEqHKqxN-w5c1Xq6jQ,310
10
10
  mm_btc/cli/cmd/create_tx_cmd.py,sha256=QHhfA91FGWf5r6DkWTaXInZJi6HYLfxueA1GtcBD2-s,703
11
11
  mm_btc/cli/cmd/decode_tx_cmd.py,sha256=0jGlUjnA1X2Q2aYwSBeAjqVNZIBsW5X2lEwIpSfWJsU,175
12
12
  mm_btc/cli/cmd/mnemonic_cmd.py,sha256=CY6tPsqOTNfM-4WhKDhqitCi2OeqSIUMEUTFIRGHwIg,1254
13
- mm_btc/cli/cmd/utxo_cmd.py,sha256=mp-lVLURpXFqO3IeBIEwnHusEvT5tFjUKxnRYC-rFqQ,294
14
- mm_btc-0.3.0.dist-info/METADATA,sha256=07SoipqPS9Ogpdp2jM5tu29r9I6xX07NQ837ijTchkY,261
15
- mm_btc-0.3.0.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
- mm_btc-0.3.0.dist-info/entry_points.txt,sha256=KphzLNE9eb9H1DO-L0gvBQaQT5ImedyVCN4a6svfiRg,46
17
- mm_btc-0.3.0.dist-info/RECORD,,
13
+ mm_btc/cli/cmd/utxo_cmd.py,sha256=B3vI2BPWMSPVvMLGBctJw-C9k9vVLUg4nKFEIYNtmgY,307
14
+ mm_btc-0.4.1.dist-info/METADATA,sha256=bt6CX5gfr_y91jQTnwavrJZ5itFuvo_jSHR_LNjpcdE,261
15
+ mm_btc-0.4.1.dist-info/WHEEL,sha256=qtCwoSJWgHk21S1Kb4ihdzI2rlJ1ZKaIurTj_ngOhyQ,87
16
+ mm_btc-0.4.1.dist-info/entry_points.txt,sha256=KphzLNE9eb9H1DO-L0gvBQaQT5ImedyVCN4a6svfiRg,46
17
+ mm_btc-0.4.1.dist-info/RECORD,,
@@ -1,10 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: mm-btc
3
- Version: 0.3.0
4
- Requires-Python: >=3.12
5
- Requires-Dist: bitcoinlib~=0.7.1
6
- Requires-Dist: bit~=0.8.0
7
- Requires-Dist: hdwallet~=3.2.3
8
- Requires-Dist: mm-crypto-utils>=0.1.3
9
- Requires-Dist: mnemonic~=0.21
10
- Requires-Dist: typer~=0.15.1
File without changes