Пример #1
0
    def on_message(self, raw_message):
        if not self.trade_client.isConnected():
            return

        try:
            req_msg = JsonMessage(raw_message)
        except InvalidMessageException as e:
            self.write_message(
                '{"MsgType":"ERROR", "Description":"Invalid message", "Detail": "' +
                str(e) +
                '"}')
            self.application.unregister_connection(self)
            self.trade_client.close()
            self.close()
            return


        if req_msg.isUserRequest():
            if req_msg.has('Password'):
                raw_message = raw_message.replace(req_msg.get('Password'), '*')
            if req_msg.has('NewPassword'):
                raw_message = raw_message.replace(req_msg.get('NewPassword'), '*')

            self.application.log('IN', self.trade_client.connection_id ,raw_message )
        else:
            self.application.log('IN', self.trade_client.connection_id, raw_message )


        if req_msg.isTestRequest() or req_msg.isHeartbeat():
            dt = datetime.datetime.now()
            response_msg = {
                'MsgType'           : '0',
                'TestReqID'         : req_msg.get('TestReqID'),
                'ServerTimestamp'   : int(mktime(dt.timetuple()) + dt.microsecond/1000.0 )
            }

            sendTime = req_msg.get('SendTime')
            if sendTime:
                response_msg['SendTime'] = sendTime


            self.write_message(str(json.dumps(response_msg, cls=JsonEncoder)))
            return


        if req_msg.isTradeHistoryRequest():  # Trade History request
            self.on_trade_history_request(req_msg)
            return

        if req_msg.isMarketDataRequest():  # Market Data Request
            self.on_market_data_request(req_msg)

            if not self.trade_client.isConnected():
                self.application.log('DEBUG', self.trade_client.connection_id, 'not self.trade_client.isConnected()' )
                self.application.unregister_connection(self)
                self.trade_client.close()
                self.close()
            return

        if req_msg.isSecurityStatusRequest():
            self.on_security_status_request(req_msg)
            return

        if req_msg.isDepositRequest():
            if not req_msg.get('DepositMethodID') and not req_msg.get('DepositID'):

                currency = req_msg.get('Currency')

                secret = uuid.uuid4().hex
                callback_url = self.application.options.callback_url + secret

                hot_wallet  = self.get_broker_wallet('hot', currency)
                cold_wallet = self.get_broker_wallet('cold', currency)
                if not hot_wallet and not cold_wallet:
                    return

                if not hot_wallet and cold_wallet:
                    dest_wallet = cold_wallet
                elif hot_wallet and not cold_wallet:
                    dest_wallet = hot_wallet
                else:
                    # 62.5% of all deposits go to the cold wallet, and 37.5% go to the hot wallet
                    dest_wallet = hot_wallet
                    if secret[0] in ('0','1','2','3','4','5','6','7','8','9'):
                        dest_wallet = cold_wallet


                if not dest_wallet:
                    return

                parameters = urllib.urlencode({
                    'method': 'create',
                    'address': dest_wallet,
                    'callback': callback_url,
                    'currency': currency
                })


                try:
                    url_payment_processor = self.application.options.url_payment_processor + '?' + parameters
                    self.application.log('DEBUG', self.trade_client.connection_id, "invoking..."  + url_payment_processor )
                    response = urllib2.urlopen(url_payment_processor)
                    data = json.load(response)
                    self.application.log('DEBUG', self.trade_client.connection_id, str(data) )

                    req_msg.set('InputAddress', data['input_address'])
                    req_msg.set('Destination', data['destination'])
                    req_msg.set('Secret', secret)
                except urllib2.HTTPError as e:
                    out_message = json.dumps({
                      'MsgType': 'ERROR',
                      'ReqID': req_msg.get('DepositReqID'),
                      'Description': 'Blockchain.info is not available at this moment, please try again within few minutes',
                      'Detail': str(e)
                    })
                    self.write_message(out_message)
                    return
                except Exception as e:
                    out_message = json.dumps({
                      'MsgType': 'ERROR',
                      'ReqID': req_msg.get('DepositReqID'),
                      'Description': 'Error retrieving a new deposit address from Blockchain.info. Please, try again',
                      'Detail': str(e)
                    })
                    self.write_message(out_message)
                    return

        try:
            resp_message = self.trade_client.sendMessage(req_msg)
            if resp_message:
                self.write_message(resp_message.raw_message)

            if resp_message and resp_message.isUserResponse():
                self.user_response = resp_message

            if not self.trade_client.isConnected():
                self.application.log('DEBUG', self.trade_client.connection_id, 'not self.trade_client.isConnected()' )
                self.application.unregister_connection(self)
                self.trade_client.close()
                self.close()
        except TradeClientException as e:
            exception_message = {
                'MsgType': 'ERROR',
                'Description': 'Invalid message',
                'Detail': str(e)
            }
            self.write_message(json.dumps(exception_message))
            self.application.unregister_connection(self)
            self.trade_client.close()
            self.close()
    def onMessage(self, payload, isBinary):
        if isBinary:
            print("Binary message received: {0} bytes".format(len(payload)))
            reactor.stop()
            return

        if self.factory.verbose:
            print 'rx:', payload

        msg = JsonMessage(payload.decode('utf8'))
        if msg.isHeartbeat():
            return

        if msg.isUserResponse():  # login response
            if msg.get('UserStatus') != 1:
                reactor.stop()
                raise RuntimeError('Wrong login')

            profile = msg.get('Profile')
            if profile['Type'] != 'BROKER':
                reactor.stop()
                raise RuntimeError('It is not a brokerage account')

            self.factory.broker_username = msg.get('Username')
            self.factory.broker_id = msg.get('UserID')
            self.factory.profile = profile
            return

        if msg.isWithdrawRefresh():
            if msg.get('BrokerID') != self.factory.broker_id:
                return  # received a message from a different broker

            msg.set('BrokerUsername', self.factory.broker_username)

            if msg.get('Status') == '1'\
              and (self.factory.methods[0] == '*' or msg.get('Method') in self.factory.methods) \
              and (self.factory.currencies[0] == '*' or  msg.get('Currency') in self.factory.currencies):
                withdraw_record = Withdraw.process_withdrawal_refresh_message(
                    self.factory.db_session, msg)
                if withdraw_record:
                    process_withdraw_message = MessageBuilder.processWithdraw(
                        action='PROGRESS',
                        withdrawId=msg.get('WithdrawID'),
                        data=msg.get('Data'),
                        percent_fee=msg.get('PercentFee'),
                        fixed_fee=msg.get('FixedFee'))

                    withdraw_record.process_req_id = process_withdraw_message[
                        'ProcessWithdrawReqID']

                    # sent a B6
                    self.sendJSON(process_withdraw_message)
                    self.factory.db_session.commit()

        if msg.isProcessWithdrawResponse():
            if not msg.get('Result'):
                return

            process_req_id = msg.get('ProcessWithdrawReqID')
            withdraw_record = Withdraw.get_withdraw_by_process_req_id(
                self.factory.db_session, process_req_id)

            should_transfer = False
            if withdraw_record:
                if withdraw_record.status == '1' and msg.get('Status') == '2'\
                  and (self.factory.methods[0] == '*' or withdraw_record.method  in self.factory.methods)\
                  and (self.factory.currencies[0] == '*' or  withdraw_record.currency in self.factory.currencies):

                    if withdraw_record.account_id not in self.factory.blocked_accounts:
                        should_transfer = True

                withdraw_record.status = msg.get('Status')
                withdraw_record.reason = msg.get('Reason')
                withdraw_record.reason_id = msg.get('ReasonID')

                self.factory.db_session.add(withdraw_record)
                self.factory.db_session.commit()

                if should_transfer:
                    self.factory.reactor.callLater(
                        0, partial(self.initiateTransfer, process_req_id))
