Skip to content

Instantly share code, notes, and snippets.

@divyasshree-BQ
Created July 19, 2024 15:50
Show Gist options
  • Save divyasshree-BQ/84bc875bf1c3c3e9088653d5c9b5d7eb to your computer and use it in GitHub Desktop.
Save divyasshree-BQ/84bc875bf1c3c3e9088653d5c9b5d7eb to your computer and use it in GitHub Desktop.
import asyncio
import pandas as pd
from gql import Client, gql
from gql.transport.websockets import WebsocketsTransport
async def run_subscription():
# Setup WebSocket connection
transport = WebsocketsTransport(
url="wss://streaming.bitquery.io/eap?token=your_token",
headers={"Sec-WebSocket-Protocol": "graphql-ws"})
# Establish the connection
await transport.connect()
print("Connected to WebSocket")
try:
general_df = pd.DataFrame()
print("General Table")
while True:
async for result in transport.subscribe(
gql("""
subscription {
Solana {
General: DEXTradeByTokens {
Block {
Time
}
Trade {
Amount
Price
Currency {
Symbol
Name
}
Side {
Amount
Currency {
Symbol
Name
MetadataAddress
}
}
Dex {
ProgramAddress
ProtocolFamily
ProtocolName
}
Market {
MarketAddress
}
Order {
LimitAmount
LimitPrice
OrderId
}
PriceInUSD
}
}
}
}
""")):
if result.data:
new_data = pd.json_normalize(
result.data['Solana']['General'])
general_df = pd.concat([general_df, new_data],
ignore_index=True)
print(general_df)
finally:
await transport.close()
def main():
asyncio.run(run_subscription())
if __name__ == "__main__":
main()
@sumittale5654
Copy link

this is giving me the bad request error

@divyasshree-BQ
Copy link
Author

@sumittale5654 please add your token.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment