Deploying a CosmWasm smart contract on XION Testnet: a step-by-step guide

Nina Drokina
3 min readFeb 1, 2024

--

I am Nina, and in this article, I’ll guide you through the process of deploying a CosmWasm smart contract on the XION Testnet. CosmWasm, built on the Cosmos blockchain, provides a robust platform for developing and deploying smart contracts. Whether you’re a seasoned developer or just starting, this step-by-step guide will help you navigate the deployment process on the XION Testnet. Let’s dive into the exciting world of smart contract development and deployment.

Advantages of deploying a CosmWasm smart contract on XION Testnet

  1. Cost-effective testing: the XION Testnet provides a cost-effective environment for testing CosmWasm smart contracts, allowing developers to experiment without real financial implications.
  2. Realistic simulation: XION Testnet closely simulates the Mainnet, offering developers an accurate testing ground for assessing contract behavior and performance.
  3. Community support: developers benefit from the XION Testnet community, gaining access to valuable resources, support, and insights from experienced users and team members.
  4. Rapid prototyping: XION Testnet enables rapid prototyping and iteration, allowing developers to quickly deploy, test, and refine CosmWasm smart contracts for faster innovation.
  5. Security and issue prevention: testing on XION Testnet helps identify and address security vulnerabilities early on, minimizing the risk of issues when deploying contracts on the Mainnet.

A step-by-step guide to deploying a CosmWasm smart contract on XION Testnet:

1. Generate an Account: to interact with the XION Testnet, you need an account. Follow these steps to generate one:

  • Use the Cosmos SDK’s cryptographic tools to generate a key pair:
xiond keys add <keyname>
  • Retrieve the public key:
xiond keys show <keyname> -p
  • Request Testnet tokens from the team or on Discord to fund your account.

2. Deploying contracts: deploying a CosmWasm smart contract involves using the CosmWasm CLI. Use the following command:

xiond tx wasm store ./contract.wasm \
--chain-id xion-local-testnet-1 \
--gas-adjustment 1.3 \
--gas-prices 0uxion \
--gas auto \
--chain-id xion-testnet-1 \
--node https://rpc.xion-testnet-1.burnt.com:443 \
--from <sender_address>

Replace <sender_address>, <chain_id>, <gas_prices>, and <testnet_rpc_url> with your specific values.

3. Interacting with the Deployed contract: now that your contract is deployed, you can interact with it using the CosmWasm CLI. For example, to execute a contract message:

xiond tx wasm execute <contract_address> '{"set": {"new_value": 42}}' --from <sender_address> --chain-id <chain_id> --gas auto --gas-prices <gas_prices> --node <testnet_rpc_url>

Replace <contract_address>, <sender_address>, <chain_id>, <gas_prices>, and <testnet_rpc_url> accordingly.

4. Confirm contract deployment and derive code IDs

To deploy smart contracts, compile the code into an executable wasm binary file. Ensure optimal gas cost by following these steps:

  • Compile using the command:
RUSTFLAGS='-C link-arg=-s' cargo wasm
  • Optimize the binary size:
sudo docker run --rm -v "$(pwd)":/code \   --mount type=volume,source="$(basename "$(pwd)")_cache",target=/code/target \   --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \   cosmwasm/rust-optimizer:0.12.6
  • Store the optimized code on the XION Testnet:
xiond tx wasm store artifacts/my_first_contract.wasm --from wallet --gas-prices 0.1uxion --gas auto --gas-adjustment 1.3 -y --output json -b block
  • Retrieve the CODE_ID using jq.

5. Instantiate the contract: create an instance of the wasm contract:

INIT='{"count":100}'
xiond tx wasm instantiate $CODE_ID "$INIT" \
--from wallet --label "my first contract" --gas-prices 0.025uxion --gas auto --gas-adjustment 1.3 -b block -y --no-admin

6. Execute the contract: interact with the deployed contract:

  • Get contract’s count:
QUERY='{"get_count":{}}' xiond query wasm contract-state smart $CONTRACT_ADDR "$QUERY" --output json
  • Increment contract’s count:
TRY_INCREMENT='{"increment": {}}' xiond tx wasm execute $CONTRACT_ADDR "$TRY_INCREMENT" --from wallet --gas-prices 0.025uxion --gas auto --gas-adjustment 1.3 -y
  • Reset contract’s count:
RESET='{"reset": {"count": 0}}' xiond tx wasm execute $CONTRACT_ADDR "$RESET" --from wallet --gas-prices 0.025uxion --gas auto --gas-adjustment 1.3 -y

Now you have successfully deployed, instantiated, and interacted with a CosmWasm smart contract on the XION Testnet.

Learn more:

Conclusion. In conclusion, deploying CosmWasm smart contracts on the XION Testnet offers a cost-effective and realistic testing environment. The active community support aids developers in rapid prototyping and iteration, while early identification of security vulnerabilities ensures a smoother transition to the Mainnet. Embracing the XION Testnet’s advantages facilitates collaborative development and provides a platform for integration testing. By leveraging these benefits, developers can confidently deploy secure and innovative CosmWasm smart contracts. Happy coding!

How to track XION?

Prepared by niniao

--

--

Nina Drokina
Nina Drokina

Written by Nina Drokina

Dynamics 365 Practice Lead | PhD in Marketing & Doctor of Economic Sciences

No responses yet