Create Subdomains
Scripts to create Solana / Bonfida Subdomains
Old Way
import { Connection, Keypair, Transaction, sendAndConfirmTransaction } from '@solana/web3.js';
import { createSubdomain } from '@bonfida/spl-name-service';
import fs from 'fs';
import dotenv from 'dotenv';
dotenv.config();
const SOLANA_RPC_URL = process.env.RPC_URL!;
const WALLET_PATH = '/Users/metasal/.config/solana/id.json'; // Path to your wallet keypair JSON file
const PARENT_DOMAIN = 'gamblethis.sol';
const SUBDOMAIN = 'example';
const FULL_DOMAIN = `${SUBDOMAIN}.${PARENT_DOMAIN}`;
const createSubdomain = async () => {
try {
console.log(`Attempting to register subdomain ${FULL_DOMAIN}...`);
const connection = new Connection(SOLANA_RPC_URL, 'confirmed');
const walletKeyData = JSON.parse(fs.readFileSync(WALLET_PATH, 'utf8'));
const walletKeypair = Keypair.fromSecretKey(
Uint8Array.from(walletKeyData)
);
console.log(`Using wallet: ${walletKeypair.publicKey.toString()}`);
console.log('Creating subdomain instruction...');
const instruction = await createSubdomain(
connection,
FULL_DOMAIN, // The full subdomain (e.g., "x.gamblethis.sol")
walletKeypair.publicKey // The wallet that owns the parent domain
);
// Create and sign transaction
const transaction = new Transaction().add(...instruction);
// Set recent blockhash and fee payer
const { blockhash } = await connection.getLatestBlockhash('finalized');
transaction.recentBlockhash = blockhash;
transaction.feePayer = walletKeypair.publicKey;
// Sign and send transaction
console.log('Sending transaction...');
const signature = await sendAndConfirmTransaction(
connection,
transaction,
[walletKeypair],
{ commitment: 'confirmed' }
);
console.log(`✅ Successfully registered subdomain ${FULL_DOMAIN}!`);
console.log(`Transaction signature: ${signature}`);
console.log(`Solana Explorer: https://explorer.solana.com/tx/${signature}`);
} catch (error) {
console.error('Error registering subdomain:');
console.error(error);
process.exit(1);
}
};
createSubdomain();