enscribe is a Solidity library that provides a simple interface for assigning ENS names to contracts. It handles the complete ENS naming flow:
- Subname creation under a parent domain
- Forward resolution (name → address)
- Reverse resolution (address → name)
The library can be installed as a dependency in your Foundry project:
forge install enscribexyz/enscribeThen add this to your remappings.txt:
enscribe/=lib/enscribe/src/The simplest way to use the library is to import it in your foundry script and call the setName function:
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.0;
import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";
import {Ens} from "enscribe/Ens.sol";
contract MyContractScript is Script {
function run() public {
vm.startBroadcast();
counter = new Counter();
Ens.setName(block.chainid, address(counter), "mycontract.mydomain.base.eth");
vm.stopBroadcast();
}
}You can now deploy and set the name on Base with forge script:
$ forge script script/Counter.s.sol:CounterScript --rpc-url <BASE RPC URL> --chain-id 8453 --broadcast --private-key <PRIVATE KEY> -vvvvSets both forward and reverse resolution for a contract address. This is the main function you'll use in most cases.
Function Signature:
function setName(
uint256 chainId,
address contractAddress,
string memory fullName
) internal returns (bool success)Parameters:
chainId: The chain ID where the contract is deployed (e.g.,1for Ethereum Mainnet,8453for Base Mainnet)contractAddress: The address of the contract to name (must not be zero address)fullName: The full ENS name to assign (e.g.,"mycontract.mydomain.eth")
Returns:
success:trueif the operation succeeded,falseotherwise
What it does:
- Creates a subname under the parent domain
- Sets forward resolution (name → address mapping)
- Sets reverse resolution (address → name mapping)
Example:
import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";
import {Ens} from "enscribe/Ens.sol";
contract CounterScript is Script {
function run() public {
vm.startBroadcast();
counter = new Counter();
Ens.setName(block.chainid, address(counter), "mycontract.mydomain.eth");
vm.stopBroadcast();
}
}Requirements:
- The caller (
msg.sender) must own the parent ENS node (e.g.,mydomain.eth) - The
contractAddressmust not be the zero address - The
fullNamemust be a valid ENS name format (contains at least one dot)
Sets only forward resolution (name → address) without setting reverse resolution. Useful when you only need forward lookup.
Function Signature:
function setForwardResolution(
uint256 chainId,
address contractAddress,
string memory fullName
) internal returns (bool success)Parameters:
chainId: The chain ID where the contract is deployedcontractAddress: The address of the contract to namefullName: The full ENS name to assign
Returns:
success:trueif the operation succeeded,falseotherwise
Example:
import {Script, console} from "forge-std/Script.sol";
import {Counter} from "../src/Counter.sol";
import {Ens} from "enscribe/Ens.sol";
contract CounterScript is Script {
function run() public {
vm.startBroadcast();
counter = new Counter();
Ens.setForwardResolution(block.chainid, address(counter), "mycontract.mydomain.eth");
vm.stopBroadcast();
}
}