Skip to content

Commit

Permalink
ENS Package tutorial (#6687)
Browse files Browse the repository at this point in the history
* ens package tutorial

* Update docs/docs/guides/ens/index.md
  • Loading branch information
jdevcs authored Jan 8, 2024
1 parent 2a40b66 commit b188714
Showing 1 changed file with 117 additions and 0 deletions.
117 changes: 117 additions & 0 deletions docs/docs/guides/ens/index.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
---
sidebar_position: 9
sidebar_label: 'Web3 ENS'
---

# Using web3.js ENS Package

In this tutorial, we'll explore how to use the web3.js ENS (Ethereum Name Service) package. The Ethereum Name Service (ENS) is a decentralized domain system built on the Ethereum blockchain. It serves as a distributed, secure, and human-readable naming system designed to map Ethereum addresses, smart contracts, and various other services to easily understandable names.

## Installing web3.js

First, install web3.js version 4 in your project using npm:

```bash
npm install web3
```

## Setting up web3 and ENS

Now, let's set up web3 and ENS in a TypeScript file:

```typescript
import Web3 from 'web3';

// Assuming you have a provider, replace 'http://localhost:8545' with your Web3 provider
const web3 = new Web3('http://localhost:8545');

// You can use ENS with web3 object:
const ens = await web3.eth.ens.getAddress('alice.eth');

```

## Installing web3.js ENS

For directly using ENS package first install ENS package and import `ENS`.

```bash
npm install web3-eth-ens
```

```typescript
import { ENS } from 'web3-eth-ens';

const ens = new ENS(undefined,'https://127.0.0.1:4545');

console.log(await ens.getAddress('vitalik.eth'));

```

## ENS Examples

### getAddress

The getAddress function retrieves the Ethereum address associated with the given ENS name. It resolves the address by querying the ENS resolver for the provided ENS name and returns the resolved Ethereum address.

```typescript
const address = await web3.eth.ens.getAddress('ethereum.eth');
console.log(address);
```

### getContenthash

The getContenthash function retrieves the content hash associated with the provided ENS name. It communicates with the ENS resolver to obtain the content hash value and returns the resolved content hash.

```typescript
const hash = await web3.eth.ens.getContenthash('ethereum.eth');
console.log(hash);
```

### getOwner

The getOwner function obtains the owner of the specified ENS name. It queries the ENS registry to fetch the owner of the ENS name and returns the owner's Ethereum address.

```typescript
const owner = await web3.eth.ens.getOwner('ethereum.eth');
console.log(owner);
```

### getPubKey

The getPubKey function fetches the public key x and y associated with the provided ENS name using the ENS resolver.

```typescript
const key = await web3.eth.ens.getPubkey('xyz.eth');
console.log(key);
```

### getResolver

The getResolver function retrieves the resolver for the given ENS name.

```typescript
const resolver = await web3.eth.ens.getResolver('xyz.eth');
console.log(resolver.options.address);
```

### getTTL

The getTTL function retrieves the Time-to-Live (TTL) value associated with the specified ENS name.

```typescript
const ttl = await web3.eth.ens.getTTL('xyz.eth');
console.log(ttl);
```

### recordExists

The recordExists function checks whether a record exists for the given ENS name.

```typescript
const result = await web3.eth.ens.recordExists('ethereum.eth');
console.log(result);
```

## Conclusion

In this tutorial, we've covered how to use the web3.js ENS package to interact with Ethereum Name Service. You should now be able to perform various ENS-related operations using web3.js version 4. For more details visit web3.js ENS [documentation](/libdocs/ENS) section.

1 comment on commit b188714

@github-actions
Copy link

Choose a reason for hiding this comment

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

Benchmark

Benchmark suite Current: b188714 Previous: 6c075db Ratio
processingTx 9224 ops/sec (±4.34%) 9301 ops/sec (±4.81%) 1.01
processingContractDeploy 38322 ops/sec (±8.95%) 39129 ops/sec (±7.62%) 1.02
processingContractMethodSend 19113 ops/sec (±7.11%) 19443 ops/sec (±5.19%) 1.02
processingContractMethodCall 39391 ops/sec (±6.00%) 38971 ops/sec (±6.34%) 0.99
abiEncode 44003 ops/sec (±6.81%) 44252 ops/sec (±6.92%) 1.01
abiDecode 31184 ops/sec (±8.30%) 30419 ops/sec (±8.89%) 0.98
sign 1631 ops/sec (±2.92%) 1656 ops/sec (±4.08%) 1.02
verify 376 ops/sec (±0.58%) 373 ops/sec (±0.78%) 0.99

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.