@mysten/kiosk 0.6.0 → 0.7.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 (44) hide show
  1. package/CHANGELOG.md +6 -0
  2. package/README.md +2 -287
  3. package/dist/client/kiosk-client.d.ts +64 -0
  4. package/dist/client/kiosk-transaction.d.ts +207 -0
  5. package/dist/client/tp-transaction.d.ts +112 -0
  6. package/dist/constants.d.ts +30 -4
  7. package/dist/index.d.ts +3 -6
  8. package/dist/index.js +1247 -257
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.mjs +1230 -223
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/query/kiosk.d.ts +2 -1
  13. package/dist/query/transfer-policy.d.ts +17 -1
  14. package/dist/tx/kiosk.d.ts +7 -23
  15. package/dist/tx/personal-kiosk.d.ts +7 -0
  16. package/dist/tx/rules/attach.d.ts +7 -0
  17. package/dist/tx/rules/resolve.d.ts +15 -0
  18. package/dist/tx/transfer-policy.d.ts +13 -16
  19. package/dist/types/index.d.ts +22 -4
  20. package/dist/types/kiosk.d.ts +30 -1
  21. package/dist/types/transfer-policy.d.ts +27 -1
  22. package/dist/utils.d.ts +32 -18
  23. package/package.json +11 -3
  24. package/src/bcs.ts +1 -0
  25. package/src/client/kiosk-client.ts +156 -0
  26. package/src/client/kiosk-transaction.ts +512 -0
  27. package/src/client/tp-transaction.ts +350 -0
  28. package/src/constants.ts +113 -6
  29. package/src/index.ts +3 -6
  30. package/src/query/kiosk.ts +51 -18
  31. package/src/query/transfer-policy.ts +82 -2
  32. package/src/tx/kiosk.ts +18 -146
  33. package/src/tx/personal-kiosk.ts +35 -0
  34. package/src/tx/rules/attach.ts +74 -0
  35. package/src/tx/rules/resolve.ts +87 -0
  36. package/src/tx/transfer-policy.ts +40 -78
  37. package/src/types/index.ts +25 -4
  38. package/src/types/kiosk.ts +26 -1
  39. package/src/types/transfer-policy.ts +35 -1
  40. package/src/utils.ts +141 -33
  41. package/dist/tx/rules.d.ts +0 -19
  42. package/dist/types/env.d.ts +0 -12
  43. package/src/tx/rules.ts +0 -58
  44. package/src/types/env.ts +0 -20
package/dist/index.js CHANGED
@@ -16,67 +16,76 @@ var __copyProps = (to, from, except, desc) => {
16
16
  return to;
17
17
  };
18
18
  var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod);
19
+ var __accessCheck = (obj, member, msg) => {
20
+ if (!member.has(obj))
21
+ throw TypeError("Cannot " + msg);
22
+ };
23
+ var __privateGet = (obj, member, getter) => {
24
+ __accessCheck(obj, member, "read from private field");
25
+ return getter ? getter.call(obj) : member.get(obj);
26
+ };
27
+ var __privateAdd = (obj, member, value) => {
28
+ if (member.has(obj))
29
+ throw TypeError("Cannot add the same private member more than once");
30
+ member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
31
+ };
32
+ var __privateSet = (obj, member, value, setter) => {
33
+ __accessCheck(obj, member, "write to private field");
34
+ setter ? setter.call(obj, value) : member.set(obj, value);
35
+ return value;
36
+ };
37
+ var __privateMethod = (obj, member, method) => {
38
+ __accessCheck(obj, member, "access private method");
39
+ return method;
40
+ };
19
41
 
20
42
  // src/index.ts
21
43
  var src_exports = {};
22
44
  __export(src_exports, {
45
+ FLOOR_PRICE_RULE_ADDRESS: () => FLOOR_PRICE_RULE_ADDRESS,
23
46
  KIOSK_ITEM: () => KIOSK_ITEM,
24
47
  KIOSK_LISTING: () => KIOSK_LISTING,
25
48
  KIOSK_LOCK: () => KIOSK_LOCK,
26
49
  KIOSK_LOCK_RULE: () => KIOSK_LOCK_RULE,
50
+ KIOSK_LOCK_RULE_ADDRESS: () => KIOSK_LOCK_RULE_ADDRESS,
27
51
  KIOSK_MODULE: () => KIOSK_MODULE,
28
52
  KIOSK_OWNER_CAP: () => KIOSK_OWNER_CAP,
29
53
  KIOSK_PURCHASE_CAP: () => KIOSK_PURCHASE_CAP,
30
54
  KIOSK_TYPE: () => KIOSK_TYPE,
31
- MAINNET_RULES_PACKAGE_ADDRESS: () => MAINNET_RULES_PACKAGE_ADDRESS,
55
+ KioskClient: () => KioskClient,
56
+ KioskTransaction: () => KioskTransaction,
57
+ Network: () => Network,
58
+ PERSONAL_KIOSK_RULE_ADDRESS: () => PERSONAL_KIOSK_RULE_ADDRESS,
32
59
  ROYALTY_RULE: () => ROYALTY_RULE,
33
- TESTNET_RULES_PACKAGE_ADDRESS: () => TESTNET_RULES_PACKAGE_ADDRESS,
60
+ ROYALTY_RULE_ADDRESS: () => ROYALTY_RULE_ADDRESS,
61
+ TRANSFER_POLICY_CAP_TYPE: () => TRANSFER_POLICY_CAP_TYPE,
34
62
  TRANSFER_POLICY_CREATED_EVENT: () => TRANSFER_POLICY_CREATED_EVENT,
35
63
  TRANSFER_POLICY_MODULE: () => TRANSFER_POLICY_MODULE,
36
64
  TRANSFER_POLICY_TYPE: () => TRANSFER_POLICY_TYPE,
37
- attachKioskLockRule: () => attachKioskLockRule,
65
+ TransferPolicyTransaction: () => TransferPolicyTransaction,
38
66
  attachListingsAndPrices: () => attachListingsAndPrices,
39
67
  attachLockedItems: () => attachLockedItems,
40
- attachRoyaltyRule: () => attachRoyaltyRule,
41
- bcs: () => import_bcs.bcs,
42
- borrow: () => borrow,
43
- borrowMut: () => borrowMut,
44
- borrowValue: () => borrowValue,
45
- confirmRequest: () => confirmRequest,
46
- createKiosk: () => createKiosk,
47
- createKioskAndShare: () => createKioskAndShare,
48
- createTransferPolicy: () => createTransferPolicy,
49
- customEnvironment: () => customEnvironment,
50
- delist: () => delist,
68
+ attachObjects: () => attachObjects,
51
69
  extractKioskData: () => extractKioskData,
52
- fetchKiosk: () => fetchKiosk,
53
70
  getAllDynamicFields: () => getAllDynamicFields,
71
+ getAllObjects: () => getAllObjects,
72
+ getAllOwnedObjects: () => getAllOwnedObjects,
73
+ getBaseRules: () => getBaseRules,
54
74
  getKioskObject: () => getKioskObject,
55
- getOwnedKiosks: () => getOwnedKiosks,
56
- getRulePackageAddress: () => getRulePackageAddress,
75
+ getNormalizedRuleType: () => getNormalizedRuleType,
57
76
  getTypeWithoutPackageAddress: () => getTypeWithoutPackageAddress,
58
- list: () => list,
59
- lock: () => lock,
60
- mainnetEnvironment: () => mainnetEnvironment,
77
+ mainnetRules: () => mainnetRules,
61
78
  objArg: () => objArg,
79
+ parseTransferPolicyCapObject: () => parseTransferPolicyCapObject,
62
80
  percentageToBasisPoints: () => percentageToBasisPoints,
63
- place: () => place,
64
- placeAndList: () => placeAndList,
65
- purchase: () => purchase,
66
- purchaseAndResolvePolicies: () => purchaseAndResolvePolicies,
67
- queryTransferPolicy: () => queryTransferPolicy,
68
- removeTransferPolicyRule: () => removeTransferPolicyRule,
69
- resolveKioskLockRule: () => resolveKioskLockRule,
70
- resolveRoyaltyRule: () => resolveRoyaltyRule,
71
- returnValue: () => returnValue,
72
- rulesPackageAddresses: () => rulesPackageAddresses,
73
- take: () => take,
74
- testnetEnvironment: () => testnetEnvironment,
75
- withdrawFromKiosk: () => withdrawFromKiosk,
76
- withdrawFromPolicy: () => withdrawFromPolicy
81
+ rules: () => rules,
82
+ testnetRules: () => testnetRules
77
83
  });
78
84
  module.exports = __toCommonJS(src_exports);
79
85
 
86
+ // src/utils.ts
87
+ var import_utils = require("@mysten/sui.js/utils");
88
+
80
89
  // src/bcs.ts
81
90
  var import_bcs = require("@mysten/sui.js/bcs");
82
91
 
@@ -93,16 +102,17 @@ var KIOSK_PURCHASE_CAP = `${KIOSK_MODULE}::PurchaseCap`;
93
102
  var TRANSFER_POLICY_MODULE = "0x2::transfer_policy";
94
103
  var TRANSFER_POLICY_CREATED_EVENT = `${TRANSFER_POLICY_MODULE}::TransferPolicyCreated`;
95
104
  var TRANSFER_POLICY_TYPE = `${TRANSFER_POLICY_MODULE}::TransferPolicy`;
105
+ var TRANSFER_POLICY_CAP_TYPE = `${TRANSFER_POLICY_MODULE}::TransferPolicyCap`;
96
106
  var KIOSK_LOCK_RULE = "kiosk_lock_rule::Rule";
97
107
  var ROYALTY_RULE = "royalty_rule::Rule";
98
108
 
99
- // src/types/env.ts
100
- var testnetEnvironment = { env: "testnet" };
101
- var mainnetEnvironment = { env: "mainnet" };
102
- var customEnvironment = (address) => ({
103
- env: "custom",
104
- address
105
- });
109
+ // src/types/index.ts
110
+ var Network = /* @__PURE__ */ ((Network2) => {
111
+ Network2["MAINNET"] = "mainnet";
112
+ Network2["TESTNET"] = "testnet";
113
+ Network2["CUSTOM"] = "custom";
114
+ return Network2;
115
+ })(Network || {});
106
116
 
