Skip to main content

simple-template

Create projectโ€‹

clone the simple template project

cargo generate --git https://github.com/CosmWasm/cw-template.git --branch 1.0-minimal --name PROJECT_NAME

Build projectโ€‹

Try to build the wasm bundle using following line:

cargo wasm

To produce a much smaller version, you can run the following command which tells the compiler to strip the unused parts of the code out:

RUSTFLAGS='-C link-arg=-s' cargo wasm

The compilation should output a .wasm file having the same name with project in target/wasm32-unknown-unknown/release/

PROJECT_NAME.wasm

Deploymentโ€‹

Upload contract source code to chainโ€‹

We generated a wasm binary executable. Let's upload the code to the blockchain. Once that is complete, you can download the bytecode to verify it.

See the list of codes that was uploaded to the testnet previously:

wasmd query wasm list-code $NOD

Now let us store the bytecode on chain and acquire the Code Id. The Code Id will later be used to create an instance of the PROJECT_NAME contract:

RES=$(wasmd tx wasm store target/wasm32-unknown-unknown/release/PROJECT_NAME.wasm --from wallet $TXFLAG -y --output json -b block)

To display the response of uploading using following line:

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