@mysten/kiosk 0.0.0-experimental-20230615203750 → 0.0.0-experimental-20230616000145

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.
package/src/tx/kiosk.ts CHANGED
@@ -1,41 +1,31 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import {
5
- SuiAddress,
6
- TransactionArgument,
7
- TransactionBlock,
8
- } from '@mysten/sui.js';
4
+ import { SuiAddress, TransactionArgument, TransactionBlock } from '@mysten/sui.js';
9
5
 
10
6
  import { getTypeWithoutPackageAddress, objArg } from '../utils';
7
+ import { confirmRequest, resolveKioskLockRule, resolveRoyaltyRule } from './transfer-policy';
11
8
  import {
12
- confirmRequest,
13
- resolveKioskLockRule,
14
- resolveRoyaltyRule,
15
- } from './transfer-policy';
16
- import {
17
- KIOSK_LOCK_RULE,
18
- KIOSK_MODULE,
19
- KIOSK_TYPE,
20
- ObjectArgument,
21
- PurchaseAndResolvePoliciesResponse,
22
- PurchaseOptionalParams,
23
- ROYALTY_RULE,
24
- RulesEnvironmentParam,
25
- TransferPolicy,
9
+ KIOSK_LOCK_RULE,
10
+ KIOSK_MODULE,
11
+ KIOSK_TYPE,
12
+ ObjectArgument,
13
+ PurchaseAndResolvePoliciesResponse,
14
+ PurchaseOptionalParams,
15
+ ROYALTY_RULE,
16
+ RulesEnvironmentParam,
17
+ TransferPolicy,
26
18
  } from '../types';
27
19
 
28
20
  /**
29
21
  * Create a new shared Kiosk and returns the [kiosk, kioskOwnerCap] tuple.
30
22
  */
31
- export function createKiosk(
32
- tx: TransactionBlock,
33
- ): [TransactionArgument, TransactionArgument] {
34
- let [kiosk, kioskOwnerCap] = tx.moveCall({
35
- target: `${KIOSK_MODULE}::new`,
36
- });
23
+ export function createKiosk(tx: TransactionBlock): [TransactionArgument, TransactionArgument] {
24
+ let [kiosk, kioskOwnerCap] = tx.moveCall({
25
+ target: `${KIOSK_MODULE}::new`,
26
+ });
37
27
 
38
- return [kiosk, kioskOwnerCap];
28
+ return [kiosk, kioskOwnerCap];
39
29
  }
40
30
 
41
31
  /**
@@ -43,17 +33,17 @@ export function createKiosk(
43
33
  * Returns the `kioskOwnerCap` object.
44
34
  */
45
35
  export function createKioskAndShare(tx: TransactionBlock): TransactionArgument {
46
- let [kiosk, kioskOwnerCap] = tx.moveCall({
47
- target: `${KIOSK_MODULE}::new`,
48
- });
36
+ let [kiosk, kioskOwnerCap] = tx.moveCall({
37
+ target: `${KIOSK_MODULE}::new`,
38
+ });
49
39
 
50
- tx.moveCall({
51
- target: `0x2::transfer::public_share_object`,
52
- typeArguments: [KIOSK_TYPE],
53
- arguments: [kiosk],
54
- });
40
+ tx.moveCall({
41
+ target: `0x2::transfer::public_share_object`,
42
+ typeArguments: [KIOSK_TYPE],
43
+ arguments: [kiosk],
44
+ });
55
45
 
56
- return kioskOwnerCap;
46
+ return kioskOwnerCap;
57
47
  }
58
48
 
59
49
  /**
@@ -61,17 +51,17 @@ export function createKioskAndShare(tx: TransactionBlock): TransactionArgument {
61
51
  * Place an item to the Kiosk.
62
52
  */
63
53
  export function place(
64
- tx: TransactionBlock,
65
- itemType: string,
66
- kiosk: ObjectArgument,
67
- kioskCap: ObjectArgument,
68
- item: ObjectArgument,
54
+ tx: TransactionBlock,
55
+ itemType: string,
56
+ kiosk: ObjectArgument,
57
+ kioskCap: ObjectArgument,
58
+ item: ObjectArgument,
69
59
  ): void {
70
- tx.moveCall({
71
- target: `${KIOSK_MODULE}::place`,
72
- typeArguments: [itemType],
73
- arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), objArg(tx, item)],
74
- });
60
+ tx.moveCall({
61
+ target: `${KIOSK_MODULE}::place`,
62
+ typeArguments: [itemType],
63
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), objArg(tx, item)],
64
+ });
75
65
  }
