Skip to content

Commit 4c6d1db

Browse files
committed
Add @ens helper
1 parent b56923a commit 4c6d1db

File tree

3 files changed

+96
-0
lines changed

3 files changed

+96
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
import { createPublicClient, http } from "viem";
2+
3+
import { mainnet } from "viem/chains";
4+
5+
import type { HelperFunction } from "../../../types";
6+
import { ComparisonType, checkArgsLength } from "../../../utils";
7+
import type { Std } from "../Std";
8+
9+
import { HelperFunctionError } from "../../../errors";
10+
11+
const mainnetClient = createPublicClient({
12+
chain: mainnet,
13+
transport: http(),
14+
});
15+
16+
function _ens(name: string) {
17+
return mainnetClient.getEnsAddress({ name });
18+
}
19+
20+
export const ens: HelperFunction<Std> = async (
21+
_,
22+
h,
23+
{ interpretNodes },
24+
): Promise<string> => {
25+
checkArgsLength(h, {
26+
type: ComparisonType.Equal,
27+
minValue: 1,
28+
});
29+
30+
const [name] = await interpretNodes(h.args);
31+
const addr = await _ens(name);
32+
if (!addr) {
33+
throw new HelperFunctionError(h, `ENS name ${name} not found`);
34+
}
35+
return addr;
36+
};

packages/evmcrispr/src/modules/std/helpers/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { HelperFunctions } from "../../../types";
22
import type { Std } from "../Std";
33
import { date } from "./date";
4+
import { ens } from "./ens";
45
import { get } from "./get";
56
import { id } from "./id";
67
import { namehash } from "./namehash";
@@ -10,6 +11,7 @@ import { token, tokenAmount, tokenBalance } from "./token";
1011

1112
export const helpers: HelperFunctions<Std> = {
1213
date,
14+
ens,
1315
get,
1416
id,
1517
namehash,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
import { expect } from "chai";
2+
import { viem } from "hardhat";
3+
4+
import type { PublicClient } from "viem";
5+
6+
import { NodeType } from "../../../../src/types";
7+
import { ComparisonType } from "../../../../src/utils";
8+
9+
import {
10+
itChecksInvalidArgsLength,
11+
preparingExpression,
12+
} from "../../../test-helpers/cas11";
13+
import { expectThrowAsync } from "../../../test-helpers/expects";
14+
import { HelperFunctionError } from "../../../../src/errors";
15+
16+
describe("Std > helpers > @ens(name)", () => {
17+
let client: PublicClient;
18+
const lazyClient = () => client;
19+
20+
before(async () => {
21+
client = await viem.getPublicClient();
22+
});
23+
24+
it("return the hashed value", async () => {
25+
const [interpret] = await preparingExpression(
26+
`@ens('vitalik.eth')`,
27+
client,
28+
);
29+
30+
expect(await interpret()).to.equals(
31+
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
32+
);
33+
});
34+
35+
it("should fail when ENS name not found", async () => {
36+
const [interpret, h] = await preparingExpression(
37+
`@ens('_notfound.eth')`,
38+
client,
39+
);
40+
const error = new HelperFunctionError(
41+
h,
42+
"ENS name _notfound.eth not found",
43+
);
44+
45+
await expectThrowAsync(async () => interpret(), error);
46+
});
47+
48+
itChecksInvalidArgsLength(
49+
NodeType.HelperFunctionExpression,
50+
"@ens",
51+
["exampleValue"],
52+
{
53+
type: ComparisonType.Equal,
54+
minValue: 1,
55+
},
56+
lazyClient,
57+
);
58+
});

0 commit comments

Comments
 (0)