Пример #3
0
    def on_message(self, raw_message):
        if self.honey_pot_connection:
            self.application.log('INFO', "HONEY_POT", raw_message)

        if self.trade_client is None or not self.trade_client.isConnected():
            return

        self.last_message_datetime.append(datetime.now())
        message_time_last_second = self.last_message_datetime[-1] - timedelta(
            seconds=1)
        for x in xrange(0, len(self.last_message_datetime)):
            if self.last_message_datetime[x] > message_time_last_second:
                self.last_message_datetime = self.last_message_datetime[x:]
                break
        if len(self.last_message_datetime
               ) > 15:  # higher than 15 messages per second
            self.application.log(
                "ERROR", "TOO_MANY_MESSAGES",
                "Exceed 15 messages per second. [ip=" + self.remote_ip + ",'" +
                raw_message + "']")
            self.write_message(
                '{"MsgType":"ERROR", "Description":"Too many messages per second", "Detail": "16 messages in the last second"}'
            )
            self.application.unregister_connection(self)
            self.trade_client.close()
            self.close()
            return

        try:
            req_msg = JsonMessage(raw_message)
        except InvalidMessageException as e:
            self.write_message(
                '{"MsgType":"ERROR", "Description":"Invalid message", "Detail": "'
                + str(e) + '"}')
            self.application.unregister_connection(self)
            self.trade_client.close()
            self.close()
            return

        req_msg.set('RemoteIP', self.remote_ip)

        if req_msg.isUserRequest():
            if req_msg.has('Password'):
                raw_message = raw_message.replace(req_msg.get('Password'), '*')
            if req_msg.has('NewPassword'):
                raw_message = raw_message.replace(req_msg.get('NewPassword'),
                                                  '*')
            self.application.log('IN', self.trade_client.connection_id,
                                 raw_message)

        if req_msg.isTestRequest() or req_msg.isHeartbeat():
            dt = datetime.now()
            response_msg = {
                'MsgType':
                '0',
                'TestReqID':
                req_msg.get('TestReqID'),
                'ServerTimestamp':
                int(mktime(dt.timetuple()) + dt.microsecond / 1000.0)
            }

            sendTime = req_msg.get('SendTime')
            if sendTime:
                response_msg['SendTime'] = sendTime

            self.write_message(str(json.dumps(response_msg, cls=JsonEncoder)))
            return

        if req_msg.isTradeHistoryRequest():  # Trade History request
            self.on_trade_history_request(req_msg)
            return

        if req_msg.isMarketDataRequest():  # Market Data Request
            self.on_market_data_request(req_msg)

            if not self.trade_client.isConnected():
                self.application.log('DEBUG', self.trade_client.connection_id,
                                     'not self.trade_client.isConnected()')
                self.application.unregister_connection(self)
                self.trade_client.close()
                self.close()
            return

        if req_msg.isSecurityStatusRequest():
            self.on_security_status_request(req_msg)
            return

        if req_msg.isDepositRequest():
            if not req_msg.get('DepositMethodID') and not req_msg.get(
                    'DepositID'):

                currency = req_msg.get('Currency')

                secret = uuid.uuid4().hex
                callback_url = self.application.options.callback_url + secret

                hot_wallet = self.get_broker_wallet('hot', currency)
                cold_wallet = self.get_broker_wallet('cold', currency)
                if not hot_wallet and not cold_wallet:
                    return

                if not hot_wallet and cold_wallet:
                    dest_wallet = cold_wallet
                elif hot_wallet and not cold_wallet:
                    dest_wallet = hot_wallet
                else:
                    # 62.5% of all deposits go to the cold wallet, and 37.5% go to the hot wallet
                    dest_wallet = hot_wallet
                    if secret[0] in ('0', '1', '2', '3', '4', '5', '6', '7',
                                     '8', '9'):
                        dest_wallet = cold_wallet

                if not dest_wallet:
                    return

                parameters = urllib.urlencode({
                    'method': 'create',
                    'address': dest_wallet,
                    'callback': callback_url,
                    'currency': currency
                })

                try:
                    url_payment_processor = self.application.options.url_payment_processor + '?' + parameters
                    self.application.log('DEBUG',
                                         self.trade_client.connection_id,
                                         "invoking..." + url_payment_processor)
                    response = urllib2.urlopen(url_payment_processor)
                    data = json.load(response)
                    self.application.log('DEBUG',
                                         self.trade_client.connection_id,
                                         str(data))

                    req_msg.set('InputAddress', data['input_address'])
                    req_msg.set('Destination', data['destination'])
                    req_msg.set('Secret', secret)
                except urllib2.HTTPError as e:
                    out_message = json.dumps({
                        'MsgType':
                        'ERROR',
                        'ReqID':
                        req_msg.get('DepositReqID'),
                        'Description':
                        'Blockchain.info is not available at this moment, please try again within few minutes',
                        'Detail':
                        str(e)
                    })
                    self.write_message(out_message)
                    return
                except Exception as e:
                    out_message = json.dumps({
                        'MsgType':
                        'ERROR',
                        'ReqID':
                        req_msg.get('DepositReqID'),
                        'Description':
                        'Error retrieving a new deposit address from Blockchain.info. Please, try again',
                        'Detail':
                        str(e)
                    })
                    self.write_message(out_message)
                    return

        try:
            resp_message = self.trade_client.sendMessage(req_msg)
            if resp_message:
                self.write_message(resp_message.raw_message)

            if resp_message and resp_message.isUserResponse():
                self.user_response = resp_message
                if self.is_user_logged():
                    self.application.log('LOGIN_OK',
                                         self.trade_client.connection_id,
                                         raw_message)
                    #TODO: Request open order list
                    #self.trade_client.

                else:
                    self.application.log('LOGIN_FAILED',
                                         self.trade_client.connection_id,
                                         raw_message)

            if not self.trade_client.isConnected():
                self.application.log('DEBUG', self.trade_client.connection_id,
                                     'not self.trade_client.isConnected()')
                self.application.unregister_connection(self)
                self.trade_client.close()
                self.close()
        except TradeClientException as e:
            exception_message = {
                'MsgType': 'ERROR',
                'Description': 'Invalid message',
                'Detail': str(e)
            }
            self.write_message(json.dumps(exception_message))
            self.application.unregister_connection(self)
            self.trade_client.close()
            self.close()
