@haneullabs/kiosk 0.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (126) hide show
  1. package/CHANGELOG.md +1070 -0
  2. package/README.md +8 -0
  3. package/dist/cjs/bcs.d.ts +23 -0
  4. package/dist/cjs/bcs.js +50 -0
  5. package/dist/cjs/bcs.js.map +7 -0
  6. package/dist/cjs/client/kiosk-client.d.ts +76 -0
  7. package/dist/cjs/client/kiosk-client.js +123 -0
  8. package/dist/cjs/client/kiosk-client.js.map +7 -0
  9. package/dist/cjs/client/kiosk-transaction.d.ts +186 -0
  10. package/dist/cjs/client/kiosk-transaction.js +462 -0
  11. package/dist/cjs/client/kiosk-transaction.js.map +7 -0
  12. package/dist/cjs/client/tp-transaction.d.ts +114 -0
  13. package/dist/cjs/client/tp-transaction.js +307 -0
  14. package/dist/cjs/client/tp-transaction.js.map +7 -0
  15. package/dist/cjs/constants.d.ts +31 -0
  16. package/dist/cjs/constants.js +102 -0
  17. package/dist/cjs/constants.js.map +7 -0
  18. package/dist/cjs/index.d.ts +6 -0
  19. package/dist/cjs/index.js +24 -0
  20. package/dist/cjs/index.js.map +7 -0
  21. package/dist/cjs/package.json +5 -0
  22. package/dist/cjs/query/kiosk.d.ts +8 -0
  23. package/dist/cjs/query/kiosk.js +181 -0
  24. package/dist/cjs/query/kiosk.js.map +7 -0
  25. package/dist/cjs/query/transfer-policy.d.ts +29 -0
  26. package/dist/cjs/query/transfer-policy.js +92 -0
  27. package/dist/cjs/query/transfer-policy.js.map +7 -0
  28. package/dist/cjs/tx/kiosk.d.ts +71 -0
  29. package/dist/cjs/tx/kiosk.js +130 -0
  30. package/dist/cjs/tx/kiosk.js.map +7 -0
  31. package/dist/cjs/tx/personal-kiosk.d.ts +7 -0
  32. package/dist/cjs/tx/personal-kiosk.js +38 -0
  33. package/dist/cjs/tx/personal-kiosk.js.map +7 -0
  34. package/dist/cjs/tx/rules/attach.d.ts +7 -0
  35. package/dist/cjs/tx/rules/attach.js +62 -0
  36. package/dist/cjs/tx/rules/attach.js.map +7 -0
  37. package/dist/cjs/tx/rules/resolve.d.ts +15 -0
  38. package/dist/cjs/tx/rules/resolve.js +109 -0
  39. package/dist/cjs/tx/rules/resolve.js.map +7 -0
  40. package/dist/cjs/tx/transfer-policy.d.ts +29 -0
  41. package/dist/cjs/tx/transfer-policy.js +78 -0
  42. package/dist/cjs/tx/transfer-policy.js.map +7 -0
  43. package/dist/cjs/types/index.d.ts +27 -0
  44. package/dist/cjs/types/index.js +33 -0
  45. package/dist/cjs/types/index.js.map +7 -0
  46. package/dist/cjs/types/kiosk.d.ts +160 -0
  47. package/dist/cjs/types/kiosk.js +37 -0
  48. package/dist/cjs/types/kiosk.js.map +7 -0
  49. package/dist/cjs/types/transfer-policy.d.ts +53 -0
  50. package/dist/cjs/types/transfer-policy.js +35 -0
  51. package/dist/cjs/types/transfer-policy.js.map +7 -0
  52. package/dist/cjs/utils.d.ts +51 -0
  53. package/dist/cjs/utils.js +198 -0
  54. package/dist/cjs/utils.js.map +7 -0
  55. package/dist/esm/bcs.d.ts +23 -0
  56. package/dist/esm/bcs.js +35 -0
  57. package/dist/esm/bcs.js.map +7 -0
  58. package/dist/esm/client/kiosk-client.d.ts +76 -0
  59. package/dist/esm/client/kiosk-client.js +114 -0
  60. package/dist/esm/client/kiosk-client.js.map +7 -0
  61. package/dist/esm/client/kiosk-transaction.d.ts +186 -0
  62. package/dist/esm/client/kiosk-transaction.js +432 -0
  63. package/dist/esm/client/kiosk-transaction.js.map +7 -0
  64. package/dist/esm/client/tp-transaction.d.ts +114 -0
  65. package/dist/esm/client/tp-transaction.js +298 -0
  66. package/dist/esm/client/tp-transaction.js.map +7 -0
  67. package/dist/esm/constants.d.ts +31 -0
  68. package/dist/esm/constants.js +87 -0
  69. package/dist/esm/constants.js.map +7 -0
  70. package/dist/esm/index.d.ts +6 -0
  71. package/dist/esm/index.js +7 -0
  72. package/dist/esm/index.js.map +7 -0
  73. package/dist/esm/package.json +5 -0
  74. package/dist/esm/query/kiosk.d.ts +8 -0
  75. package/dist/esm/query/kiosk.js +169 -0
  76. package/dist/esm/query/kiosk.js.map +7 -0
  77. package/dist/esm/query/transfer-policy.d.ts +29 -0
  78. package/dist/esm/query/transfer-policy.js +76 -0
  79. package/dist/esm/query/transfer-policy.js.map +7 -0
  80. package/dist/esm/tx/kiosk.d.ts +71 -0
  81. package/dist/esm/tx/kiosk.js +110 -0
  82. package/dist/esm/tx/kiosk.js.map +7 -0
  83. package/dist/esm/tx/personal-kiosk.d.ts +7 -0
  84. package/dist/esm/tx/personal-kiosk.js +18 -0
  85. package/dist/esm/tx/personal-kiosk.js.map +7 -0
  86. package/dist/esm/tx/rules/attach.d.ts +7 -0
  87. package/dist/esm/tx/rules/attach.js +42 -0
  88. package/dist/esm/tx/rules/attach.js.map +7 -0
  89. package/dist/esm/tx/rules/resolve.d.ts +15 -0
  90. package/dist/esm/tx/rules/resolve.js +89 -0
  91. package/dist/esm/tx/rules/resolve.js.map +7 -0
  92. package/dist/esm/tx/transfer-policy.d.ts +29 -0
  93. package/dist/esm/tx/transfer-policy.js +58 -0
  94. package/dist/esm/tx/transfer-policy.js.map +7 -0
  95. package/dist/esm/types/index.d.ts +27 -0
  96. package/dist/esm/types/index.js +12 -0
  97. package/dist/esm/types/index.js.map +7 -0
  98. package/dist/esm/types/kiosk.d.ts +160 -0
  99. package/dist/esm/types/kiosk.js +17 -0
  100. package/dist/esm/types/kiosk.js.map +7 -0
  101. package/dist/esm/types/transfer-policy.d.ts +53 -0
  102. package/dist/esm/types/transfer-policy.js +15 -0
  103. package/dist/esm/types/transfer-policy.js.map +7 -0
  104. package/dist/esm/utils.d.ts +51 -0
  105. package/dist/esm/utils.js +183 -0
  106. package/dist/esm/utils.js.map +7 -0
  107. package/dist/tsconfig.esm.tsbuildinfo +1 -0
  108. package/dist/tsconfig.tsbuildinfo +1 -0
  109. package/package.json +59 -0
  110. package/src/bcs.ts +39 -0
  111. package/src/client/kiosk-client.ts +163 -0
  112. package/src/client/kiosk-transaction.ts +526 -0
  113. package/src/client/tp-transaction.ts +357 -0
  114. package/src/constants.ts +121 -0
  115. package/src/index.ts +9 -0
  116. package/src/query/kiosk.ts +264 -0
  117. package/src/query/transfer-policy.ts +134 -0
  118. package/src/tx/kiosk.ts +243 -0
  119. package/src/tx/personal-kiosk.ts +34 -0
  120. package/src/tx/rules/attach.ts +73 -0
  121. package/src/tx/rules/resolve.ts +126 -0
  122. package/src/tx/transfer-policy.ts +120 -0
  123. package/src/types/index.ts +35 -0
  124. package/src/types/kiosk.ts +178 -0
  125. package/src/types/transfer-policy.ts +71 -0
  126. package/src/utils.ts +286 -0
