UNPKG

@dfinity/pic

Version:

An Internet Computer Protocol canister testing library for TypeScript and JavaScript.

86 lines (85 loc) 2.99 kB
import { IDL } from '@icp-sdk/core/candid'; import { Principal } from '@icp-sdk/core/principal'; import { Identity } from '@icp-sdk/core/agent'; import { PocketIcClient } from './pocket-ic-client'; /** * Typesafe method of a canister. * * @category Types */ export interface ActorMethod<Args extends any[] = any[], Ret = any> { (...args: Args): Promise<Ret>; } /** * Candid interface of a canister. * * @category Types */ export type ActorInterface<T = object> = { [K in keyof T]: ActorMethod; }; /** * A type-safe class that implements the Candid interface of a canister. * This is acquired by calling {@link PocketIc.setupCanister | setupCanister} * or {@link PocketIc.createActor | createActor}. * * @category API * @typeParam T The type of the {@link Actor}. Must implement {@link ActorInterface}. * @interface */ export type Actor<T extends ActorInterface<T> = ActorInterface> = T & { /** * @ignore */ new (): Actor<T>; /** * Set a Principal to be used as sender for all calls to the canister. * * @param principal The Principal to set. * * @see [Principal](https://js.icp.build/core/latest/libs/principal/api/classes/principal/) * * @example * ```ts * import { PocketIc } from '@dfinity/pic'; * import { Principal } from '@icp-sdk/core/principal'; * import { _SERVICE, idlFactory } from '../declarations'; * * const wasmPath = resolve('..', '..', 'canister.wasm'); * * const pic = await PocketIc.create(); * const fixture = await pic.setupCanister<_SERVICE>(idlFactory, wasmPath); * const { actor } = fixture; * * actor.setPrincipal(Principal.anonymous()); * ``` */ setPrincipal(principal: Principal): void; /** * Set a Principal to be used as sender for all calls to the canister. * This is a convenience method over {@link setPrincipal} that accepts an * Identity and internally extracts the Principal. * * @param identity The identity to set. * * @see [Identity](https://js.icp.build/core/latest/libs/agent/api/interfaces/identity/) * @see [Principal](https://js.icp.build/core/latest/libs/principal/api/classes/principal/) * * @example * ```ts * import { PocketIc } from '@dfinity/pic'; * import { AnonymousIdentity } from '@icp-sdk/core/agent'; * import { _SERVICE, idlFactory } from '../declarations'; * * const wasmPath = resolve('..', '..', 'canister.wasm'); * * const pic = await PocketIc.create(); * const fixture = await pic.setupCanister<_SERVICE>(idlFactory, wasmPath); * const { actor } = fixture; * * actor.setIdentity(new AnonymousIdentity()); * ``` */ setIdentity(identity: Identity): void; }; export declare function createActorClass<T extends ActorInterface<T> = ActorInterface>(interfaceFactory: IDL.InterfaceFactory, canisterId: Principal, pocketIcClient: PocketIcClient): Actor<T>;