-
Notifications
You must be signed in to change notification settings - Fork 0
/
actor.ts
40 lines (36 loc) · 1.14 KB
/
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
import { IDL } from "@dfinity/candid";
import { Principal } from "@dfinity/principal";
import { _WS_CANISTER_SERVICE, wsMessageIdl, wsOpenIdl } from "./idl";
import type { CanisterWsMessageArguments, CanisterWsOpenArguments } from "./idl";
import { WsAgent } from "./agent";
const _callCanisterMethod = async (
canisterId: Principal,
agent: WsAgent,
methodName: string,
idlFunc: IDL.FuncClass,
args: unknown[],
): Promise<void> => {
const cid = Principal.from(canisterId);
const arg = IDL.encode(idlFunc.argTypes, args);
return agent.call(cid, {
methodName,
arg,
effectiveCanisterId: cid,
});
};
/**
* Calls the ws_open method on the canister.
*/
export const callCanisterWsOpen = async (
canisterId: Principal,
agent: WsAgent,
args: CanisterWsOpenArguments,
): Promise<void> => _callCanisterMethod(canisterId, agent, "ws_open", wsOpenIdl, [args]);
/**
* Calls the ws_message method on the canister.
*/
export const callCanisterWsMessage = async (
canisterId: Principal,
agent: WsAgent,
args: CanisterWsMessageArguments,
): Promise<void> => _callCanisterMethod(canisterId, agent, "ws_message", wsMessageIdl, [args, []]);