@mysten/kiosk 0.6.0 → 0.7.1

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 (44) hide show
  1. package/CHANGELOG.md +17 -0
  2. package/README.md +2 -287
  3. package/dist/client/kiosk-client.d.ts +64 -0
  4. package/dist/client/kiosk-transaction.d.ts +182 -0
  5. package/dist/client/tp-transaction.d.ts +112 -0
  6. package/dist/constants.d.ts +30 -4
  7. package/dist/index.d.ts +3 -6
  8. package/dist/index.js +1256 -264
  9. package/dist/index.js.map +1 -1
  10. package/dist/index.mjs +1239 -230
  11. package/dist/index.mjs.map +1 -1
  12. package/dist/query/kiosk.d.ts +2 -1
  13. package/dist/query/transfer-policy.d.ts +17 -1
  14. package/dist/tx/kiosk.d.ts +11 -27
  15. package/dist/tx/personal-kiosk.d.ts +7 -0
  16. package/dist/tx/rules/attach.d.ts +7 -0
  17. package/dist/tx/rules/resolve.d.ts +15 -0
  18. package/dist/tx/transfer-policy.d.ts +14 -17
  19. package/dist/types/index.d.ts +23 -5
  20. package/dist/types/kiosk.d.ts +30 -1
  21. package/dist/types/transfer-policy.d.ts +27 -1
  22. package/dist/utils.d.ts +32 -18
  23. package/package.json +12 -4
  24. package/src/bcs.ts +1 -0
  25. package/src/client/kiosk-client.ts +156 -0
  26. package/src/client/kiosk-transaction.ts +519 -0
  27. package/src/client/tp-transaction.ts +350 -0
  28. package/src/constants.ts +113 -6
  29. package/src/index.ts +3 -6
  30. package/src/query/kiosk.ts +51 -18
  31. package/src/query/transfer-policy.ts +82 -2
  32. package/src/tx/kiosk.ts +37 -158
  33. package/src/tx/personal-kiosk.ts +35 -0
  34. package/src/tx/rules/attach.ts +74 -0
  35. package/src/tx/rules/resolve.ts +87 -0
  36. package/src/tx/transfer-policy.ts +46 -79
  37. package/src/types/index.ts +26 -5
  38. package/src/types/kiosk.ts +26 -1
  39. package/src/types/transfer-policy.ts +35 -1
  40. package/src/utils.ts +143 -35
  41. package/dist/tx/rules.d.ts +0 -19
  42. package/dist/types/env.d.ts +0 -12
  43. package/src/tx/rules.ts +0 -58
  44. package/src/types/env.ts +0 -20
@@ -1,4 +1,4 @@
1
- import { PaginatedObjectsResponse } from '@mysten/sui.js/client';
1
+ import { PaginatedObjectsResponse, SuiObjectData, SuiObjectDataOptions } from '@mysten/sui.js/client';
2
2
  import { TransactionArgument } from '@mysten/sui.js/transactions';
3
3
  import { ObjectArgument } from '.';
4
4
  /** The Kiosk module. */
@@ -82,6 +82,10 @@ export type KioskItem = {
82
82
  isLocked: boolean;
83
83
  /** Optional listing */
84
84
  listing?: KioskListing;
85
+ /** The ID of the kiosk the item is placed in */
86
+ kioskId: string;
87
+ /** Optional Kiosk Data */
88
+ data?: SuiObjectData;
85
89
  };
86
90
  /**
87
91
  * Aggregated data from the Kiosk.
@@ -99,16 +103,41 @@ export type PagedKioskData = {
99
103
  hasNextPage: boolean;
100
104
  };
101
105
  export type FetchKioskOptions = {
106
+ /** Include the base kiosk object, which includes the profits, the owner and the base fields. */
102
107
  withKioskFields?: boolean;
108
+ /** Include the listing prices. */
103
109
  withListingPrices?: boolean;
110
+ /** Include the objects for the Items in the kiosk. Defaults to `display` only. */
111
+ withObjects?: boolean;
112
+ /** Pass the data options for the objects, when fetching, in case you want to query other details. */
113
+ objectOptions?: SuiObjectDataOptions;
104
114
  };
105
115
  export type OwnedKiosks = {
106
116
  kioskOwnerCaps: KioskOwnerCap[];
107
117
  kioskIds: string[];
108
118
  } & Omit<PaginatedObjectsResponse, 'data'>;
