XION
DiscordGithub
  • Welcome to XION
  • XION'S Core
    • Concepts
      • Generalized Chain Abstraction
      • Intro to Account Abstraction
      • XION's Meta Accounts
      • Meta Accounts Design
      • Architecture & Tech Glossary
      • Use Cases
  • developers
    • XION Quick Start
      • Zero to App in 5 Minutes
        • Launch a User Map App on XION in 5 Minutes
        • React Native Mobile App on XION in 5 Minutes
    • Mobile App Development
      • Set up your XION Mobile Development Environment
      • Create Mobile App and Integrate Meta Account Authentication
      • Building a React Native Mobile App with Abstraxion (Xion.js)
    • Getting Started (Advanced)
      • Set Up Local Environment
        • Setting up your Local Smart Contract Development Environment for XION
          • Setting up your XION Smart Contract Development Environment on Windows (WSL2 + Ubuntu)
        • Set Up an Integrated Development Environment (IDE)
        • Interacting with Xion Chain using Xion Daemon
      • Your First Contract
        • Deploying Your First Smart Contract on XION
      • Gasless UX & Permission Grants
        • Enabling Gasless Transactions with Treasury Contracts
      • App Development
        • Account Abstraction with Gasless Transactions
        • Interact with XION via your Backend Service
    • Re-using Existing Contracts
      • Deployed Contracts on XION
      • Non-Fungible Tokens (NFTs)
      • Fungible Tokens
      • Marketplace
      • Multisig
      • Proxy Contracts
      • Membership Management
      • Governance
      • Valuts
      • SCV Audited Contracts
    • Web3 for Web2 Developers
      • Web2 vs Web3 App Architecture: A Comparison
      • Misconceptions and Misapplied Web2 Patterns
      • Recommended Architecture for Apps on XION
    • Building for Mainnet
      • Xion Testnet: Your Development Playground
      • Building with Audited & Battle-Tested Contracts
      • Community Engagement: Building Support for Your app
      • Deploying to Xion Mainnet
        • Smart Contract Audits: Process, Costs & Support
        • Governance Process to Deploying Smart Contracts to Mainnet
    • Learn & Build
      • Token Factory
        • Creating, Minting, and Interacting with a Token Factory Token
        • Building a React dApp to Interact with Token Factory Tokens
        • Integrating a Token Factory Token in a Smart Contract
      • Websockets
        • WebSockets with Xion: Real-Time Communication
      • Oracles
        • Creating a Smart Contract with Pyth Oracle Integration
      • Indexers: Optimized Data Retrieval
        • SubQuery
      • Use Cases
        • Building a Per-User Data Storage App
        • Build a TODO App using the Collection-Document Storage Smart Contract
    • Reference and Resources
      • Requesting XION Testnet Tokens
      • Public Endpoints & Resources
      • Block Explorers
      • Governance
        • Deploying Smart Contracts to Mainnet
      • Developer Tools: Abstract
      • IBC Denoms on XION Networks
      • Frequently Asked Questions
      • XION Token Contract Addresses on EVM Chains
  • Nodes & Validators
    • Run a Node
      • System Specifications
      • Build the Xion Daemon
      • Download the Xion Daemon
      • Configure the Xion Daemon
        • app.toml
        • client.toml
        • config.toml
      • Join the XION Network
        • xion-testnet-1
      • Confirm node is running
    • Become a Validator
      • Initial Setup
      • Obtain a XION Wallet Address
      • Obtain Funds
        • Testnet
      • Create Validator
    • IBC Relayers and Tokens
  • Others
    • Resources
Powered by GitBook
On this page
  • Prerequisites
  • Generate an account
  • Generate an Account Key Pair
  • Fund Your Account
  • Compile and Optimize the Contract
  • Clone the Repository
  • Optimize Contract
  • Upload Optimized Contract On-chain
  • Retrieve the Code ID
  • Instantiate the Contract
  • Retrieve the Contract Address
  • Querying the Contract
  • Execute Transactions
  • Increment the Contract’s Count
  • Reset the Contract’s Count

