Package: nibiru-py
Nibiru Python SDK -nibiruPython-based client for interacting with the Nibiru blockchain.Thenibiru-pypackage allows you to index, query, and send transactions on the Nibiru Blockchain using Python. It provides access to market data for analysis, visualization, indicator development, algorithmic trading, strategy backtesting, bot programming, and related software engineering.The package is intended to be used by coders, developers, technically-skilled traders and  data-scientists for building trading algorithms.README ContentsUser GuidelinesDocumentation WebsiteInstallation fromPyPIDevelopment GuidelinesSetting up a professional dev environment withpyenvandpoetryPyenv for managing multiple Python interpretersInstallingpoetryfor dependency resolution and publishing packagesInstalling external dependenciesRunning testsMakefile and Protocol BuffersOther dependenciesGenerating types wth protobufLintingGotchasUser GuidelinesDocumentation WebsiteDocumentation can be found here:Nibiru-py documentationLearn more about opening and managing your spot and perp positionshereLearn about querying the chain using the SdkhereInstallation fromPyPIpipinstallnibiru-py# requires Python 3.9You may need to updatepipto get this to run:python-mpipinstall--upgradepipDevelopment GuidelinesOur recommended setup ispyenvin combination withpoetry.pyenvis a tool for installing and managing Python interpreters. This will let you seamlessly switch between Python versions.poetryis used for managing virtual environments, dependency resolution, package installations, package building, and package publishing.We assume you're on a Unix machine such as WSL2 Ubuntu, MacOS, or a common Linux distro.Currently,nibiru-pyis created with Python 3.9.13. It may be compatible with higher versions, but we only run end-to-end tests in 3.9.13.Setting up a professional dev environment withpyenvandpoetryPyenv for managing multiple Python interpretersIf you're on MacOS or a common Linux distro, you can installpyenvwith brew.brewinstallpyenvYou'll then need to add the following snippet to your shell config, e.g. your.bash_profile,.bashrc, or.zshrc.exportPYENV_ROOT="$HOME/.pyenv"exportPATH="$PYENV_ROOT/bin:$PATH"eval"$(pyenvinit-)"eval"$(pyenvinit--path)"After usingsourceon your config or restarting the shell, you should have thepyenvroot command.The command use to install any version of python ispyenv install. Display additional info for this command withpyenv install --help.pyenvinstall3.9.13# example for nibiru-pyOnce you have a version installed, you can print out the versions on your machine with:pyenvversions# example output
  system
* 3.9.13 (set by /home/realu/.python-version)
  3.10.4In this example, I have 2 different interpreters installed on my machine. The one with the*is currently set as myglobal interpreter. This is set manually using thepyenv globalcommand.pyenvglobal3.10.4# switches the global interpreter to 3.10.4You can verify this works as expected usingpython --version. You may be familiar with usingpython3as the command instead ofpython. Withpyenv, this is not necessary.Additional usage and installation instructions can be found in thepyenv repo.Installingpoetryfor dependency resolution and publishing packagesReference:Poetry docsPoetry can be installed with bothcurlandpip. We recommended usingcurlso that it will be global to your machine.NOTE We highly, highly, highly recommend that you DO NOT usebrewto installpoetry.
