Skip to content

Commit 72a48a3

Browse files
committed
1) MATTERCLOUD_API_KEY environment variable required for Mattercloud api activation
2) sort utxos by confirmations (most confs at top) and value (lower value at top).
1 parent 520549a commit 72a48a3

File tree

5 files changed

+31
-36
lines changed

5 files changed

+31
-36
lines changed

bitsv/network/services/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
from .bitindex3 import (
2-
BitIndex3, BitIndex3MainNet, BitIndex3TestNet, BitIndex3STN)
1+
from .mattercloud import (
2+
MatterCloud, MatterCloudMainNet, MatterCloudTestNet, MatterCloudSTN)
33
from .network import NetworkAPI, set_service_timeout, DEFAULT_TIMEOUT
44
from .whatsonchain import WhatsonchainNormalised
55
from .bchsvexplorer import BCHSVExplorerAPI

bitsv/network/services/bchsvexplorer.py

Lines changed: 7 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,20 +55,6 @@ def get_transactions(cls, address):
5555
r.raise_for_status() # pragma: no cover
5656
return r.json()['transactions']
5757

58-
def woc_tx_to_transaction(response):
59-
tx_inputs = []
60-
for vin in response['vin']:
61-
tx_input = TxInput(txid=vin['txid'], index=vin['vout'])
62-
tx_inputs.append(tx_input)
63-
64-
tx_outputs = []
65-
for vout in response['vout']:
66-
tx_output = TxOutput(scriptpubkey=vout['scriptPubKey']['hex'], amount=vout['value'])
67-
tx_outputs.append(tx_output)
68-
tx = Transaction(response['txid'], tx_inputs, tx_outputs)
69-
70-
return tx
71-
7258
@classmethod
7359
def get_transaction(cls, txid):
7460
r = requests.get(cls.MAIN_TX_API.format(txid), timeout=DEFAULT_TIMEOUT)
@@ -99,13 +85,14 @@ def raw_get_transaction(cls, txid):
9985
def get_unspents(cls, address):
10086
r = requests.get(cls.MAIN_UNSPENT_API.format(address), timeout=DEFAULT_TIMEOUT)
10187
r.raise_for_status() # pragma: no cover
102-
return [
103-
Unspent(currency_to_satoshi(tx['amount'], 'bsv'),
104-
tx['scriptPubKey'],
105-
tx['txid'],
106-
tx['vout'])
107-
for tx in r.json()
88+
utxos = [
89+
Unspent(amount=currency_to_satoshi(utxo['amount'], 'bsv'),
90+
confirmations=utxo['confirmations'],
91+
txid=utxo['txid'],
92+
txindex=utxo['vout'])
93+
for utxo in r.json()
10894
]
95+
return sorted(utxos, key=lambda utxo: (-utxo.confirmations, utxo.amount))
10996

11097
@classmethod
11198
def send_transaction(cls, rawtx): # pragma: no cover
Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
import json
2+
23
import requests
34

45
from bitsv.network.meta import Unspent
56
from bitsv.network.transaction import Transaction, TxInput, TxOutput
67

8+
MATTERCLOUD_API_KEY_VARNAME = 'MATTERCLOUD_API_KEY'
9+
710

811
def woc_tx_to_transaction(response):
912
tx_inputs = []
@@ -20,9 +23,9 @@ def woc_tx_to_transaction(response):
2023
return tx
2124

2225

23-
class BitIndex3:
26+
class MatterCloud:
2427
"""
25-
Implements version 3 of the BitIndex API
28+
Implements version 3 of the MatterCloud API
2629
2730
:param network: select 'main', 'test', or 'stn'
2831
:type network: ``str``
@@ -68,12 +71,13 @@ def get_unspents(self, address, sort=False, sort_direction='asc'):
6871
headers=self.headers,
6972
)
7073
r.raise_for_status()
71-
return [Unspent(
74+
utxos = [Unspent(
7275
amount=tx['satoshis'],
7376
confirmations=tx['confirmations'],
7477
txid=tx['txid'],
7578
txindex=tx['vout'],
7679
) for tx in r.json()]
80+
return sorted(utxos, key=lambda utxo: (-utxo.confirmations, utxo.amount))
7781

7882
def get_balance(self, address):
7983
"""
@@ -283,7 +287,7 @@ def get_xpub_addresses(
283287
:param address: Filter by a specific address in the xpub
284288
"""
285289
r = requests.get(
286-
'https://api.mattercloud.net/api/v3/{self.network}/xpub/{xpub}/addrs'.format(self.network, xpub),
290+
'https://api.mattercloud.net/api/v3/{}/xpub/{}/addrs'.format(self.network, xpub),
287291
params={
288292
'offset': offset,
289293
'limit': limit,
@@ -393,7 +397,7 @@ def update_webhook_monitored_addresses(self, address):
393397
return r.json()
394398

395399

396-
class BitIndex3MainNet(BitIndex3):
400+
class MatterCloudMainNet(MatterCloud):
397401
"""
398402
Implements version 3 of the BitIndex API using the mainnet network
399403
"""
@@ -402,7 +406,7 @@ def __init__(self, *args, **kwargs):
402406
return super().__init__(*args, **kwargs, network='main')
403407

404408

405-
class BitIndex3TestNet(BitIndex3):
409+
class MatterCloudTestNet(MatterCloud):
406410
"""
407411
Implements version 3 of the BitIndex API using the testnet network
408412
"""
@@ -411,7 +415,7 @@ def __init__(self, *args, **kwargs):
411415
return super().__init__(*args, **kwargs, network='test')
412416

413417

414-
class BitIndex3STN(BitIndex3):
418+
class MatterCloudSTN(MatterCloud):
415419
"""
416420
Implements version 3 of the BitIndex API using the STN network
417421
"""

bitsv/network/services/network.py

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import os
12
from functools import wraps
23

34
import requests
@@ -7,7 +8,7 @@
78

89
from .whatsonchain import WhatsonchainNormalised
910

10-
from .bitindex3 import BitIndex3
11+
from .mattercloud import MatterCloud, MATTERCLOUD_API_KEY_VARNAME
1112
from .bchsvexplorer import BCHSVExplorerAPI
1213

1314
DEFAULT_TIMEOUT = 30
@@ -77,21 +78,24 @@ def __init__(self, network):
7778
self.network = network
7879

7980
# Instantiate Normalized apis
80-
self.bitindex3 = BitIndex3(api_key=None, network=self.network)
8181
self.bchsvexplorer = BCHSVExplorerAPI # classmethods, mainnet only
8282
self.whatsonchain = WhatsonchainNormalised(network=self.network)
8383

8484
# Allows extra apis for 'main' that may not support testnet (e.g. blockchair)
8585
if network == 'main':
86-
self.list_of_apis = collections.deque([self.whatsonchain, self.bitindex3,
87-
self.bchsvexplorer])
86+
self.list_of_apis = collections.deque([self.whatsonchain, self.bchsvexplorer])
8887
elif network == 'test':
89-
self.list_of_apis = collections.deque([self.whatsonchain, self.bitindex3])
88+
self.list_of_apis = collections.deque([self.whatsonchain])
9089
elif network == 'stn':
91-
self.list_of_apis = collections.deque([self.whatsonchain, self.bitindex3])
90+
self.list_of_apis = collections.deque([self.whatsonchain])
9291
else:
9392
raise ValueError("network must be either 'main', 'test' or 'stn'")
9493

94+
mattercloud_api_key = os.environ.get(MATTERCLOUD_API_KEY_VARNAME, None)
95+
if mattercloud_api_key:
96+
self.bitindex3 = MatterCloud(api_key=mattercloud_api_key, network=self.network)
97+
self.list_of_apis.appendleft(self.bitindex3)
98+
9599
@retry_annotation(IGNORED_ERRORS, tries=DEFAULT_RETRY)
96100
def retry_wrapper_call(self, api_call, param):
97101
return api_call(param)

bitsv/network/services/whatsonchain.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def woc_utxos_to_unspents(woc_utxos, block_height):
3131
txid=utxo['tx_hash'],
3232
txindex=utxo['tx_pos'])
3333
utxos.append(u)
34-
return utxos
34+
return sorted(utxos, key=lambda utxo: (-utxo.confirmations, utxo.amount))
3535

3636

3737
class WhatsonchainNormalised(Whatsonchain):

0 commit comments

Comments
 (0)