🥷
Namespace
  • [ Introduction ]
    • ENS
    • Namespace
  • Official Links
  • [ Dapps ]
    • Overview
    • Onchain Subs (App)
      • Search & Register
      • Wizard
        • Listing an ENS Name
        • Features List
      • ENS Widget
      • Farcaster Frames
        • Default Frame
        • Custom Frame
    • Offchain Subs (DevPortal)
      • Subname create/manage
      • Resolver set
      • API keys
  • How-to Guides and Demos
  • [ Dev Docs ]
    • SDK
      • Offchain Manager
        • Installation
        • Generate API key
        • Create or Update Subname
        • DeleteSubname
        • IsSubnameAvailable
        • GetFilteredSubnames
        • AddressRecords
          • ChainName
        • TextRecords
        • DataRecords
      • Indexer Manager
        • Installation
        • Methods
        • GetL2Subname
        • GetL2Subnames
      • Mint Manager
        • Installation
        • GetMintDetails
        • GetMintTransactionParameters
        • IsSubnameAvailable
        • How To Mint L1 & L2 subname via mint-manager
      • Namespace Client (deprecated)
        • Installation
        • GetListedName
        • GetMintDetails
        • GetMintTransactionParameters
        • IsSubnameAvailable
        • GenerateAuthToken
    • APIs
      • Offchain Manager
      • Mint Manager
    • Infrastructure
      • Namespace L2 Subnames
  • [ ecosystem ]
    • Use Cases
  • [ Jobs ]
    • 🧑‍💻 Full-Stack Dev
    • 🥷 Dev-Rel Lead
    • 💼 BD Lead
Powered by GitBook
On this page
  • 01. Listing an ENS name
  • 02. Installing @namespacesdk/mint-manager library
  • 03. Create and configure Instance of MintClient
  • 04. Create Instances of Viem Public and Wallet Client
  • 04. Implement mint functionality
  1. [ Dev Docs ]
  2. SDK
  3. Mint Manager

How To Mint L1 & L2 subname via mint-manager

PreviousIsSubnameAvailableNextNamespace Client (deprecated)

Last updated 19 hours ago

This guide will show you how to implement a subname minting functionality using @namespacesdk/mint-manager library

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

01. 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 .

02. Installing @namespacesdk/mint-manager library

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

npm install @namespacesdk/mint-manager@latest viem

yarn add @namespacesdk/mint-manager viem

03. Create and configure Instance of MintClient

mint-client.ts
import { MintClientConfig, createMintClient, MintClient } from "@namespacesdk/mint-manager"

export const MY_ENS_NAME = "namespace.eth";
const TOKEN = process.env.ALCHEMY_TOKEN

const clientConfig: MintClientConfig = {
  customRpcUrls: {
    [base.id]: `https://base-mainnet.g.alchemy.com/v2/${TOKEN}`
  },
  mintSource: "my-ens-dapp"
}
export const mintClient: MintClient = createMintClient(clientConfig) 

04. Create Instances of Viem Public and Wallet Client

Since we use Viem under the hood, we would need to instantiate Viem client, which will be used to actualy interact with ethereum/base chain

web3-client.ts
import { createPublicClient, createWalletClient } from "viem";
import { base } from "viem/chains"
import { privateKeyToAccount } from "viem/account"

const TOKEN = process.env.ALCHEMY_TOKEN
const WALLET_KEY = process.env.WALLET_PRIVATE_KEY
const ALCHEMY_RPC = `https://base-mainnet.g.alchemy.com/v2/${TOKEN}`

const wallet = privateKeyToAccount(WALLET_KEY)

export const BASE_ID = base.id
export const WALLET_ADDRESS = wallet.address
export const publicClient = createPublicClient({
    transport: http(ALCHEMY_RPC),
    chain: base
})

export const walletClient = createWalletClient({
    transport: http(ALCHEMY_RPC),
    chain: base
})

04. Implement mint functionality

import { publicClient, walletClient, MY_ENS_NAME } from "./web3-client";
import { mintClient, BASE_ID, WALLET_ADDRESS } from "./mint-client";

const ETH_COIN = 60;

const mintSubname = async () => { 
   const subnameLabel = "superman";
   // we will mint superman.namespace.eth
   const fullName = `${subnameLabel}.${MY_ENS_NAME}`
   
   const isAvailable = await mintClient.isL2SubnameAvailable(fullName, BASE_ID)
   if (!isAvailable) {
      throw new Error(`${fullName} is already taken!`);
   }
   
   const mintDetails = await mintClient.getMintDetails({
      minterAddress: WALLET_ADDRESS,
      parentName: MY_ENS_NAME
      label: subnameLabel;
   });
   
   if (!mintDetails.canMint) {
      throw new Error(`Subname cannot be minted, reason:    ${mintDetails.validationErrors[0] || "Unknown Reason"} `)
   }
   
   const totalPrice = mintDetails.estimatedPriceEth + mintDetails.estimatedFeeEth;
   console.log(`The price for minting a subname will be: ${totalPrice} ETH`)
   
   const txParameters = await mintClient.getMintTransactionParameters({
      parentName: MY_ENS_NAME,
      label: subnameLabel,
      minterAddress: WALLET_ADDRESS,
      records: {
         addresses: {
            [ETH_COIN]: WALLET_ADDRESS
         }
      }
   })
   
   const { request } = await publicClient.simulateContract({
      address: txParameters.contractAddress,
      args: txParameters.args,
      value: txParameters.value,
      account: WALLET_ADDRESS,
      functionName: txParameters.functionName,
      abi: txParameters.abi
   })
   
   const transactionHash = await walletClient.writeContract(request);
   console.log(`Subname ${fullSubname} has been minted! Transaction hash: ${transaction Hash}`)
}

platform
viem