Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implement cross_canister_calls example, notify, trap #1850

Merged
merged 2 commits into from
Jul 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion examples/cross_canister_calls/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions examples/cross_canister_calls/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "cross_canister_calls_end_to_end_test_functional_syntax",
"scripts": {
"pre_tests": "ts-node --transpile-only --ignore=false test/pretest.ts",
"tests": "npm run pre_tests && jest",
Expand Down
2 changes: 1 addition & 1 deletion examples/cross_canister_calls/test/tests.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { ActorSubclass } from '@dfinity/agent';
import { getCanisterId } from 'azle/dfx';
import { expect, it, Test } from 'azle/test/jest';

import { getCanisterId } from '../../../dfx';
import { _SERVICE as CANISTER1_SERVICE } from './dfx_generated/canister1/canister1.did';
import { _SERVICE as CANISTER2_SERVICE } from './dfx_generated/canister2/canister2.did';

Expand Down
8 changes: 4 additions & 4 deletions src/lib/stable/ic_apis/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { v4 } from 'uuid'; // TODO is uuid experimental?

import { IDL, Principal } from '../';

export async function call(
export async function call<T>(
canisterId: Principal | string,
method: string,
options?: {
Expand All @@ -11,7 +11,7 @@ export async function call(
args?: any[];
payment?: bigint;
}
): Promise<any> {
): Promise<T> {
// TODO this should use a Result remember
return new Promise((resolve, reject) => {
if (globalThis._azleIc === undefined) {
Expand All @@ -29,9 +29,9 @@ export async function call(
// TODO if they are over a certain amount old we can delete them
globalThis._azleResolveIds[globalResolveId] = (result: ArrayBuffer) => {
if (returnIdl === undefined) {
resolve(undefined);
resolve(undefined as T);
} else {
resolve(IDL.decode([returnIdl], result)[0]);
resolve(IDL.decode([returnIdl], result)[0] as T);
}

delete globalThis._azleResolveIds[globalResolveId];
Expand Down
2 changes: 2 additions & 0 deletions src/lib/stable/ic_apis/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export { call } from './call';
export { notify } from './notify';
export { trap } from './trap';
42 changes: 42 additions & 0 deletions src/lib/stable/ic_apis/notify.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import { IDL, Principal } from '../';

/**
* Performs a cross-canister call without awaiting the result
* @param canisterId
* @param method
* @param argsRaw
* @param payment
* @returns
*/
export function notify(
canisterId: Principal | string,
method: string,
options?: {
paramIdls?: IDL.Type[];
args?: any[];
payment?: bigint;
}
): void {
if (globalThis._azleIc === undefined) {
lastmjs marked this conversation as resolved.
Show resolved Hide resolved
return undefined as any;
}

const paramIdls = options?.paramIdls ?? [];
const args = options?.args ?? [];
const payment = options?.payment ?? 0n;

const canisterIdPrincipal =
typeof canisterId === 'string'
? Principal.fromText(canisterId)
: canisterId;
const canisterIdBytes = canisterIdPrincipal.toUint8Array().buffer;
const argsRawBuffer = new Uint8Array(IDL.encode(paramIdls, args)).buffer;
const paymentString = payment.toString();

return globalThis._azleIc.notifyRaw(
canisterIdBytes,
method,
argsRawBuffer,
paymentString
);
}
12 changes: 12 additions & 0 deletions src/lib/stable/ic_apis/trap.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Stops execution and rejects the current request with a `CANISTER_ERROR`
* (5) rejection code and the provided message
* @param message the rejection message
*/
export function trap(message: string): never {
if (globalThis._azleIc === undefined) {
return undefined as never;
}

return globalThis._azleIc.trap(message);
}
1 change: 0 additions & 1 deletion src/lib/stable/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
export * from '../ic';
export * from '../stable_structures/stable_b_tree_map';
export * from '../stable_structures/stable_json';
export { heartbeat } from './heartbeat';
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
{
"canisters": {
"canister1": {
"type": "azle",
"main": "src/canister1/index.ts",
"candid": "src/canister1/index.did",
"candid_gen": "custom",
"env": ["CANISTER2_PRINCIPAL", "AZLE_TEST_FETCH"],
"assets": [["src/canister2/index.did", "candid/canister2.did"]],
"declarations": {
"output": "test/dfx_generated/canister1",
"node_compatibility": true
}
},
"canister2": {
"type": "azle",
"main": "src/canister2/index.ts",
"candid": "src/canister2/index.did",
"candid_gen": "custom",
"declarations": {
"output": "test/dfx_generated/canister2",
"node_compatibility": true
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('ts-jest').JestConfigWithTsJest} */
module.exports = {
preset: 'ts-jest',
testEnvironment: 'node',
transform: {
'^.+\\.ts$': 'ts-jest',
'^.+\\.js$': 'ts-jest'
},
transformIgnorePatterns: ['/node_modules/(?!(azle)/)'] // Make sure azle is transformed
};
Loading
Loading