Пример #4
0
  def onMessage(self, payload, isBinary):
    if isBinary:
      print("Binary message received: {0} bytes".format(len(payload)))
      reactor.stop()
      return

    if self.factory.verbose:
      print 'rx:',payload

    msg = JsonMessage(payload.decode('utf8'))
    if msg.isHeartbeat():
      return

    if msg.isUserResponse(): # login response
      if msg.get('UserStatus') != 1:
        reactor.stop()
        raise RuntimeError('Wrong login')

      profile = msg.get('Profile')
      if profile['Type'] != 'BROKER':
        reactor.stop()
        raise RuntimeError('It is not a brokerage account')

      self.factory.broker_username = msg.get('Username')
      self.factory.broker_id = msg.get('UserID')
      self.factory.profile = profile
      return


    if msg.isVerifyCustomerRefresh():
      broker_id         = msg.get('BrokerID')
      if broker_id != self.factory.broker_id:
        return # received a message from a different broker

      verified          = msg.get('Verified')
      if verified != 1:
        return

      user_id           = msg.get('ClientID')
      username          = msg.get('Username')
      verification_data = msg.get('VerificationData')
      if verification_data:
        verification_data = json.loads(verification_data)

      edentiti_status = get_verification_data(verification_data, 'edentiti_status')
      if edentiti_status == "IN_PROGRESS":
        return

      street_address = get_verification_data(verification_data, 'address')['street1']
      flat_number = ''
      if '/' in street_address:
        flat_number = street_address[:street_address.find('/') ]
        street_address = street_address[street_address.find('/')+1: ]

      street_address_parts = street_address.split(" ")
      street_type = ""
      if len(street_address_parts) > 1:
        street_type = street_address_parts[-1]
        street_address = ' '.join(street_address_parts[:-1])

      street_address_parts = street_address.split(" ")
      street_number = ""
      if street_address_parts and street_address_parts[0].isdigit():
        street_number = street_address_parts[0]
        street_address = ' '.join(street_address_parts[1:])

      street_name = street_address

      try:
        res = self.factory.wsdl_client.registerUser(
            customerId=self.factory.edentiti_customer_id,
            password=self.factory.edentiti_password,
            ruleId='default',
            name={
              'givenName': get_verification_data(verification_data, 'name')['first'],
              'middleNames': get_verification_data(verification_data, 'name')['middle'],
              'surname': get_verification_data(verification_data, 'name')['last']
            },
            currentResidentialAddress={
              'country':get_verification_data(verification_data, 'address')['country_code'],
              'flatNumber': flat_number,
              'postcode': get_verification_data(verification_data, 'address')['postal_code'],
              'propertyName':'',
              'state':get_verification_data(verification_data, 'address')['state'],
              'streetName':street_name,
              'streetNumber': street_number ,
              'streetType': street_type.upper(),
              'suburb':get_verification_data(verification_data, 'address')['city']
            },
            homePhone=get_verification_data(verification_data, 'phone_number'),
            dob=datetime.datetime.strptime( get_verification_data(verification_data, 'date_of_birth') ,"%Y-%m-%d"))

        dt = datetime.datetime.now()
        createdAt = int(mktime(dt.timetuple()) + dt.microsecond/1000000.0)
        edentiti_verification_data = {
          "service_provider":"edentiti",
          "edentiti_status": res['return']['outcome'],
          "id": res['return']['transactionId'],
          "user_id": res['return']['userId'],
          "status": res['return']['outcome'],
          "created_at": createdAt,
          "updated_at": createdAt
        }
        if res['return']['outcome'] == "IN_PROGRESS":
          edentiti_verification_data["status"] = "progress"
          verified = 2

        if edentiti_verification_data["status"] == "valid":
          verified = 3

        self.sendJSON( MessageBuilder.verifyCustomer(user_id, verified, json.dumps(edentiti_verification_data)))

      except Exception:
        pass