Was this helpful?

Edit on GitHub
  1. developers
  2. Getting Started (Advanced)
  3. Your First Contract

Deploying Your First Smart Contract on XION

PreviousYour First ContractNextGasless UX & Permission Grants

Last updated 1 month ago

Was this helpful?

This guide will walk you through the process of deploying a smart contract on the XION network. The deployment process involves compiling an optimized version of your contract code, uploading this optimized code to the blockchain, and then creating an instance of the contract. Once instantiated, the contract will have its own unique address and data store, allowing you to interact with it through transactions and queries.

Prerequisites

Before deploying your smart contract on-chain, ensure you have completed the following setup steps:

  • Set up your local environment: Follow the to configure your development environment.

  • Install the XION daemon: Set up the XION CLI by following the to interact with the blockchain.

  • Make sure you have installed and running, as it is required to compile your contract.

Generate an account

To execute transactions on-chain, you need at least one funded account. This section will guide you through the process of creating and funding an account using xiond.

Generate an Account Key Pair

To interact with the XION blockchain, you need a cryptographic key pair, consisting of a public key and a private key. The public key is used to derive your address, while the private key is required to sign transactions.

Generate a New Key Pair

Use the following command to create a new key pair and add it to your local key store:

xiond keys add <keyname>

Replace <keyname> with a name of your choice for easy reference.

Retrieve Public Key

If you have already generated a key pair and need to retrieve its public key, use the following command:

xiond keys show <keyname>

Replace <keyname> with the name of your key to display its public key.

Fund Your Account

Your account is not fully registered on-chain until it is involved in a transaction. The easiest way to achieve this is by funding your account, which allows it to interact with the network.

Request Testnet Tokens

You can obtain testnet tokens through one of the following methods:

  • Discord Faucet: Request tokens by using the faucet bot in the XION Discord.

Mainnet Tokens

If you’re deploying contracts on XION Mainnet, you can acquire XION tokens through various decentralized and centralized exchanges.

Compile and Optimize the Contract

  • Set an initial counter value

  • Increment or reset the counter

  • Query the current counter value

Clone the Repository

First, you need to download the contract code that will be compiled. Use the following commands to clone the repository and navigate into the project directory:

git clone https://github.com/burnt-labs/cw-counter
cd cw-counter

Optimize Contract

docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/optimizer:0.16.0

Why Optimization is Important

Optimization ensures that the contract is compiled into a compact WebAssembly (WASM) binary, reducing on-chain storage costs and improving execution performance.

Once compiled, the optimized contract will be available at

./artifacts/cw_counter.wasm

Upload Optimized Contract On-chain

First, set your wallet address or key name by executing the following in your terminal:

WALLET="your-wallet-address-or-key-name-here"

Now, upload the contract to the blockchain:

RES=$(xiond tx wasm store ./artifacts/cw_counter.wasm \
      --chain-id xion-testnet-2 \
      --gas-adjustment 1.3 \
      --gas-prices 0.001uxion \
      --gas auto \
      -y --output json \
      --node https://rpc.xion-testnet-2.burnt.com:443 \
      --from $WALLET)

After running the command, extract the transaction hash by executing:

echo $RES

Example output:

{
  "height": "0",
  "txhash": "B557242F3BBF2E68D228EBF6A792C3C617C8C8C984440405A578FBBB8A385035",
  ...
}

Copy the txhash value for the next step.

Retrieve the Code ID

The Code ID is required for creating an instance of your contract.

Set your transaction hash you retrieved above by executing:

TXHASH="your-txhash-here"

Query the blockchain to get the Code ID:

CODE_ID=$(xiond query tx $TXHASH \
  --node https://rpc.xion-testnet-2.burnt.com:443 \
  --output json | jq -r '.events[-1].attributes[1].value')

Now, display the retrieved Code ID:

echo $CODE_ID

Example output:

1213

Instantiate the Contract

