@iota/kiosk 0.0.0-dev-20251015065834

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 (127) hide show
  1. package/CHANGELOG.md +198 -0
  2. package/LICENSE +201 -0
  3. package/README.md +39 -0
  4. package/dist/cjs/bcs.d.ts +38 -0
  5. package/dist/cjs/bcs.js +49 -0
  6. package/dist/cjs/bcs.js.map +7 -0
  7. package/dist/cjs/client/kiosk-client.d.ts +76 -0
  8. package/dist/cjs/client/kiosk-client.js +126 -0
  9. package/dist/cjs/client/kiosk-client.js.map +7 -0
  10. package/dist/cjs/client/kiosk-transaction.d.ts +185 -0
  11. package/dist/cjs/client/kiosk-transaction.js +473 -0
  12. package/dist/cjs/client/kiosk-transaction.js.map +7 -0
  13. package/dist/cjs/client/tp-transaction.d.ts +114 -0
  14. package/dist/cjs/client/tp-transaction.js +312 -0
  15. package/dist/cjs/client/tp-transaction.js.map +7 -0
  16. package/dist/cjs/constants.d.ts +24 -0
  17. package/dist/cjs/constants.js +66 -0
  18. package/dist/cjs/constants.js.map +7 -0
  19. package/dist/cjs/index.d.ts +6 -0
  20. package/dist/cjs/index.js +24 -0
  21. package/dist/cjs/index.js.map +7 -0
  22. package/dist/cjs/package.json +5 -0
  23. package/dist/cjs/query/kiosk.d.ts +14 -0
  24. package/dist/cjs/query/kiosk.js +129 -0
  25. package/dist/cjs/query/kiosk.js.map +7 -0
  26. package/dist/cjs/query/transfer-policy.d.ts +29 -0
  27. package/dist/cjs/query/transfer-policy.js +94 -0
  28. package/dist/cjs/query/transfer-policy.js.map +7 -0
  29. package/dist/cjs/tx/kiosk.d.ts +71 -0
  30. package/dist/cjs/tx/kiosk.js +130 -0
  31. package/dist/cjs/tx/kiosk.js.map +7 -0
  32. package/dist/cjs/tx/personal-kiosk.d.ts +7 -0
  33. package/dist/cjs/tx/personal-kiosk.js +38 -0
  34. package/dist/cjs/tx/personal-kiosk.js.map +7 -0
  35. package/dist/cjs/tx/rules/attach.d.ts +7 -0
  36. package/dist/cjs/tx/rules/attach.js +62 -0
  37. package/dist/cjs/tx/rules/attach.js.map +7 -0
  38. package/dist/cjs/tx/rules/resolve.d.ts +15 -0
  39. package/dist/cjs/tx/rules/resolve.js +79 -0
  40. package/dist/cjs/tx/rules/resolve.js.map +7 -0
  41. package/dist/cjs/tx/transfer-policy.d.ts +29 -0
  42. package/dist/cjs/tx/transfer-policy.js +78 -0
  43. package/dist/cjs/tx/transfer-policy.js.map +7 -0
  44. package/dist/cjs/types/index.d.ts +17 -0
  45. package/dist/cjs/types/index.js +20 -0
  46. package/dist/cjs/types/index.js.map +7 -0
  47. package/dist/cjs/types/kiosk.d.ts +159 -0
  48. package/dist/cjs/types/kiosk.js +37 -0
  49. package/dist/cjs/types/kiosk.js.map +7 -0
  50. package/dist/cjs/types/transfer-policy.d.ts +51 -0
  51. package/dist/cjs/types/transfer-policy.js +35 -0
  52. package/dist/cjs/types/transfer-policy.js.map +7 -0
  53. package/dist/cjs/utils.d.ts +51 -0
  54. package/dist/cjs/utils.js +201 -0
  55. package/dist/cjs/utils.js.map +7 -0
  56. package/dist/esm/bcs.d.ts +38 -0
  57. package/dist/esm/bcs.js +34 -0
  58. package/dist/esm/bcs.js.map +7 -0
  59. package/dist/esm/client/kiosk-client.d.ts +76 -0
  60. package/dist/esm/client/kiosk-client.js +110 -0
  61. package/dist/esm/client/kiosk-client.js.map +7 -0
  62. package/dist/esm/client/kiosk-transaction.d.ts +185 -0
  63. package/dist/esm/client/kiosk-transaction.js +443 -0
  64. package/dist/esm/client/kiosk-transaction.js.map +7 -0
  65. package/dist/esm/client/tp-transaction.d.ts +114 -0
  66. package/dist/esm/client/tp-transaction.js +303 -0
  67. package/dist/esm/client/tp-transaction.js.map +7 -0
  68. package/dist/esm/constants.d.ts +24 -0
  69. package/dist/esm/constants.js +51 -0
  70. package/dist/esm/constants.js.map +7 -0
  71. package/dist/esm/index.d.ts +6 -0
  72. package/dist/esm/index.js +7 -0
  73. package/dist/esm/index.js.map +7 -0
  74. package/dist/esm/package.json +5 -0
  75. package/dist/esm/query/kiosk.d.ts +14 -0
  76. package/dist/esm/query/kiosk.js +117 -0
  77. package/dist/esm/query/kiosk.js.map +7 -0
  78. package/dist/esm/query/transfer-policy.d.ts +29 -0
  79. package/dist/esm/query/transfer-policy.js +78 -0
  80. package/dist/esm/query/transfer-policy.js.map +7 -0
  81. package/dist/esm/tx/kiosk.d.ts +71 -0
  82. package/dist/esm/tx/kiosk.js +110 -0
  83. package/dist/esm/tx/kiosk.js.map +7 -0
  84. package/dist/esm/tx/personal-kiosk.d.ts +7 -0
  85. package/dist/esm/tx/personal-kiosk.js +18 -0
  86. package/dist/esm/tx/personal-kiosk.js.map +7 -0
  87. package/dist/esm/tx/rules/attach.d.ts +7 -0
  88. package/dist/esm/tx/rules/attach.js +42 -0
  89. package/dist/esm/tx/rules/attach.js.map +7 -0
  90. package/dist/esm/tx/rules/resolve.d.ts +15 -0
  91. package/dist/esm/tx/rules/resolve.js +59 -0
  92. package/dist/esm/tx/rules/resolve.js.map +7 -0
  93. package/dist/esm/tx/transfer-policy.d.ts +29 -0
  94. package/dist/esm/tx/transfer-policy.js +58 -0
  95. package/dist/esm/tx/transfer-policy.js.map +7 -0
  96. package/dist/esm/types/index.d.ts +17 -0
  97. package/dist/esm/types/index.js +3 -0
  98. package/dist/esm/types/index.js.map +7 -0
  99. package/dist/esm/types/kiosk.d.ts +159 -0
  100. package/dist/esm/types/kiosk.js +17 -0
  101. package/dist/esm/types/kiosk.js.map +7 -0
  102. package/dist/esm/types/transfer-policy.d.ts +51 -0
  103. package/dist/esm/types/transfer-policy.js +15 -0
  104. package/dist/esm/types/transfer-policy.js.map +7 -0
  105. package/dist/esm/utils.d.ts +51 -0
  106. package/dist/esm/utils.js +186 -0
  107. package/dist/esm/utils.js.map +7 -0
  108. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  109. package/dist/tsconfig.tsbuildinfo +1 -0
  110. package/package.json +53 -0
  111. package/src/bcs.ts +39 -0
  112. package/src/client/kiosk-client.ts +169 -0
  113. package/src/client/kiosk-transaction.ts +538 -0
  114. package/src/client/tp-transaction.ts +363 -0
  115. package/src/constants.ts +85 -0
  116. package/src/index.ts +10 -0
  117. package/src/query/kiosk.ts +193 -0
  118. package/src/query/transfer-policy.ts +137 -0
  119. package/src/tx/kiosk.ts +244 -0
  120. package/src/tx/personal-kiosk.ts +35 -0
  121. package/src/tx/rules/attach.ts +74 -0
  122. package/src/tx/rules/resolve.ts +87 -0
  123. package/src/tx/transfer-policy.ts +121 -0
  124. package/src/types/index.ts +25 -0
  125. package/src/types/kiosk.ts +178 -0
  126. package/src/types/transfer-policy.ts +70 -0
  127. package/src/utils.ts +289 -0
