bitcoinwatcher 2.5__py3-none-any.whl → 2.6.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.
@@ -7,9 +7,9 @@ from ordipool.ordipool.mempoolio import Mempool
7
7
  from bitcoin.models.address_tx_data import AddressTxData
8
8
  from bitcoin.tx_extractors.bitcoin_rpc import BitcoinRPCAddressDataExtractor
9
9
  from bitcoin.tx_extractors.default import DefaultTxAddressDataExtractor
10
- from bitcoin.tx_extractors.mempool import MempoolTxAddressDataExtractor
11
10
  from bitcoin.tx_listener.abstract_tx_listener import AbstractTxListener
12
11
  from bitcoin.utils.constants import default_host
12
+ from bitcoin.utils.context_aware_logging import logger
13
13
 
14
14
 
15
15
  class AbstractAddressListener(AbstractTxListener, ABC):
@@ -37,6 +37,7 @@ class AbstractAddressListener(AbstractTxListener, ABC):
37
37
  # get all address in the inputs and outputs along with the amount
38
38
  if tx.coinbase:
39
39
  return
40
+ logger.debug(f"Extracting default tx data")
40
41
  address_tx_data = self.default_tx_extractor.extract(tx)
41
42
  # filter the address we are interested in
42
43
  addresses_for_events = self.filter_address_tx_data(address_tx_data)
@@ -44,9 +45,10 @@ class AbstractAddressListener(AbstractTxListener, ABC):
44
45
  return
45
46
  # get the address tx data from mempool for full details if any address matches
46
47
  try:
48
+ logger.info(f"Extracting rpc tx data")
47
49
  address_tx_data = self.rpc_tx_extractor.extract(tx)
48
50
  except Exception as e:
49
- print(f"Error in getting rpc tx data, taking defaults: {e}")
51
+ logger.error(f"Error in getting rpc tx data, taking defaults", exc_info=True)
50
52
  address_tx_data = address_tx_data
51
53
 
52
54
  for address in addresses_for_events:
@@ -12,7 +12,7 @@ class SimpleAddressListener(AbstractAddressListener):
12
12
  address_tx_data))
13
13
  total_amount_in_input = sum(map(lambda x: x.amount_in_btc(), all_input))
14
14
  # scale ito 4 decimal places
15
- print("Transaction status: ", address_tx_data[0].tx_status)
15
+ print("Transaction status: ", address_tx_data[0].is_confirmed)
16
16
  total_amount_in_output = round(total_amount_in_output, self.DECIMAL_SCALE)
17
17
  print(f"Address {subscribed_address} received {total_amount_in_output} BTC in tx {address_tx_data[0].tx_id}")
18
18
  print(f"Address {subscribed_address} spent {total_amount_in_input} BTC in tx {address_tx_data[0].tx_id}")
@@ -23,7 +23,7 @@ class SimpleAddressListener(AbstractAddressListener):
23
23
 
24
24
 
25
25
  if __name__ == '__main__':
26
- address = ["bc1qcq2uv5nk6hec6kvag3wyevp6574qmsm9scjxc2"]
26
+ address = ["bc1qp8j9sx6609h7llqufurxjgrwsqwt020tqzn0gs", "bc1qcq2uv5nk6hec6kvag3wyevp6574qmsm9scjxc2"]
27
27
  address_watcher = SimpleAddressListener(address)
28
28
  zmq_listener = ZMQTXListener(address_watcher)
29
29
  zmq_listener.start()
@@ -7,6 +7,7 @@ from bitcoin.address_listener.address_listener import AddressTxData
7
7
  from bitcoin.models.address_tx_data import AddressTxType
8
8
  from bitcoin.utils.bitcoin_rpc import BitcoinRPC
9
9
  from bitcoin.tx_extractors.abstract_extractor import AbstractTxAddressDataExtractor
10
+ from bitcoin.utils.context_aware_logging import logger, ctx_tx_status
10
11
 
11
12
 
12
13
  class BitcoinRPCAddressDataExtractor(AbstractTxAddressDataExtractor):
@@ -32,6 +33,7 @@ class BitcoinRPCAddressDataExtractor(AbstractTxAddressDataExtractor):
32
33
  type=AddressTxType.INPUT)
33
34
 
34
35
  def extract(self, tx: Transaction) -> [AddressTxData]:
36
+ logger.info("Extracting rpc tx data")
35
37
  outputs = tx.outputs
36
38
  tx_id = tx.txid
37
39
  address_tx_data = []
@@ -39,6 +41,8 @@ class BitcoinRPCAddressDataExtractor(AbstractTxAddressDataExtractor):
39
41
  # bulk get all the inputs from BitcoinRPC using thread pool
40
42
  inputs_data = self.fetch_all_inputs(inputs)
