A python library for interacting with cosmos based blockchain networks
To install the project use:
pip3 install cosmpy
Below is a simple example for querying an account's balance and sending funds from one account to another using RestClient
:
from cosmpy.clients.signing_cosmwasm_client import SigningCosmWasmClient
from cosmpy.common.rest_client import RestClient
from cosmpy.crypto.address import Address
from cosmpy.crypto.keypairs import PrivateKey
from cosmpy.protos.cosmos.base.v1beta1.coin_pb2 import Coin
# Data
rest_endpoint_address = "http://the_rest_endpoint"
alice_private_key = PrivateKey(bytes.fromhex("<private_key_in_hex_format>"))
chain_id = "some_chain_id"
denom = "some_denomination"
bob_address = Address("some_address")
channel = RestClient(rest_endpoint_addres)
client = SigningCosmWasmClient(private_key, channel, chain_id)
# Query Alice's Balance
res = client.get_balance(client.address, denom)
print(f"Alice's Balance: {res.balance.amount} {res.balance.denom}")
# Send 1 <denom> from Alice to Bob
client.send_tokens(bob_address, [Coin(amount="1", denom=denom)])
To see the documentation, first run:
make generate-docs
Then (if on Linux or MacOS):
make open-docs
And if on windows, open docs/build/html/index.html
.
Under the examples
directory, you can find examples of basic ledger interactions with cosmpy
using both REST and gRPC, e.g. querying, sending a transaction, interacting with a smart contract, and performing atomic swaps. To run any example <example_file_name>
:
python ./examples/<example_file_name>.py