@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
@@ -2,8 +2,17 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { SuiClient } from '@mysten/sui.js/client';
5
+ import { isValidSuiAddress } from '@mysten/sui.js/utils';
6
+
5
7
  import { bcs } from '../bcs';
6
- import { TRANSFER_POLICY_CREATED_EVENT, TRANSFER_POLICY_TYPE, TransferPolicy } from '../types';
8
+ import {
9
+ TRANSFER_POLICY_CAP_TYPE,
10
+ TRANSFER_POLICY_CREATED_EVENT,
11
+ TRANSFER_POLICY_TYPE,
12
+ TransferPolicy,
13
+ TransferPolicyCap,
14
+ } from '../types';
15
+ import { getAllOwnedObjects, parseTransferPolicyCapObject } from '../utils';
7
16
 
8
17
  /**
9
18
  * Searches the `TransferPolicy`-s for the given type. The seach is performed via
@@ -40,7 +49,7 @@ export async function queryTransferPolicy(
40
49
  throw new Error(`Invalid policy: ${policy?.objectId}, expected object, got package`);
41
50
  }
42
51
 
43
- let parsed = bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, 'base64');
52
+ const parsed = bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, 'base64');
44
53
 
45
54
  return {
46
55
  id: policy?.objectId,
@@ -51,3 +60,74 @@ export async function queryTransferPolicy(
51
60
  } as TransferPolicy;
52
61
  });
53
62
  }
63
+
64
+ /**
65
+ * A function to fetch all the user's kiosk Caps
66
+ * And a list of the kiosk address ids.
67
+ * Returns a list of `kioskOwnerCapIds` and `kioskIds`.
68
+ * Extra options allow pagination.
69
+ * @returns TransferPolicyCap Object ID | undefined if not found.
70
+ */
71
+ export async function queryTransferPolicyCapsByType(
72
+ client: SuiClient,
73
+ address: string,
74
+ type: string,
75
+ ): Promise<TransferPolicyCap[]> {
76
+ if (!isValidSuiAddress(address)) return [];
77
+
78
+ const filter = {
79
+ MatchAll: [
80
+ {
81
+ StructType: `${TRANSFER_POLICY_CAP_TYPE}<${type}>`,
82
+ },
83
+ ],
84
+ };
85
+
86
+ // fetch owned kiosk caps, paginated.
87
+ const data = await getAllOwnedObjects({
88
+ client,
89
+ filter,
90
+ owner: address,
91
+ });
92
+
93
+ return data
94
+ .map((item) => parseTransferPolicyCapObject(item))
95
+ .filter((item) => !!item) as TransferPolicyCap[];
96
+ }
97
+
98
+ /**
99
+ * A function to fetch all the user's kiosk Caps
100
+ * And a list of the kiosk address ids.
101
+ * Returns a list of `kioskOwnerCapIds` and `kioskIds`.
102
+ * Extra options allow pagination.
103
+ * @returns TransferPolicyCap Object ID | undefined if not found.
104
+ */
105
+ export async function queryOwnedTransferPolicies(
106
+ client: SuiClient,
107
+ address: string,
108
+ ): Promise<TransferPolicyCap[] | undefined> {
109
+ if (!isValidSuiAddress(address)) return;
110
+
111
+ const filter = {
112
+ MatchAll: [
113
+ {
114
+ MoveModule: {
115
+ module: 'transfer_policy',
116
+ package: '0x2',
117
+ },
118
+ },
119
+ ],
120
+ };
121
+
122
+ // fetch all owned kiosk caps, paginated.
123
+ const data = await getAllOwnedObjects({ client, owner: address, filter });
124
+
125
+ const policies: TransferPolicyCap[] = [];
126
+
127
+ for (const item of data) {
128
+ const data = parseTransferPolicyCapObject(item);
129
+ if (data) policies.push(data);
130
+ }
131
+
132
+ return policies;
133
+ }
package/src/tx/kiosk.ts CHANGED
@@ -1,27 +1,16 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { TransactionBlock, TransactionArgument } from '@mysten/sui.js/transactions';
4
+ import { TransactionArgument, TransactionBlock } from '@mysten/sui.js/transactions';
5
5
 
