🥷
Namespace
  • [ Introduction ]
    • ENS
    • Namespace
      • Products and Services
      • Use Cases
  • [ Namespace Platform ]
    • Overview
    • Search & Registration
    • Manager
      • Listing an ENS Name
      • Features List
    • ENS Widget
    • Farcaster Frames
      • Default Frame
      • Custom Frame
    • Tutorials and Demos
  • [ Dev Docs ]
    • Overview
    • How-To Guides
      • Mint L1 or L2 subnames using SDK
    • Smart contracts
      • Namespace Minter
    • Backend API
      • POST /api/v1/mint
      • POST /api/v1/mint/verified
      • GET /api/v1/listings/availabe
    • Offchain Subnames API
      • Setting Up Offchain Resolver
      • Minting
        • Create a new ENS subname
      • Subnames
        • Resolve subname address
        • Check subname availability
        • Retrieve Complete Subname Dataset
        • Subname Search
        • Delete Subname
      • Text Records
        • Create or update subname text record
        • Delete a subname text record
        • Retrieve single subname text record
        • Retrieve all of subname's text records
      • Custom Data
        • Create or update custom subname data
        • Delete custom subname data
        • Retrieve single custom data object
        • Retrieve all of subname's custom data
    • Namespace SDK
      • Offchain Manager
        • Installation
        • Methods
        • Generate API key
        • CreateSubname
        • DeleteSubname
        • IsSubnameAvailable
        • GetFilteredSubnames
        • TextRecords
        • DataRecords
      • Indexer Manager
        • Installation
        • Methods
        • GetL2Subname
        • GetL2Subnames
        • GetL1Subnames
        • GetL1Subnames
        • GetL1Subname
      • Mint Manager (soon)
      • Namespace Client (deprecated)
        • Installation
        • GetListedName
        • GetMintDetails
        • GetMintTransactionParameters
        • IsSubnameAvailable
        • GenerateAuthToken
    • Namespace L2 Subnames
  • [ ecosystem ]
    • Partnerships
    • Integrations
    • Links
    • ENS ideas and projects
  • [ Legal ]
    • Disclamer
    • Terms of Service
    • Privacy Policy
Powered by GitBook
On this page
  • 1. Listing an ENS name
  • 2. Installing namespace-sdk library
  • 3. Create a NamespaceClient instance
  • 4. Getting a listed name and checking for subname availability
  • 5. Getting mint transaction parameters
  • 6. Execute mint transaction
  1. [ Dev Docs ]
  2. How-To Guides

Mint L1 or L2 subnames using SDK

PreviousHow-To GuidesNextSmart contracts

Last updated 5 months ago

This guide will show you how to implement a subname minting functionality using

Namespace SDK is a typescript library, which can be used to interact with Namespace API and smart contracts. It currently allows devs to mint subnames under listed ENS names and abstracts the different chain logic.

1. Listing an ENS name

The prerequisite for minting is for the parent ENS name to be listed on the Namespace platform. This can be done using the Manager on our .

For testing purposes, we can use an already listed name "mint-on-namespace.eth". This name is listed on the Sepolia testnet and the subnames will be minted on the BaseSepolia testnet.

Get in touch with us on our or if you need some testnet ETH.

2. Installing namespace-sdk library

The second step would be to include namespace-SDK dependency in our typescript project. Since the SDK uses under the hood, we would also need to include it as a dependency.

yarn add namespace-sdk viem

npm install namespace-sdk viem

3. Create a NamespaceClient instance

We will use createNamespaceClientfunction to set up a Namespace Client with a given ChainId.

