-
Notifications
You must be signed in to change notification settings - Fork 35
feat: example to stream oracle prices from chain #422
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
hmoragrega
wants to merge
2
commits into
master
Choose a base branch
from
f/stream-oracle-prices
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| import asyncio | ||
| from contextlib import suppress | ||
| from typing import Any, Dict | ||
|
|
||
| from grpc import RpcError | ||
|
|
||
| from pyinjective.async_client_v2 import AsyncClient | ||
| from pyinjective.core.network import Network | ||
|
|
||
| ORACLE_TYPE = "chainlinkdatastreams" | ||
| ORACLE_SYMBOLS = { | ||
| "INJ": "0x000344d7a7d81f051ee273a63f94f8bef7d44ca89aa03e0c5bf4d085df19adb6", | ||
| "USDC": "0x00038f83323b6b08116d1614cf33a9bd71ab5e0abf0c9f1b783a74a43e7bd992", | ||
| } | ||
|
|
||
|
|
||
| async def oracle_price_event_processor(event: Dict[str, Any]): | ||
| for oracle_price in event.get("oraclePrices", []): | ||
| print( | ||
| { | ||
| "blockHeight": event["blockHeight"], | ||
| "blockTime": event["blockTime"], | ||
| **oracle_price, | ||
| } | ||
| ) | ||
|
|
||
|
|
||
| def stream_error_processor(exception: RpcError): | ||
| print(f"There was an error listening to oracle price updates ({exception})") | ||
|
|
||
|
|
||
| def stream_closed_processor(): | ||
| print("The oracle price updates stream has been closed") | ||
|
|
||
|
|
||
| async def main() -> None: | ||
| network = Network.mainnet() | ||
|
|
||
| client = AsyncClient(network) | ||
| composer = await client.composer() | ||
|
|
||
| print(f"Streaming {ORACLE_TYPE} prices for {', '.join(ORACLE_SYMBOLS)}") | ||
| # To receive all oracle price updates, use composer.chain_stream_oracle_price_filter() without symbols. | ||
| oracle_price_filter = composer.chain_stream_oracle_price_filter(symbols=list(ORACLE_SYMBOLS.values())) | ||
|
|
||
| task = asyncio.get_event_loop().create_task( | ||
| client.listen_chain_stream_updates( | ||
| callback=oracle_price_event_processor, | ||
| on_end_callback=stream_closed_processor, | ||
| on_status_callback=stream_error_processor, | ||
| oracle_price_filter=oracle_price_filter, | ||
| ) | ||
| ) | ||
|
|
||
| try: | ||
| await asyncio.sleep(delay=60) | ||
| finally: | ||
| task.cancel() | ||
| with suppress(asyncio.CancelledError): | ||
| await task | ||
| await client.close_chain_stream_channel() | ||
|
|
||
|
|
||
| if __name__ == "__main__": | ||
| asyncio.get_event_loop().run_until_complete(main()) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🩺 Stability & Availability | 🟡 Minor | ⚡ Quick win
🔎 Supported by static analysis
🏁 Script executed:
Repository: InjectiveLabs/sdk-python
Length of output: 50381
🏁 Script executed:
Repository: InjectiveLabs/sdk-python
Length of output: 5283
🏁 Script executed:
Repository: InjectiveLabs/sdk-python
Length of output: 253
🏁 Script executed:
Repository: InjectiveLabs/sdk-python
Length of output: 5655
Close the stream channel when the listener task fails.
If
GrpcApiStreamAssistant.listen_streamraises a non-asyncio.CancelledErrorexception throughclient.listen_chain_stream_updates,await taskpropagates it because the suppression covers onlyasyncio.CancelledError. Execution then skipsawait client.close_chain_stream_channel(), leaving the stream channel open. Move channel closure into an innerfinally.🤖 Prompt for AI Agents
Source: MCP tools