SatSale

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 4084d1ca6f34a45898c9bc3e639fa3188aee1eb8
parent 6cce6cd58ba71ce777eaec300752934710d4512e
Author: NicholasFarrow <nicholas.w.farrow@gmail.com>
Date:   Mon, 14 Dec 2020 21:45:15 +1100

price feed function with basic exception handling. Single source of price

Diffstat:
MREADME.md | 26+++++++++++++-------------
Ainvoice/price_feed.py | 36++++++++++++++++++++++++++++++++++++
2 files changed, 49 insertions(+), 13 deletions(-)

diff --git a/README.md b/README.md @@ -33,26 +33,26 @@ Requires end-to-end payment, handling both success and failure # On php post, -# Call btcpy with invoice commands -# Invoice is in USD (default) and converts automatically to BTC -# conversion_to_usd.py -# Call bitcoind to create a new address -# Save invoice with address to database +* Call btcpy with invoice commands +* Invoice is in USD (default) and converts automatically to BTC +* conversion_to_usd.py +* Call bitcoind to create a new address +* Save invoice with address to database - Pandas - SQL? - Other? -# read address form database back: -# Pass address back to website -# Wait for user send... +* read address form database back: +* Pass address back to website +* Wait for user send... # btc_pay -# Check bitcoind mempool every 5 seconds for incoming transactions, -# wait for x confirmations (x also determines invoice_timeout) -# After x confirmations, return as paid to website and unlock/call any other function. This could even be a custom script like sale_success_command.sh (link to digital good, call other APIs) -# Need to integrate with at least 1 shop, woocommerce, as a proof of concept. +* Check bitcoind mempool every 5 seconds for incoming transactions, +* wait for x confirmations (x also determines invoice_timeout) +* After x confirmations, return as paid to website and unlock/call any other function. This could even be a custom script like sale_success_command.sh (link to digital good, call other APIs) +* Need to integrate with at least 1 shop, woocommerce, as a proof of concept. # lighting_pay -# Same but integrate c_lightning.py for proof of concept. +* Same but integrate c_lightning.py for proof of concept. # success/ diff --git a/invoice/price_feed.py b/invoice/price_feed.py @@ -0,0 +1,36 @@ +import requests + +def get_price(currency): + price_feed = "https://api.coindesk.com/v1/bpi/currentprice.json" + r = requests.get(price_feed) + + try: + price_data = r.json() + prices = price_data['bpi'] + except: + print("Failed to reach {}.".format(price_feed)) + return None + + try: + price = prices[currency]['rate'].replace(',', '') + except: + print("Failed to find currency {} from {}.".format(currency, price_feed)) + return None + + return price + + +def get_btc_value(dollar_value, currency): + if (price := get_price(currency)) is not None: + + try: + float_value = float(price) * dollar_value + if not isinstance(float_value, float): + raise Exception("Dollar value should be a float.") + except Exception as e: + print(e) + raise + + return float_value + + raise Exception("Failed to get dollar value.")