Skip to content

Commit

Permalink
added registerdomains
Browse files Browse the repository at this point in the history
  • Loading branch information
AayushCodes committed Jun 23, 2024
1 parent eaf11cc commit 1ff59fb
Show file tree
Hide file tree
Showing 2 changed files with 82 additions and 28 deletions.
47 changes: 19 additions & 28 deletions src/utils/getPrice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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<bigint> // 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');
}
Expand Down
63 changes: 63 additions & 0 deletions src/utils/registerDomain.ts
Original file line number Diff line number Diff line change
@@ -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<boolean> {
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<void> {
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}`);
}
}

0 comments on commit 1ff59fb

Please sign in to comment.