76
66
 
77
67
  /**
@@ -83,23 +73,18 @@ export function place(
83
73
  * locked without an option to take it out.
84
74
  */
85
75
  export function lock(
86
- tx: TransactionBlock,
87
- itemType: string,
88
- kiosk: ObjectArgument,
89
- kioskCap: ObjectArgument,
90
- policy: ObjectArgument,
91
- item: ObjectArgument,
76
+ tx: TransactionBlock,
77
+ itemType: string,
78
+ kiosk: ObjectArgument,
79
+ kioskCap: ObjectArgument,
80
+ policy: ObjectArgument,
81
+ item: ObjectArgument,
92
82
  ): void {
93
- tx.moveCall({
94
- target: `${KIOSK_MODULE}::lock`,
95
- typeArguments: [itemType],
96
- arguments: [
97
- objArg(tx, kiosk),
98
- objArg(tx, kioskCap),
99
- objArg(tx, policy),
100
- objArg(tx, item),
101
- ],
102
- });
83
+ tx.moveCall({
84
+ target: `${KIOSK_MODULE}::lock`,
85
+ typeArguments: [itemType],
86
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), objArg(tx, policy), objArg(tx, item)],
87
+ });
103
88
  }
104
89
 
105
90
  /**
@@ -107,23 +92,19 @@ export function lock(
107
92
  * Take an item from the Kiosk.
108
93
  */
109
94
  export function take(
110
- tx: TransactionBlock,
111
- itemType: string,
112
- kiosk: ObjectArgument,
113
- kioskCap: ObjectArgument,
114
- itemId: SuiAddress,
95
+ tx: TransactionBlock,
96
+ itemType: string,
97
+ kiosk: ObjectArgument,
98
+ kioskCap: ObjectArgument,
99
+ itemId: SuiAddress,
115
100
  ): TransactionArgument {
116
- let [item] = tx.moveCall({
117
- target: `${KIOSK_MODULE}::take`,
118
- typeArguments: [itemType],
119
- arguments: [
120
- objArg(tx, kiosk),
121
- objArg(tx, kioskCap),
122
- tx.pure(itemId, 'address'),
123
- ],
124
- });
125
-
126
- return item;
101
+ let [item] = tx.moveCall({
102
+ target: `${KIOSK_MODULE}::take`,
103
+ typeArguments: [itemType],
104
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, 'address')],
105
+ });
106
+
107
+ return item;
127
108
  }
128
109
 
129
110
  /**
@@ -131,23 +112,23 @@ export function take(
131
112
  * List an item for sale.
132
113
  */
133
114
  export function list(
134
- tx: TransactionBlock,
135
- itemType: string,
136
- kiosk: ObjectArgument,
137
- kioskCap: ObjectArgument,
138
- itemId: SuiAddress,
139
- price: string | bigint,
115
+ tx: TransactionBlock,
116
+ itemType: string,
117
+ kiosk: ObjectArgument,
118
+ kioskCap: ObjectArgument,
119
+ itemId: SuiAddress,
120
+ price: string | bigint,
140
121
  ): void {
141
- tx.moveCall({
142
- target: `${KIOSK_MODULE}::list`,
143
- typeArguments: [itemType],
144
- arguments: [
145
- objArg(tx, kiosk),
146
- objArg(tx, kioskCap),
147
- tx.pure(itemId, 'address'),
148
- tx.pure(price, 'u64'),
149
- ],
150
- });
122
+ tx.moveCall({
123
+ target: `${KIOSK_MODULE}::list`,
124
+ typeArguments: [itemType],
125
+ arguments: [
126
+ objArg(tx, kiosk),
127
+ objArg(tx, kioskCap),
128
+ tx.pure(itemId, 'address'),
129
+ tx.pure(price, 'u64'),
130
+ ],
131
+ });
151
132
  }
