@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/CHANGELOG.md +20 -0
- package/README.md +34 -34
- package/dist/index.js +14 -65
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +14 -65
- package/dist/index.mjs.map +1 -1
- package/dist/utils.d.ts +1 -1
- package/package.json +9 -13
- package/src/bcs.ts +17 -17
- package/src/constants.ts +2 -2
- package/src/query/kiosk.ts +92 -98
- package/src/query/transfer-policy.ts +31 -37
- package/src/tx/kiosk.ts +232 -289
- package/src/tx/transfer-policy.ts +90 -90
- package/src/types/env.ts +2 -2
- package/src/types/index.ts +2 -10
- package/src/types/kiosk.ts +54 -54
- package/src/types/transfer-policy.ts +6 -6
- package/src/utils.ts +139 -156
package/src/query/kiosk.ts
CHANGED
|
@@ -2,76 +2,74 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
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
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
15
|
+
attachListingsAndPrices,
|
|
16
|
+
attachLockedItems,
|
|
17
|
+
extractKioskData,
|
|
18
|
+
getAllDynamicFields,
|
|
19
|
+
getKioskObject,
|
|
20
20
|
} from '../utils';
|
|
21
21
|
import {
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
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
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
30
|
+
provider: JsonRpcProvider,
|
|
31
|
+
kioskId: SuiAddress,
|
|
32
|
+
pagination: PaginationArguments<string>,
|
|
33
|
+
options: FetchKioskOptions,
|
|
34
34
|
): Promise<PagedKioskData> {
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
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
|
-
|
|
42
|
-
|
|
41
|
+
const listings: KioskListing[] = [];
|
|
42
|
+
const lockedItemIds: ObjectId[] = [];
|
|
43
43
|
|
|
44
|
-
|
|
45
|
-
|
|
44
|
+
// extracted kiosk data.
|
|
45
|
+
const kioskData = extractKioskData(data, listings, lockedItemIds);
|
|
46
46
|
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
58
|
-
|
|
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
|
-
|
|
65
|
-
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
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
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
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
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
82
|
+
provider: JsonRpcProvider,
|
|
83
|
+
address: SuiAddress,
|
|
84
|
+
options?: {
|
|
85
|
+
pagination?: PaginationArguments<string>;
|
|
86
|
+
},
|
|
89
87
|
): Promise<OwnedKiosks> {
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
88
|
+
if (!isValidSuiAddress(address))
|
|
89
|
+
return {
|
|
90
|
+
nextCursor: null,
|
|
91
|
+
hasNextPage: false,
|
|
92
|
+
kioskOwnerCaps: [],
|
|
93
|
+
kioskIds: [],
|
|
94
|
+
};
|
|
97
95
|
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
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
|
-
|
|
109
|
-
|
|
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
|
-
|
|
114
|
-
|
|
115
|
-
|
|
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
|
-
|
|
120
|
-
|
|
121
|
-
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
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
|
-
|
|
23
|
-
|
|
18
|
+
provider: JsonRpcProvider,
|
|
19
|
+
type: string,
|
|
24
20
|
): Promise<TransferPolicy[]> {
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
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
|
-
|
|
43
|
+
let parsed = bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, 'base64');
|
|
50
44
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
56
|
-
|
|
57
|
-
|
|
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
|
}
|