From f1208eb5ff17f95a59a4b651e91a889640a5ee39 Mon Sep 17 00:00:00 2001 From: Benjamin DeMann Date: Tue, 3 Oct 2023 11:15:46 -0600 Subject: [PATCH] use azle type aliases everywhere --- src/lib/ic/accept_message.ts | 4 +++- src/lib/ic/arg_data_raw.ts | 2 +- src/lib/ic/arg_data_raw_size.ts | 4 +++- src/lib/ic/call.ts | 3 ++- src/lib/ic/call128.ts | 3 ++- src/lib/ic/call_raw.ts | 3 ++- src/lib/ic/call_raw_128.ts | 3 ++- src/lib/ic/caller.ts | 2 +- src/lib/ic/canister_balance.ts | 7 +++++-- src/lib/ic/canister_balance_128.ts | 7 +++++-- src/lib/ic/canister_version.ts | 7 +++++-- src/lib/ic/clear_timer.ts | 6 +++--- src/lib/ic/instruction_counter.ts | 7 +++++-- src/lib/ic/method_name.ts | 4 +++- src/lib/ic/msg_cycles_accept.ts | 6 ++++-- src/lib/ic/msg_cycles_accept_128.ts | 8 +++++--- src/lib/ic/msg_cycles_available.ts | 7 +++++-- src/lib/ic/msg_cycles_available_128.ts | 7 +++++-- src/lib/ic/msg_cycles_refunded.ts | 7 +++++-- src/lib/ic/msg_cycles_refunded_128.ts | 7 +++++-- src/lib/ic/notify.ts | 3 ++- src/lib/ic/performance_counter.ts | 8 ++++++-- src/lib/ic/print.ts | 4 +++- src/lib/ic/reject.ts | 5 ++++- src/lib/ic/reject_message.ts | 4 +++- src/lib/ic/reply.ts | 3 ++- src/lib/ic/reply_raw.ts | 5 ++++- src/lib/ic/set_certified_data.ts | 4 +++- src/lib/ic/set_timer.ts | 3 +-- src/lib/ic/set_timer_interval.ts | 3 +-- src/lib/ic/stable_64_grow.ts | 13 ++++++++----- src/lib/ic/stable_64_read.ts | 3 ++- src/lib/ic/stable_64_size.ts | 7 +++++-- src/lib/ic/stable_64_write.ts | 4 +++- src/lib/ic/stable_bytes.ts | 4 +++- src/lib/ic/stable_grow.ts | 5 +++-- src/lib/ic/stable_read.ts | 5 +++-- src/lib/ic/stable_size.ts | 8 ++++++-- src/lib/ic/stable_write.ts | 4 +++- src/lib/ic/time.ts | 5 +++-- src/lib/ic/types.ts | 3 --- 41 files changed, 139 insertions(+), 68 deletions(-) diff --git a/src/lib/ic/accept_message.ts b/src/lib/ic/accept_message.ts index aac117f805..53805b9b12 100644 --- a/src/lib/ic/accept_message.ts +++ b/src/lib/ic/accept_message.ts @@ -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' ); diff --git a/src/lib/ic/arg_data_raw.ts b/src/lib/ic/arg_data_raw.ts index a7a4c1f779..a26da132ae 100644 --- a/src/lib/ic/arg_data_raw.ts +++ b/src/lib/ic/arg_data_raw.ts @@ -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()); } diff --git a/src/lib/ic/arg_data_raw_size.ts b/src/lib/ic/arg_data_raw_size.ts index 2d13c89092..fb3371d323 100644 --- a/src/lib/ic/arg_data_raw_size.ts +++ b/src/lib/ic/arg_data_raw_size.ts @@ -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' ); diff --git a/src/lib/ic/call.ts b/src/lib/ic/call.ts index 68d5c39183..e036667026 100644 --- a/src/lib/ic/call.ts +++ b/src/lib/ic/call.ts @@ -1,3 +1,4 @@ +import { nat64 } from '../candid/types/primitive/nats/nat64'; import { callRaw } from './call_raw'; import { ArgsType, ReturnTypeOf } from './types'; @@ -15,7 +16,7 @@ export function call any>( method: T, config?: { args?: ArgsType; - cycles?: bigint; + cycles?: nat64; } ): ReturnTypeOf { // TODO probably get rid of .crossCanisterCallback diff --git a/src/lib/ic/call128.ts b/src/lib/ic/call128.ts index 51b0e01ee3..c2d0a1c419 100644 --- a/src/lib/ic/call128.ts +++ b/src/lib/ic/call128.ts @@ -1,3 +1,4 @@ +import { nat } from '../candid/types/primitive/nats/nat'; import { callRaw128 } from './call_raw_128'; import { ArgsType, ReturnTypeOf } from './types'; @@ -15,7 +16,7 @@ export function call128 any>( method: T, config?: { args?: ArgsType; - cycles?: bigint; + cycles?: nat; } ): ReturnTypeOf { return method.crossCanisterCallback( diff --git a/src/lib/ic/call_raw.ts b/src/lib/ic/call_raw.ts index 87fe4691b8..81bf6152a1 100644 --- a/src/lib/ic/call_raw.ts +++ b/src/lib/ic/call_raw.ts @@ -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]( @@ -16,7 +17,7 @@ import { v4 } from 'uuid'; */ export function callRaw( canisterId: Principal, - method: string, + method: text, argsRaw: blob, payment: nat64 ): Promise { diff --git a/src/lib/ic/call_raw_128.ts b/src/lib/ic/call_raw_128.ts index 9b89d7041e..19d3257c8e 100644 --- a/src/lib/ic/call_raw_128.ts +++ b/src/lib/ic/call_raw_128.ts @@ -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]( @@ -16,7 +17,7 @@ import { v4 } from 'uuid'; */ export function callRaw128( canisterId: Principal, - method: string, + method: text, argsRaw: blob, payment: nat ): Promise { diff --git a/src/lib/ic/caller.ts b/src/lib/ic/caller.ts index c2cc04a881..97543dea94 100644 --- a/src/lib/ic/caller.ts +++ b/src/lib/ic/caller.ts @@ -1,4 +1,4 @@ -import { Principal } from '@dfinity/principal'; +import { Principal } from '../candid/types/reference/principal'; /** * Returns the caller of the current call diff --git a/src/lib/ic/canister_balance.ts b/src/lib/ic/canister_balance.ts index d940ff4b20..ca83658e91 100644 --- a/src/lib/ic/canister_balance.ts +++ b/src/lib/ic/canister_balance.ts @@ -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 + ); } diff --git a/src/lib/ic/canister_balance_128.ts b/src/lib/ic/canister_balance_128.ts index 000ce50788..5b69388081 100644 --- a/src/lib/ic/canister_balance_128.ts +++ b/src/lib/ic/canister_balance_128.ts @@ -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 + ); } diff --git a/src/lib/ic/canister_version.ts b/src/lib/ic/canister_version.ts index 4125c78a0d..8a09cc45db 100644 --- a/src/lib/ic/canister_version.ts +++ b/src/lib/ic/canister_version.ts @@ -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 + ); } diff --git a/src/lib/ic/clear_timer.ts b/src/lib/ic/clear_timer.ts index a63ed72c04..f6b937200e 100644 --- a/src/lib/ic/clear_timer.ts +++ b/src/lib/ic/clear_timer.ts @@ -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; }; diff --git a/src/lib/ic/instruction_counter.ts b/src/lib/ic/instruction_counter.ts index 89892dbd52..c46730a383 100644 --- a/src/lib/ic/instruction_counter.ts +++ b/src/lib/ic/instruction_counter.ts @@ -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 @@ -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 + ); } diff --git a/src/lib/ic/method_name.ts b/src/lib/ic/method_name.ts index ee52630e6e..fa27c4d514 100644 --- a/src/lib/ic/method_name.ts +++ b/src/lib/ic/method_name.ts @@ -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' ); diff --git a/src/lib/ic/msg_cycles_accept.ts b/src/lib/ic/msg_cycles_accept.ts index 8d938b8b66..ac6dfbdb0c 100644 --- a/src/lib/ic/msg_cycles_accept.ts +++ b/src/lib/ic/msg_cycles_accept.ts @@ -6,7 +6,7 @@ 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; @@ -14,5 +14,7 @@ export function msgCyclesAccept(maxAmount: nat64): bigint { const msgCyclesAcceptCandidBytes = globalThis._azleIc.msgCyclesAccept(maxAmountCandidBytes); - return IDL.decode([IDL.Nat64], msgCyclesAcceptCandidBytes)[0]; + return BigInt( + IDL.decode([IDL.Nat64], msgCyclesAcceptCandidBytes)[0] as number + ); } diff --git a/src/lib/ic/msg_cycles_accept_128.ts b/src/lib/ic/msg_cycles_accept_128.ts index 520c5b9a96..172764ef0d 100644 --- a/src/lib/ic/msg_cycles_accept_128.ts +++ b/src/lib/ic/msg_cycles_accept_128.ts @@ -1,12 +1,12 @@ 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; @@ -14,5 +14,7 @@ export function msgCyclesAccept128(maxAmount: nat64): bigint { const msgCyclesAccept128CandidBytes = globalThis._azleIc.msgCyclesAccept128(maxAmountCandidBytes); - return IDL.decode([IDL.Nat], msgCyclesAccept128CandidBytes)[0]; + return BigInt( + IDL.decode([IDL.Nat], msgCyclesAccept128CandidBytes)[0] as number + ); } diff --git a/src/lib/ic/msg_cycles_available.ts b/src/lib/ic/msg_cycles_available.ts index 54d3668afa..b1c8dcb986 100644 --- a/src/lib/ic/msg_cycles_available.ts +++ b/src/lib/ic/msg_cycles_available.ts @@ -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 + ); } diff --git a/src/lib/ic/msg_cycles_available_128.ts b/src/lib/ic/msg_cycles_available_128.ts index 7adf6fd8b5..90e878cbc5 100644 --- a/src/lib/ic/msg_cycles_available_128.ts +++ b/src/lib/ic/msg_cycles_available_128.ts @@ -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 + ); } diff --git a/src/lib/ic/msg_cycles_refunded.ts b/src/lib/ic/msg_cycles_refunded.ts index 2340a4fc84..3bdc841680 100644 --- a/src/lib/ic/msg_cycles_refunded.ts +++ b/src/lib/ic/msg_cycles_refunded.ts @@ -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 + ); } diff --git a/src/lib/ic/msg_cycles_refunded_128.ts b/src/lib/ic/msg_cycles_refunded_128.ts index 02b48cc3b0..839d88dd8e 100644 --- a/src/lib/ic/msg_cycles_refunded_128.ts +++ b/src/lib/ic/msg_cycles_refunded_128.ts @@ -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 + ); } diff --git a/src/lib/ic/notify.ts b/src/lib/ic/notify.ts index c396811667..18be210fc2 100644 --- a/src/lib/ic/notify.ts +++ b/src/lib/ic/notify.ts @@ -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'; @@ -35,7 +36,7 @@ export function notify any>( method: T, config?: { args?: ArgsType; - cycles?: bigint; + cycles?: nat; } ): Void { return method.crossCanisterCallback( diff --git a/src/lib/ic/performance_counter.ts b/src/lib/ic/performance_counter.ts index 439356b8a0..61418da01a 100644 --- a/src/lib/ic/performance_counter.ts +++ b/src/lib/ic/performance_counter.ts @@ -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 @@ -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; @@ -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 + ); } diff --git a/src/lib/ic/print.ts b/src/lib/ic/print.ts index 4d6078253a..eedb4e6936 100644 --- a/src/lib/ic/print.ts +++ b/src/lib/ic/print.ts @@ -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' ); diff --git a/src/lib/ic/reject.ts b/src/lib/ic/reject.ts index 31cfb69663..8dbb284eee 100644 --- a/src/lib/ic/reject.ts +++ b/src/lib/ic/reject.ts @@ -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' ); diff --git a/src/lib/ic/reject_message.ts b/src/lib/ic/reject_message.ts index 68bca0ce01..b4586815bb 100644 --- a/src/lib/ic/reject_message.ts +++ b/src/lib/ic/reject_message.ts @@ -1,3 +1,5 @@ +import { text } from '../candid/types/primitive/text'; + /** * Returns the rejection message from the most recently executed * cross-canister call @@ -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' ); diff --git a/src/lib/ic/reply.ts b/src/lib/ic/reply.ts index 41d0a90d36..1026fe98d0 100644 --- a/src/lib/ic/reply.ts +++ b/src/lib/ic/reply.ts @@ -1,5 +1,6 @@ import { IDL } from '@dfinity/candid'; import { CandidType, toIDLType } from '../candid'; +import { Void } from '../candid/types/primitive/void'; import { EncodeVisitor } from '../candid/serde/visitors/encode_visitor'; /** @@ -9,7 +10,7 @@ import { EncodeVisitor } from '../candid/serde/visitors/encode_visitor'; * is the generic type supplied to `Manual`. Otherwise will result in an * uncaught `TypeError`. */ -export function reply(data: any, type: CandidType): void { +export function reply(data: any, type: CandidType): Void { if (type.name === 'AzleVoid') { // return type is void const bytes = new Uint8Array(IDL.encode([], [])).buffer; diff --git a/src/lib/ic/reply_raw.ts b/src/lib/ic/reply_raw.ts index bad3aa8b9d..4d46e17e64 100644 --- a/src/lib/ic/reply_raw.ts +++ b/src/lib/ic/reply_raw.ts @@ -1,3 +1,6 @@ +import { Void } from '../../../examples/robust_imports/src/import_coverage/types'; +import { blob } from '../candid/types/constructed/blob'; + /** * Used to manually reply to an ingress message. Intended to be used in * canister methods with a {@link Manual} return type. @@ -15,6 +18,6 @@ * } * ``` */ -export function replyRaw(replyBuffer: Uint8Array): void { +export function replyRaw(replyBuffer: blob): Void { return globalThis._azleIc.replyRaw(replyBuffer.buffer); } diff --git a/src/lib/ic/set_certified_data.ts b/src/lib/ic/set_certified_data.ts index 4473ab7060..d3fbdd0d14 100644 --- a/src/lib/ic/set_certified_data.ts +++ b/src/lib/ic/set_certified_data.ts @@ -1,4 +1,6 @@ import { IDL } from '@dfinity/candid'; +import { blob } from '../candid/types/constructed/blob'; +import { Void } from '../candid/types/primitive/void'; /** * Sets the certified data of this canister. @@ -22,7 +24,7 @@ import { IDL } from '@dfinity/candid'; * @param data the data to be set * @returns */ -export function setCertifiedData(data: Uint8Array): void { +export function setCertifiedData(data: blob): Void { const dataBytes = new Uint8Array(IDL.encode([IDL.Vec(IDL.Nat8)], [data])) .buffer; diff --git a/src/lib/ic/set_timer.ts b/src/lib/ic/set_timer.ts index 0e57705bbf..dd75ea3789 100644 --- a/src/lib/ic/set_timer.ts +++ b/src/lib/ic/set_timer.ts @@ -1,5 +1,4 @@ import { IDL } from '@dfinity/candid'; -import { nat64 } from '../candid/types/primitive/nats/nat64'; import { Duration, TimerId } from './types'; import { v4 } from 'uuid'; @@ -16,7 +15,7 @@ export function setTimer( delay: Duration, callback: () => void | Promise ): TimerId { - const encode = (value: nat64) => { + const encode = (value: Duration) => { return new Uint8Array(IDL.encode([IDL.Nat64], [value])).buffer; }; diff --git a/src/lib/ic/set_timer_interval.ts b/src/lib/ic/set_timer_interval.ts index 363ab29950..bb9a8856d8 100644 --- a/src/lib/ic/set_timer_interval.ts +++ b/src/lib/ic/set_timer_interval.ts @@ -1,5 +1,4 @@ import { IDL } from '@dfinity/candid'; -import { nat64 } from '../candid/types/primitive/nats/nat64'; import { Duration, TimerId } from './types'; import { v4 } from 'uuid'; @@ -16,7 +15,7 @@ export function setTimerInterval( interval: Duration, callback: () => void | Promise ): TimerId { - const encode = (value: nat64) => { + const encode = (value: Duration) => { return new Uint8Array(IDL.encode([IDL.Nat64], [value])).buffer; }; diff --git a/src/lib/ic/stable_64_grow.ts b/src/lib/ic/stable_64_grow.ts index a613bd742a..e627241a20 100644 --- a/src/lib/ic/stable_64_grow.ts +++ b/src/lib/ic/stable_64_grow.ts @@ -1,4 +1,5 @@ import { IDL } from '@dfinity/candid'; +import { nat64 } from '../candid/types/primitive/nats/nat64'; /** * Attempts to grow the stable memory by `newPages`. @@ -6,13 +7,15 @@ import { IDL } from '@dfinity/candid'; * @param newPages * @returns the previous size that was reserved. */ -export function stable64Grow(newPages: bigint): bigint { +export function stable64Grow(newPages: nat64): nat64 { const newPagesCandidBytes = new Uint8Array( IDL.encode([IDL.Nat64], [newPages]) ).buffer; - return IDL.decode( - [IDL.Nat64], - globalThis._azleIc.stable64Grow(newPagesCandidBytes) - )[0]; + return BigInt( + IDL.decode( + [IDL.Nat64], + globalThis._azleIc.stable64Grow(newPagesCandidBytes) + )[0] as number + ); } diff --git a/src/lib/ic/stable_64_read.ts b/src/lib/ic/stable_64_read.ts index 476a7383b3..5bb045bc94 100644 --- a/src/lib/ic/stable_64_read.ts +++ b/src/lib/ic/stable_64_read.ts @@ -1,4 +1,5 @@ import { IDL } from '@dfinity/candid'; +import { nat64 } from '../candid/types/primitive/nats/nat64'; /** * Reads data from the stable memory location specified by an offset. @@ -7,7 +8,7 @@ import { IDL } from '@dfinity/candid'; * @param length the length of buffer to read * @returns the raw bytes in stable memory */ -export function stable64Read(offset: bigint, length: bigint): Uint8Array { +export function stable64Read(offset: nat64, length: nat64): Uint8Array { const paramsCandidBytes = new Uint8Array( IDL.encode([IDL.Nat64, IDL.Nat64], [offset, length]) ).buffer; diff --git a/src/lib/ic/stable_64_size.ts b/src/lib/ic/stable_64_size.ts index ffe0b7a5f6..4fe33ffb00 100644 --- a/src/lib/ic/stable_64_size.ts +++ b/src/lib/ic/stable_64_size.ts @@ -1,10 +1,13 @@ import { IDL } from '@dfinity/candid'; +import { nat64 } from '../candid/types/primitive/nats/nat64'; /** * Gets current size of the stable memory (in WASM pages). Supports 64-bit * addressed memory. * @returns the current memory size */ -export function stable64Size(): bigint { - return IDL.decode([IDL.Nat64], globalThis._azleIc.stable64Size())[0]; +export function stable64Size(): nat64 { + return BigInt( + IDL.decode([IDL.Nat64], globalThis._azleIc.stable64Size())[0] as number + ); } diff --git a/src/lib/ic/stable_64_write.ts b/src/lib/ic/stable_64_write.ts index 5be27b6b10..9f959c7a70 100644 --- a/src/lib/ic/stable_64_write.ts +++ b/src/lib/ic/stable_64_write.ts @@ -1,4 +1,6 @@ import { IDL } from '@dfinity/candid'; +import { blob } from '../candid/types/constructed/blob'; +import { nat64 } from '../candid/types/primitive/nats/nat64'; /** * Writes data to the stable memory location specified by an offset. @@ -10,7 +12,7 @@ import { IDL } from '@dfinity/candid'; * @param offset the location at which to write * @param buffer the data to write */ -export function stable64Write(offset: bigint, buffer: Uint8Array): void { +export function stable64Write(offset: nat64, buffer: blob): void { const paramsCandidBytes = new Uint8Array( IDL.encode([IDL.Nat64, IDL.Vec(IDL.Nat8)], [offset, buffer]) ).buffer; diff --git a/src/lib/ic/stable_bytes.ts b/src/lib/ic/stable_bytes.ts index 5b96c1f46b..bc3c11f6cf 100644 --- a/src/lib/ic/stable_bytes.ts +++ b/src/lib/ic/stable_bytes.ts @@ -1,3 +1,5 @@ +import { blob } from '../candid/types/constructed/blob'; + /** * Gets a copy of stable memory * @@ -5,6 +7,6 @@ * been written to. * @returns a copy of the stable memory */ -export function stableBytes(): Uint8Array { +export function stableBytes(): blob { return new Uint8Array(globalThis._azleIc.stableBytes()); } diff --git a/src/lib/ic/stable_grow.ts b/src/lib/ic/stable_grow.ts index ef4984237e..6dfd4b3170 100644 --- a/src/lib/ic/stable_grow.ts +++ b/src/lib/ic/stable_grow.ts @@ -1,11 +1,12 @@ import { IDL } from '@dfinity/candid'; +import { nat32 } from '../candid/types/primitive/nats/nat32'; /** * Attempts to grow the stable memory by `newPages`. * @param newPages * @returns the previous size that was reserved. */ -export function stableGrow(newPages: number): number { +export function stableGrow(newPages: nat32): nat32 { const newPagesCandidBytes = new Uint8Array( IDL.encode([IDL.Nat32], [newPages]) ).buffer; @@ -13,5 +14,5 @@ export function stableGrow(newPages: number): number { return IDL.decode( [IDL.Nat32], globalThis._azleIc.stableGrow(newPagesCandidBytes) - )[0]; + )[0] as number; } diff --git a/src/lib/ic/stable_read.ts b/src/lib/ic/stable_read.ts index 8cabc82b12..fc1e7b9cab 100644 --- a/src/lib/ic/stable_read.ts +++ b/src/lib/ic/stable_read.ts @@ -1,4 +1,5 @@ import { IDL } from '@dfinity/candid'; +import { nat32 } from '../candid/types/primitive/nats/nat32'; /** * Reads data from the stable memory location specified by an offset @@ -6,9 +7,9 @@ import { IDL } from '@dfinity/candid'; * @param length the length of buffer to read * @returns the raw bytes in stable memory */ -export function stableRead(offset: number, buffer: number): Uint8Array { +export function stableRead(offset: nat32, length: nat32): Uint8Array { const paramsCandidBytes = new Uint8Array( - IDL.encode([IDL.Nat32, IDL.Nat32], [offset, buffer]) + IDL.encode([IDL.Nat32, IDL.Nat32], [offset, length]) ).buffer; return new Uint8Array(globalThis._azleIc.stableRead(paramsCandidBytes)); diff --git a/src/lib/ic/stable_size.ts b/src/lib/ic/stable_size.ts index b31d48902f..7f83534987 100644 --- a/src/lib/ic/stable_size.ts +++ b/src/lib/ic/stable_size.ts @@ -1,9 +1,13 @@ import { IDL } from '@dfinity/candid'; +import { nat32 } from '../candid/types/primitive/nats/nat32'; /** * Gets current size of the stable memory (in WASM pages) * @returns the current memory size */ -export function stableSize(): number { - return IDL.decode([IDL.Nat32], globalThis._azleIc.stableSize())[0]; +export function stableSize(): nat32 { + return IDL.decode( + [IDL.Nat32], + globalThis._azleIc.stableSize() + )[0] as number; } diff --git a/src/lib/ic/stable_write.ts b/src/lib/ic/stable_write.ts index 67bcbb54f9..cc4fc447e9 100644 --- a/src/lib/ic/stable_write.ts +++ b/src/lib/ic/stable_write.ts @@ -1,4 +1,6 @@ import { IDL } from '@dfinity/candid'; +import { nat32 } from '../candid/types/primitive/nats/nat32'; +import { blob } from '../candid/types/constructed/blob'; /** * Writes data to the stable memory location specified by an offset @@ -9,7 +11,7 @@ import { IDL } from '@dfinity/candid'; * @param offset the location at which to write * @param buffer the data to write */ -export function stableWrite(offset: number, buffer: Uint8Array): void { +export function stableWrite(offset: nat32, buffer: blob): void { const paramsCandidBytes = new Uint8Array( IDL.encode([IDL.Nat32, IDL.Vec(IDL.Nat8)], [offset, buffer]) ).buffer; diff --git a/src/lib/ic/time.ts b/src/lib/ic/time.ts index 6ccef79299..695788ed5b 100644 --- a/src/lib/ic/time.ts +++ b/src/lib/ic/time.ts @@ -1,10 +1,11 @@ import { IDL } from '@dfinity/candid'; +import { nat64 } from '../candid/types/primitive/nats/nat64'; /** * Gets current timestamp, in nanoseconds since the epoch (1970-01-01) * @returns the current timestamp */ -export function time(): bigint { +export function time(): nat64 { const timeCandidBytes = globalThis._azleIc.time(); - return IDL.decode([IDL.Nat64], timeCandidBytes)[0]; + return BigInt(IDL.decode([IDL.Nat64], timeCandidBytes)[0] as number); } diff --git a/src/lib/ic/types.ts b/src/lib/ic/types.ts index ab6997d5cd..670868d4cd 100644 --- a/src/lib/ic/types.ts +++ b/src/lib/ic/types.ts @@ -2,9 +2,6 @@ import { nat64, AzleNat64 } from '../candid/types/primitive/nats/nat64'; export type ArgsType = T extends (...args: infer U) => any ? U : any; export type ReturnTypeOf = T extends (...args: any[]) => infer R ? R : any; -export type ReturnTypeOfPromise = T extends (...args: any[]) => infer R - ? Promise - : never; /** * Represents a duration of time in seconds.