Start your first wasm contract

Install RIZON

Supported Systems

We currently supports the operating systems below.
  • Ubuntu 18.04 or later
  • MacOS 10.14 or later

Prerequisites

You should install the packages below before you build the source.

Build

git clone the source and change directory.
git clone https://github.com/rizon-world/rizon.git
cd rizon
git checkout features/wasm_1_2_x
Make it.
make install
The built binary - rizond - will be located in your $GOBIN.
Check the version of built binary.
rizond version
The version is
v0.4.1-3-g916ef87

Deploy own network

Please refer to Deploy own network

Write wasm contract

Don't forget to install Rust and Docker before writing contract.

Setup Rust

Setup Rust toolchain and add the wasm32-unknown-unknown compilation target.
rustup default stable
rustup target add wasm32-unknown-unknown
After that, install cargo-run-script and cargo-generate.
cargo install cargo-run-script
cargo install cargo-generate

Draft template

To launch your smart contract easily with the recommended folder structure and build options using a template.
cargo generate --git https://github.com/CosmWasm/cw-template.git --branch 1.0 --name my-first-contract
cd my-first-contract
This helps get you started by providing the basic boilerplate and structure for a smart contract. You’ll find in the src/lib.rs file that the standard CosmWasm entrypoints instantiate(), execute(), and query() are properly exposed and hooked up.

Build contract

To build your contract, run the following command, first.
cargo wasm
Next, you need to make sure the output WASM binary is as small as possible in order to minimize fees and stay under the size limit for the blockchain.
cargo run-script optimize
If you use arm64 architecture, run this command instead of the above.
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-arm64:0.12.6
This will result in an optimized build of artifacts/my_first_contract.wasm .

Build schemas

You can then build the schemas.
cargo schema
Then your schemas should be in your schema/ directory.

Execute contract

Upload code

rizond tx wasm store artifacts/my_first_contract.wasm --from my_key --keyring-backend test --chain-id=my_testnet --gas=auto --gas-adjustment=1.25 --broadcast-mode=block
...
confirm transaction before signing and broadcasting [y/N]: y
code: 0
codespace: ""
data: 0A460A1E2F636F736D7761736D2E7761736D2E76312E4D736753746F7265436F646512240801122010A27E086B148D7C16A37C08174EF718668F76CBDBDD3A664C0C6C947D45FD3C
events:
- attributes:
- index: true
key: ZmVl
value: ""
- index: true
key: ZmVlX3BheWVy
value: cml6b24xd2hqcjlmZjhxM2F1aDZmbmVsOHdzN2ZxNmdjZXhmZnAyYWF1NTM=
type: tx
- attributes:
- index: true
key: YWNjX3NlcQ==
value: cml6b24xd2hqcjlmZjhxM2F1aDZmbmVsOHdzN2ZxNmdjZXhmZnAyYWF1NTMvMQ==
type: tx
- attributes:
- index: true
key: c2lnbmF0dXJl
value: SGFLTzZCanZTMmg4NkFieWw4L0Q0VGlzb21uT1dBYWFNNmRudnNTc1FkZFhMZ0d0VEdwaHZYVU9Tb25JREZTbmtBaGdIU045QWVzVFRJUklaUjdJeGc9PQ==
type: tx
- attributes:
- index: true
key: YWN0aW9u
value: L2Nvc213YXNtLndhc20udjEuTXNnU3RvcmVDb2Rl
type: message
- attributes:
- index: true
key: bW9kdWxl
value: d2FzbQ==
- index: true
key: c2VuZGVy
value: cml6b24xd2hqcjlmZjhxM2F1aDZmbmVsOHdzN2ZxNmdjZXhmZnAyYWF1NTM=
type: message
- attributes:
- index: true
key: Y29kZV9jaGVja3N1bQ==
value: MTBhMjdlMDg2YjE0OGQ3YzE2YTM3YzA4MTc0ZWY3MTg2NjhmNzZjYmRiZGQzYTY2NGMwYzZjOTQ3ZDQ1ZmQzYw==
- index: true
key: Y29kZV9pZA==
value: MQ==
type: store_code
gas_used: "955313"
gas_wanted: "1192301"
height: "551"
info: ""
logs:
- events:
- attributes:
- key: action
value: /cosmwasm.wasm.v1.MsgStoreCode
- key: module
value: wasm
- key: sender
value: rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53
type: message
- attributes:
- key: code_checksum
value: 10a27e086b148d7c16a37c08174ef718668f76cbdbdd3a664c0c6c947d45fd3c
- key: code_id
value: "1"
type: store_code
log: ""
msg_index: 0
raw_log: '[{"events":[{"type":"message","attributes":[{"key":"action","value":"/cosmwasm.wasm.v1.MsgStoreCode"},{"key":"module","value":"wasm"},{"key":"sender","value":"rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53"}]},{"type":"store_code","attributes":[{"key":"code_checksum","value":"10a27e086b148d7c16a37c08174ef718668f76cbdbdd3a664c0c6c947d45fd3c"},{"key":"code_id","value":"1"}]}]}]'
timestamp: ""
tx: null
txhash: 537A7CE1AF87EFC61290C9A6E1BC6F86051EA4BC6072B6FA40BA460B8E8F6408
Now you can see your contract has been successfully instantiated with code_id 1.
You can check it.
rizond query wasm code-info 1
code_id: "1"
creator: rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53
data_hash: 10A27E086B148D7C16A37C08174EF718668F76CBDBDD3A664C0C6C947D45FD3C
instantiate_permission:
address: ""
addresses: []
permission: Everybody

Create contract

