diff --git a/examples/composite_queries/src/canister1/index.ts b/examples/composite_queries/src/canister1/index.ts index a186c4994f..69a13742a6 100644 --- a/examples/composite_queries/src/canister1/index.ts +++ b/examples/composite_queries/src/canister1/index.ts @@ -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; diff --git a/examples/motoko_examples/whoami/src/index.did b/examples/motoko_examples/whoami/src/index.did index 938102e04d..29556c8d4c 100644 --- a/examples/motoko_examples/whoami/src/index.did +++ b/examples/motoko_examples/whoami/src/index.did @@ -4,4 +4,4 @@ service: (principal) -> { idQuick: () -> (principal) query; installer: () -> (principal) query; whoami: () -> (principal); -} \ No newline at end of file +} diff --git a/examples/motoko_examples/whoami/src/index.ts b/examples/motoko_examples/whoami/src/index.ts index 16387afade..da1e1b0e2b 100644 --- a/examples/motoko_examples/whoami/src/index.ts +++ b/examples/motoko_examples/whoami/src/index.ts @@ -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; diff --git a/examples/recursion/src/recursion/index.ts b/examples/recursion/src/recursion/index.ts index 4959221a93..5548c9f77c 100644 --- a/examples/recursion/src/recursion/index.ts +++ b/examples/recursion/src/recursion/index.ts @@ -20,7 +20,7 @@ import MyFullCanister from '../recursive_canister'; // These are the types that can be recursive // Record // Record can't be recursive by itself. It needs something to be able to terminate it. It needs to work with Variants, Opts, and Vec -const varRecord = Recursive(() => Record({ myVar: myVar })); +const varRecord = Recursive(() => Record({ myVar })); const vecRecord = Recursive(() => Record({ myVecRecords: Vec(vecRecord) })); const optRecord = Recursive(() => Record({ myOpt: Opt(optRecord) })); const myVar = Variant({ num: int8, varRec: varRecord }); @@ -33,7 +33,7 @@ const recVariant = Recursive(() => const optTuple = Recursive(() => Tuple(Opt(optTuple), Opt(optTuple))); const vecTuple = Recursive(() => Tuple(Vec(vecTuple), Vec(vecTuple))); const varTuple = Recursive(() => Tuple(myTupleVar, myTupleVar)); -const myTupleVar = Variant({ num: int8, varTuple: varTuple }); +const myTupleVar = Variant({ num: int8, varTuple }); // Vec // Vec can't be recursive by itself. At the end of it all it needs to have a concrete type. // Opt diff --git a/src/lib_functional/candid/index.ts b/src/lib_functional/candid/index.ts index 8932efd828..7a131edee7 100644 --- a/src/lib_functional/candid/index.ts +++ b/src/lib_functional/candid/index.ts @@ -77,7 +77,7 @@ export type TypeMapping = T extends () => any : T extends AzleVoid ? void : T extends AzleTuple - ? { [K in keyof U]: TypeMapping } + ? { [K in keyof U]: U[K] extends any ? any : TypeMapping } : T extends AzleVec ? TypeMapping[] : T extends AzleOpt diff --git a/src/lib_functional/candid/reference/recursive.ts b/src/lib_functional/candid/reference/recursive.ts index 83ee4a69f3..71ae6a3a43 100644 --- a/src/lib_functional/candid/reference/recursive.ts +++ b/src/lib_functional/candid/reference/recursive.ts @@ -2,7 +2,7 @@ import { v4 } from 'uuid'; import { IDL } from '@dfinity/candid'; import { Parent } from '../../../lib_new/utils'; -export function Recursive any>(idlCallback: T): T { +export function Recursive(idlCallback: any): any { const name = v4(); let result = (...args: any[]) => { diff --git a/src/lib_functional/canister_methods/index.ts b/src/lib_functional/canister_methods/index.ts index ecf236a735..70bdc97c0a 100644 --- a/src/lib_functional/canister_methods/index.ts +++ b/src/lib_functional/canister_methods/index.ts @@ -1,6 +1,6 @@ import { IDL } from '../../lib_new/index'; import { ic } from '../../lib_new/ic'; -import { TypeMapping } from '..'; +import { CandidType, TypeMapping } from '..'; import { DecodeVisitor, EncodeVisitor @@ -33,7 +33,10 @@ export type CanisterMethodInfo, K> = { guard: (() => any) | undefined; }; -export type Callback, Return> = ( +export type Callback< + Params extends ReadonlyArray, + Return extends CandidType +> = ( ...args: { [K in keyof Params]: TypeMapping } ) => TypeMapping | Promise>; diff --git a/src/lib_functional/canister_methods/query.ts b/src/lib_functional/canister_methods/query.ts index 20d9940744..bbf56bc58c 100644 --- a/src/lib_functional/canister_methods/query.ts +++ b/src/lib_functional/canister_methods/query.ts @@ -6,7 +6,7 @@ import { createParents, executeMethod } from '.'; -import { CandidType, RecursiveType, TypeMapping } from '../candid'; +import { CandidType, TypeMapping } from '../candid'; import { toParamIDLTypes, toReturnIDLType } from '../../lib_new/utils'; export function query< diff --git a/src/lib_new/ic.ts b/src/lib_new/ic.ts index ac17a075fb..2fd19746c3 100644 --- a/src/lib_new/ic.ts +++ b/src/lib_new/ic.ts @@ -442,8 +442,8 @@ type Ic = { trap: (message: string) => never; }; -type ArgsType = T extends (...args: infer U) => any ? U : never; -type ReturnTypeOf = T extends (...args: any[]) => infer R ? R : never; +type ArgsType = T extends (...args: infer U) => any ? U : any; +type ReturnTypeOf = T extends (...args: any[]) => infer R ? R : any; type ReturnTypeOfPromise = T extends (...args: any[]) => infer R ? Promise : never;