Skip to content

Commit

Permalink
Feat: add getEnsText and useEnsText (#3459)
Browse files Browse the repository at this point in the history
* feat: add getEnsText and useEnsText

* docs: useEnsText

* chore: changeset

---------

Co-authored-by: Tom Meagher <tom@meagher.co>
  • Loading branch information
marthendalnunes and tmm committed Jan 16, 2024
1 parent 1d07f13 commit d950b66
Show file tree
Hide file tree
Showing 22 changed files with 843 additions and 0 deletions.
5 changes: 5 additions & 0 deletions .changeset/giant-peas-perform.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@wagmi/core": minor
---

Added `getEnsText` action.
6 changes: 6 additions & 0 deletions .changeset/gold-trains-peel.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
---
"@wagmi/connectors": patch
"create-wagmi": patch
---

Bumped dependencies
5 changes: 5 additions & 0 deletions .changeset/moody-jars-wave.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"wagmi": minor
---

Added `useEnsText` action.
8 changes: 8 additions & 0 deletions docs/.vitepress/sidebar.ts
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,10 @@ export function getSidebar() {
text: 'useEnsResolver',
link: '/react/api/hooks/useEnsResolver',
},
{
text: 'useEnsText',
link: '/react/api/hooks/useEnsText',
},
{
text: 'useFeeHistory',
link: '/react/api/hooks/useFeeHistory',
Expand Down Expand Up @@ -507,6 +511,10 @@ export function getSidebar() {
text: 'getEnsResolver',
link: '/core/api/actions/getEnsResolver',
},
{
text: 'getEnsText',
link: '/core/api/actions/getEnsText',
},
{
text: 'getFeeHistory',
link: '/core/api/actions/getFeeHistory',
Expand Down
195 changes: 195 additions & 0 deletions docs/core/api/actions/getEnsText.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,195 @@
<script setup>
const packageName = '@wagmi/core'
const actionName = 'getEnsText'
const typeName = 'GetEnsText'
</script>

# getEnsText

Action for fetching a text record for a specified ENS name and key.

## Import

```ts
import { getEnsText } from '@wagmi/core'
```

## Usage

::: code-group
```ts [index.ts]
import { getEnsText } from '@wagmi/core'
import { normalize } from 'viem/ens'
import { config } from './config'

const ensText = getEnsText(config, {
name: normalize('wevm.eth'),
key: 'com.twitter',
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

::: warning
Since ENS names prohibit certain forbidden characters (e.g. underscore) and have other validation rules, you likely want to [normalize ENS names](https://docs.ens.domains/contract-api-reference/name-processing#normalising-names) with [UTS-46 normalization](https://unicode.org/reports/tr46) before passing them to `getEnsText`. You can use Viem's built-in [`normalize`](https://viem.sh/docs/ens/utilities/normalize) function for this.
:::

## Parameters

```ts
import { type GetEnsTextParameters } from '@wagmi/core'
```

---

### blockNumber

`bigint | undefined`

Block number to get the text at.

::: code-group
```ts [index.ts]
import { getEnsText } from '@wagmi/core'
import { normalize } from 'viem/ens'
import { config } from './config'

const ensText = getEnsText(config, {
blockNumber: 17829139n, // [!code focus]
name: normalize('wevm.eth'),
key: 'com.twitter',
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

### blockTag

`'latest' | 'earliest' | 'pending' | 'safe' | 'finalized' | undefined`

Block tag to get the text at.

::: code-group
```ts [index.ts]
import { getEnsText } from '@wagmi/core'
import { normalize } from 'viem/ens'
import { config } from './config'

const ensText = getEnsText(config, {
blockTag: 'latest', // [!code focus]
name: normalize('wevm.eth'),
key: 'com.twitter',
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

---

### chainId

`config['chains'][number]['id'] | undefined`

ID of chain to use when fetching data.

::: code-group
```ts [index.ts]
import { getEnsText } from '@wagmi/core'
import { mainnet } from '@wagmi/core/chains'
import { normalize } from 'viem/ens'
import { config } from './config'

const ensText = await getEnsText(config, {
chainId: mainnet.id, // [!code focus]
name: normalize('wevm.eth'),
key: 'com.twitter',
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

### key

`string`

ENS key to get Text for.

::: code-group
```ts [index.ts]
import { getEnsText } from '@wagmi/core'
import { normalize } from 'viem/ens'
import { config } from './config'

const ensText = await getEnsText(config, {
name: normalize('wevm.eth'),
key: 'com.twitter', // [!code focus]
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

### name

`string`

Name to get the text for.

::: code-group
```ts [index.ts]
import { getEnsText } from '@wagmi/core'
import { normalize } from 'viem/ens'
import { config } from './config'

const ensText = await getEnsText(config, {
name: normalize('wevm.eth'), // [!code focus]
key: 'com.twitter',
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

### universalResolverAddress

`Address | undefined`

- Address of ENS Universal Resolver Contract.
- Defaults to current chain's Universal Resolver Contract address.

::: code-group
```ts [index.ts]
import { getEnsText } from '@wagmi/core'
import { normalize } from 'viem/ens'
import { config } from './config'

const ensText = await getEnsText(config, {
name: normalize('wevm.eth'),
key: 'com.twitter',
universalResolverAddress: '0x74E20Bd2A1fE0cdbe45b9A1d89cb7e0a45b36376', // [!code focus]
})
```
<<< @/snippets/core/config.ts[config.ts]
:::

## Return Type

```ts
import { type GetEnsTextReturnType } from '@wagmi/core'
```

`string | null`

The text record for ENS name.

Returns `null` if name does not have text assigned.

## Error

```ts
import { type getEnsTextError } from '@wagmi/core'
```

<!--@include: @shared/query-imports.md-->

## Viem

- [`getEnsText`](https://viem.sh/docs/ens/actions/getEnsText.html)
Loading

1 comment on commit d950b66

@vercel
Copy link

@vercel vercel bot commented on d950b66 Jan 16, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.