Skip to content
Permalink
Branch: master
Find file Copy path
Fetching contributors…
Cannot retrieve contributors at this time
62 lines (48 sloc) 2.14 KB

Working with ENS

Before you can begin interacting with ENS, you will need to obtain a reference to the ENS registry. How you do this depends on the library you are using.

Example code for the Javascript-based APIs (ethereum-ens, web3.js, ethjs-ens, and ethers.js) here expect that they are being run inside a DApp browser, such as Chrome with metamask installed, which exposes the ethereum object.

{% tabs %} {% tab title="ethereum-ens" %}

var ENS = require('ethereum-ens');

var accounts = ethereum.enable();
var ens = new ENS(ethereum);

{% endtab %}

{% tab title="web3.js" %}

var Web3 = require("web3")

var accounts = ethereum.enable();
var web3 = new Web3(ethereum);
var ens = web3.eth.ens;

{% endtab %}

{% tab title="ethjs-ens" %}

const ENS = require('ethjs-ens');
// Currently requires both provider and
// either a network or registryAddress param
var accounts = ethereum.enable();
const ens = new ENS({ ethereum, network: '1' });

{% endtab %}

{% tab title="ethers.js" %}

var ethers = require('ethers');
var provider = new ethers.providers.Web3Provider(ethereum);
// ENS functionality is provided directly on the core provider object.

{% endtab %}

{% tab title="web3.py" %}

from ens.auto import ns

{% endtab %}

{% tab title="web3j" %}

EnsResolver ens = new EnsResolver(web3j, 300 /* sync threshold, seconds */);

{% endtab %} {% endtabs %}

Some web3 libraries - at present, ethers.js, web3j, and web3.py - have integrated support for name resolution. In these libraries, you can pass in an ENS name anywhere you can supply an address, meaning you do not need to interact directly with their ENS APIs unless you want to manually resolve names or do other ENS operations.

If no library is available for your platform, you can instantiate the ENS registry contract directly using the interface definition here. Addresses for the ENS registry on each supported network are available in the ENS Deployments page.

You can’t perform that action at this time.