@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.
@@ -2,76 +2,74 @@
2
2
  // SPDX-License-Identifier: Apache-2.0
3
3
 
4
4
  import {
5
- JsonRpcProvider,
6
- ObjectId,
7
- PaginationArguments,
8
- SuiAddress,
9
- SuiObjectData,
10
- SuiObjectResponse,
11
- getObjectFields,
12
- isValidSuiAddress,
5
+ JsonRpcProvider,
6
+ ObjectId,
7
+ PaginationArguments,
8
+ SuiAddress,
9
+ SuiObjectData,
10
+ SuiObjectResponse,
11
+ getObjectFields,
12
+ isValidSuiAddress,
13
13
  } from '@mysten/sui.js';
14
14
  import {
15
- attachListingsAndPrices,
16
- attachLockedItems,
17
- extractKioskData,
18
- getAllDynamicFields,
19
- getKioskObject,
15
+ attachListingsAndPrices,
16
+ attachLockedItems,
17
+ extractKioskData,
18
+ getAllDynamicFields,
19
+ getKioskObject,
20
20
  } from '../utils';
21
21
  import {
22
- FetchKioskOptions,
23
- KIOSK_OWNER_CAP,
24
- KioskListing,
25
- OwnedKiosks,
26
- PagedKioskData,
22
+ FetchKioskOptions,
23
+ KIOSK_OWNER_CAP,
24
+ KioskListing,
25
+ OwnedKiosks,
26
+ PagedKioskData,
27
27
  } from '../types';
28
28
 
29
29
  export async function fetchKiosk(
30
- provider: JsonRpcProvider,
31
- kioskId: SuiAddress,
32
- pagination: PaginationArguments<string>,
33
- options: FetchKioskOptions,
30
+ provider: JsonRpcProvider,
31
+ kioskId: SuiAddress,
32
+ pagination: PaginationArguments<string>,
33
+ options: FetchKioskOptions,
34
34
  ): Promise<PagedKioskData> {
35
- // TODO: Replace the `getAllDynamicFields` with a paginated
36
- // response, once we have better RPC support for
37
- // type filtering & batch fetching.
38
- // This can't work with pagination currently.
39
- const data = await getAllDynamicFields(provider, kioskId, pagination);
35
+ // TODO: Replace the `getAllDynamicFields` with a paginated
36
+ // response, once we have better RPC support for
37
+ // type filtering & batch fetching.
38
+ // This can't work with pagination currently.
39
+ const data = await getAllDynamicFields(provider, kioskId, pagination);
40
40
 
41
- const listings: KioskListing[] = [];
42
- const lockedItemIds: ObjectId[] = [];
41
+ const listings: KioskListing[] = [];
42
+ const lockedItemIds: ObjectId[] = [];
43
43
 
44
- // extracted kiosk data.
45
- const kioskData = extractKioskData(data, listings, lockedItemIds);
44
+ // extracted kiosk data.
45
+ const kioskData = extractKioskData(data, listings, lockedItemIds);
46
46
 
47
- // split the fetching in two queries as we are most likely passing different options for each kind.
48
- // For items, we usually seek the Display.
49
- // For listings we usually seek the DF value (price) / exclusivity.
50
- const [kiosk, listingObjects] = await Promise.all([
51
- options.withKioskFields
52
- ? getKioskObject(provider, kioskId)
53
- : Promise.resolve(undefined),
54
- options.withListingPrices
55
- ? provider.multiGetObjects({
56
- ids: kioskData.listingIds,
57
- options: {
58
- showContent: true,
59
- },
60
- })
61
- : Promise.resolve([]),
62
- ]);
47
+ // split the fetching in two queries as we are most likely passing different options for each kind.
48
+ // For items, we usually seek the Display.
49
+ // For listings we usually seek the DF value (price) / exclusivity.
50
+ const [kiosk, listingObjects] = await Promise.all([
51
+ options.withKioskFields ? getKioskObject(provider, kioskId) : Promise.resolve(undefined),
52
+ options.withListingPrices
53
+ ? provider.multiGetObjects({
54
+ ids: kioskData.listingIds,
55
+ options: {
56
+ showContent: true,
57
+ },
58
+ })
59
+ : Promise.resolve([]),
60
+ ]);
63
61
 
64
- if (options.withKioskFields) kioskData.kiosk = kiosk;
65
- // attach items listings. IF we have `options.withListingPrices === true`, it will also attach the prices.
66
- attachListingsAndPrices(kioskData, listings, listingObjects);
67
- // add `locked` status to items that are locked.
68
- attachLockedItems(kioskData, lockedItemIds);
62
+ if (options.withKioskFields) kioskData.kiosk = kiosk;
63
+ // attach items listings. IF we have `options.withListingPrices === true`, it will also attach the prices.
64
+ attachListingsAndPrices(kioskData, listings, listingObjects);
65
+ // add `locked` status to items that are locked.
66
+ attachLockedItems(kioskData, lockedItemIds);
69
67
 
70
- return {
71
- data: kioskData,
72
- nextCursor: null,
73
- hasNextPage: false,
74
- };
68
+ return {
69
+ data: kioskData,
70
+ nextCursor: null,
71
+ hasNextPage: false,
72
+ };
75
73
  }
