Code Monkey home page Code Monkey logo

pyth-client-py's Introduction

Pyth Client in Python

pytest

A Python library to retrieve data from Pyth account structures off the Solana blockchain.

NOTE: This library requires Python 3.7 or greater due to from __future__ import annotations.

Usage

Install the library:

pip install pythclient

You can then read the current Pyth price using the following:

from pythclient.pythclient import PythClient
from pythclient.pythaccounts import PythPriceAccount
from pythclient.utils import get_key

solana_network="devnet"
async with PythClient(
    first_mapping_account_key=get_key(solana_network, "mapping"),
    program_key=get_key(solana_network, "program") if use_program else None,
) as c:
    await c.refresh_all_prices()
    products = await c.get_products()
    for p in products:
        print(p.attrs)
        prices = await p.get_prices()
        for _, pr in prices.items():
            print(
                pr.price_type,
                pr.aggregate_price,
                "p/m",
                pr.aggregate_price_confidence_interval,
            )

This code snippet lists the products on pyth and the price for each product. Sample output is:

{'symbol': 'Crypto.ETH/USD', 'asset_type': 'Crypto', 'quote_currency': 'USD', 'description': 'ETH/USD', 'generic_symbol': 'ETHUSD', 'base': 'ETH'}
PythPriceType.PRICE 4390.286 p/m 2.4331
{'symbol': 'Crypto.SOL/USD', 'asset_type': 'Crypto', 'quote_currency': 'USD', 'description': 'SOL/USD', 'generic_symbol': 'SOLUSD', 'base': 'SOL'}
PythPriceType.PRICE 192.27550000000002 p/m 0.0485
{'symbol': 'Crypto.SRM/USD', 'asset_type': 'Crypto', 'quote_currency': 'USD', 'description': 'SRM/USD', 'generic_symbol': 'SRMUSD', 'base': 'SRM'}
PythPriceType.PRICE 4.23125 p/m 0.0019500000000000001
...

The library also includes an asynchronous API that updates the prices using a websocket subscription or account polling. See examples/dump.py for a more detailed usage example.

Developer Setup

Using python 3.7 or newer, create, and activate a virtual environment:

python3 -m venv ve
. ve/bin/activate

To install this library in editable mode with test dependencies:

pip install -e '.[testing]'

To run the unit tests:

pytest

If html based test coverage is more your jam:

pytest --cov-report=html

The coverage webpages will be in the htmlcov directory.

Distribution

To build the package for distribution to pypi, first install dependencies:

python3 -m pip install --upgrade twine build

Next, edit setup.py and bump the version field. Then build the package by running:

python3 -m build

This command will produce packages in the dist/ directory. Upload these packages to pypi by running:

python3 -m twine upload --repository pypi dist/*

This command will require you to log in to a pypi account.

pyth-client-py's People

Contributors

cctdaniel avatar erancx avatar jayantk avatar sejeff avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.