Deploy a CosmWasm Smart Contract
Generate an account
To generate an account on a Cosmos-based testnet, ours being XION, a user would typically use the Cosmos SDK and its associated libraries to interact with the blockchain. Here's a general outline of the steps you would take to generate an account.
It is important to have all prerequisites and dependencies installed and some knowledge known on them. You may find this information located inside the installation tab.
Generate Account Key Pair:
Use the Cosmos SDK's cryptographic tools to generate a key pair for your account. This key pair will include a public key and a private key.
To generate a key and add it to your local keybase:
You can get the public key of a key pair that you have previously generated by using the show command of the cosmos keys CLI. Please replace with the name of your key.
Create an Account on the Blockchain:
Use the generated key pair to create an account on the network. This involves sending a transaction to the network's blockchain to register your account. The simplest way to do this is to request testnet tokens from a member of the team or in discord.
Deploying Contracts
Use the CosmWasm CLI to deploy the created contract to the testnet. The specific command and options may vary depending depending on version.
xiond tx wasm store contract.tar.gz --from <sender_address> --chain-id <chain_id> --gas auto --gas-prices <gas_prices> --node <testnet_rpc_url>
Replace <sender_address>, <chain_id>, <gas_prices>
, and <testnet_rpc_url>
with the appropriate values for your testnet environment. These can be found in the Network section.
or use the populated version here:
Our testnet allows for zero gas price but will not be the case with our mainnet.
Where to find appropriate values?
<sender_address>
:
This is the address of your account on the testnet. If you don't have one, you'll need to create an account on the XION's testnet and obtain the address. More information on how to generate a key pair and the associated address can be found here.
<chain_id>
:
The chain ID is a unique identifier for the specific blockchain network you're deploying to. You can usually find the chain ID in the XION's testnet's documentation or information.
<gas_prices>:
Gas prices are typically denominated in the native token of the blockchain. You may need to refer to the testnet's documentation or forums to find the appropriate gas prices.
<testnet_rpc_url>:
You'll need this URL to submit transactions and query data. Testnets often provide RPC endpoints that you can use. These can be found in the XION's testnet's documentation or developer resources.
Remember that these values are specific to the XION testnet you''ll be deploying to.
Interacting with the Deployed Contract
Now that the contract is deployed, the user can interact with it using the CosmWasm CLI or other tools. For example, to execute a contract message, you can use a command such as the following.
Replace <contract_address>, <sender_address>, <chain_id>, <gas_prices>
, and <testnet_rpc_url>
with the appropriate values.
Confirm Contract deployment and Derive code IDs
Confirm Contract deployment
To deploy smart contracts, you must compile the code and make it an executable wasm binary file. We will compile the wasm contract with stable toolchain.
Compile using the command below:
toolchain
means the compiler of Rust, and rust has three release channels:stable
,beta
, andnightly
, of which thestable
channel is the most recently released version.rustup
helps you manage these different versions easily.cargo
is the Rust package manager. In the .cargo/config, you can view the wasm compilation options as follows:
So when we run the
cargo wasm
command, thecargo build --release —-target wasm32-unknown-unknown
command is executed according to the option in the config file above.
3. This produces a file about 155K
. To reduce gas costs, the binary size should be as small as possible. This will result in a less costly deployment, and lower fees on every interaction.
Optimized Compilation
You can do further optimization using rust-optimizer. rust-optimizer produces reproducible builds of CosmWasm smart contracts and does heavy optimization on the build size, using binary stripping and wasm-opt
.
Binary file will be at artifacts/my_first_contract.wasm
folder and its size will be about 130K
, which is more smaller than when only RUTFLAGS was used.
Store to Xion Testnet chain
We have the wasm binary executable ready. Now it is time to store the code to the Xion Testnet blockchain.
xiond tx wasm store
: upload a wasm binary--from
: name or address of private key with which to sign.--gas-prices
: gas prices in decimal format to determine the transaction fee.--gas
: gas limit to set per-transaction. set to "auto
" to calculate sufficient gas automatically--gas-adjustment
: adjustment factor to be multiplied against the estimate returned by the tx simulation.-y
: to skip tx broadcasting prompt confirmation.--output
: output format.-b
: transaction broadcasting mode
Once that is complete, you can get the CODE_ID
easily using jq
.
jq
is an open source that helps extract data from JSON. Install it according to your OS using the following command:
If you are using an operating system other than the one listed above, please refer to the official document.
Set the $RES Variable to Parse Output
The user can directly set the output of the xiond keys add myaccount command to a variable, and then use string manipulation to extract the address.
Here's an example:
In this example, we're using the jq tool to parse the JSON output and extract the address.
Make sure to have jq installed for this approach.
Pipe xiond Output to jq for Parsing
A user can pipe the output of xiond keys add myaccount directly into jq for parsing:
In this case, the output of xiond keys add myaccount --output json is directly passed to jq for parsing, and the address is extracted.
Run the following command to set the CODE_ID
as a variable:
CODE_ID
as a variable:Instantiate the contract
We can now create an instance of this wasm contract. First, set the initial state of the instance in the INIT
variable and run the instantiate
command.
xiond tx wasm instantiate
: instantiate a wasm contract using CODE_ID of the uploaded binary.--label
: human-readable name for this contract in lists.--no-admin
: you must set this explicitly if you don’t want an admin.
If you have succeeded in instantiating the contract, you can search for output txhash
in Xion Explorer to verify your deployment.
Get the contract address using the command following:
xiond query wasm list-contract-by-code
: list wasm all bytecode on the chain for given code id
Execute the Contract
Now, let's see if the contract we deployed works well.
Get contract’s count
Send a get_count
query to check the count value. The previously set INIT
state is output as it is.: {"data":{"count":100}}
xiond query wasm contract-state smart
: calls contract with given address with query data and prints the returned result
Increment contract’s count
This time, let's send an increment
transaction that increases the count value by +1. Because the transaction changes the internal state of the contract, you must pay gas fees.
If you run the get_count
query again after sending the increment
transaction, you can see that +1 has increased from the previous count value.
xiond tx wasm execute
: execute a command on a wasm contract
Reset contract’s count
Lastly, let’s send a reset
transaction. Like increment, reset transaction also changes the internal state of contract, so you must pay gas fees.
\
Last updated