Skip to content

Commit

Permalink
Merge pull request #60 from aadyanthaya/greedbot-bitcoinpayment
Browse files Browse the repository at this point in the history
Test bench payment support using merchant log api
  • Loading branch information
DarrenWestwood authored May 4, 2023
2 parents 776c827 + 66bafc0 commit 5a36de9
Showing 1 changed file with 27 additions and 19 deletions.
46 changes: 27 additions & 19 deletions blockonomics.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from decimal import Decimal
import json
import re
from urllib.parse import parse_qs, urlparse

import database as db

Expand Down Expand Up @@ -64,43 +65,50 @@ def check_for_pending_transactions(self) -> None:

response = self._get_history_for_addresses(addresses=pending_addresses)

# Update Pending Transactions
for transaction in response.get('pending', []):
# Update Transactions for logs
for transaction in response.get('logs', []):
self.handle_update(
address=transaction['addr'][0],
status=transaction['status'],
satoshi=transaction['value'],
txid=transaction['txid']
)

# Update Confirmed Transactions
for transaction in response.get('history', []):
self.handle_update(
address=transaction['addr'][0],
status=2,
satoshi=transaction['value'],
txid=transaction['txid']
)

def _get_history_for_addresses(self, addresses: list) -> dict:

api_key = configloader.user_cfg["Bitcoin"]["api_key"]

url = "https://www.blockonomics.co/api/searchhistory"
body = { "addr": ", ".join(addresses) }
url = "https://www.blockonomics.co/api/merchant_logs"
body = {"match_callback": f"https://www.blockonomics.co/api/test_callback?secret={Blockonomics._get_secret()}"}
headers = { "Authorization": "Bearer %s" % api_key }

r = requests.post(
r = requests.get(
url=url,
data=json.dumps(body),
params=body,
headers=headers
)

if r.status_code == 200:
return r.json()
data = r.json()
formatted_data = {"logs": []}

for transaction in data:
parsed_url = urlparse(transaction['url'])
query_params = parse_qs(parsed_url.query)

if 'addr' in query_params and 'status' in query_params and 'txid' in query_params and 'value' in query_params:
formatted_data['logs'].append({
'addr': [query_params['addr'][0]],
'status': int(query_params['status'][0]),
'value': int(query_params['value'][0]),
'txid': query_params['txid'][0]
})


return formatted_data
else:
log.error("Get Payments History failed, Status: %s, Response: %s" % (r.status_code, r.content))
return {"pending": [], "history": []}
return {"logs": []}


def _satoshi_to_fiat(self, satoshi, transaction_price) -> float:
"""Convert satoshi to fiat"""
Expand Down Expand Up @@ -178,4 +186,4 @@ def handle_update(self, address, status, satoshi, txid) -> str:
else:
self.session.commit()
return "Transaction already proccessed"


0 comments on commit 5a36de9

Please sign in to comment.