Skip to main content

The Python SDK for Terra

Project description



py-sdk-logo

The Python SDK for Terra

(Unfamiliar with Terra? Check out Terra Docs)

GitHub Python pip

Explore the Docs »
PyPI Package · GitHub Repository

The Terra Software Development Kit (SDK) in Python is a simple library toolkit for building software that can interact with the Terra blockchain and provides simple abstractions over core data structures, serialization, key management, and API request generation.

Features

  • Written in Python offering extensive support libraries
  • Versatile support for key management solutions
  • Exposes the Terra API through LCDClient

Table of Contents


API Reference

An intricate reference to the APIs on the Terra SDK can be found here.


Getting Started

A walk-through of the steps to get started with the Terra SDK alongside with a few use case examples are provided below. Alternatively, a tutorial video is also available here as reference.

Requirements

Terra SDK requires Python v3.7+.

Installation

NOTE: All code starting with a $ is meant to run on your terminal (a bash prompt). All code starting with a >>> is meant to run in a python interpreter, like ipython.

Terra SDK can be installed (preferably in a virtual environment from PyPI using pip) as follows:

$ pip install -U terra_sdk

You might have pip3 installed instead of pip; proceed according to your own setup.

Dependencies

Terra SDK uses Poetry to manage dependencies. To get set up with all the required dependencies, run:

$ pip install poetry
$ poetry install

Tests

Terra SDK provides extensive tests for data classes and functions. To run them, after the steps in Dependencies:

$ make test

Code Quality

Terra SDK uses Black, isort, and Mypy for checking code quality and maintaining style. To reformat, after the steps in Dependencies:

$ make qa && make format

Usage Examples

Terra SDK can help you read block data, sign and send transactions, deploy and interact with contracts, and many more. Following examples are provided to help get building started; use cases and functionalities of the Terra SDK are not limited to the following examples and can be found in full here.

In order to interact with the Terra blockchain, you'll need a connection to a Terra node. This can be done through setting up an LCDClient (The LCDClient is an object representing an HTTP connection to a Terra LCD node.):

>>> from terra_sdk.client.lcd import LCDClient
>>> terra = LCDClient(chain_id="columbus-5", url="https://lcd.terra.dev")

Getting Blockchain Information

Once properly configured, the LCDClient instance will allow you to interact with the Terra blockchain. Try getting the latest block height:

>>> terra.tendermint.block_info()['block']['header']['height']

'1687543'

Async Usage

If you want to make asynchronous, non-blocking LCD requests, you can use AsyncLCDClient. The interface is similar to LCDClient, except the module and wallet API functions must be awaited.


>>> import asyncio 
>>> from terra_sdk.client.lcd import AsyncLCDClient

>>> async def main():
      terra = AsyncLCDClient("https://lcd.terra.dev", "columbus-5")
      total_supply = await terra.supply.total()
      print(total_supply)
      await terra.session.close # you must close the session

>>> asyncio.get_event_loop().run_until_complete(main())

Building and Signing Transactions

If you wish to perform a state-changing operation on the Terra blockchain such as sending tokens, swapping assets, withdrawing rewards, or even invoking functions on smart contracts, you must create a transaction and broadcast it to the network. Terra SDK provides functions that help create StdTx objects.

Example Using a Wallet (recommended)

A Wallet allows you to create and sign a transaction in a single step by automatically fetching the latest information from the blockchain (chain ID, account number, sequence).

Use LCDClient.wallet() to create a Wallet from any Key instance. The Key provided should correspond to the account you intend to sign the transaction with.

>>> from terra_sdk.client.lcd import LCDClient
>>> from terra_sdk.key.mnemonic import MnemonicKey

>>> mk = MnemonicKey(mnemonic=MNEMONIC) 
>>> terra = LCDClient("https://lcd.terra.dev", "columbus-5")
>>> wallet = terra.wallet(mk)

Once you have your Wallet, you can simply create a StdTx using Wallet.create_and_sign_tx.

>>> from terra_sdk.core.auth import StdFee
>>> from terra_sdk.core.bank import MsgSend

>>> tx = wallet.create_and_sign_tx(
        msgs=[MsgSend(
            wallet.key.acc_address,
            RECIPIENT,
            "1000000uluna"    # send 1 luna
        )],
        memo="test transaction!",
        fee=StdFee(200000, "120000uluna")
    )

You should now be able to broadcast your transaction to the network.

>>> result = terra.tx.broadcast(tx)
>>> print(result)

Contributing

