Python SDK for interacting with Nibiru.
Project description
Nibiru Python SDK - nibiru
Python-based client for interacting with the Nibiru blockchain.
The nibiru package 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 Contents
- User Guidelines
- Development Guidelines
User Guidelines
Documentation Website
Documentation can be found here: Nibiru-py documentation
- Learn more about opening and managing your spot and perp positions here
- Learn about querying the chain using the Sdk here
Installation from PyPI
pip install nibiru # requires Python 3.9
You may need to update pip to get this to run:
python -m pip install --upgrade pip
Development Guidelines
Our recommended setup is pyenv in combination with poetry.
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 is 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 with pyenv and poetry
Pyenv for managing multiple Python interpreters
If you're on MacOS or a common Linux distro, you can install pyenv with brew.
brew install pyenv
You'll then need to add the following snippet to your shell config, e.g. your .bash_profile, .bashrc, or .zshrc.
export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv init --path)"
After using source on your config or restarting the shell, you should have the pyenv root command.
The command use to install any version of python is pyenv install. Display additional info for this command with pyenv install --help.
pyenv install 3.9.13 # example for nibiru
Once you have a version installed, you can print out the versions on your machine with:
pyenv versions
# example output
system
* 3.9.13 (set by /home/realu/.python-version)
3.10.4
In this example, I have 2 different interpreters installed on my machine. The one with the * is currently set as my global interpreter. This is set manually using the pyenv global command.
pyenv global 3.10.4 # switches the global interpreter to 3.10.4
You can verify this works as expected using python --version. You may be familiar with using python3 as the command instead of python. With pyenv, this is not necessary.
Additional usage and installation instructions can be found in the pyenv repo.
Installing poetry for dependency resolution and publishing packages
Reference: Poetry docs
Poetry can be installed with both curl and pip. We recommended using curl so that it will be global to your machine.
NOTE We highly, highly, highly recommend that you DO NOT use brew to install poetry.
If you use brew, it's going to install directly to your system, which prevents you from being able to leverage pyenv to seamlessly switch between Python interpreters.
# installation with pip: recommended option in tandem with pyenv
pip install poetry
# For UNIX systems - installation with curl
curl -sSL https://install.python-poetry.org/ | python -
After this installation command, add the poetry binary to the path in your shell config (if it's not done automatically).
export PATH=$PATH:$HOME/.poetry/bin
Installing external dependencies
The nibiru project is defined by its pyproject.toml. At the root of the repo, simply call:
poetry install
This will resolve dependencies between each of the project's packages and install them into a virtual environment.
Running tests
Setting environment variables
There's currently a "devnet" running in GCP that the CI workflows use. You can find these secrets at this notion page if you have access to it or contact one of the CODEOWNERS (@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 .env file to set environment variables for the tests.
The variables used in the CI build can be found in the env section of the pytests.yml workflow:
jobs:
tests:
env:
# https://www.notion.so/nibiru/Resources-and-Repo-Configs-b31aa8074a2b419d80b0c946ed5efab0
CHAIN_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 .env configuration like this.
# Example configuration for the Nibiry Python SDK
HOST="..."
VALIDATOR_MNEMONIC="..."
ORACLE_MNEMONIC="..."
GRPC_PORT="..."
LCD_PORT="..."
CHAIN_ID="..."
NETWORK_INSECURE=true
Running the tests with poetry + pytest
After following the instructions for setting up poetry, you can run the tests with poetry run pytest:
poetry run pytest -p no:warnings # silences warnings
(option B). Install the nibiru package with pip
# from local
# build and install
pip install .
# from local build
pip uninstall nibiru
pip install nibiru --no-index --find-links /path/to/nibiru/py-sdk/dist
# from pypi
pip uninstall nibiru
pip install nibiru
Makefile and Protocol Buffers
Other dependencies
To run shell scripts and commands in the Makefile, you'll need to install the following tools depending on your operating system.
-
Ubuntu
sudo apt install python3.X-dev autoconf automake build-essential libffi-dev libtool pkg-config
-
macOS
brew install autoconf automake libtool
-
Fedora
sudo dnf install python3-devel autoconf automake gcc gcc-c++ libffi-devel libtool make pkgconfig
Generating types wth protobuf
The objective is to run make proto-gen, which simply executes scripts/protocgen.sh.
In order to do this, you'll need to install a few packages on your system.
python -m pip install --user grpcio-tools
pip install mypy-protobuf
If you get a permissions error such as
rm: cannot remove 'proto/proto/epochs/query.proto': Permission denied
call sudo chown -R [USER-NAME] proto using the name of user directory.
You can find the value for [USER-NAME] quickly by running whoami. In other words, this should work:
sudo chown -R $(whoami) proto
You're done generating types once you've successfully called
make proto-gen
poetry build # equivalently, you can run `python -m build`
Linting
Enable git hook which will perform linting before each commit:
poetry run pre-commit install
This will keep your code clean.
Gotchas
The protobuf package must be version 3.20.x or lower. Otherwise, the following error appears at runtime.
nibiru/clients/__init__.py:1: in <module>
from nibiru.clients.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
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file nibiru-0.1.0.tar.gz.
File metadata
- Download URL: nibiru-0.1.0.tar.gz
- Upload date:
- Size: 31.6 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.15 CPython/3.9.13 Linux/5.15.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
e23a543b668fa99b1953517e6c22cd827d4de5188488e371867ce740c91c2cff
|
|
| MD5 |
4f2f57937fe117d9d018a094c78ccaba
|
|
| BLAKE2b-256 |
b8ed9b0d4c90ea0161fdc256bed451b020e68bac6cb4834f29fac5391b48b3e6
|
File details
Details for the file nibiru-0.1.0-py3-none-any.whl.
File metadata
- Download URL: nibiru-0.1.0-py3-none-any.whl
- Upload date:
- Size: 32.0 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/1.1.15 CPython/3.9.13 Linux/5.15.0-1017-azure
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
be386d79e587829003cf417fefd2c347457ce9f40deac549854e9eb65558a546
|
|
| MD5 |
a8d57766280c4465c275b8700a3c538f
|
|
| BLAKE2b-256 |
18302ae71441a6fed6457370e5baa48513cd65b9e50a7c8ac33f3293333513cc
|