Cosmos CLI

The repository cosmos-rs provides Rust library and CLI utility for interacting with Cosmos blockchains over gRPC.

This page documents the CLI part of cosmos-rs.

Installation

cargo install --git https://github.com/fpco/cosmos-rs cosmos-bin --locked

Usage

Countertrade contract is used as an example smart contract in this page. But the concepts apply to other contracts too.

Export environment variables

export COSMOS_GRPC="https://grpc.osmotest5.osmosis.zone"
export COSMOS_NETWORK="osmosis-testnet"
export COSMOS_WALLET="REDACTED"

Note that COSMOS_GRPC is optional and can be used to override the node being used. If not specified, it will use the default node for that network.

Deploy smart contracts

❯ cosmos contract store-code ./wasm/artifacts/levana_perpswap_cosmos_countertrade.wasm
Code ID: 9979
❯ cosmos contract instantiate 9979 "osmodev countertrade contract" '{"factory":"osmo1v2599ea7c25kszxjchzm7rfa34rkd75rh0rhrjj2dpg9v9aw0jmqv20um8","admin":"osmo1pqurdp8msxmy6ecue6xdcygg8dsz0ejvcqh5wf","config":{"min_funding":null,"target_funding":null,"max_funding":null,"max_leverage":null,"iterations":null,"take_profit_factor":null}}'
Contract: osmo1v68cqz5t3g46hfaedyx3m0rqpt6az7yuzlz7yhp90p9uwx7lt6pqfxzkyj

How to find out the exact message that should be passed to the contract ? You have to look into the InstantiateMsg type that is defined in your smart contract code.

Upload newer version of smart contract

❯ cosmos contract store-code ./wasm/artifacts/levana_perpswap_cosmos_countertrade.wasm
Code ID: 10000
❯ cosmos contract migrate osmo1v68cqz5t3g46hfaedyx3m0rqpt6az7yuzlz7yhp90p9uwx7lt6pqfxzkyj 10000 {}
$ cosmos bank print-balances "$MY_WALLET"
49419641uosmo

Query markets

$ cosmos contract query $CONTRACT '{"markets":{}}'
{"markets":[{"id":"ATOM_USD","collateral":"1000","shares":"1000","position":null,"too_many_positions":false}],"next_start_after":null}

Query Cw20 balance

$ cosmos contract query osmo108hkyx02uekpxgrz42388k3l3ryr26k95pq6rmcxxv9qczvp9y9sssm5kk "{\"balance\": {\"address\":\"$MY_WALLET\"}}"
{"balance":"7000000"}

For interacting with cw20 smart contract, you can refer to the corresponding documentation.