CosmWasm Contract
This tutorial covers building, publishing and interacting with a CosmWasm contract on a Wasm Minitia.
Note: CosmWasm contracts are only supported on Minitias that are using WasmVM.
Step 0: Clone cw-contracts
In this tutorial, we will use nameservice
module of cw-contracts
repository.
git clone https://github.com/deus-labs/cw-contracts.git
cd cw-contracts
git checkout main
cd contracts/nameservice
Step 1: Compile a contract
By using the command below, a Wasm binary to be published on Wasm Minitia is compiled.
RUSTFLAGS='-C link-arg=-s' cargo wasm
When compiling is completed, target/wasm32-unknown-unknown/release/cw_nameservice.wasm
file will be generated.
Step 1-1: Compile with rust-optimizer (Advanced)
Wasm binary file must be as small as possible in order to reduce gas costs for both the deployment and for every interaction with the deployed contract. The rust-optimizer is a tool which can be used to make the binary size as small as possible while ensuring that all contract features are still working.
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.11
By running the above command, the binary file will be generated within artifacts/cw_nameservice.wasm
.
Step 2: Store a contract
Let's store cw_nameservice.wasm
contract created in the previous step onto the Minitia.
> minitiad tx wasm store [binary-path] \
--from test-account \
--gas auto --gas-adjustment 1.5 \
--gas-prices [gas-price] \
--node [rpc-url]:[rpc-port] \
--chain-id [chain-id]
Step 3: Instantiate a contract
In this step, we will instantiate a new contract using the stored code in the previous step.
First, codeId
is required in order to instantiate the stored cw_nameservice.wasm
contract. codeID
can be fetched with the command line below.
> RES=$(minitiad q tx --type=hash [txhash] --node [rpc-url]:[rpc-port] --output json)
CODE_ID=$(echo $RES | jq -r '.events[-1].attributes[1].value')
echo $CODE_ID
Let's instantiate a new wasm contract using the stored code.
> INIT='{"purchase_price":{"amount":"100","denom":"l2/2588fd87a8e081f6a557f43ff14f05dddf5e34cb27afcefd6eaf81f1daea30d0"},"transfer_price":{"amount":"100","denom":"l2/2588fd87a8e081f6a557f43ff14f05dddf5e34cb27afcefd6eaf81f1daea30d0"}}'
minitiad tx wasm instantiate 1 "$INIT" --label=nameservice --no-admin \
--from test-account \
--gas auto --gas-adjustment 1.5 \
--gas-prices [gas-price] \
--node [rpc-url]:[rpc-port] \
--chain-id [chain-id]
Step 4: Interact with a contract
We are now ready to interact with the instantiated CosmWasm contract.
Contract address is required in order to interact with the instantiated contract. The contract address can be found by using the below command:
> RES=$(minitiad q tx --type=hash [txhash] --node [rpc-url]:[rpc-port] --output json)
CONTRACT_ADDRESS=$(echo $RES | jq -r '.events[-1].attributes[0].value')
echo $CONTRACT_ADDRESS
Execute a contract function
In this example, we will use the register
function from the published contract, to register test
as a name service domain.
> REGISTER='{"register":{"name":"test"}}'
minitiad tx wasm execute [contract_address] "$REGISTER" --amount 100[denom] \
--from test-account \
--gas auto --gas-adjustment 1.5 \
--gas-prices [gas-price] \
--node [rpc-url]:[rpc-port] \
--chain-id [chain-id]
Query Results
Once the register
function is successfully executed, we can confirm the name that was registered through a contract query.
> NAME_QUERY='{"resolve_record": {"name": "test"}}'
minitiad query wasm contract-state smart [contract_address] "$NAME_QUERY" --node [rpc-url]:[rpc-port]
# data:
# address: init150748hf2e3mjed5w0xqkp9wn8xs55nnzneg452
Last updated