Python SDK for interacting with the Osmosis AMM.
The osmosispy
and osmosis_proto
package allows you to interact with the Osmosis AMM using Python.
python -m pip install --upgrade pip
pip install osmosispy # requires Python 3.11.2+
example.py
#!/usr/bin/env python3
import osmosispy
mnemonic_key = "fat patch excite gold bubble large tunnel vote fine title hover junior advice cable ordinary column mass aunt trigger lucky hope animal abandon mansion"
# authorize in the mainnet
network = osmosispy.Network.mainnet()
trader = osmosispy.Sdk.authorize(key=mnemonic_key).with_network(network)
# print the address
print(trader.address)
$ python3 example.py
osmo1jggt8pcj2d8m9n62luytf8sdncj5uxfs3su2my
For more examples see the examples directory.
The publish workflow looks like this:
-
Code-gen the new types from the chain. If there are changes, these should be committed.
poetry run make proto-gen
-
Increment the package version. For example, use
poetry version preminor
to do a pre-release for a minor version.poetry version [update-keyword]
-
Create a tag and push it the remote origin.
git tag -asm "v1.2.3" v1.2.3 git push --tags
-
The tag will trigger a GitHub Action Workflow.
-
protobuf-compiler installed on the computer
For example in MacOs:
brew install protobuf
-
mypy-protobuf library installed on python
pip install mypy-protobuf
-
Some background:
- The Osmosis SDK uses protobuf files to communicate with the osmosis chain, similar to the Cosmos SDK.
- The protobuf files of the most important SDKs on Cosmos are located in the
repos_protobufs
folder. - The Python files converted from
repos_protobufs
are located in theosmosis_proto
folder.
-
In the
scripts/generate_python_from_protobuf.py
file, you can convert the proto files to Python files. -
In the variable
PROTO_FILES
, there are two examples:concentratedliquidity/v1beta1/query.proto
concentratedliquidity/v1beta1/tick_info.proto
-
Choose the paths of the proto files you want to convert. You can add more proto files to
PROTO_FILES
to be converted into Python files. -
Run the script with the command
python generate_python_from_protobuf.py
in the directory of the script:python generate_python_from_protobuf.py
-
If the script runs successfully, the Python files will be generated in the
tmp
folder. Copy the files to their destination in theosmosis_proto
folder. Try to maintain the same folder structure as therepos_protobufs
folder, like how the protobuf files are organized. Read the section "Alert" below for more information. -
If the script retrieves a "File not found" error, there is probably a missing dependency. This means some protobuf files of some SDK are not present in the
repos_protobufs
folder.- The error will provide the name of the missing dependency.
- Copy the dependency name, search for it on GitHub, and add the new proto files to the
repos_protobufs
folder. - Add the new path of the proto SDK repositories to the global variables, as in the example:
GOGO_PROTO_DIR = os.path.join(ROOT_PROTO_DIR, "gogoproto")
-
The version of the Protobuf compiler used in the files that are converted to .py
contains an issue. The Protobuf compiler imports a non-existent library:
from google.protobuf import runtime_version as _runtime_version
Additionally, the following code (an example) is included:
_runtime_version.ValidateProtobufRuntimeVersion(
_runtime_version.Domain.PUBLIC,
5,
27,
1,
'',
'osmosis/concentratedliquidity/v1beta1/query.proto'
)
This code needs to be manually removed from each generated .py
file. While this is somewhat tedious, after making these changes, the compilation of the repository will work correctly.