src/commands/edit.ts 6.9 K raw
1
import {
2
	setTextRecord,
3
	setAddressRecord,
4
	setResolver as setResolverRecord,
5
	setPrimaryName as setPrimaryNameRecord,
6
	setAbiRecord,
7
} from "@ensdomains/ensjs/wallet";
8
import { normalize } from "viem/ens";
9
import { spinner, walletClient } from "../utils";
10
import { encodeAbi } from "@ensdomains/ensjs/utils";
11
import { readFile } from "node:fs/promises";
12
13
export async function setTxt(options: {
14
	name: string;
15
	record: string;
16
	value: string;
17
	resolverAddress?: string;
18
}) {
19
	try {
20
		spinner.start();
21
		const wallet = await walletClient();
22
23
		if (!wallet) {
24
			spinner.stop();
25
			console.error(
26
				"Error: Wallet not configured. Please set ATLAS_PRIVATE_KEY environment variable.",
27
			);
28
			return;
29
		}
30
31
		// Get resolver if not provided
32
		let resolverAddress = options.resolverAddress;
33
		if (!resolverAddress) {
34
			const { ensClient } = await import("../utils");
35
			const resolver = await ensClient.getEnsResolver({
36
				name: normalize(options.name),
37
			});
38
			resolverAddress = resolver || undefined;
39
		}
40
41
		if (!resolverAddress) {
42
			spinner.stop();
43
			console.error("Error: No resolver found for this name");
44
			return;
45
		}
46
47
		const hash = await setTextRecord(wallet, {
48
			name: options.name,
49
			key: options.record,
50
			value: options.value,
51
			resolverAddress: resolverAddress as `0x${string}`,
52
		});
53
54
		spinner.stop();
55
		if (options.value === "") {
56
			console.log(`✓ TXT record cleared successfully`);
57
		} else {
58
			console.log(`✓ TXT record set successfully`);
59
		}
60
		console.log(`Transaction hash: ${hash}`);
61
	} catch (error) {
62
		const e = error as { shortMessage?: string; message: string };
63
		spinner.stop();
64
		console.error("Error setting TXT record:", e.shortMessage || e.message);
65
		console.error(
66
			"If you are receiving HTTP errors consider setting ETH_RPC_URL as an environemnt variable",
67
		);
68
	}
69
}
70
71
export async function setAddress(options: {
72
	name: string;
73
	coin: string;
74
	value: string;
75
	resolverAddress?: string;
76
}) {
77
	try {
78
		spinner.start();
79
		const wallet = await walletClient();
80
81
		if (!wallet) {
82
			spinner.stop();
83
			console.error(
84
				"Error: Wallet not configured. Please set ATLAS_PRIVATE_KEY environment variable.",
85
			);
86
			return;
87
		}
88
89
		// Get resolver if not provided
90
		let resolverAddress = options.resolverAddress;
91
		if (!resolverAddress) {
92
			const { ensClient } = await import("../utils");
93
			const resolver = await ensClient.getEnsResolver({
94
				name: normalize(options.name),
95
			});
96
			resolverAddress = resolver || undefined;
97
		}
98
99
		if (!resolverAddress) {
100
			spinner.stop();
101
			console.error("Error: No resolver found for this name");
102
			return;
103
		}
104
105
		const hash = await setAddressRecord(wallet, {
106
			name: options.name,
107
			coin: options.coin,
108
			value: options.value === "null" ? null : options.value,
109
			resolverAddress: resolverAddress as `0x${string}`,
110
		});
111
112
		spinner.stop();
113
		if (options.value === "null") {
114
			console.log(`✓ Address record cleared successfully`);
115
		} else {
116
			console.log(`✓ Address record set successfully`);
117
		}
118
		console.log(`Transaction hash: ${hash}`);
119
	} catch (error) {
120
		const e = error as { shortMessage?: string; message: string };
121
		spinner.stop();
122
		console.error("Error setting address record:", e.shortMessage || e.message);
123
		console.error(
124
			"If you are receiving HTTP errors consider setting ETH_RPC_URL as an environemnt variable",
125
		);
126
	}
127
}
128
129
export async function setResolver(options: {
130
	name: string;
131
	resolverAddress: string;
132
	contract?: "registry" | "nameWrapper";
133
}) {
134
	try {
135
		spinner.start();
136
		const wallet = await walletClient();
137
138
		if (!wallet) {
139
			spinner.stop();
140
			console.error(
141
				"Error: Wallet not configured. Please set ATLAS_PRIVATE_KEY environment variable.",
142
			);
143
			return;
144
		}
145
146
		const hash = await setResolverRecord(wallet, {
147
			name: options.name,
148
			contract: options.contract || "registry",
149
			resolverAddress: options.resolverAddress as `0x${string}`,
150
		});
151
152
		spinner.stop();
153
		console.log(`✓ Resolver set successfully`);
154
		console.log(`Transaction hash: ${hash}`);
155
	} catch (error) {
156
		const e = error as { shortMessage?: string; message: string };
157
		spinner.stop();
158
		console.error("Error setting resolver:", e.shortMessage || e.message);
159
		console.error(
160
			"If you are receiving HTTP errors consider setting ETH_RPC_URL as an environemnt variable",
161
		);
162
	}
163
}
164
165
export async function setPrimaryName(options: { name: string }) {
166
	try {
167
		spinner.start();
168
		const wallet = await walletClient();
169
170
		if (!wallet) {
171
			spinner.stop();
172
			console.error(
173
				"Error: Wallet not configured. Please set ATLAS_PRIVATE_KEY environment variable.",
174
			);
175
			return;
176
		}
177
178
		const hash = await setPrimaryNameRecord(wallet, {
179
			name: options.name,
180
		});
181
182
		spinner.stop();
183
		console.log(`✓ Primary name set successfully`);
184
		console.log(`Transaction hash: ${hash}`);
185
	} catch (error) {
186
		const e = error as { shortMessage?: string; message: string };
187
		spinner.stop();
188
		console.error("Error setting primary name:", e.shortMessage || e.message);
189
		console.error(
190
			"If you are receiving HTTP errors consider setting ETH_RPC_URL as an environemnt variable",
191
		);
192
	}
193
}
194
195
export async function setAbi(options: {
196
	name: string;
197
	abiPath: string;
198
	encodeAs?: "json" | "zlib" | "cbor" | "uri";
199
	resolverAddress?: string;
200
}) {
201
	try {
202
		spinner.start();
203
		const wallet = await walletClient();
204
205
		if (!wallet) {
206
			spinner.stop();
207
			console.error(
208
				"Error: Wallet not configured. Please set ATLAS_PRIVATE_KEY environment variable.",
209
			);
210
			return;
211
		}
212
213
		let encodedAbi: `0x${string}`;
214
215
		// Handle null case to clear ABI
216
		if (options.abiPath === "null") {
217
			// Encode empty ABI to clear the record
218
			encodedAbi = await encodeAbi({
219
				encodeAs: options.encodeAs || "json",
220
				data: null,
221
			});
222
		} else {
223
			// Read ABI file
224
			const abiContent = await readFile(options.abiPath, "utf-8");
225
			const abi = JSON.parse(abiContent);
226
227
			// Encode ABI
228
			encodedAbi = await encodeAbi({
229
				encodeAs: options.encodeAs || "json",
230
				data: abi,
231
			});
232
		}
233
234
		// Get resolver if not provided
235
		let resolverAddress = options.resolverAddress;
236
		if (!resolverAddress) {
237
			const { ensClient } = await import("../utils");
238
			const resolver = await ensClient.getEnsResolver({
239
				name: normalize(options.name),
240
			});
241
			resolverAddress = resolver || undefined;
242
		}
243
244
		if (!resolverAddress) {
245
			spinner.stop();
246
			console.error("Error: No resolver found for this name");
247
			return;
248
		}
249
250
		const hash = await setAbiRecord(wallet, {
251
			name: options.name,
252
			encodedAbi,
253
			resolverAddress: resolverAddress as `0x${string}`,
254
		});
255
256
		spinner.stop();
257
		if (options.abiPath === "null") {
258
			console.log(`✓ ABI record cleared successfully`);
259
		} else {
260
			console.log(`✓ ABI record set successfully`);
261
		}
262
		console.log(`Transaction hash: ${hash}`);
263
	} catch (error) {
264
		const e = error as { shortMessage?: string; message: string };
265
		spinner.stop();
266
		console.error("Error setting ABI record:", e.shortMessage || e.message);
267
		console.error(
268
			"If you are receiving HTTP errors consider setting ETH_RPC_URL as an environemnt variable",
269
		);
270
	}
271
}