You have now uploaded the code for your contract, but still don’t have a contract.
Create it with the following command.
rizond tx wasm instantiate 1 '{"count":0}' --label testcode1 --admin $(rizond keys show my_key -a --keyring-backend test) --from my_key --keyring-backend test --chain-id=my_testnet --gas=auto --gas-adjustment=1.25 --broadcast-mode=block
Then you should get a response like
confirm transaction before signing and broadcasting [y/N]: y
code: 0
codespace: ""
data: 0A6E0A282F636F736D7761736D2E7761736D2E76312E4D7367496E7374616E7469617465436F6E747261637412420A4072697A6F6E3134686A32746176713866706573647778786375343472747933686839307668756A7276636D73746C347A723374786D6676773973357776306C73
events:
- attributes:
- index: true
key: ZmVl
value: ""
- index: true
key: ZmVlX3BheWVy
value: cml6b24xd2hqcjlmZjhxM2F1aDZmbmVsOHdzN2ZxNmdjZXhmZnAyYWF1NTM=
type: tx
- attributes:
- index: true
key: YWNjX3NlcQ==
value: cml6b24xd2hqcjlmZjhxM2F1aDZmbmVsOHdzN2ZxNmdjZXhmZnAyYWF1NTMvMg==
type: tx
- attributes:
- index: true
key: c2lnbmF0dXJl
value: Q24zeEUxUTNmY0xUOHJ1Qnd6M29hdGJpeVZTTS9KTFlWQWlQV29SZ0IxRVZVWmw1QVRhbXNFb2N6cndveklySFc5Wmc5Q1F5SGMwajlLU0NkYVZPQWc9PQ==
type: tx
- attributes:
- index: true
key: YWN0aW9u
value: L2Nvc213YXNtLndhc20udjEuTXNnSW5zdGFudGlhdGVDb250cmFjdA==
type: message
- attributes:
- index: true
key: bW9kdWxl
value: d2FzbQ==
- index: true
key: c2VuZGVy
value: cml6b24xd2hqcjlmZjhxM2F1aDZmbmVsOHdzN2ZxNmdjZXhmZnAyYWF1NTM=
type: message
- attributes:
- index: true
key: X2NvbnRyYWN0X2FkZHJlc3M=
value: cml6b24xNGhqMnRhdnE4ZnBlc2R3eHhjdTQ0cnR5M2hoOTB2aHVqcnZjbXN0bDR6cjN0eG1mdnc5czV3djBscw==
- index: true
key: Y29kZV9pZA==
value: MQ==
type: instantiate
- attributes:
- index: true
key: X2NvbnRyYWN0X2FkZHJlc3M=
value: cml6b24xNGhqMnRhdnE4ZnBlc2R3eHhjdTQ0cnR5M2hoOTB2aHVqcnZjbXN0bDR6cjN0eG1mdnc5czV3djBscw==
- index: true
key: bWV0aG9k
value: aW5zdGFudGlhdGU=
- index: true
key: b3duZXI=
value: cml6b24xd2hqcjlmZjhxM2F1aDZmbmVsOHdzN2ZxNmdjZXhmZnAyYWF1NTM=
- index: true
key: Y291bnQ=
value: MA==
type: wasm
gas_used: "146741"
gas_wanted: "181585"
height: "633"
info: ""
logs:
- events:
- attributes:
- key: _contract_address
value: rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls
- key: code_id
value: "1"
type: instantiate
- attributes:
- key: action
value: /cosmwasm.wasm.v1.MsgInstantiateContract
- key: module
value: wasm
- key: sender
value: rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53
type: message
- attributes:
- key: _contract_address
value: rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls
- key: method
value: instantiate
- key: owner
value: rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53
- key: count
value: "0"
type: wasm
log: ""
msg_index: 0
raw_log: '[{"events":[{"type":"instantiate","attributes":[{"key":"_contract_address","value":"rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls"},{"key":"code_id","value":"1"}]},{"type":"message","attributes":[{"key":"action","value":"/cosmwasm.wasm.v1.MsgInstantiateContract"},{"key":"module","value":"wasm"},{"key":"sender","value":"rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53"}]},{"type":"wasm","attributes":[{"key":"_contract_address","value":"rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls"},{"key":"method","value":"instantiate"},{"key":"owner","value":"rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53"},{"key":"count","value":"0"}]}]}]'
timestamp: ""
tx: null
txhash: DD9690F379963568CEF5B57D2AF2FBFF46C8239C259B9A40C59094D3A8FC4ECA
Now you can see the contract address from the result.
Check your contract information through it.
rizond query wasm contract rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls
address: rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls
contract_info:
admin: rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53
code_id: "1"
created:
block_height: "256"
tx_index: "0"
creator: rizon1whjr9ff8q3auh6fnel8ws7fq6gcexffp2aau53
extension: null
ibc_port_id: ""
label: testcode1

Execute contract

Query

First, query the initial value of count to check it is stored well.
rizond query wasm contract-state smart rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls '{"get_count":{}}'
Then you can see the result
data:
count: 0
You initialized count as '{"count":0}' so this is correct, good.
Reset and increment
Next reset the count value to 3.
rizond tx wasm execute rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls '{"reset":{"count":3}}' --from my_key --keyring-backend test --chain-id=my_testnet --gas=auto --gas-adjustment=1.25 --broadcast-mode=block
And increment it once.
rizond tx wasm execute rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls '{"increment":{}}' --from my_key --keyring-backend test --chain-id=my_testnet --gas=auto --gas-adjustment=1.25 --broadcast-mode=block
Now you should have count as 4, let's make sure the result of your execution.
rizond query wasm contract-state smart rizon14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9s5wv0ls '{"get_count":{}}'
The expected result is
data:
count: 4
Do you get the correct count? Great!
Congratulations! You have created your first wasm contract.
If you want to know next level, try to read the official cosmwasm document.