Cosmos 🪐 + Python 🐍 = 😍


Open your terminal, navigate to your project, and install via PyPI

$ pip3 install cosmpy

What is Cosmpy?

Cosmpy is a Python library for interacting with Cosmos-based blockchains.

  • A simplified Cosmos-SDK agnostic API for basic chain operations.
  • An easy API for deploying and interacting with CosmWasm-based smart contracts.
  • Low-level access to primitive ledger APIs if required.

🔍Getting Started

☎️Connecting to a network

To start interacting with a blockchain, you first need to establish a connection to a network node. This is shown in the code sample below:

from cosmpy.aerial.client import LedgerClient, NetworkConfig ledger = LedgerClient(NetworkConfig.fetch_mainnet())

For convenience, some networks' configurations are provided automatically (for example's chain). Users wanting to target other chains can customise the NetworkConfig object with their own settings, as shown in the example below:

cfg = NetworkConfig( chain_id="cosmoshub-4", url="grpc+https://...", fee_minimum_gas_price=1, fee_denomination="uatom", staking_denomination="uatom", ) ledger = LedgerClient(cfg)

💰️Querying a balance

Once you have successfully created your LedgerClient, you can start interactnig with the network. The most basic operation you might want to do is to query an account balance. This is done simply with the following code:

balance = ledger.query_bank_balance('fetch1h2l3cnu7e23whmd5yrfeunacez9tv0plv5rxqy')

By default, this will query the fee denomination that is configured in your NetworkConfig. However, this can also be customised by the user, as follows:

balance = ledger.query_bank_balance('cosmos1h2l3cnu7e23whmd5yrfeunacez9tv0plv5rxqy', denom='uatom')

🗝Wallets and Keys

To start making changes to the network, you will need to start sending transactions to it. This in tern involves managing private keys and addresses. Luckily Cosmpy makes this relatively straightforward.

The following code outlines how to both generate a completely new private key and how to recover a previously generated one:

from cosmpy.aerial.wallet import LocalWallet from cosmpy.crypto.keypairs import PrivateKey # you can create a random private key sk = PrivateKey() # you can recover a private key sk = PrivateKey('<base64 encoded private key>')

The PrivateKey object is one of CosmPy's low-level primitives. This is why it is generally paired with a Wallet object in most scenarios. This is illustrated in the following code example:

wallet = Wallet(sk) print(wallet.address()) # will print the address for the wallet

Creating the wallet allows users to query useful information such as the address from the wallet directly:

📬Sending Tokens

Once you have your wallet configured, you can start sending transactions to the network. The LedgerClient object provides useful utilities to do common operations. The following example shows how to send 10 atestfet tokens to another address:

destination_address = 'fetch1h2l3cnu7e23whmd5yrfeunacez9tv0plv5rxqy' tx = ledger.send_tokens(destination_address, 10, "atestfet", wallet) # block until the transaction has been successful or failed tx.wait_to_complete()

🥇What's next?

Mastered all of the above? Great! But you have only scratched the surface. Check out ourmain documentation sitefor more guides and resources.