From 8b2d279e130f13ed54bd91d2fc8104ca673eb8f8 Mon Sep 17 00:00:00 2001 From: Dan Steren Date: Sun, 1 Oct 2023 13:11:06 -0600 Subject: [PATCH] Fix service calls --- src/lib_functional/candid/serde.ts | 34 ++++++++++++++++++++++++++++++ src/lib_new/service.ts | 10 +++------ src/lib_new/utils.ts | 6 +----- 3 files changed, 38 insertions(+), 12 deletions(-) diff --git a/src/lib_functional/candid/serde.ts b/src/lib_functional/candid/serde.ts index c7a6e126b1..8af18a8257 100644 --- a/src/lib_functional/candid/serde.ts +++ b/src/lib_functional/candid/serde.ts @@ -60,6 +60,12 @@ export function decode( // Azle-augmented ones const realIDL = toIDLType(fakeIdl, []); + const idlIsAzleVoid = Array.isArray(realIDL); + + if (idlIsAzleVoid) { + return undefined; + } + const candidDecodedValue = IDL.decode([realIDL], data)[0] as any; return realIDL.accept(new DecodeVisitor(), { @@ -67,3 +73,31 @@ export function decode( js_data: candidDecodedValue }); } + +export function encodeMultiple( + data: any[], + fakeIdls: (IDL.Type | CandidType)[] +): Uint8Array { + const { values, idls } = data.reduce<{ + values: any[]; + idls: IDL.Type[]; + }>( + (acc, datum, index) => { + const fakeIdl = fakeIdls[index]; + const realIDL = toIDLType(fakeIdl, []); + + const encodeReadyValue = realIDL.accept(new EncodeVisitor(), { + js_class: fakeIdl, + js_data: datum + }); + + return { + values: [...acc.values, encodeReadyValue], + idls: [...acc.idls, realIDL] + }; + }, + { values: [], idls: [] } + ); + + return new Uint8Array(IDL.encode(idls, values)); +} diff --git a/src/lib_new/service.ts b/src/lib_new/service.ts index 1e480a6b5d..ab515f7733 100644 --- a/src/lib_new/service.ts +++ b/src/lib_new/service.ts @@ -1,3 +1,4 @@ +import { decode, encodeMultiple } from '../lib_functional/candid/serde'; import { ic, IDL, Principal } from './index'; import { CandidClass, @@ -108,9 +109,7 @@ export function serviceCall( cycles: bigint, ...args: any[] ) { - const encodedArgs = new Uint8Array( - IDL.encode(toParamIDLTypes(paramsIdls), args) - ); + const encodedArgs = encodeMultiple(args, paramsIdls); if (notify) { try { @@ -131,10 +130,7 @@ export function serviceCall( cycles ); - const returnIdls = toReturnIDLType(returnIdl); - const decodedResult = IDL.decode(returnIdls, encodedResult)[0]; - - return decodedResult; + return decode(encodedResult, returnIdl); } }; } diff --git a/src/lib_new/utils.ts b/src/lib_new/utils.ts index 2255869811..a0ae846fa6 100644 --- a/src/lib_new/utils.ts +++ b/src/lib_new/utils.ts @@ -111,11 +111,7 @@ export function toReturnIDLType( ): IDL.Type[] { const idlType = toIDLType(returnIdl, parents); - if (Array.isArray(idlType)) { - return [...idlType]; - } - - return [idlType]; + return Array.isArray(idlType) ? idlType : [idlType]; } export function isAsync(originalFunction: any) {