Skip to content

Commit

Permalink
Merge pull request #1907 from demergent-labs/candid_generation
Browse files Browse the repository at this point in the history
Candid generation
  • Loading branch information
lastmjs authored Jul 17, 2024
2 parents a787888 + 68479d2 commit 1efffe4
Show file tree
Hide file tree
Showing 150 changed files with 5,200 additions and 1,069 deletions.
11 changes: 9 additions & 2 deletions src/compiler/compile_typescript_code.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export async function compileTypeScriptToJavaScript(
esmExternals: string[]
): Promise<Result<JavaScript, unknown>> {
try {
const imports = `
const imports = /*TS*/ `
import 'reflect-metadata';
// Trying to make sure that all globalThis dependencies are defined
Expand All @@ -26,19 +26,26 @@ export async function compileTypeScriptToJavaScript(
ethers.FetchRequest.registerGetUrl(ethersGetUrl);
import { toDidString } from 'azle/src/lib/candid/did_file/to_did_string';
import { IDL } from 'azle';
import { DidVisitor, getDefaultVisitorData } from 'azle/src/lib/candid/did_file/visitor';
export { Principal } from '@dfinity/principal';
export * from './${main}';
import * as CanisterMethods from './${main}';
if (isClassSyntaxExport(CanisterMethods)) {
const canister = new CanisterMethods.default();
const canisterIdlType = IDL.Service(globalThis._azleCanisterMethodIdlTypes);
const candid = canisterIdlType.accept(new DidVisitor(), {
...getDefaultVisitorData(),
isFirstService: true,
systemFuncs: globalThis._azleInitAndPostUpgradeIdlTypes
});
globalThis._azleCanisterClassInstance = canister;
globalThis.candidInfoFunction = () => {
return JSON.stringify({
candid: '',
candid: toDidString(candid),
canisterMethods: globalThis._azleCanisterMethods
});
};
Expand Down
9 changes: 9 additions & 0 deletions src/lib/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { v4 } from 'uuid';
import { azleFetch } from './fetch';
import { ic } from './ic';
import { AzleIc } from './ic/types/azle_ic';
import { IDL } from './stable';
import { jsonReplacer } from './stable_structures/stable_json';

type CanisterMethods = {
Expand Down Expand Up @@ -64,6 +65,10 @@ declare global {
var _azleCanisterMethods: CanisterMethods;
// eslint-disable-next-line no-var
var _azleCanisterMethodsIndex: number;
// eslint-disable-next-line no-var
var _azleCanisterMethodIdlTypes: { [key: string]: IDL.FuncClass };
// eslint-disable-next-line no-var
var _azleInitAndPostUpgradeIdlTypes: IDL.FuncClass[];
}

globalThis._azleInsideCanister =
Expand All @@ -79,6 +84,10 @@ if (globalThis._azleInsideCanister) {
callbacks: {}
};

globalThis._azleCanisterMethodIdlTypes = {};

globalThis._azleInitAndPostUpgradeIdlTypes = [];

globalThis._azleInitCalled = false;
globalThis._azlePostUpgradeCalled = false;

Expand Down
4 changes: 4 additions & 0 deletions src/lib/stable/globals.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,9 @@ declare global {

globalThis._azleCanisterMethodsIndex = 0;

globalThis._azleCanisterMethodIdlTypes = {};

globalThis._azleInitAndPostUpgradeIdlTypes = [];

globalThis.TextDecoder = TextDecoder;
globalThis.TextEncoder = TextEncoder;
4 changes: 4 additions & 0 deletions src/lib/stable/init.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export function init<This, Args extends any[], Return>(
index
};

globalThis._azleInitAndPostUpgradeIdlTypes.push(
IDL.Func(paramIdlTypes, [], ['init'])
);

globalThis._azleCanisterMethods.callbacks[index.toString()] = (
...args: any[]
): void => {
Expand Down
4 changes: 4 additions & 0 deletions src/lib/stable/post_upgrade.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ export function postUpgrade<This, Args extends any[], Return>(
index
};

globalThis._azleInitAndPostUpgradeIdlTypes.push(
IDL.Func(paramIdlTypes, [], ['post_upgrade'])
);

globalThis._azleCanisterMethods.callbacks[index.toString()] = (
...args: any[]
): void => {
Expand Down
12 changes: 10 additions & 2 deletions src/lib/stable/query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,21 @@ export function query<This, Args extends any[], Return>(
context: ClassMethodDecoratorContext
): void => {
const index = globalThis._azleCanisterMethodsIndex++;
const name = context.name as string;
const composite = options?.composite ?? false;

globalThis._azleCanisterMethods.queries.push({
name: context.name as string,
name,
index,
composite: options?.composite ?? false
composite
});

globalThis._azleCanisterMethodIdlTypes[name] = IDL.Func(
paramIdlTypes,
returnIdlType === undefined ? [] : [returnIdlType],
['query']
);

globalThis._azleCanisterMethods.callbacks[index.toString()] = (
...args: any[]
): void => {
Expand Down
11 changes: 7 additions & 4 deletions src/lib/stable/update.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,14 @@ export function update<This, Args extends any[], Return>(
context: ClassMethodDecoratorContext
): void => {
const index = globalThis._azleCanisterMethodsIndex++;
const name = context.name as string;

globalThis._azleCanisterMethods.updates.push({
name: context.name as string,
index
});
globalThis._azleCanisterMethods.updates.push({ name, index });

globalThis._azleCanisterMethodIdlTypes[name] = IDL.Func(
paramIdlTypes,
returnIdlType === undefined ? [] : [returnIdlType]
);

globalThis._azleCanisterMethods.callbacks[index.toString()] = (
...args: any[]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"async_await": {
"type": "azle",
"main": "src/async_await.ts",
"candid": "src/async_await.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/async_await",
"node_compatibility": true
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"audio_recorder": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/audio_recorder",
"node_compatibility": true
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions tests/end_to_end/candid_rpc/class_syntax/bitcoin/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"bitcoin": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/bitcoin",
"node_compatibility": true
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions tests/end_to_end/candid_rpc/class_syntax/blob_array/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"blob_array": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/blob_array",
"node_compatibility": true
Expand Down

This file was deleted.

3 changes: 1 addition & 2 deletions tests/end_to_end/candid_rpc/class_syntax/bytes/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"bytes_canister": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/bytes_canister",
"node_compatibility": true
Expand Down
3 changes: 0 additions & 3 deletions tests/end_to_end/candid_rpc/class_syntax/bytes/src/index.did

This file was deleted.

3 changes: 1 addition & 2 deletions tests/end_to_end/candid_rpc/class_syntax/call_raw/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"call_raw": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/call_raw",
"node_compatibility": true
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"candid_encoding": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/candid_encoding",
"node_compatibility": true
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"candid_keywords": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/candid_keywords",
"node_compatibility": true
Expand Down

This file was deleted.

5 changes: 2 additions & 3 deletions tests/end_to_end/candid_rpc/class_syntax/canister/dfx.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"canister": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"env": ["SOME_CANISTER_PRINCIPAL"],
"assets": [["src/some_canister.did", "candid/some_canister.did"]],
"declarations": {
Expand All @@ -16,7 +15,7 @@
"type": "azle",
"main": "src/some_canister.ts",
"candid": "src/some_canister.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/some_canister",
"node_compatibility": true
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,7 @@
"complex_init": {
"type": "azle",
"main": "src/complex_init/index.ts",
"candid": "src/complex_init/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/complex_init",
"node_compatibility": true
Expand All @@ -13,8 +12,7 @@
"rec_init": {
"type": "azle",
"main": "src/rec_init/index.ts",
"candid": "src/rec_init/index.did",
"candid_gen": "custom",
"candid_gen": "automatic",
"declarations": {
"output": "test/dfx_generated/rec_init",
"node_compatibility": true
Expand Down
Loading

0 comments on commit 1efffe4

Please sign in to comment.