Skip to content

Add get_count_confirmations method to NetworkAPI #172

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
37 changes: 37 additions & 0 deletions bit/network/services.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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=""):
Expand Down Expand Up @@ -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.')
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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', ),
Expand Down