Skip to content

Commit

Permalink
make whoami and composite queries examples non-recursive, fix types u…
Browse files Browse the repository at this point in the history
…p a bit
  • Loading branch information
lastmjs committed Oct 1, 2023
1 parent d8a8a5e commit 0fdec36
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 47 deletions.
3 changes: 1 addition & 2 deletions examples/composite_queries/src/canister1/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -64,12 +64,11 @@ const CompQueryCanister = Canister({
}),
// Composite query calling queries on the same canister
incCanister1: query([], nat, async () => {
// TODO This is not an ideal solution but will work for now
const self: any = CompQueryCanister(ic.id());

counter += 1n;

const canister1AResult: any = await ic.call(self.incCounter);
const canister1AResult = await ic.call(self.incCounter);
const canister1BResult = await ic.call(self.incCounter);

return counter + canister1AResult + canister1BResult;
Expand Down
2 changes: 1 addition & 1 deletion examples/motoko_examples/whoami/src/index.did
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,4 @@ service: (principal) -> {
idQuick: () -> (principal) query;
installer: () -> (principal) query;
whoami: () -> (principal);
}
}
80 changes: 38 additions & 42 deletions examples/motoko_examples/whoami/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,55 +5,51 @@ import {
postUpgrade,
Principal,
query,
Recursive,
update,
Void
update
} from 'azle';

// 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 = Recursive(() =>
Canister({
// Manually save the calling principal and argument for later access.
init: init([Principal], (somebody) => {
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: update([], Principal, () => {
return ic.caller();
}),
// Return the principal identifier of this canister.
id: update([], Principal, async () => {
const self = WhoAmI(ic.id());
const WhoAmI = Canister({
// Manually save the calling principal and argument for later access.
init: init([Principal], (somebody) => {
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: update([], Principal, () => {
return ic.caller();
}),
// Return the principal identifier of this canister.
id: update([], Principal, async () => {
const self: any = 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.
idQuick: query([], Principal, () => {
return 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.
idQuick: query([], Principal, () => {
return ic.id();
})
);
});

export default WhoAmI;
4 changes: 2 additions & 2 deletions src/lib_new/ic.ts
Original file line number Diff line number Diff line change
Expand Up @@ -442,8 +442,8 @@ type Ic = {
trap: (message: string) => never;
};

type ArgsType<T> = T extends (...args: infer U) => any ? U : never;
type ReturnTypeOf<T> = T extends (...args: any[]) => infer R ? R : never;
type ArgsType<T> = T extends (...args: infer U) => any ? U : any;
type ReturnTypeOf<T> = T extends (...args: any[]) => infer R ? R : any;
type ReturnTypeOfPromise<T> = T extends (...args: any[]) => infer R
? Promise<R>
: never;
Expand Down

0 comments on commit 0fdec36

Please sign in to comment.