Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Updated motoko examples to functional runtime syntax #1271

Merged
merged 22 commits into from
Sep 26, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
4a390fa
Update calc example to functional runtime syntax
dansteren Sep 22, 2023
c7e2c7c
Update counter to functional runtime syntax
dansteren Sep 22, 2023
24572b3
Update echo example to functional runtime syntax
dansteren Sep 22, 2023
ba23cbf
Update factorial to functional runtime syntax
dansteren Sep 22, 2023
8bba2f5
Update hello example to functional runtime syntax
dansteren Sep 22, 2023
0e73411
Update hello-world to functional runtime syntax
dansteren Sep 22, 2023
903dd15
Update http_counter to functional runtime syntax
dansteren Sep 25, 2023
7cf9db9
Update minimal-count to functional runtime syntax
dansteren Sep 25, 2023
e1690a1
Update persistent-storage to functional syntax
dansteren Sep 25, 2023
e3bec7f
Update phone-book to functional runtime syntax
dansteren Sep 25, 2023
714d4ab
Update quicksort to functional runtime syntax
dansteren Sep 25, 2023
f4c9d31
Update simple-to-do to functional runtime syntax
dansteren Sep 25, 2023
387b881
Update superheroes to functional runtime syntax
dansteren Sep 25, 2023
7c6b4b2
Update whoami to functional runtime syntax
dansteren Sep 25, 2023
9fe6f04
Update notify_raw to functional runtime syntax
dansteren Sep 25, 2023
b5a3c24
Update null_example
dansteren Sep 25, 2023
ed52877
Update optional_types to functional runtime syntax
dansteren Sep 25, 2023
8733610
Update outgoing-http-requests to functional syntax
dansteren Sep 25, 2023
d78865c
Update rejections to functional runtime syntax
dansteren Sep 25, 2023
08ceb35
Delete rust_type_conversions example
dansteren Sep 25, 2023
cde7785
Remove logs from StableBTreeMap.insert
dansteren Sep 26, 2023
3022db4
Align spacing with Jordan's examples
dansteren Sep 26, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 19 additions & 20 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,28 +17,9 @@
# "examples/complex_types",
# "examples/func_types",
# "examples/generics",
# "examples/motoko_examples/calc",
# "examples/motoko_examples/counter",
# "examples/motoko_examples/echo",
# "examples/motoko_examples/factorial",
# "examples/motoko_examples/hello",
# "examples/motoko_examples/hello-world",
# "examples/motoko_examples/http_counter",
# "examples/motoko_examples/minimal-counter-dapp",
# "examples/motoko_examples/persistent-storage",
# "examples/motoko_examples/phone-book",
# "examples/motoko_examples/quicksort",
# "examples/motoko_examples/simple-to-do",
# "examples/motoko_examples/superheroes",
# "examples/motoko_examples/whoami",
# "examples/notify_raw",
# "examples/null_example",
# "examples/optional_types",
# "examples/outgoing_http_requests",
# "examples/rejections",
# "examples/motoko_examples/superheroes", # blocked by recursive
# "examples/robust_imports",
# "examples/run_time_errors",
# "examples/rust_type_conversions",
# "examples/tuple_types",

name: Azle Tests
Expand Down Expand Up @@ -110,12 +91,30 @@ jobs:
"examples/list_of_lists",
"examples/management_canister",
"examples/manual_reply",
"examples/motoko_examples/calc",
"examples/motoko_examples/counter",
"examples/motoko_examples/echo",
"examples/motoko_examples/factorial",
"examples/motoko_examples/hello",
"examples/motoko_examples/hello-world",
"examples/motoko_examples/http_counter",
"examples/motoko_examples/minimal-counter-dapp",
"examples/motoko_examples/persistent-storage",
"examples/motoko_examples/phone-book",
"examples/motoko_examples/quicksort",
"examples/motoko_examples/simple-to-do",
"examples/motoko_examples/threshold_ecdsa",
"examples/motoko_examples/whoami",
"examples/notify_raw",
"examples/null_example",
"examples/optional_types",
"examples/outgoing_http_requests",
"examples/pre_and_post_upgrade",
"examples/primitive_types",
"examples/principal",
"examples/query",
"examples/randomness",
"examples/rejections",
"examples/service",
"examples/simple_erc20",
"examples/simple_user_accounts",
Expand Down
3 changes: 2 additions & 1 deletion examples/motoko_examples/calc/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/calc/calc.wasm.gz",
"wasm": ".azle/calc/calc.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/calc",
"node_compatibility": true
Expand Down
55 changes: 23 additions & 32 deletions examples/motoko_examples/calc/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,41 +1,32 @@
import { int, update, Opt, Void, Some, None, Service } from 'azle';
import { int, None, Opt, Service, Some, update, Void } from 'azle';

