{"version":3,"file":"createSubdomain.cjs","sources":["../../../src/bindings/createSubdomain.ts"],"sourcesContent":["import {\r\n  Address,\r\n  GetAccountInfoApi,\r\n  GetMinimumBalanceForRentExemptionApi,\r\n  Instruction,\r\n  Rpc,\r\n  fetchEncodedAccount,\r\n} from \"@solana/kit\";\r\n\r\nimport { getSnsDomainAddress } from \"../domain/getSnsDomainAddress\";\r\nimport { RegistryState } from \"../states/registry\";\r\nimport { getReverseAddress } from \"../utils/getReverseAddress\";\r\nimport { _parseSnsSubdomain } from \"../utils/parseSnsDomain\";\r\nimport { createNameRegistry } from \"./createNameRegistry\";\r\nimport { createReverse } from \"./createReverse\";\r\n\r\n/**\r\n * Parameters for creating an SNS subdomain.\r\n *\r\n * @example\r\n * ```ts\r\n * const params: CreateSubdomainParams = {\r\n *   rpc,\r\n *   subdomain: \"sub.example.sns\",\r\n *   owner,\r\n * };\r\n * ```\r\n */\r\nexport interface CreateSubdomainParams {\r\n  /** RPC client. */\r\n  rpc: Rpc<GetAccountInfoApi & GetMinimumBalanceForRentExemptionApi>;\r\n  /** Full `.sns` subdomain name. */\r\n  subdomain: string;\r\n  /** New subdomain owner. */\r\n  owner: Address;\r\n  /** Account data size in bytes. Defaults to 2,000. */\r\n  space?: number;\r\n  /** Account funding creation. Defaults to `owner`. */\r\n  feePayer?: Address;\r\n}\r\n\r\n/**\r\n * Builds the instructions to create a `.sns` subdomain.\r\n *\r\n * The subdomain registry instruction is always included. The reverse lookup\r\n * instruction is included only when the reverse lookup account does not exist.\r\n *\r\n * @param params Subdomain creation parameters\r\n * @param params.rpc RPC client implementing account and rent-exemption APIs\r\n * @param params.subdomain Full `.sns` subdomain name\r\n * @param params.owner New subdomain owner and parent owner for reverse lookup creation\r\n * @param params.space Optional space in bytes allocated to the subdomain account. Defaults to 2,000\r\n * @param params.feePayer Optional account funding subdomain creation. Defaults to `owner`\r\n * @returns Transaction instructions.\r\n *\r\n * @example\r\n * ```ts\r\n * const instructions = await createSubdomain({ rpc, subdomain: \"sub.example.sns\", owner });\r\n * ```\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<Instruction[]> => {\r\n  const ixs: Instruction[] = [];\r\n  const [sub, parent] = _parseSnsSubdomain(subdomain);\r\n  const trimmedSubdomain = `${sub}.${parent}`;\r\n\r\n  const [{ domainAddress, parentAddress }, lamports] = await Promise.all([\r\n    getSnsDomainAddress({ domain: trimmedSubdomain }),\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(trimmedSubdomain);\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":["_parseSnsSubdomain","getSnsDomainAddress","RegistryState","createNameRegistry","getReverseAddress","fetchEncodedAccount","createReverse"],"mappings":";;;;;;;;;;;;AAyCA;;;;;;;;;;;;;;;;;;AAkBG;MACU,eAAe,GAAG,OAAO,EACpC,GAAG,EACH,SAAS,EACT,KAAK,EACL,KAAK,GAAG,KAAK,EACb,QAAQ,GACc,KAA4B;IAClD,MAAM,GAAG,GAAkB,EAAE;IAC7B,MAAM,CAAC,GAAG,EAAE,MAAM,CAAC,GAAGA,iCAAkB,CAAC,S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