41
43
  is_confirmed = self.bitcoinrpc.is_confirmed(tx_id)
44
+ ctx_tx_status.set(is_confirmed)
45
+ logger.info("Transaction is_confirmed: %s", is_confirmed)
42
46
  for input in inputs:
43
47
  address = input.address
44
48
  amount = 0
@@ -3,6 +3,7 @@ from bitcoinlib.transactions import Transaction
3
3
  from bitcoin.address_listener.address_listener import AddressTxData
4
4
  from bitcoin.models.address_tx_data import AddressTxType
5
5
  from bitcoin.tx_extractors.abstract_extractor import AbstractTxAddressDataExtractor
6
+ from bitcoin.utils.context_aware_logging import ctx_tx_status
6
7
 
7
8
 
8
9
  class DefaultTxAddressDataExtractor(AbstractTxAddressDataExtractor):
@@ -11,6 +12,7 @@ class DefaultTxAddressDataExtractor(AbstractTxAddressDataExtractor):
11
12
  address_tx_data = []
12
13
  inputs = tx.inputs
13
14
  is_confirmed = tx.status == "confirmed"
15
+ ctx_tx_status.set(is_confirmed)
14
16
  for input in inputs:
15
17
  address = input.address
16
18
  amount = 0
@@ -5,6 +5,7 @@ from bitcoinlib.transactions import Transaction
5
5
 
6
6
  from bitcoin.tx_listener.abstract_tx_listener import AbstractTxListener
7
7
  from bitcoin.utils.constants import default_host, default_port
8
+ from bitcoin.utils.context_aware_logging import ctx_tx, logger
8
9
 
9
10
 
10
11
  class ZMQTXListener:
@@ -20,15 +21,16 @@ class ZMQTXListener:
20
21
 
21
22
  def start(self):
22
23
  connections = self.socket.connect(self.zmq_url)
23
- print(f"Connected to {connections}")
24
+ logger.info(f"Connected to {connections}")
24
25
  self.socket.setsockopt_string(zmq.SUBSCRIBE, "rawtx")
25
26
  while True:
26
27
  topic, body, seq = self.socket.recv_multipart()
27
28
  try:
28
29
  tx = Transaction.parse(body, strict=False)
30
+ ctx_tx.set(tx.txid)
29
31
  self.tx_listener.on_tx(tx)
30
32
  except Exception as e:
31
- print(f"Error in parsing tx: {e}")
33
+ logger.error(f"Error in parsing tx: {e}", exc_info=True)
32
34
 
33
35
  def stop(self):
34
36
  self.socket.close()
@@ -18,7 +18,6 @@ if __name__ == '__main__':
18
18
  txid = 'a6293e898b056fbea0329d071b1b237e4449ff464cdbc7a9ed8a770b97aafd4c'
19
19
  times = 1000
20
20
  # start = time.time()
21
- # print("mempool starting")
22
21
  # for i in range(times):
23
22
  # print(i)
24
23
  # mempool.get_transaction(txid)
@@ -29,5 +28,4 @@ if __name__ == '__main__':
29
28
  for i in range(times):
30
29
  tx = bitcoinrpc.get_transaction(txid)
31
30
  end = time.time()
32
- print(f"bitcoinrpc took: {end - start}")
33
31
 
@@ -3,6 +3,7 @@ import os
3
3
  from bitcoinrpc.authproxy import AuthServiceProxy, JSONRPCException
4
4
 
5
5
  from bitcoin.utils.constants import default_host
6
+ from bitcoin.utils.context_aware_logging import ctx_tx, ctx_tx_status, logger, get_logger
6
7
 
7
8
 
8
9
  class BitcoinRPC:
@@ -27,10 +28,12 @@ class BitcoinRPC:
27
28
 
28
29
 
29
30
  if __name__ == '__main__':
31
+ logger = get_logger(__name__)
30
32
  rpc = BitcoinRPC()
31
33
  txid = '686d025f16d9f20353665a9d865e575e3e4d14214f6f7045149a17dd6bf0fac6'
32
34
  try:
33
- transaction = rpc.is_confirmed(txid)
34
- print(transaction)
35
+ ctx_tx.set('686d025f16d9f20353665a9d865e575e3e4d14214f6f7045149a17dd6bf0fac6')
36
+ ctx_tx_status.set('confirmed')
37
+ logger.info("Transaction is confirmed")
35
38
  except JSONRPCException as e:
36
39
  print(f"An error occurred: {e}")
