@haneullabs/kiosk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/CHANGELOG.md +1070 -0
  2. package/README.md +8 -0
  3. package/dist/cjs/bcs.d.ts +23 -0
  4. package/dist/cjs/bcs.js +50 -0
  5. package/dist/cjs/bcs.js.map +7 -0
  6. package/dist/cjs/client/kiosk-client.d.ts +76 -0
  7. package/dist/cjs/client/kiosk-client.js +123 -0
  8. package/dist/cjs/client/kiosk-client.js.map +7 -0
  9. package/dist/cjs/client/kiosk-transaction.d.ts +186 -0
  10. package/dist/cjs/client/kiosk-transaction.js +462 -0
  11. package/dist/cjs/client/kiosk-transaction.js.map +7 -0
  12. package/dist/cjs/client/tp-transaction.d.ts +114 -0
  13. package/dist/cjs/client/tp-transaction.js +307 -0
  14. package/dist/cjs/client/tp-transaction.js.map +7 -0
  15. package/dist/cjs/constants.d.ts +31 -0
  16. package/dist/cjs/constants.js +102 -0
  17. package/dist/cjs/constants.js.map +7 -0
  18. package/dist/cjs/index.d.ts +6 -0
  19. package/dist/cjs/index.js +24 -0
  20. package/dist/cjs/index.js.map +7 -0
  21. package/dist/cjs/package.json +5 -0
  22. package/dist/cjs/query/kiosk.d.ts +8 -0
  23. package/dist/cjs/query/kiosk.js +181 -0
  24. package/dist/cjs/query/kiosk.js.map +7 -0
  25. package/dist/cjs/query/transfer-policy.d.ts +29 -0
  26. package/dist/cjs/query/transfer-policy.js +92 -0
  27. package/dist/cjs/query/transfer-policy.js.map +7 -0
  28. package/dist/cjs/tx/kiosk.d.ts +71 -0
  29. package/dist/cjs/tx/kiosk.js +130 -0
  30. package/dist/cjs/tx/kiosk.js.map +7 -0
  31. package/dist/cjs/tx/personal-kiosk.d.ts +7 -0
  32. package/dist/cjs/tx/personal-kiosk.js +38 -0
  33. package/dist/cjs/tx/personal-kiosk.js.map +7 -0
  34. package/dist/cjs/tx/rules/attach.d.ts +7 -0
  35. package/dist/cjs/tx/rules/attach.js +62 -0
  36. package/dist/cjs/tx/rules/attach.js.map +7 -0
  37. package/dist/cjs/tx/rules/resolve.d.ts +15 -0
  38. package/dist/cjs/tx/rules/resolve.js +109 -0
  39. package/dist/cjs/tx/rules/resolve.js.map +7 -0
  40. package/dist/cjs/tx/transfer-policy.d.ts +29 -0
  41. package/dist/cjs/tx/transfer-policy.js +78 -0
  42. package/dist/cjs/tx/transfer-policy.js.map +7 -0
  43. package/dist/cjs/types/index.d.ts +27 -0
  44. package/dist/cjs/types/index.js +33 -0
  45. package/dist/cjs/types/index.js.map +7 -0
  46. package/dist/cjs/types/kiosk.d.ts +160 -0
  47. package/dist/cjs/types/kiosk.js +37 -0
  48. package/dist/cjs/types/kiosk.js.map +7 -0
  49. package/dist/cjs/types/transfer-policy.d.ts +53 -0
  50. package/dist/cjs/types/transfer-policy.js +35 -0
  51. package/dist/cjs/types/transfer-policy.js.map +7 -0
  52. package/dist/cjs/utils.d.ts +51 -0
  53. package/dist/cjs/utils.js +198 -0
  54. package/dist/cjs/utils.js.map +7 -0
  55. package/dist/esm/bcs.d.ts +23 -0
  56. package/dist/esm/bcs.js +35 -0
  57. package/dist/esm/bcs.js.map +7 -0
  58. package/dist/esm/client/kiosk-client.d.ts +76 -0
  59. package/dist/esm/client/kiosk-client.js +114 -0
  60. package/dist/esm/client/kiosk-client.js.map +7 -0
  61. package/dist/esm/client/kiosk-transaction.d.ts +186 -0
  62. package/dist/esm/client/kiosk-transaction.js +432 -0
  63. package/dist/esm/client/kiosk-transaction.js.map +7 -0
  64. package/dist/esm/client/tp-transaction.d.ts +114 -0
  65. package/dist/esm/client/tp-transaction.js +298 -0
  66. package/dist/esm/client/tp-transaction.js.map +7 -0
  67. package/dist/esm/constants.d.ts +31 -0
  68. package/dist/esm/constants.js +87 -0
  69. package/dist/esm/constants.js.map +7 -0
  70. package/dist/esm/index.d.ts +6 -0
  71. package/dist/esm/index.js +7 -0
  72. package/dist/esm/index.js.map +7 -0
  73. package/dist/esm/package.json +5 -0
  74. package/dist/esm/query/kiosk.d.ts +8 -0
  75. package/dist/esm/query/kiosk.js +169 -0
  76. package/dist/esm/query/kiosk.js.map +7 -0
  77. package/dist/esm/query/transfer-policy.d.ts +29 -0
  78. package/dist/esm/query/transfer-policy.js +76 -0
  79. package/dist/esm/query/transfer-policy.js.map +7 -0
  80. package/dist/esm/tx/kiosk.d.ts +71 -0
  81. package/dist/esm/tx/kiosk.js +110 -0
  82. package/dist/esm/tx/kiosk.js.map +7 -0
  83. package/dist/esm/tx/personal-kiosk.d.ts +7 -0
  84. package/dist/esm/tx/personal-kiosk.js +18 -0
  85. package/dist/esm/tx/personal-kiosk.js.map +7 -0
  86. package/dist/esm/tx/rules/attach.d.ts +7 -0
  87. package/dist/esm/tx/rules/attach.js +42 -0
  88. package/dist/esm/tx/rules/attach.js.map +7 -0
  89. package/dist/esm/tx/rules/resolve.d.ts +15 -0
  90. package/dist/esm/tx/rules/resolve.js +89 -0
  91. package/dist/esm/tx/rules/resolve.js.map +7 -0
  92. package/dist/esm/tx/transfer-policy.d.ts +29 -0
  93. package/dist/esm/tx/transfer-policy.js +58 -0
  94. package/dist/esm/tx/transfer-policy.js.map +7 -0
  95. package/dist/esm/types/index.d.ts +27 -0
  96. package/dist/esm/types/index.js +12 -0
  97. package/dist/esm/types/index.js.map +7 -0
  98. package/dist/esm/types/kiosk.d.ts +160 -0
  99. package/dist/esm/types/kiosk.js +17 -0
  100. package/dist/esm/types/kiosk.js.map +7 -0
  101. package/dist/esm/types/transfer-policy.d.ts +53 -0
  102. package/dist/esm/types/transfer-policy.js +15 -0
  103. package/dist/esm/types/transfer-policy.js.map +7 -0
  104. package/dist/esm/utils.d.ts +51 -0
  105. package/dist/esm/utils.js +183 -0
  106. package/dist/esm/utils.js.map +7 -0
  107. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  108. package/dist/tsconfig.tsbuildinfo +1 -0
  109. package/package.json +59 -0
  110. package/src/bcs.ts +39 -0
  111. package/src/client/kiosk-client.ts +163 -0
  112. package/src/client/kiosk-transaction.ts +526 -0
  113. package/src/client/tp-transaction.ts +357 -0
  114. package/src/constants.ts +121 -0
  115. package/src/index.ts +9 -0
  116. package/src/query/kiosk.ts +264 -0
  117. package/src/query/transfer-policy.ts +134 -0
  118. package/src/tx/kiosk.ts +243 -0
  119. package/src/tx/personal-kiosk.ts +34 -0
  120. package/src/tx/rules/attach.ts +73 -0
  121. package/src/tx/rules/resolve.ts +126 -0
  122. package/src/tx/transfer-policy.ts +120 -0
  123. package/src/types/index.ts +35 -0
  124. package/src/types/kiosk.ts +178 -0
  125. package/src/types/transfer-policy.ts +71 -0
  126. package/src/utils.ts +286 -0
