コード例 #1
0
ファイル: rates.py プロジェクト: twinsant/bit
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())
コード例 #2
0
ファイル: rates.py プロジェクト: twinsant/bit
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())
コード例 #3
0
ファイル: rates.py プロジェクト: twinsant/bit
    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))
コード例 #4
0
ファイル: rates.py プロジェクト: twinsant/bit
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))
コード例 #5
0
 def test_zero_places(self):
     assert Decimal(satoshi_to_currency(100000,
                                        'jpy')).as_tuple().exponent == 0
コード例 #6
0
ファイル: rates.py プロジェクト: twinsant/bit
 def currency_to_satoshi(cls, currency):
     headers = {"x-accept-version": "2.0.0", "Accept": "application/json"}
     r = requests.get(cls.SINGLE_RATE + currency, headers=headers)
     r.raise_for_status()
     rate = r.json()['data']['rate']
     return int(ONE / Decimal(rate) * BTC)
コード例 #7
0
ファイル: rates.py プロジェクト: twinsant/bit
 def currency_to_satoshi(cls, currency):
     r = requests.get(cls.SINGLE_RATE.format(currency))
     r.raise_for_status()
     rate = r.text
     return int(Decimal(rate) * BTC)
コード例 #8
0
ファイル: test_utils.py プロジェクト: Nugetzrul3/sweet
def test_decimal():
    assert Decimal(0.8) == Decimal('0.8')
コード例 #9
0
ファイル: rates.py プロジェクト: twinsant/bit
from collections import OrderedDict
from decimal import ROUND_DOWN
from functools import wraps
from time import time

import requests

from bit.constants import SATOSHI, uBTC, mBTC, BTC
from bit.utils import Decimal

DEFAULT_CACHE_TIME = 60

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

SUPPORTED_CURRENCIES = OrderedDict([('satoshi', 'Satoshi'),
                                    ('ubtc', 'Microbitcoin'),
                                    ('mbtc', 'Millibitcoin'),
                                    ('btc', 'Bitcoin'),
                                    ('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'),
                                    ('chf', 'Swiss Franc'),
コード例 #10
0
 def currency_to_satoshi(cls, currency):
     r = requests.get(cls.SINGLE_RATE + currency)
     r.raise_for_status()
     rate = r.json()['rate']
     return int(ONE / Decimal(rate) * BTC)
コード例 #11
0
 def currency_to_satoshi(cls, currency: str) -> int:
     rate = requests.get(cls.SINGLE_RATE.format(currency)).json()["price"]
     return int(ONE / Decimal(rate) * PPC)
コード例 #12
0
 def currency_to_satoshi(cls, currency):
     rate = requests.get(cls.SINGLE_RATE.format(currency)).json()
     return int(Decimal(rate) * BTC)
コード例 #13
0
 def currency_to_satoshi(cls, currency):
     rate = requests.get(cls.SINGLE_RATE + currency).json()['rate']
     return int(ONE / Decimal(rate) * BTC)
コード例 #14
0
ファイル: rates.py プロジェクト: Nugetzrul3/sweet
 def currency_to_satoshi(cls, currency):
     headers = {"accept": "application/json"}
     r = requests.get(cls.SINGLE_RATE + currency, headers=headers)
     r.raise_for_status()
     rate = r.json()['sugarchain']['usd']
     return int(ONE / Decimal(rate) * SUGAR)