@@ -0,0 +1,59 @@
1
+ import logging
2
+ from contextvars import ContextVar
3
+ from datetime import datetime
4
+
5
+ import pytz
6
+
7
+ logger = logging.getLogger(__name__)
8
+ root = logging.getLogger()
9
+ root.setLevel(logging.INFO)
10
+
11
+ class TimezoneFormatter(logging.Formatter):
12
+ def __init__(self, fmt=None, datefmt=None, tz=None):
13
+ super().__init__(fmt, datefmt)
14
+ self.tz = tz
15
+
16
+ def formatTime(self, record, datefmt=None):
17
+ dt = datetime.fromtimestamp(record.created, self.tz)
18
+ if datefmt:
19
+ s = dt.strftime(datefmt)
20
+ else:
21
+ s = dt.isoformat()
22
+ return s
23
+
24
+
25
+ formatter = TimezoneFormatter(
26
+ ' %(asctime)s %(levelname)s txid=%(tx_id)s tx_status=%(tx_status)s [%(module)s:%(lineno)d] %(funcName)s: %(message)s',
27
+ datefmt='%Y-%m-%d %H:%M:%S',
28
+ tz=pytz.timezone('Asia/Kolkata')
29
+ )
30
+
31
+
32
+ ctx_tx = ContextVar('tx_id', default='')
33
+ ctx_tx_status = ContextVar('tx_status', default='')
34
+
35
+ class ExcludeModuleFilter(logging.Filter):
36
+ def filter(self, record):
37
+ return not (record.name.startswith('bitcoinlib.transactions') or record.name.startswith('bitcoinlib.scripts'))
38
+
39
+ class TXContextFilter(logging.Filter):
40
+
41
+ def __init__(self):
42
+ super().__init__()
43
+
44
+ def filter(self, record):
45
+ tx_id = ctx_tx.get()
46
+ record.tx_id = tx_id
47
+ tx_status = ctx_tx_status.get()
48
+ record.tx_status = tx_status
49
+ return True
50
+
51
+ ch = logging.StreamHandler()
52
+ f = TXContextFilter()
53
+ ch.setFormatter(formatter)
54
+ ch.addFilter(f)
55
+ ch.addFilter(ExcludeModuleFilter())
56
+ root.addHandler(ch)
57
+
58
+ def get_logger(name):
59
+ return logging.getLogger(name)
@@ -1,9 +1,9 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: bitcoinwatcher
3
- Version: 2.5
3
+ Version: 2.6.1
4
4
  Summary: bitcoinwatcher is a Python library that implements a ZMQ subscriber and provides abstractions to build custom address watchers. This library is designed to make it easy for developers to monitor Bitcoin addresses and react to changes in their state.
5
5
  Author: twosatsmaxi
6
- License: Apache License
6
+ License: Apache License
7
7
  Version 2.0, January 2004
8
8
  http://www.apache.org/licenses/
9
9
 
@@ -223,11 +223,12 @@ Requires-Dist: charset-normalizer==3.3.2
223
223
  Requires-Dist: idna==3.7
224
224
  Requires-Dist: pyzmq==25.1.2
225
225
  Requires-Dist: requests==2.31.0
226
- Requires-Dist: six==1.16.0
227
226
  Requires-Dist: urllib3==2.2.1
228
- Requires-Dist: bitcoinlib==0.6.14
227
+ Requires-Dist: bitcoinlib==0.7.0
229
228
  Requires-Dist: ordipool==1.2.0
229
+ Requires-Dist: pyzmq==25.1.2
230
230
  Requires-Dist: python-bitcoinrpc==1.0
231
+ Requires-Dist: pytz~=2024.2
231
232
 
232
233
  # bitcoinwatcher
233
234
 
@@ -1,7 +1,7 @@
1
1
  bitcoin/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
2
2
  bitcoin/address_listener/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
3
- bitcoin/address_listener/address_listener.py,sha256=Vnti0KIPJVt1vSOM0EawhFvrYeDhThhMbcBhAHaDy1Q,2342
4
- bitcoin/address_listener/simple_address_listener.py,sha256=WG8eTrd3AdkrIs1TAGRsXe_erAtE_Ep8hidZDksGk8g,1625
3
+ bitcoin/address_listener/address_listener.py,sha256=_ybn0IgOLL6rrf2WKOwJsI_MjWdiFs8qiGWl-Kj6kf0,2445
4
+ bitcoin/address_listener/simple_address_listener.py,sha256=yQTxWVthsMiYkXzWYmGMCzP0LWTUokgWFkqEf8GsUB4,1674
5
5
  bitcoin/models/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
6
6
  bitcoin/models/address_tx_data.py,sha256=KSSCzAjYxLEgXIELVSsnSEhgY65xi0K3HUjoamVV_LQ,750
7
7
  bitcoin/tests/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
@@ -12,20 +12,21 @@ bitcoin/tests/data/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuF
12
12
  bitcoin/tests/data/transactions.py,sha256=qIX9seGYguQ1lvLOCF8oe3NQhMJZP0SsBmz1iFbszhg,396279
13
13
  bitcoin/tx_extractors/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