152
133
 
153
134
  /**
@@ -155,21 +136,17 @@ export function list(
155
136
  * List an item for sale.
156
137
  */
157
138
  export function delist(
158
- tx: TransactionBlock,
159
- itemType: string,
160
- kiosk: ObjectArgument,
161
- kioskCap: ObjectArgument,
162
- itemId: SuiAddress,
139
+ tx: TransactionBlock,
140
+ itemType: string,
141
+ kiosk: ObjectArgument,
142
+ kioskCap: ObjectArgument,
143
+ itemId: SuiAddress,
163
144
  ): void {
164
- tx.moveCall({
165
- target: `${KIOSK_MODULE}::delist`,
166
- typeArguments: [itemType],
167
- arguments: [
168
- objArg(tx, kiosk),
169
- objArg(tx, kioskCap),
170
- tx.pure(itemId, 'address'),
171
- ],
172
- });
145
+ tx.moveCall({
146
+ target: `${KIOSK_MODULE}::delist`,
147
+ typeArguments: [itemType],
148
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, 'address')],
149
+ });
173
150
  }
174
151
 
175
152
  /**
@@ -177,23 +154,18 @@ export function delist(
177
154
  * Place an item to the Kiosk and list it for sale.
178
155
  */
179
156
  export function placeAndList(
180
- tx: TransactionBlock,
181
- itemType: string,
182
- kiosk: ObjectArgument,
183
- kioskCap: ObjectArgument,
184
- item: ObjectArgument,
185
- price: string | bigint,
157
+ tx: TransactionBlock,
158
+ itemType: string,
159
+ kiosk: ObjectArgument,
160
+ kioskCap: ObjectArgument,
161
+ item: ObjectArgument,
162
+ price: string | bigint,
186
163
  ): void {
187
- tx.moveCall({
188
- target: `${KIOSK_MODULE}::place_and_list`,
189
- typeArguments: [itemType],
190
- arguments: [
191
- objArg(tx, kiosk),
192
- objArg(tx, kioskCap),
193
- objArg(tx, item),
194
- tx.pure(price, 'u64'),
195
- ],
196
- });
164
+ tx.moveCall({
165
+ target: `${KIOSK_MODULE}::place_and_list`,
166
+ typeArguments: [itemType],
167
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), objArg(tx, item), tx.pure(price, 'u64')],
168
+ });
197
169
  }
198
170
 
199
171
  /**
@@ -201,23 +173,19 @@ export function placeAndList(
201
173
  * a TransferRequest which needs to be dealt with (via a matching TransferPolicy).
202
174
  */
203
175
  export function purchase(
204
- tx: TransactionBlock,
205
- itemType: string,
206
- kiosk: ObjectArgument,
207
- itemId: SuiAddress,
208
- payment: ObjectArgument,
176
+ tx: TransactionBlock,
177
+ itemType: string,
178
+ kiosk: ObjectArgument,
179
+ itemId: SuiAddress,
180
+ payment: ObjectArgument,
209
181
  ): [TransactionArgument, TransactionArgument] {
210
- let [item, transferRequest] = tx.moveCall({
211
- target: `${KIOSK_MODULE}::purchase`,
212
- typeArguments: [itemType],
213
- arguments: [
214
- objArg(tx, kiosk),
215
- tx.pure(itemId, 'address'),
216
- objArg(tx, payment),
217
- ],
218
- });
219
-
220
- return [item, transferRequest];
182
+ let [item, transferRequest] = tx.moveCall({
183
+ target: `${KIOSK_MODULE}::purchase`,
184
+ typeArguments: [itemType],
185
+ arguments: [objArg(tx, kiosk), tx.pure(itemId, 'address'), objArg(tx, payment)],
186
+ });
187
+
188
+ return [item, transferRequest];
221
189
  }
