@mysten/kiosk 0.2.0 → 0.3.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.
package/src/utils.ts CHANGED
@@ -4,6 +4,7 @@
4
4
  import {
5
5
  JsonRpcProvider,
6
6
  ObjectId,
7
+ PaginationArguments,
7
8
  SharedObjectRef,
8
9
  SuiObjectRef,
9
10
  SuiObjectResponse,
@@ -11,18 +12,28 @@ import {
11
12
  TransactionBlock,
12
13
  getObjectFields,
13
14
  } from '@mysten/sui.js';
14
- import { KioskData, KioskListing } from './query/kiosk';
15
15
  import { DynamicFieldInfo } from '@mysten/sui.js/dist/types/dynamic_fields';
16
- import { bcs, Kiosk } from './bcs';
17
-
18
- /**
19
- * A valid argument for any of the Kiosk functions.
20
- */
21
- export type ObjectArgument =
22
- | string
23
- | TransactionArgument
24
- | SharedObjectRef
25
- | SuiObjectRef;
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';
28
+
29
+ /* A simple map to the rule package addresses */
30
+ // TODO: Supply the mainnet and devnet addresses.
31
+ export const rulesPackageAddresses = {
32
+ mainnet: MAINNET_RULES_PACKAGE_ADDRESS,
33
+ testnet: TESTNET_RULES_PACKAGE_ADDRESS,
34
+ devnet: '',
35
+ custom: null,
36
+ };
26
37
 
27
38
  /**
28
39
  * Convert any valid input into a TransactionArgument.
@@ -68,7 +79,7 @@ export async function getKioskObject(
68
79
  throw new Error(`Invalid kiosk query: ${id}, expected object, got package`);
69
80
  }
70
81
 
71
- return bcs.de('0x2::kiosk::Kiosk', queryRes.data.bcs!.bcsBytes, 'base64');
82
+ return bcs.de(KIOSK_TYPE, queryRes.data.bcs!.bcsBytes, 'base64');
72
83
  }
73
84
 
74
85
  // helper to extract kiosk data from dynamic fields.
@@ -109,18 +120,18 @@ export function extractKioskData(
109
120
  }
110
121
 
111
122
  // e.g. 0x2::kiosk::Item -> kiosk::Item
112
- export const getTypeWithoutPackageAddress = (type: string) => {
123
+ export function getTypeWithoutPackageAddress(type: string) {
113
124
  return type.split('::').slice(-2).join('::');
114
- };
125
+ }
115
126
 
116
127
  /**
117
128
  * A helper that attaches the listing prices to kiosk listings.
118
129
  */
119
- export const attachListingsAndPrices = (
130
+ export function attachListingsAndPrices(
120
131
  kioskData: KioskData,
121
132
  listings: KioskListing[],
122
133
  listingObjects: SuiObjectResponse[],
123
- ) => {
134
+ ) {
124
135
  // map item listings as {item_id: KioskListing}
125
136
  // for easier mapping on the nex
126
137
  const itemListings = listings.reduce<Record<ObjectId, KioskListing>>(
@@ -143,15 +154,15 @@ export const attachListingsAndPrices = (
143
154
  kioskData.items.map((item) => {
144
155
  item.listing = itemListings[item.objectId] || undefined;
145
156
  });
146
- };
157
+ }
147
158
 
148
159
  /**
149
160
  * A Helper to attach locked state to items in Kiosk Data.
150
161
  */
151
- export const attachLockedItems = (
162
+ export function attachLockedItems(
152
163
  kioskData: KioskData,
153
164
  lockedItemIds: ObjectId[],
154
- ) => {
165
+ ) {
155
166
  // map lock status in an array of type { item_id: true }
156
167
  const lockedStatuses = lockedItemIds.reduce<Record<ObjectId, boolean>>(
157
168
  (acc: Record<ObjectId, boolean>, item: string) => {
@@ -165,4 +176,47 @@ export const attachLockedItems = (
165
176
  kioskData.items.map((item) => {
166
177
  item.isLocked = lockedStatuses[item.objectId] || false;
167
178
  });
168
- };
179
+ }
180
+
181
+ /**
182
+ * A helper to get a rule's environment address.
183
+ */
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];
194
+ }
195
+
196
+ /**
197
+ * A helper to fetch all DF pages.
198
+ * We need that to fetch the kiosk DFs consistently, until we have
199
+ * RPC calls that allow filtering of Type / batch fetching of spec
200
+ */
201
+ export async function getAllDynamicFields(
202
+ provider: JsonRpcProvider,
203
+ parentId: ObjectId,
204
+ pagination: PaginationArguments<string>,
205
+ ) {
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;
222
+ }