Simple Template
Create
Clone and create the project form the git repository as shown below:
cargo generate --git https://github.com/CosmWasm/cw-template.git --branch 1.0-minimal --name PROJECT_NAME
Build
Go to the project folder and build the project with Cargo.
cargo wasm
To compile a much smaller bytecode, you can use the following command which tells the compiler to strip the unused parts of the code:
RUSTFLAGS='-C link-arg=-s' cargo wasm
The compilation should output a .wasm
file having the same name with project in the following folder: target/wasm32-unknown-unknown/release/
.
PROJECT_NAME.wasm
Deploy
Upload contract source code to chain
The file we generated in the previous instruction is a wasm binary executable. To deploy, we need to upload the code to the blockchain. To do that you need to use a store
transaction type. Once that is done you can download the bytecode and verify it.
Store the contract bytecode in the blockchain with the following command:
RES=$(wasmd tx wasm store target/wasm32-unknown-unknown/release/PROJECT_NAME.wasm --from wallet $TXFLAG -y --output json -b block)
wallet
is your aurad wallet name you want to use to send the transaction.PROJECT_NAME
is the name you gave to the project.
You can see the list of Codes that have been uploaded to the blockchain.
wasmd query wasm list-code $NOD
Display the response of the transaction:
echo $RES
The response contains a Code Id of the uploaded wasm binary. The following is an easier way to get the Code Id from the response:
CODE_ID=$(echo $RES | jq -r '.logs[0].events[-1].attributes[0].value')
echo $CODE_ID
Let's see the list of contracts instantiated using the Code Id above.
wasmd query wasm list-contract-by-code $CODE_ID $NODE --output json
The response should be an empty list as we have not instantiated any contract yet.
{"contracts":[],"pagination":{"next_key":null,"total":"0"}}
Before we instantiate a contract with the Code Id and interact with it, let us check if the code stored on the blockchain is indeed the PROJECT_NAME.wasm binary we uploaded.
# Download the wasm binary from the chain and compare it to the original one
wasmd query wasm code $CODE_ID $NODE download.wasm
# The two binaries should be identical
diff target/wasm32-unknown-unknown/release/PROJECT_NAME.wasm download.wasm
Instantiating the Contract
We can now create an instance of the wasm contract. Following the instantiation, we can make queries and this time receive non-empty responses.
# Prepare the instantiation message
INIT='{"purchase_price":{"amount":"100","denom":"umlg"},"transfer_price":{"amount":"999","denom":"umlg"}}'
# Instantiate the contract
wasmd tx wasm instantiate $CODE_ID "$INIT" --from wallet --label "a project label" $TXFLAG -y --no-admin
# Check the contract details and account balance
wasmd query wasm list-contract-by-code $CODE_ID $NODE --output json
CONTRACT=$(wasmd query wasm list-contract-by-code $CODE_ID $NODE --output json | jq -r '.contracts[-1]')
echo $CONTRACT
# See the contract details
wasmd query wasm contract $CONTRACT $NODE
# Check the contract balance
wasmd query bank balances $CONTRACT $NODE
# Upon instantiation the cw_nameservice contract will store the instatiation message data in the contract's storage with the storage key "config".
# Query the entire contract state
wasmd query wasm contract-state all $CONTRACT $NODE