109
119
  export type KioskOwnerCap = {
120
+ isPersonal?: boolean;
110
121
  objectId: string;
111
122
  kioskId: string;
112
123
  digest: string;
113
124
  version: string;
114
125
  };
126
+ export type PurchaseOptions = {
127
+ extraArgs?: Record<string, any>;
128
+ };
129
+ export type ItemId = {
130
+ itemType: string;
131
+ itemId: string;
132
+ };
133
+ export type ItemReference = {
134
+ itemType: string;
135
+ item: ObjectArgument;
136
+ };
137
+ export type ItemValue = {
138
+ itemType: string;
139
+ item: TransactionArgument;
140
+ };
141
+ export type Price = {
142
+ price: string | bigint;
143
+ };
@@ -1,14 +1,26 @@
1
- import { ObjectOwner } from '@mysten/sui.js/client';
1
+ import { type ObjectOwner } from '@mysten/sui.js/client';
2
+ import { TransactionObjectArgument, type TransactionBlock } from '@mysten/sui.js/transactions';
3
+ import { ObjectArgument } from '.';
2
4
  /** The Transfer Policy module. */
3
5
  export declare const TRANSFER_POLICY_MODULE = "0x2::transfer_policy";
4
6
  /** Name of the event emitted when a TransferPolicy for T is created. */
5
7
  export declare const TRANSFER_POLICY_CREATED_EVENT: string;
6
8
  /** The Transfer Policy Type */
7
9
  export declare const TRANSFER_POLICY_TYPE: string;
10
+ /** The Transfer Policy Cap Type */
11
+ export declare const TRANSFER_POLICY_CAP_TYPE: string;
8
12
  /** The Kiosk Lock Rule */
9
13
  export declare const KIOSK_LOCK_RULE = "kiosk_lock_rule::Rule";
10
14
  /** The Royalty rule */
11
15
  export declare const ROYALTY_RULE = "royalty_rule::Rule";
16
+ /**
17
+ * The Transfer Policy Cap in a consumable way.
18
+ */
19
+ export type TransferPolicyCap = {
20
+ policyId: string;
21
+ policyCapId: string;
22
+ type: string;
23
+ };
12
24
  /** The `TransferPolicy` object */
