@mysten/kiosk 0.0.0-experimental-20230615203750 → 0.0.0-experimental-20230616000145
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 +26 -26
- 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/package.json +9 -5
- 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 +138 -155
package/src/utils.ts
CHANGED
|
@@ -2,37 +2,28 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
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
15
|
import { DynamicFieldInfo } from '@mysten/sui.js/dist/types/dynamic_fields';
|
|
16
16
|
import { bcs } from './bcs';
|
|
17
|
-
import {
|
|
18
|
-
|
|
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
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
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
|
-
|
|
47
|
-
|
|
37
|
+
tx: TransactionBlock,
|
|
38
|
+
arg: string | SharedObjectRef | SuiObjectRef | TransactionArgument,
|
|
48
39
|
): TransactionArgument {
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
40
|
+
if (typeof arg === 'string') {
|
|
41
|
+
return tx.object(arg);
|
|
42
|
+
}
|
|
52
43
|
|
|
53
|
-
|
|
54
|
-
|
|
55
|
-
|
|
44
|
+
if ('digest' in arg && 'version' in arg && 'objectId' in arg) {
|
|
45
|
+
return tx.objectRef(arg);
|
|
46
|
+
}
|
|
56
47
|
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
48
|
+
if ('objectId' in arg && 'initialSharedVersion' in arg && 'mutable' in arg) {
|
|
49
|
+
return tx.sharedObjectRef(arg);
|
|
50
|
+
}
|
|
60
51
|
|
|
61
|
-
|
|
62
|
-
|
|
63
|
-
|
|
52
|
+
if ('kind' in arg) {
|
|
53
|
+
return arg;
|
|
54
|
+
}
|
|
64
55
|
|
|
65
|
-
|
|
56
|
+
throw new Error('Invalid argument type');
|
|
66
57
|
}
|
|
67
58
|
|
|
68
|
-
export async function getKioskObject(
|
|
69
|
-
|
|
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
|
-
|
|
75
|
-
|
|
76
|
-
|
|
62
|
+
if (!queryRes || queryRes.error || !queryRes.data) {
|
|
63
|
+
throw new Error(`Kiosk ${id} not found; ${queryRes.error}`);
|
|
64
|
+
}
|
|
77
65
|
|
|
78
|
-
|
|
79
|
-
|
|
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
|
-
|
|
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
|
-
|
|
88
|
-
|
|
89
|
-
|
|
75
|
+
data: DynamicFieldInfo[],
|
|
76
|
+
listings: KioskListing[],
|
|
77
|
+
lockedItemIds: string[],
|
|
90
78
|
): KioskData {
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
|
|
110
|
-
|
|
111
|
-
|
|
112
|
-
|
|
113
|
-
|
|
114
|
-
|
|
115
|
-
|
|
116
|
-
|
|
117
|
-
|
|
118
|
-
|
|
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
|
-
|
|
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
|
-
|
|
132
|
-
|
|
133
|
-
|
|
119
|
+
kioskData: KioskData,
|
|
120
|
+
listings: KioskListing[],
|
|
121
|
+
listingObjects: SuiObjectResponse[],
|
|
134
122
|
) {
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
146
|
-
|
|
147
|
-
|
|
148
|
-
|
|
149
|
-
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
154
|
-
|
|
155
|
-
|
|
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
|
-
|
|
164
|
-
|
|
165
|
-
) {
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
|
|
172
|
-
|
|
173
|
-
|
|
174
|
-
|
|
175
|
-
|
|
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
|
-
|
|
186
|
-
)
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
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
|
-
|
|
203
|
-
|
|
204
|
-
|
|
185
|
+
provider: JsonRpcProvider,
|
|
186
|
+
parentId: ObjectId,
|
|
187
|
+
pagination: PaginationArguments<string>,
|
|
205
188
|
) {
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
209
|
-
|
|
210
|
-
|
|
211
|
-
|
|
212
|
-
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
|
|
220
|
-
|
|
221
|
-
|
|
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
|
}
|