From d67d8b54af81ba1c24d01ecd8756e7d9989ec993 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Wed, 28 Aug 2024 17:36:13 -0500 Subject: [PATCH] attempt to use class variables in most places in the class api, remove unused candid files --- .../audio_recorder/package-lock.json | 5 +- .../class_syntax/audio_recorder/src/index.ts | 36 ++-- .../complex_init/package-lock.json | 4 +- .../complex_init/src/complex_init/index.ts | 12 +- .../complex_init/src/rec_init/index.ts | 8 +- .../composite_queries/src/canister2/index.did | 7 - .../composite_queries/src/canister3/index.did | 3 - .../class_syntax/counter/src/index.ts | 10 +- .../src/canister2/index.did | 9 - .../src/canister2/index.ts | 44 ++--- .../class_syntax/cycles/src/cycles/index.did | 4 - .../ethereum_json_rpc/src/index.ts | 10 +- .../func_types/canisters/func_types/index.ts | 8 +- .../func_types/canisters/notifiers/index.did | 3 - .../heartbeat/src/heartbeat_async/index.ts | 8 +- .../heartbeat/src/heartbeat_sync/index.ts | 10 +- .../candid_rpc/class_syntax/init/src/index.ts | 20 +-- .../class_syntax/key_value_store/src/index.ts | 8 +- .../management_canister/src/index.ts | 12 +- .../motoko_examples/calc/src/index.ts | 22 +-- .../motoko_examples/counter/src/index.ts | 10 +- .../motoko_examples/http_counter/src/index.ts | 16 +- .../src/minimal_dapp/index.ts | 14 +- .../persistent-storage/src/index.ts | 22 +-- .../phone-book/src/phone_book/index.ts | 8 +- .../motoko_examples/simple-to-do/src/index.ts | 24 +-- .../superheroes/src/superheroes/index.ts | 34 ++-- .../motoko_examples/whoami/src/index.ts | 26 +-- .../notify_raw/src/canister2/index.ts | 8 +- .../pre_and_post_upgrade/src/index.ts | 24 +-- .../class_syntax/randomness/src/index.ts | 8 +- .../src/recursive_canister/index.did | 6 - .../recursion/src/recursive_canister/index.ts | 8 +- .../rejections/src/nonexistent.did | 3 - .../rejections/src/some_canister/index.did | 5 - .../class_syntax/simple_erc20/src/index.ts | 34 ++-- .../simple_user_accounts/src/index.ts | 16 +- .../src/index.ts | 34 ++-- .../stable_structures/src/canister1/index.ts | 102 +++++------ .../stable_structures/src/canister2/index.ts | 114 ++++++------ .../stable_structures/src/canister3/index.ts | 162 +++++++++--------- .../class_syntax/update/src/index.ts | 8 +- 42 files changed, 448 insertions(+), 481 deletions(-) delete mode 100644 tests/end_to_end/candid_rpc/class_syntax/composite_queries/src/canister2/index.did delete mode 100644 tests/end_to_end/candid_rpc/class_syntax/composite_queries/src/canister3/index.did delete mode 100644 tests/end_to_end/candid_rpc/class_syntax/cross_canister_calls/src/canister2/index.did delete mode 100644 tests/end_to_end/candid_rpc/class_syntax/cycles/src/cycles/index.did delete mode 100644 tests/end_to_end/candid_rpc/class_syntax/func_types/canisters/notifiers/index.did delete mode 100644 tests/end_to_end/candid_rpc/class_syntax/recursion/src/recursive_canister/index.did delete mode 100644 tests/end_to_end/candid_rpc/class_syntax/rejections/src/nonexistent.did delete mode 100644 tests/end_to_end/candid_rpc/class_syntax/rejections/src/some_canister/index.did diff --git a/tests/end_to_end/candid_rpc/class_syntax/audio_recorder/package-lock.json b/tests/end_to_end/candid_rpc/class_syntax/audio_recorder/package-lock.json index 3bd9687c3b..c286cc1b6a 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/audio_recorder/package-lock.json +++ b/tests/end_to_end/candid_rpc/class_syntax/audio_recorder/package-lock.json @@ -31,9 +31,10 @@ } }, "../../functional_syntax/audio_recorder": { + "name": "audio_recorder_end_to_end_test_functional_syntax", "dev": true, "dependencies": { - "azle": "0.22.0" + "azle": "0.23.0" }, "devDependencies": { "@dfinity/agent": "^0.19.3", @@ -7833,7 +7834,7 @@ "version": "file:../../functional_syntax/audio_recorder", "requires": { "@dfinity/agent": "^0.19.3", - "azle": "0.22.0", + "azle": "0.23.0", "jest": "^29.7.0", "ts-jest": "^29.1.4", "tsx": "^4.15.7", diff --git a/tests/end_to_end/candid_rpc/class_syntax/audio_recorder/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/audio_recorder/src/index.ts index 9719d82c30..1b4de8ce73 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/audio_recorder/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/audio_recorder/src/index.ts @@ -28,10 +28,10 @@ type Recording = { userId: Principal; }; -let users = StableBTreeMap(0); -let recordings = StableBTreeMap(1); - export default class { + users = StableBTreeMap(0); + recordings = StableBTreeMap(1); + @update([IDL.Text], User) createUser(username: string): User { const id = generateId(); @@ -42,19 +42,19 @@ export default class { username }; - users.insert(user.id, user); + this.users.insert(user.id, user); return user; } @query([], IDL.Vec(User)) readUsers(): User[] { - return users.values(); + return this.users.values(); } @query([IDL.Principal], IDL.Opt(User)) readUserById(id: Principal): [User] | [] { - const result = users.get(id); + const result = this.users.get(id); if (result === null) { return []; } else { @@ -64,17 +64,17 @@ export default class { @update([IDL.Principal], User) deleteUser(id: Principal): User { - const user = users.get(id); + const user = this.users.get(id); if (user === null) { throw new Error(`User does not exist: ${id.toText()}`); } user.recordingIds.forEach((recordingId) => { - recordings.remove(recordingId); + this.recordings.remove(recordingId); }); - users.remove(user.id); + this.users.remove(user.id); return user; } @@ -85,7 +85,7 @@ export default class { name: string, userId: Principal ): Recording { - const user = users.get(userId); + const user = this.users.get(userId); if (user === null) { throw new Error(`User does not exist: ${userId.toText()}`); @@ -100,26 +100,26 @@ export default class { userId }; - recordings.insert(recording.id, recording); + this.recordings.insert(recording.id, recording); const updatedUser: User = { ...user, recordingIds: [...user.recordingIds, recording.id] }; - users.insert(updatedUser.id, updatedUser); + this.users.insert(updatedUser.id, updatedUser); return recording; } @query([], IDL.Vec(Recording)) readRecordings(): Recording[] { - return recordings.values(); + return this.recordings.values(); } @query([IDL.Principal], IDL.Opt(Recording)) readRecordingById(id: Principal): [Recording] | [] { - const result = recordings.get(id); + const result = this.recordings.get(id); if (result === null) { return []; } else { @@ -129,13 +129,13 @@ export default class { @update([IDL.Principal], Recording) deleteRecording(id: Principal): Recording { - const recording = recordings.get(id); + const recording = this.recordings.get(id); if (recording === null) { throw new Error(`Recording does not exist: ${id.toText()}`); } - const user = users.get(recording.userId); + const user = this.users.get(recording.userId); if (user === null) { throw new Error( @@ -150,9 +150,9 @@ export default class { ) }; - users.insert(updatedUser.id, updatedUser); + this.users.insert(updatedUser.id, updatedUser); - recordings.remove(id); + this.recordings.remove(id); return recording; } diff --git a/tests/end_to_end/candid_rpc/class_syntax/complex_init/package-lock.json b/tests/end_to_end/candid_rpc/class_syntax/complex_init/package-lock.json index cf58bc122d..a0795010c1 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/complex_init/package-lock.json +++ b/tests/end_to_end/candid_rpc/class_syntax/complex_init/package-lock.json @@ -34,7 +34,7 @@ "name": "complex_init_end_to_end_test_functional_syntax", "dev": true, "dependencies": { - "azle": "0.22.0" + "azle": "0.23.0" }, "devDependencies": { "@dfinity/agent": "^0.19.2", @@ -8470,7 +8470,7 @@ "version": "file:../../functional_syntax/complex_init", "requires": { "@dfinity/agent": "^0.19.2", - "azle": "0.22.0", + "azle": "0.23.0", "jest": "^29.7.0", "ts-jest": "^29.1.4", "tsx": "^4.15.7", diff --git a/tests/end_to_end/candid_rpc/class_syntax/complex_init/src/complex_init/index.ts b/tests/end_to_end/candid_rpc/class_syntax/complex_init/src/complex_init/index.ts index 13ff1f1784..49823372bc 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/complex_init/src/complex_init/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/complex_init/src/complex_init/index.ts @@ -5,19 +5,19 @@ const User = IDL.Record({ }); type User = { id: string }; -let greeting = 'Hello User'; -let user: [User] | [] = []; - export default class { + greeting = 'Hello User'; + user: [User] | [] = []; + @init([IDL.Tuple(IDL.Text, User)]) init(tuple: [string, User]): void { - greeting = tuple[0]; - user = [tuple[1]]; + this.greeting = tuple[0]; + this.user = [tuple[1]]; return undefined; } @query([], IDL.Text) greetUser(): string { - return `${greeting} ${user[0]?.id ?? '??'}`; + return `${this.greeting} ${this.user[0]?.id ?? '??'}`; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/complex_init/src/rec_init/index.ts b/tests/end_to_end/candid_rpc/class_syntax/complex_init/src/rec_init/index.ts index d52513c541..3cab8d8e95 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/complex_init/src/rec_init/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/complex_init/src/rec_init/index.ts @@ -11,18 +11,18 @@ Node.fill( type Node = { Leaf: null } | { Branch: Node }; -let tree: Node = { Leaf: null }; - export default class { + tree: Node = { Leaf: null }; + @init([Node]) init(node: Node): void { - tree = node; + this.tree = node; return undefined; } @query([], IDL.Nat) countBranches(): bigint { - return countBranches(tree); + return countBranches(this.tree); } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/composite_queries/src/canister2/index.did b/tests/end_to_end/candid_rpc/class_syntax/composite_queries/src/canister2/index.did deleted file mode 100644 index 01f39353d7..0000000000 --- a/tests/end_to_end/candid_rpc/class_syntax/composite_queries/src/canister2/index.did +++ /dev/null @@ -1,7 +0,0 @@ -service: () -> { - deepQuery: () -> (text) query; - incCounter: () -> (nat) query; - manualQuery: () -> (text) query; - simpleQuery: () -> (text) query; - updateQuery: () -> (text); -} diff --git a/tests/end_to_end/candid_rpc/class_syntax/composite_queries/src/canister3/index.did b/tests/end_to_end/candid_rpc/class_syntax/composite_queries/src/canister3/index.did deleted file mode 100644 index 78eacc0a1e..0000000000 --- a/tests/end_to_end/candid_rpc/class_syntax/composite_queries/src/canister3/index.did +++ /dev/null @@ -1,3 +0,0 @@ -service: () -> { - deepQuery: () -> (text) query; -} diff --git a/tests/end_to_end/candid_rpc/class_syntax/counter/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/counter/src/index.ts index c904e7d9f9..bd2decfdc0 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/counter/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/counter/src/index.ts @@ -1,17 +1,17 @@ import { IDL, query, update } from 'azle'; -let count: bigint = 0n; - export default class { + count: bigint = 0n; + @query([], IDL.Nat64) readCount(): bigint { - return count; + return this.count; } @update([], IDL.Nat64) incrementCount(): bigint { - count += 1n; + this.count += 1n; - return count; + return this.count; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/cross_canister_calls/src/canister2/index.did b/tests/end_to_end/candid_rpc/class_syntax/cross_canister_calls/src/canister2/index.did deleted file mode 100644 index fc5cb68b4f..0000000000 --- a/tests/end_to_end/candid_rpc/class_syntax/cross_canister_calls/src/canister2/index.did +++ /dev/null @@ -1,9 +0,0 @@ -service: () -> { - account: (record {id:text}) -> (opt record {id:text; balance:nat64}) query; - accounts: () -> (vec record {id:text; balance:nat64}) query; - balance: (text) -> (nat64) query; - getNotification: () -> (text) query; - receiveNotification: (text) -> (); - transfer: (text, text, nat64) -> (nat64); - trap: () -> (empty) query; -} diff --git a/tests/end_to_end/candid_rpc/class_syntax/cross_canister_calls/src/canister2/index.ts b/tests/end_to_end/candid_rpc/class_syntax/cross_canister_calls/src/canister2/index.ts index 9ad9525b23..451c00a490 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/cross_canister_calls/src/canister2/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/cross_canister_calls/src/canister2/index.ts @@ -2,56 +2,56 @@ import { IDL, query, trap, update } from 'azle'; import { Account, AccountArgs, State } from './types'; -let state: State = { - accounts: { - '0': { - id: '0', - balance: 100n - } - }, - notification: '' -}; - export default class { + state: State = { + accounts: { + '0': { + id: '0', + balance: 100n + } + }, + notification: '' + }; + @update([IDL.Text, IDL.Text, IDL.Nat64], IDL.Nat64) transfer(from: string, to: string, amount: bigint): bigint { - const fromAccount: Account | undefined = state.accounts[from]; + const fromAccount: Account | undefined = this.state.accounts[from]; if (fromAccount === undefined) { - state.accounts[from] = { + this.state.accounts[from] = { id: from, balance: 0n }; } - const fromBalance = state.accounts[from].balance; + const fromBalance = this.state.accounts[from].balance; if (fromBalance < amount) { return 0n; } - const toBalance: bigint | undefined = state.accounts[to]?.balance; + const toBalance: bigint | undefined = this.state.accounts[to]?.balance; if (toBalance === undefined) { - state.accounts[to] = { + this.state.accounts[to] = { id: to, balance: 0n }; } - state.accounts[from].balance -= amount; - state.accounts[to].balance += amount; + this.state.accounts[from].balance -= amount; + this.state.accounts[to].balance += amount; return amount; } @query([IDL.Text], IDL.Nat64) balance(id: string): bigint { - return state.accounts[id]?.balance ?? 0n; + return this.state.accounts[id]?.balance ?? 0n; } @query([AccountArgs], IDL.Opt(Account)) account(accountArgs: AccountArgs): [Account] | [] { - const account = state.accounts[accountArgs.id]; + const account = this.state.accounts[accountArgs.id]; return account ? [account] : []; } @query([], IDL.Vec(Account)) accounts(): Account[] { - return Object.values(state.accounts); + return Object.values(this.state.accounts); } @query([], IDL.Empty) @@ -61,11 +61,11 @@ export default class { @update([IDL.Text]) receiveNotification(message: string): void { - state.notification = message; + this.state.notification = message; } @query([], IDL.Text) getNotification(): string { - return state.notification; + return this.state.notification; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/cycles/src/cycles/index.did b/tests/end_to_end/candid_rpc/class_syntax/cycles/src/cycles/index.did deleted file mode 100644 index 867da2474e..0000000000 --- a/tests/end_to_end/candid_rpc/class_syntax/cycles/src/cycles/index.did +++ /dev/null @@ -1,4 +0,0 @@ -service: () -> { - getCanisterBalance: () -> (nat64) query; - receiveCycles: () -> (nat64); -} diff --git a/tests/end_to_end/candid_rpc/class_syntax/ethereum_json_rpc/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/ethereum_json_rpc/src/index.ts index 7bbb1825e7..4e75c17f5a 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/ethereum_json_rpc/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/ethereum_json_rpc/src/index.ts @@ -16,17 +16,17 @@ import { PRINCIPAL } from 'azle/canisters/management'; -let stableStorage = StableBTreeMap(0); - export default class { + stableStorage = StableBTreeMap(0); + @init([IDL.Text]) init(ethereumUrl: string): void { - stableStorage.insert('ethereumUrl', ethereumUrl); + this.stableStorage.insert('ethereumUrl', ethereumUrl); } @update([IDL.Text], IDL.Text) async ethGetBalance(ethereumAddress: string): Promise { - const url = stableStorage.get('ethereumUrl'); + const url = this.stableStorage.get('ethereumUrl'); if (url === null) { throw new Error('ethereumUrl is not defined'); @@ -37,7 +37,7 @@ export default class { @update([IDL.Nat32], IDL.Text) async ethGetBlockByNumber(number: number): Promise { - const urlOpt = stableStorage.get('ethereumUrl'); + const urlOpt = this.stableStorage.get('ethereumUrl'); if (urlOpt === null) { throw new Error('ethereumUrl is not defined'); diff --git a/tests/end_to_end/candid_rpc/class_syntax/func_types/canisters/func_types/index.ts b/tests/end_to_end/candid_rpc/class_syntax/func_types/canisters/func_types/index.ts index f012b8e5ed..669df8eea8 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/func_types/canisters/func_types/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/func_types/canisters/func_types/index.ts @@ -55,12 +55,12 @@ const NullFunc = IDL.Func( ); type NullFunc = Func; -let stableStorage = StableBTreeMap(0); - export default class { + stableStorage = StableBTreeMap(0); + @init([]) init(): void { - stableStorage.insert('stableFunc', [ + this.stableStorage.insert('stableFunc', [ Principal.from('aaaaa-aa'), 'start_canister' ]); @@ -68,7 +68,7 @@ export default class { @query([], StableFunc) getStableFunc(): StableFunc { - const stableFunc = stableStorage.get('stableFunc'); + const stableFunc = this.stableStorage.get('stableFunc'); if (stableFunc === null) { return [Principal.from('aaaaa-aa'), 'raw_rand']; } diff --git a/tests/end_to_end/candid_rpc/class_syntax/func_types/canisters/notifiers/index.did b/tests/end_to_end/candid_rpc/class_syntax/func_types/canisters/notifiers/index.did deleted file mode 100644 index 9ea58c8334..0000000000 --- a/tests/end_to_end/candid_rpc/class_syntax/func_types/canisters/notifiers/index.did +++ /dev/null @@ -1,3 +0,0 @@ -service: () -> { - getNotifier: () -> (func (vec nat8) -> () oneway) query; -} diff --git a/tests/end_to_end/candid_rpc/class_syntax/heartbeat/src/heartbeat_async/index.ts b/tests/end_to_end/candid_rpc/class_syntax/heartbeat/src/heartbeat_async/index.ts index 8736b13132..b4a89571e0 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/heartbeat/src/heartbeat_async/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/heartbeat/src/heartbeat_async/index.ts @@ -1,19 +1,19 @@ import { call, heartbeat, IDL, query } from 'azle'; -let initialized: Uint8Array = Uint8Array.from([]); - export default class { + initialized: Uint8Array = Uint8Array.from([]); + @heartbeat async heartbeat(): Promise { const randomness = await getRandomness(); - initialized = randomness; + this.initialized = randomness; console.info('heartbeat initialized', randomness.length); } @query([], IDL.Vec(IDL.Nat8)) getInitialized(): Uint8Array { - return initialized; + return this.initialized; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/heartbeat/src/heartbeat_sync/index.ts b/tests/end_to_end/candid_rpc/class_syntax/heartbeat/src/heartbeat_sync/index.ts index c55e352801..a99245be7a 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/heartbeat/src/heartbeat_sync/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/heartbeat/src/heartbeat_sync/index.ts @@ -1,16 +1,16 @@ import { heartbeat, IDL, query } from 'azle'; -let initialized = false; - export default class { + initialized = false; + @heartbeat heartbeat(): void { - initialized = true; - console.info('heartbeat initialized', initialized); + this.initialized = true; + console.info('heartbeat initialized', this.initialized); } @query([], IDL.Bool) getInitialized(): boolean { - return initialized; + return this.initialized; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/init/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/init/src/index.ts index 864949b907..98c40756a2 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/init/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/init/src/index.ts @@ -13,30 +13,30 @@ const Reaction = IDL.Variant({ }); type Reaction = { Fire: null } | { Wave: null }; -let user: [User] | [] = []; -let reaction: [Reaction] | [] = []; -let owner: [Principal] | [] = []; - export default class { + user: [User] | [] = []; + reaction: [Reaction] | [] = []; + owner: [Principal] | [] = []; + @init([User, Reaction, IDL.Principal]) init(initUser: User, initReaction: Reaction, initOwner: Principal): void { - user = [initUser]; - reaction = [initReaction]; - owner = [initOwner]; + this.user = [initUser]; + this.reaction = [initReaction]; + this.owner = [initOwner]; } @query([], IDL.Opt(User)) getUser(): [User] | [] { - return user; + return this.user; } @query([], IDL.Opt(Reaction)) getReaction(): [Reaction] | [] { - return reaction; + return this.reaction; } @query([], IDL.Opt(IDL.Principal)) getOwner(): [Principal] | [] { - return owner; + return this.owner; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/key_value_store/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/key_value_store/src/index.ts index a0a2ee60e4..680aaca565 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/key_value_store/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/key_value_store/src/index.ts @@ -1,17 +1,17 @@ import { IDL, query, update } from 'azle'; -let store: Map = new Map(); - export default class { + store: Map = new Map(); + @query([IDL.Text], IDL.Opt(IDL.Text)) get(key: string): [string] | [] { - const keyOrUndefined = store.get(key); + const keyOrUndefined = this.store.get(key); return keyOrUndefined ? [keyOrUndefined] : []; } @update([IDL.Text, IDL.Text]) set(key: string, value: string): void { - store.set(key, value); + this.store.set(key, value); } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/management_canister/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/management_canister/src/index.ts index fbbe51a289..6551ec24ca 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/management_canister/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/management_canister/src/index.ts @@ -29,16 +29,16 @@ type State = { createdCanisterId: Principal; }; -let state: State = { - createdCanisterId: Principal.fromText('aaaaa-aa') -}; - export default class { + state: State = { + createdCanisterId: Principal.fromText('aaaaa-aa') + }; + @update([], CreateCanisterResult) async executeCreateCanister(): Promise { const createCanisterResult = await createCanister(); - state.createdCanisterId = createCanisterResult.canister_id; + this.state.createdCanisterId = createCanisterResult.canister_id; return createCanisterResult; } @@ -298,7 +298,7 @@ export default class { @query([], IDL.Principal) getCreatedCanisterId(): Principal { - return state.createdCanisterId; + return this.state.createdCanisterId; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/calc/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/calc/src/index.ts index d6240d90a7..12b333ba86 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/calc/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/calc/src/index.ts @@ -1,27 +1,27 @@ import { IDL, update } from 'azle'; -let cell: bigint = 0n; - export default class { + cell: bigint = 0n; + @update([IDL.Int], IDL.Int) add(n: bigint): bigint { - cell += n; + this.cell += n; - return cell; + return this.cell; } @update([IDL.Int], IDL.Int) sub(n: bigint): bigint { - cell -= n; + this.cell -= n; - return cell; + return this.cell; } @update([IDL.Int], IDL.Int) mul(n: bigint): bigint { - cell *= n; + this.cell *= n; - return cell; + return this.cell; } @update([IDL.Int], IDL.Opt(IDL.Int)) @@ -29,13 +29,13 @@ export default class { if (n === 0n) { return []; } else { - cell /= n; - return [cell]; + this.cell /= n; + return [this.cell]; } } @update([]) clearall(): void { - cell = 0n; + this.cell = 0n; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/counter/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/counter/src/index.ts index ffa0362219..fa3579507d 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/counter/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/counter/src/index.ts @@ -1,20 +1,20 @@ import { IDL, query, update } from 'azle'; -let counter: bigint = 0n; - export default class { + counter: bigint = 0n; + @query([], IDL.Nat) get(): bigint { - return counter; + return this.counter; } @update([IDL.Nat]) set(n: bigint): void { - counter = n; + this.counter = n; } @update([]) inc(): void { - counter += 1n; + this.counter += 1n; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/http_counter/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/http_counter/src/index.ts index 1f9d643e2f..ca0299d6cb 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/http_counter/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/http_counter/src/index.ts @@ -97,12 +97,12 @@ export type HttpResponse = { streaming_strategy: [StreamingStrategy] | []; }; -let stableStorage = StableBTreeMap(0); - export default class { + stableStorage = StableBTreeMap(0); + @init([]) init(): void { - stableStorage.insert('counter', 0n); + this.stableStorage.insert('counter', 0n); } @query([HttpRequest], HttpResponse) @@ -128,7 +128,7 @@ export default class { }; } - const counter = stableStorage.get('counter'); + const counter = this.stableStorage.get('counter'); if (counter === null) { trap('counter does not exist'); @@ -185,16 +185,16 @@ export default class { @update([HttpRequest], HttpResponse) http_request_update(req: HttpRequest): HttpResponse { if (req.method === 'POST') { - const counterOpt = stableStorage.get('counter'); + const counterOpt = this.stableStorage.get('counter'); const counter = counterOpt === null ? trap('counter does not exist') : counterOpt; - stableStorage.insert('counter', counter + 1n); + this.stableStorage.insert('counter', counter + 1n); if (req.headers.find(isGzip) === undefined) { - const counterOpt = stableStorage.get('counter'); + const counterOpt = this.stableStorage.get('counter'); const counter = counterOpt === null ? trap('counter does not exist') @@ -248,7 +248,7 @@ export default class { }; } case 'next': { - const counterOpt = stableStorage.get('counter'); + const counterOpt = this.stableStorage.get('counter'); const counter = counterOpt === null ? trap('counter does not exist') diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts index 775c4a219c..aa91039d56 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/minimal-counter-dapp/src/minimal_dapp/index.ts @@ -1,24 +1,24 @@ import { IDL, query, update } from 'azle'; -let counter: bigint = 0n; - export default class { + counter: bigint = 0n; + @update([], IDL.Nat) count(): bigint { - counter += 1n; + this.counter += 1n; - return counter; + return this.counter; } @query([], IDL.Nat) getCount(): bigint { - return counter; + return this.counter; } @update([], IDL.Nat) reset(): bigint { - counter = 0n; + this.counter = 0n; - return counter; + return this.counter; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/persistent-storage/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/persistent-storage/src/index.ts index 184cb85c71..1907efe711 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/persistent-storage/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/persistent-storage/src/index.ts @@ -8,28 +8,28 @@ import { update } from 'azle'; -let stableStorage = StableBTreeMap(0); -let redeployed = false; - export default class { + stableStorage = StableBTreeMap(0); + redeployed = false; + @init([]) init(): void { - stableStorage.insert('counter', 0n); + this.stableStorage.insert('counter', 0n); } @postUpgrade([]) postUpgrade(): void { - redeployed = true; + this.redeployed = true; } @query([], IDL.Bool) getRedeployed(): boolean { - return redeployed; + return this.redeployed; } @update([], IDL.Nat) increment(): bigint { - let counter = stableStorage.get('counter'); + let counter = this.stableStorage.get('counter'); if (counter === null) { trap('counter not defined'); @@ -37,14 +37,14 @@ export default class { const incrementedCounter = counter + 1n; - stableStorage.insert('counter', incrementedCounter); + this.stableStorage.insert('counter', incrementedCounter); return incrementedCounter; } @query([], IDL.Nat) get(): bigint { - const counter = stableStorage.get('counter'); + const counter = this.stableStorage.get('counter'); if (counter === null) { trap('counter not defined'); @@ -55,9 +55,9 @@ export default class { @update([], IDL.Nat) reset(): bigint { - stableStorage.insert('counter', 0n); + this.stableStorage.insert('counter', 0n); - const counter = stableStorage.get('counter'); + const counter = this.stableStorage.get('counter'); if (counter === null) { trap('counter not defined'); diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/phone-book/src/phone_book/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/phone-book/src/phone_book/index.ts index a9ce850ced..af5045cf58 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/phone-book/src/phone_book/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/phone-book/src/phone_book/index.ts @@ -9,17 +9,17 @@ export type Entry = { phone: string; }; -let phoneBook = new Map(); - export default class { + phoneBook = new Map(); + @update([IDL.Text, Entry]) insert(name: string, entry: Entry): void { - phoneBook.set(name, entry); + this.phoneBook.set(name, entry); } @query([IDL.Text], IDL.Opt(Entry)) lookup(name: string): [Entry] | [] { - const entryOrUndefined = phoneBook.get(name); + const entryOrUndefined = this.phoneBook.get(name); return entryOrUndefined ? [entryOrUndefined] : []; } diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/simple-to-do/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/simple-to-do/src/index.ts index 1018030fef..486a180a1f 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/simple-to-do/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/simple-to-do/src/index.ts @@ -9,34 +9,34 @@ export type ToDo = { completed: boolean; }; -let todos: Map = new Map(); -let nextId: bigint = 0n; - export default class { + todos: Map = new Map(); + nextId: bigint = 0n; + @query([], IDL.Vec(ToDo)) getTodos(): ToDo[] { - return Array.from(todos.values()); + return Array.from(this.todos.values()); } // Returns the ID that was given to the ToDo item @update([IDL.Text], IDL.Nat) addTodo(description: string): bigint { - const id = nextId; - todos.set(id, { + const id = this.nextId; + this.todos.set(id, { description: description, completed: false }); - nextId += 1n; + this.nextId += 1n; return id; } @update([IDL.Nat]) completeTodo(id: bigint): void { - let todo = todos.get(id); + let todo = this.todos.get(id); if (todo !== undefined) { - todos.set(id, { + this.todos.set(id, { description: todo.description, completed: true }); @@ -46,7 +46,7 @@ export default class { @query([], IDL.Text) showTodos(): string { let output = '\n___TO-DOs___'; - for (const todoEntry of [...todos]) { + for (const todoEntry of [...this.todos]) { output += `\n${todoEntry[1].description}`; if (todoEntry[1].completed) { output += ' ✔'; @@ -65,6 +65,8 @@ export default class { // ); // ``` // See: https://github.com/demergent-labs/azle/issues/574 - todos = new Map([...todos].filter(([_key, value]) => !value.completed)); + this.todos = new Map( + [...this.todos].filter(([_key, value]) => !value.completed) + ); } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/superheroes/src/superheroes/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/superheroes/src/superheroes/index.ts index 80160da981..3ef35e1ea2 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/superheroes/src/superheroes/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/superheroes/src/superheroes/index.ts @@ -19,25 +19,25 @@ type Superhero = { }; /** - * Application State + * High-Level API */ +export default class { + /** + * Application State + */ -// The next available superhero identifier. -let next: SuperheroId = 0; + // The next available superhero identifier. + next: SuperheroId = 0; -// The superhero data store. -let superheroes: Map = new Map(); + // The superhero data store. + superheroes: Map = new Map(); -/** - * High-Level API - */ -export default class { // Create a superhero. @update([Superhero], SuperheroId) create(superhero: Superhero): SuperheroId { - let superheroId = next; - next += 1; - superheroes.set(superheroId, superhero); + let superheroId = this.next; + this.next += 1; + this.superheroes.set(superheroId, superhero); return superheroId; } @@ -45,16 +45,16 @@ export default class { // Read a superhero. @query([SuperheroId], IDL.Opt(Superhero)) read(superheroId: SuperheroId): [Superhero] | [] { - const superheroOrUndefined = superheroes.get(superheroId); + const superheroOrUndefined = this.superheroes.get(superheroId); return superheroOrUndefined ? [superheroOrUndefined] : []; } // Update a superhero. @update([SuperheroId, Superhero], IDL.Bool) update(superheroId: SuperheroId, superhero: Superhero): boolean { - let result = superheroes.get(superheroId); + let result = this.superheroes.get(superheroId); if (result) { - superheroes.set(superheroId, superhero); + this.superheroes.set(superheroId, superhero); } return !!result; @@ -63,9 +63,9 @@ export default class { // Delete a superhero. @update([SuperheroId], IDL.Bool) deleteHero(superheroId: SuperheroId): boolean { - let result = superheroes.get(superheroId); + let result = this.superheroes.get(superheroId); if (result) { - superheroes.delete(superheroId); + this.superheroes.delete(superheroId); } return !!result; diff --git a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/whoami/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/whoami/src/index.ts index 477d7d13de..f1ecb2aa35 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/whoami/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/motoko_examples/whoami/src/index.ts @@ -10,45 +10,51 @@ import { update } from 'azle'; -// We use the zero principal but any principal could be used. -let install: Principal = Principal.fromText('aaaaa-aa'); -let someone: Principal = Principal.fromText('aaaaa-aa'); - class WhoAmI { + // We use the zero principal but any principal could be used. + install: Principal = Principal.fromText('aaaaa-aa'); + someone: Principal = Principal.fromText('aaaaa-aa'); + // Manually save the calling principal and argument for later access. @init([IDL.Principal]) init(somebody: Principal): void { - install = caller(); - someone = somebody; + this.install = caller(); + this.someone = somebody; } + // Manually re-save these variables after new deploys. @postUpgrade([IDL.Principal]) postUpgrade(somebody: Principal): void { - install = caller(); - someone = somebody; + this.install = caller(); + this.someone = somebody; } + // Return the principal identifier of the wallet canister that installed this // canister. @query([], IDL.Principal) installer(): Principal { - return install; + return this.install; } + // Return the principal identifier that was provided as an installation // argument to this canister. @query([], IDL.Principal) argument(): Principal { - return someone; + return this.someone; } + // Return the principal identifier of the caller of this method. @update([], IDL.Principal) whoami(): Principal { return caller(); } + // Return the principal identifier of this canister. @update([], IDL.Principal) async id(): Promise { return await call(id(), 'whoami', { returnIdlType: IDL.Principal }); } + // Return the principal identifier of this canister via the global `ic` object. // This is much quicker than `id()` above because it isn't making a cross- // canister call to itself. Additionally, it can now be a `Query` which means it diff --git a/tests/end_to_end/candid_rpc/class_syntax/notify_raw/src/canister2/index.ts b/tests/end_to_end/candid_rpc/class_syntax/notify_raw/src/canister2/index.ts index c83c76d487..2f9eb40930 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/notify_raw/src/canister2/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/notify_raw/src/canister2/index.ts @@ -1,15 +1,15 @@ import { IDL, query, update } from 'azle'; -let notified: boolean = false; - export default class { + notified: boolean = false; + @update([]) receiveNotification(): void { - notified = true; + this.notified = true; } @query([], IDL.Bool) getNotified(): boolean { - return notified; + return this.notified; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/pre_and_post_upgrade/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/pre_and_post_upgrade/src/index.ts index 9387c0df33..e959246d3a 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/pre_and_post_upgrade/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/pre_and_post_upgrade/src/index.ts @@ -17,31 +17,31 @@ type Entry = { value: bigint; }; -let stableStorage = StableBTreeMap(0); +export default class { + stableStorage = StableBTreeMap(0); -let entries: { - [key: string]: bigint; -} = {}; + entries: { + [key: string]: bigint; + } = {}; -export default class { @init([]) init(): void { console.info('init'); - stableStorage.insert('entries', []); + this.stableStorage.insert('entries', []); } @postUpgrade([]) postUpgrade(): void { console.info('postUpgrade'); - const stableEntries = stableStorage.get('entries'); + const stableEntries = this.stableStorage.get('entries'); if (stableEntries === null) { return; } - entries = stableEntries.reduce((result, entry) => { + this.entries = stableEntries.reduce((result, entry) => { return { ...result, [entry.key]: entry.value @@ -53,9 +53,9 @@ export default class { preUpgrade(): void { console.info('preUpgrade'); - stableStorage.insert( + this.stableStorage.insert( 'entries', - Object.entries(entries).map((entry) => { + Object.entries(this.entries).map((entry) => { return { key: entry[0], value: entry[1] + 1n @@ -66,12 +66,12 @@ export default class { @update([Entry]) setEntry(entry: Entry): void { - entries[entry.key] = entry.value; + this.entries[entry.key] = entry.value; } @query([], IDL.Vec(Entry)) getEntries(): Entry[] { - return Object.entries(entries).map((entry) => { + return Object.entries(this.entries).map((entry) => { return { key: entry[0], value: entry[1] diff --git a/tests/end_to_end/candid_rpc/class_syntax/randomness/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/randomness/src/index.ts index 79ed8c1bc9..7eecc299b0 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/randomness/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/randomness/src/index.ts @@ -1,16 +1,16 @@ import { IDL, postUpgrade, query, update } from 'azle'; -let redeployed = false; - export default class { + redeployed = false; + @postUpgrade([]) postUpgrade(): void { - redeployed = true; + this.redeployed = true; } @query([], IDL.Bool) getRedeployed(): boolean { - return redeployed; + return this.redeployed; } @update([], IDL.Float64) diff --git a/tests/end_to_end/candid_rpc/class_syntax/recursion/src/recursive_canister/index.did b/tests/end_to_end/candid_rpc/class_syntax/recursion/src/recursive_canister/index.did deleted file mode 100644 index 7c6f8c66e8..0000000000 --- a/tests/end_to_end/candid_rpc/class_syntax/recursion/src/recursive_canister/index.did +++ /dev/null @@ -1,6 +0,0 @@ -type rec_0 = service {getMessage: () -> (text) query; myQuery: (rec_0) -> (rec_0) query;}; -type rec_1 = service {getMessage: () -> (text) query; myQuery: (rec_1) -> (rec_1) query;}; -service: (text) -> { - getMessage: () -> (text) query; - myQuery: (rec_0) -> (rec_1) query; -} diff --git a/tests/end_to_end/candid_rpc/class_syntax/recursion/src/recursive_canister/index.ts b/tests/end_to_end/candid_rpc/class_syntax/recursion/src/recursive_canister/index.ts index 67e72a38ad..333a6c7558 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/recursion/src/recursive_canister/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/recursion/src/recursive_canister/index.ts @@ -2,12 +2,12 @@ import { IDL, init, query } from 'azle'; import { MyCanister } from './types'; -let myMessage = ''; - export default class { + myMessage = ''; + @init([IDL.Text]) init(message: string): void { - myMessage = message; + this.myMessage = message; } @query([MyCanister], MyCanister) @@ -17,6 +17,6 @@ export default class { @query([], IDL.Text) getMessage(): string { - return myMessage; + return this.myMessage; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/rejections/src/nonexistent.did b/tests/end_to_end/candid_rpc/class_syntax/rejections/src/nonexistent.did deleted file mode 100644 index 1b175230bd..0000000000 --- a/tests/end_to_end/candid_rpc/class_syntax/rejections/src/nonexistent.did +++ /dev/null @@ -1,3 +0,0 @@ -service: () -> { - method: () -> (); -} diff --git a/tests/end_to_end/candid_rpc/class_syntax/rejections/src/some_canister/index.did b/tests/end_to_end/candid_rpc/class_syntax/rejections/src/some_canister/index.did deleted file mode 100644 index 4cef085492..0000000000 --- a/tests/end_to_end/candid_rpc/class_syntax/rejections/src/some_canister/index.did +++ /dev/null @@ -1,5 +0,0 @@ -service: () -> { - accept: () -> (bool) query; - error: () -> (empty) query; - reject: (text) -> (empty) query; -} diff --git a/tests/end_to_end/candid_rpc/class_syntax/simple_erc20/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/simple_erc20/src/index.ts index 8bd3c37018..2df1fba9a5 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/simple_erc20/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/simple_erc20/src/index.ts @@ -14,14 +14,14 @@ type State = { totalSupply: bigint; }; -let state: State = { - accounts: {}, - name: '', - ticker: '', - totalSupply: 0n -}; - export default class { + state: State = { + accounts: {}, + name: '', + ticker: '', + totalSupply: 0n + }; + @update([IDL.Text, IDL.Text, IDL.Text, IDL.Nat64], IDL.Bool) initializeSupply( name: string, @@ -29,8 +29,8 @@ export default class { ticker: string, totalSupply: bigint ): boolean { - state = { - ...state, + this.state = { + ...this.state, accounts: { [originalAddress]: { address: originalAddress, @@ -47,36 +47,36 @@ export default class { @update([IDL.Text, IDL.Text, IDL.Nat64], IDL.Bool) transfer(fromAddress: string, toAddress: string, amount: bigint): boolean { - if (state.accounts[toAddress] === undefined) { - state.accounts[toAddress] = { + if (this.state.accounts[toAddress] === undefined) { + this.state.accounts[toAddress] = { address: toAddress, balance: 0n }; } - state.accounts[fromAddress].balance -= amount; - state.accounts[toAddress].balance += amount; + this.state.accounts[fromAddress].balance -= amount; + this.state.accounts[toAddress].balance += amount; return true; } @query([IDL.Text], IDL.Nat64) balance(address: string): bigint { - return state.accounts[address]?.balance ?? 0n; + return this.state.accounts[address]?.balance ?? 0n; } @query([], IDL.Text) ticker(): string { - return state.ticker; + return this.state.ticker; } @query([], IDL.Text) name(): string { - return state.name; + return this.state.name; } @query([], IDL.Nat64) totalSupply(): bigint { - return state.totalSupply; + return this.state.totalSupply; } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/simple_user_accounts/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/simple_user_accounts/src/index.ts index 8b0ae04109..7818a3165b 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/simple_user_accounts/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/simple_user_accounts/src/index.ts @@ -6,10 +6,6 @@ type Db = { }; }; -let db: Db = { - users: {} -}; - const User = IDL.Record({ id: IDL.Text, username: IDL.Text @@ -20,26 +16,30 @@ type User = { }; export default class { + db: Db = { + users: {} + }; + @query([IDL.Text], IDL.Opt(User)) getUserById(id: string): [User] | [] { - const userOrUndefined = db.users[id]; + const userOrUndefined = this.db.users[id]; return userOrUndefined ? [userOrUndefined] : []; } @query([], IDL.Vec(User)) getAllUsers(): User[] { - return Object.values(db.users); + return Object.values(this.db.users); } @update([IDL.Text], User) createUser(username: string): User { - const id = Object.keys(db.users).length.toString(); + const id = Object.keys(this.db.users).length.toString(); const user: User = { id, username }; - db.users[id] = user; + this.db.users[id] = user; return user; } diff --git a/tests/end_to_end/candid_rpc/class_syntax/stable_b_tree_map_instruction_threshold/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/stable_b_tree_map_instruction_threshold/src/index.ts index a65ecf9d71..44a7b99409 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/stable_b_tree_map_instruction_threshold/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/stable_b_tree_map_instruction_threshold/src/index.ts @@ -8,8 +8,6 @@ type SmallRecord = { id: Principal; }; -let smallRecordMap = StableBTreeMap(0); - const MediumRecord = IDL.Record({ id: IDL.Text, username: IDL.Text, @@ -23,8 +21,6 @@ type MediumRecord = { internetIdentity: Principal; }; -let mediumRecordMap = StableBTreeMap(1); - const LargeRecord = IDL.Record({ id: IDL.Text, username: IDL.Text, @@ -44,15 +40,17 @@ type LargeRecord = { mediumRecord: MediumRecord; }; -let largeRecordMap = StableBTreeMap(2); - export default class { + smallRecordMap = StableBTreeMap(0); + mediumRecordMap = StableBTreeMap(1); + largeRecordMap = StableBTreeMap(2); + @update([IDL.Nat32]) insertSmallRecord(numToInsert: number): void { for (let i = 0; i < numToInsert; i++) { const id = v4(); - smallRecordMap.insert(id, { + this.smallRecordMap.insert(id, { id: Principal.fromText('aaaaa-aa') }); } @@ -60,17 +58,17 @@ export default class { @query([IDL.Nat32], IDL.Vec(IDL.Text)) keysSmallRecord(numToReturn: number): string[] { - return smallRecordMap.keys(0, numToReturn); + return this.smallRecordMap.keys(0, numToReturn); } @query([IDL.Nat32], IDL.Vec(SmallRecord)) valuesSmallRecord(numToReturn: number): SmallRecord[] { - return smallRecordMap.values(0, numToReturn); + return this.smallRecordMap.values(0, numToReturn); } @query([IDL.Nat32], IDL.Vec(IDL.Tuple(IDL.Text, SmallRecord))) itemsSmallRecord(numToReturn: number): [string, SmallRecord][] { - return smallRecordMap.items(0, numToReturn); + return this.smallRecordMap.items(0, numToReturn); } @update([IDL.Nat32]) @@ -78,7 +76,7 @@ export default class { for (let i = 0; i < numToInsert; i++) { const id = v4(); - mediumRecordMap.insert(id, { + this.mediumRecordMap.insert(id, { id, username: `lastmjs${i}`, age: BigInt(i), @@ -89,17 +87,17 @@ export default class { @query([IDL.Nat32], IDL.Vec(IDL.Text)) keysMediumRecord(numToReturn: number): string[] { - return mediumRecordMap.keys(0, numToReturn); + return this.mediumRecordMap.keys(0, numToReturn); } @query([IDL.Nat32], IDL.Vec(MediumRecord)) valuesMediumRecord(numToReturn: number): MediumRecord[] { - return mediumRecordMap.values(0, numToReturn); + return this.mediumRecordMap.values(0, numToReturn); } @query([IDL.Nat32], IDL.Vec(IDL.Tuple(IDL.Text, MediumRecord))) itemsMediumRecord(numToReturn: number): [string, MediumRecord][] { - return mediumRecordMap.items(0, numToReturn); + return this.mediumRecordMap.items(0, numToReturn); } @update([IDL.Nat32]) @@ -107,7 +105,7 @@ export default class { for (let i = 0; i < numToInsert; i++) { const id = v4(); - largeRecordMap.insert(id, { + this.largeRecordMap.insert(id, { id, username: `lastmjs${i}`, age: BigInt(i), @@ -126,16 +124,16 @@ export default class { @query([IDL.Nat32], IDL.Vec(IDL.Text)) keysLargeRecord(numToReturn: number): string[] { - return largeRecordMap.keys(0, numToReturn); + return this.largeRecordMap.keys(0, numToReturn); } @query([IDL.Nat32], IDL.Vec(LargeRecord)) valuesLargeRecord(numToReturn: number): LargeRecord[] { - return largeRecordMap.values(0, numToReturn); + return this.largeRecordMap.values(0, numToReturn); } @query([IDL.Nat32], IDL.Vec(IDL.Tuple(IDL.Text, LargeRecord))) itemsLargeRecord(numToReturn: number): [string, LargeRecord][] { - return largeRecordMap.items(0, numToReturn); + return this.largeRecordMap.items(0, numToReturn); } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister1/index.ts b/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister1/index.ts index f3211f75ab..d19c3293c6 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister1/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister1/index.ts @@ -2,13 +2,13 @@ import { IDL, query, StableBTreeMap, update } from 'azle'; import { Reaction, User } from '../types'; -let stableMap0 = StableBTreeMap(0); -let stableMap1 = StableBTreeMap(1); -let stableMap2 = StableBTreeMap(2); -let stableMap3 = StableBTreeMap(3); -let stableMap4 = StableBTreeMap(4); - export default class { + stableMap0 = StableBTreeMap(0); + stableMap1 = StableBTreeMap(1); + stableMap2 = StableBTreeMap(2); + stableMap3 = StableBTreeMap(3); + stableMap4 = StableBTreeMap(4); + @query([], IDL.Bool) getRedeployed(): boolean { return globalThis._azlePostUpgradeCalled; @@ -18,12 +18,12 @@ export default class { @query([IDL.Nat8], IDL.Bool) stableMap0ContainsKey(key: number): boolean { - return stableMap0.containsKey(key); + return this.stableMap0.containsKey(key); } @query([IDL.Nat8], IDL.Opt(IDL.Text)) stableMap0Get(key: number): [string] | [] { - const result = stableMap0.get(key); + const result = this.stableMap0.get(key); if (result === null) { return []; } else { @@ -33,7 +33,7 @@ export default class { @update([IDL.Nat8, IDL.Text], IDL.Opt(IDL.Text)) stableMap0Insert(key: number, value: string): [string] | [] { - const result = stableMap0.insert(key, value); + const result = this.stableMap0.insert(key, value); if (result === null) { return []; } else { @@ -43,27 +43,27 @@ export default class { @query([], IDL.Bool) stableMap0IsEmpty(): boolean { - return stableMap0.isEmpty(); + return this.stableMap0.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Nat8, IDL.Text))) stableMap0Items(): [number, string][] { - return stableMap0.items(); + return this.stableMap0.items(); } @query([], IDL.Vec(IDL.Nat8)) stableMap0Keys(): Uint8Array { - return Uint8Array.from(stableMap0.keys()); + return Uint8Array.from(this.stableMap0.keys()); } @query([], IDL.Nat64) stableMap0Len(): bigint { - return stableMap0.len(); + return this.stableMap0.len(); } @update([IDL.Nat8], IDL.Opt(IDL.Text)) stableMap0Remove(key: number): [string] | [] { - const result = stableMap0.remove(key); + const result = this.stableMap0.remove(key); if (result === null) { return []; } else { @@ -73,19 +73,19 @@ export default class { @query([], IDL.Vec(IDL.Text)) stableMap0Values(): string[] { - return stableMap0.values(); + return this.stableMap0.values(); } // stableMap1 methods @query([IDL.Nat16], IDL.Bool) stableMap1ContainsKey(key: number): boolean { - return stableMap1.containsKey(key); + return this.stableMap1.containsKey(key); } @query([IDL.Nat16], IDL.Opt(IDL.Vec(IDL.Nat8))) stableMap1Get(key: number): [Uint8Array] | [] { - const result = stableMap1.get(key); + const result = this.stableMap1.get(key); if (result === null) { return []; } else { @@ -95,7 +95,7 @@ export default class { @update([IDL.Nat16, IDL.Vec(IDL.Nat8)], IDL.Opt(IDL.Vec(IDL.Nat8))) stableMap1Insert(key: number, value: Uint8Array): [Uint8Array] | [] { - const result = stableMap1.insert(key, value); + const result = this.stableMap1.insert(key, value); if (result === null) { return []; } else { @@ -105,27 +105,27 @@ export default class { @query([], IDL.Bool) stableMap1IsEmpty(): boolean { - return stableMap1.isEmpty(); + return this.stableMap1.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Nat16, IDL.Vec(IDL.Nat8)))) stableMap1Items(): [number, Uint8Array][] { - return stableMap1.items(); + return this.stableMap1.items(); } @query([], IDL.Vec(IDL.Nat16)) stableMap1Keys(): number[] { - return stableMap1.keys(); + return this.stableMap1.keys(); } @query([], IDL.Nat64) stableMap1Len(): bigint { - return stableMap1.len(); + return this.stableMap1.len(); } @update([IDL.Nat16], IDL.Opt(IDL.Vec(IDL.Nat8))) stableMap1Remove(key: number): [Uint8Array] | [] { - const result = stableMap1.remove(key); + const result = this.stableMap1.remove(key); if (result === null) { return []; } else { @@ -135,19 +135,19 @@ export default class { @query([], IDL.Vec(IDL.Vec(IDL.Nat8))) stableMap1Values(): Uint8Array[] { - return stableMap1.values(); + return this.stableMap1.values(); } // stableMap2 methods @query([IDL.Nat32], IDL.Bool) stableMap2ContainsKey(key: number): boolean { - return stableMap2.containsKey(key); + return this.stableMap2.containsKey(key); } @query([IDL.Nat32], IDL.Opt(IDL.Nat)) stableMap2Get(key: number): [bigint] | [] { - const result = stableMap2.get(key); + const result = this.stableMap2.get(key); if (result === null) { return []; } else { @@ -157,7 +157,7 @@ export default class { @update([IDL.Nat32, IDL.Nat], IDL.Opt(IDL.Nat)) stableMap2Insert(key: number, value: bigint): [bigint] | [] { - const result = stableMap2.insert(key, value); + const result = this.stableMap2.insert(key, value); if (result === null) { return []; } else { @@ -167,27 +167,27 @@ export default class { @query([], IDL.Bool) stableMap2IsEmpty(): boolean { - return stableMap2.isEmpty(); + return this.stableMap2.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Nat32, IDL.Nat))) stableMap2Items(): [number, bigint][] { - return stableMap2.items(); + return this.stableMap2.items(); } @query([], IDL.Vec(IDL.Nat32)) stableMap2Keys(): number[] { - return stableMap2.keys(); + return this.stableMap2.keys(); } @query([], IDL.Nat64) stableMap2Len(): bigint { - return stableMap2.len(); + return this.stableMap2.len(); } @update([IDL.Nat32], IDL.Opt(IDL.Nat)) stableMap2Remove(key: number): [bigint] | [] { - const result = stableMap2.remove(key); + const result = this.stableMap2.remove(key); if (result === null) { return []; } else { @@ -197,19 +197,19 @@ export default class { @query([], IDL.Vec(IDL.Nat)) stableMap2Values(): bigint[] { - return stableMap2.values(); + return this.stableMap2.values(); } // stableMap3 methods @query([Reaction], IDL.Bool) stableMap3ContainsKey(key: Reaction): boolean { - return stableMap3.containsKey(key); + return this.stableMap3.containsKey(key); } @query([Reaction], IDL.Opt(IDL.Int)) stableMap3Get(key: Reaction): [bigint] | [] { - const result = stableMap3.get(key); + const result = this.stableMap3.get(key); if (result === null) { return []; } else { @@ -219,7 +219,7 @@ export default class { @update([Reaction, IDL.Int], IDL.Opt(IDL.Int)) stableMap3Insert(key: Reaction, value: bigint): [bigint] | [] { - const result = stableMap3.insert(key, value); + const result = this.stableMap3.insert(key, value); if (result === null) { return []; } else { @@ -229,27 +229,27 @@ export default class { @query([], IDL.Bool) stableMap3IsEmpty(): boolean { - return stableMap3.isEmpty(); + return this.stableMap3.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(Reaction, IDL.Int))) stableMap3Items(): [Reaction, bigint][] { - return stableMap3.items(); + return this.stableMap3.items(); } @query([], IDL.Vec(Reaction)) stableMap3Keys(): Reaction[] { - return stableMap3.keys(); + return this.stableMap3.keys(); } @query([], IDL.Nat64) stableMap3Len(): bigint { - return stableMap3.len(); + return this.stableMap3.len(); } @update([Reaction], IDL.Opt(IDL.Int)) stableMap3Remove(key: Reaction): [bigint] | [] { - const result = stableMap3.remove(key); + const result = this.stableMap3.remove(key); if (result === null) { return []; } else { @@ -259,19 +259,19 @@ export default class { @query([], IDL.Vec(IDL.Int)) stableMap3Values(): bigint[] { - return stableMap3.values(); + return this.stableMap3.values(); } // stableMap4 methods @query([User], IDL.Bool) stableMap4ContainsKey(key: User): boolean { - return stableMap4.containsKey(key); + return this.stableMap4.containsKey(key); } @query([User], IDL.Opt(IDL.Float32)) stableMap4Get(key: User): [number] | [] { - const result = stableMap4.get(key); + const result = this.stableMap4.get(key); if (result === null) { return []; } else { @@ -281,7 +281,7 @@ export default class { @update([User, IDL.Float32], IDL.Opt(IDL.Float32)) stableMap4Insert(key: User, value: number): [number] | [] { - const result = stableMap4.insert(key, value); + const result = this.stableMap4.insert(key, value); if (result === null) { return []; } else { @@ -291,27 +291,27 @@ export default class { @query([], IDL.Bool) stableMap4IsEmpty(): boolean { - return stableMap4.isEmpty(); + return this.stableMap4.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(User, IDL.Float32))) stableMap4Items(): [User, number][] { - return stableMap4.items(); + return this.stableMap4.items(); } @query([], IDL.Vec(User)) stableMap4Keys(): User[] { - return stableMap4.keys(); + return this.stableMap4.keys(); } @query([], IDL.Nat64) stableMap4Len(): bigint { - return stableMap4.len(); + return this.stableMap4.len(); } @update([User], IDL.Opt(IDL.Float32)) stableMap4Remove(key: User): [number] | [] { - const result = stableMap4.remove(key); + const result = this.stableMap4.remove(key); if (result === null) { return []; } else { @@ -321,6 +321,6 @@ export default class { @query([], IDL.Vec(IDL.Float32)) stableMap4Values(): number[] { - return stableMap4.values(); + return this.stableMap4.values(); } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister2/index.ts b/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister2/index.ts index d0dc3dbbb2..c069ccea57 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister2/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister2/index.ts @@ -1,12 +1,12 @@ import { IDL, query, StableBTreeMap, update } from 'azle'; -let stableMap5 = StableBTreeMap<[string] | [], number>(5); -let stableMap6 = StableBTreeMap(6); -let stableMap7 = StableBTreeMap(7); -let stableMap8 = StableBTreeMap(8); -let stableMap9 = StableBTreeMap(9); - export default class { + stableMap5 = StableBTreeMap<[string] | [], number>(5); + stableMap6 = StableBTreeMap(6); + stableMap7 = StableBTreeMap(7); + stableMap8 = StableBTreeMap(8); + stableMap9 = StableBTreeMap(9); + @query([], IDL.Bool) getRedeployed(): boolean { return globalThis._azlePostUpgradeCalled; @@ -16,12 +16,12 @@ export default class { @query([IDL.Opt(IDL.Text)], IDL.Bool) stableMap5ContainsKey(key: [string] | []): boolean { - return stableMap5.containsKey(key); + return this.stableMap5.containsKey(key); } @query([IDL.Opt(IDL.Text)], IDL.Opt(IDL.Float64)) stableMap5Get(key: [string] | []): [number] | [] { - const result = stableMap5.get(key); + const result = this.stableMap5.get(key); if (result === null) { return []; } else { @@ -31,7 +31,7 @@ export default class { @update([IDL.Opt(IDL.Text), IDL.Float64], IDL.Opt(IDL.Float64)) stableMap5Insert(key: [string] | [], value: number): [number] | [] { - const result = stableMap5.insert(key, value); + const result = this.stableMap5.insert(key, value); if (result === null) { return []; } else { @@ -41,27 +41,27 @@ export default class { @query([], IDL.Bool) stableMap5IsEmpty(): boolean { - return stableMap5.isEmpty(); + return this.stableMap5.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Opt(IDL.Text), IDL.Float64))) stableMap5Items(): [[string] | [], number][] { - return stableMap5.items(); + return this.stableMap5.items(); } @query([], IDL.Vec(IDL.Opt(IDL.Text))) stableMap5Keys(): ([string] | [])[] { - return stableMap5.keys(); + return this.stableMap5.keys(); } @query([], IDL.Nat64) stableMap5Len(): bigint { - return stableMap5.len(); + return this.stableMap5.len(); } @update([IDL.Opt(IDL.Text)], IDL.Opt(IDL.Float64)) stableMap5Remove(key: [string] | []): [number] | [] { - const result = stableMap5.remove(key); + const result = this.stableMap5.remove(key); if (result === null) { return []; } else { @@ -71,19 +71,19 @@ export default class { @query([], IDL.Vec(IDL.Float64)) stableMap5Values(): number[] { - return stableMap5.values(); + return this.stableMap5.values(); } // stableMap6 methods @query([IDL.Vec(IDL.Nat64)], IDL.Bool) stableMap6ContainsKey(key: bigint[]): boolean { - return stableMap6.containsKey(key); + return this.stableMap6.containsKey(key); } @query([IDL.Vec(IDL.Nat64)], IDL.Opt(IDL.Bool)) stableMap6Get(key: bigint[]): [boolean] | [] { - const result = stableMap6.get(key); + const result = this.stableMap6.get(key); if (result === null) { return []; } else { @@ -93,7 +93,7 @@ export default class { @update([IDL.Vec(IDL.Nat64), IDL.Bool], IDL.Opt(IDL.Bool)) stableMap6Insert(key: bigint[], value: boolean): [boolean] | [] { - const result = stableMap6.insert(key, value); + const result = this.stableMap6.insert(key, value); if (result === null) { return []; } else { @@ -103,27 +103,27 @@ export default class { @query([], IDL.Bool) stableMap6IsEmpty(): boolean { - return stableMap6.isEmpty(); + return this.stableMap6.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Vec(IDL.Nat64), IDL.Bool))) stableMap6Items(): [bigint[], boolean][] { - return stableMap6.items(); + return this.stableMap6.items(); } @query([], IDL.Vec(IDL.Vec(IDL.Nat64))) stableMap6Keys(): bigint[][] { - return stableMap6.keys(); + return this.stableMap6.keys(); } @query([], IDL.Nat64) stableMap6Len(): bigint { - return stableMap6.len(); + return this.stableMap6.len(); } @update([IDL.Vec(IDL.Nat64)], IDL.Opt(IDL.Bool)) stableMap6Remove(key: bigint[]): [boolean] | [] { - const result = stableMap6.remove(key); + const result = this.stableMap6.remove(key); if (result === null) { return []; } else { @@ -133,20 +133,20 @@ export default class { @query([], IDL.Vec(IDL.Bool)) stableMap6Values(): boolean[] { - return stableMap6.values(); + return this.stableMap6.values(); } // stableMap7 methods @query([IDL.Null], IDL.Bool) stableMap7ContainsKey(key: null): boolean { - return stableMap7.containsKey(key); + return this.stableMap7.containsKey(key); } @query([IDL.Null], IDL.Opt(IDL.Null)) stableMap7Get(key: null): [null] | [] { - const result = stableMap7.get(key); - if (stableMap7.containsKey(key)) { + const result = this.stableMap7.get(key); + if (this.stableMap7.containsKey(key)) { return [result]; } else { return []; @@ -155,8 +155,8 @@ export default class { @update([IDL.Null, IDL.Null], IDL.Opt(IDL.Null)) stableMap7Insert(key: null, value: null): [null] | [] { - const hasOldValue = stableMap7.containsKey(key); - const result = stableMap7.insert(key, value); + const hasOldValue = this.stableMap7.containsKey(key); + const result = this.stableMap7.insert(key, value); if (hasOldValue) { return [result]; } else { @@ -166,28 +166,28 @@ export default class { @query([], IDL.Bool) stableMap7IsEmpty(): boolean { - return stableMap7.isEmpty(); + return this.stableMap7.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Null, IDL.Null))) stableMap7Items(): [null, null][] { - return stableMap7.items(); + return this.stableMap7.items(); } @query([], IDL.Vec(IDL.Null)) stableMap7Keys(): null[] { - return stableMap7.keys(); + return this.stableMap7.keys(); } @query([], IDL.Nat64) stableMap7Len(): bigint { - return stableMap7.len(); + return this.stableMap7.len(); } @update([IDL.Null], IDL.Opt(IDL.Null)) stableMap7Remove(key: null): [null] | [] { - const hasOldValue = stableMap7.containsKey(key); - const result = stableMap7.remove(key); + const hasOldValue = this.stableMap7.containsKey(key); + const result = this.stableMap7.remove(key); if (hasOldValue) { return [result]; } else { @@ -197,20 +197,20 @@ export default class { @query([], IDL.Vec(IDL.Null)) stableMap7Values(): null[] { - return stableMap7.values(); + return this.stableMap7.values(); } // stableMap8 methods @query([IDL.Bool], IDL.Bool) stableMap8ContainsKey(key: boolean): boolean { - return stableMap8.containsKey(key); + return this.stableMap8.containsKey(key); } @query([IDL.Bool], IDL.Opt(IDL.Null)) stableMap8Get(key: boolean): [null] | [] { - const result = stableMap8.get(key); - if (stableMap8.containsKey(key)) { + const result = this.stableMap8.get(key); + if (this.stableMap8.containsKey(key)) { return [result]; } else { return []; @@ -219,8 +219,8 @@ export default class { @update([IDL.Bool, IDL.Null], IDL.Opt(IDL.Null)) stableMap8Insert(key: boolean, value: null): [null] | [] { - const hasOldValue = stableMap8.containsKey(key); - const result = stableMap8.insert(key, value); + const hasOldValue = this.stableMap8.containsKey(key); + const result = this.stableMap8.insert(key, value); if (hasOldValue) { return [result]; } else { @@ -230,28 +230,28 @@ export default class { @query([], IDL.Bool) stableMap8IsEmpty(): boolean { - return stableMap8.isEmpty(); + return this.stableMap8.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Bool, IDL.Null))) stableMap8Items(): [boolean, null][] { - return stableMap8.items(); + return this.stableMap8.items(); } @query([], IDL.Vec(IDL.Bool)) stableMap8Keys(): boolean[] { - return stableMap8.keys(); + return this.stableMap8.keys(); } @query([], IDL.Nat64) stableMap8Len(): bigint { - return stableMap8.len(); + return this.stableMap8.len(); } @update([IDL.Bool], IDL.Opt(IDL.Null)) stableMap8Remove(key: boolean): [null] | [] { - const hasOldValue = stableMap8.containsKey(key); - const result = stableMap8.remove(key); + const hasOldValue = this.stableMap8.containsKey(key); + const result = this.stableMap8.remove(key); if (hasOldValue) { return [result]; } else { @@ -261,19 +261,19 @@ export default class { @query([], IDL.Vec(IDL.Null)) stableMap8Values(): null[] { - return stableMap8.values(); + return this.stableMap8.values(); } // stableMap9 methods @query([IDL.Float64], IDL.Bool) stableMap9ContainsKey(key: number): boolean { - return stableMap9.containsKey(key); + return this.stableMap9.containsKey(key); } @query([IDL.Float64], IDL.Opt(IDL.Vec(IDL.Text))) stableMap9Get(key: number): [string[]] | [] { - const result = stableMap9.get(key); + const result = this.stableMap9.get(key); if (result === null) { return []; } else { @@ -283,7 +283,7 @@ export default class { @update([IDL.Float64, IDL.Vec(IDL.Text)], IDL.Opt(IDL.Vec(IDL.Text))) stableMap9Insert(key: number, value: string[]): [string[]] | [] { - const result = stableMap9.insert(key, value); + const result = this.stableMap9.insert(key, value); if (result === null) { return []; } else { @@ -293,27 +293,27 @@ export default class { @query([], IDL.Bool) stableMap9IsEmpty(): boolean { - return stableMap9.isEmpty(); + return this.stableMap9.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Float64, IDL.Vec(IDL.Text)))) stableMap9Items(): [number, string[]][] { - return stableMap9.items(); + return this.stableMap9.items(); } @query([], IDL.Vec(IDL.Float64)) stableMap9Keys(): number[] { - return stableMap9.keys(); + return this.stableMap9.keys(); } @query([], IDL.Nat64) stableMap9Len(): bigint { - return stableMap9.len(); + return this.stableMap9.len(); } @update([IDL.Float64], IDL.Opt(IDL.Vec(IDL.Text))) stableMap9Remove(key: number): [string[]] | [] { - const result = stableMap9.remove(key); + const result = this.stableMap9.remove(key); if (result === null) { return []; } else { @@ -323,6 +323,6 @@ export default class { @query([], IDL.Vec(IDL.Vec(IDL.Text))) stableMap9Values(): string[][] { - return stableMap9.values(); + return this.stableMap9.values(); } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister3/index.ts b/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister3/index.ts index 98355d805c..a66807e20e 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister3/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/stable_structures/src/canister3/index.ts @@ -2,16 +2,16 @@ import { IDL, Principal, query, StableBTreeMap, update } from 'azle'; import { Callback, Reaction, User } from '../types'; -let stableMap10 = StableBTreeMap(10); -let stableMap11 = StableBTreeMap(11); -let stableMap12 = StableBTreeMap(12); -let stableMap13 = StableBTreeMap(13); -let stableMap14 = StableBTreeMap(14); -let stableMap15 = StableBTreeMap(15); -let stableMap16 = StableBTreeMap(16); -let stableMap17 = StableBTreeMap(17); - export default class { + stableMap10 = StableBTreeMap(10); + stableMap11 = StableBTreeMap(11); + stableMap12 = StableBTreeMap(12); + stableMap13 = StableBTreeMap(13); + stableMap14 = StableBTreeMap(14); + stableMap15 = StableBTreeMap(15); + stableMap16 = StableBTreeMap(16); + stableMap17 = StableBTreeMap(17); + @query([], IDL.Bool) getRedeployed(): boolean { return globalThis._azlePostUpgradeCalled; @@ -21,12 +21,12 @@ export default class { @query([IDL.Float32], IDL.Bool) stableMap10ContainsKey(key: number): boolean { - return stableMap10.containsKey(key); + return this.stableMap10.containsKey(key); } @query([IDL.Float32], IDL.Opt(IDL.Opt(IDL.Bool))) stableMap10Get(key: number): [[boolean] | []] | [] { - const result = stableMap10.get(key); + const result = this.stableMap10.get(key); if (result === null) { return []; } else { @@ -39,7 +39,7 @@ export default class { key: number, value: [boolean] | [] ): [[boolean] | []] | [] { - const result = stableMap10.insert(key, value); + const result = this.stableMap10.insert(key, value); if (result === null) { return []; } else { @@ -49,27 +49,27 @@ export default class { @query([], IDL.Bool) stableMap10IsEmpty(): boolean { - return stableMap10.isEmpty(); + return this.stableMap10.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Float32, IDL.Opt(IDL.Bool)))) stableMap10Items(): [number, [boolean] | []][] { - return stableMap10.items(); + return this.stableMap10.items(); } @query([], IDL.Vec(IDL.Float32)) stableMap10Keys(): number[] { - return stableMap10.keys(); + return this.stableMap10.keys(); } @query([], IDL.Nat64) stableMap10Len(): bigint { - return stableMap10.len(); + return this.stableMap10.len(); } @update([IDL.Float32], IDL.Opt(IDL.Opt(IDL.Bool))) stableMap10Remove(key: number): [[boolean] | []] | [] { - const result = stableMap10.remove(key); + const result = this.stableMap10.remove(key); if (result === null) { return []; } else { @@ -79,19 +79,19 @@ export default class { @query([], IDL.Vec(IDL.Opt(IDL.Bool))) stableMap10Values(): ([boolean] | [])[] { - return stableMap10.values(); + return this.stableMap10.values(); } // stableMap11 methods @query([IDL.Nat], IDL.Bool) stableMap11ContainsKey(key: bigint): boolean { - return stableMap11.containsKey(key); + return this.stableMap11.containsKey(key); } @query([IDL.Nat], IDL.Opt(User)) stableMap11Get(key: bigint): [User] | [] { - const result = stableMap11.get(key); + const result = this.stableMap11.get(key); if (result === null) { return []; } else { @@ -101,7 +101,7 @@ export default class { @update([IDL.Nat, User], IDL.Opt(User)) stableMap11Insert(key: bigint, value: User): [User] | [] { - const result = stableMap11.insert(key, value); + const result = this.stableMap11.insert(key, value); if (result === null) { return []; } else { @@ -111,27 +111,27 @@ export default class { @query([], IDL.Bool) stableMap11IsEmpty(): boolean { - return stableMap11.isEmpty(); + return this.stableMap11.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Nat, User))) stableMap11Items(): [bigint, User][] { - return stableMap11.items(); + return this.stableMap11.items(); } @query([], IDL.Vec(IDL.Nat)) stableMap11Keys(): bigint[] { - return stableMap11.keys(); + return this.stableMap11.keys(); } @query([], IDL.Nat64) stableMap11Len(): bigint { - return stableMap11.len(); + return this.stableMap11.len(); } @update([IDL.Nat], IDL.Opt(User)) stableMap11Remove(key: bigint): [User] | [] { - const result = stableMap11.remove(key); + const result = this.stableMap11.remove(key); if (result === null) { return []; } else { @@ -141,19 +141,19 @@ export default class { @query([], IDL.Vec(User)) stableMap11Values(): User[] { - return stableMap11.values(); + return this.stableMap11.values(); } // stableMap12 methods @query([IDL.Vec(IDL.Nat8)], IDL.Bool) stableMap12ContainsKey(key: Uint8Array): boolean { - return stableMap12.containsKey(key); + return this.stableMap12.containsKey(key); } @query([IDL.Vec(IDL.Nat8)], IDL.Opt(Reaction)) stableMap12Get(key: Uint8Array): [Reaction] | [] { - const result = stableMap12.get(key); + const result = this.stableMap12.get(key); if (result === null) { return []; } else { @@ -163,7 +163,7 @@ export default class { @update([IDL.Vec(IDL.Nat8), Reaction], IDL.Opt(Reaction)) stableMap12Insert(key: Uint8Array, value: Reaction): [Reaction] | [] { - const result = stableMap12.insert(key, value); + const result = this.stableMap12.insert(key, value); if (result === null) { return []; } else { @@ -173,27 +173,27 @@ export default class { @query([], IDL.Bool) stableMap12IsEmpty(): boolean { - return stableMap12.isEmpty(); + return this.stableMap12.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Vec(IDL.Nat8), Reaction))) stableMap12Items(): [Uint8Array, Reaction][] { - return stableMap12.items(); + return this.stableMap12.items(); } @query([], IDL.Vec(IDL.Vec(IDL.Nat8))) stableMap12Keys(): Uint8Array[] { - return stableMap12.keys(); + return this.stableMap12.keys(); } @query([], IDL.Nat64) stableMap12Len(): bigint { - return stableMap12.len(); + return this.stableMap12.len(); } @update([IDL.Vec(IDL.Nat8)], IDL.Opt(Reaction)) stableMap12Remove(key: Uint8Array): [Reaction] | [] { - const result = stableMap12.remove(key); + const result = this.stableMap12.remove(key); if (result === null) { return []; } else { @@ -203,19 +203,19 @@ export default class { @query([], IDL.Vec(Reaction)) stableMap12Values(): Reaction[] { - return stableMap12.values(); + return this.stableMap12.values(); } // stableMap13 methods @query([IDL.Text], IDL.Bool) stableMap13ContainsKey(key: string): boolean { - return stableMap13.containsKey(key); + return this.stableMap13.containsKey(key); } @query([IDL.Text], IDL.Opt(IDL.Principal)) stableMap13Get(key: string): [Principal] | [] { - const result = stableMap13.get(key); + const result = this.stableMap13.get(key); if (result === null) { return []; } else { @@ -225,7 +225,7 @@ export default class { @update([IDL.Text, IDL.Principal], IDL.Opt(IDL.Principal)) stableMap13Insert(key: string, value: Principal): [Principal] | [] { - const result = stableMap13.insert(key, value); + const result = this.stableMap13.insert(key, value); if (result === null) { return []; } else { @@ -235,27 +235,27 @@ export default class { @query([], IDL.Bool) stableMap13IsEmpty(): boolean { - return stableMap13.isEmpty(); + return this.stableMap13.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Text, IDL.Principal))) stableMap13Items(): [string, Principal][] { - return stableMap13.items(); + return this.stableMap13.items(); } @query([], IDL.Vec(IDL.Text)) stableMap13Keys(): string[] { - return stableMap13.keys(); + return this.stableMap13.keys(); } @query([], IDL.Nat64) stableMap13Len(): bigint { - return stableMap13.len(); + return this.stableMap13.len(); } @update([IDL.Text], IDL.Opt(IDL.Principal)) stableMap13Remove(key: string): [Principal] | [] { - const result = stableMap13.remove(key); + const result = this.stableMap13.remove(key); if (result === null) { return []; } else { @@ -265,19 +265,19 @@ export default class { @query([], IDL.Vec(IDL.Principal)) stableMap13Values(): Principal[] { - return stableMap13.values(); + return this.stableMap13.values(); } // stableMap14 methods @query([IDL.Text], IDL.Bool) stableMap14ContainsKey(key: string): boolean { - return stableMap14.containsKey(key); + return this.stableMap14.containsKey(key); } @query([IDL.Text], IDL.Opt(Callback)) stableMap14Get(key: string): [Callback] | [] { - const result = stableMap14.get(key); + const result = this.stableMap14.get(key); if (result === null) { return []; } else { @@ -287,7 +287,7 @@ export default class { @update([IDL.Text, Callback], IDL.Opt(Callback)) stableMap14Insert(key: string, value: Callback): [Callback] | [] { - const result = stableMap14.insert(key, value); + const result = this.stableMap14.insert(key, value); if (result === null) { return []; } else { @@ -297,27 +297,27 @@ export default class { @query([], IDL.Bool) stableMap14IsEmpty(): boolean { - return stableMap14.isEmpty(); + return this.stableMap14.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Text, Callback))) stableMap14Items(): [string, Callback][] { - return stableMap14.items(); + return this.stableMap14.items(); } @query([], IDL.Vec(IDL.Text)) stableMap14Keys(): string[] { - return stableMap14.keys(); + return this.stableMap14.keys(); } @query([], IDL.Nat64) stableMap14Len(): bigint { - return stableMap14.len(); + return this.stableMap14.len(); } @update([IDL.Text], IDL.Opt(Callback)) stableMap14Remove(key: string): [Callback] | [] { - const result = stableMap14.remove(key); + const result = this.stableMap14.remove(key); if (result === null) { return []; } else { @@ -327,19 +327,19 @@ export default class { @query([], IDL.Vec(Callback)) stableMap14Values(): Callback[] { - return stableMap14.values(); + return this.stableMap14.values(); } // stableMap15 methods @query([Callback], IDL.Bool) stableMap15ContainsKey(key: Callback): boolean { - return stableMap15.containsKey(key); + return this.stableMap15.containsKey(key); } @query([Callback], IDL.Opt(IDL.Text)) stableMap15Get(key: Callback): [string] | [] { - const result = stableMap15.get(key); + const result = this.stableMap15.get(key); if (result === null) { return []; } else { @@ -349,7 +349,7 @@ export default class { @update([Callback, IDL.Text], IDL.Opt(IDL.Text)) stableMap15Insert(key: Callback, value: string): [string] | [] { - const result = stableMap15.insert(key, value); + const result = this.stableMap15.insert(key, value); if (result === null) { return []; } else { @@ -359,27 +359,27 @@ export default class { @query([], IDL.Bool) stableMap15IsEmpty(): boolean { - return stableMap15.isEmpty(); + return this.stableMap15.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(Callback, IDL.Text))) stableMap15Items(): [Callback, string][] { - return stableMap15.items(); + return this.stableMap15.items(); } @query([], IDL.Vec(Callback)) stableMap15Keys(): Callback[] { - return stableMap15.keys(); + return this.stableMap15.keys(); } @query([], IDL.Nat64) stableMap15Len(): bigint { - return stableMap15.len(); + return this.stableMap15.len(); } @update([Callback], IDL.Opt(IDL.Text)) stableMap15Remove(key: Callback): [string] | [] { - const result = stableMap15.remove(key); + const result = this.stableMap15.remove(key); if (result === null) { return []; } else { @@ -389,19 +389,19 @@ export default class { @query([], IDL.Vec(IDL.Text)) stableMap15Values(): string[] { - return stableMap15.values(); + return this.stableMap15.values(); } // stableMap16 methods @query([IDL.Text], IDL.Bool) stableMap16ContainsKey(key: string): boolean { - return stableMap16.containsKey(key); + return this.stableMap16.containsKey(key); } @query([IDL.Text], IDL.Opt(IDL.Text)) stableMap16Get(key: string): [string] | [] { - const result = stableMap16.get(key); + const result = this.stableMap16.get(key); if (result === null) { return []; } else { @@ -411,7 +411,7 @@ export default class { @update([IDL.Text, IDL.Text], IDL.Opt(IDL.Text)) stableMap16Insert(key: string, value: string): [string] | [] { - const result = stableMap16.insert(key, JSON.parse(value)); + const result = this.stableMap16.insert(key, JSON.parse(value)); if (result === null) { return []; } else { @@ -421,29 +421,29 @@ export default class { @query([], IDL.Bool) stableMap16IsEmpty(): boolean { - return stableMap16.isEmpty(); + return this.stableMap16.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Text, IDL.Text))) stableMap16Items(): [string, string][] { - return stableMap16.items().map(([key, value]) => { + return this.stableMap16.items().map(([key, value]) => { return [key, JSON.stringify(value)]; }); } @query([], IDL.Vec(IDL.Text)) stableMap16Keys(): string[] { - return stableMap16.keys(); + return this.stableMap16.keys(); } @query([], IDL.Nat64) stableMap16Len(): bigint { - return stableMap16.len(); + return this.stableMap16.len(); } @update([IDL.Text], IDL.Opt(IDL.Text)) stableMap16Remove(key: string): [string] | [] { - const result = stableMap16.remove(key); + const result = this.stableMap16.remove(key); if (result === null) { return []; } else { @@ -453,19 +453,19 @@ export default class { @query([], IDL.Vec(IDL.Text)) stableMap16Values(): string[] { - return stableMap16.values().map((value) => JSON.stringify(value)); + return this.stableMap16.values().map((value) => JSON.stringify(value)); } // stableMap17 methods @query([IDL.Text], IDL.Bool) stableMap17ContainsKey(key: string): boolean { - return stableMap17.containsKey(JSON.parse(key)); + return this.stableMap17.containsKey(JSON.parse(key)); } @query([IDL.Text], IDL.Opt(IDL.Text)) stableMap17Get(key: string): [string] | [] { - const result = stableMap17.get(JSON.parse(key)); + const result = this.stableMap17.get(JSON.parse(key)); if (result === null) { return []; } else { @@ -475,7 +475,7 @@ export default class { @update([IDL.Text, IDL.Text], IDL.Opt(IDL.Text)) stableMap17Insert(key: string, value: string): [string] | [] { - const result = stableMap17.insert(JSON.parse(key), value); + const result = this.stableMap17.insert(JSON.parse(key), value); if (result === null) { return []; } else { @@ -485,29 +485,29 @@ export default class { @query([], IDL.Bool) stableMap17IsEmpty(): boolean { - return stableMap17.isEmpty(); + return this.stableMap17.isEmpty(); } @query([], IDL.Vec(IDL.Tuple(IDL.Text, IDL.Text))) stableMap17Items(): [string, string][] { - return stableMap17.items().map(([key, value]) => { + return this.stableMap17.items().map(([key, value]) => { return [JSON.stringify(key), value]; }); } @query([], IDL.Vec(IDL.Text)) stableMap17Keys(): string[] { - return stableMap17.keys().map((key) => JSON.stringify(key)); + return this.stableMap17.keys().map((key) => JSON.stringify(key)); } @query([], IDL.Nat64) stableMap17Len(): bigint { - return stableMap17.len(); + return this.stableMap17.len(); } @update([IDL.Text], IDL.Opt(IDL.Text)) stableMap17Remove(key: string): [string] | [] { - const result = stableMap17.remove(JSON.parse(key)); + const result = this.stableMap17.remove(JSON.parse(key)); if (result === null) { return []; } else { @@ -517,6 +517,6 @@ export default class { @query([], IDL.Vec(IDL.Text)) stableMap17Values(): string[] { - return stableMap17.values(); + return this.stableMap17.values(); } } diff --git a/tests/end_to_end/candid_rpc/class_syntax/update/src/index.ts b/tests/end_to_end/candid_rpc/class_syntax/update/src/index.ts index d217f09dad..47dba5e4b1 100644 --- a/tests/end_to_end/candid_rpc/class_syntax/update/src/index.ts +++ b/tests/end_to_end/candid_rpc/class_syntax/update/src/index.ts @@ -1,15 +1,15 @@ import { IDL, query, update } from 'azle'; -let currentMessage: string = ''; - export default class { + currentMessage: string = ''; + @query([], IDL.Text) getCurrentMessage(): string { - return currentMessage; + return this.currentMessage; } @update([IDL.Text]) simpleUpdate(message: string): void { - currentMessage = message; + this.currentMessage = message; } }