-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
b56923a
commit 4c6d1db
Showing
3 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
); | ||
}); |