How-to receive a stream of trading events - subscribe to pool changes

If you identified a list of pools that are of interest for your use case, there is a way to receive real-time stream of trading events corresponding to these pools.

Using the code snippet below you can subscribe to the changes in the pool state:

import json
import requests
 
# Your pool IDs list
subscription_data = [
    "fa8dee6cf0627a82a2610019596758fc36c1ebc4b7e389fdabc44857fdf5c9b0e29ac56f1a584bccd487c445ad45383c6347d03d39869f759daad68284781723",
    "88726ba865186558078abd49d8b1d1eb2418b824a99afef443197445ee0bab70a96d2f5ab0f7256309b5df0ef56e7f9b0f8a37e84f0f36816badbcee18f04f34",
    "b752b73a8a38773b7499a6f9d516ecd14fb68e4c14b1e9a81cc8dac15ee4af1ce83ad10ec59b89f3a9ba38e6a77946239758b370523b57e6ca590472161d048e",
    "7e785da757fd529d2c090240c0f4d04cf6d43757b1ce702b2ed835057d59b118ba5534c5757a5a9fee0f1af8909ebd50a55eb2a5c9783c5637727508e48a2e47",
    "4cab43a35ef5d3fc8267670b547a404522e563ed399a8372888eb3d56177d5eca65949f258e75bc128c0521ff49e4e9d8207578721dc5e7310cadc1c50059f50",
]
resp = requests.post(
    url=f"https://api.charli3.io/api/v1/tokens/stream",
    json=subscription_data,
    stream=True,
)
for line in resp.iter_lines(decode_unicode=True):
    text = line.decode(encoding="utf-8")
    event = json.loads(text)
    print(event)