Skip to content

Commit 3da117b

Browse files
authored
chore: migrate wasm/counter to icp-cli (#1437)
1 parent c9228d7 commit 3da117b

5 files changed

Lines changed: 84 additions & 34 deletions

File tree

.github/workflows/wasm_counter.yml

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
name: wasm_counter
2+
3+
on:
4+
push:
5+
branches: [master]
6+
pull_request:
7+
paths:
8+
- wasm/counter/**
9+
- .github/workflows/wasm_counter.yml
10+
11+
concurrency:
12+
group: ${{ github.workflow }}-${{ github.ref }}
13+
cancel-in-progress: true
14+
15+
jobs:
16+
wasm-counter:
17+
runs-on: ubuntu-24.04
18+
container: ghcr.io/dfinity/icp-dev-env-all:1.0.1
19+
env:
20+
ICP_CLI_GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
21+
steps:
22+
- uses: actions/checkout@34e114876b0b11c390a56381ad16ebd13914f8d5 # v4.3.1
23+
- name: Install wabt
24+
run: apt-get update && apt-get install -y wabt
25+
- name: Deploy and test
26+
working-directory: wasm/counter
27+
run: |
28+
icp network start -d
29+
icp deploy
30+
bash test.sh

wasm/counter/README.md

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,34 @@
11
# Counter canister in WebAssembly
22

3-
This example demonstrates a counter application, written in WebAssembly directly. The compiled Wasm module is only 389 bytes.
3+
This example demonstrates a counter application written directly in WebAssembly Text Format (WAT). The compiled Wasm module is only 389 bytes.
44

5-
It can be a good way to learn and experiment with [the IC system API](https://github.com/dfinity-lab/ic-ref/blob/0.17.0/spec/index.adoc#canister-interface-system-api).
5+
It can be a good way to learn and experiment with the [IC System API](https://docs.internetcomputer.org/references/ic-interface-spec/canister-interface/#system-api).
66

77
## Prerequisites
88

9-
Install the `wat2wasm` tool, which is part of [WABT](https://github.com/WebAssembly/wabt).
10-
For example, on Mac, you can run the following command:
9+
- [icp-cli](https://cli.internetcomputer.org/): `npm install -g @icp-sdk/icp-cli @icp-sdk/ic-wasm`
10+
- [WABT](https://github.com/WebAssembly/wabt) (for `wat2wasm`): `brew install wabt`
1111

12-
```
13-
$ brew install wabt
12+
## Deploy and test
13+
14+
```bash
15+
icp network start -d
16+
icp deploy
17+
bash test.sh
18+
icp network stop
1419
```
1520

16-
## Build
21+
## Manual testing
1722

18-
```
19-
$ dfx start [--background]
20-
$ dfx deploy [--no-wallet] counter
21-
22-
$ dfx canister call counter get
23-
(0 : int64)
24-
$ dfx canister call counter inc
25-
()
26-
$ dfx canister call counter get
27-
(1 : int64)
28-
$ dfx canister call counter set '(42)'
29-
()
30-
$ dfx canister call counter get
31-
(42 : int64)
23+
```bash
24+
icp canister call --query counter get '()'
25+
# (0 : int64)
26+
27+
icp canister call counter inc '()'
28+
icp canister call --query counter get '()'
29+
# (1 : int64)
30+
31+
icp canister call counter set '(42 : int64)'
32+
icp canister call --query counter get '()'
33+
# (42 : int64)
3234
```

wasm/counter/dfx.json

Lines changed: 0 additions & 13 deletions
This file was deleted.

wasm/counter/icp.yaml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
canisters:
2+
- name: counter
3+
build:
4+
steps:
5+
- type: script
6+
commands:
7+
- wat2wasm counter.wat -o counter.wasm
8+
- ic-wasm counter.wasm -o "$ICP_WASM_OUTPUT_PATH" metadata candid:service -f counter.did -v public

wasm/counter/test.sh

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#!/usr/bin/env bash
2+
set -e
3+
4+
echo "--- Initial value should be 0 ---"
5+
result=$(icp canister call --query counter get '()')
6+
echo "$result"
7+
echo "$result" | grep -q '(0' && echo "PASS" || (echo "FAIL" && exit 1)
8+
9+
echo "--- Increment counter ---"
10+
icp canister call counter inc '()'
11+
12+
echo "--- Value should be 1 after inc ---"
13+
result=$(icp canister call --query counter get '()')
14+
echo "$result"
15+
echo "$result" | grep -q '(1' && echo "PASS" || (echo "FAIL" && exit 1)
16+
17+
echo "--- Set counter to 42 ---"
18+
icp canister call counter set '(42 : int64)'
19+
20+
echo "--- Value should be 42 after set ---"
21+
result=$(icp canister call --query counter get '()')
22+
echo "$result"
23+
echo "$result" | grep -q '(42' && echo "PASS" || (echo "FAIL" && exit 1)

0 commit comments

Comments
 (0)