From d755eb4aac0643e93baef70df2550191626a3246 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Mon, 23 Oct 2023 16:08:49 -0500 Subject: [PATCH 01/30] initial implementation of stable structures with toBytes and fromBytes, StableJson implemented basically --- .../stable_structures/src/canister3/index.did | 36 +++++ .../stable_structures/src/canister3/index.ts | 10 +- .../src/canister3/stable_map_14.ts | 44 +++++++ .../src/canister3/stable_map_15.ts | 44 +++++++ .../src/canister3/stable_map_16.ts | 66 ++++++++++ .../src/canister3/stable_map_17.ts | 46 +++++++ examples/stable_structures/src/types.ts | 4 +- examples/stable_structures/test/tests.ts | 62 +++++++-- package.json | 2 +- src/lib/candid/candid_type.ts | 4 +- src/lib/candid/type_mapping.ts | 38 +++--- src/lib/candid/types/constructed/blob.ts | 12 +- src/lib/candid/types/constructed/opt.ts | 10 ++ src/lib/candid/types/constructed/record.ts | 10 +- src/lib/candid/types/constructed/tuple.ts | 10 ++ src/lib/candid/types/constructed/variant.ts | 11 +- src/lib/candid/types/constructed/vec.ts | 10 ++ src/lib/candid/types/primitive/bool.ts | 12 +- src/lib/candid/types/primitive/empty.ts | 12 +- .../candid/types/primitive/floats/float32.ts | 12 +- .../candid/types/primitive/floats/float64.ts | 12 +- src/lib/candid/types/primitive/ints/int.ts | 12 +- src/lib/candid/types/primitive/ints/int16.ts | 12 +- src/lib/candid/types/primitive/ints/int32.ts | 12 +- src/lib/candid/types/primitive/ints/int64.ts | 12 +- src/lib/candid/types/primitive/ints/int8.ts | 12 +- src/lib/candid/types/primitive/nats/nat.ts | 12 +- src/lib/candid/types/primitive/nats/nat16.ts | 12 +- src/lib/candid/types/primitive/nats/nat32.ts | 12 +- src/lib/candid/types/primitive/nats/nat64.ts | 12 +- src/lib/candid/types/primitive/nats/nat8.ts | 12 +- src/lib/candid/types/primitive/null.ts | 12 +- src/lib/candid/types/primitive/reserved.ts | 12 +- src/lib/candid/types/primitive/text.ts | 13 +- src/lib/candid/types/primitive/void.ts | 13 +- src/lib/candid/types/reference/func.ts | 10 +- src/lib/candid/types/reference/principal.ts | 10 ++ .../candid/types/reference/service/index.ts | 4 +- src/lib/stable_b_tree_map.ts | 123 ++++++++++-------- 39 files changed, 676 insertions(+), 108 deletions(-) create mode 100644 examples/stable_structures/src/canister3/stable_map_14.ts create mode 100644 examples/stable_structures/src/canister3/stable_map_15.ts create mode 100644 examples/stable_structures/src/canister3/stable_map_16.ts create mode 100644 examples/stable_structures/src/canister3/stable_map_17.ts diff --git a/examples/stable_structures/src/canister3/index.did b/examples/stable_structures/src/canister3/index.did index be638ad66d..ad8907e513 100644 --- a/examples/stable_structures/src/canister3/index.did +++ b/examples/stable_structures/src/canister3/index.did @@ -36,4 +36,40 @@ service: () -> { stableMap13Len: () -> (nat64) query; stableMap13Remove: (text) -> (opt principal); stableMap13Values: () -> (vec principal) query; + stableMap14ContainsKey: (text) -> (bool) query; + stableMap14Get: (text) -> (opt func (record {title:text}) -> (variant {Sad; Happy})) query; + stableMap14Insert: (text, func (record {title:text}) -> (variant {Sad; Happy})) -> (opt func (record {title:text}) -> (variant {Sad; Happy})); + stableMap14IsEmpty: () -> (bool) query; + stableMap14Items: () -> (vec record {text; func (record {title:text}) -> (variant {Sad; Happy})}) query; + stableMap14Keys: () -> (vec text) query; + stableMap14Len: () -> (nat64) query; + stableMap14Remove: (text) -> (opt func (record {title:text}) -> (variant {Sad; Happy})); + stableMap14Values: () -> (vec func (record {title:text}) -> (variant {Sad; Happy})) query; + stableMap15ContainsKey: (func (record {title:text}) -> (variant {Sad; Happy})) -> (bool) query; + stableMap15Get: (func (record {title:text}) -> (variant {Sad; Happy})) -> (opt text) query; + stableMap15Insert: (func (record {title:text}) -> (variant {Sad; Happy}), text) -> (opt text); + stableMap15IsEmpty: () -> (bool) query; + stableMap15Items: () -> (vec record {func (record {title:text}) -> (variant {Sad; Happy}); text}) query; + stableMap15Keys: () -> (vec func (record {title:text}) -> (variant {Sad; Happy})) query; + stableMap15Len: () -> (nat64) query; + stableMap15Remove: (func (record {title:text}) -> (variant {Sad; Happy})) -> (opt text); + stableMap15Values: () -> (vec text) query; + stableMap16ContainsKey: (text) -> (bool) query; + stableMap16Get: (text) -> (opt text) query; + stableMap16Insert: (text, text) -> (opt text); + stableMap16IsEmpty: () -> (bool) query; + stableMap16Items: () -> (vec record {text; text}) query; + stableMap16Keys: () -> (vec text) query; + stableMap16Len: () -> (nat64) query; + stableMap16Remove: (text) -> (opt text); + stableMap16Values: () -> (vec text) query; + stableMap17ContainsKey: (text) -> (bool) query; + stableMap17Get: (text) -> (opt text) query; + stableMap17Insert: (text, text) -> (opt text); + stableMap17IsEmpty: () -> (bool) query; + stableMap17Items: () -> (vec record {text; text}) query; + stableMap17Keys: () -> (vec text) query; + stableMap17Len: () -> (nat64) query; + stableMap17Remove: (text) -> (opt text); + stableMap17Values: () -> (vec text) query; } diff --git a/examples/stable_structures/src/canister3/index.ts b/examples/stable_structures/src/canister3/index.ts index d456906b3a..9d2b96df72 100644 --- a/examples/stable_structures/src/canister3/index.ts +++ b/examples/stable_structures/src/canister3/index.ts @@ -4,6 +4,10 @@ import { stableMap10Methods } from './stable_map_10'; import { stableMap11Methods } from './stable_map_11'; import { stableMap12Methods } from './stable_map_12'; import { stableMap13Methods } from './stable_map_13'; +import { stableMap14Methods } from './stable_map_14'; +import { stableMap15Methods } from './stable_map_15'; +import { stableMap16Methods } from './stable_map_16'; +import { stableMap17Methods } from './stable_map_17'; let redeployed = false; @@ -17,5 +21,9 @@ export default Canister({ ...stableMap10Methods, ...stableMap11Methods, ...stableMap12Methods, - ...stableMap13Methods + ...stableMap13Methods, + ...stableMap14Methods, + ...stableMap15Methods, + ...stableMap16Methods, + ...stableMap17Methods }); diff --git a/examples/stable_structures/src/canister3/stable_map_14.ts b/examples/stable_structures/src/canister3/stable_map_14.ts new file mode 100644 index 0000000000..13a63fcbfb --- /dev/null +++ b/examples/stable_structures/src/canister3/stable_map_14.ts @@ -0,0 +1,44 @@ +import { + bool, + nat64, + Opt, + query, + StableBTreeMap, + text, + Tuple, + update, + Vec +} from 'azle'; +import { Callback } from '../types'; + +let stableMap14 = StableBTreeMap(text, Callback, 14); + +export const stableMap14Methods = { + stableMap14ContainsKey: query([text], bool, (key) => { + return stableMap14.containsKey(key); + }), + stableMap14Get: query([text], Opt(Callback), (key) => { + return stableMap14.get(key); + }), + stableMap14Insert: update([text, Callback], Opt(Callback), (key, value) => { + return stableMap14.insert(key, value); + }), + stableMap14IsEmpty: query([], bool, () => { + return stableMap14.isEmpty(); + }), + stableMap14Items: query([], Vec(Tuple(text, Callback)), () => { + return stableMap14.items(); + }), + stableMap14Keys: query([], Vec(text), () => { + return stableMap14.keys(); + }), + stableMap14Len: query([], nat64, () => { + return stableMap14.len(); + }), + stableMap14Remove: update([text], Opt(Callback), (key) => { + return stableMap14.remove(key); + }), + stableMap14Values: query([], Vec(Callback), () => { + return stableMap14.values(); + }) +}; diff --git a/examples/stable_structures/src/canister3/stable_map_15.ts b/examples/stable_structures/src/canister3/stable_map_15.ts new file mode 100644 index 0000000000..6a49097876 --- /dev/null +++ b/examples/stable_structures/src/canister3/stable_map_15.ts @@ -0,0 +1,44 @@ +import { + bool, + nat64, + Opt, + query, + StableBTreeMap, + text, + Tuple, + update, + Vec +} from 'azle'; +import { Callback } from '../types'; + +let stableMap15 = StableBTreeMap(Callback, text, 15); + +export const stableMap15Methods = { + stableMap15ContainsKey: query([Callback], bool, (key) => { + return stableMap15.containsKey(key); + }), + stableMap15Get: query([Callback], Opt(text), (key) => { + return stableMap15.get(key); + }), + stableMap15Insert: update([Callback, text], Opt(text), (key, value) => { + return stableMap15.insert(key, value); + }), + stableMap15IsEmpty: query([], bool, () => { + return stableMap15.isEmpty(); + }), + stableMap15Items: query([], Vec(Tuple(Callback, text)), () => { + return stableMap15.items(); + }), + stableMap15Keys: query([], Vec(Callback), () => { + return stableMap15.keys(); + }), + stableMap15Len: query([], nat64, () => { + return stableMap15.len(); + }), + stableMap15Remove: update([Callback], Opt(text), (key) => { + return stableMap15.remove(key); + }), + stableMap15Values: query([], Vec(text), () => { + return stableMap15.values(); + }) +}; diff --git a/examples/stable_structures/src/canister3/stable_map_16.ts b/examples/stable_structures/src/canister3/stable_map_16.ts new file mode 100644 index 0000000000..cac0f0d6fd --- /dev/null +++ b/examples/stable_structures/src/canister3/stable_map_16.ts @@ -0,0 +1,66 @@ +import { + bool, + nat64, + None, + Opt, + query, + Some, + StableBTreeMap, + StableJson, + text, + Tuple, + update, + Vec +} from 'azle'; + +let stableMap16 = StableBTreeMap(text, StableJson, 16); + +export const stableMap16Methods = { + stableMap16ContainsKey: query([text], bool, (key) => { + return stableMap16.containsKey(key); + }), + stableMap16Get: query([text], Opt(text), (key) => { + const result = stableMap16.get(key); + + if ('None' in result) { + return None; + } + + return Some(JSON.stringify(result.Some)); + }), + stableMap16Insert: update([text, text], Opt(text), (key, value) => { + const result = stableMap16.insert(key, JSON.parse(value)); + + if ('None' in result) { + return None; + } + + return Some(JSON.stringify(result.Some)); + }), + stableMap16IsEmpty: query([], bool, () => { + return stableMap16.isEmpty(); + }), + stableMap16Items: query([], Vec(Tuple(text, text)), () => { + return stableMap16.items().map(([key, value]) => { + return [key, JSON.stringify(value)]; + }); + }), + stableMap16Keys: query([], Vec(text), () => { + return stableMap16.keys(); + }), + stableMap16Len: query([], nat64, () => { + return stableMap16.len(); + }), + stableMap16Remove: update([text], Opt(text), (key) => { + const result = stableMap16.remove(key); + + if ('None' in result) { + return None; + } + + return Some(JSON.stringify(result.Some)); + }), + stableMap16Values: query([], Vec(text), () => { + return stableMap16.values().map((value) => JSON.stringify(value)); + }) +}; diff --git a/examples/stable_structures/src/canister3/stable_map_17.ts b/examples/stable_structures/src/canister3/stable_map_17.ts new file mode 100644 index 0000000000..ac7c9f5074 --- /dev/null +++ b/examples/stable_structures/src/canister3/stable_map_17.ts @@ -0,0 +1,46 @@ +import { + bool, + nat64, + Opt, + query, + StableBTreeMap, + StableJson, + text, + Tuple, + update, + Vec +} from 'azle'; + +let stableMap17 = StableBTreeMap(StableJson, text, 17); + +export const stableMap17Methods = { + stableMap17ContainsKey: query([text], bool, (key) => { + return stableMap17.containsKey(JSON.parse(key)); + }), + stableMap17Get: query([text], Opt(text), (key) => { + return stableMap17.get(JSON.parse(key)); + }), + stableMap17Insert: update([text, text], Opt(text), (key, value) => { + return stableMap17.insert(JSON.parse(key), value); + }), + stableMap17IsEmpty: query([], bool, () => { + return stableMap17.isEmpty(); + }), + stableMap17Items: query([], Vec(Tuple(text, text)), () => { + return stableMap17.items().map(([key, value]) => { + return [JSON.stringify(key), value]; + }); + }), + stableMap17Keys: query([], Vec(text), () => { + return stableMap17.keys().map((key) => JSON.stringify(key)); + }), + stableMap17Len: query([], nat64, () => { + return stableMap17.len(); + }), + stableMap17Remove: update([text], Opt(text), (key) => { + return stableMap17.remove(JSON.parse(key)); + }), + stableMap17Values: query([], Vec(text), () => { + return stableMap17.values(); + }) +}; diff --git a/examples/stable_structures/src/types.ts b/examples/stable_structures/src/types.ts index 5065825886..322e20f3fe 100644 --- a/examples/stable_structures/src/types.ts +++ b/examples/stable_structures/src/types.ts @@ -1,4 +1,4 @@ -import { Null, Record, text, Variant, Vec } from 'azle'; +import { Func, Null, Record, text, Variant, Vec } from 'azle'; export const BlogPost = Record({ title: text @@ -13,3 +13,5 @@ export const User = Record({ username: text, posts: Vec(BlogPost) }); + +export const Callback = Func([BlogPost], Reaction, 'update'); diff --git a/examples/stable_structures/test/tests.ts b/examples/stable_structures/test/tests.ts index 35cf4000d7..c7ce855e50 100644 --- a/examples/stable_structures/test/tests.ts +++ b/examples/stable_structures/test/tests.ts @@ -35,6 +35,10 @@ const STABLE_MAP_KEYS: [ float32, nat, blob, + string, + string, + [Principal, string], + string, string ] = [ 0, @@ -59,7 +63,14 @@ const STABLE_MAP_KEYS: [ 10.23, 0n, new Uint8Array(HELLO_BYTES), - 'hello' + 'hello', + 'world', + [Principal.fromText('aaaaa-aa'), 'test_method'], + 'json', + JSON.stringify({ + hello: 'hello', + world: 'world' + }) ]; const STABLE_MAP_KEYSCOMPS: [ @@ -76,6 +87,10 @@ const STABLE_MAP_KEYSCOMPS: [ (a: float32 | undefined, b: float32) => boolean, (a: nat | undefined, b: nat) => boolean, (a: blob | undefined, b: blob) => boolean, + (a: string | undefined, b: string) => boolean, + (a: string | undefined, b: string) => boolean, + (a: [Principal, string] | undefined, b: [Principal, string]) => boolean, + (a: string | undefined, b: string) => boolean, (a: string | undefined, b: string) => boolean ] = [ simpleEquals, @@ -95,7 +110,15 @@ const STABLE_MAP_KEYSCOMPS: [ (a, b) => a?.toFixed(2) === b.toFixed(2), simpleEquals, (a, b) => a !== undefined && a.every((value, index) => value === b[index]), - simpleEquals + simpleEquals, + simpleEquals, + (a, b) => + a !== undefined && a[0].toText() === b[0].toText() && a[1] === b[1], + simpleEquals, + (a, b) => + a !== undefined && + JSON.parse(a).hello === JSON.parse(b).hello && + JSON.parse(a).world === JSON.parse(b).world ]; const STABLEMAPVALUES: [ @@ -112,7 +135,11 @@ const STABLEMAPVALUES: [ boolean[], typeof User, typeof Reaction, - Principal + Principal, + [Principal, string], + string, + string, + string ] = [ 'hello', new Uint8Array(HELLO_BYTES), @@ -134,7 +161,14 @@ const STABLEMAPVALUES: [ ] }, { Sad: null }, - Principal.fromText('aaaaa-aa') + Principal.fromText('aaaaa-aa'), + [Principal.fromText('aaaaa-aa'), 'test_method'], + 'syrup', + JSON.stringify({ + hello: 'hello', + world: 'world' + }), + 'candyland' ]; const STABLEMAPVALUECOMPS: [ @@ -151,7 +185,11 @@ const STABLEMAPVALUECOMPS: [ (a: boolean[] | undefined, b: boolean[]) => boolean, (a: typeof User | undefined, b: typeof User) => boolean, (a: typeof Reaction | undefined, b: typeof Reaction) => boolean, - (a: Principal | undefined, b: Principal) => boolean + (a: Principal | undefined, b: Principal) => boolean, + (a: [Principal, string] | undefined, b: [Principal, string]) => boolean, + (a: string | undefined, b: string) => boolean, + (a: string | undefined, b: string) => boolean, + (a: string | undefined, b: string) => boolean ] = [ simpleEquals, (a, b) => a !== undefined && a.every((value, index) => value === b[index]), @@ -171,7 +209,15 @@ const STABLEMAPVALUECOMPS: [ (a, b) => a !== undefined && Object.keys(a).every((value) => Object.keys(b).includes(value)), - (a, b) => a !== undefined && a.toText() === b.toText() + (a, b) => a !== undefined && a.toText() === b.toText(), + (a, b) => + a !== undefined && a[0].toText() === b[0].toText() && a[1] === b[1], + simpleEquals, + (a, b) => + a !== undefined && + JSON.parse(a).hello === JSON.parse(b).hello && + JSON.parse(a).world === JSON.parse(b).world, + simpleEquals ]; export function getTests( @@ -182,7 +228,7 @@ export function getTests( return [ ...preRedeployTests(stableStructuresCanister_1, 0, 4), ...preRedeployTests(stableStructuresCanister_2, 5, 9), - ...preRedeployTests(stableStructuresCanister_3, 10, 13), + ...preRedeployTests(stableStructuresCanister_3, 10, 17), { name: 'redeploy canisters', prep: async () => { @@ -207,7 +253,7 @@ export function getTests( }, ...postRedeployTests(stableStructuresCanister_1, 0, 4), ...postRedeployTests(stableStructuresCanister_2, 5, 9), - ...postRedeployTests(stableStructuresCanister_3, 10, 13) + ...postRedeployTests(stableStructuresCanister_3, 10, 17) ]; } diff --git a/package.json b/package.json index c273955719..51286110b9 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "azle", "version": "0.18.5", - "rust_version": "1.68.2", + "rust_version": "1.73.0", "dfx_version": "0.15.1", "description": "TypeScript and JavaScript CDK for the Internet Computer", "scripts": { diff --git a/src/lib/candid/candid_type.ts b/src/lib/candid/candid_type.ts index 62e98f12a4..67f46d62a2 100644 --- a/src/lib/candid/candid_type.ts +++ b/src/lib/candid/candid_type.ts @@ -1,3 +1,5 @@ +import { Serializable } from '../stable_b_tree_map'; + export type CandidType = { _azleCandidType?: '_azleCandidType'; -}; +} & Partial; diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index 96cf0491a9..d705641f8b 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -27,35 +27,35 @@ export type TypeMapping = RecursionLevel extends 10 ? T : T extends () => any ? ReturnType - : T extends AzleText + : T extends typeof AzleText ? string - : T extends AzleBool + : T extends typeof AzleBool ? bool - : T extends AzleInt + : T extends typeof AzleInt ? int - : T extends AzleInt64 + : T extends typeof AzleInt64 ? int64 - : T extends AzleInt32 + : T extends typeof AzleInt32 ? int32 - : T extends AzleInt16 + : T extends typeof AzleInt16 ? int16 - : T extends AzleInt8 + : T extends typeof AzleInt8 ? int8 - : T extends AzleNat + : T extends typeof AzleNat ? nat - : T extends AzleNat64 + : T extends typeof AzleNat64 ? nat64 - : T extends AzleNat32 + : T extends typeof AzleNat32 ? nat32 - : T extends AzleNat16 + : T extends typeof AzleNat16 ? nat16 - : T extends AzleNat8 + : T extends typeof AzleNat8 ? nat8 - : T extends AzleFloat64 + : T extends typeof AzleFloat64 ? float64 - : T extends AzleFloat32 + : T extends typeof AzleFloat32 ? float32 - : T extends AzleVoid + : T extends typeof AzleVoid ? void : T extends AzleTuple ? { @@ -88,14 +88,14 @@ export type TypeMapping = RecursionLevel extends 10 ? Opt> : T extends AzleResult ? Result, TypeMapping> - : T extends AzleBlob + : T extends typeof AzleBlob ? blob : T extends typeof Principal ? Principal - : T extends AzleNull + : T extends typeof AzleNull ? Null - : T extends AzleReserved + : T extends typeof AzleReserved ? reserved - : T extends AzleEmpty + : T extends typeof AzleEmpty ? empty : T; diff --git a/src/lib/candid/types/constructed/blob.ts b/src/lib/candid/types/constructed/blob.ts index 2bac4860e3..dca067a247 100644 --- a/src/lib/candid/types/constructed/blob.ts +++ b/src/lib/candid/types/constructed/blob.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class AzleBlob { _azleKind: 'AzleBlob' = 'AzleBlob'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Vec(IDL.Nat8); } } -export const blob: AzleBlob = AzleBlob as any; +export const blob = AzleBlob; export type blob = Uint8Array; diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index e398d01003..9ad92a8f14 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -1,4 +1,6 @@ import { CandidType, Parent, toIdl } from '../../index'; +import { decode } from '../../serde/decode'; +import { encode } from '../../serde/encode'; import { RequireExactlyOne } from './variant'; import { IDL } from '@dfinity/candid'; @@ -35,6 +37,14 @@ export class AzleOpt { _azleCandidType?: '_azleCandidType'; _azleKind: 'AzleOpt' = 'AzleOpt'; + toBytes(data: number): Uint8Array { + return encode(this, data); + } + + fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + getIdl(parents: Parent[]) { return IDL.Opt(toIdl(this.innerType, parents)); } diff --git a/src/lib/candid/types/constructed/record.ts b/src/lib/candid/types/constructed/record.ts index 71ed42f160..9ef624f868 100644 --- a/src/lib/candid/types/constructed/record.ts +++ b/src/lib/candid/types/constructed/record.ts @@ -1,6 +1,8 @@ import { CandidType, TypeMapping, Parent } from '../../index'; import { IDL } from '@dfinity/candid'; import { toIdlMap, CandidMap } from './to_idl_map'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export function Record< T extends { @@ -10,9 +12,15 @@ export function Record< obj: T ): { [K in keyof T]: TypeMapping; -} & { _azleCandidType?: '_azleCandidType' } { +} & CandidType { return { ...obj, + toBytes(data: number): Uint8Array { + return encode(this, data); + }, + fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + }, getIdl(parents: Parent[]) { return IDL.Record(toIdlMap(obj as CandidMap, parents)); } diff --git a/src/lib/candid/types/constructed/tuple.ts b/src/lib/candid/types/constructed/tuple.ts index ae1cb84584..e5fff0f987 100644 --- a/src/lib/candid/types/constructed/tuple.ts +++ b/src/lib/candid/types/constructed/tuple.ts @@ -1,6 +1,8 @@ import { CandidType } from '../../index'; import { Parent, toIdl } from '../../index'; import { IDL } from '@dfinity/candid'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class AzleTuple { constructor(t: CandidType[]) { @@ -10,6 +12,14 @@ export class AzleTuple { innerTypes: CandidType[]; _azleCandidType?: '_azleCandidType'; + toBytes(data: number): Uint8Array { + return encode(this, data); + } + + fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + getIdl(parents: Parent[]) { const idls = this.innerTypes.map((value) => { return toIdl(value, parents); diff --git a/src/lib/candid/types/constructed/variant.ts b/src/lib/candid/types/constructed/variant.ts index 5a7e06b84f..58a5924781 100644 --- a/src/lib/candid/types/constructed/variant.ts +++ b/src/lib/candid/types/constructed/variant.ts @@ -1,4 +1,6 @@ import { CandidType, TypeMapping } from '../..'; +import { decode } from '../../serde/decode'; +import { encode } from '../../serde/encode'; import { toIdlMap, CandidMap } from './to_idl_map'; import { IDL } from '@dfinity/candid'; @@ -10,9 +12,16 @@ export function Variant< obj: T ): RequireExactlyOne<{ [K in keyof T]: TypeMapping; -}> & { _azleCandidType?: '_azleCandidType' } { +}> & + CandidType { return { ...obj, + toBytes(data: number): Uint8Array { + return encode(this, data); + }, + fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + }, getIdl(parents: any) { return IDL.Variant(toIdlMap(obj as CandidMap, parents)); } diff --git a/src/lib/candid/types/constructed/vec.ts b/src/lib/candid/types/constructed/vec.ts index df0adfd809..b09e57b552 100644 --- a/src/lib/candid/types/constructed/vec.ts +++ b/src/lib/candid/types/constructed/vec.ts @@ -1,6 +1,8 @@ import { CandidType } from '../../index'; import { Parent, toIdl } from '../../index'; import { IDL } from '@dfinity/candid'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class AzleVec { constructor(t: any) { @@ -10,6 +12,14 @@ export class AzleVec { innerType: CandidType; _azleCandidType?: '_azleCandidType'; + toBytes(data: number): Uint8Array { + return encode(this, data); + } + + fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + getIdl(parents: Parent[]) { return IDL.Vec(toIdl(this.innerType, parents)); } diff --git a/src/lib/candid/types/primitive/bool.ts b/src/lib/candid/types/primitive/bool.ts index 0460f9917b..a1f53a5913 100644 --- a/src/lib/candid/types/primitive/bool.ts +++ b/src/lib/candid/types/primitive/bool.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class AzleBool { _azleKind: 'AzleBool' = 'AzleBool'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Bool; } } -export const bool: AzleBool = AzleBool as any; +export const bool = AzleBool; export type bool = boolean; diff --git a/src/lib/candid/types/primitive/empty.ts b/src/lib/candid/types/primitive/empty.ts index 105c12ebcb..f605a87faa 100644 --- a/src/lib/candid/types/primitive/empty.ts +++ b/src/lib/candid/types/primitive/empty.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class AzleEmpty { _azleKind: 'AzleEmpty' = 'AzleEmpty'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Empty; } } -export const empty: AzleEmpty = AzleEmpty as any; +export const empty = AzleEmpty; export type empty = never; diff --git a/src/lib/candid/types/primitive/floats/float32.ts b/src/lib/candid/types/primitive/floats/float32.ts index 2fd1ea7cd4..f591f36740 100644 --- a/src/lib/candid/types/primitive/floats/float32.ts +++ b/src/lib/candid/types/primitive/floats/float32.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleFloat32 { _azleKind: 'AzleFloat32' = 'AzleFloat32'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Float32; } } -export const float32: AzleFloat32 = AzleFloat32 as any; +export const float32 = AzleFloat32; export type float32 = number; diff --git a/src/lib/candid/types/primitive/floats/float64.ts b/src/lib/candid/types/primitive/floats/float64.ts index a84dc16554..c68a288875 100644 --- a/src/lib/candid/types/primitive/floats/float64.ts +++ b/src/lib/candid/types/primitive/floats/float64.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleFloat64 { _azleKind: 'AzleFloat64' = 'AzleFloat64'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Float64; } } -export const float64: AzleFloat64 = AzleFloat64 as any; +export const float64 = AzleFloat64; export type float64 = number; diff --git a/src/lib/candid/types/primitive/ints/int.ts b/src/lib/candid/types/primitive/ints/int.ts index 6016d28b8c..b4566080dd 100644 --- a/src/lib/candid/types/primitive/ints/int.ts +++ b/src/lib/candid/types/primitive/ints/int.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleInt { _azleKind: 'AzleInt' = 'AzleInt'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Int; } } -export const int: AzleInt = AzleInt as any; +export const int = AzleInt; export type int = bigint; diff --git a/src/lib/candid/types/primitive/ints/int16.ts b/src/lib/candid/types/primitive/ints/int16.ts index cc73e160b3..3403a5d4b3 100644 --- a/src/lib/candid/types/primitive/ints/int16.ts +++ b/src/lib/candid/types/primitive/ints/int16.ts @@ -1,12 +1,22 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleInt16 { _azleKind: 'AzleInt16' = 'AzleInt16'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Int16; } } -export const int16: AzleInt16 = AzleInt16 as any; +export const int16 = AzleInt16; export type int16 = number; diff --git a/src/lib/candid/types/primitive/ints/int32.ts b/src/lib/candid/types/primitive/ints/int32.ts index 3d2f708847..ebf1fd2abc 100644 --- a/src/lib/candid/types/primitive/ints/int32.ts +++ b/src/lib/candid/types/primitive/ints/int32.ts @@ -1,12 +1,22 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleInt32 { _azleKind: 'AzleInt32' = 'AzleInt32'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Int32; } } -export const int32: AzleInt32 = AzleInt32 as any; +export const int32 = AzleInt32; export type int32 = number; diff --git a/src/lib/candid/types/primitive/ints/int64.ts b/src/lib/candid/types/primitive/ints/int64.ts index ab5f92c88c..5cb1b73c8e 100644 --- a/src/lib/candid/types/primitive/ints/int64.ts +++ b/src/lib/candid/types/primitive/ints/int64.ts @@ -1,12 +1,22 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleInt64 { _azleKind: 'AzleInt64' = 'AzleInt64'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Int64; } } -export const int64: AzleInt64 = AzleInt64 as any; +export const int64 = AzleInt64; export type int64 = bigint; diff --git a/src/lib/candid/types/primitive/ints/int8.ts b/src/lib/candid/types/primitive/ints/int8.ts index baba4aa495..ad82f6250e 100644 --- a/src/lib/candid/types/primitive/ints/int8.ts +++ b/src/lib/candid/types/primitive/ints/int8.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleInt8 { _azleKind: 'AzleInt8' = 'AzleInt8'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Int8; } } -export const int8: AzleInt8 = AzleInt8 as any; +export const int8 = AzleInt8; export type int8 = number; diff --git a/src/lib/candid/types/primitive/nats/nat.ts b/src/lib/candid/types/primitive/nats/nat.ts index a33c9c5273..d7ad62a7a2 100644 --- a/src/lib/candid/types/primitive/nats/nat.ts +++ b/src/lib/candid/types/primitive/nats/nat.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleNat { _azleKind: 'AzleNat' = 'AzleNat'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Nat; } } -export const nat: AzleNat = AzleNat as any; +export const nat = AzleNat; export type nat = bigint; diff --git a/src/lib/candid/types/primitive/nats/nat16.ts b/src/lib/candid/types/primitive/nats/nat16.ts index d8108ef235..c50b277f1f 100644 --- a/src/lib/candid/types/primitive/nats/nat16.ts +++ b/src/lib/candid/types/primitive/nats/nat16.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleNat16 { _azleKind: 'AzleNat16' = 'AzleNat16'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Nat16; } } -export const nat16: AzleNat16 = AzleNat16 as any; +export const nat16 = AzleNat16; export type nat16 = number; diff --git a/src/lib/candid/types/primitive/nats/nat32.ts b/src/lib/candid/types/primitive/nats/nat32.ts index 735cd36d58..7d5cbc21a9 100644 --- a/src/lib/candid/types/primitive/nats/nat32.ts +++ b/src/lib/candid/types/primitive/nats/nat32.ts @@ -1,12 +1,22 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleNat32 { _azleKind: 'AzleNat32' = 'AzleNat32'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Nat32; } } -export const nat32: AzleNat32 = AzleNat32 as any; +export const nat32 = AzleNat32; export type nat32 = number; diff --git a/src/lib/candid/types/primitive/nats/nat64.ts b/src/lib/candid/types/primitive/nats/nat64.ts index 82f36fcdce..543c6ae9a8 100644 --- a/src/lib/candid/types/primitive/nats/nat64.ts +++ b/src/lib/candid/types/primitive/nats/nat64.ts @@ -1,12 +1,22 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleNat64 { _azleKind: 'AzleNat64' = 'AzleNat64'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Nat64; } } -export const nat64: AzleNat64 = AzleNat64 as any; +export const nat64 = AzleNat64; export type nat64 = bigint; diff --git a/src/lib/candid/types/primitive/nats/nat8.ts b/src/lib/candid/types/primitive/nats/nat8.ts index 266d45d5c1..8ca55a383f 100644 --- a/src/lib/candid/types/primitive/nats/nat8.ts +++ b/src/lib/candid/types/primitive/nats/nat8.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../../serde/encode'; +import { decode } from '../../../serde/decode'; export class AzleNat8 { _azleKind: 'AzleNat8' = 'AzleNat8'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Nat8; } } -export const nat8: AzleNat8 = AzleNat8 as any; +export const nat8 = AzleNat8; export type nat8 = number; diff --git a/src/lib/candid/types/primitive/null.ts b/src/lib/candid/types/primitive/null.ts index 465c627ade..1d00628369 100644 --- a/src/lib/candid/types/primitive/null.ts +++ b/src/lib/candid/types/primitive/null.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class AzleNull { _azleKind: 'AzleNull' = 'AzleNull'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Null; } } -export const Null: AzleNull = AzleNull as any; +export const Null = AzleNull; export type Null = null; diff --git a/src/lib/candid/types/primitive/reserved.ts b/src/lib/candid/types/primitive/reserved.ts index dedb6a3518..491d0f5998 100644 --- a/src/lib/candid/types/primitive/reserved.ts +++ b/src/lib/candid/types/primitive/reserved.ts @@ -1,13 +1,23 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class AzleReserved { _azleKind: 'AzleReserved' = 'AzleReserved'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return IDL.Reserved; } } -export const reserved: AzleReserved = AzleReserved as any; +export const reserved = AzleReserved; export type reserved = any; diff --git a/src/lib/candid/types/primitive/text.ts b/src/lib/candid/types/primitive/text.ts index c727904699..26649140fa 100644 --- a/src/lib/candid/types/primitive/text.ts +++ b/src/lib/candid/types/primitive/text.ts @@ -1,13 +1,24 @@ import { IDL } from '@dfinity/candid'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class AzleText { _azleKind: 'AzleText' = 'AzleText'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + // TODO fix all of the return types + static fromBytes(bytes: Uint8Array): string { + return decode(this, bytes); + } + static getIdl() { return IDL.Text; } } -export const text: AzleText = AzleText as any; +export const text = AzleText; export type text = string; diff --git a/src/lib/candid/types/primitive/void.ts b/src/lib/candid/types/primitive/void.ts index a1c02ddf1a..37bbf2c5f2 100644 --- a/src/lib/candid/types/primitive/void.ts +++ b/src/lib/candid/types/primitive/void.ts @@ -1,11 +1,22 @@ +import { decode } from '../../serde/decode'; +import { encode } from '../../serde/encode'; + export class AzleVoid { _azleKind: 'AzleVoid' = 'AzleVoid'; _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl() { return []; } } -export const Void: AzleVoid = AzleVoid as any; +export const Void = AzleVoid; export type Void = void; diff --git a/src/lib/candid/types/reference/func.ts b/src/lib/candid/types/reference/func.ts index a520a3937f..51605d9c3c 100644 --- a/src/lib/candid/types/reference/func.ts +++ b/src/lib/candid/types/reference/func.ts @@ -1,6 +1,8 @@ import { CandidType, Parent, toIdlArray } from '../../index'; import { IDL } from '@dfinity/candid'; import { Principal } from './principal'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; type Mode = 'query' | 'update' | 'oneway'; @@ -14,8 +16,14 @@ export function Func( paramCandidTypes: CandidType[], returnCandidTypes: CandidType, mode: Mode -): [Principal, string] & { _azleCandidType?: '_azleCandidType' } { +): [Principal, string] & CandidType { return { + toBytes(data: number): Uint8Array { + return encode(this, data); + }, + fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + }, getIdl(parents: Parent[]) { return IDL.Func( toIdlArray(paramCandidTypes, parents), diff --git a/src/lib/candid/types/reference/principal.ts b/src/lib/candid/types/reference/principal.ts index f98637387f..adeed7db6d 100644 --- a/src/lib/candid/types/reference/principal.ts +++ b/src/lib/candid/types/reference/principal.ts @@ -1,9 +1,19 @@ import { IDL } from '@dfinity/candid'; import { Principal as DfinityPrincipal } from '@dfinity/principal'; +import { encode } from '../../serde/encode'; +import { decode } from '../../serde/decode'; export class Principal extends DfinityPrincipal { static _azleCandidType?: '_azleCandidType'; + static toBytes(data: number): Uint8Array { + return encode(this, data); + } + + static fromBytes(bytes: Uint8Array): number { + return decode(this, bytes); + } + static getIdl?() { return IDL.Principal; } diff --git a/src/lib/candid/types/reference/service/index.ts b/src/lib/candid/types/reference/service/index.ts index a1f6a4b402..539f8a6e08 100644 --- a/src/lib/candid/types/reference/service/index.ts +++ b/src/lib/candid/types/reference/service/index.ts @@ -1,5 +1,5 @@ import { CanisterMethodInfo } from '../../../../canister_methods/types/canister_method_info'; -import { TypeMapping } from '../../../index'; +import { CandidType, TypeMapping } from '../../../index'; import { _AzleRecursiveFunction } from '../../../recursive'; import { Principal } from '../principal'; import { createCanisterFunction } from './canister_function'; @@ -30,7 +30,7 @@ type _AzleCanisterReturnType = { export function Canister( canisterOptions: T -): CallableObject & { _azleCandidType?: '_azleCandidType' } { +): CallableObject & CandidType { let result: _AzleCanisterReturnType = (parentOrPrincipal: any) => { const canisterFunction = createCanisterFunction(canisterOptions); diff --git a/src/lib/stable_b_tree_map.ts b/src/lib/stable_b_tree_map.ts index bd41ff57b1..06ba033ecf 100644 --- a/src/lib/stable_b_tree_map.ts +++ b/src/lib/stable_b_tree_map.ts @@ -1,19 +1,41 @@ -import { CandidType, TypeMapping } from './candid'; +import { TypeMapping } from './candid'; import { None, Opt, Some } from './candid/types/constructed/opt'; import { nat64 } from './candid/types/primitive/nats/nat64'; import { nat8 } from './candid/types/primitive/nats/nat8'; import { encode, decode } from './candid/serde'; +// TODO we should probably try to make it work with bigint, Principal, etc +// TODO out of the box +// TODO we probably need to allow the user to pass in their own encoding/decoding for Json as well +// TODO we need a way to make the types good in TypeMapping +export class StableJson { + static toBytes(data: any): Uint8Array { + return Uint8Array.from(Buffer.from(JSON.stringify(data))); + } + + static fromBytes(bytes: Uint8Array): any { + return JSON.parse(Buffer.from(bytes).toString()); + } +} + +export interface Serializable { + toBytes: (data: any) => Uint8Array; + fromBytes: (bytes: Uint8Array) => any; +} + export function StableBTreeMap< - Key extends CandidType, - Value extends CandidType + Key extends Partial, + Value extends Partial >(keyType: Key, valueType: Value, memoryId: nat8) { - if (globalThis._azleIc === undefined) { - return undefined as any; - } + // TODO we don't really need to candid encode this, it's just a number const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - globalThis._azleIc.stableBTreeMapInit(candidEncodedMemoryId); + if (globalThis._azleIc !== undefined) { + globalThis._azleIc.stableBTreeMapInit(candidEncodedMemoryId); + } + + isSerializable(keyType); + isSerializable(valueType); return { /** @@ -26,12 +48,11 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - const candidEncodedKey = encode(keyType, key).buffer; + const encodedKey = keyType.toBytes(key).buffer; return globalThis._azleIc.stableBTreeMapContainsKey( candidEncodedMemoryId, - candidEncodedKey + encodedKey ); }, /** @@ -44,18 +65,17 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - const candidEncodedKey = encode(keyType, key).buffer; + const encodedKey = keyType.toBytes(key).buffer; - const candidEncodedValue = globalThis._azleIc.stableBTreeMapGet( + const encodedResult = globalThis._azleIc.stableBTreeMapGet( candidEncodedMemoryId, - candidEncodedKey + encodedKey ); - if (candidEncodedValue === undefined) { + if (encodedResult === undefined) { return None; } else { - return Some(decode(valueType, candidEncodedValue)); + return Some(valueType.fromBytes(new Uint8Array(encodedResult))); } }, /** @@ -72,21 +92,19 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - const candidEncodedKey = encode(keyType, key).buffer; - const candidEncodedValue = encode(valueType, value).buffer; + const encodedKey = keyType.toBytes(key).buffer; + const encodedValue = valueType.toBytes(value).buffer; - const candidEncodedResultValue = - globalThis._azleIc.stableBTreeMapInsert( - candidEncodedMemoryId, - candidEncodedKey, - candidEncodedValue - ); + const encodedResult = globalThis._azleIc.stableBTreeMapInsert( + candidEncodedMemoryId, + encodedKey, + encodedValue + ); - if (candidEncodedResultValue === undefined) { + if (encodedResult === undefined) { return None; } else { - return Some(decode(valueType, candidEncodedResultValue)); + return Some(valueType.fromBytes(new Uint8Array(encodedResult))); } }, /** @@ -98,8 +116,6 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - return globalThis._azleIc.stableBTreeMapIsEmpty( candidEncodedMemoryId ); @@ -113,17 +129,15 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - - const candidEncodedItems = globalThis._azleIc.stableBTreeMapItems( + const encodedItems = globalThis._azleIc.stableBTreeMapItems( candidEncodedMemoryId ); // TODO too much copying - return candidEncodedItems.map((candidEncodedItem) => { + return encodedItems.map(([encodedKey, encodedValue]) => { return [ - decode(keyType, candidEncodedItem[0]), - decode(valueType, candidEncodedItem[1]) + keyType.fromBytes(new Uint8Array(encodedKey)), + valueType.fromBytes(new Uint8Array(encodedValue)) ]; }); }, @@ -136,15 +150,13 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - - const candidEncodedKeys = globalThis._azleIc.stableBTreeMapKeys( + const encodedKeys = globalThis._azleIc.stableBTreeMapKeys( candidEncodedMemoryId ); // TODO too much copying - return candidEncodedKeys.map((candidEncodedKey) => { - return decode(keyType, candidEncodedKey); + return encodedKeys.map((encodedKey) => { + return keyType.fromBytes(new Uint8Array(encodedKey)); }); }, /** @@ -156,8 +168,6 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - const candidEncodedLen = globalThis._azleIc.stableBTreeMapLen( candidEncodedMemoryId ); @@ -174,18 +184,17 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - const candidEncodedKey = encode(keyType, key).buffer; + const encodedKey = keyType.toBytes(key).buffer; - const candidEncodedValue = globalThis._azleIc.stableBTreeMapRemove( + const encodedValue = globalThis._azleIc.stableBTreeMapRemove( candidEncodedMemoryId, - candidEncodedKey + encodedKey ); - if (candidEncodedValue === undefined) { + if (encodedValue === undefined) { return None; } else { - return Some(decode(valueType, candidEncodedValue)); + return Some(valueType.fromBytes(new Uint8Array(encodedValue))); } }, /** @@ -197,16 +206,24 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - - const candidEncodedValues = globalThis._azleIc.stableBTreeMapValues( + const encodedValues = globalThis._azleIc.stableBTreeMapValues( candidEncodedMemoryId ); // TODO too much copying - return candidEncodedValues.map((candidEncodedValue) => { - return decode(valueType, candidEncodedValue); + return encodedValues.map((encodedValue) => { + return valueType.fromBytes(new Uint8Array(encodedValue)); }); } }; } + +function isSerializable(obj: any): asserts obj is Serializable { + if (obj.toBytes === undefined) { + throw new Error(`value must have a toBytes method`); + } + + if (obj.fromBytes === undefined) { + throw new Error(`value must have a fromBytes method`); + } +} From df26b97a3b161e3501ffd93a67d5217803a40d32 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Tue, 24 Oct 2023 16:37:52 -0500 Subject: [PATCH 02/30] add type tests, almost done removing type errors --- examples/audio_recorder/test/tests.ts | 15 ++- examples/basic_bitcoin/src/bitcoin_api.ts | 2 + .../basic_bitcoin/src/bitcoin_plugin/index.ts | 2 + examples/basic_bitcoin/src/bitcoin_wallet.ts | 2 + examples/basic_bitcoin/src/ecdsa_api.ts | 2 + examples/basic_bitcoin/src/index.ts | 2 + examples/basic_bitcoin/src/types.ts | 2 + examples/bitcoin/test/setup.ts | 2 +- examples/ckbtc/test/test.ts | 2 +- examples/ckbtc/test/tests.ts | 3 + .../ckbtc/wallet/frontend/elements/ck-app.ts | 2 + examples/complex_init/test/test.ts | 1 + examples/complex_init/test/tests.ts | 1 + examples/generics/src/index.ts | 2 + examples/list_of_lists/test/tests.ts | 7 +- examples/manual_reply/test/tests.ts | 9 +- examples/motoko_examples/calc/test/test.ts | 1 + examples/motoko_examples/calc/test/tests.ts | 1 + examples/motoko_examples/counter/test/test.ts | 1 + .../motoko_examples/counter/test/tests.ts | 1 + examples/motoko_examples/echo/test/test.ts | 1 + examples/motoko_examples/echo/test/tests.ts | 1 + .../motoko_examples/factorial/test/test.ts | 1 + .../motoko_examples/factorial/test/tests.ts | 1 + .../motoko_examples/hello-world/test/test.ts | 1 + .../motoko_examples/hello-world/test/tests.ts | 1 + examples/motoko_examples/hello/test/test.ts | 1 + examples/motoko_examples/hello/test/tests.ts | 1 + .../minimal-counter-dapp/test/test.ts | 1 + .../minimal-counter-dapp/test/tests.ts | 1 + .../motoko_examples/quicksort/test/test.ts | 1 + .../motoko_examples/quicksort/test/tests.ts | 1 + .../motoko_examples/simple-to-do/test/test.ts | 1 + .../simple-to-do/test/tests.ts | 18 +-- .../motoko_examples/superheroes/test/tests.ts | 1 + examples/plugins/ic_sqlite_plugin/index.ts | 2 + examples/plugins/src/index.ts | 2 + .../canisters/azle/data_types/opt/index.ts | 6 +- .../azle/data_types/variant/index.ts | 4 +- examples/principal/test/tests.ts | 104 ++++++++---------- examples/query/test/tests.ts | 1 + examples/recursion/test/test.ts | 2 + examples/recursion/test/tests.ts | 2 + examples/run_time_errors/src/blob.ts | 2 + examples/run_time_errors/src/func.ts | 2 + examples/run_time_errors/src/index.ts | 2 + examples/run_time_errors/src/numbers.ts | 2 + examples/run_time_errors/src/opt.ts | 2 + examples/run_time_errors/src/primitives.ts | 2 + examples/run_time_errors/src/principals.ts | 2 + examples/run_time_errors/src/records.ts | 2 + examples/run_time_errors/src/results.ts | 2 + examples/run_time_errors/src/throws.ts | 2 + examples/run_time_errors/src/variants.ts | 2 + examples/run_time_errors/src/vecs.ts | 2 + .../run_time_errors/test/invalid_blobs.ts | 2 + .../run_time_errors/test/invalid_funcs.ts | 2 + .../run_time_errors/test/invalid_numbers.ts | 2 + examples/run_time_errors/test/invalid_opts.ts | 2 + .../test/invalid_primitives.ts | 2 + .../test/invalid_principals.ts | 2 + .../run_time_errors/test/invalid_records.ts | 2 + .../run_time_errors/test/invalid_results.ts | 2 + .../run_time_errors/test/invalid_variants.ts | 2 + examples/run_time_errors/test/invalid_vecs.ts | 2 + examples/run_time_errors/test/pretest.ts | 2 + examples/run_time_errors/test/test.ts | 2 + examples/run_time_errors/test/tests.ts | 2 + .../run_time_errors/test/thrown_errors.ts | 2 + .../src/canister1/stable_map_0.ts | 2 +- .../src/canister1/stable_map_1.ts | 2 +- examples/update/test/tests.ts | 6 +- src/compiler/utils/types.ts | 4 +- src/lib/candid/candid_type.ts | 8 +- src/lib/candid/type_mapping.ts | 10 +- src/lib/candid/types/constructed/blob.ts | 2 +- src/lib/candid/types/constructed/record.ts | 4 +- src/lib/candid/types/constructed/variant.ts | 4 +- src/lib/candid/types/primitive/bool.ts | 2 +- src/lib/candid/types/primitive/empty.ts | 2 +- .../candid/types/primitive/floats/float32.ts | 2 +- .../candid/types/primitive/floats/float64.ts | 2 +- src/lib/candid/types/primitive/ints/int.ts | 2 +- src/lib/candid/types/primitive/ints/int16.ts | 2 +- src/lib/candid/types/primitive/ints/int32.ts | 2 +- src/lib/candid/types/primitive/ints/int64.ts | 2 +- src/lib/candid/types/primitive/ints/int8.ts | 2 +- src/lib/candid/types/primitive/nats/nat.ts | 2 +- src/lib/candid/types/primitive/nats/nat16.ts | 4 +- src/lib/candid/types/primitive/nats/nat32.ts | 2 +- src/lib/candid/types/primitive/nats/nat64.ts | 2 +- src/lib/candid/types/primitive/nats/nat8.ts | 4 +- src/lib/candid/types/primitive/null.ts | 2 +- src/lib/candid/types/primitive/reserved.ts | 2 +- src/lib/candid/types/primitive/text.ts | 5 +- src/lib/candid/types/primitive/void.ts | 3 + src/lib/candid/types/reference/func.ts | 3 +- .../service/canister_function/index.ts | 2 +- src/lib/canister_methods/methods/query.ts | 12 ++ src/lib/ic/types/duration.ts | 2 +- src/lib/ic/types/timer_id.ts | 2 +- tsconfig.json | 3 +- type_tests/candid/constructed/blob.ts | 4 + type_tests/candid/constructed/opt.ts | 4 + type_tests/candid/constructed/record.ts | 85 ++++++++++++++ type_tests/candid/constructed/tuple.ts | 0 type_tests/candid/constructed/variant.ts | 0 type_tests/candid/constructed/vec.ts | 9 ++ type_tests/candid/manual.ts | 7 ++ type_tests/candid/primitive/bool.ts | 4 + type_tests/candid/primitive/empty.ts | 4 + type_tests/candid/primitive/floats/float32.ts | 4 + type_tests/candid/primitive/floats/float64.ts | 4 + type_tests/candid/primitive/ints/int.ts | 4 + type_tests/candid/primitive/ints/int16.ts | 4 + type_tests/candid/primitive/ints/int32.ts | 4 + type_tests/candid/primitive/ints/int64.ts | 4 + type_tests/candid/primitive/ints/int8.ts | 4 + type_tests/candid/primitive/nats/nat.ts | 4 + type_tests/candid/primitive/nats/nat16.ts | 4 + type_tests/candid/primitive/nats/nat32.ts | 4 + type_tests/candid/primitive/nats/nat64.ts | 4 + type_tests/candid/primitive/nats/nat8.ts | 4 + type_tests/candid/primitive/null.ts | 4 + type_tests/candid/primitive/reserved.ts | 4 + type_tests/candid/primitive/text.ts | 4 + type_tests/candid/primitive/void.ts | 7 ++ type_tests/index.ts | 72 ++++++++++++ type_tests/package-lock.json | 21 ++++ type_tests/package.json | 5 + 130 files changed, 529 insertions(+), 130 deletions(-) create mode 100644 type_tests/candid/constructed/blob.ts create mode 100644 type_tests/candid/constructed/opt.ts create mode 100644 type_tests/candid/constructed/record.ts create mode 100644 type_tests/candid/constructed/tuple.ts create mode 100644 type_tests/candid/constructed/variant.ts create mode 100644 type_tests/candid/constructed/vec.ts create mode 100644 type_tests/candid/manual.ts create mode 100644 type_tests/candid/primitive/bool.ts create mode 100644 type_tests/candid/primitive/empty.ts create mode 100644 type_tests/candid/primitive/floats/float32.ts create mode 100644 type_tests/candid/primitive/floats/float64.ts create mode 100644 type_tests/candid/primitive/ints/int.ts create mode 100644 type_tests/candid/primitive/ints/int16.ts create mode 100644 type_tests/candid/primitive/ints/int32.ts create mode 100644 type_tests/candid/primitive/ints/int64.ts create mode 100644 type_tests/candid/primitive/ints/int8.ts create mode 100644 type_tests/candid/primitive/nats/nat.ts create mode 100644 type_tests/candid/primitive/nats/nat16.ts create mode 100644 type_tests/candid/primitive/nats/nat32.ts create mode 100644 type_tests/candid/primitive/nats/nat64.ts create mode 100644 type_tests/candid/primitive/nats/nat8.ts create mode 100644 type_tests/candid/primitive/null.ts create mode 100644 type_tests/candid/primitive/reserved.ts create mode 100644 type_tests/candid/primitive/text.ts create mode 100644 type_tests/candid/primitive/void.ts create mode 100644 type_tests/index.ts create mode 100644 type_tests/package-lock.json create mode 100644 type_tests/package.json diff --git a/examples/audio_recorder/test/tests.ts b/examples/audio_recorder/test/tests.ts index d7398f9b6d..3e2cef553f 100644 --- a/examples/audio_recorder/test/tests.ts +++ b/examples/audio_recorder/test/tests.ts @@ -1,15 +1,15 @@ import { ok, Test } from 'azle/test'; import { - Recording, - _SERVICE, - User + rec_2, + rec_7, + _SERVICE } from './dfx_generated/audio_recorder/audio_recorder.did'; import { ActorSubclass } from '@dfinity/agent'; // TODO to be more thorough we could test all of the error cases as well -let global_user: User; -let global_recording: Recording; +let global_user: rec_2; +let global_recording: rec_7; export function get_tests( audio_recorder_canister: ActorSubclass<_SERVICE> @@ -18,9 +18,8 @@ export function get_tests( { name: 'create_user', test: async () => { - const user = await audio_recorder_canister.createUser( - 'lastmjs' - ); + const user = + await audio_recorder_canister.createUser('lastmjs'); global_user = user; diff --git a/examples/basic_bitcoin/src/bitcoin_api.ts b/examples/basic_bitcoin/src/bitcoin_api.ts index c482bfceea..cdfbbf1142 100644 --- a/examples/basic_bitcoin/src/bitcoin_api.ts +++ b/examples/basic_bitcoin/src/bitcoin_api.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { blob, ic, match, nat64, Opt, Vec } from 'azle'; import { BitcoinNetwork, diff --git a/examples/basic_bitcoin/src/bitcoin_plugin/index.ts b/examples/basic_bitcoin/src/bitcoin_plugin/index.ts index 53f368b170..4620517d80 100644 --- a/examples/basic_bitcoin/src/bitcoin_plugin/index.ts +++ b/examples/basic_bitcoin/src/bitcoin_plugin/index.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { blob, int32, nat32, nat64, registerPlugin, Vec } from 'azle'; export type BitcoinAddress = { diff --git a/examples/basic_bitcoin/src/bitcoin_wallet.ts b/examples/basic_bitcoin/src/bitcoin_wallet.ts index 544fa422b3..36d9835448 100644 --- a/examples/basic_bitcoin/src/bitcoin_wallet.ts +++ b/examples/basic_bitcoin/src/bitcoin_wallet.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + //! A demo of a very bare-bones bitcoin "wallet". //! //! The wallet here showcases how bitcoin addresses can be be computed diff --git a/examples/basic_bitcoin/src/ecdsa_api.ts b/examples/basic_bitcoin/src/ecdsa_api.ts index a019b207b2..24aa9aa607 100644 --- a/examples/basic_bitcoin/src/ecdsa_api.ts +++ b/examples/basic_bitcoin/src/ecdsa_api.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { blob, ic, match, Opt, Vec } from 'azle'; import { managementCanister } from 'azle/canisters/management'; diff --git a/examples/basic_bitcoin/src/index.ts b/examples/basic_bitcoin/src/index.ts index bc997ee7ac..a776d9c87c 100644 --- a/examples/basic_bitcoin/src/index.ts +++ b/examples/basic_bitcoin/src/index.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { blob, $init, match, nat64, $postUpgrade, $update, Vec } from 'azle'; import { BitcoinNetwork, diff --git a/examples/basic_bitcoin/src/types.ts b/examples/basic_bitcoin/src/types.ts index c641f96178..2f2e17321f 100644 --- a/examples/basic_bitcoin/src/types.ts +++ b/examples/basic_bitcoin/src/types.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { nat64, Record } from 'azle'; export type SendRequest = Record<{ diff --git a/examples/bitcoin/test/setup.ts b/examples/bitcoin/test/setup.ts index 04a7ee591b..00fef8e7b7 100644 --- a/examples/bitcoin/test/setup.ts +++ b/examples/bitcoin/test/setup.ts @@ -45,7 +45,7 @@ export function installBitcoin() { } export async function whileRunningBitcoinDaemon( - callback: () => Promise | void + callback: () => Promise | void ) { installBitcoin(); const bitcoinDaemon = await startBitcoinDaemon(); diff --git a/examples/ckbtc/test/test.ts b/examples/ckbtc/test/test.ts index 33d91ece9d..d7497a0c4d 100644 --- a/examples/ckbtc/test/test.ts +++ b/examples/ckbtc/test/test.ts @@ -5,7 +5,7 @@ import { existsSync, rmSync } from 'fs-extra'; import { getTests } from './tests'; export async function whileRunningBitcoinDaemon( - callback: () => Promise | void + callback: () => Promise | void ) { const bitcoinDaemon = await startBitcoinDaemon(); await callback(); diff --git a/examples/ckbtc/test/tests.ts b/examples/ckbtc/test/tests.ts index 3683a4c88f..9cb6cfd077 100644 --- a/examples/ckbtc/test/tests.ts +++ b/examples/ckbtc/test/tests.ts @@ -5,7 +5,10 @@ import { Identity } from '@dfinity/agent'; import { Ed25519KeyIdentity } from '@dfinity/identity'; import { getCanisterId } from 'azle/test'; +// @ts-ignore import { _SERVICE } from '../wallet/frontend/dfx_generated/wallet_backend/wallet_backend.did'; + +// @ts-ignore import { createActor } from '../wallet/frontend/dfx_generated/wallet_backend'; type Config = { diff --git a/examples/ckbtc/wallet/frontend/elements/ck-app.ts b/examples/ckbtc/wallet/frontend/elements/ck-app.ts index 0122b793c6..4ab02c9c87 100644 --- a/examples/ckbtc/wallet/frontend/elements/ck-app.ts +++ b/examples/ckbtc/wallet/frontend/elements/ck-app.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { LitElement, html } from 'lit'; import { customElement, state } from 'lit/decorators.js'; import { AuthClient } from '@dfinity/auth-client'; diff --git a/examples/complex_init/test/test.ts b/examples/complex_init/test/test.ts index a8d214f6d3..26ef8a33d8 100644 --- a/examples/complex_init/test/test.ts +++ b/examples/complex_init/test/test.ts @@ -1,5 +1,6 @@ import { getCanisterId, runTests } from 'azle/test'; import { createActor as createComplexActor } from '../test/dfx_generated/complex_init'; +// @ts-ignore import { createActor as createRecActor } from '../test/dfx_generated/rec_init'; import { get_rec_tests, get_tests } from './tests'; diff --git a/examples/complex_init/test/tests.ts b/examples/complex_init/test/tests.ts index cf96b8917d..da1ac55ecb 100644 --- a/examples/complex_init/test/tests.ts +++ b/examples/complex_init/test/tests.ts @@ -1,6 +1,7 @@ import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE as _COMPLEX_SERVICE } from './dfx_generated/complex_init/complex_init.did'; +// @ts-ignore import { _SERVICE as _REC_SERVICE } from './dfx_generated/rec_init/rec_init.did'; export function get_tests( diff --git a/examples/generics/src/index.ts b/examples/generics/src/index.ts index 4cfd8e2633..cd8ab7e8bd 100644 --- a/examples/generics/src/index.ts +++ b/examples/generics/src/index.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { nat64, $query, Record, Result, Tuple, Variant, Vec } from 'azle'; type SimpleResult = Result; diff --git a/examples/list_of_lists/test/tests.ts b/examples/list_of_lists/test/tests.ts index 1bf9cf0044..742a075ce6 100644 --- a/examples/list_of_lists/test/tests.ts +++ b/examples/list_of_lists/test/tests.ts @@ -7,6 +7,9 @@ import { Principal } from '@dfinity/principal'; type DeepArray = arr | Array>; type arr = + | BigInt64Array + | bigint[] + | number[] | Int8Array | Int16Array | Int32Array @@ -482,7 +485,9 @@ function arrEqual(a: DeepArray, b: DeepArray): boolean { if (Array.isArray(a) && Array.isArray(b)) { return ( a.length === b.length && - a.every((value, index) => arrEqual(value, b[index])) + (a as any).every((value: any, index: any) => + arrEqual(value, (b as any)[index]) + ) ); } if (Array.isArray(a) || Array.isArray(b)) { diff --git a/examples/manual_reply/test/tests.ts b/examples/manual_reply/test/tests.ts index aaca7bd84e..81f2772f4b 100644 --- a/examples/manual_reply/test/tests.ts +++ b/examples/manual_reply/test/tests.ts @@ -1,8 +1,5 @@ import { Test } from 'azle/test'; -import { - ManualReply, - _SERVICE -} from './dfx_generated/manual_reply/manual_reply.did'; +import { rec_0, _SERVICE } from './dfx_generated/manual_reply/manual_reply.did'; import { ActorSubclass } from '@dfinity/agent'; export function getTests(manualReplyCanister: ActorSubclass<_SERVICE>): Test[] { @@ -107,7 +104,7 @@ export function getTests(manualReplyCanister: ActorSubclass<_SERVICE>): Test[] { name: 'update reply with record', test: async () => { const result = await manualReplyCanister.updateRecord(); - const expectedResult: ManualReply = { + const expectedResult: rec_0 = { id: 'b0283eb7-9c0e-41e5-8089-3345e6a8fa6a', orbitals: [ { @@ -257,7 +254,7 @@ export function getTests(manualReplyCanister: ActorSubclass<_SERVICE>): Test[] { name: 'query reply with record', test: async () => { const result = await manualReplyCanister.queryRecord(); - const expectedResult: ManualReply = { + const expectedResult: rec_0 = { id: 'b0283eb7-9c0e-41e5-8089-3345e6a8fa6a', orbitals: [ { diff --git a/examples/motoko_examples/calc/test/test.ts b/examples/motoko_examples/calc/test/test.ts index 4da958bfc2..bdf202f609 100644 --- a/examples/motoko_examples/calc/test/test.ts +++ b/examples/motoko_examples/calc/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from './dfx_generated/calc'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/calc/test/tests.ts b/examples/motoko_examples/calc/test/tests.ts index 369afb189b..16de684ffe 100644 --- a/examples/motoko_examples/calc/test/tests.ts +++ b/examples/motoko_examples/calc/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from './dfx_generated/calc/calc.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/motoko_examples/counter/test/test.ts b/examples/motoko_examples/counter/test/test.ts index fbca135d63..4464500e76 100644 --- a/examples/motoko_examples/counter/test/test.ts +++ b/examples/motoko_examples/counter/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from './dfx_generated/counter'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/counter/test/tests.ts b/examples/motoko_examples/counter/test/tests.ts index 58475220a0..7e27cac790 100644 --- a/examples/motoko_examples/counter/test/tests.ts +++ b/examples/motoko_examples/counter/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from './dfx_generated/counter/counter.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/motoko_examples/echo/test/test.ts b/examples/motoko_examples/echo/test/test.ts index 08db9e0187..f52360d7bd 100644 --- a/examples/motoko_examples/echo/test/test.ts +++ b/examples/motoko_examples/echo/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from './dfx_generated/echo'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/echo/test/tests.ts b/examples/motoko_examples/echo/test/tests.ts index 37bb664691..fe26bbbeb9 100644 --- a/examples/motoko_examples/echo/test/tests.ts +++ b/examples/motoko_examples/echo/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from './dfx_generated/echo/echo.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/motoko_examples/factorial/test/test.ts b/examples/motoko_examples/factorial/test/test.ts index 2e3dcaf2ec..8b2976c944 100644 --- a/examples/motoko_examples/factorial/test/test.ts +++ b/examples/motoko_examples/factorial/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from './dfx_generated/factorial'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/factorial/test/tests.ts b/examples/motoko_examples/factorial/test/tests.ts index 7563a2e433..832672b4cd 100644 --- a/examples/motoko_examples/factorial/test/tests.ts +++ b/examples/motoko_examples/factorial/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from './dfx_generated/factorial/factorial.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/motoko_examples/hello-world/test/test.ts b/examples/motoko_examples/hello-world/test/test.ts index a31930ceb9..e715b8f203 100644 --- a/examples/motoko_examples/hello-world/test/test.ts +++ b/examples/motoko_examples/hello-world/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from './dfx_generated/hello_world'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/hello-world/test/tests.ts b/examples/motoko_examples/hello-world/test/tests.ts index 5bd1127755..41693e8212 100644 --- a/examples/motoko_examples/hello-world/test/tests.ts +++ b/examples/motoko_examples/hello-world/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from './dfx_generated/hello_world/hello_world.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/motoko_examples/hello/test/test.ts b/examples/motoko_examples/hello/test/test.ts index b629ef31bb..3cf718d89b 100644 --- a/examples/motoko_examples/hello/test/test.ts +++ b/examples/motoko_examples/hello/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from '../dfx_generated/hello'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/hello/test/tests.ts b/examples/motoko_examples/hello/test/tests.ts index e183c420c3..3f8e007fcc 100644 --- a/examples/motoko_examples/hello/test/tests.ts +++ b/examples/motoko_examples/hello/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from '../dfx_generated/hello/hello.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/motoko_examples/minimal-counter-dapp/test/test.ts b/examples/motoko_examples/minimal-counter-dapp/test/test.ts index 13c4ef981e..9c07c62323 100644 --- a/examples/motoko_examples/minimal-counter-dapp/test/test.ts +++ b/examples/motoko_examples/minimal-counter-dapp/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from '../src/declarations/minimal_dapp'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/minimal-counter-dapp/test/tests.ts b/examples/motoko_examples/minimal-counter-dapp/test/tests.ts index ec3a60ab30..522cf26690 100644 --- a/examples/motoko_examples/minimal-counter-dapp/test/tests.ts +++ b/examples/motoko_examples/minimal-counter-dapp/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from '../src/declarations/minimal_dapp/minimal_dapp.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/motoko_examples/quicksort/test/test.ts b/examples/motoko_examples/quicksort/test/test.ts index 5504fcfbab..df81a7a00f 100644 --- a/examples/motoko_examples/quicksort/test/test.ts +++ b/examples/motoko_examples/quicksort/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from './dfx_generated/quicksort'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/quicksort/test/tests.ts b/examples/motoko_examples/quicksort/test/tests.ts index 0116961202..607f918f3a 100644 --- a/examples/motoko_examples/quicksort/test/tests.ts +++ b/examples/motoko_examples/quicksort/test/tests.ts @@ -1,5 +1,6 @@ import { Ok, Test } from 'azle/test'; import { int } from 'azle'; +// @ts-ignore import { _SERVICE } from './dfx_generated/quicksort/quicksort.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/motoko_examples/simple-to-do/test/test.ts b/examples/motoko_examples/simple-to-do/test/test.ts index 3a5801c7b9..b4ad78de67 100644 --- a/examples/motoko_examples/simple-to-do/test/test.ts +++ b/examples/motoko_examples/simple-to-do/test/test.ts @@ -1,4 +1,5 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from './dfx_generated/simple_to_do/'; import { getTests } from './tests'; diff --git a/examples/motoko_examples/simple-to-do/test/tests.ts b/examples/motoko_examples/simple-to-do/test/tests.ts index 3388ff7d64..1876b691ec 100644 --- a/examples/motoko_examples/simple-to-do/test/tests.ts +++ b/examples/motoko_examples/simple-to-do/test/tests.ts @@ -1,5 +1,6 @@ import { Test } from 'azle/test'; import { ToDo } from '../src'; +// @ts-ignore import { _SERVICE } from './dfx_generated/simple_to_do/simple_to_do.did'; import { ActorSubclass } from '@dfinity/agent'; @@ -14,7 +15,7 @@ export function getTests(todoCanister: ActorSubclass<_SERVICE>): Test[] { const result = await todoCanister.addTodo( FIRST_TODO_DESCRIPTION ); - const expectedResult: ToDo[] = [ + const expectedResult: (typeof ToDo)[] = [ { description: FIRST_TODO_DESCRIPTION, completed: false @@ -33,7 +34,7 @@ export function getTests(todoCanister: ActorSubclass<_SERVICE>): Test[] { const result = await todoCanister.addTodo( SECOND_TODO_DESCRIPTION ); - const expectedResult: ToDo[] = [ + const expectedResult: (typeof ToDo)[] = [ { description: FIRST_TODO_DESCRIPTION, completed: false @@ -64,7 +65,7 @@ export function getTests(todoCanister: ActorSubclass<_SERVICE>): Test[] { name: 'complete todo', test: async () => { const result = await todoCanister.completeTodo(1n); - const expectedResult: ToDo[] = [ + const expectedResult: (typeof ToDo)[] = [ { description: FIRST_TODO_DESCRIPTION, completed: false @@ -97,7 +98,7 @@ export function getTests(todoCanister: ActorSubclass<_SERVICE>): Test[] { name: 'clear completed todos', test: async () => { const result = await todoCanister.clearCompleted(); - const expectedResult: ToDo[] = [ + const expectedResult: (typeof ToDo)[] = [ { description: FIRST_TODO_DESCRIPTION, completed: false @@ -116,7 +117,7 @@ export function getTests(todoCanister: ActorSubclass<_SERVICE>): Test[] { name: 'complete todo', test: async () => { const result = await todoCanister.completeTodo(0n); - const expectedResult: ToDo[] = [ + const expectedResult: (typeof ToDo)[] = [ { description: FIRST_TODO_DESCRIPTION, completed: true @@ -145,14 +146,17 @@ export function getTests(todoCanister: ActorSubclass<_SERVICE>): Test[] { ]; } -function equalTodoList(listA: ToDo[], listB: ToDo[]): boolean { +function equalTodoList( + listA: (typeof ToDo)[], + listB: (typeof ToDo)[] +): boolean { return ( listA.length === listB.length && listA.every((item, index) => equalTodo(item, listB[index])) ); } -function equalTodo(todoA: ToDo, todoB: ToDo): boolean { +function equalTodo(todoA: typeof ToDo, todoB: typeof ToDo): boolean { return ( todoA.description === todoB.description && todoA.completed === todoB.completed diff --git a/examples/motoko_examples/superheroes/test/tests.ts b/examples/motoko_examples/superheroes/test/tests.ts index e0872f277f..1fc9c290b0 100644 --- a/examples/motoko_examples/superheroes/test/tests.ts +++ b/examples/motoko_examples/superheroes/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE, Superhero } from '../src/declarations/superheroes.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/plugins/ic_sqlite_plugin/index.ts b/examples/plugins/ic_sqlite_plugin/index.ts index b09edc69b2..0bce98e393 100644 --- a/examples/plugins/ic_sqlite_plugin/index.ts +++ b/examples/plugins/ic_sqlite_plugin/index.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { Record, registerPlugin, Result, Variant, Vec } from 'azle'; type SQLite = { diff --git a/examples/plugins/src/index.ts b/examples/plugins/src/index.ts index 74259bd04c..7b26b92804 100644 --- a/examples/plugins/src/index.ts +++ b/examples/plugins/src/index.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { $query, $update } from 'azle'; import { ExecuteResult, QueryResult, SQLite } from '../ic_sqlite_plugin'; diff --git a/examples/primitive_ops/canisters/azle/data_types/opt/index.ts b/examples/primitive_ops/canisters/azle/data_types/opt/index.ts index e0bd5c0d67..f6e271e0cd 100644 --- a/examples/primitive_ops/canisters/azle/data_types/opt/index.ts +++ b/examples/primitive_ops/canisters/azle/data_types/opt/index.ts @@ -1,4 +1,4 @@ -import { ic, nat32, Opt, update } from 'azle'; +import { ic, nat32, None, Opt, Some, update } from 'azle'; import { PerfResult } from '../../perf_result'; let opt_init_heap_storage: { [key: string]: Opt | undefined } = {}; @@ -9,7 +9,7 @@ export const opt_init_stack = update([nat32], PerfResult, (num_inits) => { let i = 0; while (i < num_inits) { - let value: Opt = i % 2 === 0 ? [true] : []; + let value: Opt = i % 2 === 0 ? Some(true) : None; // TODO std::convert::identity(value); consider something like Rust to ensure the value assignment above is never optimized away i += 1; } @@ -28,7 +28,7 @@ export const opt_init_heap = update([nat32], PerfResult, (num_inits) => { let i = 0; while (i < num_inits) { - opt_init_heap_storage[`bool${i}`] = i % 2 === 0 ? [true] : []; + opt_init_heap_storage[`bool${i}`] = i % 2 === 0 ? Some(true) : None; i += 1; } diff --git a/examples/primitive_ops/canisters/azle/data_types/variant/index.ts b/examples/primitive_ops/canisters/azle/data_types/variant/index.ts index 3ba198fd75..25473201aa 100644 --- a/examples/primitive_ops/canisters/azle/data_types/variant/index.ts +++ b/examples/primitive_ops/canisters/azle/data_types/variant/index.ts @@ -1,11 +1,11 @@ -import { ic, nat32, Null, principal, update, Variant } from 'azle'; +import { ic, nat32, Null, Principal, update, Variant } from 'azle'; import { PerfResult } from '../../perf_result'; const Reaction = Variant({ Bad: Null, Good: Null, ThumbsUp: nat32, - Tip: principal + Tip: Principal }); let variant_init_heap_storage: { [key: string]: typeof Reaction | undefined } = diff --git a/examples/principal/test/tests.ts b/examples/principal/test/tests.ts index 7247848d89..d08fa10edf 100644 --- a/examples/principal/test/tests.ts +++ b/examples/principal/test/tests.ts @@ -68,7 +68,7 @@ export function getTests(principalCanister: ActorSubclass<_SERVICE>): Test[] { ]; } -function getFromHexTests(principalCanister: ActorSubclass): Test[] { +function getFromHexTests(principalCanister: ActorSubclass<_SERVICE>): Test[] { return [ { name: 'principalFromHex aaaaa-aa', @@ -167,7 +167,7 @@ function getFromHexTests(principalCanister: ActorSubclass): Test[] { ]; } -function getFromTextTests(principalCanister: ActorSubclass): Test[] { +function getFromTextTests(principalCanister: ActorSubclass<_SERVICE>): Test[] { return [ { name: 'principalFromText aaaaa-aa', @@ -266,7 +266,7 @@ function getFromTextTests(principalCanister: ActorSubclass): Test[] { ]; } -function getFromBlobTests(principalCanister: ActorSubclass): Test[] { +function getFromBlobTests(principalCanister: ActorSubclass<_SERVICE>): Test[] { return [ { name: 'principalFromBlob aaaaa-aa', @@ -365,16 +365,15 @@ function getFromBlobTests(principalCanister: ActorSubclass): Test[] { ]; } -function getToHexTests(principalCanister: ActorSubclass): Test[] { +function getToHexTests(principalCanister: ActorSubclass<_SERVICE>): Test[] { return [ { name: 'principalToHex aaaaa-aa', test: async () => { const principal = Principal.fromText('aaaaa-aa'); - const result = await principalCanister.principalToHex( - principal - ); + const result = + await principalCanister.principalToHex(principal); return { Ok: result === principal.toHex() @@ -388,9 +387,8 @@ function getToHexTests(principalCanister: ActorSubclass): Test[] { 'rrkah-fqaaa-aaaaa-aaaaq-cai' ); - const result = await principalCanister.principalToHex( - principal - ); + const result = + await principalCanister.principalToHex(principal); return { Ok: result === principal.toHex() @@ -404,9 +402,8 @@ function getToHexTests(principalCanister: ActorSubclass): Test[] { 'ryjl3-tyaaa-aaaaa-aaaba-cai' ); - const result = await principalCanister.principalToHex( - principal - ); + const result = + await principalCanister.principalToHex(principal); return { Ok: result === principal.toHex() @@ -420,9 +417,8 @@ function getToHexTests(principalCanister: ActorSubclass): Test[] { 'jiyou-fiaaa-aaaam-aad6q-cai' ); - const result = await principalCanister.principalToHex( - principal - ); + const result = + await principalCanister.principalToHex(principal); return { Ok: result === principal.toHex() @@ -436,9 +432,8 @@ function getToHexTests(principalCanister: ActorSubclass): Test[] { 'jqklt-hiaaa-aaaam-aaeba-cai' ); - const result = await principalCanister.principalToHex( - principal - ); + const result = + await principalCanister.principalToHex(principal); return { Ok: result === principal.toHex() @@ -452,9 +447,8 @@ function getToHexTests(principalCanister: ActorSubclass): Test[] { 'qaxqg-4ymay-xutcp-nnull-fvtqf-5p6d4-mxbja-i6t5s-wz7kb-csadv-qqe' ); - const result = await principalCanister.principalToHex( - principal - ); + const result = + await principalCanister.principalToHex(principal); return { Ok: result === principal.toHex() @@ -464,16 +458,15 @@ function getToHexTests(principalCanister: ActorSubclass): Test[] { ]; } -function getToTextTests(principalCanister: ActorSubclass): Test[] { +function getToTextTests(principalCanister: ActorSubclass<_SERVICE>): Test[] { return [ { name: 'principalToText aaaaa-aa', test: async () => { const principal = Principal.fromText('aaaaa-aa'); - const result = await principalCanister.principalToText( - principal - ); + const result = + await principalCanister.principalToText(principal); return { Ok: result === principal.toText() @@ -487,9 +480,8 @@ function getToTextTests(principalCanister: ActorSubclass): Test[] { 'rrkah-fqaaa-aaaaa-aaaaq-cai' ); - const result = await principalCanister.principalToText( - principal - ); + const result = + await principalCanister.principalToText(principal); return { Ok: result === principal.toText() @@ -503,9 +495,8 @@ function getToTextTests(principalCanister: ActorSubclass): Test[] { 'ryjl3-tyaaa-aaaaa-aaaba-cai' ); - const result = await principalCanister.principalToText( - principal - ); + const result = + await principalCanister.principalToText(principal); return { Ok: result === principal.toText() @@ -519,9 +510,8 @@ function getToTextTests(principalCanister: ActorSubclass): Test[] { 'jiyou-fiaaa-aaaam-aad6q-cai' ); - const result = await principalCanister.principalToText( - principal - ); + const result = + await principalCanister.principalToText(principal); return { Ok: result === principal.toText() @@ -535,9 +525,8 @@ function getToTextTests(principalCanister: ActorSubclass): Test[] { 'jqklt-hiaaa-aaaam-aaeba-cai' ); - const result = await principalCanister.principalToText( - principal - ); + const result = + await principalCanister.principalToText(principal); return { Ok: result === principal.toText() @@ -551,9 +540,8 @@ function getToTextTests(principalCanister: ActorSubclass): Test[] { 'qaxqg-4ymay-xutcp-nnull-fvtqf-5p6d4-mxbja-i6t5s-wz7kb-csadv-qqe' ); - const result = await principalCanister.principalToText( - principal - ); + const result = + await principalCanister.principalToText(principal); return { Ok: result === principal.toText() @@ -563,16 +551,15 @@ function getToTextTests(principalCanister: ActorSubclass): Test[] { ]; } -function getToBlobTests(principalCanister: ActorSubclass): Test[] { +function getToBlobTests(principalCanister: ActorSubclass<_SERVICE>): Test[] { return [ { name: 'principalToBlob aaaaa-aa', test: async () => { const principal = Principal.fromText('aaaaa-aa'); - const result = await principalCanister.principalToBlob( - principal - ); + const result = + await principalCanister.principalToBlob(principal); return { Ok: @@ -589,9 +576,8 @@ function getToBlobTests(principalCanister: ActorSubclass): Test[] { 'rrkah-fqaaa-aaaaa-aaaaq-cai' ); - const result = await principalCanister.principalToBlob( - principal - ); + const result = + await principalCanister.principalToBlob(principal); return { Ok: @@ -608,9 +594,8 @@ function getToBlobTests(principalCanister: ActorSubclass): Test[] { 'ryjl3-tyaaa-aaaaa-aaaba-cai' ); - const result = await principalCanister.principalToBlob( - principal - ); + const result = + await principalCanister.principalToBlob(principal); return { Ok: @@ -627,9 +612,8 @@ function getToBlobTests(principalCanister: ActorSubclass): Test[] { 'jiyou-fiaaa-aaaam-aad6q-cai' ); - const result = await principalCanister.principalToBlob( - principal - ); + const result = + await principalCanister.principalToBlob(principal); return { Ok: @@ -646,9 +630,8 @@ function getToBlobTests(principalCanister: ActorSubclass): Test[] { 'jqklt-hiaaa-aaaam-aaeba-cai' ); - const result = await principalCanister.principalToBlob( - principal - ); + const result = + await principalCanister.principalToBlob(principal); return { Ok: @@ -665,9 +648,8 @@ function getToBlobTests(principalCanister: ActorSubclass): Test[] { 'qaxqg-4ymay-xutcp-nnull-fvtqf-5p6d4-mxbja-i6t5s-wz7kb-csadv-qqe' ); - const result = await principalCanister.principalToBlob( - principal - ); + const result = + await principalCanister.principalToBlob(principal); return { Ok: @@ -681,7 +663,7 @@ function getToBlobTests(principalCanister: ActorSubclass): Test[] { } function getSelfAuthenticatingTests( - principalCanister: ActorSubclass + principalCanister: ActorSubclass<_SERVICE> ): Test[] { return [ { diff --git a/examples/query/test/tests.ts b/examples/query/test/tests.ts index cc202e609b..ecb7ca0fee 100644 --- a/examples/query/test/tests.ts +++ b/examples/query/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from '../dfx_generated/query/query.did'; import { ActorSubclass } from '@dfinity/agent'; diff --git a/examples/recursion/test/test.ts b/examples/recursion/test/test.ts index 34e97ee022..8ff2b98d6e 100644 --- a/examples/recursion/test/test.ts +++ b/examples/recursion/test/test.ts @@ -1,5 +1,7 @@ import { getCanisterId, runTests } from 'azle/test'; +// @ts-ignore import { createActor } from './dfx_generated/recursion'; +// @ts-ignore import { createActor as createRecursiveActor } from './dfx_generated/recursive_canister'; import { getRecursiveCanisterTests, getTests } from './tests'; diff --git a/examples/recursion/test/tests.ts b/examples/recursion/test/tests.ts index ee1f2e50a0..fb03522ca3 100644 --- a/examples/recursion/test/tests.ts +++ b/examples/recursion/test/tests.ts @@ -11,7 +11,9 @@ import { rec_4, rec_6, rec_8 + // @ts-ignore } from './dfx_generated/recursion/recursion.did'; +// @ts-ignore import { _SERVICE as _REC_SERVICE } from './dfx_generated/recursive_canister/recursive_canister.did'; import { ActorSubclass } from '@dfinity/agent'; import { Principal } from '@dfinity/principal'; diff --git a/examples/run_time_errors/src/blob.ts b/examples/run_time_errors/src/blob.ts index abd3d44f91..fb534ee9d8 100644 --- a/examples/run_time_errors/src/blob.ts +++ b/examples/run_time_errors/src/blob.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { blob, query, Service } from 'azle'; export function returnNonObjectAsInvalidBlob(): blob { diff --git a/examples/run_time_errors/src/func.ts b/examples/run_time_errors/src/func.ts index f7130ca474..84a518f84a 100644 --- a/examples/run_time_errors/src/func.ts +++ b/examples/run_time_errors/src/func.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { func, Principal, Void } from 'azle'; @func([], Void, 'query') diff --git a/examples/run_time_errors/src/index.ts b/examples/run_time_errors/src/index.ts index 02a1704a1e..57b42da9b3 100644 --- a/examples/run_time_errors/src/index.ts +++ b/examples/run_time_errors/src/index.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import * as _blob from './blob'; import * as _func from './func'; import * as _number from './numbers'; diff --git a/examples/run_time_errors/src/numbers.ts b/examples/run_time_errors/src/numbers.ts index 05dd7f52e1..5137bf2c01 100644 --- a/examples/run_time_errors/src/numbers.ts +++ b/examples/run_time_errors/src/numbers.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { int, int8, diff --git a/examples/run_time_errors/src/opt.ts b/examples/run_time_errors/src/opt.ts index 78340b5459..6490b15818 100644 --- a/examples/run_time_errors/src/opt.ts +++ b/examples/run_time_errors/src/opt.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { Opt } from 'azle'; export function returnNonObject(): Opt { diff --git a/examples/run_time_errors/src/primitives.ts b/examples/run_time_errors/src/primitives.ts index 6727efc904..66700a8410 100644 --- a/examples/run_time_errors/src/primitives.ts +++ b/examples/run_time_errors/src/primitives.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { blob, empty, text } from 'azle'; export function returnInvalidBooleanValue(): boolean { diff --git a/examples/run_time_errors/src/principals.ts b/examples/run_time_errors/src/principals.ts index 7ba7df555f..caba7e9d5c 100644 --- a/examples/run_time_errors/src/principals.ts +++ b/examples/run_time_errors/src/principals.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { Principal } from 'azle'; export function returnStringAsInvalidPrincipal(): Principal { diff --git a/examples/run_time_errors/src/records.ts b/examples/run_time_errors/src/records.ts index 80e368b1b3..74354ad84c 100644 --- a/examples/run_time_errors/src/records.ts +++ b/examples/run_time_errors/src/records.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { bool, Record, Vec, candid, int, text } from 'azle'; export class OtherUserDefinedRecord extends Record { diff --git a/examples/run_time_errors/src/results.ts b/examples/run_time_errors/src/results.ts index 5429b4a58b..e22bacbd0a 100644 --- a/examples/run_time_errors/src/results.ts +++ b/examples/run_time_errors/src/results.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { Result } from 'azle'; export function returnNonObjectAsInvalidResult(): Result { diff --git a/examples/run_time_errors/src/throws.ts b/examples/run_time_errors/src/throws.ts index f1efff4aaa..c043a7cc14 100644 --- a/examples/run_time_errors/src/throws.ts +++ b/examples/run_time_errors/src/throws.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ic } from 'azle'; import RunTimeErrorService from './index'; diff --git a/examples/run_time_errors/src/variants.ts b/examples/run_time_errors/src/variants.ts index 6509a50abf..2648e676f2 100644 --- a/examples/run_time_errors/src/variants.ts +++ b/examples/run_time_errors/src/variants.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { Tuple, Null, Variant, Vec, bool, candid, int, text } from 'azle'; import { UserDefinedRecord } from './records'; diff --git a/examples/run_time_errors/src/vecs.ts b/examples/run_time_errors/src/vecs.ts index 7683b4033c..898f551268 100644 --- a/examples/run_time_errors/src/vecs.ts +++ b/examples/run_time_errors/src/vecs.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { Vec } from 'azle'; export function returnNonObjectAsInvalidVec(): Vec { diff --git a/examples/run_time_errors/test/invalid_blobs.ts b/examples/run_time_errors/test/invalid_blobs.ts index 9ff54c1dee..0116056623 100644 --- a/examples/run_time_errors/test/invalid_blobs.ts +++ b/examples/run_time_errors/test/invalid_blobs.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_funcs.ts b/examples/run_time_errors/test/invalid_funcs.ts index 0bce13b6ee..284dbcf167 100644 --- a/examples/run_time_errors/test/invalid_funcs.ts +++ b/examples/run_time_errors/test/invalid_funcs.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_numbers.ts b/examples/run_time_errors/test/invalid_numbers.ts index a20018e43d..9ffe7f5572 100644 --- a/examples/run_time_errors/test/invalid_numbers.ts +++ b/examples/run_time_errors/test/invalid_numbers.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_opts.ts b/examples/run_time_errors/test/invalid_opts.ts index 044190bf01..2467f8fd90 100644 --- a/examples/run_time_errors/test/invalid_opts.ts +++ b/examples/run_time_errors/test/invalid_opts.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_primitives.ts b/examples/run_time_errors/test/invalid_primitives.ts index 5e885055f8..3ef19af9a0 100644 --- a/examples/run_time_errors/test/invalid_primitives.ts +++ b/examples/run_time_errors/test/invalid_primitives.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_principals.ts b/examples/run_time_errors/test/invalid_principals.ts index af6db43169..488681b460 100644 --- a/examples/run_time_errors/test/invalid_principals.ts +++ b/examples/run_time_errors/test/invalid_principals.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_records.ts b/examples/run_time_errors/test/invalid_records.ts index 234ae16261..60335fd833 100644 --- a/examples/run_time_errors/test/invalid_records.ts +++ b/examples/run_time_errors/test/invalid_records.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_results.ts b/examples/run_time_errors/test/invalid_results.ts index 16c4a20e00..fd06e2aab2 100644 --- a/examples/run_time_errors/test/invalid_results.ts +++ b/examples/run_time_errors/test/invalid_results.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_variants.ts b/examples/run_time_errors/test/invalid_variants.ts index 11460ecf96..c244e5befc 100644 --- a/examples/run_time_errors/test/invalid_variants.ts +++ b/examples/run_time_errors/test/invalid_variants.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/invalid_vecs.ts b/examples/run_time_errors/test/invalid_vecs.ts index 622b66f7bd..251acd1894 100644 --- a/examples/run_time_errors/test/invalid_vecs.ts +++ b/examples/run_time_errors/test/invalid_vecs.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/pretest.ts b/examples/run_time_errors/test/pretest.ts index c7f7a40fca..8d74a4e4e8 100644 --- a/examples/run_time_errors/test/pretest.ts +++ b/examples/run_time_errors/test/pretest.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { execSync } from 'child_process'; async function pretest() { diff --git a/examples/run_time_errors/test/test.ts b/examples/run_time_errors/test/test.ts index c36a5b869a..1005423a03 100644 --- a/examples/run_time_errors/test/test.ts +++ b/examples/run_time_errors/test/test.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { getCanisterId, runTests } from 'azle/test'; import { createActor } from './dfx_generated/run_time_errors'; import { getTests } from './tests'; diff --git a/examples/run_time_errors/test/tests.ts b/examples/run_time_errors/test/tests.ts index bf80abf893..8771460d37 100644 --- a/examples/run_time_errors/test/tests.ts +++ b/examples/run_time_errors/test/tests.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/run_time_errors/test/thrown_errors.ts b/examples/run_time_errors/test/thrown_errors.ts index abd196320c..61d2254448 100644 --- a/examples/run_time_errors/test/thrown_errors.ts +++ b/examples/run_time_errors/test/thrown_errors.ts @@ -1,3 +1,5 @@ +// @ts-nocheck + import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; import { _SERVICE } from './dfx_generated/run_time_errors/run_time_errors.did'; diff --git a/examples/stable_structures/src/canister1/stable_map_0.ts b/examples/stable_structures/src/canister1/stable_map_0.ts index b187de71fc..b203012f5c 100644 --- a/examples/stable_structures/src/canister1/stable_map_0.ts +++ b/examples/stable_structures/src/canister1/stable_map_0.ts @@ -30,7 +30,7 @@ export const stableMap0Methods = { return stableMap0.items(); }), stableMap0Keys: query([], Vec(nat8), () => { - return stableMap0.keys(); + return Uint8Array.from(stableMap0.keys()); }), stableMap0Len: query([], nat64, () => { return stableMap0.len(); diff --git a/examples/stable_structures/src/canister1/stable_map_1.ts b/examples/stable_structures/src/canister1/stable_map_1.ts index a3a32146f5..a46f0772f5 100644 --- a/examples/stable_structures/src/canister1/stable_map_1.ts +++ b/examples/stable_structures/src/canister1/stable_map_1.ts @@ -30,7 +30,7 @@ export const stableMap1Methods = { return stableMap1.items(); }), stableMap1Keys: query([], Vec(nat16), () => { - return stableMap1.keys(); + return Uint16Array.from(stableMap1.keys()); }), stableMap1Len: query([], nat64, () => { return stableMap1.len(); diff --git a/examples/update/test/tests.ts b/examples/update/test/tests.ts index 4d7ef1dad9..e96dc84d08 100644 --- a/examples/update/test/tests.ts +++ b/examples/update/test/tests.ts @@ -1,4 +1,5 @@ import { Test } from 'azle/test'; +// @ts-ignore import { _SERVICE } from '../dfx_generated/update/update.did'; import { ActorSubclass } from '@dfinity/agent'; @@ -7,9 +8,8 @@ export function getTests(updateCanister: ActorSubclass<_SERVICE>): Test[] { { name: 'simpleUpdate', test: async () => { - const result = await updateCanister.simpleUpdate( - 'Why hello there' - ); + const result = + await updateCanister.simpleUpdate('Why hello there'); return { Ok: result === undefined diff --git a/src/compiler/utils/types.ts b/src/compiler/utils/types.ts index 38a856390c..a117c5f1f5 100644 --- a/src/compiler/utils/types.ts +++ b/src/compiler/utils/types.ts @@ -1,4 +1,4 @@ -import { Variant } from '../../lib'; +import { RequireExactlyOne } from '../../lib'; export type AzleError = { error?: string; @@ -61,7 +61,7 @@ export type RunOptions = { export type Rust = string; -export type SpawnSyncError = Variant<{ +export type SpawnSyncError = RequireExactlyOne<{ Error: string; Signal: NodeJS.Signals; Status: number; diff --git a/src/lib/candid/candid_type.ts b/src/lib/candid/candid_type.ts index 67f46d62a2..008f33a774 100644 --- a/src/lib/candid/candid_type.ts +++ b/src/lib/candid/candid_type.ts @@ -1,5 +1,9 @@ import { Serializable } from '../stable_b_tree_map'; -export type CandidType = { +// export type CandidType = { +// _azleCandidType?: '_azleCandidType'; +// } & Partial; + +export interface CandidType { _azleCandidType?: '_azleCandidType'; -} & Partial; +} diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index eed8589aea..5e30186cb7 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -57,6 +57,8 @@ export type TypeMapping = RecursionLevel extends 10 ? float32 : T extends typeof AzleVoid ? void + : T extends AzleVoid + ? void : T extends AzleTuple ? { [K in keyof U]: TypeMapping< @@ -82,10 +84,12 @@ export type TypeMapping = RecursionLevel extends 10 : 10 >; } - : T extends AzleVec - ? Uint8Array : T extends AzleVec - ? TypeMapping[] + ? U extends { _azleKind: 'AzleNat8' } + ? Uint8Array + : U extends { _azleKind: 'AzleNat16' } + ? Uint16Array + : TypeMapping[] : T extends AzleOpt ? Opt> : T extends AzleResult diff --git a/src/lib/candid/types/constructed/blob.ts b/src/lib/candid/types/constructed/blob.ts index dca067a247..bca3464d94 100644 --- a/src/lib/candid/types/constructed/blob.ts +++ b/src/lib/candid/types/constructed/blob.ts @@ -4,7 +4,7 @@ import { decode } from '../../serde/decode'; export class AzleBlob { _azleKind: 'AzleBlob' = 'AzleBlob'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/constructed/record.ts b/src/lib/candid/types/constructed/record.ts index 3f211a5472..36486614b6 100644 --- a/src/lib/candid/types/constructed/record.ts +++ b/src/lib/candid/types/constructed/record.ts @@ -5,6 +5,7 @@ import { TypeMapping } from '../../type_mapping'; import { Parent } from '../../to_idl'; import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; +import { Serializable } from '../../../stable_b_tree_map'; export function Record< T extends { @@ -14,7 +15,8 @@ export function Record< obj: T ): { [K in keyof T]: TypeMapping; -} & CandidType { +} & CandidType & + Partial { return { ...obj, toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/constructed/variant.ts b/src/lib/candid/types/constructed/variant.ts index 885bafbb06..6a3683c84f 100644 --- a/src/lib/candid/types/constructed/variant.ts +++ b/src/lib/candid/types/constructed/variant.ts @@ -4,6 +4,7 @@ import { toIdlMap, CandidMap } from './to_idl_map'; import { IDL } from '@dfinity/candid'; import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; +import { Serializable } from '../../../stable_b_tree_map'; export function Variant< T extends { @@ -14,7 +15,8 @@ export function Variant< ): RequireExactlyOne<{ [K in keyof T]: TypeMapping; }> & - CandidType { + CandidType & + Partial { return { ...obj, toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/primitive/bool.ts b/src/lib/candid/types/primitive/bool.ts index a1f53a5913..addda36be9 100644 --- a/src/lib/candid/types/primitive/bool.ts +++ b/src/lib/candid/types/primitive/bool.ts @@ -4,7 +4,7 @@ import { decode } from '../../serde/decode'; export class AzleBool { _azleKind: 'AzleBool' = 'AzleBool'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/empty.ts b/src/lib/candid/types/primitive/empty.ts index f605a87faa..281caa57b2 100644 --- a/src/lib/candid/types/primitive/empty.ts +++ b/src/lib/candid/types/primitive/empty.ts @@ -4,7 +4,7 @@ import { decode } from '../../serde/decode'; export class AzleEmpty { _azleKind: 'AzleEmpty' = 'AzleEmpty'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/floats/float32.ts b/src/lib/candid/types/primitive/floats/float32.ts index f591f36740..77e2315c5c 100644 --- a/src/lib/candid/types/primitive/floats/float32.ts +++ b/src/lib/candid/types/primitive/floats/float32.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleFloat32 { _azleKind: 'AzleFloat32' = 'AzleFloat32'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/floats/float64.ts b/src/lib/candid/types/primitive/floats/float64.ts index c68a288875..a3d1de1ccd 100644 --- a/src/lib/candid/types/primitive/floats/float64.ts +++ b/src/lib/candid/types/primitive/floats/float64.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleFloat64 { _azleKind: 'AzleFloat64' = 'AzleFloat64'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int.ts b/src/lib/candid/types/primitive/ints/int.ts index b4566080dd..4ed32eaf10 100644 --- a/src/lib/candid/types/primitive/ints/int.ts +++ b/src/lib/candid/types/primitive/ints/int.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleInt { _azleKind: 'AzleInt' = 'AzleInt'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int16.ts b/src/lib/candid/types/primitive/ints/int16.ts index 3403a5d4b3..66257a351f 100644 --- a/src/lib/candid/types/primitive/ints/int16.ts +++ b/src/lib/candid/types/primitive/ints/int16.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleInt16 { _azleKind: 'AzleInt16' = 'AzleInt16'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int32.ts b/src/lib/candid/types/primitive/ints/int32.ts index ebf1fd2abc..c3d9442937 100644 --- a/src/lib/candid/types/primitive/ints/int32.ts +++ b/src/lib/candid/types/primitive/ints/int32.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleInt32 { _azleKind: 'AzleInt32' = 'AzleInt32'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int64.ts b/src/lib/candid/types/primitive/ints/int64.ts index 5cb1b73c8e..9200e6fa9c 100644 --- a/src/lib/candid/types/primitive/ints/int64.ts +++ b/src/lib/candid/types/primitive/ints/int64.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleInt64 { _azleKind: 'AzleInt64' = 'AzleInt64'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/ints/int8.ts b/src/lib/candid/types/primitive/ints/int8.ts index ad82f6250e..8f512e73e1 100644 --- a/src/lib/candid/types/primitive/ints/int8.ts +++ b/src/lib/candid/types/primitive/ints/int8.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleInt8 { _azleKind: 'AzleInt8' = 'AzleInt8'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat.ts b/src/lib/candid/types/primitive/nats/nat.ts index d7ad62a7a2..291dacfaa9 100644 --- a/src/lib/candid/types/primitive/nats/nat.ts +++ b/src/lib/candid/types/primitive/nats/nat.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleNat { _azleKind: 'AzleNat' = 'AzleNat'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat16.ts b/src/lib/candid/types/primitive/nats/nat16.ts index c50b277f1f..cc6b345128 100644 --- a/src/lib/candid/types/primitive/nats/nat16.ts +++ b/src/lib/candid/types/primitive/nats/nat16.ts @@ -3,8 +3,8 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleNat16 { - _azleKind: 'AzleNat16' = 'AzleNat16'; - _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleNat16' = 'AzleNat16'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat32.ts b/src/lib/candid/types/primitive/nats/nat32.ts index 7d5cbc21a9..7a823cad2c 100644 --- a/src/lib/candid/types/primitive/nats/nat32.ts +++ b/src/lib/candid/types/primitive/nats/nat32.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleNat32 { _azleKind: 'AzleNat32' = 'AzleNat32'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat64.ts b/src/lib/candid/types/primitive/nats/nat64.ts index 543c6ae9a8..78e3ceaba0 100644 --- a/src/lib/candid/types/primitive/nats/nat64.ts +++ b/src/lib/candid/types/primitive/nats/nat64.ts @@ -4,7 +4,7 @@ import { decode } from '../../../serde/decode'; export class AzleNat64 { _azleKind: 'AzleNat64' = 'AzleNat64'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/nats/nat8.ts b/src/lib/candid/types/primitive/nats/nat8.ts index 8ca55a383f..fec632b17b 100644 --- a/src/lib/candid/types/primitive/nats/nat8.ts +++ b/src/lib/candid/types/primitive/nats/nat8.ts @@ -3,8 +3,8 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleNat8 { - _azleKind: 'AzleNat8' = 'AzleNat8'; - _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleNat8' = 'AzleNat8'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/null.ts b/src/lib/candid/types/primitive/null.ts index 1d00628369..dceb71f308 100644 --- a/src/lib/candid/types/primitive/null.ts +++ b/src/lib/candid/types/primitive/null.ts @@ -4,7 +4,7 @@ import { decode } from '../../serde/decode'; export class AzleNull { _azleKind: 'AzleNull' = 'AzleNull'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/reserved.ts b/src/lib/candid/types/primitive/reserved.ts index 491d0f5998..b45596fad7 100644 --- a/src/lib/candid/types/primitive/reserved.ts +++ b/src/lib/candid/types/primitive/reserved.ts @@ -4,7 +4,7 @@ import { decode } from '../../serde/decode'; export class AzleReserved { _azleKind: 'AzleReserved' = 'AzleReserved'; - _azleCandidType?: '_azleCandidType'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/text.ts b/src/lib/candid/types/primitive/text.ts index 26649140fa..cf11d912de 100644 --- a/src/lib/candid/types/primitive/text.ts +++ b/src/lib/candid/types/primitive/text.ts @@ -3,8 +3,9 @@ import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; export class AzleText { - _azleKind: 'AzleText' = 'AzleText'; - _azleCandidType?: '_azleCandidType'; + _azleText: 'AzleText' = 'AzleText'; + static _azleText: 'AzleText' = 'AzleText'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/void.ts b/src/lib/candid/types/primitive/void.ts index 37bbf2c5f2..f26f48e292 100644 --- a/src/lib/candid/types/primitive/void.ts +++ b/src/lib/candid/types/primitive/void.ts @@ -2,7 +2,10 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleVoid { + static _azleKind: 'AzleVoid' = 'AzleVoid'; _azleKind: 'AzleVoid' = 'AzleVoid'; + + static _azleCandidType?: '_azleCandidType'; _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/reference/func.ts b/src/lib/candid/types/reference/func.ts index 51605d9c3c..aad4c0fa80 100644 --- a/src/lib/candid/types/reference/func.ts +++ b/src/lib/candid/types/reference/func.ts @@ -3,6 +3,7 @@ import { IDL } from '@dfinity/candid'; import { Principal } from './principal'; import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; +import { Serializable } from '../../../stable_b_tree_map'; type Mode = 'query' | 'update' | 'oneway'; @@ -16,7 +17,7 @@ export function Func( paramCandidTypes: CandidType[], returnCandidTypes: CandidType, mode: Mode -): [Principal, string] & CandidType { +): [Principal, string] & CandidType & Partial { return { toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/reference/service/canister_function/index.ts b/src/lib/candid/types/reference/service/canister_function/index.ts index 93e8e76ec6..496813d741 100644 --- a/src/lib/candid/types/reference/service/canister_function/index.ts +++ b/src/lib/candid/types/reference/service/canister_function/index.ts @@ -133,7 +133,7 @@ function createCanisterFunctionBase( return { ...acc, - [key]: (...args: any[]) => { + [key]: (...args: any) => { return serviceCall( principal as any, key, diff --git a/src/lib/canister_methods/methods/query.ts b/src/lib/canister_methods/methods/query.ts index 8b7b0ffb98..3ac6d1c853 100644 --- a/src/lib/canister_methods/methods/query.ts +++ b/src/lib/canister_methods/methods/query.ts @@ -6,6 +6,18 @@ import { executeMethod } from '../execute_method'; import { isAsync } from '../is_async'; import { MethodArgs } from '../types/method_args'; +// type IfHasKey = K extends keyof T +// ? undefined extends T[K] +// ? Yes +// : No +// : No; + +// type Test1 = IfHasKey; + +// interface CandidType { +// _azleCandidType?: '_azleCandidType'; +// } + export function query< const Params extends ReadonlyArray, Return extends CandidType, diff --git a/src/lib/ic/types/duration.ts b/src/lib/ic/types/duration.ts index 6bf397b6e6..1a6056913b 100644 --- a/src/lib/ic/types/duration.ts +++ b/src/lib/ic/types/duration.ts @@ -4,4 +4,4 @@ import { nat64, AzleNat64 } from '../../candid/types/primitive/nats/nat64'; * Represents a duration of time in seconds. */ export type Duration = nat64; // TODO: Consider modeling this after the corresponding struct in Rust -export const Duration: AzleNat64 = AzleNat64 as any; +export const Duration = AzleNat64; diff --git a/src/lib/ic/types/timer_id.ts b/src/lib/ic/types/timer_id.ts index 5bb9fa7ecc..527477afd3 100644 --- a/src/lib/ic/types/timer_id.ts +++ b/src/lib/ic/types/timer_id.ts @@ -5,4 +5,4 @@ import { nat64, AzleNat64 } from '../../candid/types/primitive/nats/nat64'; * functions. Pass to {@link ic.clearTimer} to remove the timer. */ export type TimerId = nat64; // TODO: Consider modeling this after the corresponding struct in Rust -export const TimerId: AzleNat64 = AzleNat64 as any; +export const TimerId = AzleNat64; diff --git a/tsconfig.json b/tsconfig.json index 2479119139..3da2afadc3 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -7,6 +7,7 @@ "moduleResolution": "node", "allowJs": true, "outDir": "HACK_BECAUSE_OF_ALLOW_JS", - "resolveJsonModule": true + "resolveJsonModule": true, + "esModuleInterop": true } } diff --git a/type_tests/candid/constructed/blob.ts b/type_tests/candid/constructed/blob.ts new file mode 100644 index 0000000000..f491544f8c --- /dev/null +++ b/type_tests/candid/constructed/blob.ts @@ -0,0 +1,4 @@ +import { blob } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; + +export const TestBlob: CandidType = blob; diff --git a/type_tests/candid/constructed/opt.ts b/type_tests/candid/constructed/opt.ts new file mode 100644 index 0000000000..2c155ee23d --- /dev/null +++ b/type_tests/candid/constructed/opt.ts @@ -0,0 +1,4 @@ +import { Opt, text } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; + +export const TestOpt: CandidType = Opt(text); diff --git a/type_tests/candid/constructed/record.ts b/type_tests/candid/constructed/record.ts new file mode 100644 index 0000000000..6c17deaa9e --- /dev/null +++ b/type_tests/candid/constructed/record.ts @@ -0,0 +1,85 @@ +// empty is not present because its type is never which is difficult to test + +import { + bool, + CandidType, + float32, + float64, + int, + int8, + int16, + int32, + int64, + nat, + nat8, + nat16, + nat32, + nat64, + Null, + Record, + reserved, + text, + Void +} from 'azle'; + +export const ExampleRecord = Record({ + bool: bool, + float32: float32, + float64: float64, + int: int, + int8: int8, + int16: int16, + int32: int32, + int64: int64, + nat: nat, + nat8: nat8, + nat16: nat16, + nat32: nat32, + nat64: nat64, + null: Null, + reserved: reserved, + text: text, + void: Void +}); + +export const TestCandidType: CandidType = ExampleRecord; + +export const TestExampleRecord: { + bool: boolean; + float32: number; + float64: number; + int: bigint; + int8: number; + int16: number; + int32: number; + int64: bigint; + nat: bigint; + nat8: number; + nat16: number; + nat32: number; + nat64: bigint; + null: null; + reserved: any; + text: string; + void: void; +} = ExampleRecord; + +export const ExampleRecordInstance: typeof ExampleRecord = { + bool: true, + float32: 0, + float64: 0, + int: 0n, + int8: 0, + int16: 0, + int32: 0, + int64: 0n, + nat: 0n, + nat8: 0, + nat16: 0, + nat32: 0, + nat64: 0n, + null: null, + reserved: 'anything', + text: '', + void: undefined +}; diff --git a/type_tests/candid/constructed/tuple.ts b/type_tests/candid/constructed/tuple.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/type_tests/candid/constructed/variant.ts b/type_tests/candid/constructed/variant.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/type_tests/candid/constructed/vec.ts b/type_tests/candid/constructed/vec.ts new file mode 100644 index 0000000000..2afb7de8fe --- /dev/null +++ b/type_tests/candid/constructed/vec.ts @@ -0,0 +1,9 @@ +import { nat8, nat16, text, Vec } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; +import { typeMapping } from '../..'; + +// TODO probably test Vec with lots of things like record +export const TestCandidType: CandidType = Vec(text); + +export const TestVecNat8: Uint8Array = typeMapping(Vec(nat8)); +export const TestVecNat16: Uint16Array = typeMapping(Vec(nat16)); diff --git a/type_tests/candid/manual.ts b/type_tests/candid/manual.ts new file mode 100644 index 0000000000..6d1ee6a9f7 --- /dev/null +++ b/type_tests/candid/manual.ts @@ -0,0 +1,7 @@ +import { text, Manual } from 'azle'; +import { CandidType } from '../../src/lib/candid/candid_type'; +import { typeMapping } from '../'; + +export const TestCandidType: CandidType = Manual(text); + +export const TestTypeMapping: void = typeMapping(Manual(text)); diff --git a/type_tests/candid/primitive/bool.ts b/type_tests/candid/primitive/bool.ts new file mode 100644 index 0000000000..8650c07f70 --- /dev/null +++ b/type_tests/candid/primitive/bool.ts @@ -0,0 +1,4 @@ +import { bool } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; + +export const TestBool: CandidType = bool; diff --git a/type_tests/candid/primitive/empty.ts b/type_tests/candid/primitive/empty.ts new file mode 100644 index 0000000000..bf2cce7f4f --- /dev/null +++ b/type_tests/candid/primitive/empty.ts @@ -0,0 +1,4 @@ +import { empty } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; + +export const TestEmpty: CandidType = empty; diff --git a/type_tests/candid/primitive/floats/float32.ts b/type_tests/candid/primitive/floats/float32.ts new file mode 100644 index 0000000000..249ce60b1d --- /dev/null +++ b/type_tests/candid/primitive/floats/float32.ts @@ -0,0 +1,4 @@ +import { float32 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestFloat32: CandidType = float32; diff --git a/type_tests/candid/primitive/floats/float64.ts b/type_tests/candid/primitive/floats/float64.ts new file mode 100644 index 0000000000..da6f109f06 --- /dev/null +++ b/type_tests/candid/primitive/floats/float64.ts @@ -0,0 +1,4 @@ +import { float64 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestFloat64: CandidType = float64; diff --git a/type_tests/candid/primitive/ints/int.ts b/type_tests/candid/primitive/ints/int.ts new file mode 100644 index 0000000000..f3ee98a00c --- /dev/null +++ b/type_tests/candid/primitive/ints/int.ts @@ -0,0 +1,4 @@ +import { int } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestInt: CandidType = int; diff --git a/type_tests/candid/primitive/ints/int16.ts b/type_tests/candid/primitive/ints/int16.ts new file mode 100644 index 0000000000..99d33ed890 --- /dev/null +++ b/type_tests/candid/primitive/ints/int16.ts @@ -0,0 +1,4 @@ +import { int16 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestInt16: CandidType = int16; diff --git a/type_tests/candid/primitive/ints/int32.ts b/type_tests/candid/primitive/ints/int32.ts new file mode 100644 index 0000000000..c8084c26b1 --- /dev/null +++ b/type_tests/candid/primitive/ints/int32.ts @@ -0,0 +1,4 @@ +import { int32 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestInt32: CandidType = int32; diff --git a/type_tests/candid/primitive/ints/int64.ts b/type_tests/candid/primitive/ints/int64.ts new file mode 100644 index 0000000000..ae590b2ca1 --- /dev/null +++ b/type_tests/candid/primitive/ints/int64.ts @@ -0,0 +1,4 @@ +import { int64 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestInt64: CandidType = int64; diff --git a/type_tests/candid/primitive/ints/int8.ts b/type_tests/candid/primitive/ints/int8.ts new file mode 100644 index 0000000000..a7c8c92981 --- /dev/null +++ b/type_tests/candid/primitive/ints/int8.ts @@ -0,0 +1,4 @@ +import { int8 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestInt8: CandidType = int8; diff --git a/type_tests/candid/primitive/nats/nat.ts b/type_tests/candid/primitive/nats/nat.ts new file mode 100644 index 0000000000..9ecfafd17a --- /dev/null +++ b/type_tests/candid/primitive/nats/nat.ts @@ -0,0 +1,4 @@ +import { nat } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestNat: CandidType = nat; diff --git a/type_tests/candid/primitive/nats/nat16.ts b/type_tests/candid/primitive/nats/nat16.ts new file mode 100644 index 0000000000..fce59dae57 --- /dev/null +++ b/type_tests/candid/primitive/nats/nat16.ts @@ -0,0 +1,4 @@ +import { nat16 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestNat16: CandidType = nat16; diff --git a/type_tests/candid/primitive/nats/nat32.ts b/type_tests/candid/primitive/nats/nat32.ts new file mode 100644 index 0000000000..3a91dbd28c --- /dev/null +++ b/type_tests/candid/primitive/nats/nat32.ts @@ -0,0 +1,4 @@ +import { nat32 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestNat32: CandidType = nat32; diff --git a/type_tests/candid/primitive/nats/nat64.ts b/type_tests/candid/primitive/nats/nat64.ts new file mode 100644 index 0000000000..0aaa1b8110 --- /dev/null +++ b/type_tests/candid/primitive/nats/nat64.ts @@ -0,0 +1,4 @@ +import { nat64 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestNat64: CandidType = nat64; diff --git a/type_tests/candid/primitive/nats/nat8.ts b/type_tests/candid/primitive/nats/nat8.ts new file mode 100644 index 0000000000..c774ce536a --- /dev/null +++ b/type_tests/candid/primitive/nats/nat8.ts @@ -0,0 +1,4 @@ +import { nat8 } from 'azle'; +import { CandidType } from '../../../../src/lib/candid/candid_type'; + +export const TestNat8: CandidType = nat8; diff --git a/type_tests/candid/primitive/null.ts b/type_tests/candid/primitive/null.ts new file mode 100644 index 0000000000..67cc9529cd --- /dev/null +++ b/type_tests/candid/primitive/null.ts @@ -0,0 +1,4 @@ +import { Null } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; + +export const TestNull: CandidType = Null; diff --git a/type_tests/candid/primitive/reserved.ts b/type_tests/candid/primitive/reserved.ts new file mode 100644 index 0000000000..00a99f1a6f --- /dev/null +++ b/type_tests/candid/primitive/reserved.ts @@ -0,0 +1,4 @@ +import { reserved } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; + +export const TestReserved: CandidType = reserved; diff --git a/type_tests/candid/primitive/text.ts b/type_tests/candid/primitive/text.ts new file mode 100644 index 0000000000..7a790f8cd8 --- /dev/null +++ b/type_tests/candid/primitive/text.ts @@ -0,0 +1,4 @@ +import { text } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; + +export const TestText: CandidType = text; diff --git a/type_tests/candid/primitive/void.ts b/type_tests/candid/primitive/void.ts new file mode 100644 index 0000000000..85cc789804 --- /dev/null +++ b/type_tests/candid/primitive/void.ts @@ -0,0 +1,7 @@ +import { Void } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; +import { typeMapping } from '../..'; + +export const TestCandidType: CandidType = Void; + +export const TestTypeMapping: void = typeMapping(Void); diff --git a/type_tests/index.ts b/type_tests/index.ts new file mode 100644 index 0000000000..492298eb24 --- /dev/null +++ b/type_tests/index.ts @@ -0,0 +1,72 @@ +import { + nat64, + query, + text, + StableJson, + Record, + Serializable, + Void +} from 'azle'; +import { CandidType } from '../src/lib/candid/candid_type'; +import { expectTypeOf } from 'expect-type'; +import { TypeMapping } from '../src/lib/candid/type_mapping'; + +export function typeMapping(value: T): TypeMapping { + return value as any; +} + +expectTypeOf(0).not.toMatchTypeOf; +expectTypeOf(0n).not.toMatchTypeOf; +expectTypeOf('').not.toMatchTypeOf; + +// expectTypeOf({}).not.toMatchTypeOf; // TODO this is wrong + +expectTypeOf(text).toMatchTypeOf; +expectTypeOf(text).toMatchTypeOf; +expectTypeOf(StableJson).not.toMatchTypeOf; // TODO this is wrong +expectTypeOf(StableJson).toMatchTypeOf; // TODO this is wrong + +const TestText: CandidType = text; + +// @ts-expect-error +const TestStableJson: CandidType = StableJson; + +const ExampleRecord = Record({ + text: text, + nat64: nat64 +}); + +const TestExampleRecord: { + text: string; + nat64: bigint; +} = ExampleRecord; + +const ExampleRecordInstance: typeof ExampleRecord = { + text: '', + nat64: 0n +}; + +query([text], Void); + +// query([StableJson], Void); + +// type HasOptionalField = undefined extends T[K] +// ? true +// : false; + +// type Test = HasOptionalField; + +// type IfHasKey = K extends keyof T +// ? undefined extends T[K] +// ? Yes +// : No +// : No; + +// type Test1 = IfHasKey; + +// type + +// expectTypeOf(TestRecord).toEqualTypeOf<{ +// text: string; +// nat64: bigint; +// }>; diff --git a/type_tests/package-lock.json b/type_tests/package-lock.json new file mode 100644 index 0000000000..b5830bc2a2 --- /dev/null +++ b/type_tests/package-lock.json @@ -0,0 +1,21 @@ +{ + "name": "type_tests", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "devDependencies": { + "expect-type": "^0.17.3" + } + }, + "node_modules/expect-type": { + "version": "0.17.3", + "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-0.17.3.tgz", + "integrity": "sha512-K0ZdZJ97jiAtaOwhEHHz/f0N6Xbj5reRz5g6+5BO7+OvqQ7PMQz0/c8bFSJs1zPotNJL5HJaC6t6lGPEAtGyOw==", + "dev": true, + "engines": { + "node": ">=12.0.0" + } + } + } +} diff --git a/type_tests/package.json b/type_tests/package.json new file mode 100644 index 0000000000..b8844944c3 --- /dev/null +++ b/type_tests/package.json @@ -0,0 +1,5 @@ +{ + "devDependencies": { + "expect-type": "^0.17.3" + } +} From a856229dd68e4ee90c5f85a6545d06927ed1133d Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 13:43:07 -0500 Subject: [PATCH 03/30] add some more test cases --- src/lib/candid/type_mapping.ts | 2 ++ src/lib/candid/types/constructed/blob.ts | 2 ++ src/lib/candid/types/constructed/opt.ts | 2 ++ src/lib/candid/types/constructed/vec.ts | 4 ++++ src/lib/candid/types/primitive/empty.ts | 5 ++++- src/lib/candid/types/primitive/null.ts | 2 ++ src/lib/candid/types/primitive/reserved.ts | 2 ++ src/lib/candid/types/primitive/text.ts | 8 +++++--- src/lib/candid/types/reference/principal.ts | 3 +++ .../service/canister_function/index.ts | 18 ++++++++++-------- src/lib/ic/call.ts | 2 +- src/lib/ic/notify.ts | 7 +------ type_tests/candid/primitive/text.ts | 6 +++++- 13 files changed, 43 insertions(+), 20 deletions(-) diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index 5e30186cb7..160747c958 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -89,6 +89,8 @@ export type TypeMapping = RecursionLevel extends 10 ? Uint8Array : U extends { _azleKind: 'AzleNat16' } ? Uint16Array + : T extends AzleVec // TODO I do not know why we have to do this? + ? TypeMapping[] : TypeMapping[] : T extends AzleOpt ? Opt> diff --git a/src/lib/candid/types/constructed/blob.ts b/src/lib/candid/types/constructed/blob.ts index bca3464d94..a72672a09a 100644 --- a/src/lib/candid/types/constructed/blob.ts +++ b/src/lib/candid/types/constructed/blob.ts @@ -4,6 +4,8 @@ import { decode } from '../../serde/decode'; export class AzleBlob { _azleKind: 'AzleBlob' = 'AzleBlob'; + static _azleKind: 'AzleKind' = 'AzleKind'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index ddf1e44b6a..3dea3fc420 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -36,7 +36,9 @@ export class AzleOpt { innerType: CandidType; _azleCandidType?: '_azleCandidType'; + _azleKind: 'AzleOpt' = 'AzleOpt'; + static _azleKind: 'AzleOpt' = 'AzleOpt'; toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/constructed/vec.ts b/src/lib/candid/types/constructed/vec.ts index 5c9211bab2..d0f97b5fe4 100644 --- a/src/lib/candid/types/constructed/vec.ts +++ b/src/lib/candid/types/constructed/vec.ts @@ -10,8 +10,12 @@ export class AzleVec { } innerType: CandidType; + _azleCandidType?: '_azleCandidType'; + _azleKind: 'AzleVec' = 'AzleVec'; + static _azleKind: 'AzleVec' = 'AzleVec'; + toBytes(data: number): Uint8Array { return encode(this, data); } diff --git a/src/lib/candid/types/primitive/empty.ts b/src/lib/candid/types/primitive/empty.ts index 281caa57b2..52a7909a9c 100644 --- a/src/lib/candid/types/primitive/empty.ts +++ b/src/lib/candid/types/primitive/empty.ts @@ -4,7 +4,10 @@ import { decode } from '../../serde/decode'; export class AzleEmpty { _azleKind: 'AzleEmpty' = 'AzleEmpty'; - static _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleEmpty' = 'AzleEmpty'; + + _azleCandidType?: '_azleCandidType' = '_azleCandidType'; + static _azleCandidType?: '_azleCandidType' = '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/primitive/null.ts b/src/lib/candid/types/primitive/null.ts index dceb71f308..e8990c1639 100644 --- a/src/lib/candid/types/primitive/null.ts +++ b/src/lib/candid/types/primitive/null.ts @@ -4,6 +4,8 @@ import { decode } from '../../serde/decode'; export class AzleNull { _azleKind: 'AzleNull' = 'AzleNull'; + static _azleKind: 'AzleKind' = 'AzleKind'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/primitive/reserved.ts b/src/lib/candid/types/primitive/reserved.ts index b45596fad7..a3464d1342 100644 --- a/src/lib/candid/types/primitive/reserved.ts +++ b/src/lib/candid/types/primitive/reserved.ts @@ -4,6 +4,8 @@ import { decode } from '../../serde/decode'; export class AzleReserved { _azleKind: 'AzleReserved' = 'AzleReserved'; + static _azleKind: 'AzleKind' = 'AzleKind'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/primitive/text.ts b/src/lib/candid/types/primitive/text.ts index cf11d912de..5fb939b6ea 100644 --- a/src/lib/candid/types/primitive/text.ts +++ b/src/lib/candid/types/primitive/text.ts @@ -3,9 +3,11 @@ import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; export class AzleText { - _azleText: 'AzleText' = 'AzleText'; - static _azleText: 'AzleText' = 'AzleText'; - static _azleCandidType?: '_azleCandidType'; + _azleKind: 'AzleText' = 'AzleText'; + static _azleKind: 'AzleText' = 'AzleText'; + + _azleCandidType?: '_azleCandidType' = '_azleCandidType'; + static _azleCandidType?: '_azleCandidType' = '_azleCandidType'; static toBytes(data: number): Uint8Array { return encode(this, data); diff --git a/src/lib/candid/types/reference/principal.ts b/src/lib/candid/types/reference/principal.ts index adeed7db6d..e9d9fe6f87 100644 --- a/src/lib/candid/types/reference/principal.ts +++ b/src/lib/candid/types/reference/principal.ts @@ -4,6 +4,9 @@ import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; export class Principal extends DfinityPrincipal { + _azleKind: 'Principal' = 'Principal'; + static _azleKind: 'Principal' = 'Principal'; + static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/reference/service/canister_function/index.ts b/src/lib/candid/types/reference/service/canister_function/index.ts index 496813d741..cb60b743e0 100644 --- a/src/lib/candid/types/reference/service/canister_function/index.ts +++ b/src/lib/candid/types/reference/service/canister_function/index.ts @@ -133,13 +133,18 @@ function createCanisterFunctionBase( return { ...acc, - [key]: (...args: any) => { + [key]: ( + notify: boolean, + callFunction: CallRawFunction | NotifyRawFunction, + cycles: bigint, + args: any[] + ) => { return serviceCall( principal as any, key, value.paramCandidTypes, value.returnCandidType - )(...args); + )(notify, callFunction, cycles, args); } }; }, @@ -159,15 +164,12 @@ function serviceCall( paramCandidTypes: CandidType[], returnCandidType: CandidType ) { - // This must remain a function and not an arrow function - // in order to set the context (this) correctly - return async function ( - this: any, // TODO in lib_new this was Service, I'm not sure we need this anymore + return async ( notify: boolean, callFunction: CallRawFunction | NotifyRawFunction, cycles: bigint, - ...args: any[] - ) { + args: any[] + ) => { const encodedArgs = encode(paramCandidTypes, args); if (notify) { diff --git a/src/lib/ic/call.ts b/src/lib/ic/call.ts index e097668c33..2149a5829d 100644 --- a/src/lib/ic/call.ts +++ b/src/lib/ic/call.ts @@ -32,7 +32,7 @@ export function call any>( config?.cycles128 ); - return method(false, callFunction, cycles, ...(config?.args ?? [])); + return method(false, callFunction, cycles, config?.args ?? []); } function getCallFunctionAndCycles( diff --git a/src/lib/ic/notify.ts b/src/lib/ic/notify.ts index 9f37f23c29..23f4d03ff1 100644 --- a/src/lib/ic/notify.ts +++ b/src/lib/ic/notify.ts @@ -43,10 +43,5 @@ export function notify any>( return undefined as any; } - return method( - true, - notifyRaw, - config?.cycles ?? 0n, - ...(config?.args ?? []) - ); + return method(true, notifyRaw, config?.cycles ?? 0n, config?.args ?? []); } diff --git a/type_tests/candid/primitive/text.ts b/type_tests/candid/primitive/text.ts index 7a790f8cd8..cdc4a0f693 100644 --- a/type_tests/candid/primitive/text.ts +++ b/type_tests/candid/primitive/text.ts @@ -1,4 +1,8 @@ import { text } from 'azle'; import { CandidType } from '../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../..'; -export const TestText: CandidType = text; +export const TestCandidType: CandidType = text; +export const TestSerializable: Serializable = text; +export const TestTypeMapping: string = typeMapping(text); From 6d4b8b2175d4e15e49dfbdcd57d042f755d1be53 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 14:18:45 -0500 Subject: [PATCH 04/30] adding more type tests, fixing all type errors, adding typechecking as a condition into the CI/CD tests --- .github/workflows/test.yml | 4 ++++ examples/robust_imports/src/azle_coverage/index.ts | 4 ++-- examples/robust_imports/src/type_alias_decls/index.ts | 6 +++--- src/lib/candid/types/reference/principal.ts | 1 - type_tests/candid/constructed/blob.ts | 6 +++++- type_tests/candid/primitive/bool.ts | 6 +++++- type_tests/candid/primitive/empty.ts | 9 ++++++++- type_tests/candid/primitive/floats/float32.ts | 6 +++++- type_tests/candid/primitive/floats/float64.ts | 6 +++++- type_tests/candid/primitive/ints/int.ts | 6 +++++- type_tests/candid/primitive/ints/int16.ts | 6 +++++- type_tests/candid/primitive/ints/int32.ts | 6 +++++- type_tests/candid/primitive/ints/int64.ts | 6 +++++- type_tests/candid/primitive/ints/int8.ts | 6 +++++- type_tests/candid/primitive/nats/nat.ts | 6 +++++- type_tests/candid/primitive/nats/nat16.ts | 6 +++++- type_tests/candid/primitive/nats/nat32.ts | 6 +++++- type_tests/candid/primitive/nats/nat64.ts | 6 +++++- type_tests/candid/primitive/nats/nat8.ts | 6 +++++- type_tests/candid/primitive/null.ts | 6 +++++- type_tests/candid/primitive/reserved.ts | 9 ++++++++- type_tests/candid/primitive/void.ts | 3 ++- type_tests/candid/reference/func.ts | 0 type_tests/candid/reference/principal.ts | 8 ++++++++ type_tests/candid/reference/service.ts | 0 25 files changed, 110 insertions(+), 24 deletions(-) create mode 100644 type_tests/candid/reference/func.ts create mode 100644 type_tests/candid/reference/principal.ts create mode 100644 type_tests/candid/reference/service.ts diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f2ffd04be8..997cc7994a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -215,6 +215,10 @@ jobs: shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: npm link azle + - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} + shell: bash -l {0} + working-directory: ${{ matrix.example_directories }} + run: npm run typecheck - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} diff --git a/examples/robust_imports/src/azle_coverage/index.ts b/examples/robust_imports/src/azle_coverage/index.ts index 522cc2de19..d3572f4054 100644 --- a/examples/robust_imports/src/azle_coverage/index.ts +++ b/examples/robust_imports/src/azle_coverage/index.ts @@ -299,7 +299,7 @@ export const removeRambutanSkins = ugni([], rambutan, () => { const opt = soncoya.get(0); if ('None' in opt) { - ic.trap('soncoya is None'); + return ic.trap('soncoya is None'); } const preparedFruit = opt.Some; @@ -331,7 +331,7 @@ export const pickElderberry = ugni([], elderberry, () => { const opt = soncoya.get(0); if ('None' in opt) { - ic.trap('soncoya is None'); + return ic.trap('soncoya is None'); } const preparedFruit = opt.Some; diff --git a/examples/robust_imports/src/type_alias_decls/index.ts b/examples/robust_imports/src/type_alias_decls/index.ts index 5df9664099..23d8cb2376 100644 --- a/examples/robust_imports/src/type_alias_decls/index.ts +++ b/examples/robust_imports/src/type_alias_decls/index.ts @@ -165,7 +165,7 @@ export const getMyRecord = azle.query([], MyRecordFromAlias, () => { depth: { depth: 3 }, tups: ['Hello', 1.23], description: { ugly: null }, - list: [1, 2, 3, 4, 5, 6, 7] + list: Uint16Array.from([1, 2, 3, 4, 5, 6, 7]) }; }); @@ -176,7 +176,7 @@ export const getMyRecordAlias = azle.query([], MyRecordFromAliasAlias, () => { depth: { depth: 3 }, tups: ['Hello', 1.23], description: { ugly: null }, - list: [1, 2, 3, 4, 5, 6, 7] + list: Uint16Array.from([1, 2, 3, 4, 5, 6, 7]) }; }); @@ -187,7 +187,7 @@ export const getSuperAlias = azle.query([], SuperAlias, () => { depth: { depth: 3 }, tups: ['Hello', 1.23], description: { ugly: null }, - list: [1, 2, 3, 4, 5, 6, 7] + list: Uint16Array.from([1, 2, 3, 4, 5, 6, 7]) }; }); diff --git a/src/lib/candid/types/reference/principal.ts b/src/lib/candid/types/reference/principal.ts index e9d9fe6f87..4ec6841746 100644 --- a/src/lib/candid/types/reference/principal.ts +++ b/src/lib/candid/types/reference/principal.ts @@ -4,7 +4,6 @@ import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; export class Principal extends DfinityPrincipal { - _azleKind: 'Principal' = 'Principal'; static _azleKind: 'Principal' = 'Principal'; static _azleCandidType?: '_azleCandidType'; diff --git a/type_tests/candid/constructed/blob.ts b/type_tests/candid/constructed/blob.ts index f491544f8c..72d4dff631 100644 --- a/type_tests/candid/constructed/blob.ts +++ b/type_tests/candid/constructed/blob.ts @@ -1,4 +1,8 @@ import { blob } from 'azle'; import { CandidType } from '../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../..'; -export const TestBlob: CandidType = blob; +export const TestCandidType: CandidType = blob; +export const TestSerializable: Serializable = blob; +export const TestTypeMapping: Uint8Array = typeMapping(blob); diff --git a/type_tests/candid/primitive/bool.ts b/type_tests/candid/primitive/bool.ts index 8650c07f70..51d4dd520b 100644 --- a/type_tests/candid/primitive/bool.ts +++ b/type_tests/candid/primitive/bool.ts @@ -1,4 +1,8 @@ import { bool } from 'azle'; import { CandidType } from '../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../..'; -export const TestBool: CandidType = bool; +export const TestCandidType: CandidType = bool; +export const TestSerializable: Serializable = bool; +export const TestTypeMapping: boolean = typeMapping(bool); diff --git a/type_tests/candid/primitive/empty.ts b/type_tests/candid/primitive/empty.ts index bf2cce7f4f..0b5d52439d 100644 --- a/type_tests/candid/primitive/empty.ts +++ b/type_tests/candid/primitive/empty.ts @@ -1,4 +1,11 @@ import { empty } from 'azle'; import { CandidType } from '../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../..'; -export const TestEmpty: CandidType = empty; +export const TestCandidType: CandidType = empty; +export const TestSerializable: Serializable = empty; + +type IsNever = [T] extends [never] ? true : false; +const typeMapped = typeMapping(empty); +export const TestTypeMapping: IsNever = true; diff --git a/type_tests/candid/primitive/floats/float32.ts b/type_tests/candid/primitive/floats/float32.ts index 249ce60b1d..685fa2adae 100644 --- a/type_tests/candid/primitive/floats/float32.ts +++ b/type_tests/candid/primitive/floats/float32.ts @@ -1,4 +1,8 @@ import { float32 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestFloat32: CandidType = float32; +export const TestCandidType: CandidType = float32; +export const TestSerializable: Serializable = float32; +export const TestTypeMapping: number = typeMapping(float32); diff --git a/type_tests/candid/primitive/floats/float64.ts b/type_tests/candid/primitive/floats/float64.ts index da6f109f06..676a619155 100644 --- a/type_tests/candid/primitive/floats/float64.ts +++ b/type_tests/candid/primitive/floats/float64.ts @@ -1,4 +1,8 @@ import { float64 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestFloat64: CandidType = float64; +export const TestCandidType: CandidType = float64; +export const TestSerializable: Serializable = float64; +export const TestTypeMapping: number = typeMapping(float64); diff --git a/type_tests/candid/primitive/ints/int.ts b/type_tests/candid/primitive/ints/int.ts index f3ee98a00c..3882b9fe26 100644 --- a/type_tests/candid/primitive/ints/int.ts +++ b/type_tests/candid/primitive/ints/int.ts @@ -1,4 +1,8 @@ import { int } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestInt: CandidType = int; +export const TestCandidType: CandidType = int; +export const TestSerializable: Serializable = int; +export const TestTypeMapping: bigint = typeMapping(int); diff --git a/type_tests/candid/primitive/ints/int16.ts b/type_tests/candid/primitive/ints/int16.ts index 99d33ed890..8637d27943 100644 --- a/type_tests/candid/primitive/ints/int16.ts +++ b/type_tests/candid/primitive/ints/int16.ts @@ -1,4 +1,8 @@ import { int16 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestInt16: CandidType = int16; +export const TestCandidType: CandidType = int16; +export const TestSerializable: Serializable = int16; +export const TestTypeMapping: number = typeMapping(int16); diff --git a/type_tests/candid/primitive/ints/int32.ts b/type_tests/candid/primitive/ints/int32.ts index c8084c26b1..a26052755a 100644 --- a/type_tests/candid/primitive/ints/int32.ts +++ b/type_tests/candid/primitive/ints/int32.ts @@ -1,4 +1,8 @@ import { int32 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestInt32: CandidType = int32; +export const TestCandidType: CandidType = int32; +export const TestSerializable: Serializable = int32; +export const TestTypeMapping: number = typeMapping(int32); diff --git a/type_tests/candid/primitive/ints/int64.ts b/type_tests/candid/primitive/ints/int64.ts index ae590b2ca1..967f74f48c 100644 --- a/type_tests/candid/primitive/ints/int64.ts +++ b/type_tests/candid/primitive/ints/int64.ts @@ -1,4 +1,8 @@ import { int64 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestInt64: CandidType = int64; +export const TestCandidType: CandidType = int64; +export const TestSerializable: Serializable = int64; +export const TestTypeMapping: bigint = typeMapping(int64); diff --git a/type_tests/candid/primitive/ints/int8.ts b/type_tests/candid/primitive/ints/int8.ts index a7c8c92981..53bf999e36 100644 --- a/type_tests/candid/primitive/ints/int8.ts +++ b/type_tests/candid/primitive/ints/int8.ts @@ -1,4 +1,8 @@ import { int8 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestInt8: CandidType = int8; +export const TestCandidType: CandidType = int8; +export const TestSerializable: Serializable = int8; +export const TestTypeMapping: number = typeMapping(int8); diff --git a/type_tests/candid/primitive/nats/nat.ts b/type_tests/candid/primitive/nats/nat.ts index 9ecfafd17a..b0c74c4e6d 100644 --- a/type_tests/candid/primitive/nats/nat.ts +++ b/type_tests/candid/primitive/nats/nat.ts @@ -1,4 +1,8 @@ import { nat } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestNat: CandidType = nat; +export const TestCandidType: CandidType = nat; +export const TestSerializable: Serializable = nat; +export const TestTypeMapping: bigint = typeMapping(nat); diff --git a/type_tests/candid/primitive/nats/nat16.ts b/type_tests/candid/primitive/nats/nat16.ts index fce59dae57..222d236aa4 100644 --- a/type_tests/candid/primitive/nats/nat16.ts +++ b/type_tests/candid/primitive/nats/nat16.ts @@ -1,4 +1,8 @@ import { nat16 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestNat16: CandidType = nat16; +export const TestCandidType: CandidType = nat16; +export const TestSerializable: Serializable = nat16; +export const TestTypeMapping: number = typeMapping(nat16); diff --git a/type_tests/candid/primitive/nats/nat32.ts b/type_tests/candid/primitive/nats/nat32.ts index 3a91dbd28c..c304eea057 100644 --- a/type_tests/candid/primitive/nats/nat32.ts +++ b/type_tests/candid/primitive/nats/nat32.ts @@ -1,4 +1,8 @@ import { nat32 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestNat32: CandidType = nat32; +export const TestCandidType: CandidType = nat32; +export const TestSerializable: Serializable = nat32; +export const TestTypeMapping: number = typeMapping(nat32); diff --git a/type_tests/candid/primitive/nats/nat64.ts b/type_tests/candid/primitive/nats/nat64.ts index 0aaa1b8110..c86868c4a1 100644 --- a/type_tests/candid/primitive/nats/nat64.ts +++ b/type_tests/candid/primitive/nats/nat64.ts @@ -1,4 +1,8 @@ import { nat64 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestNat64: CandidType = nat64; +export const TestCandidType: CandidType = nat64; +export const TestSerializable: Serializable = nat64; +export const TestTypeMapping: bigint = typeMapping(nat64); diff --git a/type_tests/candid/primitive/nats/nat8.ts b/type_tests/candid/primitive/nats/nat8.ts index c774ce536a..d1dee94fbe 100644 --- a/type_tests/candid/primitive/nats/nat8.ts +++ b/type_tests/candid/primitive/nats/nat8.ts @@ -1,4 +1,8 @@ import { nat8 } from 'azle'; import { CandidType } from '../../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../../..'; -export const TestNat8: CandidType = nat8; +export const TestCandidType: CandidType = nat8; +export const TestSerializable: Serializable = nat8; +export const TestTypeMapping: number = typeMapping(nat8); diff --git a/type_tests/candid/primitive/null.ts b/type_tests/candid/primitive/null.ts index 67cc9529cd..3ab1c8675f 100644 --- a/type_tests/candid/primitive/null.ts +++ b/type_tests/candid/primitive/null.ts @@ -1,4 +1,8 @@ import { Null } from 'azle'; import { CandidType } from '../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../..'; -export const TestNull: CandidType = Null; +export const TestCandidType: CandidType = Null; +export const TestSerializable: Serializable = Null; +export const TestTypeMapping: null = typeMapping(Null); diff --git a/type_tests/candid/primitive/reserved.ts b/type_tests/candid/primitive/reserved.ts index 00a99f1a6f..c45355990b 100644 --- a/type_tests/candid/primitive/reserved.ts +++ b/type_tests/candid/primitive/reserved.ts @@ -1,4 +1,11 @@ import { reserved } from 'azle'; import { CandidType } from '../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../..'; -export const TestReserved: CandidType = reserved; +export const TestCandidType: CandidType = reserved; +export const TestSerializable: Serializable = reserved; + +type IsAny = 0 extends 1 & T ? true : false; +const typeMapped = typeMapping(reserved); +export const TestTypeMapping: IsAny = true; diff --git a/type_tests/candid/primitive/void.ts b/type_tests/candid/primitive/void.ts index 85cc789804..d51eeca6b7 100644 --- a/type_tests/candid/primitive/void.ts +++ b/type_tests/candid/primitive/void.ts @@ -1,7 +1,8 @@ import { Void } from 'azle'; import { CandidType } from '../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; export const TestCandidType: CandidType = Void; - +export const TestSerializable: Serializable = Void; export const TestTypeMapping: void = typeMapping(Void); diff --git a/type_tests/candid/reference/func.ts b/type_tests/candid/reference/func.ts new file mode 100644 index 0000000000..e69de29bb2 diff --git a/type_tests/candid/reference/principal.ts b/type_tests/candid/reference/principal.ts new file mode 100644 index 0000000000..1cd7f96b18 --- /dev/null +++ b/type_tests/candid/reference/principal.ts @@ -0,0 +1,8 @@ +import { Principal } from 'azle'; +import { CandidType } from '../../../src/lib/candid/candid_type'; +import { Serializable } from '../../../src/lib/stable_b_tree_map'; +import { typeMapping } from '../..'; + +export const TestCandidType: CandidType = Principal; +export const TestSerializable: Serializable = Principal; +export const TestTypeMapping: Principal = typeMapping(Principal); diff --git a/type_tests/candid/reference/service.ts b/type_tests/candid/reference/service.ts new file mode 100644 index 0000000000..e69de29bb2 From a9f7279974877b232e70646cd99ec06aeeaa14e4 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 14:23:48 -0500 Subject: [PATCH 05/30] fix typechecking --- .github/workflows/test.yml | 1 - 1 file changed, 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 997cc7994a..051a80f91c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -217,7 +217,6 @@ jobs: run: npm link azle - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} - working-directory: ${{ matrix.example_directories }} run: npm run typecheck - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} From f75a9ddb1f7a2339d004e6d05502923eee685e58 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 14:40:16 -0500 Subject: [PATCH 06/30] try typechecking just from the directory --- .github/workflows/test.yml | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 051a80f91c..64303de81a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -217,7 +217,8 @@ jobs: run: npm link azle - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} - run: npm run typecheck + working-directory: ${{ matrix.example_directories }} + run: npx tsc --noEmit - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} From 2b85454063131927cb07fba850d7176667e06510 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 14:45:20 -0500 Subject: [PATCH 07/30] run the type checking after running the tests fully --- .github/workflows/test.yml | 8 ++++---- src/lib/index.ts | 1 + 2 files changed, 5 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 64303de81a..13f4c0b2c6 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -215,10 +215,6 @@ jobs: shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: npm link azle - - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} - shell: bash -l {0} - working-directory: ${{ matrix.example_directories }} - run: npx tsc --noEmit - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} @@ -231,6 +227,10 @@ jobs: shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: AZLE_NUM_PROPTEST_RUNS=100 npm test + - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} + shell: bash -l {0} + working-directory: ${{ matrix.example_directories }} + run: npx tsc --noEmit check-basic-integration-tests-success: needs: basic-integration-tests diff --git a/src/lib/index.ts b/src/lib/index.ts index 8dad6d07e7..96c0973c63 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -1,4 +1,5 @@ import './globals'; +import '../../type_tests'; export * from './candid'; export * from './canister_methods'; export * from './ic'; From 283ca202e68213d03e814f1a970f2972ab05acac Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 14:53:58 -0500 Subject: [PATCH 08/30] use the global tsc --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 13f4c0b2c6..92f497b830 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -230,7 +230,7 @@ jobs: - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} - run: npx tsc --noEmit + run: $GITHUB_WORKSPACE/node_modules/.bin/tsc --noEmit check-basic-integration-tests-success: needs: basic-integration-tests From 584a81de62fddde25a7c24edbdf08fb2650b614c Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 15:01:12 -0500 Subject: [PATCH 09/30] type tests now import azle relatively, not with a bare specifier --- type_tests/candid/constructed/blob.ts | 2 +- type_tests/candid/constructed/opt.ts | 2 +- type_tests/candid/constructed/record.ts | 2 +- type_tests/candid/constructed/vec.ts | 2 +- type_tests/candid/manual.ts | 2 +- type_tests/candid/primitive/bool.ts | 2 +- type_tests/candid/primitive/empty.ts | 2 +- type_tests/candid/primitive/floats/float32.ts | 2 +- type_tests/candid/primitive/floats/float64.ts | 2 +- type_tests/candid/primitive/ints/int.ts | 2 +- type_tests/candid/primitive/ints/int16.ts | 2 +- type_tests/candid/primitive/ints/int32.ts | 2 +- type_tests/candid/primitive/ints/int64.ts | 2 +- type_tests/candid/primitive/ints/int8.ts | 2 +- type_tests/candid/primitive/nats/nat.ts | 2 +- type_tests/candid/primitive/nats/nat16.ts | 2 +- type_tests/candid/primitive/nats/nat32.ts | 2 +- type_tests/candid/primitive/nats/nat64.ts | 2 +- type_tests/candid/primitive/nats/nat8.ts | 2 +- type_tests/candid/primitive/null.ts | 2 +- type_tests/candid/primitive/reserved.ts | 2 +- type_tests/candid/primitive/text.ts | 2 +- type_tests/candid/primitive/void.ts | 2 +- type_tests/candid/reference/principal.ts | 2 +- type_tests/index.ts | 2 +- 25 files changed, 25 insertions(+), 25 deletions(-) diff --git a/type_tests/candid/constructed/blob.ts b/type_tests/candid/constructed/blob.ts index 72d4dff631..8a1ddab4b3 100644 --- a/type_tests/candid/constructed/blob.ts +++ b/type_tests/candid/constructed/blob.ts @@ -1,4 +1,4 @@ -import { blob } from 'azle'; +import { blob } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; diff --git a/type_tests/candid/constructed/opt.ts b/type_tests/candid/constructed/opt.ts index 2c155ee23d..ff7062b55d 100644 --- a/type_tests/candid/constructed/opt.ts +++ b/type_tests/candid/constructed/opt.ts @@ -1,4 +1,4 @@ -import { Opt, text } from 'azle'; +import { Opt, text } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; export const TestOpt: CandidType = Opt(text); diff --git a/type_tests/candid/constructed/record.ts b/type_tests/candid/constructed/record.ts index 6c17deaa9e..fc67a02f0b 100644 --- a/type_tests/candid/constructed/record.ts +++ b/type_tests/candid/constructed/record.ts @@ -20,7 +20,7 @@ import { reserved, text, Void -} from 'azle'; +} from '../../../src/lib'; export const ExampleRecord = Record({ bool: bool, diff --git a/type_tests/candid/constructed/vec.ts b/type_tests/candid/constructed/vec.ts index 2afb7de8fe..e7f939c916 100644 --- a/type_tests/candid/constructed/vec.ts +++ b/type_tests/candid/constructed/vec.ts @@ -1,4 +1,4 @@ -import { nat8, nat16, text, Vec } from 'azle'; +import { nat8, nat16, text, Vec } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { typeMapping } from '../..'; diff --git a/type_tests/candid/manual.ts b/type_tests/candid/manual.ts index 6d1ee6a9f7..5f747bb2d2 100644 --- a/type_tests/candid/manual.ts +++ b/type_tests/candid/manual.ts @@ -1,4 +1,4 @@ -import { text, Manual } from 'azle'; +import { text, Manual } from '../../src/lib'; import { CandidType } from '../../src/lib/candid/candid_type'; import { typeMapping } from '../'; diff --git a/type_tests/candid/primitive/bool.ts b/type_tests/candid/primitive/bool.ts index 51d4dd520b..f49e0518ce 100644 --- a/type_tests/candid/primitive/bool.ts +++ b/type_tests/candid/primitive/bool.ts @@ -1,4 +1,4 @@ -import { bool } from 'azle'; +import { bool } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; diff --git a/type_tests/candid/primitive/empty.ts b/type_tests/candid/primitive/empty.ts index 0b5d52439d..ac216eb760 100644 --- a/type_tests/candid/primitive/empty.ts +++ b/type_tests/candid/primitive/empty.ts @@ -1,4 +1,4 @@ -import { empty } from 'azle'; +import { empty } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; diff --git a/type_tests/candid/primitive/floats/float32.ts b/type_tests/candid/primitive/floats/float32.ts index 685fa2adae..41fb565a90 100644 --- a/type_tests/candid/primitive/floats/float32.ts +++ b/type_tests/candid/primitive/floats/float32.ts @@ -1,4 +1,4 @@ -import { float32 } from 'azle'; +import { float32 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/floats/float64.ts b/type_tests/candid/primitive/floats/float64.ts index 676a619155..6fb58b9411 100644 --- a/type_tests/candid/primitive/floats/float64.ts +++ b/type_tests/candid/primitive/floats/float64.ts @@ -1,4 +1,4 @@ -import { float64 } from 'azle'; +import { float64 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/ints/int.ts b/type_tests/candid/primitive/ints/int.ts index 3882b9fe26..58847a8848 100644 --- a/type_tests/candid/primitive/ints/int.ts +++ b/type_tests/candid/primitive/ints/int.ts @@ -1,4 +1,4 @@ -import { int } from 'azle'; +import { int } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/ints/int16.ts b/type_tests/candid/primitive/ints/int16.ts index 8637d27943..68d9d37648 100644 --- a/type_tests/candid/primitive/ints/int16.ts +++ b/type_tests/candid/primitive/ints/int16.ts @@ -1,4 +1,4 @@ -import { int16 } from 'azle'; +import { int16 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/ints/int32.ts b/type_tests/candid/primitive/ints/int32.ts index a26052755a..6d5ae1d4c5 100644 --- a/type_tests/candid/primitive/ints/int32.ts +++ b/type_tests/candid/primitive/ints/int32.ts @@ -1,4 +1,4 @@ -import { int32 } from 'azle'; +import { int32 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/ints/int64.ts b/type_tests/candid/primitive/ints/int64.ts index 967f74f48c..3083a16fed 100644 --- a/type_tests/candid/primitive/ints/int64.ts +++ b/type_tests/candid/primitive/ints/int64.ts @@ -1,4 +1,4 @@ -import { int64 } from 'azle'; +import { int64 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/ints/int8.ts b/type_tests/candid/primitive/ints/int8.ts index 53bf999e36..3ec32eefa4 100644 --- a/type_tests/candid/primitive/ints/int8.ts +++ b/type_tests/candid/primitive/ints/int8.ts @@ -1,4 +1,4 @@ -import { int8 } from 'azle'; +import { int8 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/nats/nat.ts b/type_tests/candid/primitive/nats/nat.ts index b0c74c4e6d..8a795958fd 100644 --- a/type_tests/candid/primitive/nats/nat.ts +++ b/type_tests/candid/primitive/nats/nat.ts @@ -1,4 +1,4 @@ -import { nat } from 'azle'; +import { nat } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/nats/nat16.ts b/type_tests/candid/primitive/nats/nat16.ts index 222d236aa4..432c1f31b7 100644 --- a/type_tests/candid/primitive/nats/nat16.ts +++ b/type_tests/candid/primitive/nats/nat16.ts @@ -1,4 +1,4 @@ -import { nat16 } from 'azle'; +import { nat16 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/nats/nat32.ts b/type_tests/candid/primitive/nats/nat32.ts index c304eea057..978be27e4a 100644 --- a/type_tests/candid/primitive/nats/nat32.ts +++ b/type_tests/candid/primitive/nats/nat32.ts @@ -1,4 +1,4 @@ -import { nat32 } from 'azle'; +import { nat32 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/nats/nat64.ts b/type_tests/candid/primitive/nats/nat64.ts index c86868c4a1..3e50514de0 100644 --- a/type_tests/candid/primitive/nats/nat64.ts +++ b/type_tests/candid/primitive/nats/nat64.ts @@ -1,4 +1,4 @@ -import { nat64 } from 'azle'; +import { nat64 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/nats/nat8.ts b/type_tests/candid/primitive/nats/nat8.ts index d1dee94fbe..5f4eb74dd5 100644 --- a/type_tests/candid/primitive/nats/nat8.ts +++ b/type_tests/candid/primitive/nats/nat8.ts @@ -1,4 +1,4 @@ -import { nat8 } from 'azle'; +import { nat8 } from '../../../../src/lib'; import { CandidType } from '../../../../src/lib/candid/candid_type'; import { Serializable } from '../../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../../..'; diff --git a/type_tests/candid/primitive/null.ts b/type_tests/candid/primitive/null.ts index 3ab1c8675f..2549d715fe 100644 --- a/type_tests/candid/primitive/null.ts +++ b/type_tests/candid/primitive/null.ts @@ -1,4 +1,4 @@ -import { Null } from 'azle'; +import { Null } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; diff --git a/type_tests/candid/primitive/reserved.ts b/type_tests/candid/primitive/reserved.ts index c45355990b..995348fee0 100644 --- a/type_tests/candid/primitive/reserved.ts +++ b/type_tests/candid/primitive/reserved.ts @@ -1,4 +1,4 @@ -import { reserved } from 'azle'; +import { reserved } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; diff --git a/type_tests/candid/primitive/text.ts b/type_tests/candid/primitive/text.ts index cdc4a0f693..94f846ce8c 100644 --- a/type_tests/candid/primitive/text.ts +++ b/type_tests/candid/primitive/text.ts @@ -1,4 +1,4 @@ -import { text } from 'azle'; +import { text } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; diff --git a/type_tests/candid/primitive/void.ts b/type_tests/candid/primitive/void.ts index d51eeca6b7..c09f0d24fa 100644 --- a/type_tests/candid/primitive/void.ts +++ b/type_tests/candid/primitive/void.ts @@ -1,4 +1,4 @@ -import { Void } from 'azle'; +import { Void } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; diff --git a/type_tests/candid/reference/principal.ts b/type_tests/candid/reference/principal.ts index 1cd7f96b18..c26ec70c88 100644 --- a/type_tests/candid/reference/principal.ts +++ b/type_tests/candid/reference/principal.ts @@ -1,4 +1,4 @@ -import { Principal } from 'azle'; +import { Principal } from '../../../src/lib'; import { CandidType } from '../../../src/lib/candid/candid_type'; import { Serializable } from '../../../src/lib/stable_b_tree_map'; import { typeMapping } from '../..'; diff --git a/type_tests/index.ts b/type_tests/index.ts index 492298eb24..7c8632e2db 100644 --- a/type_tests/index.ts +++ b/type_tests/index.ts @@ -6,7 +6,7 @@ import { Record, Serializable, Void -} from 'azle'; +} from '../src/lib'; import { CandidType } from '../src/lib/candid/candid_type'; import { expectTypeOf } from 'expect-type'; import { TypeMapping } from '../src/lib/candid/type_mapping'; From 95608e3930a7a8ed7d70c7c29c362ec1b3d61196 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 15:08:13 -0500 Subject: [PATCH 10/30] remove expect-type of --- type_tests/index.ts | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/type_tests/index.ts b/type_tests/index.ts index 7c8632e2db..8fe4eea8d2 100644 --- a/type_tests/index.ts +++ b/type_tests/index.ts @@ -8,23 +8,23 @@ import { Void } from '../src/lib'; import { CandidType } from '../src/lib/candid/candid_type'; -import { expectTypeOf } from 'expect-type'; +// import { expectTypeOf } from 'expect-type'; import { TypeMapping } from '../src/lib/candid/type_mapping'; export function typeMapping(value: T): TypeMapping { return value as any; } -expectTypeOf(0).not.toMatchTypeOf; -expectTypeOf(0n).not.toMatchTypeOf; -expectTypeOf('').not.toMatchTypeOf; +// expectTypeOf(0).not.toMatchTypeOf; +// expectTypeOf(0n).not.toMatchTypeOf; +// expectTypeOf('').not.toMatchTypeOf; // expectTypeOf({}).not.toMatchTypeOf; // TODO this is wrong -expectTypeOf(text).toMatchTypeOf; -expectTypeOf(text).toMatchTypeOf; -expectTypeOf(StableJson).not.toMatchTypeOf; // TODO this is wrong -expectTypeOf(StableJson).toMatchTypeOf; // TODO this is wrong +// expectTypeOf(text).toMatchTypeOf; +// expectTypeOf(text).toMatchTypeOf; +// expectTypeOf(StableJson).not.toMatchTypeOf; // TODO this is wrong +// expectTypeOf(StableJson).toMatchTypeOf; // TODO this is wrong const TestText: CandidType = text; From edcee295f6b29025296115c6dd101ab28fad5430 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 15:50:20 -0500 Subject: [PATCH 11/30] skip the lib check --- .github/workflows/test.yml | 2 +- examples/audio_recorder/test/tests.ts | 10 +++------- 2 files changed, 4 insertions(+), 8 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 92f497b830..1123b25d49 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -230,7 +230,7 @@ jobs: - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} - run: $GITHUB_WORKSPACE/node_modules/.bin/tsc --noEmit + run: $GITHUB_WORKSPACE/node_modules/.bin/tsc --noEmit --skipLibCheck # TODO can we just add all of the tsconfig.json stuff here? check-basic-integration-tests-success: needs: basic-integration-tests diff --git a/examples/audio_recorder/test/tests.ts b/examples/audio_recorder/test/tests.ts index 3e2cef553f..36d15a935c 100644 --- a/examples/audio_recorder/test/tests.ts +++ b/examples/audio_recorder/test/tests.ts @@ -1,15 +1,11 @@ import { ok, Test } from 'azle/test'; -import { - rec_2, - rec_7, - _SERVICE -} from './dfx_generated/audio_recorder/audio_recorder.did'; +import { _SERVICE } from './dfx_generated/audio_recorder/audio_recorder.did'; import { ActorSubclass } from '@dfinity/agent'; // TODO to be more thorough we could test all of the error cases as well -let global_user: rec_2; -let global_recording: rec_7; +let global_user: any; +let global_recording: any; export function get_tests( audio_recorder_canister: ActorSubclass<_SERVICE> From edb75c9bd3a96b572b8a49556aac43262265fccf Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 16:01:50 -0500 Subject: [PATCH 12/30] fix a few things, change tsc check to have options --- .github/workflows/test.yml | 2 +- .../src/declarations/phone_book/phone_book.did | 6 ++---- .../declarations/phone_book/phone_book.did.d.ts | 12 ++---------- .../src/declarations/phone_book/phone_book.did.js | 14 ++++++++++---- .../phone-book/src/phone_book/index.did | 6 ++---- examples/motoko_examples/phone-book/test/tests.ts | 11 +++-------- examples/timers/test/tests.ts | 4 ++-- 7 files changed, 22 insertions(+), 33 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 1123b25d49..b95657cb23 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -230,7 +230,7 @@ jobs: - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} - run: $GITHUB_WORKSPACE/node_modules/.bin/tsc --noEmit --skipLibCheck # TODO can we just add all of the tsconfig.json stuff here? + run: $GITHUB_WORKSPACE/node_modules/.bin/tsc --noEmit --skipLibCheck --target es2020 --strict --moduleResolution node --allowJs check-basic-integration-tests-success: needs: basic-integration-tests diff --git a/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did b/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did index 816f0918ae..2cf23ca350 100644 --- a/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did +++ b/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did @@ -1,6 +1,4 @@ -type rec_0 = record {desc:text; phone:text}; -type rec_1 = record {desc:text; phone:text}; service: () -> { - insert: (text, rec_0) -> (); - lookup: (text) -> (opt rec_1) query; + insert: (text, record {desc:text; phone:text}) -> (); + lookup: (text) -> (opt record {desc:text; phone:text}) query; } diff --git a/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did.d.ts b/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did.d.ts index 93e9f329d0..3f09710196 100644 --- a/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did.d.ts +++ b/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did.d.ts @@ -1,15 +1,7 @@ import type { Principal } from '@dfinity/principal'; import type { ActorMethod } from '@dfinity/agent'; -export interface rec_0 { - desc: string; - phone: string; -} -export interface rec_1 { - desc: string; - phone: string; -} export interface _SERVICE { - insert: ActorMethod<[string, rec_0], undefined>; - lookup: ActorMethod<[string], [] | [rec_1]>; + insert: ActorMethod<[string, { desc: string; phone: string }], undefined>; + lookup: ActorMethod<[string], [] | [{ desc: string; phone: string }]>; } diff --git a/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did.js b/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did.js index 604f621520..a46f852d13 100644 --- a/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did.js +++ b/examples/motoko_examples/phone-book/src/declarations/phone_book/phone_book.did.js @@ -1,9 +1,15 @@ export const idlFactory = ({ IDL }) => { - const rec_0 = IDL.Record({ desc: IDL.Text, phone: IDL.Text }); - const rec_1 = IDL.Record({ desc: IDL.Text, phone: IDL.Text }); return IDL.Service({ - insert: IDL.Func([IDL.Text, rec_0], [], []), - lookup: IDL.Func([IDL.Text], [IDL.Opt(rec_1)], ['query']) + insert: IDL.Func( + [IDL.Text, IDL.Record({ desc: IDL.Text, phone: IDL.Text })], + [], + [] + ), + lookup: IDL.Func( + [IDL.Text], + [IDL.Opt(IDL.Record({ desc: IDL.Text, phone: IDL.Text }))], + ['query'] + ) }); }; export const init = ({ IDL }) => { diff --git a/examples/motoko_examples/phone-book/src/phone_book/index.did b/examples/motoko_examples/phone-book/src/phone_book/index.did index 816f0918ae..2cf23ca350 100644 --- a/examples/motoko_examples/phone-book/src/phone_book/index.did +++ b/examples/motoko_examples/phone-book/src/phone_book/index.did @@ -1,6 +1,4 @@ -type rec_0 = record {desc:text; phone:text}; -type rec_1 = record {desc:text; phone:text}; service: () -> { - insert: (text, rec_0) -> (); - lookup: (text) -> (opt rec_1) query; + insert: (text, record {desc:text; phone:text}) -> (); + lookup: (text) -> (opt record {desc:text; phone:text}) query; } diff --git a/examples/motoko_examples/phone-book/test/tests.ts b/examples/motoko_examples/phone-book/test/tests.ts index f4e1469687..8a35a60d31 100644 --- a/examples/motoko_examples/phone-book/test/tests.ts +++ b/examples/motoko_examples/phone-book/test/tests.ts @@ -1,11 +1,8 @@ import { Test } from 'azle/test'; -import { - _SERVICE, - rec_0 as Entry -} from '../src/declarations/phone_book/phone_book.did'; +import { _SERVICE } from '../src/declarations/phone_book/phone_book.did'; import { ActorSubclass } from '@dfinity/agent'; -const TEST_PHONE_BOOK_RECORD: Entry = { +const TEST_PHONE_BOOK_RECORD = { desc: 'This is a test record', phone: '555-555-5555' }; @@ -27,9 +24,7 @@ export function getTests(phoneBookCanister: ActorSubclass<_SERVICE>): Test[] { { name: 'look up', test: async () => { - const result: Entry | undefined = ( - await phoneBookCanister.lookup('Test') - )[0]; + const result = (await phoneBookCanister.lookup('Test'))[0]; return { Ok: result !== undefined && diff --git a/examples/timers/test/tests.ts b/examples/timers/test/tests.ts index 50245832f2..eea5ff2139 100644 --- a/examples/timers/test/tests.ts +++ b/examples/timers/test/tests.ts @@ -1,8 +1,8 @@ import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; -import { _SERVICE, rec_0 } from './dfx_generated/timers/timers.did'; +import { _SERVICE } from './dfx_generated/timers/timers.did'; -let timerIds: rec_0 = { +let timerIds = { single: 0n, inline: 0n, capture: 0n, From 0ebf3006ed9a6ec750b000460392e2926c0d4648 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 16:13:43 -0500 Subject: [PATCH 13/30] fix up some types --- examples/manual_reply/src/index.did | 2 +- examples/manual_reply/test/tests.ts | 6 +++--- examples/outgoing_http_requests/test/tests.ts | 12 +++++------- property_tests/tests/query_methods/test/test.ts | 2 +- 4 files changed, 10 insertions(+), 12 deletions(-) diff --git a/examples/manual_reply/src/index.did b/examples/manual_reply/src/index.did index 5d668e09e2..18aebc0cb0 100644 --- a/examples/manual_reply/src/index.did +++ b/examples/manual_reply/src/index.did @@ -11,7 +11,7 @@ service: () -> { queryString: () -> (text) query; queryVariant: () -> (variant {Elemental; Mixed; Toxic}) query; queryVoid: () -> () query; - replyRaw: () -> (record {int:int; bool:bool; text:text; myBlob:vec nat8; myVariant:variant {Large; Small; Medium}}); + replyRaw: () -> (record {"int":int; "bool":bool; "text":text; myBlob:vec nat8; myVariant:variant {Large; Small; Medium}}); updateBlob: () -> (vec nat8); updateFloat32: () -> (float32); updateInt8: () -> (int8); diff --git a/examples/manual_reply/test/tests.ts b/examples/manual_reply/test/tests.ts index 81f2772f4b..38a2377117 100644 --- a/examples/manual_reply/test/tests.ts +++ b/examples/manual_reply/test/tests.ts @@ -1,5 +1,5 @@ import { Test } from 'azle/test'; -import { rec_0, _SERVICE } from './dfx_generated/manual_reply/manual_reply.did'; +import { _SERVICE } from './dfx_generated/manual_reply/manual_reply.did'; import { ActorSubclass } from '@dfinity/agent'; export function getTests(manualReplyCanister: ActorSubclass<_SERVICE>): Test[] { @@ -104,7 +104,7 @@ export function getTests(manualReplyCanister: ActorSubclass<_SERVICE>): Test[] { name: 'update reply with record', test: async () => { const result = await manualReplyCanister.updateRecord(); - const expectedResult: rec_0 = { + const expectedResult = { id: 'b0283eb7-9c0e-41e5-8089-3345e6a8fa6a', orbitals: [ { @@ -254,7 +254,7 @@ export function getTests(manualReplyCanister: ActorSubclass<_SERVICE>): Test[] { name: 'query reply with record', test: async () => { const result = await manualReplyCanister.queryRecord(); - const expectedResult: rec_0 = { + const expectedResult = { id: 'b0283eb7-9c0e-41e5-8089-3345e6a8fa6a', orbitals: [ { diff --git a/examples/outgoing_http_requests/test/tests.ts b/examples/outgoing_http_requests/test/tests.ts index 8b8d92475e..2eb1287b71 100644 --- a/examples/outgoing_http_requests/test/tests.ts +++ b/examples/outgoing_http_requests/test/tests.ts @@ -1,9 +1,7 @@ import { ActorSubclass } from '@dfinity/agent'; import { Test } from 'azle/test'; -import { - HttpResponse, - _SERVICE -} from './dfx_generated/outgoing_http_requests/outgoing_http_requests.did'; +import { _SERVICE } from './dfx_generated/outgoing_http_requests/outgoing_http_requests.did'; +import { HttpResponse } from 'azle/canisters/management'; import decodeUtf8 from 'decode-utf8'; export function getTests( @@ -16,7 +14,7 @@ export function getTests( const result = await outgoingHttpRequestsCanister.xkcd(); return { - Ok: checkXkcdResult(result) + Ok: checkXkcdResult(result as any) }; } }, @@ -26,14 +24,14 @@ export function getTests( const result = await outgoingHttpRequestsCanister.xkcdRaw(); return { - Ok: checkXkcdResult(result) + Ok: checkXkcdResult(result as any) }; } } ]; } -function checkXkcdResult(result: HttpResponse): boolean { +function checkXkcdResult(result: typeof HttpResponse): boolean { const resultJson = JSON.parse(decodeUtf8(Uint8Array.from(result.body))); const expectedJson = JSON.parse( `{"month": "9", "num": 642, "link": "", "year": "2009", "news": "", "safe_title": "Creepy", "alt": "And I even got out my adorable new netbook!", "img": "https://imgs.xkcd.com/comics/creepy.png", "title": "Creepy", "day": "28"}` diff --git a/property_tests/tests/query_methods/test/test.ts b/property_tests/tests/query_methods/test/test.ts index e28dec58eb..66d52a3405 100644 --- a/property_tests/tests/query_methods/test/test.ts +++ b/property_tests/tests/query_methods/test/test.ts @@ -3,7 +3,7 @@ import fc from 'fast-check'; import { writeFileSync } from 'fs'; import { getActor } from '../../../get_actor'; -import { Test, getCanisterId, runTests } from 'azle/test'; +import { Test, getCanisterId, runTests } from '../../../../test'; // TODO Canister // TODO Record From abe930d0b6476c1720772b893bf5a1598dc4aa47 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 16:15:20 -0500 Subject: [PATCH 14/30] update to checkout v4 --- .github/workflows/test.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 4994b87b0d..6dd784fae1 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -35,7 +35,7 @@ jobs: should_run_tests: ${{ steps.should_run_tests.outputs.should_run_tests }} # We only want the next job to run the tests once we have finished deploying to npm and GitHub example_directories: ${{ steps.example_directories.outputs.example_directories }} steps: - - uses: actions/checkout@v2 + - uses: actions/checkout@v4 # if: contains(github.head_ref, 'release--') with: ref: ${{ contains(github.head_ref, 'release--') && github.event.pull_request.head.ref || github.ref }} # This is necessary for this job to be able to commit and push to the origin remote properly @@ -191,7 +191,7 @@ jobs: example_directories: ${{ needs.release-candidate-deploy.outputs.should_run_tests == 'true' && fromJSON(needs.release-candidate-deploy.outputs.example_directories) || fromJSON('["dummy"]') }} steps: - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} - uses: actions/checkout@v2 + uses: actions/checkout@v4 - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} uses: actions/setup-node@v3 with: From 538a3ff73dafb53da2db60b058bb3d54286e3be2 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 16:48:53 -0500 Subject: [PATCH 15/30] add a dummy tsconfig.jsonfile --- examples/vanilla_js/tsconfig.json | 3 +++ 1 file changed, 3 insertions(+) create mode 100644 examples/vanilla_js/tsconfig.json diff --git a/examples/vanilla_js/tsconfig.json b/examples/vanilla_js/tsconfig.json new file mode 100644 index 0000000000..d8ecf5c194 --- /dev/null +++ b/examples/vanilla_js/tsconfig.json @@ -0,0 +1,3 @@ +{ + "dummy": "This is here because of Azle's TypeChecking infrastructure" +} From 1c04f73fa3e84f9c3d52262e03f583d73bb89f39 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 16:56:03 -0500 Subject: [PATCH 16/30] attempt to exclude new everywhere --- .github/workflows/test.yml | 2 +- tsconfig.json | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 6dd784fae1..c17e852814 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -232,7 +232,7 @@ jobs: shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: AZLE_NUM_PROPTEST_RUNS=100 AZLE_PROPTEST_VERBOSE=true npm test - - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests }} + - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && matrix.example_directories != "new" }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: $GITHUB_WORKSPACE/node_modules/.bin/tsc --noEmit --skipLibCheck --target es2020 --strict --moduleResolution node --allowJs diff --git a/tsconfig.json b/tsconfig.json index 3da2afadc3..6baa26230d 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -9,5 +9,6 @@ "outDir": "HACK_BECAUSE_OF_ALLOW_JS", "resolveJsonModule": true, "esModuleInterop": true - } + }, + "exclude": ["examples/new"] } From 62603cc98fdc2502d0cd3582df417ac888d8b44d Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 16:57:06 -0500 Subject: [PATCH 17/30] fix test.yml syntax --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index c17e852814..ec08a6df84 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -232,7 +232,7 @@ jobs: shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: AZLE_NUM_PROPTEST_RUNS=100 AZLE_PROPTEST_VERBOSE=true npm test - - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && matrix.example_directories != "new" }} + - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && matrix.example_directories != 'new' }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: $GITHUB_WORKSPACE/node_modules/.bin/tsc --noEmit --skipLibCheck --target es2020 --strict --moduleResolution node --allowJs From 29ac523d49bfb239ddff3f4d66824a449c8683d3 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 17:08:32 -0500 Subject: [PATCH 18/30] add full tsconfig.json file for vanill.js unfortunately --- examples/vanilla_js/tsconfig.json | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/examples/vanilla_js/tsconfig.json b/examples/vanilla_js/tsconfig.json index d8ecf5c194..2638f0d8bc 100644 --- a/examples/vanilla_js/tsconfig.json +++ b/examples/vanilla_js/tsconfig.json @@ -1,3 +1,9 @@ { - "dummy": "This is here because of Azle's TypeChecking infrastructure" + "compilerOptions": { + "strict": true, + "target": "ES2020", + "moduleResolution": "node", + "allowJs": true, + "outDir": "HACK_BECAUSE_OF_ALLOW_JS" + } } From 84cbf45c1e4b702cbc568bfdc1246325f40764e1 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Thu, 2 Nov 2023 17:40:44 -0500 Subject: [PATCH 19/30] fix new name --- .github/workflows/test.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index ec08a6df84..9ef7a5a4a9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -232,7 +232,7 @@ jobs: shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: AZLE_NUM_PROPTEST_RUNS=100 AZLE_PROPTEST_VERBOSE=true npm test - - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && matrix.example_directories != 'new' }} + - if: ${{ needs.release-candidate-deploy.outputs.should_run_tests && matrix.example_directories != 'examples/new' }} shell: bash -l {0} working-directory: ${{ matrix.example_directories }} run: $GITHUB_WORKSPACE/node_modules/.bin/tsc --noEmit --skipLibCheck --target es2020 --strict --moduleResolution node --allowJs From 61e5539a23afc6220a27e88951f74146ccd226f9 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Tue, 7 Nov 2023 08:10:40 -0600 Subject: [PATCH 20/30] adding in more infrastructure for type tests, primitives done, any not allowed --- examples/list_of_lists/src/index.ts | 14 +-- .../robust_imports/src/azle_coverage/index.ts | 2 +- .../src/import_coverage/index.ts | 2 +- .../src/canister1/stable_map_2.ts | 2 +- src/lib/candid/type_mapping.ts | 13 ++- src/lib/candid/types/constructed/opt.ts | 10 ++- src/lib/candid/types/constructed/tuple.ts | 11 ++- src/lib/candid/types/primitive/ints/int16.ts | 2 +- src/lib/candid/types/primitive/ints/int32.ts | 2 +- src/lib/candid/types/primitive/ints/int8.ts | 2 +- src/lib/candid/types/primitive/nats/nat32.ts | 2 +- type_tests/assert_type.ts | 21 +++++ type_tests/candid/constructed/blob.ts | 19 ++-- type_tests/candid/constructed/opt.ts | 55 +++++++++++- type_tests/candid/constructed/record.ts | 7 +- type_tests/candid/constructed/tuple.ts | 88 +++++++++++++++++++ type_tests/candid/constructed/variant.ts | 80 +++++++++++++++++ type_tests/candid/constructed/vec.ts | 40 +++++++-- type_tests/candid/manual.ts | 13 +-- type_tests/candid/primitive/bool.ts | 19 ++-- type_tests/candid/primitive/empty.ts | 20 +++-- type_tests/candid/primitive/floats/float32.ts | 19 ++-- type_tests/candid/primitive/floats/float64.ts | 19 ++-- type_tests/candid/primitive/ints/int.ts | 19 ++-- type_tests/candid/primitive/ints/int16.ts | 19 ++-- type_tests/candid/primitive/ints/int32.ts | 19 ++-- type_tests/candid/primitive/ints/int64.ts | 19 ++-- type_tests/candid/primitive/ints/int8.ts | 19 ++-- type_tests/candid/primitive/nats/nat.ts | 19 ++-- type_tests/candid/primitive/nats/nat16.ts | 19 ++-- type_tests/candid/primitive/nats/nat32.ts | 19 ++-- type_tests/candid/primitive/nats/nat64.ts | 19 ++-- type_tests/candid/primitive/nats/nat8.ts | 19 ++-- type_tests/candid/primitive/null.ts | 19 ++-- type_tests/candid/primitive/reserved.ts | 18 ++-- type_tests/candid/primitive/text.ts | 19 ++-- type_tests/candid/primitive/void.ts | 19 ++-- type_tests/candid/reference/principal.ts | 19 ++-- type_tests/index.ts | 38 ++++---- 39 files changed, 605 insertions(+), 179 deletions(-) create mode 100644 type_tests/assert_type.ts diff --git a/examples/list_of_lists/src/index.ts b/examples/list_of_lists/src/index.ts index 9fa4f2bc29..c81badb9a4 100644 --- a/examples/list_of_lists/src/index.ts +++ b/examples/list_of_lists/src/index.ts @@ -60,20 +60,20 @@ export default Canister({ [ [ [ - [[1], [2]], + [Int8Array.from([1]), Int8Array.from([2])], [ - [1, 2, 3], - [4, 5, 6] + Int8Array.from([1, 2, 3]), + Int8Array.from([4, 5, 6]) ] ] ], - [[[[1]]], [[[2]]]], - [[[[3]]]] + [[[Int8Array.from([1])]], [[Int8Array.from([2])]]], + [[[Int8Array.from([3])]]] ] ], [ - [[[[[1]]]], [[[[2]]]]], - [[[[[3]]]], [[[[4]]]]] + [[[[Int8Array.from([1])]]], [[[Int8Array.from([2])]]]], + [[[[Int8Array.from([3])]]], [[[Int8Array.from([4])]]]] ] ]; }), diff --git a/examples/robust_imports/src/azle_coverage/index.ts b/examples/robust_imports/src/azle_coverage/index.ts index d3572f4054..27056eff7d 100644 --- a/examples/robust_imports/src/azle_coverage/index.ts +++ b/examples/robust_imports/src/azle_coverage/index.ts @@ -256,7 +256,7 @@ export const putTheCoconutInTheLime = kiwi( [coconut], Lime(coconut), (coconut) => { - return [coconut]; + return Int16Array.from([coconut]); } ); diff --git a/examples/robust_imports/src/import_coverage/index.ts b/examples/robust_imports/src/import_coverage/index.ts index f73c0cc9b6..ac3fd35a51 100644 --- a/examples/robust_imports/src/import_coverage/index.ts +++ b/examples/robust_imports/src/import_coverage/index.ts @@ -92,7 +92,7 @@ export const returnFathomlessVec = icQuery( [], ic.FathomlessVec(ic.azle.int16), () => { - return [1, 2, 3, 4, 5, 6, 7]; + return Int16Array.from([1, 2, 3, 4, 5, 6, 7]); } ); diff --git a/examples/stable_structures/src/canister1/stable_map_2.ts b/examples/stable_structures/src/canister1/stable_map_2.ts index 4641c0080f..31b171b4dc 100644 --- a/examples/stable_structures/src/canister1/stable_map_2.ts +++ b/examples/stable_structures/src/canister1/stable_map_2.ts @@ -30,7 +30,7 @@ export const stableMap2Methods = { return stableMap2.items(); }), stableMap2Keys: query([], Vec(nat32), () => { - return stableMap2.keys(); + return Uint32Array.from(stableMap2.keys()); }), stableMap2Len: query([], nat64, () => { return stableMap2.len(); diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index 160747c958..8bfb3983f7 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -23,6 +23,9 @@ import { AzleNat64, nat64 } from './types/primitive/nats/nat64'; import { AzleResult, Result } from '../system_types'; import { Principal } from './types/reference/principal'; +// TODO I believe we have some unnecessary cases and constructs in here now +// TODO we probably don't need AzleTuple or AzleOpt +// TODO remove and run tests export type TypeMapping = RecursionLevel extends 10 ? T : T extends () => any @@ -89,6 +92,14 @@ export type TypeMapping = RecursionLevel extends 10 ? Uint8Array : U extends { _azleKind: 'AzleNat16' } ? Uint16Array + : U extends { _azleKind: 'AzleNat32' } + ? Uint32Array + : U extends { _azleKind: 'AzleInt8' } + ? Int8Array + : U extends { _azleKind: 'AzleInt16' } + ? Int16Array + : U extends { _azleKind: 'AzleInt32' } + ? Int32Array : T extends AzleVec // TODO I do not know why we have to do this? ? TypeMapping[] : TypeMapping[] @@ -103,7 +114,7 @@ export type TypeMapping = RecursionLevel extends 10 : T extends typeof AzleNull ? Null : T extends typeof AzleReserved - ? reserved + ? any : T extends typeof AzleEmpty ? empty : T; diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index 3dea3fc420..6544b7e6c4 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -1,7 +1,9 @@ +import { Serializable } from '../../../stable_b_tree_map'; import { CandidType } from '../../candid_type'; import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; import { Parent, toIdl } from '../../to_idl'; +import { TypeMapping } from '../../type_mapping'; import { RequireExactlyOne } from './variant'; import { IDL } from '@dfinity/candid'; @@ -24,9 +26,13 @@ export function Some(value: T) { export const None = { None: null }; // TODO what happens if we pass something to Opt() that can't be converted to CandidClass? -export function Opt(t: T): AzleOpt { +export function Opt( + t: T +): RequireExactlyOne<{ Some: TypeMapping; None: null }> & + CandidType & + Partial { // return IDL.Opt(toCandidClass(t)); - return new AzleOpt(t); + return new AzleOpt(t) as any; } export class AzleOpt { diff --git a/src/lib/candid/types/constructed/tuple.ts b/src/lib/candid/types/constructed/tuple.ts index a930e59ee2..3c68a45343 100644 --- a/src/lib/candid/types/constructed/tuple.ts +++ b/src/lib/candid/types/constructed/tuple.ts @@ -3,6 +3,8 @@ import { Parent, toIdl } from '../../to_idl'; import { IDL } from '@dfinity/candid'; import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; +import { TypeMapping } from '../../type_mapping'; +import { Serializable } from '../../../stable_b_tree_map'; export class AzleTuple { constructor(t: CandidType[]) { @@ -28,7 +30,12 @@ export class AzleTuple { } } -export function Tuple(...types: T): AzleTuple { - return new AzleTuple(types); +export function Tuple( + ...types: T +): { + [P in keyof T]: TypeMapping; +} & CandidType & + Partial { + return new AzleTuple(types) as any; } export type Tuple = T; diff --git a/src/lib/candid/types/primitive/ints/int16.ts b/src/lib/candid/types/primitive/ints/int16.ts index 66257a351f..bdd2653fe1 100644 --- a/src/lib/candid/types/primitive/ints/int16.ts +++ b/src/lib/candid/types/primitive/ints/int16.ts @@ -3,7 +3,7 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleInt16 { - _azleKind: 'AzleInt16' = 'AzleInt16'; + static _azleKind: 'AzleInt16' = 'AzleInt16'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/primitive/ints/int32.ts b/src/lib/candid/types/primitive/ints/int32.ts index c3d9442937..13523ea74a 100644 --- a/src/lib/candid/types/primitive/ints/int32.ts +++ b/src/lib/candid/types/primitive/ints/int32.ts @@ -3,7 +3,7 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleInt32 { - _azleKind: 'AzleInt32' = 'AzleInt32'; + static _azleKind: 'AzleInt32' = 'AzleInt32'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/primitive/ints/int8.ts b/src/lib/candid/types/primitive/ints/int8.ts index 8f512e73e1..05e096a682 100644 --- a/src/lib/candid/types/primitive/ints/int8.ts +++ b/src/lib/candid/types/primitive/ints/int8.ts @@ -3,7 +3,7 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleInt8 { - _azleKind: 'AzleInt8' = 'AzleInt8'; + static _azleKind: 'AzleInt8' = 'AzleInt8'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/src/lib/candid/types/primitive/nats/nat32.ts b/src/lib/candid/types/primitive/nats/nat32.ts index 7a823cad2c..048828178c 100644 --- a/src/lib/candid/types/primitive/nats/nat32.ts +++ b/src/lib/candid/types/primitive/nats/nat32.ts @@ -3,7 +3,7 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleNat32 { - _azleKind: 'AzleNat32' = 'AzleNat32'; + static _azleKind: 'AzleNat32' = 'AzleNat32'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: number): Uint8Array { diff --git a/type_tests/assert_type.ts b/type_tests/assert_type.ts new file mode 100644 index 0000000000..089b6c48e7 --- /dev/null +++ b/type_tests/assert_type.ts @@ -0,0 +1,21 @@ +import { CandidType } from '../src/lib/candid/candid_type'; +import { Serializable } from '../src/lib/stable_b_tree_map'; + +export type IsAny = 0 extends 1 & T ? true : false; +export type IsExact = [T] extends [U] + ? [U] extends [T] + ? true + : false + : false; +export type NotAnyAndExact = IsAny extends true + ? false + : IsExact; +export type AssertType = T; + +type IfAny = 0 extends 1 & T ? Y : N; +type NotAny = IfAny; + +export function testCandidType(value: NotAny) {} +export function testSerializable< + T extends Serializable | Partial +>(value: NotAny) {} diff --git a/type_tests/candid/constructed/blob.ts b/type_tests/candid/constructed/blob.ts index 8a1ddab4b3..0b4d394762 100644 --- a/type_tests/candid/constructed/blob.ts +++ b/type_tests/candid/constructed/blob.ts @@ -1,8 +1,15 @@ import { blob } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = blob; -export const TestSerializable: Serializable = blob; -export const TestTypeMapping: Uint8Array = typeMapping(blob); +testCandidType(blob); +testSerializable(blob); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, Uint8Array> +>; diff --git a/type_tests/candid/constructed/opt.ts b/type_tests/candid/constructed/opt.ts index ff7062b55d..9ef54dcfb8 100644 --- a/type_tests/candid/constructed/opt.ts +++ b/type_tests/candid/constructed/opt.ts @@ -1,4 +1,53 @@ -import { Opt, text } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; +// TODO These tests are just for one type, float32 +// TODO it will take a lot of effort (not that much though) to get all types tested with Vec -export const TestOpt: CandidType = Opt(text); +import { float32, Opt, RequireExactlyOne } from '../../../src/lib'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; + +testCandidType(Opt(float32)); +testSerializable(Opt(float32)); + +const testTypeMapping = Opt(float32); +export type TestTypeMapping = AssertType< + NotAnyAndExact< + TypeMapping, + RequireExactlyOne<{ Some: number; None: null }> + > +>; + +testCandidType(Opt(Opt(float32))); +testSerializable(Opt(Opt(float32))); + +const testTypeMappingDouble = Opt(Opt(float32)); +export type TestTypeMappingDouble = AssertType< + NotAnyAndExact< + TypeMapping, + RequireExactlyOne<{ + Some: RequireExactlyOne<{ Some: number; None: null }>; + None: null; + }> + > +>; + +testCandidType(Opt(Opt(Opt(float32)))); +testSerializable(Opt(Opt(Opt(float32)))); + +const testTypeMappingTriple = Opt(Opt(Opt(float32))); +export type TestTypeMappingTriple = AssertType< + NotAnyAndExact< + TypeMapping, + RequireExactlyOne<{ + Some: RequireExactlyOne<{ + Some: RequireExactlyOne<{ Some: number; None: null }>; + None: null; + }>; + None: null; + }> + > +>; diff --git a/type_tests/candid/constructed/record.ts b/type_tests/candid/constructed/record.ts index fc67a02f0b..ee53325193 100644 --- a/type_tests/candid/constructed/record.ts +++ b/type_tests/candid/constructed/record.ts @@ -1,8 +1,9 @@ +// TODO there is much more we could test here + // empty is not present because its type is never which is difficult to test import { bool, - CandidType, float32, float64, int, @@ -21,6 +22,7 @@ import { text, Void } from '../../../src/lib'; +import { testCandidType, testSerializable } from '../../assert_type'; export const ExampleRecord = Record({ bool: bool, @@ -42,7 +44,8 @@ export const ExampleRecord = Record({ void: Void }); -export const TestCandidType: CandidType = ExampleRecord; +testCandidType(ExampleRecord); +testSerializable(ExampleRecord); export const TestExampleRecord: { bool: boolean; diff --git a/type_tests/candid/constructed/tuple.ts b/type_tests/candid/constructed/tuple.ts index e69de29bb2..544c7ac012 100644 --- a/type_tests/candid/constructed/tuple.ts +++ b/type_tests/candid/constructed/tuple.ts @@ -0,0 +1,88 @@ +// TODO there is much more we could test here + +// empty is not present because its type is never which is difficult to test + +import { + bool, + float32, + float64, + int, + int8, + int16, + int32, + int64, + nat, + nat8, + nat16, + nat32, + nat64, + Null, + reserved, + text, + Tuple, + Void +} from '../../../src/lib'; +import { testCandidType, testSerializable } from '../../assert_type'; + +export const ExampleTuple = Tuple( + bool, + float32, + float64, + int, + int8, + int16, + int32, + int64, + nat, + nat8, + nat16, + nat32, + nat64, + Null, + reserved, + text, + Void +); + +testCandidType(ExampleTuple); +testSerializable(ExampleTuple); + +export const TestExampleTuple: [ + boolean, + number, + number, + bigint, + number, + number, + number, + bigint, + bigint, + number, + number, + number, + bigint, + null, + any, + string, + void +] = ExampleTuple; + +export const ExampleTupleInstance: typeof ExampleTuple = [ + true, + 0, + 0, + 0n, + 0, + 0, + 0, + 0n, + 0n, + 0, + 0, + 0, + 0n, + null, + 'anything', + '', + undefined +]; diff --git a/type_tests/candid/constructed/variant.ts b/type_tests/candid/constructed/variant.ts index e69de29bb2..a9adab9d12 100644 --- a/type_tests/candid/constructed/variant.ts +++ b/type_tests/candid/constructed/variant.ts @@ -0,0 +1,80 @@ +// TODO there is much more we could test here + +// empty is not present because its type is never which is difficult to test + +import { + float32, + float64, + int8, + int16, + int32, + nat, + nat8, + nat16, + nat64, + Null, + reserved, + text, + Variant, + RequireExactlyOne +} from '../../../src/lib'; +import { testCandidType, testSerializable } from '../../assert_type'; + +export const ExampleVariant = Variant({ + bool: Null, + float32: float32, + float64: float64, + int: Null, + int8: int8, + int16: int16, + int32: int32, + int64: Null, + nat: nat, + nat8: nat8, + nat16: nat16, + nat32: Null, + nat64: nat64, + null: Null, + reserved: reserved, + text: text, + void: Null +}); + +testCandidType(ExampleVariant); +testSerializable(ExampleVariant); + +export const TestExampleVariant: RequireExactlyOne<{ + bool: null; + float32: number; + float64: number; + int: null; + int8: number; + int16: number; + int32: number; + int64: null; + nat: bigint; + nat8: number; + nat16: number; + nat32: null; + nat64: bigint; + null: null; + reserved: any; + text: string; + void: null; +}> = ExampleVariant; + +export const ExampleVariantInstance0: typeof ExampleVariant = { + bool: null +}; + +export const ExampleVariantInstance1: typeof ExampleVariant = { + float32: 0 +}; + +export const ExampleVariantInstance2: typeof ExampleVariant = { + float64: 0 +}; + +export const ExampleVariantInstance3: typeof ExampleVariant = { + int: null +}; diff --git a/type_tests/candid/constructed/vec.ts b/type_tests/candid/constructed/vec.ts index e7f939c916..95ef01f0fd 100644 --- a/type_tests/candid/constructed/vec.ts +++ b/type_tests/candid/constructed/vec.ts @@ -1,9 +1,35 @@ -import { nat8, nat16, text, Vec } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { typeMapping } from '../..'; +// TODO These tests are just for one type, float32 +// TODO it will take a lot of effort (not that much though) to get all types tested with Vec -// TODO probably test Vec with lots of things like record -export const TestCandidType: CandidType = Vec(text); +import { float32, Vec } from '../../../src/lib'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestVecNat8: Uint8Array = typeMapping(Vec(nat8)); -export const TestVecNat16: Uint16Array = typeMapping(Vec(nat16)); +testCandidType(Vec(float32)); +testSerializable(Vec(float32)); + +const testTypeMapping = Vec(float32); +export type TestTypeMapping = AssertType< + NotAnyAndExact, number[]> +>; + +testCandidType(Vec(Vec(float32))); +testSerializable(Vec(Vec(float32))); + +const testTypeMappingDouble = Vec(Vec(float32)); +export type TestTypeMappingDouble = AssertType< + NotAnyAndExact, number[][]> +>; + +testCandidType(Vec(Vec(Vec(float32)))); +testSerializable(Vec(Vec(Vec(float32)))); + +const testTypeMappingTriple = Vec(Vec(Vec(float32))); +export type TestTypeMappingTriple = AssertType< + NotAnyAndExact, number[][][]> +>; diff --git a/type_tests/candid/manual.ts b/type_tests/candid/manual.ts index 5f747bb2d2..2f1b233217 100644 --- a/type_tests/candid/manual.ts +++ b/type_tests/candid/manual.ts @@ -1,7 +1,10 @@ -import { text, Manual } from '../../src/lib'; -import { CandidType } from '../../src/lib/candid/candid_type'; -import { typeMapping } from '../'; +import { Manual, text } from '../../src/lib'; +import { AssertType, NotAnyAndExact, testCandidType } from '../assert_type'; +import { TypeMapping } from '../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = Manual(text); +testCandidType(Manual(text)); -export const TestTypeMapping: void = typeMapping(Manual(text)); +const testTypeMapping = Manual(text); +export type TestTypeMapping = AssertType< + NotAnyAndExact, void> +>; diff --git a/type_tests/candid/primitive/bool.ts b/type_tests/candid/primitive/bool.ts index f49e0518ce..bc998438df 100644 --- a/type_tests/candid/primitive/bool.ts +++ b/type_tests/candid/primitive/bool.ts @@ -1,8 +1,15 @@ import { bool } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = bool; -export const TestSerializable: Serializable = bool; -export const TestTypeMapping: boolean = typeMapping(bool); +testCandidType(bool); +testSerializable(bool); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, boolean> +>; diff --git a/type_tests/candid/primitive/empty.ts b/type_tests/candid/primitive/empty.ts index ac216eb760..2cb007944d 100644 --- a/type_tests/candid/primitive/empty.ts +++ b/type_tests/candid/primitive/empty.ts @@ -1,11 +1,15 @@ import { empty } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = empty; -export const TestSerializable: Serializable = empty; +testCandidType(empty); +testSerializable(empty); -type IsNever = [T] extends [never] ? true : false; -const typeMapped = typeMapping(empty); -export const TestTypeMapping: IsNever = true; +export type TestTypeMapping = AssertType< + NotAnyAndExact, never> +>; diff --git a/type_tests/candid/primitive/floats/float32.ts b/type_tests/candid/primitive/floats/float32.ts index 41fb565a90..162db72246 100644 --- a/type_tests/candid/primitive/floats/float32.ts +++ b/type_tests/candid/primitive/floats/float32.ts @@ -1,8 +1,15 @@ import { float32 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = float32; -export const TestSerializable: Serializable = float32; -export const TestTypeMapping: number = typeMapping(float32); +testCandidType(float32); +testSerializable(float32); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, number> +>; diff --git a/type_tests/candid/primitive/floats/float64.ts b/type_tests/candid/primitive/floats/float64.ts index 6fb58b9411..69f7ff2b9a 100644 --- a/type_tests/candid/primitive/floats/float64.ts +++ b/type_tests/candid/primitive/floats/float64.ts @@ -1,8 +1,15 @@ import { float64 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = float64; -export const TestSerializable: Serializable = float64; -export const TestTypeMapping: number = typeMapping(float64); +testCandidType(float64); +testSerializable(float64); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, number> +>; diff --git a/type_tests/candid/primitive/ints/int.ts b/type_tests/candid/primitive/ints/int.ts index 58847a8848..ae710c9a34 100644 --- a/type_tests/candid/primitive/ints/int.ts +++ b/type_tests/candid/primitive/ints/int.ts @@ -1,8 +1,15 @@ import { int } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = int; -export const TestSerializable: Serializable = int; -export const TestTypeMapping: bigint = typeMapping(int); +testCandidType(int); +testSerializable(int); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, bigint> +>; diff --git a/type_tests/candid/primitive/ints/int16.ts b/type_tests/candid/primitive/ints/int16.ts index 68d9d37648..acb5727c21 100644 --- a/type_tests/candid/primitive/ints/int16.ts +++ b/type_tests/candid/primitive/ints/int16.ts @@ -1,8 +1,15 @@ import { int16 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = int16; -export const TestSerializable: Serializable = int16; -export const TestTypeMapping: number = typeMapping(int16); +testCandidType(int16); +testSerializable(int16); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, number> +>; diff --git a/type_tests/candid/primitive/ints/int32.ts b/type_tests/candid/primitive/ints/int32.ts index 6d5ae1d4c5..fc2a90cf65 100644 --- a/type_tests/candid/primitive/ints/int32.ts +++ b/type_tests/candid/primitive/ints/int32.ts @@ -1,8 +1,15 @@ import { int32 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = int32; -export const TestSerializable: Serializable = int32; -export const TestTypeMapping: number = typeMapping(int32); +testCandidType(int32); +testSerializable(int32); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, number> +>; diff --git a/type_tests/candid/primitive/ints/int64.ts b/type_tests/candid/primitive/ints/int64.ts index 3083a16fed..283aeeba7c 100644 --- a/type_tests/candid/primitive/ints/int64.ts +++ b/type_tests/candid/primitive/ints/int64.ts @@ -1,8 +1,15 @@ import { int64 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = int64; -export const TestSerializable: Serializable = int64; -export const TestTypeMapping: bigint = typeMapping(int64); +testCandidType(int64); +testSerializable(int64); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, bigint> +>; diff --git a/type_tests/candid/primitive/ints/int8.ts b/type_tests/candid/primitive/ints/int8.ts index 3ec32eefa4..2bcbc11145 100644 --- a/type_tests/candid/primitive/ints/int8.ts +++ b/type_tests/candid/primitive/ints/int8.ts @@ -1,8 +1,15 @@ import { int8 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = int8; -export const TestSerializable: Serializable = int8; -export const TestTypeMapping: number = typeMapping(int8); +testCandidType(int8); +testSerializable(int8); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, number> +>; diff --git a/type_tests/candid/primitive/nats/nat.ts b/type_tests/candid/primitive/nats/nat.ts index 8a795958fd..2bba4efee3 100644 --- a/type_tests/candid/primitive/nats/nat.ts +++ b/type_tests/candid/primitive/nats/nat.ts @@ -1,8 +1,15 @@ import { nat } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = nat; -export const TestSerializable: Serializable = nat; -export const TestTypeMapping: bigint = typeMapping(nat); +testCandidType(nat); +testSerializable(nat); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, bigint> +>; diff --git a/type_tests/candid/primitive/nats/nat16.ts b/type_tests/candid/primitive/nats/nat16.ts index 432c1f31b7..d9e5beb719 100644 --- a/type_tests/candid/primitive/nats/nat16.ts +++ b/type_tests/candid/primitive/nats/nat16.ts @@ -1,8 +1,15 @@ import { nat16 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = nat16; -export const TestSerializable: Serializable = nat16; -export const TestTypeMapping: number = typeMapping(nat16); +testCandidType(nat16); +testSerializable(nat16); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, number> +>; diff --git a/type_tests/candid/primitive/nats/nat32.ts b/type_tests/candid/primitive/nats/nat32.ts index 978be27e4a..2eee0d9c78 100644 --- a/type_tests/candid/primitive/nats/nat32.ts +++ b/type_tests/candid/primitive/nats/nat32.ts @@ -1,8 +1,15 @@ import { nat32 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = nat32; -export const TestSerializable: Serializable = nat32; -export const TestTypeMapping: number = typeMapping(nat32); +testCandidType(nat32); +testSerializable(nat32); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, number> +>; diff --git a/type_tests/candid/primitive/nats/nat64.ts b/type_tests/candid/primitive/nats/nat64.ts index 3e50514de0..b7e0e51cc7 100644 --- a/type_tests/candid/primitive/nats/nat64.ts +++ b/type_tests/candid/primitive/nats/nat64.ts @@ -1,8 +1,15 @@ import { nat64 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = nat64; -export const TestSerializable: Serializable = nat64; -export const TestTypeMapping: bigint = typeMapping(nat64); +testCandidType(nat64); +testSerializable(nat64); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, bigint> +>; diff --git a/type_tests/candid/primitive/nats/nat8.ts b/type_tests/candid/primitive/nats/nat8.ts index 5f4eb74dd5..75bd79ff0d 100644 --- a/type_tests/candid/primitive/nats/nat8.ts +++ b/type_tests/candid/primitive/nats/nat8.ts @@ -1,8 +1,15 @@ import { nat8 } from '../../../../src/lib'; -import { CandidType } from '../../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../../assert_type'; +import { TypeMapping } from '../../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = nat8; -export const TestSerializable: Serializable = nat8; -export const TestTypeMapping: number = typeMapping(nat8); +testCandidType(nat8); +testSerializable(nat8); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, number> +>; diff --git a/type_tests/candid/primitive/null.ts b/type_tests/candid/primitive/null.ts index 2549d715fe..97eb9e1bd2 100644 --- a/type_tests/candid/primitive/null.ts +++ b/type_tests/candid/primitive/null.ts @@ -1,8 +1,15 @@ import { Null } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = Null; -export const TestSerializable: Serializable = Null; -export const TestTypeMapping: null = typeMapping(Null); +testCandidType(Null); +testSerializable(Null); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, null> +>; diff --git a/type_tests/candid/primitive/reserved.ts b/type_tests/candid/primitive/reserved.ts index 995348fee0..b90920ae9c 100644 --- a/type_tests/candid/primitive/reserved.ts +++ b/type_tests/candid/primitive/reserved.ts @@ -1,11 +1,13 @@ import { reserved } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../..'; +import { + AssertType, + IsAny, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = reserved; -export const TestSerializable: Serializable = reserved; +testCandidType(reserved); +testSerializable(reserved); -type IsAny = 0 extends 1 & T ? true : false; -const typeMapped = typeMapping(reserved); -export const TestTypeMapping: IsAny = true; +export type TestTypeMapping = AssertType>>; diff --git a/type_tests/candid/primitive/text.ts b/type_tests/candid/primitive/text.ts index 94f846ce8c..0fcfeb96a6 100644 --- a/type_tests/candid/primitive/text.ts +++ b/type_tests/candid/primitive/text.ts @@ -1,8 +1,15 @@ import { text } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = text; -export const TestSerializable: Serializable = text; -export const TestTypeMapping: string = typeMapping(text); +testCandidType(text); +testSerializable(text); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, string> +>; diff --git a/type_tests/candid/primitive/void.ts b/type_tests/candid/primitive/void.ts index c09f0d24fa..3cc483755b 100644 --- a/type_tests/candid/primitive/void.ts +++ b/type_tests/candid/primitive/void.ts @@ -1,8 +1,15 @@ import { Void } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = Void; -export const TestSerializable: Serializable = Void; -export const TestTypeMapping: void = typeMapping(Void); +testCandidType(Void); +testSerializable(Void); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, void> +>; diff --git a/type_tests/candid/reference/principal.ts b/type_tests/candid/reference/principal.ts index c26ec70c88..be66ad3191 100644 --- a/type_tests/candid/reference/principal.ts +++ b/type_tests/candid/reference/principal.ts @@ -1,8 +1,15 @@ import { Principal } from '../../../src/lib'; -import { CandidType } from '../../../src/lib/candid/candid_type'; -import { Serializable } from '../../../src/lib/stable_b_tree_map'; -import { typeMapping } from '../..'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; -export const TestCandidType: CandidType = Principal; -export const TestSerializable: Serializable = Principal; -export const TestTypeMapping: Principal = typeMapping(Principal); +testCandidType(Principal); +testSerializable(Principal); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, Principal> +>; diff --git a/type_tests/index.ts b/type_tests/index.ts index 8fe4eea8d2..a7cdef0556 100644 --- a/type_tests/index.ts +++ b/type_tests/index.ts @@ -11,9 +11,9 @@ import { CandidType } from '../src/lib/candid/candid_type'; // import { expectTypeOf } from 'expect-type'; import { TypeMapping } from '../src/lib/candid/type_mapping'; -export function typeMapping(value: T): TypeMapping { - return value as any; -} +// export function typeMapping(value: T): TypeMapping { +// return value as any; +// } // expectTypeOf(0).not.toMatchTypeOf; // expectTypeOf(0n).not.toMatchTypeOf; @@ -26,27 +26,27 @@ export function typeMapping(value: T): TypeMapping { // expectTypeOf(StableJson).not.toMatchTypeOf; // TODO this is wrong // expectTypeOf(StableJson).toMatchTypeOf; // TODO this is wrong -const TestText: CandidType = text; +// const TestText: CandidType = text; -// @ts-expect-error -const TestStableJson: CandidType = StableJson; +// // @ts-expect-error +// const TestStableJson: CandidType = StableJson; -const ExampleRecord = Record({ - text: text, - nat64: nat64 -}); +// const ExampleRecord = Record({ +// text: text, +// nat64: nat64 +// }); -const TestExampleRecord: { - text: string; - nat64: bigint; -} = ExampleRecord; +// const TestExampleRecord: { +// text: string; +// nat64: bigint; +// } = ExampleRecord; -const ExampleRecordInstance: typeof ExampleRecord = { - text: '', - nat64: 0n -}; +// const ExampleRecordInstance: typeof ExampleRecord = { +// text: '', +// nat64: 0n +// }; -query([text], Void); +// query([text], Void); // query([StableJson], Void); From 18ba0ed69a378be89bab3e50e3f3cef6a3e92a41 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Tue, 7 Nov 2023 08:56:54 -0600 Subject: [PATCH 21/30] add in func, service, init, post_upgrade, query, update basic tests --- type_tests/candid/reference/func.ts | 20 ++++++++ type_tests/candid/reference/service.ts | 24 ++++++++++ type_tests/canister_methods/init.ts | 46 ++++++++++++++++++ type_tests/canister_methods/post_upgrade.ts | 46 ++++++++++++++++++ type_tests/canister_methods/query.ts | 52 +++++++++++++++++++++ type_tests/canister_methods/update.ts | 52 +++++++++++++++++++++ 6 files changed, 240 insertions(+) create mode 100644 type_tests/canister_methods/init.ts create mode 100644 type_tests/canister_methods/post_upgrade.ts create mode 100644 type_tests/canister_methods/query.ts create mode 100644 type_tests/canister_methods/update.ts diff --git a/type_tests/candid/reference/func.ts b/type_tests/candid/reference/func.ts index e69de29bb2..fae5e87bc4 100644 --- a/type_tests/candid/reference/func.ts +++ b/type_tests/candid/reference/func.ts @@ -0,0 +1,20 @@ +// TODO we aren't really testing that Func only accepts CandidType +// TODO we aren't really testing the params and return type + +import { Func, Principal, Void } from '../../../src/lib'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; + +const TestFunc = Func([], Void, 'query'); + +testCandidType(TestFunc); +testSerializable(TestFunc); + +export type TestTypeMapping = AssertType< + NotAnyAndExact, [Principal, string]> +>; diff --git a/type_tests/candid/reference/service.ts b/type_tests/candid/reference/service.ts index e69de29bb2..1a13b2784a 100644 --- a/type_tests/candid/reference/service.ts +++ b/type_tests/candid/reference/service.ts @@ -0,0 +1,24 @@ +// TODO what's going on with the types? +// TODO if you pass in a Canister as a parameter to query or update +// TODO shouldn't it already have its id assigned? +// TODO also if you pass one in as a parameter +// TODO it seems to have an IDL function, even though the type says it shouldn't +// TODO we probably need to rework Service/Canister a bit + +import { Canister, Func, Principal, Void } from '../../../src/lib'; +import { + AssertType, + NotAnyAndExact, + testCandidType, + testSerializable +} from '../../assert_type'; +import { TypeMapping } from '../../../src/lib/candid/type_mapping'; + +const TestCanister = Canister({}); + +testCandidType(TestCanister); +// testSerializable(TestCanister); + +// export type TestTypeMapping = AssertType< +// NotAnyAndExact, [Principal, string]> +// >; diff --git a/type_tests/canister_methods/init.ts b/type_tests/canister_methods/init.ts new file mode 100644 index 0000000000..354f37bce2 --- /dev/null +++ b/type_tests/canister_methods/init.ts @@ -0,0 +1,46 @@ +import { + bool, + init, + nat, + Null, + Record, + RequireExactlyOne, + text, + Variant +} from '../../src/lib'; +import { AssertType, NotAnyAndExact } from '../assert_type'; + +const User = Record({ + id: text +}); + +const Reaction = Variant({ + Happy: Null, + Sad: Null +}); + +init( + [bool, nat, text, User, Reaction], + (param0, param1, param2, param3, param4) => { + type Param0 = AssertType>; + type Param1 = AssertType>; + type Param2 = AssertType>; + type Param3 = AssertType< + NotAnyAndExact< + typeof param3, + { + id: string; + } + > + >; + type Param4 = AssertType< + NotAnyAndExact< + typeof param4, + RequireExactlyOne<{ + Happy: null; + Sad: null; + }> + > + >; + } +); diff --git a/type_tests/canister_methods/post_upgrade.ts b/type_tests/canister_methods/post_upgrade.ts new file mode 100644 index 0000000000..97255c2d7e --- /dev/null +++ b/type_tests/canister_methods/post_upgrade.ts @@ -0,0 +1,46 @@ +import { + bool, + nat, + Null, + postUpgrade, + Record, + RequireExactlyOne, + text, + Variant +} from '../../src/lib'; +import { AssertType, NotAnyAndExact } from '../assert_type'; + +const User = Record({ + id: text +}); + +const Reaction = Variant({ + Happy: Null, + Sad: Null +}); + +postUpgrade( + [bool, nat, text, User, Reaction], + (param0, param1, param2, param3, param4) => { + type Param0 = AssertType>; + type Param1 = AssertType>; + type Param2 = AssertType>; + type Param3 = AssertType< + NotAnyAndExact< + typeof param3, + { + id: string; + } + > + >; + type Param4 = AssertType< + NotAnyAndExact< + typeof param4, + RequireExactlyOne<{ + Happy: null; + Sad: null; + }> + > + >; + } +); diff --git a/type_tests/canister_methods/query.ts b/type_tests/canister_methods/query.ts new file mode 100644 index 0000000000..c5db8a9208 --- /dev/null +++ b/type_tests/canister_methods/query.ts @@ -0,0 +1,52 @@ +// TODO we aren't really testing that the params and return type only accept CandidType +// TODO The return type is not being tested here + +import { + bool, + nat, + Null, + query, + Record, + RequireExactlyOne, + text, + Variant +} from '../../src/lib'; +import { AssertType, NotAnyAndExact } from '../assert_type'; + +const User = Record({ + id: text +}); + +const Reaction = Variant({ + Happy: Null, + Sad: Null +}); + +query( + [bool, nat, text, User, Reaction], + text, + (param0, param1, param2, param3, param4) => { + type Param0 = AssertType>; + type Param1 = AssertType>; + type Param2 = AssertType>; + type Param3 = AssertType< + NotAnyAndExact< + typeof param3, + { + id: string; + } + > + >; + type Param4 = AssertType< + NotAnyAndExact< + typeof param4, + RequireExactlyOne<{ + Happy: null; + Sad: null; + }> + > + >; + + return param2; + } +); diff --git a/type_tests/canister_methods/update.ts b/type_tests/canister_methods/update.ts new file mode 100644 index 0000000000..1cfbd0c6f6 --- /dev/null +++ b/type_tests/canister_methods/update.ts @@ -0,0 +1,52 @@ +// TODO we aren't really testing that the params and return type only accept CandidType +// TODO The return type is not being tested here + +import { + bool, + nat, + Null, + Record, + RequireExactlyOne, + text, + update, + Variant +} from '../../src/lib'; +import { AssertType, NotAnyAndExact } from '../assert_type'; + +const User = Record({ + id: text +}); + +const Reaction = Variant({ + Happy: Null, + Sad: Null +}); + +update( + [bool, nat, text, User, Reaction], + text, + (param0, param1, param2, param3, param4) => { + type Param0 = AssertType>; + type Param1 = AssertType>; + type Param2 = AssertType>; + type Param3 = AssertType< + NotAnyAndExact< + typeof param3, + { + id: string; + } + > + >; + type Param4 = AssertType< + NotAnyAndExact< + typeof param4, + RequireExactlyOne<{ + Happy: null; + Sad: null; + }> + > + >; + + return param2; + } +); From e20e1f05deec4aba0f3894dd83c145aa0e5b744b Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Tue, 7 Nov 2023 09:04:14 -0600 Subject: [PATCH 22/30] remove types from serializable methods --- src/lib/candid/types/constructed/blob.ts | 4 ++-- src/lib/candid/types/constructed/opt.ts | 4 ++-- src/lib/candid/types/constructed/record.ts | 4 ++-- src/lib/candid/types/constructed/tuple.ts | 4 ++-- src/lib/candid/types/constructed/variant.ts | 4 ++-- src/lib/candid/types/constructed/vec.ts | 4 ++-- src/lib/candid/types/primitive/bool.ts | 4 ++-- src/lib/candid/types/primitive/empty.ts | 4 ++-- src/lib/candid/types/primitive/floats/float32.ts | 4 ++-- src/lib/candid/types/primitive/floats/float64.ts | 4 ++-- src/lib/candid/types/primitive/ints/int.ts | 4 ++-- src/lib/candid/types/primitive/ints/int16.ts | 4 ++-- src/lib/candid/types/primitive/ints/int32.ts | 4 ++-- src/lib/candid/types/primitive/ints/int64.ts | 4 ++-- src/lib/candid/types/primitive/ints/int8.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat16.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat32.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat64.ts | 4 ++-- src/lib/candid/types/primitive/nats/nat8.ts | 4 ++-- src/lib/candid/types/primitive/null.ts | 4 ++-- src/lib/candid/types/primitive/reserved.ts | 4 ++-- src/lib/candid/types/primitive/text.ts | 5 ++--- src/lib/candid/types/primitive/void.ts | 4 ++-- src/lib/candid/types/reference/func.ts | 4 ++-- src/lib/candid/types/reference/principal.ts | 4 ++-- src/lib/stable_b_tree_map.ts | 4 ++-- 27 files changed, 54 insertions(+), 55 deletions(-) diff --git a/src/lib/candid/types/constructed/blob.ts b/src/lib/candid/types/constructed/blob.ts index a72672a09a..db3132514e 100644 --- a/src/lib/candid/types/constructed/blob.ts +++ b/src/lib/candid/types/constructed/blob.ts @@ -8,11 +8,11 @@ export class AzleBlob { static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index 6544b7e6c4..7fa9f9c779 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -46,11 +46,11 @@ export class AzleOpt { _azleKind: 'AzleOpt' = 'AzleOpt'; static _azleKind: 'AzleOpt' = 'AzleOpt'; - toBytes(data: number): Uint8Array { + toBytes(data: any) { return encode(this, data); } - fromBytes(bytes: Uint8Array): number { + fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/constructed/record.ts b/src/lib/candid/types/constructed/record.ts index 36486614b6..f8f21b12c2 100644 --- a/src/lib/candid/types/constructed/record.ts +++ b/src/lib/candid/types/constructed/record.ts @@ -19,10 +19,10 @@ export function Record< Partial { return { ...obj, - toBytes(data: number): Uint8Array { + toBytes(data: any) { return encode(this, data); }, - fromBytes(bytes: Uint8Array): number { + fromBytes(bytes: Uint8Array) { return decode(this, bytes); }, getIdl(parents: Parent[]) { diff --git a/src/lib/candid/types/constructed/tuple.ts b/src/lib/candid/types/constructed/tuple.ts index 3c68a45343..58e7376352 100644 --- a/src/lib/candid/types/constructed/tuple.ts +++ b/src/lib/candid/types/constructed/tuple.ts @@ -14,11 +14,11 @@ export class AzleTuple { innerTypes: CandidType[]; _azleCandidType?: '_azleCandidType'; - toBytes(data: number): Uint8Array { + toBytes(data: any) { return encode(this, data); } - fromBytes(bytes: Uint8Array): number { + fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/constructed/variant.ts b/src/lib/candid/types/constructed/variant.ts index 6a3683c84f..cb915a890e 100644 --- a/src/lib/candid/types/constructed/variant.ts +++ b/src/lib/candid/types/constructed/variant.ts @@ -19,10 +19,10 @@ export function Variant< Partial { return { ...obj, - toBytes(data: number): Uint8Array { + toBytes(data: any) { return encode(this, data); }, - fromBytes(bytes: Uint8Array): number { + fromBytes(bytes: Uint8Array) { return decode(this, bytes); }, getIdl(parents: any) { diff --git a/src/lib/candid/types/constructed/vec.ts b/src/lib/candid/types/constructed/vec.ts index d0f97b5fe4..5744aa24f0 100644 --- a/src/lib/candid/types/constructed/vec.ts +++ b/src/lib/candid/types/constructed/vec.ts @@ -16,11 +16,11 @@ export class AzleVec { _azleKind: 'AzleVec' = 'AzleVec'; static _azleKind: 'AzleVec' = 'AzleVec'; - toBytes(data: number): Uint8Array { + toBytes(data: any) { return encode(this, data); } - fromBytes(bytes: Uint8Array): number { + fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/bool.ts b/src/lib/candid/types/primitive/bool.ts index addda36be9..f79ae40829 100644 --- a/src/lib/candid/types/primitive/bool.ts +++ b/src/lib/candid/types/primitive/bool.ts @@ -6,11 +6,11 @@ export class AzleBool { _azleKind: 'AzleBool' = 'AzleBool'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/empty.ts b/src/lib/candid/types/primitive/empty.ts index 52a7909a9c..7a1fabbfec 100644 --- a/src/lib/candid/types/primitive/empty.ts +++ b/src/lib/candid/types/primitive/empty.ts @@ -9,11 +9,11 @@ export class AzleEmpty { _azleCandidType?: '_azleCandidType' = '_azleCandidType'; static _azleCandidType?: '_azleCandidType' = '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/floats/float32.ts b/src/lib/candid/types/primitive/floats/float32.ts index 77e2315c5c..c204019708 100644 --- a/src/lib/candid/types/primitive/floats/float32.ts +++ b/src/lib/candid/types/primitive/floats/float32.ts @@ -6,11 +6,11 @@ export class AzleFloat32 { _azleKind: 'AzleFloat32' = 'AzleFloat32'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/floats/float64.ts b/src/lib/candid/types/primitive/floats/float64.ts index a3d1de1ccd..1c7e90c55c 100644 --- a/src/lib/candid/types/primitive/floats/float64.ts +++ b/src/lib/candid/types/primitive/floats/float64.ts @@ -6,11 +6,11 @@ export class AzleFloat64 { _azleKind: 'AzleFloat64' = 'AzleFloat64'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/ints/int.ts b/src/lib/candid/types/primitive/ints/int.ts index 4ed32eaf10..58a13ef8f2 100644 --- a/src/lib/candid/types/primitive/ints/int.ts +++ b/src/lib/candid/types/primitive/ints/int.ts @@ -6,11 +6,11 @@ export class AzleInt { _azleKind: 'AzleInt' = 'AzleInt'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/ints/int16.ts b/src/lib/candid/types/primitive/ints/int16.ts index bdd2653fe1..f9628bbb92 100644 --- a/src/lib/candid/types/primitive/ints/int16.ts +++ b/src/lib/candid/types/primitive/ints/int16.ts @@ -6,11 +6,11 @@ export class AzleInt16 { static _azleKind: 'AzleInt16' = 'AzleInt16'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/ints/int32.ts b/src/lib/candid/types/primitive/ints/int32.ts index 13523ea74a..7333bb5dea 100644 --- a/src/lib/candid/types/primitive/ints/int32.ts +++ b/src/lib/candid/types/primitive/ints/int32.ts @@ -6,11 +6,11 @@ export class AzleInt32 { static _azleKind: 'AzleInt32' = 'AzleInt32'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/ints/int64.ts b/src/lib/candid/types/primitive/ints/int64.ts index 9200e6fa9c..fb6bc8586f 100644 --- a/src/lib/candid/types/primitive/ints/int64.ts +++ b/src/lib/candid/types/primitive/ints/int64.ts @@ -6,11 +6,11 @@ export class AzleInt64 { _azleKind: 'AzleInt64' = 'AzleInt64'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/ints/int8.ts b/src/lib/candid/types/primitive/ints/int8.ts index 05e096a682..1cdc1e5edb 100644 --- a/src/lib/candid/types/primitive/ints/int8.ts +++ b/src/lib/candid/types/primitive/ints/int8.ts @@ -6,11 +6,11 @@ export class AzleInt8 { static _azleKind: 'AzleInt8' = 'AzleInt8'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/nats/nat.ts b/src/lib/candid/types/primitive/nats/nat.ts index 291dacfaa9..2df07233e3 100644 --- a/src/lib/candid/types/primitive/nats/nat.ts +++ b/src/lib/candid/types/primitive/nats/nat.ts @@ -6,11 +6,11 @@ export class AzleNat { _azleKind: 'AzleNat' = 'AzleNat'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/nats/nat16.ts b/src/lib/candid/types/primitive/nats/nat16.ts index cc6b345128..03494e5547 100644 --- a/src/lib/candid/types/primitive/nats/nat16.ts +++ b/src/lib/candid/types/primitive/nats/nat16.ts @@ -6,11 +6,11 @@ export class AzleNat16 { static _azleKind: 'AzleNat16' = 'AzleNat16'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/nats/nat32.ts b/src/lib/candid/types/primitive/nats/nat32.ts index 048828178c..9ae5f275f1 100644 --- a/src/lib/candid/types/primitive/nats/nat32.ts +++ b/src/lib/candid/types/primitive/nats/nat32.ts @@ -6,11 +6,11 @@ export class AzleNat32 { static _azleKind: 'AzleNat32' = 'AzleNat32'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/nats/nat64.ts b/src/lib/candid/types/primitive/nats/nat64.ts index 78e3ceaba0..c9b7d1adc7 100644 --- a/src/lib/candid/types/primitive/nats/nat64.ts +++ b/src/lib/candid/types/primitive/nats/nat64.ts @@ -6,11 +6,11 @@ export class AzleNat64 { _azleKind: 'AzleNat64' = 'AzleNat64'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/nats/nat8.ts b/src/lib/candid/types/primitive/nats/nat8.ts index fec632b17b..a29609e991 100644 --- a/src/lib/candid/types/primitive/nats/nat8.ts +++ b/src/lib/candid/types/primitive/nats/nat8.ts @@ -6,11 +6,11 @@ export class AzleNat8 { static _azleKind: 'AzleNat8' = 'AzleNat8'; static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/null.ts b/src/lib/candid/types/primitive/null.ts index e8990c1639..f6120aba81 100644 --- a/src/lib/candid/types/primitive/null.ts +++ b/src/lib/candid/types/primitive/null.ts @@ -8,11 +8,11 @@ export class AzleNull { static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/reserved.ts b/src/lib/candid/types/primitive/reserved.ts index a3464d1342..5056dee426 100644 --- a/src/lib/candid/types/primitive/reserved.ts +++ b/src/lib/candid/types/primitive/reserved.ts @@ -8,11 +8,11 @@ export class AzleReserved { static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/text.ts b/src/lib/candid/types/primitive/text.ts index 5fb939b6ea..795d163cd5 100644 --- a/src/lib/candid/types/primitive/text.ts +++ b/src/lib/candid/types/primitive/text.ts @@ -9,12 +9,11 @@ export class AzleText { _azleCandidType?: '_azleCandidType' = '_azleCandidType'; static _azleCandidType?: '_azleCandidType' = '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - // TODO fix all of the return types - static fromBytes(bytes: Uint8Array): string { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/primitive/void.ts b/src/lib/candid/types/primitive/void.ts index f26f48e292..917b95c30d 100644 --- a/src/lib/candid/types/primitive/void.ts +++ b/src/lib/candid/types/primitive/void.ts @@ -8,11 +8,11 @@ export class AzleVoid { static _azleCandidType?: '_azleCandidType'; _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/candid/types/reference/func.ts b/src/lib/candid/types/reference/func.ts index aad4c0fa80..e372b523c3 100644 --- a/src/lib/candid/types/reference/func.ts +++ b/src/lib/candid/types/reference/func.ts @@ -19,10 +19,10 @@ export function Func( mode: Mode ): [Principal, string] & CandidType & Partial { return { - toBytes(data: number): Uint8Array { + toBytes(data: any) { return encode(this, data); }, - fromBytes(bytes: Uint8Array): number { + fromBytes(bytes: Uint8Array) { return decode(this, bytes); }, getIdl(parents: Parent[]) { diff --git a/src/lib/candid/types/reference/principal.ts b/src/lib/candid/types/reference/principal.ts index 4ec6841746..0472a90cfc 100644 --- a/src/lib/candid/types/reference/principal.ts +++ b/src/lib/candid/types/reference/principal.ts @@ -8,11 +8,11 @@ export class Principal extends DfinityPrincipal { static _azleCandidType?: '_azleCandidType'; - static toBytes(data: number): Uint8Array { + static toBytes(data: any) { return encode(this, data); } - static fromBytes(bytes: Uint8Array): number { + static fromBytes(bytes: Uint8Array) { return decode(this, bytes); } diff --git a/src/lib/stable_b_tree_map.ts b/src/lib/stable_b_tree_map.ts index 7335b62d4d..66cdb6c131 100644 --- a/src/lib/stable_b_tree_map.ts +++ b/src/lib/stable_b_tree_map.ts @@ -9,11 +9,11 @@ import { encode, decode } from './candid/serde'; // TODO we probably need to allow the user to pass in their own encoding/decoding for Json as well // TODO we need a way to make the types good in TypeMapping export class StableJson { - static toBytes(data: any): Uint8Array { + static toBytes(data: any) { return Uint8Array.from(Buffer.from(JSON.stringify(data))); } - static fromBytes(bytes: Uint8Array): any { + static fromBytes(bytes: Uint8Array) { return JSON.parse(Buffer.from(bytes).toString()); } } From 4928680c9580b306c1058560eb904298dd8256f0 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Tue, 7 Nov 2023 09:36:53 -0600 Subject: [PATCH 23/30] fix all of the kind and azleCandidTypes in the classes --- src/lib/candid/types/constructed/blob.ts | 3 ++- src/lib/candid/types/constructed/opt.ts | 4 +++- src/lib/candid/types/constructed/tuple.ts | 5 +++++ src/lib/candid/types/constructed/vec.ts | 3 ++- src/lib/candid/types/primitive/bool.ts | 3 +++ src/lib/candid/types/primitive/empty.ts | 4 ++-- src/lib/candid/types/primitive/floats/float32.ts | 3 +++ src/lib/candid/types/primitive/floats/float64.ts | 3 +++ src/lib/candid/types/primitive/ints/int.ts | 3 +++ src/lib/candid/types/primitive/ints/int16.ts | 3 +++ src/lib/candid/types/primitive/ints/int32.ts | 3 +++ src/lib/candid/types/primitive/ints/int64.ts | 3 +++ src/lib/candid/types/primitive/ints/int8.ts | 3 +++ src/lib/candid/types/primitive/nats/nat.ts | 3 +++ src/lib/candid/types/primitive/nats/nat16.ts | 3 +++ src/lib/candid/types/primitive/nats/nat32.ts | 3 +++ src/lib/candid/types/primitive/nats/nat64.ts | 3 +++ src/lib/candid/types/primitive/nats/nat8.ts | 3 +++ src/lib/candid/types/primitive/null.ts | 3 ++- src/lib/candid/types/primitive/reserved.ts | 3 ++- src/lib/candid/types/primitive/text.ts | 4 ++-- src/lib/candid/types/primitive/void.ts | 4 ++-- src/lib/candid/types/reference/principal.ts | 1 - 23 files changed, 61 insertions(+), 12 deletions(-) diff --git a/src/lib/candid/types/constructed/blob.ts b/src/lib/candid/types/constructed/blob.ts index db3132514e..8d12ff7905 100644 --- a/src/lib/candid/types/constructed/blob.ts +++ b/src/lib/candid/types/constructed/blob.ts @@ -4,8 +4,9 @@ import { decode } from '../../serde/decode'; export class AzleBlob { _azleKind: 'AzleBlob' = 'AzleBlob'; - static _azleKind: 'AzleKind' = 'AzleKind'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleBlob' = 'AzleBlob'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index 7fa9f9c779..992a9956e4 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -41,10 +41,12 @@ export class AzleOpt { } innerType: CandidType; - _azleCandidType?: '_azleCandidType'; _azleKind: 'AzleOpt' = 'AzleOpt'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleOpt' = 'AzleOpt'; + static _azleCandidType?: '_azleCandidType'; toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/constructed/tuple.ts b/src/lib/candid/types/constructed/tuple.ts index 58e7376352..1707be56a8 100644 --- a/src/lib/candid/types/constructed/tuple.ts +++ b/src/lib/candid/types/constructed/tuple.ts @@ -12,8 +12,13 @@ export class AzleTuple { } innerTypes: CandidType[]; + + _azleKind: 'AzleTuple' = 'AzleTuple'; _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleTuple' = 'AzleTuple'; + static _azleCandidType?: '_azleCandidType'; + toBytes(data: any) { return encode(this, data); } diff --git a/src/lib/candid/types/constructed/vec.ts b/src/lib/candid/types/constructed/vec.ts index 5744aa24f0..697e887c24 100644 --- a/src/lib/candid/types/constructed/vec.ts +++ b/src/lib/candid/types/constructed/vec.ts @@ -11,10 +11,11 @@ export class AzleVec { innerType: CandidType; + _azleKind: 'AzleVec' = 'AzleVec'; _azleCandidType?: '_azleCandidType'; - _azleKind: 'AzleVec' = 'AzleVec'; static _azleKind: 'AzleVec' = 'AzleVec'; + static _azleCandidType?: '_azleCandidType'; toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/primitive/bool.ts b/src/lib/candid/types/primitive/bool.ts index f79ae40829..43d3884f05 100644 --- a/src/lib/candid/types/primitive/bool.ts +++ b/src/lib/candid/types/primitive/bool.ts @@ -4,6 +4,9 @@ import { decode } from '../../serde/decode'; export class AzleBool { _azleKind: 'AzleBool' = 'AzleBool'; + _azleCandidType?: '_azleCandidType'; + + static _azleKind: 'AzleBool' = 'AzleBool'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/empty.ts b/src/lib/candid/types/primitive/empty.ts index 7a1fabbfec..1aab1af3a3 100644 --- a/src/lib/candid/types/primitive/empty.ts +++ b/src/lib/candid/types/primitive/empty.ts @@ -4,9 +4,9 @@ import { decode } from '../../serde/decode'; export class AzleEmpty { _azleKind: 'AzleEmpty' = 'AzleEmpty'; - static _azleKind: 'AzleEmpty' = 'AzleEmpty'; - _azleCandidType?: '_azleCandidType' = '_azleCandidType'; + + static _azleKind: 'AzleEmpty' = 'AzleEmpty'; static _azleCandidType?: '_azleCandidType' = '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/floats/float32.ts b/src/lib/candid/types/primitive/floats/float32.ts index c204019708..593ef0449d 100644 --- a/src/lib/candid/types/primitive/floats/float32.ts +++ b/src/lib/candid/types/primitive/floats/float32.ts @@ -4,6 +4,9 @@ import { decode } from '../../../serde/decode'; export class AzleFloat32 { _azleKind: 'AzleFloat32' = 'AzleFloat32'; + _azleCandidType?: '_azleCandidType'; + + static _azleKind: 'AzleFloat32' = 'AzleFloat32'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/floats/float64.ts b/src/lib/candid/types/primitive/floats/float64.ts index 1c7e90c55c..0ff87e9352 100644 --- a/src/lib/candid/types/primitive/floats/float64.ts +++ b/src/lib/candid/types/primitive/floats/float64.ts @@ -4,6 +4,9 @@ import { decode } from '../../../serde/decode'; export class AzleFloat64 { _azleKind: 'AzleFloat64' = 'AzleFloat64'; + _azleCandidType?: '_azleCandidType'; + + static _azleKind: 'AzleFloat64' = 'AzleFloat64'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/ints/int.ts b/src/lib/candid/types/primitive/ints/int.ts index 58a13ef8f2..edb4b9aa42 100644 --- a/src/lib/candid/types/primitive/ints/int.ts +++ b/src/lib/candid/types/primitive/ints/int.ts @@ -4,6 +4,9 @@ import { decode } from '../../../serde/decode'; export class AzleInt { _azleKind: 'AzleInt' = 'AzleInt'; + _azleCandidType?: '_azleCandidType'; + + static _azleKind: 'AzleInt' = 'AzleInt'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/ints/int16.ts b/src/lib/candid/types/primitive/ints/int16.ts index f9628bbb92..dc5e35720b 100644 --- a/src/lib/candid/types/primitive/ints/int16.ts +++ b/src/lib/candid/types/primitive/ints/int16.ts @@ -3,6 +3,9 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleInt16 { + _azleKind: 'AzleInt16' = 'AzleInt16'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleInt16' = 'AzleInt16'; static _azleCandidType?: '_azleCandidType'; diff --git a/src/lib/candid/types/primitive/ints/int32.ts b/src/lib/candid/types/primitive/ints/int32.ts index 7333bb5dea..83a6dd2052 100644 --- a/src/lib/candid/types/primitive/ints/int32.ts +++ b/src/lib/candid/types/primitive/ints/int32.ts @@ -3,6 +3,9 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleInt32 { + _azleKind: 'AzleInt32' = 'AzleInt32'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleInt32' = 'AzleInt32'; static _azleCandidType?: '_azleCandidType'; diff --git a/src/lib/candid/types/primitive/ints/int64.ts b/src/lib/candid/types/primitive/ints/int64.ts index fb6bc8586f..4ccbcc6359 100644 --- a/src/lib/candid/types/primitive/ints/int64.ts +++ b/src/lib/candid/types/primitive/ints/int64.ts @@ -4,6 +4,9 @@ import { decode } from '../../../serde/decode'; export class AzleInt64 { _azleKind: 'AzleInt64' = 'AzleInt64'; + _azleCandidType?: '_azleCandidType'; + + static _azleKind: 'AzleInt64' = 'AzleInt64'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/ints/int8.ts b/src/lib/candid/types/primitive/ints/int8.ts index 1cdc1e5edb..a47147d61c 100644 --- a/src/lib/candid/types/primitive/ints/int8.ts +++ b/src/lib/candid/types/primitive/ints/int8.ts @@ -3,6 +3,9 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleInt8 { + _azleKind: 'AzleInt8' = 'AzleInt8'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleInt8' = 'AzleInt8'; static _azleCandidType?: '_azleCandidType'; diff --git a/src/lib/candid/types/primitive/nats/nat.ts b/src/lib/candid/types/primitive/nats/nat.ts index 2df07233e3..2d0c145b82 100644 --- a/src/lib/candid/types/primitive/nats/nat.ts +++ b/src/lib/candid/types/primitive/nats/nat.ts @@ -4,6 +4,9 @@ import { decode } from '../../../serde/decode'; export class AzleNat { _azleKind: 'AzleNat' = 'AzleNat'; + _azleCandidType?: '_azleCandidType'; + + static _azleKind: 'AzleNat' = 'AzleNat'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/nats/nat16.ts b/src/lib/candid/types/primitive/nats/nat16.ts index 03494e5547..ebca64aee4 100644 --- a/src/lib/candid/types/primitive/nats/nat16.ts +++ b/src/lib/candid/types/primitive/nats/nat16.ts @@ -3,6 +3,9 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleNat16 { + _azleKind: 'AzleNat16' = 'AzleNat16'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleNat16' = 'AzleNat16'; static _azleCandidType?: '_azleCandidType'; diff --git a/src/lib/candid/types/primitive/nats/nat32.ts b/src/lib/candid/types/primitive/nats/nat32.ts index 9ae5f275f1..bf8c1b5403 100644 --- a/src/lib/candid/types/primitive/nats/nat32.ts +++ b/src/lib/candid/types/primitive/nats/nat32.ts @@ -3,6 +3,9 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleNat32 { + _azleKind: 'AzleNat32' = 'AzleNat32'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleNat32' = 'AzleNat32'; static _azleCandidType?: '_azleCandidType'; diff --git a/src/lib/candid/types/primitive/nats/nat64.ts b/src/lib/candid/types/primitive/nats/nat64.ts index c9b7d1adc7..d7bc9b60b8 100644 --- a/src/lib/candid/types/primitive/nats/nat64.ts +++ b/src/lib/candid/types/primitive/nats/nat64.ts @@ -4,6 +4,9 @@ import { decode } from '../../../serde/decode'; export class AzleNat64 { _azleKind: 'AzleNat64' = 'AzleNat64'; + _azleCandidType?: '_azleCandidType'; + + static _azleKind: 'AzleNat64' = 'AzleNat64'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/nats/nat8.ts b/src/lib/candid/types/primitive/nats/nat8.ts index a29609e991..24979dcb39 100644 --- a/src/lib/candid/types/primitive/nats/nat8.ts +++ b/src/lib/candid/types/primitive/nats/nat8.ts @@ -3,6 +3,9 @@ import { encode } from '../../../serde/encode'; import { decode } from '../../../serde/decode'; export class AzleNat8 { + _azleKind: 'AzleNat8' = 'AzleNat8'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleNat8' = 'AzleNat8'; static _azleCandidType?: '_azleCandidType'; diff --git a/src/lib/candid/types/primitive/null.ts b/src/lib/candid/types/primitive/null.ts index f6120aba81..6b0cd74281 100644 --- a/src/lib/candid/types/primitive/null.ts +++ b/src/lib/candid/types/primitive/null.ts @@ -4,8 +4,9 @@ import { decode } from '../../serde/decode'; export class AzleNull { _azleKind: 'AzleNull' = 'AzleNull'; - static _azleKind: 'AzleKind' = 'AzleKind'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleNull' = 'AzleNull'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/reserved.ts b/src/lib/candid/types/primitive/reserved.ts index 5056dee426..8a86fd6ecd 100644 --- a/src/lib/candid/types/primitive/reserved.ts +++ b/src/lib/candid/types/primitive/reserved.ts @@ -4,8 +4,9 @@ import { decode } from '../../serde/decode'; export class AzleReserved { _azleKind: 'AzleReserved' = 'AzleReserved'; - static _azleKind: 'AzleKind' = 'AzleKind'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleReserved' = 'AzleReserved'; static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/text.ts b/src/lib/candid/types/primitive/text.ts index 795d163cd5..c184fbf50c 100644 --- a/src/lib/candid/types/primitive/text.ts +++ b/src/lib/candid/types/primitive/text.ts @@ -4,9 +4,9 @@ import { decode } from '../../serde/decode'; export class AzleText { _azleKind: 'AzleText' = 'AzleText'; - static _azleKind: 'AzleText' = 'AzleText'; - _azleCandidType?: '_azleCandidType' = '_azleCandidType'; + + static _azleKind: 'AzleText' = 'AzleText'; static _azleCandidType?: '_azleCandidType' = '_azleCandidType'; static toBytes(data: any) { diff --git a/src/lib/candid/types/primitive/void.ts b/src/lib/candid/types/primitive/void.ts index 917b95c30d..ebb911b13c 100644 --- a/src/lib/candid/types/primitive/void.ts +++ b/src/lib/candid/types/primitive/void.ts @@ -2,11 +2,11 @@ import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; export class AzleVoid { - static _azleKind: 'AzleVoid' = 'AzleVoid'; _azleKind: 'AzleVoid' = 'AzleVoid'; + _azleCandidType?: '_azleCandidType'; + static _azleKind: 'AzleVoid' = 'AzleVoid'; static _azleCandidType?: '_azleCandidType'; - _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { return encode(this, data); diff --git a/src/lib/candid/types/reference/principal.ts b/src/lib/candid/types/reference/principal.ts index 0472a90cfc..7655bb9ffa 100644 --- a/src/lib/candid/types/reference/principal.ts +++ b/src/lib/candid/types/reference/principal.ts @@ -5,7 +5,6 @@ import { decode } from '../../serde/decode'; export class Principal extends DfinityPrincipal { static _azleKind: 'Principal' = 'Principal'; - static _azleCandidType?: '_azleCandidType'; static toBytes(data: any) { From f6f6423b64a397359ddc420b8b9d79130e194a06 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Tue, 7 Nov 2023 11:51:37 -0600 Subject: [PATCH 24/30] working on stablejson --- .github/workflows/test.yml | 1 + examples/stable_json/.gitignore | 4 + examples/stable_json/dfx.json | 16 + examples/stable_json/package-lock.json | 1155 ++++++++++++++++++++++++ examples/stable_json/package.json | 14 + examples/stable_json/src/index.did | 3 + examples/stable_json/src/index.ts | 16 + examples/stable_json/test/pretest.ts | 19 + examples/stable_json/test/test.ts | 11 + examples/stable_json/test/tests.ts | 8 + examples/stable_json/tsconfig.json | 9 + src/lib/candid/type_mapping.ts | 3 + src/lib/stable_b_tree_map.ts | 8 +- 13 files changed, 1266 insertions(+), 1 deletion(-) create mode 100644 examples/stable_json/.gitignore create mode 100644 examples/stable_json/dfx.json create mode 100644 examples/stable_json/package-lock.json create mode 100644 examples/stable_json/package.json create mode 100644 examples/stable_json/src/index.did create mode 100644 examples/stable_json/src/index.ts create mode 100644 examples/stable_json/test/pretest.ts create mode 100644 examples/stable_json/test/test.ts create mode 100644 examples/stable_json/test/tests.ts create mode 100644 examples/stable_json/tsconfig.json diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index 9ef7a5a4a9..90b7d9ca4a 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -124,6 +124,7 @@ jobs: "examples/robust_imports", "examples/simple_erc20", "examples/simple_user_accounts", + "examples/stable_json", "examples/stable_memory", "examples/stable_structures", "examples/timers", diff --git a/examples/stable_json/.gitignore b/examples/stable_json/.gitignore new file mode 100644 index 0000000000..61ffd3f9d2 --- /dev/null +++ b/examples/stable_json/.gitignore @@ -0,0 +1,4 @@ +.azle +.dfx +dfx_generated +node_modules diff --git a/examples/stable_json/dfx.json b/examples/stable_json/dfx.json new file mode 100644 index 0000000000..e27016afdc --- /dev/null +++ b/examples/stable_json/dfx.json @@ -0,0 +1,16 @@ +{ + "canisters": { + "stable_json": { + "type": "custom", + "main": "src/index.ts", + "candid": "src/index.did", + "build": "npx azle stable_json", + "wasm": ".azle/stable_json/stable_json.wasm", + "gzip": true, + "declarations": { + "output": "test/dfx_generated/stable_json", + "node_compatibility": true + } + } + } +} diff --git a/examples/stable_json/package-lock.json b/examples/stable_json/package-lock.json new file mode 100644 index 0000000000..d69cae788b --- /dev/null +++ b/examples/stable_json/package-lock.json @@ -0,0 +1,1155 @@ +{ + "name": "stable_json", + "lockfileVersion": 3, + "requires": true, + "packages": { + "": { + "dependencies": { + "azle": "0.18.6" + }, + "devDependencies": { + "@dfinity/agent": "^0.19.2", + "ts-node": "^10.9.1", + "typescript": "^5.2.2" + } + }, + "node_modules/@cspotcode/source-map-consumer": { + "version": "0.8.0", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-consumer/-/source-map-consumer-0.8.0.tgz", + "integrity": "sha512-41qniHzTU8yAGbCp04ohlmSrZf8bkf/iJsl3V0dRGsQN/5GFfx+LbCSsCpp2gqrqjTVg/K6O8ycoV35JIwAzAg==", + "engines": { + "node": ">= 12" + } + }, + "node_modules/@cspotcode/source-map-support": { + "version": "0.8.1", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.8.1.tgz", + "integrity": "sha512-IchNf6dN4tHoMFIn/7OE8LWZ19Y6q/67Bmf6vnGREv8RSbBVb9LPJxEcnwrcwX6ixSvaiGoomAUvu4YSxXrVgw==", + "dev": true, + "dependencies": { + "@jridgewell/trace-mapping": "0.3.9" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/@dfinity/agent": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/@dfinity/agent/-/agent-0.19.2.tgz", + "integrity": "sha512-KLRWEjeU9SyyaS7IBVJ9ZUcufxufr55e/kRIyClK157+0pkTG9a8xKjUIMx3QzKvLsqqzXL238nWwdoP6jAD8g==", + "dev": true, + "dependencies": { + "@noble/hashes": "^1.3.1", + "base64-arraybuffer": "^0.2.0", + "borc": "^2.1.1", + "simple-cbor": "^0.4.1" + }, + "peerDependencies": { + "@dfinity/candid": "^0.19.2", + "@dfinity/principal": "^0.19.2" + } + }, + "node_modules/@dfinity/candid": { + "version": "0.19.2", + "resolved": "git+ssh://git@github.com/demergent-labs/candid.git#88ffa6d9a85b175fcf3ef2a79c9fe4c0f034c02d", + "license": "Apache-2.0", + "peerDependencies": { + "@dfinity/principal": "^0.19.2" + } + }, + "node_modules/@dfinity/principal": { + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.19.2.tgz", + "integrity": "sha512-vsKN6BKya70bQUsjgKRDlR2lOpv/XpUkCMIiji6rjMtKHIuWEB5Eu3JqZsOuBmWo3A3TT/K/osT9VPm0k4qdYQ==", + "dependencies": { + "@noble/hashes": "^1.3.1" + } + }, + "node_modules/@esbuild/android-arm": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm/-/android-arm-0.19.4.tgz", + "integrity": "sha512-uBIbiYMeSsy2U0XQoOGVVcpIktjLMEKa7ryz2RLr7L/vTnANNEsPVAh4xOv7ondGz6ac1zVb0F8Jx20rQikffQ==", + "cpu": [ + "arm" + ], + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-arm64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-arm64/-/android-arm64-0.19.4.tgz", + "integrity": "sha512-mRsi2vJsk4Bx/AFsNBqOH2fqedxn5L/moT58xgg51DjX1la64Z3Npicut2VbhvDFO26qjWtPMsVxCd80YTFVeg==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/android-x64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/android-x64/-/android-x64-0.19.4.tgz", + "integrity": "sha512-4iPufZ1TMOD3oBlGFqHXBpa3KFT46aLl6Vy7gwed0ZSYgHaZ/mihbYb4t7Z9etjkC9Al3ZYIoOaHrU60gcMy7g==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "android" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/darwin-arm64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-arm64/-/darwin-arm64-0.19.4.tgz", + "integrity": "sha512-Lviw8EzxsVQKpbS+rSt6/6zjn9ashUZ7Tbuvc2YENgRl0yZTktGlachZ9KMJUsVjZEGFVu336kl5lBgDN6PmpA==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/darwin-x64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/darwin-x64/-/darwin-x64-0.19.4.tgz", + "integrity": "sha512-YHbSFlLgDwglFn0lAO3Zsdrife9jcQXQhgRp77YiTDja23FrC2uwnhXMNkAucthsf+Psr7sTwYEryxz6FPAVqw==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-arm64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-arm64/-/freebsd-arm64-0.19.4.tgz", + "integrity": "sha512-vz59ijyrTG22Hshaj620e5yhs2dU1WJy723ofc+KUgxVCM6zxQESmWdMuVmUzxtGqtj5heHyB44PjV/HKsEmuQ==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/freebsd-x64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/freebsd-x64/-/freebsd-x64-0.19.4.tgz", + "integrity": "sha512-3sRbQ6W5kAiVQRBWREGJNd1YE7OgzS0AmOGjDmX/qZZecq8NFlQsQH0IfXjjmD0XtUYqr64e0EKNFjMUlPL3Cw==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "freebsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm/-/linux-arm-0.19.4.tgz", + "integrity": "sha512-z/4ArqOo9EImzTi4b6Vq+pthLnepFzJ92BnofU1jgNlcVb+UqynVFdoXMCFreTK7FdhqAzH0vmdwW5373Hm9pg==", + "cpu": [ + "arm" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-arm64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-arm64/-/linux-arm64-0.19.4.tgz", + "integrity": "sha512-ZWmWORaPbsPwmyu7eIEATFlaqm0QGt+joRE9sKcnVUG3oBbr/KYdNE2TnkzdQwX6EDRdg/x8Q4EZQTXoClUqqA==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ia32": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ia32/-/linux-ia32-0.19.4.tgz", + "integrity": "sha512-EGc4vYM7i1GRUIMqRZNCTzJh25MHePYsnQfKDexD8uPTCm9mK56NIL04LUfX2aaJ+C9vyEp2fJ7jbqFEYgO9lQ==", + "cpu": [ + "ia32" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-loong64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-loong64/-/linux-loong64-0.19.4.tgz", + "integrity": "sha512-WVhIKO26kmm8lPmNrUikxSpXcgd6HDog0cx12BUfA2PkmURHSgx9G6vA19lrlQOMw+UjMZ+l3PpbtzffCxFDRg==", + "cpu": [ + "loong64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-mips64el": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-mips64el/-/linux-mips64el-0.19.4.tgz", + "integrity": "sha512-keYY+Hlj5w86hNp5JJPuZNbvW4jql7c1eXdBUHIJGTeN/+0QFutU3GrS+c27L+NTmzi73yhtojHk+lr2+502Mw==", + "cpu": [ + "mips64el" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-ppc64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-ppc64/-/linux-ppc64-0.19.4.tgz", + "integrity": "sha512-tQ92n0WMXyEsCH4m32S21fND8VxNiVazUbU4IUGVXQpWiaAxOBvtOtbEt3cXIV3GEBydYsY8pyeRMJx9kn3rvw==", + "cpu": [ + "ppc64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-riscv64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-riscv64/-/linux-riscv64-0.19.4.tgz", + "integrity": "sha512-tRRBey6fG9tqGH6V75xH3lFPpj9E8BH+N+zjSUCnFOX93kEzqS0WdyJHkta/mmJHn7MBaa++9P4ARiU4ykjhig==", + "cpu": [ + "riscv64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-s390x": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-s390x/-/linux-s390x-0.19.4.tgz", + "integrity": "sha512-152aLpQqKZYhThiJ+uAM4PcuLCAOxDsCekIbnGzPKVBRUDlgaaAfaUl5NYkB1hgY6WN4sPkejxKlANgVcGl9Qg==", + "cpu": [ + "s390x" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/linux-x64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/linux-x64/-/linux-x64-0.19.4.tgz", + "integrity": "sha512-Mi4aNA3rz1BNFtB7aGadMD0MavmzuuXNTaYL6/uiYIs08U7YMPETpgNn5oue3ICr+inKwItOwSsJDYkrE9ekVg==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/netbsd-x64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/netbsd-x64/-/netbsd-x64-0.19.4.tgz", + "integrity": "sha512-9+Wxx1i5N/CYo505CTT7T+ix4lVzEdz0uCoYGxM5JDVlP2YdDC1Bdz+Khv6IbqmisT0Si928eAxbmGkcbiuM/A==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "netbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/openbsd-x64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/openbsd-x64/-/openbsd-x64-0.19.4.tgz", + "integrity": "sha512-MFsHleM5/rWRW9EivFssop+OulYVUoVcqkyOkjiynKBCGBj9Lihl7kh9IzrreDyXa4sNkquei5/DTP4uCk25xw==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "openbsd" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/sunos-x64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/sunos-x64/-/sunos-x64-0.19.4.tgz", + "integrity": "sha512-6Xq8SpK46yLvrGxjp6HftkDwPP49puU4OF0hEL4dTxqCbfx09LyrbUj/D7tmIRMj5D5FCUPksBbxyQhp8tmHzw==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "sunos" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-arm64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-arm64/-/win32-arm64-0.19.4.tgz", + "integrity": "sha512-PkIl7Jq4mP6ke7QKwyg4fD4Xvn8PXisagV/+HntWoDEdmerB2LTukRZg728Yd1Fj+LuEX75t/hKXE2Ppk8Hh1w==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-ia32": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-ia32/-/win32-ia32-0.19.4.tgz", + "integrity": "sha512-ga676Hnvw7/ycdKB53qPusvsKdwrWzEyJ+AtItHGoARszIqvjffTwaaW3b2L6l90i7MO9i+dlAW415INuRhSGg==", + "cpu": [ + "ia32" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@esbuild/win32-x64": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/@esbuild/win32-x64/-/win32-x64-0.19.4.tgz", + "integrity": "sha512-HP0GDNla1T3ZL8Ko/SHAS2GgtjOg+VmWnnYLhuTksr++EnduYB0f3Y2LzHsUwb2iQ13JGoY6G3R8h6Du/WG6uA==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=12" + } + }, + "node_modules/@jridgewell/resolve-uri": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/@jridgewell/resolve-uri/-/resolve-uri-3.1.1.tgz", + "integrity": "sha512-dSYZh7HhCDtCKm4QakX0xFpsRDqjjtZf/kjI/v3T3Nwt5r8/qz/M19F9ySyOqU94SXBmeG9ttTul+YnR4LOxFA==", + "dev": true, + "engines": { + "node": ">=6.0.0" + } + }, + "node_modules/@jridgewell/sourcemap-codec": { + "version": "1.4.15", + "resolved": "https://registry.npmjs.org/@jridgewell/sourcemap-codec/-/sourcemap-codec-1.4.15.tgz", + "integrity": "sha512-eF2rxCRulEKXHTRiDrDy6erMYWqNw4LPdQ8UQA4huuxaQsVeRPFl2oM8oDGxMFhJUWZf9McpLtJasDDZb/Bpeg==", + "dev": true + }, + "node_modules/@jridgewell/trace-mapping": { + "version": "0.3.9", + "resolved": "https://registry.npmjs.org/@jridgewell/trace-mapping/-/trace-mapping-0.3.9.tgz", + "integrity": "sha512-3Belt6tdc8bPgAtbcmdtNJlirVoTmEb5e2gC94PnkwEW9jI6CAHUeoG85tjWP5WquqfavoMtMwiG4P926ZKKuQ==", + "dev": true, + "dependencies": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "node_modules/@noble/hashes": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz", + "integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==", + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } + }, + "node_modules/@swc/core": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core/-/core-1.3.91.tgz", + "integrity": "sha512-r950d0fdlZ8qbSDyvApn3HyCojiZE8xpgJzQvypeMi32dalYwugdJKWyLB55JIGMRGJ8+lmVvY4MPGkSR3kXgA==", + "hasInstallScript": true, + "dependencies": { + "@swc/counter": "^0.1.1", + "@swc/types": "^0.1.5" + }, + "engines": { + "node": ">=10" + }, + "funding": { + "type": "opencollective", + "url": "https://opencollective.com/swc" + }, + "optionalDependencies": { + "@swc/core-darwin-arm64": "1.3.91", + "@swc/core-darwin-x64": "1.3.91", + "@swc/core-linux-arm-gnueabihf": "1.3.91", + "@swc/core-linux-arm64-gnu": "1.3.91", + "@swc/core-linux-arm64-musl": "1.3.91", + "@swc/core-linux-x64-gnu": "1.3.91", + "@swc/core-linux-x64-musl": "1.3.91", + "@swc/core-win32-arm64-msvc": "1.3.91", + "@swc/core-win32-ia32-msvc": "1.3.91", + "@swc/core-win32-x64-msvc": "1.3.91" + }, + "peerDependencies": { + "@swc/helpers": "^0.5.0" + }, + "peerDependenciesMeta": { + "@swc/helpers": { + "optional": true + } + } + }, + "node_modules/@swc/core-darwin-arm64": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-arm64/-/core-darwin-arm64-1.3.91.tgz", + "integrity": "sha512-7kHGiQ1he5khcEeJuHDmLZPM3rRL/ith5OTmV6bOPsoHi46kLeixORW+ts1opC3tC9vu6xbk16xgX0QAJchc1w==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-darwin-x64": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-darwin-x64/-/core-darwin-x64-1.3.91.tgz", + "integrity": "sha512-8SpU18FbFpZDVzsHsAwdI1thF/picQGxq9UFxa8W+T9SDnbsqwFJv/6RqKJeJoDV6qFdl2OLjuO0OL7xrp0qnQ==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "darwin" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-arm-gnueabihf": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm-gnueabihf/-/core-linux-arm-gnueabihf-1.3.91.tgz", + "integrity": "sha512-fOq4Cy8UbwX1yf0WB0d8hWZaIKCnPtPGguRqdXGLfwvhjZ9SIErT6PnmGTGRbQCNCIkOZWHKyTU0r8t2dN3haQ==", + "cpu": [ + "arm" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-arm64-gnu": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-gnu/-/core-linux-arm64-gnu-1.3.91.tgz", + "integrity": "sha512-fki4ioRP/Esy4vdp8T34RCV+V9dqkRmOt763pf74pdiyFV2dPLXa5lnw/XvR1RTfPGknrYgjEQLCfZlReTryRw==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-arm64-musl": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-linux-arm64-musl/-/core-linux-arm64-musl-1.3.91.tgz", + "integrity": "sha512-XrG+DUUqNtfVLcJ20imby7fpBwQNG5VsEQBzQndSonPyUOa2YkTbBb60YDondfQGDABopuHH8gHN8o2H2/VCnQ==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-x64-gnu": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-gnu/-/core-linux-x64-gnu-1.3.91.tgz", + "integrity": "sha512-d11bYhX+YPBr/Frcjc6eVn3C0LuS/9U1Li9EmQ+6s9EpYtYRl2ygSlC8eueLbaiazBnCVYFnc8bU4o0kc5B9sw==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-linux-x64-musl": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-linux-x64-musl/-/core-linux-x64-musl-1.3.91.tgz", + "integrity": "sha512-2SRp5Dke2P4jCQePkDx9trkkTstnRpZJVw5r3jvYdk0zeO6iC4+ZPvvoWXJLigqQv/fZnIiSUfJ6ssOoaEqTzQ==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "linux" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-arm64-msvc": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-win32-arm64-msvc/-/core-win32-arm64-msvc-1.3.91.tgz", + "integrity": "sha512-l9qKXikOxj42UIjbeZpz9xtBmr736jOMqInNP8mVF2/U+ws5sI8zJjcOFFtfis4ru7vWCXhB1wtltdlJYO2vGA==", + "cpu": [ + "arm64" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-ia32-msvc": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-win32-ia32-msvc/-/core-win32-ia32-msvc-1.3.91.tgz", + "integrity": "sha512-+s+52O0QVPmzOgjEe/rcb0AK6q/J7EHKwAyJCu/FaYO9df5ovE0HJjSKP6HAF0dGPO5hkENrXuNGujofUH9vtQ==", + "cpu": [ + "ia32" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/core-win32-x64-msvc": { + "version": "1.3.91", + "resolved": "https://registry.npmjs.org/@swc/core-win32-x64-msvc/-/core-win32-x64-msvc-1.3.91.tgz", + "integrity": "sha512-7u9HDQhjUC3Gv43EFW84dZtduWCSa4MgltK+Sp9zEGti6WXqDPu/ESjvDsQEVYTBEMEvZs/xVAXPgLVHorV5nQ==", + "cpu": [ + "x64" + ], + "optional": true, + "os": [ + "win32" + ], + "engines": { + "node": ">=10" + } + }, + "node_modules/@swc/counter": { + "version": "0.1.2", + "resolved": "https://registry.npmjs.org/@swc/counter/-/counter-0.1.2.tgz", + "integrity": "sha512-9F4ys4C74eSTEUNndnER3VJ15oru2NumfQxS8geE+f3eB5xvfxpWyqE5XlVnxb/R14uoXi6SLbBwwiDSkv+XEw==" + }, + "node_modules/@swc/types": { + "version": "0.1.5", + "resolved": "https://registry.npmjs.org/@swc/types/-/types-0.1.5.tgz", + "integrity": "sha512-myfUej5naTBWnqOCc/MdVOLVjXUXtIA+NpDrDBKJtLLg2shUjBu3cZmB/85RyitKc55+lUUyl7oRfLOvkr2hsw==" + }, + "node_modules/@tsconfig/node10": { + "version": "1.0.9", + "resolved": "https://registry.npmjs.org/@tsconfig/node10/-/node10-1.0.9.tgz", + "integrity": "sha512-jNsYVVxU8v5g43Erja32laIDHXeoNvFEpX33OK4d6hljo3jDhCBDhx5dhCCTMWUojscpAagGiRkBKxpdl9fxqA==" + }, + "node_modules/@tsconfig/node12": { + "version": "1.0.11", + "resolved": "https://registry.npmjs.org/@tsconfig/node12/-/node12-1.0.11.tgz", + "integrity": "sha512-cqefuRsh12pWyGsIoBKJA9luFu3mRxCA+ORZvA4ktLSzIuCUtWVxGIuXigEwO5/ywWFMZ2QEGKWvkZG1zDMTag==" + }, + "node_modules/@tsconfig/node14": { + "version": "1.0.3", + "resolved": "https://registry.npmjs.org/@tsconfig/node14/-/node14-1.0.3.tgz", + "integrity": "sha512-ysT8mhdixWK6Hw3i1V2AeRqZ5WfXg1G43mqoYlM2nc6388Fq5jcXyr5mRsqViLx/GJYdoL0bfXD8nmF+Zn/Iow==" + }, + "node_modules/@tsconfig/node16": { + "version": "1.0.4", + "resolved": "https://registry.npmjs.org/@tsconfig/node16/-/node16-1.0.4.tgz", + "integrity": "sha512-vxhUy4J8lyeyinH7Azl1pdd43GJhZH/tP2weN8TntQblOY+A0XbT8DJk1/oCPuOOyg/Ja757rG0CgHcWC8OfMA==" + }, + "node_modules/@types/node": { + "version": "20.5.9", + "resolved": "https://registry.npmjs.org/@types/node/-/node-20.5.9.tgz", + "integrity": "sha512-PcGNd//40kHAS3sTlzKB9C9XL4K0sTup8nbG5lC14kzEteTNuAFh9u5nA0o5TWnSG2r/JNPRXFVcHJIIeRlmqQ==", + "peer": true + }, + "node_modules/@types/uuid": { + "version": "9.0.4", + "resolved": "https://registry.npmjs.org/@types/uuid/-/uuid-9.0.4.tgz", + "integrity": "sha512-zAuJWQflfx6dYJM62vna+Sn5aeSWhh3OB+wfUEACNcqUSc0AGc5JKl+ycL1vrH7frGTXhJchYjE1Hak8L819dA==" + }, + "node_modules/acorn": { + "version": "8.10.0", + "resolved": "https://registry.npmjs.org/acorn/-/acorn-8.10.0.tgz", + "integrity": "sha512-F0SAmZ8iUtS//m8DmCTA0jlh6TDKkHQyK6xc6V4KDTyZKA9dnvX9/3sRTVQrWm79glUAZbnmmNcdYwUIHWVybw==", + "bin": { + "acorn": "bin/acorn" + }, + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/acorn-walk": { + "version": "8.2.0", + "resolved": "https://registry.npmjs.org/acorn-walk/-/acorn-walk-8.2.0.tgz", + "integrity": "sha512-k+iyHEuPgSw6SbuDpGQM+06HQUa04DZ3o+F6CSzXMvvI5KMvnaEqXe+YVe555R9nn6GPt404fos4wcgpw12SDA==", + "engines": { + "node": ">=0.4.0" + } + }, + "node_modules/arg": { + "version": "4.1.3", + "resolved": "https://registry.npmjs.org/arg/-/arg-4.1.3.tgz", + "integrity": "sha512-58S9QDqG0Xx27YwPSt9fJxivjYl432YCwfDMfZ+71RAqUrZef7LrKQZ3LHLOwCS4FLNBplP533Zx895SeOCHvA==" + }, + "node_modules/azle": { + "version": "0.18.6", + "resolved": "https://registry.npmjs.org/azle/-/azle-0.18.6.tgz", + "integrity": "sha512-XEebokPOpNAQ0+B7x6fj6cPVSvmbEYm2p9/bSvFxuKQ4VcnzmsOdX7HCZyYYTKsD5eTc4XxuaHqqdagmdzbJPw==", + "dependencies": { + "@dfinity/candid": "github:demergent-labs/candid#minimum_viable", + "@dfinity/principal": "^0.19.0", + "@swc/core": "^1.3.86", + "@types/uuid": "^9.0.4", + "buffer": "^6.0.3", + "esbuild": "^0.19.3", + "fs-extra": "10.0.1", + "js-sha256": "0.9.0", + "text-encoding": "0.7.0", + "ts-node": "10.3.1", + "typescript": "^5.2.2", + "uuid": "^9.0.1" + }, + "bin": { + "azle": "bin.js" + } + }, + "node_modules/azle/node_modules/@cspotcode/source-map-support": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/@cspotcode/source-map-support/-/source-map-support-0.7.0.tgz", + "integrity": "sha512-X4xqRHqN8ACt2aHVe51OxeA2HjbcL4MqFqXkrmQszJ1NOUuUu5u6Vqx/0lZSVNku7velL5FC/s5uEAj1lsBMhA==", + "dependencies": { + "@cspotcode/source-map-consumer": "0.8.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/azle/node_modules/buffer": { + "version": "6.0.3", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", + "integrity": "sha512-FTiCpNxtwiZZHEZbcbTIcZjERVICn9yq/pDFkTl95/AxzD1naBctN7YO68riM/gLSDY7sdrMby8hofADYuuqOA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.2.1" + } + }, + "node_modules/azle/node_modules/ts-node": { + "version": "10.3.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.3.1.tgz", + "integrity": "sha512-Yw3W2mYzhHfCHOICGNJqa0i+rbL0rAyg7ZIHxU+K4pgY8gd2Lh1j+XbHCusJMykbj6RZMJVOY0MlHVd+GOivcw==", + "dependencies": { + "@cspotcode/source-map-support": "0.7.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/base64-arraybuffer": { + "version": "0.2.0", + "resolved": "https://registry.npmjs.org/base64-arraybuffer/-/base64-arraybuffer-0.2.0.tgz", + "integrity": "sha512-7emyCsu1/xiBXgQZrscw/8KPRT44I4Yq9Pe6EGs3aPRTsWuggML1/1DTuZUuIaJPIm1FTDUVXl4x/yW8s0kQDQ==", + "dev": true, + "engines": { + "node": ">= 0.6.0" + } + }, + "node_modules/base64-js": { + "version": "1.5.1", + "resolved": "https://registry.npmjs.org/base64-js/-/base64-js-1.5.1.tgz", + "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/bignumber.js": { + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.2.tgz", + "integrity": "sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==", + "dev": true, + "engines": { + "node": "*" + } + }, + "node_modules/borc": { + "version": "2.1.2", + "resolved": "https://registry.npmjs.org/borc/-/borc-2.1.2.tgz", + "integrity": "sha512-Sy9eoUi4OiKzq7VovMn246iTo17kzuyHJKomCfpWMlI6RpfN1gk95w7d7gH264nApVLg0HZfcpz62/g4VH1Y4w==", + "dev": true, + "dependencies": { + "bignumber.js": "^9.0.0", + "buffer": "^5.5.0", + "commander": "^2.15.0", + "ieee754": "^1.1.13", + "iso-url": "~0.4.7", + "json-text-sequence": "~0.1.0", + "readable-stream": "^3.6.0" + }, + "engines": { + "node": ">=4" + } + }, + "node_modules/buffer": { + "version": "5.7.1", + "resolved": "https://registry.npmjs.org/buffer/-/buffer-5.7.1.tgz", + "integrity": "sha512-EHcyIPBQ4BSGlvjB16k5KgAJ27CIsHY/2JBmCRReo48y9rQ3MaUzWX3KVlBa4U7MyX02HdVj0K7C3WaB3ju7FQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ], + "dependencies": { + "base64-js": "^1.3.1", + "ieee754": "^1.1.13" + } + }, + "node_modules/commander": { + "version": "2.20.3", + "resolved": "https://registry.npmjs.org/commander/-/commander-2.20.3.tgz", + "integrity": "sha512-GpVkmM8vF2vQUkj2LvZmD35JxeJOLCwJ9cUkugyk2nuhbv3+mJvpLYYt+0+USMxE+oj+ey/lJEnhZw75x/OMcQ==", + "dev": true + }, + "node_modules/create-require": { + "version": "1.1.1", + "resolved": "https://registry.npmjs.org/create-require/-/create-require-1.1.1.tgz", + "integrity": "sha512-dcKFX3jn0MpIaXjisoRvexIJVEKzaq7z2rZKxf+MSr9TkdmHmsU4m2lcLojrj/FHl8mk5VxMmYA+ftRkP/3oKQ==" + }, + "node_modules/delimit-stream": { + "version": "0.1.0", + "resolved": "https://registry.npmjs.org/delimit-stream/-/delimit-stream-0.1.0.tgz", + "integrity": "sha512-a02fiQ7poS5CnjiJBAsjGLPp5EwVoGHNeu9sziBd9huppRfsAFIpv5zNLv0V1gbop53ilngAf5Kf331AwcoRBQ==", + "dev": true + }, + "node_modules/diff": { + "version": "4.0.2", + "resolved": "https://registry.npmjs.org/diff/-/diff-4.0.2.tgz", + "integrity": "sha512-58lmxKSA4BNyLz+HHMUzlOEpg09FV+ev6ZMe3vJihgdxzgcwZ8VoEEPmALCZG9LmqfVoNMMKpttIYTVG6uDY7A==", + "engines": { + "node": ">=0.3.1" + } + }, + "node_modules/esbuild": { + "version": "0.19.4", + "resolved": "https://registry.npmjs.org/esbuild/-/esbuild-0.19.4.tgz", + "integrity": "sha512-x7jL0tbRRpv4QUyuDMjONtWFciygUxWaUM1kMX2zWxI0X2YWOt7MSA0g4UdeSiHM8fcYVzpQhKYOycZwxTdZkA==", + "hasInstallScript": true, + "bin": { + "esbuild": "bin/esbuild" + }, + "engines": { + "node": ">=12" + }, + "optionalDependencies": { + "@esbuild/android-arm": "0.19.4", + "@esbuild/android-arm64": "0.19.4", + "@esbuild/android-x64": "0.19.4", + "@esbuild/darwin-arm64": "0.19.4", + "@esbuild/darwin-x64": "0.19.4", + "@esbuild/freebsd-arm64": "0.19.4", + "@esbuild/freebsd-x64": "0.19.4", + "@esbuild/linux-arm": "0.19.4", + "@esbuild/linux-arm64": "0.19.4", + "@esbuild/linux-ia32": "0.19.4", + "@esbuild/linux-loong64": "0.19.4", + "@esbuild/linux-mips64el": "0.19.4", + "@esbuild/linux-ppc64": "0.19.4", + "@esbuild/linux-riscv64": "0.19.4", + "@esbuild/linux-s390x": "0.19.4", + "@esbuild/linux-x64": "0.19.4", + "@esbuild/netbsd-x64": "0.19.4", + "@esbuild/openbsd-x64": "0.19.4", + "@esbuild/sunos-x64": "0.19.4", + "@esbuild/win32-arm64": "0.19.4", + "@esbuild/win32-ia32": "0.19.4", + "@esbuild/win32-x64": "0.19.4" + } + }, + "node_modules/fs-extra": { + "version": "10.0.1", + "resolved": "https://registry.npmjs.org/fs-extra/-/fs-extra-10.0.1.tgz", + "integrity": "sha512-NbdoVMZso2Lsrn/QwLXOy6rm0ufY2zEOKCDzJR/0kBsb0E6qed0P3iYK+Ath3BfvXEeu4JhEtXLgILx5psUfag==", + "dependencies": { + "graceful-fs": "^4.2.0", + "jsonfile": "^6.0.1", + "universalify": "^2.0.0" + }, + "engines": { + "node": ">=12" + } + }, + "node_modules/graceful-fs": { + "version": "4.2.11", + "resolved": "https://registry.npmjs.org/graceful-fs/-/graceful-fs-4.2.11.tgz", + "integrity": "sha512-RbJ5/jmFcNNCcDV5o9eTnBLJ/HszWV0P73bc+Ff4nS/rJj+YaS6IGyiOL0VoBYX+l1Wrl3k63h/KrH+nhJ0XvQ==" + }, + "node_modules/ieee754": { + "version": "1.2.1", + "resolved": "https://registry.npmjs.org/ieee754/-/ieee754-1.2.1.tgz", + "integrity": "sha512-dcyqhDvX1C46lXZcVqCpK+FtMRQVdIMN6/Df5js2zouUsqG7I6sFxitIC+7KYK29KdXOLHdu9zL4sFnoVQnqaA==", + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/inherits": { + "version": "2.0.4", + "resolved": "https://registry.npmjs.org/inherits/-/inherits-2.0.4.tgz", + "integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ==", + "dev": true + }, + "node_modules/iso-url": { + "version": "0.4.7", + "resolved": "https://registry.npmjs.org/iso-url/-/iso-url-0.4.7.tgz", + "integrity": "sha512-27fFRDnPAMnHGLq36bWTpKET+eiXct3ENlCcdcMdk+mjXrb2kw3mhBUg1B7ewAC0kVzlOPhADzQgz1SE6Tglog==", + "dev": true, + "engines": { + "node": ">=10" + } + }, + "node_modules/js-sha256": { + "version": "0.9.0", + "resolved": "https://registry.npmjs.org/js-sha256/-/js-sha256-0.9.0.tgz", + "integrity": "sha512-sga3MHh9sgQN2+pJ9VYZ+1LPwXOxuBJBA5nrR5/ofPfuiJBE2hnjsaN8se8JznOmGLN2p49Pe5U/ttafcs/apA==" + }, + "node_modules/json-text-sequence": { + "version": "0.1.1", + "resolved": "https://registry.npmjs.org/json-text-sequence/-/json-text-sequence-0.1.1.tgz", + "integrity": "sha512-L3mEegEWHRekSHjc7+sc8eJhba9Clq1PZ8kMkzf8OxElhXc8O4TS5MwcVlj9aEbm5dr81N90WHC5nAz3UO971w==", + "dev": true, + "dependencies": { + "delimit-stream": "0.1.0" + } + }, + "node_modules/jsonfile": { + "version": "6.1.0", + "resolved": "https://registry.npmjs.org/jsonfile/-/jsonfile-6.1.0.tgz", + "integrity": "sha512-5dgndWOriYSm5cnYaJNhalLNDKOqFwyDB/rr1E9ZsGciGvKPs8R2xYGCacuf3z6K1YKDz182fd+fY3cn3pMqXQ==", + "dependencies": { + "universalify": "^2.0.0" + }, + "optionalDependencies": { + "graceful-fs": "^4.1.6" + } + }, + "node_modules/make-error": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/make-error/-/make-error-1.3.6.tgz", + "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==" + }, + "node_modules/readable-stream": { + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", + "dev": true, + "dependencies": { + "inherits": "^2.0.3", + "string_decoder": "^1.1.1", + "util-deprecate": "^1.0.1" + }, + "engines": { + "node": ">= 6" + } + }, + "node_modules/safe-buffer": { + "version": "5.2.1", + "resolved": "https://registry.npmjs.org/safe-buffer/-/safe-buffer-5.2.1.tgz", + "integrity": "sha512-rp3So07KcdmmKbGvgaNxQSJr7bGVSVk5S9Eq1F+ppbRo70+YeaDxkw5Dd8NPN+GD6bjnYm2VuPuCXmpuYvmCXQ==", + "dev": true, + "funding": [ + { + "type": "github", + "url": "https://github.com/sponsors/feross" + }, + { + "type": "patreon", + "url": "https://www.patreon.com/feross" + }, + { + "type": "consulting", + "url": "https://feross.org/support" + } + ] + }, + "node_modules/simple-cbor": { + "version": "0.4.1", + "resolved": "https://registry.npmjs.org/simple-cbor/-/simple-cbor-0.4.1.tgz", + "integrity": "sha512-rijcxtwx2b4Bje3sqeIqw5EeW7UlOIC4YfOdwqIKacpvRQ/D78bWg/4/0m5e0U91oKvlGh7LlJuZCu07ISCC7w==", + "dev": true + }, + "node_modules/string_decoder": { + "version": "1.3.0", + "resolved": "https://registry.npmjs.org/string_decoder/-/string_decoder-1.3.0.tgz", + "integrity": "sha512-hkRX8U1WjJFd8LsDJ2yQ/wWWxaopEsABU1XfkM8A+j0+85JAGppt16cr1Whg6KIbb4okU6Mql6BOj+uup/wKeA==", + "dev": true, + "dependencies": { + "safe-buffer": "~5.2.0" + } + }, + "node_modules/text-encoding": { + "version": "0.7.0", + "resolved": "https://registry.npmjs.org/text-encoding/-/text-encoding-0.7.0.tgz", + "integrity": "sha512-oJQ3f1hrOnbRLOcwKz0Liq2IcrvDeZRHXhd9RgLrsT+DjWY/nty1Hi7v3dtkaEYbPYe0mUoOfzRrMwfXXwgPUA==", + "deprecated": "no longer maintained" + }, + "node_modules/ts-node": { + "version": "10.9.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", + "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==", + "dev": true, + "dependencies": { + "@cspotcode/source-map-support": "^0.8.0", + "@tsconfig/node10": "^1.0.7", + "@tsconfig/node12": "^1.0.7", + "@tsconfig/node14": "^1.0.0", + "@tsconfig/node16": "^1.0.2", + "acorn": "^8.4.1", + "acorn-walk": "^8.1.1", + "arg": "^4.1.0", + "create-require": "^1.1.0", + "diff": "^4.0.1", + "make-error": "^1.1.1", + "v8-compile-cache-lib": "^3.0.1", + "yn": "3.1.1" + }, + "bin": { + "ts-node": "dist/bin.js", + "ts-node-cwd": "dist/bin-cwd.js", + "ts-node-esm": "dist/bin-esm.js", + "ts-node-script": "dist/bin-script.js", + "ts-node-transpile-only": "dist/bin-transpile.js", + "ts-script": "dist/bin-script-deprecated.js" + }, + "peerDependencies": { + "@swc/core": ">=1.2.50", + "@swc/wasm": ">=1.2.50", + "@types/node": "*", + "typescript": ">=2.7" + }, + "peerDependenciesMeta": { + "@swc/core": { + "optional": true + }, + "@swc/wasm": { + "optional": true + } + } + }, + "node_modules/typescript": { + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", + "bin": { + "tsc": "bin/tsc", + "tsserver": "bin/tsserver" + }, + "engines": { + "node": ">=14.17" + } + }, + "node_modules/universalify": { + "version": "2.0.0", + "resolved": "https://registry.npmjs.org/universalify/-/universalify-2.0.0.tgz", + "integrity": "sha512-hAZsKq7Yy11Zu1DE0OzWjw7nnLZmJZYTDZZyEFHZdUhV8FkH5MCfoU1XMaxXovpyW5nq5scPqq0ZDP9Zyl04oQ==", + "engines": { + "node": ">= 10.0.0" + } + }, + "node_modules/util-deprecate": { + "version": "1.0.2", + "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", + "dev": true + }, + "node_modules/uuid": { + "version": "9.0.1", + "resolved": "https://registry.npmjs.org/uuid/-/uuid-9.0.1.tgz", + "integrity": "sha512-b+1eJOlsR9K8HJpow9Ok3fiWOWSIcIzXodvv0rQjVoOVNpWMpxf1wZNpt4y9h10odCNrqnYp1OBzRktckBe3sA==", + "funding": [ + "https://github.com/sponsors/broofa", + "https://github.com/sponsors/ctavan" + ], + "bin": { + "uuid": "dist/bin/uuid" + } + }, + "node_modules/v8-compile-cache-lib": { + "version": "3.0.1", + "resolved": "https://registry.npmjs.org/v8-compile-cache-lib/-/v8-compile-cache-lib-3.0.1.tgz", + "integrity": "sha512-wa7YjyUGfNZngI/vtK0UHAN+lgDCxBPCylVXGp0zu59Fz5aiGtNXaq3DhIov063MorB+VfufLh3JlF2KdTK3xg==", + "dev": true + }, + "node_modules/yn": { + "version": "3.1.1", + "resolved": "https://registry.npmjs.org/yn/-/yn-3.1.1.tgz", + "integrity": "sha512-Ux4ygGWsu2c7isFWe8Yu1YluJmqVhxqK2cLXNQA5AcC3QfbGNpM7fu0Y8b/z16pXLnFxZYvWhd3fhBY9DLmC6Q==", + "engines": { + "node": ">=6" + } + } + } +} diff --git a/examples/stable_json/package.json b/examples/stable_json/package.json new file mode 100644 index 0000000000..f6450ebd78 --- /dev/null +++ b/examples/stable_json/package.json @@ -0,0 +1,14 @@ +{ + "scripts": { + "pretest": "ts-node --transpile-only --ignore=false test/pretest.ts", + "test": "ts-node --transpile-only --ignore=false test/test.ts" + }, + "dependencies": { + "azle": "0.18.6" + }, + "devDependencies": { + "@dfinity/agent": "^0.19.2", + "ts-node": "^10.9.1", + "typescript": "^5.2.2" + } +} diff --git a/examples/stable_json/src/index.did b/examples/stable_json/src/index.did new file mode 100644 index 0000000000..4d27f94689 --- /dev/null +++ b/examples/stable_json/src/index.did @@ -0,0 +1,3 @@ +service: () -> { + test: () -> () query; +} diff --git a/examples/stable_json/src/index.ts b/examples/stable_json/src/index.ts new file mode 100644 index 0000000000..6fcda53358 --- /dev/null +++ b/examples/stable_json/src/index.ts @@ -0,0 +1,16 @@ +import { Canister, query, StableBTreeMap, StableJson, text, Void } from 'azle'; + +// function Json(value: T): T { +// return { +// toBytes +// }; +// } + +// let map = StableBTreeMap(text, text, 0); +// let map1 = StableBTreeMap(Json(text), Json(text), 0); + +export default Canister({ + test: query([], Void, () => { + // map1.insert('0', '0'); + }) +}); diff --git a/examples/stable_json/test/pretest.ts b/examples/stable_json/test/pretest.ts new file mode 100644 index 0000000000..dc27cd1bb9 --- /dev/null +++ b/examples/stable_json/test/pretest.ts @@ -0,0 +1,19 @@ +import { execSync } from 'child_process'; + +async function pretest() { + await new Promise((resolve) => setTimeout(resolve, 5000)); + + execSync(`dfx canister uninstall-code stable_json || true`, { + stdio: 'inherit' + }); + + execSync(`dfx deploy stable_json`, { + stdio: 'inherit' + }); + + execSync(`dfx generate stable_json`, { + stdio: 'inherit' + }); +} + +pretest(); diff --git a/examples/stable_json/test/test.ts b/examples/stable_json/test/test.ts new file mode 100644 index 0000000000..1792f5d37a --- /dev/null +++ b/examples/stable_json/test/test.ts @@ -0,0 +1,11 @@ +import { getCanisterId, runTests } from 'azle/test'; +import { createActor } from '../test/dfx_generated/stable_json'; +import { getTests } from './tests'; + +const stableJsonCanister = createActor(getCanisterId('stable_json'), { + agentOptions: { + host: 'http://127.0.0.1:8000' + } +}); + +runTests(getTests(stableJsonCanister)); diff --git a/examples/stable_json/test/tests.ts b/examples/stable_json/test/tests.ts new file mode 100644 index 0000000000..cd0a0939ed --- /dev/null +++ b/examples/stable_json/test/tests.ts @@ -0,0 +1,8 @@ +import { Principal } from '@dfinity/principal'; +import { ActorSubclass } from '@dfinity/agent'; +import { Test } from 'azle/test'; +import { _SERVICE } from './dfx_generated/stable_json/stable_json.did'; + +export function getTests(stableJsonCanister: ActorSubclass<_SERVICE>): Test[] { + return []; +} diff --git a/examples/stable_json/tsconfig.json b/examples/stable_json/tsconfig.json new file mode 100644 index 0000000000..2638f0d8bc --- /dev/null +++ b/examples/stable_json/tsconfig.json @@ -0,0 +1,9 @@ +{ + "compilerOptions": { + "strict": true, + "target": "ES2020", + "moduleResolution": "node", + "allowJs": true, + "outDir": "HACK_BECAUSE_OF_ALLOW_JS" + } +} diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index 8bfb3983f7..b203b82173 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -22,6 +22,7 @@ import { AzleNat32, nat32 } from './types/primitive/nats/nat32'; import { AzleNat64, nat64 } from './types/primitive/nats/nat64'; import { AzleResult, Result } from '../system_types'; import { Principal } from './types/reference/principal'; +import { StableJson } from '../stable_b_tree_map'; // TODO I believe we have some unnecessary cases and constructs in here now // TODO we probably don't need AzleTuple or AzleOpt @@ -117,4 +118,6 @@ export type TypeMapping = RecursionLevel extends 10 ? any : T extends typeof AzleEmpty ? empty + : T extends StableJson + ? U : T; diff --git a/src/lib/stable_b_tree_map.ts b/src/lib/stable_b_tree_map.ts index 66cdb6c131..100a10232f 100644 --- a/src/lib/stable_b_tree_map.ts +++ b/src/lib/stable_b_tree_map.ts @@ -4,11 +4,17 @@ import { nat64 } from './candid/types/primitive/nats/nat64'; import { nat8 } from './candid/types/primitive/nats/nat8'; import { encode, decode } from './candid/serde'; +// TODO we should probably allow the user to pass in their own types +// TODO when they want to use a custom serializable +// TODO perhaps we need a way to extend TypeMapping + // TODO we should probably try to make it work with bigint, Principal, etc // TODO out of the box // TODO we probably need to allow the user to pass in their own encoding/decoding for Json as well // TODO we need a way to make the types good in TypeMapping -export class StableJson { +export class StableJson { + static _azleKind: 'StableJson' = 'StableJson'; + static toBytes(data: any) { return Uint8Array.from(Buffer.from(JSON.stringify(data))); } From cbd43f29467b9cc37ee42162b8f8942200bd1eb0 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Tue, 7 Nov 2023 15:21:01 -0600 Subject: [PATCH 25/30] clean up remaining stuff before merge --- examples/audio_recorder/package-lock.json | 106 ++++++++++------------ examples/audio_recorder/package.json | 2 +- examples/audio_recorder/src/index.ts | 4 +- examples/audio_recorder/test/tests.ts | 7 +- property_tests/index.ts | 11 ++- src/lib/candid/candid_type.ts | 6 -- src/lib/canister_methods/methods/query.ts | 12 --- type_tests/index.ts | 72 --------------- type_tests/package-lock.json | 21 ----- type_tests/package.json | 5 - 10 files changed, 64 insertions(+), 182 deletions(-) delete mode 100644 type_tests/index.ts delete mode 100644 type_tests/package-lock.json delete mode 100644 type_tests/package.json diff --git a/examples/audio_recorder/package-lock.json b/examples/audio_recorder/package-lock.json index 369fe4f19e..796d702ead 100644 --- a/examples/audio_recorder/package-lock.json +++ b/examples/audio_recorder/package-lock.json @@ -8,7 +8,7 @@ "azle": "0.18.6" }, "devDependencies": { - "@dfinity/agent": "0.11.1", + "@dfinity/agent": "^0.19.3", "ts-node": "10.7.0", "typescript": "4.6.3" } @@ -33,35 +33,38 @@ } }, "node_modules/@dfinity/agent": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/@dfinity/agent/-/agent-0.11.1.tgz", - "integrity": "sha512-Z1zw8l3d+AG3uu7d8G/Rd9Q5MWT9gB+Cori/Rqb6IjSEribRhL36ulCSkDYZJU/dhqSUp1VlvX5u51+wgv+MLg==", + "version": "0.19.3", + "resolved": "https://registry.npmjs.org/@dfinity/agent/-/agent-0.19.3.tgz", + "integrity": "sha512-q410aNLoOA1ZkwdAMgSo6t++pjISn9TfSybRXhPRI5Ume7eG6+6qYr/rlvcXy7Nb2+Ar7LTsHNn34IULfjni7w==", "dev": true, "dependencies": { + "@noble/hashes": "^1.3.1", "base64-arraybuffer": "^0.2.0", - "bignumber.js": "^9.0.0", "borc": "^2.1.1", - "js-sha256": "0.9.0", "simple-cbor": "^0.4.1" }, "peerDependencies": { - "@dfinity/candid": "^0.11.1", - "@dfinity/principal": "^0.11.1" + "@dfinity/candid": "^0.19.3", + "@dfinity/principal": "^0.19.3" } }, "node_modules/@dfinity/candid": { - "version": "0.11.3", - "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-0.11.3.tgz", - "integrity": "sha512-xX7xNj2lLt7SIlvy0sqNp4fpcTD/xnwEu9APj0tnIF64cnsxIiS12T1Z8jl9g80jCQ1CbRPQf4cfsOfS3Cd2OA==", + "version": "0.19.3", + "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-0.19.3.tgz", + "integrity": "sha512-yXfbLSWTeRd4G0bLLxYoDqpXH3Jim0P+1PPZOoktXNC1X1hB+ea3yrZebX75t4GVoQK7123F7mxWHiPjuV2tQQ==", "dev": true, - "peer": true + "peer": true, + "peerDependencies": { + "@dfinity/principal": "^0.19.3" + } }, "node_modules/@dfinity/principal": { - "version": "0.11.3", - "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.11.3.tgz", - "integrity": "sha512-+AJGDJ+RsveybSdxuTQFr2DPNZFpPfXnyixAOFWWdElVniSwnO/SwqQChR0AWvJdy/fKqoAXK+ZzyLG0uqSetA==", - "dev": true, - "peer": true + "version": "0.19.3", + "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.19.3.tgz", + "integrity": "sha512-+nixVvdGt7ECxRvLXDXsvU9q9sSPssBtDQ4bXa149SK6gcYcmZ6lfWIi3DJNqj3tGROxILVBsguel9tECappsA==", + "dependencies": { + "@noble/hashes": "^1.3.1" + } }, "node_modules/@esbuild/android-arm": { "version": "0.19.4", @@ -686,14 +689,6 @@ "@dfinity/principal": "^0.19.2" } }, - "node_modules/azle/node_modules/@dfinity/principal": { - "version": "0.19.3", - "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.19.3.tgz", - "integrity": "sha512-+nixVvdGt7ECxRvLXDXsvU9q9sSPssBtDQ4bXa149SK6gcYcmZ6lfWIi3DJNqj3tGROxILVBsguel9tECappsA==", - "dependencies": { - "@noble/hashes": "^1.3.1" - } - }, "node_modules/azle/node_modules/buffer": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", @@ -798,9 +793,9 @@ ] }, "node_modules/bignumber.js": { - "version": "9.1.1", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.1.tgz", - "integrity": "sha512-pHm4LsMJ6lzgNGVfZHjMoO8sdoRhOzOH4MLmY65Jg70bpxCKu5iOHNJyfF6OyvYw7t8Fpf35RuzUyqnQsj8Vig==", + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.2.tgz", + "integrity": "sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==", "dev": true, "engines": { "node": "*" @@ -992,9 +987,9 @@ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==" }, "node_modules/readable-stream": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", - "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", "dev": true, "dependencies": { "inherits": "^2.0.3", @@ -1158,31 +1153,32 @@ } }, "@dfinity/agent": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/@dfinity/agent/-/agent-0.11.1.tgz", - "integrity": "sha512-Z1zw8l3d+AG3uu7d8G/Rd9Q5MWT9gB+Cori/Rqb6IjSEribRhL36ulCSkDYZJU/dhqSUp1VlvX5u51+wgv+MLg==", + "version": "0.19.3", + "resolved": "https://registry.npmjs.org/@dfinity/agent/-/agent-0.19.3.tgz", + "integrity": "sha512-q410aNLoOA1ZkwdAMgSo6t++pjISn9TfSybRXhPRI5Ume7eG6+6qYr/rlvcXy7Nb2+Ar7LTsHNn34IULfjni7w==", "dev": true, "requires": { + "@noble/hashes": "^1.3.1", "base64-arraybuffer": "^0.2.0", - "bignumber.js": "^9.0.0", "borc": "^2.1.1", - "js-sha256": "0.9.0", "simple-cbor": "^0.4.1" } }, "@dfinity/candid": { - "version": "0.11.3", - "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-0.11.3.tgz", - "integrity": "sha512-xX7xNj2lLt7SIlvy0sqNp4fpcTD/xnwEu9APj0tnIF64cnsxIiS12T1Z8jl9g80jCQ1CbRPQf4cfsOfS3Cd2OA==", + "version": "0.19.3", + "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-0.19.3.tgz", + "integrity": "sha512-yXfbLSWTeRd4G0bLLxYoDqpXH3Jim0P+1PPZOoktXNC1X1hB+ea3yrZebX75t4GVoQK7123F7mxWHiPjuV2tQQ==", "dev": true, - "peer": true + "peer": true, + "requires": {} }, "@dfinity/principal": { - "version": "0.11.3", - "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.11.3.tgz", - "integrity": "sha512-+AJGDJ+RsveybSdxuTQFr2DPNZFpPfXnyixAOFWWdElVniSwnO/SwqQChR0AWvJdy/fKqoAXK+ZzyLG0uqSetA==", - "dev": true, - "peer": true + "version": "0.19.3", + "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.19.3.tgz", + "integrity": "sha512-+nixVvdGt7ECxRvLXDXsvU9q9sSPssBtDQ4bXa149SK6gcYcmZ6lfWIi3DJNqj3tGROxILVBsguel9tECappsA==", + "requires": { + "@noble/hashes": "^1.3.1" + } }, "@esbuild/android-arm": { "version": "0.19.4", @@ -1480,14 +1476,6 @@ "from": "@dfinity/candid@github:demergent-labs/candid#minimum_viable", "requires": {} }, - "@dfinity/principal": { - "version": "0.19.3", - "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.19.3.tgz", - "integrity": "sha512-+nixVvdGt7ECxRvLXDXsvU9q9sSPssBtDQ4bXa149SK6gcYcmZ6lfWIi3DJNqj3tGROxILVBsguel9tECappsA==", - "requires": { - "@noble/hashes": "^1.3.1" - } - }, "buffer": { "version": "6.0.3", "resolved": "https://registry.npmjs.org/buffer/-/buffer-6.0.3.tgz", @@ -1535,9 +1523,9 @@ "integrity": "sha512-AKpaYlHn8t4SVbOHCy+b5+KKgvR4vrsD8vbvrbiQJps7fKDTkjkDry6ji0rUJjC0kzbNePLwzxq8iypo41qeWA==" }, "bignumber.js": { - "version": "9.1.1", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.1.tgz", - "integrity": "sha512-pHm4LsMJ6lzgNGVfZHjMoO8sdoRhOzOH4MLmY65Jg70bpxCKu5iOHNJyfF6OyvYw7t8Fpf35RuzUyqnQsj8Vig==", + "version": "9.1.2", + "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.1.2.tgz", + "integrity": "sha512-2/mKyZH9K85bzOEfhXDBFZTGd1CTs+5IHpeFQo9luiBG7hghdC851Pj2WAhb6E3R6b9tZj/XKhbg4fum+Kepug==", "dev": true }, "borc": { @@ -1677,9 +1665,9 @@ "integrity": "sha512-s8UhlNe7vPKomQhC1qFelMokr/Sc3AgNbso3n74mVPA5LTZwkB9NlXf4XPamLxJE8h0gh73rM94xvwRT2CVInw==" }, "readable-stream": { - "version": "3.6.0", - "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.0.tgz", - "integrity": "sha512-BViHy7LKeTz4oNnkcLJ+lVSL6vpiFeX6/d3oSH8zCW7UxP2onchk+vTGB143xuFjHS3deTgkKoXXymXqymiIdA==", + "version": "3.6.2", + "resolved": "https://registry.npmjs.org/readable-stream/-/readable-stream-3.6.2.tgz", + "integrity": "sha512-9u/sniCrY3D5WdsERHzHE4G2YCXqoG5FTHUiCC4SIbr6XcLZBY05ya9EKjYek9O5xOAwjGq+1JdGBAS7Q9ScoA==", "dev": true, "requires": { "inherits": "^2.0.3", diff --git a/examples/audio_recorder/package.json b/examples/audio_recorder/package.json index 4dc76667ab..9680020133 100644 --- a/examples/audio_recorder/package.json +++ b/examples/audio_recorder/package.json @@ -7,7 +7,7 @@ "azle": "0.18.6" }, "devDependencies": { - "@dfinity/agent": "0.11.1", + "@dfinity/agent": "^0.19.3", "ts-node": "10.7.0", "typescript": "4.6.3" } diff --git a/examples/audio_recorder/src/index.ts b/examples/audio_recorder/src/index.ts index cc9747015e..164233578d 100644 --- a/examples/audio_recorder/src/index.ts +++ b/examples/audio_recorder/src/index.ts @@ -17,14 +17,14 @@ import { Vec } from 'azle'; -const User = Record({ +export const User = Record({ id: Principal, createdAt: nat64, recordingIds: Vec(Principal), username: text }); -const Recording = Record({ +export const Recording = Record({ id: Principal, audio: blob, createdAt: nat64, diff --git a/examples/audio_recorder/test/tests.ts b/examples/audio_recorder/test/tests.ts index 36d15a935c..114964f173 100644 --- a/examples/audio_recorder/test/tests.ts +++ b/examples/audio_recorder/test/tests.ts @@ -1,11 +1,12 @@ import { ok, Test } from 'azle/test'; +import { User, Recording } from '../src/index'; import { _SERVICE } from './dfx_generated/audio_recorder/audio_recorder.did'; import { ActorSubclass } from '@dfinity/agent'; // TODO to be more thorough we could test all of the error cases as well -let global_user: any; -let global_recording: any; +let global_user: typeof User; +let global_recording: typeof Recording; export function get_tests( audio_recorder_canister: ActorSubclass<_SERVICE> @@ -43,7 +44,7 @@ export function get_tests( const recording = result.Ok; - global_recording = recording; + global_recording = recording as any; return { Ok: diff --git a/property_tests/index.ts b/property_tests/index.ts index cbbc45deb8..1a88fd7dc3 100644 --- a/property_tests/index.ts +++ b/property_tests/index.ts @@ -32,10 +32,19 @@ export function runPropTests(testArb: fc.Arbitrary) { stdio: 'inherit' }); - return await runTests( + const result = await runTests( canister.tests, process.env.AZLE_PROPTEST_VERBOSE !== 'true' ); + + execSync( + `node_modules/.bin/tsc --noEmit --skipLibCheck --target es2020 --strict --moduleResolution node --allowJs`, + { + stdio: 'inherit' + } + ); + + return result; }), { numRuns: Number(process.env.AZLE_PROPTEST_NUM_RUNS ?? 1), diff --git a/src/lib/candid/candid_type.ts b/src/lib/candid/candid_type.ts index 008f33a774..e53504df8e 100644 --- a/src/lib/candid/candid_type.ts +++ b/src/lib/candid/candid_type.ts @@ -1,9 +1,3 @@ -import { Serializable } from '../stable_b_tree_map'; - -// export type CandidType = { -// _azleCandidType?: '_azleCandidType'; -// } & Partial; - export interface CandidType { _azleCandidType?: '_azleCandidType'; } diff --git a/src/lib/canister_methods/methods/query.ts b/src/lib/canister_methods/methods/query.ts index 3ac6d1c853..8b7b0ffb98 100644 --- a/src/lib/canister_methods/methods/query.ts +++ b/src/lib/canister_methods/methods/query.ts @@ -6,18 +6,6 @@ import { executeMethod } from '../execute_method'; import { isAsync } from '../is_async'; import { MethodArgs } from '../types/method_args'; -// type IfHasKey = K extends keyof T -// ? undefined extends T[K] -// ? Yes -// : No -// : No; - -// type Test1 = IfHasKey; - -// interface CandidType { -// _azleCandidType?: '_azleCandidType'; -// } - export function query< const Params extends ReadonlyArray, Return extends CandidType, diff --git a/type_tests/index.ts b/type_tests/index.ts deleted file mode 100644 index a7cdef0556..0000000000 --- a/type_tests/index.ts +++ /dev/null @@ -1,72 +0,0 @@ -import { - nat64, - query, - text, - StableJson, - Record, - Serializable, - Void -} from '../src/lib'; -import { CandidType } from '../src/lib/candid/candid_type'; -// import { expectTypeOf } from 'expect-type'; -import { TypeMapping } from '../src/lib/candid/type_mapping'; - -// export function typeMapping(value: T): TypeMapping { -// return value as any; -// } - -// expectTypeOf(0).not.toMatchTypeOf; -// expectTypeOf(0n).not.toMatchTypeOf; -// expectTypeOf('').not.toMatchTypeOf; - -// expectTypeOf({}).not.toMatchTypeOf; // TODO this is wrong - -// expectTypeOf(text).toMatchTypeOf; -// expectTypeOf(text).toMatchTypeOf; -// expectTypeOf(StableJson).not.toMatchTypeOf; // TODO this is wrong -// expectTypeOf(StableJson).toMatchTypeOf; // TODO this is wrong - -// const TestText: CandidType = text; - -// // @ts-expect-error -// const TestStableJson: CandidType = StableJson; - -// const ExampleRecord = Record({ -// text: text, -// nat64: nat64 -// }); - -// const TestExampleRecord: { -// text: string; -// nat64: bigint; -// } = ExampleRecord; - -// const ExampleRecordInstance: typeof ExampleRecord = { -// text: '', -// nat64: 0n -// }; - -// query([text], Void); - -// query([StableJson], Void); - -// type HasOptionalField = undefined extends T[K] -// ? true -// : false; - -// type Test = HasOptionalField; - -// type IfHasKey = K extends keyof T -// ? undefined extends T[K] -// ? Yes -// : No -// : No; - -// type Test1 = IfHasKey; - -// type - -// expectTypeOf(TestRecord).toEqualTypeOf<{ -// text: string; -// nat64: bigint; -// }>; diff --git a/type_tests/package-lock.json b/type_tests/package-lock.json deleted file mode 100644 index b5830bc2a2..0000000000 --- a/type_tests/package-lock.json +++ /dev/null @@ -1,21 +0,0 @@ -{ - "name": "type_tests", - "lockfileVersion": 3, - "requires": true, - "packages": { - "": { - "devDependencies": { - "expect-type": "^0.17.3" - } - }, - "node_modules/expect-type": { - "version": "0.17.3", - "resolved": "https://registry.npmjs.org/expect-type/-/expect-type-0.17.3.tgz", - "integrity": "sha512-K0ZdZJ97jiAtaOwhEHHz/f0N6Xbj5reRz5g6+5BO7+OvqQ7PMQz0/c8bFSJs1zPotNJL5HJaC6t6lGPEAtGyOw==", - "dev": true, - "engines": { - "node": ">=12.0.0" - } - } - } -} diff --git a/type_tests/package.json b/type_tests/package.json deleted file mode 100644 index b8844944c3..0000000000 --- a/type_tests/package.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "devDependencies": { - "expect-type": "^0.17.3" - } -} From 9b39e915e24512653706016ccd16d88f4dc31569 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 8 Nov 2023 14:48:52 -0600 Subject: [PATCH 26/30] working on stable_json, adding explicit type parameters for StableBTreeMap --- examples/audio_recorder/src/index.ts | 7 +- examples/pre_and_post_upgrade/src/index.ts | 3 +- examples/stable_json/package-lock.json | 3 +- examples/stable_json/package.json | 3 +- examples/stable_json/src/index.did | 3 +- examples/stable_json/src/index.ts | 56 ++++++-- .../src/canister3/stable_map_16.ts | 2 +- .../src/canister3/stable_map_17.ts | 2 +- .../src/ic/stable_b_tree_map_values.rs | 27 +++- src/lib/candid/type_mapping.ts | 3 - src/lib/candid/types/constructed/opt.ts | 2 +- src/lib/candid/types/constructed/record.ts | 2 +- src/lib/candid/types/constructed/tuple.ts | 2 +- src/lib/candid/types/constructed/variant.ts | 2 +- src/lib/candid/types/reference/func.ts | 2 +- src/lib/ic/types/azle_ic.ts | 36 +++-- src/lib/index.ts | 3 +- .../stable_b_tree_map.ts | 93 +++++-------- src/lib/stable_structures/stable_json.ts | 125 ++++++++++++++++++ type_tests/assert_type.ts | 2 +- type_tests/index.ts | 2 + .../stable_structures/stable_b_tree_map.ts | 49 +++++++ 22 files changed, 317 insertions(+), 112 deletions(-) rename src/lib/{ => stable_structures}/stable_b_tree_map.ts (68%) create mode 100644 src/lib/stable_structures/stable_json.ts create mode 100644 type_tests/index.ts create mode 100644 type_tests/stable_structures/stable_b_tree_map.ts diff --git a/examples/audio_recorder/src/index.ts b/examples/audio_recorder/src/index.ts index 164233578d..f37142feed 100644 --- a/examples/audio_recorder/src/index.ts +++ b/examples/audio_recorder/src/index.ts @@ -23,6 +23,7 @@ export const User = Record({ recordingIds: Vec(Principal), username: text }); +type User = typeof User; export const Recording = Record({ id: Principal, @@ -31,14 +32,16 @@ export const Recording = Record({ name: text, userId: Principal }); +type Recording = typeof Recording; const AudioRecorderError = Variant({ RecordingDoesNotExist: Principal, UserDoesNotExist: Principal }); +type AudioRecorderError = typeof AudioRecorderError; -let users = StableBTreeMap(Principal, User, 0); -let recordings = StableBTreeMap(Principal, Recording, 1); +let users = StableBTreeMap(Principal, User, 0); +let recordings = StableBTreeMap(Principal, Recording, 1); export default Canister({ createUser: update([text], User, (username) => { diff --git a/examples/pre_and_post_upgrade/src/index.ts b/examples/pre_and_post_upgrade/src/index.ts index 726feb50d7..d2770f42ab 100644 --- a/examples/pre_and_post_upgrade/src/index.ts +++ b/examples/pre_and_post_upgrade/src/index.ts @@ -17,8 +17,9 @@ const Entry = Record({ key: text, value: nat64 }); +type Entry = typeof Entry; -let stableStorage = StableBTreeMap(text, Vec(Entry), 0); +let stableStorage = StableBTreeMap>(text, Vec(Entry), 0); let entries: { [key: string]: nat64; diff --git a/examples/stable_json/package-lock.json b/examples/stable_json/package-lock.json index d69cae788b..7239a5c4e3 100644 --- a/examples/stable_json/package-lock.json +++ b/examples/stable_json/package-lock.json @@ -5,7 +5,8 @@ "packages": { "": { "dependencies": { - "azle": "0.18.6" + "azle": "0.18.6", + "uuid": "^9.0.1" }, "devDependencies": { "@dfinity/agent": "^0.19.2", diff --git a/examples/stable_json/package.json b/examples/stable_json/package.json index f6450ebd78..874f68b507 100644 --- a/examples/stable_json/package.json +++ b/examples/stable_json/package.json @@ -4,7 +4,8 @@ "test": "ts-node --transpile-only --ignore=false test/test.ts" }, "dependencies": { - "azle": "0.18.6" + "azle": "0.18.6", + "uuid": "^9.0.1" }, "devDependencies": { "@dfinity/agent": "^0.19.2", diff --git a/examples/stable_json/src/index.did b/examples/stable_json/src/index.did index 4d27f94689..3deded9b0c 100644 --- a/examples/stable_json/src/index.did +++ b/examples/stable_json/src/index.did @@ -1,3 +1,4 @@ service: () -> { - test: () -> () query; + insert: () -> (); + values: () -> (vec record {id:principal; age:nat; signature:vec nat8; username:text}) query; } diff --git a/examples/stable_json/src/index.ts b/examples/stable_json/src/index.ts index 6fcda53358..959037a6a4 100644 --- a/examples/stable_json/src/index.ts +++ b/examples/stable_json/src/index.ts @@ -1,16 +1,52 @@ -import { Canister, query, StableBTreeMap, StableJson, text, Void } from 'azle'; +// TODO get rid of unnecessary memory id candid bytes converions +// TODO get rid of saying candid_bytes in Rust +// TODO add type tests for stablebtreemap +// TODO the stableType doesn't work for Func +// TODO make sure Serializable is actually working well +// TODO seems like only one of the properties actually needs to be there -// function Json(value: T): T { -// return { -// toBytes -// }; -// } +import { + blob, + Canister, + nat, + nat32, + Principal, + query, + Record, + Serializable, + StableJson, + StableBTreeMap, + text, + update, + Vec, + Void, + Func, + Opt +} from 'azle'; +import { v1 } from 'uuid'; -// let map = StableBTreeMap(text, text, 0); -// let map1 = StableBTreeMap(Json(text), Json(text), 0); +const User = Record({ + id: Principal, + username: text, + age: nat, + signature: blob +}); +type User = typeof User; + +let map = StableBTreeMap(User, StableJson(), 0); export default Canister({ - test: query([], Void, () => { - // map1.insert('0', '0'); + insert: update([], Void, () => { + for (let i = 0; i < 1_000; i++) { + map.insert(v1(), { + id: Principal.fromText('aaaaa-aa'), + username: i.toString(), + age: BigInt(i), + signature: Uint8Array.from([0, 1, 2, 3, 4, 5]) + }); + } + }), + values: query([], Vec(User), () => { + return map.values(0, 5); }) }); diff --git a/examples/stable_structures/src/canister3/stable_map_16.ts b/examples/stable_structures/src/canister3/stable_map_16.ts index cac0f0d6fd..82b148d70c 100644 --- a/examples/stable_structures/src/canister3/stable_map_16.ts +++ b/examples/stable_structures/src/canister3/stable_map_16.ts @@ -13,7 +13,7 @@ import { Vec } from 'azle'; -let stableMap16 = StableBTreeMap(text, StableJson, 16); +let stableMap16 = StableBTreeMap(text, StableJson(), 16); export const stableMap16Methods = { stableMap16ContainsKey: query([text], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_17.ts b/examples/stable_structures/src/canister3/stable_map_17.ts index ac7c9f5074..348cb39156 100644 --- a/examples/stable_structures/src/canister3/stable_map_17.ts +++ b/examples/stable_structures/src/canister3/stable_map_17.ts @@ -11,7 +11,7 @@ import { Vec } from 'azle'; -let stableMap17 = StableBTreeMap(StableJson, text, 17); +let stableMap17 = StableBTreeMap(StableJson(), text, 17); export const stableMap17Methods = { stableMap17ContainsKey: query([text], bool, (key) => { diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_values.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_values.rs index 54161a4952..5a858d6fd6 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_values.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_values.rs @@ -9,24 +9,41 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) - .expect("stable_b_tree_map_get argument 0 is undefined") + .expect("stable_b_tree_map_values argument 0 is undefined") + .to_js_value()? + .try_into()?; + + let start_index: usize = args + .get(1) + .expect("stable_b_tree_map_values argument 1 is undefined") + .to_js_value()? + .try_into()?; + + let length: usize = args + .get(2) + .expect("stable_b_tree_map_values argument 2 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let values: Vec> = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); + let stable_b_tree_map = &stable_b_tree_maps[&(memory_id as u8)]; - stable_b_tree_maps[&memory_id] + stable_b_tree_map .iter() + .skip(start_index) + .take(if length == 0 { + stable_b_tree_map.len().try_into().unwrap() + } else { + length + }) .map(|(_, value)| value.candid_bytes) .collect() }); let js_values: Vec = values.into_iter().map(|value| value.into()).collect(); - let js_value: JSValue = js_values.into(); to_qjs_value(&context, &js_value) diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index b203b82173..8bfb3983f7 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -22,7 +22,6 @@ import { AzleNat32, nat32 } from './types/primitive/nats/nat32'; import { AzleNat64, nat64 } from './types/primitive/nats/nat64'; import { AzleResult, Result } from '../system_types'; import { Principal } from './types/reference/principal'; -import { StableJson } from '../stable_b_tree_map'; // TODO I believe we have some unnecessary cases and constructs in here now // TODO we probably don't need AzleTuple or AzleOpt @@ -118,6 +117,4 @@ export type TypeMapping = RecursionLevel extends 10 ? any : T extends typeof AzleEmpty ? empty - : T extends StableJson - ? U : T; diff --git a/src/lib/candid/types/constructed/opt.ts b/src/lib/candid/types/constructed/opt.ts index 992a9956e4..9ac96b2ceb 100644 --- a/src/lib/candid/types/constructed/opt.ts +++ b/src/lib/candid/types/constructed/opt.ts @@ -1,4 +1,4 @@ -import { Serializable } from '../../../stable_b_tree_map'; +import { Serializable } from '../../../stable_structures/stable_b_tree_map'; import { CandidType } from '../../candid_type'; import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; diff --git a/src/lib/candid/types/constructed/record.ts b/src/lib/candid/types/constructed/record.ts index f8f21b12c2..2f7fde1059 100644 --- a/src/lib/candid/types/constructed/record.ts +++ b/src/lib/candid/types/constructed/record.ts @@ -5,7 +5,7 @@ import { TypeMapping } from '../../type_mapping'; import { Parent } from '../../to_idl'; import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; -import { Serializable } from '../../../stable_b_tree_map'; +import { Serializable } from '../../../stable_structures/stable_b_tree_map'; export function Record< T extends { diff --git a/src/lib/candid/types/constructed/tuple.ts b/src/lib/candid/types/constructed/tuple.ts index 1707be56a8..8a7e98b18b 100644 --- a/src/lib/candid/types/constructed/tuple.ts +++ b/src/lib/candid/types/constructed/tuple.ts @@ -4,7 +4,7 @@ import { IDL } from '@dfinity/candid'; import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; import { TypeMapping } from '../../type_mapping'; -import { Serializable } from '../../../stable_b_tree_map'; +import { Serializable } from '../../../stable_structures/stable_b_tree_map'; export class AzleTuple { constructor(t: CandidType[]) { diff --git a/src/lib/candid/types/constructed/variant.ts b/src/lib/candid/types/constructed/variant.ts index cb915a890e..97820f33c1 100644 --- a/src/lib/candid/types/constructed/variant.ts +++ b/src/lib/candid/types/constructed/variant.ts @@ -4,7 +4,7 @@ import { toIdlMap, CandidMap } from './to_idl_map'; import { IDL } from '@dfinity/candid'; import { decode } from '../../serde/decode'; import { encode } from '../../serde/encode'; -import { Serializable } from '../../../stable_b_tree_map'; +import { Serializable } from '../../../stable_structures/stable_b_tree_map'; export function Variant< T extends { diff --git a/src/lib/candid/types/reference/func.ts b/src/lib/candid/types/reference/func.ts index e372b523c3..f63cecb4c8 100644 --- a/src/lib/candid/types/reference/func.ts +++ b/src/lib/candid/types/reference/func.ts @@ -3,7 +3,7 @@ import { IDL } from '@dfinity/candid'; import { Principal } from './principal'; import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; -import { Serializable } from '../../../stable_b_tree_map'; +import { Serializable } from '../../../stable_structures/stable_b_tree_map'; type Mode = 'query' | 'update' | 'oneway'; diff --git a/src/lib/ic/types/azle_ic.ts b/src/lib/ic/types/azle_ic.ts index 169eb0edcf..c915aab141 100644 --- a/src/lib/ic/types/azle_ic.ts +++ b/src/lib/ic/types/azle_ic.ts @@ -86,33 +86,31 @@ export type AzleIc = { notify: () => never; reply: () => never; // Stable B Tree Map Functions - stableBTreeMapInit: (candidEncodedMemoryId: ArrayBufferLike) => void; + stableBTreeMapInit: (memoryId: number) => void; stableBTreeMapContainsKey: ( - candidEncodedMemoryId: ArrayBufferLike, - candidEncodedKey: ArrayBufferLike + memoryId: number, + encodedKey: ArrayBufferLike ) => boolean; stableBTreeMapGet: ( - candidEncodedMemoryId: ArrayBufferLike, - candidEncodedKey: ArrayBufferLike + memoryId: number, + encodedKey: ArrayBufferLike ) => ArrayBuffer | undefined; stableBTreeMapInsert: ( - candidEncodedMemoryId: ArrayBufferLike, - candidEncodedKey: ArrayBufferLike, - candidEncodedValue: ArrayBufferLike + memoryId: number, + encodedKey: ArrayBufferLike, + encodedValue: ArrayBufferLike ) => ArrayBuffer | undefined; - stableBTreeMapIsEmpty: (candidEncodedMemoryId: ArrayBuffer) => boolean; - stableBTreeMapItems: ( - candidEncodedMemoryId: ArrayBufferLike - ) => [ArrayBuffer, ArrayBuffer][]; - stableBTreeMapKeys: ( - candidEncodedMemoryId: ArrayBufferLike - ) => ArrayBuffer[]; - stableBTreeMapLen: (candidEncodedMemoryId: ArrayBufferLike) => ArrayBuffer; + stableBTreeMapIsEmpty: (memoryId: number) => boolean; + stableBTreeMapItems: (memoryId: number) => [ArrayBuffer, ArrayBuffer][]; + stableBTreeMapKeys: (memoryId: number) => ArrayBuffer[]; + stableBTreeMapLen: (memoryId: number) => ArrayBuffer; stableBTreeMapRemove( - candidEncodedMemoryId: ArrayBufferLike, - candidEncodedKey: ArrayBufferLike + memoryId: number, + encodedKey: ArrayBufferLike ): ArrayBuffer; stableBTreeMapValues: ( - candidEncodedMemoryId: ArrayBufferLike + memoryId: number, + startIndex: number, + length: number ) => ArrayBuffer[]; }; diff --git a/src/lib/index.ts b/src/lib/index.ts index 96c0973c63..27a38557b0 100644 --- a/src/lib/index.ts +++ b/src/lib/index.ts @@ -3,5 +3,6 @@ import '../../type_tests'; export * from './candid'; export * from './canister_methods'; export * from './ic'; -export * from './stable_b_tree_map'; +export * from './stable_structures/stable_b_tree_map'; +export * from './stable_structures/stable_json'; export * from './system_types'; diff --git a/src/lib/stable_b_tree_map.ts b/src/lib/stable_structures/stable_b_tree_map.ts similarity index 68% rename from src/lib/stable_b_tree_map.ts rename to src/lib/stable_structures/stable_b_tree_map.ts index 100a10232f..27e54b8051 100644 --- a/src/lib/stable_b_tree_map.ts +++ b/src/lib/stable_structures/stable_b_tree_map.ts @@ -1,43 +1,20 @@ -import { TypeMapping } from './candid/type_mapping'; -import { None, Opt, Some } from './candid/types/constructed/opt'; -import { nat64 } from './candid/types/primitive/nats/nat64'; -import { nat8 } from './candid/types/primitive/nats/nat8'; -import { encode, decode } from './candid/serde'; - -// TODO we should probably allow the user to pass in their own types -// TODO when they want to use a custom serializable -// TODO perhaps we need a way to extend TypeMapping - -// TODO we should probably try to make it work with bigint, Principal, etc -// TODO out of the box -// TODO we probably need to allow the user to pass in their own encoding/decoding for Json as well -// TODO we need a way to make the types good in TypeMapping -export class StableJson { - static _azleKind: 'StableJson' = 'StableJson'; - - static toBytes(data: any) { - return Uint8Array.from(Buffer.from(JSON.stringify(data))); - } - - static fromBytes(bytes: Uint8Array) { - return JSON.parse(Buffer.from(bytes).toString()); - } -} +import { None, Opt, Some } from '../candid/types/constructed/opt'; +import { nat64 } from '../candid/types/primitive/nats/nat64'; +import { nat8 } from '../candid/types/primitive/nats/nat8'; +import { encode, decode } from '../candid/serde'; export interface Serializable { toBytes: (data: any) => Uint8Array; fromBytes: (bytes: Uint8Array) => any; } -export function StableBTreeMap< - Key extends Partial, - Value extends Partial ->(keyType: Key, valueType: Value, memoryId: nat8) { - // TODO we don't really need to candid encode this, it's just a number - const candidEncodedMemoryId = encode(nat8, memoryId).buffer; - +export function StableBTreeMap( + keyType: Partial, + valueType: Partial, + memoryId: nat8 +) { if (globalThis._azleIc !== undefined) { - globalThis._azleIc.stableBTreeMapInit(candidEncodedMemoryId); + globalThis._azleIc.stableBTreeMapInit(memoryId); } isSerializable(keyType); @@ -49,7 +26,7 @@ export function StableBTreeMap< * @param key the key to check. * @returns `true` if the key exists in the map, `false` otherwise. */ - containsKey(key: TypeMapping): boolean { + containsKey(key: Key): boolean { if (globalThis._azleIc === undefined) { return undefined as any; } @@ -57,7 +34,7 @@ export function StableBTreeMap< const encodedKey = keyType.toBytes(key).buffer; return globalThis._azleIc.stableBTreeMapContainsKey( - candidEncodedMemoryId, + memoryId, encodedKey ); }, @@ -66,7 +43,7 @@ export function StableBTreeMap< * @param key the location from which to retrieve. * @returns the value associated with the given key, if it exists. */ - get(key: TypeMapping): Opt> { + get(key: Key): Opt { if (globalThis._azleIc === undefined) { return undefined as any; } @@ -74,7 +51,7 @@ export function StableBTreeMap< const encodedKey = keyType.toBytes(key).buffer; const encodedResult = globalThis._azleIc.stableBTreeMapGet( - candidEncodedMemoryId, + memoryId, encodedKey ); @@ -90,10 +67,7 @@ export function StableBTreeMap< * @param value the value to insert. * @returns the previous value of the key, if present. */ - insert( - key: TypeMapping, - value: TypeMapping - ): Opt> { + insert(key: Key, value: Value): Opt { if (globalThis._azleIc === undefined) { return undefined as any; } @@ -102,7 +76,7 @@ export function StableBTreeMap< const encodedValue = valueType.toBytes(value).buffer; const encodedResult = globalThis._azleIc.stableBTreeMapInsert( - candidEncodedMemoryId, + memoryId, encodedKey, encodedValue ); @@ -122,22 +96,19 @@ export function StableBTreeMap< return undefined as any; } - return globalThis._azleIc.stableBTreeMapIsEmpty( - candidEncodedMemoryId - ); + return globalThis._azleIc.stableBTreeMapIsEmpty(memoryId); }, /** * Retrieves the items in the map in sorted order. * @returns tuples representing key/value pairs. */ - items(): [TypeMapping, TypeMapping][] { + items(): [Key, Value][] { if (globalThis._azleIc === undefined) { return undefined as any; } - const encodedItems = globalThis._azleIc.stableBTreeMapItems( - candidEncodedMemoryId - ); + const encodedItems = + globalThis._azleIc.stableBTreeMapItems(memoryId); // TODO too much copying return encodedItems.map(([encodedKey, encodedValue]) => { @@ -151,14 +122,12 @@ export function StableBTreeMap< * The keys for each element in the map in sorted order. * @returns they keys in the map. */ - keys(): TypeMapping[] { + keys(): Key[] { if (globalThis._azleIc === undefined) { return undefined as any; } - const encodedKeys = globalThis._azleIc.stableBTreeMapKeys( - candidEncodedMemoryId - ); + const encodedKeys = globalThis._azleIc.stableBTreeMapKeys(memoryId); // TODO too much copying return encodedKeys.map((encodedKey) => { @@ -174,10 +143,10 @@ export function StableBTreeMap< return undefined as any; } - const candidEncodedLen = globalThis._azleIc.stableBTreeMapLen( - candidEncodedMemoryId - ); + const candidEncodedLen = + globalThis._azleIc.stableBTreeMapLen(memoryId); + // TODO let's try just using a simple string instead of decode considering how expensive decode is return decode(nat64, candidEncodedLen); }, /** @@ -185,7 +154,7 @@ export function StableBTreeMap< * @param key the location from which to remove. * @returns the previous value at the key if it exists, `null` otherwise. */ - remove(key: TypeMapping): Opt> { + remove(key: Key): Opt { if (globalThis._azleIc === undefined) { return undefined as any; } @@ -193,7 +162,7 @@ export function StableBTreeMap< const encodedKey = keyType.toBytes(key).buffer; const encodedValue = globalThis._azleIc.stableBTreeMapRemove( - candidEncodedMemoryId, + memoryId, encodedKey ); @@ -205,15 +174,19 @@ export function StableBTreeMap< }, /** * The values in the map in sorted order. + * @param startIndex the starting index to begin retrieval + * @param length the number of values to retrieve * @returns the values in the map. */ - values(): TypeMapping[] { + values(startIndex: number = 0, length: number = 0): Value[] { if (globalThis._azleIc === undefined) { return undefined as any; } const encodedValues = globalThis._azleIc.stableBTreeMapValues( - candidEncodedMemoryId + memoryId, + startIndex, + length ); // TODO too much copying diff --git a/src/lib/stable_structures/stable_json.ts b/src/lib/stable_structures/stable_json.ts new file mode 100644 index 0000000000..879adaff8c --- /dev/null +++ b/src/lib/stable_structures/stable_json.ts @@ -0,0 +1,125 @@ +import { Serializable } from './stable_b_tree_map'; +import { Principal } from '../candid/types/reference/principal'; + +export function StableJson(options?: { + replacer?: typeof replacer; + reviver?: typeof reviver; +}): Serializable { + return { + toBytes(data: any) { + return Uint8Array.from( + Buffer.from(JSON.stringify(data, options?.replacer ?? replacer)) + ); + }, + fromBytes(bytes: Uint8Array) { + return JSON.parse( + Buffer.from(bytes).toString(), + options?.reviver ?? reviver + ); + } + }; +} + +export function replacer(_key: string, value: any): any { + if (typeof value === 'bigint') { + return { + __bigint__: value.toString() + }; + } + + if (value instanceof Int8Array) { + return { + __int8array__: Array.from(value) + }; + } + + if (value instanceof Int16Array) { + return { + __int16array__: Array.from(value) + }; + } + + if (value instanceof Int32Array) { + return { + __int32array__: Array.from(value) + }; + } + + if (value instanceof BigInt64Array) { + return { + __bigint64array__: Array.from(value) + }; + } + + if (value instanceof Uint8Array) { + return { + __uint8array__: Array.from(value) + }; + } + + if (value instanceof Uint16Array) { + return { + __uint16array__: Array.from(value) + }; + } + + if (value instanceof Uint32Array) { + return { + __uint32array__: Array.from(value) + }; + } + + if (value instanceof BigUint64Array) { + return { + __biguint64array__: Array.from(value) + }; + } + + return value; +} + +export function reviver(_key: string, value: any): any { + if (typeof value === 'object') { + if (typeof value.__bigint__ === 'string') { + return BigInt(value.__bigint__); + } + + if (typeof value.__principal__ === 'string') { + return Principal.fromText(value.__principal__); + } + + if (typeof value.__int8array__ === 'object') { + return Int8Array.from(value.__int8array__); + } + + if (typeof value.__int16array__ === 'object') { + return Int16Array.from(value.__int16array__); + } + + if (typeof value.__int32array__ === 'object') { + return Int32Array.from(value.__int32array__); + } + + if (typeof value.__bigint64array__ === 'object') { + return BigInt64Array.from(value.__bigint64array__); + } + + if (typeof value.__uint8array__ === 'object') { + return Uint8Array.from(value.__uint8array__); + } + + if (typeof value.__uint16array__ === 'object') { + return Uint16Array.from(value.__uint16array__); + } + + if (typeof value.__uint32array__ === 'object') { + return Uint32Array.from(value.__uint32array__); + } + + if (typeof value.__biguint64array__ === 'object') { + return BigUint64Array.from(value.__biguint64array__); + } + } + + return value; +} diff --git a/type_tests/assert_type.ts b/type_tests/assert_type.ts index 089b6c48e7..b0e8488486 100644 --- a/type_tests/assert_type.ts +++ b/type_tests/assert_type.ts @@ -1,5 +1,5 @@ import { CandidType } from '../src/lib/candid/candid_type'; -import { Serializable } from '../src/lib/stable_b_tree_map'; +import { Serializable } from '../src/lib/stable_structures/stable_b_tree_map'; export type IsAny = 0 extends 1 & T ? true : false; export type IsExact = [T] extends [U] diff --git a/type_tests/index.ts b/type_tests/index.ts new file mode 100644 index 0000000000..4db07d560d --- /dev/null +++ b/type_tests/index.ts @@ -0,0 +1,2 @@ +// This file is here to allow importing the type_tests directory in azle/src/lib/index.ts +// For type checking purposes diff --git a/type_tests/stable_structures/stable_b_tree_map.ts b/type_tests/stable_structures/stable_b_tree_map.ts new file mode 100644 index 0000000000..ef2ecc5323 --- /dev/null +++ b/type_tests/stable_structures/stable_b_tree_map.ts @@ -0,0 +1,49 @@ +import { Opt, StableBTreeMap, text } from '../../src/lib'; +import { AssertType, NotAnyAndExact } from '../assert_type'; + +// TODO we would need to test all of the types for StableBTreeMap +// TODO maybe we should just do it for the primitive types, and for opt and vec as well? + +// TODO these tests just make sure that the basic methods are there +// TODO we want to maybe test that the type arguments work correctly as well + +let mapText = StableBTreeMap(text, text, 0); + +export type TestContainsKey = AssertType< + NotAnyAndExact boolean> +>; + +export type TestGet = AssertType< + NotAnyAndExact Opt> +>; + +export type TestInsert = AssertType< + NotAnyAndExact Opt> +>; + +export type TestIsEmpty = AssertType< + NotAnyAndExact boolean> +>; + +export type TestItems = AssertType< + NotAnyAndExact [any, any][]> +>; + +export type TestKeys = AssertType< + NotAnyAndExact any[]> +>; + +export type TestLen = AssertType< + NotAnyAndExact bigint> +>; + +export type TestRemove = AssertType< + NotAnyAndExact Opt> +>; + +export type TestValues = AssertType< + NotAnyAndExact< + typeof mapText.values, + (startIndex?: number, length?: number) => any[] + > +>; From 53a6bd0d79c41324ce932a23d7947e23fae71520 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 8 Nov 2023 15:08:13 -0600 Subject: [PATCH 27/30] use usize conversion for memory id of stable b tree map --- .../src/ic/stable_b_tree_map_contains_key.rs | 9 ++++----- .../canister/src/ic/stable_b_tree_map_get.rs | 5 ++--- .../canister/src/ic/stable_b_tree_map_init.rs | 9 ++++----- .../src/ic/stable_b_tree_map_insert.rs | 18 ++++++++++-------- .../src/ic/stable_b_tree_map_is_empty.rs | 7 +++---- .../canister/src/ic/stable_b_tree_map_items.rs | 7 +++---- .../canister/src/ic/stable_b_tree_map_keys.rs | 7 +++---- .../canister/src/ic/stable_b_tree_map_len.rs | 7 +++---- .../src/ic/stable_b_tree_map_remove.rs | 9 ++++----- 9 files changed, 36 insertions(+), 42 deletions(-) diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_contains_key.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_contains_key.rs index 1ddbc82095..91e4787fba 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_contains_key.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_contains_key.rs @@ -9,16 +9,15 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) - .expect("stable_b_tree_map_get argument 0 is undefined") + .expect("stable_b_tree_map_contains_key argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let key: Vec = args .get(1) - .expect("stable_b_tree_map_get argument 1 is undefined") + .expect("stable_b_tree_map_contains_key argument 1 is undefined") .to_js_value()? .try_into()?; @@ -26,7 +25,7 @@ pub fn native_function<'a>( .with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); - stable_b_tree_maps[&memory_id] + stable_b_tree_maps[&(memory_id as u8)] .contains_key(&AzleStableBTreeMapKey { candid_bytes: key }) }) .into(); diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_get.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_get.rs index e4d888ae72..56f858be4d 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_get.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_get.rs @@ -9,12 +9,11 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) .expect("stable_b_tree_map_get argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let key: Vec = args .get(1) @@ -25,7 +24,7 @@ pub fn native_function<'a>( let value_option = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); - stable_b_tree_maps[&memory_id].get(&AzleStableBTreeMapKey { candid_bytes: key }) + stable_b_tree_maps[&(memory_id as u8)].get(&AzleStableBTreeMapKey { candid_bytes: key }) }); // TODO could we somehow encode the entire option here more easily diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_init.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_init.rs index 6f0dbaeaba..7d24001cee 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_init.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_init.rs @@ -10,19 +10,18 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) - .expect("stable_b_tree_map_get argument 0 is undefined") + .expect("stable_b_tree_map_init argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let mut stable_b_tree_maps = stable_b_tree_maps.borrow_mut(); stable_b_tree_maps.insert( - memory_id, + memory_id as u8, StableBTreeMap::init( - MEMORY_MANAGER_REF_CELL.with(|m| m.borrow().get(MemoryId::new(memory_id))), + MEMORY_MANAGER_REF_CELL.with(|m| m.borrow().get(MemoryId::new(memory_id as u8))), ), ); }); diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_insert.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_insert.rs index 9981dbd689..0a1581d307 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_insert.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_insert.rs @@ -9,12 +9,11 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) .expect("stable_b_tree_map_insert argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let key: Vec = args .get(1) @@ -30,12 +29,15 @@ pub fn native_function<'a>( let value_option = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let mut stable_b_tree_maps = stable_b_tree_maps.borrow_mut(); - let result = stable_b_tree_maps.get_mut(&memory_id).unwrap().insert( - AzleStableBTreeMapKey { candid_bytes: key }, - AzleStableBTreeMapValue { - candid_bytes: value, - }, - ); + let result = stable_b_tree_maps + .get_mut(&(memory_id as u8)) + .unwrap() + .insert( + AzleStableBTreeMapKey { candid_bytes: key }, + AzleStableBTreeMapValue { + candid_bytes: value, + }, + ); result }); diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_is_empty.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_is_empty.rs index 9ad99ed2de..da14a4d81c 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_is_empty.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_is_empty.rs @@ -9,18 +9,17 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) - .expect("stable_b_tree_map_get argument 0 is undefined") + .expect("stable_b_tree_map_is_empty argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let result_js_value: JSValue = STABLE_B_TREE_MAPS .with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); - stable_b_tree_maps[&memory_id].is_empty() + stable_b_tree_maps[&(memory_id as u8)].is_empty() }) .into(); diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_items.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_items.rs index 04d2d6da1b..9b22a128cf 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_items.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_items.rs @@ -9,17 +9,16 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) - .expect("stable_b_tree_map_get argument 0 is undefined") + .expect("stable_b_tree_map_items argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let items: Vec>> = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); - stable_b_tree_maps[&memory_id] + stable_b_tree_maps[&(memory_id as u8)] .iter() .map(|(key, value)| vec![key.candid_bytes, value.candid_bytes]) .collect() diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_keys.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_keys.rs index c5e647e25e..8b232f2c0c 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_keys.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_keys.rs @@ -9,17 +9,16 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) - .expect("stable_b_tree_map_get argument 0 is undefined") + .expect("stable_b_tree_map_keys argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let keys: Vec> = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); - stable_b_tree_maps[&memory_id] + stable_b_tree_maps[&(memory_id as u8)] .iter() .map(|(key, _)| key.candid_bytes) .collect() diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_len.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_len.rs index 12ee920126..ad34b9964d 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_len.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_len.rs @@ -9,17 +9,16 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) - .expect("stable_b_tree_map_get argument 0 is undefined") + .expect("stable_b_tree_map_len argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let len = STABLE_B_TREE_MAPS.with(|stable_b_tree_maps| { let stable_b_tree_maps = stable_b_tree_maps.borrow(); - stable_b_tree_maps[&memory_id].len() + stable_b_tree_maps[&(memory_id as u8)].len() }); let len_js_value: JSValue = candid::encode_one(len)?.into(); diff --git a/src/compiler/rust/canister/src/ic/stable_b_tree_map_remove.rs b/src/compiler/rust/canister/src/ic/stable_b_tree_map_remove.rs index d695a04c8e..802e288902 100644 --- a/src/compiler/rust/canister/src/ic/stable_b_tree_map_remove.rs +++ b/src/compiler/rust/canister/src/ic/stable_b_tree_map_remove.rs @@ -9,16 +9,15 @@ pub fn native_function<'a>( _this: &CallbackArg, args: &[CallbackArg], ) -> Result, anyhow::Error> { - let memory_id_candid_bytes: Vec = args + let memory_id: usize = args .get(0) - .expect("stable_b_tree_map_get argument 0 is undefined") + .expect("stable_b_tree_map_remove argument 0 is undefined") .to_js_value()? .try_into()?; - let memory_id: u8 = candid::decode_one(&memory_id_candid_bytes)?; let key: Vec = args .get(1) - .expect("stable_b_tree_map_get argument 1 is undefined") + .expect("stable_b_tree_map_remove argument 1 is undefined") .to_js_value()? .try_into()?; @@ -26,7 +25,7 @@ pub fn native_function<'a>( let mut stable_b_tree_maps = stable_b_tree_maps.borrow_mut(); stable_b_tree_maps - .get_mut(&memory_id) + .get_mut(&(memory_id as u8)) .unwrap() .remove(&AzleStableBTreeMapKey { candid_bytes: key }) }); From 8f53df88071ddea894fb34b7e7ce5b5c5879ff1b Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 8 Nov 2023 15:48:49 -0600 Subject: [PATCH 28/30] add big typed arrays to type mapping, patch service tests with any, clean up --- examples/audio_recorder/src/index.ts | 4 ++-- examples/audio_recorder/test/tests.ts | 5 ++--- property_tests/tests/service/test/test.ts | 2 +- src/lib/candid/type_mapping.ts | 4 ++++ 4 files changed, 9 insertions(+), 6 deletions(-) diff --git a/examples/audio_recorder/src/index.ts b/examples/audio_recorder/src/index.ts index f37142feed..99fbdd4a7f 100644 --- a/examples/audio_recorder/src/index.ts +++ b/examples/audio_recorder/src/index.ts @@ -17,7 +17,7 @@ import { Vec } from 'azle'; -export const User = Record({ +const User = Record({ id: Principal, createdAt: nat64, recordingIds: Vec(Principal), @@ -25,7 +25,7 @@ export const User = Record({ }); type User = typeof User; -export const Recording = Record({ +const Recording = Record({ id: Principal, audio: blob, createdAt: nat64, diff --git a/examples/audio_recorder/test/tests.ts b/examples/audio_recorder/test/tests.ts index 114964f173..5c1a9309b4 100644 --- a/examples/audio_recorder/test/tests.ts +++ b/examples/audio_recorder/test/tests.ts @@ -1,12 +1,11 @@ import { ok, Test } from 'azle/test'; -import { User, Recording } from '../src/index'; import { _SERVICE } from './dfx_generated/audio_recorder/audio_recorder.did'; import { ActorSubclass } from '@dfinity/agent'; // TODO to be more thorough we could test all of the error cases as well -let global_user: typeof User; -let global_recording: typeof Recording; +let global_user: any; +let global_recording: any; export function get_tests( audio_recorder_canister: ActorSubclass<_SERVICE> diff --git a/property_tests/tests/service/test/test.ts b/property_tests/tests/service/test/test.ts index 4c01199ed2..c4623f4f02 100644 --- a/property_tests/tests/service/test/test.ts +++ b/property_tests/tests/service/test/test.ts @@ -75,7 +75,7 @@ function generateBody( .map((param, index) => { const paramName = `param${index}`; - const paramIsAService = `${paramName}.principal.toText() === "${param.value.toText()}"`; + const paramIsAService = `(${paramName} as any).principal.toText() === "${param.value.toText()}"`; const throwError = `throw new Error('${paramName} must be a Service');`; diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index 8bfb3983f7..cc20ecd994 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -94,12 +94,16 @@ export type TypeMapping = RecursionLevel extends 10 ? Uint16Array : U extends { _azleKind: 'AzleNat32' } ? Uint32Array + : U extends { _azleKind: 'AzleNat64' } + ? BigUint64Array : U extends { _azleKind: 'AzleInt8' } ? Int8Array : U extends { _azleKind: 'AzleInt16' } ? Int16Array : U extends { _azleKind: 'AzleInt32' } ? Int32Array + : U extends { _azleKind: 'AzleInt64' } + ? BigInt64Array : T extends AzleVec // TODO I do not know why we have to do this? ? TypeMapping[] : TypeMapping[] From 0252691e5454bd7630bbf4efaf890381810222af Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 8 Nov 2023 16:30:58 -0600 Subject: [PATCH 29/30] number types are now unique types with an azleKind --- examples/ethereum_json_rpc/src/index.ts | 2 +- .../func_types/canisters/func_types/index.ts | 3 +- .../motoko_examples/http_counter/src/index.ts | 2 +- .../persistent-storage/src/index.ts | 2 +- .../src/import_coverage/index.ts | 2 +- .../src/canister1/stable_map_0.ts | 2 +- .../src/canister1/stable_map_1.ts | 2 +- .../src/canister1/stable_map_2.ts | 2 +- .../src/canister1/stable_map_3.ts | 2 +- .../src/canister1/stable_map_4.ts | 2 +- .../src/canister2/stable_map_5.ts | 2 +- .../src/canister2/stable_map_6.ts | 2 +- .../src/canister2/stable_map_7.ts | 2 +- .../src/canister2/stable_map_8.ts | 2 +- .../src/canister2/stable_map_9.ts | 2 +- .../src/canister3/stable_map_10.ts | 2 +- .../src/canister3/stable_map_11.ts | 2 +- .../src/canister3/stable_map_12.ts | 2 +- .../src/canister3/stable_map_13.ts | 2 +- .../src/canister3/stable_map_14.ts | 2 +- .../src/canister3/stable_map_15.ts | 2 +- .../src/canister3/stable_map_16.ts | 2 +- .../src/canister3/stable_map_17.ts | 2 +- examples/stable_structures/src/types.ts | 3 + src/lib/candid/type_mapping.ts | 16 +-- src/lib/candid/types/constructed/vec.ts | 3 +- src/lib/candid/types/primitive/ints/int16.ts | 2 +- src/lib/candid/types/primitive/ints/int32.ts | 2 +- src/lib/candid/types/primitive/ints/int64.ts | 2 +- src/lib/candid/types/primitive/ints/int8.ts | 2 +- src/lib/candid/types/primitive/nats/nat16.ts | 2 +- src/lib/candid/types/primitive/nats/nat32.ts | 2 +- src/lib/candid/types/primitive/nats/nat64.ts | 2 +- src/lib/candid/types/primitive/nats/nat8.ts | 2 +- type_tests/candid/constructed/vec.ts | 125 +++++++++++++++++- 35 files changed, 169 insertions(+), 41 deletions(-) diff --git a/examples/ethereum_json_rpc/src/index.ts b/examples/ethereum_json_rpc/src/index.ts index 6fb7f39983..c9ce2e593e 100644 --- a/examples/ethereum_json_rpc/src/index.ts +++ b/examples/ethereum_json_rpc/src/index.ts @@ -16,7 +16,7 @@ import { managementCanister } from 'azle/canisters/management'; -let stableStorage = StableBTreeMap(text, text, 0); +let stableStorage = StableBTreeMap(text, text, 0); export default Canister({ init: init([text], (ethereumUrl) => { diff --git a/examples/func_types/canisters/func_types/index.ts b/examples/func_types/canisters/func_types/index.ts index fe9cec1156..c741d4a57b 100644 --- a/examples/func_types/canisters/func_types/index.ts +++ b/examples/func_types/canisters/func_types/index.ts @@ -37,6 +37,7 @@ const Reaction = Variant({ }); const StableFunc = Func([nat64, text], Void, 'query'); +type StableFunc = typeof StableFunc; const NullFunc = Func( [Opt(Null), Vec(Null), Null, Vec(Vec(Null)), Vec(Opt(Null))], @@ -44,7 +45,7 @@ const NullFunc = Func( 'query' ); -let stableStorage = StableBTreeMap(text, StableFunc, 0); +let stableStorage = StableBTreeMap(text, StableFunc, 0); export default Canister({ init: init([], () => { diff --git a/examples/motoko_examples/http_counter/src/index.ts b/examples/motoko_examples/http_counter/src/index.ts index 4c669b19f2..334d68972a 100644 --- a/examples/motoko_examples/http_counter/src/index.ts +++ b/examples/motoko_examples/http_counter/src/index.ts @@ -60,7 +60,7 @@ const HttpRequest = Record({ certificate_version: Opt(nat16) }); -let stableStorage = StableBTreeMap(text, nat, 0); +let stableStorage = StableBTreeMap(text, nat, 0); export default Canister({ init: init([], () => { diff --git a/examples/motoko_examples/persistent-storage/src/index.ts b/examples/motoko_examples/persistent-storage/src/index.ts index 4046991b6e..70407264e4 100644 --- a/examples/motoko_examples/persistent-storage/src/index.ts +++ b/examples/motoko_examples/persistent-storage/src/index.ts @@ -11,7 +11,7 @@ import { update } from 'azle'; -let stableStorage = StableBTreeMap(text, nat, 0); +let stableStorage = StableBTreeMap(text, nat, 0); let redeployed = false; export default Canister({ diff --git a/examples/robust_imports/src/import_coverage/index.ts b/examples/robust_imports/src/import_coverage/index.ts index ac3fd35a51..0d7bc40cac 100644 --- a/examples/robust_imports/src/import_coverage/index.ts +++ b/examples/robust_imports/src/import_coverage/index.ts @@ -141,7 +141,7 @@ export const makeCavernousRecord = dollarSignQuery( ); export const typeCheck = dollarSignQuery( - [CoveredVec(CoveredOpt(ic.nat16))], + [CoveredVec(CoveredOpt(ic.int16))], coveredInt16, (vec) => { if (vec.length === 1) { diff --git a/examples/stable_structures/src/canister1/stable_map_0.ts b/examples/stable_structures/src/canister1/stable_map_0.ts index b203012f5c..cd7ec78c15 100644 --- a/examples/stable_structures/src/canister1/stable_map_0.ts +++ b/examples/stable_structures/src/canister1/stable_map_0.ts @@ -11,7 +11,7 @@ import { Vec } from 'azle'; -let stableMap0 = StableBTreeMap(nat8, text, 0); +let stableMap0 = StableBTreeMap(nat8, text, 0); export const stableMap0Methods = { stableMap0ContainsKey: query([nat8], bool, (key) => { diff --git a/examples/stable_structures/src/canister1/stable_map_1.ts b/examples/stable_structures/src/canister1/stable_map_1.ts index a46f0772f5..077ce4d2fd 100644 --- a/examples/stable_structures/src/canister1/stable_map_1.ts +++ b/examples/stable_structures/src/canister1/stable_map_1.ts @@ -11,7 +11,7 @@ import { Vec } from 'azle'; -let stableMap1 = StableBTreeMap(nat16, blob, 1); +let stableMap1 = StableBTreeMap(nat16, blob, 1); export const stableMap1Methods = { stableMap1ContainsKey: query([nat16], bool, (key) => { diff --git a/examples/stable_structures/src/canister1/stable_map_2.ts b/examples/stable_structures/src/canister1/stable_map_2.ts index 31b171b4dc..95492ab6aa 100644 --- a/examples/stable_structures/src/canister1/stable_map_2.ts +++ b/examples/stable_structures/src/canister1/stable_map_2.ts @@ -11,7 +11,7 @@ import { Vec } from 'azle'; -let stableMap2 = StableBTreeMap(nat32, nat, 2); +let stableMap2 = StableBTreeMap(nat32, nat, 2); export const stableMap2Methods = { stableMap2ContainsKey: query([nat32], bool, (key) => { diff --git a/examples/stable_structures/src/canister1/stable_map_3.ts b/examples/stable_structures/src/canister1/stable_map_3.ts index 531517cfc0..5be0912d2c 100644 --- a/examples/stable_structures/src/canister1/stable_map_3.ts +++ b/examples/stable_structures/src/canister1/stable_map_3.ts @@ -11,7 +11,7 @@ import { } from 'azle'; import { Reaction } from '../types'; -let stableMap3 = StableBTreeMap(Reaction, int, 3); +let stableMap3 = StableBTreeMap(Reaction, int, 3); export const stableMap3Methods = { stableMap3ContainsKey: query([Reaction], bool, (key) => { diff --git a/examples/stable_structures/src/canister1/stable_map_4.ts b/examples/stable_structures/src/canister1/stable_map_4.ts index 4c615e3b0d..56415d1977 100644 --- a/examples/stable_structures/src/canister1/stable_map_4.ts +++ b/examples/stable_structures/src/canister1/stable_map_4.ts @@ -11,7 +11,7 @@ import { } from 'azle'; import { User } from '../types'; -export let stableMap4 = StableBTreeMap(User, float32, 4); +export let stableMap4 = StableBTreeMap(User, float32, 4); export const stableMap4Methods = { stableMap4ContainsKey: query([User], bool, (key) => { diff --git a/examples/stable_structures/src/canister2/stable_map_5.ts b/examples/stable_structures/src/canister2/stable_map_5.ts index 0a5bfc1b2d..9d588b3175 100644 --- a/examples/stable_structures/src/canister2/stable_map_5.ts +++ b/examples/stable_structures/src/canister2/stable_map_5.ts @@ -11,7 +11,7 @@ import { Tuple } from 'azle'; -let stableMap5 = StableBTreeMap(Opt(text), float64, 5); +let stableMap5 = StableBTreeMap, float64>(Opt(text), float64, 5); export const stableMap5Methods = { stableMap5ContainsKey: query([Opt(text)], bool, (key) => { diff --git a/examples/stable_structures/src/canister2/stable_map_6.ts b/examples/stable_structures/src/canister2/stable_map_6.ts index a2aff5b5fc..a3f9d90379 100644 --- a/examples/stable_structures/src/canister2/stable_map_6.ts +++ b/examples/stable_structures/src/canister2/stable_map_6.ts @@ -9,7 +9,7 @@ import { Tuple } from 'azle'; -let stableMap6 = StableBTreeMap(Vec(nat64), bool, 6); +let stableMap6 = StableBTreeMap, bool>(Vec(nat64), bool, 6); export const stableMap6Methods = { stableMap6ContainsKey: query([Vec(nat64)], bool, (key) => { diff --git a/examples/stable_structures/src/canister2/stable_map_7.ts b/examples/stable_structures/src/canister2/stable_map_7.ts index 3a5366af8a..8f9f491aba 100644 --- a/examples/stable_structures/src/canister2/stable_map_7.ts +++ b/examples/stable_structures/src/canister2/stable_map_7.ts @@ -10,7 +10,7 @@ import { Vec } from 'azle'; -let stableMap7 = StableBTreeMap(Null, Null, 7); +let stableMap7 = StableBTreeMap(Null, Null, 7); export const stableMap7Methods = { stableMap7ContainsKey: query([Null], bool, (key) => { diff --git a/examples/stable_structures/src/canister2/stable_map_8.ts b/examples/stable_structures/src/canister2/stable_map_8.ts index 4ccaf6cb49..14f5a6cc39 100644 --- a/examples/stable_structures/src/canister2/stable_map_8.ts +++ b/examples/stable_structures/src/canister2/stable_map_8.ts @@ -10,7 +10,7 @@ import { Tuple } from 'azle'; -let stableMap8 = StableBTreeMap(bool, Null, 8); +let stableMap8 = StableBTreeMap(bool, Null, 8); export const stableMap8Methods = { stableMap8ContainsKey: query([bool], bool, (key) => { diff --git a/examples/stable_structures/src/canister2/stable_map_9.ts b/examples/stable_structures/src/canister2/stable_map_9.ts index 5d45e426f2..2c06ad8f60 100644 --- a/examples/stable_structures/src/canister2/stable_map_9.ts +++ b/examples/stable_structures/src/canister2/stable_map_9.ts @@ -11,7 +11,7 @@ import { Tuple } from 'azle'; -let stableMap9 = StableBTreeMap(float64, Vec(text), 9); +let stableMap9 = StableBTreeMap>(float64, Vec(text), 9); export const stableMap9Methods = { stableMap9ContainsKey: query([float64], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_10.ts b/examples/stable_structures/src/canister3/stable_map_10.ts index e5d9ad3922..a3a5f650be 100644 --- a/examples/stable_structures/src/canister3/stable_map_10.ts +++ b/examples/stable_structures/src/canister3/stable_map_10.ts @@ -10,7 +10,7 @@ import { Vec } from 'azle'; -let stableMap10 = StableBTreeMap(float32, Opt(bool), 10); +let stableMap10 = StableBTreeMap>(float32, Opt(bool), 10); export const stableMap10Methods = { stableMap10ContainsKey: query([float32], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_11.ts b/examples/stable_structures/src/canister3/stable_map_11.ts index 4faa84fa52..713d882e8c 100644 --- a/examples/stable_structures/src/canister3/stable_map_11.ts +++ b/examples/stable_structures/src/canister3/stable_map_11.ts @@ -11,7 +11,7 @@ import { } from 'azle'; import { User } from '../types'; -let stableMap11 = StableBTreeMap(nat, User, 11); +let stableMap11 = StableBTreeMap(nat, User, 11); export const stableMap11Methods = { stableMap11ContainsKey: query([nat], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_12.ts b/examples/stable_structures/src/canister3/stable_map_12.ts index 43df7be73a..d94223abf1 100644 --- a/examples/stable_structures/src/canister3/stable_map_12.ts +++ b/examples/stable_structures/src/canister3/stable_map_12.ts @@ -11,7 +11,7 @@ import { } from 'azle'; import { Reaction } from '../types'; -let stableMap12 = StableBTreeMap(blob, Reaction, 12); +let stableMap12 = StableBTreeMap(blob, Reaction, 12); export const stableMap12Methods = { stableMap12ContainsKey: query([blob], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_13.ts b/examples/stable_structures/src/canister3/stable_map_13.ts index a6136fe580..65fb014e70 100644 --- a/examples/stable_structures/src/canister3/stable_map_13.ts +++ b/examples/stable_structures/src/canister3/stable_map_13.ts @@ -11,7 +11,7 @@ import { Vec } from 'azle'; -let stableMap13 = StableBTreeMap(text, Principal, 13); +let stableMap13 = StableBTreeMap(text, Principal, 13); export const stableMap13Methods = { stableMap13ContainsKey: query([text], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_14.ts b/examples/stable_structures/src/canister3/stable_map_14.ts index 13a63fcbfb..60f352eaf5 100644 --- a/examples/stable_structures/src/canister3/stable_map_14.ts +++ b/examples/stable_structures/src/canister3/stable_map_14.ts @@ -11,7 +11,7 @@ import { } from 'azle'; import { Callback } from '../types'; -let stableMap14 = StableBTreeMap(text, Callback, 14); +let stableMap14 = StableBTreeMap(text, Callback, 14); export const stableMap14Methods = { stableMap14ContainsKey: query([text], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_15.ts b/examples/stable_structures/src/canister3/stable_map_15.ts index 6a49097876..7b89f5b8ab 100644 --- a/examples/stable_structures/src/canister3/stable_map_15.ts +++ b/examples/stable_structures/src/canister3/stable_map_15.ts @@ -11,7 +11,7 @@ import { } from 'azle'; import { Callback } from '../types'; -let stableMap15 = StableBTreeMap(Callback, text, 15); +let stableMap15 = StableBTreeMap(Callback, text, 15); export const stableMap15Methods = { stableMap15ContainsKey: query([Callback], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_16.ts b/examples/stable_structures/src/canister3/stable_map_16.ts index 82b148d70c..c1090be395 100644 --- a/examples/stable_structures/src/canister3/stable_map_16.ts +++ b/examples/stable_structures/src/canister3/stable_map_16.ts @@ -13,7 +13,7 @@ import { Vec } from 'azle'; -let stableMap16 = StableBTreeMap(text, StableJson(), 16); +let stableMap16 = StableBTreeMap(text, StableJson(), 16); export const stableMap16Methods = { stableMap16ContainsKey: query([text], bool, (key) => { diff --git a/examples/stable_structures/src/canister3/stable_map_17.ts b/examples/stable_structures/src/canister3/stable_map_17.ts index 348cb39156..c30b3ac39e 100644 --- a/examples/stable_structures/src/canister3/stable_map_17.ts +++ b/examples/stable_structures/src/canister3/stable_map_17.ts @@ -11,7 +11,7 @@ import { Vec } from 'azle'; -let stableMap17 = StableBTreeMap(StableJson(), text, 17); +let stableMap17 = StableBTreeMap<{}, text>(StableJson(), text, 17); export const stableMap17Methods = { stableMap17ContainsKey: query([text], bool, (key) => { diff --git a/examples/stable_structures/src/types.ts b/examples/stable_structures/src/types.ts index 322e20f3fe..ae1710c422 100644 --- a/examples/stable_structures/src/types.ts +++ b/examples/stable_structures/src/types.ts @@ -8,10 +8,13 @@ export const Reaction = Variant({ Happy: Null, Sad: Null }); +export type Reaction = typeof Reaction; export const User = Record({ username: text, posts: Vec(BlogPost) }); +export type User = typeof User; export const Callback = Func([BlogPost], Reaction, 'update'); +export type Callback = typeof Callback; diff --git a/src/lib/candid/type_mapping.ts b/src/lib/candid/type_mapping.ts index cc20ecd994..055d1b3483 100644 --- a/src/lib/candid/type_mapping.ts +++ b/src/lib/candid/type_mapping.ts @@ -88,21 +88,21 @@ export type TypeMapping = RecursionLevel extends 10 >; } : T extends AzleVec - ? U extends { _azleKind: 'AzleNat8' } + ? U extends { _azleKind?: 'AzleNat8' } ? Uint8Array - : U extends { _azleKind: 'AzleNat16' } + : U extends { _azleKind?: 'AzleNat16' } ? Uint16Array - : U extends { _azleKind: 'AzleNat32' } + : U extends { _azleKind?: 'AzleNat32' } ? Uint32Array - : U extends { _azleKind: 'AzleNat64' } + : U extends { _azleKind?: 'AzleNat64' } ? BigUint64Array - : U extends { _azleKind: 'AzleInt8' } + : U extends { _azleKind?: 'AzleInt8' } ? Int8Array - : U extends { _azleKind: 'AzleInt16' } + : U extends { _azleKind?: 'AzleInt16' } ? Int16Array - : U extends { _azleKind: 'AzleInt32' } + : U extends { _azleKind?: 'AzleInt32' } ? Int32Array - : U extends { _azleKind: 'AzleInt64' } + : U extends { _azleKind?: 'AzleInt64' } ? BigInt64Array : T extends AzleVec // TODO I do not know why we have to do this? ? TypeMapping[] diff --git a/src/lib/candid/types/constructed/vec.ts b/src/lib/candid/types/constructed/vec.ts index 697e887c24..ba5d1726bd 100644 --- a/src/lib/candid/types/constructed/vec.ts +++ b/src/lib/candid/types/constructed/vec.ts @@ -3,6 +3,7 @@ import { Parent, toIdl } from '../../to_idl'; import { IDL } from '@dfinity/candid'; import { encode } from '../../serde/encode'; import { decode } from '../../serde/decode'; +import { TypeMapping } from '../../type_mapping'; export class AzleVec { constructor(t: any) { @@ -30,7 +31,7 @@ export class AzleVec { } } -export type Vec = T[]; +export type Vec = TypeMapping>; export function Vec(t: T): AzleVec { return new AzleVec(t); } diff --git a/src/lib/candid/types/primitive/ints/int16.ts b/src/lib/candid/types/primitive/ints/int16.ts index dc5e35720b..a9eddd16ea 100644 --- a/src/lib/candid/types/primitive/ints/int16.ts +++ b/src/lib/candid/types/primitive/ints/int16.ts @@ -22,4 +22,4 @@ export class AzleInt16 { } } export const int16 = AzleInt16; -export type int16 = number; +export type int16 = number & { _azleKind?: 'AzleInt16' }; diff --git a/src/lib/candid/types/primitive/ints/int32.ts b/src/lib/candid/types/primitive/ints/int32.ts index 83a6dd2052..43b6e80e93 100644 --- a/src/lib/candid/types/primitive/ints/int32.ts +++ b/src/lib/candid/types/primitive/ints/int32.ts @@ -22,4 +22,4 @@ export class AzleInt32 { } } export const int32 = AzleInt32; -export type int32 = number; +export type int32 = number & { _azleKind?: 'AzleInt32' }; diff --git a/src/lib/candid/types/primitive/ints/int64.ts b/src/lib/candid/types/primitive/ints/int64.ts index 4ccbcc6359..b4a841682d 100644 --- a/src/lib/candid/types/primitive/ints/int64.ts +++ b/src/lib/candid/types/primitive/ints/int64.ts @@ -22,4 +22,4 @@ export class AzleInt64 { } } export const int64 = AzleInt64; -export type int64 = bigint; +export type int64 = bigint & { _azleKind?: 'AzleInt64' }; diff --git a/src/lib/candid/types/primitive/ints/int8.ts b/src/lib/candid/types/primitive/ints/int8.ts index a47147d61c..e839abbfe8 100644 --- a/src/lib/candid/types/primitive/ints/int8.ts +++ b/src/lib/candid/types/primitive/ints/int8.ts @@ -23,4 +23,4 @@ export class AzleInt8 { } export const int8 = AzleInt8; -export type int8 = number; +export type int8 = number & { _azleKind?: 'AzleInt8' }; diff --git a/src/lib/candid/types/primitive/nats/nat16.ts b/src/lib/candid/types/primitive/nats/nat16.ts index ebca64aee4..85829c2c0a 100644 --- a/src/lib/candid/types/primitive/nats/nat16.ts +++ b/src/lib/candid/types/primitive/nats/nat16.ts @@ -23,4 +23,4 @@ export class AzleNat16 { } export const nat16 = AzleNat16; -export type nat16 = number; +export type nat16 = number & { _azleKind?: 'AzleNat16' }; diff --git a/src/lib/candid/types/primitive/nats/nat32.ts b/src/lib/candid/types/primitive/nats/nat32.ts index bf8c1b5403..73ec11397a 100644 --- a/src/lib/candid/types/primitive/nats/nat32.ts +++ b/src/lib/candid/types/primitive/nats/nat32.ts @@ -22,4 +22,4 @@ export class AzleNat32 { } } export const nat32 = AzleNat32; -export type nat32 = number; +export type nat32 = number & { _azleKind?: 'AzleNat32' }; diff --git a/src/lib/candid/types/primitive/nats/nat64.ts b/src/lib/candid/types/primitive/nats/nat64.ts index d7bc9b60b8..8d8b6b9c33 100644 --- a/src/lib/candid/types/primitive/nats/nat64.ts +++ b/src/lib/candid/types/primitive/nats/nat64.ts @@ -22,4 +22,4 @@ export class AzleNat64 { } } export const nat64 = AzleNat64; -export type nat64 = bigint; +export type nat64 = bigint & { _azleKind?: 'AzleNat64' }; diff --git a/src/lib/candid/types/primitive/nats/nat8.ts b/src/lib/candid/types/primitive/nats/nat8.ts index 24979dcb39..fe8024a574 100644 --- a/src/lib/candid/types/primitive/nats/nat8.ts +++ b/src/lib/candid/types/primitive/nats/nat8.ts @@ -23,4 +23,4 @@ export class AzleNat8 { } export const nat8 = AzleNat8; -export type nat8 = number; +export type nat8 = number & { _azleKind?: 'AzleNat8' }; diff --git a/type_tests/candid/constructed/vec.ts b/type_tests/candid/constructed/vec.ts index 95ef01f0fd..3c20a45c32 100644 --- a/type_tests/candid/constructed/vec.ts +++ b/type_tests/candid/constructed/vec.ts @@ -1,7 +1,18 @@ // TODO These tests are just for one type, float32 // TODO it will take a lot of effort (not that much though) to get all types tested with Vec -import { float32, Vec } from '../../../src/lib'; +import { + float32, + int64, + int32, + int16, + int8, + nat64, + nat32, + nat16, + nat8, + Vec +} from '../../../src/lib'; import { AssertType, NotAnyAndExact, @@ -10,6 +21,8 @@ import { } from '../../assert_type'; import { TypeMapping } from '../../../src/lib/candid/type_mapping'; +// test float32 + testCandidType(Vec(float32)); testSerializable(Vec(float32)); @@ -33,3 +46,113 @@ const testTypeMappingTriple = Vec(Vec(Vec(float32))); export type TestTypeMappingTriple = AssertType< NotAnyAndExact, number[][][]> >; + +// test nat64 + +testCandidType(Vec(nat64)); +testSerializable(Vec(nat64)); + +const testTypeMappingNat64 = Vec(nat64); +export type TestTypeMappingNat64 = AssertType< + NotAnyAndExact, BigUint64Array> +>; + +// test nat32 + +testCandidType(Vec(nat32)); +testSerializable(Vec(nat32)); + +const testTypeMappingNat32 = Vec(nat32); +export type TestTypeMappingNat32 = AssertType< + NotAnyAndExact, Uint32Array> +>; + +// test nat16 + +testCandidType(Vec(nat16)); +testSerializable(Vec(nat16)); + +const testTypeMappingNat16 = Vec(nat16); +export type TestTypeMappingNat16 = AssertType< + NotAnyAndExact, Uint16Array> +>; + +// test nat8 + +testCandidType(Vec(nat8)); +testSerializable(Vec(nat8)); + +const testTypeMappingNat8 = Vec(nat8); +export type TestTypeMappingNat8 = AssertType< + NotAnyAndExact, Uint8Array> +>; + +// test int64 + +testCandidType(Vec(int64)); +testSerializable(Vec(int64)); + +const testTypeMappingInt64 = Vec(int64); +export type TestTypeMappingInt64 = AssertType< + NotAnyAndExact, BigInt64Array> +>; + +// test int32 + +testCandidType(Vec(int32)); +testSerializable(Vec(int32)); + +const testTypeMappingInt32 = Vec(int32); +export type TestTypeMappingInt32 = AssertType< + NotAnyAndExact, Int32Array> +>; + +// test int16 + +testCandidType(Vec(int16)); +testSerializable(Vec(int16)); + +const testTypeMappingInt16 = Vec(int16); +export type TestTypeMappingInt16 = AssertType< + NotAnyAndExact, Int16Array> +>; + +// test int8 + +testCandidType(Vec(int8)); +testSerializable(Vec(int8)); + +const testTypeMappingInt8 = Vec(int8); +export type TestTypeMappingInt8 = AssertType< + NotAnyAndExact, Int8Array> +>; + +// test Vec type + +export type TestVecTypeNat64 = AssertType< + NotAnyAndExact, BigUint64Array> +>; + +export type TestVecTypeNat32 = AssertType< + NotAnyAndExact, Uint32Array> +>; + +export type TestVecTypeNat16 = AssertType< + NotAnyAndExact, Uint16Array> +>; + +export type TestVecTypeNat8 = AssertType, Uint8Array>>; + +export type TestVecTypeInt64 = AssertType< + NotAnyAndExact, BigInt64Array> +>; + +export type TestVecTypeInt32 = AssertType< + NotAnyAndExact, Int32Array> +>; + +export type TestVecTypeInt16 = AssertType< + NotAnyAndExact, Int16Array> +>; + +export type TestVecTypeInt8 = AssertType, Int8Array>>; From 8b58d217b09f8ee60a034706d177717e30d0a488 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 8 Nov 2023 16:39:43 -0600 Subject: [PATCH 30/30] fix service arbitrary to make fields unique --- property_tests/arbitraries/candid/reference/service_arb.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/property_tests/arbitraries/candid/reference/service_arb.ts b/property_tests/arbitraries/candid/reference/service_arb.ts index cf093fe742..79f7ffbd29 100644 --- a/property_tests/arbitraries/candid/reference/service_arb.ts +++ b/property_tests/arbitraries/candid/reference/service_arb.ts @@ -66,7 +66,7 @@ const ServiceMethodArb = fc export const ServiceArb = fc .tuple( UniqueIdentifierArb('typeDeclaration'), - fc.array(ServiceMethodArb), + fc.uniqueArray(ServiceMethodArb, { selector: (entry) => entry.name }), PrincipalArb ) .map(([name, serviceMethods, principal]): CandidMeta => {