@iota/kiosk 0.0.0-dev-20251015065834

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (127) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/LICENSE +201 -0
  3. package/README.md +39 -0
  4. package/dist/cjs/bcs.d.ts +38 -0
  5. package/dist/cjs/bcs.js +49 -0
  6. package/dist/cjs/bcs.js.map +7 -0
  7. package/dist/cjs/client/kiosk-client.d.ts +76 -0
  8. package/dist/cjs/client/kiosk-client.js +126 -0
  9. package/dist/cjs/client/kiosk-client.js.map +7 -0
  10. package/dist/cjs/client/kiosk-transaction.d.ts +185 -0
  11. package/dist/cjs/client/kiosk-transaction.js +473 -0
  12. package/dist/cjs/client/kiosk-transaction.js.map +7 -0
  13. package/dist/cjs/client/tp-transaction.d.ts +114 -0
  14. package/dist/cjs/client/tp-transaction.js +312 -0
  15. package/dist/cjs/client/tp-transaction.js.map +7 -0
  16. package/dist/cjs/constants.d.ts +24 -0
  17. package/dist/cjs/constants.js +66 -0
  18. package/dist/cjs/constants.js.map +7 -0
  19. package/dist/cjs/index.d.ts +6 -0
  20. package/dist/cjs/index.js +24 -0
  21. package/dist/cjs/index.js.map +7 -0
  22. package/dist/cjs/package.json +5 -0
  23. package/dist/cjs/query/kiosk.d.ts +14 -0
  24. package/dist/cjs/query/kiosk.js +129 -0
  25. package/dist/cjs/query/kiosk.js.map +7 -0
  26. package/dist/cjs/query/transfer-policy.d.ts +29 -0
  27. package/dist/cjs/query/transfer-policy.js +94 -0
  28. package/dist/cjs/query/transfer-policy.js.map +7 -0
  29. package/dist/cjs/tx/kiosk.d.ts +71 -0
  30. package/dist/cjs/tx/kiosk.js +130 -0
  31. package/dist/cjs/tx/kiosk.js.map +7 -0
  32. package/dist/cjs/tx/personal-kiosk.d.ts +7 -0
  33. package/dist/cjs/tx/personal-kiosk.js +38 -0
  34. package/dist/cjs/tx/personal-kiosk.js.map +7 -0
  35. package/dist/cjs/tx/rules/attach.d.ts +7 -0
  36. package/dist/cjs/tx/rules/attach.js +62 -0
  37. package/dist/cjs/tx/rules/attach.js.map +7 -0
  38. package/dist/cjs/tx/rules/resolve.d.ts +15 -0
  39. package/dist/cjs/tx/rules/resolve.js +79 -0
  40. package/dist/cjs/tx/rules/resolve.js.map +7 -0
  41. package/dist/cjs/tx/transfer-policy.d.ts +29 -0
  42. package/dist/cjs/tx/transfer-policy.js +78 -0
  43. package/dist/cjs/tx/transfer-policy.js.map +7 -0
  44. package/dist/cjs/types/index.d.ts +17 -0
  45. package/dist/cjs/types/index.js +20 -0
  46. package/dist/cjs/types/index.js.map +7 -0
  47. package/dist/cjs/types/kiosk.d.ts +159 -0
  48. package/dist/cjs/types/kiosk.js +37 -0
  49. package/dist/cjs/types/kiosk.js.map +7 -0
  50. package/dist/cjs/types/transfer-policy.d.ts +51 -0
  51. package/dist/cjs/types/transfer-policy.js +35 -0
  52. package/dist/cjs/types/transfer-policy.js.map +7 -0
  53. package/dist/cjs/utils.d.ts +51 -0
  54. package/dist/cjs/utils.js +201 -0
  55. package/dist/cjs/utils.js.map +7 -0
  56. package/dist/esm/bcs.d.ts +38 -0
  57. package/dist/esm/bcs.js +34 -0
  58. package/dist/esm/bcs.js.map +7 -0
  59. package/dist/esm/client/kiosk-client.d.ts +76 -0
  60. package/dist/esm/client/kiosk-client.js +110 -0
  61. package/dist/esm/client/kiosk-client.js.map +7 -0
  62. package/dist/esm/client/kiosk-transaction.d.ts +185 -0
  63. package/dist/esm/client/kiosk-transaction.js +443 -0
  64. package/dist/esm/client/kiosk-transaction.js.map +7 -0
  65. package/dist/esm/client/tp-transaction.d.ts +114 -0
  66. package/dist/esm/client/tp-transaction.js +303 -0
  67. package/dist/esm/client/tp-transaction.js.map +7 -0
  68. package/dist/esm/constants.d.ts +24 -0
  69. package/dist/esm/constants.js +51 -0
  70. package/dist/esm/constants.js.map +7 -0
  71. package/dist/esm/index.d.ts +6 -0
  72. package/dist/esm/index.js +7 -0
  73. package/dist/esm/index.js.map +7 -0
  74. package/dist/esm/package.json +5 -0
  75. package/dist/esm/query/kiosk.d.ts +14 -0
  76. package/dist/esm/query/kiosk.js +117 -0
  77. package/dist/esm/query/kiosk.js.map +7 -0
  78. package/dist/esm/query/transfer-policy.d.ts +29 -0
  79. package/dist/esm/query/transfer-policy.js +78 -0
  80. package/dist/esm/query/transfer-policy.js.map +7 -0
  81. package/dist/esm/tx/kiosk.d.ts +71 -0
  82. package/dist/esm/tx/kiosk.js +110 -0
  83. package/dist/esm/tx/kiosk.js.map +7 -0
  84. package/dist/esm/tx/personal-kiosk.d.ts +7 -0
  85. package/dist/esm/tx/personal-kiosk.js +18 -0
  86. package/dist/esm/tx/personal-kiosk.js.map +7 -0
  87. package/dist/esm/tx/rules/attach.d.ts +7 -0
  88. package/dist/esm/tx/rules/attach.js +42 -0
  89. package/dist/esm/tx/rules/attach.js.map +7 -0
  90. package/dist/esm/tx/rules/resolve.d.ts +15 -0
  91. package/dist/esm/tx/rules/resolve.js +59 -0
  92. package/dist/esm/tx/rules/resolve.js.map +7 -0
  93. package/dist/esm/tx/transfer-policy.d.ts +29 -0
  94. package/dist/esm/tx/transfer-policy.js +58 -0
  95. package/dist/esm/tx/transfer-policy.js.map +7 -0
  96. package/dist/esm/types/index.d.ts +17 -0
  97. package/dist/esm/types/index.js +3 -0
  98. package/dist/esm/types/index.js.map +7 -0
  99. package/dist/esm/types/kiosk.d.ts +159 -0
  100. package/dist/esm/types/kiosk.js +17 -0
  101. package/dist/esm/types/kiosk.js.map +7 -0
  102. package/dist/esm/types/transfer-policy.d.ts +51 -0
  103. package/dist/esm/types/transfer-policy.js +15 -0
  104. package/dist/esm/types/transfer-policy.js.map +7 -0
  105. package/dist/esm/utils.d.ts +51 -0
  106. package/dist/esm/utils.js +186 -0
  107. package/dist/esm/utils.js.map +7 -0
  108. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  109. package/dist/tsconfig.tsbuildinfo +1 -0
  110. package/package.json +53 -0
  111. package/src/bcs.ts +39 -0
  112. package/src/client/kiosk-client.ts +169 -0
  113. package/src/client/kiosk-transaction.ts +538 -0
  114. package/src/client/tp-transaction.ts +363 -0
  115. package/src/constants.ts +85 -0
  116. package/src/index.ts +10 -0
  117. package/src/query/kiosk.ts +193 -0
  118. package/src/query/transfer-policy.ts +137 -0
  119. package/src/tx/kiosk.ts +244 -0
  120. package/src/tx/personal-kiosk.ts +35 -0
  121. package/src/tx/rules/attach.ts +74 -0
  122. package/src/tx/rules/resolve.ts +87 -0
  123. package/src/tx/transfer-policy.ts +121 -0
  124. package/src/types/index.ts +25 -0
  125. package/src/types/kiosk.ts +178 -0
  126. package/src/types/transfer-policy.ts +70 -0
  127. package/src/utils.ts +289 -0
