@mysten/kiosk 0.0.0-experimental-20230525143947
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 +18 -0
- package/LICENSE +201 -0
- package/README.md +119 -0
- package/package.json +50 -0
- package/src/bcs.ts +69 -0
- package/src/index.ts +9 -0
- package/src/query/kiosk.ts +126 -0
- package/src/query/transfer-policy.ts +61 -0
- package/src/tx/kiosk.ts +389 -0
- package/src/tx/transfer-policy.ts +127 -0
- package/src/utils.ts +117 -0
package/CHANGELOG.md
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
# @mysten/kiosk
|
|
2
|
+
|
|
3
|
+
## 0.0.0-experimental-20230525143947
|
|
4
|
+
|
|
5
|
+
### Major Changes
|
|
6
|
+
|
|
7
|
+
- 4ea96d909a: Kiosk SDK for managing, querying and interacting with Kiosk and TransferPolicy objects
|
|
8
|
+
|
|
9
|
+
### Patch Changes
|
|
10
|
+
|
|
11
|
+
- Updated dependencies [4ea96d909a]
|
|
12
|
+
- Updated dependencies [470c27af50]
|
|
13
|
+
- Updated dependencies [03828224c9]
|
|
14
|
+
- Updated dependencies [671faefe3c]
|
|
15
|
+
- Updated dependencies [9ce7e051b4]
|
|
16
|
+
- Updated dependencies [9ce7e051b4]
|
|
17
|
+
- Updated dependencies [bb50698551]
|
|
18
|
+
- @mysten/sui.js@0.0.0-experimental-20230525143947
|
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,119 @@
|
|
|
1
|
+
# Kiosk SDK
|
|
2
|
+
|
|
3
|
+
> **This package is still in active development. Use at your own risk**.
|
|
4
|
+
> Currently, the only supported environment is **Sui Testnet**.
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
This Kiosk SDK library provides different utilities to interact/create/manage a [Kiosk](https://github.com/MystenLabs/sui/tree/main/kiosk).
|
|
8
|
+
|
|
9
|
+
## Installation
|
|
10
|
+
|
|
11
|
+
To install, add `@mysten/kiosk` package to your project
|
|
12
|
+
|
|
13
|
+
```
|
|
14
|
+
npm i @mysten/kiosk
|
|
15
|
+
```
|
|
16
|
+
|
|
17
|
+
You can also use your preferred package manager, such as yarn or pnpm.
|
|
18
|
+
|
|
19
|
+
## Examples
|
|
20
|
+
|
|
21
|
+
Here are some indicative examples on how to use the kiosk SDK.
|
|
22
|
+
|
|
23
|
+
<details>
|
|
24
|
+
<summary>Getting the listings & items by the kiosk's id</summary>
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { fetchKiosk } from '@mysten/kiosk';
|
|
28
|
+
import { Connection, JsonRpcProvider } from '@mysten/sui.js';
|
|
29
|
+
|
|
30
|
+
const provider = new JsonRpcProvider(
|
|
31
|
+
new Connection({ fullnode: 'https://fullnode.testnet.sui.io:443' }),
|
|
32
|
+
);
|
|
33
|
+
|
|
34
|
+
const getKiosk = async () => {
|
|
35
|
+
const kioskAddress = `0xSomeKioskAddress`;
|
|
36
|
+
|
|
37
|
+
const {
|
|
38
|
+
data: res,
|
|
39
|
+
nextCursor,
|
|
40
|
+
hasNextPage,
|
|
41
|
+
} = await fetchKiosk(provider, kioskAddress, { limit: 100 }); // could also add `cursor` for pagination
|
|
42
|
+
|
|
43
|
+
console.log(res); // { listings: [], items: [], itemIds: [], listingIds: [] }
|
|
44
|
+
console.log(nextCursor); // null
|
|
45
|
+
console.log(hasNextPage); // false
|
|
46
|
+
};
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
</details>
|
|
50
|
+
|
|
51
|
+
<details>
|
|
52
|
+
<summary>Create a kiosk, share it and get transfer the `kioskOwnerCap` to the wallet's address</summary>
|
|
53
|
+
|
|
54
|
+
```typescript
|
|
55
|
+
import { createKioskAndShare } from '@mysten/kiosk';
|
|
56
|
+
import { TransactionBlock } from '@mysten/sui.js';
|
|
57
|
+
|
|
58
|
+
const createKiosk = async () => {
|
|
59
|
+
const accountAddress = '0xSomeSuiAddress';
|
|
60
|
+
|
|
61
|
+
const tx = new TransactionBlock();
|
|
62
|
+
const kiosk_cap = createKioskAndShare(tx);
|
|
63
|
+
|
|
64
|
+
tx.transferObjects([kiosk_cap], tx.pure(accountAddress, 'address'));
|
|
65
|
+
|
|
66
|
+
// ... continue to sign and execute the transaction
|
|
67
|
+
// ...
|
|
68
|
+
};
|
|
69
|
+
```
|
|
70
|
+
|
|
71
|
+
</details>
|
|
72
|
+
|
|
73
|
+
<details>
|
|
74
|
+
<summary>Place an item and list it for sale in the kiosk</summary>
|
|
75
|
+
|
|
76
|
+
```typescript
|
|
77
|
+
import { placeAndList } from '@mysten/kiosk';
|
|
78
|
+
import { TransactionBlock } from '@mysten/sui.js';
|
|
79
|
+
|
|
80
|
+
const placeAndListToKiosk = async () => {
|
|
81
|
+
const kiosk = 'SomeKioskId';
|
|
82
|
+
const kioskCap = 'KioskCapObjectId';
|
|
83
|
+
const itemType = '0xItemAddr::some:ItemType';
|
|
84
|
+
const item = 'SomeItemId';
|
|
85
|
+
const price = '100000';
|
|
86
|
+
|
|
87
|
+
const tx = new TransactionBlock();
|
|
88
|
+
|
|
89
|
+
placeAndList(tx, itemType, kiosk, kioskCap, item, price);
|
|
90
|
+
|
|
91
|
+
// ... continue to sign and execute the transaction
|
|
92
|
+
// ...
|
|
93
|
+
};
|
|
94
|
+
```
|
|
95
|
+
|
|
96
|
+
</details>
|
|
97
|
+
|
|
98
|
+
<details>
|
|
99
|
+
<summary>Withdraw profits from your kiosk</summary>
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { withdrawFromKiosk } from '@mysten/kiosk';
|
|
103
|
+
import { TransactionBlock } from '@mysten/sui.js';
|
|
104
|
+
|
|
105
|
+
const withdraw = async () => {
|
|
106
|
+
const kiosk = 'SomeKioskId';
|
|
107
|
+
const kioskCap = 'KioskCapObjectId';
|
|
108
|
+
const amount = '100000';
|
|
109
|
+
|
|
110
|
+
const tx = new TransactionBlock();
|
|
111
|
+
|
|
112
|
+
withdrawFromKiosk(tx, kiosk, kioskCap, amount);
|
|
113
|
+
|
|
114
|
+
// ... continue to sign and execute the transaction
|
|
115
|
+
// ...
|
|
116
|
+
};
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
</details>
|
package/package.json
ADDED
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@mysten/kiosk",
|
|
3
|
+
"author": "Mysten Labs <build@mystenlabs.com>",
|
|
4
|
+
"description": "Sui Kiosk library",
|
|
5
|
+
"version": "0.0.0-experimental-20230525143947",
|
|
6
|
+
"license": "Apache-2.0",
|
|
7
|
+
"main": "./dist/index.js",
|
|
8
|
+
"module": "./dist/index.mjs",
|
|
9
|
+
"types": "./dist/index.d.ts",
|
|
10
|
+
"files": [
|
|
11
|
+
"dist",
|
|
12
|
+
"src",
|
|
13
|
+
"CHANGELOG.md"
|
|
14
|
+
],
|
|
15
|
+
"engines": {
|
|
16
|
+
"node": ">=16"
|
|
17
|
+
},
|
|
18
|
+
"exports": {
|
|
19
|
+
".": {
|
|
20
|
+
"types": "./dist/index.d.ts",
|
|
21
|
+
"source": "./src/index.ts",
|
|
22
|
+
"import": "./dist/index.mjs",
|
|
23
|
+
"require": "./dist/index.js"
|
|
24
|
+
}
|
|
25
|
+
},
|
|
26
|
+
"dependencies": {
|
|
27
|
+
"@mysten/sui.js": "0.0.0-experimental-20230525143947"
|
|
28
|
+
},
|
|
29
|
+
"devDependencies": {
|
|
30
|
+
"eslint": "^8.38.0",
|
|
31
|
+
"prettier": "^2.8.7",
|
|
32
|
+
"tsup": "^6.7.0",
|
|
33
|
+
"typescript": "^5.0.4"
|
|
34
|
+
},
|
|
35
|
+
"prettier": {
|
|
36
|
+
"printWidth": 80,
|
|
37
|
+
"semi": true,
|
|
38
|
+
"singleQuote": true,
|
|
39
|
+
"trailingComma": "all"
|
|
40
|
+
},
|
|
41
|
+
"scripts": {
|
|
42
|
+
"build": "pnpm build:types && pnpm build:tsup",
|
|
43
|
+
"build:tsup": "tsup ./src/index.ts --format esm,cjs --sourcemap",
|
|
44
|
+
"build:types": "tsc --build",
|
|
45
|
+
"test": "echo \"Error: no test specified\"",
|
|
46
|
+
"lint": "eslint --max-warnings=0 ./src/**/**",
|
|
47
|
+
"prettier:fix": "prettier --write .",
|
|
48
|
+
"pre-commit": "pnpm prettier:fix && pnpm lint && pnpm build"
|
|
49
|
+
}
|
|
50
|
+
}
|
package/src/bcs.ts
ADDED
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { ObjectOwner, bcs } from '@mysten/sui.js';
|
|
5
|
+
|
|
6
|
+
/**
|
|
7
|
+
* The Kiosk object fields (for BCS queries).
|
|
8
|
+
*/
|
|
9
|
+
export type Kiosk = {
|
|
10
|
+
id: string;
|
|
11
|
+
profits: string;
|
|
12
|
+
owner: string;
|
|
13
|
+
itemCount: number;
|
|
14
|
+
allowExtensions: boolean;
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
// Register the `Kiosk` struct for faster queries.
|
|
18
|
+
bcs.registerStructType('0x2::kiosk::Kiosk', {
|
|
19
|
+
id: 'address',
|
|
20
|
+
profits: 'u64',
|
|
21
|
+
owner: 'address',
|
|
22
|
+
itemCount: 'u32',
|
|
23
|
+
allowExtensions: 'bool',
|
|
24
|
+
});
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* PurchaseCap object fields (for BCS queries).
|
|
28
|
+
*/
|
|
29
|
+
export type PurchaseCap = {
|
|
30
|
+
id: string;
|
|
31
|
+
kioskId: string;
|
|
32
|
+
itemId: string;
|
|
33
|
+
minPrice: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
36
|
+
// Register the `PurchaseCap` for faster queries.
|
|
37
|
+
bcs.registerStructType('0x2::kiosk::PurchaseCap', {
|
|
38
|
+
id: 'address',
|
|
39
|
+
kioskId: 'address',
|
|
40
|
+
itemId: 'address',
|
|
41
|
+
minPrice: 'u64',
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
/** Event emitted when a TransferPolicy is created. */
|
|
45
|
+
export type TransferPolicyCreated = {
|
|
46
|
+
id: string;
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
// Register the `TransferPolicyCreated` event data.
|
|
50
|
+
bcs.registerStructType('0x2::transfer_policy::TransferPolicyCreated', {
|
|
51
|
+
id: 'address',
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
/** The `TransferPolicy` object */
|
|
55
|
+
export type TransferPolicy = {
|
|
56
|
+
id: string;
|
|
57
|
+
type: string;
|
|
58
|
+
balance: string;
|
|
59
|
+
rules: string[];
|
|
60
|
+
owner: ObjectOwner;
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
bcs.registerStructType('0x2::transfer_policy::TransferPolicy', {
|
|
64
|
+
id: 'address',
|
|
65
|
+
balance: 'u64',
|
|
66
|
+
rules: ['vector', 'string'],
|
|
67
|
+
});
|
|
68
|
+
|
|
69
|
+
export { bcs };
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
export * from './tx/kiosk';
|
|
5
|
+
export * from './tx/transfer-policy';
|
|
6
|
+
export * from './query/kiosk';
|
|
7
|
+
export * from './bcs';
|
|
8
|
+
export * from './utils';
|
|
9
|
+
export * from './query/transfer-policy';
|
|
@@ -0,0 +1,126 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
JsonRpcProvider,
|
|
6
|
+
PaginationArguments,
|
|
7
|
+
SuiAddress,
|
|
8
|
+
SuiObjectDataOptions,
|
|
9
|
+
SuiObjectResponse,
|
|
10
|
+
getObjectFields,
|
|
11
|
+
} from '@mysten/sui.js';
|
|
12
|
+
import { extractKioskData, getKioskObject, getObjects } from '../utils';
|
|
13
|
+
import { Kiosk } from '../bcs';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* A dynamic field `Listing { ID, isExclusive }` attached to the Kiosk.
|
|
17
|
+
* Holds a `u64` value - the price of the item.
|
|
18
|
+
*/
|
|
19
|
+
export type KioskListing = {
|
|
20
|
+
/** The ID of the Item */
|
|
21
|
+
itemId: string;
|
|
22
|
+
/**
|
|
23
|
+
* Whether or not there's a `PurchaseCap` issued. `true` means that
|
|
24
|
+
* the listing is controlled by some logic and can't be purchased directly.
|
|
25
|
+
*
|
|
26
|
+
* TODO: consider renaming the field for better indication.
|
|
27
|
+
*/
|
|
28
|
+
isExclusive: boolean;
|
|
29
|
+
/** The ID of the listing */
|
|
30
|
+
listingId: string;
|
|
31
|
+
price?: string;
|
|
32
|
+
};
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* A dynamic field `Item { ID }` attached to the Kiosk.
|
|
36
|
+
* Holds an Item `T`. The type of the item is known upfront.
|
|
37
|
+
*/
|
|
38
|
+
export type KioskItem = {
|
|
39
|
+
/** The ID of the Item */
|
|
40
|
+
itemId: string;
|
|
41
|
+
/** The type of the Item */
|
|
42
|
+
itemType: string;
|
|
43
|
+
};
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Aggregated data from the Kiosk.
|
|
47
|
+
*/
|
|
48
|
+
export type KioskData = {
|
|
49
|
+
items: KioskItem[] | SuiObjectResponse[];
|
|
50
|
+
listings: KioskListing[];
|
|
51
|
+
itemIds: string[];
|
|
52
|
+
listingIds: string[];
|
|
53
|
+
kiosk?: Kiosk;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
export type PagedKioskData = {
|
|
57
|
+
data: KioskData;
|
|
58
|
+
nextCursor: string | null;
|
|
59
|
+
hasNextPage: boolean;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type FetchKioskOptions = {
|
|
63
|
+
includeKioskFields?: boolean;
|
|
64
|
+
includeItems?: boolean;
|
|
65
|
+
itemOptions?: SuiObjectDataOptions;
|
|
66
|
+
withListingPrices?: boolean;
|
|
67
|
+
listingOptions?: SuiObjectDataOptions;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
/**
|
|
71
|
+
*
|
|
72
|
+
*/
|
|
73
|
+
export async function fetchKiosk(
|
|
74
|
+
provider: JsonRpcProvider,
|
|
75
|
+
kioskId: SuiAddress,
|
|
76
|
+
pagination: PaginationArguments<string>,
|
|
77
|
+
{
|
|
78
|
+
includeKioskFields = false,
|
|
79
|
+
includeItems = false,
|
|
80
|
+
withListingPrices = false,
|
|
81
|
+
itemOptions = { showDisplay: true, showType: true },
|
|
82
|
+
}: FetchKioskOptions,
|
|
83
|
+
): Promise<PagedKioskData> {
|
|
84
|
+
const { data, nextCursor, hasNextPage } = await provider.getDynamicFields({
|
|
85
|
+
parentId: kioskId,
|
|
86
|
+
...pagination,
|
|
87
|
+
});
|
|
88
|
+
|
|
89
|
+
// extracted kiosk data.
|
|
90
|
+
const kioskData = extractKioskData(data);
|
|
91
|
+
|
|
92
|
+
// split the fetching in two queries as we are most likely passing different options for each kind.
|
|
93
|
+
// For items, we usually seek the Display.
|
|
94
|
+
// For listings we usually seek the DF value (price) / exclusivity.
|
|
95
|
+
const [kiosk, itemObjects, listingObjects] = await Promise.all([
|
|
96
|
+
includeKioskFields
|
|
97
|
+
? getKioskObject(provider, kioskId)
|
|
98
|
+
: Promise.resolve(undefined),
|
|
99
|
+
includeItems
|
|
100
|
+
? getObjects(provider, kioskData.itemIds, itemOptions)
|
|
101
|
+
: Promise.resolve([]),
|
|
102
|
+
withListingPrices
|
|
103
|
+
? getObjects(provider, kioskData.listingIds, {
|
|
104
|
+
showBcs: true,
|
|
105
|
+
showContent: true,
|
|
106
|
+
})
|
|
107
|
+
: Promise.resolve([]),
|
|
108
|
+
]);
|
|
109
|
+
|
|
110
|
+
if (includeKioskFields) kioskData.kiosk = kiosk;
|
|
111
|
+
if (includeItems) kioskData.items = itemObjects;
|
|
112
|
+
if (withListingPrices)
|
|
113
|
+
kioskData.listings.map((l, i) => {
|
|
114
|
+
const fields = getObjectFields(listingObjects[i]);
|
|
115
|
+
// l.price = bcs.de('u64', listingObjects[i].data?.bcs.bcsBytes, 'base64');
|
|
116
|
+
// TODO: Figure out a way to do this with BCS to avoid querying content.
|
|
117
|
+
l.price = fields?.value;
|
|
118
|
+
return l;
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
return {
|
|
122
|
+
data: kioskData,
|
|
123
|
+
nextCursor,
|
|
124
|
+
hasNextPage,
|
|
125
|
+
};
|
|
126
|
+
}
|
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { JsonRpcProvider } from '@mysten/sui.js';
|
|
5
|
+
import { TransferPolicy, bcs } from '../bcs';
|
|
6
|
+
|
|
7
|
+
/** Name of the event emitted when a TransferPolicy for T is created. */
|
|
8
|
+
export const TRANSFER_POLICY_CREATED_EVENT = `0x2::transfer_policy::TransferPolicyCreated`;
|
|
9
|
+
|
|
10
|
+
/**
|
|
11
|
+
* Searches the `TransferPolicy`-s for the given type. The seach is performed via
|
|
12
|
+
* the `TransferPolicyCreated` event. The policy can either be owned or shared,
|
|
13
|
+
* and the caller needs to filter the results accordingly (ie single owner can not
|
|
14
|
+
* be accessed by anyone but the owner).
|
|
15
|
+
*
|
|
16
|
+
* @param provider
|
|
17
|
+
* @param type
|
|
18
|
+
*/
|
|
19
|
+
export async function queryTransferPolicy(
|
|
20
|
+
provider: JsonRpcProvider,
|
|
21
|
+
type: string,
|
|
22
|
+
): Promise<TransferPolicy[]> {
|
|
23
|
+
// console.log('event type: %s', `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`);
|
|
24
|
+
const { data } = await provider.queryEvents({
|
|
25
|
+
query: {
|
|
26
|
+
MoveEventType: `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`,
|
|
27
|
+
},
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const search = data.map((event) => event.parsedJson as { id: string });
|
|
31
|
+
const policies = await provider.multiGetObjects({
|
|
32
|
+
ids: search.map((policy) => policy.id),
|
|
33
|
+
options: { showBcs: true, showOwner: true },
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return policies
|
|
37
|
+
.filter((policy) => !!policy && 'data' in policy)
|
|
38
|
+
.map(({ data: policy }) => {
|
|
39
|
+
// should never happen; policies are objects and fetched via an event.
|
|
40
|
+
// policies are filtered for null and undefined above.
|
|
41
|
+
if (!policy || !policy.bcs || !('bcsBytes' in policy.bcs)) {
|
|
42
|
+
throw new Error(
|
|
43
|
+
`Invalid policy: ${policy?.objectId}, expected object, got package`,
|
|
44
|
+
);
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
let parsed = bcs.de(
|
|
48
|
+
'0x2::transfer_policy::TransferPolicy',
|
|
49
|
+
policy.bcs.bcsBytes,
|
|
50
|
+
'base64',
|
|
51
|
+
);
|
|
52
|
+
|
|
53
|
+
return {
|
|
54
|
+
id: policy?.objectId,
|
|
55
|
+
type: `0x2::transfer_policy::TransferPolicy<${type}>`,
|
|
56
|
+
owner: policy?.owner!,
|
|
57
|
+
rules: parsed.rules,
|
|
58
|
+
balance: parsed.balance,
|
|
59
|
+
} as TransferPolicy;
|
|
60
|
+
});
|
|
61
|
+
}
|
package/src/tx/kiosk.ts
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
SuiAddress,
|
|
6
|
+
TransactionArgument,
|
|
7
|
+
TransactionBlock,
|
|
8
|
+
} from '@mysten/sui.js';
|
|
9
|
+
|
|
10
|
+
import { ObjectArgument, getTypeWithoutPackageAddress, objArg } from '../utils';
|
|
11
|
+
import { KioskListing } from '../query/kiosk';
|
|
12
|
+
import { TransferPolicy } from '../bcs';
|
|
13
|
+
import { confirmRequest, resolveRoyaltyRule } from './transfer-policy';
|
|
14
|
+
|
|
15
|
+
/** The Kiosk module. */
|
|
16
|
+
export const KIOSK_MODULE = '0x2::kiosk';
|
|
17
|
+
|
|
18
|
+
/** The Kiosk type. */
|
|
19
|
+
export const KIOSK_TYPE = `${KIOSK_MODULE}::Kiosk`;
|
|
20
|
+
|
|
21
|
+
/** The Kiosk Owner Cap Type */
|
|
22
|
+
export const KIOSK_OWNER_CAP = `${KIOSK_MODULE}::KioskOwnerCap`;
|
|
23
|
+
|
|
24
|
+
/**
|
|
25
|
+
* Create a new shared Kiosk and returns the [kiosk, kioskOwnerCap] tuple.
|
|
26
|
+
*/
|
|
27
|
+
export function createKiosk(
|
|
28
|
+
tx: TransactionBlock,
|
|
29
|
+
): [TransactionArgument, TransactionArgument] {
|
|
30
|
+
let [kiosk, kioskOwnerCap] = tx.moveCall({
|
|
31
|
+
target: `${KIOSK_MODULE}::new`,
|
|
32
|
+
});
|
|
33
|
+
|
|
34
|
+
return [kiosk, kioskOwnerCap];
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Calls the `kiosk::new()` function and shares the kiosk.
|
|
39
|
+
* Returns the `kioskOwnerCap` object.
|
|
40
|
+
*/
|
|
41
|
+
export function createKioskAndShare(tx: TransactionBlock): TransactionArgument {
|
|
42
|
+
let [kiosk, kioskOwnerCap] = tx.moveCall({
|
|
43
|
+
target: `${KIOSK_MODULE}::new`,
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
tx.moveCall({
|
|
47
|
+
target: `0x2::transfer::public_share_object`,
|
|
48
|
+
typeArguments: [KIOSK_TYPE],
|
|
49
|
+
arguments: [kiosk],
|
|
50
|
+
});
|
|
51
|
+
|
|
52
|
+
return kioskOwnerCap;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/**
|
|
56
|
+
* Call the `kiosk::place<T>(Kiosk, KioskOwnerCap, Item)` function.
|
|
57
|
+
* Place an item to the Kiosk.
|
|
58
|
+
*/
|
|
59
|
+
export function place(
|
|
60
|
+
tx: TransactionBlock,
|
|
61
|
+
itemType: string,
|
|
62
|
+
kiosk: ObjectArgument,
|
|
63
|
+
kioskCap: ObjectArgument,
|
|
64
|
+
item: ObjectArgument,
|
|
65
|
+
): void {
|
|
66
|
+
tx.moveCall({
|
|
67
|
+
target: `${KIOSK_MODULE}::place`,
|
|
68
|
+
typeArguments: [itemType],
|
|
69
|
+
arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), objArg(tx, item)],
|
|
70
|
+
});
|
|
71
|
+
}
|
|
72
|
+
|
|
73
|
+
/**
|
|
74
|
+
* Call the `kiosk::lock<T>(Kiosk, KioskOwnerCap, TransferPolicy, Item)`
|
|
75
|
+
* function. Lock an item in the Kiosk.
|
|
76
|
+
*
|
|
77
|
+
* Unlike `place` this function requires a `TransferPolicy` to exist
|
|
78
|
+
* and be passed in. This is done to make sure the item does not get
|
|
79
|
+
* locked without an option to take it out.
|
|
80
|
+
*/
|
|
81
|
+
export function lock(
|
|
82
|
+
tx: TransactionBlock,
|
|
83
|
+
itemType: string,
|
|
84
|
+
kiosk: ObjectArgument,
|
|
85
|
+
kioskCap: ObjectArgument,
|
|
86
|
+
policy: ObjectArgument,
|
|
87
|
+
item: ObjectArgument,
|
|
88
|
+
): void {
|
|
89
|
+
tx.moveCall({
|
|
90
|
+
target: `${KIOSK_MODULE}::lock`,
|
|
91
|
+
typeArguments: [itemType],
|
|
92
|
+
arguments: [
|
|
93
|
+
objArg(tx, kiosk),
|
|
94
|
+
objArg(tx, kioskCap),
|
|
95
|
+
objArg(tx, policy),
|
|
96
|
+
objArg(tx, item),
|
|
97
|
+
],
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Call the `kiosk::take<T>(Kiosk, KioskOwnerCap, ID)` function.
|
|
103
|
+
* Take an item from the Kiosk.
|
|
104
|
+
*/
|
|
105
|
+
export function take(
|
|
106
|
+
tx: TransactionBlock,
|
|
107
|
+
itemType: string,
|
|
108
|
+
kiosk: ObjectArgument,
|
|
109
|
+
kioskCap: ObjectArgument,
|
|
110
|
+
itemId: SuiAddress,
|
|
111
|
+
): TransactionArgument {
|
|
112
|
+
let [item] = tx.moveCall({
|
|
113
|
+
target: `${KIOSK_MODULE}::take`,
|
|
114
|
+
typeArguments: [itemType],
|
|
115
|
+
arguments: [
|
|
116
|
+
objArg(tx, kiosk),
|
|
117
|
+
objArg(tx, kioskCap),
|
|
118
|
+
tx.pure(itemId, 'address'),
|
|
119
|
+
],
|
|
120
|
+
});
|
|
121
|
+
|
|
122
|
+
return item;
|
|
123
|
+
}
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* Call the `kiosk::list<T>(Kiosk, KioskOwnerCap, ID, u64)` function.
|
|
127
|
+
* List an item for sale.
|
|
128
|
+
*/
|
|
129
|
+
export function list(
|
|
130
|
+
tx: TransactionBlock,
|
|
131
|
+
itemType: string,
|
|
132
|
+
kiosk: ObjectArgument,
|
|
133
|
+
kioskCap: ObjectArgument,
|
|
134
|
+
itemId: SuiAddress,
|
|
135
|
+
price: string | bigint,
|
|
136
|
+
): void {
|
|
137
|
+
tx.moveCall({
|
|
138
|
+
target: `${KIOSK_MODULE}::list`,
|
|
139
|
+
typeArguments: [itemType],
|
|
140
|
+
arguments: [
|
|
141
|
+
objArg(tx, kiosk),
|
|
142
|
+
objArg(tx, kioskCap),
|
|
143
|
+
tx.pure(itemId, 'address'),
|
|
144
|
+
tx.pure(price, 'u64'),
|
|
145
|
+
],
|
|
146
|
+
});
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
/**
|
|
150
|
+
* Call the `kiosk::list<T>(Kiosk, KioskOwnerCap, ID, u64)` function.
|
|
151
|
+
* List an item for sale.
|
|
152
|
+
*/
|
|
153
|
+
export function delist(
|
|
154
|
+
tx: TransactionBlock,
|
|
155
|
+
itemType: string,
|
|
156
|
+
kiosk: ObjectArgument,
|
|
157
|
+
kioskCap: ObjectArgument,
|
|
158
|
+
itemId: SuiAddress,
|
|
159
|
+
): void {
|
|
160
|
+
tx.moveCall({
|
|
161
|
+
target: `${KIOSK_MODULE}::delist`,
|
|
162
|
+
typeArguments: [itemType],
|
|
163
|
+
arguments: [
|
|
164
|
+
objArg(tx, kiosk),
|
|
165
|
+
objArg(tx, kioskCap),
|
|
166
|
+
tx.pure(itemId, 'address'),
|
|
167
|
+
],
|
|
168
|
+
});
|
|
169
|
+
}
|
|
170
|
+
|
|
171
|
+
/**
|
|
172
|
+
* Call the `kiosk::place_and_list<T>(Kiosk, KioskOwnerCap, Item, u64)` function.
|
|
173
|
+
* Place an item to the Kiosk and list it for sale.
|
|
174
|
+
*/
|
|
175
|
+
export function placeAndList(
|
|
176
|
+
tx: TransactionBlock,
|
|
177
|
+
itemType: string,
|
|
178
|
+
kiosk: ObjectArgument,
|
|
179
|
+
kioskCap: ObjectArgument,
|
|
180
|
+
item: ObjectArgument,
|
|
181
|
+
price: string | bigint,
|
|
182
|
+
): void {
|
|
183
|
+
tx.moveCall({
|
|
184
|
+
target: `${KIOSK_MODULE}::place_and_list`,
|
|
185
|
+
typeArguments: [itemType],
|
|
186
|
+
arguments: [
|
|
187
|
+
objArg(tx, kiosk),
|
|
188
|
+
objArg(tx, kioskCap),
|
|
189
|
+
objArg(tx, item),
|
|
190
|
+
tx.pure(price, 'u64'),
|
|
191
|
+
],
|
|
192
|
+
});
|
|
193
|
+
}
|
|
194
|
+
|
|
195
|
+
/**
|
|
196
|
+
* Call the `kiosk::purchase<T>(Kiosk, ID, Coin<SUI>)` function and receive an Item and
|
|
197
|
+
* a TransferRequest which needs to be dealt with (via a matching TransferPolicy).
|
|
198
|
+
*/
|
|
199
|
+
export function purchase(
|
|
200
|
+
tx: TransactionBlock,
|
|
201
|
+
itemType: string,
|
|
202
|
+
kiosk: ObjectArgument,
|
|
203
|
+
itemId: SuiAddress,
|
|
204
|
+
payment: ObjectArgument,
|
|
205
|
+
): [TransactionArgument, TransactionArgument] {
|
|
206
|
+
let [item, transferRequest] = tx.moveCall({
|
|
207
|
+
target: `${KIOSK_MODULE}::purchase`,
|
|
208
|
+
typeArguments: [itemType],
|
|
209
|
+
arguments: [
|
|
210
|
+
objArg(tx, kiosk),
|
|
211
|
+
tx.pure(itemId, 'address'),
|
|
212
|
+
objArg(tx, payment),
|
|
213
|
+
],
|
|
214
|
+
});
|
|
215
|
+
|
|
216
|
+
return [item, transferRequest];
|
|
217
|
+
}
|
|
218
|
+
|
|
219
|
+
/**
|
|
220
|
+
* Call the `kiosk::withdraw(Kiosk, KioskOwnerCap, Option<u64>)` function and receive a Coin<SUI>.
|
|
221
|
+
* If the amount is null, then the entire balance will be withdrawn.
|
|
222
|
+
*/
|
|
223
|
+
export function withdrawFromKiosk(
|
|
224
|
+
tx: TransactionBlock,
|
|
225
|
+
kiosk: ObjectArgument,
|
|
226
|
+
kioskCap: ObjectArgument,
|
|
227
|
+
amount: string | bigint | null,
|
|
228
|
+
): TransactionArgument {
|
|
229
|
+
let amountArg =
|
|
230
|
+
amount !== null
|
|
231
|
+
? tx.pure(amount, 'Option<u64>')
|
|
232
|
+
: tx.pure({ None: true }, 'Option<u64>');
|
|
233
|
+
|
|
234
|
+
let [coin] = tx.moveCall({
|
|
235
|
+
target: `${KIOSK_MODULE}::withdraw`,
|
|
236
|
+
arguments: [objArg(tx, kiosk), objArg(tx, kioskCap), amountArg],
|
|
237
|
+
});
|
|
238
|
+
|
|
239
|
+
return coin;
|
|
240
|
+
}
|
|
241
|
+
|
|
242
|
+
/**
|
|
243
|
+
* Call the `kiosk::borrow<T>(Kiosk, KioskOwnerCap, ID): &T` function.
|
|
244
|
+
* Immutably borrow an item from the Kiosk.
|
|
245
|
+
*/
|
|
246
|
+
export function borrow(
|
|
247
|
+
tx: TransactionBlock,
|
|
248
|
+
itemType: string,
|
|
249
|
+
kiosk: ObjectArgument,
|
|
250
|
+
kioskCap: ObjectArgument,
|
|
251
|
+
itemId: SuiAddress,
|
|
252
|
+
): TransactionArgument {
|
|
253
|
+
let [item] = tx.moveCall({
|
|
254
|
+
target: `${KIOSK_MODULE}::borrow`,
|
|
255
|
+
typeArguments: [itemType],
|
|
256
|
+
arguments: [
|
|
257
|
+
objArg(tx, kiosk),
|
|
258
|
+
objArg(tx, kioskCap),
|
|
259
|
+
tx.pure(itemId, 'address'),
|
|
260
|
+
],
|
|
261
|
+
});
|
|
262
|
+
|
|
263
|
+
return item;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
/**
|
|
267
|
+
* Call the `kiosk::borrow_mut<T>(Kiosk, KioskOwnerCap, ID): &mut T` function.
|
|
268
|
+
* Mutably borrow an item from the Kiosk.
|
|
269
|
+
*/
|
|
270
|
+
export function borrowMut(
|
|
271
|
+
tx: TransactionBlock,
|
|
272
|
+
itemType: string,
|
|
273
|
+
kiosk: ObjectArgument,
|
|
274
|
+
kioskCap: ObjectArgument,
|
|
275
|
+
itemId: SuiAddress,
|
|
276
|
+
): TransactionArgument {
|
|
277
|
+
let [item] = tx.moveCall({
|
|
278
|
+
target: `${KIOSK_MODULE}::borrow_mut`,
|
|
279
|
+
typeArguments: [itemType],
|
|
280
|
+
arguments: [
|
|
281
|
+
objArg(tx, kiosk),
|
|
282
|
+
objArg(tx, kioskCap),
|
|
283
|
+
tx.pure(itemId, 'address'),
|
|
284
|
+
],
|
|
285
|
+
});
|
|
286
|
+
|
|
287
|
+
return item;
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
/**
|
|
291
|
+
* Call the `kiosk::borrow_value<T>(Kiosk, KioskOwnerCap, ID): T` function.
|
|
292
|
+
* Immutably borrow an item from the Kiosk and return it in the end.
|
|
293
|
+
*
|
|
294
|
+
* Requires calling `returnValue` to return the item.
|
|
295
|
+
*/
|
|
296
|
+
export function borrowValue(
|
|
297
|
+
tx: TransactionBlock,
|
|
298
|
+
itemType: string,
|
|
299
|
+
kiosk: ObjectArgument,
|
|
300
|
+
kioskCap: ObjectArgument,
|
|
301
|
+
itemId: SuiAddress,
|
|
302
|
+
): [TransactionArgument, TransactionArgument] {
|
|
303
|
+
let [item, promise] = tx.moveCall({
|
|
304
|
+
target: `${KIOSK_MODULE}::borrow_val`,
|
|
305
|
+
typeArguments: [itemType],
|
|
306
|
+
arguments: [
|
|
307
|
+
objArg(tx, kiosk),
|
|
308
|
+
objArg(tx, kioskCap),
|
|
309
|
+
tx.pure(itemId, 'address'),
|
|
310
|
+
],
|
|
311
|
+
});
|
|
312
|
+
|
|
313
|
+
return [item, promise];
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
/**
|
|
317
|
+
* Call the `kiosk::return_value<T>(Kiosk, Item, Borrow)` function.
|
|
318
|
+
* Return an item to the Kiosk after it was `borrowValue`-d.
|
|
319
|
+
*/
|
|
320
|
+
export function returnValue(
|
|
321
|
+
tx: TransactionBlock,
|
|
322
|
+
itemType: string,
|
|
323
|
+
kiosk: ObjectArgument,
|
|
324
|
+
item: TransactionArgument,
|
|
325
|
+
promise: TransactionArgument,
|
|
326
|
+
): void {
|
|
327
|
+
tx.moveCall({
|
|
328
|
+
target: `${KIOSK_MODULE}::return_val`,
|
|
329
|
+
typeArguments: [itemType],
|
|
330
|
+
arguments: [objArg(tx, kiosk), item, promise],
|
|
331
|
+
});
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* Completes the full purchase flow that includes:
|
|
336
|
+
* 1. Purchasing the item.
|
|
337
|
+
* 2. Resolving all the transfer policies (if any).
|
|
338
|
+
* 3. Returns the PurchasedItem OR places the item in the user's kiosk (if there's a kiosk lock policy).
|
|
339
|
+
*/
|
|
340
|
+
export function purchaseAndResolvePolicies(
|
|
341
|
+
tx: TransactionBlock,
|
|
342
|
+
itemType: string,
|
|
343
|
+
listing: KioskListing,
|
|
344
|
+
kioskId: string,
|
|
345
|
+
itemId: string,
|
|
346
|
+
policy: TransferPolicy,
|
|
347
|
+
): TransactionArgument | null {
|
|
348
|
+
// if we don't pass the listing or the listing doens't have a price, return.
|
|
349
|
+
if (!listing || listing?.price === undefined) return null;
|
|
350
|
+
|
|
351
|
+
// Split the coin for the amount of the listing.
|
|
352
|
+
const coin = tx.splitCoins(tx.gas, [tx.pure(listing.price)]);
|
|
353
|
+
|
|
354
|
+
// initialize the purchase `kiosk::purchase`
|
|
355
|
+
const [purchasedItem, transferRequest] = purchase(
|
|
356
|
+
tx,
|
|
357
|
+
itemType,
|
|
358
|
+
kioskId,
|
|
359
|
+
itemId,
|
|
360
|
+
coin,
|
|
361
|
+
);
|
|
362
|
+
|
|
363
|
+
// Start resolving rules.
|
|
364
|
+
// For now, we only support royalty rule.
|
|
365
|
+
// Will need some tweaking to make it function properly with the other
|
|
366
|
+
// ruleset.
|
|
367
|
+
for (let rule of policy.rules) {
|
|
368
|
+
const ruleWithoutAddr = getTypeWithoutPackageAddress(rule);
|
|
369
|
+
|
|
370
|
+
switch (ruleWithoutAddr) {
|
|
371
|
+
case 'royalty_rule::Rule':
|
|
372
|
+
resolveRoyaltyRule(
|
|
373
|
+
tx,
|
|
374
|
+
itemType,
|
|
375
|
+
listing.price,
|
|
376
|
+
policy.id,
|
|
377
|
+
transferRequest,
|
|
378
|
+
);
|
|
379
|
+
break;
|
|
380
|
+
default:
|
|
381
|
+
break;
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
// confirm the Transfer Policy request.
|
|
386
|
+
confirmRequest(tx, itemType, policy.id, transferRequest);
|
|
387
|
+
|
|
388
|
+
return purchasedItem;
|
|
389
|
+
}
|
|
@@ -0,0 +1,127 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import { TransactionArgument, TransactionBlock } from '@mysten/sui.js';
|
|
5
|
+
import { ObjectArgument, objArg } from '../utils';
|
|
6
|
+
|
|
7
|
+
/** The Transfer Policy module. */
|
|
8
|
+
export const TRANSFER_POLICY_MODULE = '0x2::transfer_policy';
|
|
9
|
+
|
|
10
|
+
/** The Transer Policy Rules package address */
|
|
11
|
+
// TODO: Figure out how we serve this for both testnet & mainnet (different package)
|
|
12
|
+
export const TRANSFER_POLICY_RULES_PACKAGE_ADDRESS =
|
|
13
|
+
'bd8fc1947cf119350184107a3087e2dc27efefa0dd82e25a1f699069fe81a585';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Call the `transfer_policy::new` function to create a new transfer policy.
|
|
17
|
+
* Returns `transferPolicyCap`
|
|
18
|
+
*/
|
|
19
|
+
export function createTransferPolicy(
|
|
20
|
+
tx: TransactionBlock,
|
|
21
|
+
itemType: string,
|
|
22
|
+
publisher: ObjectArgument,
|
|
23
|
+
): TransactionArgument {
|
|
24
|
+
let [transferPolicy, transferPolicyCap] = tx.moveCall({
|
|
25
|
+
target: `${TRANSFER_POLICY_MODULE}::new`,
|
|
26
|
+
typeArguments: [itemType],
|
|
27
|
+
arguments: [objArg(tx, publisher)],
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
tx.moveCall({
|
|
31
|
+
target: `0x2::transfer::public_share_object`,
|
|
32
|
+
typeArguments: [itemType],
|
|
33
|
+
arguments: [transferPolicy],
|
|
34
|
+
});
|
|
35
|
+
|
|
36
|
+
return transferPolicyCap;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
/**
|
|
40
|
+
* Call the `transfer_policy::withdraw` function to withdraw profits from a transfer policy.
|
|
41
|
+
*/
|
|
42
|
+
export function withdrawFromPolicy(
|
|
43
|
+
tx: TransactionBlock,
|
|
44
|
+
itemType: string,
|
|
45
|
+
policy: ObjectArgument,
|
|
46
|
+
policyCap: ObjectArgument,
|
|
47
|
+
amount: string | bigint | null,
|
|
48
|
+
): TransactionArgument {
|
|
49
|
+
let amountArg =
|
|
50
|
+
amount !== null
|
|
51
|
+
? tx.pure(amount, 'Option<u64>')
|
|
52
|
+
: tx.pure({ None: true }, 'Option<u64>');
|
|
53
|
+
|
|
54
|
+
let [profits] = tx.moveCall({
|
|
55
|
+
target: `${TRANSFER_POLICY_MODULE}::withdraw`,
|
|
56
|
+
typeArguments: [itemType],
|
|
57
|
+
arguments: [objArg(tx, policy), objArg(tx, policyCap), amountArg],
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
return profits;
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
/**
|
|
64
|
+
* Call the `transfer_policy::confirm_request` function to unblock the
|
|
65
|
+
* transaction.
|
|
66
|
+
*/
|
|
67
|
+
export function confirmRequest(
|
|
68
|
+
tx: TransactionBlock,
|
|
69
|
+
itemType: string,
|
|
70
|
+
policy: ObjectArgument,
|
|
71
|
+
request: TransactionArgument,
|
|
72
|
+
): void {
|
|
73
|
+
tx.moveCall({
|
|
74
|
+
target: `${TRANSFER_POLICY_MODULE}::confirm_request`,
|
|
75
|
+
typeArguments: [itemType],
|
|
76
|
+
arguments: [objArg(tx, policy), request],
|
|
77
|
+
});
|
|
78
|
+
}
|
|
79
|
+
|
|
80
|
+
/**
|
|
81
|
+
* Calls the `transfer_policy::remove_rule` function to remove a Rule from the transfer policy's ruleset.
|
|
82
|
+
*/
|
|
83
|
+
export function removeTransferPolicyRule(
|
|
84
|
+
tx: TransactionBlock,
|
|
85
|
+
itemType: string,
|
|
86
|
+
ruleType: string,
|
|
87
|
+
configType: string,
|
|
88
|
+
policy: ObjectArgument,
|
|
89
|
+
policyCap: TransactionArgument,
|
|
90
|
+
): void {
|
|
91
|
+
tx.moveCall({
|
|
92
|
+
target: `${TRANSFER_POLICY_MODULE}::remove_rule`,
|
|
93
|
+
typeArguments: [itemType, ruleType, configType],
|
|
94
|
+
arguments: [objArg(tx, policy), policyCap],
|
|
95
|
+
});
|
|
96
|
+
}
|
|
97
|
+
|
|
98
|
+
/**
|
|
99
|
+
* Calculates the amount to be paid for the royalty rule to be resolved,
|
|
100
|
+
* splits the coin to pass the exact amount,
|
|
101
|
+
* then calls the `royalty_rule::pay` function to resolve the royalty rule.
|
|
102
|
+
*/
|
|
103
|
+
export function resolveRoyaltyRule(
|
|
104
|
+
tx: TransactionBlock,
|
|
105
|
+
itemType: string,
|
|
106
|
+
price: string,
|
|
107
|
+
policyId: string,
|
|
108
|
+
transferRequest: TransactionArgument,
|
|
109
|
+
) {
|
|
110
|
+
const policyObj = objArg(tx, policyId);
|
|
111
|
+
// calculates the amount
|
|
112
|
+
const [amount] = tx.moveCall({
|
|
113
|
+
target: `${TRANSFER_POLICY_RULES_PACKAGE_ADDRESS}::royalty_rule::fee_amount`,
|
|
114
|
+
typeArguments: [itemType],
|
|
115
|
+
arguments: [policyObj, objArg(tx, price)],
|
|
116
|
+
});
|
|
117
|
+
|
|
118
|
+
// splits the coin.
|
|
119
|
+
const feeCoin = tx.splitCoins(tx.gas, [amount]);
|
|
120
|
+
|
|
121
|
+
// pays the policy
|
|
122
|
+
tx.moveCall({
|
|
123
|
+
target: `${TRANSFER_POLICY_RULES_PACKAGE_ADDRESS}::royalty_rule::pay`,
|
|
124
|
+
typeArguments: [itemType],
|
|
125
|
+
arguments: [policyObj, transferRequest, feeCoin],
|
|
126
|
+
});
|
|
127
|
+
}
|
package/src/utils.ts
ADDED
|
@@ -0,0 +1,117 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import {
|
|
5
|
+
JsonRpcProvider,
|
|
6
|
+
SharedObjectRef,
|
|
7
|
+
SuiObjectDataOptions,
|
|
8
|
+
SuiObjectRef,
|
|
9
|
+
TransactionArgument,
|
|
10
|
+
TransactionBlock,
|
|
11
|
+
} from '@mysten/sui.js';
|
|
12
|
+
import { KioskData } from './query/kiosk';
|
|
13
|
+
import { DynamicFieldInfo } from '@mysten/sui.js/dist/types/dynamic_fields';
|
|
14
|
+
import { bcs, Kiosk } from './bcs';
|
|
15
|
+
|
|
16
|
+
/**
|
|
17
|
+
* A valid argument for any of the Kiosk functions.
|
|
18
|
+
*/
|
|
19
|
+
export type ObjectArgument =
|
|
20
|
+
| string
|
|
21
|
+
| TransactionArgument
|
|
22
|
+
| SharedObjectRef
|
|
23
|
+
| SuiObjectRef;
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* Convert any valid input into a TransactionArgument.
|
|
27
|
+
*
|
|
28
|
+
* @param tx The transaction to use for creating the argument.
|
|
29
|
+
* @param arg The argument to convert.
|
|
30
|
+
* @returns The converted TransactionArgument.
|
|
31
|
+
*/
|
|
32
|
+
export function objArg(
|
|
33
|
+
tx: TransactionBlock,
|
|
34
|
+
arg: string | SharedObjectRef | SuiObjectRef | TransactionArgument,
|
|
35
|
+
): TransactionArgument {
|
|
36
|
+
if (typeof arg === 'string') {
|
|
37
|
+
return tx.object(arg);
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
if ('digest' in arg && 'version' in arg && 'objectId' in arg) {
|
|
41
|
+
return tx.objectRef(arg);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
if ('objectId' in arg && 'initialSharedVersion' in arg && 'mutable' in arg) {
|
|
45
|
+
return tx.sharedObjectRef(arg);
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
if ('kind' in arg) {
|
|
49
|
+
return arg;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
throw new Error('Invalid argument type');
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export async function getKioskObject(
|
|
56
|
+
provider: JsonRpcProvider,
|
|
57
|
+
id: string,
|
|
58
|
+
): Promise<Kiosk> {
|
|
59
|
+
const queryRes = await provider.getObject({ id, options: { showBcs: true } });
|
|
60
|
+
|
|
61
|
+
if (!queryRes || queryRes.error || !queryRes.data) {
|
|
62
|
+
throw new Error(`Kiosk ${id} not found; ${queryRes.error}`);
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
if (!queryRes.data.bcs || !('bcsBytes' in queryRes.data.bcs)) {
|
|
66
|
+
throw new Error(`Invalid kiosk query: ${id}, expected object, got package`);
|
|
67
|
+
}
|
|
68
|
+
|
|
69
|
+
return bcs.de('0x2::kiosk::Kiosk', queryRes.data.bcs!.bcsBytes, 'base64');
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
// helper to extract kiosk data from dynamic fields.
|
|
73
|
+
export function extractKioskData(data: DynamicFieldInfo[]): KioskData {
|
|
74
|
+
return data.reduce<KioskData>(
|
|
75
|
+
(acc: KioskData, val: DynamicFieldInfo) => {
|
|
76
|
+
const type = getTypeWithoutPackageAddress(val.name.type);
|
|
77
|
+
|
|
78
|
+
switch (type) {
|
|
79
|
+
case 'kiosk::Item':
|
|
80
|
+
acc.itemIds.push(val.objectId);
|
|
81
|
+
acc.items.push({
|
|
82
|
+
itemId: val.objectId,
|
|
83
|
+
itemType: val.objectType,
|
|
84
|
+
});
|
|
85
|
+
break;
|
|
86
|
+
case 'kiosk::Listing':
|
|
87
|
+
acc.listingIds.push(val.objectId);
|
|
88
|
+
acc.listings.push({
|
|
89
|
+
itemId: val.name.value.id,
|
|
90
|
+
listingId: val.objectId,
|
|
91
|
+
isExclusive: val.name.value.is_exclusive,
|
|
92
|
+
});
|
|
93
|
+
break;
|
|
94
|
+
}
|
|
95
|
+
return acc;
|
|
96
|
+
},
|
|
97
|
+
{ listings: [], items: [], itemIds: [], listingIds: [] },
|
|
98
|
+
);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
// simple multiGetObjects wrapper to simplify cases on functions.
|
|
102
|
+
export function getObjects(
|
|
103
|
+
provider: JsonRpcProvider,
|
|
104
|
+
ids: string[],
|
|
105
|
+
options: SuiObjectDataOptions,
|
|
106
|
+
) {
|
|
107
|
+
if (ids.length === 0) {
|
|
108
|
+
return Promise.resolve([]);
|
|
109
|
+
}
|
|
110
|
+
|
|
111
|
+
return provider.multiGetObjects({ ids, options });
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
// e.g. 0x2::kiosk::Item -> kiosk::Item
|
|
115
|
+
export const getTypeWithoutPackageAddress = (type: string) => {
|
|
116
|
+
return type.split('::').slice(-2).join('::');
|
|
117
|
+
};
|