bitvend/bitvend/utils.py

39 lines
1017 B
Python

# -*- coding: utf-8 -*-
import cachetools
import requests
@cachetools.cached(cachetools.TTLCache(32, 600))
def get_exchange_rate(currency='PLN'):
# Returns current exchange rate for selected currency
return requests.get('https://blockchain.info/pl/ticker').json()[currency]['last']
def to_local_currency(sat, safe=False):
# Returns satoshi in local lowest denomination currency (grosze)
try:
rate = get_exchange_rate()
except:
if safe:
return 0
raise
return int(sat / 1000000.0 * rate)
def from_local_currency(val, safe=False):
# Returns satoshi value from local currency
try:
rate = get_exchange_rate()
except:
if safe:
return 0
raise
return int(val / rate * 1000000)
def sat_to_btc(amount):
# Converts satoshi to BTC
return amount / 100000000.0
def format_btc(amount):
# Formats satoshi to human-readable format
return (u'฿%.8f' % (sat_to_btc(amount),)).rstrip('0').rstrip('.')