UNPKG

@samudai_xyz/web3-sdk

Version:

## All in one web3 integrations for Samudai

121 lines (120 loc) 5.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.ClaimSubdomain = void 0; const ethers_1 = require("ethers"); const constants_1 = require("../utils/constants"); const Contract_ABI_1 = require("../contracts/Contract_ABI"); const namehash = require('@ensdomains/eth-ens-namehash'); const contentHash = require('content-hash'); const bundler_1 = require("@biconomy/bundler"); const account_1 = require("@biconomy/account"); const core_types_1 = require("@biconomy/core-types"); const paymaster_1 = require("@biconomy/paymaster"); const modules_1 = require("@biconomy/modules"); class ClaimSubdomain { constructor() { this.cid = ''; this.parentHash = namehash.hash(constants_1.ENS_DOMAIN_NAME); this.createSmartAccount = async () => { try { const module = await modules_1.ECDSAOwnershipValidationModule.create({ signer: this.wallet, moduleAddress: modules_1.DEFAULT_ECDSA_OWNERSHIP_MODULE, }); const biconomySmartAccount = await account_1.BiconomySmartAccountV2.create({ chainId: core_types_1.ChainId.MAINNET, bundler: this.bundler, paymaster: this.paymaster, entryPointAddress: account_1.DEFAULT_ENTRYPOINT_ADDRESS, defaultValidationModule: module, activeValidationModule: module, }); console.log('address: ', await biconomySmartAccount.getAccountAddress()); return biconomySmartAccount; } catch (error) { throw new Error('Error creating smart account.'); } }; this.setCID = (cid) => { try { this.cid = cid; } catch (error) { throw new Error('Error setting CID.'); } }; this.isSubdomainAvailable = async (subname) => { try { const subdomainName = subname + '.' + constants_1.ENS_DOMAIN_NAME; const subdomainHash = namehash.hash(subdomainName); const tx = await this.contractInstance.getData(subdomainHash); if (tx[0] === '0x0000000000000000000000000000000000000000') { return true; } else { return false; } } catch (error) { throw new Error('Error finding the availability of subdomain.'); } }; this.claimSubdomain = async (subname, ownerAddress) => { try { const isAvailable = await this.isSubdomainAvailable(subname); if (isAvailable === true && subname !== '') { const smartAccount = await this.createSmartAccount(); const cidHash = '0x' + contentHash.fromIpfs(this.cid); const fuses = constants_1.PARENT_CANNOT_CONTROL | constants_1.CANNOT_UNWRAP | constants_1.CANNOT_SET_RESOLVER | constants_1.CAN_EXTEND_EXPIRY; const minTx = await this.contractInstance.populateTransaction.createSubdomainWithContentHashV2(this.parentHash, subname, cidHash, fuses, constants_1.MAINNET.RESOLVER, ownerAddress); const tx = { to: constants_1.MAINNET.PROXY_CONTRACT_ADDRESS, data: minTx.data, }; const userOp = await smartAccount.buildUserOp([tx]); const biconomyPaymaster = smartAccount.paymaster; const paymasterServiceData = { mode: paymaster_1.PaymasterMode.SPONSORED, smartAccountInfo: { name: 'BICONOMY', version: '2.0.0', }, }; const paymasterAndDataResponse = await biconomyPaymaster.getPaymasterAndData(userOp, paymasterServiceData); userOp.paymasterAndData = paymasterAndDataResponse.paymasterAndData; const userOpResponse = await smartAccount.sendUserOp(userOp); const { receipt } = await userOpResponse.wait(1); return { transactionHash: receipt.transactionHash, success: true, }; } else { return { transactionHash: '', success: false, }; } } catch (error) { throw new Error('Error claiming the subdomain.'); } }; this.provider = new ethers_1.ethers.providers.JsonRpcProvider(constants_1.MAINNET.RPC_URL); this.wallet = new ethers_1.ethers.Wallet(constants_1.PVT_KEY, this.provider); this.contractInstance = new ethers_1.ethers.Contract(constants_1.MAINNET.PROXY_CONTRACT_ADDRESS, Contract_ABI_1.ImplementationContractABI, this.wallet); this.bundler = new bundler_1.Bundler({ bundlerUrl: constants_1.MAINNET.BUNDLER_URL, chainId: core_types_1.ChainId.MAINNET, entryPointAddress: account_1.DEFAULT_ENTRYPOINT_ADDRESS, }); this.paymaster = new paymaster_1.BiconomyPaymaster({ paymasterUrl: constants_1.MAINNET.PAYMASTER_URL, }); } } exports.ClaimSubdomain = ClaimSubdomain;