GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — Merge pull request #422 from InjectiveLabs/f/stream-oracle-prices · InjectiveLabs/sdk-python@896e330

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
  • examples/chain_client
    • 8_ChainStreamOraclePrices.py
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎examples/chain_client/8_ChainStreamOraclePrices.py‎

Copy file name to clipboard
+65Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import asyncio
2+
from contextlib import suppress
3+
from typing import Any, Dict
4+
5+
from grpc import RpcError
6+
7+
from pyinjective.async_client_v2 import AsyncClient
8+
from pyinjective.core.network import Network
9+
10+
ORACLE_TYPE = "chainlinkdatastreams"
11+
ORACLE_SYMBOLS = {
12+
"INJ": "0x000344d7a7d81f051ee273a63f94f8bef7d44ca89aa03e0c5bf4d085df19adb6",
13+
"USDC": "0x00038f83323b6b08116d1614cf33a9bd71ab5e0abf0c9f1b783a74a43e7bd992",
14+
}
15+
16+
17+
async def oracle_price_event_processor(event: Dict[str, Any]):
18+
for oracle_price in event.get("oraclePrices", []):
19+
print(
20+
{
21+
"blockHeight": event["blockHeight"],
22+
"blockTime": event["blockTime"],
23+
**oracle_price,
24+
}
25+
)
26+
27+
28+
def stream_error_processor(exception: RpcError):
29+
print(f"There was an error listening to oracle price updates ({exception})")
30+
31+
32+
def stream_closed_processor():
33+
print("The oracle price updates stream has been closed")
34+
35+
36+
async def main() -> None:
37+
network = Network.mainnet()
38+
39+
client = AsyncClient(network)
40+
composer = await client.composer()
41+
42+
print(f"Streaming {ORACLE_TYPE} prices for {', '.join(ORACLE_SYMBOLS)}")
43+
# To receive all oracle price updates, use composer.chain_stream_oracle_price_filter() without symbols.
44+
oracle_price_filter = composer.chain_stream_oracle_price_filter(symbols=list(ORACLE_SYMBOLS.values()))
45+
46+
task = asyncio.get_event_loop().create_task(
47+
client.listen_chain_stream_updates(
48+
callback=oracle_price_event_processor,
49+
on_end_callback=stream_closed_processor,
50+
on_status_callback=stream_error_processor,
51+
oracle_price_filter=oracle_price_filter,
52+
)
53+
)
54+
55+
try:
56+
await asyncio.sleep(delay=60)
57+
finally:
58+
task.cancel()
59+
with suppress(asyncio.CancelledError):
60+
await task
61+
await client.close_chain_stream_channel()
62+
63+
64+
if __name__ == "__main__":
65+
asyncio.get_event_loop().run_until_complete(main())

0 commit comments

Comments
0 (0)
You are visitor 01402792 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.