13
25
  export type TransferPolicy = {
14
26
  id: string;
@@ -21,3 +33,17 @@ export type TransferPolicy = {
21
33
  export type TransferPolicyCreated = {
22
34
  id: string;
23
35
  };
36
+ export type RuleResolvingParams = {
37
+ transactionBlock: TransactionBlock;
38
+ itemType: string;
39
+ itemId: string;
40
+ price: string;
41
+ policyId: ObjectArgument;
42
+ sellerKiosk: ObjectArgument;
43
+ kiosk: ObjectArgument;
44
+ kioskCap: ObjectArgument;
45
+ transferRequest: TransactionObjectArgument;
46
+ purchasedItem: TransactionObjectArgument;
47
+ packageId: string;
48
+ extraArgs: Record<string, any>;
49
+ };
package/dist/utils.d.ts CHANGED
@@ -1,44 +1,53 @@
1
1
  import { SharedObjectRef } from '@mysten/sui.js/bcs';
2
- import { SuiObjectRef, SuiObjectResponse } from '@mysten/sui.js/client';
3
- import { TransactionBlock, TransactionArgument } from '@mysten/sui.js/transactions';
4
- import { type DynamicFieldInfo } from '@mysten/sui.js/client';
5
- import { Kiosk, KioskData, KioskListing, RulesEnvironmentParam } from './types';
6
- import { SuiClient, PaginationArguments } from '@mysten/sui.js/client';
7
- export declare const rulesPackageAddresses: {
8
- mainnet: string;
9
- testnet: string;
10
- devnet: string;
11
- custom: null;
12
- };
2
+ import { PaginationArguments, SuiClient, SuiObjectData, SuiObjectDataFilter, SuiObjectDataOptions, SuiObjectRef, SuiObjectResponse, type DynamicFieldInfo } from '@mysten/sui.js/client';
3
+ import { TransactionBlock, TransactionObjectArgument } from '@mysten/sui.js/transactions';
4
+ import { Kiosk, KioskData, KioskListing, TransferPolicyCap } from './types';
13
5
  /**
14
6
  * Convert any valid input into a TransactionArgument.
15
7
  *
16
- * @param tx The transaction to use for creating the argument.
8
+ * @param txb The Transaction Block
17
9
  * @param arg The argument to convert.
18
10
  * @returns The converted TransactionArgument.
19
11
  */
20
- export declare function objArg(tx: TransactionBlock, arg: string | SharedObjectRef | SuiObjectRef | TransactionArgument): TransactionArgument;
12
+ export declare function objArg(txb: TransactionBlock, arg: string | SharedObjectRef | SuiObjectRef | TransactionObjectArgument): TransactionObjectArgument;
21
13
  export declare function getKioskObject(client: SuiClient, id: string): Promise<Kiosk>;
22
- export declare function extractKioskData(data: DynamicFieldInfo[], listings: KioskListing[], lockedItemIds: string[]): KioskData;
14
+ export declare function extractKioskData(data: DynamicFieldInfo[], listings: KioskListing[], lockedItemIds: string[], kioskId: string): KioskData;
23
15
  export declare function getTypeWithoutPackageAddress(type: string): string;
24
16
  /**
25
17
  * A helper that attaches the listing prices to kiosk listings.
26
18
  */
27
19
  export declare function attachListingsAndPrices(kioskData: KioskData, listings: KioskListing[], listingObjects: SuiObjectResponse[]): void;
28
20
  /**
29
- * A Helper to attach locked state to items in Kiosk Data.
21
+ * A helper that attaches the listing prices to kiosk listings.
30
22
  */
31
- export declare function attachLockedItems(kioskData: KioskData, lockedItemIds: string[]): void;
23
+ export declare function attachObjects(kioskData: KioskData, objects: SuiObjectData[]): void;
32
24
  /**
33
- * A helper to get a rule's environment address.
25
+ * A Helper to attach locked state to items in Kiosk Data.
34
26
  */
35
- export declare function getRulePackageAddress(environment: RulesEnvironmentParam): string;
27
+ export declare function attachLockedItems(kioskData: KioskData, lockedItemIds: string[]): void;
36
28
  /**
37
29
  * A helper to fetch all DF pages.
38
30
  * We need that to fetch the kiosk DFs consistently, until we have
39
31
  * RPC calls that allow filtering of Type / batch fetching of spec
40
32
  */
41
33
  export declare function getAllDynamicFields(client: SuiClient, parentId: string, pagination: PaginationArguments<string>): Promise<DynamicFieldInfo[]>;
34
+ /**
35
+ * A helper to fetch all objects that works with pagination.
36
+ * It will fetch all objects in the array, and limit it to 50/request.
37
+ * Requests are sent using `Promise.all`.
38
+ */
39
+ export declare function getAllObjects(client: SuiClient, ids: string[], options: SuiObjectDataOptions, limit?: number): Promise<SuiObjectResponse[]>;
40
+ /**
41
+ * A helper to return all owned objects, with an optional filter.
42
+ * It parses all the pages and returns the data.
43
+ */
44
+ export declare function getAllOwnedObjects({ client, owner, filter, limit, options, }: {
45
+ client: SuiClient;
46
+ owner: string;
47
+ filter?: SuiObjectDataFilter;
48
+ options?: SuiObjectDataOptions;
49
+ limit?: number;
50
+ }): Promise<SuiObjectResponse[]>;
42
51
  /**
43
52
  * Converts a number to basis points.
44
53
  * Supports up to 2 decimal points.
@@ -46,3 +55,8 @@ export declare function getAllDynamicFields(client: SuiClient, parentId: string,
46
55
  * @param percentage A percentage amount in the range [0, 100] including decimals.
47
56
  */
48
57
  export declare function percentageToBasisPoints(percentage: number): number;
58
+ /**
59
+ * A helper to parse a transfer policy Cap into a usable object.
60
+ */
61
+ export declare function parseTransferPolicyCapObject(item: SuiObjectResponse): TransferPolicyCap | undefined;
62
+ export declare function getNormalizedRuleType(rule: string): string;
package/package.json CHANGED
@@ -2,7 +2,7 @@
2
2
  "name": "@mysten/kiosk",
3
3
  "author": "Mysten Labs <build@mystenlabs.com>",
4
4
  "description": "Sui Kiosk library",
5
- "version": "0.6.0",
5
+ "version": "0.7.1",
6
6
  "license": "Apache-2.0",
7
7
  "main": "./dist/index.js",
8
8
  "module": "./dist/index.mjs",
@@ -24,11 +24,17 @@
24
24
  }
25
25
  },
