From 335b821e23a27a8a555dc8cb9b607da78f840743 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 18:54:11 -0500 Subject: [PATCH 01/10] bitcoin example completed --- canisters/management/bitcoin.ts | 151 +++++++----------- canisters/management/index.ts | 27 +++- examples/bitcoin/src/index.did | 8 +- examples/bitcoin/src/index.ts | 27 ++-- src/lib_functional/candid/reference/record.ts | 1 + .../candid/reference/variant.ts | 1 + 6 files changed, 97 insertions(+), 118 deletions(-) diff --git a/canisters/management/bitcoin.ts b/canisters/management/bitcoin.ts index 811263c4cf..057a08e685 100644 --- a/canisters/management/bitcoin.ts +++ b/canisters/management/bitcoin.ts @@ -2,7 +2,6 @@ import { blob, - candid, nat32, nat64, Null, @@ -11,7 +10,7 @@ import { text, Variant, Vec -} from '../../src/lib_new'; +} from '../../src/lib_functional'; export type BitcoinAddress = text; export const BitcoinAddress = text; @@ -24,97 +23,57 @@ export const MillisatoshiPerByte = nat64; export type Satoshi = nat64; export const Satoshi = nat64; -export class BitcoinNetwork extends Variant { - @candid(Null) - Mainnet?: Null; - - @candid(Null) - Regtest?: Null; - - @candid(Null) - Testnet?: Null; -} - -export class Outpoint extends Record { - @candid(blob) - txid: blob; - - @candid(nat32) - vout: nat32; -} - -export class Utxo extends Record { - @candid(nat32) - height: nat32; - - @candid(Outpoint) - outpoint: Outpoint; - - @candid(Satoshi) - value: Satoshi; -} - -export class UtxosFilter extends Variant { - @candid(nat32) - MinConfirmations?: nat32; - - @candid(Page) - Page?: Page; -} - -export class GetBalanceArgs extends Record { - @candid(BitcoinAddress) - address: BitcoinAddress; - - @candid(Opt(nat32)) - min_confirmations: Opt; - - @candid(BitcoinNetwork) - network: BitcoinNetwork; -} - -export class GetCurrentFeePercentilesArgs extends Record { - @candid(BitcoinNetwork) - network: BitcoinNetwork; -} - -export class GetUtxosArgs extends Record { - @candid(BitcoinAddress) - address: BitcoinAddress; - - @candid(Opt(UtxosFilter)) - filter: Opt; - - @candid(BitcoinNetwork) - network: BitcoinNetwork; -} - -export class GetUtxosResult extends Record { - @candid(Opt(Page)) - next_page: Opt; - - @candid(BlockHash) - tip_block_hash: BlockHash; - - @candid(nat32) - tip_height: nat32; - - @candid(Vec(Utxo)) - utxos: Vec; -} - -export class SendTransactionArgs extends Record { - @candid(blob) - transaction: blob; - - @candid(BitcoinNetwork) - network: BitcoinNetwork; -} - -export class SendTransactionError extends Variant { - @candid(Null) - MalformedTransaction?: Null; - - @candid(Null) - QueueFull?: Null; -} +export const BitcoinNetwork = Variant({ + Mainnet: Null, + Regtest: Null, + Testnet: Null +}); + +export const Outpoint = Record({ + txid: blob, + vout: nat32 +}); + +export const Utxo = Record({ + height: nat32, + outpoint: Outpoint, + value: Satoshi +}); + +export const UtxosFilter = Variant({ + MinConfirmations: nat32, + Page: Page +}); + +export const GetBalanceArgs = Record({ + address: BitcoinAddress, + min_confirmations: Opt(nat32), + network: BitcoinNetwork +}); + +export const GetCurrentFeePercentilesArgs = Record({ + network: BitcoinNetwork +}); + +export const GetUtxosArgs = Record({ + address: BitcoinAddress, + filter: Opt(UtxosFilter), + network: BitcoinNetwork +}); + +export const GetUtxosResult = Record({ + next_page: Opt(Page), + tip_block_hash: BlockHash, + tip_height: nat32, + utxos: Vec(Utxo) +}); + +export const SendTransactionArgs = Record({ + transaction: blob, + network: BitcoinNetwork +}); + +export const SendTransactionError = Variant({ + MalformedTransaction: Null, + QueueFull: Null +}); diff --git a/canisters/management/index.ts b/canisters/management/index.ts index fdbc36c211..4f063f428b 100644 --- a/canisters/management/index.ts +++ b/canisters/management/index.ts @@ -1,5 +1,30 @@ -import { blob, Principal, Service, update } from '../../src/lib_functional'; +import { + blob, + Principal, + Service, + update, + Vec, + Void +} from '../../src/lib_functional'; +import { + GetBalanceArgs, + GetCurrentFeePercentilesArgs, + GetUtxosArgs, + GetUtxosResult, + MillisatoshiPerByte, + Satoshi, + SendTransactionArgs +} from './bitcoin'; + +export * from './bitcoin'; export const managementCanister = Service({ + bitcoin_get_balance: update([GetBalanceArgs], Satoshi), + bitcoin_get_current_fee_percentiles: update( + [GetCurrentFeePercentilesArgs], + Vec(MillisatoshiPerByte) + ), + bitcoin_get_utxos: update([GetUtxosArgs], GetUtxosResult), + bitcoin_send_transaction: update([SendTransactionArgs], Void), raw_rand: update([], blob) })(Principal.fromText('aaaaa-aa')); diff --git a/examples/bitcoin/src/index.did b/examples/bitcoin/src/index.did index bc830c1f89..b74fa19ac0 100644 --- a/examples/bitcoin/src/index.did +++ b/examples/bitcoin/src/index.did @@ -1,9 +1,9 @@ -type rec_2 = record {txid:vec nat8; vout:nat32}; -type rec_1 = record {height:nat32; value:nat64; outpoint:rec_2}; -type rec_0 = record {next_page:opt vec nat8; tip_height:nat32; tip_block_hash:vec nat8; utxos:vec rec_1}; +type rec_14 = record {txid:vec nat8; vout:nat32}; +type rec_13 = record {height:nat32; value:nat64; outpoint:rec_14}; +type rec_12 = record {next_page:opt vec nat8; tip_height:nat32; tip_block_hash:vec nat8; utxos:vec rec_13}; service: () -> { getBalance: (text) -> (nat64); getCurrentFeePercentiles: () -> (vec nat64); - getUtxos: (text) -> (rec_0); + getUtxos: (text) -> (rec_12); sendTransaction: (vec nat8) -> (bool); } diff --git a/examples/bitcoin/src/index.ts b/examples/bitcoin/src/index.ts index 70ee0f4108..89b95f62dd 100644 --- a/examples/bitcoin/src/index.ts +++ b/examples/bitcoin/src/index.ts @@ -10,9 +10,8 @@ const BITCOIN_API_CYCLE_COST = 100_000_000n; const BITCOIN_BASE_TRANSACTION_COST = 5_000_000_000n; const BITCOIN_CYCLE_COST_PER_TRANSACTION_BYTE = 20_000_000n; -export default class extends Service { - @update([text], Satoshi) - async getBalance(address: string): Promise { +export default Service({ + getBalance: update([text], Satoshi, async (address) => { return await ic.call(managementCanister.bitcoin_get_balance, { args: [ { @@ -23,10 +22,8 @@ export default class extends Service { ], cycles: BITCOIN_API_CYCLE_COST }); - } - - @update([], Vec(MillisatoshiPerByte)) - async getCurrentFeePercentiles(): Promise> { + }), + getCurrentFeePercentiles: update([], Vec(MillisatoshiPerByte), async () => { return await ic.call( managementCanister.bitcoin_get_current_fee_percentiles, { @@ -38,10 +35,8 @@ export default class extends Service { cycles: BITCOIN_API_CYCLE_COST } ); - } - - @update([text], GetUtxosResult) - async getUtxos(address: text): Promise { + }), + getUtxos: update([text], GetUtxosResult, async (address) => { return await ic.call(managementCanister.bitcoin_get_utxos, { args: [ { @@ -52,10 +47,8 @@ export default class extends Service { ], cycles: BITCOIN_API_CYCLE_COST }); - } - - @update([blob], bool) - async sendTransaction(transaction: blob): Promise { + }), + sendTransaction: update([blob], bool, async (transaction) => { const transactionFee = BITCOIN_BASE_TRANSACTION_COST + BigInt(transaction.length) * @@ -72,5 +65,5 @@ export default class extends Service { }); return true; - } -} + }) +}); diff --git a/src/lib_functional/candid/reference/record.ts b/src/lib_functional/candid/reference/record.ts index e6732dd705..c52f3e9de2 100644 --- a/src/lib_functional/candid/reference/record.ts +++ b/src/lib_functional/candid/reference/record.ts @@ -9,6 +9,7 @@ export function Record(obj: T): { const name = v4(); return { + ...obj, getIDL(parents: any) { const idl = IDL.Rec(); idl.fill( diff --git a/src/lib_functional/candid/reference/variant.ts b/src/lib_functional/candid/reference/variant.ts index ab272d5bfb..f530380bb9 100644 --- a/src/lib_functional/candid/reference/variant.ts +++ b/src/lib_functional/candid/reference/variant.ts @@ -9,6 +9,7 @@ export function Variant(obj: T): RequireExactlyOne<{ const name = v4(); return { + ...obj, getIDL(parents: any) { const idl = IDL.Rec(); idl.fill( From f4d40833d4b86d48878e32d30b73264e996b8a03 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 19:02:57 -0500 Subject: [PATCH 02/10] blob_array example completed --- examples/async_await/dfx.json | 3 ++- examples/audio_recorder/dfx.json | 3 ++- examples/bitcoin/dfx.json | 3 ++- examples/blob_array/dfx.json | 3 ++- examples/blob_array/src/index.ts | 15 ++++++--------- 5 files changed, 14 insertions(+), 13 deletions(-) diff --git a/examples/async_await/dfx.json b/examples/async_await/dfx.json index ccffbfebed..425a4963bf 100644 --- a/examples/async_await/dfx.json +++ b/examples/async_await/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/async_await.ts", "candid": "src/async_await.did", - "wasm": ".azle/async_await/async_await.wasm.gz", + "wasm": ".azle/async_await/async_await.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/async_await", "node_compatibility": true diff --git a/examples/audio_recorder/dfx.json b/examples/audio_recorder/dfx.json index 3683d4aa37..65df40b322 100644 --- a/examples/audio_recorder/dfx.json +++ b/examples/audio_recorder/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/audio_recorder/audio_recorder.wasm.gz", + "wasm": ".azle/audio_recorder/audio_recorder.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/audio_recorder", "node_compatibility": true diff --git a/examples/bitcoin/dfx.json b/examples/bitcoin/dfx.json index 1f44c9ce37..3942f82d05 100644 --- a/examples/bitcoin/dfx.json +++ b/examples/bitcoin/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/bitcoin/bitcoin.wasm.gz", + "wasm": ".azle/bitcoin/bitcoin.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/bitcoin", "node_compatibility": true diff --git a/examples/blob_array/dfx.json b/examples/blob_array/dfx.json index d47937a3f6..744bb07531 100644 --- a/examples/blob_array/dfx.json +++ b/examples/blob_array/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/blob_array/blob_array.wasm.gz", + "wasm": ".azle/blob_array/blob_array.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/blob_array", "node_compatibility": true diff --git a/examples/blob_array/src/index.ts b/examples/blob_array/src/index.ts index 2dea6d17b4..87a84ebb3f 100644 --- a/examples/blob_array/src/index.ts +++ b/examples/blob_array/src/index.ts @@ -1,16 +1,13 @@ import { blob, query, Service, Vec } from 'azle'; -export default class extends Service { - @query([], blob) - getBlob(): blob { +export default Service({ + getBlob: query([], blob, () => { return stringToBlob('hello'); - } - - @query([], Vec(blob)) - getBlobs(): Vec { + }), + getBlobs: query([], Vec(blob), () => { return [stringToBlob('hello'), stringToBlob('world')]; - } -} + }) +}); function stringToBlob(string: string): blob { return Buffer.from(string); From be4cdd24bf2b628e892f2b8c04d4791f6a6c2296 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 19:04:17 -0500 Subject: [PATCH 03/10] add bitcoin and blob_array --- .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 bd1bbfb236..813571a5b8 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,8 +14,6 @@ # The check-basic-integration-tests-success job is designed to ensure that all jobs spun up from the matrix in the basic-integration-tests have succeeded # All Examples TODO restore when https://github.com/demergent-labs/azle/issues/1192 is resolved -# "examples/bitcoin", -# "examples/blob_array", # "examples/bytes", # "examples/call_raw", # "examples/candid_encoding", @@ -114,6 +112,8 @@ jobs: [ "examples/async_await", "examples/audio_recorder", + "examples/bitcoin", + "examples/blob_array", "examples/primitive_types", "examples/principal", "examples/query", From a5fbdb6d0746859ff938032ee2437c2c694b6ece Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 19:08:01 -0500 Subject: [PATCH 04/10] bytes example complete --- examples/bytes/dfx.json | 3 ++- examples/bytes/src/index.ts | 9 ++++----- 2 files changed, 6 insertions(+), 6 deletions(-) diff --git a/examples/bytes/dfx.json b/examples/bytes/dfx.json index 1b34fa6c81..cfde62ab53 100644 --- a/examples/bytes/dfx.json +++ b/examples/bytes/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/bytes_canister/bytes_canister.wasm.gz", + "wasm": ".azle/bytes_canister/bytes_canister.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/bytes_canister", "node_compatibility": true diff --git a/examples/bytes/src/index.ts b/examples/bytes/src/index.ts index 76c1498241..f368b667ff 100644 --- a/examples/bytes/src/index.ts +++ b/examples/bytes/src/index.ts @@ -1,8 +1,7 @@ import { blob, Service, update } from 'azle'; -export default class extends Service { - @update([blob], blob) - getBytes(bytes: blob): blob { +export default Service({ + getBytes: update([blob], blob, (bytes) => { return bytes; - } -} + }) +}); From e218eee437cb6dd5a4431ca81f03dd12f92be456 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 19:08:47 -0500 Subject: [PATCH 05/10] add bytes to test.yml --- .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 813571a5b8..dab17a2886 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,6 @@ # The check-basic-integration-tests-success job is designed to ensure that all jobs spun up from the matrix in the basic-integration-tests have succeeded # All Examples TODO restore when https://github.com/demergent-labs/azle/issues/1192 is resolved -# "examples/bytes", # "examples/call_raw", # "examples/candid_encoding", # "examples/complex_init", @@ -114,6 +113,7 @@ jobs: "examples/audio_recorder", "examples/bitcoin", "examples/blob_array", + "examples/bytes", "examples/primitive_types", "examples/principal", "examples/query", From efc82c155bd8221599b567d5048761c6ca0c261d Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 19:16:42 -0500 Subject: [PATCH 06/10] refactor call_raw --- .github/workflows/test.yml | 2 +- examples/call_raw/dfx.json | 3 +- examples/call_raw/package-lock.json | 238 ++++++++++++++++++++-------- examples/call_raw/package.json | 6 +- examples/call_raw/src/call_raw.ts | 73 ++++----- examples/call_raw/test/test.ts | 4 +- 6 files changed, 211 insertions(+), 115 deletions(-) diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index dab17a2886..e879a96bea 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,6 @@ # The check-basic-integration-tests-success job is designed to ensure that all jobs spun up from the matrix in the basic-integration-tests have succeeded # All Examples TODO restore when https://github.com/demergent-labs/azle/issues/1192 is resolved -# "examples/call_raw", # "examples/candid_encoding", # "examples/complex_init", # "examples/complex_types", @@ -114,6 +113,7 @@ jobs: "examples/bitcoin", "examples/blob_array", "examples/bytes", + "examples/call_raw", "examples/primitive_types", "examples/principal", "examples/query", diff --git a/examples/call_raw/dfx.json b/examples/call_raw/dfx.json index d9d88bdc9c..8d08c8f79c 100644 --- a/examples/call_raw/dfx.json +++ b/examples/call_raw/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/call_raw.ts", "candid": "src/call_raw.did", - "wasm": ".azle/call_raw/call_raw.wasm.gz", + "wasm": ".azle/call_raw/call_raw.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/call_raw", "node_compatibility": true diff --git a/examples/call_raw/package-lock.json b/examples/call_raw/package-lock.json index b6d94f3aaa..745dc27b37 100644 --- a/examples/call_raw/package-lock.json +++ b/examples/call_raw/package-lock.json @@ -8,9 +8,9 @@ "azle": "0.17.1" }, "devDependencies": { - "@dfinity/agent": "0.11.1", - "ts-node": "10.7.0", - "typescript": "4.6.3" + "@dfinity/agent": "^0.19.2", + "ts-node": "^10.9.1", + "typescript": "^5.2.2" } }, "node_modules/@cspotcode/source-map-consumer": { @@ -33,33 +33,77 @@ } }, "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.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", - "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.2", + "@dfinity/principal": "^0.19.2" } }, "node_modules/@dfinity/candid": { - "version": "0.11.1", - "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-0.11.1.tgz", - "integrity": "sha512-EYZZg+x7OgZxa56D9SWMeTlaMb09HJ7wIfL42+l/e0lgrx+sTXYxKe8bM9FrUW9AWo9/gKkOGV/IxJL/Acncng==", + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-0.19.2.tgz", + "integrity": "sha512-X2hCqNMhnnmwtnOc0WnymOZYx3qphjEMuSYbBr7tMIkV7Hwt9BmXXlLnQTxUytTPxf+3he0GcS3KzsSQ9CK8ew==", "dev": true, - "peer": true + "peer": true, + "peerDependencies": { + "@dfinity/principal": "^0.19.2" + } }, "node_modules/@dfinity/principal": { - "version": "0.11.2", - "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.11.2.tgz", - "integrity": "sha512-vReWruqIl16yQeKOrCLDLDf2aTEJsXcKeW9qbwVfmV0kwLNE3B2Z6tbRjYbY7s+KwpysD5B1b48ZbIwI00BeyQ==" + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.19.2.tgz", + "integrity": "sha512-vsKN6BKya70bQUsjgKRDlR2lOpv/XpUkCMIiji6rjMtKHIuWEB5Eu3JqZsOuBmWo3A3TT/K/osT9VPm0k4qdYQ==", + "dev": true, + "peer": true, + "dependencies": { + "@noble/hashes": "^1.3.1" + } + }, + "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==", + "dev": true, + "engines": { + "node": ">= 16" + }, + "funding": { + "url": "https://paulmillr.com/funding/" + } }, "node_modules/@swc/core": { "version": "1.2.151", @@ -359,6 +403,11 @@ "resolved": "https://registry.npmjs.org/azle-syn/-/azle-syn-0.0.0.tgz", "integrity": "sha512-fWExJb5/hOEJOuBQ8hMMHRs9WryYeLLa9/ydqPWxbwjMEpE8RKdU1dTK6mdZtzNMhbeHdyne2pU1iVKiKmImGw==" }, + "node_modules/azle/node_modules/@dfinity/principal": { + "version": "0.11.2", + "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.11.2.tgz", + "integrity": "sha512-vReWruqIl16yQeKOrCLDLDf2aTEJsXcKeW9qbwVfmV0kwLNE3B2Z6tbRjYbY7s+KwpysD5B1b48ZbIwI00BeyQ==" + }, "node_modules/azle/node_modules/ts-node": { "version": "10.3.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.3.1.tgz", @@ -441,9 +490,9 @@ ] }, "node_modules/bignumber.js": { - "version": "9.0.2", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.2.tgz", - "integrity": "sha512-GAcQvbpsM0pUb0zw1EI0KhQEZ+lRwR5fYaAp3vPOYuP7aDvGy6cVN6XHLauvF8SOga2y0dcLcjt3iQDTSEliyw==", + "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": "*" @@ -505,7 +554,7 @@ "node_modules/delimit-stream": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/delimit-stream/-/delimit-stream-0.1.0.tgz", - "integrity": "sha1-m4MZR3wOX4rrPONXrjBfwl6hzSs=", + "integrity": "sha512-a02fiQ7poS5CnjiJBAsjGLPp5EwVoGHNeu9sziBd9huppRfsAFIpv5zNLv0V1gbop53ilngAf5Kf331AwcoRBQ==", "dev": true }, "node_modules/diff": { @@ -911,7 +960,7 @@ "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": "sha1-py8hfcSvxGKf/1/rME3BvVGi89I=", + "integrity": "sha512-L3mEegEWHRekSHjc7+sc8eJhba9Clq1PZ8kMkzf8OxElhXc8O4TS5MwcVlj9aEbm5dr81N90WHC5nAz3UO971w==", "dev": true, "dependencies": { "delimit-stream": "0.1.0" @@ -934,9 +983,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", @@ -983,12 +1032,12 @@ } }, "node_modules/ts-node": { - "version": "10.7.0", - "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.7.0.tgz", - "integrity": "sha512-TbIGS4xgJoX2i3do417KSaep1uRAW/Lu+WAL2doDHC0D6ummjirVOXU5/7aiZotbQ5p1Zp9tP7U6cYhA0O7M8A==", + "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.7.0", + "@cspotcode/source-map-support": "^0.8.0", "@tsconfig/node10": "^1.0.7", "@tsconfig/node12": "^1.0.7", "@tsconfig/node14": "^1.0.0", @@ -999,7 +1048,7 @@ "create-require": "^1.1.0", "diff": "^4.0.1", "make-error": "^1.1.1", - "v8-compile-cache-lib": "^3.0.0", + "v8-compile-cache-lib": "^3.0.1", "yn": "3.1.1" }, "bin": { @@ -1025,17 +1074,29 @@ } } }, + "node_modules/ts-node/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/typescript": { - "version": "4.6.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.3.tgz", - "integrity": "sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==", + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", "dev": true, "bin": { "tsc": "bin/tsc", "tsserver": "bin/tsserver" }, "engines": { - "node": ">=4.2.0" + "node": ">=14.17" } }, "node_modules/universalify": { @@ -1049,7 +1110,7 @@ "node_modules/util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", "dev": true }, "node_modules/v8-compile-cache-lib": { @@ -1082,29 +1143,62 @@ } }, "@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.2", + "resolved": "https://registry.npmjs.org/@dfinity/agent/-/agent-0.19.2.tgz", + "integrity": "sha512-KLRWEjeU9SyyaS7IBVJ9ZUcufxufr55e/kRIyClK157+0pkTG9a8xKjUIMx3QzKvLsqqzXL238nWwdoP6jAD8g==", "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.1", - "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-0.11.1.tgz", - "integrity": "sha512-EYZZg+x7OgZxa56D9SWMeTlaMb09HJ7wIfL42+l/e0lgrx+sTXYxKe8bM9FrUW9AWo9/gKkOGV/IxJL/Acncng==", + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/@dfinity/candid/-/candid-0.19.2.tgz", + "integrity": "sha512-X2hCqNMhnnmwtnOc0WnymOZYx3qphjEMuSYbBr7tMIkV7Hwt9BmXXlLnQTxUytTPxf+3he0GcS3KzsSQ9CK8ew==", "dev": true, - "peer": true + "peer": true, + "requires": {} }, "@dfinity/principal": { - "version": "0.11.2", - "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.11.2.tgz", - "integrity": "sha512-vReWruqIl16yQeKOrCLDLDf2aTEJsXcKeW9qbwVfmV0kwLNE3B2Z6tbRjYbY7s+KwpysD5B1b48ZbIwI00BeyQ==" + "version": "0.19.2", + "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.19.2.tgz", + "integrity": "sha512-vsKN6BKya70bQUsjgKRDlR2lOpv/XpUkCMIiji6rjMtKHIuWEB5Eu3JqZsOuBmWo3A3TT/K/osT9VPm0k4qdYQ==", + "dev": true, + "peer": true, + "requires": { + "@noble/hashes": "^1.3.1" + } + }, + "@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 + }, + "@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 + }, + "@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, + "requires": { + "@jridgewell/resolve-uri": "^3.0.3", + "@jridgewell/sourcemap-codec": "^1.4.10" + } + }, + "@noble/hashes": { + "version": "1.3.2", + "resolved": "https://registry.npmjs.org/@noble/hashes/-/hashes-1.3.2.tgz", + "integrity": "sha512-MVC8EAQp7MvEcm30KWENFjgR+Mkmf+D189XJTkFIlwohU5hcBbn1ZkKq7KVTi2Hme3PMGF390DaL52beVrIihQ==", + "dev": true }, "@swc/core": { "version": "1.2.151", @@ -1260,6 +1354,11 @@ "typescript": "4.4.4" }, "dependencies": { + "@dfinity/principal": { + "version": "0.11.2", + "resolved": "https://registry.npmjs.org/@dfinity/principal/-/principal-0.11.2.tgz", + "integrity": "sha512-vReWruqIl16yQeKOrCLDLDf2aTEJsXcKeW9qbwVfmV0kwLNE3B2Z6tbRjYbY7s+KwpysD5B1b48ZbIwI00BeyQ==" + }, "ts-node": { "version": "10.3.1", "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.3.1.tgz", @@ -1304,9 +1403,9 @@ "dev": true }, "bignumber.js": { - "version": "9.0.2", - "resolved": "https://registry.npmjs.org/bignumber.js/-/bignumber.js-9.0.2.tgz", - "integrity": "sha512-GAcQvbpsM0pUb0zw1EI0KhQEZ+lRwR5fYaAp3vPOYuP7aDvGy6cVN6XHLauvF8SOga2y0dcLcjt3iQDTSEliyw==", + "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": { @@ -1348,7 +1447,7 @@ "delimit-stream": { "version": "0.1.0", "resolved": "https://registry.npmjs.org/delimit-stream/-/delimit-stream-0.1.0.tgz", - "integrity": "sha1-m4MZR3wOX4rrPONXrjBfwl6hzSs=", + "integrity": "sha512-a02fiQ7poS5CnjiJBAsjGLPp5EwVoGHNeu9sziBd9huppRfsAFIpv5zNLv0V1gbop53ilngAf5Kf331AwcoRBQ==", "dev": true }, "diff": { @@ -1544,7 +1643,7 @@ "json-text-sequence": { "version": "0.1.1", "resolved": "https://registry.npmjs.org/json-text-sequence/-/json-text-sequence-0.1.1.tgz", - "integrity": "sha1-py8hfcSvxGKf/1/rME3BvVGi89I=", + "integrity": "sha512-L3mEegEWHRekSHjc7+sc8eJhba9Clq1PZ8kMkzf8OxElhXc8O4TS5MwcVlj9aEbm5dr81N90WHC5nAz3UO971w==", "dev": true, "requires": { "delimit-stream": "0.1.0" @@ -1565,9 +1664,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", @@ -1597,12 +1696,12 @@ } }, "ts-node": { - "version": "10.7.0", - "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.7.0.tgz", - "integrity": "sha512-TbIGS4xgJoX2i3do417KSaep1uRAW/Lu+WAL2doDHC0D6ummjirVOXU5/7aiZotbQ5p1Zp9tP7U6cYhA0O7M8A==", + "version": "10.9.1", + "resolved": "https://registry.npmjs.org/ts-node/-/ts-node-10.9.1.tgz", + "integrity": "sha512-NtVysVPkxxrwFGUUxGYhfux8k78pQB3JqYBXlLRZgdGUqTO5wU/UyHop5p70iEbGhB7q5KmiZiU0Y3KlJrScEw==", "dev": true, "requires": { - "@cspotcode/source-map-support": "0.7.0", + "@cspotcode/source-map-support": "^0.8.0", "@tsconfig/node10": "^1.0.7", "@tsconfig/node12": "^1.0.7", "@tsconfig/node14": "^1.0.0", @@ -1613,14 +1712,25 @@ "create-require": "^1.1.0", "diff": "^4.0.1", "make-error": "^1.1.1", - "v8-compile-cache-lib": "^3.0.0", + "v8-compile-cache-lib": "^3.0.1", "yn": "3.1.1" + }, + "dependencies": { + "@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, + "requires": { + "@jridgewell/trace-mapping": "0.3.9" + } + } } }, "typescript": { - "version": "4.6.3", - "resolved": "https://registry.npmjs.org/typescript/-/typescript-4.6.3.tgz", - "integrity": "sha512-yNIatDa5iaofVozS/uQJEl3JRWLKKGJKh6Yaiv0GLGSuhpFJe7P3SbHZ8/yjAHRQwKRoA6YZqlfjXWmVzoVSMw==", + "version": "5.2.2", + "resolved": "https://registry.npmjs.org/typescript/-/typescript-5.2.2.tgz", + "integrity": "sha512-mI4WrpHsbCIcwT9cF4FZvr80QUeKvsUsUvKDoR+X/7XHQH98xYD8YHZg7ANtz2GtZt/CBq2QJ0thkGJMHfqc1w==", "dev": true }, "universalify": { @@ -1631,7 +1741,7 @@ "util-deprecate": { "version": "1.0.2", "resolved": "https://registry.npmjs.org/util-deprecate/-/util-deprecate-1.0.2.tgz", - "integrity": "sha1-RQ1Nyfpw3nMnYvvS1KKJgUGaDM8=", + "integrity": "sha512-EPD5q1uXyFxJpCrLnCc1nHnq3gOa6DZBocAIiI2TaSCA7VCJ1UJDMagCzIkXNsUYfD1daK//LTEQ8xiIbrHtcw==", "dev": true }, "v8-compile-cache-lib": { diff --git a/examples/call_raw/package.json b/examples/call_raw/package.json index c07d970dd5..6bc660e4c9 100644 --- a/examples/call_raw/package.json +++ b/examples/call_raw/package.json @@ -7,8 +7,8 @@ "azle": "0.17.1" }, "devDependencies": { - "@dfinity/agent": "0.11.1", - "ts-node": "10.7.0", - "typescript": "4.6.3" + "@dfinity/agent": "^0.19.2", + "ts-node": "^10.9.1", + "typescript": "^5.2.2" } } diff --git a/examples/call_raw/src/call_raw.ts b/examples/call_raw/src/call_raw.ts index c4e5192a10..510f2b94b6 100644 --- a/examples/call_raw/src/call_raw.ts +++ b/examples/call_raw/src/call_raw.ts @@ -1,47 +1,32 @@ -import { - ic, - nat, - nat64, - principal, - Principal, - Result, - Service, - text, - update -} from 'azle'; +import { ic, nat, nat64, principal, Result, Service, text, update } from 'azle'; -export default class extends Service { - @update([principal, text, text, nat64], Result(text, text)) - async executeCallRaw( - canisterId: Principal, - method: text, - candidArgs: text, - payment: nat64 - ): Promise> { - const result = await ic.callRaw( - canisterId, - method, - ic.candidEncode(candidArgs), - payment - ); +export default Service({ + executeCallRaw: update( + [principal, text, text, nat64], + Result(text, text), + async (canisterId, method, candidArgs, payment) => { + const result = await ic.callRaw( + canisterId, + method, + ic.candidEncode(candidArgs), + payment + ); - return Result.Ok(ic.candidDecode(result)); - } + return Result.Ok(ic.candidDecode(result)); + } + ), + executeCallRaw128: update( + [principal, text, text, nat], + Result(text, text), + async (canisterId, method, candidArgs, payment) => { + const result = await ic.callRaw128( + canisterId, + method, + ic.candidEncode(candidArgs), + payment + ); - @update([principal, text, text, nat], Result(text, text)) - async executeCallRaw128( - canisterId: Principal, - method: text, - candidArgs: text, - payment: nat - ): Promise> { - const result = await ic.callRaw128( - canisterId, - method, - ic.candidEncode(candidArgs), - payment - ); - - return Result.Ok(ic.candidDecode(result)); - } -} + return Result.Ok(ic.candidDecode(result)); + } + ) +}); diff --git a/examples/call_raw/test/test.ts b/examples/call_raw/test/test.ts index c29b1a2e11..068696508a 100644 --- a/examples/call_raw/test/test.ts +++ b/examples/call_raw/test/test.ts @@ -2,10 +2,10 @@ import { getCanisterId, runTests } from 'azle/test'; import { createActor } from '../test/dfx_generated/call_raw'; import { get_tests } from './tests'; -const call_raw_canister = createActor(getCanisterId('call_raw'), { +const callRawCanister = createActor(getCanisterId('call_raw'), { agentOptions: { host: 'http://127.0.0.1:8000' } }); -runTests(get_tests(call_raw_canister)); +runTests(get_tests(callRawCanister)); From f2c965cdd582768c69e2a2f2de71a1bf33222b01 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 20:18:22 -0500 Subject: [PATCH 07/10] candid encoding example refactored --- examples/candid_encoding/dfx.json | 3 ++- examples/candid_encoding/src/index.ts | 15 ++++++--------- 2 files changed, 8 insertions(+), 10 deletions(-) diff --git a/examples/candid_encoding/dfx.json b/examples/candid_encoding/dfx.json index 7e45eacc17..77185fa6c5 100644 --- a/examples/candid_encoding/dfx.json +++ b/examples/candid_encoding/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/candid_encoding/candid_encoding.wasm.gz", + "wasm": ".azle/candid_encoding/candid_encoding.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/candid_encoding", "node_compatibility": true diff --git a/examples/candid_encoding/src/index.ts b/examples/candid_encoding/src/index.ts index bdec7ff72b..d6ce91bf50 100644 --- a/examples/candid_encoding/src/index.ts +++ b/examples/candid_encoding/src/index.ts @@ -1,15 +1,12 @@ import { blob, ic, query, Service, text } from 'azle'; -export default class extends Service { +export default Service({ // encodes a Candid string to Candid bytes - @query([text], blob) - candidEncode(candidString: text): blob { + candidEncode: query([text], blob, (candidString) => { return ic.candidEncode(candidString); - } - + }), // decodes Candid bytes to a Candid string - @query([blob], text) - candidDecode(candidEncoded: blob): text { + candidDecode: query([blob], text, (candidEncoded) => { return ic.candidDecode(candidEncoded); - } -} + }) +}); From 3ddae115437a281a4e4d3c19adcd5a2768f0d80d Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 20:19:14 -0500 Subject: [PATCH 08/10] add to test.yml --- .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 e879a96bea..da21531fc9 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,6 @@ # The check-basic-integration-tests-success job is designed to ensure that all jobs spun up from the matrix in the basic-integration-tests have succeeded # All Examples TODO restore when https://github.com/demergent-labs/azle/issues/1192 is resolved -# "examples/candid_encoding", # "examples/complex_init", # "examples/complex_types", # "examples/composite_queries", @@ -114,6 +113,7 @@ jobs: "examples/blob_array", "examples/bytes", "examples/call_raw", + "examples/candid_encoding", "examples/primitive_types", "examples/principal", "examples/query", From 01bccce506eb1784f8bec85ed14779a7c1ec8907 Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 21:17:36 -0500 Subject: [PATCH 09/10] init implemented --- examples/complex_init/dfx.json | 3 +- examples/complex_init/src/index.ts | 46 +++++++------------ .../azle_generate_rearchitecture/src/main.rs | 16 +++---- src/lib_functional/candid/index.ts | 12 ++++- .../candid/reference/service.ts | 16 ++++++- src/lib_functional/canister_methods/index.ts | 8 +++- src/lib_functional/canister_methods/init.ts | 45 ++++++++++++++++++ src/lib_functional/canister_methods/query.ts | 1 + src/lib_functional/canister_methods/update.ts | 1 + 9 files changed, 105 insertions(+), 43 deletions(-) create mode 100644 src/lib_functional/canister_methods/init.ts diff --git a/examples/complex_init/dfx.json b/examples/complex_init/dfx.json index 78ff0cc0c4..7728afb2ad 100644 --- a/examples/complex_init/dfx.json +++ b/examples/complex_init/dfx.json @@ -6,7 +6,8 @@ "root": "src", "ts": "src/index.ts", "candid": "src/index.did", - "wasm": ".azle/complex_init/complex_init.wasm.gz", + "wasm": ".azle/complex_init/complex_init.wasm", + "gzip": true, "declarations": { "output": "test/dfx_generated/complex_init", "node_compatibility": true diff --git a/examples/complex_init/src/index.ts b/examples/complex_init/src/index.ts index a5e69a9e9e..e3e82435d6 100644 --- a/examples/complex_init/src/index.ts +++ b/examples/complex_init/src/index.ts @@ -1,32 +1,20 @@ -import { - candid, - init, - Opt, - query, - Record, - Service, - text, - Tuple, - Void -} from 'azle'; +import { init, Opt, query, Record, Service, text, Tuple } from 'azle'; -class User extends Record { - @candid(text) - id: text; -} +const User = Record({ + id: text +}); -export default class extends Service { - greeting: text = 'Hello User'; - user: Opt = []; +let greeting: text = 'Hello User'; +let user: Opt = []; - @init([Tuple(text, User)]) - init(tuple: Tuple<[string, User]>): Void { - this.greeting = tuple[0]; - this.user = [tuple[1]]; - } - - @query([], text) - greetUser(): text { - return `${this.greeting} ${this.user[0]?.id ?? '??'}`; - } -} +// TODO tuple types aren't done, they don't have TypeScript types +export default Service({ + init: init([Tuple(text, User)], (tuple) => { + greeting = tuple[0]; + user = [tuple[1]]; + return undefined; + }), + greetUser: query([], text, () => { + return `${greeting} ${user[0]?.id ?? '??'}`; + }) +}); diff --git a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs index 4c0b8ef8c4..2ad79ee237 100644 --- a/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs +++ b/src/compiler/typescript_to_rust/azle_generate_rearchitecture/src/main.rs @@ -50,6 +50,12 @@ fn main() -> Result<(), String> { let ic = ic::generate(); + let init_method_call = compiler_info.canister_methods.init.map(|init_method| { + let js_function_name = &init_method.name; + + quote!(execute_js(#js_function_name, true);) + }); + let pre_upgrade_method = compiler_info .canister_methods .pre_upgrade @@ -218,20 +224,12 @@ fn main() -> Result<(), String> { context.eval_global("exports.js", "globalThis.exports = {};").unwrap(); context.eval_global("main.js", std::str::from_utf8(MAIN_JS).unwrap()).unwrap(); - let _azle_init_name = global.get_property("_azleInitName").unwrap(); - let _azle_init_name_string = if !_azle_init_name.is_undefined() { - let _azle_init_name_js_value: JSValue = from_qjs_value(&_azle_init_name).unwrap(); - _azle_init_name_js_value.try_into().unwrap() - } else { "".to_string() }; - CONTEXT.with(|ctx| { let mut ctx = ctx.borrow_mut(); *ctx = Some(context); }); - if _azle_init_name_string != "" { - execute_js(&_azle_init_name_string, true); - } + #init_method_call } #[ic_cdk_macros::post_upgrade] diff --git a/src/lib_functional/candid/index.ts b/src/lib_functional/candid/index.ts index 38b672b806..adb5c74671 100644 --- a/src/lib_functional/candid/index.ts +++ b/src/lib_functional/candid/index.ts @@ -40,7 +40,9 @@ import { bool, AzlePrincipal, AzleResult, - Result + Result, + AzleTuple, + Tuple } from '../../lib_new'; export type TypeMapping = T extends IDL.TextClass @@ -73,7 +75,13 @@ export type TypeMapping = T extends IDL.TextClass ? float32 : T extends never[] ? void - : T extends AzleVec + : T extends AzleTuple + ? any + : // : T extends AzleTuple<[infer U]> + // ? [TypeMapping] + // : T extends AzleTuple<[infer U, infer W]> + // ? [TypeMapping, TypeMapping] + T extends AzleVec ? TypeMapping[] : T extends AzleOpt ? [TypeMapping] | [] diff --git a/src/lib_functional/candid/reference/service.ts b/src/lib_functional/candid/reference/service.ts index 186bf58a68..f152bf4628 100644 --- a/src/lib_functional/candid/reference/service.ts +++ b/src/lib_functional/candid/reference/service.ts @@ -70,6 +70,16 @@ export function Service( [] ); + const initOption = Object.entries(serviceOptions).find( + ([key, value]) => value.mode === 'init' + ); + const init = + initOption === undefined + ? undefined + : { + name: initOption[0] + }; + const queries = Object.entries(serviceOptions) .filter((entry) => { const key = entry[0]; @@ -148,8 +158,11 @@ export function Service( returnFunction.candid = `${ candidTypes.length === 0 ? '' : candidTypes.join('\n') + '\n' - }service: () -> { + }service: (${initOption?.[1].candid ?? ''}) -> { ${Object.entries(serviceOptions) + .filter( + ([_, value]) => value.mode === 'query' || value.mode === 'update' + ) .map((entry) => { return `${entry[0]}: ${entry[1].candid}`; }) @@ -157,6 +170,7 @@ export function Service( } `; + returnFunction.init = init; returnFunction.queries = queries; returnFunction.updates = updates; returnFunction.callbacks = callbacks; diff --git a/src/lib_functional/canister_methods/index.ts b/src/lib_functional/canister_methods/index.ts index fdfb8e0af3..5d987fe070 100644 --- a/src/lib_functional/canister_methods/index.ts +++ b/src/lib_functional/canister_methods/index.ts @@ -6,11 +6,12 @@ import { EncodeVisitor } from '../../lib_new/visitors/encode_decode'; +export * from './init'; export * from './query'; export * from './update'; export type CanisterMethodInfo, K> = { - mode: 'query' | 'update'; + mode: 'query' | 'update' | 'init'; callback?: (...args: any) => any; candid: string; candidTypes: string[]; @@ -23,6 +24,7 @@ export type Callback, Return> = ( ) => TypeMapping | Promise>; export function executeMethod( + mode: CanisterMethodInfo['mode'], paramCandid: any, returnCandid: any, args: any[], @@ -41,6 +43,10 @@ export function executeMethod( const result = callback(...myDecodedObject); + if (mode === 'init') { + return; + } + if ( result !== undefined && result !== null && diff --git a/src/lib_functional/canister_methods/init.ts b/src/lib_functional/canister_methods/init.ts new file mode 100644 index 0000000000..e319da13ed --- /dev/null +++ b/src/lib_functional/canister_methods/init.ts @@ -0,0 +1,45 @@ +import { + handleRecursiveParams, + handleRecursiveReturn, + newTypesToStingArr +} from '../../lib_new/method_decorators'; +import { Callback, CanisterMethodInfo, executeMethod } from '.'; +import { TypeMapping } from '../candid'; +import { Void } from '../../lib_new'; + +export function init< + const Params extends ReadonlyArray, + GenericCallback extends Callback +>( + paramsIdls: Params, + callback?: Awaited> extends TypeMapping + ? GenericCallback + : never +): CanisterMethodInfo { + const paramCandid = handleRecursiveParams(paramsIdls as any); + const returnCandid = handleRecursiveReturn(Void as any, paramCandid[2]); + + const finalCallback = + callback === undefined + ? undefined + : (...args: any[]) => { + executeMethod( + 'init', + paramCandid, + returnCandid, + args, + callback, + paramsIdls as any, + Void + ); + }; + + return { + mode: 'init', + callback: finalCallback, + candid: paramCandid[1].join(', '), + candidTypes: newTypesToStingArr(returnCandid[2]), + paramsIdls: paramsIdls as any, + returnIdl: Void + }; +} diff --git a/src/lib_functional/canister_methods/query.ts b/src/lib_functional/canister_methods/query.ts index 25565892d7..1df9db032b 100644 --- a/src/lib_functional/canister_methods/query.ts +++ b/src/lib_functional/canister_methods/query.ts @@ -29,6 +29,7 @@ export function query< ? undefined : (...args: any[]) => { executeMethod( + 'query', paramCandid, returnCandid, args, diff --git a/src/lib_functional/canister_methods/update.ts b/src/lib_functional/canister_methods/update.ts index 42f110ba0a..491f57f5f3 100644 --- a/src/lib_functional/canister_methods/update.ts +++ b/src/lib_functional/canister_methods/update.ts @@ -28,6 +28,7 @@ export function update< ? undefined : (...args: any[]) => { executeMethod( + 'update', paramCandid, returnCandid, args, From 75f1e2e1803fe888910aff533c5f8ab754205d7c Mon Sep 17 00:00:00 2001 From: Jordan Last Date: Sat, 23 Sep 2023 21:19:22 -0500 Subject: [PATCH 10/10] add complex_init --- .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 da21531fc9..59001dd23c 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -14,7 +14,6 @@ # The check-basic-integration-tests-success job is designed to ensure that all jobs spun up from the matrix in the basic-integration-tests have succeeded # All Examples TODO restore when https://github.com/demergent-labs/azle/issues/1192 is resolved -# "examples/complex_init", # "examples/complex_types", # "examples/composite_queries", # "examples/counter", @@ -114,6 +113,7 @@ jobs: "examples/bytes", "examples/call_raw", "examples/candid_encoding", + "examples/complex_init", "examples/primitive_types", "examples/principal", "examples/query",