From a80eb9b596a7889dc93d2fae3e282fd9605507f6 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 4 Oct 2023 16:14:53 -0600 Subject: [PATCH 1/2] add opt and vec tests --- examples/recursion/src/recursion/index.did | 126 +++++++++++-------- examples/recursion/src/recursion/index.ts | 22 +++- examples/recursion/test/tests.ts | 133 ++++++++++++++++++++- 3 files changed, 223 insertions(+), 58 deletions(-) diff --git a/examples/recursion/src/recursion/index.did b/examples/recursion/src/recursion/index.did index 2070c9fbaa..49134618d7 100644 --- a/examples/recursion/src/recursion/index.did +++ b/examples/recursion/src/recursion/index.did @@ -1,53 +1,77 @@ -type rec_21 = func (rec_21) -> (rec_21) query; -type rec_22 = func (rec_22) -> (rec_22) query; -type rec_23 = func (rec_23) -> (rec_23) query; -type rec_0 = record {myOpt:opt rec_0}; -type rec_1 = record {myOpt:opt rec_1}; -type rec_12 = record {myOpt:opt rec_12}; -type rec_4 = record {myVar:variant {num:int8; varRec:rec_4}}; -type rec_5 = record {myVar:variant {num:int8; varRec:rec_5}}; -type rec_14 = record {myVar:variant {num:int8; varRec:rec_14}}; -type rec_2 = record {myVecRecords:vec rec_2}; -type rec_3 = record {myVecRecords:vec rec_3}; -type rec_13 = record {myVecRecords:vec rec_13}; -type rec_26 = service {getMessage: () -> (text) query; myQuery: (rec_26) -> (rec_26) query;}; -type rec_27 = service {getMessage: () -> (text) query; myQuery: (rec_27) -> (rec_27) query;}; -type rec_29 = service {getMessage: () -> (text) query; myQuery: (rec_29) -> (rec_29) query;}; -type rec_30 = service {getMessage: () -> (text) query; myQuery: (rec_30) -> (rec_30) query;}; -type rec_28 = service {getMessage: () -> (text) query; myQuery: (rec_28) -> (rec_28) query;}; -type rec_24 = service {myQuery: (rec_24) -> (rec_24) query;}; -type rec_25 = service {myQuery: (rec_25) -> (rec_25) query;}; -type rec_8 = record {opt rec_8; opt rec_8}; -type rec_9 = record {opt rec_9; opt rec_9}; -type rec_16 = record {opt rec_16; opt rec_16}; -type rec_18 = record {variant {num:int8; varTuple:rec_18}; variant {num:int8; varTuple:rec_18}}; -type rec_19 = record {variant {num:int8; varTuple:rec_19}; variant {num:int8; varTuple:rec_19}}; -type rec_20 = record {variant {num:int8; varTuple:rec_20}; variant {num:int8; varTuple:rec_20}}; -type rec_10 = record {vec rec_10; vec rec_10}; -type rec_11 = record {vec rec_11; vec rec_11}; -type rec_17 = record {vec rec_17; vec rec_17}; -type rec_6 = variant {num:int8; recVariant:rec_6}; -type rec_7 = variant {num:int8; recVariant:rec_7}; -type rec_15 = variant {num:int8; recVariant:rec_15}; +type rec_37 = func (rec_37) -> (rec_37) query; +type rec_38 = func (rec_38) -> (rec_38) query; +type rec_39 = func (rec_39) -> (rec_39) query; +type rec_10 = opt rec_10; +type rec_11 = opt rec_11; +type rec_12 = opt record {rec_12; rec_12}; +type rec_13 = opt record {rec_13; rec_13}; +type rec_8 = opt variant {Leaf:int8; Branch:rec_8}; +type rec_9 = opt variant {Leaf:int8; Branch:rec_9}; +type rec_14 = opt vec rec_14; +type rec_15 = opt vec rec_15; +type rec_16 = record {myOpt:opt rec_16}; +type rec_17 = record {myOpt:opt rec_17}; +type rec_28 = record {myOpt:opt rec_28}; +type rec_20 = record {myVar:variant {num:int8; varRec:rec_20}}; +type rec_21 = record {myVar:variant {num:int8; varRec:rec_21}}; +type rec_30 = record {myVar:variant {num:int8; varRec:rec_30}}; +type rec_18 = record {myVecRecords:vec rec_18}; +type rec_19 = record {myVecRecords:vec rec_19}; +type rec_29 = record {myVecRecords:vec rec_29}; +type rec_42 = service {getMessage: () -> (text) query; myQuery: (rec_42) -> (rec_42) query;}; +type rec_43 = service {getMessage: () -> (text) query; myQuery: (rec_43) -> (rec_43) query;}; +type rec_45 = service {getMessage: () -> (text) query; myQuery: (rec_45) -> (rec_45) query;}; +type rec_46 = service {getMessage: () -> (text) query; myQuery: (rec_46) -> (rec_46) query;}; +type rec_44 = service {getMessage: () -> (text) query; myQuery: (rec_44) -> (rec_44) query;}; +type rec_40 = service {myQuery: (rec_40) -> (rec_40) query;}; +type rec_41 = service {myQuery: (rec_41) -> (rec_41) query;}; +type rec_24 = record {opt rec_24; opt rec_24}; +type rec_25 = record {opt rec_25; opt rec_25}; +type rec_32 = record {opt rec_32; opt rec_32}; +type rec_34 = record {variant {num:int8; varTuple:rec_34}; variant {num:int8; varTuple:rec_34}}; +type rec_35 = record {variant {num:int8; varTuple:rec_35}; variant {num:int8; varTuple:rec_35}}; +type rec_36 = record {variant {num:int8; varTuple:rec_36}; variant {num:int8; varTuple:rec_36}}; +type rec_26 = record {vec rec_26; vec rec_26}; +type rec_27 = record {vec rec_27; vec rec_27}; +type rec_33 = record {vec rec_33; vec rec_33}; +type rec_22 = variant {num:int8; recVariant:rec_22}; +type rec_23 = variant {num:int8; recVariant:rec_23}; +type rec_31 = variant {num:int8; recVariant:rec_31}; +type rec_2 = vec opt rec_2; +type rec_3 = vec opt rec_3; +type rec_4 = vec record {rec_4; rec_4}; +type rec_5 = vec record {rec_5; rec_5}; +type rec_0 = vec variant {Leaf:int8; Branch:rec_0}; +type rec_1 = vec variant {Leaf:int8; Branch:rec_1}; +type rec_6 = vec rec_6; +type rec_7 = vec rec_7; service: () -> { - testRecFunc: (rec_21) -> (rec_22) query; - testRecFuncReturn: () -> (rec_23) query; - testRecRecordWithOpt: (rec_0) -> (rec_1) query; - testRecRecordWithOptReturn: () -> (rec_12) query; - testRecRecordWithVariant: (rec_4) -> (rec_5) query; - testRecRecordWithVariantReturn: () -> (rec_14) query; - testRecRecordWithVec: (rec_2) -> (rec_3) query; - testRecRecordWithVecReturn: () -> (rec_13) query; - testRecService: (rec_26) -> (rec_27) query; - testRecServiceCall: (rec_29) -> (rec_30); - testRecServiceReturn: () -> (rec_28) query; - testRecServiceSimple: (rec_24) -> (rec_25) query; - testRecTupleWithOpt: (rec_8) -> (rec_9) query; - testRecTupleWithOptReturn: () -> (rec_16) query; - testRecTupleWithVariant: (rec_18) -> (rec_19) query; - testRecTupleWithVariantReturn: () -> (rec_20) query; - testRecTupleWithVec: (rec_10) -> (rec_11) query; - testRecTupleWithVecReturn: () -> (rec_17) query; - testRecVariant: (rec_6) -> (rec_7) query; - testRecVariantReturn: () -> (rec_15) query; + testRecFunc: (rec_37) -> (rec_38) query; + testRecFuncReturn: () -> (rec_39) query; + testRecOptWithOpt: (rec_10) -> (rec_11) query; + testRecOptWithTuple: (rec_12) -> (rec_13) query; + testRecOptWithVariant: (rec_8) -> (rec_9) query; + testRecOptWithVec: (rec_14) -> (rec_15) query; + testRecRecordWithOpt: (rec_16) -> (rec_17) query; + testRecRecordWithOptReturn: () -> (rec_28) query; + testRecRecordWithVariant: (rec_20) -> (rec_21) query; + testRecRecordWithVariantReturn: () -> (rec_30) query; + testRecRecordWithVec: (rec_18) -> (rec_19) query; + testRecRecordWithVecReturn: () -> (rec_29) query; + testRecService: (rec_42) -> (rec_43) query; + testRecServiceCall: (rec_45) -> (rec_46); + testRecServiceReturn: () -> (rec_44) query; + testRecServiceSimple: (rec_40) -> (rec_41) query; + testRecTupleWithOpt: (rec_24) -> (rec_25) query; + testRecTupleWithOptReturn: () -> (rec_32) query; + testRecTupleWithVariant: (rec_34) -> (rec_35) query; + testRecTupleWithVariantReturn: () -> (rec_36) query; + testRecTupleWithVec: (rec_26) -> (rec_27) query; + testRecTupleWithVecReturn: () -> (rec_33) query; + testRecVariant: (rec_22) -> (rec_23) query; + testRecVariantReturn: () -> (rec_31) query; + testRecVecWithOpt: (rec_2) -> (rec_3) query; + testRecVecWithTuple: (rec_4) -> (rec_5) query; + testRecVecWithVariant: (rec_0) -> (rec_1) query; + testRecVecWithVec: (rec_6) -> (rec_7) query; } diff --git a/examples/recursion/src/recursion/index.ts b/examples/recursion/src/recursion/index.ts index 5548c9f77c..0ffb600640 100644 --- a/examples/recursion/src/recursion/index.ts +++ b/examples/recursion/src/recursion/index.ts @@ -20,10 +20,11 @@ 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 })); +const varRecord = Recursive(() => + Record({ myVar: Variant({ num: int8, varRec: varRecord }) }) +); const vecRecord = Recursive(() => Record({ myVecRecords: Vec(vecRecord) })); const optRecord = Recursive(() => Record({ myOpt: Opt(optRecord) })); -const myVar = Variant({ num: int8, varRec: varRecord }); // Variant // Variant is the only type that can be recursive all by itself but it does need a way to end the recursion const recVariant = Recursive(() => @@ -35,8 +36,15 @@ const vecTuple = Recursive(() => Tuple(Vec(vecTuple), Vec(vecTuple))); const varTuple = Recursive(() => Tuple(myTupleVar, myTupleVar)); 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. +const varVec = Recursive(() => Vec(Variant({ Leaf: int8, Branch: varVec }))); +const optVec = Recursive(() => Vec(Opt(optVec))); +const tupleVec = Recursive(() => Vec(Tuple(tupleVec, tupleVec))); +const vecVec = Recursive(() => Vec(vecVec)); // Opt +const varOpt = Recursive(() => Opt(Variant({ Leaf: int8, Branch: varOpt }))); +const optOpt = Recursive(() => Opt(optOpt)); +const tupleOpt = Recursive(() => Opt(Tuple(tupleOpt, tupleOpt))); +const vecOpt = Recursive(() => Opt(Vec(vecOpt))); // Service const MyCanister = Recursive(() => Canister({ @@ -47,6 +55,14 @@ const MyCanister = Recursive(() => const myFunc = Recursive(() => Func([myFunc], myFunc, 'query')); export default Canister({ + testRecVecWithVariant: query([varVec], varVec, (param) => param), + testRecVecWithOpt: query([optVec], optVec, (param) => param), + testRecVecWithTuple: query([tupleVec], tupleVec, (param) => param), + testRecVecWithVec: query([vecVec], vecVec, (param) => param), + testRecOptWithVariant: query([varOpt], varOpt, (param) => param), + testRecOptWithOpt: query([optOpt], optOpt, (param) => param), + testRecOptWithTuple: query([tupleOpt], tupleOpt, (param) => param), + testRecOptWithVec: query([vecOpt], vecOpt, (param) => param), testRecRecordWithOpt: query([optRecord], optRecord, (param) => param), testRecRecordWithVec: query([vecRecord], vecRecord, (param) => param), testRecRecordWithVariant: query([varRecord], varRecord, (param) => param), diff --git a/examples/recursion/test/tests.ts b/examples/recursion/test/tests.ts index f901177a10..ee1f2e50a0 100644 --- a/examples/recursion/test/tests.ts +++ b/examples/recursion/test/tests.ts @@ -1,8 +1,16 @@ import { Test, getCanisterId } from 'azle/test'; import { _SERVICE, - rec_313, - rec_321 + rec_0, + rec_10, + rec_12, + rec_14, + rec_2, + rec_24, + rec_26, + rec_4, + rec_6, + rec_8 } from './dfx_generated/recursion/recursion.did'; import { _SERVICE as _REC_SERVICE } from './dfx_generated/recursive_canister/recursive_canister.did'; import { ActorSubclass } from '@dfinity/agent'; @@ -199,7 +207,7 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] { { name: 'recursive tuples with vec', test: async () => { - const input: rec_321 = [[[[], [[[], []]]]], []]; + const input: rec_26 = [[[[], [[[], []]]]], []]; const result = await recursion_canister.testRecTupleWithVec(input); @@ -231,7 +239,7 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] { { name: 'recursive tuples with opt', test: async () => { - const input: rec_313 = [[[[], [[[], []]]]], []]; + const input: rec_24 = [[[[], [[[], []]]]], []]; const result = await recursion_canister.testRecTupleWithOpt(input); @@ -390,6 +398,123 @@ export function getTests(recursion_canister: ActorSubclass<_SERVICE>): Test[] { Ok: result === `(service "${principalId}")` }; } + }, + { + name: 'recursive vec with variant', + test: async () => { + const input: rec_0 = [ + { Leaf: 1 }, + { Branch: [{ Leaf: 2 }] }, + { Leaf: 3 }, + { + Branch: [ + { + Branch: [ + { Branch: [{ Leaf: 4 }, { Leaf: 5 }] }, + { Leaf: 6 } + ] + } + ] + } + ]; + const result = + await recursion_canister.testRecVecWithVariant(input); + + return { + Ok: deepCompare(result, input) + }; + } + }, + { + name: 'recursive vec with tuple', + test: async () => { + const input: rec_4 = [ + [[], []], + [[[[], []]], []], + [[], [[[], []]]], + [[[[], []]], [[[], []]]] + ]; + const result = + await recursion_canister.testRecVecWithTuple(input); + + return { + Ok: deepCompare(result, input) + }; + } + }, + { + name: 'recursive vec with opt', + test: async () => { + const input: rec_2 = [[[[], [[[], []]]]], []]; + const result = + await recursion_canister.testRecVecWithOpt(input); + + return { + Ok: deepCompare(result, input) + }; + } + }, + { + name: 'recursive vec with vec', + test: async () => { + const input: rec_6 = [[[[], [[[], []]]]], []]; + const result = + await recursion_canister.testRecVecWithVec(input); + + return { + Ok: deepCompare(result, input) + }; + } + }, + { + name: 'recursive opt with vec', + test: async () => { + const input: rec_14 = [[[], [], [], []]]; + const result = + await recursion_canister.testRecOptWithVec(input); + + return { + Ok: deepCompare(result, input) + }; + } + }, + { + name: 'recursive opt with tuple', + test: async () => { + const input: rec_12 = [[[], []]]; + const result = + await recursion_canister.testRecOptWithTuple(input); + + return { + Ok: deepCompare(result, input) + }; + } + }, + { + name: 'recursive opt with variant', + test: async () => { + const input: rec_8 = [{ Branch: [] }]; + const result = + await recursion_canister.testRecOptWithVariant(input); + + return { + Ok: deepCompare(result, input) + }; + } + }, + { + name: 'recursive opt with opt', + test: async () => { + const input: rec_10 = [ + [[[[[[[[[[[[[[[[[[[[[[[[[[[[[[]]]]]]]]]]]]]]]]]]]]]]]]]]]]]] + ]; + const result = + await recursion_canister.testRecOptWithOpt(input); + + return { + Ok: deepCompare(result, input) + }; + } } ]; } From 43fad0d2471ee46fae2ebfe3c037555e224dfc79 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Wed, 4 Oct 2023 16:36:06 -0600 Subject: [PATCH 2/2] clean up canister methods --- src/lib/candid/types/reference/service.ts | 87 +++++++++------------ src/lib/canister_methods/heartbeat.ts | 24 +++--- src/lib/canister_methods/index.ts | 15 +--- src/lib/canister_methods/init.ts | 47 ++++++----- src/lib/canister_methods/inspect_message.ts | 32 +++----- src/lib/canister_methods/post_upgrade.ts | 47 ++++++----- src/lib/canister_methods/pre_upgrade.ts | 24 +++--- src/lib/canister_methods/query.ts | 48 ++++++------ src/lib/canister_methods/update.ts | 46 +++++------ 9 files changed, 160 insertions(+), 210 deletions(-) diff --git a/src/lib/candid/types/reference/service.ts b/src/lib/candid/types/reference/service.ts index e56c794982..0921950280 100644 --- a/src/lib/candid/types/reference/service.ts +++ b/src/lib/candid/types/reference/service.ts @@ -16,12 +16,6 @@ type CanisterOptions = { [key: string]: CanisterMethodInfo; }; -type _AzleCanisterOptions = { - [key: string]: ( - parentOrUndefined: _AzleRecursiveFunction | undefined - ) => CanisterMethodInfo; -}; - type _AzleFunctionReturnType = { (principal: Principal): void; init?: any; @@ -57,20 +51,13 @@ type CallableObject = { } & CanisterReturn; export function Canister( - serviceOptions: T + canisterOptions: T ): CallableObject & { _azleCandidType?: '_azleCandidType' } { - const _azleCanisterOptions = - serviceOptions as unknown as _AzleCanisterOptions; let result: _AzleCanisterReturnType = (parentOrPrincipal: any) => { - const originalPrincipal = parentOrPrincipal; - const parentOrUndefined = - parentOrPrincipal !== undefined && parentOrPrincipal._isPrincipal - ? undefined - : parentOrPrincipal; - const callbacks = Object.entries(_azleCanisterOptions).reduce( + const callbacks = Object.entries(canisterOptions).reduce( (acc, entry) => { const key = entry[0]; - const value = entry[1](parentOrUndefined); + const value = entry[1]; return { ...acc, @@ -91,8 +78,8 @@ export function Canister( {} ); - const initOption = Object.entries(_azleCanisterOptions).find( - ([key, value]) => value(parentOrUndefined).mode === 'init' + const initOption = Object.entries(canisterOptions).find( + ([key, value]) => value.mode === 'init' ); const init = initOption === undefined @@ -101,8 +88,8 @@ export function Canister( name: initOption[0] }; - const postUpgradeOption = Object.entries(_azleCanisterOptions).find( - ([key, value]) => value(parentOrUndefined).mode === 'postUpgrade' + const postUpgradeOption = Object.entries(canisterOptions).find( + ([key, value]) => value.mode === 'postUpgrade' ); const postUpgrade = postUpgradeOption === undefined @@ -111,8 +98,8 @@ export function Canister( name: postUpgradeOption[0] }; - const preUpgradeOption = Object.entries(_azleCanisterOptions).find( - ([key, value]) => value(parentOrUndefined).mode === 'preUpgrade' + const preUpgradeOption = Object.entries(canisterOptions).find( + ([key, value]) => value.mode === 'preUpgrade' ); const preUpgrade = preUpgradeOption === undefined @@ -121,8 +108,8 @@ export function Canister( name: preUpgradeOption[0] }; - const heartbeatOption = Object.entries(_azleCanisterOptions).find( - ([key, value]) => value(parentOrUndefined).mode === 'heartbeat' + const heartbeatOption = Object.entries(canisterOptions).find( + ([key, value]) => value.mode === 'heartbeat' ); const heartbeat = heartbeatOption === undefined @@ -131,8 +118,8 @@ export function Canister( name: heartbeatOption[0] }; - const inspectMessageOption = Object.entries(_azleCanisterOptions).find( - ([key, value]) => value(parentOrUndefined).mode === 'inspectMessage' + const inspectMessageOption = Object.entries(canisterOptions).find( + ([key, value]) => value.mode === 'inspectMessage' ); const inspectMessage = inspectMessageOption === undefined @@ -141,16 +128,16 @@ export function Canister( name: inspectMessageOption[0] }; - const queries = Object.entries(_azleCanisterOptions) + const queries = Object.entries(canisterOptions) .filter((entry) => { const key = entry[0]; - const value = entry[1](parentOrUndefined); + const value = entry[1]; return value.mode === 'query'; }) .map((entry) => { const key = entry[0]; - const value = entry[1](parentOrUndefined); + const value = entry[1]; return { name: key, @@ -159,16 +146,16 @@ export function Canister( }; }); - const updates = Object.entries(_azleCanisterOptions) + const updates = Object.entries(canisterOptions) .filter((entry) => { const key = entry[0]; - const value = entry[1](parentOrUndefined); + const value = entry[1]; return value.mode === 'update'; }) .map((entry) => { const key = entry[0]; - const value = entry[1](parentOrUndefined); + const value = entry[1]; return { name: key, @@ -179,10 +166,10 @@ export function Canister( let returnFunction: _AzleFunctionReturnType = ( principal: Principal ) => { - const callbacks = Object.entries(_azleCanisterOptions).reduce( + const callbacks = Object.entries(canisterOptions).reduce( (acc, entry) => { const key = entry[0]; - const value = entry[1](parentOrUndefined); + const value = entry[1]; return { ...acc, @@ -220,23 +207,22 @@ export function Canister( returnFunction.getSystemFunctionIDLs = ( parents: Parent[] ): IDL.FuncClass[] => { - const serviceFunctionInfo = - _azleCanisterOptions as unknown as ServiceFunctionInfo; + const serviceFunctionInfo = canisterOptions as ServiceFunctionInfo; return Object.entries(serviceFunctionInfo).reduce( (accumulator, [_methodName, functionInfo]) => { - const mode = functionInfo(parentOrUndefined).mode; + const mode = functionInfo.mode; if (mode === 'update' || mode === 'query') { // We don't want init, post upgrade, etc showing up in the idl return accumulator; } const paramRealIdls = toParamIDLTypes( - functionInfo(parentOrUndefined).paramCandidTypes, + functionInfo.paramCandidTypes, parents ); const returnRealIdl = toReturnIDLType( - functionInfo(parentOrUndefined).returnCandidType, + functionInfo.returnCandidType, parents ); return [ @@ -248,21 +234,20 @@ export function Canister( ); }; returnFunction.getIDL = (parents: Parent[]): IDL.ServiceClass => { - const serviceFunctionInfo = - _azleCanisterOptions as unknown as ServiceFunctionInfo; + const serviceFunctionInfo = canisterOptions as ServiceFunctionInfo; const record = Object.entries(serviceFunctionInfo).reduce( (accumulator, [methodName, functionInfo]) => { - const paramRealIdls = toParamIDLTypes( - functionInfo(parentOrUndefined).paramCandidTypes, + const paramIdlTypes = toParamIDLTypes( + functionInfo.paramCandidTypes, parents ); - const returnRealIdl = toReturnIDLType( - functionInfo(parentOrUndefined).returnCandidType, + const returnIdlTypes = toReturnIDLType( + functionInfo.returnCandidType, parents ); - const mode = functionInfo(parentOrUndefined).mode; + const mode = functionInfo.mode; let annotations: string[] = []; if (mode === 'update') { // do nothing @@ -276,8 +261,8 @@ export function Canister( return { ...accumulator, [methodName]: IDL.Func( - paramRealIdls, - returnRealIdl, + paramIdlTypes, + returnIdlTypes, annotations ) }; @@ -288,8 +273,8 @@ export function Canister( return IDL.Service(record); }; - if (originalPrincipal !== undefined && originalPrincipal._isPrincipal) { - return returnFunction(originalPrincipal); + if (parentOrPrincipal !== undefined && parentOrPrincipal._isPrincipal) { + return returnFunction(parentOrPrincipal); } return returnFunction; @@ -364,5 +349,5 @@ type FunctionInfo = { }; interface ServiceFunctionInfo { - [key: string]: (parent: _AzleRecursiveFunction | undefined) => FunctionInfo; + [key: string]: FunctionInfo; } diff --git a/src/lib/canister_methods/heartbeat.ts b/src/lib/canister_methods/heartbeat.ts index ae5ec61dda..ce759ca73a 100644 --- a/src/lib/canister_methods/heartbeat.ts +++ b/src/lib/canister_methods/heartbeat.ts @@ -4,18 +4,16 @@ import { Void } from '../candid/types/primitive/void'; export function heartbeat( callback: () => void | Promise ): CanisterMethodInfo<[], Void> { - return (() => { - const finalCallback = (...args: any[]) => { - executeMethod('heartbeat', args, callback, [], Void, false, []); - }; + const finalCallback = (...args: any[]) => { + executeMethod('heartbeat', args, callback, [], Void, false); + }; - return { - mode: 'heartbeat', - callback: finalCallback, - paramCandidTypes: [], - returnCandidType: Void, - async: isAsync(callback), - guard: undefined - } as CanisterMethodInfo<[], Void>; - }) as any; + return { + mode: 'heartbeat', + callback: finalCallback, + paramCandidTypes: [], + returnCandidType: Void, + async: isAsync(callback), + guard: undefined + }; } diff --git a/src/lib/canister_methods/index.ts b/src/lib/canister_methods/index.ts index 35ed2b7a55..cac3087d7a 100644 --- a/src/lib/canister_methods/index.ts +++ b/src/lib/canister_methods/index.ts @@ -1,6 +1,6 @@ import { AzleVoid } from '../candid/types/primitive/void'; import { ic } from '../ic'; -import { CandidType, TypeMapping, Parent } from '../candid'; +import { CandidType, TypeMapping } from '../candid'; import { decodeMultiple, encode } from '../candid/serde'; export * from './heartbeat'; @@ -46,8 +46,7 @@ export function executeMethod( callback: any, paramCandidTypes: CandidType[], returnCandidType: CandidType, - manual: boolean, - parents: Parent[] + manual: boolean ) { if (mode === 'heartbeat') { const result = callback(); @@ -70,7 +69,7 @@ export function executeMethod( return; } - const decodedArgs = decodeMultiple(paramCandidTypes, args[0], parents); + const decodedArgs = decodeMultiple(paramCandidTypes, args[0]); const result = callback(...decodedArgs); @@ -94,7 +93,7 @@ export function executeMethod( console.log(`final instructions: ${ic.instructionCounter()}`); if (!manual) { - ic.replyRaw(encode(returnCandidType, result, parents)); + ic.replyRaw(encode(returnCandidType, result)); } }) .catch((error: any) => { @@ -109,12 +108,6 @@ export function executeMethod( } } -export function createParents(parent: any): Parent[] { - return parent === undefined - ? [] - : [{ idl: parent, name: parent._azleName }]; -} - export function isAsync(originalFunction: any) { if (originalFunction[Symbol.toStringTag] === 'AsyncFunction') { return true; diff --git a/src/lib/canister_methods/init.ts b/src/lib/canister_methods/init.ts index 90a4a47da0..a7964cd3ca 100644 --- a/src/lib/canister_methods/init.ts +++ b/src/lib/canister_methods/init.ts @@ -1,4 +1,4 @@ -import { Callback, CanisterMethodInfo, createParents, executeMethod } from '.'; +import { Callback, CanisterMethodInfo, executeMethod } from '.'; import { CandidType, TypeMapping } from '../candid'; import { Void } from '../candid/types/primitive/void'; @@ -11,29 +11,26 @@ export function init< ? GenericCallback : never ): CanisterMethodInfo { - return ((parent: any) => { - const finalCallback = - callback === undefined - ? undefined - : (...args: any[]) => { - executeMethod( - 'init', - args, - callback, - paramCandidTypes as unknown as CandidType[], - Void, - false, - createParents(parent) - ); - }; + const finalCallback = + callback === undefined + ? undefined + : (...args: any[]) => { + executeMethod( + 'init', + args, + callback, + paramCandidTypes as unknown as CandidType[], + Void, + false + ); + }; - return { - mode: 'init', - callback: finalCallback, - paramCandidTypes: paramCandidTypes as any, - returnCandidType: Void, - async: false, - guard: undefined - } as CanisterMethodInfo; - }) as any; + return { + mode: 'init', + callback: finalCallback, + paramCandidTypes: paramCandidTypes as any, + returnCandidType: Void, + async: false, + guard: undefined + }; } diff --git a/src/lib/canister_methods/inspect_message.ts b/src/lib/canister_methods/inspect_message.ts index a90833e79f..557f23f990 100644 --- a/src/lib/canister_methods/inspect_message.ts +++ b/src/lib/canister_methods/inspect_message.ts @@ -4,26 +4,16 @@ import { Void } from '../candid/types/primitive/void'; export function inspectMessage( callback: () => void | Promise ): CanisterMethodInfo<[], Void> { - return (() => { - const finalCallback = (...args: any[]) => { - executeMethod( - 'inspectMessage', - args, - callback, - [], - Void, - false, - [] - ); - }; + const finalCallback = (...args: any[]) => { + executeMethod('inspectMessage', args, callback, [], Void, false); + }; - return { - mode: 'inspectMessage', - callback: finalCallback, - paramCandidTypes: [], - returnCandidType: Void, - async: false, - guard: undefined - } as CanisterMethodInfo<[], Void>; - }) as any; + return { + mode: 'inspectMessage', + callback: finalCallback, + paramCandidTypes: [], + returnCandidType: Void, + async: false, + guard: undefined + }; } diff --git a/src/lib/canister_methods/post_upgrade.ts b/src/lib/canister_methods/post_upgrade.ts index 4b34b622e2..7043b639cd 100644 --- a/src/lib/canister_methods/post_upgrade.ts +++ b/src/lib/canister_methods/post_upgrade.ts @@ -1,4 +1,4 @@ -import { Callback, CanisterMethodInfo, createParents, executeMethod } from '.'; +import { Callback, CanisterMethodInfo, executeMethod } from '.'; import { CandidType, TypeMapping } from '../candid'; import { Void } from '../candid/types/primitive/void'; @@ -11,29 +11,26 @@ export function postUpgrade< ? GenericCallback : never ): CanisterMethodInfo { - return ((parent: any) => { - const finalCallback = - callback === undefined - ? undefined - : (...args: any[]) => { - executeMethod( - 'postUpgrade', - args, - callback, - paramCandidTypes as unknown as CandidType[], - Void, - false, - createParents(parent) - ); - }; + const finalCallback = + callback === undefined + ? undefined + : (...args: any[]) => { + executeMethod( + 'postUpgrade', + args, + callback, + paramCandidTypes as unknown as CandidType[], + Void, + false + ); + }; - return { - mode: 'postUpgrade', - callback: finalCallback, - paramCandidTypes: paramCandidTypes as unknown as CandidType[], - returnCandidType: Void, - async: false, - guard: undefined - } as CanisterMethodInfo; - }) as any; + return { + mode: 'postUpgrade', + callback: finalCallback, + paramCandidTypes: paramCandidTypes as unknown as CandidType[], + returnCandidType: Void, + async: false, + guard: undefined + }; } diff --git a/src/lib/canister_methods/pre_upgrade.ts b/src/lib/canister_methods/pre_upgrade.ts index 6ffa18fe95..b0669e27ce 100644 --- a/src/lib/canister_methods/pre_upgrade.ts +++ b/src/lib/canister_methods/pre_upgrade.ts @@ -4,18 +4,16 @@ import { Void } from '../candid/types/primitive/void'; export function preUpgrade( callback: () => void | Promise ): CanisterMethodInfo<[], Void> { - return (() => { - const finalCallback = (...args: any[]) => { - executeMethod('preUpgrade', args, callback, [], Void, false, []); - }; + const finalCallback = (...args: any[]) => { + executeMethod('preUpgrade', args, callback, [], Void, false); + }; - return { - mode: 'preUpgrade', - callback: finalCallback, - paramCandidTypes: [], - returnCandidType: Void, - async: isAsync(callback), - guard: undefined - } as CanisterMethodInfo<[], Void>; - }) as any; + return { + mode: 'preUpgrade', + callback: finalCallback, + paramCandidTypes: [], + returnCandidType: Void, + async: isAsync(callback), + guard: undefined + }; } diff --git a/src/lib/canister_methods/query.ts b/src/lib/canister_methods/query.ts index d9a7947aac..af6a9ec684 100644 --- a/src/lib/canister_methods/query.ts +++ b/src/lib/canister_methods/query.ts @@ -2,7 +2,6 @@ import { Callback, CanisterMethodInfo, MethodArgs, - createParents, executeMethod, isAsync } from '.'; @@ -20,30 +19,27 @@ export function query< : never, methodArgs?: MethodArgs ): CanisterMethodInfo { - return ((parent: any) => { - // TODO maybe the cross canister callback should be made here? - const finalCallback = - callback === undefined - ? undefined - : (...args: any[]) => { - executeMethod( - 'query', - args, - callback, - paramCandidTypes as unknown as CandidType[], - returnCandidType, - methodArgs?.manual ?? false, - createParents(parent) - ); - }; + // TODO maybe the cross canister callback should be made here? + const finalCallback = + callback === undefined + ? undefined + : (...args: any[]) => { + executeMethod( + 'query', + args, + callback, + paramCandidTypes as unknown as CandidType[], + returnCandidType, + methodArgs?.manual ?? false + ); + }; - return { - mode: 'query', - callback: finalCallback, - paramCandidTypes: paramCandidTypes as unknown as CandidType[], - returnCandidType, - async: callback === undefined ? false : isAsync(callback), - guard: methodArgs?.guard - } as CanisterMethodInfo; - }) as any; + return { + mode: 'query', + callback: finalCallback, + paramCandidTypes: paramCandidTypes as unknown as CandidType[], + returnCandidType, + async: callback === undefined ? false : isAsync(callback), + guard: methodArgs?.guard + }; } diff --git a/src/lib/canister_methods/update.ts b/src/lib/canister_methods/update.ts index d02d74729b..3d45de9874 100644 --- a/src/lib/canister_methods/update.ts +++ b/src/lib/canister_methods/update.ts @@ -2,7 +2,6 @@ import { Callback, CanisterMethodInfo, MethodArgs, - createParents, executeMethod, isAsync } from '.'; @@ -20,29 +19,26 @@ export function update< : never, methodArgs?: MethodArgs ): CanisterMethodInfo { - return ((parent: any) => { - const finalCallback = - callback === undefined - ? undefined - : (...args: any[]) => { - executeMethod( - 'update', - args, - callback, - paramCandidTypes as unknown as CandidType[], - returnCandidType, - methodArgs?.manual ?? false, - createParents(parent) - ); - }; + const finalCallback = + callback === undefined + ? undefined + : (...args: any[]) => { + executeMethod( + 'update', + args, + callback, + paramCandidTypes as unknown as CandidType[], + returnCandidType, + methodArgs?.manual ?? false + ); + }; - return { - mode: 'update', - callback: finalCallback, - paramCandidTypes: paramCandidTypes as unknown as CandidType[], - returnCandidType, - async: callback === undefined ? false : isAsync(callback), - guard: methodArgs?.guard - } as CanisterMethodInfo; - }) as any; + return { + mode: 'update', + callback: finalCallback, + paramCandidTypes: paramCandidTypes as unknown as CandidType[], + returnCandidType, + async: callback === undefined ? false : isAsync(callback), + guard: methodArgs?.guard + }; }