From 5b4690ec1d93bb1b687111ddc9f05df8ed219501 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Mon, 25 Sep 2023 09:00:01 -0500 Subject: [PATCH] refactor ic_api --- .github/workflows/test.yml | 2 +- examples/ic_api/dfx.json | 3 +- examples/ic_api/src/index.ts | 124 ++++++++++++++-------------------- examples/ic_api/test/test.ts | 4 +- examples/ic_api/test/tests.ts | 2 +- 5 files changed, 58 insertions(+), 77 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index e79b80391d..4f7f294356 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -17,7 +17,6 @@ # "examples/complex_types", # "examples/func_types", # "examples/generics", -# "examples/ic_api", # "examples/imports", # "examples/init", # "examples/inline_types", @@ -114,6 +113,7 @@ jobs: "examples/ethereum_json_rpc", "examples/guard_functions", "examples/heartbeat", + "examples/ic_api", "examples/primitive_types", "examples/principal", "examples/query", diff --git a/examples/ic_api/dfx.json b/examples/ic_api/dfx.json index b881b1966c..b8da010ce2 100644 --- a/examples/ic_api/dfx.json +++ b/examples/ic_api/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/ic_api/ic_api.wasm.gz", + "wasm": ".azle/ic_api/ic_api.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/ic_api", "node_compatibility": true diff --git a/examples/ic_api/src/index.ts b/examples/ic_api/src/index.ts index 31cf2bc361..6756afa481 100644 --- a/examples/ic_api/src/index.ts +++ b/examples/ic_api/src/index.ts @@ -26,7 +26,7 @@ import { // string: text; // }; -export default class extends Service { +export default Service({ // // returns the argument data as an array. // argDataZeroParams(): Vec { // return ic.argData(); @@ -55,111 +55,91 @@ export default class extends Service { // } // returns the argument data as bytes. - @query([blob, int8, bool, text], blob) - argDataRaw(arg1: blob, arg2: int8, arg3: bool, arg4: text): blob { - return ic.argDataRaw(); - } - + argDataRaw: query( + [blob, int8, bool, text], + blob, + (arg1, arg2, arg3, arg4) => { + return ic.argDataRaw(); + } + ), // returns the length of the argument data in bytes - @query([blob, int8, bool, text], nat32) - argDataRawSize(arg1: blob, arg2: int8, arg3: bool, arg4: text): nat32 { - return ic.argDataRawSize(); - } - + argDataRawSize: query( + [blob, int8, bool, text], + nat32, + (arg1, arg2, arg3, arg4) => { + return ic.argDataRawSize(); + } + ), // returns the principal of the identity that called this function - @query([], principal) - caller(): Principal { + caller: query([], principal, () => { return ic.caller(); - } - + }), // returns the amount of cycles available in the canister - @query([], nat64) - canisterBalance(): nat64 { + canisterBalance: query([], nat64, () => { return ic.canisterBalance(); - } - + }), // returns the amount of cycles available in the canister - @query([], nat) - canisterBalance128(): nat { + canisterBalance128: query([], nat, () => { return ic.canisterBalance128(); - } - + }), // returns the canister's version number - @query([], nat64) - canisterVersion(): nat64 { + canisterVersion: query([], nat64, () => { return ic.canisterVersion(); - } - + }), // When called from a query call, returns the data certificate // authenticating certified data set by this canister. Otherwise returns // None. - @query([], Opt(blob)) - dataCertificate(): Opt { + dataCertificate: query([], Opt(blob), () => { return ic.dataCertificate(); - } - + }), // When called from a query call, returns the data certificate // authenticating certified data set by this canister. Otherwise returns // None. - @update([], Opt(blob)) - dataCertificateNull(): Opt { + dataCertificateNull: update([], Opt(blob), () => { return ic.dataCertificate(); - } - + }), // returns this canister's id - @query([], principal) - id(): Principal { + id: query([], principal, () => { return ic.id(); - } - + }), // Returns the number of instructions that the canister executed since the last // entry point. - @query([], nat64) - instructionCounter(): nat64 { + instructionCounter: query([], nat64, () => { return ic.instructionCounter(); - } - + }), // determines whether the given principal is a controller of the canister - @query([principal], bool) - isController(principal: Principal): bool { + isController: query([principal], bool, (principal) => { return ic.isController(principal); - } - - @query([], nat64) - performanceCounter(): nat64 { + }), + performanceCounter: query([], nat64, () => { return ic.performanceCounter(0); - } - + }), // prints a message through the local replica's output - @query([text], bool) - print(message: text): bool { + print: query([text], bool, (message) => { ic.print(message); return true; - } - - @query([text], empty, { manual: true }) - reject(message: text): Manual { - ic.reject(message); - } - + }), + reject: query( + [text], + Manual(empty), + (message) => { + ic.reject(message); + }, + { manual: true } + ), // sets up to 32 bytes of certified data - @update([blob], Void) - setCertifiedData(data: blob): Void { + setCertifiedData: update([blob], Void, (data) => { ic.setCertifiedData(data); - } - + }), // returns the current timestamp - @query([], nat64) - time(): nat64 { + time: query([], nat64, () => { return ic.time(); - } - + }), // traps with a message, stopping execution and discarding all state within the call - @query([text], bool) - trap(message: text): bool { + trap: query([text], bool, (message) => { ic.trap(message); return true; - } -} + }) +}); diff --git a/examples/ic_api/test/test.ts b/examples/ic_api/test/test.ts index abd21b10eb..cad199b6c8 100644 --- a/examples/ic_api/test/test.ts +++ b/examples/ic_api/test/test.ts @@ -2,10 +2,10 @@ import { getCanisterId, runTests } from 'azle/test'; import { createActor } from './dfx_generated/ic_api'; import { getTests } from './tests'; -const ic_api_canister = createActor(getCanisterId('ic_api'), { +const icApiCanister = createActor(getCanisterId('ic_api'), { agentOptions: { host: 'http://127.0.0.1:8000' } }); -runTests(getTests(ic_api_canister as any)); +runTests(getTests(icApiCanister as any)); diff --git a/examples/ic_api/test/tests.ts b/examples/ic_api/test/tests.ts index f7aab58c1c..56a40f07ab 100644 --- a/examples/ic_api/test/tests.ts +++ b/examples/ic_api/test/tests.ts @@ -314,7 +314,7 @@ function isNone(option: [] | T[]): boolean { return option.length === 0; } -function candidDecode(bytes: Uint8Array): string { +function candidDecode(bytes: Uint8Array | number[]): string { const hexString = [...bytes] .map((byte) => byte.toString(16).padStart(2, '0')) .join('');