76
74
 
77
75
  /**
@@ -81,50 +79,46 @@ export async function fetchKiosk(
81
79
  * Extra options allow pagination.
82
80
  */
83
81
  export async function getOwnedKiosks(
84
- provider: JsonRpcProvider,
85
- address: SuiAddress,
86
- options?: {
87
- pagination?: PaginationArguments<string>;
88
- },
82
+ provider: JsonRpcProvider,
83
+ address: SuiAddress,
84
+ options?: {
85
+ pagination?: PaginationArguments<string>;
86
+ },
89
87
  ): Promise<OwnedKiosks> {
90
- if (!isValidSuiAddress(address))
91
- return {
92
- nextCursor: null,
93
- hasNextPage: false,
94
- kioskOwnerCaps: [],
95
- kioskIds: [],
96
- };
88
+ if (!isValidSuiAddress(address))
89
+ return {
90
+ nextCursor: null,
91
+ hasNextPage: false,
92
+ kioskOwnerCaps: [],
93
+ kioskIds: [],
94
+ };
97
95
 
98
- // fetch owned kiosk caps, paginated.
99
- const { data, hasNextPage, nextCursor } = await provider.getOwnedObjects({
100
- owner: address,
101
- filter: { StructType: KIOSK_OWNER_CAP },
102
- options: {
103
- showContent: true,
104
- },
105
- ...(options?.pagination || {}),
106
- });
96
+ // fetch owned kiosk caps, paginated.
97
+ const { data, hasNextPage, nextCursor } = await provider.getOwnedObjects({
98
+ owner: address,
99
+ filter: { StructType: KIOSK_OWNER_CAP },
100
+ options: {
101
+ showContent: true,
102
+ },
103
+ ...(options?.pagination || {}),
104
+ });
107
105
 
108
- // get kioskIds from the OwnerCaps.
109
- const kioskIdList = data?.map(
110
- (x: SuiObjectResponse) => getObjectFields(x)?.for,
111
- );
106
+ // get kioskIds from the OwnerCaps.
107
+ const kioskIdList = data?.map((x: SuiObjectResponse) => getObjectFields(x)?.for);
112
108
 
113
- // clean up data that might have an error in them.
114
- // only return valid objects.
115
- const filteredData = data
116
- .filter((x) => 'data' in x)
117
- .map((x) => x.data) as SuiObjectData[];
109
+ // clean up data that might have an error in them.
110
+ // only return valid objects.
111
+ const filteredData = data.filter((x) => 'data' in x).map((x) => x.data) as SuiObjectData[];
118
112
 
119
- return {
120
- nextCursor,
121
- hasNextPage,
122
- kioskOwnerCaps: filteredData.map((x, idx) => ({
123
- digest: x.digest,
124
- version: x.version,
125
- objectId: x.objectId,
126
- kioskId: kioskIdList[idx],
127
- })),
128
- kioskIds: kioskIdList,
129
- };
113
+ return {
114
+ nextCursor,
115
+ hasNextPage,
116
+ kioskOwnerCaps: filteredData.map((x, idx) => ({
117
+ digest: x.digest,
118
+ version: x.version,
119
+ objectId: x.objectId,
120
+ kioskId: kioskIdList[idx],
121
+ })),
122
+ kioskIds: kioskIdList,
123
+ };
130
124
  }
