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