@haneullabs/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 +1070 -0
- package/README.md +8 -0
- package/dist/cjs/bcs.d.ts +23 -0
- package/dist/cjs/bcs.js +50 -0
- package/dist/cjs/bcs.js.map +7 -0
- package/dist/cjs/client/kiosk-client.d.ts +76 -0
- package/dist/cjs/client/kiosk-client.js +123 -0
- package/dist/cjs/client/kiosk-client.js.map +7 -0
- package/dist/cjs/client/kiosk-transaction.d.ts +186 -0
- package/dist/cjs/client/kiosk-transaction.js +462 -0
- package/dist/cjs/client/kiosk-transaction.js.map +7 -0
- package/dist/cjs/client/tp-transaction.d.ts +114 -0
- package/dist/cjs/client/tp-transaction.js +307 -0
- package/dist/cjs/client/tp-transaction.js.map +7 -0
- package/dist/cjs/constants.d.ts +31 -0
- package/dist/cjs/constants.js +102 -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 +8 -0
- package/dist/cjs/query/kiosk.js +181 -0
- package/dist/cjs/query/kiosk.js.map +7 -0
- package/dist/cjs/query/transfer-policy.d.ts +29 -0
- package/dist/cjs/query/transfer-policy.js +92 -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 +109 -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 +27 -0
- package/dist/cjs/types/index.js +33 -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 +53 -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 +198 -0
- package/dist/cjs/utils.js.map +7 -0
- package/dist/esm/bcs.d.ts +23 -0
- package/dist/esm/bcs.js +35 -0
- package/dist/esm/bcs.js.map +7 -0
- package/dist/esm/client/kiosk-client.d.ts +76 -0
- package/dist/esm/client/kiosk-client.js +114 -0
- package/dist/esm/client/kiosk-client.js.map +7 -0
- package/dist/esm/client/kiosk-transaction.d.ts +186 -0
- package/dist/esm/client/kiosk-transaction.js +432 -0
- package/dist/esm/client/kiosk-transaction.js.map +7 -0
- package/dist/esm/client/tp-transaction.d.ts +114 -0
- package/dist/esm/client/tp-transaction.js +298 -0
- package/dist/esm/client/tp-transaction.js.map +7 -0
- package/dist/esm/constants.d.ts +31 -0
- package/dist/esm/constants.js +87 -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 +8 -0
- package/dist/esm/query/kiosk.js +169 -0
- package/dist/esm/query/kiosk.js.map +7 -0
- package/dist/esm/query/transfer-policy.d.ts +29 -0
- package/dist/esm/query/transfer-policy.js +76 -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 +89 -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 +27 -0
- package/dist/esm/types/index.js +12 -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 +53 -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 +183 -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 +59 -0
- package/src/bcs.ts +39 -0
- package/src/client/kiosk-client.ts +163 -0
- package/src/client/kiosk-transaction.ts +526 -0
- package/src/client/tp-transaction.ts +357 -0
- package/src/constants.ts +121 -0
- package/src/index.ts +9 -0
- package/src/query/kiosk.ts +264 -0
- package/src/query/transfer-policy.ts +134 -0
- package/src/tx/kiosk.ts +243 -0
- package/src/tx/personal-kiosk.ts +34 -0
- package/src/tx/rules/attach.ts +73 -0
- package/src/tx/rules/resolve.ts +126 -0
- package/src/tx/transfer-policy.ts +120 -0
- package/src/types/index.ts +35 -0
- package/src/types/kiosk.ts +178 -0
- package/src/types/transfer-policy.ts +71 -0
- package/src/utils.ts +286 -0
|
@@ -0,0 +1,163 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type { PaginationArguments, HaneulClient } from '@haneullabs/haneul/client';
|
|
5
|
+
|
|
6
|
+
import {
|
|
7
|
+
FLOOR_PRICE_RULE_ADDRESS,
|
|
8
|
+
getBaseRules,
|
|
9
|
+
KIOSK_LOCK_RULE_ADDRESS,
|
|
10
|
+
PERSONAL_KIOSK_RULE_ADDRESS,
|
|
11
|
+
ROYALTY_RULE_ADDRESS,
|
|
12
|
+
rules,
|
|
13
|
+
} from '../constants.js';
|
|
14
|
+
import type { BaseRulePackageIds, TransferPolicyRule } from '../constants.js';
|
|
15
|
+
import { fetchKiosk, fetchKioskExtension, getOwnedKiosks } from '../query/kiosk.js';
|
|
16
|
+
import {
|
|
17
|
+
queryOwnedTransferPolicies,
|
|
18
|
+
queryTransferPolicy,
|
|
19
|
+
queryTransferPolicyCapsByType,
|
|
20
|
+
} from '../query/transfer-policy.js';
|
|
21
|
+
import { Network } from '../types/index.js';
|
|
22
|
+
import type {
|
|
23
|
+
FetchKioskOptions,
|
|
24
|
+
KioskClientOptions,
|
|
25
|
+
KioskData,
|
|
26
|
+
OwnedKiosks,
|
|
27
|
+
} from '../types/index.js';
|
|
28
|
+
|
|
29
|
+
/**
|
|
30
|
+
* A Client that allows you to interact with kiosk.
|
|
31
|
+
* Offers utilities to query kiosk, craft transactions to edit your own kiosk,
|
|
32
|
+
* purchase, manage transfer policies, create new kiosks etc.
|
|
33
|
+
* If you pass packageIds, all functionality will be managed using these packages.
|
|
34
|
+
*/
|
|
35
|
+
export class KioskClient {
|
|
36
|
+
client: HaneulClient;
|
|
37
|
+
network: Network;
|
|
38
|
+
rules: TransferPolicyRule[];
|
|
39
|
+
packageIds?: BaseRulePackageIds;
|
|
40
|
+
|
|
41
|
+
constructor(options: KioskClientOptions) {
|
|
42
|
+
this.client = options.client;
|
|
43
|
+
this.network = options.network;
|
|
44
|
+
this.rules = rules; // add all the default rules.
|
|
45
|
+
this.packageIds = options.packageIds;
|
|
46
|
+
|
|
47
|
+
// Add the custom Package Ids too on the rule list.
|
|
48
|
+
// Only adds the rules that are passed in the packageId object.
|
|
49
|
+
if (options.packageIds) this.rules.push(...getBaseRules(options.packageIds));
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
/// Querying
|
|
53
|
+
|
|
54
|
+
/**
|
|
55
|
+
* Get an addresses's owned kiosks.
|
|
56
|
+
* @param address The address for which we want to retrieve the kiosks.
|
|
57
|
+
* @param pagination Optional pagination arguments.
|
|
58
|
+
* @returns An Object containing all the `kioskOwnerCap` objects as well as the kioskIds.
|
|
59
|
+
*/
|
|
60
|
+
async getOwnedKiosks({
|
|
61
|
+
address,
|
|
62
|
+
pagination,
|
|
63
|
+
}: {
|
|
64
|
+
address: string;
|
|
65
|
+
pagination?: PaginationArguments<string>;
|
|
66
|
+
}): Promise<OwnedKiosks> {
|
|
67
|
+
const personalPackageId =
|
|
68
|
+
this.packageIds?.personalKioskRulePackageId || PERSONAL_KIOSK_RULE_ADDRESS[this.network];
|
|
69
|
+
|
|
70
|
+
return getOwnedKiosks(this.client, address, {
|
|
71
|
+
pagination,
|
|
72
|
+
personalKioskType: personalPackageId
|
|
73
|
+
? `${personalPackageId}::personal_kiosk::PersonalKioskCap`
|
|
74
|
+
: '',
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
|
|
78
|
+
/**
|
|
79
|
+
* Fetches the kiosk contents.
|
|
80
|
+
* @param id The ID of the kiosk to fetch.
|
|
81
|
+
* @param options Optional to control the fetch behavior.
|
|
82
|
+
* @returns
|
|
83
|
+
*/
|
|
84
|
+
async getKiosk({ id, options }: { id: string; options?: FetchKioskOptions }): Promise<KioskData> {
|
|
85
|
+
return (await fetchKiosk(this.client, id, {}, options || {})).data;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
/**
|
|
89
|
+
* Fetch the extension data (if any) for a kiosk, by type
|
|
90
|
+
* @param kioskId The ID of the kiosk to lookup
|
|
91
|
+
* @param extensionType The Type of the extension (can be used from by using the type returned by `getKiosk()`)
|
|
92
|
+
*/
|
|
93
|
+
async getKioskExtension({ kioskId, type }: { kioskId: string; type: string }) {
|
|
94
|
+
return fetchKioskExtension(this.client, kioskId, type);
|
|
95
|
+
}
|
|
96
|
+
|
|
97
|
+
/**
|
|
98
|
+
* Query the Transfer Policy(ies) for type `T`.
|
|
99
|
+
* @param type The Type we're querying for (E.g `0xMyAddress::hero::Hero`)
|
|
100
|
+
*/
|
|
101
|
+
async getTransferPolicies({ type }: { type: string }) {
|
|
102
|
+
return queryTransferPolicy(this.client, type);
|
|
103
|
+
}
|
|
104
|
+
|
|
105
|
+
/**
|
|
106
|
+
* Query all the owned transfer policies for an address.
|
|
107
|
+
* Returns `TransferPolicyCap` which uncludes `policyId, policyCapId, type`.
|
|
108
|
+
* @param address The address we're searching the owned transfer policies for.
|
|
109
|
+
*/
|
|
110
|
+
async getOwnedTransferPolicies({ address }: { address: string }) {
|
|
111
|
+
return queryOwnedTransferPolicies(this.client, address);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
/**
|
|
115
|
+
* Query the Transfer Policy Cap for type `T`, owned by `address`
|
|
116
|
+
* @param type The Type `T` for the object
|
|
117
|
+
* @param address The address that owns the cap.
|
|
118
|
+
*/
|
|
119
|
+
async getOwnedTransferPoliciesByType({ type, address }: { type: string; address: string }) {
|
|
120
|
+
return queryTransferPolicyCapsByType(this.client, address, type);
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
// Someone would just have to create a `kiosk-client.ts` file in their project, initialize a KioskClient
|
|
124
|
+
// and call the `addRuleResolver` function. Each rule has a `resolve` function.
|
|
125
|
+
// The resolve function is automatically called on `purchaseAndResolve` function call.
|
|
126
|
+
addRuleResolver(rule: TransferPolicyRule) {
|
|
127
|
+
if (this.rules.find((x) => x.rule === rule.rule))
|
|
128
|
+
throw new Error(`Rule ${rule.rule} resolver already exists.`);
|
|
129
|
+
this.rules.push(rule);
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* A convenient helper to get the packageIds for our supported ruleset,
|
|
134
|
+
* based on `kioskClient` configuration.
|
|
135
|
+
*/
|
|
136
|
+
getRulePackageId(
|
|
137
|
+
rule:
|
|
138
|
+
| 'kioskLockRulePackageId'
|
|
139
|
+
| 'royaltyRulePackageId'
|
|
140
|
+
| 'personalKioskRulePackageId'
|
|
141
|
+
| 'floorPriceRulePackageId',
|
|
142
|
+
) {
|
|
143
|
+
const rules = this.packageIds || {};
|
|
144
|
+
const network = this.network;
|
|
145
|
+
|
|
146
|
+
/// Check existence of rule based on network and throw an error if it's not found.
|
|
147
|
+
/// We always have a fallback for testnet or mainnet.
|
|
148
|
+
if (!rules[rule] && network !== Network.MAINNET && network !== Network.TESTNET) {
|
|
149
|
+
throw new Error(`Missing packageId for rule ${rule}`);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
switch (rule) {
|
|
153
|
+
case 'kioskLockRulePackageId':
|
|
154
|
+
return rules[rule] || KIOSK_LOCK_RULE_ADDRESS[network];
|
|
155
|
+
case 'royaltyRulePackageId':
|
|
156
|
+
return rules[rule] || ROYALTY_RULE_ADDRESS[network];
|
|
157
|
+
case 'personalKioskRulePackageId':
|
|
158
|
+
return rules[rule] || PERSONAL_KIOSK_RULE_ADDRESS[network];
|
|
159
|
+
case 'floorPriceRulePackageId':
|
|
160
|
+
return rules[rule] || FLOOR_PRICE_RULE_ADDRESS[network];
|
|
161
|
+
}
|
|
162
|
+
}
|
|
163
|
+
}
|
|
@@ -0,0 +1,526 @@
|
|
|
1
|
+
// Copyright (c) Mysten Labs, Inc.
|
|
2
|
+
// SPDX-License-Identifier: Apache-2.0
|
|
3
|
+
|
|
4
|
+
import type {
|
|
5
|
+
Transaction,
|
|
6
|
+
TransactionArgument,
|
|
7
|
+
TransactionObjectArgument,
|
|
8
|
+
} from '@haneullabs/haneul/transactions';
|
|
9
|
+
|
|
10
|
+
import * as kioskTx from '../tx/kiosk.js';
|
|
11
|
+
import { convertToPersonalTx, transferPersonalCapTx } from '../tx/personal-kiosk.js';
|
|
12
|
+
import { confirmRequest } from '../tx/transfer-policy.js';
|
|
13
|
+
import type {
|
|
14
|
+
ItemId,
|
|
15
|
+
ItemReference,
|
|
16
|
+
ItemValue,
|
|
17
|
+
KioskOwnerCap,
|
|
18
|
+
ObjectArgument,
|
|
19
|
+
Price,
|
|
20
|
+
PurchaseOptions,
|
|
21
|
+
} from '../types/index.js';
|
|
22
|
+
import { getNormalizedRuleType } from '../utils.js';
|
|
23
|
+
import type { KioskClient } from './kiosk-client.js';
|
|
24
|
+
|
|
25
|
+
export type KioskTransactionParams = {
|
|
26
|
+
/** The Transaction for this run */
|
|
27
|
+
transaction: Transaction;
|
|
28
|
+
|
|
29
|
+
/** @deprecated use transaction instead */
|
|
30
|
+
transactionBlock?: Transaction;
|
|
31
|
+
/**
|
|
32
|
+
* You can create a new KioskClient by calling `new KioskClient()`
|
|
33
|
+
*/
|
|
34
|
+
kioskClient: KioskClient;
|
|
35
|
+
/**
|
|
36
|
+
* You can optionally pass in the `cap` as returned
|
|
37
|
+
* from `kioskClient.getOwnedKiosks` when initializing the client
|
|
38
|
+
* Otherwise, you can set it by calling `kioskTransaction.setCap()`
|
|
39
|
+
*/
|
|
40
|
+
cap?: KioskOwnerCap;
|
|
41
|
+
};
|
|
42
|
+
|
|
43
|
+
/**
|
|
44
|
+
* A helper for building transactions that involve kiosk.
|
|
45
|
+
*/
|
|
46
|
+
export class KioskTransaction {
|
|
47
|
+
transaction: Transaction;
|
|
48
|
+
kioskClient: KioskClient;
|
|
49
|
+
kiosk?: TransactionObjectArgument;
|
|
50
|
+
kioskCap?: TransactionObjectArgument;
|
|
51
|
+
// If we're pending `share` of a new kiosk, `finalize()` will share it.
|
|
52
|
+
#pendingShare?: boolean;
|
|
53
|
+
// If we're pending transferring of the cap, `finalize()` will either error or transfer the cap if it's a new personal.
|
|
54
|
+
#pendingTransfer?: boolean;
|
|
55
|
+
// The promise that the personalCap will be returned on `finalize()`.
|
|
56
|
+
#promise?: TransactionArgument | undefined;
|
|
57
|
+
// The personal kiosk argument.
|
|
58
|
+
#personalCap?: TransactionObjectArgument;
|
|
59
|
+
// A flag that checks whether kiosk TX is finalized.
|
|
60
|
+
#finalized: boolean = false;
|
|
61
|
+
|
|
62
|
+
constructor({
|
|
63
|
+
transactionBlock,
|
|
64
|
+
transaction = transactionBlock!,
|
|
65
|
+
kioskClient,
|
|
66
|
+
cap,
|
|
67
|
+
}: KioskTransactionParams) {
|
|
68
|
+
this.transaction = transaction;
|
|
69
|
+
this.kioskClient = kioskClient;
|
|
70
|
+
|
|
71
|
+
if (cap) this.setCap(cap);
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Creates a kiosk and saves `kiosk` and `kioskOwnerCap` in state.
|
|
76
|
+
* Helpful if we want to chain some actions before sharing + transferring the cap to the specified address.
|
|
77
|
+
* @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
|
|
78
|
+
*/
|
|
79
|
+
create() {
|
|
80
|
+
this.#validateFinalizedStatus();
|
|
81
|
+
this.#setPendingStatuses({
|
|
82
|
+
share: true,
|
|
83
|
+
transfer: true,
|
|
84
|
+
});
|
|
85
|
+
const [kiosk, cap] = kioskTx.createKiosk(this.transaction);
|
|
86
|
+
this.kiosk = kiosk;
|
|
87
|
+
this.kioskCap = cap;
|
|
88
|
+
return this;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Creates a personal kiosk & shares it.
|
|
93
|
+
* The `PersonalKioskCap` is transferred to the signer.
|
|
94
|
+
* @param borrow If true, the `kioskOwnerCap` is borrowed from the `PersonalKioskCap` to be used in next transactions.
|
|
95
|
+
*/
|
|
96
|
+
createPersonal(borrow?: boolean) {
|
|
97
|
+
this.#pendingShare = true;
|
|
98
|
+
return this.create().convertToPersonal(borrow);
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
/**
|
|
102
|
+
* Converts a kiosk to a Personal (Soulbound) Kiosk.
|
|
103
|
+
* Requires initialization by either calling `ktxb.create()` or `ktxb.setCap()`.
|
|
104
|
+
*/
|
|
105
|
+
convertToPersonal(borrow?: boolean) {
|
|
106
|
+
this.#validateKioskIsSet();
|
|
107
|
+
|
|
108
|
+
const cap = convertToPersonalTx(
|
|
109
|
+
this.transaction,
|
|
110
|
+
this.kiosk!,
|
|
111
|
+
this.kioskCap!,
|
|
112
|
+
this.kioskClient.getRulePackageId('personalKioskRulePackageId'),
|
|
113
|
+
);
|
|
114
|
+
|
|
115
|
+
// if we enable `borrow`, we borrow the kioskCap from the cap.
|
|
116
|
+
if (borrow) this.#borrowFromPersonalCap(cap);
|
|
117
|
+
else this.#personalCap = cap;
|
|
118
|
+
|
|
119
|
+
this.#setPendingStatuses({ transfer: true });
|
|
120
|
+
return this;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
/**
|
|
124
|
+
* Single function way to create a kiosk, share it and transfer the cap to the specified address.
|
|
125
|
+
*/
|
|
126
|
+
createAndShare(address: string) {
|
|
127
|
+
this.#validateFinalizedStatus();
|
|
128
|
+
const cap = kioskTx.createKioskAndShare(this.transaction);
|
|
129
|
+
this.transaction.transferObjects([cap], this.transaction.pure.address(address));
|
|
130
|
+
return this;
|
|
131
|
+
}
|
|
132
|
+
|
|
133
|
+
/**
|
|
134
|
+
* Shares the kiosk.
|
|
135
|
+
*/
|
|
136
|
+
share() {
|
|
137
|
+
this.#validateKioskIsSet();
|
|
138
|
+
this.#setPendingStatuses({ share: false });
|
|
139
|
+
kioskTx.shareKiosk(this.transaction, this.kiosk!);
|
|
140
|
+
return this;
|
|
141
|
+
}
|
|
142
|
+
|
|
143
|
+
/**
|
|
144
|
+
* Should be called only after `create` is called.
|
|
145
|
+
* It shares the kiosk & transfers the cap to the specified address.
|
|
146
|
+
*/
|
|
147
|
+
shareAndTransferCap(address: string) {
|
|
148
|
+
if (this.#personalCap)
|
|
149
|
+
throw new Error('You can only call `shareAndTransferCap` on a non-personal kiosk.');
|
|
150
|
+
this.#setPendingStatuses({ transfer: false });
|
|
151
|
+
this.share();
|
|
152
|
+
this.transaction.transferObjects([this.kioskCap!], this.transaction.pure.address(address));
|
|
153
|
+
return this;
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
/**
|
|
157
|
+
* A function to borrow an item from a kiosk & execute any function with it.
|
|
158
|
+
* Example: You could borrow a Fren out of a kiosk, attach an accessory (or mix), and return it.
|
|
159
|
+
*/
|
|
160
|
+
borrowTx({ itemType, itemId }: ItemId, callback: (item: TransactionArgument) => void) {
|
|
161
|
+
this.#validateKioskIsSet();
|
|
162
|
+
const [itemObj, promise] = kioskTx.borrowValue(
|
|
163
|
+
this.transaction,
|
|
164
|
+
itemType,
|
|
165
|
+
this.kiosk!,
|
|
166
|
+
this.kioskCap!,
|
|
167
|
+
itemId,
|
|
168
|
+
);
|
|
169
|
+
|
|
170
|
+
callback(itemObj);
|
|
171
|
+
|
|
172
|
+
return this.return({ itemType, item: itemObj, promise });
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
/**
|
|
176
|
+
* Borrows an item from the kiosk.
|
|
177
|
+
* This will fail if the item is listed for sale.
|
|
178
|
+
*
|
|
179
|
+
* Requires calling `return`.
|
|
180
|
+
*/
|
|
181
|
+
borrow({ itemType, itemId }: ItemId): [TransactionArgument, TransactionArgument] {
|
|
182
|
+
this.#validateKioskIsSet();
|
|
183
|
+
const [itemObj, promise] = kioskTx.borrowValue(
|
|
184
|
+
this.transaction,
|
|
185
|
+
itemType,
|
|
186
|
+
this.kiosk!,
|
|
187
|
+
this.kioskCap!,
|
|
188
|
+
itemId,
|
|
189
|
+
);
|
|
190
|
+
|
|
191
|
+
return [itemObj, promise];
|
|
192
|
+
}
|
|
193
|
+
|
|
194
|
+
/**
|
|
195
|
+
* Returns the item back to the kiosk.
|
|
196
|
+
* Accepts the parameters returned from the `borrow` function.
|
|
197
|
+
*/
|
|
198
|
+
return({ itemType, item, promise }: ItemValue & { promise: TransactionArgument }) {
|
|
199
|
+
this.#validateKioskIsSet();
|
|
200
|
+
kioskTx.returnValue(this.transaction, itemType, this.kiosk!, item, promise);
|
|
201
|
+
return this;
|
|
202
|
+
}
|
|
203
|
+
|
|
204
|
+
/**
|
|
205
|
+
* A function to withdraw from kiosk
|
|
206
|
+
* @param address Where to trasnfer the coin.
|
|
207
|
+
* @param amount The amount we aim to withdraw.
|
|
208
|
+
*/
|
|
209
|
+
withdraw(address: string, amount?: string | bigint | number) {
|
|
210
|
+
this.#validateKioskIsSet();
|
|
211
|
+
const coin = kioskTx.withdrawFromKiosk(this.transaction, this.kiosk!, this.kioskCap!, amount);
|
|
212
|
+
this.transaction.transferObjects([coin], this.transaction.pure.address(address));
|
|
213
|
+
return this;
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
/**
|
|
217
|
+
* A function to place an item in the kiosk.
|
|
218
|
+
* @param itemType The type `T` of the item
|
|
219
|
+
* @param item The ID or Transaction Argument of the item
|
|
220
|
+
*/
|
|
221
|
+
place({ itemType, item }: ItemReference) {
|
|
222
|
+
this.#validateKioskIsSet();
|
|
223
|
+
kioskTx.place(this.transaction, itemType, this.kiosk!, this.kioskCap!, item);
|
|
224
|
+
return this;
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/**
|
|
228
|
+
* A function to place an item in the kiosk and list it for sale in one transaction.
|
|
229
|
+
* @param itemType The type `T` of the item
|
|
230
|
+
* @param item The ID or Transaction Argument of the item
|
|
231
|
+
* @param price The price in GEUNHWA
|
|
232
|
+
*/
|
|
233
|
+
placeAndList({ itemType, item, price }: ItemReference & Price) {
|
|
234
|
+
this.#validateKioskIsSet();
|
|
235
|
+
kioskTx.placeAndList(this.transaction, itemType, this.kiosk!, this.kioskCap!, item, price);
|
|
236
|
+
return this;
|
|
237
|
+
}
|
|
238
|
+
|
|
239
|
+
/**
|
|
240
|
+
* A function to list an item in the kiosk.
|
|
241
|
+
* @param itemType The type `T` of the item
|
|
242
|
+
* @param itemId The ID of the item
|
|
243
|
+
* @param price The price in GEUNHWA
|
|
244
|
+
*/
|
|
245
|
+
list({ itemType, itemId, price }: ItemId & { price: string | bigint }) {
|
|
246
|
+
this.#validateKioskIsSet();
|
|
247
|
+
kioskTx.list(this.transaction, itemType, this.kiosk!, this.kioskCap!, itemId, price);
|
|
248
|
+
return this;
|
|
249
|
+
}
|
|
250
|
+
|
|
251
|
+
/**
|
|
252
|
+
* A function to delist an item from the kiosk.
|
|
253
|
+
* @param itemType The type `T` of the item
|
|
254
|
+
* @param itemId The ID of the item
|
|
255
|
+
*/
|
|
256
|
+
delist({ itemType, itemId }: ItemId) {
|
|
257
|
+
this.#validateKioskIsSet();
|
|
258
|
+
kioskTx.delist(this.transaction, itemType, this.kiosk!, this.kioskCap!, itemId);
|
|
259
|
+
return this;
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
/**
|
|
263
|
+
* A function to take an item from the kiosk. The transaction won't succeed if the item is listed or locked.
|
|
264
|
+
|
|
265
|
+
* @param itemType The type `T` of the item
|
|
266
|
+
* @param itemId The ID of the item
|
|
267
|
+
*/
|
|
268
|
+
take({ itemType, itemId }: ItemId): TransactionObjectArgument {
|
|
269
|
+
this.#validateKioskIsSet();
|
|
270
|
+
return kioskTx.take(this.transaction, itemType, this.kiosk!, this.kioskCap!, itemId);
|
|
271
|
+
}
|
|
272
|
+
|
|
273
|
+
/**
|
|
274
|
+
* Transfer a non-locked/non-listed item to an address.
|
|
275
|
+
*
|
|
276
|
+
* @param itemType The type `T` of the item
|
|
277
|
+
* @param itemId The ID of the item
|
|
278
|
+
* @param address The destination address
|
|
279
|
+
*/
|
|
280
|
+
transfer({ itemType, itemId, address }: ItemId & { address: string }) {
|
|
281
|
+
this.#validateKioskIsSet();
|
|
282
|
+
const item = this.take({ itemType, itemId });
|
|
283
|
+
this.transaction.transferObjects([item], this.transaction.pure.address(address));
|
|
284
|
+
return this;
|
|
285
|
+
}
|
|
286
|
+
|
|
287
|
+
/**
|
|
288
|
+
* A function to take lock an item in the kiosk.
|
|
289
|
+
|
|
290
|
+
* @param itemType The type `T` of the item
|
|
291
|
+
* @param item The ID or Transaction Argument of the item
|
|
292
|
+
* @param itemId The ID of the item - Deprecated: Use `item` instead.
|
|
293
|
+
* @param policy The Policy ID or Transaction Argument for item T
|
|
294
|
+
*/
|
|
295
|
+
lock({
|
|
296
|
+
itemType,
|
|
297
|
+
item,
|
|
298
|
+
itemId,
|
|
299
|
+
policy,
|
|
300
|
+
}: ItemReference & { policy: ObjectArgument; itemId?: string }) {
|
|
301
|
+
this.#validateKioskIsSet();
|
|
302
|
+
kioskTx.lock(this.transaction, itemType, this.kiosk!, this.kioskCap!, policy, itemId ?? item);
|
|
303
|
+
return this;
|
|
304
|
+
}
|
|
305
|
+
|
|
306
|
+
/**
|
|
307
|
+
* Purchase an item from a seller's kiosk.
|
|
308
|
+
* Returns [item, transferRequest]
|
|
309
|
+
* Can be called like: `const [item, transferRequest] = kioskTx.purchase({...})`
|
|
310
|
+
* @param itemType The type `T` of the item
|
|
311
|
+
* @param itemId The ID of the item
|
|
312
|
+
* @param price The price in GEUNHWA
|
|
313
|
+
* @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
|
|
314
|
+
*/
|
|
315
|
+
purchase({
|
|
316
|
+
itemType,
|
|
317
|
+
itemId,
|
|
318
|
+
price,
|
|
319
|
+
sellerKiosk,
|
|
320
|
+
}: ItemId & Price & { sellerKiosk: ObjectArgument }): [
|
|
321
|
+
TransactionObjectArgument,
|
|
322
|
+
TransactionObjectArgument,
|
|
323
|
+
] {
|
|
324
|
+
// Split the coin for the amount of the listing.
|
|
325
|
+
const coin = this.transaction.splitCoins(this.transaction.gas, [
|
|
326
|
+
this.transaction.pure.u64(price),
|
|
327
|
+
]);
|
|
328
|
+
return kioskTx.purchase(this.transaction, itemType, sellerKiosk, itemId, coin);
|
|
329
|
+
}
|
|
330
|
+
|
|
331
|
+
/**
|
|
332
|
+
* A function to purchase and resolve a transfer policy.
|
|
333
|
+
* If the transfer policy has the `lock` rule, the item is locked in the kiosk.
|
|
334
|
+
* Otherwise, the item is placed in the kiosk.
|
|
335
|
+
* @param itemType The type of the item
|
|
336
|
+
* @param itemId The id of the item
|
|
337
|
+
* @param price The price of the specified item
|
|
338
|
+
* @param sellerKiosk The kiosk which is selling the item. Can be an id or an object argument.
|
|
339
|
+
* @param extraArgs Used to pass arguments for custom rule resolvers.
|
|
340
|
+
*/
|
|
341
|
+
async purchaseAndResolve({
|
|
342
|
+
itemType,
|
|
343
|
+
itemId,
|
|
344
|
+
price,
|
|
345
|
+
sellerKiosk,
|
|
346
|
+
extraArgs,
|
|
347
|
+
}: ItemId & Price & { sellerKiosk: ObjectArgument } & PurchaseOptions) {
|
|
348
|
+
this.#validateKioskIsSet();
|
|
349
|
+
// Get a list of the transfer policies.
|
|
350
|
+
const policies = await this.kioskClient.getTransferPolicies({ type: itemType });
|
|
351
|
+
|
|
352
|
+
if (policies.length === 0) {
|
|
353
|
+
throw new Error(
|
|
354
|
+
`The type ${itemType} doesn't have a Transfer Policy so it can't be traded through kiosk.`,
|
|
355
|
+
);
|
|
356
|
+
}
|
|
357
|
+
|
|
358
|
+
const policy = policies[0]; // we now pick the first one. We need to add an option to define which one.
|
|
359
|
+
|
|
360
|
+
// initialize the purchase `kiosk::purchase`
|
|
361
|
+
const [purchasedItem, transferRequest] = this.purchase({
|
|
362
|
+
itemType,
|
|
363
|
+
itemId,
|
|
364
|
+
price,
|
|
365
|
+
sellerKiosk,
|
|
366
|
+
});
|
|
367
|
+
|
|
368
|
+
let canTransferOutsideKiosk = true;
|
|
369
|
+
|
|
370
|
+
for (const rule of policy.rules) {
|
|
371
|
+
const ruleDefinition = this.kioskClient.rules.find(
|
|
372
|
+
(x) => getNormalizedRuleType(x.rule) === getNormalizedRuleType(rule),
|
|
373
|
+
);
|
|
374
|
+
if (!ruleDefinition) throw new Error(`No resolver for the following rule: ${rule}.`);
|
|
375
|
+
|
|
376
|
+
if (ruleDefinition.hasLockingRule) canTransferOutsideKiosk = false;
|
|
377
|
+
|
|
378
|
+
await ruleDefinition.resolveRuleFunction({
|
|
379
|
+
packageId: ruleDefinition.packageId,
|
|
380
|
+
transactionBlock: this.transaction,
|
|
381
|
+
transaction: this.transaction,
|
|
382
|
+
itemType,
|
|
383
|
+
itemId,
|
|
384
|
+
price: price.toString(),
|
|
385
|
+
sellerKiosk,
|
|
386
|
+
policyId: policy.id,
|
|
387
|
+
transferRequest,
|
|
388
|
+
purchasedItem,
|
|
389
|
+
kiosk: this.kiosk!,
|
|
390
|
+
kioskCap: this.kioskCap!,
|
|
391
|
+
extraArgs: extraArgs || {},
|
|
392
|
+
kioskClient: this.kioskClient,
|
|
393
|
+
});
|
|
394
|
+
}
|
|
395
|
+
|
|
396
|
+
confirmRequest(this.transaction, itemType, policy.id, transferRequest);
|
|
397
|
+
|
|
398
|
+
if (canTransferOutsideKiosk) this.place({ itemType, item: purchasedItem });
|
|
399
|
+
|
|
400
|
+
return this;
|
|
401
|
+
}
|
|
402
|
+
|
|
403
|
+
/**
|
|
404
|
+
* A function to setup the client using an existing `ownerCap`,
|
|
405
|
+
* as return from the `kioskClient.getOwnedKiosks` function.
|
|
406
|
+
* @param cap `KioskOwnerCap` object as returned from `getOwnedKiosks` SDK call.
|
|
407
|
+
*/
|
|
408
|
+
setCap(cap: KioskOwnerCap) {
|
|
409
|
+
this.#validateFinalizedStatus();
|
|
410
|
+
this.kiosk = this.transaction.object(cap.kioskId);
|
|
411
|
+
if (!cap.isPersonal) {
|
|
412
|
+
this.kioskCap = this.transaction.object(cap.objectId);
|
|
413
|
+
return;
|
|
414
|
+
}
|
|
415
|
+
|
|
416
|
+
return this.#borrowFromPersonalCap(cap.objectId);
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
/**
|
|
420
|
+
* A function that ends up the kiosk building tx & returns the `kioskOwnerCap` back to the
|
|
421
|
+
* `PersonalKioskCap`, in case we are operating on a personal kiosk.
|
|
422
|
+
* It will also share the `kiosk` if it's not shared, and finalize the transfer of the personal cap if it's pending.
|
|
423
|
+
*/
|
|
424
|
+
finalize() {
|
|
425
|
+
this.#validateKioskIsSet();
|
|
426
|
+
// If we're pending the sharing of the new kiosk, share it.
|
|
427
|
+
if (this.#pendingShare) this.share();
|
|
428
|
+
|
|
429
|
+
// If we're operating on a non-personal kiosk, we don't need to do anything else.
|
|
430
|
+
if (!this.#personalCap) {
|
|
431
|
+
// If we're pending transfer though, we inform user to call `shareAndTransferCap()`.
|
|
432
|
+
if (this.#pendingTransfer)
|
|
433
|
+
throw new Error(
|
|
434
|
+
'You need to transfer the `kioskOwnerCap` by calling `shareAndTransferCap()` before wrap',
|
|
435
|
+
);
|
|
436
|
+
return;
|
|
437
|
+
}
|
|
438
|
+
|
|
439
|
+
const packageId = this.kioskClient.getRulePackageId('personalKioskRulePackageId');
|
|
440
|
+
|
|
441
|
+
// if we have a promise, return the `ownerCap` back to the personal cap.
|
|
442
|
+
if (this.#promise) {
|
|
443
|
+
this.transaction.moveCall({
|
|
444
|
+
target: `${packageId}::personal_kiosk::return_val`,
|
|
445
|
+
arguments: [this.#personalCap, this.transaction.object(this.kioskCap!), this.#promise!],
|
|
446
|
+
});
|
|
447
|
+
}
|
|
448
|
+
|
|
449
|
+
// If we are pending transferring the personalCap, we do it here.
|
|
450
|
+
if (this.#pendingTransfer)
|
|
451
|
+
transferPersonalCapTx(this.transaction, this.#personalCap, packageId);
|
|
452
|
+
|
|
453
|
+
// Mark the transaction as finalized, so no other functions can be called.
|
|
454
|
+
this.#finalized = true;
|
|
455
|
+
}
|
|
456
|
+
|
|
457
|
+
// Some setters in case we want custom behavior.
|
|
458
|
+
setKioskCap(cap: TransactionObjectArgument) {
|
|
459
|
+
this.#validateFinalizedStatus();
|
|
460
|
+
this.kioskCap = cap;
|
|
461
|
+
return this;
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
setKiosk(kiosk: TransactionObjectArgument) {
|
|
465
|
+
this.#validateFinalizedStatus();
|
|
466
|
+
this.kiosk = kiosk;
|
|
467
|
+
return this;
|
|
468
|
+
}
|
|
469
|
+
|
|
470
|
+
// Some getters
|
|
471
|
+
/*
|
|
472
|
+
* Returns the active transaction's kiosk, or undefined if `setCap` or `create()` hasn't been called yet.
|
|
473
|
+
*/
|
|
474
|
+
getKiosk() {
|
|
475
|
+
this.#validateFinalizedStatus();
|
|
476
|
+
if (!this.kiosk) throw new Error('Kiosk is not set.');
|
|
477
|
+
return this.kiosk;
|
|
478
|
+
}
|
|
479
|
+
|
|
480
|
+
/*
|
|
481
|
+
* Returns the active transaction's kioskOwnerCap, or undefined if `setCap` or `create()` hasn't been called yet.
|
|
482
|
+
*/
|
|
483
|
+
getKioskCap() {
|
|
484
|
+
this.#validateFinalizedStatus();
|
|
485
|
+
if (!this.kioskCap) throw new Error('Kiosk cap is not set');
|
|
486
|
+
return this.kioskCap;
|
|
487
|
+
}
|
|
488
|
+
|
|
489
|
+
/**
|
|
490
|
+
* A function to borrow from `personalCap`.
|
|
491
|
+
*/
|
|
492
|
+
#borrowFromPersonalCap(personalCap: ObjectArgument) {
|
|
493
|
+
const [kioskCap, promise] = this.transaction.moveCall({
|
|
494
|
+
target: `${this.kioskClient.getRulePackageId(
|
|
495
|
+
'personalKioskRulePackageId',
|
|
496
|
+
)}::personal_kiosk::borrow_val`,
|
|
497
|
+
arguments: [this.transaction.object(personalCap)],
|
|
498
|
+
});
|
|
499
|
+
|
|
500
|
+
this.kioskCap = kioskCap;
|
|
501
|
+
this.#personalCap = this.transaction.object(personalCap);
|
|
502
|
+
this.#promise = promise;
|
|
503
|
+
|
|
504
|
+
return this;
|
|
505
|
+
}
|
|
506
|
+
|
|
507
|
+
#setPendingStatuses({ share, transfer }: { share?: boolean; transfer?: boolean }) {
|
|
508
|
+
if (transfer !== undefined) this.#pendingTransfer = transfer;
|
|
509
|
+
if (share !== undefined) this.#pendingShare = share;
|
|
510
|
+
}
|
|
511
|
+
|
|
512
|
+
#validateKioskIsSet() {
|
|
513
|
+
this.#validateFinalizedStatus();
|
|
514
|
+
|
|
515
|
+
if (!this.kiosk || !this.kioskCap)
|
|
516
|
+
throw new Error(
|
|
517
|
+
'You need to initialize the client by either supplying an existing owner cap or by creating a new by calling `.create()`',
|
|
518
|
+
);
|
|
519
|
+
}
|
|
520
|
+
|
|
521
|
+
// Validates that `finalize`
|
|
522
|
+
#validateFinalizedStatus() {
|
|
523
|
+
if (this.#finalized)
|
|
524
|
+
throw new Error("You can't add more transactions to a finalized kiosk transaction.");
|
|
525
|
+
}
|
|
526
|
+
}
|