The ENS interface defines the core contract specification that lies at the heart of ENS resolution. All ENS lookups start by querying a contract that implements this interface.
Overview
The ENS interface is the base specification for the ENS Registry. It defines methods for:
- Managing domain ownership and transfers
- Setting and querying resolvers
- Managing TTL (Time To Live) values
- Creating and managing subdomains
- Operator approvals for managing records on behalf of owners
Import
import '@ensdomains/ens-contracts/contracts/registry/ENS.sol';
Events
NewOwner
event NewOwner(bytes32 indexed node, bytes32 indexed label, address owner)
Logged when the owner of a node assigns a new owner to a subnode.
The hash of the subdomain label
The address of the new owner
Transfer
event Transfer(bytes32 indexed node, address owner)
Logged when the owner of a node transfers ownership to a new account.
The node being transferred
The address of the new owner
NewResolver
event NewResolver(bytes32 indexed node, address resolver)
Logged when the resolver for a node changes.
The node whose resolver is being updated
The address of the new resolver
NewTTL
event NewTTL(bytes32 indexed node, uint64 ttl)
Logged when the TTL of a node changes.
The node whose TTL is being updated
The new TTL value in seconds
ApprovalForAll
event ApprovalForAll(address indexed owner, address indexed operator, bool approved)
Logged when an operator is added or removed.
The address that owns the records
The address being approved or revoked
True if approved, false if revoked
Functions
setRecord
function setRecord(
bytes32 node,
address owner,
address resolver,
uint64 ttl
) external
Sets the owner, resolver, and TTL for a node in a single operation.
The address of the new owner
The address of the resolver
setSubnodeRecord
function setSubnodeRecord(
bytes32 node,
bytes32 label,
address owner,
address resolver,
uint64 ttl
) external
Sets the owner, resolver, and TTL for a subnode in a single operation.
The hash of the label specifying the subnode
The address of the new owner
The address of the resolver
setSubnodeOwner
function setSubnodeOwner(
bytes32 node,
bytes32 label,
address owner
) external returns (bytes32)
Creates a new subnode and sets its owner.
The hash of the label specifying the subnode
The address of the new owner
Returns: The created subnode hash keccak256(abi.encodePacked(node, label))
setResolver
function setResolver(bytes32 node, address resolver) external
Sets the resolver address for a node.
The address of the resolver
setOwner
function setOwner(bytes32 node, address owner) external
Transfers ownership of a node to a new address.
The node to transfer ownership of
The address of the new owner
setTTL
function setTTL(bytes32 node, uint64 ttl) external
Sets the TTL for a node.
setApprovalForAll
function setApprovalForAll(address operator, bool approved) external
Enables or disables approval for a third party (“operator”) to manage all of the caller’s ENS records.
Address to add to the set of authorized operators
True if the operator is approved, false to revoke approval
owner
function owner(bytes32 node) external view returns (address)
Returns the address that owns the specified node.
Returns: The address of the owner
resolver
function resolver(bytes32 node) external view returns (address)
Returns the address of the resolver for the specified node.
Returns: The address of the resolver
ttl
function ttl(bytes32 node) external view returns (uint64)
Returns the TTL of a node and any records associated with it.
Returns: The TTL of the node in seconds
recordExists
function recordExists(bytes32 node) external view returns (bool)
Returns whether a record exists for the specified node.
Returns: True if the record exists, false otherwise
isApprovedForAll
function isApprovedForAll(
address owner,
address operator
) external view returns (bool)
Queries if an address is an authorized operator for another address.
The address that owns the records
The address that acts on behalf of the owner
Returns: True if operator is an approved operator for owner, false otherwise
Usage Example
Basic Usage
Managing Domains
import '@ensdomains/ens-contracts/contracts/registry/ENS.sol';
contract MyContract {
ENS public ens;
constructor(ENS _ens) {
ens = _ens;
}
function resolveAddress(bytes32 node) public view returns (address) {
address resolverAddress = ens.resolver(node);
// Use the resolver to get more information
return resolverAddress;
}
function checkOwnership(bytes32 node) public view returns (address) {
return ens.owner(node);
}
}
import '@ensdomains/ens-contracts/contracts/registry/ENS.sol';
contract DomainManager {
ENS public ens;
constructor(ENS _ens) {
ens = _ens;
}
function createSubdomain(
bytes32 parentNode,
bytes32 label,
address owner,
address resolver
) public {
// Create subdomain and set resolver in one call
ens.setSubnodeRecord(parentNode, label, owner, resolver, 0);
}
function transferDomain(bytes32 node, address newOwner) public {
ens.setOwner(node, newOwner);
}
}