222
190
 
223
191
  /**
@@ -225,27 +193,27 @@ export function purchase(
225
193
  * If the amount is null, then the entire balance will be withdrawn.
226
194
  */
227
195
  export function withdrawFromKiosk(
228
- tx: TransactionBlock,
229
- kiosk: ObjectArgument,
230
- kioskCap: ObjectArgument,
231
- amount: string | bigint | null,
196
+ tx: TransactionBlock,
197
+ kiosk: ObjectArgument,
198
+ kioskCap: ObjectArgument,
199
+ amount: string | bigint | null,
232
200
  ): TransactionArgument {
233
- let amountArg =
234
- amount !== null
235
- ? tx.pure(
236
- {
237
- Some: amount,
238
- },
239
- 'Option<u64>',
240
- )
241
- : tx.pure({ None: true }, 'Option<u64>');
242
-
243
- let [coin] = tx.moveCall({
244
- target: `${KIOSK_MODULE}::withdraw`,
245
- arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), amountArg],
246
- });
247
-
248
- return coin;
201
+ let amountArg =
202
+ amount !== null
203
+ ? tx.pure(
204
+ {
205
+ Some: amount,
206
+ },
207
+ 'Option<u64>',
208
+ )
209
+ : tx.pure({ None: true }, 'Option<u64>');
210
+
211
+ let [coin] = tx.moveCall({
212
+ target: `${KIOSK_MODULE}::withdraw`,
213
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), amountArg],
214
+ });
215
+
216
+ return coin;
249
217
  }
250
218
 
251
219
  /**
@@ -253,23 +221,19 @@ export function withdrawFromKiosk(
253
221
  * Immutably borrow an item from the Kiosk.
254
222
  */
255
223
  export function borrow(
256
- tx: TransactionBlock,
257
- itemType: string,
258
- kiosk: ObjectArgument,
259
- kioskCap: ObjectArgument,
260
- itemId: SuiAddress,
224
+ tx: TransactionBlock,
225
+ itemType: string,
226
+ kiosk: ObjectArgument,
227
+ kioskCap: ObjectArgument,
228
+ itemId: SuiAddress,
261
229
  ): TransactionArgument {
262
- let [item] = tx.moveCall({
263
- target: `${KIOSK_MODULE}::borrow`,
264
- typeArguments: [itemType],
265
- arguments: [
266
- objArg(tx, kiosk),
267
- objArg(tx, kioskCap),
268
- tx.pure(itemId, 'address'),
269
- ],
270
- });
271
-
272
- return item;
230
+ let [item] = tx.moveCall({
231
+ target: `${KIOSK_MODULE}::borrow`,
232
+ typeArguments: [itemType],
233
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, 'address')],
234
+ });
235
+
236
+ return item;
273
237
  }
274
238
 
275
239
  /**
@@ -277,23 +241,19 @@ export function borrow(
277
241
  * Mutably borrow an item from the Kiosk.
278
242
  */
279
243
  export function borrowMut(
280
- tx: TransactionBlock,
281
- itemType: string,
282
- kiosk: ObjectArgument,
283
- kioskCap: ObjectArgument,
284
- itemId: SuiAddress,
244
+ tx: TransactionBlock,
245
+ itemType: string,
246
+ kiosk: ObjectArgument,
247
+ kioskCap: ObjectArgument,
248
+ itemId: SuiAddress,
285
249
  ): TransactionArgument {
286
- let [item] = tx.moveCall({
287
- target: `${KIOSK_MODULE}::borrow_mut`,
288
- typeArguments: [itemType],
289
- arguments: [
290
- objArg(tx, kiosk),
291
- objArg(tx, kioskCap),
292
- tx.pure(itemId, 'address'),
293
- ],
294
- });
295
-
296
- return item;
250
+ let [item] = tx.moveCall({
251
+ target: `${KIOSK_MODULE}::borrow_mut`,
252
+ typeArguments: [itemType],
253
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, 'address')],
254
+ });
255
+
256
+ return item;
297
257
  }