@@ -0,0 +1,264 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type {
5
+ PaginatedObjectsResponse,
6
+ PaginationArguments,
7
+ HaneulClient,
8
+ HaneulObjectData,
9
+ HaneulObjectResponse,
10
+ } from '@haneullabs/haneul/client';
11
+ import { isValidHaneulAddress } from '@haneullabs/haneul/utils';
12
+
13
+ import type {
14
+ FetchKioskOptions,
15
+ KioskExtension,
16
+ KioskListing,
17
+ OwnedKiosks,
18
+ PagedKioskData,
19
+ } from '../types/index.js';
20
+ import { KIOSK_OWNER_CAP } from '../types/index.js';
21
+ import {
22
+ attachListingsAndPrices,
23
+ attachLockedItems,
24
+ attachObjects,
25
+ extractKioskData,
26
+ getAllDynamicFields,
27
+ getAllObjects,
28
+ getKioskObject,
29
+ } from '../utils.js';
30
+
31
+ export async function fetchKiosk(
32
+ client: HaneulClient,
33
+ kioskId: string,
34
+ pagination: PaginationArguments<string>,
35
+ options: FetchKioskOptions,
36
+ ): Promise<PagedKioskData> {
37
+ // TODO: Replace the `getAllDynamicFields` with a paginated
38
+ // response, once we have better RPC support for
39
+ // type filtering & batch fetching.
40
+ // This can't work with pagination currently.
41
+ const data = await getAllDynamicFields(client, kioskId, pagination);
42
+
43
+ const listings: KioskListing[] = [];
44
+ const lockedItemIds: string[] = [];
45
+
46
+ // extracted kiosk data.
47
+ const kioskData = extractKioskData(data, listings, lockedItemIds, kioskId);
48
+
49
+ // split the fetching in two queries as we are most likely passing different options for each kind.
50
+ // For items, we usually seek the Display.
51
+ // For listings we usually seek the DF value (price) / exclusivity.
52
+ const [kiosk, listingObjects, items] = await Promise.all([
53
+ options.withKioskFields ? getKioskObject(client, kioskId) : Promise.resolve(undefined),
54
+ options.withListingPrices
55
+ ? getAllObjects(client, kioskData.listingIds, {
56
+ showContent: true,
57
+ })
58
+ : Promise.resolve([]),
59
+ options.withObjects
60
+ ? getAllObjects(client, kioskData.itemIds, options.objectOptions || { showDisplay: true })
61
+ : Promise.resolve([]),
62
+ ]);
63
+
64
+ if (options.withKioskFields) kioskData.kiosk = kiosk;
65
+ // attach items listings. IF we have `options.withListingPrices === true`, it will also attach the prices.
66
+ attachListingsAndPrices(kioskData, listings, listingObjects);
67
+ // add `locked` status to items that are locked.
68
+ attachLockedItems(kioskData, lockedItemIds);
69
+
70
+ // Attach the objects for the queried items.
71
+ attachObjects(
72
+ kioskData,
73
+ items.filter((x) => !!x.data).map((x) => x.data!),
74
+ );
75
+
76
+ return {
77
+ data: kioskData,
78
+ nextCursor: null,
79
+ hasNextPage: false,
80
+ };
81
+ }
82
+
83
+ /**
84
+ * A function to fetch all the user's kiosk Caps
85
+ * And a list of the kiosk address ids.
86
+ * Returns a list of `kioskOwnerCapIds` and `kioskIds`.
87
+ * Extra options allow pagination.
88
+ */
89
+
90
+ const DEFAULT_PAGE_SIZE = 50;
91
+ const PERSON_KIOSK_CURSOR = 'personal';
92
+ const OWNED_KIOSKS_CURSOR = 'owned';
93
+ export async function getOwnedKiosks(
94
+ client: HaneulClient,
95
+ address: string,
96
+ options?: {
97
+ pagination?: PaginationArguments<string>;
98
+ personalKioskType: string;
99
+ },
100
+ ): Promise<OwnedKiosks> {
101
+ // TODO: this should throw an error, but I am not going to change it right now incase it breaks existing code.
102
+ if (!isValidHaneulAddress(address)) {
103
+ return {
104
+ nextCursor: null,
105
+ hasNextPage: false,
106
+ kioskOwnerCaps: [],
107
+ kioskIds: [],
108
+ };
109
+ }
110
+
111
+ const limit = options?.pagination?.limit ?? DEFAULT_PAGE_SIZE;
112
+ const [cursorType, cursor] = options?.pagination?.cursor?.split(':') ?? [
113
+ PERSON_KIOSK_CURSOR,
114
+ null,
115
+ ];
116
+
117
+ if (options?.personalKioskType && cursorType === PERSON_KIOSK_CURSOR) {
118
+ const personalKioskResponse = formatOwnedKioskResponse(
119
+ await client.getOwnedObjects({
120
+ owner: address,
121
+ filter: {
122
+ StructType: options.personalKioskType,
123
+ },
124
+ options: {
125
+ showContent: true,
126
+ showType: true,
127
+ },
128
+ cursor,
129
+ limit,
130
+ }),
131
+ PERSON_KIOSK_CURSOR,
132
+ );
133
+
134
+ if (personalKioskResponse.hasNextPage) {
135
+ return personalKioskResponse;
136
+ }
137
+
138
+ const remainingLimit = limit - personalKioskResponse.kioskOwnerCaps.length;
139
+
140
+ // If we have all personal kiosks, but don't have space for the owned kiosks
141
+ // we need to start loading owned kiosks for the next page, but don't have a real cursor
142
+ if (remainingLimit < 1) {
143
+ return {
144
+ nextCursor: `${OWNED_KIOSKS_CURSOR}:`,
145
+ hasNextPage: true,
146
+ kioskOwnerCaps: personalKioskResponse.kioskOwnerCaps,
147
+ kioskIds: personalKioskResponse.kioskIds,
148
+ };
149
+ }
150
+
151
+ const ownedKiosksResponse = formatOwnedKioskResponse(
152
+ await client.getOwnedObjects({
153
+ owner: address,
154
+ filter: {
155
+ StructType: KIOSK_OWNER_CAP,
156
+ },
157
+ options: {
158
+ showContent: true,
159
+ showType: true,
160
+ },
161
+ limit: remainingLimit,
162
+ }),
163
+ OWNED_KIOSKS_CURSOR,
164
+ );
165
+
166
+ return {
167
+ nextCursor: ownedKiosksResponse.nextCursor,
168
+ hasNextPage: ownedKiosksResponse.hasNextPage,
169
+ kioskOwnerCaps: [
170
+ ...personalKioskResponse.kioskOwnerCaps,
171
+ ...ownedKiosksResponse.kioskOwnerCaps,
172
+ ],
173
+ kioskIds: [...personalKioskResponse.kioskIds, ...ownedKiosksResponse.kioskIds],
174
+ };
175
+ }
176
+
177
+ return formatOwnedKioskResponse(
178
+ await client.getOwnedObjects({
179
+ owner: address,
180
+ filter: {
181
+ StructType: KIOSK_OWNER_CAP,
182
+ },
183
+ options: {
184
+ showContent: true,
185
+ showType: true,
186
+ },
187
+ // cursor might be an empty string if the number of personal kiosks was a multiple of the limit.
188
+ cursor: cursor ? cursor : null,
189
+ limit,
190
+ }),
191
+ OWNED_KIOSKS_CURSOR,
192
+ );
193
+ }
194
+
195
+ function formatOwnedKioskResponse(
196
+ response: PaginatedObjectsResponse,
197
+ cursorType: string,
198
+ ): OwnedKiosks {
199
+ const { data, hasNextPage, nextCursor } = response;
200
+ // get kioskIds from the OwnerCaps.
201
+ const kioskIdList = data?.map((x: HaneulObjectResponse) => {
202
+ const fields =
203
+ x.data?.content?.dataType === 'moveObject'
204
+ ? (x.data.content.fields as unknown as
205
+ | {
206
+ cap: { fields: { for: string } };
207
+ for?: never;
208
+ }
209
+ | {
210
+ cap?: never;
211
+ for: string;
212
+ })
213
+ : null;
214
+ return fields?.cap ? fields?.cap?.fields?.for : (fields?.for as string);
215
+ });
216
+
217
+ // clean up data that might have an error in them.
218
+ // only return valid objects.
219
+ const filteredData = data.filter((x) => 'data' in x).map((x) => x.data) as HaneulObjectData[];
220
+
221
+ return {
222
+ nextCursor: nextCursor ? `${cursorType}:${nextCursor}` : nextCursor,
223
+ hasNextPage,
224
+ kioskOwnerCaps: filteredData.map((x, idx) => ({
225
+ isPersonal: x.type !== KIOSK_OWNER_CAP,
226
+ digest: x.digest,
227
+ version: x.version,
228
+ objectId: x.objectId,
229
+ kioskId: kioskIdList[idx],
230
+ })),
231
+ kioskIds: kioskIdList,
232
+ };
233
+ }
234
+
235
+ // Get a kiosk extension data for a given kioskId and extensionType.
236
+ export async function fetchKioskExtension(
237
+ client: HaneulClient,
238
+ kioskId: string,
239
+ extensionType: string,
240
+ ): Promise<KioskExtension | null> {
241
+ const extension = await client.getDynamicFieldObject({
242
+ parentId: kioskId,
243
+ name: {
244
+ type: `0x2::kiosk_extension::ExtensionKey<${extensionType}>`,
245
+ value: {
246
+ dummy_field: false,
247
+ },
248
+ },
249
+ });
250
+
251
+ if (!extension.data) return null;
252
+
253
+ const fields = (extension?.data?.content as { fields: { [k: string]: any } })?.fields?.value
254
+ ?.fields;
255
+
256
+ return {
257
+ objectId: extension.data.objectId,
258
+ type: extensionType,
259
+ isEnabled: fields?.is_enabled,
260
+ permissions: fields?.permissions,
261
+ storageId: fields?.storage?.fields?.id?.id,
262
+ storageSize: fields?.storage?.fields?.size,
263
+ };
264
+ }
@@ -0,0 +1,134 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { HaneulClient } from '@haneullabs/haneul/client';
5
+ import { fromBase64, isValidHaneulAddress } from '@haneullabs/haneul/utils';
6
+
7
+ import '../bcs.js';
8
+
9
+ import { TransferPolicyType } from '../bcs.js';
10
+ import type { TransferPolicy, TransferPolicyCap } from '../types/index.js';
11
+ import {
12
+ TRANSFER_POLICY_CAP_TYPE,
13
+ TRANSFER_POLICY_CREATED_EVENT,
14
+ TRANSFER_POLICY_TYPE,
15
+ } from '../types/index.js';
16
+ import { getAllOwnedObjects, parseTransferPolicyCapObject } from '../utils.js';
17
+
18
+ /**
19
+ * Searches the `TransferPolicy`-s for the given type. The seach is performed via
20
+ * the `TransferPolicyCreated` event. The policy can either be owned or shared,
21
+ * and the caller needs to filter the results accordingly (ie single owner can not
22
+ * be accessed by anyone but the owner).
23
+ *
24
+ * @param provider
25
+ * @param type
26
+ */
27
+ export async function queryTransferPolicy(
28
+ client: HaneulClient,
29
+ type: string,
30
+ ): Promise<TransferPolicy[]> {
31
+ // console.log('event type: %s', `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`);
32
+ const { data } = await client.queryEvents({
33
+ query: {
34
+ MoveEventType: `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`,
35
+ },
36
+ });
37
+
38
+ const search = data.map((event) => event.parsedJson as { id: string });
39
+ const policies = await client.multiGetObjects({
40
+ ids: search.map((policy) => policy.id),
41
+ options: { showBcs: true, showOwner: true },
42
+ });
43
+
44
+ return policies
45
+ .filter((policy) => !!policy && 'data' in policy)
46
+ .map(({ data: policy }) => {
47
+ // should never happen; policies are objects and fetched via an event.
48
+ // policies are filtered for null and undefined above.
49
+ if (!policy || !policy.bcs || !('bcsBytes' in policy.bcs)) {
50
+ throw new Error(`Invalid policy: ${policy?.objectId}, expected object, got package`);
51
+ }
52
+
53
+ const parsed = TransferPolicyType.parse(fromBase64(policy.bcs.bcsBytes));
54
+
55
+ return {
56
+ id: policy?.objectId,
57
+ type: `${TRANSFER_POLICY_TYPE}<${type}>`,
58
+ owner: policy?.owner!,
59
+ rules: parsed.rules,
60
+ balance: parsed.balance,
61
+ } as TransferPolicy;
62
+ });
63
+ }
64
+
65
+ /**
66
+ * A function to fetch all the user's kiosk Caps
67
+ * And a list of the kiosk address ids.
68
+ * Returns a list of `kioskOwnerCapIds` and `kioskIds`.
69
+ * Extra options allow pagination.
70
+ * @returns TransferPolicyCap Object ID | undefined if not found.
71
+ */
72
+ export async function queryTransferPolicyCapsByType(
73
+ client: HaneulClient,
74
+ address: string,
75
+ type: string,
76
+ ): Promise<TransferPolicyCap[]> {
77
+ if (!isValidHaneulAddress(address)) return [];
78
+
79
+ const filter = {
80
+ MatchAll: [
81
+ {
82
+ StructType: `${TRANSFER_POLICY_CAP_TYPE}<${type}>`,
83
+ },
84
+ ],
85
+ };
86
+
87
+ // fetch owned kiosk caps, paginated.
88
+ const data = await getAllOwnedObjects({
89
+ client,
90
+ filter,
91
+ owner: address,
92
+ });
93
+
94
+ return data
95
+ .map((item) => parseTransferPolicyCapObject(item))
96
+ .filter((item) => !!item) as TransferPolicyCap[];
97
+ }
98
+
99
+ /**
100
+ * A function to fetch all the user's kiosk Caps
101
+ * And a list of the kiosk address ids.
102
+ * Returns a list of `kioskOwnerCapIds` and `kioskIds`.
103
+ * Extra options allow pagination.
104
+ * @returns TransferPolicyCap Object ID | undefined if not found.
105
+ */
106
+ export async function queryOwnedTransferPolicies(
107
+ client: HaneulClient,
108
+ address: string,
109
+ ): Promise<TransferPolicyCap[] | undefined> {
110
+ if (!isValidHaneulAddress(address)) return;
111
+
112
+ const filter = {
113
+ MatchAll: [
114
+ {
115
+ MoveModule: {
116
+ module: 'transfer_policy',
117
+ package: '0x2',
118
+ },
119
+ },
120
+ ],
121
+ };
122
+
123
+ // fetch all owned kiosk caps, paginated.
124
+ const data = await getAllOwnedObjects({ client, owner: address, filter });
125
+
126
+ const policies: TransferPolicyCap[] = [];
127
+
128
+ for (const item of data) {
129
+ const data = parseTransferPolicyCapObject(item);
130
+ if (data) policies.push(data);
131
+ }
132
+
133
+ return policies;
134
+ }
@@ -0,0 +1,243 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { bcs } from '@haneullabs/haneul/bcs';
5
+ import type {
6
+ Transaction,
7
+ TransactionArgument,
8
+ TransactionObjectArgument,
9
+ } from '@haneullabs/haneul/transactions';
10
+
11
+ import type { ObjectArgument } from '../types/index.js';
12
+ import { KIOSK_MODULE, KIOSK_TYPE } from '../types/index.js';
13
+
14
+ /**
15
+ * Create a new shared Kiosk and returns the [kiosk, kioskOwnerCap] tuple.
16
+ */
17
+ export function createKiosk(
18
+ tx: Transaction,
19
+ ): [TransactionObjectArgument, TransactionObjectArgument] {
20
+ const [kiosk, kioskOwnerCap] = tx.moveCall({
21
+ target: `${KIOSK_MODULE}::new`,
22
+ });
23
+
24
+ return [kiosk, kioskOwnerCap];
25
+ }
26
+
27
+ /**
28
+ * Calls the `kiosk::new()` function and shares the kiosk.
29
+ * Returns the `kioskOwnerCap` object.
30
+ */
31
+ export function createKioskAndShare(tx: Transaction): TransactionObjectArgument {
32
+ const [kiosk, kioskOwnerCap] = createKiosk(tx);
33
+ shareKiosk(tx, kiosk);
34
+ return kioskOwnerCap;
35
+ }
36
+
37
+ /**
38
+ * Converts Transfer Policy to a shared object.
39
+ */
40
+ export function shareKiosk(tx: Transaction, kiosk: TransactionArgument) {
41
+ tx.moveCall({
42
+ target: `0x2::transfer::public_share_object`,
43
+ typeArguments: [KIOSK_TYPE],
44
+ arguments: [kiosk],
45
+ });
46
+ }
47
+
48
+ /**
49
+ * Call the `kiosk::place<T>(Kiosk, KioskOwnerCap, Item)` function.
50
+ * Place an item to the Kiosk.
51
+ */
52
+ export function place(
53
+ tx: Transaction,
54
+ itemType: string,
55
+ kiosk: ObjectArgument,
56
+ kioskCap: ObjectArgument,
57
+ item: ObjectArgument,
58
+ ): void {
59
+ tx.moveCall({
60
+ target: `${KIOSK_MODULE}::place`,
61
+ typeArguments: [itemType],
62
+ arguments: [tx.object(kiosk), tx.object(kioskCap), tx.object(item)],
63
+ });
64
+ }
65
+
66
+ /**
67
+ * Call the `kiosk::lock<T>(Kiosk, KioskOwnerCap, TransferPolicy, Item)`
68
+ * function. Lock an item in the Kiosk.
69
+ *
70
+ * Unlike `place` this function requires a `TransferPolicy` to exist
71
+ * and be passed in. This is done to make sure the item does not get
72
+ * locked without an option to take it out.
73
+ */
74
+ export function lock(
75
+ tx: Transaction,
76
+ itemType: string,
77
+ kiosk: ObjectArgument,
78
+ kioskCap: ObjectArgument,
79
+ policy: ObjectArgument,
80
+ item: ObjectArgument,
81
+ ): void {
82
+ tx.moveCall({
83
+ target: `${KIOSK_MODULE}::lock`,
84
+ typeArguments: [itemType],
85
+ arguments: [tx.object(kiosk), tx.object(kioskCap), tx.object(policy), tx.object(item)],
86
+ });
87
+ }
88
+
89
+ /**
90
+ * Call the `kiosk::take<T>(Kiosk, KioskOwnerCap, ID)` function.
91
+ * Take an item from the Kiosk.
92
+ */
93
+ export function take(
94
+ tx: Transaction,
95
+ itemType: string,
96
+ kiosk: ObjectArgument,
97
+ kioskCap: ObjectArgument,
98
+ itemId: string,
99
+ ): TransactionObjectArgument {
100
+ const [item] = tx.moveCall({
101
+ target: `${KIOSK_MODULE}::take`,
102
+ typeArguments: [itemType],
103
+ arguments: [tx.object(kiosk), tx.object(kioskCap), tx.pure.id(itemId)],
104
+ });
105
+
106
+ return item;
107
+ }
108
+
109
+ /**
110
+ * Call the `kiosk::list<T>(Kiosk, KioskOwnerCap, ID, u64)` function.
111
+ * List an item for sale.
112
+ */
113
+ export function list(
114
+ tx: Transaction,
115
+ itemType: string,
116
+ kiosk: ObjectArgument,
117
+ kioskCap: ObjectArgument,
118
+ itemId: string,
119
+ price: string | bigint,
120
+ ): void {
121
+ tx.moveCall({
122
+ target: `${KIOSK_MODULE}::list`,
123
+ typeArguments: [itemType],
124
+ arguments: [tx.object(kiosk), tx.object(kioskCap), tx.pure.id(itemId), tx.pure.u64(price)],
125
+ });
126
+ }
127
+
128
+ /**
129
+ * Call the `kiosk::list<T>(Kiosk, KioskOwnerCap, ID, u64)` function.
130
+ * List an item for sale.
131
+ */
132
+ export function delist(
133
+ tx: Transaction,
134
+ itemType: string,
135
+ kiosk: ObjectArgument,
136
+ kioskCap: ObjectArgument,
137
+ itemId: string,
138
+ ): void {
139
+ tx.moveCall({
140
+ target: `${KIOSK_MODULE}::delist`,
141
+ typeArguments: [itemType],
142
+ arguments: [tx.object(kiosk), tx.object(kioskCap), tx.pure.id(itemId)],
143
+ });
144
+ }
145
+
146
+ /**
147
+ * Call the `kiosk::place_and_list<T>(Kiosk, KioskOwnerCap, Item, u64)` function.
148
+ * Place an item to the Kiosk and list it for sale.
149
+ */
150
+ export function placeAndList(
151
+ tx: Transaction,
152
+ itemType: string,
153
+ kiosk: ObjectArgument,
154
+ kioskCap: ObjectArgument,
155
+ item: ObjectArgument,
156
+ price: string | bigint,
157
+ ): void {
158
+ tx.moveCall({
159
+ target: `${KIOSK_MODULE}::place_and_list`,
160
+ typeArguments: [itemType],
161
+ arguments: [tx.object(kiosk), tx.object(kioskCap), tx.object(item), tx.pure.u64(price)],
162
+ });
163
+ }
164
+
165
+ /**
166
+ * Call the `kiosk::purchase<T>(Kiosk, ID, Coin<SUI>)` function and receive an Item and
167
+ * a TransferRequest which needs to be dealt with (via a matching TransferPolicy).
168
+ */
169
+ export function purchase(
170
+ tx: Transaction,
171
+ itemType: string,
172
+ kiosk: ObjectArgument,
173
+ itemId: string,
174
+ payment: ObjectArgument,
175
+ ): [TransactionObjectArgument, TransactionObjectArgument] {
176
+ const [item, transferRequest] = tx.moveCall({
177
+ target: `${KIOSK_MODULE}::purchase`,
178
+ typeArguments: [itemType],
179
+ arguments: [tx.object(kiosk), tx.pure.id(itemId), tx.object(payment)],
180
+ });
181
+
182
+ return [item, transferRequest];
183
+ }
184
+
185
+ /**
186
+ * Call the `kiosk::withdraw(Kiosk, KioskOwnerCap, Option<u64>)` function and receive a Coin<SUI>.
187
+ * If the amount is null, then the entire balance will be withdrawn.
188
+ */
189
+ export function withdrawFromKiosk(
190
+ tx: Transaction,
191
+ kiosk: ObjectArgument,
192
+ kioskCap: ObjectArgument,
193
+ amount?: string | bigint | number,
194
+ ): TransactionObjectArgument {
195
+ const amountArg = bcs.option(bcs.u64()).serialize(amount);
196
+
197
+ const [coin] = tx.moveCall({
198
+ target: `${KIOSK_MODULE}::withdraw`,
199
+ arguments: [tx.object(kiosk), tx.object(kioskCap), amountArg],
200
+ });
201
+
202
+ return coin;
203
+ }
204
+
205
+ /**
206
+ * Call the `kiosk::borrow_value<T>(Kiosk, KioskOwnerCap, ID): T` function.
207
+ * Immutably borrow an item from the Kiosk and return it in the end.
208
+ *
209
+ * Requires calling `returnValue` to return the item.
210
+ */
211
+ export function borrowValue(
212
+ tx: Transaction,
213
+ itemType: string,
214
+ kiosk: ObjectArgument,
215
+ kioskCap: ObjectArgument,
216
+ itemId: string,
217
+ ): [TransactionArgument, TransactionArgument] {
218
+ const [item, promise] = tx.moveCall({
219
+ target: `${KIOSK_MODULE}::borrow_val`,
220
+ typeArguments: [itemType],
221
+ arguments: [tx.object(kiosk), tx.object(kioskCap), tx.pure.id(itemId)],
222
+ });
223
+
224
+ return [item, promise];
225
+ }
226
+
227
+ /**
228
+ * Call the `kiosk::return_value<T>(Kiosk, Item, Borrow)` function.
229
+ * Return an item to the Kiosk after it was `borrowValue`-d.
230
+ */
231
+ export function returnValue(
232
+ tx: Transaction,
233
+ itemType: string,
234
+ kiosk: ObjectArgument,
235
+ item: TransactionArgument,
236
+ promise: TransactionArgument,
237
+ ): void {
238
+ tx.moveCall({
239
+ target: `${KIOSK_MODULE}::return_val`,
240
+ typeArguments: [itemType],
241
+ arguments: [tx.object(kiosk), item, promise],
242
+ });
243
+ }
@@ -0,0 +1,34 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import type { Transaction, TransactionObjectArgument } from '@haneullabs/haneul/transactions';
5
+
6
+ import type { ObjectArgument } from '../types/index.js';
7
+
8
+ export function convertToPersonalTx(
9
+ tx: Transaction,
10
+ kiosk: ObjectArgument,
11
+ kioskOwnerCap: ObjectArgument,
12
+ packageId: string,
13
+ ): TransactionObjectArgument {
14
+ const personalKioskCap = tx.moveCall({
15
+ target: `${packageId}::personal_kiosk::new`,
16
+ arguments: [tx.object(kiosk), tx.object(kioskOwnerCap)],
17
+ });
18
+
19
+ return personalKioskCap;
20
+ }
21
+
22
+ /**
23
+ * Transfers the personal kiosk Cap to the sender.
24
+ */
25
+ export function transferPersonalCapTx(
26
+ tx: Transaction,
27
+ personalKioskCap: TransactionObjectArgument,
28
+ packageId: string,
29
+ ) {
30
+ tx.moveCall({
31
+ target: `${packageId}::personal_kiosk::transfer_to_sender`,
32
+ arguments: [personalKioskCap],
33
+ });
34
+ }