I want to return the response without disturbing the data coming from subscription of node in django

Viewed 25

async def get_event(self,address): infura_ws_url = 'wss://ropsten.infura.io/ws/v3/7c074579719748599c087f6090c413e2' address_checksumed = self.w3.toChecksumAddress(address) async with connect(infura_ws_url) as ws: await ws.send('{"jsonrpc": "2.0", "id": 1, "method": "eth_subscribe", "params": ["newPendingTransactions"]}') # print("HERE") subscription_response = await ws.recv() print(subscription_response)

        while True:
            try:
                message = await asyncio.wait_for(ws.recv(), timeout=15)
                response = json.loads(message)
               
                txHash = response['params']['result']
               
                tx =self.w3.eth.get_transaction(txHash)
               
                if tx.to == address_checksumed :
                    print("Pending transaction fincoming:")
                    print({
                        "hash": txHash,
                        "from": tx["from"],
                        "value": self.w3.fromWei(tx["value"], 'ether')
                    })
                    transaction_receipt_json = {
                        "transaction_hash": txHash,
                        "from": tx["from"],
                        "value": self.w3.fromWei(tx["value"], 'ether') 
                    } 
                    return transaction_receipt_json        
                    
                    # return Response(transaction_receipt_json)                                         
                pass
            except Exception as e:
                print("Exception")
                print(e.args)
                print(e.__str__)

                pass


@action(detail=False, methods=['GET'], url_path='subscribe-deposit')
def subscribe_deposit_address(self, request):
    address = self.request.query_params.get('address')
    # get_event.delay(address,)

    # return Response('Address subscribed')
    loop = asyncio.new_event_loop()
    asyncio.set_event_loop(loop)
    while True:
        resp = loop.run_until_complete(self.get_event(address))
        return Response(resp)

when I call this API from postman it subscribe the pending tx and returns when any transaction pending contains my address as destination but immediately after returning the response on postman my request ends and I don't want it that my request get end I want my request don't end and whenever any pending tx comes which contains my address as destination it returns the tx in response

0 Answers
Related