def __init__(self): self.yml_config = ConfigFileReader() self.fee = self.yml_config.get_reserved_fee_for_transferring( currency='btc') self.confirms = self.yml_config.get_min_transfer_confirmations( currency='btc') self.btc_rpc_call = BTCRPCCall(wallet='receive', currency='btc') self.coin_to_be_send_dict = self.__init_dict_of_accounts()
def __get_txIds(self, account="", btc_service=BTCRPCCall()): if not isinstance(btc_service, BTCRPCCall): raise TypeError("Expected object BTCRPCCall, got %s" % (type(btc_service),)) transactions = btc_service.list_transactions(account=account, count=88) transactions_with_tx_id = [] for transaction in transactions: if 'txid' in transaction and transaction['category'] == 'receive': transaction_with_tx_id = check_multi_receives.TxIdTransaction(txid=transaction['txid'], received=transaction['amount'], confirmations=transaction['confirmations'], date=datetime.fromtimestamp(transaction['time'])) transactions_with_tx_id.append(transaction_with_tx_id.__dict__) # txIds = map(lambda transaction: transaction['txid'], transactions_with_tx_id) return transactions_with_tx_id
def __receive_amount_for_risk(self, wallet_address="", tx_ids=[], btc_service=BTCRPCCall()): if not isinstance(btc_service, BTCRPCCall): raise TypeError("Expected object BTCRPCCall, got %s" % (type(btc_service),)) result = Decimal( btc_service.amount_received_by_address(address=wallet_address, confirms=RISK_HIGH_CONFIRMATIONS)) low_risk_counter = 0 medium_risk_counter = 0 high_risk_counter = 0 for tx_id in tx_ids: log.info("tx_id confirmation is %d.", tx_id["confirmations"]) if tx_id["confirmations"] == RISK_HIGH_CONFIRMATIONS: high_risk_counter += 1 if tx_id["confirmations"] >= RISK_MEDIUM_CONFIRMATIONS and tx_id["confirmations"] < RISK_LOW_CONFIRMATIONS: medium_risk_counter += 1 if tx_id["confirmations"] >= RISK_LOW_CONFIRMATIONS: low_risk_counter += 1 log.info("low_risk_counter: %d, medium_risk_counter: %d, high_risk_counter: %d", low_risk_counter, medium_risk_counter, high_risk_counter) if high_risk_counter >= 1: log.info("received with 0 confirmed") log.info(result) log.info("high") return {"result": result, "risk": 'high'} if medium_risk_counter >= 1: log.info("received with 1 confirmed") log.info(result) log.info("medium") return {"result": result, "risk": 'medium'} if low_risk_counter >= 1: log.info("received with 6 confirmed") log.info(result) log.info("low") return {"result": result, "risk": 'low'}
def post(self, request): log.info(request.data) post_serializers = check_multi_receives.PostParametersSerializer(data=request.data) response_list = [] if post_serializers.is_valid(): log.info(post_serializers.data["transactions"]) transactions = post_serializers.data["transactions"] try: btc_rpc_call = BTCRPCCall() is_test_net = constantutil.check_service_is_test_net(btc_rpc_call) for transaction in transactions: log.info(transaction) transaction_address = transaction["address"] address_validation = btc_rpc_call.do_validate_address(address=transaction_address) if address_validation["isvalid"] is False: return Response(transaction_address + " is not a valid address", status=status.HTTP_400_BAD_REQUEST) if address_validation["ismine"] is False: log.info(transaction_address + " is not an address of the wallet") return Response(transaction_address + " is not an address of the wallet", status=status.HTTP_400_BAD_REQUEST) tx_ids = self.__get_txIds(transaction["address"], btc_service=btc_rpc_call) log.debug(tx_ids) received_with_risk = self.__receive_amount_for_risk(wallet_address=transaction_address, tx_ids=tx_ids, btc_service=btc_rpc_call) received = Decimal(received_with_risk["result"]) if received_with_risk else 0.0 risk = received_with_risk["risk"] if received_with_risk else "low" log.info("received: %f, risk: %s", received, risk) response = check_multi_receives.ReceiveInformationResponse(currency=transaction["currency"], address=transaction_address, received=received, risk=risk, txs=tx_ids) print(address_validation) response_list.append(response.__dict__) receives_response = check_multi_receives.ReceivesInformationResponse(receives=response_list, test=is_test_net) except JSONRPCException as ex: log.error("Error: %s" % ex.error['message']) error_message = "Bitcoin RPC error, check if username and password for node is correct. Message from " \ "python-bitcoinrpc: " + ex.message receives_response = check_multi_receives.ReceivesInformationResponse(receives=response_list, test=True, error=1, error_message=error_message ) except socket_error as serr: if serr.errno != errno.ECONNREFUSED: receives_response = check_multi_receives.ReceivesInformationResponse(receives=[], test=True, error=1, error_message="A general socket error was raised." ) else: receives_response = check_multi_receives.ReceivesInformationResponse(receives=[], test=True, error=1, error_message="Connection refused error, " "check if the wallet node is down." ) response_dict = receives_response.__dict__ response_serializer = check_multi_receives.ReceivesInformationResponseSerializer(data=response_dict) if response_serializer.is_valid(): return Response(response_serializer.data, status=status.HTTP_201_CREATED) else: return Response(response_serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE) return Response(post_serializers.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): global response_serializer post_serializer = transfers_using_sendtoaddress.PostParametersSerializer( data=request.data) yml_config = ConfigFileReader() if post_serializer.is_valid(): transfer_list = post_serializer.data["transfers"] response_list = [] try: btc_rpc_call = BTCRPCCall() is_test_net = constantutil.check_service_is_test_net( btc_rpc_call) for transfer in transfer_list: log.info(transfer) currency = transfer["currency"] txFee = transfer["txFee"] send_amount = transfer["amount"] log.info(send_amount) to_address = yml_config.get_safe_address_to_be_transferred( currency=currency) log.info("%s, %s, %s" % (currency, to_address, send_amount)) to_address_is_valid = (btc_rpc_call.do_validate_address( address=to_address))["isvalid"] log.info("%s" % (to_address_is_valid)) if to_address_is_valid: try: if lock.locked() is False: lock.acquire() btc_rpc_call.set_tx_fee(txFee) send_response_tx_id = btc_rpc_call.send_to_address( address=to_address, amount=send_amount) lock.release() transaction = btc_rpc_call.do_get_transaction( send_response_tx_id) response = \ transfers_using_sendtoaddress.TransferInformationResponse(currency=currency, to_address=to_address, amount=Decimal(str(send_amount)), fee=abs(transaction["fee"]), message="Transfer is done", status="ok", txid=send_response_tx_id) except JSONRPCException as ex: if lock.locked() is True: lock.release() log.error("Error: %s" % ex.error['message']) response = transfers_using_sendtoaddress.TransferInformationResponse( currency=currency, to_address=to_address, amount=Decimal(str(send_amount)), message=ex.error['message'], status="fail", txid="") except (LockTimeoutException, LockException): log.error("Error: %s" % "LockTimeoutException or LockException") response = transfers_using_sendtoaddress.TransferInformationResponse( currency=currency, to_address=to_address, amount=Decimal(str(send_amount)), message="LockTimeoutException or LockException", status="fail", txid="") except (ConnectionError, ServerDown): log.error( "Error: ConnectionError or ServerDown exception" ) response = transfers_using_sendtoaddress.TransferInformationResponse( currency=currency, to_address=to_address, amount=Decimal(str(send_amount)), message= "Error: ConnectionError or ServerDown exception", status="fail", txid="") response_list.append(response.__dict__) else: log.info("do nothing") response = transfers_using_sendtoaddress.TransferInformationResponse( currency=currency, to_address=to_address, amount=Decimal(str(send_amount)), message="to_address is not valid", status="fail", txid="") response_list.append(response.__dict__) log.info(response_list) transfers_response = transfers_using_sendtoaddress.TransfersInformationResponse( transfers=response_list, test=is_test_net) except JSONRPCException as ex: if lock.locked() is True: lock.release() log.error("Error: %s" % ex.error['message']) transfers_response = transfers_using_sendtoaddress.TransfersInformationResponse( transfers=[], test=True, error=1, error_message= "Bitcoin RPC error, check if username and password " "for node is correct. Message from python-bitcoinrpc: " + ex.message) except socket_error as serr: if lock.locked() is True: lock.release() if serr.errno != errno.ECONNREFUSED: transfers_response = transfers_using_sendtoaddress.TransfersInformationResponse( transfers=[], test=True, error=1, error_message="A general socket error was raised.") else: transfers_response = transfers_using_sendtoaddress.TransfersInformationResponse( transfers=[], test=True, error=1, error_message= "Connection refused error, check if the wallet" " node is down.") response_dict = transfers_response.__dict__ response_serializer = transfers_using_sendtoaddress.TransfersInformationResponseSerializer( data=response_dict) if response_serializer.is_valid(): return Response(response_serializer.data, status=status.HTTP_200_OK) else: return Response(response_serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE) return Response(post_serializer.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): serializer_post = send_many_vo.SendManyPostParametersSerializer( data=request.data) if serializer_post.is_valid(): log.info(serializer_post.data) currency = serializer_post.data["currency"] wallet = serializer_post.data["wallet"] txFee = serializer_post.data["txFee"] btc_rpc_call = BTCRPCCall(wallet=wallet, currency=currency) from_account = serializer_post.data['fromAddress'] log.info(from_account) amounts = serializer_post.data['toSend'] amounts_dict = dict() for amount in amounts: if amount['toAddress'] in amounts_dict: #As the values in amounts_dict[amount['toAddress']] and amount['amount'] is a string, They need to be converted current_amount = float(amounts_dict[amount['toAddress']]) extra_amount = float(amount['amount']) new_amount = current_amount + extra_amount amounts_dict[amount['toAddress']] = '%.9f' % new_amount else: amounts_dict[amount['toAddress']] = amount['amount'] response = None try: is_test_net = constantutil.check_service_is_test_net( btc_rpc_call) if lock.locked() is False: lock.acquire() btc_rpc_call.set_tx_fee(txFee) isSuccess, result = btc_rpc_call.send_many( from_account=from_account, amounts=amounts_dict) lock.release() if (isSuccess): log.info(result) transaction = btc_rpc_call.do_get_transaction(result) if transaction is None: response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message="BTC server - " + wallet + "is done.", test=is_test_net, error=1) else: response = send_many_vo.SendManyResponse( tx_id=result, status=status.HTTP_200_OK, fee=abs(transaction["fee"]), message="Send many is done.", test=is_test_net) elif result is not None and isinstance( result, JSONRPCException): if lock.locked() is True: lock.release() log.info("Error: %s" % result.error['message']) response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message=result.error['message'], test=is_test_net, error=1) elif result is not None and isinstance( result, socket.error): log.info(result.errno == errno.ECONNREFUSED) log.info(result.message) response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message=result.message, test=is_test_net, error=1) except (LockTimeoutException, LockException): is_test_net = constantutil.check_service_is_test_net( btc_rpc_call) log.error("Error: %s" % "LockTimeoutException or LockException") response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message="LockTimeoutException or LockException", test=is_test_net, error=1) except (ConnectionError, ServerDown): log.error("Error: ConnectionError or ServerDown exception") response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message="Error: ConnectionError or ServerDown exception", test=True, error=1) except JSONRPCException as ex: if lock.locked() is True: lock.release() log.error("Error: %s" % ex.error['message']) error_message = "Bitcoin RPC error, check if username and password for node is correct. Message from " \ "python-bitcoinrpc: " + ex.message response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message=error_message, test=True, error=1, error_message=error_message) except socket_error as serr: if lock.locked() is True: lock.release() if serr.errno != errno.ECONNREFUSED: error_message = "A general socket error was raised." response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message=error_message, test=True, error=1, error_message=error_message) else: error_message = "Connection refused error, check if the wallet node is down." response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message=error_message, test=True, error=1, error_message=error_message) if (response is not None): send_many_response_serializer = send_many_vo.SendManyResponseSerializer( data=response.__dict__) else: is_test_net = constantutil.check_service_is_test_net( btc_rpc_call) response = send_many_vo.SendManyResponse( status=status.HTTP_500_INTERNAL_SERVER_ERROR, fee=0, message="Error: response is None", test=is_test_net, error=1) send_many_response_serializer = send_many_vo.SendManyResponseSerializer( data=response.__dict__) if send_many_response_serializer.is_valid(): return Response(send_many_response_serializer.data, status=status.HTTP_200_OK) else: return Response(send_many_response_serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE) return Response(serializer_post.errors, status=status.HTTP_400_BAD_REQUEST)
def post(self, request): post_serializers = wallet_balance.GetWalletBalancePostParameterSerializer( data=request.data) wallet_balance_response_list = [] if post_serializers.is_valid(): currency = post_serializers.data["currency"] wallet_list = yml_config.get_wallet_list(currency) log.info(wallet_list) for wallet in wallet_list: try: log.info(wallet) btc_rpc_call = BTCRPCCall(wallet=wallet, currency=currency) is_test_net = constantutil.check_service_is_test_net( btc_rpc_call) log.info(is_test_net) balance = btc_rpc_call.get_wallet_balance() log.info(format(balance, '0.8f')) wallet_balance_response = wallet_balance.WalletBalanceResponse( wallet=wallet, balance=Decimal(balance), test=is_test_net) log.info(wallet_balance_response.__dict__) wallet_balance_response_list.append( wallet_balance_response.__dict__) except socket_error as serr: if serr.errno != errno.ECONNREFUSED: wallet_balance_response = wallet_balance.WalletBalanceResponse( wallet=wallet, balance=Decimal(0), test=True, error=1, error_message="A general socket error was raised.") wallet_balance_response_list.append( wallet_balance_response.__dict__) else: wallet_balance_response = wallet_balance.WalletBalanceResponse( wallet=wallet, balance=Decimal(0), test=True, error=1, error_message= "Connection refused error, check if the wallet node is down." ) wallet_balance_response_list.append( wallet_balance_response.__dict__) except JSONRPCException as ex: wallet_balance_response = wallet_balance.WalletBalanceResponse( wallet=wallet, balance=Decimal(0), test=True, error=1, error_message= "Bitcoin RPC error, check if username and password for node is correct. Message from python-bitcoinrpc: " + ex.message) wallet_balance_response_list.append( wallet_balance_response.__dict__) wallets_balance_response = wallet_balance.WalletsBalanceResponse( wallets=wallet_balance_response_list) wallets_balance_response_serializer = \ wallet_balance.WalletsBalanceResponseSerializer(data=wallets_balance_response.__dict__) if wallets_balance_response_serializer.is_valid(): return Response(wallets_balance_response_serializer.data, status=status.HTTP_201_CREATED) else: return Response(wallets_balance_response_serializer.errors, status=status.HTTP_406_NOT_ACCEPTABLE) return Response(post_serializers.errors, status=status.HTTP_400_BAD_REQUEST)