This contract defines a simple counter app. It supports 4 basic functions:
Instantiate the counter with a starting count
Increment the counter by 1
Reset the counter
Read the current count
To run unit tests, you can run
cargo unit-test
which will give an output similar to the following
running 4 tests
test contract::tests::increment ... ok
test contract::tests::reset ... ok
test contract::tests::proper_initialization ... ok
test integration_tests::tests::count::count ... ok
test result: ok. 4 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out; finished in 0.00s
Deploy boilerplate smart contract
In order to deploy a contract, it is recommended to first compile it. Confirm that the prerequisites listed in the CosmWasm docs are met, and then run the following
cargo wasm
Before uploading, it may be advisable to consider using the rust-optimizer to potentially reduce the size of the binary that will be uploaded. Additionally, it is important to ensure that the correct version of the optimizer is being used for your specific processor architecture.
if [ -z "${contract}" ];
then contract=artifacts/counter.wasm
fi
if [ -z "${keyname}" ];
then keyname=admin
fi
if [ -z "${password}" ];
then password="12345678\n"
fi
gamed=~/go/bin/gameid
code=$(printf $password | $gameid tx wasm store $contract -y --from=$keyname --chain-id=sei-chain --gas=10000000 --fees=10000000usei --broadcast-mode=block | grep -A 1 "code_id" | sed -n 's/.*value: "//p' | sed -n 's/"//p')
printf "Code id is %s\n" $code
admin_addr=$(printf $password |$gameid keys show $keyname | grep -A 1 "address" | sed -n 's/.*address: //p')
printf "Admin addr id is %s\n" $admin_addr
addr=$(printf $password |$gameid tx wasm instantiate $code '{"count": 0}' --from $keyname --broadcast-mode=block --label "counter" --chain-id sei-chain --gas=30000000 --fees=3000000usei --admin=$admin_addr -y | grep -A 1 -m 1 "key: _contract_address" | sed -n 's/.*value: //p' | xargs)
printf "Deployed counter address is %s\n" $addr
NB: if you are using an ARM processor (M1/M2 Mac) then you will need to change the contract name to counter-aarch64.wasm in line 2 of the deploy script above
The output will be as follows
$ ./deploy.sh
Code id is 1
Admin addr id is sei1tn08ycqyk8wy7v72n0zz9l6nvk46rgj3aqmewj
Deployed counter address is sei14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sh9m79m
Increment count
Next we want to increment the counter and confirm that the state is updated
Create a new file called increment.sh with the following
if [ -z "${keyname}" ];
then keyname=admin
fi
if [ -z "${password}" ];
then password="12345678\n"
fi
seid=~/go/bin/seid
contract='' # Fill in contract address from deployment step, ex. sei14hj2tavq8fpesdwxxcu44rty3hh90vhujrvcmstl4zr3txmfvw9sh9m79m
increment_resp=$(printf $password |$seid tx wasm execute $contract '{"increment":{}}' --from $keyname --broadcast-mode=block --chain-id sei-chain --gas=30000000 --fees=3000000usei -y )
new_count=$($seid q wasm contract-state smart $contract '{"get_count":{}}' | grep -A 1 "count:" | awk -F: '/count:/{getline; print $2}')
printf "New count: %s\n" "$new_count"
and then run the script twice
$ ./increment.sh
New count: 1
$ ./increment.sh
New count: 2