From e1af7e4ca181d33acdad8eac2104aa2c917945b8 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Mon, 25 Sep 2023 12:56:37 -0600 Subject: [PATCH] Update whoami to functional runtime syntax Blocked by postUpgrade function. --- examples/motoko_examples/whoami/dfx.json | 3 +- examples/motoko_examples/whoami/src/index.ts | 74 +++++++++----------- 2 files changed, 37 insertions(+), 40 deletions(-) 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(); - } -} + }) +});