Skip to content

Commit

Permalink
add candidDecode, add raw option to call
Browse files Browse the repository at this point in the history
  • Loading branch information
lastmjs committed Jun 24, 2024
1 parent 59327a4 commit 3a3fc78
Show file tree
Hide file tree
Showing 17 changed files with 10,118 additions and 39 deletions.
3 changes: 2 additions & 1 deletion examples/call_raw/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/call_raw/package.json
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
{
"name": "call_raw_end_to_end_test_functional_syntax",
"scripts": {
"pretest": "ts-node --transpile-only --ignore=false test/pretest.ts",
"test": "jest"
Expand Down
10 changes: 4 additions & 6 deletions examples/call_raw/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,15 @@ import {
ic,
nat,
nat64,
Ok,
Principal,
Result,
text,
update
} from 'azle/experimental';

export default Canister({
executeCallRaw: update(
[Principal, text, text, nat64],
Result(text, text),
text,
async (canisterId, method, candidArgs, payment) => {
const result = await ic.callRaw(
canisterId,
Expand All @@ -22,12 +20,12 @@ export default Canister({
payment
);

return Ok(ic.candidDecode(result));
return ic.candidDecode(result);
}
),
executeCallRaw128: update(
[Principal, text, text, nat],
Result(text, text),
text,
async (canisterId, method, candidArgs, payment) => {
const result = await ic.callRaw128(
canisterId,
Expand All @@ -36,7 +34,7 @@ export default Canister({
payment
);

return Ok(ic.candidDecode(result));
return ic.candidDecode(result);
}
)
});
30 changes: 6 additions & 24 deletions examples/call_raw/test/tests.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,7 @@ export function getTests(call_raw_canister: ActorSubclass<_SERVICE>): Test {
0n
);

if ('Err' in result) {
throw new Error(result.Err);
}

expect(result).toHaveProperty('Ok');
expect(result.Ok).toMatch('blob');
expect(result).toMatch('blob');
});

it('calls create_canister via execute_call_raw', async () => {
Expand All @@ -30,13 +25,8 @@ export function getTests(call_raw_canister: ActorSubclass<_SERVICE>): Test {
'(record { settings = null })',
100_000_000_000n
);

if ('Err' in result) {
throw new Error(result.Err);
}

expect(result.Ok).toMatch('record');
expect(result.Ok).toMatch('principal');
expect(result).toMatch('record');
expect(result).toMatch('principal');
});

it('calls raw_rand via execute_call_raw128', async () => {
Expand All @@ -47,11 +37,7 @@ export function getTests(call_raw_canister: ActorSubclass<_SERVICE>): Test {
0n
);

if ('Err' in result) {
throw new Error(result.Err);
}

expect(result.Ok).toMatch('blob');
expect(result).toMatch('blob');
});

it('calls create_canister via execute_call_raw128', async () => {
Expand All @@ -62,12 +48,8 @@ export function getTests(call_raw_canister: ActorSubclass<_SERVICE>): Test {
100_000_000_000n
);

if ('Err' in result) {
throw new Error(result.Err);
}

expect(result.Ok).toMatch('record');
expect(result.Ok).toMatch('principal');
expect(result).toMatch('record');
expect(result).toMatch('principal');
});
};
}
22 changes: 14 additions & 8 deletions src/lib/stable/ic_apis/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,17 @@ import { v4 } from 'uuid'; // TODO is uuid experimental?

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

export async function call<T>(
export async function call(
canisterId: Principal | string,
method: string,
options?: {
paramIdls?: IDL.Type[];
returnIdl?: IDL.Type;
args?: any[];
payment?: bigint;
raw?: Uint8Array;
}
): Promise<T> {
): Promise<any> {
// TODO this should use a Result remember
return new Promise((resolve, reject) => {
if (globalThis._azleIc === undefined) {
Expand All @@ -23,15 +24,18 @@ export async function call<T>(
const globalRejectId = `_reject_${promiseId}`;

const returnIdl = options?.returnIdl;
const raw = options?.raw;

// TODO perhaps we should be more robust
// TODO for example, we can keep the time with these
// TODO if they are over a certain amount old we can delete them
globalThis._azleResolveIds[globalResolveId] = (result: ArrayBuffer) => {
if (returnIdl === undefined) {
resolve(undefined as T);
if (raw !== undefined) {
resolve(new Uint8Array(result));
} else if (returnIdl === undefined) {
resolve(undefined);
} else {
resolve(IDL.decode([returnIdl], result)[0] as T);
resolve(IDL.decode([returnIdl], result)[0]);
}

delete globalThis._azleResolveIds[globalResolveId];
Expand All @@ -54,13 +58,15 @@ export async function call<T>(
? Principal.fromText(canisterId)
: canisterId;
const canisterIdBytes = canisterIdPrincipal.toUint8Array().buffer;
const argsRawBuffer = new Uint8Array(IDL.encode(paramIdls, args))
.buffer;
const argsRawBuffer =
raw === undefined
? new Uint8Array(IDL.encode(paramIdls, args)).buffer
: raw.buffer;
const paymentString = payment.toString();

// TODO consider finally, what if deletion goes wrong
try {
globalThis._azleIc.callRaw(
globalThis._azleIc.callRaw128(
promiseId,
canisterIdBytes,
method,
Expand Down
12 changes: 12 additions & 0 deletions src/lib/stable/ic_apis/candid_decode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
/**
* Converts a candid value into a Candid string
* @param candidEncoded a raw Candid value
* @returns the Candid string
*/
export function candidDecode(candidEncoded: Uint8Array): string {
if (globalThis._azleIc === undefined) {
return undefined as any;
}

return globalThis._azleIc.candidDecode(candidEncoded.buffer);
}
1 change: 1 addition & 0 deletions src/lib/stable/ic_apis/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
export { call } from './call';
export { candidDecode } from './candid_decode';
export { candidEncode } from './candid_encode';
export { id } from './id';
export { notify } from './notify';
Expand Down
4 changes: 4 additions & 0 deletions tests/end_to_end/candid_rpc/class_syntax/call_raw/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
.azle
.dfx
dfx_generated
node_modules
14 changes: 14 additions & 0 deletions tests/end_to_end/candid_rpc/class_syntax/call_raw/dfx.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
{
"canisters": {
"call_raw": {
"type": "azle",
"main": "src/index.ts",
"candid": "src/index.did",
"candid_gen": "custom",
"declarations": {
"output": "test/dfx_generated/call_raw",
"node_compatibility": true
}
}
}
}
10 changes: 10 additions & 0 deletions tests/end_to_end/candid_rpc/class_syntax/call_raw/jest.config.js
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 3a3fc78

Please sign in to comment.