Skip to content

Commit

Permalink
fix typing for ic object
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 3, 2023
1 parent 2002449 commit bf01489
Show file tree
Hide file tree
Showing 12 changed files with 124 additions and 28 deletions.
11 changes: 9 additions & 2 deletions src/lib/globals.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,17 @@
import { Ic, ic } from './ic';
import { ic } from './ic';
import { AzleIc } from './ic/types/azle_ic';
import { Buffer } from 'buffer';

declare global {
var _azleIc: Ic;
var _azleIc: AzleIc;
var _azleResolveIds: { [key: string]: (buf: ArrayBuffer) => void };
var _azleRejectIds: { [key: string]: (err: any) => void };
var icTimers: { [key: string]: string };
var _azleTimerCallbackIds: { [key: string]: () => void };
}

interface global {}

export declare var globalThis: {
_azleCandidInitParams: any[];
_azleCandidMethods: any[];
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/call.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export function call<T extends (...args: any[]) => any>(
}
): ReturnTypeOf<T> {
// TODO probably get rid of .crossCanisterCallback
return method.crossCanisterCallback(
return (method as any).crossCanisterCallback(
'_AZLE_CROSS_CANISTER_CALL',
false,
callRaw,
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/call128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ export function call128<T extends (...args: any[]) => any>(
cycles?: nat;
}
): ReturnTypeOf<T> {
return method.crossCanisterCallback(
return (method as any).crossCanisterCallback(
'_AZLE_CROSS_CANISTER_CALL',
false,
callRaw128,
Expand Down
16 changes: 8 additions & 8 deletions src/lib/ic/call_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ export function callRaw(
// 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[globalResolveId] = (bytes: ArrayBuffer) => {
globalThis._azleResolveIds[globalResolveId] = (bytes: ArrayBuffer) => {
resolve(new Uint8Array(bytes));

delete globalThis[globalResolveId];
delete globalThis[globalRejectId];
delete globalThis._azleResolveIds[globalResolveId];
delete globalThis._azleRejectIds[globalRejectId];
};

globalThis[globalRejectId] = (error: any) => {
globalThis._azleRejectIds[globalRejectId] = (error: any) => {
reject(error);

delete globalThis[globalResolveId];
delete globalThis[globalRejectId];
delete globalThis._azleResolveIds[globalResolveId];
delete globalThis._azleRejectIds[globalRejectId];
};

const canisterIdBytes = canisterId.toUint8Array().buffer;
Expand All @@ -60,8 +60,8 @@ export function callRaw(
paymentCandidBytes
);
} catch (error) {
delete globalThis[globalResolveId];
delete globalThis[globalRejectId];
delete globalThis._azleResolveIds[globalResolveId];
delete globalThis._azleRejectIds[globalRejectId];
throw error;
}
});
Expand Down
16 changes: 8 additions & 8 deletions src/lib/ic/call_raw_128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,18 +30,18 @@ export function callRaw128(
// 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[globalResolveId] = (bytes: ArrayBuffer) => {
globalThis._azleResolveIds[globalResolveId] = (bytes: ArrayBuffer) => {
resolve(new Uint8Array(bytes));

delete globalThis[globalResolveId];
delete globalThis[globalRejectId];
delete globalThis._azleResolveIds[globalResolveId];
delete globalThis._azleRejectIds[globalRejectId];
};

globalThis[globalRejectId] = (error: any) => {
globalThis._azleRejectIds[globalRejectId] = (error: any) => {
reject(error);

delete globalThis[globalResolveId];
delete globalThis[globalRejectId];
delete globalThis._azleResolveIds[globalResolveId];
delete globalThis._azleRejectIds[globalRejectId];
};

const canisterIdBytes = canisterId.toUint8Array().buffer;
Expand All @@ -60,8 +60,8 @@ export function callRaw128(
paymentCandidBytes
);
} catch (error) {
delete globalThis[globalResolveId];
delete globalThis[globalRejectId];
delete globalThis._azleResolveIds[globalResolveId];
delete globalThis._azleRejectIds[globalRejectId];
throw error;
}
});
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/clear_timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@ export function clearTimer(timerId: TimerId): Void {
const timerCallbackId = globalThis.icTimers[timerId.toString()];

delete globalThis.icTimers[timerId.toString()];
delete globalThis[timerCallbackId];
delete globalThis._azleTimerCallbackIds[timerCallbackId];
}
3 changes: 1 addition & 2 deletions src/lib/ic/data_certificate.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,7 @@ import { None, Opt, Some } from '../candid/types/constructed/opt';
* @returns the data certificate or None
*/
export function dataCertificate(): Opt<blob> {
const rawRustValue: ArrayBuffer | undefined =
globalThis._azleIc.dataCertificate();
const rawRustValue = globalThis._azleIc.dataCertificate();

return rawRustValue === undefined
? None
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/notify.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ export function notify<T extends (...args: any[]) => any>(
cycles?: nat;
}
): Void {
return method.crossCanisterCallback(
return (method as any).crossCanisterCallback(
'_AZLE_CROSS_CANISTER_CALL',
true,
notifyRaw,
Expand Down
4 changes: 2 additions & 2 deletions src/lib/ic/set_timer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,12 @@ export function setTimer(

globalThis.icTimers[timerId.toString()] = timerCallbackId;

globalThis[timerCallbackId] = () => {
globalThis._azleTimerCallbackIds[timerCallbackId] = () => {
try {
callback();
} finally {
delete globalThis.icTimers[timerId.toString()];
delete globalThis[timerCallbackId];
delete globalThis._azleTimerCallbackIds[timerCallbackId];
}
};

Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/set_timer_interval.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ export function setTimerInterval(

// We don't delete this even if the callback throws because
// it still needs to be here for the next tick
globalThis[timerCallbackId] = callback;
globalThis._azleTimerCallbackIds[timerCallbackId] = callback;

return timerId;
}
90 changes: 90 additions & 0 deletions src/lib/ic/types/azle_ic.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
// This unverified signature is helpful for catching where we use this so we can pick up the types easily. If I do any then i will miss them all
type UnverifiedSignature = () => void;
/**
* The interface for our rust methods it slightly different than the interface
* we expose to the users. This is the interface for the rust functions.
*/
export type AzleIc = {
argDataRaw: () => ArrayBufferLike;
argDataRawSize: () => bigint;
callRaw: (
promiseId: string,
canisterIdBytes: ArrayBufferLike,
method: string,
argsRaw: ArrayBufferLike,
paymentCandidBytes: ArrayBufferLike
) => void;
callRaw128: (
promiseId: string,
canisterIdBytes: ArrayBufferLike,
method: string,
argsRaw: ArrayBufferLike,
paymentCandidBytes: ArrayBufferLike
) => void;
caller: () => ArrayBufferLike;
candidDecode: (candidBytes: ArrayBufferLike) => string;
candidEncode: (candidString: string) => ArrayBufferLike;
canisterBalance: () => ArrayBufferLike;
canisterBalance128: () => ArrayBufferLike;
canisterVersion: () => ArrayBufferLike;
clearTimer: (timerIdBytes: ArrayBufferLike) => void;
dataCertificate: () => ArrayBufferLike | undefined;
id: () => string;
instructionCounter: () => ArrayBufferLike;
isController: (principalBytes: ArrayBufferLike) => boolean;
msgCyclesAccept: (maxAmountCandidBytes: ArrayBufferLike) => ArrayBufferLike;
msgCyclesAccept128: (
maxAmountCandidBytes: ArrayBufferLike
) => ArrayBufferLike;
msgCyclesAvailable: () => ArrayBufferLike;
msgCyclesAvailable128: () => ArrayBufferLike;
msgCyclesRefunded: () => ArrayBufferLike;
msgCyclesRefunded128: () => ArrayBufferLike;
notifyRaw: (
canisterIdBytes: ArrayBufferLike,
method: string,
argsRawBuffer: ArrayBufferLike,
paymentCandidBytes: ArrayBufferLike
) => void;
performanceCounter: (
counterTypeCandidBytes: ArrayBufferLike
) => ArrayBufferLike;
rejectCode: () => number;
replyRaw: (bytes: ArrayBufferLike) => void;
setCertifiedData: (dataBytes: ArrayBufferLike) => void;
setTimer: (
delayBytes: ArrayBufferLike,
timerCallbackId: string
) => ArrayBufferLike;
setTimerInterval: (
intervalBytes: ArrayBufferLike,
timerCallbackId: string
) => ArrayBufferLike;
stableBytes: () => ArrayBufferLike;
stableGrow: (newPagesCandidBytes: ArrayBufferLike) => ArrayBufferLike;
stableRead: (paramsCandidBytes: ArrayBufferLike) => ArrayBufferLike;
stableSize: () => ArrayBufferLike;
stableWrite: (paramsCandidBytes: ArrayBufferLike) => void;
stable64Grow: (newPagesCandidBytes: ArrayBufferLike) => ArrayBufferLike;
stable64Read: (paramsCandidBytes: ArrayBufferLike) => ArrayBufferLike;
stable64Size: () => ArrayBufferLike;
stable64Write: (paramsCandidBytes: ArrayBufferLike) => void;
time: () => ArrayBufferLike;
// These calls aren't intercepted by our IC object, they go right to the
// rust version and come out. Since they don't need to be intercepted I am
// assuming that their types are the same as the types declared by out
// interceptor.
acceptMessage: () => void;
methodName: () => string;
print: (...args: any) => void;
reject: (message: string) => void;
rejectMessage: () => string;
trap: (message: string) => never;
// These calls are intercepted by our IC object and redirected to their
// corresponding raw version. The rust version is never called, we don't
// have enough info about types to do so
call: () => never;
call128: () => never;
notify: () => never;
reply: () => never;
};
2 changes: 1 addition & 1 deletion src/lib/ic/types.ts → src/lib/ic/types/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { nat64, AzleNat64 } from '../candid/types/primitive/nats/nat64';
import { nat64, AzleNat64 } from '../../candid/types/primitive/nats/nat64';

export type ArgsType<T> = T extends (...args: infer U) => any ? U : any;
export type ReturnTypeOf<T> = T extends (...args: any[]) => infer R ? R : any;
Expand Down

0 comments on commit bf01489

Please sign in to comment.