Skip to content

Commit

Permalink
Add @ens helper
Browse files Browse the repository at this point in the history
  • Loading branch information
sembrestels committed Oct 12, 2024
1 parent b56923a commit 4c6d1db
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 0 deletions.
36 changes: 36 additions & 0 deletions packages/evmcrispr/src/modules/std/helpers/ens.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { createPublicClient, http } from "viem";

import { mainnet } from "viem/chains";

import type { HelperFunction } from "../../../types";
import { ComparisonType, checkArgsLength } from "../../../utils";
import type { Std } from "../Std";

import { HelperFunctionError } from "../../../errors";

const mainnetClient = createPublicClient({
chain: mainnet,
transport: http(),
});

function _ens(name: string) {
return mainnetClient.getEnsAddress({ name });
}

export const ens: HelperFunction<Std> = async (
_,
h,
{ interpretNodes },
): Promise<string> => {
checkArgsLength(h, {
type: ComparisonType.Equal,
minValue: 1,
});

const [name] = await interpretNodes(h.args);
const addr = await _ens(name);
if (!addr) {
throw new HelperFunctionError(h, `ENS name ${name} not found`);
}
return addr;
};
2 changes: 2 additions & 0 deletions packages/evmcrispr/src/modules/std/helpers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { HelperFunctions } from "../../../types";
import type { Std } from "../Std";
import { date } from "./date";
import { ens } from "./ens";
import { get } from "./get";
import { id } from "./id";
import { namehash } from "./namehash";
Expand All @@ -10,6 +11,7 @@ import { token, tokenAmount, tokenBalance } from "./token";

export const helpers: HelperFunctions<Std> = {
date,
ens,
get,
id,
namehash,
Expand Down
58 changes: 58 additions & 0 deletions packages/evmcrispr/test/modules/std/helpers/ens.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
import { expect } from "chai";
import { viem } from "hardhat";

import type { PublicClient } from "viem";

import { NodeType } from "../../../../src/types";
import { ComparisonType } from "../../../../src/utils";

import {
itChecksInvalidArgsLength,
preparingExpression,
} from "../../../test-helpers/cas11";
import { expectThrowAsync } from "../../../test-helpers/expects";
import { HelperFunctionError } from "../../../../src/errors";

describe("Std > helpers > @ens(name)", () => {
let client: PublicClient;
const lazyClient = () => client;

before(async () => {
client = await viem.getPublicClient();
});

it("return the hashed value", async () => {
const [interpret] = await preparingExpression(
`@ens('vitalik.eth')`,
client,
);

expect(await interpret()).to.equals(
"0xd8dA6BF26964aF9D7eEd9e03E53415D37aA96045",
);
});

it("should fail when ENS name not found", async () => {
const [interpret, h] = await preparingExpression(
`@ens('_notfound.eth')`,
client,
);
const error = new HelperFunctionError(
h,
"ENS name _notfound.eth not found",
);

await expectThrowAsync(async () => interpret(), error);
});

itChecksInvalidArgsLength(
NodeType.HelperFunctionExpression,
"@ens",
["exampleValue"],
{
type: ComparisonType.Equal,
minValue: 1,
},
lazyClient,
);
});

0 comments on commit 4c6d1db

Please sign in to comment.