-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
92 lines (83 loc) · 2.26 KB
/
index.js
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
const { createPublicClient, http, namehash } = require("viem");
const { base } = require("viem/chains");
const L2ResolverAbi = require("./abi/L2ResolverAbi");
const {
convertReverseNodeToBytes,
} = require("./utils/convertReverseNodeToBytes");
const {
BasenameTextRecordKeys,
textRecordsKeysEnabled,
} = require("./utils/constant");
// Function to resolve a Basename
const BASENAME_L2_RESOLVER_ADDRESS =
"0xC6d566A56A1aFf6508b41f6c90ff131615583BCD";
const baseClient = createPublicClient({
chain: base,
transport: http("https://mainnet.base.org"),
});
async function getBasenameAvatar(basename) {
const avatar = await baseClient.getEnsAvatar({
name: basename,
universalResolverAddress: BASENAME_L2_RESOLVER_ADDRESS,
});
return avatar;
}
function buildBasenameTextRecordContract(basename, key) {
return {
abi: L2ResolverAbi,
address: BASENAME_L2_RESOLVER_ADDRESS,
args: [namehash(basename), key],
functionName: "text",
};
}
// Get a single TextRecord
async function getBasenameTextRecord(basename, key) {
try {
const contractParameters = buildBasenameTextRecordContract(basename, key);
const textRecord = await baseClient.readContract(contractParameters);
return textRecord;
} catch (error) {
throw new Error(error.message);
}
}
// Get a all TextRecords
async function getBasenameTextRecords(
basename,
textRecordsKeys = textRecordsKeysEnabled
) {
try {
const readContracts = textRecordsKeys.map((key) =>
buildBasenameTextRecordContract(basename, key)
);
const textRecords = await baseClient.multicall({
contracts: readContracts,
});
return textRecords;
} catch (error) {
throw new Error(error.message);
}
}
async function getBasename(address) {
try {
const addressReverseNode = convertReverseNodeToBytes(address, base.id);
const basename = await baseClient.readContract({
abi: L2ResolverAbi,
address: BASENAME_L2_RESOLVER_ADDRESS,
functionName: "name",
args: [addressReverseNode],
});
if (basename) {
return basename;
}
} catch (error) {
throw new Error(error.message);
}
}
module.exports = {
getBasename,
getBasenameAvatar,
getBasenameTextRecord,
getBasenameTextRecords,
BasenameTextRecordKeys,
textRecordsKeysEnabled,
};