Skip to content

Commit

Permalink
Merge pull request #1271 from demergent-labs/1249_functional_motoko_e…
Browse files Browse the repository at this point in the history
…xamples

Updated motoko examples to functional runtime syntax
  • Loading branch information
lastmjs authored Sep 26, 2023
2 parents aef582c + 3022db4 commit 3b5d2c5
Show file tree
Hide file tree
Showing 48 changed files with 538 additions and 680 deletions.
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) => {
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

0 comments on commit 3b5d2c5

Please sign in to comment.