@mysten/kiosk 0.6.0 → 0.7.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/CHANGELOG.md +17 -0
- package/README.md +2 -287
- package/dist/client/kiosk-client.d.ts +64 -0
- package/dist/client/kiosk-transaction.d.ts +182 -0
- package/dist/client/tp-transaction.d.ts +112 -0
- package/dist/constants.d.ts +30 -4
- package/dist/index.d.ts +3 -6
- package/dist/index.js +1256 -264
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1239 -230
- package/dist/index.mjs.map +1 -1
- package/dist/query/kiosk.d.ts +2 -1
- package/dist/query/transfer-policy.d.ts +17 -1
- package/dist/tx/kiosk.d.ts +11 -27
- package/dist/tx/personal-kiosk.d.ts +7 -0
- package/dist/tx/rules/attach.d.ts +7 -0
- package/dist/tx/rules/resolve.d.ts +15 -0
- package/dist/tx/transfer-policy.d.ts +14 -17
- package/dist/types/index.d.ts +23 -5
- package/dist/types/kiosk.d.ts +30 -1
- package/dist/types/transfer-policy.d.ts +27 -1
- package/dist/utils.d.ts +32 -18
- package/package.json +12 -4
- package/src/bcs.ts +1 -0
- package/src/client/kiosk-client.ts +156 -0
- package/src/client/kiosk-transaction.ts +519 -0
- package/src/client/tp-transaction.ts +350 -0
- package/src/constants.ts +113 -6
- package/src/index.ts +3 -6
- package/src/query/kiosk.ts +51 -18
- package/src/query/transfer-policy.ts +82 -2
- package/src/tx/kiosk.ts +37 -158
- package/src/tx/personal-kiosk.ts +35 -0
- package/src/tx/rules/attach.ts +74 -0
- package/src/tx/rules/resolve.ts +87 -0
- package/src/tx/transfer-policy.ts +46 -79
- package/src/types/index.ts +26 -5
- package/src/types/kiosk.ts +26 -1
- package/src/types/transfer-policy.ts +35 -1
- package/src/utils.ts +143 -35
- package/dist/tx/rules.d.ts +0 -19
- package/dist/types/env.d.ts +0 -12
- package/src/tx/rules.ts +0 -58
- package/src/types/env.ts +0 -20
|
@@ -1,15 +1,15 @@
|
|
|
1
1
|
// Copyright (c) Mysten Labs, Inc.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import {
|
|
5
|
-
import { getRulePackageAddress, objArg } from '../utils';
|
|
6
|
-
import { lock } from './kiosk';
|
|
4
|
+
import { bcs } from '@mysten/sui.js/bcs';
|
|
7
5
|
import {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
6
|
+
TransactionArgument,
|
|
7
|
+
TransactionBlock,
|
|
8
|
+
TransactionObjectArgument,
|
|
9
|
+
} from '@mysten/sui.js/transactions';
|
|
10
|
+
|
|
11
|
+
import { ObjectArgument, TRANSFER_POLICY_MODULE, TRANSFER_POLICY_TYPE } from '../types';
|
|
12
|
+
import { objArg } from '../utils';
|
|
13
13
|
|
|
14
14
|
/**
|
|
15
15
|
* Call the `transfer_policy::new` function to create a new transfer policy.
|
|
@@ -19,20 +19,48 @@ export function createTransferPolicy(
|
|
|
19
19
|
tx: TransactionBlock,
|
|
20
20
|
itemType: string,
|
|
21
21
|
publisher: ObjectArgument,
|
|
22
|
-
):
|
|
23
|
-
|
|
22
|
+
): TransactionObjectArgument {
|
|
23
|
+
const [transferPolicy, transferPolicyCap] = createTransferPolicyWithoutSharing(
|
|
24
|
+
tx,
|
|
25
|
+
itemType,
|
|
26
|
+
publisher,
|
|
27
|
+
);
|
|
28
|
+
|
|
29
|
+
shareTransferPolicy(tx, itemType, transferPolicy);
|
|
30
|
+
|
|
31
|
+
return transferPolicyCap;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
/**
|
|
35
|
+
* Creates a transfer Policy and returns both the Policy and the Cap.
|
|
36
|
+
* Used if we want to use the policy before making it a shared object.
|
|
37
|
+
*/
|
|
38
|
+
export function createTransferPolicyWithoutSharing(
|
|
39
|
+
tx: TransactionBlock,
|
|
40
|
+
itemType: string,
|
|
41
|
+
publisher: ObjectArgument,
|
|
42
|
+
): [TransactionObjectArgument, TransactionObjectArgument] {
|
|
43
|
+
const [transferPolicy, transferPolicyCap] = tx.moveCall({
|
|
24
44
|
target: `${TRANSFER_POLICY_MODULE}::new`,
|
|
25
45
|
typeArguments: [itemType],
|
|
26
46
|
arguments: [objArg(tx, publisher)],
|
|
27
47
|
});
|
|
28
48
|
|
|
49
|
+
return [transferPolicy, transferPolicyCap];
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Converts Transfer Policy to a shared object.
|
|
53
|
+
*/
|
|
54
|
+
export function shareTransferPolicy(
|
|
55
|
+
tx: TransactionBlock,
|
|
56
|
+
itemType: string,
|
|
57
|
+
transferPolicy: TransactionObjectArgument,
|
|
58
|
+
) {
|
|
29
59
|
tx.moveCall({
|
|
30
60
|
target: `0x2::transfer::public_share_object`,
|
|
31
61
|
typeArguments: [`${TRANSFER_POLICY_TYPE}<${itemType}>`],
|
|
32
62
|
arguments: [transferPolicy],
|
|
33
63
|
});
|
|
34
|
-
|
|
35
|
-
return transferPolicyCap;
|
|
36
64
|
}
|
|
37
65
|
|
|
38
66
|
/**
|
|
@@ -43,14 +71,11 @@ export function withdrawFromPolicy(
|
|
|
43
71
|
itemType: string,
|
|
44
72
|
policy: ObjectArgument,
|
|
45
73
|
policyCap: ObjectArgument,
|
|
46
|
-
amount
|
|
47
|
-
):
|
|
48
|
-
|
|
49
|
-
amount !== null
|
|
50
|
-
? tx.pure({ Some: amount }, 'Option<u64>')
|
|
51
|
-
: tx.pure({ None: true }, 'Option<u64>');
|
|
74
|
+
amount?: string | bigint | null,
|
|
75
|
+
): TransactionObjectArgument {
|
|
76
|
+
const amountArg = bcs.option(bcs.u64()).serialize(amount);
|
|
52
77
|
|
|
53
|
-
|
|
78
|
+
const [profits] = tx.moveCall({
|
|
54
79
|
target: `${TRANSFER_POLICY_MODULE}::withdraw`,
|
|
55
80
|
typeArguments: [itemType],
|
|
56
81
|
arguments: [objArg(tx, policy), objArg(tx, policyCap), amountArg],
|
|
@@ -85,69 +110,11 @@ export function removeTransferPolicyRule(
|
|
|
85
110
|
ruleType: string,
|
|
86
111
|
configType: string,
|
|
87
112
|
policy: ObjectArgument,
|
|
88
|
-
policyCap:
|
|
113
|
+
policyCap: ObjectArgument,
|
|
89
114
|
): void {
|
|
90
115
|
tx.moveCall({
|
|
91
116
|
target: `${TRANSFER_POLICY_MODULE}::remove_rule`,
|
|
92
117
|
typeArguments: [itemType, ruleType, configType],
|
|
93
|
-
arguments: [objArg(tx, policy), policyCap],
|
|
94
|
-
});
|
|
95
|
-
}
|
|
96
|
-
|
|
97
|
-
/**
|
|
98
|
-
* Calculates the amount to be paid for the royalty rule to be resolved,
|
|
99
|
-
* splits the coin to pass the exact amount,
|
|
100
|
-
* then calls the `royalty_rule::pay` function to resolve the royalty rule.
|
|
101
|
-
*/
|
|
102
|
-
export function resolveRoyaltyRule(
|
|
103
|
-
tx: TransactionBlock,
|
|
104
|
-
itemType: string,
|
|
105
|
-
price: string,
|
|
106
|
-
policyId: ObjectArgument,
|
|
107
|
-
transferRequest: TransactionArgument,
|
|
108
|
-
environment: RulesEnvironmentParam,
|
|
109
|
-
) {
|
|
110
|
-
const policyObj = objArg(tx, policyId);
|
|
111
|
-
// calculates the amount
|
|
112
|
-
const [amount] = tx.moveCall({
|
|
113
|
-
target: `${getRulePackageAddress(environment)}::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: `${getRulePackageAddress(environment)}::royalty_rule::pay`,
|
|
124
|
-
typeArguments: [itemType],
|
|
125
|
-
arguments: [policyObj, transferRequest, feeCoin],
|
|
126
|
-
});
|
|
127
|
-
}
|
|
128
|
-
|
|
129
|
-
/**
|
|
130
|
-
* Locks the item in the supplied kiosk and
|
|
131
|
-
* proves to the `kiosk_lock` rule that the item was indeed locked,
|
|
132
|
-
* by calling the `kiosk_lock_rule::prove` function to resolve it.
|
|
133
|
-
*/
|
|
134
|
-
export function resolveKioskLockRule(
|
|
135
|
-
tx: TransactionBlock,
|
|
136
|
-
itemType: string,
|
|
137
|
-
item: TransactionArgument,
|
|
138
|
-
kiosk: ObjectArgument,
|
|
139
|
-
kioskCap: ObjectArgument,
|
|
140
|
-
policyId: ObjectArgument,
|
|
141
|
-
transferRequest: TransactionArgument,
|
|
142
|
-
environment: RulesEnvironmentParam,
|
|
143
|
-
) {
|
|
144
|
-
// lock item in the kiosk.
|
|
145
|
-
lock(tx, itemType, kiosk, kioskCap, policyId, item);
|
|
146
|
-
|
|
147
|
-
// proves that the item is locked in the kiosk to the TP.
|
|
148
|
-
tx.moveCall({
|
|
149
|
-
target: `${getRulePackageAddress(environment)}::kiosk_lock_rule::prove`,
|
|
150
|
-
typeArguments: [itemType],
|
|
151
|
-
arguments: [transferRequest, objArg(tx, kiosk)],
|
|
118
|
+
arguments: [objArg(tx, policy), objArg(tx, policyCap)],
|
|
152
119
|
});
|
|
153
120
|
}
|
package/src/types/index.ts
CHANGED
|
@@ -1,15 +1,36 @@
|
|
|
1
1
|
// Copyright (c) Mysten Labs, Inc.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { SharedObjectRef } from '@mysten/sui.js/bcs';
|
|
5
|
-
import { SuiObjectRef } from '@mysten/sui.js/client';
|
|
6
|
-
import {
|
|
4
|
+
import { type SharedObjectRef } from '@mysten/sui.js/bcs';
|
|
5
|
+
import { type SuiClient, type SuiObjectRef } from '@mysten/sui.js/client';
|
|
6
|
+
import { TransactionObjectArgument } from '@mysten/sui.js/transactions';
|
|
7
|
+
|
|
8
|
+
import { BaseRulePackageIds } from '../constants';
|
|
7
9
|
|
|
8
10
|
export * from './kiosk';
|
|
9
11
|
export * from './transfer-policy';
|
|
10
|
-
export * from './env';
|
|
11
12
|
|
|
12
13
|
/**
|
|
13
14
|
* A valid argument for any of the Kiosk functions.
|
|
14
15
|
*/
|
|
15
|
-
export type ObjectArgument = string |
|
|
16
|
+
export type ObjectArgument = string | TransactionObjectArgument | SharedObjectRef | SuiObjectRef;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* A Network selector.
|
|
20
|
+
* Kiosk SDK supports mainnet & testnet.
|
|
21
|
+
* Pass `custom` for any other network (devnet, localnet).
|
|
22
|
+
*/
|
|
23
|
+
export enum Network {
|
|
24
|
+
MAINNET = 'mainnet',
|
|
25
|
+
TESTNET = 'testnet',
|
|
26
|
+
CUSTOM = 'custom',
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* The Client Options for Both KioskClient & TransferPolicyManager.
|
|
31
|
+
*/
|
|
32
|
+
export type KioskClientOptions = {
|
|
33
|
+
client: SuiClient;
|
|
34
|
+
network: Network;
|
|
35
|
+
packageIds?: BaseRulePackageIds;
|
|
36
|
+
};
|
package/src/types/kiosk.ts
CHANGED
|
@@ -1,8 +1,13 @@
|
|
|
1
1
|
// Copyright (c) Mysten Labs, Inc.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import {
|
|
4
|
+
import {
|
|
5
|
+
PaginatedObjectsResponse,
|
|
6
|
+
SuiObjectData,
|
|
7
|
+
SuiObjectDataOptions,
|
|
8
|
+
} from '@mysten/sui.js/client';
|
|
5
9
|
import { TransactionArgument } from '@mysten/sui.js/transactions';
|
|
10
|
+
|
|
6
11
|
import { ObjectArgument } from '.';
|
|
7
12
|
|
|
8
13
|
/** The Kiosk module. */
|
|
@@ -98,6 +103,10 @@ export type KioskItem = {
|
|
|
98
103
|
isLocked: boolean;
|
|
99
104
|
/** Optional listing */
|
|
100
105
|
listing?: KioskListing;
|
|
106
|
+
/** The ID of the kiosk the item is placed in */
|
|
107
|
+
kioskId: string;
|
|
108
|
+
/** Optional Kiosk Data */
|
|
109
|
+
data?: SuiObjectData;
|
|
101
110
|
};
|
|
102
111
|
/**
|
|
103
112
|
* Aggregated data from the Kiosk.
|
|
@@ -117,8 +126,14 @@ export type PagedKioskData = {
|
|
|
117
126
|
};
|
|
118
127
|
|
|
119
128
|
export type FetchKioskOptions = {
|
|
129
|
+
/** Include the base kiosk object, which includes the profits, the owner and the base fields. */
|
|
120
130
|
withKioskFields?: boolean;
|
|
131
|
+
/** Include the listing prices. */
|
|
121
132
|
withListingPrices?: boolean;
|
|
133
|
+
/** Include the objects for the Items in the kiosk. Defaults to `display` only. */
|
|
134
|
+
withObjects?: boolean;
|
|
135
|
+
/** Pass the data options for the objects, when fetching, in case you want to query other details. */
|
|
136
|
+
objectOptions?: SuiObjectDataOptions;
|
|
122
137
|
};
|
|
123
138
|
|
|
124
139
|
export type OwnedKiosks = {
|
|
@@ -127,8 +142,18 @@ export type OwnedKiosks = {
|
|
|
127
142
|
} & Omit<PaginatedObjectsResponse, 'data'>;
|
|
128
143
|
|
|
129
144
|
export type KioskOwnerCap = {
|
|
145
|
+
isPersonal?: boolean;
|
|
130
146
|
objectId: string;
|
|
131
147
|
kioskId: string;
|
|
132
148
|
digest: string;
|
|
133
149
|
version: string;
|
|
134
150
|
};
|
|
151
|
+
|
|
152
|
+
export type PurchaseOptions = {
|
|
153
|
+
extraArgs?: Record<string, any>;
|
|
154
|
+
};
|
|
155
|
+
|
|
156
|
+
export type ItemId = { itemType: string; itemId: string };
|
|
157
|
+
export type ItemReference = { itemType: string; item: ObjectArgument };
|
|
158
|
+
export type ItemValue = { itemType: string; item: TransactionArgument };
|
|
159
|
+
export type Price = { price: string | bigint };
|
|
@@ -1,7 +1,10 @@
|
|
|
1
1
|
// Copyright (c) Mysten Labs, Inc.
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
|
-
import { ObjectOwner } from '@mysten/sui.js/client';
|
|
4
|
+
import { type ObjectOwner } from '@mysten/sui.js/client';
|
|
5
|
+
import { TransactionObjectArgument, type TransactionBlock } from '@mysten/sui.js/transactions';
|
|
6
|
+
|
|
7
|
+
import { ObjectArgument } from '.';
|
|
5
8
|
|
|
6
9
|
/** The Transfer Policy module. */
|
|
7
10
|
export const TRANSFER_POLICY_MODULE = '0x2::transfer_policy';
|
|
@@ -12,12 +15,24 @@ export const TRANSFER_POLICY_CREATED_EVENT = `${TRANSFER_POLICY_MODULE}::Transfe
|
|
|
12
15
|
/** The Transfer Policy Type */
|
|
13
16
|
export const TRANSFER_POLICY_TYPE = `${TRANSFER_POLICY_MODULE}::TransferPolicy`;
|
|
14
17
|
|
|
18
|
+
/** The Transfer Policy Cap Type */
|
|
19
|
+
export const TRANSFER_POLICY_CAP_TYPE = `${TRANSFER_POLICY_MODULE}::TransferPolicyCap`;
|
|
20
|
+
|
|
15
21
|
/** The Kiosk Lock Rule */
|
|
16
22
|
export const KIOSK_LOCK_RULE = 'kiosk_lock_rule::Rule';
|
|
17
23
|
|
|
18
24
|
/** The Royalty rule */
|
|
19
25
|
export const ROYALTY_RULE = 'royalty_rule::Rule';
|
|
20
26
|
|
|
27
|
+
/**
|
|
28
|
+
* The Transfer Policy Cap in a consumable way.
|
|
29
|
+
*/
|
|
30
|
+
export type TransferPolicyCap = {
|
|
31
|
+
policyId: string;
|
|
32
|
+
policyCapId: string;
|
|
33
|
+
type: string;
|
|
34
|
+
};
|
|
35
|
+
|
|
21
36
|
/** The `TransferPolicy` object */
|
|
22
37
|
export type TransferPolicy = {
|
|
23
38
|
id: string;
|
|
@@ -31,3 +46,22 @@ export type TransferPolicy = {
|
|
|
31
46
|
export type TransferPolicyCreated = {
|
|
32
47
|
id: string;
|
|
33
48
|
};
|
|
49
|
+
|
|
50
|
+
// The object a Rule resolving function accepts
|
|
51
|
+
// It can accept a set of fixed fields, that are part of every purchase flow as well any extra arguments to resolve custom policies!
|
|
52
|
+
// Each rule resolving function should check that the key it's seeking is in the object
|
|
53
|
+
// e.g. `if(!'my_key' in ruleParams!) throw new Error("Can't resolve that rule!")`
|
|
54
|
+
export type RuleResolvingParams = {
|
|
55
|
+
transactionBlock: TransactionBlock;
|
|
56
|
+
itemType: string;
|
|
57
|
+
itemId: string;
|
|
58
|
+
price: string;
|
|
59
|
+
policyId: ObjectArgument;
|
|
60
|
+
sellerKiosk: ObjectArgument;
|
|
61
|
+
kiosk: ObjectArgument;
|
|
62
|
+
kioskCap: ObjectArgument;
|
|
63
|
+
transferRequest: TransactionObjectArgument;
|
|
64
|
+
purchasedItem: TransactionObjectArgument;
|
|
65
|
+
packageId: string;
|
|
66
|
+
extraArgs: Record<string, any>; // extraParams contains more possible {key, values} to pass for custom rules.
|
|
67
|
+
};
|
package/src/utils.ts
CHANGED
|
@@ -2,44 +2,52 @@
|
|
|
2
2
|
// SPDX-License-Identifier: Apache-2.0
|
|
3
3
|
|
|
4
4
|
import { SharedObjectRef } from '@mysten/sui.js/bcs';
|
|
5
|
-
import {
|
|
6
|
-
|
|
7
|
-
|
|
5
|
+
import {
|
|
6
|
+
PaginationArguments,
|
|
7
|
+
SuiClient,
|
|
8
|
+
SuiObjectData,
|
|
9
|
+
SuiObjectDataFilter,
|
|
10
|
+
SuiObjectDataOptions,
|
|
11
|
+
SuiObjectRef,
|
|
12
|
+
SuiObjectResponse,
|
|
13
|
+
type DynamicFieldInfo,
|
|
14
|
+
} from '@mysten/sui.js/client';
|
|
15
|
+
import { TransactionBlock, TransactionObjectArgument } from '@mysten/sui.js/transactions';
|
|
16
|
+
import { normalizeSuiAddress } from '@mysten/sui.js/utils';
|
|
17
|
+
|
|
8
18
|
import { bcs } from './bcs';
|
|
9
|
-
import {
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
custom: null,
|
|
20
|
-
};
|
|
19
|
+
import {
|
|
20
|
+
Kiosk,
|
|
21
|
+
KIOSK_TYPE,
|
|
22
|
+
KioskData,
|
|
23
|
+
KioskListing,
|
|
24
|
+
TRANSFER_POLICY_CAP_TYPE,
|
|
25
|
+
TransferPolicyCap,
|
|
26
|
+
} from './types';
|
|
27
|
+
|
|
28
|
+
const DEFAULT_QUERY_LIMIT = 50;
|
|
21
29
|
|
|
22
30
|
/**
|
|
23
31
|
* Convert any valid input into a TransactionArgument.
|
|
24
32
|
*
|
|
25
|
-
* @param
|
|
33
|
+
* @param txb The Transaction Block
|
|
26
34
|
* @param arg The argument to convert.
|
|
27
35
|
* @returns The converted TransactionArgument.
|
|
28
36
|
*/
|
|
29
37
|
export function objArg(
|
|
30
|
-
|
|
31
|
-
arg: string | SharedObjectRef | SuiObjectRef |
|
|
32
|
-
):
|
|
38
|
+
txb: TransactionBlock,
|
|
39
|
+
arg: string | SharedObjectRef | SuiObjectRef | TransactionObjectArgument,
|
|
40
|
+
): TransactionObjectArgument {
|
|
33
41
|
if (typeof arg === 'string') {
|
|
34
|
-
return
|
|
42
|
+
return txb.object(arg);
|
|
35
43
|
}
|
|
36
44
|
|
|
37
45
|
if ('digest' in arg && 'version' in arg && 'objectId' in arg) {
|
|
38
|
-
return
|
|
46
|
+
return txb.objectRef(arg);
|
|
39
47
|
}
|
|
40
48
|
|
|
41
49
|
if ('objectId' in arg && 'initialSharedVersion' in arg && 'mutable' in arg) {
|
|
42
|
-
return
|
|
50
|
+
return txb.sharedObjectRef(arg);
|
|
43
51
|
}
|
|
44
52
|
|
|
45
53
|
if ('kind' in arg) {
|
|
@@ -68,6 +76,7 @@ export function extractKioskData(
|
|
|
68
76
|
data: DynamicFieldInfo[],
|
|
69
77
|
listings: KioskListing[],
|
|
70
78
|
lockedItemIds: string[],
|
|
79
|
+
kioskId: string,
|
|
71
80
|
): KioskData {
|
|
72
81
|
return data.reduce<KioskData>(
|
|
73
82
|
(acc: KioskData, val: DynamicFieldInfo) => {
|
|
@@ -80,6 +89,7 @@ export function extractKioskData(
|
|
|
80
89
|
objectId: val.objectId,
|
|
81
90
|
type: val.objectType,
|
|
82
91
|
isLocked: false,
|
|
92
|
+
kioskId,
|
|
83
93
|
});
|
|
84
94
|
break;
|
|
85
95
|
case 'kiosk::Listing':
|
|
@@ -139,6 +149,23 @@ export function attachListingsAndPrices(
|
|
|
139
149
|
});
|
|
140
150
|
}
|
|
141
151
|
|
|
152
|
+
/**
|
|
153
|
+
* A helper that attaches the listing prices to kiosk listings.
|
|
154
|
+
*/
|
|
155
|
+
export function attachObjects(kioskData: KioskData, objects: SuiObjectData[]) {
|
|
156
|
+
const mapping = objects.reduce<Record<string, SuiObjectData>>(
|
|
157
|
+
(acc: Record<string, SuiObjectData>, obj) => {
|
|
158
|
+
acc[obj.objectId] = obj;
|
|
159
|
+
return acc;
|
|
160
|
+
},
|
|
161
|
+
{},
|
|
162
|
+
);
|
|
163
|
+
|
|
164
|
+
kioskData.items.forEach((item) => {
|
|
165
|
+
item.data = mapping[item.objectId] || undefined;
|
|
166
|
+
});
|
|
167
|
+
}
|
|
168
|
+
|
|
142
169
|
/**
|
|
143
170
|
* A Helper to attach locked state to items in Kiosk Data.
|
|
144
171
|
*/
|
|
@@ -158,19 +185,6 @@ export function attachLockedItems(kioskData: KioskData, lockedItemIds: string[])
|
|
|
158
185
|
});
|
|
159
186
|
}
|
|
160
187
|
|
|
161
|
-
/**
|
|
162
|
-
* A helper to get a rule's environment address.
|
|
163
|
-
*/
|
|
164
|
-
export function getRulePackageAddress(environment: RulesEnvironmentParam): string {
|
|
165
|
-
// if we have custom environment, we return it.
|
|
166
|
-
if (environment.env === 'custom') {
|
|
167
|
-
if (!environment.address)
|
|
168
|
-
throw new Error('Please supply the custom package address for rules.');
|
|
169
|
-
return environment.address;
|
|
170
|
-
}
|
|
171
|
-
return rulesPackageAddresses[environment.env];
|
|
172
|
-
}
|
|
173
|
-
|
|
174
188
|
/**
|
|
175
189
|
* A helper to fetch all DF pages.
|
|
176
190
|
* We need that to fetch the kiosk DFs consistently, until we have
|
|
@@ -199,6 +213,70 @@ export async function getAllDynamicFields(
|
|
|
199
213
|
return data;
|
|
200
214
|
}
|
|
201
215
|
|
|
216
|
+
/**
|
|
217
|
+
* A helper to fetch all objects that works with pagination.
|
|
218
|
+
* It will fetch all objects in the array, and limit it to 50/request.
|
|
219
|
+
* Requests are sent using `Promise.all`.
|
|
220
|
+
*/
|
|
221
|
+
export async function getAllObjects(
|
|
222
|
+
client: SuiClient,
|
|
223
|
+
ids: string[],
|
|
224
|
+
options: SuiObjectDataOptions,
|
|
225
|
+
limit: number = DEFAULT_QUERY_LIMIT,
|
|
226
|
+
) {
|
|
227
|
+
const chunks = Array.from({ length: Math.ceil(ids.length / limit) }, (_, index) =>
|
|
228
|
+
ids.slice(index * limit, index * limit + limit),
|
|
229
|
+
);
|
|
230
|
+
|
|
231
|
+
const results = await Promise.all(
|
|
232
|
+
chunks.map((chunk) => {
|
|
233
|
+
return client.multiGetObjects({
|
|
234
|
+
ids: chunk,
|
|
235
|
+
options,
|
|
236
|
+
});
|
|
237
|
+
}),
|
|
238
|
+
);
|
|
239
|
+
|
|
240
|
+
return results.flat();
|
|
241
|
+
}
|
|
242
|
+
|
|
243
|
+
/**
|
|
244
|
+
* A helper to return all owned objects, with an optional filter.
|
|
245
|
+
* It parses all the pages and returns the data.
|
|
246
|
+
*/
|
|
247
|
+
export async function getAllOwnedObjects({
|
|
248
|
+
client,
|
|
249
|
+
owner,
|
|
250
|
+
filter,
|
|
251
|
+
limit = DEFAULT_QUERY_LIMIT,
|
|
252
|
+
options = { showType: true, showContent: true },
|
|
253
|
+
}: {
|
|
254
|
+
client: SuiClient;
|
|
255
|
+
owner: string;
|
|
256
|
+
filter?: SuiObjectDataFilter;
|
|
257
|
+
options?: SuiObjectDataOptions;
|
|
258
|
+
limit?: number;
|
|
259
|
+
}) {
|
|
260
|
+
let hasNextPage = true;
|
|
261
|
+
let cursor = undefined;
|
|
262
|
+
const data: SuiObjectResponse[] = [];
|
|
263
|
+
|
|
264
|
+
while (hasNextPage) {
|
|
265
|
+
const result = await client.getOwnedObjects({
|
|
266
|
+
owner,
|
|
267
|
+
filter,
|
|
268
|
+
limit,
|
|
269
|
+
cursor,
|
|
270
|
+
options,
|
|
271
|
+
});
|
|
272
|
+
data.push(...result.data);
|
|
273
|
+
hasNextPage = result.hasNextPage;
|
|
274
|
+
cursor = result.nextCursor;
|
|
275
|
+
}
|
|
276
|
+
|
|
277
|
+
return data;
|
|
278
|
+
}
|
|
279
|
+
|
|
202
280
|
/**
|
|
203
281
|
* Converts a number to basis points.
|
|
204
282
|
* Supports up to 2 decimal points.
|
|
@@ -210,3 +288,33 @@ export function percentageToBasisPoints(percentage: number) {
|
|
|
210
288
|
throw new Error('Percentage needs to be in the [0,100] range.');
|
|
211
289
|
return Math.ceil(percentage * 100);
|
|
212
290
|
}
|
|
291
|
+
|
|
292
|
+
/**
|
|
293
|
+
* A helper to parse a transfer policy Cap into a usable object.
|
|
294
|
+
*/
|
|
295
|
+
export function parseTransferPolicyCapObject(
|
|
296
|
+
item: SuiObjectResponse,
|
|
297
|
+
): TransferPolicyCap | undefined {
|
|
298
|
+
const type = (item?.data?.content as { type: string })?.type;
|
|
299
|
+
|
|
300
|
+
//@ts-ignore-next-line
|
|
301
|
+
const policy = item?.data?.content?.fields?.policy_id as string;
|
|
302
|
+
|
|
303
|
+
if (!type.includes(TRANSFER_POLICY_CAP_TYPE)) return undefined;
|
|
304
|
+
|
|
305
|
+
// Transform 0x2::transfer_policy::TransferPolicyCap<itemType> -> itemType
|
|
306
|
+
const objectType = type.replace(TRANSFER_POLICY_CAP_TYPE + '<', '').slice(0, -1);
|
|
307
|
+
|
|
308
|
+
return {
|
|
309
|
+
policyId: policy,
|
|
310
|
+
policyCapId: item.data?.objectId!,
|
|
311
|
+
type: objectType,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
|
|
315
|
+
// Normalizes the packageId part of a rule's type.
|
|
316
|
+
export function getNormalizedRuleType(rule: string) {
|
|
317
|
+
const normalizedRuleAddress = rule.split('::');
|
|
318
|
+
normalizedRuleAddress[0] = normalizeSuiAddress(normalizedRuleAddress[0]);
|
|
319
|
+
return normalizedRuleAddress.join('::');
|
|
320
|
+
}
|
package/dist/tx/rules.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
import { TransactionBlock } from '@mysten/sui.js/transactions';
|
|
2
|
-
import { ObjectArgument, RulesEnvironmentParam } from '../types';
|
|
3
|
-
/**
|
|
4
|
-
* Adds the Kiosk Royalty rule to the Transfer Policy.
|
|
5
|
-
* You can pass the percentage, as well as a minimum amount.
|
|
6
|
-
* The royalty that will be paid is the MAX(percentage, minAmount).
|
|
7
|
-
* You can pass 0 in either value if you want only percentage royalty, or a fixed amount fee.
|
|
8
|
-
* (but you should define at least one of them for the rule to make sense).
|
|
9
|
-
*
|
|
10
|
-
* @param percentageBps The royalty percentage in basis points. Use `percentageToBasisPoints` helper to convert from percentage [0,100].
|
|
11
|
-
* @param minAmount The minimum royalty amount per request in MIST.
|
|
12
|
-
*/
|
|
13
|
-
export declare function attachRoyaltyRule(tx: TransactionBlock, type: string, policy: ObjectArgument, policyCap: ObjectArgument, percentageBps: number | string, // this is in basis points.
|
|
14
|
-
minAmount: number | string, environment: RulesEnvironmentParam): void;
|
|
15
|
-
/**
|
|
16
|
-
* Adds the Kiosk Lock Rule to the Transfer Policy.
|
|
17
|
-
* This Rule forces buyer to lock the item in the kiosk, preserving strong royalties.
|
|
18
|
-
*/
|
|
19
|
-
export declare function attachKioskLockRule(tx: TransactionBlock, type: string, policy: ObjectArgument, policyCap: ObjectArgument, environment: RulesEnvironmentParam): void;
|
package/dist/types/env.d.ts
DELETED
|
@@ -1,12 +0,0 @@
|
|
|
1
|
-
export type Environment = 'mainnet' | 'testnet' | 'devnet' | 'custom';
|
|
2
|
-
/** A Parameter to support environments for rules. */
|
|
3
|
-
export type RulesEnvironmentParam = {
|
|
4
|
-
env: Environment;
|
|
5
|
-
address?: string;
|
|
6
|
-
};
|
|
7
|
-
/** A default Testnet Environment object */
|
|
8
|
-
export declare const testnetEnvironment: RulesEnvironmentParam;
|
|
9
|
-
/** A default Mainnet Environment object */
|
|
10
|
-
export declare const mainnetEnvironment: RulesEnvironmentParam;
|
|
11
|
-
/** A helper to easily export a custom environment */
|
|
12
|
-
export declare const customEnvironment: (address: string) => RulesEnvironmentParam;
|
package/src/tx/rules.ts
DELETED
|
@@ -1,58 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
import { TransactionBlock } from '@mysten/sui.js/transactions';
|
|
5
|
-
import { ObjectArgument, RulesEnvironmentParam } from '../types';
|
|
6
|
-
import { getRulePackageAddress, objArg } from '../utils';
|
|
7
|
-
|
|
8
|
-
/**
|
|
9
|
-
* Adds the Kiosk Royalty rule to the Transfer Policy.
|
|
10
|
-
* You can pass the percentage, as well as a minimum amount.
|
|
11
|
-
* The royalty that will be paid is the MAX(percentage, minAmount).
|
|
12
|
-
* You can pass 0 in either value if you want only percentage royalty, or a fixed amount fee.
|
|
13
|
-
* (but you should define at least one of them for the rule to make sense).
|
|
14
|
-
*
|
|
15
|
-
* @param percentageBps The royalty percentage in basis points. Use `percentageToBasisPoints` helper to convert from percentage [0,100].
|
|
16
|
-
* @param minAmount The minimum royalty amount per request in MIST.
|
|
17
|
-
*/
|
|
18
|
-
export function attachRoyaltyRule(
|
|
19
|
-
tx: TransactionBlock,
|
|
20
|
-
type: string,
|
|
21
|
-
policy: ObjectArgument,
|
|
22
|
-
policyCap: ObjectArgument,
|
|
23
|
-
percentageBps: number | string, // this is in basis points.
|
|
24
|
-
minAmount: number | string,
|
|
25
|
-
environment: RulesEnvironmentParam,
|
|
26
|
-
) {
|
|
27
|
-
if (Number(percentageBps) < 0 || Number(percentageBps) > 10_000)
|
|
28
|
-
throw new Error('Invalid basis point percentage. Use a value between [0,10000].');
|
|
29
|
-
|
|
30
|
-
tx.moveCall({
|
|
31
|
-
target: `${getRulePackageAddress(environment)}::royalty_rule::add`,
|
|
32
|
-
typeArguments: [type],
|
|
33
|
-
arguments: [
|
|
34
|
-
objArg(tx, policy),
|
|
35
|
-
objArg(tx, policyCap),
|
|
36
|
-
tx.pure(percentageBps, 'u16'),
|
|
37
|
-
tx.pure(minAmount, 'u64'),
|
|
38
|
-
],
|
|
39
|
-
});
|
|
40
|
-
}
|
|
41
|
-
|
|
42
|
-
/**
|
|
43
|
-
* Adds the Kiosk Lock Rule to the Transfer Policy.
|
|
44
|
-
* This Rule forces buyer to lock the item in the kiosk, preserving strong royalties.
|
|
45
|
-
*/
|
|
46
|
-
export function attachKioskLockRule(
|
|
47
|
-
tx: TransactionBlock,
|
|
48
|
-
type: string,
|
|
49
|
-
policy: ObjectArgument,
|
|
50
|
-
policyCap: ObjectArgument,
|
|
51
|
-
environment: RulesEnvironmentParam,
|
|
52
|
-
) {
|
|
53
|
-
tx.moveCall({
|
|
54
|
-
target: `${getRulePackageAddress(environment)}::kiosk_lock_rule::add`,
|
|
55
|
-
typeArguments: [type],
|
|
56
|
-
arguments: [objArg(tx, policy), objArg(tx, policyCap)],
|
|
57
|
-
});
|
|
58
|
-
}
|
package/src/types/env.ts
DELETED
|
@@ -1,20 +0,0 @@
|
|
|
1
|
-
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
-
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
-
|
|
4
|
-
/* A list of environments. */
|
|
5
|
-
export type Environment = 'mainnet' | 'testnet' | 'devnet' | 'custom';
|
|
6
|
-
|
|
7
|
-
/** A Parameter to support environments for rules. */
|
|
8
|
-
export type RulesEnvironmentParam = { env: Environment; address?: string };
|
|
9
|
-
|
|
10
|
-
/** A default Testnet Environment object */
|
|
11
|
-
export const testnetEnvironment: RulesEnvironmentParam = { env: 'testnet' };
|
|
12
|
-
|
|
13
|
-
/** A default Mainnet Environment object */
|
|
14
|
-
export const mainnetEnvironment: RulesEnvironmentParam = { env: 'mainnet' };
|
|
15
|
-
|
|
16
|
-
/** A helper to easily export a custom environment */
|
|
17
|
-
export const customEnvironment = (address: string): RulesEnvironmentParam => ({
|
|
18
|
-
env: 'custom',
|
|
19
|
-
address,
|
|
20
|
-
});
|