export default class extends Service {
cell: int = 0n;
let cell: int = 0n;

@update([int], int)
add(n: int): int {
this.cell += n;
export default Service({
add: update([int], int, (n) => {
cell += n;

return this.cell;
}
return cell;
}),
sub: update([int], int, (n) => {
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
cell -= n;

@update([int], int)
sub(n: int): int {
this.cell -= n;
return cell;
}),
mul: update([int], int, (n) => {
cell *= n;

return this.cell;
}

@update([int], int)
mul(n: int): int {
this.cell *= n;

return this.cell;
}

@update([int], Opt(int))
div(n: int) {
return cell;
}),
div: update([int], Opt(int), (n) => {
if (n === 0n) {
return None;
} else {
this.cell /= n;
return Some(this.cell);
cell /= n;
return Some(cell);
}
}

@update([], Void)
clearall(): void {
this.cell = 0n;
}
}
}),
clearall: update([], Void, () => {
cell = 0n;
})
});
3 changes: 2 additions & 1 deletion examples/motoko_examples/counter/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/counter/counter.wasm.gz",
"wasm": ".azle/counter/counter.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/counter",
"node_compatibility": true
Expand Down
31 changes: 13 additions & 18 deletions examples/motoko_examples/counter/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,20 +1,15 @@
import { nat, query, Service, update, Void } from 'azle';

export default class extends Service {
counter: nat = 0n;

@query([], nat)
get(): nat {
return this.counter;
}

@update([nat], Void)
set(n: nat): void {
this.counter = n;
}

@update([], Void)
inc(): void {
this.counter += 1n;
}
}
let counter: nat = 0n;

export default Service({
get: query([], nat, () => {
return counter;
}),
set: update([nat], Void, (n) => {
counter = n;
}),
inc: update([], Void, () => {
counter += 1n;
})
});
3 changes: 2 additions & 1 deletion examples/motoko_examples/echo/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/echo/echo.wasm.gz",
"wasm": ".azle/echo/echo.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/echo",
"node_compatibility": true
Expand Down
12 changes: 5 additions & 7 deletions examples/motoko_examples/echo/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
import { Service, query, text } from 'azle';
import { query, Service, text } from 'azle';

export default class extends Service {
// Say the given phrase.
@query([text], text)
say(phrase: text): text {
export default Service({
say: query([text], text, (phrase) => {
return phrase;
}
}
})
});
3 changes: 2 additions & 1 deletion examples/motoko_examples/factorial/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/factorial/factorial.wasm.gz",
"wasm": ".azle/factorial/factorial.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/factorial",
"node_compatibility": true
Expand Down
11 changes: 5 additions & 6 deletions examples/motoko_examples/factorial/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
import { Service, nat, update } from 'azle';
import { nat, Service, update } from 'azle';

export default class extends Service {
export default Service({
// Calculate the product of all positive integers less than or equal to `n`.
@update([nat], nat)
fac(n: nat): nat {
fac: update([nat], nat, (n) => {
return go(n);
}
}
})
});

// We implement the recursion in a helper function.
function go(m: nat): nat {
Expand Down
3 changes: 2 additions & 1 deletion examples/motoko_examples/hello-world/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/hello_world/hello_world.wasm.gz",
"wasm": ".azle/hello_world/hello_world.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/hello_world",
"node_compatibility": true
Expand Down
9 changes: 4 additions & 5 deletions examples/motoko_examples/hello-world/src/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { query, Service, Void } from 'azle';

export default class extends Service {
@query([], Void)
main(): void {
export default Service({
main: query([], Void, () => {
console.log('Hello World!');
}
}
})
});
3 changes: 2 additions & 1 deletion examples/motoko_examples/hello/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src/hello",
"ts": "src/hello/index.ts",
"candid": "src/hello/index.did",
"wasm": ".azle/hello/hello.wasm.gz",
"wasm": ".azle/hello/hello.wasm",
"gzip": true,
"declarations": {
"output": "dfx_generated/hello",
"node_compatibility": true
Expand Down
11 changes: 5 additions & 6 deletions examples/motoko_examples/hello/src/hello/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
import { Service, query, text } from 'azle';
import { query, Service, text } from 'azle';

export default class extends Service {
@query([text], text)
greet(name: string): string {
export default Service({
greet: query([text], text, (name) => {
return `Hello, ${name}!`;
}
}
})
});
3 changes: 2 additions & 1 deletion examples/motoko_examples/http_counter/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
"root": "src",
"ts": "src/index.ts",
"candid": "src/index.did",
"wasm": ".azle/http_counter/http_counter.wasm.gz",
"wasm": ".azle/http_counter/http_counter.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/http_counter",
"node_compatibility": true
Expand Down
Loading
Loading