Skip to main content

Babylon Genesis Smart Contracts Deployment Guide

This guide walks you through deploying a smart contract on the Babylon Genesis blockchain. The example contract we'll deploy is a storage contract that allows saving data with Bitcoin timestamps and verifying Bitcoin finalization status.

Prerequisites​

Before starting, ensure you have installed:

  • Rust - Required for building the CosmWASM contract and CLI tool
  • Docker - Used for contract optimization

Detailed Deployment Steps​

1. Repository Setup​

First, clone this repository containing the Babylon smart contract code. The repository includes submodules for Babylon core and contract dependencies.

Clone and update the submodules:

git submodule update --init --recursive

This command initializes and fetches all necessary submodule code: babylond binary and storage_contract. It might take a few minutes to complete as close to 1GB of data will be downloaded.

2. Babylond CLI Installation​

The Babylond CLI is your primary tool for interacting with the Babylon Genesis blockchain.

Verify your Rust installation first:

cargo --version
# Expected output similar to:
cargo 1.84.1 (66221abde 2024-11-19)

Build and install the CLI:

cd babylon
make install

Verify the installation:

babylond version
# Should output a version hash similar to:
main-112821536b0ada40aa29e34b53206f56c61bf631

3. Environment Configuration​

Load network-specific variables:

# For Phase 2 testnet (Babylon Genesis)
source env-phase2-testnet.sh

# OR for Phase 3 devnet
source env-phase3-devnet.sh

These files set crucial variables:

  • $homeDir: Babylond configuration directory
  • $chainId: Network identifier
  • $feeToken: Token used for transaction fees
  • $key: Your wallet name
  • $nodeUrl: RPC endpoint
  • $apiUrl: REST API endpoint

Verify the configuration:

echo $homeDir, $chainId, $feeToken, $key, $nodeUrl, $apiUrl

# Expected output:
/Users/<your_username>/.babylond, bbn-test-6, ubbn, test-key, https://babylon-testnet-rpc.polkachu.com, https://babylon-testnet-api.polkachu.com

4. Wallet Management​

Create a New Wallet​

Create a test wallet using the local keyring:

babylond keys add test-key --keyring-backend=test

This command:

  • Creates a new key pair named test-key
  • Stores it in the test keyring
  • Outputs the address and recovery phrase
  • IMPORTANT: Save the mnemonic phrase securely for recovery using --restore command

Verify the wallet:

babylond keys list --keyring-backend=test
# Expected output:
- address: bbn1y7h7nmxpwlfuj8m72qa0uwc8nqflzgragve5zv
name: test-key
pubkey: '{"@type":"/cosmos.crypto.secp256k1.PubKey","key":"Aw48YzsPg4bPhToeAfBM/sIL+nmgi7oxPjJ8sQXVl0lW"}'
type: local

Fund Your Wallet​

You need test tokens (tBABY) to deploy contracts. Get them through:

Xangle Babylon Genesis Testnet Faucet:

  1. Visit Xangle Faucet
  2. Create an account
  3. Request test tokens with wallet you just created above

Verify your balance:

babylond query bank balances $(babylond keys show $key -a --keyring-backend=test) --node=$nodeUrl

Expected output should show some tBABY tokens.

5. Contract Building​

Verify Docker​

The build process uses Docker for consistent environments:

docker run hello-world

Build Contract​

Navigate to the contract directory and build:

cd storage-contract
cargo run-script optimize

This creates an optimized WASM file in the artifacts directory.

6. Contract Deployment Process​

Step 1: Store Contract Code​

Upload the WASM file to the blockchain:

babylond tx wasm store ./artifacts/storage_contract-aarch64.wasm \
--from=$key \
--gas=auto \
--gas-prices=0.002$feeToken \
--gas-adjustment=1.3 \
--chain-id="$chainId" \
-b=sync \
--yes \
--keyring-backend=test \
--log_format=json \
--home=$homeDir \
--node=$nodeUrl

Key parameters explained:

  • --gas=auto: Automatically estimates required gas
  • --gas-prices=0.002$feeToken: Sets the gas price to gas = autoCalculation x 0.002ubbn
  • --gas-adjustment=1.3: Adds 30% to estimated gas for safety so gas = autoCalculation x 0.002ubbn x 1.3
  • -b=sync: Waits for transaction to be broadcast
  • --yes: Automatically confirms the transaction

Step 2: Get Contract Code ID​

After storing, get the unique code identifier:

codeID=$(babylond query wasm list-code \
--node $nodeUrl \
-o json \
| jq \
--arg ADDR "$(babylond keys show $key -a --keyring-backend=test)" \
'.code_infos[] | select(.creator==$ADDR).code_id | tonumber')

This command filters contracts against your wallet address to find the one you just uploaded.

Step 3: Instantiate Contract​

Create a new instance of the contrac using tx wasm instantiate command:

babylond tx wasm instantiate $codeID '{}' \
--from=$key \
--no-admin \
--label="storage_contract" \
--gas=auto \
--gas-prices=0.002$feeToken \
--gas-adjustment=1.3 \
--chain-id="$chainId" \
-b=sync \
--yes \
--keyring-backend=test \
--log_format=json \
--home=$homeDir \
--node=$nodeUrl

Parameters explained:

  • '{}': Empty initialization parameters for the storage_contract
  • --no-admin: Creates contract without admin privileges
  • --label: Human-readable identifier

Get the contract's address and store it in a variable:

contractAddress=$(babylond query wasm list-contract-by-code $codeID \
--node=$nodeUrl \
-o json \
| jq -r '.contracts[0]')

7. Contract Interaction​

Save Data​

To store data in the contract using tx wasm execute command:

# Your data to store
data="This is example plain-text data"

# Convert to hex format (required by contract)
hexData=$(echo -n "$data" | xxd -ps -c0)

# Create the execution message for the save_data function
executeMsg="{\"save_data\":{\"data\":\"$hexData\"}}"

# Send transaction via `tx wasm execute` command
babylond tx wasm execute $contractAddress "$executeMsg" \
--from=$key \
--gas=auto \
--gas-prices=0.002$feeToken \
--gas-adjustment=1.3 \
--chain-id="$chainId" \
-b=sync \
--yes \
--keyring-backend=test \
--log_format=json \
--home=$homeDir \
--node=$nodeUrl

Query Data​

To retrieve stored data using query wasm contract command:

# Create hash of original data for verification
hashedData=$(echo -n "$data" | sha256sum | cut -f1 -d' ')

# Prepare query message
quer

As a reault you should see the following output with data store and finalized status and btc_timestamp information:

{
"data": {
"finalized": false,
"latest_finalized_epoch": "644",
"data": {
"data": "54686973206973206578616d706c6520706c61696e2d746578742064617461",
"btc_height": "234328",
"btc_timestamp": "1738908922",
"saved_at_btc_epoch": "658"
}
}
}