# Counter smart contract tutorial

## CosmWasm

CosmWasm is a library for writing Rust smart contracts. Gameluk supports CosmWasm 1.0.0+ smart contracts.

Dependencies

Following the instructions in the [CosmWasm github](https://github.com/CosmWasm/cw-template) to install cargo-generate and cargo-run-script

## Create a boilerplate smart contract

Go to a folder in which you want to create a boilerplate smart contract and run

```
cargo generate --git https://github.com/CosmWasm/cw-template.git --branch 1.0 --name counter -d minimal=false
```

{% hint style="info" %}
If you encounter an error saying `` `no such command: generate` `` please make sure you have `cargo-generate` installed

```rust
cargo install cargo-generate
```

{% endhint %}

This will create a repository with the following structure

```
.
├── Cargo.toml
├── LICENSE
├── NOTICE
├── README.md
└── src
    ├── bin
    │   └── schema.rs
    ├── contract.rs
    ├── error.rs
    ├── helpers.rs
    ├── lib.rs
    ├── msg.rs
    └── state.rs
```

This contract defines a simple counter app. It supports 4 basic functions:&#x20;

* 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 ](https://github.com/CosmWasm/cw-template/blob/main/Developing.md)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.

**x86 64-bits (Intel/AMD):**

```
 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.14.0
```

**ARM 64-bits (M1/M2 Macs):**

```
 docker run --rm -v "$(pwd)":/code \
  --mount type=volume,source="$(basename "$(pwd)")_cache",target=/target \
  --mount type=volume,source=registry_cache,target=/usr/local/cargo/registry \
  cosmwasm/rust-optimizer-arm64:0.14.0
```

Then create a new file called **deploy.sh**

```
.
├── Cargo.lock
├── Cargo.toml
├── Developing.md
├── Importing.md
├── LICENSE
├── NOTICE
├── Publishing.md
├── README.md
├── artifacts
│   ├── checksums.txt
│   ├── checksums_intermediate.txt
│   └── counter.wasm
├── deploy.sh
├── src
│   ├── bin
│   │   └── schema.rs
│   ├── contract.rs
│   ├── error.rs
│   ├── helpers.rs
│   ├── integration_tests.rs
│   ├── lib.rs
│   ├── msg.rs
│   └── state.rs
└── target
    ├── ...
```

Paste the following code into **deploy.sh**

```
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
```

{% hint style="info" %}
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
{% endhint %}

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
```
