• Stars
    star
    136
  • Rank 260,545 (Top 6 %)
  • Language
    Python
  • License
    MIT License
  • Created over 3 years ago
  • Updated 10 months ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Python SDK for Terra


py-sdk-logo

The Python SDK for Terra

(Unfamiliar with Terra? Check out the 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 with 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 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.

❗ If you want to communicate with Terra Classic, use terra-sdk==2.x

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. The following examples are provided to help you get 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="phoenix-1", url="https://phoenix-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://phoenix-lcd.terra.dev", "phoenix-1")
      total_supply = await terra.bank.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.

NOTE: If you are using MacOS and got an exception 'bad key length' from MnemonicKey, please check your python implementation. if python3 -c "import ssl; print(ssl.OPENSSL_VERSION)" returns LibreSSL 2.8.3, you need to reinstall python via pyenv or homebrew.

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

>>> mk = MnemonicKey(mnemonic=MNEMONIC)
>>> terra = LCDClient("https://phoenix-lcd.terra.dev", "phoenix-1")
>>> 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.fee import Fee
>>> from terra_sdk.core.bank import MsgSend
>>> from terra_sdk.client.lcd.api.tx import CreateTxOptions

>>> tx = wallet.create_and_sign_tx(CreateTxOptions(
        msgs=[MsgSend(
            wallet.key.acc_address,
            RECIPIENT,
            "1000000uluna"    # send 1 luna
        )],
        memo="test transaction!",
        fee=Fee(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 submitting your findings to the Terra Bugcrowd submission form. The issue will be assessed as soon as possible. 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 if there is a pull request on the Pull requests page. Be sure to check both the Open and Closed tabs addressing the issue.

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 check out 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 to avoid duplicating efforts. 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 and concise.
  • Works across all supported versions of Python. (3.7+)
  • Follows the existing style of the code base (Flake8).
  • Has comments included as needed.
  • Includes a test case that demonstrates the previous flaw that now passes with the included patch, or demonstrates the newly added feature.
  • Must include documentation for changing or adding any public APIs.
  • 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 improvements by submitting 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.

More Repositories

1

classic-core

GO implementation of the Terra Protocol
JavaScript
986
star
2

core

GO implementation of the Terra Protocol
JavaScript
357
star
3

terra.js

JavaScript SDK for Terra, written in TypeScript
TypeScript
264
star
4

LocalTerra

One-click local Terra testnet and ecosystem for rapid prototyping
Dockerfile
177
star
5

docs

πŸ—‚ The official documentation for the Terra blockchain
SCSS
112
star
6

station

πŸ›°οΈ Station wallet
TypeScript
94
star
7

wallet-provider

Library to make React dApps easier using Terra Station Extension or Terra Station Mobile.
TypeScript
89
star
8

shuttle

TypeScript
85
star
9

bridge-web-app

πŸͺ Terra Bridge web app
TypeScript
82
star
10

validator-profiles

Profiles for Validators of the Terra Blockchain
Jinja
81
star
11

station-legacy

Web Application to interact with Terra Core
TypeScript
67
star
12

mantlemint

Go
65
star
13

fcd-classic

Terra ETL + RestFul API Server
HTML
62
star
14

assets

JavaScript
57
star
15

cosmwasm-contracts

Rust
51
star
16

finder

The Terra Finder is a tool to search through blocks, transactions, and accounts on the Terra blockchain.
TypeScript
50
star
17

oracle-feeder

Oracle Feeder Daemon
TypeScript
49
star
18

terra-cosmwasm

Terra bindings for CosmWasm
Rust
49
star
19

alliance

🀝 Alliance chain with x/alliance module
Go
49
star
20

terrain

πŸ›  A Terra development environment for seamless smart contract development
TypeScript
46
star
21

research

Models and simulations
Python
44
star
22

classic-testnet

Configuration files for Terra testnet releases
Python
44
star
23

station-mobile

πŸ›°οΈ Station wallet mobile
TypeScript
34
star
24

faucet

Faucet to get free coins for the Terra testnet
Go
34
star
25

station-extension

πŸ›°οΈ Station Chrome and Firefox extension
TypeScript
33
star
26

station-desktop

JavaScript
27
star
27

houston

A development environment, testing framework and smart contract pipeline for Terra, aiming to make life as a Terra developer easier.
TypeScript
27
star
28

terraswap-graph

Terraswap Indexer + GraphQL
TypeScript
25
star
29

mantle-sdk

All type definition of Terra, database scheme, and extract scripts
Go
25
star
30

my-terra-token

Simple CosmWasm smart contract on Terra
Rust
21
star
31

documentation

White paper & protocol specifications
TeX
20
star
32

terra.go

Go library for terra blockchain
Go
19
star
33

hive-graph

GraphQL interface for Terra blockchain
TypeScript
17
star
34

mantle

Go
16
star
35

warp-contracts

Rust
15
star
36

jigu

The Python SDK for Terra.
Python
14
star
37

genesis-tools

Genesis Build Script for Terra 2.0
Python
10
star
38

enterprise-contracts

Rust
10
star
39

tefi-oracle-contracts

Oracle SmartContracts for TeFi projects
Rust
10
star
40

smart-contract-webapp-template

This is a basic template for developing smart contract and web app together. This template is based on cw-template of CosmWasm.
TypeScript
10
star
41

react-native-mnemonic-key

Performant replacement for MnemonicKey for React Native
Swift
9
star
42

use-station

TypeScript
8
star
43

warp-sdk

TypeScript
8
star
44

station-assets

πŸ›°οΈ Assets for Station
JavaScript
8
star
45

feather.js

πŸ’» JavaScript SDK for Station, written in TypeScript
TypeScript
8
star
46

terrain-frontend-template

Frontend template for developing frontends on Terra
TypeScript
7
star
47

terra.proto

proto generator env for multiple languages
TypeScript
7
star
48

enterprise-app

TypeScript
7
star
49

feather-core

Templates for feather chain scaffolding
Go
7
star
50

bridge-sdk

Javascript Terra Bridge SDK
TypeScript
7
star
51

terra.kt

Kotlin, Java SDK for Terra
Kotlin
6
star
52

warp-web-app

TypeScript
6
star
53

terra-wallet-java

Native Terra wallet library for Android
Java
6
star
54

testnet

Testnet Infos of Terra 2.0
6
star
55

wallet-kit

new version of wallet-provider
TypeScript
6
star
56

api-public

API for some Station features
TypeScript
5
star
57

fcd

HTML
5
star
58

libp2p-episub

Episub: Proximity Aware Epidemic PubSub for libp2p
Rust
4
star
59

terrain-desktop

TypeScript
4
star
60

core-genesis-exporter

temp
JavaScript
4
star
61

delegations

[WIP] Hub for managing Terraform Labs delegations
4
star
62

deposit-tracker

Track deposit transactions of an address
TypeScript
4
star
63

amino-js

TypeScript
3
star
64

mainnet

3
star
65

warp-docs

The official Warp documentation.
JavaScript
3
star
66

classic-docs

CSS
3
star
67

TIPs

Terra Improvement Proposals (TIPs) main repository
3
star
68

oracle-feeder-go

Oracle Feeder Daemon
Go
3
star
69

terrain-core-template

Core template for @terra-money/terrain to instantiate projects
TypeScript
3
star
70

level-to-rocks

LevelDB to RocksDB converter
Go
3
star
71

enterprise-sdk

TypeScript
2
star
72

cra-react18-wallet-provider-template

TypeScript
2
star
73

react-base-components

TypeScript
2
star
74

bounties

2
star
75

mantle-compatibility

bells and whistles required for mantle to operate with different versions of cosmos/tendermint
Go
2
star
76

alliance-estimator

TypeScript
2
star
77

alliance-docs

SCSS
2
star
78

msg-reader

JavaScript
2
star
79

santa

Santa subsidizes block rewards on Columbus!
Go
2
star
80

ledger-station-js

TypeScript
2
star
81

terra-web-extensions

Next Browser extensions (In development)
TypeScript
2
star
82

validator-images

JavaScript
2
star
83

key-utils

TypeScript
2
star
84

nostr-bindings

Smart contract to bind terra <-> nostr keys
Rust
2
star
85

new-docs

New docs site
JavaScript
2
star
86

synthetic-assets

TypeScript
2
star
87

terra-tutorials

A collection of example code used for tutorials
2
star
88

templates

Terra app template registry
1
star
89

alliance-nft-collection

Code to create a collection to reward participants for the Game Of Alliance
TypeScript
1
star
90

log-finder-ruleset

TypeScript
1
star
91

templates.terra.money

TypeScript
1
star
92

terrariums

TypeScript
1
star
93

screening-test-backend

1
star
94

gitcoin-onboarding

1
star
95

cosmwasm-cache-rebuilder

Simple runtime dedicated for rebuilding cosmwasm cache
Rust
1
star
96

ics-proxy

Rust
1
star
97

airdrop

This app is used to claim the airdrops from other chains into Terra with support for Terra Station, Metamask, Wallet Connect and Phantom Wallet.
Rust
1
star
98

terra-wallet-ios

Native Terra wallet library for iOS
Swift
1
star
99

alliance-landing

Landing page for the Alliance an open-source Cosmos SDK module.
TypeScript
1
star
100

amino-decoder

rest server for amino decoding
Go
1
star