If you usebrew, it's going to install directly to your system, which prevents you from being able to leveragepyenvto seamlessly switch between Python interpreters.# installation with pip: recommended option in tandem with pyenvpipinstallpoetry# For UNIX systems - installation with curlcurl-sSLhttps://install.python-poetry.org/|python-After this installation command, add thepoetrybinary to the path in your shell config (if it's not done automatically).exportPATH=$PATH:$HOME/.poetry/binInstalling external dependenciesThenibiru-pyproject is defined by itspyproject.toml. At the root of the repo, simply call:poetryinstallThis will resolve dependencies between each of the project's packages and install them into a virtual environment.Running testsSetting environment variablesThere's currently a "devnet" running in GCP that the CI workflows use. You can find these secrets atthis notion pageif you have access to it or contact one of theCODEOWNERS(@Unique-Divine, @matthiasmatt, @nibiruheisenberg).
This is useful so that you can run every part of the package code without needing to visit other repositories.Set up a.envfile to set environment variables for the tests.
The variables used in the CI build can be found in theenvsection of thepytests.ymlworkflow:jobs:tests:env:# https://www.notion.so/nibiru/Resources-and-Repo-Configs-b31aa8074a2b419d80b0c946ed5efab0CHAIN_ID:${{ secrets.CHAIN_ID }}HOST:${{ secrets.HOST }}VALIDATOR_MNEMONIC:${{ secrets.VALIDATOR_MNEMONIC }}GRPC_PORT:${{ secrets.GRPC_PORT }}LCD_PORT:${{ secrets.LCD_PORT }}You'll need an.envconfiguration like this.# Example configuration for the Nibiry Python SDKHOST="..."VALIDATOR_MNEMONIC="..."GRPC_PORT="..."LCD_PORT="..."CHAIN_ID="..."Running the tests withpoetry+pytestAfter following the instructions for setting uppoetry, you can run the tests withpoetry run pytest:poetryrunpytest-pno:warnings# silences warnings(option B). Install thenibiru-pypackage withpip# from local# build and installpipinstall.# from local buildpipuninstallnibiru-py
pipinstallnibiru-py--no-index--find-links/path/to/nibiru/nibiru-py/dist# from pypipipuninstallnibiru-py
pipinstallnibiru-pyMakefile and Protocol BuffersOther dependenciesTo run shell scripts and commands in theMakefile, you'll need to install the following tools depending on your operating system.Ubuntusudoaptinstallpython3.X-devautoconfautomakebuild-essentiallibffi-devlibtoolpkg-configmacOSbrewinstallautoconfautomakelibtoolFedorasudodnfinstallpython3-develautoconfautomakegccgcc-c++libffi-devellibtoolmakepkgconfigGenerating types wth protobufThe objective is to runmake proto-gen, which simply executesscripts/protocgen.sh.In order to do this, you'll need to install a few packages on your system.python-mpipinstall--usergrpcio-tools
pipinstallmypy-protobufIf you get a permissions error such asrm: cannot remove 'proto/proto/epochs/query.proto': Permission deniedcallsudo chown -R [USER-NAME] protousing the name of user directory.
You can find the value for[USER-NAME]quickly by runningwhoami. In other words, this should work:sudochown-R$(whoami)protoYou're done generating types once you've successfully calledmakeproto-gen
poetrybuild# equivalently, you can run `python -m build`LintingEnable git hook which will perform linting before each commit:pre-commitinstallThis will keep your code clean.GotchasTheprotobufpackage must be version 3.20.x or lower. Otherwise, the following error appears at runtime.nibiru/clients/__init__.py:1: in <module>
    from .dex import Dex  # noqa
nibiru/clients/dex.py:8: in <module>
    from nibiru.proto.dex.v1 import query_pb2 as dex_type
nibiru/proto/dex/v1/query_pb2.py:16: in <module>
    from google.api import annotations_pb2 as google_dot_api_dot_annotations__pb2
../../../anaconda3/envs/divine/lib/python3.9/site-packages/google/api/annotations_pb2.py:30: in <module>
    from google.api import http_pb2 as google_dot_api_dot_http__pb2
../../../anaconda3/envs/divine/lib/python3.9/site-packages/google/api/http_pb2.py:48: in <module>
    _descriptor.FieldDescriptor(
../../../anaconda3/envs/divine/lib/python3.9/site-packages/google/protobuf/descriptor.py:560: in __new__
    _message.Message._CheckCalledFromGeneratedFile()
E   TypeError: Descriptors cannot not be created directly.
E   If this call came from a _pb2.py file, your generated code is out of date and must be regenerated with protoc >= 3.19.0.
E   If you cannot immediately regenerate your protos, some other possible workarounds are:
E    1. Downgrade the protobuf package to 3.20.x or lower.
E    2. Set PROTOCOL_BUFFERS_PYTHON_IMPLEMENTATION=python (but this will use pure-Python parsing and will be much slower).
E
E   More information: https://developers.google.com/protocol-buffers/docs/news/2022-05-06#python-updates