{"version":3,"file":"transfer-policy.mjs","names":["TransferPolicyStruct","TransferPolicyCapStruct"],"sources":["../../src/query/transfer-policy.ts"],"sourcesContent":["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport { isValidSuiAddress } from '@mysten/sui/utils';\n\nimport {\n\tTransferPolicyCap as TransferPolicyCapStruct,\n\tTransferPolicy as TransferPolicyStruct,\n} from '../contracts/0x2/transfer_policy.js';\nimport type { KioskCompatibleClient, TransferPolicy, TransferPolicyCap } from '../types/index.js';\nimport {\n\tTRANSFER_POLICY_CAP_TYPE,\n\tTRANSFER_POLICY_CREATED_EVENT,\n\tTRANSFER_POLICY_TYPE,\n} from '../types/index.js';\nimport { queryEvents } from './client-utils.js';\n\n/**\n * Searches the `TransferPolicy`-s for the given type. The search is performed via\n * the `TransferPolicyCreated` event. The policy can either be owned or shared,\n * and the caller needs to filter the results accordingly (ie single owner can not\n * be accessed by anyone but the owner).\n *\n * @param client - A Sui client with the Core API\n * @param type - The type of the asset (e.g., \"0x123::nft::NFT\")\n */\nexport async function queryTransferPolicy(\n\tclient: KioskCompatibleClient,\n\ttype: string,\n): Promise<TransferPolicy[]> {\n\tconst data = await queryEvents(client, `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`);\n\n\tconst search = data.map((event) => event.json as { id: string });\n\n\tconst { objects } = await client.core.getObjects({\n\t\tobjectIds: search.map((policy: { id: string }) => policy.id),\n\t\tinclude: { content: true },\n\t});\n\n\treturn objects\n\t\t.filter((obj) => !(obj instanceof Error) && obj.content)\n\t\t.map((obj) => {\n\t\t\tif (obj instanceof Error) {\n\t\t\t\tthrow obj;\n\t\t\t}\n\n\t\t\tif (!obj.content) {\n\t\t\t\tthrow new Error(`Invalid policy: ${obj.objectId}, expected object with content`);\n\t\t\t}\n\n\t\t\tconst parsed = TransferPolicyStruct.parse(obj.content);\n\n\t\t\treturn {\n\t\t\t\tid: obj.objectId,\n\t\t\t\ttype: `${TRANSFER_POLICY_TYPE}<${type}>`,\n\t\t\t\towner: obj.owner,\n\t\t\t\trules: parsed.rules.contents.map((rule) => rule.name),\n\t\t\t\tbalance: parsed.balance.value.toString(),\n\t\t\t} as TransferPolicy;\n\t\t});\n}\n\n/**\n * Fetches all TransferPolicyCap objects owned by an address for a specific type.\n *\n * @param client - The Sui client\n * @param address - The owner address\n * @param type - The type of the asset\n * @returns Array of TransferPolicyCap objects\n */\nexport async function queryTransferPolicyCapsByType(\n\tclient: KioskCompatibleClient,\n\taddress: string,\n\ttype: string,\n): Promise<TransferPolicyCap[]> {\n\tif (!isValidSuiAddress(address)) return [];\n\n\tconst policies = [];\n\tlet hasNextPage = true;\n\tlet cursor: string | null = null;\n\n\twhile (hasNextPage) {\n\t\tconst result: Awaited<ReturnType<typeof client.core.listOwnedObjects>> =\n\t\t\tawait client.core.listOwnedObjects({\n\t\t\t\towner: address,\n\t\t\t\ttype: `${TRANSFER_POLICY_CAP_TYPE}<${type}>`,\n\t\t\t\tcursor,\n\t\t\t\tlimit: 50,\n\t\t\t\tinclude: {\n\t\t\t\t\tcontent: true,\n\t\t\t\t},\n\t\t\t});\n\n\t\tfor (const obj of result.objects) {\n\t\t\tif (obj.content) {\n\t\t\t\tpolicies.push({\n\t\t\t\t\tpolicyId: TransferPolicyCapStruct.parse(obj.content).policy_id,\n\t\t\t\t\tpolicyCapId: obj.objectId,\n\t\t\t\t\ttype,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\thasNextPage = result.hasNextPage;\n\t\tcursor = result.cursor;\n\t}\n\n\treturn policies;\n}\n\n/**\n * Fetches all TransferPolicyCap objects owned by an address (all types).\n *\n * Uses struct-level filtering (without type parameters) to efficiently query\n * only TransferPolicyCap objects. This matches all generic instantiations\n * (e.g., TransferPolicyCap<T> for any T) and is supported natively by all clients.\n *\n * @param client - The Sui client\n * @param address - The owner address\n * @returns Array of TransferPolicyCap objects or undefined if address is invalid\n */\nexport async function queryOwnedTransferPolicies(\n\tclient: KioskCompatibleClient,\n\taddress: string,\n): Promise<TransferPolicyCap[] | undefined> {\n\tif (!isValidSuiAddress(address)) return;\n\n\tlet hasNextPage = true;\n\tlet cursor: string | null = null;\n\tconst policies: TransferPolicyCap[] = [];\n\n\twhile (hasNextPage) {\n\t\tconst result: Awaited<ReturnType<typeof client.core.listOwnedObjects>> =\n\t\t\tawait client.core.listOwnedObjects({\n\t\t\t\towner: address,\n\t\t\t\ttype: TRANSFER_POLICY_CAP_TYPE,\n\t\t\t\tcursor,\n\t\t\t\tlimit: 50,\n\t\t\t\tinclude: {\n\t\t\t\t\tcontent: true,\n\t\t\t\t},\n\t\t\t});\n\n\t\t// All results are TransferPolicyCap objects, extract the type parameter\n\t\tfor (const obj of result.objects) {\n\t\t\tif (obj.content) {\n\t\t\t\t// Extract the type parameter T from \"0x2::transfer_policy::TransferPolicyCap<T>\"\n\t\t\t\tconst objectType = obj.type!.replace(`${TRANSFER_POLICY_CAP_TYPE}<`, '').slice(0, -1);\n\n\t\t\t\tpolicies.push({\n\t\t\t\t\tpolicyId: TransferPolicyCapStruct.parse(obj.content).policy_id,\n\t\t\t\t\tpolicyCapId: obj.objectId,\n\t\t\t\t\ttype: objectType,\n\t\t\t\t});\n\t\t\t}\n\t\t}\n\n\t\thasNextPage = result.hasNextPage;\n\t\tcursor = result.cursor;\n\t}\n\n\treturn policies;\n}\n"],"mappings":";;;;;;;;;;;;;;;AA0BA,eAAsB,oBACrB,QACA,MAC4B;CAG5B,MAAM,UAFO,MAAM,YAAY,QAAQ,GAAG,8BAA8B,GAAG,KAAK,GAAG,EAE/D,KAAK,UAAU,MAAM,KAAuB;CAEhE,MAAM,EAAE,YAAY,MAAM,OAAO,KAAK,WAAW;EAChD,WAAW,OAAO,KAAK,WAA2B,OAAO,GAAG;EAC5D,SAAS,EAAE,SAAS,MAAM;EAC1B,CAAC;AAEF,QAAO,QACL,QAAQ,QAAQ,EAAE,eAAe,UAAU,IAAI,QAAQ,CACvD,KAAK,QAAQ;AACb,MAAI,eAAe,M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