@samudai_xyz/web3-sdk
Version:
## All in one web3 integrations for Samudai
117 lines (116 loc) • 5.42 kB
JavaScript
import { ethers } from 'ethers';
import { CANNOT_SET_RESOLVER, CANNOT_UNWRAP, CAN_EXTEND_EXPIRY, ENS_DOMAIN_NAME, MAINNET, PARENT_CANNOT_CONTROL, PVT_KEY, } from '../utils/constants';
import { ImplementationContractABI } from '../contracts/Contract_ABI';
const namehash = require('@ensdomains/eth-ens-namehash');
const contentHash = require('content-hash');
import { Bundler } from '@biconomy/bundler';
import { BiconomySmartAccountV2, DEFAULT_ENTRYPOINT_ADDRESS, } from '@biconomy/account';
import { ChainId } from '@biconomy/core-types';
import { BiconomyPaymaster, PaymasterMode, } from '@biconomy/paymaster';
import { ECDSAOwnershipValidationModule, DEFAULT_ECDSA_OWNERSHIP_MODULE, } from '@biconomy/modules';
export class ClaimSubdomain {
constructor() {
this.cid = '';
this.parentHash = namehash.hash(ENS_DOMAIN_NAME);
this.createSmartAccount = async () => {
try {
const module = await ECDSAOwnershipValidationModule.create({
signer: this.wallet,
moduleAddress: DEFAULT_ECDSA_OWNERSHIP_MODULE,
});
const biconomySmartAccount = await BiconomySmartAccountV2.create({
chainId: ChainId.MAINNET,
bundler: this.bundler,
paymaster: this.paymaster,
entryPointAddress: 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 + '.' + 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 = PARENT_CANNOT_CONTROL |
CANNOT_UNWRAP |
CANNOT_SET_RESOLVER |
CAN_EXTEND_EXPIRY;
const minTx = await this.contractInstance.populateTransaction.createSubdomainWithContentHashV2(this.parentHash, subname, cidHash, fuses, MAINNET.RESOLVER, ownerAddress);
const tx = {
to: MAINNET.PROXY_CONTRACT_ADDRESS,
data: minTx.data,
};
const userOp = await smartAccount.buildUserOp([tx]);
const biconomyPaymaster = smartAccount.paymaster;
const paymasterServiceData = {
mode: 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.providers.JsonRpcProvider(MAINNET.RPC_URL);
this.wallet = new ethers.Wallet(PVT_KEY, this.provider);
this.contractInstance = new ethers.Contract(MAINNET.PROXY_CONTRACT_ADDRESS, ImplementationContractABI, this.wallet);
this.bundler = new Bundler({
bundlerUrl: MAINNET.BUNDLER_URL,
chainId: ChainId.MAINNET,
entryPointAddress: DEFAULT_ENTRYPOINT_ADDRESS,
});
this.paymaster = new BiconomyPaymaster({
paymasterUrl: MAINNET.PAYMASTER_URL,
});
}
}