26
26
  "dependencies": {
27
- "@mysten/sui.js": "0.42.0"
27
+ "@mysten/sui.js": "0.43.0"
28
28
  },
29
29
  "devDependencies": {
30
+ "cross-env": "^7.0.3",
31
+ "tmp": "^0.2.1",
32
+ "ts-retry-promise": "^0.7.0",
30
33
  "tsup": "^7.1.0",
31
- "typescript": "^5.1.6"
34
+ "typescript": "^5.1.6",
35
+ "vite": "^4.4.4",
36
+ "vitest": "^0.33.0",
37
+ "wait-on": "^7.0.1"
32
38
  },
33
39
  "scripts": {
34
40
  "build": "pnpm build:types && pnpm build:tsup",
@@ -41,6 +47,8 @@
41
47
  "eslint:check": "eslint --max-warnings=0 .",
42
48
  "eslint:fix": "pnpm run eslint:check --fix",
43
49
  "lint": "pnpm run eslint:check && pnpm run prettier:check",
44
- "lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix"
50
+ "lint:fix": "pnpm run eslint:fix && pnpm run prettier:fix",
51
+ "test:e2e": "wait-on http://127.0.0.1:9123 -l --timeout 120000 && vitest run e2e",
52
+ "prepare:e2e": "cargo build --bin sui-test-validator --bin sui --profile dev && cross-env RUST_LOG=info,sui=error,anemo_tower=warn,consensus=off cargo run --bin sui-test-validator"
45
53
  }
46
54
  }
