Skip to content

Commit

Permalink
Update whoami to functional runtime syntax
Browse files Browse the repository at this point in the history
Blocked by postUpgrade function.
  • Loading branch information
dansteren committed Sep 25, 2023
1 parent ba87aaf commit e1af7e4
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 40 deletions.
3 changes: 2 additions & 1 deletion examples/motoko_examples/whoami/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/whoami/whoami.wasm.gz",
"wasm": ".azle/whoami/whoami.wasm",
"gzip": true,
"declarations": {
"output": "test/dfx_generated/whoami",
"node_compatibility": true
Expand Down
74 changes: 35 additions & 39 deletions examples/motoko_examples/whoami/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<Principal> {
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();
}
}
})
});

0 comments on commit e1af7e4

Please sign in to comment.