-
Notifications
You must be signed in to change notification settings - Fork 1
/
base.ts
122 lines (104 loc) · 3.33 KB
/
base.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
import {
Actor,
ActorConfig,
ActorSubclass,
HttpAgent,
HttpAgentOptions,
} from "@dfinity/agent";
import { IDL } from "@dfinity/candid";
import * as FavoritesDid from "../canisters/favorites/favorites.did";
import * as RegistrarDid from "../canisters/registrar/registrar.did";
import * as RegistryDid from "../canisters/registry/registry.did";
import * as ResolverDid from "../canisters/resolver/resolver.did";
import {
IC_LOCAL_HOST,
IC_PUBLIC_HOST,
MAINNET_CANISTER_ID_GROUP,
TICP_CANISTER_ID_GROUP,
} from "./config";
import { InMemoryNameRecordsCacheStore, NameRecordsCacheStore } from "./cache";
export interface IcNamingClientInitOptions {
net: "MAINNET" | "TICP";
mode: "production" | "local";
httpAgentOptions?: HttpAgentOptions;
enableTTL?: boolean;
nameRecordsCacheStore?: NameRecordsCacheStore;
}
export interface InternalStores {
nameRecordsCacheStore?: NameRecordsCacheStore;
}
export class IcNamingClientBase {
private _options: IcNamingClientInitOptions;
private _httpAgent: HttpAgent;
protected enableTTL: boolean;
protected nameRecordsCacheStore?: NameRecordsCacheStore;
protected favorites;
protected registrar;
protected registry;
protected resolver;
constructor(options: IcNamingClientInitOptions) {
this._options = options;
this._httpAgent = this._initHttpAgent();
this.enableTTL = options.enableTTL ?? false;
this.nameRecordsCacheStore = this.enableTTL
? options.nameRecordsCacheStore ?? new InMemoryNameRecordsCacheStore()
: undefined;
this._init_actors_before();
const canisterIdMapping =
options.net === "MAINNET"
? MAINNET_CANISTER_ID_GROUP
: TICP_CANISTER_ID_GROUP;
this.favorites = this._createActor<FavoritesDid._SERVICE>(
toDidModuleType(FavoritesDid).idlFactory,
canisterIdMapping.favorites
);
this.registrar = this._createActor<RegistrarDid._SERVICE>(
toDidModuleType(RegistrarDid).idlFactory,
canisterIdMapping.registrar
);
this.registry = this._createActor<RegistryDid._SERVICE>(
toDidModuleType(RegistryDid).idlFactory,
canisterIdMapping.registry
);
this.resolver = this._createActor<ResolverDid._SERVICE>(
toDidModuleType(ResolverDid).idlFactory,
canisterIdMapping.resolver
);
}
private _initHttpAgent() {
return new HttpAgent({
host: this._options.mode === "local" ? IC_LOCAL_HOST : IC_PUBLIC_HOST,
...this._options.httpAgentOptions,
});
}
private _init_actors_before() {
if (this._options.mode === "local") {
this._httpAgent.fetchRootKey().catch((err) => {
console.warn(
"Unable to fetch root key. Check to ensure that your local replica is running"
);
console.error(err);
// TODO: maybe throw ?
});
}
}
private _createActor<ServiceType>(
factory: IDL.InterfaceFactory,
canisterId: ActorConfig["canisterId"]
) {
return Actor.createActor(factory, {
agent: this._httpAgent,
canisterId,
}) as ActorSubclass<ServiceType>;
}
protected async dispatchNameRecordsCache(
fn: (store: NameRecordsCacheStore) => Promise<void>
) {
if (this._options.nameRecordsCacheStore) {
await fn(this._options.nameRecordsCacheStore);
}
}
}
const toDidModuleType = <T>(module: T) => {
return module as T & { idlFactory: IDL.InterfaceFactory };
};