예제 #1
0
    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)
예제 #2
0
    def post(self, request):
        log.info(request.DATA)
        post_serializers = check_multi_receives.PostParametersSerializer(data=request.DATA)
        btc_rpc_call = BTCRPCCall()
        is_test_net = constantutil.check_service_is_test_net(btc_rpc_call)

        response_list = []
        if post_serializers.is_valid():
            log.info(post_serializers.data["transactions"])
            transactions = post_serializers.data["transactions"]
            for transaction in transactions:
                log.info(transaction)

                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
                    )

                received_with_risk = self.__receive_amount_for_risk(
                    wallet_address=transaction["address"],
                    expected_amount=transaction["amount"],
                    btc_service=btc_rpc_call,
                )
                tx_ids = self.__get_txIds(transaction["address"], btc_service=btc_rpc_call)
                log.info(tx_ids)
                log.info(Decimal(received_with_risk["result"]))
                response = check_multi_receives.ReceiveInformationResponse(
                    currency=transaction["currency"],
                    address=transaction["address"],
                    received=Decimal(received_with_risk["result"]),
                    risk=received_with_risk["risk"],
                    txs=tx_ids,
                )

                response_list.append(response.__dict__)

            receives_response = check_multi_receives.ReceivesInformationResponse(
                receives=response_list, test=is_test_net
            )
            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):
    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)
예제 #5
0
    def post(self, request):
        post_serializer = transfers.PostParametersSerializer(data=request.DATA)

        # from_account_balance = btc_rpc_call.get_balance(account=serializer)

        yml_config = ConfigFileReader()


        if post_serializer.is_valid():

            btc_rpc_call = BTCRPCCall()
            is_test_net = constantutil.check_service_is_test_net(btc_rpc_call)

            transfer_list = post_serializer.data["transfers"]
            log.info(is_test_net)
            response_list = []


            for transfer in transfer_list:
                log.info(transfer)
                currency = transfer["currency"]
                from_address = transfer["from_address"]

                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, %s" % (currency, from_address,  to_address, send_amount))
                #log.info("%s %s" % currency, from_address)

                from_address_is_valid = (btc_rpc_call.do_validate_address(address=from_address))["isvalid"]
                to_address_is_valid = (btc_rpc_call.do_validate_address(address=to_address))["isvalid"]

                log.info("%s, %s" % (from_address_is_valid, to_address_is_valid))

                if from_address_is_valid and to_address_is_valid:
                    try:

                        send_response_tx_id = btc_rpc_call.send_from(from_account=from_address,
                                                                     to_address=to_address, amount=send_amount)

                        response = transfers.TransferInformationResponse(currency=currency,
                                                                         from_address=from_address,
                                                                         to_address=to_address,
                                                                         amount=Decimal(str(send_amount)),
                                                                         status="ok",
                                                                         txid=send_response_tx_id)

                        response_list.append(response.__dict__)
                    except JSONRPCException as ex:
                        log.info("Error: %s" % ex.error['message'])
                        response = transfers.TransferInformationResponse(currency=currency,
                                                                         from_address=from_address,
                                                                         to_address=to_address,
                                                                         amount=Decimal(str(send_amount)),
                                                                         status="fail",
                                                                         txid="")
                        response_list.append(response.__dict__)

                else:
                    log.info("do nothing")
                    response = transfers.TransferInformationResponse(currency=currency,
                                                                     from_address=from_address,
                                                                     to_address=to_address,
                                                                     amount=Decimal(str(send_amount)),
                                                                     status="fail",
                                                                     txid="")
                    response_list.append(response.__dict__)

            log.info(response_list)

            transfers_response = transfers.TransfersInformationResponse(transfers=response_list, test=is_test_net)

            response_dict = transfers_response.__dict__

        response_serializer = transfers.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_serializers.errors, status=status.HTTP_400_BAD_REQUEST)