@mysten/kiosk 0.3.1 → 0.3.3

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