@@ -0,0 +1,25 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // Modifications Copyright (c) 2024 IOTA Stiftung
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ import type { IotaClient, NetworkId } from '@iota/iota-sdk/client';
6
+ import type { TransactionObjectArgument } from '@iota/iota-sdk/transactions';
7
+
8
+ import type { BaseRulePackageIds } from '../constants.js';
9
+
10
+ export * from './kiosk.js';
11
+ export * from './transfer-policy.js';
12
+
13
+ /**
14
+ * A valid argument for any of the Kiosk functions.
15
+ */
16
+ export type ObjectArgument = string | TransactionObjectArgument;
17
+
18
+ /**
19
+ * The Client Options for Both KioskClient & TransferPolicyManager.
20
+ */
21
+ export type KioskClientOptions = {
22
+ client: IotaClient;
23
+ network: NetworkId;
24
+ packageIds?: BaseRulePackageIds;
25
+ };
@@ -0,0 +1,178 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // Modifications Copyright (c) 2024 IOTA Stiftung
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ import type {
6
+ PaginatedObjectsResponse,
7
+ IotaObjectData,
8
+ IotaObjectDataOptions,
9
+ } from '@iota/iota-sdk/client';
10
+ import type { TransactionArgument } from '@iota/iota-sdk/transactions';
11
+
12
+ import type { ObjectArgument } from './index.js';
13
+
14
+ /** The Kiosk module. */
15
+ export const KIOSK_MODULE = '0x2::kiosk';
16
+
17
+ /** The Kiosk type. */
18
+ export const KIOSK_TYPE = `${KIOSK_MODULE}::Kiosk`;
19
+
20
+ /** The Kiosk Owner Cap Type */
21
+ export const KIOSK_OWNER_CAP = `${KIOSK_MODULE}::KioskOwnerCap`;
22
+
23
+ /** The Kiosk Item Type */
24
+ export const KIOSK_ITEM = `${KIOSK_MODULE}::Item`;
25
+
26
+ /** The Kiosk Listing Type */
27
+ export const KIOSK_LISTING = `${KIOSK_MODULE}::Listing`;
28
+
29
+ /** The Kiosk Lock Type */
30
+ export const KIOSK_LOCK = `${KIOSK_MODULE}::Lock`;
31
+
32
+ /** The Kiosk PurchaseCap type */
33
+ export const KIOSK_PURCHASE_CAP = `${KIOSK_MODULE}::PurchaseCap`;
34
+
35
+ /**
36
+ * The Kiosk object fields (for BCS queries).
37
+ */
38
+ export type Kiosk = {
39
+ id: string;
40
+ profits: string;
41
+ owner: string;
42
+ itemCount: number;
43
+ };
44
+
45
+ /**
46
+ * PurchaseCap object fields (for BCS queries).
47
+ */
48
+ export type PurchaseCap = {
49
+ id: string;
50
+ kioskId: string;
51
+ itemId: string;
52
+ minPrice: string;
53
+ };
54
+
55
+ /**
56
+ * The response type of a successful purchase flow.
57
+ * Returns the item, and a `canTransfer` param.
58
+ */
59
+ export type PurchaseAndResolvePoliciesResponse = {
60
+ item: TransactionArgument;
61
+ canTransfer: boolean;
62
+ };
63
+
64
+ /**
65
+ * Optional parameters for `purchaseAndResolvePolicies` flow.
66
+ * This gives us the chance to extend the function in further releases
67
+ * without introducing more breaking changes.
68
+ */
69
+ export type PurchaseOptionalParams = {
70
+ ownedKiosk?: ObjectArgument;
71
+ ownedKioskCap?: ObjectArgument;
72
+ };
73
+
74
+ /**
75
+ * A dynamic field `Listing { ID, isExclusive }` attached to the Kiosk.
76
+ * Holds a `u64` value - the price of the item.
77
+ */
78
+ export type KioskListing = {
79
+ /** The ID of the Item */
80
+ objectId: string;
81
+ /**
82
+ * Whether or not there's a `PurchaseCap` issued. `true` means that
83
+ * the listing is controlled by some logic and can't be purchased directly.
84
+ *
85
+ * TODO: consider renaming the field for better indication.
86
+ */
87
+ isExclusive: boolean;
88
+ /** The ID of the listing */
89
+ listingId: string;
90
+ price?: string;
91
+ };
92
+
93
+ /**
94
+ * A dynamic field `Item { ID }` attached to the Kiosk.
95
+ * Holds an Item `T`. The type of the item is known upfront.
96
+ */
97
+ export type KioskItem = {
98
+ /** The ID of the Item */
99
+ objectId: string;
100
+ /** The type of the Item */
101
+ type: string;
102
+ /** Whether the item is Locked (there must be a `Lock` Dynamic Field) */
103
+ isLocked: boolean;
104
+ /** Optional listing */
105
+ listing?: KioskListing;
106
+ /** The ID of the kiosk the item is placed in */
107
+ kioskId: string;
108
+ /** Optional Kiosk Data */
109
+ data?: IotaObjectData;
110
+ };
111
+
112
+ /** The overview type returned from `getKiosk` */
113
+ export type KioskExtensionOverview = {
114
+ /** The ID of the extension's DF */
115
+ objectId: string;
116
+ /** The inner type of the Extension */
117
+ type: string;
118
+ };
119
+ /**
120
+ * Hold the KioskExtension data
121
+ */
122
+ export type KioskExtension = KioskExtensionOverview & {
123
+ /** These fields are only there if we have `withExtensions` flag */
124
+ isEnabled: boolean;
125
+ permissions: string;
126
+ storageId: string;
127
+ storageSize: number;
128
+ };
129
+
130
+ /**
131
+ * Aggregated data from the Kiosk.
132
+ */
133
+ export type KioskData = {
134
+ items: KioskItem[];
135
+ itemIds: string[];
136
+ listingIds: string[];
137
+ kiosk?: Kiosk;
138
+ extensions: KioskExtensionOverview[]; // type will be defined on later versions of the SDK.
139
+ };
140
+
141
+ export type PagedKioskData = {
142
+ data: KioskData;
143
+ nextCursor: string | null | undefined;
144
+ hasNextPage: boolean;
145
+ };
146
+
147
+ export type FetchKioskOptions = {
148
+ /** Include the base kiosk object, which includes the profits, the owner and the base fields. */
149
+ withKioskFields?: boolean;
150
+ /** Include the listing prices. */
151
+ withListingPrices?: boolean;
152
+ /** Include the objects for the Items in the kiosk. Defaults to `display` only. */
153
+ withObjects?: boolean;
154
+ /** Pass the data options for the objects, when fetching, in case you want to query other details. */
155
+ objectOptions?: IotaObjectDataOptions;
156
+ };
157
+
158
+ export type OwnedKiosks = {
159
+ kioskOwnerCaps: KioskOwnerCap[];
160
+ kioskIds: string[];
161
+ } & Omit<PaginatedObjectsResponse, 'data'>;
162
+
163
+ export type KioskOwnerCap = {
164
+ isPersonal?: boolean;
165
+ objectId: string;
166
+ kioskId: string;
167
+ digest: string;
168
+ version: string;
169
+ };
170
+
171
+ export type PurchaseOptions = {
172
+ extraArgs?: Record<string, any>;
173
+ };
174
+
175
+ export type ItemId = { itemType: string; itemId: string };
176
+ export type ItemReference = { itemType: string; item: ObjectArgument };
177
+ export type ItemValue = { itemType: string; item: TransactionArgument };
178
+ export type Price = { price: string | bigint };
@@ -0,0 +1,70 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // Modifications Copyright (c) 2024 IOTA Stiftung
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ import type { ObjectOwner } from '@iota/iota-sdk/client';
6
+ import type { Transaction, TransactionObjectArgument } from '@iota/iota-sdk/transactions';
7
+
8
+ import type { ObjectArgument } from './index.js';
9
+
10
+ /** The Transfer Policy module. */
11
+ export const TRANSFER_POLICY_MODULE = '0x2::transfer_policy';
12
+
13
+ /** Name of the event emitted when a TransferPolicy for T is created. */
14
+ export const TRANSFER_POLICY_CREATED_EVENT = `${TRANSFER_POLICY_MODULE}::TransferPolicyCreated`;
15
+
16
+ /** The Transfer Policy Type */
17
+ export const TRANSFER_POLICY_TYPE = `${TRANSFER_POLICY_MODULE}::TransferPolicy`;
18
+
19
+ /** The Transfer Policy Cap Type */
20
+ export const TRANSFER_POLICY_CAP_TYPE = `${TRANSFER_POLICY_MODULE}::TransferPolicyCap`;
21
+
22
+ /** The Kiosk Lock Rule */
23
+ export const KIOSK_LOCK_RULE = 'kiosk_lock_rule::Rule';
24
+
25
+ /** The Royalty rule */
26
+ export const ROYALTY_RULE = 'royalty_rule::Rule';
27
+
28
+ /**
29
+ * The Transfer Policy Cap in a consumable way.
30
+ */
31
+ export type TransferPolicyCap = {
32
+ policyId: string;
33
+ policyCapId: string;
34
+ type: string;
35
+ };
36
+
37
+ /** The `TransferPolicy` object */
38
+ export type TransferPolicy = {
39
+ id: string;
40
+ type: string;
41
+ balance: string;
42
+ rules: string[];
43
+ owner: ObjectOwner;
44
+ };
45
+
46
+ /** Event emitted when a TransferPolicy is created. */
47
+ export type TransferPolicyCreated = {
48
+ id: string;
49
+ };
50
+
51
+ // The object a Rule resolving function accepts
52
+ // It can accept a set of fixed fields, that are part of every purchase flow as well any extra arguments to resolve custom policies!
53
+ // Each rule resolving function should check that the key it's seeking is in the object
54
+ // e.g. `if(!'my_key' in ruleParams!) throw new Error("Can't resolve that rule!")`
55
+ export type RuleResolvingParams = {
56
+ transaction: Transaction;
57
+ /** @deprecated use transaction instead */
58
+ transactionBlock: Transaction;
59
+ itemType: string;
60
+ itemId: string;
61
+ price: string;
62
+ policyId: ObjectArgument;
63
+ sellerKiosk: ObjectArgument;
64
+ kiosk: ObjectArgument;
65
+ kioskCap: ObjectArgument;
66
+ transferRequest: TransactionObjectArgument;
67
+ purchasedItem: TransactionObjectArgument;
68
+ packageId: string;
69
+ extraArgs: Record<string, any>; // extraParams contains more possible {key, values} to pass for custom rules.
70
+ };
package/src/utils.ts ADDED
@@ -0,0 +1,289 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // Modifications Copyright (c) 2024 IOTA Stiftung
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ import type {
6
+ DynamicFieldInfo,
7
+ PaginationArguments,
8
+ IotaClient,
9
+ IotaObjectData,
10
+ IotaObjectDataFilter,
11
+ IotaObjectDataOptions,
12
+ IotaObjectResponse,
13
+ } from '@iota/iota-sdk/client';
14
+ import {
15
+ fromBase64,
16
+ normalizeStructTag,
17
+ normalizeIotaAddress,
18
+ parseStructTag,
19
+ } from '@iota/iota-sdk/utils';
20
+
21
+ import { KioskType } from './bcs.js';
22
+ import type { Kiosk, KioskData, KioskListing, TransferPolicyCap } from './types/index.js';
23
+ import { TRANSFER_POLICY_CAP_TYPE } from './types/index.js';
24
+
25
+ const DEFAULT_QUERY_LIMIT = 50;
26
+
27
+ export async function getKioskObject(client: IotaClient, id: string): Promise<Kiosk> {
28
+ const queryRes = await client.getObject({ id, options: { showBcs: true } });
29
+
30
+ if (!queryRes || queryRes.error || !queryRes.data) {
31
+ throw new Error(`Kiosk ${id} not found; ${queryRes.error}`);
32
+ }
33
+
34
+ if (!queryRes.data.bcs || !('bcsBytes' in queryRes.data.bcs)) {
35
+ throw new Error(`Invalid kiosk query: ${id}, expected object, got package`);
36
+ }
37
+
38
+ return KioskType.parse(fromBase64(queryRes.data.bcs!.bcsBytes));
39
+ }
40
+
41
+ // helper to extract kiosk data from dynamic fields.
42
+ export function extractKioskData(
43
+ data: DynamicFieldInfo[],
44
+ listings: KioskListing[],
45
+ lockedItemIds: string[],
46
+ kioskId: string,
47
+ ): KioskData {
48
+ return data.reduce<KioskData>(
49
+ (acc: KioskData, val: DynamicFieldInfo) => {
50
+ const type = val.name.type;
51
+
52
+ if (type.startsWith('0x2::kiosk::Item')) {
53
+ acc.itemIds.push(val.objectId);
54
+ acc.items.push({
55
+ objectId: val.objectId,
56
+ type: val.objectType,
57
+ isLocked: false,
58
+ kioskId,
59
+ });
60
+ }
61
+ if (type.startsWith('0x2::kiosk::Listing')) {
62
+ acc.listingIds.push(val.objectId);
63
+ listings.push({
64
+ objectId: (val.name.value as { id: string }).id,
65
+ listingId: val.objectId,
66
+ isExclusive: (val.name.value as { is_exclusive: boolean }).is_exclusive,
67
+ });
68
+ }
69
+ if (type.startsWith('0x2::kiosk::Lock')) {
70
+ lockedItemIds?.push((val.name.value as { id: string }).id);
71
+ }
72
+
73
+ if (type.startsWith('0x2::kiosk_extension::ExtensionKey')) {
74
+ acc.extensions.push({
75
+ objectId: val.objectId,
76
+ type: normalizeStructTag(parseStructTag(val.name.type).typeParams[0]),
77
+ });
78
+ }
79
+
80
+ return acc;
81
+ },
82
+ { items: [], itemIds: [], listingIds: [], extensions: [] },
83
+ );
84
+ }
85
+
86
+ /**
87
+ * A helper that attaches the listing prices to kiosk listings.
88
+ */
89
+ export function attachListingsAndPrices(
90
+ kioskData: KioskData,
91
+ listings: KioskListing[],
92
+ listingObjects: IotaObjectResponse[],
93
+ ) {
94
+ // map item listings as {item_id: KioskListing}
95
+ // for easier mapping on the nex
96
+ const itemListings = listings.reduce<Record<string, KioskListing>>(
97
+ (acc: Record<string, KioskListing>, item, idx) => {
98
+ acc[item.objectId] = { ...item };
99
+
100
+ // return in case we don't have any listing objects.
101
+ // that's the case when we don't have the `listingPrices` included.
102
+ if (listingObjects.length === 0) return acc;
103
+
104
+ const content = listingObjects[idx].data?.content;
105
+ const data = content?.dataType === 'moveObject' ? content?.fields : null;
106
+
107
+ if (!data) return acc;
108
+
109
+ acc[item.objectId].price = (data as { value: string }).value;
110
+ return acc;
111
+ },
112
+ {},
113
+ );
114
+
115
+ kioskData.items.forEach((item) => {
116
+ item.listing = itemListings[item.objectId] || undefined;
117
+ });
118
+ }
119
+
120
+ /**
121
+ * A helper that attaches the listing prices to kiosk listings.
122
+ */
123
+ export function attachObjects(kioskData: KioskData, objects: IotaObjectData[]) {
124
+ const mapping = objects.reduce<Record<string, IotaObjectData>>(
125
+ (acc: Record<string, IotaObjectData>, obj) => {
126
+ acc[obj.objectId] = obj;
127
+ return acc;
128
+ },
129
+ {},
130
+ );
131
+
132
+ kioskData.items.forEach((item) => {
133
+ item.data = mapping[item.objectId] || undefined;
134
+ });
135
+ }
136
+
137
+ /**
138
+ * A Helper to attach locked state to items in Kiosk Data.
139
+ */
140
+ export function attachLockedItems(kioskData: KioskData, lockedItemIds: string[]) {
141
+ // map lock status in an array of type { item_id: true }
142
+ const lockedStatuses = lockedItemIds.reduce<Record<string, boolean>>(
143
+ (acc: Record<string, boolean>, item: string) => {
144
+ acc[item] = true;
145
+ return acc;
146
+ },
147
+ {},
148
+ );
149
+
150
+ // parse lockedItemIds and attach their locked status.
151
+ kioskData.items.forEach((item) => {
152
+ item.isLocked = lockedStatuses[item.objectId] || false;
153
+ });
154
+ }
155
+
156
+ /**
157
+ * A helper to fetch all DF pages.
158
+ * We need that to fetch the kiosk DFs consistently, until we have
159
+ * RPC calls that allow filtering of Type / batch fetching of spec
160
+ */
161
+ export async function getAllDynamicFields(
162
+ client: IotaClient,
163
+ parentId: string,
164
+ pagination: PaginationArguments<string>,
165
+ ) {
166
+ let hasNextPage = true;
167
+ let cursor = undefined;
168
+ const data: DynamicFieldInfo[] = [];
169
+
170
+ while (hasNextPage) {
171
+ const result = await client.getDynamicFields({
172
+ parentId,
173
+ limit: pagination.limit || undefined,
174
+ cursor,
175
+ });
176
+ data.push(...result.data);
177
+ hasNextPage = result.hasNextPage;
178
+ cursor = result.nextCursor;
179
+ }
180
+
181
+ return data;
182
+ }
183
+
184
+ /**
185
+ * A helper to fetch all objects that works with pagination.
186
+ * It will fetch all objects in the array, and limit it to 50/request.
187
+ * Requests are sent using `Promise.all`.
188
+ */
189
+ export async function getAllObjects(
190
+ client: IotaClient,
191
+ ids: string[],
192
+ options: IotaObjectDataOptions,
193
+ limit: number = DEFAULT_QUERY_LIMIT,
194
+ ) {
195
+ const chunks = Array.from({ length: Math.ceil(ids.length / limit) }, (_, index) =>
196
+ ids.slice(index * limit, index * limit + limit),
197
+ );
198
+
199
+ const results = await Promise.all(
200
+ chunks.map((chunk) => {
201
+ return client.multiGetObjects({
202
+ ids: chunk,
203
+ options,
204
+ });
205
+ }),
206
+ );
207
+
208
+ return results.flat();
209
+ }
210
+
211
+ /**
212
+ * A helper to return all owned objects, with an optional filter.
213
+ * It parses all the pages and returns the data.
214
+ */
215
+ export async function getAllOwnedObjects({
216
+ client,
217
+ owner,
218
+ filter,
219
+ limit = DEFAULT_QUERY_LIMIT,
220
+ options = { showType: true, showContent: true },
221
+ }: {
222
+ client: IotaClient;
223
+ owner: string;
224
+ filter?: IotaObjectDataFilter;
225
+ options?: IotaObjectDataOptions;
226
+ limit?: number;
227
+ }) {
228
+ let hasNextPage = true;
229
+ let cursor = undefined;
230
+ const data: IotaObjectResponse[] = [];
231
+
232
+ while (hasNextPage) {
233
+ const result = await client.getOwnedObjects({
234
+ owner,
235
+ filter,
236
+ limit,
237
+ cursor,
238
+ options,
239
+ });
240
+ data.push(...result.data);
241
+ hasNextPage = result.hasNextPage;
242
+ cursor = result.nextCursor;
243
+ }
244
+
245
+ return data;
246
+ }
247
+
248
+ /**
249
+ * Converts a number to basis points.
250
+ * Supports up to 2 decimal points.
251
+ * E.g 9.95 -> 995
252
+ * @param percentage A percentage amount in the range [0, 100] including decimals.
253
+ */
254
+ export function percentageToBasisPoints(percentage: number) {
255
+ if (percentage < 0 || percentage > 100)
256
+ throw new Error('Percentage needs to be in the [0,100] range.');
257
+ return Math.ceil(percentage * 100);
258
+ }
259
+
260
+ /**
261
+ * A helper to parse a transfer policy Cap into a usable object.
262
+ */
263
+ export function parseTransferPolicyCapObject(
264
+ item: IotaObjectResponse,
265
+ ): TransferPolicyCap | undefined {
266
+ const type = (item?.data?.content as { type: string })?.type;
267
+
268
+ //@ts-expect-error Silencing error here
269
+ const policy = item?.data?.content?.fields?.policy_id as string;
270
+
271
+ if (!type.includes(TRANSFER_POLICY_CAP_TYPE)) return undefined;
272
+
273
+ // Transform 0x2::transfer_policy::TransferPolicyCap<itemType> -> itemType
274
+ const objectType = type.replace(TRANSFER_POLICY_CAP_TYPE + '<', '').slice(0, -1);
275
+
276
+ return {
277
+ policyId: policy,
278
+ // eslint-disable-next-line @typescript-eslint/no-non-null-asserted-optional-chain
279
+ policyCapId: item.data?.objectId!,
280
+ type: objectType,
281
+ };
282
+ }
283
+
284
+ // Normalizes the packageId part of a rule's type.
285
+ export function getNormalizedRuleType(rule: string) {
286
+ const normalizedRuleAddress = rule.split('::');
287
+ normalizedRuleAddress[0] = normalizeIotaAddress(normalizedRuleAddress[0]);
288
+ return normalizedRuleAddress.join('::');
289
+ }