{"version":3,"file":"createSubdomain.cjs","sources":["../../../src/bindings/createSubdomain.ts"],"sourcesContent":["import {\r\n  Address,\r\n  GetAccountInfoApi,\r\n  GetMinimumBalanceForRentExemptionApi,\r\n  IInstruction,\r\n  Rpc,\r\n  fetchEncodedAccount,\r\n} from \"@solana/kit\";\r\n\r\nimport { getDomainAddress } from \"../domain/getDomainAddress\";\r\nimport { InvalidDomainError } from \"../errors\";\r\nimport { RegistryState } from \"../states/registry\";\r\nimport { getReverseAddress } from \"../utils/getReverseAddress\";\r\nimport { createNameRegistry } from \"./createNameRegistry\";\r\nimport { createReverse } from \"./createReverse\";\r\n\r\ninterface CreateSubdomainParams {\r\n  rpc: Rpc<GetAccountInfoApi & GetMinimumBalanceForRentExemptionApi>;\r\n  subdomain: string;\r\n  owner: Address;\r\n  space?: number;\r\n  feePayer?: Address;\r\n}\r\n\r\n/**\r\n * Creates a subdomain for the specified domain. This includes setting up the subdomain\r\n * registry and its reverse lookup record if not already existing.\r\n *\r\n * @param params - An object containing the following properties:\r\n *   - `rpc`: An RPC interface implementing GetAccountInfoApi and GetMinimumBalanceForRentExemptionApi.\r\n *   - `subdomain`: The subdomain to create, with or without .sol (e.g., something.sns.sol or something.sns).\r\n *   - `owner`: The address of the owner of the parent domain.\r\n *   - `space`: (Optional) The space in bytes allocated to the subdomain account (default: 2,000).\r\n *   - `feePayer`: (Optional) The address funding the subdomain creation (default: owner address).\r\n * @returns A promise that resolves to an array of instructions required to create the subdomain\r\n *   and its reverse lookup record.\r\n */\r\nexport const createSubdomain = async ({\r\n  rpc,\r\n  subdomain,\r\n  owner,\r\n  space = 2_000,\r\n  feePayer,\r\n}: CreateSubdomainParams): Promise<IInstruction[]> => {\r\n  const ixs: IInstruction[] = [];\r\n  const sub = subdomain.split(\".\")[0];\r\n\r\n  if (!sub) {\r\n    throw new InvalidDomainError(\"The subdomain name is malformed\");\r\n  }\r\n\r\n  const [{ domainAddress, parentAddress }, lamports] = await Promise.all([\r\n    getDomainAddress({ domain: subdomain }),\r\n    rpc\r\n      .getMinimumBalanceForRentExemption(\r\n        BigInt(space + RegistryState.HEADER_LEN)\r\n      )\r\n      .send(),\r\n  ]);\r\n\r\n  const ix_create = await createNameRegistry({\r\n    rpc,\r\n    name: \"\\0\".concat(sub),\r\n    space,\r\n    payer: feePayer || owner,\r\n    owner,\r\n    lamports,\r\n    classAddress: undefined,\r\n    parentAddress,\r\n  });\r\n  ixs.push(ix_create);\r\n\r\n  // Create the reverse name\r\n  const reverseKey = await getReverseAddress(subdomain);\r\n  const reverseAccount = await fetchEncodedAccount(rpc, reverseKey);\r\n\r\n  if (!reverseAccount.exists) {\r\n    const ix_reverse = await createReverse({\r\n      domainAddress,\r\n      domain: \"\\0\".concat(sub),\r\n      payer: feePayer || owner,\r\n      parentAddress,\r\n      parentOwner: owner,\r\n    });\r\n    ixs.push(ix_reverse);\r\n  }\r\n\r\n  return ixs;\r\n};\r\n"],"names":["async","rpc","subdomain","owner","space","feePayer","ixs","sub","split","InvalidDomainError","domainAddress","parentAddress","lamports","Promise","all","getDomainAddress","domain","getMinimumBalanceForRentExemption","BigInt","RegistryState","HEADER_LEN","send","ix_create","createNameRegistry","name","concat","payer","classAddress","undefined","push","reverseKey","getReverseAddress","fetchEncodedAccount","exists","ix_reverse","createReverse","parentOwner"],"mappings":"gSAqC+BA,OAC7BC,MACAC,YACAC,QACAC,QAAQ,IACRC,eAEA,MAAMC,EAAsB,GACtBC,EAAML,EAAUM,MAAM,KAAK,GAEjC,IAAKD,EACH,MAAM,IAAIE,EAAkBA,mBAAC,mCAG/B,OAAOC,cAAEA,EAAaC,cAAEA,GAAiBC,SAAkBC,QAAQC,IAAI,CACrEC,mBAAiB,CAAEC,OAAQd,IAC3BD,EACGgB,kCACCC,OAAOd,EAAQe,EAAAA,cAAcC,aAE9BC,SAGCC,QAAkBC,qBAAmB,CACzCtB,MACAuB,KAAM,KAAKC,OAAOlB,GAClBH,QACAsB,MAAOrB,GAAYF,EACnBA,QACAS,WACAe,kBAAcC,EACdjB,kBAEFL,EAAIuB,KAAKP,GAGT,MAAMQ,QAAmBC,EAAiBA,kBAAC7B,GAG3C,WAF6B8B,sBAAoB/B,EAAK6B,IAElCG,OAAQ,CAC1B,MAAMC,QAAmBC,gBAAc,CACrCzB,gBACAM,OAAQ,KAAKS,OAAOlB,GACpBmB,MAAOrB,GAAYF,EACnBQ,gBACAyB,YAAajC,IAEfG,EAAIuB,KAAKK,GAGX,OAAO5B,CAAG"}