diff --git a/src/utils/getPrice.ts b/src/utils/getPrice.ts index dc093d1..fc631ea 100644 --- a/src/utils/getPrice.ts +++ b/src/utils/getPrice.ts @@ -2,7 +2,10 @@ import { createPublicClient, http } from 'viem'; import { abi } from './abi'; import { getChainDetailsByTLD } from './getChainDetailsbyTLD'; -export async function getTotalPrice(domainArray: string[], tld: string) { +export async function getTotalPrice( + domainArray: string[], + tld: string +): Promise<{ totalPrice: bigint }> { try { const chainDetails = getChainDetailsByTLD(tld); if (!chainDetails) { @@ -16,37 +19,25 @@ export async function getTotalPrice(domainArray: string[], tld: string) { transport: http(), }); - const contractCallRegisterPromises = domainArray.map(item => - publicClient.readContract({ - abi: abi, - address: registryAddress as `0x${string}`, - functionName: 'priceToRegister', - args: [item.length], - }) + const contractCallPromises = domainArray.map( + item => + publicClient.readContract({ + abi: abi, + address: registryAddress as `0x${string}`, + functionName: 'priceToRegister', + args: [item.length], + }) as Promise // Explicitly cast the promise to return a bigint ); - const contractCallRenewPromises = domainArray.map(item => - publicClient.readContract({ - abi: abi, - address: registryAddress as `0x${string}`, - functionName: 'priceToRenew', - args: [item.length], - }) - ); - - const cartItemsPriceRegister = await Promise.all( - contractCallRegisterPromises - ); - const cartItemsPriceRenew = await Promise.all(contractCallRenewPromises); + const cartItemsPriceRegister = await Promise.all(contractCallPromises); - if (cartItemsPriceRegister && cartItemsPriceRenew) { - const cartItemsPrice = domainArray.map((item, index) => ({ - name: item, - priceToRegister: cartItemsPriceRegister[index], - priceToRenew: cartItemsPriceRenew[index], - })); + if (cartItemsPriceRegister) { + const totalPrice = cartItemsPriceRegister.reduce( + (total, price) => total + price, + BigInt(0) + ); - return { cartItemsPrice }; + return { totalPrice }; } else { throw new Error('Failed to fetch prices'); } diff --git a/src/utils/registerDomain.ts b/src/utils/registerDomain.ts new file mode 100644 index 0000000..7767047 --- /dev/null +++ b/src/utils/registerDomain.ts @@ -0,0 +1,63 @@ +import { zeroAddress } from 'viem'; +import { abi } from './abi'; +import { getChainDetailsByTLD } from './getChainDetailsbyTLD'; + +import type { WalletClient } from 'viem'; +import { getTotalPrice } from './getPrice'; +import { resolveDomain } from './resolveDomain'; + +async function checkDomain(domain: string): Promise { + try { + const domainData: any = await resolveDomain(domain); + // eslint-disable-next-line @typescript-eslint/no-unsafe-member-access + return domainData.owner !== '0x0000000000000000000000000000000000000000'; + } catch (error) { + console.error('Error checking domain:', error); + return false; + } +} + +export async function registerDomain( + walletClient: WalletClient, + domainNames: string[], + ownerAddresses: string[], + tld: string, + referralAddress: string = zeroAddress, + credits = 0 +): Promise { + try { + if (domainNames.length !== ownerAddresses.length) + throw new Error('Domain names and owner addresses length mismatch'); + + const chainDetails = getChainDetailsByTLD(tld); + if (!chainDetails) { + throw new Error('Invalid TLD'); + } + const { registryAddress, chain } = chainDetails; + + const totalPriceObject = await getTotalPrice(domainNames, tld); + const totalPrice = totalPriceObject.totalPrice; + + for (let i = 0; i < domainNames.length; i++) { + const domain = `${domainNames[i]}.${tld}`; + const exists = await checkDomain(domain); + if (exists) { + throw new Error(`Domain ${domain} already exists`); + } + } + + const expiries: number[] = new Array(ownerAddresses.length).fill(1); + await walletClient.writeContract({ + account: walletClient.account, + chain: chain, + address: registryAddress as `0x${string}`, + abi: abi, + functionName: 'registerDomains', + value: totalPrice, + args: [ownerAddresses, domainNames, expiries, referralAddress, credits], + }); + } catch (error) { + console.error('An error occurred during domain registration:', error); + throw new Error(`Domain registration failed: ${error.message}`); + } +}