@iota/kiosk 0.1.0
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 +12 -0
- package/LICENSE +201 -0
- package/README.md +8 -0
- package/dist/cjs/bcs.d.ts +2 -0
- package/dist/cjs/bcs.js +47 -0
- package/dist/cjs/bcs.js.map +7 -0
- package/dist/cjs/client/kiosk-client.d.ts +74 -0
- package/dist/cjs/client/kiosk-client.js +121 -0
- package/dist/cjs/client/kiosk-client.js.map +7 -0
- package/dist/cjs/client/kiosk-transaction.d.ts +184 -0
- package/dist/cjs/client/kiosk-transaction.js +511 -0
- package/dist/cjs/client/kiosk-transaction.js.map +7 -0
- package/dist/cjs/client/tp-transaction.d.ts +112 -0
- package/dist/cjs/client/tp-transaction.js +323 -0
- package/dist/cjs/client/tp-transaction.js.map +7 -0
- package/dist/cjs/constants.d.ts +24 -0
- package/dist/cjs/constants.js +66 -0
- package/dist/cjs/constants.js.map +7 -0
- package/dist/cjs/index.d.ts +6 -0
- package/dist/cjs/index.js +24 -0
- package/dist/cjs/index.js.map +7 -0
- package/dist/cjs/package.json +5 -0
- package/dist/cjs/query/kiosk.d.ts +14 -0
- package/dist/cjs/query/kiosk.js +128 -0
- package/dist/cjs/query/kiosk.js.map +7 -0
- package/dist/cjs/query/transfer-policy.d.ts +28 -0
- package/dist/cjs/query/transfer-policy.js +96 -0
- package/dist/cjs/query/transfer-policy.js.map +7 -0
- package/dist/cjs/tx/kiosk.d.ts +71 -0
- package/dist/cjs/tx/kiosk.js +130 -0
- package/dist/cjs/tx/kiosk.js.map +7 -0
- package/dist/cjs/tx/personal-kiosk.d.ts +7 -0
- package/dist/cjs/tx/personal-kiosk.js +38 -0
- package/dist/cjs/tx/personal-kiosk.js.map +7 -0
- package/dist/cjs/tx/rules/attach.d.ts +7 -0
- package/dist/cjs/tx/rules/attach.js +62 -0
- package/dist/cjs/tx/rules/attach.js.map +7 -0
- package/dist/cjs/tx/rules/resolve.d.ts +15 -0
- package/dist/cjs/tx/rules/resolve.js +81 -0
- package/dist/cjs/tx/rules/resolve.js.map +7 -0
- package/dist/cjs/tx/transfer-policy.d.ts +29 -0
- package/dist/cjs/tx/transfer-policy.js +78 -0
- package/dist/cjs/tx/transfer-policy.js.map +7 -0
- package/dist/cjs/types/index.d.ts +17 -0
- package/dist/cjs/types/index.js +20 -0
- package/dist/cjs/types/index.js.map +7 -0
- package/dist/cjs/types/kiosk.d.ts +160 -0
- package/dist/cjs/types/kiosk.js +37 -0
- package/dist/cjs/types/kiosk.js.map +7 -0
- package/dist/cjs/types/transfer-policy.d.ts +49 -0
- package/dist/cjs/types/transfer-policy.js +35 -0
- package/dist/cjs/types/transfer-policy.js.map +7 -0
- package/dist/cjs/utils.d.ts +51 -0
- package/dist/cjs/utils.js +204 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/esm/bcs.d.ts +2 -0
- package/dist/esm/bcs.js +32 -0
- package/dist/esm/bcs.js.map +7 -0
- package/dist/esm/client/kiosk-client.d.ts +74 -0
- package/dist/esm/client/kiosk-client.js +105 -0
- package/dist/esm/client/kiosk-client.js.map +7 -0
- package/dist/esm/client/kiosk-transaction.d.ts +184 -0
- package/dist/esm/client/kiosk-transaction.js +481 -0
- package/dist/esm/client/kiosk-transaction.js.map +7 -0
- package/dist/esm/client/tp-transaction.d.ts +112 -0
- package/dist/esm/client/tp-transaction.js +314 -0
- package/dist/esm/client/tp-transaction.js.map +7 -0
- package/dist/esm/constants.d.ts +24 -0
- package/dist/esm/constants.js +51 -0
- package/dist/esm/constants.js.map +7 -0
- package/dist/esm/index.d.ts +6 -0
- package/dist/esm/index.js +7 -0
- package/dist/esm/index.js.map +7 -0
- package/dist/esm/package.json +5 -0
- package/dist/esm/query/kiosk.d.ts +14 -0
- package/dist/esm/query/kiosk.js +116 -0
- package/dist/esm/query/kiosk.js.map +7 -0
- package/dist/esm/query/transfer-policy.d.ts +28 -0
- package/dist/esm/query/transfer-policy.js +80 -0
- package/dist/esm/query/transfer-policy.js.map +7 -0
- package/dist/esm/tx/kiosk.d.ts +71 -0
- package/dist/esm/tx/kiosk.js +110 -0
- package/dist/esm/tx/kiosk.js.map +7 -0
- package/dist/esm/tx/personal-kiosk.d.ts +7 -0
- package/dist/esm/tx/personal-kiosk.js +18 -0
- package/dist/esm/tx/personal-kiosk.js.map +7 -0
- package/dist/esm/tx/rules/attach.d.ts +7 -0
- package/dist/esm/tx/rules/attach.js +42 -0
- package/dist/esm/tx/rules/attach.js.map +7 -0
- package/dist/esm/tx/rules/resolve.d.ts +15 -0
- package/dist/esm/tx/rules/resolve.js +61 -0
- package/dist/esm/tx/rules/resolve.js.map +7 -0
- package/dist/esm/tx/transfer-policy.d.ts +29 -0
- package/dist/esm/tx/transfer-policy.js +58 -0
- package/dist/esm/tx/transfer-policy.js.map +7 -0
- package/dist/esm/types/index.d.ts +17 -0
- package/dist/esm/types/index.js +3 -0
- package/dist/esm/types/index.js.map +7 -0
- package/dist/esm/types/kiosk.d.ts +160 -0
- package/dist/esm/types/kiosk.js +17 -0
- package/dist/esm/types/kiosk.js.map +7 -0
- package/dist/esm/types/transfer-policy.d.ts +49 -0
- package/dist/esm/types/transfer-policy.js +15 -0
- package/dist/esm/types/transfer-policy.js.map +7 -0
- package/dist/esm/utils.d.ts +51 -0
- package/dist/esm/utils.js +184 -0
- package/dist/esm/utils.js.map +7 -0
- package/dist/tsconfig.esm.tsbuildinfo +1 -0
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +52 -0
- package/src/bcs.ts +42 -0
- package/src/client/kiosk-client.ts +159 -0
- package/src/client/kiosk-transaction.ts +542 -0
- package/src/client/tp-transaction.ts +360 -0
- package/src/constants.ts +85 -0
- package/src/index.ts +10 -0
- package/src/query/kiosk.ts +190 -0
- package/src/query/transfer-policy.ts +135 -0
- package/src/tx/kiosk.ts +244 -0
- 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 +121 -0
- package/src/types/index.ts +25 -0
- package/src/types/kiosk.ts +179 -0
- package/src/types/transfer-policy.ts +68 -0
- package/src/utils.ts +284 -0
|
@@ -0,0 +1,135 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// Modifications Copyright (c) 2024 IOTA Stiftung
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
import type { IotaClient } from '@iota/iota-sdk/client';
|
|
6
|
+
import { isValidIotaAddress } from '@iota/iota-sdk/utils';
|
|
7
|
+
|
|
8
|
+
import { bcs } from '../bcs.js';
|
|
9
|
+
import type { TransferPolicy, TransferPolicyCap } from '../types/index.js';
|
|
10
|
+
import {
|
|
11
|
+
TRANSFER_POLICY_CAP_TYPE,
|
|
12
|
+
TRANSFER_POLICY_CREATED_EVENT,
|
|
13
|
+
TRANSFER_POLICY_TYPE,
|
|
14
|
+
} from '../types/index.js';
|
|
15
|
+
import { getAllOwnedObjects, parseTransferPolicyCapObject } from '../utils.js';
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* Searches the `TransferPolicy`-s for the given type. The search is performed via
|
|
19
|
+
* the `TransferPolicyCreated` event. The policy can either be owned or shared,
|
|
20
|
+
* and the caller needs to filter the results accordingly (ie single owner can not
|
|
21
|
+
* be accessed by anyone but the owner).
|
|
22
|
+
*
|
|
23
|
+
* @param provider
|
|
24
|
+
* @param type
|
|
25
|
+
*/
|
|
26
|
+
export async function queryTransferPolicy(
|
|
27
|
+
client: IotaClient,
|
|
28
|
+
type: string,
|
|
29
|
+
): Promise<TransferPolicy[]> {
|
|
30
|
+
// console.log('event type: %s', `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`);
|
|
31
|
+
const { data } = await client.queryEvents({
|
|
32
|
+
query: {
|
|
33
|
+
MoveEventType: `${TRANSFER_POLICY_CREATED_EVENT}<${type}>`,
|
|
34
|
+
},
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const search = data.map((event) => event.parsedJson as { id: string });
|
|
38
|
+
const policies = await client.multiGetObjects({
|
|
39
|
+
ids: search.map((policy) => policy.id),
|
|
40
|
+
options: { showBcs: true, showOwner: true },
|
|
41
|
+
});
|
|
42
|
+
|
|
43
|
+
return policies
|
|
44
|
+
.filter((policy) => !!policy && 'data' in policy)
|
|
45
|
+
.map(({ data: policy }) => {
|
|
46
|
+
// should never happen; policies are objects and fetched via an event.
|
|
47
|
+
// policies are filtered for null and undefined above.
|
|
48
|
+
if (!policy || !policy.bcs || !('bcsBytes' in policy.bcs)) {
|
|
49
|
+
throw new Error(
|
|
50
|
+
`Invalid policy: ${policy?.objectId}, expected object, got package`,
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
const parsed = bcs.de(TRANSFER_POLICY_TYPE, policy.bcs.bcsBytes, 'base64');
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
id: policy?.objectId,
|
|
58
|
+
type: `${TRANSFER_POLICY_TYPE}<${type}>`,
|
|
59
|
+
owner: policy?.owner,
|
|
60
|
+
rules: parsed.rules,
|
|
61
|
+
balance: parsed.balance,
|
|
62
|
+
} as TransferPolicy;
|
|
63
|
+
});
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/**
|
|
67
|
+
* A function to fetch all the user's kiosk Caps
|
|
68
|
+
* And a list of the kiosk address ids.
|
|
69
|
+
* Returns a list of `kioskOwnerCapIds` and `kioskIds`.
|
|
70
|
+
* Extra options allow pagination.
|
|
71
|
+
* @returns TransferPolicyCap Object ID | undefined if not found.
|
|
72
|
+
*/
|
|
73
|
+
export async function queryTransferPolicyCapsByType(
|
|
74
|
+
client: IotaClient,
|
|
75
|
+
address: string,
|
|
76
|
+
type: string,
|
|
77
|
+
): Promise<TransferPolicyCap[]> {
|
|
78
|
+
if (!isValidIotaAddress(address)) return [];
|
|
79
|
+
|
|
80
|
+
const filter = {
|
|
81
|
+
MatchAll: [
|
|
82
|
+
{
|
|
83
|
+
StructType: `${TRANSFER_POLICY_CAP_TYPE}<${type}>`,
|
|
84
|
+
},
|
|
85
|
+
],
|
|
86
|
+
};
|
|
87
|
+
|
|
88
|
+
// fetch owned kiosk caps, paginated.
|
|
89
|
+
const data = await getAllOwnedObjects({
|
|
90
|
+
client,
|
|
91
|
+
filter,
|
|
92
|
+
owner: address,
|
|
93
|
+
});
|
|
94
|
+
|
|
95
|
+
return data
|
|
96
|
+
.map((item) => parseTransferPolicyCapObject(item))
|
|
97
|
+
.filter((item) => !!item) as TransferPolicyCap[];
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
/**
|
|
101
|
+
* A function to fetch all the user's kiosk Caps
|
|
102
|
+
* And a list of the kiosk address ids.
|
|
103
|
+
* Returns a list of `kioskOwnerCapIds` and `kioskIds`.
|
|
104
|
+
* Extra options allow pagination.
|
|
105
|
+
* @returns TransferPolicyCap Object ID | undefined if not found.
|
|
106
|
+
*/
|
|
107
|
+
export async function queryOwnedTransferPolicies(
|
|
108
|
+
client: IotaClient,
|
|
109
|
+
address: string,
|
|
110
|
+
): Promise<TransferPolicyCap[] | undefined> {
|
|
111
|
+
if (!isValidIotaAddress(address)) return;
|
|
112
|
+
|
|
113
|
+
const filter = {
|
|
114
|
+
MatchAll: [
|
|
115
|
+
{
|
|
116
|
+
MoveModule: {
|
|
117
|
+
module: 'transfer_policy',
|
|
118
|
+
package: '0x2',
|
|
119
|
+
},
|
|
120
|
+
},
|
|
121
|
+
],
|
|
122
|
+
};
|
|
123
|
+
|
|
124
|
+
// fetch all owned kiosk caps, paginated.
|
|
125
|
+
const data = await getAllOwnedObjects({ client, owner: address, filter });
|
|
126
|
+
|
|
127
|
+
const policies: TransferPolicyCap[] = [];
|
|
128
|
+
|
|
129
|
+
for (const item of data) {
|
|
130
|
+
const data = parseTransferPolicyCapObject(item);
|
|
131
|
+
if (data) policies.push(data);
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
return policies;
|
|
135
|
+
}
|
package/src/tx/kiosk.ts
ADDED
|
@@ -0,0 +1,244 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// Modifications Copyright (c) 2024 IOTA Stiftung
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
import { bcs } from '@iota/iota-sdk/bcs';
|
|
6
|
+
import type {
|
|
7
|
+
TransactionArgument,
|
|
8
|
+
TransactionBlock,
|
|
9
|
+
TransactionObjectArgument,
|
|
10
|
+
} from '@iota/iota-sdk/transactions';
|
|
11
|
+
|
|
12
|
+
import type { ObjectArgument } from '../types/index.js';
|
|
13
|
+
import { KIOSK_MODULE, KIOSK_TYPE } from '../types/index.js';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Create a new shared Kiosk and returns the [kiosk, kioskOwnerCap] tuple.
|
|
17
|
+
*/
|
|
18
|
+
export function createKiosk(
|
|
19
|
+
tx: TransactionBlock,
|
|
20
|
+
): [TransactionObjectArgument, TransactionObjectArgument] {
|
|
21
|
+
const [kiosk, kioskOwnerCap] = tx.moveCall({
|
|
22
|
+
target: `${KIOSK_MODULE}::new`,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
return [kiosk, kioskOwnerCap];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Calls the `kiosk::new()` function and shares the kiosk.
|
|
30
|
+
* Returns the `kioskOwnerCap` object.
|
|
31
|
+
*/
|
|
32
|
+
export function createKioskAndShare(tx: TransactionBlock): TransactionObjectArgument {
|
|
33
|
+
const [kiosk, kioskOwnerCap] = createKiosk(tx);
|
|
34
|
+
shareKiosk(tx, kiosk);
|
|
35
|
+
return kioskOwnerCap;
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Converts Transfer Policy to a shared object.
|
|
40
|
+
*/
|
|
41
|
+
export function shareKiosk(tx: TransactionBlock, kiosk: TransactionArgument) {
|
|
42
|
+
tx.moveCall({
|
|
43
|
+
target: `0x2::transfer::public_share_object`,
|
|
44
|
+
typeArguments: [KIOSK_TYPE],
|
|
45
|
+
arguments: [kiosk],
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
/**
|
|
50
|
+
* Call the `kiosk::place<T>(Kiosk, KioskOwnerCap, Item)` function.
|
|
51
|
+
* Place an item to the Kiosk.
|
|
52
|
+
*/
|
|
53
|
+
export function place(
|
|
54
|
+
tx: TransactionBlock,
|
|
55
|
+
itemType: string,
|
|
56
|
+
kiosk: ObjectArgument,
|
|
57
|
+
kioskCap: ObjectArgument,
|
|
58
|
+
item: ObjectArgument,
|
|
59
|
+
): void {
|
|
60
|
+
tx.moveCall({
|
|
61
|
+
target: `${KIOSK_MODULE}::place`,
|
|
62
|
+
typeArguments: [itemType],
|
|
63
|
+
arguments: [tx.object(kiosk), tx.object(kioskCap), tx.object(item)],
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Call the `kiosk::lock<T>(Kiosk, KioskOwnerCap, TransferPolicy, Item)`
|
|
69
|
+
* function. Lock an item in the Kiosk.
|
|
70
|
+
*
|
|
71
|
+
* Unlike `place` this function requires a `TransferPolicy` to exist
|
|
72
|
+
* and be passed in. This is done to make sure the item does not get
|
|
73
|
+
* locked without an option to take it out.
|
|
74
|
+
*/
|
|
75
|
+
export function lock(
|
|
76
|
+
tx: TransactionBlock,
|
|
77
|
+
itemType: string,
|
|
78
|
+
kiosk: ObjectArgument,
|
|
79
|
+
kioskCap: ObjectArgument,
|
|
80
|
+
policy: ObjectArgument,
|
|
81
|
+
item: ObjectArgument,
|
|
82
|
+
): void {
|
|
83
|
+
tx.moveCall({
|
|
84
|
+
target: `${KIOSK_MODULE}::lock`,
|
|
85
|
+
typeArguments: [itemType],
|
|
86
|
+
arguments: [tx.object(kiosk), tx.object(kioskCap), tx.object(policy), tx.object(item)],
|
|
87
|
+
});
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
/**
|
|
91
|
+
* Call the `kiosk::take<T>(Kiosk, KioskOwnerCap, ID)` function.
|
|
92
|
+
* Take an item from the Kiosk.
|
|
93
|
+
*/
|
|
94
|
+
export function take(
|
|
95
|
+
tx: TransactionBlock,
|
|
96
|
+
itemType: string,
|
|
97
|
+
kiosk: ObjectArgument,
|
|
98
|
+
kioskCap: ObjectArgument,
|
|
99
|
+
itemId: string,
|
|
100
|
+
): TransactionObjectArgument {
|
|
101
|
+
const [item] = tx.moveCall({
|
|
102
|
+
target: `${KIOSK_MODULE}::take`,
|
|
103
|
+
typeArguments: [itemType],
|
|
104
|
+
arguments: [tx.object(kiosk), tx.object(kioskCap), tx.pure.id(itemId)],
|
|
105
|
+
});
|
|
106
|
+
|
|
107
|
+
return item;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Call the `kiosk::list<T>(Kiosk, KioskOwnerCap, ID, u64)` function.
|
|
112
|
+
* List an item for sale.
|
|
113
|
+
*/
|
|
114
|
+
export function list(
|
|
115
|
+
tx: TransactionBlock,
|
|
116
|
+
itemType: string,
|
|
117
|
+
kiosk: ObjectArgument,
|
|
118
|
+
kioskCap: ObjectArgument,
|
|
119
|
+
itemId: string,
|
|
120
|
+
price: string | bigint,
|
|
121
|
+
): void {
|
|
122
|
+
tx.moveCall({
|
|
123
|
+
target: `${KIOSK_MODULE}::list`,
|
|
124
|
+
typeArguments: [itemType],
|
|
125
|
+
arguments: [tx.object(kiosk), tx.object(kioskCap), tx.pure.id(itemId), tx.pure.u64(price)],
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
|
|
129
|
+
/**
|
|
130
|
+
* Call the `kiosk::list<T>(Kiosk, KioskOwnerCap, ID, u64)` function.
|
|
131
|
+
* List an item for sale.
|
|
132
|
+
*/
|
|
133
|
+
export function delist(
|
|
134
|
+
tx: TransactionBlock,
|
|
135
|
+
itemType: string,
|
|
136
|
+
kiosk: ObjectArgument,
|
|
137
|
+
kioskCap: ObjectArgument,
|
|
138
|
+
itemId: string,
|
|
139
|
+
): void {
|
|
140
|
+
tx.moveCall({
|
|
141
|
+
target: `${KIOSK_MODULE}::delist`,
|
|
142
|
+
typeArguments: [itemType],
|
|
143
|
+
arguments: [tx.object(kiosk), tx.object(kioskCap), tx.pure.id(itemId)],
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
/**
|
|
148
|
+
* Call the `kiosk::place_and_list<T>(Kiosk, KioskOwnerCap, Item, u64)` function.
|
|
149
|
+
* Place an item to the Kiosk and list it for sale.
|
|
150
|
+
*/
|
|
151
|
+
export function placeAndList(
|
|
152
|
+
tx: TransactionBlock,
|
|
153
|
+
itemType: string,
|
|
154
|
+
kiosk: ObjectArgument,
|
|
155
|
+
kioskCap: ObjectArgument,
|
|
156
|
+
item: ObjectArgument,
|
|
157
|
+
price: string | bigint,
|
|
158
|
+
): void {
|
|
159
|
+
tx.moveCall({
|
|
160
|
+
target: `${KIOSK_MODULE}::place_and_list`,
|
|
161
|
+
typeArguments: [itemType],
|
|
162
|
+
arguments: [tx.object(kiosk), tx.object(kioskCap), tx.object(item), tx.pure.u64(price)],
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
|
|
166
|
+
/**
|
|
167
|
+
* Call the `kiosk::purchase<T>(Kiosk, ID, Coin<IOTA>)` function and receive an Item and
|
|
168
|
+
* a TransferRequest which needs to be dealt with (via a matching TransferPolicy).
|
|
169
|
+
*/
|
|
170
|
+
export function purchase(
|
|
171
|
+
tx: TransactionBlock,
|
|
172
|
+
itemType: string,
|
|
173
|
+
kiosk: ObjectArgument,
|
|
174
|
+
itemId: string,
|
|
175
|
+
payment: ObjectArgument,
|
|
176
|
+
): [TransactionObjectArgument, TransactionObjectArgument] {
|
|
177
|
+
const [item, transferRequest] = tx.moveCall({
|
|
178
|
+
target: `${KIOSK_MODULE}::purchase`,
|
|
179
|
+
typeArguments: [itemType],
|
|
180
|
+
arguments: [tx.object(kiosk), tx.pure.id(itemId), tx.object(payment)],
|
|
181
|
+
});
|
|
182
|
+
|
|
183
|
+
return [item, transferRequest];
|
|
184
|
+
}
|
|
185
|
+
|
|
186
|
+
/**
|
|
187
|
+
* Call the `kiosk::withdraw(Kiosk, KioskOwnerCap, Option<u64>)` function and receive a Coin<IOTA>.
|
|
188
|
+
* If the amount is null, then the entire balance will be withdrawn.
|
|
189
|
+
*/
|
|
190
|
+
export function withdrawFromKiosk(
|
|
191
|
+
tx: TransactionBlock,
|
|
192
|
+
kiosk: ObjectArgument,
|
|
193
|
+
kioskCap: ObjectArgument,
|
|
194
|
+
amount?: string | bigint | number,
|
|
195
|
+
): TransactionObjectArgument {
|
|
196
|
+
const amountArg = bcs.option(bcs.u64()).serialize(amount);
|
|
197
|
+
|
|
198
|
+
const [coin] = tx.moveCall({
|
|
199
|
+
target: `${KIOSK_MODULE}::withdraw`,
|
|
200
|
+
arguments: [tx.object(kiosk), tx.object(kioskCap), amountArg],
|
|
201
|
+
});
|
|
202
|
+
|
|
203
|
+
return coin;
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
/**
|
|
207
|
+
* Call the `kiosk::borrow_value<T>(Kiosk, KioskOwnerCap, ID): T` function.
|
|
208
|
+
* Immutably borrow an item from the Kiosk and return it in the end.
|
|
209
|
+
*
|
|
210
|
+
* Requires calling `returnValue` to return the item.
|
|
211
|
+
*/
|
|
212
|
+
export function borrowValue(
|
|
213
|
+
tx: TransactionBlock,
|
|
214
|
+
itemType: string,
|
|
215
|
+
kiosk: ObjectArgument,
|
|
216
|
+
kioskCap: ObjectArgument,
|
|
217
|
+
itemId: string,
|
|
218
|
+
): [TransactionArgument, TransactionArgument] {
|
|
219
|
+
const [item, promise] = tx.moveCall({
|
|
220
|
+
target: `${KIOSK_MODULE}::borrow_val`,
|
|
221
|
+
typeArguments: [itemType],
|
|
222
|
+
arguments: [tx.object(kiosk), tx.object(kioskCap), tx.pure.id(itemId)],
|
|
223
|
+
});
|
|
224
|
+
|
|
225
|
+
return [item, promise];
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
/**
|
|
229
|
+
* Call the `kiosk::return_value<T>(Kiosk, Item, Borrow)` function.
|
|
230
|
+
* Return an item to the Kiosk after it was `borrowValue`-d.
|
|
231
|
+
*/
|
|
232
|
+
export function returnValue(
|
|
233
|
+
tx: TransactionBlock,
|
|
234
|
+
itemType: string,
|
|
235
|
+
kiosk: ObjectArgument,
|
|
236
|
+
item: TransactionArgument,
|
|
237
|
+
promise: TransactionArgument,
|
|
238
|
+
): void {
|
|
239
|
+
tx.moveCall({
|
|
240
|
+
target: `${KIOSK_MODULE}::return_val`,
|
|
241
|
+
typeArguments: [itemType],
|
|
242
|
+
arguments: [tx.object(kiosk), item, promise],
|
|
243
|
+
});
|
|
244
|
+
}
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// Modifications Copyright (c) 2024 IOTA Stiftung
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
import type { TransactionBlock, TransactionObjectArgument } from '@iota/iota-sdk/transactions';
|
|
6
|
+
|
|
7
|
+
import type { ObjectArgument } from '../types/index.js';
|
|
8
|
+
|
|
9
|
+
export function convertToPersonalTx(
|
|
10
|
+
tx: TransactionBlock,
|
|
11
|
+
kiosk: ObjectArgument,
|
|
12
|
+
kioskOwnerCap: ObjectArgument,
|
|
13
|
+
packageId: string,
|
|
14
|
+
): TransactionObjectArgument {
|
|
15
|
+
const personalKioskCap = tx.moveCall({
|
|
16
|
+
target: `${packageId}::personal_kiosk::new`,
|
|
17
|
+
arguments: [tx.object(kiosk), tx.object(kioskOwnerCap)],
|
|
18
|
+
});
|
|
19
|
+
|
|
20
|
+
return personalKioskCap;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* Transfers the personal kiosk Cap to the sender.
|
|
25
|
+
*/
|
|
26
|
+
export function transferPersonalCapTx(
|
|
27
|
+
tx: TransactionBlock,
|
|
28
|
+
personalKioskCap: TransactionObjectArgument,
|
|
29
|
+
packageId: string,
|
|
30
|
+
) {
|
|
31
|
+
tx.moveCall({
|
|
32
|
+
target: `${packageId}::personal_kiosk::transfer_to_sender`,
|
|
33
|
+
arguments: [personalKioskCap],
|
|
34
|
+
});
|
|
35
|
+
}
|
|
@@ -0,0 +1,74 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// Modifications Copyright (c) 2024 IOTA Stiftung
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
import type { TransactionBlock } from '@iota/iota-sdk/transactions';
|
|
6
|
+
|
|
7
|
+
import type { ObjectArgument } from '../../types/index.js';
|
|
8
|
+
|
|
9
|
+
export function attachKioskLockRuleTx(
|
|
10
|
+
tx: TransactionBlock,
|
|
11
|
+
type: string,
|
|
12
|
+
policy: ObjectArgument,
|
|
13
|
+
policyCap: ObjectArgument,
|
|
14
|
+
packageId: string,
|
|
15
|
+
) {
|
|
16
|
+
tx.moveCall({
|
|
17
|
+
target: `${packageId}::kiosk_lock_rule::add`,
|
|
18
|
+
typeArguments: [type],
|
|
19
|
+
arguments: [tx.object(policy), tx.object(policyCap)],
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function attachRoyaltyRuleTx(
|
|
24
|
+
tx: TransactionBlock,
|
|
25
|
+
type: string,
|
|
26
|
+
policy: ObjectArgument,
|
|
27
|
+
policyCap: ObjectArgument,
|
|
28
|
+
percentageBps: number | string, // this is in basis points.
|
|
29
|
+
minAmount: number | string,
|
|
30
|
+
packageId: string,
|
|
31
|
+
) {
|
|
32
|
+
if (Number(percentageBps) < 0 || Number(percentageBps) > 10_000)
|
|
33
|
+
throw new Error('Invalid basis point percentage. Use a value between [0,10000].');
|
|
34
|
+
|
|
35
|
+
tx.moveCall({
|
|
36
|
+
target: `${packageId}::royalty_rule::add`,
|
|
37
|
+
typeArguments: [type],
|
|
38
|
+
arguments: [
|
|
39
|
+
tx.object(policy),
|
|
40
|
+
tx.object(policyCap),
|
|
41
|
+
tx.pure.u16(Number(percentageBps)),
|
|
42
|
+
tx.pure.u64(minAmount),
|
|
43
|
+
],
|
|
44
|
+
});
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
export function attachPersonalKioskRuleTx(
|
|
48
|
+
tx: TransactionBlock,
|
|
49
|
+
type: string,
|
|
50
|
+
policy: ObjectArgument,
|
|
51
|
+
policyCap: ObjectArgument,
|
|
52
|
+
packageId: string,
|
|
53
|
+
) {
|
|
54
|
+
tx.moveCall({
|
|
55
|
+
target: `${packageId}::personal_kiosk_rule::add`,
|
|
56
|
+
typeArguments: [type],
|
|
57
|
+
arguments: [tx.object(policy), tx.object(policyCap)],
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
export function attachFloorPriceRuleTx(
|
|
62
|
+
tx: TransactionBlock,
|
|
63
|
+
type: string,
|
|
64
|
+
policy: ObjectArgument,
|
|
65
|
+
policyCap: ObjectArgument,
|
|
66
|
+
minPrice: string | bigint,
|
|
67
|
+
packageId: string,
|
|
68
|
+
) {
|
|
69
|
+
tx.moveCall({
|
|
70
|
+
target: `${packageId}::floor_price_rule::add`,
|
|
71
|
+
typeArguments: [type],
|
|
72
|
+
arguments: [tx.object(policy), tx.object(policyCap), tx.pure.u64(minPrice)],
|
|
73
|
+
});
|
|
74
|
+
}
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// Modifications Copyright (c) 2024 IOTA Stiftung
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
import type { RuleResolvingParams } from '../../types/index.js';
|
|
6
|
+
import { lock } from '../kiosk.js';
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A helper to resolve the royalty rule.
|
|
10
|
+
*/
|
|
11
|
+
export function resolveRoyaltyRule(params: RuleResolvingParams) {
|
|
12
|
+
const { transactionBlock: txb, itemType, price, packageId, transferRequest, policyId } = params;
|
|
13
|
+
|
|
14
|
+
const policyObj = txb.object(policyId);
|
|
15
|
+
|
|
16
|
+
// calculates the amount
|
|
17
|
+
const [amount] = txb.moveCall({
|
|
18
|
+
target: `${packageId}::royalty_rule::fee_amount`,
|
|
19
|
+
typeArguments: [itemType],
|
|
20
|
+
arguments: [policyObj, txb.pure.u64(price || '0')],
|
|
21
|
+
});
|
|
22
|
+
|
|
23
|
+
// splits the coin.
|
|
24
|
+
const feeCoin = txb.splitCoins(txb.gas, [amount]);
|
|
25
|
+
|
|
26
|
+
// pays the policy
|
|
27
|
+
txb.moveCall({
|
|
28
|
+
target: `${packageId}::royalty_rule::pay`,
|
|
29
|
+
typeArguments: [itemType],
|
|
30
|
+
arguments: [policyObj, transferRequest, feeCoin],
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function resolveKioskLockRule(params: RuleResolvingParams) {
|
|
35
|
+
const {
|
|
36
|
+
transactionBlock: txb,
|
|
37
|
+
packageId,
|
|
38
|
+
itemType,
|
|
39
|
+
kiosk,
|
|
40
|
+
kioskCap,
|
|
41
|
+
policyId,
|
|
42
|
+
purchasedItem,
|
|
43
|
+
transferRequest,
|
|
44
|
+
} = params;
|
|
45
|
+
|
|
46
|
+
if (!kiosk || !kioskCap) throw new Error('Missing Owned Kiosk or Owned Kiosk Cap');
|
|
47
|
+
|
|
48
|
+
lock(txb, itemType, kiosk, kioskCap, policyId, purchasedItem);
|
|
49
|
+
|
|
50
|
+
// proves that the item is locked in the kiosk to the TP.
|
|
51
|
+
txb.moveCall({
|
|
52
|
+
target: `${packageId}::kiosk_lock_rule::prove`,
|
|
53
|
+
typeArguments: [itemType],
|
|
54
|
+
arguments: [transferRequest, txb.object(kiosk)],
|
|
55
|
+
});
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
/**
|
|
59
|
+
* A helper to resolve the personalKioskRule.
|
|
60
|
+
* @param params
|
|
61
|
+
*/
|
|
62
|
+
export function resolvePersonalKioskRule(params: RuleResolvingParams) {
|
|
63
|
+
const { transactionBlock: txb, packageId, itemType, kiosk, transferRequest } = params;
|
|
64
|
+
|
|
65
|
+
if (!kiosk) throw new Error('Missing owned Kiosk.');
|
|
66
|
+
|
|
67
|
+
// proves that the destination kiosk is personal.
|
|
68
|
+
txb.moveCall({
|
|
69
|
+
target: `${packageId}::personal_kiosk_rule::prove`,
|
|
70
|
+
typeArguments: [itemType],
|
|
71
|
+
arguments: [txb.object(kiosk), transferRequest],
|
|
72
|
+
});
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Resolves the floor price rule.
|
|
77
|
+
*/
|
|
78
|
+
export function resolveFloorPriceRule(params: RuleResolvingParams) {
|
|
79
|
+
const { transactionBlock: txb, packageId, itemType, policyId, transferRequest } = params;
|
|
80
|
+
|
|
81
|
+
// proves that the destination kiosk is personal
|
|
82
|
+
txb.moveCall({
|
|
83
|
+
target: `${packageId}::floor_price_rule::prove`,
|
|
84
|
+
typeArguments: [itemType],
|
|
85
|
+
arguments: [txb.object(policyId), transferRequest],
|
|
86
|
+
});
|
|
87
|
+
}
|
|
@@ -0,0 +1,121 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// Modifications Copyright (c) 2024 IOTA Stiftung
|
|
3
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
4
|
+
|
|
5
|
+
import { bcs } from '@iota/iota-sdk/bcs';
|
|
6
|
+
import type {
|
|
7
|
+
TransactionArgument,
|
|
8
|
+
TransactionBlock,
|
|
9
|
+
TransactionObjectArgument,
|
|
10
|
+
} from '@iota/iota-sdk/transactions';
|
|
11
|
+
|
|
12
|
+
import type { ObjectArgument } from '../types/index.js';
|
|
13
|
+
import { TRANSFER_POLICY_MODULE, TRANSFER_POLICY_TYPE } from '../types/index.js';
|
|
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
|
+
): TransactionObjectArgument {
|
|
24
|
+
const [transferPolicy, transferPolicyCap] = createTransferPolicyWithoutSharing(
|
|
25
|
+
tx,
|
|
26
|
+
itemType,
|
|
27
|
+
publisher,
|
|
28
|
+
);
|
|
29
|
+
|
|
30
|
+
shareTransferPolicy(tx, itemType, transferPolicy);
|
|
31
|
+
|
|
32
|
+
return transferPolicyCap;
|
|
33
|
+
}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Creates a transfer Policy and returns both the Policy and the Cap.
|
|
37
|
+
* Used if we want to use the policy before making it a shared object.
|
|
38
|
+
*/
|
|
39
|
+
export function createTransferPolicyWithoutSharing(
|
|
40
|
+
tx: TransactionBlock,
|
|
41
|
+
itemType: string,
|
|
42
|
+
publisher: ObjectArgument,
|
|
43
|
+
): [TransactionObjectArgument, TransactionObjectArgument] {
|
|
44
|
+
const [transferPolicy, transferPolicyCap] = tx.moveCall({
|
|
45
|
+
target: `${TRANSFER_POLICY_MODULE}::new`,
|
|
46
|
+
typeArguments: [itemType],
|
|
47
|
+
arguments: [tx.object(publisher)],
|
|
48
|
+
});
|
|
49
|
+
|
|
50
|
+
return [transferPolicy, transferPolicyCap];
|
|
51
|
+
}
|
|
52
|
+
/**
|
|
53
|
+
* Converts Transfer Policy to a shared object.
|
|
54
|
+
*/
|
|
55
|
+
export function shareTransferPolicy(
|
|
56
|
+
tx: TransactionBlock,
|
|
57
|
+
itemType: string,
|
|
58
|
+
transferPolicy: TransactionObjectArgument,
|
|
59
|
+
) {
|
|
60
|
+
tx.moveCall({
|
|
61
|
+
target: `0x2::transfer::public_share_object`,
|
|
62
|
+
typeArguments: [`${TRANSFER_POLICY_TYPE}<${itemType}>`],
|
|
63
|
+
arguments: [transferPolicy],
|
|
64
|
+
});
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* Call the `transfer_policy::withdraw` function to withdraw profits from a transfer policy.
|
|
69
|
+
*/
|
|
70
|
+
export function withdrawFromPolicy(
|
|
71
|
+
tx: TransactionBlock,
|
|
72
|
+
itemType: string,
|
|
73
|
+
policy: ObjectArgument,
|
|
74
|
+
policyCap: ObjectArgument,
|
|
75
|
+
amount?: string | bigint | null,
|
|
76
|
+
): TransactionObjectArgument {
|
|
77
|
+
const amountArg = bcs.option(bcs.u64()).serialize(amount);
|
|
78
|
+
|
|
79
|
+
const [profits] = tx.moveCall({
|
|
80
|
+
target: `${TRANSFER_POLICY_MODULE}::withdraw`,
|
|
81
|
+
typeArguments: [itemType],
|
|
82
|
+
arguments: [tx.object(policy), tx.object(policyCap), amountArg],
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
return profits;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Call the `transfer_policy::confirm_request` function to unblock the
|
|
90
|
+
* transaction.
|
|
91
|
+
*/
|
|
92
|
+
export function confirmRequest(
|
|
93
|
+
tx: TransactionBlock,
|
|
94
|
+
itemType: string,
|
|
95
|
+
policy: ObjectArgument,
|
|
96
|
+
request: TransactionArgument,
|
|
97
|
+
): void {
|
|
98
|
+
tx.moveCall({
|
|
99
|
+
target: `${TRANSFER_POLICY_MODULE}::confirm_request`,
|
|
100
|
+
typeArguments: [itemType],
|
|
101
|
+
arguments: [tx.object(policy), request],
|
|
102
|
+
});
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Calls the `transfer_policy::remove_rule` function to remove a Rule from the transfer policy's ruleset.
|
|
107
|
+
*/
|
|
108
|
+
export function removeTransferPolicyRule(
|
|
109
|
+
tx: TransactionBlock,
|
|
110
|
+
itemType: string,
|
|
111
|
+
ruleType: string,
|
|
112
|
+
configType: string,
|
|
113
|
+
policy: ObjectArgument,
|
|
114
|
+
policyCap: ObjectArgument,
|
|
115
|
+
): void {
|
|
116
|
+
tx.moveCall({
|
|
117
|
+
target: `${TRANSFER_POLICY_MODULE}::remove_rule`,
|
|
118
|
+
typeArguments: [itemType, ruleType, configType],
|
|
119
|
+
arguments: [tx.object(policy), tx.object(policyCap)],
|
|
120
|
+
});
|
|
121
|
+
}
|