@mysten/kiosk 0.3.0 → 0.3.2

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.
package/src/utils.ts CHANGED
@@ -2,31 +2,28 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import {
5
- JsonRpcProvider,
6
- ObjectId,
7
- SharedObjectRef,
8
- SuiObjectRef,
9
- SuiObjectResponse,
10
- TransactionArgument,
11
- TransactionBlock,
12
- getObjectFields,
5
+ JsonRpcProvider,
6
+ ObjectId,
7
+ PaginationArguments,
8
+ SharedObjectRef,
9
+ SuiObjectRef,
10
+ SuiObjectResponse,
11
+ TransactionArgument,
12
+ TransactionBlock,
13
+ getObjectFields,
13
14
  } from '@mysten/sui.js';
14
- import { KioskData, KioskListing } from './query/kiosk';
15
- import { DynamicFieldInfo } from '@mysten/sui.js/dist/types/dynamic_fields';
15
+ import { type DynamicFieldInfo } from '@mysten/sui.js';
16
16
  import { bcs } from './bcs';
17
- import { KIOSK_TYPE, Kiosk, RulesEnvironmentParam } from './types';
18
- import {
19
- MAINNET_RULES_PACKAGE_ADDRESS,
20
- TESTNET_RULES_PACKAGE_ADDRESS,
21
- } from './constants';
17
+ import { KIOSK_TYPE, Kiosk, KioskData, KioskListing, RulesEnvironmentParam } from './types';
18
+ import { MAINNET_RULES_PACKAGE_ADDRESS, TESTNET_RULES_PACKAGE_ADDRESS } from './constants';
22
19
 
23
20
  /* A simple map to the rule package addresses */
24
21
  // TODO: Supply the mainnet and devnet addresses.
25
22
  export const rulesPackageAddresses = {
26
- mainnet: MAINNET_RULES_PACKAGE_ADDRESS,
27
- testnet: TESTNET_RULES_PACKAGE_ADDRESS,
28
- devnet: '',
29
- custom: null,
23
+ mainnet: MAINNET_RULES_PACKAGE_ADDRESS,
24
+ testnet: TESTNET_RULES_PACKAGE_ADDRESS,
25
+ devnet: '',
26
+ custom: null,
30
27
  };
31
28
 
32
29
  /**
@@ -37,152 +34,172 @@ export const rulesPackageAddresses = {
37
34
  * @returns The converted TransactionArgument.
38
35
  */
39
36
  export function objArg(
40
- tx: TransactionBlock,
41
- arg: string | SharedObjectRef | SuiObjectRef | TransactionArgument,
37
+ tx: TransactionBlock,
38
+ arg: string | SharedObjectRef | SuiObjectRef | TransactionArgument,
42
39
  ): TransactionArgument {
43
- if (typeof arg === 'string') {
44
- return tx.object(arg);
45
- }
40
+ if (typeof arg === 'string') {
41
+ return tx.object(arg);
42
+ }
46
43
 
47
- if ('digest' in arg && 'version' in arg && 'objectId' in arg) {
48
- return tx.objectRef(arg);
49
- }
44
+ if ('digest' in arg && 'version' in arg && 'objectId' in arg) {
45
+ return tx.objectRef(arg);
46
+ }
50
47
 
51
- if ('objectId' in arg && 'initialSharedVersion' in arg && 'mutable' in arg) {
52
- return tx.sharedObjectRef(arg);
53
- }
48
+ if ('objectId' in arg && 'initialSharedVersion' in arg && 'mutable' in arg) {
49
+ return tx.sharedObjectRef(arg);
50
+ }
54
51
 
55
- if ('kind' in arg) {
56
- return arg;
57
- }
52
+ if ('kind' in arg) {
53
+ return arg;
54
+ }
58
55
 
59
- throw new Error('Invalid argument type');
56
+ throw new Error('Invalid argument type');
60
57
  }
61
58
 
