@iota/kiosk 0.1.0

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 +12 -0
  2. package/LICENSE +201 -0
  3. package/README.md +8 -0
  4. package/dist/cjs/bcs.d.ts +2 -0
  5. package/dist/cjs/bcs.js +47 -0
  6. package/dist/cjs/bcs.js.map +7 -0
  7. package/dist/cjs/client/kiosk-client.d.ts +74 -0
  8. package/dist/cjs/client/kiosk-client.js +121 -0
  9. package/dist/cjs/client/kiosk-client.js.map +7 -0
  10. package/dist/cjs/client/kiosk-transaction.d.ts +184 -0
  11. package/dist/cjs/client/kiosk-transaction.js +511 -0
  12. package/dist/cjs/client/kiosk-transaction.js.map +7 -0
  13. package/dist/cjs/client/tp-transaction.d.ts +112 -0
  14. package/dist/cjs/client/tp-transaction.js +323 -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 +128 -0
  25. package/dist/cjs/query/kiosk.js.map +7 -0
  26. package/dist/cjs/query/transfer-policy.d.ts +28 -0
  27. package/dist/cjs/query/transfer-policy.js +96 -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 +81 -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 +160 -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 +49 -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 +204 -0
  55. package/dist/cjs/utils.js.map +7 -0
  56. package/dist/esm/bcs.d.ts +2 -0
  57. package/dist/esm/bcs.js +32 -0
  58. package/dist/esm/bcs.js.map +7 -0
  59. package/dist/esm/client/kiosk-client.d.ts +74 -0
  60. package/dist/esm/client/kiosk-client.js +105 -0
  61. package/dist/esm/client/kiosk-client.js.map +7 -0
  62. package/dist/esm/client/kiosk-transaction.d.ts +184 -0
  63. package/dist/esm/client/kiosk-transaction.js +481 -0
  64. package/dist/esm/client/kiosk-transaction.js.map +7 -0
  65. package/dist/esm/client/tp-transaction.d.ts +112 -0
  66. package/dist/esm/client/tp-transaction.js +314 -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 +116 -0
  77. package/dist/esm/query/kiosk.js.map +7 -0
  78. package/dist/esm/query/transfer-policy.d.ts +28 -0
  79. package/dist/esm/query/transfer-policy.js +80 -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 +61 -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 +160 -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 +49 -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 +184 -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 +52 -0
  111. package/src/bcs.ts +42 -0
  112. package/src/client/kiosk-client.ts +159 -0
  113. package/src/client/kiosk-transaction.ts +542 -0
  114. package/src/client/tp-transaction.ts +360 -0
  115. package/src/constants.ts +85 -0
  116. package/src/index.ts +10 -0
  117. package/src/query/kiosk.ts +190 -0
  118. package/src/query/transfer-policy.ts +135 -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 +179 -0
  126. package/src/types/transfer-policy.ts +68 -0
  127. package/src/utils.ts +284 -0