298
258
 
299
259
  /**
@@ -303,23 +263,19 @@ export function borrowMut(
303
263
  * Requires calling `returnValue` to return the item.
304
264
  */
305
265
  export function borrowValue(
306
- tx: TransactionBlock,
307
- itemType: string,
308
- kiosk: ObjectArgument,
309
- kioskCap: ObjectArgument,
310
- itemId: SuiAddress,
266
+ tx: TransactionBlock,
267
+ itemType: string,
268
+ kiosk: ObjectArgument,
269
+ kioskCap: ObjectArgument,
270
+ itemId: SuiAddress,
311
271
  ): [TransactionArgument, TransactionArgument] {
312
- let [item, promise] = tx.moveCall({
313
- target: `${KIOSK_MODULE}::borrow_val`,
314
- typeArguments: [itemType],
315
- arguments: [
316
- objArg(tx, kiosk),
317
- objArg(tx, kioskCap),
318
- tx.pure(itemId, 'address'),
319
- ],
320
- });
321
-
322
- return [item, promise];
272
+ let [item, promise] = tx.moveCall({
273
+ target: `${KIOSK_MODULE}::borrow_val`,
274
+ typeArguments: [itemType],
275
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, 'address')],
276
+ });
277
+
278
+ return [item, promise];
323
279
  }
324
280
 
325
281
  /**
@@ -327,17 +283,17 @@ export function borrowValue(
327
283
  * Return an item to the Kiosk after it was `borrowValue`-d.
328
284
  */
329
285
  export function returnValue(
330
- tx: TransactionBlock,
331
- itemType: string,
332
- kiosk: ObjectArgument,
333
- item: TransactionArgument,
334
- promise: TransactionArgument,
286
+ tx: TransactionBlock,
287
+ itemType: string,
288
+ kiosk: ObjectArgument,
289
+ item: TransactionArgument,
290
+ promise: TransactionArgument,
335
291
  ): void {
336
- tx.moveCall({
337
- target: `${KIOSK_MODULE}::return_val`,
338
- typeArguments: [itemType],
339
- arguments: [objArg(tx, kiosk), item, promise],
340
- });
292
+ tx.moveCall({
293
+ target: `${KIOSK_MODULE}::return_val`,
294
+ typeArguments: [itemType],
295
+ arguments: [objArg(tx, kiosk), item, promise],
296
+ });
341
297
  }
342
298
 
343
299
  /**
@@ -350,77 +306,64 @@ export function returnValue(
350
306
  * to transfer it to an address or place it in their kiosk.
351
307
  */