When you uploaded the WASM bytecode, you stored the contract code on the blockchain, but no contract instance was created yet. The uploaded code can be used to create multiple contract instances, each with its own state.

Set the contract's initialization message by executing:

MSG='{ "count": 1 }'

Instantiate the contract with the Code ID from the previous step:

xiond tx wasm instantiate $CODE_ID "$MSG" \
  --from $WALLET \
  --label "cw-counter" \
  --gas-prices 0.025uxion \
  --gas auto \
  --gas-adjustment 1.3 \
  -y --no-admin \
  --chain-id xion-testnet-2 \
  --node https://rpc.xion-testnet-2.burnt.com:443

Example output:

gas estimate: 217976
code: 0
txhash: 09D48FE11BE8D8BD4FCE11D236D80D180E7ED7707186B1659F5BADC4EC116F30

Copy the new transaction hash for the next step.

Retrieve the Contract Address

Once a contract instance is created, it is assigned a unique contract address and its own data store, which can be queried for state changes and interactions.

Set the new transaction hash from the contract instantiation step:

TXHASH="your-txhash-here"

Query the blockchain to get the contract address:

CONTRACT=$(xiond query tx $TXHASH \
  --node https://rpc.xion-testnet-2.burnt.com:443 \
  --output json | jq -r '.events[] | select(.type == "instantiate") | .attributes[] | select(.key == "_contract_address") | .value')

Display the contract address:

echo $CONTRACT

Example output:

xion1v6476wrjmw8fhsh20rl4h6jadeh5sdvlhrt8jyk2szrl3pdj4musyxj6gl

Querying the Contract

The deployed contract includes a method to query the current count value. You can use this to retrieve the stored state.

To fetch the current count, use the following:

QUERY='{"get_count":{}}'

Run the following command to send the query request to the contract:

xiond query wasm contract-state smart $CONTRACT "$QUERY" --output json --node https://rpc.xion-testnet-2.burnt.com:443

This command will return the current count value stored in the contract.

Execute Transactions

Now that the contract is deployed, let’s interact with it by executing transactions. The contract supports two transaction types:

  1. Increment the count → Increases the counter by +1

  2. Reset the count → Sets the counter to a specified value

Since these transactions modify the contract’s internal state, gas fees are required for execution.

Increment the Contract’s Count

Let’s send a transaction to increase the count by 1:

TRY_INCREMENT='{"increment": {}}'
xiond tx wasm execute $CONTRACT "$TRY_INCREMENT" \
  --from $WALLET \
  --gas-prices 0.025uxion \
  --gas auto \
  --gas-adjustment 1.3 \
  -y \
  --node https://rpc.xion-testnet-2.burnt.com:443 \
  --chain-id xion-testnet-2

After executing this transaction, you can run the get_count query again to verify that the count has increased by +1 from its previous value.

Reset the Contract’s Count

Now, let’s send a transaction to reset the count to a specific value (e.g., 0):

RESET='{"reset": {"count": 0}}'
xiond tx wasm execute $CONTRACT "$RESET" \
  --from $WALLET \
  --gas-prices 0.025uxion \
  --gas auto \
  --gas-adjustment 1.3 \
  -y \
  --node https://rpc.xion-testnet-2.burnt.com:443 \
  --chain-id xion-testnet-2

Like the increment transaction, the reset transaction also modifies the contract’s state and requires gas fees.

After querying the contract again you will confirm that the count has been reset to 0 or the value you specified.

Faucet Web Page: Visit the and follow the instructions to receive testnet tokens.

For more details on accessing testnet tokens, see our .

To demonstrate the deployment process, we’ll use a simple smart contract. This contract provides an example of state management on the XION blockchain. It allows you to:

Next, compile and optimize the smart contract using the . You need to have Docker running to execute the command below:

Each contract instance requires a unique initialization message based on its expected parameters. In the case of the , the only required variable is count, which is of type i32 (a 32-bit integer).

installation and setup guide
installation instructions
Docker
XION Faucet
Faucet Page
Counter
CosmWasm Optimizing Compiler
Counter contract