Пример #1
0
 def usd_to_satoshi(cls):  # pragma: no cover
     # Special case - Uses Bitfinex to get the USD rate
     r = requests.get(cls.BITFINEX_BSVUSD_ENDPOINT)
     r.raise_for_status()
     usdbsv = r.json()['mid']
     # Get satoshis per usd
     rate = Decimal(BSV) / Decimal(usdbsv)
     return rate
Пример #2
0
    def currency_to_satoshi(cls, currency):
        if currency == 'usd':
            return cls.usd_to_satoshi()
        # Get satoshi / usd rate
        satoshis_per_usd = cls.usd_to_satoshi()

        # Get fx rate / usd rate
        r = requests.get(cls.EXCHANGERATEAPI_ENDPOINT)
        r.raise_for_status()
        fx_rate = r.json()['rates'][USD_PAIRS[currency]]

        # calculate satoshis / fx rate
        satoshis_per_fx_rate = Decimal(satoshis_per_usd) * (Decimal(1) / Decimal(fx_rate))
        return satoshis_per_fx_rate
Пример #3
0
def satoshi_to_currency(num, currency):
    """Converts a given number of satoshi to another currency as a formatted
    string rounded down to the proper number of decimal places.

    :param num: The number of satoshi.
    :type num: ``int``
    :param currency: One of the :ref:`supported currencies`.
    :type currency: ``str``
    :rtype: ``str``
    """
    return '{:f}'.format(
        Decimal(num / Decimal(EXCHANGE_RATES[currency]())).quantize(
            Decimal('0.' + '0' * CURRENCY_PRECISION[currency]),
            rounding=ROUND_DOWN).normalize())
Пример #4
0
def satoshi_to_currency_cached(num, currency):
    """Converts a given number of satoshi to another currency as a formatted
    string rounded down to the proper number of decimal places. Results are
    cached using a decorator for 60 seconds by default. See :ref:`cache times`.

    :param num: The number of satoshi.
    :type num: ``int``
    :param currency: One of the :ref:`supported currencies`.
    :type currency: ``str``
    :rtype: ``str``
    """
    return '{:f}'.format(
        Decimal(num /
                Decimal(currency_to_satoshi_cached(1, currency))).quantize(
                    Decimal('0.' + '0' * CURRENCY_PRECISION[currency]),
                    rounding=ROUND_DOWN).normalize())
Пример #5
0
    def currency_to_satoshi(cls, currency):
        r = requests.get(cls.SINGLE_RATE + currency)
        if r.status_code != 200:
            raise requests.exceptions.ConnectionError
        rate = r.json()['value']

        return int(ONE / Decimal(rate) * BSV)
Пример #6
0
    def wrapper(amount, currency):
        now = time()

        cached_rate = cached_rates[currency]

        if not cached_rate.satoshis or now - cached_rate.last_update > DEFAULT_CACHE_TIME:
            cached_rate.satoshis = EXCHANGE_RATES[currency]()
            cached_rate.last_update = now

        return int(cached_rate.satoshis * Decimal(amount))
Пример #7
0
def currency_to_satoshi(amount, currency):
    """Converts a given amount of currency to the equivalent number of
    satoshi. The amount can be either an int, float, or string as long as
    it is a valid input to :py:class:`decimal.Decimal`.

    :param amount: The quantity of currency.
    :param currency: One of the :ref:`supported currencies`.
    :type currency: ``str``
    :rtype: ``int``
    """
    satoshis = EXCHANGE_RATES[currency]()
    return int(satoshis * Decimal(amount))
Пример #8
0
 def currency_to_satoshi(cls, currency):
     r = requests.get(cls.SINGLE_RATE + currency)
     r.raise_for_status()
     rate = r.json()['value']
     return int(ONE / Decimal(rate) * BSV)
Пример #9
0
from collections import OrderedDict
from decimal import ROUND_DOWN
from functools import wraps
from time import time

import requests

from bitsv.utils import Decimal
from bitsv.constants import SATOSHI, uBSV, mBSV, BSV

DEFAULT_CACHE_TIME = 60

# Constant for use in deriving exchange
# rates when given in terms of 1 BSV.
ONE = Decimal(1)

SUPPORTED_CURRENCIES = OrderedDict([
    ('satoshi', 'Satoshi'),
    ('ubsv', 'Microbitcoincash'),
    ('mbsv', 'Millibitcoincash'),
    ('bsv', 'BitcoinCash'),
    ('usd', 'United States Dollar'),
    ('eur', 'Eurozone Euro'),
    ('gbp', 'Pound Sterling'),
    ('jpy', 'Japanese Yen'),
    ('cny', 'Chinese Yuan'),
    ('cad', 'Canadian Dollar'),
    ('aud', 'Australian Dollar'),
    ('nzd', 'New Zealand Dollar'),
    ('rub', 'Russian Ruble'),
    ('brl', 'Brazilian Real'),
Пример #10
0
 def currency_to_satoshi(cls, currency):
     rate = requests.get(cls.SINGLE_RATE.format(currency)).json()
     return int(Decimal(rate) * BCH)
Пример #11
0
 def currency_to_satoshi(cls, currency):
     rate = requests.get(cls.SINGLE_RATE + currency).json()['rate']
     return int(ONE / Decimal(rate) * BCH)
Пример #12
0
 def currency_to_satoshi(cls, currency):
     upper_currency = currency.upper()
     r = requests.get(cls.SINGLE_RATE + upper_currency)
     r.raise_for_status()
     rate = r.json()[upper_currency]
     return int(ONE / Decimal(rate) * BSV)
Пример #13
0
def test_decimal():
    assert Decimal(0.8) == Decimal('0.8')