-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate-actor.ts
44 lines (39 loc) · 1.31 KB
/
create-actor.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import { Actor, ActorConfig, ActorSubclass, Agent, HttpAgent, HttpAgentOptions } from "@dfinity/agent";
import { InterfaceFactory } from '@dfinity/candid/lib/cjs/idl';
export declare interface CreateActorOptions {
/**
* @see {@link Agent}
*/
agent?: Agent;
/**
* @see {@link HttpAgentOptions}
*/
agentOptions?: HttpAgentOptions;
/**
* @see {@link ActorConfig}
*/
actorOptions?: ActorConfig;
}
export const createActor = <SERVICE>(canisterId: string, idlFactory: InterfaceFactory, options: CreateActorOptions = {}): ActorSubclass<SERVICE> => {
const agent = options.agent || new HttpAgent({ ...options.agentOptions });
if (options.agent && options.agentOptions) {
console.warn(
"Detected both agent and agentOptions passed to createActor. Ignoring agentOptions and proceeding with the provided agent."
);
}
// Fetch root key for certificate validation during development
if (process.env.DFX_NETWORK !== "ic") {
agent.fetchRootKey().catch((err) => {
console.warn(
"Unable to fetch root key. Check to ensure that your local replica is running"
);
console.error(err);
});
}
// Creates an actor with using the candid interface and the HttpAgent
return Actor.createActor(idlFactory, {
agent,
canisterId,
...options.actorOptions,
});
};