示例#1
0
    def query_btc_account_balance(account: BTCAddress) -> FVal:
        """Queries blockchain.info for the balance of account

        May raise:
        - InputError if the given account is not a valid BTC address
        - RemotError if there is a problem querying blockchain.info
        """
        try:
            btc_resp = request_get_direct(
                url='https://blockchain.info/q/addressbalance/%s' % account,
                handle_429=True,
                # If we get a 429 then their docs suggest 10 seconds
                # https://blockchain.info/q
                backoff_in_seconds=10,
            )
        except InvalidBTCAddress:
            # TODO: Move this validation into our own code and before the balance query
            raise InputError(
                f'The given string {account} is not a valid BTC address')
        except (requests.exceptions.ConnectionError,
                UnableToDecryptRemoteData) as e:
            raise RemoteError(
                f'blockchain.info API request failed due to {str(e)}')

        return satoshis_to_btc(FVal(btc_resp))  # result is in satoshis
示例#2
0
    def query_btc_account_balance(account: BTCAddress) -> FVal:
        """Queries blockchain.info for the balance of account

        May raise:
        - RemotError if there is a problem querying blockchain.info or blockcypher
        """
        try:
            if account.lower()[0:3] == 'bc1':
                url = f'https://api.blockcypher.com/v1/btc/main/addrs/{account.lower()}/balance'
                response_data = request_get(url=url)
                if 'balance' not in response_data:
                    raise RemoteError(f'Unexpected blockcypher balance response: {response_data}')
                btc_resp = response_data['balance']
            else:
                btc_resp = request_get_direct(
                    url='https://blockchain.info/q/addressbalance/%s' % account,
                    handle_429=True,
                    # If we get a 429 then their docs suggest 10 seconds
                    # https://blockchain.info/q
                    backoff_in_seconds=10,
                )
        except (requests.exceptions.ConnectionError, UnableToDecryptRemoteData) as e:
            raise RemoteError(f'bitcoin external API request failed due to {str(e)}')

        return satoshis_to_btc(FVal(btc_resp))  # result is in satoshis
示例#3
0
 def query_btc_account_balance(account: BTCAddress) -> FVal:
     btc_resp = request_get_direct(
         url='https://blockchain.info/q/addressbalance/%s' % account,
         handle_429=True,
         # If we get a 429 then their docs suggest 10 seconds
         # https://blockchain.info/q
         backoff_in_seconds=10,
     )
     return FVal(btc_resp) * FVal('0.00000001')  # result is in satoshis
示例#4
0
 def query_btc_account_balance(account: BTCAddress) -> FVal:
     btc_resp = request_get_direct(
         'https://blockchain.info/q/addressbalance/%s' % account, )
     return FVal(btc_resp) * FVal('0.00000001')  # result is in satoshis