14
14
  bitcoin/tx_extractors/abstract_extractor.py,sha256=YLnuqc3jva15NpOX8xV4oB5PEXc6rXbG8Lx0cBXW66s,291
15
- bitcoin/tx_extractors/bitcoin_rpc.py,sha256=ebw_oEhgi22Oj6mtjBNLnxl_NIF_u63d05u3nHkUt_U,2847
16
- bitcoin/tx_extractors/default.py,sha256=Xd09vldoFIlZo2g25KQeWbPKw7Cyj6vp40AKeDn3nJM,1407
15
+ bitcoin/tx_extractors/bitcoin_rpc.py,sha256=1ehmZ4wsyE-dkfi2yYfCX0d-YFJoZPtkgey9X6gZx9U,3069
16
+ bitcoin/tx_extractors/default.py,sha256=KLT0EIbrp9yzFTQ-U5JRGI4ACHkvEMmycdXQGoPRU20,1509
17
17
  bitcoin/tx_extractors/mempool.py,sha256=mPPY1GOvBmZWn7UVP2sUaXtfcyeTCj-LmRDl2F4WhC8,2090
18
18
  bitcoin/tx_listener/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
19
19
  bitcoin/tx_listener/abstract_tx_listener.py,sha256=rGp9na-aOSl_aPaIqf44RTZaFF3SvrxIhZBujTnWqao,189
20
- bitcoin/tx_listener/zmq_listener.py,sha256=V4-WIt5PYJaYoipn0rYhcFfg1BBYxqqQGucKhGXTmkg,1110
20
+ bitcoin/tx_listener/zmq_listener.py,sha256=X3R-DwC2e_V_28WQZcThKuoAhT4_0QOscsrDTV_aHjU,1237
21
21
  bitcoin/utils/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
22
- bitcoin/utils/benchmark.py,sha256=kaTTlo6k_5uDVIt8FMeY4nH7ZxxsoWUiMsZgHfpWsMo,894
23
- bitcoin/utils/bitcoin_rpc.py,sha256=tsDwSeklp_oDZ3Bd7aUC2tpUQ2kUGwhnMr1NzACIK34,1132
22
+ bitcoin/utils/benchmark.py,sha256=HXZy9vU34MKrBoVjx3y982TFEOuSmOz5HWf5LcFwaZA,817
23
+ bitcoin/utils/bitcoin_rpc.py,sha256=wyTIahgdSyiR1ryPi75n33ERuKQK630YTO9hNs_MML4,1358
24
24
  bitcoin/utils/bitcoin_utils.py,sha256=mrnRPqUa2U2EMKu7rrPV_bW1sL2CJUfbAom0Zdamydk,631
25
25
  bitcoin/utils/constants.py,sha256=irZLlArgica2VckyckEYxH5D5KjvdF52dtBMWswqw8k,52
26
+ bitcoin/utils/context_aware_logging.py,sha256=JpTYU9FiNgrTw60cFfGNDy00Icx2x5-Zod4KOJJ6pWU,1579
26
27
  bitcoin/utils/inscription_utils.py,sha256=8QbOJ1o1n1bMFsPREGLzwFjnGzfuARgJCPr6ORhP44o,193
27
- bitcoinwatcher-2.5.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
28
- bitcoinwatcher-2.5.dist-info/METADATA,sha256=AGb2LvceN2_Jz-OSzmXpVrk8BALp2S2VZ8TKMhKyh-0,14927
29
- bitcoinwatcher-2.5.dist-info/WHEEL,sha256=R0nc6qTxuoLk7ShA2_Y-UWkN8ZdfDBG2B6Eqpz2WXbs,91
30
- bitcoinwatcher-2.5.dist-info/top_level.txt,sha256=YdUgzLdCiMlrwaKyDqHA1acEd23QFko5bv7D6nBANJ0,8
31
- bitcoinwatcher-2.5.dist-info/RECORD,,
28
+ bitcoinwatcher-2.6.1.dist-info/LICENSE,sha256=xx0jnfkXJvxRnG63LTGOxlggYnIysveWIZ6H3PNdCrQ,11357
29
+ bitcoinwatcher-2.6.1.dist-info/METADATA,sha256=1FkBp83yL2hZa5byNPkV8gYa3KwFhOcNQOlkJns4ZUc,14991
30
+ bitcoinwatcher-2.6.1.dist-info/WHEEL,sha256=PZUExdf71Ui_so67QXpySuHtCi3-J3wvF4ORK6k_S8U,91
31
+ bitcoinwatcher-2.6.1.dist-info/top_level.txt,sha256=YdUgzLdCiMlrwaKyDqHA1acEd23QFko5bv7D6nBANJ0,8
32
+ bitcoinwatcher-2.6.1.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: setuptools (72.1.0)
2
+ Generator: setuptools (75.6.0)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5