From 4a390fa356dd3e6e4dfb244ba2a5a61cd56f9900 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Fri, 22 Sep 2023 17:11:43 -0600 Subject: [PATCH 01/22] Update calc example to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/calc/dfx.json | 3 +- examples/motoko_examples/calc/src/index.ts | 51 ++++++++++------------ 3 files changed, 26 insertions(+), 30 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index bb6a0c695d..ffe90671c1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/calc", # "examples/motoko_examples/counter", # "examples/motoko_examples/echo", # "examples/motoko_examples/factorial", @@ -110,6 +109,7 @@ jobs: "examples/list_of_lists", "examples/management_canister", "examples/manual_reply", + "examples/motoko_examples/calc", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/calc/dfx.json b/examples/motoko_examples/calc/dfx.json index 559fb20555..ac4642ebf7 100644 --- a/examples/motoko_examples/calc/dfx.json +++ b/examples/motoko_examples/calc/dfx.json @@ -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 diff --git a/examples/motoko_examples/calc/src/index.ts b/examples/motoko_examples/calc/src/index.ts index d760190a97..99f6b10fa9 100644 --- a/examples/motoko_examples/calc/src/index.ts +++ b/examples/motoko_examples/calc/src/index.ts @@ -1,41 +1,36 @@ -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; + }), - @update([int], int) - sub(n: int): int { - this.cell -= n; + sub: update([int], int, (n) => { + cell -= n; - return this.cell; - } + return cell; + }), - @update([int], int) - mul(n: int): int { - this.cell *= n; + mul: update([int], int, (n) => { + cell *= n; - return this.cell; - } + return cell; + }), - @update([int], Opt(int)) - div(n: int) { + 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; + }) +}); From c7e2c7c18b98c0697265783e0fa4d20d0422030a Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Fri, 22 Sep 2023 17:20:38 -0600 Subject: [PATCH 02/22] Update counter to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/counter/dfx.json | 3 ++- examples/motoko_examples/counter/src/index.ts | 27 +++++++++---------- 3 files changed, 15 insertions(+), 17 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ffe90671c1..3f7627d63e 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/counter", # "examples/motoko_examples/echo", # "examples/motoko_examples/factorial", # "examples/motoko_examples/hello", @@ -110,6 +109,7 @@ jobs: "examples/management_canister", "examples/manual_reply", "examples/motoko_examples/calc", + "examples/motoko_examples/counter", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/counter/dfx.json b/examples/motoko_examples/counter/dfx.json index c78402bafb..4514887657 100644 --- a/examples/motoko_examples/counter/dfx.json +++ b/examples/motoko_examples/counter/dfx.json @@ -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 diff --git a/examples/motoko_examples/counter/src/index.ts b/examples/motoko_examples/counter/src/index.ts index 2fe57ba243..8e801a61c6 100644 --- a/examples/motoko_examples/counter/src/index.ts +++ b/examples/motoko_examples/counter/src/index.ts @@ -1,20 +1,17 @@ import { nat, query, Service, update, Void } from 'azle'; -export default class extends Service { - counter: nat = 0n; +let counter: nat = 0n; - @query([], nat) - get(): nat { - return this.counter; - } +export default Service({ + get: query([], nat, () => { + return counter; + }), - @update([nat], Void) - set(n: nat): void { - this.counter = n; - } + set: update([nat], Void, (n) => { + counter = n; + }), - @update([], Void) - inc(): void { - this.counter += 1n; - } -} + inc: update([], Void, () => { + counter += 1n; + }) +}); From 24572b349e5c1659aa584f6b999e159922e8219a Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Fri, 22 Sep 2023 17:23:02 -0600 Subject: [PATCH 03/22] Update echo example to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/echo/dfx.json | 3 ++- examples/motoko_examples/echo/src/index.ts | 12 +++++------- 3 files changed, 8 insertions(+), 9 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 3f7627d63e..8726aced2b 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/echo", # "examples/motoko_examples/factorial", # "examples/motoko_examples/hello", # "examples/motoko_examples/hello-world", @@ -110,6 +109,7 @@ jobs: "examples/manual_reply", "examples/motoko_examples/calc", "examples/motoko_examples/counter", + "examples/motoko_examples/echo", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/echo/dfx.json b/examples/motoko_examples/echo/dfx.json index 2edd152005..f659f00952 100644 --- a/examples/motoko_examples/echo/dfx.json +++ b/examples/motoko_examples/echo/dfx.json @@ -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 diff --git a/examples/motoko_examples/echo/src/index.ts b/examples/motoko_examples/echo/src/index.ts index 47a2892973..21723acb28 100644 --- a/examples/motoko_examples/echo/src/index.ts +++ b/examples/motoko_examples/echo/src/index.ts @@ -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; - } -} + }) +}); From ba23cbf49a167813711a0be46c7fc86683448c30 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Fri, 22 Sep 2023 17:25:37 -0600 Subject: [PATCH 04/22] Update factorial to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/factorial/dfx.json | 3 ++- examples/motoko_examples/factorial/src/index.ts | 11 +++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8726aced2b..7b66e3eb27 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/factorial", # "examples/motoko_examples/hello", # "examples/motoko_examples/hello-world", # "examples/motoko_examples/http_counter", @@ -110,6 +109,7 @@ jobs: "examples/motoko_examples/calc", "examples/motoko_examples/counter", "examples/motoko_examples/echo", + "examples/motoko_examples/factorial", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/factorial/dfx.json b/examples/motoko_examples/factorial/dfx.json index a5e7e7acdc..f71f8f8b71 100644 --- a/examples/motoko_examples/factorial/dfx.json +++ b/examples/motoko_examples/factorial/dfx.json @@ -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 diff --git a/examples/motoko_examples/factorial/src/index.ts b/examples/motoko_examples/factorial/src/index.ts index 19738c667e..5e2f112f86 100644 --- a/examples/motoko_examples/factorial/src/index.ts +++ b/examples/motoko_examples/factorial/src/index.ts @@ -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 { From 8bba2f59e4521868b83004e9fd708be260a40b39 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Fri, 22 Sep 2023 17:29:30 -0600 Subject: [PATCH 05/22] Update hello example to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/hello/dfx.json | 3 ++- examples/motoko_examples/hello/src/hello/index.ts | 11 +++++------ 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 7b66e3eb27..ac88fa0c10 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/hello", # "examples/motoko_examples/hello-world", # "examples/motoko_examples/http_counter", # "examples/motoko_examples/minimal-counter-dapp", @@ -110,6 +109,7 @@ jobs: "examples/motoko_examples/counter", "examples/motoko_examples/echo", "examples/motoko_examples/factorial", + "examples/motoko_examples/hello", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/hello/dfx.json b/examples/motoko_examples/hello/dfx.json index cd91a0a684..a1c8e27781 100644 --- a/examples/motoko_examples/hello/dfx.json +++ b/examples/motoko_examples/hello/dfx.json @@ -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 diff --git a/examples/motoko_examples/hello/src/hello/index.ts b/examples/motoko_examples/hello/src/hello/index.ts index 9ef4eec81f..b6a65ca074 100644 --- a/examples/motoko_examples/hello/src/hello/index.ts +++ b/examples/motoko_examples/hello/src/hello/index.ts @@ -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}!`; - } -} + }) +}); From 0e7341143ef550ef72d963209aaed68e5846e7b9 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Fri, 22 Sep 2023 17:31:10 -0600 Subject: [PATCH 06/22] Update hello-world to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/hello-world/dfx.json | 3 ++- examples/motoko_examples/hello-world/src/index.ts | 9 ++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ac88fa0c10..21818c1d6f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/hello-world", # "examples/motoko_examples/http_counter", # "examples/motoko_examples/minimal-counter-dapp", # "examples/motoko_examples/persistent-storage", @@ -110,6 +109,7 @@ jobs: "examples/motoko_examples/echo", "examples/motoko_examples/factorial", "examples/motoko_examples/hello", + "examples/motoko_examples/hello-world", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/hello-world/dfx.json b/examples/motoko_examples/hello-world/dfx.json index 4c6ec789e7..8cb2a10660 100644 --- a/examples/motoko_examples/hello-world/dfx.json +++ b/examples/motoko_examples/hello-world/dfx.json @@ -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 diff --git a/examples/motoko_examples/hello-world/src/index.ts b/examples/motoko_examples/hello-world/src/index.ts index 7919eed25d..20b07aaf0e 100644 --- a/examples/motoko_examples/hello-world/src/index.ts +++ b/examples/motoko_examples/hello-world/src/index.ts @@ -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!'); - } -} + }) +}); From 903dd15a3d365f1200799bfe7098b4d1170de9a3 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 11:27:19 -0600 Subject: [PATCH 07/22] Update http_counter to functional runtime syntax --- .github/workflows/test.yml | 2 +- .../motoko_examples/http_counter/dfx.json | 3 +- .../motoko_examples/http_counter/src/index.ts | 150 +++++++----------- 3 files changed, 61 insertions(+), 94 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 21818c1d6f..a7d7b80d2a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/http_counter", # "examples/motoko_examples/minimal-counter-dapp", # "examples/motoko_examples/persistent-storage", # "examples/motoko_examples/phone-book", @@ -110,6 +109,7 @@ jobs: "examples/motoko_examples/factorial", "examples/motoko_examples/hello", "examples/motoko_examples/hello-world", + "examples/motoko_examples/http_counter", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/http_counter/dfx.json b/examples/motoko_examples/http_counter/dfx.json index dccbc9ae87..4b9549efb1 100644 --- a/examples/motoko_examples/http_counter/dfx.json +++ b/examples/motoko_examples/http_counter/dfx.json @@ -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 diff --git a/examples/motoko_examples/http_counter/src/index.ts b/examples/motoko_examples/http_counter/src/index.ts index 9a89fb1296..a1c3978df3 100644 --- a/examples/motoko_examples/http_counter/src/index.ts +++ b/examples/motoko_examples/http_counter/src/index.ts @@ -1,101 +1,72 @@ import { blob, + bool, + Func, ic, init, nat, nat16, + None, Opt, query, Record, + Service, + Some, StableBTreeMap, + text, Tuple, - Variant, - Vec, update, - text, - candid, - func, - Some, - None, - bool, - Service, - Func + Variant, + Vec } from 'azle'; -class Token extends Record { +const Token = Record({ // add whatever fields you'd like - @candid(text) - arbitrary_data: text; -} - -class StreamingCallbackHttpResponse extends Record { - @candid(blob) - body: blob; - - @candid(Opt(Token)) - token: Opt; -} + arbitrary_data: text +}); -@func([text], StreamingCallbackHttpResponse, 'query') -class Callback extends Func {} +const StreamingCallbackHttpResponse = Record({ + body: blob, + token: Opt(Token) +}); -class CallbackStrategy extends Record { - @candid(Callback) - callback: Callback; +export const Callback = Func([text], StreamingCallbackHttpResponse, 'query'); - @candid(Token) - token: Token; -} +const CallbackStrategy = Record({ + callback: Callback, + token: Token +}); -class StreamingStrategy extends Variant { - @candid(CallbackStrategy) - Callback?: CallbackStrategy; -} +const StreamingStrategy = Variant({ + Callback: CallbackStrategy +}); type HeaderField = [text, text]; const HeaderField = Tuple(text, text); -class HttpResponse extends Record { - @candid(nat16) - status_code: nat16; - - @candid(Vec(HeaderField)) - headers: Vec; - - @candid(blob) - body: blob; - - @candid(Opt(StreamingStrategy)) - streaming_strategy: Opt; - - @candid(Opt(bool)) - upgrade: Opt; -} - -class HttpRequest extends Record { - @candid(text) - method: text; - - @candid(text) - url: text; - - @candid(Vec(HeaderField)) - headers: Vec; - - @candid(blob) - body: blob; -} - -export default class extends Service { - stableStorage = new StableBTreeMap(text, nat, 0); - - @init([]) - init() { - this.stableStorage.insert('counter', 0n); - } - - @query([HttpRequest], HttpResponse) - http_request(req: HttpRequest): HttpResponse { +const HttpResponse = Record({ + status_code: nat16, + headers: Vec(HeaderField), + body: blob, + streaming_strategy: Opt(StreamingStrategy), + upgrade: Opt(bool) +}); + +const HttpRequest = Record({ + method: text, + url: text, + headers: Vec(HeaderField), + body: blob +}); + +let stableStorage = StableBTreeMap(text, nat, 0); + +export default Service({ + init: init([], () => { + stableStorage.insert('counter', 0n); + }), + + http_request: query([HttpRequest], HttpResponse, (req) => { console.log('Hello from http_request'); if (req.method === 'GET') { @@ -107,10 +78,7 @@ export default class extends Service { body: encode('Counter'), streaming_strategy: Some({ Callback: { - callback: new Callback( - ic.id(), - 'http_streaming' - ), + callback: [ic.id(), 'http_streaming'], token: { arbitrary_data: 'start' } @@ -120,7 +88,7 @@ export default class extends Service { }; } - const counterOpt = this.stableStorage.get('counter'); + const counterOpt = stableStorage.get('counter'); const counter = counterOpt.length === 0 ? ic.trap('counter does not exist') @@ -172,21 +140,20 @@ export default class extends Service { streaming_strategy: None, upgrade: None }; - } + }), - @update([HttpRequest], HttpResponse) - http_request_update(req: HttpRequest): HttpResponse { + http_request_update: update([HttpRequest], HttpResponse, (req) => { if (req.method === 'POST') { - const counterOpt = this.stableStorage.get('counter'); + const counterOpt = stableStorage.get('counter'); const counter = counterOpt.length === 0 ? ic.trap('counter does not exist') : counterOpt[0]; - this.stableStorage.insert('counter', counter + 1n); + stableStorage.insert('counter', counter + 1n); if (req.headers.find(isGzip) === undefined) { - const counterOpt = this.stableStorage.get('counter'); + const counterOpt = stableStorage.get('counter'); const counter = counterOpt.length === 0 ? ic.trap('counter does not exist') @@ -228,10 +195,9 @@ export default class extends Service { streaming_strategy: None, upgrade: None }; - } + }), - @query([Token], StreamingCallbackHttpResponse) - http_streaming(token: Token): StreamingCallbackHttpResponse { + http_streaming: query([Token], StreamingCallbackHttpResponse, (token) => { console.log('Hello from http_streaming'); switch (token.arbitrary_data) { case 'start': { @@ -241,7 +207,7 @@ export default class extends Service { }; } case 'next': { - const counterOpt = this.stableStorage.get('counter'); + const counterOpt = stableStorage.get('counter'); const counter = counterOpt.length === 0 ? ic.trap('counter does not exist') @@ -262,8 +228,8 @@ export default class extends Service { return ic.trap('unreachable'); } } - } -} + }) +}); function isGzip(x: HeaderField): boolean { return ( From 7cf9db918951145f472b5f164a63fc2a686b610e Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 11:32:28 -0600 Subject: [PATCH 08/22] Update minimal-count to functional runtime syntax --- .github/workflows/test.yml | 2 +- .../minimal-counter-dapp/dfx.json | 3 +- .../src/minimal_dapp/index.ts | 31 +++++++++---------- 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index a7d7b80d2a..8cc9e962b2 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/minimal-counter-dapp", # "examples/motoko_examples/persistent-storage", # "examples/motoko_examples/phone-book", # "examples/motoko_examples/quicksort", @@ -110,6 +109,7 @@ jobs: "examples/motoko_examples/hello", "examples/motoko_examples/hello-world", "examples/motoko_examples/http_counter", + "examples/motoko_examples/minimal-counter-dapp", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/minimal-counter-dapp/dfx.json b/examples/motoko_examples/minimal-counter-dapp/dfx.json index f90b270063..b5cc224386 100644 --- a/examples/motoko_examples/minimal-counter-dapp/dfx.json +++ b/examples/motoko_examples/minimal-counter-dapp/dfx.json @@ -6,7 +6,8 @@ "root": "src/minimal_dapp", "ts": "src/minimal_dapp/index.ts", "candid": "src/minimal_dapp/index.did", - "wasm": ".azle/minimal_dapp/minimal_dapp.wasm.gz", + "wasm": ".azle/minimal_dapp/minimal_dapp.wasm", + "gzip": true, "declarations": { "output": "src/declarations/minimal_dapp", "node_compatibility": true diff --git a/examples/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts b/examples/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts index 7f5b6a9995..c6049e0a23 100644 --- a/examples/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts +++ b/examples/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts @@ -1,24 +1,21 @@ import { Service, nat, query, update } from 'azle'; -export default class extends Service { - counter: nat = 0n; +let counter: nat = 0n; - @update([], nat) - count(): nat { - this.counter += 1n; +export default Service({ + count: update([], nat, () => { + counter += 1n; - return this.counter; - } + return counter; + }), - @query([], nat) - getCount(): nat { - return this.counter; - } + getCount: query([], nat, () => { + return counter; + }), - @update([], nat) - reset(): nat { - this.counter = 0n; + reset: update([], nat, () => { + counter = 0n; - return this.counter; - } -} + return counter; + }) +}); From e1690a194720aded076b8b6512c392497593c9e7 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 11:38:38 -0600 Subject: [PATCH 09/22] Update persistent-storage to functional syntax --- .github/workflows/test.yml | 2 +- .../persistent-storage/dfx.json | 3 +- .../persistent-storage/src/index.ts | 38 +++++++++---------- 3 files changed, 20 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 8cc9e962b2..75e30f8c91 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/persistent-storage", # "examples/motoko_examples/phone-book", # "examples/motoko_examples/quicksort", # "examples/motoko_examples/simple-to-do", @@ -110,6 +109,7 @@ jobs: "examples/motoko_examples/hello-world", "examples/motoko_examples/http_counter", "examples/motoko_examples/minimal-counter-dapp", + "examples/motoko_examples/persistent-storage", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/persistent-storage/dfx.json b/examples/motoko_examples/persistent-storage/dfx.json index 644cad79d9..9e2ae41a76 100644 --- a/examples/motoko_examples/persistent-storage/dfx.json +++ b/examples/motoko_examples/persistent-storage/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/persistent_storage/persistent_storage.wasm.gz", + "wasm": ".azle/persistent_storage/persistent_storage.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/persistent_storage", "node_compatibility": true diff --git a/examples/motoko_examples/persistent-storage/src/index.ts b/examples/motoko_examples/persistent-storage/src/index.ts index fed9aa66d7..ffc0aaae4b 100644 --- a/examples/motoko_examples/persistent-storage/src/index.ts +++ b/examples/motoko_examples/persistent-storage/src/index.ts @@ -9,48 +9,44 @@ import { update } from 'azle'; -export default class extends Service { - stableStorage = new StableBTreeMap(text, nat, 0); +let stableStorage = StableBTreeMap(text, nat, 0); - @init([]) - init() { - this.stableStorage.insert('counter', 0n); - } +export default Service({ + init: init([], () => { + stableStorage.insert('counter', 0n); + }), - @update([], nat) - increment(): nat { - const counterOpt = this.stableStorage.get('counter'); + increment: update([], nat, () => { + const counterOpt = stableStorage.get('counter'); const counter = counterOpt.length === 0 ? ic.trap('counter not defined') : counterOpt[0] + 1n; - this.stableStorage.insert('counter', counter); + stableStorage.insert('counter', counter); return counter; - } + }), - @query([], nat) - get(): nat { - const counterOpt = this.stableStorage.get('counter'); + get: query([], nat, () => { + const counterOpt = stableStorage.get('counter'); const counter = counterOpt.length === 0 ? ic.trap('counter not defined') : counterOpt[0]; return counter; - } + }), - @update([], nat) - reset(): nat { - this.stableStorage.insert('counter', 0n); + reset: update([], nat, () => { + stableStorage.insert('counter', 0n); - const counterOpt = this.stableStorage.get('counter'); + const counterOpt = stableStorage.get('counter'); const counter = counterOpt.length === 0 ? ic.trap('counter not defined') : counterOpt[0]; return counter; - } -} + }) +}); From e3bec7ffecc2b98c2ca18cc834bb88d5851d0d59 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 11:47:59 -0600 Subject: [PATCH 10/22] Update phone-book to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/phone-book/dfx.json | 3 +- .../phone-book/src/phone_book/index.ts | 32 ++++++++----------- .../motoko_examples/phone-book/test/tests.ts | 6 ++-- 4 files changed, 20 insertions(+), 23 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 75e30f8c91..ca78658a0a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/phone-book", # "examples/motoko_examples/quicksort", # "examples/motoko_examples/simple-to-do", # "examples/motoko_examples/superheroes", @@ -110,6 +109,7 @@ jobs: "examples/motoko_examples/http_counter", "examples/motoko_examples/minimal-counter-dapp", "examples/motoko_examples/persistent-storage", + "examples/motoko_examples/phone-book", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/phone-book/dfx.json b/examples/motoko_examples/phone-book/dfx.json index 675095ad3d..e9c47f29b0 100644 --- a/examples/motoko_examples/phone-book/dfx.json +++ b/examples/motoko_examples/phone-book/dfx.json @@ -6,7 +6,8 @@ "root": "src/phone_book", "ts": "src/phone_book/index.ts", "candid": "src/phone_book/index.did", - "wasm": ".azle/phone_book/phone_book.wasm.gz", + "wasm": ".azle/phone_book/phone_book.wasm", + "gzip": true, "declarations": { "output": "src/declarations/phone_book", "node_compatibility": false diff --git a/examples/motoko_examples/phone-book/src/phone_book/index.ts b/examples/motoko_examples/phone-book/src/phone_book/index.ts index 4c190a4b16..a34cc9097e 100644 --- a/examples/motoko_examples/phone-book/src/phone_book/index.ts +++ b/examples/motoko_examples/phone-book/src/phone_book/index.ts @@ -1,5 +1,4 @@ import { - candid, None, Opt, query, @@ -11,26 +10,21 @@ import { Void } from 'azle'; -export class Entry extends Record { - @candid(text) - desc: text; +export const Entry = Record({ + desc: text, + phone: text +}); - @candid(text) - phone: text; -} +let phoneBook = new Map(); -export default class extends Service { - phoneBook = new Map(); +export default Service({ + insert: update([text, Entry], Void, (name, entry) => { + phoneBook.set(name, entry); + }), - @update([text, Entry], Void) - insert(name: text, entry: Entry): void { - this.phoneBook.set(name, entry); - } - - @query([text], Opt(Entry)) - lookup(name: text): Opt { - const entryOrUndefined = this.phoneBook.get(name); + lookup: query([text], Opt(Entry), (name) => { + const entryOrUndefined = phoneBook.get(name); return entryOrUndefined ? Some(entryOrUndefined) : None; - } -} + }) +}); diff --git a/examples/motoko_examples/phone-book/test/tests.ts b/examples/motoko_examples/phone-book/test/tests.ts index a8bc506011..f4e1469687 100644 --- a/examples/motoko_examples/phone-book/test/tests.ts +++ b/examples/motoko_examples/phone-book/test/tests.ts @@ -1,6 +1,8 @@ import { Test } from 'azle/test'; -import { Entry } from '../src/phone_book/index.ts.dont'; -import { _SERVICE } from '../src/declarations/phone_book/phone_book.did'; +import { + _SERVICE, + rec_0 as Entry +} from '../src/declarations/phone_book/phone_book.did'; import { ActorSubclass } from '@dfinity/agent'; const TEST_PHONE_BOOK_RECORD: Entry = { From 714d4abb8ec15f473e4cc3e5686e6f3bba811ccd Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 11:51:13 -0600 Subject: [PATCH 11/22] Update quicksort to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/quicksort/dfx.json | 3 ++- examples/motoko_examples/quicksort/src/index.ts | 9 ++++----- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ca78658a0a..df18d72029 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/quicksort", # "examples/motoko_examples/simple-to-do", # "examples/motoko_examples/superheroes", # "examples/motoko_examples/whoami", @@ -110,6 +109,7 @@ jobs: "examples/motoko_examples/minimal-counter-dapp", "examples/motoko_examples/persistent-storage", "examples/motoko_examples/phone-book", + "examples/motoko_examples/quicksort", "examples/motoko_examples/threshold_ecdsa", "examples/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/quicksort/dfx.json b/examples/motoko_examples/quicksort/dfx.json index 36fa28e0e0..e1090908d4 100644 --- a/examples/motoko_examples/quicksort/dfx.json +++ b/examples/motoko_examples/quicksort/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/quicksort/quicksort.wasm.gz", + "wasm": ".azle/quicksort/quicksort.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/quicksort", "node_compatibility": true diff --git a/examples/motoko_examples/quicksort/src/index.ts b/examples/motoko_examples/quicksort/src/index.ts index a85bdc1257..950ae2be5c 100644 --- a/examples/motoko_examples/quicksort/src/index.ts +++ b/examples/motoko_examples/quicksort/src/index.ts @@ -2,9 +2,8 @@ import { int, Vec, query, Service } from 'azle'; import { Int } from './comparison'; import * as Quicksort from './quicksort'; -export default class extends Service { - @query([Vec(int)], Vec(int)) - sort(xs: Vec): Vec { +export default Service({ + sort: query([Vec(int)], Vec(int), (xs) => { return Quicksort.sortBy(xs, Int.compare); - } -} + }) +}); From f4c9d3159dedd87d09985d5327b8839c42e2d252 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 11:57:25 -0600 Subject: [PATCH 12/22] Update simple-to-do to functional runtime syntax --- .github/workflows/test.yml | 2 +- .../motoko_examples/simple-to-do/dfx.json | 3 +- .../motoko_examples/simple-to-do/src/index.ts | 67 ++++++++----------- 3 files changed, 31 insertions(+), 41 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index df18d72029..9ce9f78665 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/simple-to-do", # "examples/motoko_examples/superheroes", # "examples/motoko_examples/whoami", # "examples/notify_raw", @@ -110,6 +109,7 @@ jobs: "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/pre_and_post_upgrade", "examples/primitive_types", diff --git a/examples/motoko_examples/simple-to-do/dfx.json b/examples/motoko_examples/simple-to-do/dfx.json index d93f604524..e35631e56f 100644 --- a/examples/motoko_examples/simple-to-do/dfx.json +++ b/examples/motoko_examples/simple-to-do/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/simple_to_do/simple_to_do.wasm.gz", + "wasm": ".azle/simple_to_do/simple_to_do.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/simple_to_do", "node_compatibility": true diff --git a/examples/motoko_examples/simple-to-do/src/index.ts b/examples/motoko_examples/simple-to-do/src/index.ts index e9bdf92491..e7cb4dc6fd 100644 --- a/examples/motoko_examples/simple-to-do/src/index.ts +++ b/examples/motoko_examples/simple-to-do/src/index.ts @@ -1,6 +1,5 @@ import { bool, - candid, nat, query, Record, @@ -11,72 +10,62 @@ import { Void } from 'azle'; -export class ToDo extends Record { - @candid(text) - description: text; +export const ToDo = Record({ + description: text, + completed: bool +}); - @candid(bool) - completed: bool; -} +let todos: Map = new Map(); +let nextId: nat = 0n; -export default class extends Service { - todos: Map = new Map(); - nextId: nat = 0n; - - @query([], Vec(ToDo)) - getTodos(): Vec { - return Array.from(this.todos.values()); - } +export default Service({ + getTodos: query([], Vec(ToDo), () => { + return Array.from(todos.values()); + }), // Returns the ID that was given to the ToDo item - @update([text], nat) - addTodo(description: text): nat { - const id = this.nextId; - this.todos.set(id, { + addTodo: update([text], nat, (description) => { + const id = nextId; + todos.set(id, { description: description, completed: false }); - this.nextId += 1n; + nextId += 1n; return id; - } + }), - @update([nat], Void) - completeTodo(id: nat): void { - let todo = this.todos.get(id); + completeTodo: update([nat], Void, (id) => { + let todo = todos.get(id); if (todo !== undefined) { - this.todos.set(id, { + todos.set(id, { description: todo.description, completed: true }); } - } + }), - @query([], text) - showTodos(): text { + showTodos: query([], text, () => { let output = '\n___TO-DOs___'; - for (const todoEntry of [...this.todos]) { + for (const todoEntry of [...todos]) { output += `\n${todoEntry[1].description}`; if (todoEntry[1].completed) { output += ' ✔'; } } return output; - } + }), - @update([], Void) - clearCompleted(): void { + clearCompleted: update([], Void, () => { // NOTE: this syntax isn't supported in Boa. If we revert to using Boa // we'll need to revert the syntax to: // ```ts - // this.todos = new Map( - // [...this.todos].filter((value) => !value[1].completed) + // todos = new Map( + // [...todos].filter((value) => !value[1].completed) // ); // ``` // See: https://github.com/demergent-labs/azle/issues/574 - this.todos = new Map( - [...this.todos].filter(([key, value]) => !value.completed) - ); - } -} + todos = new Map([...todos].filter(([key, value]) => !value.completed)); + }) +}); From 387b881b1048ae6b18f9dc401d3bcb718d07c75e Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 12:27:05 -0600 Subject: [PATCH 13/22] Update superheroes to functional runtime syntax Blocked by recursive definitions --- .github/workflows/test.yml | 2 +- examples/motoko_examples/superheroes/dfx.json | 3 +- .../superheroes/src/superheroes/index.ts | 77 +++++++++---------- 3 files changed, 39 insertions(+), 43 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9ce9f78665..2983e2264f 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,7 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/motoko_examples/superheroes", +# "examples/motoko_examples/superheroes", # blocked by recursive # "examples/motoko_examples/whoami", # "examples/notify_raw", # "examples/null_example", diff --git a/examples/motoko_examples/superheroes/dfx.json b/examples/motoko_examples/superheroes/dfx.json index 012a5b0010..4272d1e0c7 100644 --- a/examples/motoko_examples/superheroes/dfx.json +++ b/examples/motoko_examples/superheroes/dfx.json @@ -6,7 +6,8 @@ "root": "src/superheroes", "ts": "src/superheroes/index.ts", "candid": "src/superheroes/index.did", - "wasm": ".azle/superheroes/superheroes.wasm.gz", + "wasm": ".azle/superheroes/superheroes.wasm", + "gzip": true, "declarations": { "output": "src/declarations", "node_compatibility": true diff --git a/examples/motoko_examples/superheroes/src/superheroes/index.ts b/examples/motoko_examples/superheroes/src/superheroes/index.ts index 1495dcfada..ee859d087e 100644 --- a/examples/motoko_examples/superheroes/src/superheroes/index.ts +++ b/examples/motoko_examples/superheroes/src/superheroes/index.ts @@ -1,11 +1,11 @@ import { bool, - candid, nat32, None, Opt, query, Record, + Recursive, Service, Some, text, @@ -13,72 +13,67 @@ import { update } from 'azle'; -// Note: This won't be reflected in the candid until +// Note: This won't be reflected in the candid export type SuperheroId = nat32; const SuperheroId = nat32; -export type List = [text, Opt]; -const List: List = Tuple(text, Opt(List)); +const List = Tuple( + text, + Recursive(() => Opt(List)) +); // The type of a superhero. -class Superhero extends Record { - @candid(text) - name: text; - - @candid(Opt(List)) - superpowers: Opt; -} +const Superhero = Record({ + name: text, + superpowers: Opt(List) +}); /** - * High-Level API + * Application State */ -export default class extends Service { - /** - * Application State - */ - // The next available superhero identifier. - next: SuperheroId = 0; +// The next available superhero identifier. +let next: SuperheroId = 0; - // The superhero data store. - superheroes: Map = new Map(); +// The superhero data store. +let superheroes: Map = new Map(); +/** + * High-Level API + */ +export default Service({ // Create a superhero. - @update([Superhero], SuperheroId) - create(superhero: Superhero): SuperheroId { - let superheroId = this.next; - this.next += 1; - this.superheroes.set(superheroId, superhero); + create: update([Superhero], SuperheroId, (superhero) => { + let superheroId = next; + next += 1; + superheroes.set(superheroId, superhero); return superheroId; - } + }), // Read a superhero. - @query([SuperheroId], Opt(Superhero)) - read(superheroId: SuperheroId): Opt { - const superheroOrUndefined = this.superheroes.get(superheroId); + read: query([SuperheroId], Opt(Superhero), (superheroId) => { + const superheroOrUndefined = superheroes.get(superheroId); return superheroOrUndefined ? Some(superheroOrUndefined) : None; - } + }), // Update a superhero. - @update([SuperheroId, Superhero], bool) - update(superheroId: SuperheroId, superhero: Superhero): bool { - let result = this.superheroes.get(superheroId); + update: update([SuperheroId, Superhero], bool, (superheroId, superhero) => { + let result = superheroes.get(superheroId); if (result) { - this.superheroes.set(superheroId, superhero); + superheroes.set(superheroId, superhero); } return !!result; - } + }), // Delete a superhero. - @update([SuperheroId], bool) - deleteHero(superheroId: SuperheroId): bool { - let result = this.superheroes.get(superheroId); + deleteHero: update([SuperheroId], bool, (superheroId) => { + let result = superheroes.get(superheroId); if (result) { - this.superheroes.delete(superheroId); + superheroes.delete(superheroId); } return !!result; - } -} + }) +}); From 7c6b4b22f8c548626d43d42c1b4ec24fe7e02ada Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 12:56:37 -0600 Subject: [PATCH 14/22] Update whoami to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/motoko_examples/whoami/dfx.json | 3 +- examples/motoko_examples/whoami/src/index.ts | 74 +++++++++---------- examples/motoko_examples/whoami/test/tests.ts | 2 +- 4 files changed, 39 insertions(+), 42 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 2983e2264f..80f54825d3 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,6 @@ # "examples/func_types", # "examples/generics", # "examples/motoko_examples/superheroes", # blocked by recursive -# "examples/motoko_examples/whoami", # "examples/notify_raw", # "examples/null_example", # "examples/optional_types", @@ -111,6 +110,7 @@ jobs: "examples/motoko_examples/quicksort", "examples/motoko_examples/simple-to-do", "examples/motoko_examples/threshold_ecdsa", + "examples/motoko_examples/whoami", "examples/pre_and_post_upgrade", "examples/primitive_types", "examples/principal", diff --git a/examples/motoko_examples/whoami/dfx.json b/examples/motoko_examples/whoami/dfx.json index 3a298d6c6c..b171a6069f 100644 --- a/examples/motoko_examples/whoami/dfx.json +++ b/examples/motoko_examples/whoami/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/whoami/whoami.wasm.gz", + "wasm": ".azle/whoami/whoami.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/whoami", "node_compatibility": true diff --git a/examples/motoko_examples/whoami/src/index.ts b/examples/motoko_examples/whoami/src/index.ts index ed6d8c56ab..f58727736a 100644 --- a/examples/motoko_examples/whoami/src/index.ts +++ b/examples/motoko_examples/whoami/src/index.ts @@ -6,64 +6,60 @@ import { Principal, query, Service, - update, - Void + update } from 'azle'; -export default class extends Service { - // Initialize the variables to ensure that they aren't `undefined`. - // We use the zero principal but any principal could be used. - install: Principal = Principal.fromText('aaaaa-aa'); - someone: Principal = Principal.fromText('aaaaa-aa'); +// We use the zero principal but any principal could be used. +let install: Principal = Principal.fromText('aaaaa-aa'); +let someone: Principal = Principal.fromText('aaaaa-aa'); +const whoami = update([], principal, () => { + return ic.caller(); +}); + +export default Service({ // Manually save the calling principal and argument for later access. - @init([principal], Void) - init(somebody: Principal): Void { - this.install = ic.caller(); - this.someone = somebody; - } + init: init([principal], (somebody) => { + install = ic.caller(); + someone = somebody; + }), // Manually re-save these variables after new deploys. - @postUpgrade([principal], Void) - postUpgrade(somebody: Principal): Void { - this.install = ic.caller(); - this.someone = somebody; - } + postUpgrade: postUpgrade([principal], (somebody) => { + install = ic.caller(); + someone = somebody; + }), // Return the principal identifier of the wallet canister that installed this // canister. - @query([], principal) - installer(): Principal { - return this.install; - } + installer: query([], principal, () => { + return install; + }), // Return the principal identifier that was provided as an installation // argument to this canister. - @query([], principal) - argument(): Principal { - return this.someone; - } + argument: query([], principal, () => { + return someone; + }), // Return the principal identifier of the caller of this method. - @update([], principal) - whoami(): Principal { - return ic.caller(); - } + whoami, // Return the principal identifier of this canister. - @update([], principal) - async id(): Promise { - return await ic.call(this.whoami, { - args: [] - }); - } + id: update([], principal, async () => { + // TODO This is not an ideal solution but will work for now + const self = Service({ + whoami + })(ic.id()); + + return await ic.call(self.whoami); + }), // Return the principal identifier of this canister via the global `ic` object. // This is much quicker than `id()` above because it isn't making a cross- // canister call to itself. Additionally, it can now be a `Query` which means it // doesn't have to go through consensus. - @query([], principal) - idQuick(): Principal { + idQuick: query([], principal, () => { return ic.id(); - } -} + }) +}); diff --git a/examples/motoko_examples/whoami/test/tests.ts b/examples/motoko_examples/whoami/test/tests.ts index 5dfc550c3e..e82328a050 100644 --- a/examples/motoko_examples/whoami/test/tests.ts +++ b/examples/motoko_examples/whoami/test/tests.ts @@ -81,7 +81,7 @@ export function getTests( name: 'redeploy', prep: async () => { execSync( - `dfx deploy ${canisterName} --argument '(principal "${callingPrincipal}")'`, + `dfx deploy --upgrade-unchanged ${canisterName} --argument '(principal "${callingPrincipal}")'`, { stdio: 'inherit' } From 9fe6f04436eefcc2b4c243c62ce3fad64a0ac9e0 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 13:31:38 -0600 Subject: [PATCH 15/22] Update notify_raw to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/notify_raw/canisters/canister1/index.ts | 11 +++++------ examples/notify_raw/canisters/canister2/index.ts | 14 ++++++-------- examples/notify_raw/dfx.json | 6 ++++-- 4 files changed, 16 insertions(+), 17 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 80f54825d3..80f8a04655 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,6 @@ # "examples/func_types", # "examples/generics", # "examples/motoko_examples/superheroes", # blocked by recursive -# "examples/notify_raw", # "examples/null_example", # "examples/optional_types", # "examples/outgoing_http_requests", @@ -111,6 +110,7 @@ jobs: "examples/motoko_examples/simple-to-do", "examples/motoko_examples/threshold_ecdsa", "examples/motoko_examples/whoami", + "examples/notify_raw", "examples/pre_and_post_upgrade", "examples/primitive_types", "examples/principal", diff --git a/examples/notify_raw/canisters/canister1/index.ts b/examples/notify_raw/canisters/canister1/index.ts index 156d452156..2b6cf4ae17 100644 --- a/examples/notify_raw/canisters/canister1/index.ts +++ b/examples/notify_raw/canisters/canister1/index.ts @@ -1,8 +1,7 @@ -import { ic, Principal, update, Void } from 'azle'; +import { ic, Principal, Service, update, Void } from 'azle'; -export default class { - @update([], Void) - sendNotification(): Void { +export default Service({ + sendNotification: update([], Void, () => { return ic.notifyRaw( Principal.fromText( process.env.CANISTER2_PRINCIPAL ?? @@ -12,5 +11,5 @@ export default class { Uint8Array.from(ic.candidEncode('()')), 0n ); - } -} + }) +}); diff --git a/examples/notify_raw/canisters/canister2/index.ts b/examples/notify_raw/canisters/canister2/index.ts index 21c82fcddc..3466309b16 100644 --- a/examples/notify_raw/canisters/canister2/index.ts +++ b/examples/notify_raw/canisters/canister2/index.ts @@ -2,14 +2,12 @@ import { bool, query, Service, update, Void } from 'azle'; let notified: boolean = false; -export default class extends Service { - @update([], Void) - receiveNotification(): Void { +export default Service({ + receiveNotification: update([], Void, () => { notified = true; - } + }), - @query([], bool) - getNotified(): bool { + getNotified: query([], bool, () => { return notified; - } -} + }) +}); diff --git a/examples/notify_raw/dfx.json b/examples/notify_raw/dfx.json index 0a550f5f1a..750a2649e1 100644 --- a/examples/notify_raw/dfx.json +++ b/examples/notify_raw/dfx.json @@ -6,7 +6,8 @@ "root": "canisters/canister1", "ts": "canisters/canister1/index.ts", "candid": "canisters/canister1/index.did", - "wasm": ".azle/canister1/canister1.wasm.gz", + "wasm": ".azle/canister1/canister1.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/canister1", "node_compatibility": true @@ -19,7 +20,8 @@ "root": "canisters/canister2", "ts": "canisters/canister2/index.ts", "candid": "canisters/canister2/index.did", - "wasm": ".azle/canister2/canister2.wasm.gz", + "wasm": ".azle/canister2/canister2.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/canister2", "node_compatibility": true From b5a3c24060d9ec9bb26184bc73a69cbcc0edc92b Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 13:44:59 -0600 Subject: [PATCH 16/22] Update null_example --- .github/workflows/test.yml | 2 +- examples/null_example/dfx.json | 3 +- examples/null_example/src/index.ts | 117 ++++++++++++----------------- 3 files changed, 53 insertions(+), 69 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 80f8a04655..e9af1a6b48 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,6 @@ # "examples/func_types", # "examples/generics", # "examples/motoko_examples/superheroes", # blocked by recursive -# "examples/null_example", # "examples/optional_types", # "examples/outgoing_http_requests", # "examples/rejections", @@ -111,6 +110,7 @@ jobs: "examples/motoko_examples/threshold_ecdsa", "examples/motoko_examples/whoami", "examples/notify_raw", + "examples/null_example", "examples/pre_and_post_upgrade", "examples/primitive_types", "examples/principal", diff --git a/examples/null_example/dfx.json b/examples/null_example/dfx.json index 18186c0793..a9a23efef3 100644 --- a/examples/null_example/dfx.json +++ b/examples/null_example/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/null_example/null_example.wasm.gz", + "wasm": ".azle/null_example/null_example.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/null_example", "node_compatibility": true diff --git a/examples/null_example/src/index.ts b/examples/null_example/src/index.ts index 112c6fc8f9..19dd05b895 100644 --- a/examples/null_example/src/index.ts +++ b/examples/null_example/src/index.ts @@ -1,86 +1,69 @@ -import { ic, candid, query, Record, update, int, Null, Void } from 'azle'; - -class PartiallyNullRecord extends Record { - @candid(int) - firstItem: int; - - @candid(Null) - secondItem: Null; - - @candid(int) - thirdItem: int; -} - -class TwoNullRecord extends Record { - @candid(Null) - firstItem: Null; - - @candid(Null) - secondItem: Null; -} - -class ThreeNullRecord extends Record { - @candid(Null) - firstItem: null; - - @candid(Null) - secondItem: null; - - @candid(Null) - thirdItem: null; -} - -export default class { - @query([Null], Null) - nullFunction(param: null): null { +import { ic, int, Null, query, Record, Service, update, Void } from 'azle'; + +const PartiallyNullRecord = Record({ + firstItem: int, + secondItem: Null, + thirdItem: int +}); + +const TwoNullRecord = Record({ + firstItem: Null, + secondItem: Null +}); + +const ThreeNullRecord = Record({ + firstItem: Null, + secondItem: Null, + thirdItem: Null +}); + +export default Service({ + nullFunction: query([Null], Null, (param) => { return param; - } + }), - @query([], Void) - voidIsNotNull(): void { + voidIsNotNull: query([], Void, () => { ic.print( 'Even though they are both None in Python, for Candid null and void are different.' ); - } + }), - @query([], PartiallyNullRecord) - getPartiallyNullRecord(): PartiallyNullRecord { - return PartiallyNullRecord.create({ + getPartiallyNullRecord: query([], PartiallyNullRecord, () => { + return { firstItem: 1n, secondItem: null, thirdItem: 3n - }); - } - - @update([PartiallyNullRecord], PartiallyNullRecord) - setPartiallyNullRecord(param: PartiallyNullRecord): PartiallyNullRecord { - return param; - } - - @query([], TwoNullRecord) - getSmallNullRecord(): TwoNullRecord { - return TwoNullRecord.create({ + }; + }), + + setPartiallyNullRecord: update( + [PartiallyNullRecord], + PartiallyNullRecord, + (param) => { + return param; + } + ), + + getSmallNullRecord: query([], TwoNullRecord, () => { + return { firstItem: null, secondItem: null - }); - } + }; + }), - @update([TwoNullRecord], TwoNullRecord) - setSmallNullRecord(param: TwoNullRecord): TwoNullRecord { + setSmallNullRecord: update([TwoNullRecord], TwoNullRecord, (param) => { return param; - } + }), - @query([], ThreeNullRecord) - getLargeNullRecord(): ThreeNullRecord { - return ThreeNullRecord.create({ + getLargeNullRecord: query([], ThreeNullRecord, () => { + return { firstItem: null, secondItem: null, thirdItem: null - }); - } + }; + }), - @update([ThreeNullRecord], ThreeNullRecord) - setLargeNullRecord(param: ThreeNullRecord): ThreeNullRecord { + setLargeNullRecord: update([ThreeNullRecord], ThreeNullRecord, (param) => { return param; - } -} + }) +}); From ed528771abe8120dfd317fb2f32272bde755de7f Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 13:52:20 -0600 Subject: [PATCH 17/22] Update optional_types to functional runtime syntax --- .github/workflows/test.yml | 2 +- examples/optional_types/dfx.json | 3 +- examples/optional_types/src/index.ts | 74 ++++++++++++---------------- 3 files changed, 35 insertions(+), 44 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e9af1a6b48..478dd0a863 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,6 @@ # "examples/func_types", # "examples/generics", # "examples/motoko_examples/superheroes", # blocked by recursive -# "examples/optional_types", # "examples/outgoing_http_requests", # "examples/rejections", # "examples/robust_imports", @@ -111,6 +110,7 @@ jobs: "examples/motoko_examples/whoami", "examples/notify_raw", "examples/null_example", + "examples/optional_types", "examples/pre_and_post_upgrade", "examples/primitive_types", "examples/principal", diff --git a/examples/optional_types/dfx.json b/examples/optional_types/dfx.json index 7846397f0c..172f936b5c 100644 --- a/examples/optional_types/dfx.json +++ b/examples/optional_types/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/optional_types/optional_types.wasm.gz", + "wasm": ".azle/optional_types/optional_types.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/optional_types", "node_compatibility": true diff --git a/examples/optional_types/src/index.ts b/examples/optional_types/src/index.ts index 80415991dd..780a4a91cb 100644 --- a/examples/optional_types/src/index.ts +++ b/examples/optional_types/src/index.ts @@ -1,72 +1,62 @@ // TODO let's add more examples here, really test it out -import { Opt, query, Record, Vec, text, bool, candid, Null } from 'azle'; +import { bool, Null, Opt, query, Record, Service, text, Vec } from 'azle'; -class Element extends Record { - @candid(text) - id: string; -} +const Element = Record({ + id: text +}); -class Head extends Record { - @candid(Vec(Element)) - elements: Vec; -} +const Head = Record({ + elements: Vec(Element) +}); -class Html extends Record { - @candid(Opt(Head)) - head: Opt; -} +const Html = Record({ + head: Opt(Head) +}); -export default class { - @query([], Html) - getHtml(): Html { - return Html.create({ +export default Service({ + getHtml: query([], Html, () => { + return { head: [] - }); - } + }; + }), - @query([], Opt(Head)) - getHead(): Opt { + getHead: query([], Opt(Head), () => { return [ - Head.create({ + { elements: [] - }) + } ]; - } + }), - @query([], Opt(Head)) - getHeadWithElements(): Opt { + getHeadWithElements: query([], Opt(Head), () => { return [ - Head.create({ + { elements: [ { id: '0' } ] - }) + } ]; - } + }), - @query([Opt(Opt(Element))], Opt(Opt(Element))) - getElement(element: Opt>): Opt> { + getElement: query([Opt(Opt(Element))], Opt(Opt(Element)), (element) => { return element; - } + }), - @query([], Null) - getNull(): Null { + getNull: query([], Null, () => { return null; - } + }), - @query([], Opt(text)) - getOptNull(): Opt { + getOptNull: query([], Opt(text), () => { return []; - } + }), - @query([Opt(text)], bool) - stringToBoolean(optString: Opt): boolean { + stringToBoolean: query([Opt(text)], bool, (optString) => { if (optString.length > 0) { return true; } return false; - } -} + }) +}); From 87336105de2a47ddd6152652edd4078d2ffcf536 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 15:27:11 -0600 Subject: [PATCH 18/22] Update outgoing-http-requests to functional syntax --- .github/workflows/test.yml | 2 +- examples/outgoing_http_requests/dfx.json | 3 +- examples/outgoing_http_requests/src/index.did | 24 ++++----- examples/outgoing_http_requests/src/index.ts | 49 ++++++++++--------- 4 files changed, 42 insertions(+), 36 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 478dd0a863..e4153741a6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,6 @@ # "examples/func_types", # "examples/generics", # "examples/motoko_examples/superheroes", # blocked by recursive -# "examples/outgoing_http_requests", # "examples/rejections", # "examples/robust_imports", # "examples/run_time_errors", @@ -111,6 +110,7 @@ jobs: "examples/notify_raw", "examples/null_example", "examples/optional_types", + "examples/outgoing_http_requests", "examples/pre_and_post_upgrade", "examples/primitive_types", "examples/principal", diff --git a/examples/outgoing_http_requests/dfx.json b/examples/outgoing_http_requests/dfx.json index a142c3f081..1f2b061611 100644 --- a/examples/outgoing_http_requests/dfx.json +++ b/examples/outgoing_http_requests/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/outgoing_http_requests/outgoing_http_requests.wasm.gz", + "wasm": ".azle/outgoing_http_requests/outgoing_http_requests.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/outgoing_http_requests", "node_compatibility": true diff --git a/examples/outgoing_http_requests/src/index.did b/examples/outgoing_http_requests/src/index.did index 9447f44407..b329d2a5dd 100644 --- a/examples/outgoing_http_requests/src/index.did +++ b/examples/outgoing_http_requests/src/index.did @@ -1,14 +1,14 @@ -type rec_1 = record {value:text; name:text}; -type rec_0 = record {status:nat; body:vec nat8; headers:vec rec_1}; -type rec_3 = record {value:text; name:text}; -type rec_2 = record {status:nat; body:vec nat8; headers:vec rec_3}; -type rec_6 = record {value:text; name:text}; -type rec_5 = record {status:nat; body:vec nat8; headers:vec rec_6}; -type rec_4 = record {context:vec nat8; response:rec_5}; -type rec_8 = record {value:text; name:text}; -type rec_7 = record {status:nat; body:vec nat8; headers:vec rec_8}; +type rec_52 = record {value:text; name:text}; +type rec_51 = record {status:nat; body:vec nat8; headers:vec rec_52}; +type rec_54 = record {value:text; name:text}; +type rec_53 = record {status:nat; body:vec nat8; headers:vec rec_54}; +type rec_57 = record {value:text; name:text}; +type rec_56 = record {status:nat; body:vec nat8; headers:vec rec_57}; +type rec_55 = record {context:vec nat8; response:rec_56}; +type rec_59 = record {value:text; name:text}; +type rec_58 = record {status:nat; body:vec nat8; headers:vec rec_59}; service: () -> { - xkcd: () -> (rec_0); - xkcdRaw: () -> (rec_2); - xkcdTransform: (rec_4) -> (rec_7) query; + xkcd: () -> (rec_51); + xkcdRaw: () -> (rec_53); + xkcdTransform: (rec_55) -> (rec_58) query; } diff --git a/examples/outgoing_http_requests/src/index.ts b/examples/outgoing_http_requests/src/index.ts index 7f65abad58..e41d514cb0 100644 --- a/examples/outgoing_http_requests/src/index.ts +++ b/examples/outgoing_http_requests/src/index.ts @@ -1,12 +1,12 @@ import { ic, + Manual, + None, Principal, query, - update, Service, Some, - None, - Manual + update } from 'azle'; import { HttpResponse, @@ -14,9 +14,8 @@ import { managementCanister } from 'azle/canisters/management'; -export default class extends Service { - @update([], HttpResponse) - async xkcd(): Promise { +export default Service({ + xkcd: update([], HttpResponse, async () => { return await ic.call(managementCanister.http_request, { args: [ { @@ -28,22 +27,27 @@ export default class extends Service { headers: [], body: None, transform: Some({ - function: [ic.id(), 'xkcdTransform'], + function: [ic.id(), 'xkcdTransform'] as [ + Principal, + string + ], context: Uint8Array.from([]) }) } ], cycles: 50_000_000n }); - } + }), // TODO the replica logs give some concerning output: https://forum.dfinity.org/t/fix-me-in-http-outcalls-call-raw/19435 - @update([], HttpResponse, { manual: true }) - async xkcdRaw(): Promise> { - const httpResponse = await ic.callRaw( - Principal.fromText('aaaaa-aa'), - 'http_request', - ic.candidEncode(` + xkcdRaw: update( + [], + Manual(HttpResponse), + async () => { + const httpResponse = await ic.callRaw( + Principal.fromText('aaaaa-aa'), + 'http_request', + ic.candidEncode(` ( record { url = "https://xkcd.com/642/info.0.json"; @@ -57,17 +61,18 @@ export default class extends Service { } ) `), - 50_000_000n - ); + 50_000_000n + ); - ic.replyRaw(httpResponse); - } + ic.replyRaw(httpResponse); + }, + { manual: true } + ), - @query([HttpTransformArgs], HttpResponse) - xkcdTransform(args: HttpTransformArgs): HttpResponse { + xkcdTransform: query([HttpTransformArgs], HttpResponse, (args) => { return { ...args.response, headers: [] }; - } -} + }) +}); From d78865cbfaa01f6cb7049b767ffd68fbfee2aa18 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 16:36:18 -0600 Subject: [PATCH 19/22] Update rejections to functional runtime syntax --- .github/workflows/test.yml | 2 +- .../rejections/canisters/rejections/index.ts | 72 +++++++++---------- .../canisters/some_service/index.ts | 37 +++++----- examples/rejections/dfx.json | 6 +- .../candid/reference/variant.ts | 14 ++++ src/lib_new/ic.ts | 4 +- src/lib_new/system_types.ts | 35 +++------ 7 files changed, 88 insertions(+), 82 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e4153741a6..f90abfb0d9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -18,7 +18,6 @@ # "examples/func_types", # "examples/generics", # "examples/motoko_examples/superheroes", # blocked by recursive -# "examples/rejections", # "examples/robust_imports", # "examples/run_time_errors", # "examples/rust_type_conversions", @@ -116,6 +115,7 @@ jobs: "examples/principal", "examples/query", "examples/randomness", + "examples/rejections", "examples/service", "examples/simple_erc20", "examples/simple_user_accounts", diff --git a/examples/rejections/canisters/rejections/index.ts b/examples/rejections/canisters/rejections/index.ts index d6812446da..c7b9a3bcb6 100644 --- a/examples/rejections/canisters/rejections/index.ts +++ b/examples/rejections/canisters/rejections/index.ts @@ -1,5 +1,6 @@ import { ic, + init, Principal, RejectionCode, Service, @@ -7,65 +8,64 @@ import { update, Void } from 'azle'; -import { default as SomeService } from '../some_service'; +import SomeService from '../some_service'; -class Nonexistent extends Service { - @update([], Void) - method: () => Void; -} +const Nonexistent = Service({ + method: update([], Void) +}); -export default class extends Service { - nonexistentCanister = new Nonexistent( - Principal.fromText('rkp4c-7iaaa-aaaaa-aaaca-cai') - ); +let someService: typeof SomeService; +let nonexistentCanister: typeof Nonexistent; - someService = new SomeService( - Principal.fromText( - process.env.SOME_SERVICE_PRINCIPAL ?? - ic.trap('process.env.SOME_SERVICE_PRINCIPAL is undefined') - ) - ); +export default Service({ + init: init([], () => { + someService = SomeService( + Principal.fromText( + process.env.SOME_SERVICE_PRINCIPAL ?? + ic.trap('process.env.SOME_SERVICE_PRINCIPAL is undefined') + ) + ); - @update([], RejectionCode) - async getRejectionCodeNoError(): Promise { - await ic.call(this.someService.accept); + nonexistentCanister = Nonexistent( + Principal.fromText('rkp4c-7iaaa-aaaaa-aaaca-cai') + ); + }), + + getRejectionCodeNoError: update([], RejectionCode, async () => { + await ic.call(someService.accept); return ic.rejectCode(); - } + }), - @update([], RejectionCode) - async getRejectionCodeDestinationInvalid(): Promise { + getRejectionCodeDestinationInvalid: update([], RejectionCode, async () => { try { - await ic.call(this.nonexistentCanister.method); + await ic.call(nonexistentCanister.method); } catch (error) {} return ic.rejectCode(); - } + }), - @update([], RejectionCode) - async getRejectionCodeCanisterReject(): Promise { + getRejectionCodeCanisterReject: update([], RejectionCode, async () => { try { - await ic.call(this.someService.reject, { args: ['reject'] }); + await ic.call(someService.reject, { args: ['reject'] }); } catch (error) {} return ic.rejectCode(); - } + }), - @update([], RejectionCode) - async getRejectionCodeCanisterError(): Promise { + getRejectionCodeCanisterError: update([], RejectionCode, async () => { try { - await ic.call(this.someService.error); + await ic.call(someService.error); } catch (error) {} return ic.rejectCode(); - } + }), - @update([text], text) - async getRejectionMessage(message: text): Promise { + getRejectionMessage: update([text], text, async (message: text) => { try { - await ic.call(this.someService.reject, { args: [message] }); + await ic.call(someService.reject, { args: [message] }); } catch (error) {} return ic.rejectMessage(); - } -} + }) +}); diff --git a/examples/rejections/canisters/some_service/index.ts b/examples/rejections/canisters/some_service/index.ts index 03b309adfa..b1e9544360 100644 --- a/examples/rejections/canisters/some_service/index.ts +++ b/examples/rejections/canisters/some_service/index.ts @@ -1,20 +1,25 @@ -import { bool, empty, ic, query, Service, text } from 'azle'; +import { bool, empty, ic, Manual, query, Service, text } from 'azle'; -type Manual = void; +export default Service({ + reject: query( + [text], + Manual(empty), + (message) => { + ic.reject(message); + }, + { manual: true } + ), -export default class extends Service { - @query([text], empty, { manual: true }) - reject(message: text): Manual { - ic.reject(message); - } - - @query([], bool) - accept(): bool { + accept: query([], bool, () => { return true; - } + }), - @query([], empty, { manual: true }) - error(): Manual { - // This errors because neither ic.reject nor ic.reply were called - } -} + error: query( + [], + Manual(empty), + () => { + // This errors because neither ic.reject nor ic.reply were called + }, + { manual: true } + ) +}); diff --git a/examples/rejections/dfx.json b/examples/rejections/dfx.json index 0326e4c996..651260f12f 100644 --- a/examples/rejections/dfx.json +++ b/examples/rejections/dfx.json @@ -6,7 +6,8 @@ "root": "canisters/rejections", "ts": "canisters/rejections/index.ts", "candid": "canisters/rejections/index.did", - "wasm": ".azle/rejections/rejections.wasm.gz", + "wasm": ".azle/rejections/rejections.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/rejections", "node_compatibility": true @@ -19,7 +20,8 @@ "root": "canisters/some_service", "ts": "canisters/some_service/index.ts", "candid": "canisters/some_service/index.did", - "wasm": ".azle/some_service/some_service.wasm.gz", + "wasm": ".azle/some_service/some_service.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/some_service", "node_compatibility": true diff --git a/src/lib_functional/candid/reference/variant.ts b/src/lib_functional/candid/reference/variant.ts index f530380bb9..3bde886ec9 100644 --- a/src/lib_functional/candid/reference/variant.ts +++ b/src/lib_functional/candid/reference/variant.ts @@ -2,6 +2,7 @@ import { TypeMapping } from '..'; import { IDL } from '@dfinity/candid'; import { processMap } from '../../../lib_new/utils'; import { v4 } from 'uuid'; +import { Null } from '../../../lib_new/primitives'; export function Variant(obj: T): RequireExactlyOne<{ [K in keyof T]: TypeMapping; @@ -36,3 +37,16 @@ type RequireExactlyOne< Partial, never>>; }[KeysType] & Omit; + +/** + * Indicates an error was encountered during a canister method. + */ +export const RejectionCode = Variant({ + NoError: Null, + SysFatal: Null, + SysTransient: Null, + DestinationInvalid: Null, + CanisterReject: Null, + CanisterError: Null, + Unknown: Null +}); diff --git a/src/lib_new/ic.ts b/src/lib_new/ic.ts index 6fb7779b5f..138e740bc1 100644 --- a/src/lib_new/ic.ts +++ b/src/lib_new/ic.ts @@ -2,7 +2,7 @@ import '@dfinity/candid/lib/esm/idl'; // This must remain or the build fails import { Principal } from '@dfinity/principal'; import { IDL } from './index'; import { blob, nat, nat32, nat64, AzleNat64, Void, Opt } from './primitives'; -import { RejectionCode } from './system_types'; +import { RejectionCode } from '../lib_functional'; import { v4 } from 'uuid'; import { CandidClass, toIDLType } from './utils'; import { EncodeVisitor } from './visitors/encode_decode'; @@ -258,7 +258,7 @@ type Ic = { * call * @returns the rejection code */ - rejectCode: () => RejectionCode; + rejectCode: () => typeof RejectionCode; /** * Returns the rejection message from the most recently executed diff --git a/src/lib_new/system_types.ts b/src/lib_new/system_types.ts index 28d6fb9e30..354fb26738 100644 --- a/src/lib_new/system_types.ts +++ b/src/lib_new/system_types.ts @@ -1,32 +1,17 @@ -import { candid } from './index'; -import { Null } from './primitives'; -import { Variant } from './variant'; +import { RequireExactlyOne } from './variant'; /** * Indicates an error was encountered during a canister method. */ -export class RejectionCode extends Variant { - @candid(Null) - NoError?: null; - - @candid(Null) - SysFatal?: null; - - @candid(Null) - SysTransient?: null; - - @candid(Null) - DestinationInvalid?: null; - - @candid(Null) - CanisterReject?: null; - - @candid(Null) - CanisterError?: null; - - @candid(Null) - Unknown?: null; -} +export type RejectionCode = RequireExactlyOne<{ + NoError: null; + SysFatal: null; + SysTransient: null; + DestinationInvalid: null; + CanisterReject: null; + CanisterError: null; + Unknown: null; +}>; // TODO we have decided to not use callresult or notifyresult // TODO remove once we are more mature in the project From 08ceb35624c9a2b1a72bcd9ed5ccc93f81785d6e Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 16:36:50 -0600 Subject: [PATCH 20/22] Delete rust_type_conversions example See https://github.com/demergent-labs/azle/issues/1240 --- .github/workflows/test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f90abfb0d9..a28f3c936d 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -20,7 +20,6 @@ # "examples/motoko_examples/superheroes", # blocked by recursive # "examples/robust_imports", # "examples/run_time_errors", -# "examples/rust_type_conversions", # "examples/tuple_types", name: Azle Tests From cde7785f8062ef47fd1d26b63eb31c22c90b4623 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Tue, 26 Sep 2023 11:56:46 -0600 Subject: [PATCH 21/22] Remove logs from StableBTreeMap.insert --- src/lib_new/stable_b_tree_map.ts | 13 ------------- 1 file changed, 13 deletions(-) diff --git a/src/lib_new/stable_b_tree_map.ts b/src/lib_new/stable_b_tree_map.ts index 5583eaf250..eab2ef0b62 100644 --- a/src/lib_new/stable_b_tree_map.ts +++ b/src/lib_new/stable_b_tree_map.ts @@ -80,29 +80,18 @@ export function StableBTreeMap( key: TypeMapping, value: TypeMapping ): Opt> { - console.log('keyIdl'); - console.log(keyIdl); - console.log('valueIdl'); - console.log(valueIdl); - const candidEncodedMemoryId = new Uint8Array( IDL.encode([IDL.Nat8], [memoryId]) ).buffer; - console.log(0); - const candidEncodedKey = new Uint8Array( IDL.encode([keyIdl as any], [key]) ).buffer; - console.log(1); - const candidEncodedValue = new Uint8Array( IDL.encode([valueIdl as any], [value]) ).buffer; - console.log(2); - const candidEncodedResultValue = ( globalThis as any )._azleIc.stableBTreeMapInsert( @@ -111,8 +100,6 @@ export function StableBTreeMap( candidEncodedValue ); - console.log(3); - if (candidEncodedResultValue === undefined) { return []; } else { From 3022db48b6c13c2bca5cbc933539bfe9e12b185e Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Tue, 26 Sep 2023 12:05:17 -0600 Subject: [PATCH 22/22] Align spacing with Jordan's examples --- examples/motoko_examples/calc/src/index.ts | 4 ---- examples/motoko_examples/counter/src/index.ts | 2 -- examples/motoko_examples/http_counter/src/index.ts | 3 --- .../minimal-counter-dapp/src/minimal_dapp/index.ts | 2 -- examples/motoko_examples/persistent-storage/src/index.ts | 3 --- .../motoko_examples/phone-book/src/phone_book/index.ts | 1 - examples/motoko_examples/simple-to-do/src/index.ts | 4 ---- .../motoko_examples/superheroes/src/superheroes/index.ts | 3 --- examples/motoko_examples/whoami/src/index.ts | 6 ------ examples/notify_raw/canisters/canister2/index.ts | 1 - examples/null_example/src/index.ts | 7 ------- examples/optional_types/src/index.ts | 6 ------ examples/outgoing_http_requests/src/index.ts | 2 -- examples/rejections/canisters/rejections/index.ts | 5 ----- 14 files changed, 49 deletions(-) diff --git a/examples/motoko_examples/calc/src/index.ts b/examples/motoko_examples/calc/src/index.ts index 99f6b10fa9..2e49226add 100644 --- a/examples/motoko_examples/calc/src/index.ts +++ b/examples/motoko_examples/calc/src/index.ts @@ -8,19 +8,16 @@ export default Service({ return cell; }), - sub: update([int], int, (n) => { cell -= n; return cell; }), - mul: update([int], int, (n) => { cell *= n; return cell; }), - div: update([int], Opt(int), (n) => { if (n === 0n) { return None; @@ -29,7 +26,6 @@ export default Service({ return Some(cell); } }), - clearall: update([], Void, () => { cell = 0n; }) diff --git a/examples/motoko_examples/counter/src/index.ts b/examples/motoko_examples/counter/src/index.ts index 8e801a61c6..989f0daff2 100644 --- a/examples/motoko_examples/counter/src/index.ts +++ b/examples/motoko_examples/counter/src/index.ts @@ -6,11 +6,9 @@ export default Service({ get: query([], nat, () => { return counter; }), - set: update([nat], Void, (n) => { counter = n; }), - inc: update([], Void, () => { counter += 1n; }) diff --git a/examples/motoko_examples/http_counter/src/index.ts b/examples/motoko_examples/http_counter/src/index.ts index a1c3978df3..6634ea591f 100644 --- a/examples/motoko_examples/http_counter/src/index.ts +++ b/examples/motoko_examples/http_counter/src/index.ts @@ -65,7 +65,6 @@ export default Service({ init: init([], () => { stableStorage.insert('counter', 0n); }), - http_request: query([HttpRequest], HttpResponse, (req) => { console.log('Hello from http_request'); @@ -141,7 +140,6 @@ export default Service({ upgrade: None }; }), - http_request_update: update([HttpRequest], HttpResponse, (req) => { if (req.method === 'POST') { const counterOpt = stableStorage.get('counter'); @@ -196,7 +194,6 @@ export default Service({ upgrade: None }; }), - http_streaming: query([Token], StreamingCallbackHttpResponse, (token) => { console.log('Hello from http_streaming'); switch (token.arbitrary_data) { diff --git a/examples/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts b/examples/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts index c6049e0a23..ecc2b4f330 100644 --- a/examples/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts +++ b/examples/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts @@ -8,11 +8,9 @@ export default Service({ return counter; }), - getCount: query([], nat, () => { return counter; }), - reset: update([], nat, () => { counter = 0n; diff --git a/examples/motoko_examples/persistent-storage/src/index.ts b/examples/motoko_examples/persistent-storage/src/index.ts index ffc0aaae4b..a80c4e4b15 100644 --- a/examples/motoko_examples/persistent-storage/src/index.ts +++ b/examples/motoko_examples/persistent-storage/src/index.ts @@ -15,7 +15,6 @@ export default Service({ init: init([], () => { stableStorage.insert('counter', 0n); }), - increment: update([], nat, () => { const counterOpt = stableStorage.get('counter'); const counter = @@ -27,7 +26,6 @@ export default Service({ return counter; }), - get: query([], nat, () => { const counterOpt = stableStorage.get('counter'); const counter = @@ -37,7 +35,6 @@ export default Service({ return counter; }), - reset: update([], nat, () => { stableStorage.insert('counter', 0n); diff --git a/examples/motoko_examples/phone-book/src/phone_book/index.ts b/examples/motoko_examples/phone-book/src/phone_book/index.ts index a34cc9097e..fcd5c82f7b 100644 --- a/examples/motoko_examples/phone-book/src/phone_book/index.ts +++ b/examples/motoko_examples/phone-book/src/phone_book/index.ts @@ -21,7 +21,6 @@ export default Service({ insert: update([text, Entry], Void, (name, entry) => { phoneBook.set(name, entry); }), - lookup: query([text], Opt(Entry), (name) => { const entryOrUndefined = phoneBook.get(name); diff --git a/examples/motoko_examples/simple-to-do/src/index.ts b/examples/motoko_examples/simple-to-do/src/index.ts index e7cb4dc6fd..a24558d7f2 100644 --- a/examples/motoko_examples/simple-to-do/src/index.ts +++ b/examples/motoko_examples/simple-to-do/src/index.ts @@ -22,7 +22,6 @@ export default Service({ getTodos: query([], Vec(ToDo), () => { return Array.from(todos.values()); }), - // Returns the ID that was given to the ToDo item addTodo: update([text], nat, (description) => { const id = nextId; @@ -34,7 +33,6 @@ export default Service({ return id; }), - completeTodo: update([nat], Void, (id) => { let todo = todos.get(id); @@ -45,7 +43,6 @@ export default Service({ }); } }), - showTodos: query([], text, () => { let output = '\n___TO-DOs___'; for (const todoEntry of [...todos]) { @@ -56,7 +53,6 @@ export default Service({ } return output; }), - clearCompleted: update([], Void, () => { // NOTE: this syntax isn't supported in Boa. If we revert to using Boa // we'll need to revert the syntax to: diff --git a/examples/motoko_examples/superheroes/src/superheroes/index.ts b/examples/motoko_examples/superheroes/src/superheroes/index.ts index ee859d087e..f9f4e78828 100644 --- a/examples/motoko_examples/superheroes/src/superheroes/index.ts +++ b/examples/motoko_examples/superheroes/src/superheroes/index.ts @@ -50,13 +50,11 @@ export default Service({ return superheroId; }), - // Read a superhero. read: query([SuperheroId], Opt(Superhero), (superheroId) => { const superheroOrUndefined = superheroes.get(superheroId); return superheroOrUndefined ? Some(superheroOrUndefined) : None; }), - // Update a superhero. update: update([SuperheroId, Superhero], bool, (superheroId, superhero) => { let result = superheroes.get(superheroId); @@ -66,7 +64,6 @@ export default Service({ return !!result; }), - // Delete a superhero. deleteHero: update([SuperheroId], bool, (superheroId) => { let result = superheroes.get(superheroId); diff --git a/examples/motoko_examples/whoami/src/index.ts b/examples/motoko_examples/whoami/src/index.ts index f58727736a..5d6a8ddd45 100644 --- a/examples/motoko_examples/whoami/src/index.ts +++ b/examples/motoko_examples/whoami/src/index.ts @@ -23,28 +23,23 @@ export default Service({ install = ic.caller(); someone = somebody; }), - // Manually re-save these variables after new deploys. postUpgrade: postUpgrade([principal], (somebody) => { install = ic.caller(); someone = somebody; }), - // Return the principal identifier of the wallet canister that installed this // canister. installer: query([], principal, () => { return install; }), - // Return the principal identifier that was provided as an installation // argument to this canister. argument: query([], principal, () => { return someone; }), - // Return the principal identifier of the caller of this method. whoami, - // Return the principal identifier of this canister. id: update([], principal, async () => { // TODO This is not an ideal solution but will work for now @@ -54,7 +49,6 @@ export default Service({ return await ic.call(self.whoami); }), - // Return the principal identifier of this canister via the global `ic` object. // This is much quicker than `id()` above because it isn't making a cross- // canister call to itself. Additionally, it can now be a `Query` which means it diff --git a/examples/notify_raw/canisters/canister2/index.ts b/examples/notify_raw/canisters/canister2/index.ts index 3466309b16..dc2581be94 100644 --- a/examples/notify_raw/canisters/canister2/index.ts +++ b/examples/notify_raw/canisters/canister2/index.ts @@ -6,7 +6,6 @@ export default Service({ receiveNotification: update([], Void, () => { notified = true; }), - getNotified: query([], bool, () => { return notified; }) diff --git a/examples/null_example/src/index.ts b/examples/null_example/src/index.ts index 19dd05b895..6df8ab67fe 100644 --- a/examples/null_example/src/index.ts +++ b/examples/null_example/src/index.ts @@ -21,13 +21,11 @@ export default Service({ nullFunction: query([Null], Null, (param) => { return param; }), - voidIsNotNull: query([], Void, () => { ic.print( 'Even though they are both None in Python, for Candid null and void are different.' ); }), - getPartiallyNullRecord: query([], PartiallyNullRecord, () => { return { firstItem: 1n, @@ -35,7 +33,6 @@ export default Service({ thirdItem: 3n }; }), - setPartiallyNullRecord: update( [PartiallyNullRecord], PartiallyNullRecord, @@ -43,18 +40,15 @@ export default Service({ return param; } ), - getSmallNullRecord: query([], TwoNullRecord, () => { return { firstItem: null, secondItem: null }; }), - setSmallNullRecord: update([TwoNullRecord], TwoNullRecord, (param) => { return param; }), - getLargeNullRecord: query([], ThreeNullRecord, () => { return { firstItem: null, @@ -62,7 +56,6 @@ export default Service({ thirdItem: null }; }), - setLargeNullRecord: update([ThreeNullRecord], ThreeNullRecord, (param) => { return param; }) diff --git a/examples/optional_types/src/index.ts b/examples/optional_types/src/index.ts index 780a4a91cb..667e2b04be 100644 --- a/examples/optional_types/src/index.ts +++ b/examples/optional_types/src/index.ts @@ -20,7 +20,6 @@ export default Service({ head: [] }; }), - getHead: query([], Opt(Head), () => { return [ { @@ -28,7 +27,6 @@ export default Service({ } ]; }), - getHeadWithElements: query([], Opt(Head), () => { return [ { @@ -40,19 +38,15 @@ export default Service({ } ]; }), - getElement: query([Opt(Opt(Element))], Opt(Opt(Element)), (element) => { return element; }), - getNull: query([], Null, () => { return null; }), - getOptNull: query([], Opt(text), () => { return []; }), - stringToBoolean: query([Opt(text)], bool, (optString) => { if (optString.length > 0) { return true; diff --git a/examples/outgoing_http_requests/src/index.ts b/examples/outgoing_http_requests/src/index.ts index e41d514cb0..7ff6d68109 100644 --- a/examples/outgoing_http_requests/src/index.ts +++ b/examples/outgoing_http_requests/src/index.ts @@ -38,7 +38,6 @@ export default Service({ cycles: 50_000_000n }); }), - // TODO the replica logs give some concerning output: https://forum.dfinity.org/t/fix-me-in-http-outcalls-call-raw/19435 xkcdRaw: update( [], @@ -68,7 +67,6 @@ export default Service({ }, { manual: true } ), - xkcdTransform: query([HttpTransformArgs], HttpResponse, (args) => { return { ...args.response, diff --git a/examples/rejections/canisters/rejections/index.ts b/examples/rejections/canisters/rejections/index.ts index c7b9a3bcb6..07beff84d1 100644 --- a/examples/rejections/canisters/rejections/index.ts +++ b/examples/rejections/canisters/rejections/index.ts @@ -30,13 +30,11 @@ export default Service({ Principal.fromText('rkp4c-7iaaa-aaaaa-aaaca-cai') ); }), - getRejectionCodeNoError: update([], RejectionCode, async () => { await ic.call(someService.accept); return ic.rejectCode(); }), - getRejectionCodeDestinationInvalid: update([], RejectionCode, async () => { try { await ic.call(nonexistentCanister.method); @@ -44,7 +42,6 @@ export default Service({ return ic.rejectCode(); }), - getRejectionCodeCanisterReject: update([], RejectionCode, async () => { try { await ic.call(someService.reject, { args: ['reject'] }); @@ -52,7 +49,6 @@ export default Service({ return ic.rejectCode(); }), - getRejectionCodeCanisterError: update([], RejectionCode, async () => { try { await ic.call(someService.error); @@ -60,7 +56,6 @@ export default Service({ return ic.rejectCode(); }), - getRejectionMessage: update([text], text, async (message: text) => { try { await ic.call(someService.reject, { args: [message] });