From ed72fc6506c2534831b76229537b7d283b70e13a Mon Sep 17 00:00:00 2001 From: daveusa <109542778+alteralt@users.noreply.github.com> Date: Mon, 21 Nov 2022 00:57:25 +0300 Subject: [PATCH] Add get_count_confirmations method to NetworkAPI --- bit/network/services.py | 37 +++++++++++++++++++++++++++++++++++++ setup.py | 2 +- 2 files changed, 38 insertions(+), 1 deletion(-) diff --git a/bit/network/services.py b/bit/network/services.py index 9578c63..e03c1b1 100644 --- a/bit/network/services.py +++ b/bit/network/services.py @@ -2,6 +2,7 @@ import json import logging from decimal import Decimal, getcontext +import blockcypher from bit.constants import BTC from bit.network import currency_to_satoshi @@ -944,6 +945,16 @@ def broadcast_tx_testnet(cls, tx_hex): # pragma: no cover return True if r.status_code == 200 else False +class BlockcypherApi: + @classmethod + def get_count_confirmations(cls, txid): + return blockcypher.get_num_confirmations(txid) + + @classmethod + def count_confirmations_testnet(cls, txid): + return blockcypher.get_num_confirmations(txid, coin_symbol="btc-testnet") + + class NetworkAPI: IGNORED_ERRORS = ( ConnectionError, @@ -986,6 +997,9 @@ class NetworkAPI: SmartbitAPI.broadcast_tx, # Limit 5/minute BlockchainAPI.broadcast_tx, ] + GET_COUNT_CONFIRMATIONS_MAIN = [ + BlockcypherApi.get_count_confirmations + ] GET_BALANCE_TEST = [ BlockchairAPI.get_balance_testnet, @@ -1015,6 +1029,9 @@ class NetworkAPI: BitcoreAPI.broadcast_tx_testnet, SmartbitAPI.broadcast_tx_testnet, # Limit 5/minute ] + GET_COUNT_CONFIRMATIONS_TEST = [ + BlockcypherApi.count_confirmations_testnet + ] @classmethod def connect_to_node(cls, user, password, host='localhost', port=8332, use_https=False, testnet=False, path=""): @@ -1250,3 +1267,23 @@ def broadcast_tx_testnet(cls, tx_hex): # pragma: no cover raise ConnectionError('Transaction broadcast failed, or Unspents were already used.') raise ConnectionError('All APIs are unreachable.') + + @classmethod + def get_count_confirmations(cls, txid): + for api_call in cls.GET_COUNT_CONFIRMATIONS_MAIN: + try: + return api_call(txid) + except cls.IGNORED_ERRORS: + pass + + raise ConnectionError('All APIs are unreachable.') + + @classmethod + def get_count_confirmations_testnet(cls, txid): + for api_call in cls.GET_COUNT_CONFIRMATIONS_TEST: + try: + return api_call(txid) + except cls.IGNORED_ERRORS: + pass + + raise ConnectionError('All APIs are unreachable.') diff --git a/setup.py b/setup.py index 1d3344b..be00dcb 100644 --- a/setup.py +++ b/setup.py @@ -41,7 +41,7 @@ 'Programming Language :: Python :: Implementation :: PyPy' ), - install_requires=('coincurve>=4.3.0', 'requests'), + install_requires=('coincurve>=4.3.0', 'requests', 'blockcypher'), extras_require={ 'cli': ('appdirs', 'click', 'privy', 'tinydb'), 'cache': ('lmdb', ),