Community contribution, whether it's a new feature, correction, bug report, additional documentation, or any other feedback is always welcome. Please read through this section to ensure that your contribution is in the most suitable format for us to effectively process.


Reporting an Issue

First things first: Do NOT report security vulnerabilities in public issues! Please disclose responsibly by letting the Terra SDK team know upfront. We will assess the issue as soon as possible on a best-effort basis and will give you an estimate for when we have a fix and release available for an eventual public disclosure.
If you encounter a different issue with the Python SDK, check first to see if there is an existing issue on the Issues page or a pull request on the Pull requests page (both Open and Closed tabs) addressing the topic.

If there isn't a discussion on the topic there, you can file an issue. The ideal report includes:

  • A description of the problem / suggestion.
  • How to recreate the bug.
  • If relevant, including the versions of your:
    • Python interpreter
    • Terra SDK
    • Optionally of the other dependencies involved
  • If possible, create a pull request with a (failing) test case demonstrating what's wrong. This makes the process for fixing bugs quicker & gets issues resolved sooner.

Requesting a Feature

If you wish to request the addition of a feature, please first checkout the Issues page and the Pull requests page (both Open and Closed tabs). If you decide to continue with the request, think of the merits of the feature to convince the project's developers, and provide as much detail and context as possible in the form of filing an issue on the Issues page.


Contributing Code

If you wish to contribute to the repository in the form of patches, improvements, new features, etc., first scale the contribution. If it is a major development, like implementing a feature, it is recommended that you consult with the developers of the project before starting the development in order not to risk spending a lot of time working on a change that might not get merged into the project. Once confirmed, you are welcome to submit your pull request.

For new contributors, here is a quick guide:

  1. Fork the repository.
  2. Build the project using the Dependencies and Tests steps.
  3. Install a virtualenv.
  4. Develop your code and test the changes using the Tests and Code Quality steps.
  5. Commit your changes (ideally follow the Angular commit message guidelines).
  6. Push your fork and submit a pull request to the repository's main branch to propose your code.

A good pull request:

  • is clear.
  • works across all supported versions of Python. (3.7+)
  • Follows the existing style of the code base (Flake8).
  • Has comments included as needed.
  • A test case that demonstrates the previous flaw that now passes with the included patch, or demonstrates the newly added feature.
  • If it adds / changes a public API, it must also include documentation for those changes.
  • Must be appropriately licensed (MIT License).

Documentation Contributions

Documentation improvements are always welcome. The documentation files live in the docs directory of the repository and are written in reStructuredText and use Sphinx to create the full suite of documentation.
When contributing documentation, please do your best to follow the style of the documentation files. This means a soft-limit of 88 characters wide in your text files and a semi-formal, yet friendly and approachable, prose style. You can propose your imporvements by submiting a pull request as explained above.

Need more information on how to contribute?

You can give this guide read for more insight.


License

This software is licensed under the MIT license. See LICENSE for full disclosure.

© 2021 Terraform Labs, PTE.


 

Terra-logo

Powering the innovation of money.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

terra-sdk-1.0.1.tar.gz (243.7 kB view details)

Uploaded Source

Built Distribution

terra_sdk-1.0.1-py3-none-any.whl (387.7 kB view details)

Uploaded Python 3

File details

Details for the file terra-sdk-1.0.1.tar.gz.

File metadata

  • Download URL: terra-sdk-1.0.1.tar.gz
  • Upload date:
  • Size: 243.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.10 CPython/3.9.7 Linux/5.4.0-1062-azure

File hashes

Hashes for terra-sdk-1.0.1.tar.gz
Algorithm Hash digest
SHA256 74daeaaacd5243b9b29f41037cfbf131f5c5deac4e8c2d2dd544be710c927939
MD5 074fcdc46c64643c75e3c82ed80f6f05
BLAKE2b-256 aff4dd4e13b58b0b1d663eb75558fbbba18d1272f565f8bf61e54f2262491162

See more details on using hashes here.

File details

Details for the file terra_sdk-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: terra_sdk-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 387.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.10 CPython/3.9.7 Linux/5.4.0-1062-azure

File hashes

Hashes for terra_sdk-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 091be877d8d1c8c0c605b4e4d1e2e500b307d2058ee3d858edea4e5651b0e96f
MD5 f3d9ea7dadb12509b2120cfab95ed9e5
BLAKE2b-256 d96e40353d3ac103f8a136c17a2b07ae36acba619bd6e9e26f014d1f59780d14

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page