352
308
  export function purchaseAndResolvePolicies(
353
- tx: TransactionBlock,
354
- itemType: string,
355
- price: string,
356
- kiosk: ObjectArgument,
357
- itemId: SuiAddress,
358
- policy: TransferPolicy,
359
- environment: RulesEnvironmentParam,
360
- extraParams?: PurchaseOptionalParams,
309
+ tx: TransactionBlock,
310
+ itemType: string,
311
+ price: string,
312
+ kiosk: ObjectArgument,
313
+ itemId: SuiAddress,
314
+ policy: TransferPolicy,
315
+ environment: RulesEnvironmentParam,
316
+ extraParams?: PurchaseOptionalParams,
361
317
  ): PurchaseAndResolvePoliciesResponse {
362
- // if we don't pass the listing or the listing doens't have a price, return.
363
- if (price === undefined || typeof price !== 'string')
364
- throw new Error(`Price of the listing is not supplied.`);
365
-
366
- // Split the coin for the amount of the listing.
367
- const coin = tx.splitCoins(tx.gas, [tx.pure(price, 'u64')]);
368
-
369
- // initialize the purchase `kiosk::purchase`
370
- const [purchasedItem, transferRequest] = purchase(
371
- tx,
372
- itemType,
373
- kiosk,
374
- itemId,
375
- coin,
376
- );
377
-
378
- // Start resolving rules.
379
- // Right now we support `kiosk_lock_rule` and `royalty_rule`.
380
- // They can also be supplied in parallel (supports combination).
381
- let hasKioskLockRule = false;
382
-
383
- for (let rule of policy.rules) {
384
- const ruleWithoutAddr = getTypeWithoutPackageAddress(rule);
385
-
386
- switch (ruleWithoutAddr) {
387
- case ROYALTY_RULE:
388
- resolveRoyaltyRule(
389
- tx,
390
- itemType,
391
- price,
392
- policy.id,
393
- transferRequest,
394
- environment,
395
- );
396
- break;
397
- case KIOSK_LOCK_RULE:
398
- if (!extraParams?.ownedKiosk || !extraParams?.ownedKioskCap)
399
- throw new Error(
400
- `This item type ${itemType} has a 'kiosk_lock_rule', but function call is missing user's kiosk and kioskCap params`,
401
- );
402
- hasKioskLockRule = true;
403
- resolveKioskLockRule(
404
- tx,
405
- itemType,
406
- purchasedItem,
407
- extraParams.ownedKiosk,
408
- extraParams.ownedKioskCap,
409
- policy.id,
410
- transferRequest,
411
- environment,
412
- );
413
- break;
414
- default:
415
- break;
416
- }
417
- }
418
-
419
- // confirm the Transfer Policy request.
420
- confirmRequest(tx, itemType, policy.id, transferRequest);
421
-
422
- return {
423
- item: purchasedItem,
424
- canTransfer: !hasKioskLockRule,
425
- };
318
+ // if we don't pass the listing or the listing doens't have a price, return.
319
+ if (price === undefined || typeof price !== 'string')
320
+ throw new Error(`Price of the listing is not supplied.`);
321
+
322
+ // Split the coin for the amount of the listing.
323
+ const coin = tx.splitCoins(tx.gas, [tx.pure(price, 'u64')]);
324
+
325
+ // initialize the purchase `kiosk::purchase`
326
+ const [purchasedItem, transferRequest] = purchase(tx, itemType, kiosk, itemId, coin);
327
+
328
+ // Start resolving rules.
329
+ // Right now we support `kiosk_lock_rule` and `royalty_rule`.
330
+ // They can also be supplied in parallel (supports combination).
331
+ let hasKioskLockRule = false;
332
+
333
+ for (let rule of policy.rules) {
334
+ const ruleWithoutAddr = getTypeWithoutPackageAddress(rule);
335
+
336
+ switch (ruleWithoutAddr) {
337
+ case ROYALTY_RULE:
338
+ resolveRoyaltyRule(tx, itemType, price, policy.id, transferRequest, environment);
339
+ break;
340
+ case KIOSK_LOCK_RULE:
341
+ if (!extraParams?.ownedKiosk || !extraParams?.ownedKioskCap)
342
+ throw new Error(
343
+ `This item type ${itemType} has a 'kiosk_lock_rule', but function call is missing user's kiosk and kioskCap params`,
344
+ );
345
+ hasKioskLockRule = true;
346
+ resolveKioskLockRule(
347
+ tx,
348
+ itemType,
349
+ purchasedItem,
350
+ extraParams.ownedKiosk,
351
+ extraParams.ownedKioskCap,
352
+ policy.id,
353
+ transferRequest,
354
+ environment,
355
+ );
356
+ break;
357
+ default:
358
+ break;
359
+ }
360
+ }
361
+
362
+ // confirm the Transfer Policy request.
363
+ confirmRequest(tx, itemType, policy.id, transferRequest);
364
+
365
+ return {
366
+ item: purchasedItem,
367
+ canTransfer: !hasKioskLockRule,
368
+ };
426
369
  }