The ChainId specifies an id of a blockchain where the subnames are being minted from (In our case, since we will be using "mint-on-namespace.eth", we will specify baseSepolia.id (84532), this is so that viem knows which chain to look at when it simulates the mint transaction or when it checks for subname availability.

3.1

Currently, when it comes to layer 2 subnames, Namespace supports two chains for mainnet and one testnet chain. The chainId parameter depends on the l2 chain where the name is listed.

Supported chains:

Chain
ChainId
Testnet

Optimism

10

false

Base

8453

false

Base Sepolia

84532

true

namespace-client.ts
import { createNamespaceClient } from "namespace-sdk";
import { baseSepolia } from "viem/chains";

export const NamespaceClient = createNamespaceClient({
    chainId: baseSepolia.id,
    mintSource: "my-app",
    rpcUrl: "https://alchemy-url"
});

export const ENS_NAME = "mint-on-namespace.eth";

Parameters

Parameter
Required
Description

chainId

true

Specifies chain id where subnames are minted from, supported chains include

mintSource

false

Used to track the source of minted subnames on a blockchain, defaults to "namespace-sdk".

rpcUrl

false

Http RPC URL which will be used by SDK when it performs blockchain operations, defaults to a public RPC for a given chain

4. Getting a listed name and checking for subname availability

details.ts
import { NamespaceClient, ENS_NAME } from "./namespace-client";
import { Listing } from "namespace-sdk";
import { sepolia } from "viem/chains";

const getListedName = async(): Listing => {
    // Here, we're specifying sepolia id as a second parameter
    // since our name is listed on sepolia testnet
    // for names listed on mainnet, we do not need to provide a second parameter
    return NamespaceClient.getListedName(ENS_NAME, sepolia.id)
}

const isSubnameAvailable = async (listedName: Listing, subnameLabel: string): boolean => {
    return NamespaceClient.isSubnameAvailable(listedName, subnameLabel);
}

5. Getting mint transaction parameters

mint-parameters.ts
import { NamespaceClient, ENS_NAME } from "./namespace-client";
import { Listing, MintTransactionParameters, AddressRecord } from "namespace-sdk";
import { getListedName, isSubnameAvailable } from "./details.ts";
import { Address } from "viem";

// Minting a subname with provided label
export const getTxParameters = (listedName: Listing, minterAddress: Address, subnameLabel: string): Promise<MintTransactionParameters> => {
    return NamespaceClient.getMintTransactionParameters({
        minterAddress,
        subnameOwner: minterAddress,
        subnameLabel
    });
}

// Minting a subname and providing addresses/text records in the same transaction
export const getTxParametersWithRecords = (listedName: Listing, minterAddress: Address, subnameLabel: string, addresses: AddressRecord[]): Promise<MintTransactionParameters> => {
   return NamespaceClient.getMintTransactionParameters({
        minterAddress,
        subnameOwner: minterAddress,
        subnameLabel,
        records: {
             addresses: addresses,
             texts: []
        }
    });
}

6. Execute mint transaction

Now that we have all the required parameters, we can use all this to execute a mint transaction and mint a subname, using our preferred client library.

mint.ts
import { getListedName, isSubnameAvailable } from "./details.ts";
import { getTxParametersWithRecords } from "./mint-parameters";
import { Listing, MintTransactionParameters } from "namespace-sdk";
import { privateKeyToAccount } from "viem/accounts";
import { createWalletClient, http } from "viem";
import { baseSepolia } from "viem/chains";

const privateKeyAccount = privateKeyToAccount("0xmywalletkey");

const mint = async () => {
    const minterAddress = privateKeyAccount.address;
    const subnameLabel = "my-label";
    
    const listedName: Listing = await getListedName();
    const isAvailable = await isSubnameAvailable(listedName, subnameLabel);
    
    if (!isAvailable) {
        throw new Error("Subname is already taken");
    }
    
    const addressRecords = [{ coinType: 60, address: minterAddress }]; 
    const params = await getTxParametersWithRecords(listedName, minterAddress,         subnameLabel, addressRecords);
    
    const transactionHash = await getViemWalletClient().writeContract({
        functionName: params.functionName,
        args: params.args,
        abi: params.abi,
        value: params.value,
        address: params.contractAddress
    });
    
    console.log("Subname has been minted, transaction: " + transactionHash)

}

const getViemWalletClient = () => {
    return createWalletClient({
        transport: http(),
        chain: baseSepolia,
        account: privateKeyAccount
    })
}

In progress

In progress

We have now minted a subname on the BaseSepolia chain! We can now verify that it gets resolved on the ENS app using the URL: https://app.ens.domains/your-full-name.eth ( remember to switch to the Sepolia network if you've been using mint-on-namespace.eth )

Now that we have our NamespaceClient, we can use it to and check for subname .

parameters return all the needed values for executing a mint transaction

Namespace-SDK
platform
Listing an ENS Name
Discord channel
Namespace Devs Telegram group
viem
fetch the listed name
availability
Mint transaction