Пример #5
0
    def onMessage(self, payload, isBinary):
        if isBinary:
            print("Binary message received: {0} bytes".format(len(payload)))
            reactor.stop()
            return

        if self.factory.verbose:
            print 'rx:', payload

        msg = JsonMessage(payload.decode('utf8'))
        if msg.isHeartbeat():
            return

        if msg.isUserResponse():  # login response
            if msg.get('UserStatus') != 1:
                reactor.stop()
                raise RuntimeError('Wrong login')

            profile = msg.get('Profile')
            if profile['Type'] != 'BROKER':
                reactor.stop()
                raise RuntimeError('It is not a brokerage account')

            self.factory.broker_username = msg.get('Username')
            self.factory.broker_id = msg.get('UserID')
            self.factory.profile = profile
            return

        if msg.isVerifyCustomerRefresh():
            broker_id = msg.get('BrokerID')
            if broker_id != self.factory.broker_id:
                return  # received a message from a different broker

            verified = msg.get('Verified')
            if verified != 1:
                return

            user_id = msg.get('ClientID')
            username = msg.get('Username')
            verification_data = msg.get('VerificationData')
            if verification_data:
                verification_data = json.loads(verification_data)

            edentiti_status = get_verification_data(verification_data,
                                                    'edentiti_status')
            if edentiti_status == "IN_PROGRESS":
                return

            street_address = get_verification_data(verification_data,
                                                   'address')['street1']
            flat_number = ''
            if '/' in street_address:
                flat_number = street_address[:street_address.find('/')]
                street_address = street_address[street_address.find('/') + 1:]

            street_address_parts = street_address.split(" ")
            street_type = ""
            if len(street_address_parts) > 1:
                street_type = street_address_parts[-1]
                street_address = ' '.join(street_address_parts[:-1])

            street_address_parts = street_address.split(" ")
            street_number = ""
            if street_address_parts and street_address_parts[0].isdigit():
                street_number = street_address_parts[0]
                street_address = ' '.join(street_address_parts[1:])

            street_name = street_address

            try:
                res = self.factory.wsdl_client.registerUser(
                    customerId=self.factory.edentiti_customer_id,
                    password=self.factory.edentiti_password,
                    ruleId='default',
                    name={
                        'givenName':
                        get_verification_data(verification_data,
                                              'name')['first'],
                        'middleNames':
                        get_verification_data(verification_data,
                                              'name')['middle'],
                        'surname':
                        get_verification_data(verification_data,
                                              'name')['last']
                    },
                    currentResidentialAddress={
                        'country':
                        get_verification_data(verification_data,
                                              'address')['country_code'],
                        'flatNumber':
                        flat_number,
                        'postcode':
                        get_verification_data(verification_data,
                                              'address')['postal_code'],
                        'propertyName':
                        '',
                        'state':
                        get_verification_data(verification_data,
                                              'address')['state'],
                        'streetName':
                        street_name,
                        'streetNumber':
                        street_number,
                        'streetType':
                        street_type.upper(),
                        'suburb':
                        get_verification_data(verification_data,
                                              'address')['city']
                    },
                    homePhone=get_verification_data(verification_data,
                                                    'phone_number'),
                    dob=datetime.datetime.strptime(
                        get_verification_data(verification_data,
                                              'date_of_birth'), "%Y-%m-%d"))

                dt = datetime.datetime.now()
                createdAt = int(
                    mktime(dt.timetuple()) + dt.microsecond / 1000000.0)
                edentiti_verification_data = {
                    "service_provider": "edentiti",
                    "edentiti_status": res['return']['outcome'],
                    "id": res['return']['transactionId'],
                    "user_id": res['return']['userId'],
                    "status": res['return']['outcome'],
                    "created_at": createdAt,
                    "updated_at": createdAt
                }
                if res['return']['outcome'] == "IN_PROGRESS":
                    edentiti_verification_data["status"] = "progress"
                    verified = 2

                if edentiti_verification_data["status"] == "valid":
                    verified = 3

                self.sendJSON(
                    MessageBuilder.verifyCustomer(
                        user_id, verified,
                        json.dumps(edentiti_verification_data)))

            except Exception:
                pass