62
- export async function getKioskObject(
63
- provider: JsonRpcProvider,
64
- id: string,
65
- ): Promise<Kiosk> {
66
- const queryRes = await provider.getObject({ id, options: { showBcs: true } });
59
+ export async function getKioskObject(provider: JsonRpcProvider, id: string): Promise<Kiosk> {
60
+ const queryRes = await provider.getObject({ id, options: { showBcs: true } });
67
61
 
68
- if (!queryRes || queryRes.error || !queryRes.data) {
69
- throw new Error(`Kiosk ${id} not found; ${queryRes.error}`);
70
- }
62
+ if (!queryRes || queryRes.error || !queryRes.data) {
63
+ throw new Error(`Kiosk ${id} not found; ${queryRes.error}`);
64
+ }
71
65
 
72
- if (!queryRes.data.bcs || !('bcsBytes' in queryRes.data.bcs)) {
73
- throw new Error(`Invalid kiosk query: ${id}, expected object, got package`);
74
- }
66
+ if (!queryRes.data.bcs || !('bcsBytes' in queryRes.data.bcs)) {
67
+ throw new Error(`Invalid kiosk query: ${id}, expected object, got package`);
68
+ }
75
69
 
76
- return bcs.de(KIOSK_TYPE, queryRes.data.bcs!.bcsBytes, 'base64');
70
+ return bcs.de(KIOSK_TYPE, queryRes.data.bcs!.bcsBytes, 'base64');
77
71
  }
78
72
 
79
73
  // helper to extract kiosk data from dynamic fields.
80
74
  export function extractKioskData(
81
- data: DynamicFieldInfo[],
82
- listings: KioskListing[],
83
- lockedItemIds: string[],
75
+ data: DynamicFieldInfo[],
76
+ listings: KioskListing[],
77
+ lockedItemIds: string[],
84
78
  ): KioskData {
85
- return data.reduce<KioskData>(
86
- (acc: KioskData, val: DynamicFieldInfo) => {
87
- const type = getTypeWithoutPackageAddress(val.name.type);
88
-
89
- switch (type) {
90
- case 'kiosk::Item':
91
- acc.itemIds.push(val.objectId);
92
- acc.items.push({
93
- objectId: val.objectId,
94
- type: val.objectType,
95
- isLocked: false,
96
- });
97
- break;
98
- case 'kiosk::Listing':
99
- acc.listingIds.push(val.objectId);
100
- listings.push({
101
- objectId: val.name.value.id,
102
- listingId: val.objectId,
103
- isExclusive: val.name.value.is_exclusive,
104
- });
105
- break;
106
- case 'kiosk::Lock':
107
- lockedItemIds?.push(val.name.value.id);
108
- break;
109
- }
110
- return acc;
111
- },
112
- { items: [], itemIds: [], listingIds: [], extensions: [] },
113
- );
79
+ return data.reduce<KioskData>(
80
+ (acc: KioskData, val: DynamicFieldInfo) => {
81
+ const type = getTypeWithoutPackageAddress(val.name.type);
82
+
83
+ switch (type) {
84
+ case 'kiosk::Item':
85
+ acc.itemIds.push(val.objectId);
86
+ acc.items.push({
87
+ objectId: val.objectId,
88
+ type: val.objectType,
89
+ isLocked: false,
90
+ });
91
+ break;
92
+ case 'kiosk::Listing':
93
+ acc.listingIds.push(val.objectId);
94
+ listings.push({
95
+ objectId: val.name.value.id,
96
+ listingId: val.objectId,
97
+ isExclusive: val.name.value.is_exclusive,
98
+ });
99
+ break;
100
+ case 'kiosk::Lock':
101
+ lockedItemIds?.push(val.name.value.id);
102
+ break;
103
+ }
104
+ return acc;
105
+ },
106
+ { items: [], itemIds: [], listingIds: [], extensions: [] },
107
+ );
114
108
  }
115
109
 
116
110
  // e.g. 0x2::kiosk::Item -> kiosk::Item
117
111
  export function getTypeWithoutPackageAddress(type: string) {
118
- return type.split('::').slice(-2).join('::');
112
+ return type.split('::').slice(-2).join('::');
119
113
  }
120
114
 
121
115
  /**
122
116
  * A helper that attaches the listing prices to kiosk listings.
123
117
  */
124
118
  export function attachListingsAndPrices(
125
- kioskData: KioskData,
126
- listings: KioskListing[],
127
- listingObjects: SuiObjectResponse[],
119
+ kioskData: KioskData,
120
+ listings: KioskListing[],
121
+ listingObjects: SuiObjectResponse[],
128
122
  ) {
129
- // map item listings as {item_id: KioskListing}
130
- // for easier mapping on the nex
131
- const itemListings = listings.reduce<Record<ObjectId, KioskListing>>(
132
- (acc: Record<ObjectId, KioskListing>, item, idx) => {
133
- acc[item.objectId] = { ...item };
134
-
135
- // return in case we don't have any listing objects.
136
- // that's the case when we don't have the `listingPrices` included.
137
- if (listingObjects.length === 0) return acc;
138
-
139
- const data = getObjectFields(listingObjects[idx]);
140
- if (!data) return acc;
141
-
142
- acc[item.objectId].price = data.value;
143
- return acc;
144
- },
145
- {},
146
- );
147
-
148
- kioskData.items.map((item) => {
149
- item.listing = itemListings[item.objectId] || undefined;
150
- });
123
+ // map item listings as {item_id: KioskListing}
124
+ // for easier mapping on the nex
125
+ const itemListings = listings.reduce<Record<ObjectId, KioskListing>>(
126
+ (acc: Record<ObjectId, KioskListing>, item, idx) => {
127
+ acc[item.objectId] = { ...item };
128
+
129
+ // return in case we don't have any listing objects.
130
+ // that's the case when we don't have the `listingPrices` included.
131
+ if (listingObjects.length === 0) return acc;
132
+
133
+ const data = getObjectFields(listingObjects[idx]);
134
+ if (!data) return acc;
135
+
136
+ acc[item.objectId].price = data.value;
137
+ return acc;
138
+ },
139
+ {},
140
+ );
141
+
142
+ kioskData.items.forEach((item) => {
143
+ item.listing = itemListings[item.objectId] || undefined;
144
+ });
151
145
  }
152
146
 
153
147
  /**
154
148
  * A Helper to attach locked state to items in Kiosk Data.
155
149
  */
156
- export function attachLockedItems(
157
- kioskData: KioskData,
158
- lockedItemIds: ObjectId[],
159
- ) {
160
- // map lock status in an array of type { item_id: true }
161
- const lockedStatuses = lockedItemIds.reduce<Record<ObjectId, boolean>>(
162
- (acc: Record<ObjectId, boolean>, item: string) => {
163
- acc[item] = true;
164
- return acc;
165
- },
166
- {},
167
- );
168
-
169
- // parse lockedItemIds and attach their locked status.
170
- kioskData.items.map((item) => {
171
- item.isLocked = lockedStatuses[item.objectId] || false;
172
- });
150
+ export function attachLockedItems(kioskData: KioskData, lockedItemIds: ObjectId[]) {
151
+ // map lock status in an array of type { item_id: true }
152
+ const lockedStatuses = lockedItemIds.reduce<Record<ObjectId, boolean>>(
153
+ (acc: Record<ObjectId, boolean>, item: string) => {
154
+ acc[item] = true;
155
+ return acc;
156
+ },
157
+ {},
158
+ );
159
+
160
+ // parse lockedItemIds and attach their locked status.
161
+ kioskData.items.forEach((item) => {
162
+ item.isLocked = lockedStatuses[item.objectId] || false;
163
+ });
173
164
  }
174
165
 
175
166
  /**
176
167
  * A helper to get a rule's environment address.
177
168
  */
178
- export function getRulePackageAddress(
179
- environment: RulesEnvironmentParam,
180
- ): string {
181
- // if we have custom environment, we return it.
182
- if (environment.env === 'custom') {
183
- if (!environment.address)
184
- throw new Error('Please supply the custom package address for rules.');
185
- return environment.address;
186
- }
187
- return rulesPackageAddresses[environment.env];
169
+ export function getRulePackageAddress(environment: RulesEnvironmentParam): string {
170
+ // if we have custom environment, we return it.
171
+ if (environment.env === 'custom') {
172
+ if (!environment.address)
173
+ throw new Error('Please supply the custom package address for rules.');
174
+ return environment.address;
175
+ }
176
+ return rulesPackageAddresses[environment.env];
177
+ }
178
+
179
+ /**
180
+ * A helper to fetch all DF pages.
181
+ * We need that to fetch the kiosk DFs consistently, until we have
182
+ * RPC calls that allow filtering of Type / batch fetching of spec
183
+ */
184
+ export async function getAllDynamicFields(
185
+ provider: JsonRpcProvider,
186
+ parentId: ObjectId,
187
+ pagination: PaginationArguments<string>,
188
+ ) {
189
+ let hasNextPage = true;
190
+ let cursor = undefined;
191
+ const data: DynamicFieldInfo[] = [];
192
+
193
+ while (hasNextPage) {
194
+ const result = await provider.getDynamicFields({
195
+ parentId,
196
+ limit: pagination.limit || undefined,
197
+ cursor,
198
+ });
199
+ data.push(...result.data);
200
+ hasNextPage = result.hasNextPage;
201
+ cursor = result.nextCursor;
202
+ }
203
+
204
+ return data;
188
205
  }