107
117
  // src/bcs.ts
108
118
  import_bcs.bcs.registerStructType(KIOSK_TYPE, {
@@ -127,26 +137,17 @@ import_bcs.bcs.registerStructType(TRANSFER_POLICY_TYPE, {
127
137
  rules: ["vector", "string"]
128
138
  });
129
139
 
130
- // src/constants.ts
131
- var TESTNET_RULES_PACKAGE_ADDRESS = "bd8fc1947cf119350184107a3087e2dc27efefa0dd82e25a1f699069fe81a585";
132
- var MAINNET_RULES_PACKAGE_ADDRESS = "0x434b5bd8f6a7b05fede0ff46c6e511d71ea326ed38056e3bcd681d2d7c2a7879";
133
-
134
140
  // src/utils.ts
135
- var rulesPackageAddresses = {
136
- mainnet: MAINNET_RULES_PACKAGE_ADDRESS,
137
- testnet: TESTNET_RULES_PACKAGE_ADDRESS,
138
- devnet: "",
139
- custom: null
140
- };
141
- function objArg(tx, arg) {
141
+ var DEFAULT_QUERY_LIMIT = 50;
142
+ function objArg(txb, arg) {
142
143
  if (typeof arg === "string") {
143
- return tx.object(arg);
144
+ return txb.object(arg);
144
145
  }
145
146
  if ("digest" in arg && "version" in arg && "objectId" in arg) {
146
- return tx.objectRef(arg);
147
+ return txb.objectRef(arg);
147
148
  }
148
149
  if ("objectId" in arg && "initialSharedVersion" in arg && "mutable" in arg) {
149
- return tx.sharedObjectRef(arg);
150
+ return txb.sharedObjectRef(arg);
150
151
  }
151
152
  if ("kind" in arg) {
152
153
  return arg;
@@ -163,7 +164,7 @@ async function getKioskObject(client, id) {
163
164
  }
164
165
  return import_bcs.bcs.de(KIOSK_TYPE, queryRes.data.bcs.bcsBytes, "base64");
165
166
  }
166
- function extractKioskData(data, listings, lockedItemIds) {
167
+ function extractKioskData(data, listings, lockedItemIds, kioskId) {
167
168
  return data.reduce(
168
169
  (acc, val) => {
169
170
  const type = getTypeWithoutPackageAddress(val.name.type);
@@ -173,7 +174,8 @@ function extractKioskData(data, listings, lockedItemIds) {
173
174
  acc.items.push({
174
175
  objectId: val.objectId,
175
176
  type: val.objectType,
176
- isLocked: false
177
+ isLocked: false,
178
+ kioskId
177
179
  });
178
180
  break;
179
181
  case "kiosk::Listing":
@@ -215,6 +217,18 @@ function attachListingsAndPrices(kioskData, listings, listingObjects) {
215
217
  item.listing = itemListings[item.objectId] || void 0;
216
218
  });
217
219
  }
220
+ function attachObjects(kioskData, objects) {
221
+ const mapping = objects.reduce(
222
+ (acc, obj) => {
223
+ acc[obj.objectId] = obj;
224
+ return acc;
225
+ },
226
+ {}
227
+ );
228
+ kioskData.items.forEach((item) => {
229
+ item.data = mapping[item.objectId] || void 0;
230
+ });
231
+ }
218
232
  function attachLockedItems(kioskData, lockedItemIds) {
219
233
  const lockedStatuses = lockedItemIds.reduce(
220
234
  (acc, item) => {
@@ -227,14 +241,6 @@ function attachLockedItems(kioskData, lockedItemIds) {
227
241
  item.isLocked = lockedStatuses[item.objectId] || false;
228
242
  });
229
243
  }
230
- function getRulePackageAddress(environment) {
231
- if (environment.env === "custom") {
232
- if (!environment.address)
233
- throw new Error("Please supply the custom package address for rules.");
234
- return environment.address;
235
- }
236
- return rulesPackageAddresses[environment.env];
237
- }
238
244
  async function getAllDynamicFields(client, parentId, pagination) {
239
245
  let hasNextPage = true;
240
246
  let cursor = void 0;
@@ -251,89 +257,86 @@ async function getAllDynamicFields(client, parentId, pagination) {
251
257
  }
252
258
  return data;
253
259
  }
260
+ async function getAllObjects(client, ids, options, limit = DEFAULT_QUERY_LIMIT) {
261
+ const chunks = Array.from(
262
+ { length: Math.ceil(ids.length / limit) },
263
+ (_, index) => ids.slice(index * limit, index * limit + limit)
264
+ );
265
+ const results = await Promise.all(
266
+ chunks.map((chunk) => {
267
+ return client.multiGetObjects({
268
+ ids: chunk,
269
+ options
270
+ });
271
+ })
272
+ );
273
+ return results.flat();
274
+ }
275
+ async function getAllOwnedObjects({
276
+ client,
277
+ owner,
278
+ filter,
279
+ limit = DEFAULT_QUERY_LIMIT,
280
+ options = { showType: true, showContent: true }
281
+ }) {
282
+ let hasNextPage = true;
283
+ let cursor = void 0;
284
+ const data = [];
285
+ while (hasNextPage) {
286
+ const result = await client.getOwnedObjects({
287
+ owner,
288
+ filter,
289
+ limit,
290
+ cursor,
291
+ options
292
+ });
293
+ data.push(...result.data);
294
+ hasNextPage = result.hasNextPage;
295
+ cursor = result.nextCursor;
296
+ }
297
+ return data;
298
+ }
254
299
  function percentageToBasisPoints(percentage) {
255
300
  if (percentage < 0 || percentage > 100)
256
301
  throw new Error("Percentage needs to be in the [0,100] range.");
257
302
  return Math.ceil(percentage * 100);
258
303
  }
259
-
260
- // src/tx/transfer-policy.ts
261
- function createTransferPolicy(tx, itemType, publisher) {
262
- let [transferPolicy, transferPolicyCap] = tx.moveCall({
263
- target: `${TRANSFER_POLICY_MODULE}::new`,
264
- typeArguments: [itemType],
265
- arguments: [objArg(tx, publisher)]
266
- });
267
- tx.moveCall({
268
- target: `0x2::transfer::public_share_object`,
269
- typeArguments: [`${TRANSFER_POLICY_TYPE}<${itemType}>`],
270
- arguments: [transferPolicy]
271
- });
272
- return transferPolicyCap;
273
- }
274
- function withdrawFromPolicy(tx, itemType, policy, policyCap, amount) {
275
- let amountArg = amount !== null ? tx.pure({ Some: amount }, "Option<u64>") : tx.pure({ None: true }, "Option<u64>");
276
- let [profits] = tx.moveCall({
277
- target: `${TRANSFER_POLICY_MODULE}::withdraw`,
278
- typeArguments: [itemType],
279
- arguments: [objArg(tx, policy), objArg(tx, policyCap), amountArg]
280
- });
281
- return profits;
282
- }
283
- function confirmRequest(tx, itemType, policy, request) {
284
- tx.moveCall({
285
- target: `${TRANSFER_POLICY_MODULE}::confirm_request`,
286
- typeArguments: [itemType],
287
- arguments: [objArg(tx, policy), request]
288
- });
304
+ function parseTransferPolicyCapObject(item) {
305
+ const type = item?.data?.content?.type;
306
+ const policy = item?.data?.content?.fields?.policy_id;
307
+ if (!type.includes(TRANSFER_POLICY_CAP_TYPE))
308
+ return void 0;
309
+ const objectType = type.replace(TRANSFER_POLICY_CAP_TYPE + "<", "").slice(0, -1);
310
+ return {
311
+ policyId: policy,
312
+ policyCapId: item.data?.objectId,
313
+ type: objectType
314
+ };
289
315
  }
290
- function removeTransferPolicyRule(tx, itemType, ruleType, configType, policy, policyCap) {
291
- tx.moveCall({
292
- target: `${TRANSFER_POLICY_MODULE}::remove_rule`,
293
- typeArguments: [itemType, ruleType, configType],
294
- arguments: [objArg(tx, policy), policyCap]
295
- });
296
- }
297
- function resolveRoyaltyRule(tx, itemType, price, policyId, transferRequest, environment) {
298
- const policyObj = objArg(tx, policyId);
299
- const [amount] = tx.moveCall({
300
- target: `${getRulePackageAddress(environment)}::royalty_rule::fee_amount`,
301
- typeArguments: [itemType],
302
- arguments: [policyObj, objArg(tx, price)]
303
- });
304
- const feeCoin = tx.splitCoins(tx.gas, [amount]);
305
- tx.moveCall({
306
- target: `${getRulePackageAddress(environment)}::royalty_rule::pay`,
307
- typeArguments: [itemType],
308
- arguments: [policyObj, transferRequest, feeCoin]
309
- });
310
- }
311
- function resolveKioskLockRule(tx, itemType, item, kiosk, kioskCap, policyId, transferRequest, environment) {
312
- lock(tx, itemType, kiosk, kioskCap, policyId, item);
313
- tx.moveCall({
314
- target: `${getRulePackageAddress(environment)}::kiosk_lock_rule::prove`,
315
- typeArguments: [itemType],
316
- arguments: [transferRequest, objArg(tx, kiosk)]
317
- });
316
+ function getNormalizedRuleType(rule) {
317
+ const normalizedRuleAddress = rule.split("::");
318
+ normalizedRuleAddress[0] = (0, import_utils.normalizeSuiAddress)(normalizedRuleAddress[0]);
319
+ return normalizedRuleAddress.join("::");
318
320
  }
319
321
 
320
322
  // src/tx/kiosk.ts
321
323
  function createKiosk(tx) {
322
- let [kiosk, kioskOwnerCap] = tx.moveCall({
324
+ const [kiosk, kioskOwnerCap] = tx.moveCall({
323
325
  target: `${KIOSK_MODULE}::new`
324
326
  });
325
327
  return [kiosk, kioskOwnerCap];
326
328
  }
327
329
  function createKioskAndShare(tx) {
328
- let [kiosk, kioskOwnerCap] = tx.moveCall({
329
- target: `${KIOSK_MODULE}::new`
330
- });
330
+ const [kiosk, kioskOwnerCap] = createKiosk(tx);
331
+ shareKiosk(tx, kiosk);
332
+ return kioskOwnerCap;
333
+ }
334
+ function shareKiosk(tx, kiosk) {
331
335
  tx.moveCall({
332
336
  target: `0x2::transfer::public_share_object`,
333
337
  typeArguments: [KIOSK_TYPE],
334
338
  arguments: [kiosk]
335
339
  });
336
- return kioskOwnerCap;
337
340
  }
338
341
  function place(tx, itemType, kiosk, kioskCap, item) {
339
342
  tx.moveCall({
@@ -350,7 +353,7 @@ function lock(tx, itemType, kiosk, kioskCap, policy, item) {
350
353
  });
351
354
  }
352
355
  function take(tx, itemType, kiosk, kioskCap, itemId) {
353
- let [item] = tx.moveCall({
356
+ const [item] = tx.moveCall({
354
357
  target: `${KIOSK_MODULE}::take`,
355
358
  typeArguments: [itemType],
356
359
  arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, "address")]
@@ -384,7 +387,7 @@ function placeAndList(tx, itemType, kiosk, kioskCap, item, price) {
384
387
  });
385
388
  }
386
389
  function purchase(tx, itemType, kiosk, itemId, payment) {
387
- let [item, transferRequest] = tx.moveCall({
390
+ const [item, transferRequest] = tx.moveCall({
388
391
  target: `${KIOSK_MODULE}::purchase`,
389
392
  typeArguments: [itemType],
390
393
  arguments: [objArg(tx, kiosk), tx.pure(itemId, "address"), objArg(tx, payment)]
@@ -392,36 +395,15 @@ function purchase(tx, itemType, kiosk, itemId, payment) {
392
395
  return [item, transferRequest];
393
396
  }
394
397
  function withdrawFromKiosk(tx, kiosk, kioskCap, amount) {
395
- let amountArg = amount !== null ? tx.pure(
396
- {
397
- Some: amount
398
- },
399
- "Option<u64>"
400
- ) : tx.pure({ None: true }, "Option<u64>");
401
- let [coin] = tx.moveCall({
398
+ const amountArg = tx.pure(amount ? { Some: amount } : { None: true }, "Option<u64>");
399
+ const [coin] = tx.moveCall({
402
400
  target: `${KIOSK_MODULE}::withdraw`,
403
401
  arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), amountArg]
404
402
  });
405
403
  return coin;
406
404
  }
407
- function borrow(tx, itemType, kiosk, kioskCap, itemId) {
408
- let [item] = tx.moveCall({
409
- target: `${KIOSK_MODULE}::borrow`,
410
- typeArguments: [itemType],
411
- arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, "address")]
412
- });
413
- return item;
414
- }
415
- function borrowMut(tx, itemType, kiosk, kioskCap, itemId) {
416
- let [item] = tx.moveCall({
417
- target: `${KIOSK_MODULE}::borrow_mut`,
418
- typeArguments: [itemType],
419
- arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, "address")]
420
- });
421
- return item;
422
- }
423
405
  function borrowValue(tx, itemType, kiosk, kioskCap, itemId) {
424
- let [item, promise] = tx.moveCall({
406
+ const [item, promise] = tx.moveCall({
425
407
  target: `${KIOSK_MODULE}::borrow_val`,
426
408
  typeArguments: [itemType],
427
409
  arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, "address")]
@@ -435,68 +417,132 @@ function returnValue(tx, itemType, kiosk, item, promise) {
435
417
  arguments: [objArg(tx, kiosk), item, promise]
436
418
  });
437
419
  }
438
- function purchaseAndResolvePolicies(tx, itemType, price, kiosk, itemId, policy, environment, extraParams) {
439
- if (price === void 0 || typeof price !== "string")
440
- throw new Error(`Price of the listing is not supplied.`);
441
- const coin = tx.splitCoins(tx.gas, [tx.pure(price, "u64")]);
442
- const [purchasedItem, transferRequest] = purchase(tx, itemType, kiosk, itemId, coin);
443
- let hasKioskLockRule = false;
444
- for (let rule of policy.rules) {
445
- const ruleWithoutAddr = getTypeWithoutPackageAddress(rule);
446
- switch (ruleWithoutAddr) {
447
- case ROYALTY_RULE:
448
- resolveRoyaltyRule(tx, itemType, price, policy.id, transferRequest, environment);
449
- break;
450
- case KIOSK_LOCK_RULE:
451
- if (!extraParams?.ownedKiosk || !extraParams?.ownedKioskCap)
452
- throw new Error(
453
- `This item type ${itemType} has a 'kiosk_lock_rule', but function call is missing user's kiosk and kioskCap params`
454
- );
455
- hasKioskLockRule = true;
456
- resolveKioskLockRule(
457
- tx,
458
- itemType,
459
- purchasedItem,
460
- extraParams.ownedKiosk,
461
- extraParams.ownedKioskCap,
462
- policy.id,
463
- transferRequest,
464
- environment
465
- );
466
- break;
467
- default:
468
- break;
469
- }
470
- }
471
- confirmRequest(tx, itemType, policy.id, transferRequest);
472
- return {
473
- item: purchasedItem,
474
- canTransfer: !hasKioskLockRule
475
- };
476
- }
477
420
 
478
- // src/tx/rules.ts
479
- function attachRoyaltyRule(tx, type, policy, policyCap, percentageBps, minAmount, environment) {
480
- if (Number(percentageBps) < 0 || Number(percentageBps) > 1e4)
481
- throw new Error("Invalid basis point percentage. Use a value between [0,10000].");
482
- tx.moveCall({
483
- target: `${getRulePackageAddress(environment)}::royalty_rule::add`,
484
- typeArguments: [type],
485
- arguments: [
486
- objArg(tx, policy),
487
- objArg(tx, policyCap),
488
- tx.pure(percentageBps, "u16"),
489
- tx.pure(minAmount, "u64")
490
- ]
421
+ // src/tx/rules/resolve.ts
422
+ function resolveRoyaltyRule(params) {
423
+ const { transactionBlock: txb, itemType, price, packageId, transferRequest, policyId } = params;
424
+ const policyObj = objArg(txb, policyId);
425
+ const [amount] = txb.moveCall({
426
+ target: `${packageId}::royalty_rule::fee_amount`,
427
+ typeArguments: [itemType],
428
+ arguments: [policyObj, objArg(txb, price ?? "")]
429
+ });
430
+ const feeCoin = txb.splitCoins(txb.gas, [amount]);
431
+ txb.moveCall({
432
+ target: `${packageId}::royalty_rule::pay`,
433
+ typeArguments: [itemType],
434
+ arguments: [policyObj, transferRequest, feeCoin]
491
435
  });
492
436
  }
493
- function attachKioskLockRule(tx, type, policy, policyCap, environment) {
494
- tx.moveCall({
495
- target: `${getRulePackageAddress(environment)}::kiosk_lock_rule::add`,
496
- typeArguments: [type],
497
- arguments: [objArg(tx, policy), objArg(tx, policyCap)]
437
+ function resolveKioskLockRule(params) {
438
+ const {
439
+ transactionBlock: txb,
440
+ packageId,
441
+ itemType,
442
+ kiosk,
443
+ kioskCap,
444
+ policyId,
445
+ purchasedItem,
446
+ transferRequest
447
+ } = params;
448
+ if (!kiosk || !kioskCap)
449
+ throw new Error("Missing Owned Kiosk or Owned Kiosk Cap");
450
+ lock(txb, itemType, kiosk, kioskCap, policyId, purchasedItem);
451
+ txb.moveCall({
452
+ target: `${packageId}::kiosk_lock_rule::prove`,
453
+ typeArguments: [itemType],
454
+ arguments: [transferRequest, objArg(txb, kiosk)]
455
+ });
456
+ }
457
+ function resolvePersonalKioskRule(params) {
458
+ const { transactionBlock: txb, packageId, itemType, kiosk, transferRequest } = params;
459
+ if (!kiosk)
460
+ throw new Error("Missing owned Kiosk.");
461
+ txb.moveCall({
462
+ target: `${packageId}::personal_kiosk_rule::prove`,
463
+ typeArguments: [itemType],
464
+ arguments: [objArg(txb, kiosk), transferRequest]
498
465
  });
499
466
  }
467
+ function resolveFloorPriceRule(params) {
468
+ const { transactionBlock: txb, packageId, itemType, policyId, transferRequest } = params;
469
+ txb.moveCall({
470
+ target: `${packageId}::floor_price_rule::prove`,
471
+ typeArguments: [itemType],
472
+ arguments: [objArg(txb, policyId), transferRequest]
473
+ });
474
+ }
475
+
476
+ // src/constants.ts
477
+ var ROYALTY_RULE_ADDRESS = {
478
+ ["testnet" /* TESTNET */]: "bd8fc1947cf119350184107a3087e2dc27efefa0dd82e25a1f699069fe81a585",
479
+ ["mainnet" /* MAINNET */]: "0x434b5bd8f6a7b05fede0ff46c6e511d71ea326ed38056e3bcd681d2d7c2a7879",
480
+ ["custom" /* CUSTOM */]: ""
481
+ };
482
+ var KIOSK_LOCK_RULE_ADDRESS = {
483
+ ["testnet" /* TESTNET */]: "bd8fc1947cf119350184107a3087e2dc27efefa0dd82e25a1f699069fe81a585",
484
+ ["mainnet" /* MAINNET */]: "0x434b5bd8f6a7b05fede0ff46c6e511d71ea326ed38056e3bcd681d2d7c2a7879",
485
+ ["custom" /* CUSTOM */]: ""
486
+ };
487
+ var FLOOR_PRICE_RULE_ADDRESS = {
488
+ ["testnet" /* TESTNET */]: "0x06f6bdd3f2e2e759d8a4b9c252f379f7a05e72dfe4c0b9311cdac27b8eb791b1",
489
+ ["mainnet" /* MAINNET */]: "0x34cc6762780f4f6f153c924c0680cfe2a1fb4601e7d33cc28a92297b62de1e0e",
490
+ ["custom" /* CUSTOM */]: ""
491
+ };
492
+ var PERSONAL_KIOSK_RULE_ADDRESS = {
493
+ ["testnet" /* TESTNET */]: "0x06f6bdd3f2e2e759d8a4b9c252f379f7a05e72dfe4c0b9311cdac27b8eb791b1",
494
+ ["mainnet" /* MAINNET */]: "0x0cb4bcc0560340eb1a1b929cabe56b33fc6449820ec8c1980d69bb98b649b802",
495
+ ["custom" /* CUSTOM */]: ""
496
+ };
497
+ function getBaseRules({
498
+ royaltyRulePackageId,
499
+ kioskLockRulePackageId,
500
+ personalKioskRulePackageId,
501
+ floorPriceRulePackageId
502
+ }) {
503
+ const rules2 = [];
504
+ if (royaltyRulePackageId) {
505
+ rules2.push({
506
+ rule: `${royaltyRulePackageId}::royalty_rule::Rule`,
507
+ packageId: royaltyRulePackageId,
508
+ resolveRuleFunction: resolveRoyaltyRule
509
+ });
510
+ }
511
+ if (kioskLockRulePackageId) {
512
+ rules2.push({
513
+ rule: `${kioskLockRulePackageId}::kiosk_lock_rule::Rule`,
514
+ packageId: kioskLockRulePackageId,
515
+ resolveRuleFunction: resolveKioskLockRule,
516
+ hasLockingRule: true
517
+ });
518
+ }
519
+ if (personalKioskRulePackageId) {
520
+ rules2.push({
521
+ rule: `${personalKioskRulePackageId}::personal_kiosk_rule::Rule`,
522
+ packageId: personalKioskRulePackageId,
523
+ resolveRuleFunction: resolvePersonalKioskRule
524
+ });
525
+ }
526
+ if (floorPriceRulePackageId) {
527
+ rules2.push({
528
+ rule: `${floorPriceRulePackageId}::floor_price_rule::Rule`,
529
+ packageId: floorPriceRulePackageId,
530
+ resolveRuleFunction: resolveFloorPriceRule
531
+ });
532
+ }
533
+ return rules2;
534
+ }
535
+ var testnetRules = getBaseRules({
536
+ royaltyRulePackageId: ROYALTY_RULE_ADDRESS["testnet" /* TESTNET */],
537
+ kioskLockRulePackageId: KIOSK_LOCK_RULE_ADDRESS["testnet" /* TESTNET */],
538
+ personalKioskRulePackageId: PERSONAL_KIOSK_RULE_ADDRESS["testnet" /* TESTNET */],
539
+ floorPriceRulePackageId: FLOOR_PRICE_RULE_ADDRESS["testnet" /* TESTNET */]
540
+ });
541
+ var mainnetRules = getBaseRules({
542
+ royaltyRulePackageId: ROYALTY_RULE_ADDRESS["mainnet" /* MAINNET */],
543
+ kioskLockRulePackageId: KIOSK_LOCK_RULE_ADDRESS["mainnet" /* MAINNET */]
544
+ });
545
+ var rules = [...testnetRules, ...mainnetRules];
500
546
 
501
547
  // src/query/kiosk.ts
502
548
  var import_utils4 = require("@mysten/sui.js/utils");
@@ -504,20 +550,22 @@ async function fetchKiosk(client, kioskId, pagination, options) {
504
550
  const data = await getAllDynamicFields(client, kioskId, pagination);
505
551
  const listings = [];
506
552
  const lockedItemIds = [];
507
- const kioskData = extractKioskData(data, listings, lockedItemIds);
508
- const [kiosk, listingObjects] = await Promise.all([
553
+ const kioskData = extractKioskData(data, listings, lockedItemIds, kioskId);
554
+ const [kiosk, listingObjects, items] = await Promise.all([
509
555
  options.withKioskFields ? getKioskObject(client, kioskId) : Promise.resolve(void 0),
510
- options.withListingPrices ? client.multiGetObjects({
511
- ids: kioskData.listingIds,
512
- options: {
513
- showContent: true
514
- }
515
- }) : Promise.resolve([])
556
+ options.withListingPrices ? getAllObjects(client, kioskData.listingIds, {
557
+ showContent: true
558
+ }) : Promise.resolve([]),
559
+ options.withObjects ? getAllObjects(client, kioskData.itemIds, options.objectOptions || { showDisplay: true }) : Promise.resolve([])
516
560
  ]);
517
561
  if (options.withKioskFields)
518
562
  kioskData.kiosk = kiosk;
519
563
  attachListingsAndPrices(kioskData, listings, listingObjects);
520
564
  attachLockedItems(kioskData, lockedItemIds);
565
+ attachObjects(
566
+ kioskData,
567
+ items.filter((x) => !!x.data).map((x) => x.data)
568
+ );
521
569
  return {
522
570
  data: kioskData,
523
571
  nextCursor: null,
@@ -532,23 +580,37 @@ async function getOwnedKiosks(client, address, options) {
532
580
  kioskOwnerCaps: [],
533
581
  kioskIds: []
534
582
  };
583
+ const filter = {
584
+ MatchAny: [
585
+ {
586
+ StructType: KIOSK_OWNER_CAP
587
+ }
588
+ ]
589
+ };
590
+ if (options?.personalKioskType) {
591
+ filter.MatchAny.push({
592
+ StructType: options.personalKioskType
593
+ });
594
+ }
535
595
  const { data, hasNextPage, nextCursor } = await client.getOwnedObjects({
536
596
  owner: address,
537
- filter: { StructType: KIOSK_OWNER_CAP },
597
+ filter,
538
598
  options: {
539
- showContent: true
599
+ showContent: true,
600
+ showType: true
540
601
  },
541
602
  ...options?.pagination || {}
542
603
  });
543
604
  const kioskIdList = data?.map((x) => {
544
605
  const fields = x.data?.content?.dataType === "moveObject" ? x.data.content.fields : null;
545
- return fields?.for;
606
+ return fields?.cap ? fields?.cap?.fields?.for : fields?.for;
546
607
  });
547
608
  const filteredData = data.filter((x) => "data" in x).map((x) => x.data);
548
609
  return {
549
610
  nextCursor,
550
611
  hasNextPage,
551
612
  kioskOwnerCaps: filteredData.map((x, idx) => ({
613
+ isPersonal: x.type !== KIOSK_OWNER_CAP,
552
614
  digest: x.digest,
553
615
  version: x.version,
554
616
  objectId: x.objectId,
@@ -559,6 +621,7 @@ async function getOwnedKiosks(client, address, options) {
559
621
  }
560
622
 
561
623
  // src/query/transfer-policy.ts
624
+ var import_utils6 = require("@mysten/sui.js/utils");
562
625
  async function queryTransferPolicy(client, type) {
563
626
  const { data } = await client.queryEvents({
564
627
  query: {
@@ -574,7 +637,7 @@ async function queryTransferPolicy(client, type) {
574
637
  if (!policy || !policy.bcs || !("bcsBytes" in policy.bcs)) {
575
638
  throw new Error(`Invalid policy: ${policy?.objectId}, expected object, got package`);
576
639
  }
577
- let parsed = import_bcs.bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, "base64");
640
+ const parsed = import_bcs.bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, "base64");
578
641
  return {
579
642
  id: policy?.objectId,
580
643
  type: `${TRANSFER_POLICY_TYPE}<${type}>`,
@@ -584,61 +647,988 @@ async function queryTransferPolicy(client, type) {
584
647
  };
585
648
  });
586
649
  }
650
+ async function queryTransferPolicyCapsByType(client, address, type) {
651
+ if (!(0, import_utils6.isValidSuiAddress)(address))
652
+ return [];
653
+ const filter = {
654
+ MatchAll: [
655
+ {
656
+ StructType: `${TRANSFER_POLICY_CAP_TYPE}<${type}>`
657
+ }
658
+ ]
659
+ };
660
+ const data = await getAllOwnedObjects({
661
+ client,
662
+ filter,
663
+ owner: address
664
+ });
665
+ return data.map((item) => parseTransferPolicyCapObject(item)).filter((item) => !!item);
666
+ }
667
+ async function queryOwnedTransferPolicies(client, address) {
668
+ if (!(0, import_utils6.isValidSuiAddress)(address))
669
+ return;
670
+ const filter = {
671
+ MatchAll: [
672
+ {
673
+ MoveModule: {
674
+ module: "transfer_policy",
675
+ package: "0x2"
676
+ }
677
+ }
678
+ ]
679
+ };
680
+ const data = await getAllOwnedObjects({ client, owner: address, filter });
681
+ const policies = [];
682
+ for (const item of data) {
683
+ const data2 = parseTransferPolicyCapObject(item);
684
+ if (data2)
685
+ policies.push(data2);
686
+ }
687
+ return policies;
688
+ }
689
+
690
+ // src/client/kiosk-client.ts
691
+ var KioskClient = class {
692
+ constructor(options) {
693
+ this.client = options.client;
694
+ this.network = options.network;
695
+ this.rules = rules;
696
+ this.packageIds = options.packageIds;
697
+ if (options.packageIds)
698
+ this.rules.push(...getBaseRules(options.packageIds));
699
+ }
700
+ /// Querying
701
+ /**
702
+ * Get an addresses's owned kiosks.
703
+ * @param address The address for which we want to retrieve the kiosks.
704
+ * @returns An Object containing all the `kioskOwnerCap` objects as well as the kioskIds.
705
+ */
706
+ async getOwnedKiosks({ address }) {
707
+ const personalPackageId = this.packageIds?.personalKioskRulePackageId || PERSONAL_KIOSK_RULE_ADDRESS[this.network];
708
+ return getOwnedKiosks(this.client, address, {
709
+ personalKioskType: personalPackageId ? `${personalPackageId}::personal_kiosk::PersonalKioskCap` : ""
710
+ });
711
+ }
712
+ /**
713
+ * Fetches the kiosk contents.
714
+ * @param kioskId The ID of the kiosk to fetch.
715
+ * @param options Optioal
716
+ * @returns
717
+ */
718
+ async getKiosk({ id, options }) {
719
+ return (await fetchKiosk(
720
+ this.client,
721
+ id,
722
+ {
723
+ limit: 1e3
724
+ },
725
+ options || {}
726
+ )).data;
727
+ }
728
+ /**
729
+ * Query the Transfer Policy(ies) for type `T`.
730
+ * @param type The Type we're querying for (E.g `0xMyAddress::hero::Hero`)
731
+ */
732
+ async getTransferPolicies({ type }) {
733
+ return queryTransferPolicy(this.client, type);
734
+ }
735
+ /**
736
+ * Query all the owned transfer policies for an address.
737
+ * Returns `TransferPolicyCap` which uncludes `policyId, policyCapId, type`.
738
+ * @param address The address we're searching the owned transfer policies for.
739
+ */
740
+ async getOwnedTransferPolicies({ address }) {
741
+ return queryOwnedTransferPolicies(this.client, address);
742
+ }
743
+ /**
744
+ * Query the Transfer Policy Cap for type `T`, owned by `address`
745
+ * @param type The Type `T` for the object
746
+ * @param address The address that owns the cap.
747
+ */
748
+ async getOwnedTransferPoliciesByType({ type, address }) {
749
+ return queryTransferPolicyCapsByType(this.client, address, type);
750
+ }
751
+ // Someone would just have to create a `kiosk-client.ts` file in their project, initialize a KioskClient
752
+ // and call the `addRuleResolver` function. Each rule has a `resolve` function.
753
+ // The resolve function is automatically called on `purchaseAndResolve` function call.
754
+ addRuleResolver(rule) {
755
+ if (this.rules.find((x) => x.rule === rule.rule))
756
+ throw new Error(`Rule ${rule.rule} resolver already exists.`);
757
+ this.rules.push(rule);
758
+ }
759
+ /**
760
+ * A convenient helper to get the packageIds for our supported ruleset,
761
+ * based on `kioskClient` configuration.
762
+ */
763
+ getRulePackageId(rule) {
764
+ const rules2 = this.packageIds || {};
765
+ const network = this.network;
766
+ if (!rules2[rule] && network !== "mainnet" /* MAINNET */ && network !== "testnet" /* TESTNET */) {
767
+ throw new Error(`Missing packageId for rule ${rule}`);
768
+ }
769
+ switch (rule) {
770
+ case "kioskLockRulePackageId":
771
+ return rules2[rule] || KIOSK_LOCK_RULE_ADDRESS[network];
772
+ case "royaltyRulePackageId":
773
+ return rules2[rule] || ROYALTY_RULE_ADDRESS[network];
774
+ case "personalKioskRulePackageId":
775
+ return rules2[rule] || PERSONAL_KIOSK_RULE_ADDRESS[network];
776
+ case "floorPriceRulePackageId":
777
+ return rules2[rule] || FLOOR_PRICE_RULE_ADDRESS[network];
778
+ }
779
+ }
780
+ };
781
+
782
+ // src/tx/rules/attach.ts
783
+ function attachKioskLockRuleTx(tx, type, policy, policyCap, packageId) {
784
+ tx.moveCall({
785
+ target: `${packageId}::kiosk_lock_rule::add`,
786
+ typeArguments: [type],
787
+ arguments: [objArg(tx, policy), objArg(tx, policyCap)]
788
+ });
789
+ }
790
+ function attachRoyaltyRuleTx(tx, type, policy, policyCap, percentageBps, minAmount, packageId) {
791
+ if (Number(percentageBps) < 0 || Number(percentageBps) > 1e4)
792
+ throw new Error("Invalid basis point percentage. Use a value between [0,10000].");
793
+ tx.moveCall({
794
+ target: `${packageId}::royalty_rule::add`,
795
+ typeArguments: [type],
796
+ arguments: [
797
+ objArg(tx, policy),
798
+ objArg(tx, policyCap),
799
+ tx.pure(percentageBps, "u16"),
800
+ tx.pure(minAmount, "u64")
801
+ ]
802
+ });
803
+ }
804
+ function attachPersonalKioskRuleTx(tx, type, policy, policyCap, packageId) {
805
+ tx.moveCall({
806
+ target: `${packageId}::personal_kiosk_rule::add`,
807
+ typeArguments: [type],
808
+ arguments: [objArg(tx, policy), objArg(tx, policyCap)]
809
+ });
810
+ }
811
+ function attachFloorPriceRuleTx(tx, type, policy, policyCap, minPrice, packageId) {
812
+ tx.moveCall({
813
+ target: `${packageId}::floor_price_rule::add`,
814
+ typeArguments: [type],
815
+ arguments: [objArg(tx, policy), objArg(tx, policyCap), tx.pure(minPrice, "u64")]
816
+ });
817
+ }
818
+
819
+ // src/tx/transfer-policy.ts
820
+ function createTransferPolicy(tx, itemType, publisher) {
821
+ const [transferPolicy, transferPolicyCap] = createTransferPolicyWithoutSharing(
822
+ tx,
823
+ itemType,
824
+ publisher
825
+ );
826
+ shareTransferPolicy(tx, itemType, transferPolicy);
827
+ return transferPolicyCap;
828
+ }
829
+ function createTransferPolicyWithoutSharing(tx, itemType, publisher) {
830
+ const [transferPolicy, transferPolicyCap] = tx.moveCall({
831
+ target: `${TRANSFER_POLICY_MODULE}::new`,
832
+ typeArguments: [itemType],
833
+ arguments: [objArg(tx, publisher)]
834
+ });
835
+ return [transferPolicy, transferPolicyCap];
836
+ }
837
+ function shareTransferPolicy(tx, itemType, transferPolicy) {
838
+ tx.moveCall({
839
+ target: `0x2::transfer::public_share_object`,
840
+ typeArguments: [`${TRANSFER_POLICY_TYPE}<${itemType}>`],
841
+ arguments: [transferPolicy]
842
+ });
843
+ }
844
+ function withdrawFromPolicy(tx, itemType, policy, policyCap, amount) {
845
+ const amountArg = tx.pure(amount ? { Some: amount } : { None: true }, "Option<u64>");
846
+ const [profits] = tx.moveCall({
847
+ target: `${TRANSFER_POLICY_MODULE}::withdraw`,
848
+ typeArguments: [itemType],
849
+ arguments: [objArg(tx, policy), objArg(tx, policyCap), amountArg]
850
+ });
851
+ return profits;
852
+ }
853
+ function confirmRequest(tx, itemType, policy, request) {
854
+ tx.moveCall({
855
+ target: `${TRANSFER_POLICY_MODULE}::confirm_request`,
856
+ typeArguments: [itemType],
857
+ arguments: [objArg(tx, policy), request]
858
+ });
859
+ }
860
+ function removeTransferPolicyRule(tx, itemType, ruleType, configType, policy, policyCap) {
861
+ tx.moveCall({
862
+ target: `${TRANSFER_POLICY_MODULE}::remove_rule`,
863
+ typeArguments: [itemType, ruleType, configType],
864
+ arguments: [objArg(tx, policy), objArg(tx, policyCap)]
865
+ });
866
+ }
867
+
868
+ // src/client/tp-transaction.ts
869
+ var _validateInputs, validateInputs_fn, _setup, setup_fn;
870
+ var TransferPolicyTransaction = class {
871
+ constructor({ kioskClient, transactionBlock, cap }) {
872
+ // Internal function that that the policy's Id + Cap + type have been set.
873
+ __privateAdd(this, _validateInputs);
874
+ /**
875
+ * Setup the state of the TransferPolicyTransaction.
876
+ */
877
+ __privateAdd(this, _setup);
878
+ this.kioskClient = kioskClient;
879
+ this.transactionBlock = transactionBlock;
880
+ if (cap)
881
+ this.setCap(cap);
882
+ }
883
+ /**
884
+ * A function to create a new transfer policy.
885
+ * Checks if there's already an existing transfer policy to prevent
886
+ * double transfer polciy mistakes.
887
+ * There's an optional `skipCheck` flag that will just create the policy
888
+ * without checking
889
+ *
890
+ * @param type The Type (`T`) for which we're creating the transfer policy.
891
+ * @param publisher The Publisher Object Id.
892
+ * @param address Address to save the `TransferPolicyCap` object to.
893
+ * @param skipCheck (Optional) skip checking if a transfer policy already exists
894
+ */
895
+ async createAndShare({
896
+ type,
897
+ publisher,
898
+ address,
899
+ skipCheck
900
+ }) {
901
+ if (!skipCheck) {
902
+ const policies = await this.kioskClient.getTransferPolicies({ type });
903
+ if (policies.length > 0)
904
+ throw new Error("There's already transfer policy for this Type.");
905
+ }
906
+ const cap = createTransferPolicy(this.transactionBlock, type, publisher);
907
+ this.transactionBlock.transferObjects([cap], this.transactionBlock.pure(address, "address"));
908
+ }
909
+ /**
910
+ * A convenient function to create a Transfer Policy and attach some rules
911
+ * before sharing it (so you can prepare it in a single PTB)
912
+ * @param type The Type (`T`) for which we're creating the transfer policy.
913
+ * @param publisher The Publisher Object Id.
914
+ * @param address Address to save the `TransferPolicyCap` object to.
915
+ * @param skipCheck (Optional) skip checking if a transfer policy already exists
916
+ */
917
+ async create({
918
+ type,
919
+ publisher,
920
+ skipCheck
921
+ }) {
922
+ if (!skipCheck) {
923
+ const policies = await this.kioskClient.getTransferPolicies({ type });
924
+ if (policies.length > 0)
925
+ throw new Error("There's already transfer policy for this Type.");
926
+ }
927
+ const [policy, policyCap] = createTransferPolicyWithoutSharing(
928
+ this.transactionBlock,
929
+ type,
930
+ publisher
931
+ );
932
+ __privateMethod(this, _setup, setup_fn).call(this, policy, policyCap, type);
933
+ return this;
934
+ }
935
+ /**
936
+ * This can be called after calling the `create` function to share the `TransferPolicy`,
937
+ * and transfer the `TransferPolicyCap` to the specified address
938
+ *
939
+ * @param address The address to transfer the `TransferPolicyCap`
940
+ */
941
+ shareAndTransferCap(address) {
942
+ if (!this.type || !this.policyCap || !this.policy)
943
+ throw new Error("This function can only be called after `transferPolicyManager.create`");
944
+ shareTransferPolicy(this.transactionBlock, this.type, this.policy);
945
+ this.transactionBlock.transferObjects(
946
+ [this.policyCap],
947
+ this.transactionBlock.pure(address, "address")
948
+ );
949
+ }
950
+ /**
951
+ * Setup the TransferPolicy by passing a `cap` returned from `kioskClient.getOwnedTransferPolicies` or
952
+ * `kioskClient.getOwnedTransferPoliciesByType`.
953
+ * @param policyCapId The `TransferPolicyCap`
954
+ */
955
+ setCap({ policyId, policyCapId, type }) {
956
+ return __privateMethod(this, _setup, setup_fn).call(this, policyId, policyCapId, type);
957
+ }
958
+ /**
959
+ * Withdraw from the transfer policy's profits.
960
+ * @param address Address to transfer the profits to.
961
+ * @param amount (Optional) amount parameter. Will withdraw all profits if the amount is not specified.
962
+ */
963
+ withdraw(address, amount) {
964
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
965
+ const coin = withdrawFromPolicy(
966
+ this.transactionBlock,
967
+ this.type,
968
+ this.policy,
969
+ this.policyCap,
970
+ amount
971
+ );
972
+ this.transactionBlock.transferObjects([coin], this.transactionBlock.pure(address, "address"));
973
+ return this;
974
+ }
975
+ /**
976
+ * Adds the Kiosk Royalty rule to the Transfer Policy.
977
+ * You can pass the percentage, as well as a minimum amount.
978
+ * The royalty that will be paid is the MAX(percentage, minAmount).
979
+ * You can pass 0 in either value if you want only percentage royalty, or a fixed amount fee.
980
+ * (but you should define at least one of them for the rule to make sense).
981
+ *
982
+ * @param percentageBps The royalty percentage in basis points. Use `percentageToBasisPoints` helper to convert from percentage [0,100].
983
+ * @param minAmount The minimum royalty amount per request in MIST.
984
+ */
985
+ addRoyaltyRule(percentageBps, minAmount) {
986
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
987
+ attachRoyaltyRuleTx(
988
+ this.transactionBlock,
989
+ this.type,
990
+ this.policy,
991
+ this.policyCap,
992
+ percentageBps,
993
+ minAmount,
994
+ this.kioskClient.getRulePackageId("royaltyRulePackageId")
995
+ );
996
+ return this;
997
+ }
998
+ /**
999
+ * Adds the Kiosk Lock Rule to the Transfer Policy.
1000
+ * This Rule forces buyer to lock the item in the kiosk, preserving strong royalties.
1001
+ */
1002
+ addLockRule() {
1003
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
1004
+ attachKioskLockRuleTx(
1005
+ this.transactionBlock,
1006
+ this.type,
1007
+ this.policy,
1008
+ this.policyCap,
1009
+ this.kioskClient.getRulePackageId("kioskLockRulePackageId")
1010
+ );
1011
+ return this;
1012
+ }
1013
+ /**
1014
+ * Attaches the Personal Kiosk Rule, making a purchase valid only for `SoulBound` kiosks.
1015
+ */
1016
+ addPersonalKioskRule() {
1017
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
1018
+ attachPersonalKioskRuleTx(
1019
+ this.transactionBlock,
1020
+ this.type,
1021
+ this.policy,
1022
+ this.policyCap,
1023
+ this.kioskClient.getRulePackageId("personalKioskRulePackageId")
1024
+ );
1025
+ return this;
1026
+ }
1027
+ /**
1028
+ * A function to add the floor price rule to a transfer policy.
1029
+ * @param minPrice The minimum price in MIST.
1030
+ */
1031
+ addFloorPriceRule(minPrice) {
1032
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
1033
+ attachFloorPriceRuleTx(
1034
+ this.transactionBlock,
1035
+ this.type,
1036
+ this.policy,
1037
+ this.policyCap,
1038
+ minPrice,
1039
+ this.kioskClient.getRulePackageId("floorPriceRulePackageId")
1040
+ );
1041
+ return this;
1042
+ }
1043
+ /**
1044
+ * Generic helper to remove a rule, not from the SDK's base ruleset.
1045
+ * @param ruleType The Rule Type
1046
+ * @param configType The Config Type
1047
+ */
1048
+ removeRule({ ruleType, configType }) {
1049
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
1050
+ removeTransferPolicyRule(
1051
+ this.transactionBlock,
1052
+ this.type,
1053
+ ruleType,
1054
+ configType,
1055
+ this.policy,
1056
+ this.policyCap
1057
+ );
1058
+ }
1059
+ /**
1060
+ * Removes the lock rule.
1061
+ */
1062
+ removeLockRule() {
1063
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
1064
+ const packageId = this.kioskClient.getRulePackageId("kioskLockRulePackageId");
1065
+ removeTransferPolicyRule(
1066
+ this.transactionBlock,
1067
+ this.type,
1068
+ `${packageId}::kiosk_lock_rule::Rule`,
1069
+ `${packageId}::kiosk_lock_rule::Config`,
1070
+ this.policy,
1071
+ this.policyCap
1072
+ );
1073
+ return this;
1074
+ }
1075
+ /**
1076
+ * Removes the Royalty rule
1077
+ */
1078
+ removeRoyaltyRule() {
1079
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
1080
+ const packageId = this.kioskClient.getRulePackageId("royaltyRulePackageId");
1081
+ removeTransferPolicyRule(
1082
+ this.transactionBlock,
1083
+ this.type,
1084
+ `${packageId}::royalty_rule::Rule`,
1085
+ `${packageId}::royalty_rule::Config`,
1086
+ this.policy,
1087
+ this.policyCap
1088
+ );
1089
+ return this;
1090
+ }
1091
+ removePersonalKioskRule() {
1092
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
1093
+ const packageId = this.kioskClient.getRulePackageId("personalKioskRulePackageId");
1094
+ removeTransferPolicyRule(
1095
+ this.transactionBlock,
1096
+ this.type,
1097
+ `${packageId}::personal_kiosk_rule::Rule`,
1098
+ `bool`,
1099
+ this.policy,
1100
+ this.policyCap
1101
+ );
1102
+ return this;
1103
+ }
1104
+ removeFloorPriceRule() {
1105
+ __privateMethod(this, _validateInputs, validateInputs_fn).call(this);
1106
+ const packageId = this.kioskClient.getRulePackageId("floorPriceRulePackageId");
1107
+ removeTransferPolicyRule(
1108
+ this.transactionBlock,
1109
+ this.type,
1110
+ `${packageId}::floor_price_rule::Rule`,
1111
+ `${packageId}::floor_price_rule::Config`,
1112
+ this.policy,
1113
+ this.policyCap
1114
+ );
1115
+ return this;
1116
+ }
1117
+ getPolicy() {
1118
+ if (!this.policy)
1119
+ throw new Error("Policy not set.");
1120
+ return this.policy;
1121
+ }
1122
+ getPolicyCap() {
1123
+ if (!this.policyCap)
1124
+ throw new Error("Transfer Policy Cap not set.");
1125
+ return this.policyCap;
1126
+ }
1127
+ };
1128
+ _validateInputs = new WeakSet();
1129
+ validateInputs_fn = function() {
1130
+ const genericErrorMessage = `Please use 'setCap()' to setup the TransferPolicy.`;
1131
+ if (!this.policy)
1132
+ throw new Error(`${genericErrorMessage} Missing: Transfer Policy Object.`);
1133
+ if (!this.policyCap)
1134
+ throw new Error(`${genericErrorMessage} Missing: TransferPolicyCap Object ID`);
1135
+ if (!this.type)
1136
+ throw new Error(
1137
+ `${genericErrorMessage} Missing: Transfer Policy object type (e.g. {packageId}::item::Item)`
1138
+ );
1139
+ };
1140
+ _setup = new WeakSet();
1141
+ setup_fn = function(policyId, policyCap, type) {
1142
+ this.policy = policyId;
1143
+ this.policyCap = policyCap;
1144
+ this.type = type;
1145
+ return this;
1146
+ };
1147
+
1148
+ // src/tx/personal-kiosk.ts
1149
+ function convertToPersonalTx(tx, kiosk, kioskOwnerCap, packageId) {
1150
+ const personalKioskCap = tx.moveCall({
1151
+ target: `${packageId}::personal_kiosk::new`,
1152
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskOwnerCap)]
1153
+ });
1154
+ return personalKioskCap;
1155
+ }
1156
+ function transferPersonalCapTx(tx, personalKioskCap, packageId) {
1157
+ tx.moveCall({
1158
+ target: `${packageId}::personal_kiosk::transfer_to_sender`,
1159
+ arguments: [personalKioskCap]
1160
+ });
1161
+ }
1162
+
1163
+ // src/client/kiosk-transaction.ts
1164
+ var _pendingShare, _pendingTransfer, _promise, _personalCap, _finalized, _borrowFromPersonalCap, borrowFromPersonalCap_fn, _setPendingStatuses, setPendingStatuses_fn, _validateKioskIsSet, validateKioskIsSet_fn, _validateFinalizedStatus, validateFinalizedStatus_fn;
1165
+ var KioskTransaction = class {
1166
+ constructor({ transactionBlock, kioskClient, cap }) {
1167
+ /**
1168
+ * A function to borrow from `personalCap`.
1169
+ */
1170
+ __privateAdd(this, _borrowFromPersonalCap);
1171
+ __privateAdd(this, _setPendingStatuses);
1172
+ __privateAdd(this, _validateKioskIsSet);
1173
+ // Validates that `finalize`
1174
+ __privateAdd(this, _validateFinalizedStatus);
1175
+ // If we're pending `share` of a new kiosk, `finalize()` will share it.
1176
+ __privateAdd(this, _pendingShare, void 0);
1177
+ // If we're pending transferring of the cap, `finalize()` will either error or transfer the cap if it's a new personal.
1178
+ __privateAdd(this, _pendingTransfer, void 0);
1179
+ // The promise that the personalCap will be returned on `finalize()`.
1180
+ __privateAdd(this, _promise, void 0);
1181
+ // The personal kiosk argument.
1182
+ __privateAdd(this, _personalCap, void 0);
1183
+ // A flag that checks whether kiosk TX is finalized.
1184
+ __privateAdd(this, _finalized, false);
1185
+ this.transactionBlock = transactionBlock;
1186
+ this.kioskClient = kioskClient;
1187
+ if (cap)
1188
+ this.setCap(cap);
1189
+ }
1190
+ /**
1191
+ * Creates a kiosk and saves `kiosk` and `kioskOwnerCap` in state.
1192
+ * Helpful if we want to chain some actions before sharing + transferring the cap to the specified address.
1193
+ * @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
1194
+ */
1195
+ create() {
1196
+ __privateMethod(this, _validateFinalizedStatus, validateFinalizedStatus_fn).call(this);
1197
+ __privateMethod(this, _setPendingStatuses, setPendingStatuses_fn).call(this, {
1198
+ share: true,
1199
+ transfer: true
1200
+ });
1201
+ const [kiosk, cap] = createKiosk(this.transactionBlock);
1202
+ this.kiosk = kiosk;
1203
+ this.kioskCap = cap;
1204
+ return this;
1205
+ }
1206
+ /**
1207
+ * Creates a personal kiosk & shares it.
1208
+ * The `PersonalKioskCap` is transferred to the signer.
1209
+ * @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
1210
+ */
1211
+ createPersonal(borrow) {
1212
+ __privateSet(this, _pendingShare, true);
1213
+ return this.create().convertToPersonal(borrow);
1214
+ }
1215
+ /**
1216
+ * Converts a kiosk to a Personal (Soulbound) Kiosk.
1217
+ * Requires initialization by either calling `ktxb.create()` or `ktxb.setCap()`.
1218
+ */
1219
+ convertToPersonal(borrow) {
1220
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1221
+ const cap = convertToPersonalTx(
1222
+ this.transactionBlock,
1223
+ this.kiosk,
1224
+ this.kioskCap,
1225
+ this.kioskClient.getRulePackageId("personalKioskRulePackageId")
1226
+ );
1227
+ if (borrow)
1228
+ __privateMethod(this, _borrowFromPersonalCap, borrowFromPersonalCap_fn).call(this, cap);
1229
+ else
1230
+ __privateSet(this, _personalCap, cap);
1231
+ __privateMethod(this, _setPendingStatuses, setPendingStatuses_fn).call(this, { transfer: true });
1232
+ return this;
1233
+ }
1234
+ /**
1235
+ * Single function way to create a kiosk, share it and transfer the cap to the specified address.
1236
+ */
1237
+ createAndShare(address) {
1238
+ __privateMethod(this, _validateFinalizedStatus, validateFinalizedStatus_fn).call(this);
1239
+ const cap = createKioskAndShare(this.transactionBlock);
1240
+ this.transactionBlock.transferObjects([cap], this.transactionBlock.pure(address, "address"));
1241
+ }
1242
+ /**
1243
+ * Shares the kiosk.
1244
+ */
1245
+ share() {
1246
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1247
+ __privateMethod(this, _setPendingStatuses, setPendingStatuses_fn).call(this, { share: false });
1248
+ shareKiosk(this.transactionBlock, this.kiosk);
1249
+ }
1250
+ /**
1251
+ * Should be called only after `create` is called.
1252
+ * It shares the kiosk & transfers the cap to the specified address.
1253
+ */
1254
+ shareAndTransferCap(address) {
1255
+ if (__privateGet(this, _personalCap))
1256
+ throw new Error("You can only call `shareAndTransferCap` on a non-personal kiosk.");
1257
+ __privateMethod(this, _setPendingStatuses, setPendingStatuses_fn).call(this, { transfer: false });
1258
+ this.share();
1259
+ this.transactionBlock.transferObjects(
1260
+ [this.kioskCap],
1261
+ this.transactionBlock.pure(address, "address")
1262
+ );
1263
+ }
1264
+ /**
1265
+ * A function to borrow an item from a kiosk & execute any function with it.
1266
+ * Example: You could borrow a Fren out of a kiosk, attach an accessory (or mix), and return it.
1267
+ */
1268
+ borrowTx({ itemType, itemId }, callback) {
1269
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1270
+ const [itemObj, promise] = borrowValue(
1271
+ this.transactionBlock,
1272
+ itemType,
1273
+ this.kiosk,
1274
+ this.kioskCap,
1275
+ itemId
1276
+ );
1277
+ callback(itemObj);
1278
+ this.return({ itemType, item: itemObj, promise });
1279
+ }
1280
+ /**
1281
+ * Borrows an item from the kiosk.
1282
+ * This will fail if the item is listed for sale.
1283
+ *
1284
+ * Requires calling `return`.
1285
+ */
1286
+ borrow({ itemType, itemId }) {
1287
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1288
+ const [itemObj, promise] = borrowValue(
1289
+ this.transactionBlock,
1290
+ itemType,
1291
+ this.kiosk,
1292
+ this.kioskCap,
1293
+ itemId
1294
+ );
1295
+ return [itemObj, promise];
1296
+ }
1297
+ /**
1298
+ * Returns the item back to the kiosk.
1299
+ * Accepts the parameters returned from the `borrow` function.
1300
+ */
1301
+ return({ itemType, item, promise }) {
1302
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1303
+ returnValue(this.transactionBlock, itemType, this.kiosk, item, promise);
1304
+ return this;
1305
+ }
1306
+ /**
1307
+ * A function to withdraw from kiosk
1308
+ * @param address Where to trasnfer the coin.
1309
+ * @param amount The amount we aim to withdraw.
1310
+ */
1311
+ withdraw(address, amount) {
1312
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1313
+ const coin = withdrawFromKiosk(
1314
+ this.transactionBlock,
1315
+ this.kiosk,
1316
+ this.kioskCap,
1317
+ amount
1318
+ );
1319
+ this.transactionBlock.transferObjects([coin], this.transactionBlock.pure(address, "address"));
1320
+ return this;
1321
+ }
1322
+ /**
1323
+ * A function to place an item in the kiosk.
1324
+ * @param itemType The type `T` of the item
1325
+ * @param item The ID or Transaction Argument of the item
1326
+ */
1327
+ place({ itemType, item }) {
1328
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1329
+ place(this.transactionBlock, itemType, this.kiosk, this.kioskCap, item);
1330
+ return this;
1331
+ }
1332
+ /**
1333
+ * A function to place an item in the kiosk and list it for sale in one transaction.
1334
+ * @param itemType The type `T` of the item
1335
+ * @param item The ID or Transaction Argument of the item
1336
+ * @param price The price in MIST
1337
+ */
1338
+ placeAndList({ itemType, item, price }) {
1339
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1340
+ placeAndList(this.transactionBlock, itemType, this.kiosk, this.kioskCap, item, price);
1341
+ return this;
1342
+ }
1343
+ /**
1344
+ * A function to list an item in the kiosk.
1345
+ * @param itemType The type `T` of the item
1346
+ * @param itemId The ID of the item
1347
+ * @param price The price in MIST
1348
+ */
1349
+ list({ itemType, itemId, price }) {
1350
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1351
+ list(this.transactionBlock, itemType, this.kiosk, this.kioskCap, itemId, price);
1352
+ return this;
1353
+ }
1354
+ /**
1355
+ * A function to delist an item from the kiosk.
1356
+ * @param itemType The type `T` of the item
1357
+ * @param itemId The ID of the item
1358
+ */
1359
+ delist({ itemType, itemId }) {
1360
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1361
+ delist(this.transactionBlock, itemType, this.kiosk, this.kioskCap, itemId);
1362
+ return this;
1363
+ }
1364
+ /**
1365
+ * A function to take an item from the kiosk. The transaction won't succeed if the item is listed or locked.
1366
+
1367
+ * @param itemType The type `T` of the item
1368
+ * @param itemId The ID of the item
1369
+ */
1370
+ take({ itemType, itemId }) {
1371
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1372
+ return take(this.transactionBlock, itemType, this.kiosk, this.kioskCap, itemId);
1373
+ }
1374
+ /**
1375
+ * Transfer a non-locked/non-listed item to an address.
1376
+ *
1377
+ * @param itemType The type `T` of the item
1378
+ * @param itemId The ID of the item
1379
+ * @param address The destination address
1380
+ */
1381
+ transfer({ itemType, itemId, address }) {
1382
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1383
+ const item = this.take({ itemType, itemId });
1384
+ this.transactionBlock.transferObjects([item], this.transactionBlock.pure(address, "address"));
1385
+ return this;
1386
+ }
1387
+ /**
1388
+ * A function to take lock an item in the kiosk.
1389
+
1390
+ * @param itemType The type `T` of the item
1391
+ * @param itemId The ID of the item
1392
+ * @param policy The Policy ID or Transaction Argument for item T
1393
+ */
1394
+ lock({ itemType, itemId, policy }) {
1395
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1396
+ lock(this.transactionBlock, itemType, this.kiosk, this.kioskCap, policy, itemId);
1397
+ return this;
1398
+ }
1399
+ /**
1400
+ * Purchase an item from a seller's kiosk.
1401
+ * Returns [item, transferRequest]
1402
+ * Can be called like: `const [item, transferRequest] = kioskTx.purchase({...})`
1403
+ * @param itemType The type `T` of the item
1404
+ * @param itemId The ID of the item
1405
+ * @param price The price in MIST
1406
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
1407
+ */
1408
+ purchase({
1409
+ itemType,
1410
+ itemId,
1411
+ price,
1412
+ sellerKiosk
1413
+ }) {
1414
+ const coin = this.transactionBlock.splitCoins(this.transactionBlock.gas, [
1415
+ this.transactionBlock.pure(price, "u64")
1416
+ ]);
1417
+ return purchase(this.transactionBlock, itemType, sellerKiosk, itemId, coin);
1418
+ }
1419
+ /**
1420
+ * A function to purchase and resolve a transfer policy.
1421
+ * If the transfer policy has the `lock` rule, the item is locked in the kiosk.
1422
+ * Otherwise, the item is placed in the kiosk.
1423
+ * @param itemType The type of the item
1424
+ * @param itemId The id of the item
1425
+ * @param price The price of the specified item
1426
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
1427
+ * @param extraArgs Used to pass arguments for custom rule resolvers.
1428
+ */
1429
+ async purchaseAndResolve({
1430
+ itemType,
1431
+ itemId,
1432
+ price,
1433
+ sellerKiosk,
1434
+ extraArgs
1435
+ }) {
1436
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1437
+ const policies = await this.kioskClient.getTransferPolicies({ type: itemType });
1438
+ if (policies.length === 0) {
1439
+ throw new Error(
1440
+ `The type ${itemType} doesn't have a Transfer Policy so it can't be traded through kiosk.`
1441
+ );
1442
+ }
1443
+ const policy = policies[0];
1444
+ const [purchasedItem, transferRequest] = this.purchase({
1445
+ itemType,
1446
+ itemId,
1447
+ price,
1448
+ sellerKiosk
1449
+ });
1450
+ let canTransferOutsideKiosk = true;
1451
+ for (const rule of policy.rules) {
1452
+ const ruleDefinition = this.kioskClient.rules.find(
1453
+ (x) => getNormalizedRuleType(x.rule) === getNormalizedRuleType(rule)
1454
+ );
1455
+ if (!ruleDefinition)
1456
+ throw new Error(`No resolver for the following rule: ${rule}.`);
1457
+ if (ruleDefinition.hasLockingRule)
1458
+ canTransferOutsideKiosk = false;
1459
+ ruleDefinition.resolveRuleFunction({
1460
+ packageId: ruleDefinition.packageId,
1461
+ transactionBlock: this.transactionBlock,
1462
+ itemType,
1463
+ itemId,
1464
+ price: price.toString(),
1465
+ sellerKiosk,
1466
+ policyId: policy.id,
1467
+ transferRequest,
1468
+ purchasedItem,
1469
+ kiosk: this.kiosk,
1470
+ kioskCap: this.kioskCap,
1471
+ extraArgs: extraArgs || {}
1472
+ });
1473
+ }
1474
+ confirmRequest(this.transactionBlock, itemType, policy.id, transferRequest);
1475
+ if (canTransferOutsideKiosk)
1476
+ this.place({ itemType, item: purchasedItem });
1477
+ return this;
1478
+ }
1479
+ /**
1480
+ * A function to setup the client using an existing `ownerCap`,
1481
+ * as return from the `kioskClient.getOwnedKiosks` function.
1482
+ * @param cap `KioskOwnerCap` object as returned from `getOwnedKiosks` SDK call.
1483
+ */
1484
+ setCap(cap) {
1485
+ __privateMethod(this, _validateFinalizedStatus, validateFinalizedStatus_fn).call(this);
1486
+ this.kiosk = objArg(this.transactionBlock, cap.kioskId);
1487
+ if (!cap.isPersonal) {
1488
+ this.kioskCap = objArg(this.transactionBlock, cap.objectId);
1489
+ return;
1490
+ }
1491
+ return __privateMethod(this, _borrowFromPersonalCap, borrowFromPersonalCap_fn).call(this, cap.objectId);
1492
+ }
1493
+ /**
1494
+ * A function that ends up the kiosk building txb & returns the `kioskOwnerCap` back to the
1495
+ * `PersonalKioskCap`, in case we are operating on a personal kiosk.
1496
+ * It will also share the `kiosk` if it's not shared, and finalize the transfer of the personal cap if it's pending.
1497
+ */
1498
+ finalize() {
1499
+ __privateMethod(this, _validateKioskIsSet, validateKioskIsSet_fn).call(this);
1500
+ if (__privateGet(this, _pendingShare))
1501
+ this.share();
1502
+ if (!__privateGet(this, _personalCap)) {
1503
+ if (__privateGet(this, _pendingTransfer))
1504
+ throw new Error(
1505
+ "You need to transfer the `kioskOwnerCap` by calling `shareAndTransferCap()` before wrap"
1506
+ );
1507
+ return;
1508
+ }
1509
+ const packageId = this.kioskClient.getRulePackageId("personalKioskRulePackageId");
1510
+ if (__privateGet(this, _promise)) {
1511
+ this.transactionBlock.moveCall({
1512
+ target: `${packageId}::personal_kiosk::return_val`,
1513
+ arguments: [
1514
+ __privateGet(this, _personalCap),
1515
+ objArg(this.transactionBlock, this.kioskCap),
1516
+ __privateGet(this, _promise)
1517
+ ]
1518
+ });
1519
+ }
1520
+ if (__privateGet(this, _pendingTransfer))
1521
+ transferPersonalCapTx(this.transactionBlock, __privateGet(this, _personalCap), packageId);
1522
+ __privateSet(this, _finalized, true);
1523
+ }
1524
+ // Some setters in case we want custom behavior.
1525
+ setKioskCap(cap) {
1526
+ __privateMethod(this, _validateFinalizedStatus, validateFinalizedStatus_fn).call(this);
1527
+ this.kioskCap = cap;
1528
+ return this;
1529
+ }
1530
+ setKiosk(kiosk) {
1531
+ __privateMethod(this, _validateFinalizedStatus, validateFinalizedStatus_fn).call(this);
1532
+ this.kiosk = kiosk;
1533
+ return this;
1534
+ }
1535
+ // Some getters
1536
+ /*
1537
+ * Returns the active transaction's kiosk, or undefined if `setCap` or `create()` hasn't been called yet.
1538
+ */
1539
+ getKiosk() {
1540
+ __privateMethod(this, _validateFinalizedStatus, validateFinalizedStatus_fn).call(this);
1541
+ if (!this.kiosk)
1542
+ throw new Error("Kiosk is not set.");
1543
+ return this.kiosk;
1544
+ }
1545
+ /*
1546
+ * Returns the active transaction's kioskOwnerCap, or undefined if `setCap` or `create()` hasn't been called yet.
1547
+ */
1548
+ getKioskCap() {
1549
+ __privateMethod(this, _validateFinalizedStatus, validateFinalizedStatus_fn).call(this);
1550
+ if (!this.kioskCap)
1551
+ throw new Error("Kiosk cap is not set");
1552
+ return this.kioskCap;
1553
+ }
1554
+ };
1555
+ _pendingShare = new WeakMap();
1556
+ _pendingTransfer = new WeakMap();
1557
+ _promise = new WeakMap();
1558
+ _personalCap = new WeakMap();
1559
+ _finalized = new WeakMap();
1560
+ _borrowFromPersonalCap = new WeakSet();
1561
+ borrowFromPersonalCap_fn = function(personalCap) {
1562
+ const [kioskCap, promise] = this.transactionBlock.moveCall({
1563
+ target: `${this.kioskClient.getRulePackageId(
1564
+ "personalKioskRulePackageId"
1565
+ )}::personal_kiosk::borrow_val`,
1566
+ arguments: [objArg(this.transactionBlock, personalCap)]
1567
+ });
1568
+ this.kioskCap = kioskCap;
1569
+ __privateSet(this, _personalCap, objArg(this.transactionBlock, personalCap));
1570
+ __privateSet(this, _promise, promise);
1571
+ return this;
1572
+ };
1573
+ _setPendingStatuses = new WeakSet();
1574
+ setPendingStatuses_fn = function({ share, transfer }) {
1575
+ if (transfer !== void 0)
1576
+ __privateSet(this, _pendingTransfer, transfer);
1577
+ if (share !== void 0)
1578
+ __privateSet(this, _pendingShare, share);
1579
+ };
1580
+ _validateKioskIsSet = new WeakSet();
1581
+ validateKioskIsSet_fn = function() {
1582
+ __privateMethod(this, _validateFinalizedStatus, validateFinalizedStatus_fn).call(this);
1583
+ if (!this.kiosk || !this.kioskCap)
1584
+ throw new Error(
1585
+ "You need to initialize the client by either supplying an existing owner cap or by creating a new by calling `.create()`"
1586
+ );
1587
+ };
1588
+ _validateFinalizedStatus = new WeakSet();
1589
+ validateFinalizedStatus_fn = function() {
1590
+ if (__privateGet(this, _finalized))
1591
+ throw new Error("You can't add more transactions to a finalized kiosk transaction block.");
1592
+ };
587
1593
  // Annotate the CommonJS export names for ESM import in node:
588
1594
  0 && (module.exports = {
1595
+ FLOOR_PRICE_RULE_ADDRESS,
589
1596
  KIOSK_ITEM,
590
1597
  KIOSK_LISTING,
591
1598
  KIOSK_LOCK,
592
1599
  KIOSK_LOCK_RULE,
1600
+ KIOSK_LOCK_RULE_ADDRESS,
593
1601
  KIOSK_MODULE,
594
1602
  KIOSK_OWNER_CAP,
595
1603
  KIOSK_PURCHASE_CAP,
596
1604
  KIOSK_TYPE,
597
- MAINNET_RULES_PACKAGE_ADDRESS,
1605
+ KioskClient,
1606
+ KioskTransaction,
1607
+ Network,
1608
+ PERSONAL_KIOSK_RULE_ADDRESS,
598
1609
  ROYALTY_RULE,
599
- TESTNET_RULES_PACKAGE_ADDRESS,
1610
+ ROYALTY_RULE_ADDRESS,
1611
+ TRANSFER_POLICY_CAP_TYPE,
600
1612
  TRANSFER_POLICY_CREATED_EVENT,
601
1613
  TRANSFER_POLICY_MODULE,
602
1614
  TRANSFER_POLICY_TYPE,
603
- attachKioskLockRule,
1615
+ TransferPolicyTransaction,
604
1616
  attachListingsAndPrices,
605
1617
  attachLockedItems,
606
- attachRoyaltyRule,
607
- bcs,
608
- borrow,
609
- borrowMut,
610
- borrowValue,
611
- confirmRequest,
612
- createKiosk,
613
- createKioskAndShare,
614
- createTransferPolicy,
615
- customEnvironment,
616
- delist,
1618
+ attachObjects,
617
1619
  extractKioskData,
618
- fetchKiosk,
619
1620
  getAllDynamicFields,
1621
+ getAllObjects,
1622
+ getAllOwnedObjects,
1623
+ getBaseRules,
620
1624
  getKioskObject,
621
- getOwnedKiosks,
622
- getRulePackageAddress,
1625
+ getNormalizedRuleType,
623
1626
  getTypeWithoutPackageAddress,
624
- list,
625
- lock,
626
- mainnetEnvironment,
1627
+ mainnetRules,
627
1628
  objArg,
1629
+ parseTransferPolicyCapObject,
628
1630
  percentageToBasisPoints,
629
- place,
630
- placeAndList,
631
- purchase,
632
- purchaseAndResolvePolicies,
633
- queryTransferPolicy,
634
- removeTransferPolicyRule,
635
- resolveKioskLockRule,
636
- resolveRoyaltyRule,
637
- returnValue,
638
- rulesPackageAddresses,
639
- take,
640
- testnetEnvironment,
641
- withdrawFromKiosk,
642
- withdrawFromPolicy
1631
+ rules,
1632
+ testnetRules
643
1633
  });
644
1634
  //# sourceMappingURL=index.js.map