Пример #6
0
    def on_message(self, raw_message):
        if not self.trade_client.isConnected():
            return

        try:
            req_msg = JsonMessage(raw_message)
        except InvalidMessageException as e:
            self.write_message(
                '{"MsgType":"ERROR", "Description":"Invalid message", "Detail": "'
                + str(e) + '"}')
            self.application.unregister_connection(self)
            self.trade_client.close()
            self.close()
            return

        req_msg.set('RemoteIP', self.remote_ip)

        if req_msg.isUserRequest():
            if req_msg.has('Password'):
                raw_message = raw_message.replace(req_msg.get('Password'), '*')
            if req_msg.has('NewPassword'):
                raw_message = raw_message.replace(req_msg.get('NewPassword'),
                                                  '*')
            self.application.log('IN', self.trade_client.connection_id,
                                 raw_message)

        if req_msg.isTestRequest() or req_msg.isHeartbeat():
            dt = datetime.now()
            response_msg = {
                'MsgType':
                '0',
                'TestReqID':
                req_msg.get('TestReqID'),
                'ServerTimestamp':
                int(mktime(dt.timetuple()) + dt.microsecond / 1000.0)
            }

            sendTime = req_msg.get('SendTime')
            if sendTime:
                response_msg['SendTime'] = sendTime

            self.write_message(str(json.dumps(response_msg, cls=JsonEncoder)))
            return

        if req_msg.isTradeHistoryRequest():  # Trade History request
            self.on_trade_history_request(req_msg)
            return

        if req_msg.isMarketDataRequest():  # Market Data Request
            self.on_market_data_request(req_msg)

            if not self.trade_client.isConnected():
                self.application.log('DEBUG', self.trade_client.connection_id,
                                     'not self.trade_client.isConnected()')
                self.application.unregister_connection(self)
                self.trade_client.close()
                self.close()
            return

        if req_msg.isSecurityStatusRequest():
            self.on_security_status_request(req_msg)
            return

        if req_msg.isNewOrderSingle():
            if self.last_order_datetime:
                order_time = datetime.now()
                order_time_less_one_second = order_time - timedelta(
                    milliseconds=200)  # max of 5 orders per second
                if self.last_order_datetime > order_time_less_one_second:
                    #self.application.log('ORDER_REJECT', self.trade_client.connection_id, raw_message )
                    reject_message = {
                        "MsgType": "8",
                        "OrderID": None,
                        "ExecID": None,
                        "ExecType": "8",
                        "OrdStatus": "8",
                        "CumQty": 0,
                        "Symbol": req_msg.get("Symbol"),
                        "OrderQty": req_msg.get("Qty"),
                        "LastShares": 0,
                        "LastPx": 0,
                        "Price": req_msg.get("Price"),
                        "TimeInForce": "1",
                        "LeavesQty": 0,
                        "ExecSide": req_msg.get("Side"),
                        "Side": req_msg.get("Side"),
                        "OrdType": req_msg.get("OrdType"),
                        "CxlQty": req_msg.get("Qty"),
                        "ClOrdID": req_msg.get("ClOrdID"),
                        "AvgPx": 0,
                        "OrdRejReason":
                        "Exceeded the maximum number of orders sent per second.",
                        "Volume": 0
                    }
                    #self.write_message(str(json.dumps(reject_message, cls=JsonEncoder)))
                    #return

            self.last_order_datetime = datetime.now()

        if req_msg.isDepositRequest():
            if not req_msg.get('DepositMethodID') and not req_msg.get(
                    'DepositID'):

                currency = req_msg.get('Currency')

                secret = uuid.uuid4().hex
                callback_url = self.application.options.callback_url + secret

                hot_wallet = self.get_broker_wallet('hot', currency)
                cold_wallet = self.get_broker_wallet('cold', currency)
                if not hot_wallet and not cold_wallet:
                    return

                if not hot_wallet and cold_wallet:
                    dest_wallet = cold_wallet
                elif hot_wallet and not cold_wallet:
                    dest_wallet = hot_wallet
                else:
                    # 62.5% of all deposits go to the cold wallet, and 37.5% go to the hot wallet
                    dest_wallet = hot_wallet
                    if secret[0] in ('0', '1', '2', '3', '4', '5', '6', '7',
                                     '8', '9'):
                        dest_wallet = cold_wallet

                if not dest_wallet:
                    return

                parameters = urllib.urlencode({
                    'method': 'create',
                    'address': dest_wallet,
                    'callback': callback_url,
                    'currency': currency
                })

                try:
                    url_payment_processor = self.application.options.url_payment_processor + '?' + parameters
                    self.application.log('DEBUG',
                                         self.trade_client.connection_id,
                                         "invoking..." + url_payment_processor)
                    response = urllib2.urlopen(url_payment_processor)
                    data = json.load(response)
                    self.application.log('DEBUG',
                                         self.trade_client.connection_id,
                                         str(data))

                    req_msg.set('InputAddress', data['input_address'])
                    req_msg.set('Destination', data['destination'])
                    req_msg.set('Secret', secret)
                except urllib2.HTTPError as e:
                    out_message = json.dumps({
                        'MsgType':
                        'ERROR',
                        'ReqID':
                        req_msg.get('DepositReqID'),
                        'Description':
                        'Blockchain.info is not available at this moment, please try again within few minutes',
                        'Detail':
                        str(e)
                    })
                    self.write_message(out_message)
                    return
                except Exception as e:
                    out_message = json.dumps({
                        'MsgType':
                        'ERROR',
                        'ReqID':
                        req_msg.get('DepositReqID'),
                        'Description':
                        'Error retrieving a new deposit address from Blockchain.info. Please, try again',
                        'Detail':
                        str(e)
                    })
                    self.write_message(out_message)
                    return

        try:
            resp_message = self.trade_client.sendMessage(req_msg)
            if resp_message:
                self.write_message(resp_message.raw_message)

            if resp_message and resp_message.isUserResponse():
                self.user_response = resp_message
                if self.is_user_logged():
                    self.application.log('LOGIN_OK',
                                         self.trade_client.connection_id,
                                         raw_message)
                    #TODO: Request open order list
                    #self.trade_client.

                else:
                    self.application.log('LOGIN_FAILED',
                                         self.trade_client.connection_id,
                                         raw_message)

            if not self.trade_client.isConnected():
                self.application.log('DEBUG', self.trade_client.connection_id,
                                     'not self.trade_client.isConnected()')
                self.application.unregister_connection(self)
                self.trade_client.close()
                self.close()
        except TradeClientException as e:
            exception_message = {
                'MsgType': 'ERROR',
                'Description': 'Invalid message',
                'Detail': str(e)
            }
            self.write_message(json.dumps(exception_message))
            self.application.unregister_connection(self)
            self.trade_client.close()
            self.close()
    def onMessage(self, payload, isBinary):
        if isBinary:
            print ("Binary message received: {0} bytes".format(len(payload)))
            reactor.stop()
            return

        if self.factory.verbose:
            print "rx:", payload

        msg = JsonMessage(payload.decode("utf8"))
        if msg.isHeartbeat():
            return

        if msg.isUserResponse():  # login response
            if msg.get("UserStatus") != 1:
                reactor.stop()
                raise RuntimeError("Wrong login")

            profile = msg.get("Profile")
            if profile["Type"] != "BROKER":
                reactor.stop()
                raise RuntimeError("It is not a brokerage account")

            self.factory.broker_username = msg.get("Username")
            self.factory.broker_id = msg.get("UserID")
            self.factory.profile = profile
            return

        if msg.isWithdrawRefresh():
            if msg.get("BrokerID") != self.factory.broker_id:
                return  # received a message from a different broker

            msg.set("BrokerUsername", self.factory.broker_username)

            if (
                msg.get("Status") == "1"
                and (self.factory.methods[0] == "*" or msg.get("Method") in self.factory.methods)
                and (self.factory.currencies[0] == "*" or msg.get("Currency") in self.factory.currencies)
            ):
                withdraw_record = Withdraw.process_withdrawal_refresh_message(self.factory.db_session, msg)
                if withdraw_record:
                    process_withdraw_message = MessageBuilder.processWithdraw(
                        action="PROGRESS",
                        withdrawId=msg.get("WithdrawID"),
                        data=msg.get("Data"),
                        percent_fee=msg.get("PercentFee"),
                        fixed_fee=msg.get("FixedFee"),
                    )

                    withdraw_record.process_req_id = process_withdraw_message["ProcessWithdrawReqID"]

                    # sent a B6
                    self.sendJSON(process_withdraw_message)
                    self.factory.db_session.commit()

        if msg.isProcessWithdrawResponse():
            if not msg.get("Result"):
                return

            process_req_id = msg.get("ProcessWithdrawReqID")
            withdraw_record = Withdraw.get_withdraw_by_process_req_id(self.factory.db_session, process_req_id)

            should_transfer = False
            if withdraw_record:
                if (
                    withdraw_record.status == "1"
                    and msg.get("Status") == "2"
                    and (self.factory.methods[0] == "*" or withdraw_record.method in self.factory.methods)
                    and (self.factory.currencies[0] == "*" or withdraw_record.currency in self.factory.currencies)
                ):

                    if withdraw_record.account_id not in self.factory.blocked_accounts:
                        should_transfer = True

                withdraw_record.status = msg.get("Status")
                withdraw_record.reason = msg.get("Reason")
                withdraw_record.reason_id = msg.get("ReasonID")

                self.factory.db_session.add(withdraw_record)
                self.factory.db_session.commit()

                if should_transfer:
                    self.factory.reactor.callLater(0, partial(self.initiateTransfer, process_req_id))
  def onMessage(self, payload, isBinary):
    if isBinary:
      print("Binary message received: {0} bytes".format(len(payload)))
      reactor.stop()
      return

    if self.factory.verbose:
      print 'rx:',payload

    msg = JsonMessage(payload.decode('utf8'))
    if msg.isHeartbeat():
      return

    if msg.isUserResponse(): # login response
      if msg.get('UserStatus') != 1:
        reactor.stop()
        raise RuntimeError('Wrong login')

      profile = msg.get('Profile')
      if profile['Type'] != 'BROKER':
        reactor.stop()
        raise RuntimeError('It is not a brokerage account')

      self.factory.broker_username = msg.get('Username')
      self.factory.broker_id = msg.get('UserID')
      self.factory.profile = profile
      return

    if msg.isWithdrawRefresh():
      if msg.get('BrokerID') != self.factory.broker_id:
        return # received a message from a different broker

      msg.set('BrokerUsername', self.factory.broker_username )

      if msg.get('Status') == '1'\
        and (self.factory.methods[0] == '*' or msg.get('Method') in self.factory.methods) \
        and (self.factory.currencies[0] == '*' or  msg.get('Currency') in self.factory.currencies):
        withdraw_record = Withdraw.process_withdrawal_refresh_message( self.factory.db_session , msg)
        if withdraw_record:
          process_withdraw_message = MessageBuilder.processWithdraw(action      = 'PROGRESS',
                                                                    withdrawId  = msg.get('WithdrawID'),
                                                                    data        = msg.get('Data') ,
                                                                    percent_fee = msg.get('PercentFee'),
                                                                    fixed_fee   = msg.get('FixedFee') )

          withdraw_record.process_req_id = process_withdraw_message['ProcessWithdrawReqID']

          # sent a B6
          self.sendJSON( process_withdraw_message )
          self.factory.db_session.commit()

    if msg.isProcessWithdrawResponse():
      if not msg.get('Result'):
        return

      process_req_id = msg.get('ProcessWithdrawReqID')
      withdraw_record = Withdraw.get_withdraw_by_process_req_id(self.factory.db_session, process_req_id)



      should_transfer = False
      if withdraw_record:
        if withdraw_record.status == '1' and msg.get('Status') == '2'\
          and (self.factory.methods[0] == '*' or withdraw_record.method  in self.factory.methods)\
          and (self.factory.currencies[0] == '*' or  withdraw_record.currency in self.factory.currencies):

          if withdraw_record.account_id not in self.factory.blocked_accounts:
            should_transfer = True

        withdraw_record.status = msg.get('Status')
        withdraw_record.reason = msg.get('Reason')
        withdraw_record.reason_id = msg.get('ReasonID')

        self.factory.db_session.add(withdraw_record)
        self.factory.db_session.commit()

        if should_transfer:
          self.factory.reactor.callLater(0, partial(self.initiateTransfer, process_req_id) )