Skip to content

Commit 9685a85

Browse files
committed
[SDK] Expose stuff for UD extensions, improve test (#4962)
## Problem solved Short description of the bug fixed or feature added <!-- start pr-codex --> --- ## PR-Codex overview This PR focuses on enhancing the `thirdweb` SDK by exposing new functionalities related to `UnstoppableDomains`, specifically the `namehash`, `reverseNameOf`, and `exists` methods, along with related error handling and utility functions. ### Detailed summary - Added `namehash` and `reverseNameOf` functions to `UnstoppableDomains`. - Introduced `exists` function to check the existence of a tokenId. - Updated `resolveAddress` to utilize `exists` for validation. - Added error handling for non-existent domain names in tests. - Exported new types and functions from `unstoppable-domains`. > ✨ Ask PR-Codex anything about this PR by commenting with `/codex {your question}` <!-- end pr-codex -->
1 parent cd2c0f3 commit 9685a85

File tree

6 files changed

+157
-1
lines changed

6 files changed

+157
-1
lines changed

.changeset/big-fans-reflect.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"thirdweb": patch
3+
---
4+
5+
Expose UnstoppableDomains extensions: namehash and reverseNameOf
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
[
22
"function reverseNameOf(address addr) view returns (string reverseUri)",
33
"function getMany(string[] keys, uint256 tokenId) view returns (string[] values)",
4-
"function namehash(string[] labels) pure returns (uint256 hash)"
4+
"function namehash(string[] labels) pure returns (uint256 hash)",
5+
"function exists(uint256 tokenId) view returns (bool)"
56
]

packages/thirdweb/src/exports/extensions/unstoppable-domains.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,12 @@ export {
77
type ResolveAddressOptions,
88
resolveAddress,
99
} from "../../extensions/unstoppable-domains/read/resolveAddress.js";
10+
export {
11+
// Need this to resolve the tokenId, so that Social SDK can fetch the owner's Avatar from it
12+
namehash,
13+
type NamehashParams,
14+
} from "../../extensions/unstoppable-domains/__generated__/UnstoppableDomains/read/namehash.js";
15+
export {
16+
reverseNameOf,
17+
type ReverseNameOfParams,
18+
} from "../../extensions/unstoppable-domains/__generated__/UnstoppableDomains/read/reverseNameOf.js";

packages/thirdweb/src/extensions/unstoppable-domains/__generated__/UnstoppableDomains/read/exists.ts

Lines changed: 121 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

packages/thirdweb/src/extensions/unstoppable-domains/read/resolveAddress.test.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,15 @@ describe("Unstoppable Domain: resolve address", () => {
1515
).toLowerCase(),
1616
).toBe("0x12345674b599ce99958242b3D3741e7b01841DF3".toLowerCase());
1717
});
18+
19+
it("should throw an error with a non-existent domain name", async () => {
20+
await expect(() =>
21+
resolveAddress({
22+
name: "thirdwebsdk.thissuredoesnotexist",
23+
client: TEST_CLIENT,
24+
}),
25+
).rejects.toThrowError(
26+
"Could not resolve a valid tokenId from the domain: thirdwebsdk.thissuredoesnotexist. Make sure it exists.",
27+
);
28+
});
1829
});

packages/thirdweb/src/extensions/unstoppable-domains/read/resolveAddress.ts

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ThirdwebClient } from "../../../client/client.js";
44
import { getContract } from "../../../contract/contract.js";
55
import { getAddress, isAddress } from "../../../utils/address.js";
66
import { withCache } from "../../../utils/promise/withCache.js";
7+
import { exists } from "../__generated__/UnstoppableDomains/read/exists.js";
78
import { getMany } from "../__generated__/UnstoppableDomains/read/getMany.js";
89
import { namehash } from "../__generated__/UnstoppableDomains/read/namehash.js";
910
import { UD_POLYGON_MAINNET } from "../consts.js";
@@ -71,6 +72,14 @@ export async function resolveAddress(
7172
labels: name.split("."),
7273
});
7374

75+
// `namehash` always return a value, we should use `exists` to make sure it's valid
76+
const _exists = await exists({ contract, tokenId: possibleTokenId });
77+
if (!_exists) {
78+
throw new Error(
79+
`Could not resolve a valid tokenId from the domain: ${name}. Make sure it exists.`,
80+
);
81+
}
82+
7483
// Resolve ETH address from the tokenId
7584
const resolved = await getMany({
7685
contract,

0 commit comments

Comments
 (0)