Deploying Your First Smart Contract on XION
Last updated
Was this helpful?
Last updated
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.
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.
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
.
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.
Use the following command to create a new key pair and add it to your local key store:
Replace <keyname>
with a name of your choice for easy reference.
If you have already generated a key pair and need to retrieve its public key, use the following command:
Replace <keyname>
with the name of your key to display its public key.
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.
You can obtain testnet tokens through one of the following methods:
Discord Faucet: Request tokens by using the faucet bot in the XION Discord.
If you’re deploying contracts on XION Mainnet, you can acquire XION tokens through various decentralized and centralized exchanges.
Set an initial counter value
Increment or reset the counter
Query the current counter value
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:
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
First, set your wallet address or key name by executing the following in your terminal:
Now, upload the contract to the blockchain:
After running the command, extract the transaction hash by executing:
Example output:
Copy the txhash value for the next step.
The Code ID is required for creating an instance of your contract.
Set your transaction hash you retrieved above by executing:
Query the blockchain to get the Code ID:
Now, display the retrieved Code ID:
Example output:
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:
Instantiate the contract with the Code ID from the previous step:
Example output:
Copy the new transaction hash for the next step.
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:
Query the blockchain to get the contract address:
Display the contract address:
Example output:
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:
Run the following command to send the query request to the contract:
This command will return the current count value stored in the contract.
Now that the contract is deployed, let’s interact with it by executing transactions. The contract supports two transaction types:
Increment the count → Increases the counter by +1
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.
Let’s send a transaction to increase the count by 1:
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.
Now, let’s send a transaction to reset the count to a specific value (e.g., 0
):
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).