Skip to content

Commit

Permalink
Merge pull request #1850 from demergent-labs/cross_cansiter_calls
Browse files Browse the repository at this point in the history
implement cross_canister_calls example, notify, trap
  • Loading branch information
lastmjs authored Jul 8, 2024
2 parents 1cefab0 + cb42014 commit 1f479e7
Show file tree
Hide file tree
Showing 21 changed files with 10,313 additions and 7 deletions.
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) {
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

0 comments on commit 1f479e7

Please sign in to comment.