6
- import { getTypeWithoutPackageAddress, objArg } from '../utils';
7
- import { confirmRequest, resolveKioskLockRule, resolveRoyaltyRule } from './transfer-policy';
8
- import {
9
- KIOSK_LOCK_RULE,
10
- KIOSK_MODULE,
11
- KIOSK_TYPE,
12
- ObjectArgument,
13
- PurchaseAndResolvePoliciesResponse,
14
- PurchaseOptionalParams,
15
- ROYALTY_RULE,
16
- RulesEnvironmentParam,
17
- TransferPolicy,
18
- } from '../types';
6
+ import { KIOSK_MODULE, KIOSK_TYPE, ObjectArgument } from '../types';
7
+ import { objArg } from '../utils';
19
8
 
20
9
  /**
21
10
  * Create a new shared Kiosk and returns the [kiosk, kioskOwnerCap] tuple.
22
11
  */
23
12
  export function createKiosk(tx: TransactionBlock): [TransactionArgument, TransactionArgument] {
24
- let [kiosk, kioskOwnerCap] = tx.moveCall({
13
+ const [kiosk, kioskOwnerCap] = tx.moveCall({
25
14
  target: `${KIOSK_MODULE}::new`,
26
15
  });
27
16
 
@@ -33,17 +22,20 @@ export function createKiosk(tx: TransactionBlock): [TransactionArgument, Transac
33
22
  * Returns the `kioskOwnerCap` object.
34
23
  */
35
24
  export function createKioskAndShare(tx: TransactionBlock): TransactionArgument {
36
- let [kiosk, kioskOwnerCap] = tx.moveCall({
37
- target: `${KIOSK_MODULE}::new`,
38
- });
25
+ const [kiosk, kioskOwnerCap] = createKiosk(tx);
26
+ shareKiosk(tx, kiosk);
27
+ return kioskOwnerCap;
28
+ }
39
29
 
30
+ /**
31
+ * Converts Transfer Policy to a shared object.
32
+ */
33
+ export function shareKiosk(tx: TransactionBlock, kiosk: TransactionArgument) {
40
34
  tx.moveCall({
41
35
  target: `0x2::transfer::public_share_object`,
42
36
  typeArguments: [KIOSK_TYPE],
43
37
  arguments: [kiosk],
44
38
  });
45
-
46
- return kioskOwnerCap;
47
39
  }
48
40
 
49
41
  /**
@@ -98,7 +90,7 @@ export function take(
98
90
  kioskCap: ObjectArgument,
99
91
  itemId: string,
100
92
  ): TransactionArgument {
101
- let [item] = tx.moveCall({
93
+ const [item] = tx.moveCall({
102
94
  target: `${KIOSK_MODULE}::take`,
103
95
  typeArguments: [itemType],
104
96
  arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, 'address')],
@@ -179,7 +171,7 @@ export function purchase(
179
171
  itemId: string,
180
172
  payment: ObjectArgument,
181
173
  ): [TransactionArgument, TransactionArgument] {
182
- let [item, transferRequest] = tx.moveCall({
174
+ const [item, transferRequest] = tx.moveCall({
183
175
  target: `${KIOSK_MODULE}::purchase`,
184
176
  typeArguments: [itemType],
185
177
  arguments: [objArg(tx, kiosk), tx.pure(itemId, 'address'), objArg(tx, payment)],
@@ -196,19 +188,11 @@ export function withdrawFromKiosk(
196
188
  tx: TransactionBlock,
197
189
  kiosk: ObjectArgument,
198
190
  kioskCap: ObjectArgument,
199
- amount: string | bigint | null,
191
+ amount?: string | bigint | number,
200
192
  ): TransactionArgument {
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>');
193
+ const amountArg = tx.pure(amount ? { Some: amount } : { None: true }, 'Option<u64>');
210
194
 
211
- let [coin] = tx.moveCall({
195
+ const [coin] = tx.moveCall({
212
196
  target: `${KIOSK_MODULE}::withdraw`,
213
197
  arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), amountArg],
214
198
  });
@@ -216,46 +200,6 @@ export function withdrawFromKiosk(
216
200
  return coin;
217
201
  }
218
202
 
219
- /**
220
- * Call the `kiosk::borrow<T>(Kiosk, KioskOwnerCap, ID): &T` function.
221
- * Immutably borrow an item from the Kiosk.
222
- */
223
- export function borrow(
224
- tx: TransactionBlock,
225
- itemType: string,
226
- kiosk: ObjectArgument,
227
- kioskCap: ObjectArgument,
228
- itemId: string,
229
- ): TransactionArgument {
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;
237
- }
238
-
239
- /**
240
- * Call the `kiosk::borrow_mut<T>(Kiosk, KioskOwnerCap, ID): &mut T` function.
241
- * Mutably borrow an item from the Kiosk.
242
- */
243
- export function borrowMut(
244
- tx: TransactionBlock,
245
- itemType: string,
246
- kiosk: ObjectArgument,
247
- kioskCap: ObjectArgument,
248
- itemId: string,
249
- ): TransactionArgument {
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;
257
- }
258
-
259
203
  /**
260
204
  * Call the `kiosk::borrow_value<T>(Kiosk, KioskOwnerCap, ID): T` function.
261
205
  * Immutably borrow an item from the Kiosk and return it in the end.
@@ -269,7 +213,7 @@ export function borrowValue(
269
213
  kioskCap: ObjectArgument,
270
214
  itemId: string,
271
215
  ): [TransactionArgument, TransactionArgument] {
272
- let [item, promise] = tx.moveCall({
216
+ const [item, promise] = tx.moveCall({
273
217
  target: `${KIOSK_MODULE}::borrow_val`,
274
218
  typeArguments: [itemType],
275
219
  arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), tx.pure(itemId, 'address')],
@@ -295,75 +239,3 @@ export function returnValue(
295
239
  arguments: [objArg(tx, kiosk), item, promise],
296
240
  });
297
241
  }
298
-
299
- /**
300
- * Completes the full purchase flow that includes:
301
- * 1. Purchasing the item.
302
- * 2. Resolving all the transfer policies (if any).
303
- * 3. Returns the item and whether the user can transfer it or not.
304
- *
305
- * If the item can be transferred, there's an extra transaction required by the user
306
- * to transfer it to an address or place it in their kiosk.
307
- */
308
- export function purchaseAndResolvePolicies(
309
- tx: TransactionBlock,
310
- itemType: string,
311
- price: string,
312
- kiosk: ObjectArgument,
313
- itemId: string,
314
- policy: TransferPolicy,
315
- environment: RulesEnvironmentParam,
316
- extraParams?: PurchaseOptionalParams,
317
- ): PurchaseAndResolvePoliciesResponse {
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
- };
369
- }
@@ -0,0 +1,35 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { TransactionArgument, TransactionBlock } from '@mysten/sui.js/transactions';
5
+
6
+ import { ObjectArgument } from '../types';
7
+ import { objArg } from '../utils';
8
+
9
+ export function convertToPersonalTx(
10
+ tx: TransactionBlock,
11
+ kiosk: ObjectArgument,
12
+ kioskOwnerCap: ObjectArgument,
13
+ packageId: string,
14
+ ): TransactionArgument {
15
+ const personalKioskCap = tx.moveCall({
16
+ target: `${packageId}::personal_kiosk::new`,
17
+ arguments: [objArg(tx, kiosk), objArg(tx, kioskOwnerCap)],
18
+ });
19
+
20
+ return personalKioskCap;
21
+ }
22
+
23
+ /**
24
+ * Transfers the personal kiosk Cap to the sender.
25
+ */
26
+ export function transferPersonalCapTx(
27
+ tx: TransactionBlock,
28
+ personalKioskCap: TransactionArgument,
29
+ packageId: string,
30
+ ) {
31
+ tx.moveCall({
32
+ target: `${packageId}::personal_kiosk::transfer_to_sender`,
33
+ arguments: [personalKioskCap],
34
+ });
35
+ }
@@ -0,0 +1,74 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { type TransactionBlock } from '@mysten/sui.js/transactions';
5
+
6
+ import { type ObjectArgument } from '../../types';
7
+ import { objArg } from '../../utils';
8
+
9
+ export function attachKioskLockRuleTx(
10
+ tx: TransactionBlock,
11
+ type: string,
12
+ policy: ObjectArgument,
13
+ policyCap: ObjectArgument,
14
+ packageId: string,
15
+ ) {
16
+ tx.moveCall({
17
+ target: `${packageId}::kiosk_lock_rule::add`,
18
+ typeArguments: [type],
19
+ arguments: [objArg(tx, policy), objArg(tx, policyCap)],
20
+ });
21
+ }
22
+
23
+ export function attachRoyaltyRuleTx(
24
+ tx: TransactionBlock,
25
+ type: string,
26
+ policy: ObjectArgument,
27
+ policyCap: ObjectArgument,
28
+ percentageBps: number | string, // this is in basis points.
29
+ minAmount: number | string,
30
+ packageId: string,
31
+ ) {
32
+ if (Number(percentageBps) < 0 || Number(percentageBps) > 10_000)
33
+ throw new Error('Invalid basis point percentage. Use a value between [0,10000].');
34
+
35
+ tx.moveCall({
36
+ target: `${packageId}::royalty_rule::add`,
37
+ typeArguments: [type],
38
+ arguments: [
39
+ objArg(tx, policy),
40
+ objArg(tx, policyCap),
41
+ tx.pure(percentageBps, 'u16'),
42
+ tx.pure(minAmount, 'u64'),
43
+ ],
44
+ });
45
+ }
46
+
47
+ export function attachPersonalKioskRuleTx(
48
+ tx: TransactionBlock,
49
+ type: string,
50
+ policy: ObjectArgument,
51
+ policyCap: ObjectArgument,
52
+ packageId: string,
53
+ ) {
54
+ tx.moveCall({
55
+ target: `${packageId}::personal_kiosk_rule::add`,
56
+ typeArguments: [type],
57
+ arguments: [objArg(tx, policy), objArg(tx, policyCap)],
58
+ });
59
+ }
60
+
61
+ export function attachFloorPriceRuleTx(
62
+ tx: TransactionBlock,
63
+ type: string,
64
+ policy: ObjectArgument,
65
+ policyCap: ObjectArgument,
66
+ minPrice: string | bigint,
67
+ packageId: string,
68
+ ) {
69
+ tx.moveCall({
70
+ target: `${packageId}::floor_price_rule::add`,
71
+ typeArguments: [type],
72
+ arguments: [objArg(tx, policy), objArg(tx, policyCap), tx.pure(minPrice, 'u64')],
73
+ });
74
+ }
@@ -0,0 +1,87 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { type RuleResolvingParams } from '../../types';
5
+ import { objArg } from '../../utils';
6
+ import { lock } from '../kiosk';
7
+
8
+ /**
9
+ * A helper to resolve the royalty rule.
10
+ */
11
+ export function resolveRoyaltyRule(params: RuleResolvingParams) {
12
+ const { transactionBlock: txb, itemType, price, packageId, transferRequest, policyId } = params;
13
+
14
+ const policyObj = objArg(txb, policyId);
15
+
16
+ // calculates the amount
17
+ const [amount] = txb.moveCall({
18
+ target: `${packageId}::royalty_rule::fee_amount`,
19
+ typeArguments: [itemType],
20
+ arguments: [policyObj, objArg(txb, price ?? '')],
21
+ });
22
+
23
+ // splits the coin.
24
+ const feeCoin = txb.splitCoins(txb.gas, [amount]);
25
+
26
+ // pays the policy
27
+ txb.moveCall({
28
+ target: `${packageId}::royalty_rule::pay`,
29
+ typeArguments: [itemType],
30
+ arguments: [policyObj, transferRequest, feeCoin],
31
+ });
32
+ }
33
+
34
+ export function resolveKioskLockRule(params: RuleResolvingParams) {
35
+ const {
36
+ transactionBlock: txb,
37
+ packageId,
38
+ itemType,
39
+ kiosk,
40
+ kioskCap,
41
+ policyId,
42
+ purchasedItem,
43
+ transferRequest,
44
+ } = params;
45
+
46
+ if (!kiosk || !kioskCap) throw new Error('Missing Owned Kiosk or Owned Kiosk Cap');
47
+
48
+ lock(txb, itemType, kiosk, kioskCap, policyId, purchasedItem);
49
+
50
+ // proves that the item is locked in the kiosk to the TP.
51
+ txb.moveCall({
52
+ target: `${packageId}::kiosk_lock_rule::prove`,
53
+ typeArguments: [itemType],
54
+ arguments: [transferRequest, objArg(txb, kiosk)],
55
+ });
56
+ }
57
+
58
+ /**
59
+ * A helper to resolve the personalKioskRule.
60
+ * @param params
61
+ */
62
+ export function resolvePersonalKioskRule(params: RuleResolvingParams) {
63
+ const { transactionBlock: txb, packageId, itemType, kiosk, transferRequest } = params;
64
+
65
+ if (!kiosk) throw new Error('Missing owned Kiosk.');
66
+
67
+ // proves that the destination kiosk is personal.
68
+ txb.moveCall({
69
+ target: `${packageId}::personal_kiosk_rule::prove`,
70
+ typeArguments: [itemType],
71
+ arguments: [objArg(txb, kiosk), transferRequest],
72
+ });
73
+ }
74
+
75
+ /**
76
+ * Resolves the floor price rule.
77
+ */
78
+ export function resolveFloorPriceRule(params: RuleResolvingParams) {
79
+ const { transactionBlock: txb, packageId, itemType, policyId, transferRequest } = params;
80
+
81
+ // proves that the destination kiosk is personal
82
+ txb.moveCall({
83
+ target: `${packageId}::floor_price_rule::prove`,
84
+ typeArguments: [itemType],
85
+ arguments: [objArg(txb, policyId), transferRequest],
86
+ });
87
+ }
@@ -1,15 +1,10 @@
1
1
  // Copyright (c) Mysten Labs, Inc.
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
- import { TransactionBlock, TransactionArgument } from '@mysten/sui.js/transactions';
5
- import { getRulePackageAddress, objArg } from '../utils';
6
- import { lock } from './kiosk';
7
- import {
8
- ObjectArgument,
9
- RulesEnvironmentParam,
10
- TRANSFER_POLICY_MODULE,
11
- TRANSFER_POLICY_TYPE,
12
- } from '../types';
4
+ import { TransactionArgument, TransactionBlock } from '@mysten/sui.js/transactions';
5
+
6
+ import { ObjectArgument, TRANSFER_POLICY_MODULE, TRANSFER_POLICY_TYPE } from '../types';
7
+ import { objArg } from '../utils';
13
8
 
14
9
  /**
15
10
  * Call the `transfer_policy::new` function to create a new transfer policy.
@@ -20,19 +15,47 @@ export function createTransferPolicy(
20
15
  itemType: string,
21
16
  publisher: ObjectArgument,
22
17
  ): TransactionArgument {
23
- let [transferPolicy, transferPolicyCap] = tx.moveCall({
18
+ const [transferPolicy, transferPolicyCap] = createTransferPolicyWithoutSharing(
19
+ tx,
20
+ itemType,
21
+ publisher,
22
+ );
23
+
24
+ shareTransferPolicy(tx, itemType, transferPolicy);
25
+
26
+ return transferPolicyCap;
27
+ }
28
+
29
+ /**
30
+ * Creates a transfer Policy and returns both the Policy and the Cap.
31
+ * Used if we want to use the policy before making it a shared object.
32
+ */
33
+ export function createTransferPolicyWithoutSharing(
34
+ tx: TransactionBlock,
35
+ itemType: string,
36
+ publisher: ObjectArgument,
37
+ ): [TransactionArgument, TransactionArgument] {
38
+ const [transferPolicy, transferPolicyCap] = tx.moveCall({
24
39
  target: `${TRANSFER_POLICY_MODULE}::new`,
25
40
  typeArguments: [itemType],
26
41
  arguments: [objArg(tx, publisher)],
27
42
  });
28
43
 
44
+ return [transferPolicy, transferPolicyCap];
45
+ }
46
+ /**
47
+ * Converts Transfer Policy to a shared object.
48
+ */
49
+ export function shareTransferPolicy(
50
+ tx: TransactionBlock,
51
+ itemType: string,
52
+ transferPolicy: TransactionArgument,
53
+ ) {
29
54
  tx.moveCall({
30
55
  target: `0x2::transfer::public_share_object`,
31
56
  typeArguments: [`${TRANSFER_POLICY_TYPE}<${itemType}>`],
32
57
  arguments: [transferPolicy],
33
58
  });
34
-
35
- return transferPolicyCap;
36
59
  }
37
60
 
38
61
  /**
@@ -43,14 +66,11 @@ export function withdrawFromPolicy(
43
66
  itemType: string,
44
67
  policy: ObjectArgument,
45
68
  policyCap: ObjectArgument,
46
- amount: string | bigint | null,
69
+ amount?: string | bigint | null,
47
70
  ): TransactionArgument {
48
- let amountArg =
49
- amount !== null
50
- ? tx.pure({ Some: amount }, 'Option<u64>')
51
- : tx.pure({ None: true }, 'Option<u64>');
71
+ const amountArg = tx.pure(amount ? { Some: amount } : { None: true }, 'Option<u64>');
52
72
 
53
- let [profits] = tx.moveCall({
73
+ const [profits] = tx.moveCall({
54
74
  target: `${TRANSFER_POLICY_MODULE}::withdraw`,
55
75
  typeArguments: [itemType],
56
76
  arguments: [objArg(tx, policy), objArg(tx, policyCap), amountArg],
@@ -85,69 +105,11 @@ export function removeTransferPolicyRule(
85
105
  ruleType: string,
86
106
  configType: string,
87
107
  policy: ObjectArgument,
88
- policyCap: TransactionArgument,
108
+ policyCap: ObjectArgument,
89
109
  ): void {
90
110
  tx.moveCall({
91
111
  target: `${TRANSFER_POLICY_MODULE}::remove_rule`,
92
112
  typeArguments: [itemType, ruleType, configType],
93
- arguments: [objArg(tx, policy), policyCap],
94
- });
95
- }
96
-
97
- /**
98
- * Calculates the amount to be paid for the royalty rule to be resolved,
99
- * splits the coin to pass the exact amount,
100
- * then calls the `royalty_rule::pay` function to resolve the royalty rule.
101
- */
102
- export function resolveRoyaltyRule(
103
- tx: TransactionBlock,
104
- itemType: string,
105
- price: string,
106
- policyId: ObjectArgument,
107
- transferRequest: TransactionArgument,
108
- environment: RulesEnvironmentParam,
109
- ) {
110
- const policyObj = objArg(tx, policyId);
111
- // calculates the amount
112
- const [amount] = tx.moveCall({
113
- target: `${getRulePackageAddress(environment)}::royalty_rule::fee_amount`,
114
- typeArguments: [itemType],
115
- arguments: [policyObj, objArg(tx, price)],
116
- });
117
-
118
- // splits the coin.
119
- const feeCoin = tx.splitCoins(tx.gas, [amount]);
120
-
121
- // pays the policy
122
- tx.moveCall({
123
- target: `${getRulePackageAddress(environment)}::royalty_rule::pay`,
124
- typeArguments: [itemType],
125
- arguments: [policyObj, transferRequest, feeCoin],
126
- });
127
- }
128
-
129
- /**
130
- * Locks the item in the supplied kiosk and
131
- * proves to the `kiosk_lock` rule that the item was indeed locked,
132
- * by calling the `kiosk_lock_rule::prove` function to resolve it.
133
- */
134
- export function resolveKioskLockRule(
135
- tx: TransactionBlock,
136
- itemType: string,
137
- item: TransactionArgument,
138
- kiosk: ObjectArgument,
139
- kioskCap: ObjectArgument,
140
- policyId: ObjectArgument,
141
- transferRequest: TransactionArgument,
142
- environment: RulesEnvironmentParam,
143
- ) {
144
- // lock item in the kiosk.
145
- lock(tx, itemType, kiosk, kioskCap, policyId, item);
146
-
147
- // proves that the item is locked in the kiosk to the TP.
148
- tx.moveCall({
149
- target: `${getRulePackageAddress(environment)}::kiosk_lock_rule::prove`,
150
- typeArguments: [itemType],
151
- arguments: [transferRequest, objArg(tx, kiosk)],
113
+ arguments: [objArg(tx, policy), objArg(tx, policyCap)],
152
114
  });
153
115
  }