def initiateTransfer(self, process_req_id): withdraw_record = Withdraw.get_withdraw_by_process_req_id(self.factory.db_session, process_req_id) if withdraw_record.status != '2': return dest_pay = {} dest_pay[json.loads(withdraw_record.data)['Wallet']] = withdraw_record.amount try: tx_hash = self.factory.blocktrail_wallet.pay(dest_pay, change_address=self.factory.blocktrail_change_address, allow_zero_conf=True, randomize_change_idx=True) withdraw_record.response = tx_hash self.factory.db_session.add(withdraw_record) self.factory.db_session.commit() withdraw_data = json.loads(withdraw_record.data) withdraw_data['TransactionID'] = tx_hash withdraw_data['Currency'] = 'BTC' process_withdraw_message = MessageBuilder.processWithdraw(action = 'COMPLETE', withdrawId = withdraw_record.id , data = withdraw_data ) self.sendJSON( process_withdraw_message ) except Exception,e: withdraw_record.response = str(e) self.factory.db_session.add(withdraw_record) self.factory.db_session.commit() try: # send an email to the system administrator template_content = [ {'name': 'NotificationType', 'content': 'HOT_WALLET_SEND_ERROR'}, {'name': 'WithdrawalProtocol','content': 'Blocktrail'}, {'name': 'From', 'content': self.factory.blocktrail_change_address}, {'name': 'To', 'content': json.loads(withdraw_record.data)['Wallet']}, {'name': 'Amount', 'content': str(withdraw_record.amount)}, {'name': 'Currency', 'content': 'BTC'}, {'name': 'Error', 'content': str(e)} ] message = { 'from_email': '*****@*****.**', 'from_name': 'No reply', 'to': [{'email': '*****@*****.**', 'name': 'BlinkTrade system notifications', 'type': 'to' }], 'metadata': {'website': 'https://blinktrade.com/'}, 'global_merge_vars': template_content } result = self.mandrill_api.messages.send_template( template_name='system-notification', template_content=template_content, message=message) except Exception,e: print "Error sending the system notification email ", str(e)
def onBlockchainApiSuccessCallback(self, process_req_id, result): withdraw_record = Withdraw.get_withdraw_by_process_req_id( self.factory.db_session, process_req_id) result = json.loads(result) if "error" in result: withdraw_record.response = result["error"] self.factory.db_session.add(withdraw_record) self.factory.db_session.commit() if result["error"] == "Error Decrypting Wallet": reactor.stop() raise RuntimeError(result["error"]) if result["error"] == "pad block corrupted": reactor.stop() raise RuntimeError(result["error"]) elif result["error"] == "Second Password Incorrect": reactor.stop() raise RuntimeError(result["error"]) elif result[ "error"] == "Wallet Checksum did not validate. Serious error: Restore a backup if necessary.": reactor.stop() raise RuntimeError(result["error"]) elif "tx_hash" in result: tx_hash = result["tx_hash"] withdraw_data = json.loads(withdraw_record.data) withdraw_data['TransactionID'] = tx_hash withdraw_data['Currency'] = 'BTC' withdraw_record.response = json.dumps(result) self.factory.db_session.add(withdraw_record) self.factory.db_session.commit() process_withdraw_message = MessageBuilder.processWithdraw( action='COMPLETE', withdrawId=withdraw_record.id, data=withdraw_data) self.sendJSON(process_withdraw_message) print result
def onBlockchainApiSuccessCallback(self, process_req_id, result): withdraw_record = Withdraw.get_withdraw_by_process_req_id(self.factory.db_session, process_req_id) result = json.loads(result) if "error" in result: withdraw_record.response = result["error"] self.factory.db_session.add(withdraw_record) self.factory.db_session.commit() if result["error"] == "Error Decrypting Wallet": reactor.stop() raise RuntimeError(result["error"]) if result["error"] == "pad block corrupted": reactor.stop() raise RuntimeError(result["error"]) elif result["error"] == "Second Password Incorrect": reactor.stop() raise RuntimeError(result["error"]) elif result["error"] == "Wallet Checksum did not validate. Serious error: Restore a backup if necessary.": reactor.stop() raise RuntimeError(result["error"]) elif "tx_hash" in result: tx_hash = result["tx_hash"] withdraw_data = json.loads(withdraw_record.data) withdraw_data['TransactionID'] = tx_hash withdraw_data['Currency'] = 'BTC' withdraw_record.response = json.dumps(result) self.factory.db_session.add(withdraw_record) self.factory.db_session.commit() process_withdraw_message = MessageBuilder.processWithdraw(action = 'COMPLETE', withdrawId = withdraw_record.id , data = withdraw_data ) self.sendJSON( process_withdraw_message ) print result
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))
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 initiateTransfer(self, process_req_id): withdraw_record = Withdraw.get_withdraw_by_process_req_id( self.factory.db_session, process_req_id) if withdraw_record.status != '2': return dest_pay = {} dest_pay[json.loads( withdraw_record.data)['Wallet']] = withdraw_record.amount try: tx_hash = self.factory.blocktrail_wallet.pay( dest_pay, change_address=self.factory.blocktrail_change_address, allow_zero_conf=False, randomize_change_idx=True) withdraw_record.response = tx_hash self.factory.db_session.add(withdraw_record) self.factory.db_session.commit() withdraw_data = json.loads(withdraw_record.data) withdraw_data['TransactionID'] = tx_hash withdraw_data['Currency'] = 'BTC' process_withdraw_message = MessageBuilder.processWithdraw( action='COMPLETE', withdrawId=withdraw_record.id, data=withdraw_data) self.sendJSON(process_withdraw_message) except Exception, e: withdraw_record.response = str(e) self.factory.db_session.add(withdraw_record) self.factory.db_session.commit() try: # send an email to the system administrator template_content = [{ 'name': 'NotificationType', 'content': 'HOT_WALLET_SEND_ERROR' }, { 'name': 'WithdrawalProtocol', 'content': 'Blocktrail' }, { 'name': 'From', 'content': self.factory.blocktrail_change_address }, { 'name': 'To', 'content': json.loads(withdraw_record.data)['Wallet'] }, { 'name': 'Amount', 'content': '{:,.8f}'.format(withdraw_record.amount / 1e8) }, { 'name': 'Currency', 'content': 'BTC' }, { 'name': 'Error', 'content': str(e) }] message = { 'from_email': '*****@*****.**', 'from_name': 'No reply', 'to': [{ 'email': '*****@*****.**', 'name': 'BlinkTrade system notifications', 'type': 'to' }], 'metadata': { 'website': 'https://blinktrade.com/' }, 'global_merge_vars': template_content } result = self.factory.mandrill_api.messages.send_template( template_name='system-notification', template_content=template_content, message=message) except Exception, e: print "Error sending the system notification email ", str(e)