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