Package: secret-sdk
Python SDK for Secret NetworkThe Secret Software Development Kit (SDK) in Python is a simple library toolkit for building software that can interact with the Secret blockchain and provides simple abstractions over core data structures, serialization, key management, and API request generation. The SDK is based on a fork ofterra.pyon TerraFeaturesWritten in Python offering extensive support librariesVersatile support for key management solutionsExposes the Secret Rest API through LCDClientTable 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 Secret SDK can be foundhere.Getting StartedA walk-through of the steps to get started with the Secret SDK alongside with a few use case examples are provided below.RequirementsSecret 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.Secret SDK can be installed (preferably in avirtual environmentfrom PyPI usingpip) as follows:$ pip install -U secret-sdkYou might havepip3installed instead ofpip; proceed according to your own setup.DependenciesSecret SDK usesPoetryto manage dependencies. To get set up with all the required dependencies, run:$ pip install poetry
$ poetry installTestsSecret SDK provides extensive tests for data classes and functions. To run them, after the steps inDependencies:$ make testUsage ExamplesSecret SDK can help you read block data, query secret contracts, sign and send transactions, and many more.
Following examples are provided to help get building started.In order to interact with the Secret blockchain, you'll need a connection to a Secret node or an api. This can be done through setting up an LCDClient (The LCDClient is an object representing an HTTP connection to a Secret LCD node.):>>> from secret_sdk.client.lcd import LCDClient
>>> secret = LCDClient(chain_id="secret-4", url=node_rest_endpoint)Getting Blockchain InformationOnce properly configured, theLCDClientinstance will allow you to interact with the Secret blockchain. Try getting the latest block height:>>> secret.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.>>> import asyncio 
>>> from secret_sdk.client.lcd import AsyncLCDClient

>>> async def main():async with AsyncLCDClient(url=node_rest_endpoint, chain_id="secret-4") as secret:community_pool = await secret.distribution.community_pool()
            print(community_pool)await secret.session.close()  # you must close the session>>> asyncio.get_event_loop().run_until_complete(main())You can improve the efficiency of consecutive queries by making them asynchronous.>>> import asyncio
>>> import uvloop

>>> from secret_sdk.client.lcd import AsyncLCDClient
>>> from secret_sdk.exceptions import LCDResponseError

>>> def owner_of(token_id):
        return {
                "owner_of": {
                    "token_id": token_id,
                }
            }

>>> async def query_owner(secret, contract_address, token_id, query):
        try:
            msg = await secret.wasm.contract_query(contract_address, query)
            return (token_id, msg["owner_of"]["owner"])
        except LCDResponseError:
            return (token_id, "")

>>> async def query_collection(contract_address, token_ids):async with AsyncLCDClient(chain_id="secret-4", url=node_rest_endpoint) as secret:requests = [query_owner(secret, contract_address, token, owner_of(token)) for token in token_ids]owners = await asyncio.gather(*requests, return_exceptions=True)print(owners)await secret.session.close() # you must close the session>>> uvloop.install()
>>> if __name__ == '__main__':
        asyncio.run(query_collection(contract_address, token_ids))Building and Signing TransactionsIf you wish to perform a state-changing operation on the Secret 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.
Secret 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.>>> from secret_sdk.client.lcd import LCDClient
>>> from secret_sdk.key.mnemonic import MnemonicKey

>>> mk = MnemonicKey(mnemonic=MNEMONIC) 
>>> secret = LCDClient(node_rest_endpoint, "secret-4")
>>> wallet = secret.wallet(mk)Once you have your Wallet, you can create a StdTx usingWallet.create_and_sign_txthen broadcast it to the network withsecret.tx.broadcastwith your broadcast mode of choice (block, sync, async - see cosmos docs).>>> from secret_sdk.core.auth import StdFee
>>> from secret_sdk.core.bank import MsgSend

>>> send_msg = MsgSend(
            wallet.key.acc_address,
            RECIPIENT,
            "1000000uscrt"    # send 1 scrt
        )
>>> tx = wallet.create_and_sign_tx(
        msgs=[send_msg],
        memo="My first transaction!",
        fee=StdFee(200000, "120000uscrt")
    )
>>> result = secret.tx.broadcast(tx)
>>> print(result)Or use the abstractionwallet.send_tokens(seewallet.execute_txto execute a smart contract withhandle_msg).>>> tx = wallet.send_tokens(recipient_addr=RECIPIENT, transfer_amount="1000000uscrt")Batch TransactionsYou can combine muliple state-changing transactions for the same contract into a single transaction. The contract used here is from theCounter contract examplemsg = {
  'increment': {}
}

msg_list = [msg for _ in range(10)]

tx = wallet.execute_tx(
  CONTRACT_ADDR,
  msg_list,
  memo="My first batch transaction!",
)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 letting the Secret SDK team know upfront (discord , telegram). 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 request 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 interpreterSecret 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 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 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 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:Fork the repository.Build the project using theDependenciesandTestssteps.Install avirtualenv.Develop your code and test the changes using theTestsandCode Qualitysteps.Commit your changesPush your fork and submit a pull request to the repository'smainbranch 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 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 imporvements by submiting 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.