@@ -3,11 +3,7 @@
3
3
 
4
4
  import { JsonRpcProvider } from '@mysten/sui.js';
5
5
  import { bcs } from '../bcs';
6
- import {
7
- TRANSFER_POLICY_CREATED_EVENT,
8
- TRANSFER_POLICY_TYPE,
9
- TransferPolicy,
10
- } from '../types';
6
+ import { TRANSFER_POLICY_CREATED_EVENT, TRANSFER_POLICY_TYPE, TransferPolicy } from '../types';
11
7
 
12
8
  /**
13
9
  * Searches the `TransferPolicy`-s for the given type. The seach is performed via
@@ -19,41 +15,39 @@ import {
19
15
  * @param type
20
16
  */
21
17
  export async function queryTransferPolicy(
22
- provider: JsonRpcProvider,
23
- type: string,
18
+ provider: JsonRpcProvider,
19
+ type: string,
24
20
  ): Promise<TransferPolicy[]> {
25
- // console.log('event type: %s', `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`);
26
- const { data } = await provider.queryEvents({
27
- query: {
28
- MoveEventType: `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`,
29
- },
30
- });
21
+ // console.log('event type: %s', `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`);
22
+ const { data } = await provider.queryEvents({
23
+ query: {
24
+ MoveEventType: `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`,
25
+ },
26
+ });
31
27
 
32
- const search = data.map((event) => event.parsedJson as { id: string });
33
- const policies = await provider.multiGetObjects({
34
- ids: search.map((policy) => policy.id),
35
- options: { showBcs: true, showOwner: true },
36
- });
28
+ const search = data.map((event) => event.parsedJson as { id: string });
29
+ const policies = await provider.multiGetObjects({
30
+ ids: search.map((policy) => policy.id),
31
+ options: { showBcs: true, showOwner: true },
32
+ });
37
33
 
38
- return policies
39
- .filter((policy) => !!policy && 'data' in policy)
40
- .map(({ data: policy }) => {
41
- // should never happen; policies are objects and fetched via an event.
42
- // policies are filtered for null and undefined above.
43
- if (!policy || !policy.bcs || !('bcsBytes' in policy.bcs)) {
44
- throw new Error(
45
- `Invalid policy: ${policy?.objectId}, expected object, got package`,
46
- );
47
- }
34
+ return policies
35
+ .filter((policy) => !!policy && 'data' in policy)
36
+ .map(({ data: policy }) => {
37
+ // should never happen; policies are objects and fetched via an event.
38
+ // policies are filtered for null and undefined above.
39
+ if (!policy || !policy.bcs || !('bcsBytes' in policy.bcs)) {
40
+ throw new Error(`Invalid policy: ${policy?.objectId}, expected object, got package`);
41
+ }
48
42
 
49
- let parsed = bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, 'base64');
43
+ let parsed = bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, 'base64');
50
44
 
51
- return {
52
- id: policy?.objectId,
53
- type: `${TRANSFER_POLICY_TYPE}<${type}>`,
54
- owner: policy?.owner!,
55
- rules: parsed.rules,
56
- balance: parsed.balance,
57
- } as TransferPolicy;
58
- });
45
+ return {
46
+ id: policy?.objectId,
47
+ type: `${TRANSFER_POLICY_TYPE}<${type}>`,
48
+ owner: policy?.owner!,
49
+ rules: parsed.rules,
50
+ balance: parsed.balance,
51
+ } as TransferPolicy;
52
+ });
59
53
  }