Mospy Tutorial #2 —Cosmos SDK based coins (e.g. Osmosis, Juno, Stargaze)

Felix
3 min readMar 20, 2023
Mospy Tutorial 2

After covering the account creation and transaction workflow in the first article, we’ll now have a look at using mospy for other Cosmos SDK based coins like Osmosis, Juno, Akash, etc. This article will cover the core features (more on this in the introduction). I will write a more detailed article about custom modules.

Introduction

Before we get started, I’d like to give you a general overview of how the Cosmos SDK works. This is important to know when working with other chains. The functionality of blockchains based on the Cosmos SDK is divided into different modules. For example, there is a banking module that handles “multi-asset coin transfers between accounts and tracking special case pseudo-transfers” or a governance module that handles governance functionality.
Literally all Cosmos SDK based chains share the same core modules (bank, gov, staking, etc). A full list can be found in the Cosmos documentation. In addition to these core modules, each project can create its own modules with custom functionality. For example, Osmosis has its own modules for its DEX functionality. You can find a list of all their modules on their github.
A huge advantage for anyone using Cosmos SDK based coins is that the basic functionality (included in the core modules) is the same on every chain. The ‘only’ difference between a transfer transaction on Cosmos and Osmosis is the chain parameters.

Configure Mospy to work with other chains

Now that we know that we only need to adjust the chain parameters, we will look at how to do this with mospy. To get a quick overview of the different chain parameters, I recommend using the chain section of https://cosmos.directory. For Osmosis the link would be https://cosmos.directory/osmosis/chain.

Account

The parameter you need to change on the account object is the address prefix. If you are using a seed, you will also need to change the slip44 value. The address prefix is the prefix string for each address and the slip44 value is a value used for seed derivation. Most coins based on the Cosmos SDK don’t change the slip44 value and use the same as Cosmos, which is 118. In this case you don’t even need to change it.

Example for a chain which didn’t change the slip44 value (Osmosis):

from mospy import Account

account = Account(
private_key="8c2ae3f9c216f714c0a877e7a4952ec03462496e01452bd5ee79ef79d707ff6c",
hrp="osmo"
)

Example for a chain which changed the slip44 value (Bitsong):

from mospy import Account

account = Account(
private_key="8c2ae3f9c216f714c0a877e7a4952ec03462496e01452bd5ee79ef79d707ff6c",
hrp="bitsong",
slip44=639

)

Transaction

When sending transactions, you need to change the chain id. You can also find the appropriate chain id in the Cosmos directory. It’s also important to remember that the asset denom changes, and eventually the exponent. For example, Evmos uses “aevmos” with an exponent of 18. Osmosis uses uosmo, which has the same exponent as uatom.

Example for Evmos:

tx = Transaction(
account=account,
gas=2000000,
memo="The first mospy evmos transaction!",
chain_id="evmos_9001-2",
)

tx.set_fee(
denom="aevmos",
amount=40000000000000000
)
tx.add_msg(
tx_type="transfer",
sender=account,
receipient=account.address,
amount=3500000000000000,
denom="aevmos",
)

Summary

As you can see, it’s not very difficult to use the features of the core modules on different Cosmos chains. With cosmos.directory and the underlying chain registry, it’s even possible to automate parameter fetching and automatically support almost all Cosmos SDK coins.

--

--