A Typescript library used to interact with Namespace contracts and APIs. It uses Viem under the hood and can be used to:
- Find names listed on the Namespace platform
- Check the availability of subnames
- Mint subnames
This is the initial version, expect many more functionalities in the future!
Use a package manager to install the library into your project
Yarn
yarn add namespace-sdk
Npm
npm install namespace-sdk
First, we can create a simple NamespaceClient and specify the chainID. The chain id specifies a chain on which read/write blockchain operations happen. If we list our name on a Mainnet and subnames are minted on Mainnet, we'll have to specify a chainID 1. We will use a sepolia testnet as an example.
The chainID is required since the library supports minting subnames on both Layer 1 and its testnet (Sepolia) but also on Layer 2 (currently, only Base chain is supported).
import { createNamespaceClient } from "namespace-sdk";
import { sepolia } from "viem/chains";
const namespaceClient = createNamespaceClient({
chainId: sepolia.id
});
Minting ENS subnames requires a couple of steps.
First, we would need to have an ENS name that is listed on the Namespace platform. To do so, visit our Platform and check Manager
For testing purposes, you can use "namespace-sdk.eth" on the Sepolia chain.
After we list the ENS name, our platform allows minting subnames under it. We can use a library to check for subname availability and to generate mint transaction parameters.
import { createNamespaceClient, SetRecordsRequest, MintTransactionParameters } from "namespace-sdk";
import { sepolia } from "viem/chains";
const namespaceClient = createNamespaceClient({
chainId: sepolia.id,
});
const LISTED_NAME = "namespace-sdk.eth"
const ETH_COIN_TYPE = 60;
const generateMintingParameters = async (): Promise<MintTransactionParameters> => {
// Get listed name from namespace api
const listedName = await namespaceClient.getListedName(
LISTED_NAME,
sepolia.id
);
const subnameLabel = "myfunnylabel";
const minterAddress = "0x6CaBE5E77F90d58600A3C13127Acf6320Bee0aA7"
// Check for name availability
const isNotTaken = await namespaceClient.isSubnameAvailable(
listedName,
subnameLabel
);
if (!isNotTaken) {
throw Error("Subname is already taken!");
}
// Generate mint transcation parameters
const mintDetails = await namespaceClient.getMintTransactionParameters(listedName, {
minterAddress: minterAddress,
subnameLabel: subnameLabel,
subnameOwner: minterAddress,
// Optionaly, we can also set resolver records with the mint transaction
records: {
addresses: [
{
address: minterAddress,
coinType: ETH_COIN_TYPE
}
],
texts: [
{
key: "name",
value: "namespace"
}
]
}
});
return mintDetails;
};
Sending a transaction is the last step. Since the library uses Viem under the hood, we will use WalletClient to send a transaction.
import { sepolia } from "viem/chains";
import { privateKeyToAccount } from "viem/accounts";
import { createWalletClient, http } from "viem";
import { generateMintingParameters } from "./minting";
const sendMintTransaction = async () => {
// Import your wallet and create a Viem Wallet Client
const wallet = privateKeyToAccount("0xYourWallet");
const walletClient = createWalletClient({
transport: http(),
chain: sepolia,
account: wallet
})
// Generate minting parameters
const mintParams = await generateMintingParameters();
// Send transaction
const transactionHash = await walletClient.writeContract({
abi: mintParams.abi,
address: mintParams.contractAddress,
functionName: mintParams.functionName,
args: mintParams.args,
value: mintParams.value
})
console.log(transactionHash);
}