package/src/bcs.ts CHANGED
@@ -2,6 +2,7 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import { bcs } from '@mysten/sui.js/bcs';
5
+
5
6
  import {
6
7
  KIOSK_PURCHASE_CAP,
7
8
  KIOSK_TYPE,
@@ -0,0 +1,156 @@
1
+ // Copyright (c) Mysten Labs, Inc.
2
+ // SPDX-License-Identifier: Apache-2.0
3
+
4
+ import { type SuiClient } from '@mysten/sui.js/client';
5
+
6
+ import {
7
+ FLOOR_PRICE_RULE_ADDRESS,
8
+ getBaseRules,
9
+ KIOSK_LOCK_RULE_ADDRESS,
10
+ PERSONAL_KIOSK_RULE_ADDRESS,
11
+ ROYALTY_RULE_ADDRESS,
12
+ rules,
13
+ type BaseRulePackageIds,
14
+ type TransferPolicyRule,
15
+ } from '../constants';
16
+ import { fetchKiosk, getOwnedKiosks } from '../query/kiosk';
17
+ import {
18
+ queryOwnedTransferPolicies,
19
+ queryTransferPolicy,
20
+ queryTransferPolicyCapsByType,
21
+ } from '../query/transfer-policy';
22
+ import {
23
+ Network,
24
+ type FetchKioskOptions,
25
+ type KioskClientOptions,
26
+ type KioskData,
27
+ type OwnedKiosks,
28
+ } from '../types';
29
+
30
+ /**
31
+ * A Client that allows you to interact with kiosk.
32
+ * Offers utilities to query kiosk, craft transactions to edit your own kiosk,
33
+ * purchase, manage transfer policies, create new kiosks etc.
34
+ * If you pass packageIds, all functionality will be managed using these packages.
35
+ */
36
+ export class KioskClient {
37
+ client: SuiClient;
38
+ network: Network;
39
+ rules: TransferPolicyRule[];
40
+ packageIds?: BaseRulePackageIds;
41
+
42
+ constructor(options: KioskClientOptions) {
43
+ this.client = options.client;
44
+ this.network = options.network;
45
+ this.rules = rules; // add all the default rules.
46
+ this.packageIds = options.packageIds;
47
+
48
+ // Add the custom Package Ids too on the rule list.
49
+ // Only adds the rules that are passed in the packageId object.
50
+ if (options.packageIds) this.rules.push(...getBaseRules(options.packageIds));
51
+ }
52
+
53
+ /// Querying
54
+
55
+ /**
56
+ * Get an addresses's owned kiosks.
57
+ * @param address The address for which we want to retrieve the kiosks.
58
+ * @returns An Object containing all the `kioskOwnerCap` objects as well as the kioskIds.
59
+ */
60
+ async getOwnedKiosks({ address }: { address: string }): Promise<OwnedKiosks> {
61
+ const personalPackageId =
62
+ this.packageIds?.personalKioskRulePackageId || PERSONAL_KIOSK_RULE_ADDRESS[this.network];
63
+
64
+ return getOwnedKiosks(this.client, address, {
65
+ personalKioskType: personalPackageId
66
+ ? `${personalPackageId}::personal_kiosk::PersonalKioskCap`
67
+ : '',
68
+ });
69
+ }
70
+
71
+ /**
72
+ * Fetches the kiosk contents.
73
+ * @param kioskId The ID of the kiosk to fetch.
74
+ * @param options Optioal
75
+ * @returns
76
+ */
77
+ async getKiosk({ id, options }: { id: string; options?: FetchKioskOptions }): Promise<KioskData> {
78
+ return (
79
+ await fetchKiosk(
80
+ this.client,
81
+ id,
82
+ {
83
+ limit: 1000,
84
+ },
85
+ options || {},
86
+ )
87
+ ).data;
88
+ }
89
+
90
+ /**
91
+ * Query the Transfer Policy(ies) for type `T`.
92
+ * @param type The Type we're querying for (E.g `0xMyAddress::hero::Hero`)
93
+ */
94
+ async getTransferPolicies({ type }: { type: string }) {
95
+ return queryTransferPolicy(this.client, type);
96
+ }
97
+
98
+ /**
99
+ * Query all the owned transfer policies for an address.
100
+ * Returns `TransferPolicyCap` which uncludes `policyId, policyCapId, type`.
101
+ * @param address The address we're searching the owned transfer policies for.
102
+ */
103
+ async getOwnedTransferPolicies({ address }: { address: string }) {
104
+ return queryOwnedTransferPolicies(this.client, address);
105
+ }
106
+
107
+ /**
108
+ * Query the Transfer Policy Cap for type `T`, owned by `address`
109
+ * @param type The Type `T` for the object
110
+ * @param address The address that owns the cap.
111
+ */
112
+ async getOwnedTransferPoliciesByType({ type, address }: { type: string; address: string }) {
113
+ return queryTransferPolicyCapsByType(this.client, address, type);
114
+ }
115
+
116
+ // Someone would just have to create a `kiosk-client.ts` file in their project, initialize a KioskClient
117
+ // and call the `addRuleResolver` function. Each rule has a `resolve` function.
118
+ // The resolve function is automatically called on `purchaseAndResolve` function call.
119
+ addRuleResolver(rule: TransferPolicyRule) {
120
+ if (this.rules.find((x) => x.rule === rule.rule))
121
+ throw new Error(`Rule ${rule.rule} resolver already exists.`);
122
+ this.rules.push(rule);
123
+ }
124
+
125
+ /**
126
+ * A convenient helper to get the packageIds for our supported ruleset,
127
+ * based on `kioskClient` configuration.
128
+ */
129
+ getRulePackageId(
130
+ rule:
131
+ | 'kioskLockRulePackageId'
132
+ | 'royaltyRulePackageId'
133
+ | 'personalKioskRulePackageId'
134
+ | 'floorPriceRulePackageId',
135
+ ) {
136
+ const rules = this.packageIds || {};
137
+ const network = this.network;
138
+
139
+ /// Check existence of rule based on network and throw an error if it's not found.
140
+ /// We always have a fallback for testnet or mainnet.
141
+ if (!rules[rule] && network !== Network.MAINNET && network !== Network.TESTNET) {
142
+ throw new Error(`Missing packageId for rule ${rule}`);
143
+ }
144
+
145
+ switch (rule) {
146
+ case 'kioskLockRulePackageId':
147
+ return rules[rule] || KIOSK_LOCK_RULE_ADDRESS[network];
148
+ case 'royaltyRulePackageId':
149
+ return rules[rule] || ROYALTY_RULE_ADDRESS[network];
150
+ case 'personalKioskRulePackageId':
151
+ return rules[rule] || PERSONAL_KIOSK_RULE_ADDRESS[network];
152
+ case 'floorPriceRulePackageId':
153
+ return rules[rule] || FLOOR_PRICE_RULE_ADDRESS[network];
154
+ }
155
+ }
156
+ }