bitcoinwatcher 1.0.1__py3-none-any.whl → 1.2.0__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.
@@ -1,35 +1,13 @@
1
- import dataclasses
2
1
  import os
3
2
  from abc import ABC, abstractmethod
4
- from enum import Enum
5
3
 
6
4
  from bitcoinlib.transactions import Transaction
7
5
  from ordipool.ordipool.mempoolio import Mempool
8
6
 
7
+ from bitcoin.models.address_tx_data import AddressTxData
9
8
  from bitcoin.tx_listener.abstract_tx_listener import AbstractTxListener
10
9
  from bitcoin.utils.constants import default_host
11
-
12
-
13
- class AddressTxType(Enum):
14
- INPUT = "input"
15
- OUTPUT = "output"
16
-
17
-
18
- @dataclasses.dataclass
19
- class AddressTxData:
20
- tx_id: str
21
- address: str
22
- type: AddressTxType
23
- # figure out the way to get amount in vin
24
- _amount: int = 0
25
-
26
- def get_amount(self):
27
- return self._amount
28
-
29
- def amount_in_btc(self):
30
- if self.type == AddressTxType.INPUT:
31
- return "Amount is not supported for input type, yet"
32
- return self._amount / 100000000
10
+ from bitcoin.utils.tx_address_data_extractor import DefaultTxAddressDataExtractor, MempoolTxAddressDataExtractor
33
11
 
34
12
 
35
13
  class AbstractAddressListener(AbstractTxListener, ABC):
@@ -37,6 +15,8 @@ class AbstractAddressListener(AbstractTxListener, ABC):
37
15
  host = os.environ.get("RPC_HOST", default_host)
38
16
  base_url = f"http://{host}:3006/api"
39
17
  mempool = Mempool(base_url=base_url)
18
+ default_tx_extractor = DefaultTxAddressDataExtractor()
19
+ mempool_tx_extractor = MempoolTxAddressDataExtractor(mempool)
40
20
 
41
21
  def __init__(self, addresses_to_listen: {str}):
42
22
  self.addresses_to_listen = addresses_to_listen
@@ -55,27 +35,17 @@ class AbstractAddressListener(AbstractTxListener, ABC):
55
35
  # get all address in the inputs and outputs along with the amount
56
36
  if tx.coinbase:
57
37
  return
58
- outputs = tx.outputs
59
- tx_id = tx.txid
60
- tx = self.mempool.get_transaction(tx_id)
61
- address_tx_data = []
62
- # getting inputs data separately from mempool
63
- # as current library doesn't provide rich data like previous outputs and its value
64
- for input in tx.vins:
65
- address = input.prev_out.address
66
- amount = input.prev_out.value
67
- address_tx_data.append(AddressTxData(address=address,
68
- type=AddressTxType.INPUT,
69
- _amount=amount,
70
- tx_id=tx_id))
71
- for output in outputs:
72
- amount = output.value
73
- address_tx_data.append(AddressTxData(address=output.address,
74
- _amount=amount,
75
- type=AddressTxType.OUTPUT,
76
- tx_id=tx_id))
77
-
38
+ address_tx_data = self.default_tx_extractor.extract(tx)
78
39
  # filter the address we are interested in
79
40
  addresses_for_events = self.filter_address_tx_data(address_tx_data)
41
+ if len(addresses_for_events) == 0:
42
+ return
43
+ # get the address tx data from mempool for full details if any address matches
44
+ try:
45
+ address_tx_data = self.mempool_tx_extractor.extract(tx)
46
+ except Exception as e:
47
+ print(f"Error in getting mempool tx data, taking defaults: {e}")
48
+ address_tx_data = address_tx_data
49
+
80
50
  for address in addresses_for_events:
81
51
  self.consume(subscribed_address=address, address_tx_data=address_tx_data)
@@ -1,4 +1,5 @@
1
- from bitcoin.address_listener.address_listener import AbstractAddressListener, AddressTxData, AddressTxType
1
+ from bitcoin.address_listener.address_listener import AbstractAddressListener, AddressTxData
2
+ from bitcoin.models.address_tx_data import AddressTxType
2
3
  from bitcoin.tx_listener.zmq_listener import ZMQTXListener
3
4
 
4
5
 
@@ -11,6 +12,7 @@ class SimpleAddressListener(AbstractAddressListener):
11
12
  address_tx_data))
12
13
  total_amount_in_input = sum(map(lambda x: x.amount_in_btc(), all_input))
13
14
  # scale ito 4 decimal places
15
+ print("Transaction status: ", address_tx_data[0].tx_status)
14
16
  total_amount_in_output = round(total_amount_in_output, self.DECIMAL_SCALE)
15
17
  print(f"Address {subscribed_address} received {total_amount_in_output} BTC in tx {address_tx_data[0].tx_id}")
16
18
  print(f"Address {subscribed_address} spent {total_amount_in_input} BTC in tx {address_tx_data[0].tx_id}")
File without changes
@@ -0,0 +1,36 @@
1
+ import dataclasses
2
+ from enum import Enum
3
+
4
+
5
+ class AddressTxType(Enum):
6
+ INPUT = "input"
7
+ OUTPUT = "output"
8
+
9
+
10
+ class AddressTxSource(Enum):
11
+ MEMPOOL = "mempool"
12
+ DEFAULT = "default"
13
+
14
+
15
+ class TransactionStatus(Enum):
16
+ UNCONFIRMED = "unconfirmed"
17
+ CONFIRMED = "confirmed"
18
+
19
+
20
+ @dataclasses.dataclass
21
+ class AddressTxData:
22
+ tx_id: str
23
+ address: str
24
+ type: AddressTxType
25
+ # figure out the way to get amount in vin
26
+ _amount: int = 0
27
+ tx_status: str = "unconfirmed"
28
+ is_reveal_tx: bool = False
29
+
30
+ def get_amount(self):
31
+ return self._amount
32
+
33
+ def amount_in_btc(self):
34
+ if self.type == AddressTxType.INPUT:
35
+ return "Amount is not supported for input type, yet"
36
+ return self._amount / 100000000
File without changes