@@ -0,0 +1,185 @@
1
+ import type { Transaction, TransactionArgument, TransactionObjectArgument } from '@iota/iota-sdk/transactions';
2
+ import type { ItemId, ItemReference, ItemValue, KioskOwnerCap, ObjectArgument, Price, PurchaseOptions } from '../types/index.js';
3
+ import type { KioskClient } from './kiosk-client.js';
4
+ export type KioskTransactionParams = {
5
+ /** The Transaction for this run */
6
+ transaction: Transaction;
7
+ /** @deprecated use transaction instead */
8
+ transactionBlock?: Transaction;
9
+ /**
10
+ * You can create a new KioskClient by calling `new KioskClient()`
11
+ */
12
+ kioskClient: KioskClient;
13
+ /**
14
+ * You can optionally pass in the `cap` as returned
15
+ * from `kioskClient.getOwnedKiosks` when initializing the client
16
+ * Otherwise, you can set it by calling `kioskTransaction.setCap()`
17
+ */
18
+ cap?: KioskOwnerCap;
19
+ };
20
+ /**
21
+ * A helper for building transactions that involve kiosk.
22
+ */
23
+ export declare class KioskTransaction {
24
+ #private;
25
+ transaction: Transaction;
26
+ kioskClient: KioskClient;
27
+ kiosk?: TransactionObjectArgument;
28
+ kioskCap?: TransactionObjectArgument;
29
+ constructor({ transactionBlock, transaction, kioskClient, cap, }: KioskTransactionParams);
30
+ /**
31
+ * Creates a kiosk and saves `kiosk` and `kioskOwnerCap` in state.
32
+ * Helpful if we want to chain some actions before sharing + transferring the cap to the specified address.
33
+ * @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
34
+ */
35
+ create(): this;
36
+ /**
37
+ * Creates a personal kiosk & shares it.
38
+ * The `PersonalKioskCap` is transferred to the signer.
39
+ * @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
40
+ */
41
+ createPersonal(borrow?: boolean): this;
42
+ /**
43
+ * Converts a kiosk to a Personal (Soulbound) Kiosk.
44
+ * Requires initialization by either calling `ktxb.create()` or `ktxb.setCap()`.
45
+ */
46
+ convertToPersonal(borrow?: boolean): this;
47
+ /**
48
+ * Single function way to create a kiosk, share it and transfer the cap to the specified address.
49
+ */
50
+ createAndShare(address: string): void;
51
+ /**
52
+ * Shares the kiosk.
53
+ */
54
+ share(): void;
55
+ /**
56
+ * Should be called only after `create` is called.
57
+ * It shares the kiosk & transfers the cap to the specified address.
58
+ */
59
+ shareAndTransferCap(address: string): void;
60
+ /**
61
+ * A function to borrow an item from a kiosk & execute any function with it.
62
+ */
63
+ borrowTx({ itemType, itemId }: ItemId, callback: (item: TransactionArgument) => void): void;
64
+ /**
65
+ * Borrows an item from the kiosk.
66
+ * This will fail if the item is listed for sale.
67
+ *
68
+ * Requires calling `return`.
69
+ */
70
+ borrow({ itemType, itemId }: ItemId): [TransactionArgument, TransactionArgument];
71
+ /**
72
+ * Returns the item back to the kiosk.
73
+ * Accepts the parameters returned from the `borrow` function.
74
+ */
75
+ return({ itemType, item, promise }: ItemValue & {
76
+ promise: TransactionArgument;
77
+ }): this;
78
+ /**
79
+ * A function to withdraw from kiosk
80
+ * @param address Where to transfer the coin.
81
+ * @param amount The amount we aim to withdraw.
82
+ */
83
+ withdraw(address: string, amount?: string | bigint | number): this;
84
+ /**
85
+ * A function to place an item in the kiosk.
86
+ * @param itemType The type `T` of the item
87
+ * @param item The ID or Transaction Argument of the item
88
+ */
89
+ place({ itemType, item }: ItemReference): this;
90
+ /**
91
+ * A function to place an item in the kiosk and list it for sale in one transaction.
92
+ * @param itemType The type `T` of the item
93
+ * @param item The ID or Transaction Argument of the item
94
+ * @param price The price in NANOS
95
+ */
96
+ placeAndList({ itemType, item, price }: ItemReference & Price): this;
97
+ /**
98
+ * A function to list an item in the kiosk.
99
+ * @param itemType The type `T` of the item
100
+ * @param itemId The ID of the item
101
+ * @param price The price in NANOS
102
+ */
103
+ list({ itemType, itemId, price }: ItemId & {
104
+ price: string | bigint;
105
+ }): this;
106
+ /**
107
+ * A function to delist an item from the kiosk.
108
+ * @param itemType The type `T` of the item
109
+ * @param itemId The ID of the item
110
+ */
111
+ delist({ itemType, itemId }: ItemId): this;
112
+ /**
113
+ * A function to take an item from the kiosk. The transaction won't succeed if the item is listed or locked.
114
+
115
+ * @param itemType The type `T` of the item
116
+ * @param itemId The ID of the item
117
+ */
118
+ take({ itemType, itemId }: ItemId): TransactionObjectArgument;
119
+ /**
120
+ * Transfer a non-locked/non-listed item to an address.
121
+ *
122
+ * @param itemType The type `T` of the item
123
+ * @param itemId The ID of the item
124
+ * @param address The destination address
125
+ */
126
+ transfer({ itemType, itemId, address }: ItemId & {
127
+ address: string;
128
+ }): this;
129
+ /**
130
+ * A function to take lock an item in the kiosk.
131
+
132
+ * @param itemType The type `T` of the item
133
+ * @param item The ID or Transaction Argument of the item
134
+ * @param itemId The ID of the item - Deprecated: Use `item` instead.
135
+ * @param policy The Policy ID or Transaction Argument for item T
136
+ */
137
+ lock({ itemType, item, itemId, policy, }: ItemReference & {
138
+ policy: ObjectArgument;
139
+ itemId?: string;
140
+ }): this;
141
+ /**
142
+ * Purchase an item from a seller's kiosk.
143
+ * Returns [item, transferRequest]
144
+ * Can be called like: `const [item, transferRequest] = kioskTx.purchase({...})`
145
+ * @param itemType The type `T` of the item
146
+ * @param itemId The ID of the item
147
+ * @param price The price in NANOS
148
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
149
+ */
150
+ purchase({ itemType, itemId, price, sellerKiosk, }: ItemId & Price & {
151
+ sellerKiosk: ObjectArgument;
152
+ }): [
153
+ TransactionObjectArgument,
154
+ TransactionObjectArgument
155
+ ];
156
+ /**
157
+ * A function to purchase and resolve a transfer policy.
158
+ * If the transfer policy has the `lock` rule, the item is locked in the kiosk.
159
+ * Otherwise, the item is placed in the kiosk.
160
+ * @param itemType The type of the item
161
+ * @param itemId The id of the item
162
+ * @param price The price of the specified item
163
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
164
+ * @param extraArgs Used to pass arguments for custom rule resolvers.
165
+ */
166
+ purchaseAndResolve({ itemType, itemId, price, sellerKiosk, extraArgs, }: ItemId & Price & {
167
+ sellerKiosk: ObjectArgument;
168
+ } & PurchaseOptions): Promise<this>;
169
+ /**
170
+ * A function to setup the client using an existing `ownerCap`,
171
+ * as return from the `kioskClient.getOwnedKiosks` function.
172
+ * @param cap `KioskOwnerCap` object as returned from `getOwnedKiosks` SDK call.
173
+ */
174
+ setCap(cap: KioskOwnerCap): this | undefined;
175
+ /**
176
+ * A function that ends up the kiosk building tx & returns the `kioskOwnerCap` back to the
177
+ * `PersonalKioskCap`, in case we are operating on a personal kiosk.
178
+ * It will also share the `kiosk` if it's not shared, and finalize the transfer of the personal cap if it's pending.
179
+ */
180
+ finalize(): void;
181
+ setKioskCap(cap: TransactionObjectArgument): this;
182
+ setKiosk(kiosk: TransactionObjectArgument): this;
183
+ getKiosk(): TransactionObjectArgument;
184
+ getKioskCap(): TransactionObjectArgument;
185
+ }
@@ -0,0 +1,443 @@
1
+ var __typeError = (msg) => {
2
+ throw TypeError(msg);
3
+ };
4
+ var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
5
+ var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
6
+ var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
7
+ var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), setter ? setter.call(obj, value) : member.set(obj, value), value);
8
+ var __privateMethod = (obj, member, method) => (__accessCheck(obj, member, "access private method"), method);
9
+ var _pendingShare, _pendingTransfer, _promise, _personalCap, _finalized, _KioskTransaction_instances, borrowFromPersonalCap_fn, setPendingStatuses_fn, validateKioskIsSet_fn, validateFinalizedStatus_fn;
10
+ import * as kioskTx from "../tx/kiosk.js";
11
+ import { convertToPersonalTx, transferPersonalCapTx } from "../tx/personal-kiosk.js";
12
+ import { confirmRequest } from "../tx/transfer-policy.js";
13
+ import { getNormalizedRuleType } from "../utils.js";
14
+ class KioskTransaction {
15
+ constructor({
16
+ transactionBlock,
17
+ transaction = transactionBlock,
18
+ kioskClient,
19
+ cap
20
+ }) {
21
+ __privateAdd(this, _KioskTransaction_instances);
22
+ // If we're pending `share` of a new kiosk, `finalize()` will share it.
23
+ __privateAdd(this, _pendingShare);
24
+ // If we're pending transferring of the cap, `finalize()` will either error or transfer the cap if it's a new personal.
25
+ __privateAdd(this, _pendingTransfer);
26
+ // The promise that the personalCap will be returned on `finalize()`.
27
+ __privateAdd(this, _promise);
28
+ // The personal kiosk argument.
29
+ __privateAdd(this, _personalCap);
30
+ // A flag that checks whether kiosk TX is finalized.
31
+ __privateAdd(this, _finalized, false);
32
+ this.transaction = transaction;
33
+ this.kioskClient = kioskClient;
34
+ if (cap) this.setCap(cap);
35
+ }
36
+ /**
37
+ * Creates a kiosk and saves `kiosk` and `kioskOwnerCap` in state.
38
+ * Helpful if we want to chain some actions before sharing + transferring the cap to the specified address.
39
+ * @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
40
+ */
41
+ create() {
42
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
43
+ __privateMethod(this, _KioskTransaction_instances, setPendingStatuses_fn).call(this, {
44
+ share: true,
45
+ transfer: true
46
+ });
47
+ const [kiosk, cap] = kioskTx.createKiosk(this.transaction);
48
+ this.kiosk = kiosk;
49
+ this.kioskCap = cap;
50
+ return this;
51
+ }
52
+ /**
53
+ * Creates a personal kiosk & shares it.
54
+ * The `PersonalKioskCap` is transferred to the signer.
55
+ * @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
56
+ */
57
+ createPersonal(borrow) {
58
+ __privateSet(this, _pendingShare, true);
59
+ return this.create().convertToPersonal(borrow);
60
+ }
61
+ /**
62
+ * Converts a kiosk to a Personal (Soulbound) Kiosk.
63
+ * Requires initialization by either calling `ktxb.create()` or `ktxb.setCap()`.
64
+ */
65
+ convertToPersonal(borrow) {
66
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
67
+ const cap = convertToPersonalTx(
68
+ this.transaction,
69
+ this.kiosk,
70
+ this.kioskCap,
71
+ this.kioskClient.getRulePackageId("personalKioskRulePackageId")
72
+ );
73
+ if (borrow) __privateMethod(this, _KioskTransaction_instances, borrowFromPersonalCap_fn).call(this, cap);
74
+ else __privateSet(this, _personalCap, cap);
75
+ __privateMethod(this, _KioskTransaction_instances, setPendingStatuses_fn).call(this, { transfer: true });
76
+ return this;
77
+ }
78
+ /**
79
+ * Single function way to create a kiosk, share it and transfer the cap to the specified address.
80
+ */
81
+ createAndShare(address) {
82
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
83
+ const cap = kioskTx.createKioskAndShare(this.transaction);
84
+ this.transaction.transferObjects([cap], this.transaction.pure.address(address));
85
+ }
86
+ /**
87
+ * Shares the kiosk.
88
+ */
89
+ share() {
90
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
91
+ __privateMethod(this, _KioskTransaction_instances, setPendingStatuses_fn).call(this, { share: false });
92
+ kioskTx.shareKiosk(this.transaction, this.kiosk);
93
+ }
94
+ /**
95
+ * Should be called only after `create` is called.
96
+ * It shares the kiosk & transfers the cap to the specified address.
97
+ */
98
+ shareAndTransferCap(address) {
99
+ if (__privateGet(this, _personalCap))
100
+ throw new Error("You can only call `shareAndTransferCap` on a non-personal kiosk.");
101
+ __privateMethod(this, _KioskTransaction_instances, setPendingStatuses_fn).call(this, { transfer: false });
102
+ this.share();
103
+ this.transaction.transferObjects([this.kioskCap], this.transaction.pure.address(address));
104
+ }
105
+ /**
106
+ * A function to borrow an item from a kiosk & execute any function with it.
107
+ */
108
+ borrowTx({ itemType, itemId }, callback) {
109
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
110
+ const [itemObj, promise] = kioskTx.borrowValue(
111
+ this.transaction,
112
+ itemType,
113
+ this.kiosk,
114
+ this.kioskCap,
115
+ itemId
116
+ );
117
+ callback(itemObj);
118
+ this.return({ itemType, item: itemObj, promise });
119
+ }
120
+ /**
121
+ * Borrows an item from the kiosk.
122
+ * This will fail if the item is listed for sale.
123
+ *
124
+ * Requires calling `return`.
125
+ */
126
+ borrow({ itemType, itemId }) {
127
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
128
+ const [itemObj, promise] = kioskTx.borrowValue(
129
+ this.transaction,
130
+ itemType,
131
+ this.kiosk,
132
+ this.kioskCap,
133
+ itemId
134
+ );
135
+ return [itemObj, promise];
136
+ }
137
+ /**
138
+ * Returns the item back to the kiosk.
139
+ * Accepts the parameters returned from the `borrow` function.
140
+ */
141
+ return({ itemType, item, promise }) {
142
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
143
+ kioskTx.returnValue(this.transaction, itemType, this.kiosk, item, promise);
144
+ return this;
145
+ }
146
+ /**
147
+ * A function to withdraw from kiosk
148
+ * @param address Where to transfer the coin.
149
+ * @param amount The amount we aim to withdraw.
150
+ */
151
+ withdraw(address, amount) {
152
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
153
+ const coin = kioskTx.withdrawFromKiosk(
154
+ this.transaction,
155
+ this.kiosk,
156
+ this.kioskCap,
157
+ amount
158
+ );
159
+ this.transaction.transferObjects([coin], this.transaction.pure.address(address));
160
+ return this;
161
+ }
162
+ /**
163
+ * A function to place an item in the kiosk.
164
+ * @param itemType The type `T` of the item
165
+ * @param item The ID or Transaction Argument of the item
166
+ */
167
+ place({ itemType, item }) {
168
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
169
+ kioskTx.place(this.transaction, itemType, this.kiosk, this.kioskCap, item);
170
+ return this;
171
+ }
172
+ /**
173
+ * A function to place an item in the kiosk and list it for sale in one transaction.
174
+ * @param itemType The type `T` of the item
175
+ * @param item The ID or Transaction Argument of the item
176
+ * @param price The price in NANOS
177
+ */
178
+ placeAndList({ itemType, item, price }) {
179
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
180
+ kioskTx.placeAndList(this.transaction, itemType, this.kiosk, this.kioskCap, item, price);
181
+ return this;
182
+ }
183
+ /**
184
+ * A function to list an item in the kiosk.
185
+ * @param itemType The type `T` of the item
186
+ * @param itemId The ID of the item
187
+ * @param price The price in NANOS
188
+ */
189
+ list({ itemType, itemId, price }) {
190
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
191
+ kioskTx.list(this.transaction, itemType, this.kiosk, this.kioskCap, itemId, price);
192
+ return this;
193
+ }
194
+ /**
195
+ * A function to delist an item from the kiosk.
196
+ * @param itemType The type `T` of the item
197
+ * @param itemId The ID of the item
198
+ */
199
+ delist({ itemType, itemId }) {
200
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
201
+ kioskTx.delist(this.transaction, itemType, this.kiosk, this.kioskCap, itemId);
202
+ return this;
203
+ }
204
+ /**
205
+ * A function to take an item from the kiosk. The transaction won't succeed if the item is listed or locked.
206
+
207
+ * @param itemType The type `T` of the item
208
+ * @param itemId The ID of the item
209
+ */
210
+ take({ itemType, itemId }) {
211
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
212
+ return kioskTx.take(this.transaction, itemType, this.kiosk, this.kioskCap, itemId);
213
+ }
214
+ /**
215
+ * Transfer a non-locked/non-listed item to an address.
216
+ *
217
+ * @param itemType The type `T` of the item
218
+ * @param itemId The ID of the item
219
+ * @param address The destination address
220
+ */
221
+ transfer({ itemType, itemId, address }) {
222
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
223
+ const item = this.take({ itemType, itemId });
224
+ this.transaction.transferObjects([item], this.transaction.pure.address(address));
225
+ return this;
226
+ }
227
+ /**
228
+ * A function to take lock an item in the kiosk.
229
+
230
+ * @param itemType The type `T` of the item
231
+ * @param item The ID or Transaction Argument of the item
232
+ * @param itemId The ID of the item - Deprecated: Use `item` instead.
233
+ * @param policy The Policy ID or Transaction Argument for item T
234
+ */
235
+ lock({
236
+ itemType,
237
+ item,
238
+ itemId,
239
+ policy
240
+ }) {
241
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
242
+ kioskTx.lock(
243
+ this.transaction,
244
+ itemType,
245
+ this.kiosk,
246
+ this.kioskCap,
247
+ policy,
248
+ itemId ?? item
249
+ );
250
+ return this;
251
+ }
252
+ /**
253
+ * Purchase an item from a seller's kiosk.
254
+ * Returns [item, transferRequest]
255
+ * Can be called like: `const [item, transferRequest] = kioskTx.purchase({...})`
256
+ * @param itemType The type `T` of the item
257
+ * @param itemId The ID of the item
258
+ * @param price The price in NANOS
259
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
260
+ */
261
+ purchase({
262
+ itemType,
263
+ itemId,
264
+ price,
265
+ sellerKiosk
266
+ }) {
267
+ const coin = this.transaction.splitCoins(this.transaction.gas, [
268
+ this.transaction.pure.u64(price)
269
+ ]);
270
+ return kioskTx.purchase(this.transaction, itemType, sellerKiosk, itemId, coin);
271
+ }
272
+ /**
273
+ * A function to purchase and resolve a transfer policy.
274
+ * If the transfer policy has the `lock` rule, the item is locked in the kiosk.
275
+ * Otherwise, the item is placed in the kiosk.
276
+ * @param itemType The type of the item
277
+ * @param itemId The id of the item
278
+ * @param price The price of the specified item
279
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
280
+ * @param extraArgs Used to pass arguments for custom rule resolvers.
281
+ */
282
+ async purchaseAndResolve({
283
+ itemType,
284
+ itemId,
285
+ price,
286
+ sellerKiosk,
287
+ extraArgs
288
+ }) {
289
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
290
+ const policies = await this.kioskClient.getTransferPolicies({ type: itemType });
291
+ if (policies.length === 0) {
292
+ throw new Error(
293
+ `The type ${itemType} doesn't have a Transfer Policy so it can't be traded through kiosk.`
294
+ );
295
+ }
296
+ const policy = policies[0];
297
+ const [purchasedItem, transferRequest] = this.purchase({
298
+ itemType,
299
+ itemId,
300
+ price,
301
+ sellerKiosk
302
+ });
303
+ let canTransferOutsideKiosk = true;
304
+ for (const rule of policy.rules) {
305
+ const ruleDefinition = this.kioskClient.rules.find(
306
+ (x) => getNormalizedRuleType(x.rule) === getNormalizedRuleType(rule)
307
+ );
308
+ if (!ruleDefinition) throw new Error(`No resolver for the following rule: ${rule}.`);
309
+ if (ruleDefinition.hasLockingRule) canTransferOutsideKiosk = false;
310
+ ruleDefinition.resolveRuleFunction({
311
+ packageId: ruleDefinition.packageId,
312
+ transactionBlock: this.transaction,
313
+ transaction: this.transaction,
314
+ itemType,
315
+ itemId,
316
+ price: price.toString(),
317
+ sellerKiosk,
318
+ policyId: policy.id,
319
+ transferRequest,
320
+ purchasedItem,
321
+ kiosk: this.kiosk,
322
+ kioskCap: this.kioskCap,
323
+ extraArgs: extraArgs || {}
324
+ });
325
+ }
326
+ confirmRequest(this.transaction, itemType, policy.id, transferRequest);
327
+ if (canTransferOutsideKiosk) this.place({ itemType, item: purchasedItem });
328
+ return this;
329
+ }
330
+ /**
331
+ * A function to setup the client using an existing `ownerCap`,
332
+ * as return from the `kioskClient.getOwnedKiosks` function.
333
+ * @param cap `KioskOwnerCap` object as returned from `getOwnedKiosks` SDK call.
334
+ */
335
+ setCap(cap) {
336
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
337
+ this.kiosk = this.transaction.object(cap.kioskId);
338
+ if (!cap.isPersonal) {
339
+ this.kioskCap = this.transaction.object(cap.objectId);
340
+ return;
341
+ }
342
+ return __privateMethod(this, _KioskTransaction_instances, borrowFromPersonalCap_fn).call(this, cap.objectId);
343
+ }
344
+ /**
345
+ * A function that ends up the kiosk building tx & returns the `kioskOwnerCap` back to the
346
+ * `PersonalKioskCap`, in case we are operating on a personal kiosk.
347
+ * It will also share the `kiosk` if it's not shared, and finalize the transfer of the personal cap if it's pending.
348
+ */
349
+ finalize() {
350
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
351
+ if (__privateGet(this, _pendingShare)) this.share();
352
+ if (!__privateGet(this, _personalCap)) {
353
+ if (__privateGet(this, _pendingTransfer))
354
+ throw new Error(
355
+ "You need to transfer the `kioskOwnerCap` by calling `shareAndTransferCap()` before wrap"
356
+ );
357
+ return;
358
+ }
359
+ const packageId = this.kioskClient.getRulePackageId("personalKioskRulePackageId");
360
+ if (__privateGet(this, _promise)) {
361
+ this.transaction.moveCall({
362
+ target: `${packageId}::personal_kiosk::return_val`,
363
+ arguments: [
364
+ __privateGet(this, _personalCap),
365
+ this.transaction.object(this.kioskCap),
366
+ __privateGet(this, _promise)
367
+ ]
368
+ });
369
+ }
370
+ if (__privateGet(this, _pendingTransfer))
371
+ transferPersonalCapTx(this.transaction, __privateGet(this, _personalCap), packageId);
372
+ __privateSet(this, _finalized, true);
373
+ }
374
+ // Some setters in case we want custom behavior.
375
+ setKioskCap(cap) {
376
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
377
+ this.kioskCap = cap;
378
+ return this;
379
+ }
380
+ setKiosk(kiosk) {
381
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
382
+ this.kiosk = kiosk;
383
+ return this;
384
+ }
385
+ // Some getters
386
+ /*
387
+ * Returns the active transaction's kiosk, or undefined if `setCap` or `create()` hasn't been called yet.
388
+ */
389
+ getKiosk() {
390
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
391
+ if (!this.kiosk) throw new Error("Kiosk is not set.");
392
+ return this.kiosk;
393
+ }
394
+ /*
395
+ * Returns the active transaction's kioskOwnerCap, or undefined if `setCap` or `create()` hasn't been called yet.
396
+ */
397
+ getKioskCap() {
398
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
399
+ if (!this.kioskCap) throw new Error("Kiosk cap is not set");
400
+ return this.kioskCap;
401
+ }
402
+ }
403
+ _pendingShare = new WeakMap();
404
+ _pendingTransfer = new WeakMap();
405
+ _promise = new WeakMap();
406
+ _personalCap = new WeakMap();
407
+ _finalized = new WeakMap();
408
+ _KioskTransaction_instances = new WeakSet();
409
+ /**
410
+ * A function to borrow from `personalCap`.
411
+ */
412
+ borrowFromPersonalCap_fn = function(personalCap) {
413
+ const [kioskCap, promise] = this.transaction.moveCall({
414
+ target: `${this.kioskClient.getRulePackageId(
415
+ "personalKioskRulePackageId"
416
+ )}::personal_kiosk::borrow_val`,
417
+ arguments: [this.transaction.object(personalCap)]
418
+ });
419
+ this.kioskCap = kioskCap;
420
+ __privateSet(this, _personalCap, this.transaction.object(personalCap));
421
+ __privateSet(this, _promise, promise);
422
+ return this;
423
+ };
424
+ setPendingStatuses_fn = function({ share, transfer }) {
425
+ if (transfer !== void 0) __privateSet(this, _pendingTransfer, transfer);
426
+ if (share !== void 0) __privateSet(this, _pendingShare, share);
427
+ };
428
+ validateKioskIsSet_fn = function() {
429
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
430
+ if (!this.kiosk || !this.kioskCap)
431
+ throw new Error(
432
+ "You need to initialize the client by either supplying an existing owner cap or by creating a new by calling `.create()`"
433
+ );
434
+ };
435
+ // Validates that `finalize`
436
+ validateFinalizedStatus_fn = function() {
437
+ if (__privateGet(this, _finalized))
438
+ throw new Error("You can't add more transactions to a finalized kiosk transaction.");
439
+ };
440
+ export {
441
+ KioskTransaction
442
+ };
443
+ //# sourceMappingURL=kiosk-transaction.js.map