Skip to content

Instantly share code, notes, and snippets.

@SEJeff
Created January 22, 2022 07:24
Show Gist options
  • Save SEJeff/cb151ed3aef3f6ef16bd3892b0d68d00 to your computer and use it in GitHub Desktop.
Save SEJeff/cb151ed3aef3f6ef16bd3892b0d68d00 to your computer and use it in GitHub Desktop.
Example for pyth-client-py
#!/usr/bin/env python3
from __future__ import annotations
import asyncio
import os
import signal
import sys
from typing import List, Any
from loguru import logger
from pythclient.pythclient import PythClient # noqa
from pythclient.ratelimit import RateLimit # noqa
from pythclient.pythaccounts import PythPriceAccount # noqa
from pythclient.utils import get_key # noqa
logger.enable("pythclient")
RateLimit.configure_default_ratelimit(overall_cps=9, method_cps=3, connection_cps=3)
to_exit = False
def set_to_exit(sig: Any, frame: Any):
global to_exit
to_exit = True
signal.signal(signal.SIGINT, set_to_exit)
async def main():
global to_exit
use_program = len(sys.argv) >= 2 and sys.argv[1] == "program"
v2_first_mapping_account_key = get_key("devnet", "mapping")
v2_program_key = get_key("devnet", "program")
async with PythClient(
first_mapping_account_key=v2_first_mapping_account_key,
program_key=v2_program_key if use_program else None,
) as c:
await c.refresh_all_prices()
products = await c.get_products()
all_prices: List[PythPriceAccount] = []
for p in products:
print(p.key, p.attrs)
prices = await p.get_prices()
for _, pr in prices.items():
all_prices.append(pr)
print(
pr.key,
pr.product_account_key,
pr.price_type,
pr.aggregate_price,
"p/m",
pr.aggregate_price_confidence_interval,
)
if __name__ == '__main__':
asyncio.run(main())
# This is an example of running the above python in python 3.8.12
$ python --version
Python 3.8.12
$ python example-dump.py | head
89GseEmvNkzAMMEXcW9oTYzqRPXTsJ3BmNerXmgA1osV {'symbol': 'Crypto.BCH/USD', 'asset_type': 'Crypto', 'quote_currency': 'USD', 'description': 'BCH/USD', 'generic_symbol': 'BCHUSD', 'base': 'BCH'}
4EQrNZYk5KR1RnjyzbaaRbHsv8VqZWzSUtvx58wLsZbj 89GseEmvNkzAMMEXcW9oTYzqRPXTsJ3BmNerXmgA1osV PythPriceType.PRICE 296.84325 p/m 0.500475
JCnD5WiurZfoeVPEi2AXVgacg73Wd2iRDDjZDbSwdr9D {'symbol': 'Crypto.LTC/USD', 'asset_type': 'Crypto', 'quote_currency': 'USD', 'description': 'LTC/USD', 'generic_symbol': 'LTCUSD', 'base': 'LTC'}
BLArYBCUYhdWiY8PCUTpvFE21iaJq85dvxLk9bYMobcU JCnD5WiurZfoeVPEi2AXVgacg73Wd2iRDDjZDbSwdr9D PythPriceType.PRICE 0.0 p/m 0.0
G89jkM5wFLpmnbvRbeePUumxsJyzoXaRfgBVjyx2CPzQ {'symbol': 'Equity.US.AAPL/USD', 'asset_type': 'Equity', 'quote_currency': 'USD', 'description': 'APPLE INC', 'base': 'AAPL', 'country': 'US', 'cms_symbol': 'AAPL', 'cqs_symbol': 'AAPL', 'nasdaq_symbol': 'AAPL'}
CqFJLrT4rSpA46RQkVYWn8tdBDuQ7p7RXcp6Um76oaph G89jkM5wFLpmnbvRbeePUumxsJyzoXaRfgBVjyx2CPzQ PythPriceType.PRICE 162.49 p/m 0.02
GaBJpKtnyUbyKe34XuyegR7W98a9PT5cg985G974NY8R {'symbol': 'Equity.US.TSLA/USD', 'asset_type': 'Equity', 'quote_currency': 'USD', 'description': 'TESLA INC', 'base': 'TSLA', 'country': 'US', 'cms_symbol': 'TSLA', 'cqs_symbol': 'TSLA', 'nasdaq_symbol': 'TSLA'}
9TaWcpX3kdfdWQQdNtAjW12fNEKdiicmVXuourqn3xJh GaBJpKtnyUbyKe34XuyegR7W98a9PT5cg985G974NY8R PythPriceType.PRICE 917.3414600000001 p/m 39.459270000000004
Fwosgw2ikRvdzgKcQJwMacyczk3nXgoW3AtVtyVvXSAb {'symbol': 'Equity.US.QQQ/USD', 'asset_type': 'Equity', 'quote_currency': 'USD', 'description': 'INVESCO QQQ TRUST SERIES 1', 'base': 'QQQ', 'country': 'US', 'cms_symbol': 'QQQ', 'cqs_symbol': 'QQQ', 'nasdaq_symbol': 'QQQ'}
Hz23MhJM1Fnw2n7SZFBrucvJHFeiLXq36GCWD7UfeNDt Fwosgw2ikRvdzgKcQJwMacyczk3nXgoW3AtVtyVvXSAb PythPriceType.PRICE 2.0 p/m 1.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment