def post(self, request): serializer = send.SendFromPostParametersSerializer(data=request.DATA) #from_account_balance = btc_rpc_call.get_balance(account=serializer) if serializer.is_valid(): currency = serializer.data["currency"] btc_rpc_call = BTCRPCCall(wallet=serializer.data["wallet"], currency=currency) #check is testnet or not is_test_net = constantutil.check_service_is_test_net(btc_rpc_call) from_account = serializer.data["fromAddress"] to_address = serializer.data["toAddress"] fee_limit = serializer.data["feeLimit"] send_amount = serializer.data["amount"] balance = btc_rpc_call.get_balance(account=from_account) from_account_is_valid = (btc_rpc_call.do_validate_address(address=from_account))["isvalid"] to_address_is_valid = (btc_rpc_call.do_validate_address(address=to_address))["isvalid"] if not from_account_is_valid: response_serializer = self.__send_to(send_status="NOK", message="invalid send from address %s" % from_account, test=is_test_net) return Response(data=response_serializer.data, status=status.HTTP_406_NOT_ACCEPTABLE) if not to_address_is_valid: response_serializer = self.__send_to(send_status="NOK", message="invalid send to address %s" % to_address, test=is_test_net) return Response(data=response_serializer.data, status=status.HTTP_406_NOT_ACCEPTABLE) if (send_amount + fee_limit) > balance: response_serializer = self.__send_to(send_status="NOK", message="There is no enough fund from %s" % from_account, test=is_test_net) return Response(data=response_serializer.data, status=status.HTTP_406_NOT_ACCEPTABLE) try: send_response_tx_id = btc_rpc_call.send_from(from_account=from_account, to_address=to_address, amount=float(send_amount)) transaction = btc_rpc_call.do_get_transaction(send_response_tx_id) log.info(abs(transaction["fee"])) send_response = SendFromResponse(tx_id=send_response_tx_id, status="OK", fee=abs(transaction["fee"]), test=is_test_net) send_response_serializer = SendFromResponseSerializer(send_response) except JSONRPCException as ex: log.info("Error: %s" % ex.error['message']) send_response = SendFromResponse(status="NOK", message=ex.error['message'], test=is_test_net) send_response_serializer = SendFromResponseSerializer(send_response) return Response(data=send_response_serializer.data, status=status.HTTP_200_OK)
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)