> For the complete documentation index, see [llms.txt](https://docs.satlayer.xyz/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://docs.satlayer.xyz/bvs-developers-1/developer-toolbox/babylon-cli.md).

# Babylon CLI

Babylon Chain's Command Line Tool

As SatLayer is built on Babylon, we will often need to interact with Babylon Chain.  Sometimes we need to utilize the low-level cli tool `babylond` during development.  This document serves as a centralized resource for getting and installing  `babylond` as well as commonly used operations.&#x20;

Please note that this is not an exhaustive list of the commands supported by Babylon.

## Installing babylond

In order to compile Babylon, `Go >= 1.21` should be installed. Methods to install golang differ depending on the operating system you are using. Generally, please follow the instructions on the [Golang install page](https://go.dev/doc/install) to install the Go compiler.&#x20;

For Linux users, follow the installation instructions according to the distribution you are using. For Ubuntu and other Debian-based system, run:

```bash
sudo apt-get install golang-go
```

For Arch based systems, run:

```bash
sudo pacman -S go
```

For Fedora and other RHEL based systems, run:

```bash
sudo dnf install golang
```

1. Clone Babylon source code repo and `cd` into the directory

```bash
git clone https://github.com/babylonchain/babylon.git
cd babylon
```

2. To build the binary, execute&#x20;

```bash
make build
```

After compiling, you can find the binary in `./build/babylond`.

3. Install babylond

To install the binary to your user's directories, execute the following. This command installs  `babylond` to Golang's default install directory `$HOME/go/bin`. Please make sure to have this path added to your [PATH environment variable.](https://en.wikipedia.org/wiki/PATH_\(variable\))

```bash
make install
```

Then add `$HOME/go/bin` to `PATH`. For UNIX users (Linux, MacOS, etc..)

```bash
# ~/.bashrc or ~/.zshrc
export PATH=$HOME/go/bin:$PATH
```

Then `source` your shell rc scrip or start a new shell to active it.

```bash
source ~/.bashrc
# or
source ~/.zshrc
```

Or Windows or UNIX with PowerShell you should edit the `$profile` file (run `echo $profile` in PowerShell to acquire the file path) and append the following.

```powershell
# Edit $profile
$env:Path = $env:UserProfile+[IO.Path]::PathSeparator+"go"+[IO.Path]::PathSeparator+"bin;"+$env:Path
```

Close and start a new PowerShell session to activate the change.

## Key Operations

`babylond` allows the management of keys. Common operations include create, delete, export, import and listing keys. Multiple keyring backends are supported.&#x20;

By default, `babylond` asks the OS to handle key storage. But other options are available, mostly for development purposes.

```shell
# Create a new key under the name `wallet_name`, stored by the OS
./babylond keys add ${wallet_name} --keyring-backend os --keyring-dir ./   

Flag explanation:
    --keyring-backend: Select keyring backend (os|file|kwallet|pass|test|memory) (default "os").
    --keyring-dir: The client Keyring directory; if omitted, the "default" home directory will be used.
```

### Deleting Keys

To delete keys, run this command:&#x20;

```bash
# Delete the given key:
./babylond keys delete ${wallet_name}
```

### Printing keys&#x20;

The `keys export` sub-comamnd prints the key:&#x20;

```bash
# Export private keys:
./babylond keys export ${wallet_name} --output json

Flag explanation:
    --output: Output format (text|json) (default "text").
```

### Importing keys

Key import is also supported via this command:&#x20;

```bash
# Import private keys into the local keybase:
./babylond keys import
```

### Listing keys

To list all keys stored in the keyring:&#x20;

```bash
# List all keys:
./babylond keys list
```

## Query

`query` houses subcommands that queries specific information. The most common queries are  for account balances, transaction information and smart contract states. These operations requires communication with a Babylon node. The `--node` parameter specifics the Babylon (RPC) node to use. And the `--chain-id` specifics the chain ID. For testnet, these 2 parameters must be specified correctly in order for the command to work at all.

```shell
# Query native token balances:
./babylond query bank balances ${wallet_address} --chain-id ${chain_id} --node ${rpc}

Flag explanation:
    --chain-id: The network id. 
    --node: CometBFT rpc interface for the chain.
```

#### To query a past transaction

```bash
# Query tx hash:
./babylond query tx ${tx_hash} --log_format=json --node ${rpc}

Flag explanation:
    --log_format: The logging format (json|plain) (default "plain")
```

{% hint style="info" %}
Not all nodes are so-called archiver nodes. Non-archive node will truncate chain history to reduce storage space needed by the node software. Querying an old transaction against an non-archiver may result in the transaction not being found or an error.
{% endhint %}

#### To query the state of a smart contract

```bash
# This command queries the state of a smart contract. Calls contract with given address with querying data and prints the returned result
./babylond query wasm contract-state smart ${contract_address} ${json_encoded_init_args} --log_format=json --node ${rpc}
```

## CosmWasm

Babylon supports CosmWasm smart contracts. The `wasm` subcommands deals with related operations.&#x20;

Like the `query` subcommand above, `--node` and `--chain-id` are needed when use on a testnet. In addition, they are required for gas controlling parameters to limit the maximal gas spent on the operation.

```shell
# Upload a wasm binary:
./babylond tx wasm store ${wasm_directory} --from=${wallet_name} --gas=auto --gas-prices=1ubbn --gas-adjustment=1.3 --chain-id=${chain_id} -b=sync --yes --log_format=json --node ${rpc}

Flag explanation:
    --from: Name or address of private key with which to sign. 
    --gas: Gas limit to set per-transaction; set to "auto" to calculate sufficient gas automatically.
    --gas-prices: Gas prices in decimal format to determine the transaction fee (e.g. 0.1uatom).
    --gas-adjustment: Adjustment factor to be multiplied against the estimate returned by the tx simulation; if the gas limit is set manually this flag is ignored.
    -b, --broadcast-mode: Transaction broadcasting mode (sync|async) (default "sync").
    -y, --yes: Skip tx broadcasting prompt confirmation.
```

After uploading the WASM binary, a contact needs to be instantiated from the binary:

```bash
# Instatiate a wasm contract:
./babylond tx wasm instantiate ${code_id} ${json_encoded_init_args} --from=${wallet_name} --no-admin --label=${contract_label} --gas=auto --gas-prices=1ubbn --gas-adjustment=1.3 --chain-id=${chain_id} -b=sync --yes --log_format=json --node ${rpc}

Flag explanation:
    --label: A human-readable name for this contract in lists.
```

CosmWasm contracts separate query and execute functions, distinguishing between read-only and mutable operations. Queries are free, while executing a contract requires gas.

```bash
# Execute a command on a wasm contract:
./babylond tx wasm execute ${contract_address} ${json_encoded_init_args} --from=${wallet_name} --gas=auto --gas-prices=1ubbn --gas-adjustment=1.3 --chain-id=${chain_id} -b=sync --yes --log_format=json --node ${rpc}
```

In addition, EVM and contracts on CosmWasm can be upgraded (if `migrate` is enabled)

```bash
# Migrate a wasm contract to a new code version:
./babylond tx wasm migrate ${contract_address} ${new_code_id_int64} '{"migrate":{}}' --from=${wallet_name} --gas-prices=1ubbn --gas=auto --gas-adjustment=1.3 --chain-id=${chain_id} -b=sync --yes --log_format=json --node ${rpc}
```

## Others

The `tx bank send` command sends token from one address (you must own the private key) to another. The same node, chain and gas parameters apply here too.

```shell
# Transfer native tokens:
./babylond tx bank send ${from_address} ${to_address} ${amount} --node ${rpc} --keyring-backend ${keyring_backend} --keyring-dir ${keyring_dir} --chain-id=${chain_id} --fees 2ubbn

Flag explanation:
    --fees: Fees to pay along with transaction; eg: 10uatom.
```

## Reference

You can use `./babylond --help` to get full documentations about how to use babylon command line or through the [official Babylon documentation](https://docs.babylonchain.io/docs/cli/babylond/cli).
