Skip to content

Commit

Permalink
use azle type aliases everywhere
Browse files Browse the repository at this point in the history
  • Loading branch information
bdemann committed Oct 3, 2023
1 parent 598ccb1 commit f1208eb
Show file tree
Hide file tree
Showing 41 changed files with 139 additions and 68 deletions.
4 changes: 3 additions & 1 deletion src/lib/ic/accept_message.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Void } from '../candid/types/primitive/void';

/**
* Accepts the ingress message. Calling from outside the
* {@link $inspectMessage} context will cause the canister to trap.
*/
export function acceptMessage(): void {
export function acceptMessage(): Void {
throw new Error(
'This function should not be called directly. It is implemented directly on the ic object'
);
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/arg_data_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@ import { blob } from '../candid/types/constructed/blob';
* Returns the argument data as bytes.
* @returns the argument data
*/
export function argDataRaw(): Uint8Array {
export function argDataRaw(): blob {
return new Uint8Array(globalThis._azleIc.argDataRaw());
}
4 changes: 3 additions & 1 deletion src/lib/ic/arg_data_raw_size.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { nat } from '../candid/types/primitive/nats/nat';

/**
* Gets the length of the raw-argument-data-bytes
* @returns the data size
*/
export function argDataRawSize(): number {
export function argDataRawSize(): nat {
throw new Error(
'This function should not be called directly. It is implemented directly on the ic object'
);
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ic/call.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { callRaw } from './call_raw';
import { ArgsType, ReturnTypeOf } from './types';

Expand All @@ -15,7 +16,7 @@ export function call<T extends (...args: any[]) => any>(
method: T,
config?: {
args?: ArgsType<T>;
cycles?: bigint;
cycles?: nat64;
}
): ReturnTypeOf<T> {
// TODO probably get rid of .crossCanisterCallback
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ic/call128.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { nat } from '../candid/types/primitive/nats/nat';
import { callRaw128 } from './call_raw_128';
import { ArgsType, ReturnTypeOf } from './types';

Expand All @@ -15,7 +16,7 @@ export function call128<T extends (...args: any[]) => any>(
method: T,
config?: {
args?: ArgsType<T>;
cycles?: bigint;
cycles?: nat;
}
): ReturnTypeOf<T> {
return method.crossCanisterCallback(
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ic/call_raw.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Principal } from '@dfinity/principal';
import { blob } from '../candid/types/constructed/blob';
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { v4 } from 'uuid';
import { text } from '../candid/types/primitive/text';

/**
* Performs an asynchronous call to another canister using the [System API](
Expand All @@ -16,7 +17,7 @@ import { v4 } from 'uuid';
*/
export function callRaw(
canisterId: Principal,
method: string,
method: text,
argsRaw: blob,
payment: nat64
): Promise<blob> {
Expand Down
3 changes: 2 additions & 1 deletion src/lib/ic/call_raw_128.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Principal } from '@dfinity/principal';
import { blob } from '../candid/types/constructed/blob';
import { nat } from '../candid/types/primitive/nats/nat';
import { v4 } from 'uuid';
import { text } from '../candid/types/primitive/text';

/**
* Performs an asynchronous call to another canister using the [System API](
Expand All @@ -16,7 +17,7 @@ import { v4 } from 'uuid';
*/
export function callRaw128(
canisterId: Principal,
method: string,
method: text,
argsRaw: blob,
payment: nat
): Promise<blob> {
Expand Down
2 changes: 1 addition & 1 deletion src/lib/ic/caller.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Principal } from '@dfinity/principal';
import { Principal } from '../candid/types/reference/principal';

/**
* Returns the caller of the current call
Expand Down
7 changes: 5 additions & 2 deletions src/lib/ic/canister_balance.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
import { IDL } from '@dfinity/candid';
import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Gets the amount of funds available in the canister
* @returns the number of cycles in the canister
*/
export function canisterBalance(): bigint {
export function canisterBalance(): nat64 {
const canisterBalanceCandidBytes = globalThis._azleIc.canisterBalance();
return IDL.decode([IDL.Nat64], canisterBalanceCandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat64], canisterBalanceCandidBytes)[0] as number
);
}
7 changes: 5 additions & 2 deletions src/lib/ic/canister_balance_128.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IDL } from '@dfinity/candid';
import { nat } from '../candid/types/primitive/nats/nat';

/**
* Gets the amount of funds available in the canister
* @returns the number of cycles in the canister
*/
export function canisterBalance128(): bigint {
export function canisterBalance128(): nat {
const canisterBalance128CandidBytes =
globalThis._azleIc.canisterBalance128();
return IDL.decode([IDL.Nat], canisterBalance128CandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat], canisterBalance128CandidBytes)[0] as number
);
}
7 changes: 5 additions & 2 deletions src/lib/ic/canister_version.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,14 @@
import { IDL } from '@dfinity/candid';
import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Returns the canister version number
*
* @returns the version number
*/
export function canisterVersion(): bigint {
export function canisterVersion(): nat64 {
const canisterVersionCandidBytes = globalThis._azleIc.canisterVersion();
return IDL.decode([IDL.Nat64], canisterVersionCandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat64], canisterVersionCandidBytes)[0] as number
);
}
6 changes: 3 additions & 3 deletions src/lib/ic/clear_timer.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { IDL } from '@dfinity/candid';
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { Void } from '../candid/types/primitive/void';
import { TimerId } from './types';

/**
* Cancels an existing timer. Does nothing if the timer has already been canceled.
* @param id The ID of the timer to be cancelled.
*/
export function clearTimer(timerId: nat64): Void {
const encode = (value: nat64) => {
export function clearTimer(timerId: TimerId): Void {
const encode = (value: TimerId) => {
return new Uint8Array(IDL.encode([IDL.Nat64], [value])).buffer;
};

Expand Down
7 changes: 5 additions & 2 deletions src/lib/ic/instruction_counter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { IDL } from '@dfinity/candid';
import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Returns the number of instructions that the canister executed since the
Expand All @@ -8,8 +9,10 @@ import { IDL } from '@dfinity/candid';
*
* @returns the number of instructions
*/
export function instructionCounter(): bigint {
export function instructionCounter(): nat64 {
const instructionCounterCandidBytes =
globalThis._azleIc.instructionCounter();
return IDL.decode([IDL.Nat64], instructionCounterCandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat64], instructionCounterCandidBytes)[0] as number
);
}
4 changes: 3 additions & 1 deletion src/lib/ic/method_name.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { text } from '../candid/types/primitive/text';

/**
* Returns the name of the current canister methods
* @returns the current canister method
*/
export function methodName(): string {
export function methodName(): text {
throw new Error(
'This function should not be called directly. It is implemented directly on the ic object'
);
Expand Down
6 changes: 4 additions & 2 deletions src/lib/ic/msg_cycles_accept.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,15 @@ import { nat64 } from '../candid/types/primitive/nats/nat64';
* @param maxAmount the max amount of cycles to move
* @returns the actual amount moved
*/
export function msgCyclesAccept(maxAmount: nat64): bigint {
export function msgCyclesAccept(maxAmount: nat64): nat64 {
const maxAmountCandidBytes = new Uint8Array(
IDL.encode([IDL.Nat64], [maxAmount])
).buffer;

const msgCyclesAcceptCandidBytes =
globalThis._azleIc.msgCyclesAccept(maxAmountCandidBytes);

return IDL.decode([IDL.Nat64], msgCyclesAcceptCandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat64], msgCyclesAcceptCandidBytes)[0] as number
);
}
8 changes: 5 additions & 3 deletions src/lib/ic/msg_cycles_accept_128.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,20 @@
import { IDL } from '@dfinity/candid';
import { nat64 } from '../candid/types/primitive/nats/nat64';
import { nat } from '../candid/types/primitive/nats/nat';

/**
* Moves cycles from the call to the canister balance
* @param maxAmount the max amount of cycles to move
* @returns the actual amount moved
*/
export function msgCyclesAccept128(maxAmount: nat64): bigint {
export function msgCyclesAccept128(maxAmount: nat): nat {
const maxAmountCandidBytes = new Uint8Array(
IDL.encode([IDL.Nat], [maxAmount])
).buffer;

const msgCyclesAccept128CandidBytes =
globalThis._azleIc.msgCyclesAccept128(maxAmountCandidBytes);

return IDL.decode([IDL.Nat], msgCyclesAccept128CandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat], msgCyclesAccept128CandidBytes)[0] as number
);
}
7 changes: 5 additions & 2 deletions src/lib/ic/msg_cycles_available.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { IDL } from '@dfinity/candid';
import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Returns the amount of cycles that were transferred by the caller of the
* current call, and is still available in this message
* @returns the amount of cycles
*/
export function msgCyclesAvailable(): bigint {
export function msgCyclesAvailable(): nat64 {
const msgCyclesAvailableCandidBytes =
globalThis._azleIc.msgCyclesAvailable();

return IDL.decode([IDL.Nat64], msgCyclesAvailableCandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat64], msgCyclesAvailableCandidBytes)[0] as number
);
}
7 changes: 5 additions & 2 deletions src/lib/ic/msg_cycles_available_128.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { IDL } from '@dfinity/candid';
import { nat } from '../candid/types/primitive/nats/nat';

/**
* Returns the amount of cycles that were transferred by the caller of the
* current call, and is still available in this message
* @returns the amount of cycles
*/
export function msgCyclesAvailable128(): bigint {
export function msgCyclesAvailable128(): nat {
const msgCyclesAvailable128CandidBytes =
globalThis._azleIc.msgCyclesAvailable128();

return IDL.decode([IDL.Nat], msgCyclesAvailable128CandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat], msgCyclesAvailable128CandidBytes)[0] as number
);
}
7 changes: 5 additions & 2 deletions src/lib/ic/msg_cycles_refunded.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
import { IDL } from '@dfinity/candid';
import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Returns the amount of cycles that came back with the response as a refund.
* The refund has already been added to the canister balance automatically.
* @returns the amount of cycles
*/
export function msgCyclesRefunded() {
export function msgCyclesRefunded(): nat64 {
const msgCyclesRefundedCandidBytes = globalThis._azleIc.msgCyclesRefunded();

return IDL.decode([IDL.Nat64], msgCyclesRefundedCandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat64], msgCyclesRefundedCandidBytes)[0] as number
);
}
7 changes: 5 additions & 2 deletions src/lib/ic/msg_cycles_refunded_128.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { IDL } from '@dfinity/candid';
import { nat } from '../candid/types/primitive/nats/nat';

/**
* Returns the amount of cycles that came back with the response as a refund.
* The refund has already been added to the canister balance automatically.
* @returns the amount of cycles
*/
export function msgCyclesRefunded128(): bigint {
export function msgCyclesRefunded128(): nat {
const msgCyclesRefunded128CandidBytes =
globalThis._azleIc.msgCyclesRefunded128();

return IDL.decode([IDL.Nat], msgCyclesRefunded128CandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat], msgCyclesRefunded128CandidBytes)[0] as number
);
}
3 changes: 2 additions & 1 deletion src/lib/ic/notify.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { nat } from '../candid/types/primitive/nats/nat';
import { Void } from '../candid/types/primitive/void';
import { notifyRaw } from './notify_raw';
import { ArgsType } from './types';
Expand Down Expand Up @@ -35,7 +36,7 @@ export function notify<T extends (...args: any[]) => any>(
method: T,
config?: {
args?: ArgsType<T>;
cycles?: bigint;
cycles?: nat;
}
): Void {
return method.crossCanisterCallback(
Expand Down
8 changes: 6 additions & 2 deletions src/lib/ic/performance_counter.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { IDL } from '@dfinity/candid';
import { nat32 } from '../candid/types/primitive/nats/nat32';
import { nat64 } from '../candid/types/primitive/nats/nat64';

/**
* Gets the value of the specified performance counter
Expand All @@ -9,7 +11,7 @@ import { IDL } from '@dfinity/candid';
* has executed.
* @returns the performance counter metric
*/
export function performanceCounter(counterType: number): bigint {
export function performanceCounter(counterType: nat32): nat64 {
const counterTypeCandidBytes = new Uint8Array(
IDL.encode([IDL.Nat32], [counterType])
).buffer;
Expand All @@ -18,5 +20,7 @@ export function performanceCounter(counterType: number): bigint {
counterTypeCandidBytes
);

return IDL.decode([IDL.Nat64], performanceCounterCandidBytes)[0];
return BigInt(
IDL.decode([IDL.Nat64], performanceCounterCandidBytes)[0] as number
);
}
4 changes: 3 additions & 1 deletion src/lib/ic/print.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Void } from '../candid/types/primitive/void';

/**
* Prints the given message
* @param args the message to print
*/
export function print(...args: any): void {
export function print(...args: any): Void {
throw new Error(
'This function should not be called directly. It is implemented directly on the ic object'
);
Expand Down
5 changes: 4 additions & 1 deletion src/lib/ic/reject.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
import { text } from '../candid/types/primitive/text';
import { Void } from '../candid/types/primitive/void';

/**
* Rejects the current call with the provided message
* @param message the rejection message
*/
export function reject(message: string): void {
export function reject(message: text): Void {
throw new Error(
'This function should not be called directly. It is implemented directly on the ic object'
);
Expand Down
4 changes: 3 additions & 1 deletion src/lib/ic/reject_message.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import { text } from '../candid/types/primitive/text';

/**
* Returns the rejection message from the most recently executed
* cross-canister call
Expand All @@ -7,7 +9,7 @@
*
* @returns the rejection message
*/
export function rejectMessage(): string {
export function rejectMessage(): text {
throw new Error(
'This function should not be called directly. It is implemented directly on the ic object'
);
Expand Down
Loading

0 comments on commit f1208eb

Please sign in to comment.