@mysten/kiosk 0.5.3 → 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 +17 -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 +12 -4
  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
@@ -0,0 +1,512 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { type TransactionArgument, type TransactionBlock } from '@mysten/sui.js/transactions';
5
+
6
+ import * as kioskTx from '../tx/kiosk';
7
+ import { convertToPersonalTx, transferPersonalCapTx } from '../tx/personal-kiosk';
8
+ import { confirmRequest } from '../tx/transfer-policy';
9
+ import {
10
+ ItemId,
11
+ ItemReference,
12
+ ItemValue,
13
+ KioskOwnerCap,
14
+ ObjectArgument,
15
+ Price,
16
+ PurchaseOptions,
17
+ } from '../types';
18
+ import { getNormalizedRuleType, objArg } from '../utils';
19
+ import { type KioskClient } from './kiosk-client';
20
+
21
+ export type KioskTransactionParams = {
22
+ /** The TransactionBlock for this run */
23
+ transactionBlock: TransactionBlock;
24
+ /**
25
+ * You can create a new KioskClient by calling `new KioskClient()`
26
+ */
27
+ kioskClient: KioskClient;
28
+ /**
29
+ * You can optionally pass in the `cap` as returned
30
+ * from `kioskClient.getOwnedKiosks` when initializing the client
31
+ * Otherwise, you can set it by calling `kioskTransaction.setCap()`
32
+ */
33
+ cap?: KioskOwnerCap;
34
+ };
35
+
36
+ /**
37
+ * A helper for building transactions that involve kiosk.
38
+ */
39
+ export class KioskTransaction {
40
+ transactionBlock: TransactionBlock;
41
+ kioskClient: KioskClient;
42
+ kiosk?: TransactionArgument;
43
+ kioskCap?: TransactionArgument;
44
+ // If we're pending `share` of a new kiosk, `finalize()` will share it.
45
+ #pendingShare?: boolean;
46
+ // If we're pending transferring of the cap, `finalize()` will either error or transfer the cap if it's a new personal.
47
+ #pendingTransfer?: boolean;
48
+ // The promise that the personalCap will be returned on `finalize()`.
49
+ #promise?: TransactionArgument | undefined;
50
+ // The personal kiosk argument.
51
+ #personalCap?: TransactionArgument;
52
+ // A flag that checks whether kiosk TX is finalized.
53
+ #finalized: boolean = false;
54
+
55
+ constructor({ transactionBlock, kioskClient, cap }: KioskTransactionParams) {
56
+ this.transactionBlock = transactionBlock;
57
+ this.kioskClient = kioskClient;
58
+
59
+ if (cap) this.setCap(cap);
60
+ }
61
+
62
+ /**
63
+ * Creates a kiosk and saves `kiosk` and `kioskOwnerCap` in state.
64
+ * Helpful if we want to chain some actions before sharing + transferring the cap to the specified address.
65
+ * @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
66
+ */
67
+ create() {
68
+ this.#validateFinalizedStatus();
69
+ this.#setPendingStatuses({
70
+ share: true,
71
+ transfer: true,
72
+ });
73
+ const [kiosk, cap] = kioskTx.createKiosk(this.transactionBlock);
74
+ this.kiosk = kiosk;
75
+ this.kioskCap = cap;
76
+ return this;
77
+ }
78
+
79
+ /**
80
+ * Creates a personal kiosk & shares it.
81
+ * The `PersonalKioskCap` is transferred to the signer.
82
+ * @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
83
+ */
84
+ createPersonal(borrow?: boolean) {
85
+ this.#pendingShare = true;
86
+ return this.create().convertToPersonal(borrow);
87
+ }
88
+
89
+ /**
90
+ * Converts a kiosk to a Personal (Soulbound) Kiosk.
91
+ * Requires initialization by either calling `ktxb.create()` or `ktxb.setCap()`.
92
+ */
93
+ convertToPersonal(borrow?: boolean) {
94
+ this.#validateKioskIsSet();
95
+
96
+ const cap = convertToPersonalTx(
97
+ this.transactionBlock,
98
+ this.kiosk!,
99
+ this.kioskCap!,
100
+ this.kioskClient.getRulePackageId('personalKioskRulePackageId'),
101
+ );
102
+
103
+ // if we enable `borrow`, we borrow the kioskCap from the cap.
104
+ if (borrow) this.#borrowFromPersonalCap(cap);
105
+ else this.#personalCap = cap;
106
+
107
+ this.#setPendingStatuses({ transfer: true });
108
+ return this;
109
+ }
110
+
111
+ /**
112
+ * Single function way to create a kiosk, share it and transfer the cap to the specified address.
113
+ */
114
+ createAndShare(address: string) {
115
+ this.#validateFinalizedStatus();
116
+ const cap = kioskTx.createKioskAndShare(this.transactionBlock);
117
+ this.transactionBlock.transferObjects([cap], this.transactionBlock.pure(address, 'address'));
118
+ }
119
+
120
+ /**
121
+ * Shares the kiosk.
122
+ */
123
+ share() {
124
+ this.#validateKioskIsSet();
125
+ this.#setPendingStatuses({ share: false });
126
+ kioskTx.shareKiosk(this.transactionBlock, this.kiosk!);
127
+ }
128
+
129
+ /**
130
+ * Should be called only after `create` is called.
131
+ * It shares the kiosk & transfers the cap to the specified address.
132
+ */
133
+ shareAndTransferCap(address: string) {
134
+ if (this.#personalCap)
135
+ throw new Error('You can only call `shareAndTransferCap` on a non-personal kiosk.');
136
+ this.#setPendingStatuses({ transfer: false });
137
+ this.share();
138
+ this.transactionBlock.transferObjects(
139
+ [this.kioskCap!],
140
+ this.transactionBlock.pure(address, 'address'),
141
+ );
142
+ }
143
+
144
+ /**
145
+ * A function to borrow an item from a kiosk & execute any function with it.
146
+ * Example: You could borrow a Fren out of a kiosk, attach an accessory (or mix), and return it.
147
+ */
148
+ borrowTx({ itemType, itemId }: ItemId, callback: (item: TransactionArgument) => void) {
149
+ this.#validateKioskIsSet();
150
+ const [itemObj, promise] = kioskTx.borrowValue(
151
+ this.transactionBlock,
152
+ itemType,
153
+ this.kiosk!,
154
+ this.kioskCap!,
155
+ itemId,
156
+ );
157
+
158
+ callback(itemObj);
159
+
160
+ this.return({ itemType, item: itemObj, promise });
161
+ }
162
+
163
+ /**
164
+ * Borrows an item from the kiosk.
165
+ * This will fail if the item is listed for sale.
166
+ *
167
+ * Requires calling `return`.
168
+ */
169
+ borrow({ itemType, itemId }: ItemId): [TransactionArgument, TransactionArgument] {
170
+ this.#validateKioskIsSet();
171
+ const [itemObj, promise] = kioskTx.borrowValue(
172
+ this.transactionBlock,
173
+ itemType,
174
+ this.kiosk!,
175
+ this.kioskCap!,
176
+ itemId,
177
+ );
178
+
179
+ return [itemObj, promise];
180
+ }
181
+
182
+ /**
183
+ * Returns the item back to the kiosk.
184
+ * Accepts the parameters returned from the `borrow` function.
185
+ */
186
+ return({ itemType, item, promise }: ItemValue & { promise: TransactionArgument }) {
187
+ this.#validateKioskIsSet();
188
+ kioskTx.returnValue(this.transactionBlock, itemType, this.kiosk!, item, promise);
189
+ return this;
190
+ }
191
+
192
+ /**
193
+ * A function to withdraw from kiosk
194
+ * @param address Where to trasnfer the coin.
195
+ * @param amount The amount we aim to withdraw.
196
+ */
197
+ withdraw(address: string, amount?: string | bigint | number) {
198
+ this.#validateKioskIsSet();
199
+ const coin = kioskTx.withdrawFromKiosk(
200
+ this.transactionBlock,
201
+ this.kiosk!,
202
+ this.kioskCap!,
203
+ amount,
204
+ );
205
+ this.transactionBlock.transferObjects([coin], this.transactionBlock.pure(address, 'address'));
206
+ return this;
207
+ }
208
+
209
+ /**
210
+ * A function to place an item in the kiosk.
211
+ * @param itemType The type `T` of the item
212
+ * @param item The ID or Transaction Argument of the item
213
+ */
214
+ place({ itemType, item }: ItemReference) {
215
+ this.#validateKioskIsSet();
216
+ kioskTx.place(this.transactionBlock, itemType, this.kiosk!, this.kioskCap!, item);
217
+ return this;
218
+ }
219
+
220
+ /**
221
+ * A function to place an item in the kiosk and list it for sale in one transaction.
222
+ * @param itemType The type `T` of the item
223
+ * @param item The ID or Transaction Argument of the item
224
+ * @param price The price in MIST
225
+ */
226
+ placeAndList({ itemType, item, price }: ItemReference & Price) {
227
+ this.#validateKioskIsSet();
228
+ kioskTx.placeAndList(this.transactionBlock, itemType, this.kiosk!, this.kioskCap!, item, price);
229
+ return this;
230
+ }
231
+
232
+ /**
233
+ * A function to list an item in the kiosk.
234
+ * @param itemType The type `T` of the item
235
+ * @param itemId The ID of the item
236
+ * @param price The price in MIST
237
+ */
238
+ list({ itemType, itemId, price }: ItemId & { price: string | bigint }) {
239
+ this.#validateKioskIsSet();
240
+ kioskTx.list(this.transactionBlock, itemType, this.kiosk!, this.kioskCap!, itemId, price);
241
+ return this;
242
+ }
243
+
244
+ /**
245
+ * A function to delist an item from the kiosk.
246
+ * @param itemType The type `T` of the item
247
+ * @param itemId The ID of the item
248
+ */
249
+ delist({ itemType, itemId }: ItemId) {
250
+ this.#validateKioskIsSet();
251
+ kioskTx.delist(this.transactionBlock, itemType, this.kiosk!, this.kioskCap!, itemId);
252
+ return this;
253
+ }
254
+
255
+ /**
256
+ * A function to take an item from the kiosk. The transaction won't succeed if the item is listed or locked.
257
+
258
+ * @param itemType The type `T` of the item
259
+ * @param itemId The ID of the item
260
+ */
261
+ take({ itemType, itemId }: ItemId): TransactionArgument {
262
+ this.#validateKioskIsSet();
263
+ return kioskTx.take(this.transactionBlock, itemType, this.kiosk!, this.kioskCap!, itemId);
264
+ }
265
+
266
+ /**
267
+ * Transfer a non-locked/non-listed item to an address.
268
+ *
269
+ * @param itemType The type `T` of the item
270
+ * @param itemId The ID of the item
271
+ * @param address The destination address
272
+ */
273
+ transfer({ itemType, itemId, address }: ItemId & { address: string }) {
274
+ this.#validateKioskIsSet();
275
+ const item = this.take({ itemType, itemId });
276
+ this.transactionBlock.transferObjects([item], this.transactionBlock.pure(address, 'address'));
277
+ return this;
278
+ }
279
+
280
+ /**
281
+ * A function to take lock an item in the kiosk.
282
+
283
+ * @param itemType The type `T` of the item
284
+ * @param itemId The ID of the item
285
+ * @param policy The Policy ID or Transaction Argument for item T
286
+ */
287
+ lock({ itemType, itemId, policy }: ItemId & { policy: ObjectArgument }) {
288
+ this.#validateKioskIsSet();
289
+ kioskTx.lock(this.transactionBlock, itemType, this.kiosk!, this.kioskCap!, policy, itemId);
290
+ return this;
291
+ }
292
+
293
+ /**
294
+ * Purchase an item from a seller's kiosk.
295
+ * Returns [item, transferRequest]
296
+ * Can be called like: `const [item, transferRequest] = kioskTx.purchase({...})`
297
+ * @param itemType The type `T` of the item
298
+ * @param itemId The ID of the item
299
+ * @param price The price in MIST
300
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
301
+ */
302
+ purchase({
303
+ itemType,
304
+ itemId,
305
+ price,
306
+ sellerKiosk,
307
+ }: ItemId & Price & { sellerKiosk: ObjectArgument }): [TransactionArgument, TransactionArgument] {
308
+ // Split the coin for the amount of the listing.
309
+ const coin = this.transactionBlock.splitCoins(this.transactionBlock.gas, [
310
+ this.transactionBlock.pure(price, 'u64'),
311
+ ]);
312
+ return kioskTx.purchase(this.transactionBlock, itemType, sellerKiosk, itemId, coin);
313
+ }
314
+
315
+ /**
316
+ * A function to purchase and resolve a transfer policy.
317
+ * If the transfer policy has the `lock` rule, the item is locked in the kiosk.
318
+ * Otherwise, the item is placed in the kiosk.
319
+ * @param itemType The type of the item
320
+ * @param itemId The id of the item
321
+ * @param price The price of the specified item
322
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
323
+ * @param extraArgs Used to pass arguments for custom rule resolvers.
324
+ */
325
+ async purchaseAndResolve({
326
+ itemType,
327
+ itemId,
328
+ price,
329
+ sellerKiosk,
330
+ extraArgs,
331
+ }: ItemId & Price & { sellerKiosk: ObjectArgument } & PurchaseOptions) {
332
+ this.#validateKioskIsSet();
333
+ // Get a list of the transfer policies.
334
+ const policies = await this.kioskClient.getTransferPolicies({ type: itemType });
335
+
336
+ if (policies.length === 0) {
337
+ throw new Error(
338
+ `The type ${itemType} doesn't have a Transfer Policy so it can't be traded through kiosk.`,
339
+ );
340
+ }
341
+
342
+ const policy = policies[0]; // we now pick the first one. We need to add an option to define which one.
343
+
344
+ // initialize the purchase `kiosk::purchase`
345
+ const [purchasedItem, transferRequest] = this.purchase({
346
+ itemType,
347
+ itemId,
348
+ price,
349
+ sellerKiosk,
350
+ });
351
+
352
+ let canTransferOutsideKiosk = true;
353
+
354
+ for (const rule of policy.rules) {
355
+ const ruleDefinition = this.kioskClient.rules.find(
356
+ (x) => getNormalizedRuleType(x.rule) === getNormalizedRuleType(rule),
357
+ );
358
+ if (!ruleDefinition) throw new Error(`No resolver for the following rule: ${rule}.`);
359
+
360
+ if (ruleDefinition.hasLockingRule) canTransferOutsideKiosk = false;
361
+
362
+ ruleDefinition.resolveRuleFunction({
363
+ packageId: ruleDefinition.packageId,
364
+ transactionBlock: this.transactionBlock,
365
+ itemType,
366
+ itemId,
367
+ price: price.toString(),
368
+ sellerKiosk,
369
+ policyId: policy.id,
370
+ transferRequest,
371
+ purchasedItem,
372
+ kiosk: this.kiosk!,
373
+ kioskCap: this.kioskCap!,
374
+ extraArgs: extraArgs || {},
375
+ });
376
+ }
377
+
378
+ confirmRequest(this.transactionBlock, itemType, policy.id, transferRequest);
379
+
380
+ if (canTransferOutsideKiosk) this.place({ itemType, item: purchasedItem });
381
+
382
+ return this;
383
+ }
384
+
385
+ /**
386
+ * A function to setup the client using an existing `ownerCap`,
387
+ * as return from the `kioskClient.getOwnedKiosks` function.
388
+ * @param cap `KioskOwnerCap` object as returned from `getOwnedKiosks` SDK call.
389
+ */
390
+ setCap(cap: KioskOwnerCap) {
391
+ this.#validateFinalizedStatus();
392
+ this.kiosk = objArg(this.transactionBlock, cap.kioskId);
393
+ if (!cap.isPersonal) {
394
+ this.kioskCap = objArg(this.transactionBlock, cap.objectId);
395
+ return;
396
+ }
397
+
398
+ return this.#borrowFromPersonalCap(cap.objectId);
399
+ }
400
+
401
+ /**
402
+ * A function that ends up the kiosk building txb & returns the `kioskOwnerCap` back to the
403
+ * `PersonalKioskCap`, in case we are operating on a personal kiosk.
404
+ * It will also share the `kiosk` if it's not shared, and finalize the transfer of the personal cap if it's pending.
405
+ */
406
+ finalize() {
407
+ this.#validateKioskIsSet();
408
+ // If we're pending the sharing of the new kiosk, share it.
409
+ if (this.#pendingShare) this.share();
410
+
411
+ // If we're operating on a non-personal kiosk, we don't need to do anything else.
412
+ if (!this.#personalCap) {
413
+ // If we're pending transfer though, we inform user to call `shareAndTransferCap()`.
414
+ if (this.#pendingTransfer)
415
+ throw new Error(
416
+ 'You need to transfer the `kioskOwnerCap` by calling `shareAndTransferCap()` before wrap',
417
+ );
418
+ return;
419
+ }
420
+
421
+ const packageId = this.kioskClient.getRulePackageId('personalKioskRulePackageId');
422
+
423
+ // if we have a promise, return the `ownerCap` back to the personal cap.
424
+ if (this.#promise) {
425
+ this.transactionBlock.moveCall({
426
+ target: `${packageId}::personal_kiosk::return_val`,
427
+ arguments: [
428
+ this.#personalCap,
429
+ objArg(this.transactionBlock, this.kioskCap!),
430
+ this.#promise!,
431
+ ],
432
+ });
433
+ }
434
+
435
+ // If we are pending transferring the personalCap, we do it here.
436
+ if (this.#pendingTransfer)
437
+ transferPersonalCapTx(this.transactionBlock, this.#personalCap, packageId);
438
+
439
+ // Mark the transaction block as finalized, so no other functions can be called.
440
+ this.#finalized = true;
441
+ }
442
+
443
+ // Some setters in case we want custom behavior.
444
+ setKioskCap(cap: TransactionArgument) {
445
+ this.#validateFinalizedStatus();
446
+ this.kioskCap = cap;
447
+ return this;
448
+ }
449
+
450
+ setKiosk(kiosk: TransactionArgument) {
451
+ this.#validateFinalizedStatus();
452
+ this.kiosk = kiosk;
453
+ return this;
454
+ }
455
+
456
+ // Some getters
457
+ /*
458
+ * Returns the active transaction's kiosk, or undefined if `setCap` or `create()` hasn't been called yet.
459
+ */
460
+ getKiosk() {
461
+ this.#validateFinalizedStatus();
462
+ if (!this.kiosk) throw new Error('Kiosk is not set.');
463
+ return this.kiosk;
464
+ }
465
+
466
+ /*
467
+ * Returns the active transaction's kioskOwnerCap, or undefined if `setCap` or `create()` hasn't been called yet.
468
+ */
469
+ getKioskCap() {
470
+ this.#validateFinalizedStatus();
471
+ if (!this.kioskCap) throw new Error('Kiosk cap is not set');
472
+ return this.kioskCap;
473
+ }
474
+
475
+ /**
476
+ * A function to borrow from `personalCap`.
477
+ */
478
+ #borrowFromPersonalCap(personalCap: ObjectArgument) {
479
+ const [kioskCap, promise] = this.transactionBlock.moveCall({
480
+ target: `${this.kioskClient.getRulePackageId(
481
+ 'personalKioskRulePackageId',
482
+ )}::personal_kiosk::borrow_val`,
483
+ arguments: [objArg(this.transactionBlock, personalCap)],
484
+ });
485
+
486
+ this.kioskCap = kioskCap;
487
+ this.#personalCap = objArg(this.transactionBlock, personalCap);
488
+ this.#promise = promise;
489
+
490
+ return this;
491
+ }
492
+
493
+ #setPendingStatuses({ share, transfer }: { share?: boolean; transfer?: boolean }) {
494
+ if (transfer !== undefined) this.#pendingTransfer = transfer;
495
+ if (share !== undefined) this.#pendingShare = share;
496
+ }
497
+
498
+ #validateKioskIsSet() {
499
+ this.#validateFinalizedStatus();
500
+
501
+ if (!this.kiosk || !this.kioskCap)
502
+ throw new Error(
503
+ 'You need to initialize the client by either supplying an existing owner cap or by creating a new by calling `.create()`',
504
+ );
505
+ }
506
+
507
+ // Validates that `finalize`
508
+ #validateFinalizedStatus() {
509
+ if (this.#finalized)
510
+ throw new Error("You can't add more transactions to a finalized kiosk transaction block.");
511
+ }
512
+ }