def execCmdCancelOrderReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO, BinanceCTProto.CT_PROTO]: srvSendTimestamp = getTimeStamp() # Message to clients (pub/sub) sendForward = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['from'], _fromto_to=recv.fromto['to'], _timestamp=recv.timestamp, _cmdtype=recv.cmdtype, # must be: BinanceCTProto.CT_TYPE_REQUEST _resp_timestamp= srvSendTimestamp, # Client will receive the SrvSend timestamp _data=recv.data) # Message response to CopyTrade sendResponse = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=srvSendTimestamp, _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE(BinanceCTProto.CT_PROTO_RESP_OK, "Ok")) return ([True, "Ok", sendForward, sendResponse])
def execCmdPingReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO, BinanceCTProto.CT_PROTO]: sendBadResponse = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=getTimeStamp(), _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE( BinanceCTProto.CT_PROTO_RESP_PING, "Ping resp")) return ([True, "Ok", recv, sendResponse])
def execCmdGetOpenOrdersReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO, BinanceCTProto.CT_PROTO]: # TODO: send get open orders to pubsub sendResponse = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=getTimeStamp(), _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE(BinanceCTProto.CT_PROTO_RESP_OK, "Ok")) return ([True, "Ok", None, sendResponse])
# --- SAMPLE 1 ------------------- print("--- SAMPLE 1 -------------------") con = envelop_sendRecv.connection() ret, msgret = con.connectToServer(ctm_signalSource_address, int(ctm_signalSource_port), socket.AF_INET, socket.SOCK_STREAM) if ret == False: print(msgret, file=stderr) exit(1) copytrade_sample1 = BinanceCTProto.CT_PROTO() copytrade_sample1.cmd = BinanceCTProto.CT_CMD_COPYTRADE copytrade_sample1.fromto = {'from': ctm_name, 'to': 'SrvSend'} copytrade_sample1.timestamp = getTimeStamp() copytrade_sample1.cmdtype = "REQ" copytrade_sample1.response_timestamp = "" copytrade_sample1.data = BinanceCTProto.CT_PROTO_COPYTRADE_DATA( _symbol="BTCUSDT", _side="BUY", _ordid="1234567890", _ordtype="LIMIT", _price="10.10101010", _qtd=10, _priceStop="123.12", _priceLimit="321231") ret, msg = copytrade_sample1.formatToNet() if ret == False:
ping_sample1.response_timestamp = "" ping_qtdNum = int(ping_qtd) continuePing = True qtdSent = 0 while continuePing == True: con = envelop_sendRecv.connection() ret, msgret = con.connectToServer(ping_signalSource_address, int(ping_signalSource_port), socket.AF_INET, socket.SOCK_STREAM) if ret == False: print(msgret, file=stderr) exit(1) ping_sample1.timestamp = getTimeStamp() msg = ping_sample1.formatToNet() con.sendMsg(msg, len(msg)) ret, retmsg, msgRecv = con.recvMsg() if ret == False: print(f"Error: [{retmsg}]") exit(1) sleep(int(ping_interval)) if ping_qtdNum > 0: qtdSent += 1 print( f'Sent: [{msg}]\nReceived: [{msgRecv}]\nInterval [{ping_interval}]s [{qtdSent} of {ping_qtdNum}]', file=stderr)
def execCmdCopytradeReq( recv: BinanceCTProto.CT_PROTO = None ) -> [bool, str, BinanceCTProto.CT_PROTO, BinanceCTProto.CT_PROTO]: ret, retmsg = srvDB.DB.insertCmd(recv) if ret == False: return ([ False, f"Database error inserting recv cmd [{retmsg}]", None, None ]) srvSendTimestamp = getTimeStamp() # Message to clients (pub/sub) sendForward = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['from'], _fromto_to=recv.fromto['to'], _timestamp=recv.timestamp, _cmdtype=recv.cmdtype, # must be: BinanceCTProto.CT_TYPE_REQUEST _resp_timestamp= srvSendTimestamp, # Client will receive the SrvSend timestamp _data=recv.data) ret, retmsg = srvDB.DB.insertCmd(sendForward) if ret == False: sendResponseErr = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=srvSendTimestamp, _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE( BinanceCTProto.CT_PROTO_COPYTRADE_ERROR1, "Insert sendForward error")) return ([ False, f"Database error inserting forward cmd [{retmsg}]", None, sendResponse ]) # Message response to CopyTrade sendResponse = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=srvSendTimestamp, _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE(BinanceCTProto.CT_PROTO_RESP_OK, "Ok")) ret, retmsg = srvDB.DB.insertCmd(sendResponse) if ret == False: sendResponseErr = BinanceCTProto.CT_PROTO( _cmd=BinanceCTProto.CT_CMD_COPYTRADE, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=srvSendTimestamp, _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE( BinanceCTProto.CT_PROTO_COPYTRADE_ERROR2, "Insert sendResponse error")) return ([ False, f"Database error inserting response cmd [{retmsg}]", None, sendResponseErr ]) return ([True, "Ok", sendForward, sendResponse])
srvDB.DB.commit() elif recv.cmdtype == BinanceCTProto.CT_TYPE_RESPONSE: logging.info( "A RESPONSE cmd. not yet implemented (maybe... never. only in SrvDataClient)." ) else: logging.info( f"It is not a REQUEST neither a RESPONSE copytrade cmd: [recv.cmdtype]. Discarting!" ) sendBadResponse = BinanceCTProto.CT_PROTO( _cmd=recv.cmd, _fromto_from=recv.fromto['to'], _fromto_to=recv.fromto['from'], _timestamp=getTimeStamp(), _cmdtype=BinanceCTProto.CT_TYPE_RESPONSE, _resp_timestamp=recv.timestamp, _data=BinanceCTProto.CT_PROTO_RESPONSE( BinanceCTProto.CT_PROTO_COPYTRADE_ERROR3, "Undef CT request or response")) elif recv.cmd == BinanceCTProto.CT_CMD_PING: logging.info("PING!") ret, retmsg, sendForward, sendResponse = execCmdPingReq(recv) if ret == False: logging.info(f"Error execCmdPingReq() [{retmsg}]!") srvDB.DB.rollback() safeExit(1) # TODO: exit proc? elif recv.cmd == BinanceCTProto.CT_CMD_CANCELORDER: