Package: terra-classic-sdk
The Python SDK for Terra Classic(Unfamiliar with Terra?Check out the Terra Docs)Explore the Docs »PyPI Package·GitHub RepositoryThe Terra Classic Software Development Kit (SDK) in Python is a simple library toolkit for building software that can interact with the Terra Classic blockchain and provides simple abstractions over core data structures, serialization, key management, and API request generation.FeaturesWritten in Python with extensive support librariesVersatile support for key management solutionsExposes the Terra Classic API through LCDClientSupports non-Terra Classic addresses for transactionsIBC swaps between non-Terra Classic chains are partially supported (where IBC channels exist)Osmosis support (requires terra.proto 3.0.2 or newer)Recent changes2.1.4Liquidity pool support added, you can now join and exit liquidity pools on OsmosisThis is specifically matched with terra.proto version 3.1.22.1.3All packages have been upgraded to the latest version, except for betterproto which is already on a pre-release version.2.1.2Governance voting and proposal analysis now worksGovernance APIs updated to version 1Documentation reviewed and updated2.1.1Better Osmosis support. Pools can now be requested by pool ID, or the entire list can be requested.2.1.0Osmosis support. This is just a simple exposure of the messages in terra.proto.Table of ContentsAPI ReferenceGetting StartedRequirementsInstallationDependenciesTestsCode QualityUsage ExamplesGetting Blockchain InformationAsync UsageBuilding and Signing TransactionsExample Using a WalletContributingReporting an IssueRequesting a FeatureContributing CodeDocumentation ContributionsLicenseAPI ReferenceAn intricate reference to the APIs on the Terra SDK can be foundhere.Getting StartedA 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 availablehereas reference.RequirementsTerra Classic SDK requiresPython v3.7+.InstallationNOTE: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, likeipython.Terra SDK can be installed (preferably in avirtual environmentfrom PyPI usingpip) as follows:$ pip install -U terra-classic-sdkYou might need to run pip viapython -m pip install -U terra_classic_sdk. Additionally, you might havepip3installed instead ofpip; proceed according to your own setup.DependenciesTerra Classic SDK usesPoetryto manage dependencies. To get set up with all the required dependencies, run:$ pip install poetry
$ poetry installTestsTerra Classic SDK provides extensive tests for data classes and functions. To run them, after the steps inDependencies:$ make testCode QualityTerra Classic SDK usesBlack,isort, andMypyfor checking code quality and maintaining style. To reformat, after the steps inDependencies:$ make qa && make formatUsage ExamplesTerra Classic 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 Classic SDK are not limited to the following examples and can be found in fullhere.In order to interact with the Terra Classic blockchain, you'll need a connection to a Terra Classic node. This can be done through setting up an LCDClient (The LCDClient is an object representing an HTTP connection to a Terra Classic LCD node.):>>>fromterra_classic_sdk.client.lcdimportLCDClient>>>terra=LCDClient(chain_id="columbus-5",url="https://terra-classic-lcd.publicnode.com")Getting Blockchain InformationOnce properly configured, theLCDClientinstance will allow you to interact with the Terra Classic blockchain. Try getting the latest block height:>>>terra.tendermint.block_info()['block']['header']['height']'1687543'Async UsageIf 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.>>>importasyncio>>>fromterra_classic_sdk.client.lcdimportAsyncLCDClient>>>asyncdefmain():terra=AsyncLCDClient("https://terra-classic-lcd.publicnode.com","columbus-5")total_supply=awaitterra.bank.total()print(total_supply)awaitterra.session.close# you must close the session>>>asyncio.get_event_loop().run_until_complete(main())Building and Signing TransactionsIf you wish to perform a state-changing operation on the Terra Classic blockchain such as sending tokens, swapping assets, withdrawing rewards, or even invoking functions on smart contracts, you must create atransactionand broadcast it to the network.
Terra Classic SDK provides functions that help create StdTx objects.Example Using a Wallet (recommended)AWalletallows 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).UseLCDClient.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. ifpython3 -c "import ssl; print(ssl.OPENSSL_VERSION)"returns LibreSSL 2.8.3, you need to reinstall python via pyenv or homebrew.>>>fromterra_classic_sdk.client.lcdimportLCDClient>>>fromterra_classic_sdk.key.mnemonicimportMnemonicKey>>>mk=MnemonicKey(mnemonic=MNEMONIC)>>>terra=LCDClient("https://terra-classic-lcd.publicnode.com","columbus-5")>>>wallet=terra.wallet(mk)Once you have your Wallet, you can simply create a StdTx usingWallet.create_and_sign_tx.>>>fromterra_classic_sdk.core.feeimportFee>>>fromterra_classic_sdk.core.bankimportMsgSend>>>fromterra_classic_sdk.client.lcd.api.tximportCreateTxOptions>>>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)ContributingCommunity 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 IssueFirst things first:Do NOT report security vulnerabilities in public issues!Please disclose responsibly by submitting your findings to theTerra 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 theIssuespage, or if there is a pull request on thePull requestspage. 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 interpreterTerra SDKOptionally of the other dependencies involvedIf 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 FeatureIf you wish to request the addition of a feature, please first check out theIssuespage and thePull requestspage (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 theIssuespage.Contributing CodeIf 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:Fork the repository.Build the project using theDependenciesandTestssteps.Install avirtualenv.Develop your code and test the changes using theTestsandCode Qualitysteps.Commit your changes (ideally follow theAngular commit message guidelines).Push your fork and submit a pull request to the repository'smainbranch 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 ContributionsDocumentation improvements are always welcome. The documentation files live in thedocsdirectory of the repository and are written inreStructuredTextand useSphinxto 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 thisguideread for more insight.LicenseThis software is licensed under the MIT license. SeeLICENSEfor full disclosure.© 2021 Terraform Labs, PTE.Powering the innovation of money.