@@ -0,0 +1,360 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // Modifications Copyright (c) 2024 IOTA Stiftung
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ import type { TransactionBlock, TransactionObjectArgument } from '@iota/iota-sdk/transactions';
6
+
7
+ import {
8
+ attachFloorPriceRuleTx,
9
+ attachKioskLockRuleTx,
10
+ attachPersonalKioskRuleTx,
11
+ attachRoyaltyRuleTx,
12
+ } from '../tx/rules/attach.js';
13
+ import {
14
+ createTransferPolicy,
15
+ createTransferPolicyWithoutSharing,
16
+ removeTransferPolicyRule,
17
+ shareTransferPolicy,
18
+ withdrawFromPolicy,
19
+ } from '../tx/transfer-policy.js';
20
+ import type { ObjectArgument, TransferPolicyCap } from '../types/index.js';
21
+ import type { KioskClient } from './kiosk-client.js';
22
+
23
+ export type TransferPolicyBaseParams = {
24
+ type: string;
25
+ publisher: ObjectArgument;
26
+ skipCheck?: boolean;
27
+ };
28
+
29
+ export type TransferPolicyTransactionParams = {
30
+ kioskClient: KioskClient;
31
+ transactionBlock: TransactionBlock;
32
+ cap?: TransferPolicyCap;
33
+ };
34
+
35
+ export class TransferPolicyTransaction {
36
+ transactionBlock: TransactionBlock;
37
+ kioskClient: KioskClient;
38
+ policy?: ObjectArgument;
39
+ policyCap?: ObjectArgument;
40
+ type?: string;
41
+
42
+ constructor({ kioskClient, transactionBlock, cap }: TransferPolicyTransactionParams) {
43
+ this.kioskClient = kioskClient;
44
+ this.transactionBlock = transactionBlock;
45
+ if (cap) this.setCap(cap);
46
+ }
47
+
48
+ /**
49
+ * A function to create a new transfer policy.
50
+ * Checks if there's already an existing transfer policy to prevent
51
+ * double transfer policy mistakes.
52
+ * There's an optional `skipCheck` flag that will just create the policy
53
+ * without checking
54
+ *
55
+ * @param type The Type (`T`) for which we're creating the transfer policy.
56
+ * @param publisher The Publisher Object Id.
57
+ * @param address Address to save the `TransferPolicyCap` object to.
58
+ * @param skipCheck (Optional) skip checking if a transfer policy already exists
59
+ */
60
+ async createAndShare({
61
+ type,
62
+ publisher,
63
+ address,
64
+ skipCheck,
65
+ }: TransferPolicyBaseParams & {
66
+ address: string;
67
+ }) {
68
+ if (!skipCheck) {
69
+ const policies = await this.kioskClient.getTransferPolicies({ type });
70
+ if (policies.length > 0)
71
+ throw new Error("There's already transfer policy for this Type.");
72
+ }
73
+ const cap = createTransferPolicy(this.transactionBlock, type, publisher);
74
+ this.transactionBlock.transferObjects([cap], this.transactionBlock.pure.address(address));
75
+ }
76
+
77
+ /**
78
+ * A convenient function to create a Transfer Policy and attach some rules
79
+ * before sharing it (so you can prepare it in a single PTB)
80
+ * @param type The Type (`T`) for which we're creating the transfer policy.
81
+ * @param publisher The Publisher Object Id.
82
+ * @param address Address to save the `TransferPolicyCap` object to.
83
+ * @param skipCheck (Optional) skip checking if a transfer policy already exists
84
+ */
85
+ async create({
86
+ type,
87
+ publisher,
88
+ skipCheck,
89
+ }: TransferPolicyBaseParams): Promise<TransferPolicyTransaction> {
90
+ if (!skipCheck) {
91
+ const policies = await this.kioskClient.getTransferPolicies({ type });
92
+ if (policies.length > 0)
93
+ throw new Error("There's already transfer policy for this Type.");
94
+ }
95
+ const [policy, policyCap] = createTransferPolicyWithoutSharing(
96
+ this.transactionBlock,
97
+ type,
98
+ publisher,
99
+ );
100
+
101
+ this.#setup(policy, policyCap, type); // sets the client's TP to the newly created one.
102
+ return this;
103
+ }
104
+
105
+ /**
106
+ * This can be called after calling the `create` function to share the `TransferPolicy`,
107
+ * and transfer the `TransferPolicyCap` to the specified address
108
+ *
109
+ * @param address The address to transfer the `TransferPolicyCap`
110
+ */
111
+ shareAndTransferCap(address: string) {
112
+ if (!this.type || !this.policyCap || !this.policy)
113
+ throw new Error(
114
+ 'This function can only be called after `transferPolicyManager.create`',
115
+ );
116
+
117
+ shareTransferPolicy(
118
+ this.transactionBlock,
119
+ this.type,
120
+ this.policy as TransactionObjectArgument,
121
+ );
122
+ this.transactionBlock.transferObjects(
123
+ [this.policyCap as TransactionObjectArgument],
124
+ this.transactionBlock.pure.address(address),
125
+ );
126
+ }
127
+
128
+ /**
129
+ * Setup the TransferPolicy by passing a `cap` returned from `kioskClient.getOwnedTransferPolicies` or
130
+ * `kioskClient.getOwnedTransferPoliciesByType`.
131
+ * @param policyCapId The `TransferPolicyCap`
132
+ */
133
+ setCap({ policyId, policyCapId, type }: TransferPolicyCap) {
134
+ return this.#setup(policyId, policyCapId, type);
135
+ }
136
+
137
+ /**
138
+ * Withdraw from the transfer policy's profits.
139
+ * @param address Address to transfer the profits to.
140
+ * @param amount (Optional) amount parameter. Will withdraw all profits if the amount is not specified.
141
+ */
142
+ withdraw(address: string, amount?: string | bigint) {
143
+ this.#validateInputs();
144
+ // Withdraw coin for specified amount (or none)
145
+ const coin = withdrawFromPolicy(
146
+ this.transactionBlock,
147
+ this.type!,
148
+ this.policy!,
149
+ this.policyCap!,
150
+ amount,
151
+ );
152
+
153
+ this.transactionBlock.transferObjects([coin], this.transactionBlock.pure.address(address));
154
+
155
+ return this;
156
+ }
157
+
158
+ /**
159
+ * Adds the Kiosk Royalty rule to the Transfer Policy.
160
+ * You can pass the percentage, as well as a minimum amount.
161
+ * The royalty that will be paid is the MAX(percentage, minAmount).
162
+ * You can pass 0 in either value if you want only percentage royalty, or a fixed amount fee.
163
+ * (but you should define at least one of them for the rule to make sense).
164
+ *
165
+ * @param percentageBps The royalty percentage in basis points. Use `percentageToBasisPoints` helper to convert from percentage [0,100].
166
+ * @param minAmount The minimum royalty amount per request in nano.
167
+ */
168
+ addRoyaltyRule(
169
+ percentageBps: number | string, // this is in basis points.
170
+ minAmount: number | string,
171
+ ) {
172
+ this.#validateInputs();
173
+
174
+ // Hard-coding package Ids as these don't change.
175
+ // Also, it's hard to keep versioning as with network wipes, mainnet
176
+ // and testnet will conflict.
177
+ attachRoyaltyRuleTx(
178
+ this.transactionBlock,
179
+ this.type!,
180
+ this.policy!,
181
+ this.policyCap!,
182
+ percentageBps,
183
+ minAmount,
184
+ this.kioskClient.getRulePackageId('royaltyRulePackageId'),
185
+ );
186
+ return this;
187
+ }
188
+
189
+ /**
190
+ * Adds the Kiosk Lock Rule to the Transfer Policy.
191
+ * This Rule forces buyer to lock the item in the kiosk, preserving strong royalties.
192
+ */
193
+ addLockRule() {
194
+ this.#validateInputs();
195
+
196
+ attachKioskLockRuleTx(
197
+ this.transactionBlock,
198
+ this.type!,
199
+ this.policy!,
200
+ this.policyCap!,
201
+ this.kioskClient.getRulePackageId('kioskLockRulePackageId'),
202
+ );
203
+ return this;
204
+ }
205
+
206
+ /**
207
+ * Attaches the Personal Kiosk Rule, making a purchase valid only for `SoulBound` kiosks.
208
+ */
209
+ addPersonalKioskRule() {
210
+ this.#validateInputs();
211
+
212
+ attachPersonalKioskRuleTx(
213
+ this.transactionBlock,
214
+ this.type!,
215
+ this.policy!,
216
+ this.policyCap!,
217
+ this.kioskClient.getRulePackageId('personalKioskRulePackageId'),
218
+ );
219
+ return this;
220
+ }
221
+
222
+ /**
223
+ * A function to add the floor price rule to a transfer policy.
224
+ * @param minPrice The minimum price in nano.
225
+ */
226
+ addFloorPriceRule(minPrice: string | bigint) {
227
+ this.#validateInputs();
228
+
229
+ attachFloorPriceRuleTx(
230
+ this.transactionBlock,
231
+ this.type!,
232
+ this.policy!,
233
+ this.policyCap!,
234
+ minPrice,
235
+ this.kioskClient.getRulePackageId('floorPriceRulePackageId'),
236
+ );
237
+ return this;
238
+ }
239
+
240
+ /**
241
+ * Generic helper to remove a rule, not from the SDK's base ruleset.
242
+ * @param ruleType The Rule Type
243
+ * @param configType The Config Type
244
+ */
245
+ removeRule({ ruleType, configType }: { ruleType: string; configType: string }) {
246
+ this.#validateInputs();
247
+
248
+ removeTransferPolicyRule(
249
+ this.transactionBlock,
250
+ this.type!,
251
+ ruleType,
252
+ configType,
253
+ this.policy!,
254
+ this.policyCap!,
255
+ );
256
+ }
257
+
258
+ /**
259
+ * Removes the lock rule.
260
+ */
261
+ removeLockRule() {
262
+ this.#validateInputs();
263
+
264
+ const packageId = this.kioskClient.getRulePackageId('kioskLockRulePackageId');
265
+
266
+ removeTransferPolicyRule(
267
+ this.transactionBlock,
268
+ this.type!,
269
+ `${packageId}::kiosk_lock_rule::Rule`,
270
+ `${packageId}::kiosk_lock_rule::Config`,
271
+ this.policy!,
272
+ this.policyCap!,
273
+ );
274
+ return this;
275
+ }
276
+
277
+ /**
278
+ * Removes the Royalty rule
279
+ */
280
+ removeRoyaltyRule() {
281
+ this.#validateInputs();
282
+
283
+ const packageId = this.kioskClient.getRulePackageId('royaltyRulePackageId');
284
+
285
+ removeTransferPolicyRule(
286
+ this.transactionBlock,
287
+ this.type!,
288
+ `${packageId}::royalty_rule::Rule`,
289
+ `${packageId}::royalty_rule::Config`,
290
+ this.policy!,
291
+ this.policyCap!,
292
+ );
293
+ return this;
294
+ }
295
+
296
+ removePersonalKioskRule() {
297
+ this.#validateInputs();
298
+
299
+ const packageId = this.kioskClient.getRulePackageId('personalKioskRulePackageId');
300
+
301
+ removeTransferPolicyRule(
302
+ this.transactionBlock,
303
+ this.type!,
304
+ `${packageId}::personal_kiosk_rule::Rule`,
305
+ `bool`,
306
+ this.policy!,
307
+ this.policyCap!,
308
+ );
309
+ return this;
310
+ }
311
+
312
+ removeFloorPriceRule() {
313
+ this.#validateInputs();
314
+
315
+ const packageId = this.kioskClient.getRulePackageId('floorPriceRulePackageId');
316
+
317
+ removeTransferPolicyRule(
318
+ this.transactionBlock,
319
+ this.type!,
320
+ `${packageId}::floor_price_rule::Rule`,
321
+ `${packageId}::floor_price_rule::Config`,
322
+ this.policy!,
323
+ this.policyCap!,
324
+ );
325
+ return this;
326
+ }
327
+
328
+ getPolicy() {
329
+ if (!this.policy) throw new Error('Policy not set.');
330
+ return this.policy;
331
+ }
332
+
333
+ getPolicyCap() {
334
+ if (!this.policyCap) throw new Error('Transfer Policy Cap not set.');
335
+ return this.policyCap;
336
+ }
337
+
338
+ // Internal function that that the policy's Id + Cap + type have been set.
339
+ #validateInputs() {
340
+ const genericErrorMessage = `Please use 'setCap()' to setup the TransferPolicy.`;
341
+ if (!this.policy)
342
+ throw new Error(`${genericErrorMessage} Missing: Transfer Policy Object.`);
343
+ if (!this.policyCap)
344
+ throw new Error(`${genericErrorMessage} Missing: TransferPolicyCap Object ID`);
345
+ if (!this.type)
346
+ throw new Error(
347
+ `${genericErrorMessage} Missing: Transfer Policy object type (e.g. {packageId}::item::Item)`,
348
+ );
349
+ }
350
+
351
+ /**
352
+ * Setup the state of the TransferPolicyTransaction.
353
+ */
354
+ #setup(policyId: ObjectArgument, policyCap: ObjectArgument, type: string) {
355
+ this.policy = policyId;
356
+ this.policyCap = policyCap;
357
+ this.type = type;
358
+ return this;
359
+ }
360
+ }
@@ -0,0 +1,85 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // Modifications Copyright (c) 2024 IOTA Stiftung
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ // eslint-disable-next-line import/no-cycle
6
+ import type { KioskConfiguration } from '@iota/iota-sdk/client';
7
+ import { getAllNetworks } from '@iota/iota-sdk/client';
8
+ import {
9
+ resolveFloorPriceRule,
10
+ resolveKioskLockRule,
11
+ resolvePersonalKioskRule,
12
+ resolveRoyaltyRule,
13
+ } from './tx/rules//resolve.js';
14
+ import type { ObjectArgument, RuleResolvingParams } from './types/index.js';
15
+
16
+ /**
17
+ * The base rule package ids that can be extended
18
+ */
19
+ export type BaseRulePackageIds = {
20
+ royaltyRulePackageId?: string;
21
+ kioskLockRulePackageId?: string;
22
+ personalKioskRulePackageId?: string;
23
+ floorPriceRulePackageId?: string;
24
+ };
25
+
26
+ /**
27
+ * The Transfer Policy rule.
28
+ */
29
+ export type TransferPolicyRule = {
30
+ rule: string;
31
+ packageId: string;
32
+ resolveRuleFunction: (rule: RuleResolvingParams) => ObjectArgument | void;
33
+ hasLockingRule?: boolean;
34
+ };
35
+
36
+ /**
37
+ * Constructs a list of rule resolvers based on the params.
38
+ */
39
+ export function getBaseRules({
40
+ royaltyRulePackageId,
41
+ kioskLockRulePackageId,
42
+ personalKioskRulePackageId,
43
+ floorPriceRulePackageId,
44
+ }: BaseRulePackageIds): TransferPolicyRule[] {
45
+ const rules = [];
46
+
47
+ if (royaltyRulePackageId) {
48
+ rules.push({
49
+ rule: `${royaltyRulePackageId}::royalty_rule::Rule`,
50
+ packageId: royaltyRulePackageId,
51
+ resolveRuleFunction: resolveRoyaltyRule,
52
+ });
53
+ }
54
+
55
+ if (kioskLockRulePackageId) {
56
+ rules.push({
57
+ rule: `${kioskLockRulePackageId}::kiosk_lock_rule::Rule`,
58
+ packageId: kioskLockRulePackageId,
59
+ resolveRuleFunction: resolveKioskLockRule,
60
+ hasLockingRule: true,
61
+ });
62
+ }
63
+
64
+ if (personalKioskRulePackageId) {
65
+ rules.push({
66
+ rule: `${personalKioskRulePackageId}::personal_kiosk_rule::Rule`,
67
+ packageId: personalKioskRulePackageId,
68
+ resolveRuleFunction: resolvePersonalKioskRule,
69
+ });
70
+ }
71
+
72
+ if (floorPriceRulePackageId) {
73
+ rules.push({
74
+ rule: `${floorPriceRulePackageId}::floor_price_rule::Rule`,
75
+ packageId: floorPriceRulePackageId,
76
+ resolveRuleFunction: resolveFloorPriceRule,
77
+ });
78
+ }
79
+
80
+ return rules;
81
+ }
82
+
83
+ export const rules: TransferPolicyRule[] = Object.values(getAllNetworks())
84
+ .filter((network) => network.kiosk)
85
+ .flatMap((network) => getBaseRules(network.kiosk as KioskConfiguration));
package/src/index.ts ADDED
@@ -0,0 +1,10 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // Modifications Copyright (c) 2024 IOTA Stiftung
3
+ // SPDX-License-Identifier: Apache-2.0
4
+
5
+ export * from './utils.js';
6
+ export * from './types/index.js';
7
+ export * from './constants.js';
8
+ export * from './client/kiosk-client.js';
9
+ export * from './client/tp-transaction.js';
10
+ export * from './client/kiosk-transaction.js';
@@ -0,0 +1,190 @@
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
+ PaginationArguments,
7
+ IotaClient,
8
+ IotaObjectData,
9
+ IotaObjectDataFilter,
10
+ IotaObjectResponse,
11
+ } from '@iota/iota-sdk/client';
12
+ import { isValidIotaAddress } from '@iota/iota-sdk/utils';
13
+
14
+ import type {
15
+ FetchKioskOptions,
16
+ KioskExtension,
17
+ KioskListing,
18
+ OwnedKiosks,
19
+ PagedKioskData,
20
+ } from '../types/index.js';
21
+ import { KIOSK_OWNER_CAP } from '../types/index.js';
22
+ import {
23
+ attachListingsAndPrices,
24
+ attachLockedItems,
25
+ attachObjects,
26
+ extractKioskData,
27
+ getAllDynamicFields,
28
+ getAllObjects,
29
+ getKioskObject,
30
+ } from '../utils.js';
31
+
32
+ export async function fetchKiosk(
33
+ client: IotaClient,
34
+ kioskId: string,
35
+ pagination: PaginationArguments<string>,
36
+ options: FetchKioskOptions,
37
+ ): Promise<PagedKioskData> {
38
+ // TODO: Replace the `getAllDynamicFields` with a paginated
39
+ // response, once we have better RPC support for
40
+ // type filtering & batch fetching.
41
+ // This can't work with pagination currently.
42
+ const data = await getAllDynamicFields(client, kioskId, pagination);
43
+
44
+ const listings: KioskListing[] = [];
45
+ const lockedItemIds: string[] = [];
46
+
47
+ // extracted kiosk data.
48
+ const kioskData = extractKioskData(data, listings, lockedItemIds, kioskId);
49
+
50
+ // split the fetching in two queries as we are most likely passing different options for each kind.
51
+ // For items, we usually seek the Display.
52
+ // For listings we usually seek the DF value (price) / exclusivity.
53
+ const [kiosk, listingObjects, items] = await Promise.all([
54
+ options.withKioskFields ? getKioskObject(client, kioskId) : Promise.resolve(undefined),
55
+ options.withListingPrices
56
+ ? getAllObjects(client, kioskData.listingIds, {
57
+ showContent: true,
58
+ })
59
+ : Promise.resolve([]),
60
+ options.withObjects
61
+ ? getAllObjects(
62
+ client,
63
+ kioskData.itemIds,
64
+ options.objectOptions || { showDisplay: true },
65
+ )
66
+ : Promise.resolve([]),
67
+ ]);
68
+
69
+ if (options.withKioskFields) kioskData.kiosk = kiosk;
70
+ // attach items listings. IF we have `options.withListingPrices === true`, it will also attach the prices.
71
+ attachListingsAndPrices(kioskData, listings, listingObjects);
72
+ // add `locked` status to items that are locked.
73
+ attachLockedItems(kioskData, lockedItemIds);
74
+
75
+ // Attach the objects for the queried items.
76
+ attachObjects(
77
+ kioskData,
78
+ items.filter((x) => !!x.data).map((x) => x.data!),
79
+ );
80
+
81
+ return {
82
+ data: kioskData,
83
+ nextCursor: null,
84
+ hasNextPage: false,
85
+ };
86
+ }
87
+
88
+ /**
89
+ * A function to fetch all the user's kiosk Caps
90
+ * And a list of the kiosk address ids.
91
+ * Returns a list of `kioskOwnerCapIds` and `kioskIds`.
92
+ * Extra options allow pagination.
93
+ */
94
+ export async function getOwnedKiosks(
95
+ client: IotaClient,
96
+ address: string,
97
+ options?: {
98
+ pagination?: PaginationArguments<string>;
99
+ personalKioskType: string;
100
+ },
101
+ ): Promise<OwnedKiosks> {
102
+ if (!isValidIotaAddress(address))
103
+ return {
104
+ nextCursor: null,
105
+ hasNextPage: false,
106
+ kioskOwnerCaps: [],
107
+ kioskIds: [],
108
+ };
109
+
110
+ const filter: IotaObjectDataFilter = {
111
+ MatchAny: [
112
+ {
113
+ StructType: KIOSK_OWNER_CAP,
114
+ },
115
+ ],
116
+ };
117
+
118
+ if (options?.personalKioskType) {
119
+ filter.MatchAny.push({
120
+ StructType: options.personalKioskType,
121
+ });
122
+ }
123
+
124
+ // fetch owned kiosk caps, paginated.
125
+ const { data, hasNextPage, nextCursor } = await client.getOwnedObjects({
126
+ owner: address,
127
+ filter,
128
+ options: {
129
+ showContent: true,
130
+ showType: true,
131
+ },
132
+ ...(options?.pagination || {}),
133
+ });
134
+
135
+ // get kioskIds from the OwnerCaps.
136
+ const kioskIdList = data?.map((x: IotaObjectResponse) => {
137
+ const fields = x.data?.content?.dataType === 'moveObject' ? x.data.content.fields : null;
138
+ // @ts-expect-error TODO: should i remove ts ignore here?
139
+ return (fields?.cap ? fields?.cap?.fields?.for : fields?.for) as string;
140
+ // return (fields as { for: string })?.for;
141
+ });
142
+
143
+ // clean up data that might have an error in them.
144
+ // only return valid objects.
145
+ const filteredData = data.filter((x) => 'data' in x).map((x) => x.data) as IotaObjectData[];
146
+
147
+ return {
148
+ nextCursor,
149
+ hasNextPage,
150
+ kioskOwnerCaps: filteredData.map((x, idx) => ({
151
+ isPersonal: x.type !== KIOSK_OWNER_CAP,
152
+ digest: x.digest,
153
+ version: x.version,
154
+ objectId: x.objectId,
155
+ kioskId: kioskIdList[idx],
156
+ })),
157
+ kioskIds: kioskIdList,
158
+ };
159
+ }
160
+
161
+ // Get a kiosk extension data for a given kioskId and extensionType.
162
+ export async function fetchKioskExtension(
163
+ client: IotaClient,
164
+ kioskId: string,
165
+ extensionType: string,
166
+ ): Promise<KioskExtension | null> {
167
+ const extension = await client.getDynamicFieldObject({
168
+ parentId: kioskId,
169
+ name: {
170
+ type: `0x2::kiosk_extension::ExtensionKey<${extensionType}>`,
171
+ value: {
172
+ dummy_field: false,
173
+ },
174
+ },
175
+ });
176
+
177
+ if (!extension.data) return null;
178
+
179
+ const fields = (extension?.data?.content as { fields: { [k: string]: any } })?.fields?.value
180
+ ?.fields;
181
+
182
+ return {
183
+ objectId: extension.data.objectId,
184
+ type: extensionType,
185
+ isEnabled: fields?.is_enabled,
186
+ permissions: fields?.permissions,
187
+ storageId: fields?.storage?.fields?.id?.id,
188
+ storageSize: fields?.storage?.fields?.size,
189
+ };
190
+ }