-
Notifications
You must be signed in to change notification settings - Fork 39
Expand file tree
/
Copy pathEnsController.ts
More file actions
39 lines (31 loc) · 1.13 KB
/
EnsController.ts
File metadata and controls
39 lines (31 loc) · 1.13 KB
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
import { subscribeKey as subKey } from 'valtio/utils';
import { proxy, subscribe as sub } from 'valtio';
import { BlockchainApiController } from './BlockchainApiController';
import type { BlockchainApiEnsError } from '../utils/TypeUtil';
// -- Types --------------------------------------------- //
export interface EnsControllerState {
loading: boolean;
}
type StateKey = keyof EnsControllerState;
// -- State --------------------------------------------- //
const state = proxy<EnsControllerState>({
loading: false
});
// -- Controller ---------------------------------------- //
export const EnsController = {
state,
subscribe(callback: (newState: EnsControllerState) => void) {
return sub(state, () => callback(state));
},
subscribeKey<K extends StateKey>(key: K, callback: (value: EnsControllerState[K]) => void) {
return subKey(state, key, callback);
},
async resolveName(name: string) {
try {
return await BlockchainApiController.lookupEnsName(name);
} catch (e) {
const error = e as BlockchainApiEnsError;
throw new Error(error?.reasons?.[0]?.description || 'Error resolving name');
}
}
};