@@ -0,0 +1,7 @@
1
+ {
2
+ "version": 3,
3
+ "sources": ["../../../src/client/kiosk-client.ts"],
4
+ "sourcesContent": ["// Copyright (c) Mysten Labs, Inc.\n// SPDX-License-Identifier: Apache-2.0\n\nimport type { PaginationArguments, HaneulClient } from '@haneullabs/haneul/client';\n\nimport {\n\tFLOOR_PRICE_RULE_ADDRESS,\n\tgetBaseRules,\n\tKIOSK_LOCK_RULE_ADDRESS,\n\tPERSONAL_KIOSK_RULE_ADDRESS,\n\tROYALTY_RULE_ADDRESS,\n\trules,\n} from '../constants.js';\nimport type { BaseRulePackageIds, TransferPolicyRule } from '../constants.js';\nimport { fetchKiosk, fetchKioskExtension, getOwnedKiosks } from '../query/kiosk.js';\nimport {\n\tqueryOwnedTransferPolicies,\n\tqueryTransferPolicy,\n\tqueryTransferPolicyCapsByType,\n} from '../query/transfer-policy.js';\nimport { Network } from '../types/index.js';\nimport type {\n\tFetchKioskOptions,\n\tKioskClientOptions,\n\tKioskData,\n\tOwnedKiosks,\n} from '../types/index.js';\n\n/**\n * A Client that allows you to interact with kiosk.\n * Offers utilities to query kiosk, craft transactions to edit your own kiosk,\n * purchase, manage transfer policies, create new kiosks etc.\n * If you pass packageIds, all functionality will be managed using these packages.\n */\nexport class KioskClient {\n\tclient: HaneulClient;\n\tnetwork: Network;\n\trules: TransferPolicyRule[];\n\tpackageIds?: BaseRulePackageIds;\n\n\tconstructor(options: KioskClientOptions) {\n\t\tthis.client = options.client;\n\t\tthis.network = options.network;\n\t\tthis.rules = rules; // add all the default rules.\n\t\tthis.packageIds = options.packageIds;\n\n\t\t// Add the custom Package Ids too on the rule list.\n\t\t// Only adds the rules that are passed in the packageId object.\n\t\tif (options.packageIds) this.rules.push(...getBaseRules(options.packageIds));\n\t}\n\n\t/// Querying\n\n\t/**\n\t * Get an addresses's owned kiosks.\n\t * @param address The address for which we want to retrieve the kiosks.\n\t * @param pagination Optional pagination arguments.\n\t * @returns An Object containing all the `kioskOwnerCap` objects as well as the kioskIds.\n\t */\n\tasync getOwnedKiosks({\n\t\taddress,\n\t\tpagination,\n\t}: {\n\t\taddress: string;\n\t\tpagination?: PaginationArguments<string>;\n\t}): Promise<OwnedKiosks> {\n\t\tconst personalPackageId =\n\t\t\tthis.packageIds?.personalKioskRulePackageId || PERSONAL_KIOSK_RULE_ADDRESS[this.network];\n\n\t\treturn getOwnedKiosks(this.client, address, {\n\t\t\tpagination,\n\t\t\tpersonalKioskType: personalPackageId\n\t\t\t\t? `${personalPackageId}::personal_kiosk::PersonalKioskCap`\n\t\t\t\t: '',\n\t\t});\n\t}\n\n\t/**\n\t * Fetches the kiosk contents.\n\t * @param id The ID of the kiosk to fetch.\n\t * @param options Optional to control the fetch behavior.\n\t * @returns\n\t */\n\tasync getKiosk({ id, options }: { id: string; options?: FetchKioskOptions }): Promise<KioskData> {\n\t\treturn (await fetchKiosk(this.client, id, {}, options || {})).data;\n\t}\n\n\t/**\n\t * Fetch the extension data (if any) for a kiosk, by type\n\t * @param kioskId The ID of the kiosk to lookup\n\t * @param extensionType The Type of the extension (can be used from by using the type returned by `getKiosk()`)\n\t */\n\tasync getKioskExtension({ kioskId, type }: { kioskId: string; type: string }) {\n\t\treturn fetchKioskExtension(this.client, kioskId, type);\n\t}\n\n\t/**\n\t * Query the Transfer Policy(ies) for type `T`.\n\t * @param type The Type we're querying for (E.g `0xMyAddress::hero::Hero`)\n\t */\n\tasync getTransferPolicies({ type }: { type: string }) {\n\t\treturn queryTransferPolicy(this.client, type);\n\t}\n\n\t/**\n\t * Query all the owned transfer policies for an address.\n\t * Returns `TransferPolicyCap` which uncludes `policyId, policyCapId, type`.\n\t * @param address The address we're searching the owned transfer policies for.\n\t */\n\tasync getOwnedTransferPolicies({ address }: { address: string }) {\n\t\treturn queryOwnedTransferPolicies(this.client, address);\n\t}\n\n\t/**\n\t * Query the Transfer Policy Cap for type `T`, owned by `address`\n\t * @param type The Type `T` for the object\n\t * @param address The address that owns the cap.\n\t */\n\tasync getOwnedTransferPoliciesByType({ type, address }: { type: string; address: string }) {\n\t\treturn queryTransferPolicyCapsByType(this.client, address, type);\n\t}\n\n\t// Someone would just have to create a `kiosk-client.ts` file in their project, initialize a KioskClient\n\t// and call the `addRuleResolver` function. Each rule has a `resolve` function.\n\t// The resolve function is automatically called on `purchaseAndResolve` function call.\n\taddRuleResolver(rule: TransferPolicyRule) {\n\t\tif (this.rules.find((x) => x.rule === rule.rule))\n\t\t\tthrow new Error(`Rule ${rule.rule} resolver already exists.`);\n\t\tthis.rules.push(rule);\n\t}\n\n\t/**\n\t * A convenient helper to get the packageIds for our supported ruleset,\n\t * based on `kioskClient` configuration.\n\t */\n\tgetRulePackageId(\n\t\trule:\n\t\t\t| 'kioskLockRulePackageId'\n\t\t\t| 'royaltyRulePackageId'\n\t\t\t| 'personalKioskRulePackageId'\n\t\t\t| 'floorPriceRulePackageId',\n\t) {\n\t\tconst rules = this.packageIds || {};\n\t\tconst network = this.network;\n\n\t\t/// Check existence of rule based on network and throw an error if it's not found.\n\t\t/// We always have a fallback for testnet or mainnet.\n\t\tif (!rules[rule] && network !== Network.MAINNET && network !== Network.TESTNET) {\n\t\t\tthrow new Error(`Missing packageId for rule ${rule}`);\n\t\t}\n\n\t\tswitch (rule) {\n\t\t\tcase 'kioskLockRulePackageId':\n\t\t\t\treturn rules[rule] || KIOSK_LOCK_RULE_ADDRESS[network];\n\t\t\tcase 'royaltyRulePackageId':\n\t\t\t\treturn rules[rule] || ROYALTY_RULE_ADDRESS[network];\n\t\t\tcase 'personalKioskRulePackageId':\n\t\t\t\treturn rules[rule] || PERSONAL_KIOSK_RULE_ADDRESS[network];\n\t\t\tcase 'floorPriceRulePackageId':\n\t\t\t\treturn rules[rule] || FLOOR_PRICE_RULE_ADDRESS[network];\n\t\t}\n\t}\n}\n"],
5
+ "mappings": "AAKA;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACM;AAEP,SAAS,YAAY,qBAAqB,sBAAsB;AAChE;AAAA,EACC;AAAA,EACA;AAAA,EACA;AAAA,OACM;AACP,SAAS,eAAe;AAcjB,MAAM,YAAY;AAAA,EAMxB,YAAY,SAA6B;AACxC,SAAK,SAAS,QAAQ;AACtB,SAAK,UAAU,QAAQ;AACvB,SAAK,QAAQ;AACb,SAAK,aAAa,QAAQ;AAI1B,QAAI,QAAQ,WAAY,MAAK,MAAM,KAAK,GAAG,aAAa,QAAQ,UAAU,CAAC;AAAA,EAC5E;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAUA,MAAM,eAAe;AAAA,IACpB;AAAA,IACA;AAAA,EACD,GAGyB;AACxB,UAAM,oBACL,KAAK,YAAY,8BAA8B,4BAA4B,KAAK,OAAO;AAExF,WAAO,eAAe,KAAK,QAAQ,SAAS;AAAA,MAC3C;AAAA,MACA,mBAAmB,oBAChB,GAAG,iBAAiB,uCACpB;AAAA,IACJ,CAAC;AAAA,EACF;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAQA,MAAM,SAAS,EAAE,IAAI,QAAQ,GAAoE;AAChG,YAAQ,MAAM,WAAW,KAAK,QAAQ,IAAI,CAAC,GAAG,WAAW,CAAC,CAAC,GAAG;AAAA,EAC/D;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,kBAAkB,EAAE,SAAS,KAAK,GAAsC;AAC7E,WAAO,oBAAoB,KAAK,QAAQ,SAAS,IAAI;AAAA,EACtD;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,MAAM,oBAAoB,EAAE,KAAK,GAAqB;AACrD,WAAO,oBAAoB,KAAK,QAAQ,IAAI;AAAA,EAC7C;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,yBAAyB,EAAE,QAAQ,GAAwB;AAChE,WAAO,2BAA2B,KAAK,QAAQ,OAAO;AAAA,EACvD;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,EAOA,MAAM,+BAA+B,EAAE,MAAM,QAAQ,GAAsC;AAC1F,WAAO,8BAA8B,KAAK,QAAQ,SAAS,IAAI;AAAA,EAChE;AAAA;AAAA;AAAA;AAAA,EAKA,gBAAgB,MAA0B;AACzC,QAAI,KAAK,MAAM,KAAK,CAAC,MAAM,EAAE,SAAS,KAAK,IAAI;AAC9C,YAAM,IAAI,MAAM,QAAQ,KAAK,IAAI,2BAA2B;AAC7D,SAAK,MAAM,KAAK,IAAI;AAAA,EACrB;AAAA;AAAA;AAAA;AAAA;AAAA,EAMA,iBACC,MAKC;AACD,UAAMA,SAAQ,KAAK,cAAc,CAAC;AAClC,UAAM,UAAU,KAAK;AAIrB,QAAI,CAACA,OAAM,IAAI,KAAK,YAAY,QAAQ,WAAW,YAAY,QAAQ,SAAS;AAC/E,YAAM,IAAI,MAAM,8BAA8B,IAAI,EAAE;AAAA,IACrD;AAEA,YAAQ,MAAM;AAAA,MACb,KAAK;AACJ,eAAOA,OAAM,IAAI,KAAK,wBAAwB,OAAO;AAAA,MACtD,KAAK;AACJ,eAAOA,OAAM,IAAI,KAAK,qBAAqB,OAAO;AAAA,MACnD,KAAK;AACJ,eAAOA,OAAM,IAAI,KAAK,4BAA4B,OAAO;AAAA,MAC1D,KAAK;AACJ,eAAOA,OAAM,IAAI,KAAK,yBAAyB,OAAO;AAAA,IACxD;AAAA,EACD;AACD;",
6
+ "names": ["rules"]
7
+ }
@@ -0,0 +1,186 @@
1
+ import type { Transaction, TransactionArgument, TransactionObjectArgument } from '@haneullabs/haneul/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): this;
51
+ /**
52
+ * Shares the kiosk.
53
+ */
54
+ share(): this;
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): this;
60
+ /**
61
+ * A function to borrow an item from a kiosk & execute any function with it.
62
+ * Example: You could borrow a Fren out of a kiosk, attach an accessory (or mix), and return it.
63
+ */
64
+ borrowTx({ itemType, itemId }: ItemId, callback: (item: TransactionArgument) => void): this;
65
+ /**
66
+ * Borrows an item from the kiosk.
67
+ * This will fail if the item is listed for sale.
68
+ *
69
+ * Requires calling `return`.
70
+ */
71
+ borrow({ itemType, itemId }: ItemId): [TransactionArgument, TransactionArgument];
72
+ /**
73
+ * Returns the item back to the kiosk.
74
+ * Accepts the parameters returned from the `borrow` function.
75
+ */
76
+ return({ itemType, item, promise }: ItemValue & {
77
+ promise: TransactionArgument;
78
+ }): this;
79
+ /**
80
+ * A function to withdraw from kiosk
81
+ * @param address Where to trasnfer the coin.
82
+ * @param amount The amount we aim to withdraw.
83
+ */
84
+ withdraw(address: string, amount?: string | bigint | number): this;
85
+ /**
86
+ * A function to place an item in the kiosk.
87
+ * @param itemType The type `T` of the item
88
+ * @param item The ID or Transaction Argument of the item
89
+ */
90
+ place({ itemType, item }: ItemReference): this;
91
+ /**
92
+ * A function to place an item in the kiosk and list it for sale in one transaction.
93
+ * @param itemType The type `T` of the item
94
+ * @param item The ID or Transaction Argument of the item
95
+ * @param price The price in GEUNHWA
96
+ */
97
+ placeAndList({ itemType, item, price }: ItemReference & Price): this;
98
+ /**
99
+ * A function to list an item in the kiosk.
100
+ * @param itemType The type `T` of the item
101
+ * @param itemId The ID of the item
102
+ * @param price The price in GEUNHWA
103
+ */
104
+ list({ itemType, itemId, price }: ItemId & {
105
+ price: string | bigint;
106
+ }): this;
107
+ /**
108
+ * A function to delist an item from the kiosk.
109
+ * @param itemType The type `T` of the item
110
+ * @param itemId The ID of the item
111
+ */
112
+ delist({ itemType, itemId }: ItemId): this;
113
+ /**
114
+ * A function to take an item from the kiosk. The transaction won't succeed if the item is listed or locked.
115
+
116
+ * @param itemType The type `T` of the item
117
+ * @param itemId The ID of the item
118
+ */
119
+ take({ itemType, itemId }: ItemId): TransactionObjectArgument;
120
+ /**
121
+ * Transfer a non-locked/non-listed item to an address.
122
+ *
123
+ * @param itemType The type `T` of the item
124
+ * @param itemId The ID of the item
125
+ * @param address The destination address
126
+ */
127
+ transfer({ itemType, itemId, address }: ItemId & {
128
+ address: string;
129
+ }): this;
130
+ /**
131
+ * A function to take lock an item in the kiosk.
132
+
133
+ * @param itemType The type `T` of the item
134
+ * @param item The ID or Transaction Argument of the item
135
+ * @param itemId The ID of the item - Deprecated: Use `item` instead.
136
+ * @param policy The Policy ID or Transaction Argument for item T
137
+ */
138
+ lock({ itemType, item, itemId, policy, }: ItemReference & {
139
+ policy: ObjectArgument;
140
+ itemId?: string;
141
+ }): this;
142
+ /**
143
+ * Purchase an item from a seller's kiosk.
144
+ * Returns [item, transferRequest]
145
+ * Can be called like: `const [item, transferRequest] = kioskTx.purchase({...})`
146
+ * @param itemType The type `T` of the item
147
+ * @param itemId The ID of the item
148
+ * @param price The price in GEUNHWA
149
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
150
+ */
151
+ purchase({ itemType, itemId, price, sellerKiosk, }: ItemId & Price & {
152
+ sellerKiosk: ObjectArgument;
153
+ }): [
154
+ TransactionObjectArgument,
155
+ TransactionObjectArgument
156
+ ];
157
+ /**
158
+ * A function to purchase and resolve a transfer policy.
159
+ * If the transfer policy has the `lock` rule, the item is locked in the kiosk.
160
+ * Otherwise, the item is placed in the kiosk.
161
+ * @param itemType The type of the item
162
+ * @param itemId The id of the item
163
+ * @param price The price of the specified item
164
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
165
+ * @param extraArgs Used to pass arguments for custom rule resolvers.
166
+ */
167
+ purchaseAndResolve({ itemType, itemId, price, sellerKiosk, extraArgs, }: ItemId & Price & {
168
+ sellerKiosk: ObjectArgument;
169
+ } & PurchaseOptions): Promise<this>;
170
+ /**
171
+ * A function to setup the client using an existing `ownerCap`,
172
+ * as return from the `kioskClient.getOwnedKiosks` function.
173
+ * @param cap `KioskOwnerCap` object as returned from `getOwnedKiosks` SDK call.
174
+ */
175
+ setCap(cap: KioskOwnerCap): this | undefined;
176
+ /**
177
+ * A function that ends up the kiosk building tx & returns the `kioskOwnerCap` back to the
178
+ * `PersonalKioskCap`, in case we are operating on a personal kiosk.
179
+ * It will also share the `kiosk` if it's not shared, and finalize the transfer of the personal cap if it's pending.
180
+ */
181
+ finalize(): void;
182
+ setKioskCap(cap: TransactionObjectArgument): this;
183
+ setKiosk(kiosk: TransactionObjectArgument): this;
184
+ getKiosk(): TransactionObjectArgument;
185
+ getKioskCap(): TransactionObjectArgument;
186
+ }
@@ -0,0 +1,432 @@
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
+ return this;
86
+ }
87
+ /**
88
+ * Shares the kiosk.
89
+ */
90
+ share() {
91
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
92
+ __privateMethod(this, _KioskTransaction_instances, setPendingStatuses_fn).call(this, { share: false });
93
+ kioskTx.shareKiosk(this.transaction, this.kiosk);
94
+ return this;
95
+ }
96
+ /**
97
+ * Should be called only after `create` is called.
98
+ * It shares the kiosk & transfers the cap to the specified address.
99
+ */
100
+ shareAndTransferCap(address) {
101
+ if (__privateGet(this, _personalCap))
102
+ throw new Error("You can only call `shareAndTransferCap` on a non-personal kiosk.");
103
+ __privateMethod(this, _KioskTransaction_instances, setPendingStatuses_fn).call(this, { transfer: false });
104
+ this.share();
105
+ this.transaction.transferObjects([this.kioskCap], this.transaction.pure.address(address));
106
+ return this;
107
+ }
108
+ /**
109
+ * A function to borrow an item from a kiosk & execute any function with it.
110
+ * Example: You could borrow a Fren out of a kiosk, attach an accessory (or mix), and return it.
111
+ */
112
+ borrowTx({ itemType, itemId }, callback) {
113
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
114
+ const [itemObj, promise] = kioskTx.borrowValue(
115
+ this.transaction,
116
+ itemType,
117
+ this.kiosk,
118
+ this.kioskCap,
119
+ itemId
120
+ );
121
+ callback(itemObj);
122
+ return this.return({ itemType, item: itemObj, promise });
123
+ }
124
+ /**
125
+ * Borrows an item from the kiosk.
126
+ * This will fail if the item is listed for sale.
127
+ *
128
+ * Requires calling `return`.
129
+ */
130
+ borrow({ itemType, itemId }) {
131
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
132
+ const [itemObj, promise] = kioskTx.borrowValue(
133
+ this.transaction,
134
+ itemType,
135
+ this.kiosk,
136
+ this.kioskCap,
137
+ itemId
138
+ );
139
+ return [itemObj, promise];
140
+ }
141
+ /**
142
+ * Returns the item back to the kiosk.
143
+ * Accepts the parameters returned from the `borrow` function.
144
+ */
145
+ return({ itemType, item, promise }) {
146
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
147
+ kioskTx.returnValue(this.transaction, itemType, this.kiosk, item, promise);
148
+ return this;
149
+ }
150
+ /**
151
+ * A function to withdraw from kiosk
152
+ * @param address Where to trasnfer the coin.
153
+ * @param amount The amount we aim to withdraw.
154
+ */
155
+ withdraw(address, amount) {
156
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
157
+ const coin = kioskTx.withdrawFromKiosk(this.transaction, this.kiosk, this.kioskCap, amount);
158
+ this.transaction.transferObjects([coin], this.transaction.pure.address(address));
159
+ return this;
160
+ }
161
+ /**
162
+ * A function to place an item in the kiosk.
163
+ * @param itemType The type `T` of the item
164
+ * @param item The ID or Transaction Argument of the item
165
+ */
166
+ place({ itemType, item }) {
167
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
168
+ kioskTx.place(this.transaction, itemType, this.kiosk, this.kioskCap, item);
169
+ return this;
170
+ }
171
+ /**
172
+ * A function to place an item in the kiosk and list it for sale in one transaction.
173
+ * @param itemType The type `T` of the item
174
+ * @param item The ID or Transaction Argument of the item
175
+ * @param price The price in GEUNHWA
176
+ */
177
+ placeAndList({ itemType, item, price }) {
178
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
179
+ kioskTx.placeAndList(this.transaction, itemType, this.kiosk, this.kioskCap, item, price);
180
+ return this;
181
+ }
182
+ /**
183
+ * A function to list an item in the kiosk.
184
+ * @param itemType The type `T` of the item
185
+ * @param itemId The ID of the item
186
+ * @param price The price in GEUNHWA
187
+ */
188
+ list({ itemType, itemId, price }) {
189
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
190
+ kioskTx.list(this.transaction, itemType, this.kiosk, this.kioskCap, itemId, price);
191
+ return this;
192
+ }
193
+ /**
194
+ * A function to delist an item from the kiosk.
195
+ * @param itemType The type `T` of the item
196
+ * @param itemId The ID of the item
197
+ */
198
+ delist({ itemType, itemId }) {
199
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
200
+ kioskTx.delist(this.transaction, itemType, this.kiosk, this.kioskCap, itemId);
201
+ return this;
202
+ }
203
+ /**
204
+ * A function to take an item from the kiosk. The transaction won't succeed if the item is listed or locked.
205
+
206
+ * @param itemType The type `T` of the item
207
+ * @param itemId The ID of the item
208
+ */
209
+ take({ itemType, itemId }) {
210
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
211
+ return kioskTx.take(this.transaction, itemType, this.kiosk, this.kioskCap, itemId);
212
+ }
213
+ /**
214
+ * Transfer a non-locked/non-listed item to an address.
215
+ *
216
+ * @param itemType The type `T` of the item
217
+ * @param itemId The ID of the item
218
+ * @param address The destination address
219
+ */
220
+ transfer({ itemType, itemId, address }) {
221
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
222
+ const item = this.take({ itemType, itemId });
223
+ this.transaction.transferObjects([item], this.transaction.pure.address(address));
224
+ return this;
225
+ }
226
+ /**
227
+ * A function to take lock an item in the kiosk.
228
+
229
+ * @param itemType The type `T` of the item
230
+ * @param item The ID or Transaction Argument of the item
231
+ * @param itemId The ID of the item - Deprecated: Use `item` instead.
232
+ * @param policy The Policy ID or Transaction Argument for item T
233
+ */
234
+ lock({
235
+ itemType,
236
+ item,
237
+ itemId,
238
+ policy
239
+ }) {
240
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
241
+ kioskTx.lock(this.transaction, itemType, this.kiosk, this.kioskCap, policy, itemId ?? item);
242
+ return this;
243
+ }
244
+ /**
245
+ * Purchase an item from a seller's kiosk.
246
+ * Returns [item, transferRequest]
247
+ * Can be called like: `const [item, transferRequest] = kioskTx.purchase({...})`
248
+ * @param itemType The type `T` of the item
249
+ * @param itemId The ID of the item
250
+ * @param price The price in GEUNHWA
251
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
252
+ */
253
+ purchase({
254
+ itemType,
255
+ itemId,
256
+ price,
257
+ sellerKiosk
258
+ }) {
259
+ const coin = this.transaction.splitCoins(this.transaction.gas, [
260
+ this.transaction.pure.u64(price)
261
+ ]);
262
+ return kioskTx.purchase(this.transaction, itemType, sellerKiosk, itemId, coin);
263
+ }
264
+ /**
265
+ * A function to purchase and resolve a transfer policy.
266
+ * If the transfer policy has the `lock` rule, the item is locked in the kiosk.
267
+ * Otherwise, the item is placed in the kiosk.
268
+ * @param itemType The type of the item
269
+ * @param itemId The id of the item
270
+ * @param price The price of the specified item
271
+ * @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
272
+ * @param extraArgs Used to pass arguments for custom rule resolvers.
273
+ */
274
+ async purchaseAndResolve({
275
+ itemType,
276
+ itemId,
277
+ price,
278
+ sellerKiosk,
279
+ extraArgs
280
+ }) {
281
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
282
+ const policies = await this.kioskClient.getTransferPolicies({ type: itemType });
283
+ if (policies.length === 0) {
284
+ throw new Error(
285
+ `The type ${itemType} doesn't have a Transfer Policy so it can't be traded through kiosk.`
286
+ );
287
+ }
288
+ const policy = policies[0];
289
+ const [purchasedItem, transferRequest] = this.purchase({
290
+ itemType,
291
+ itemId,
292
+ price,
293
+ sellerKiosk
294
+ });
295
+ let canTransferOutsideKiosk = true;
296
+ for (const rule of policy.rules) {
297
+ const ruleDefinition = this.kioskClient.rules.find(
298
+ (x) => getNormalizedRuleType(x.rule) === getNormalizedRuleType(rule)
299
+ );
300
+ if (!ruleDefinition) throw new Error(`No resolver for the following rule: ${rule}.`);
301
+ if (ruleDefinition.hasLockingRule) canTransferOutsideKiosk = false;
302
+ await ruleDefinition.resolveRuleFunction({
303
+ packageId: ruleDefinition.packageId,
304
+ transactionBlock: this.transaction,
305
+ transaction: this.transaction,
306
+ itemType,
307
+ itemId,
308
+ price: price.toString(),
309
+ sellerKiosk,
310
+ policyId: policy.id,
311
+ transferRequest,
312
+ purchasedItem,
313
+ kiosk: this.kiosk,
314
+ kioskCap: this.kioskCap,
315
+ extraArgs: extraArgs || {},
316
+ kioskClient: this.kioskClient
317
+ });
318
+ }
319
+ confirmRequest(this.transaction, itemType, policy.id, transferRequest);
320
+ if (canTransferOutsideKiosk) this.place({ itemType, item: purchasedItem });
321
+ return this;
322
+ }
323
+ /**
324
+ * A function to setup the client using an existing `ownerCap`,
325
+ * as return from the `kioskClient.getOwnedKiosks` function.
326
+ * @param cap `KioskOwnerCap` object as returned from `getOwnedKiosks` SDK call.
327
+ */
328
+ setCap(cap) {
329
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
330
+ this.kiosk = this.transaction.object(cap.kioskId);
331
+ if (!cap.isPersonal) {
332
+ this.kioskCap = this.transaction.object(cap.objectId);
333
+ return;
334
+ }
335
+ return __privateMethod(this, _KioskTransaction_instances, borrowFromPersonalCap_fn).call(this, cap.objectId);
336
+ }
337
+ /**
338
+ * A function that ends up the kiosk building tx & returns the `kioskOwnerCap` back to the
339
+ * `PersonalKioskCap`, in case we are operating on a personal kiosk.
340
+ * It will also share the `kiosk` if it's not shared, and finalize the transfer of the personal cap if it's pending.
341
+ */
342
+ finalize() {
343
+ __privateMethod(this, _KioskTransaction_instances, validateKioskIsSet_fn).call(this);
344
+ if (__privateGet(this, _pendingShare)) this.share();
345
+ if (!__privateGet(this, _personalCap)) {
346
+ if (__privateGet(this, _pendingTransfer))
347
+ throw new Error(
348
+ "You need to transfer the `kioskOwnerCap` by calling `shareAndTransferCap()` before wrap"
349
+ );
350
+ return;
351
+ }
352
+ const packageId = this.kioskClient.getRulePackageId("personalKioskRulePackageId");
353
+ if (__privateGet(this, _promise)) {
354
+ this.transaction.moveCall({
355
+ target: `${packageId}::personal_kiosk::return_val`,
356
+ arguments: [__privateGet(this, _personalCap), this.transaction.object(this.kioskCap), __privateGet(this, _promise)]
357
+ });
358
+ }
359
+ if (__privateGet(this, _pendingTransfer))
360
+ transferPersonalCapTx(this.transaction, __privateGet(this, _personalCap), packageId);
361
+ __privateSet(this, _finalized, true);
362
+ }
363
+ // Some setters in case we want custom behavior.
364
+ setKioskCap(cap) {
365
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
366
+ this.kioskCap = cap;
367
+ return this;
368
+ }
369
+ setKiosk(kiosk) {
370
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
371
+ this.kiosk = kiosk;
372
+ return this;
373
+ }
374
+ // Some getters
375
+ /*
376
+ * Returns the active transaction's kiosk, or undefined if `setCap` or `create()` hasn't been called yet.
377
+ */
378
+ getKiosk() {
379
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
380
+ if (!this.kiosk) throw new Error("Kiosk is not set.");
381
+ return this.kiosk;
382
+ }
383
+ /*
384
+ * Returns the active transaction's kioskOwnerCap, or undefined if `setCap` or `create()` hasn't been called yet.
385
+ */
386
+ getKioskCap() {
387
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
388
+ if (!this.kioskCap) throw new Error("Kiosk cap is not set");
389
+ return this.kioskCap;
390
+ }
391
+ }
392
+ _pendingShare = new WeakMap();
393
+ _pendingTransfer = new WeakMap();
394
+ _promise = new WeakMap();
395
+ _personalCap = new WeakMap();
396
+ _finalized = new WeakMap();
397
+ _KioskTransaction_instances = new WeakSet();
398
+ /**
399
+ * A function to borrow from `personalCap`.
400
+ */
401
+ borrowFromPersonalCap_fn = function(personalCap) {
402
+ const [kioskCap, promise] = this.transaction.moveCall({
403
+ target: `${this.kioskClient.getRulePackageId(
404
+ "personalKioskRulePackageId"
405
+ )}::personal_kiosk::borrow_val`,
406
+ arguments: [this.transaction.object(personalCap)]
407
+ });
408
+ this.kioskCap = kioskCap;
409
+ __privateSet(this, _personalCap, this.transaction.object(personalCap));
410
+ __privateSet(this, _promise, promise);
411
+ return this;
412
+ };
413
+ setPendingStatuses_fn = function({ share, transfer }) {
414
+ if (transfer !== void 0) __privateSet(this, _pendingTransfer, transfer);
415
+ if (share !== void 0) __privateSet(this, _pendingShare, share);
416
+ };
417
+ validateKioskIsSet_fn = function() {
418
+ __privateMethod(this, _KioskTransaction_instances, validateFinalizedStatus_fn).call(this);
419
+ if (!this.kiosk || !this.kioskCap)
420
+ throw new Error(
421
+ "You need to initialize the client by either supplying an existing owner cap or by creating a new by calling `.create()`"
422
+ );
423
+ };
424
+ // Validates that `finalize`
425
+ validateFinalizedStatus_fn = function() {
426
+ if (__privateGet(this, _finalized))
427
+ throw new Error("You can't add more transactions to a finalized kiosk transaction.");
428
+ };
429
+ export {
430
+ KioskTransaction
431
+ };
432
+ //# sourceMappingURL=kiosk-transaction.js.map