@ocap/client 1.25.2 → 1.25.4

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.
Files changed (48) hide show
  1. package/README.md +76 -80
  2. package/dist/report.html +1 -1
  3. package/docs/_sidebar.md +22 -0
  4. package/docs/api-reference-client-methods.md +229 -0
  5. package/docs/api-reference-client-methods.zh.md +229 -0
  6. package/docs/api-reference-data-types.md +482 -0
  7. package/docs/api-reference-data-types.zh.md +482 -0
  8. package/docs/api-reference-low-level-api.md +228 -0
  9. package/docs/api-reference-low-level-api.zh.md +228 -0
  10. package/docs/api-reference-query-mutation-methods.md +814 -0
  11. package/docs/api-reference-query-mutation-methods.zh.md +814 -0
  12. package/docs/api-reference-transaction-helpers.md +649 -0
  13. package/docs/api-reference-transaction-helpers.zh.md +649 -0
  14. package/docs/api-reference.md +23 -0
  15. package/docs/api-reference.zh.md +23 -0
  16. package/docs/core-concepts-client-architecture.md +102 -0
  17. package/docs/core-concepts-client-architecture.zh.md +102 -0
  18. package/docs/core-concepts-event-subscriptions.md +123 -0
  19. package/docs/core-concepts-event-subscriptions.zh.md +123 -0
  20. package/docs/core-concepts-gas-payment.md +111 -0
  21. package/docs/core-concepts-gas-payment.zh.md +111 -0
  22. package/docs/core-concepts-transaction-lifecycle.md +183 -0
  23. package/docs/core-concepts-transaction-lifecycle.zh.md +183 -0
  24. package/docs/core-concepts.md +22 -0
  25. package/docs/core-concepts.zh.md +22 -0
  26. package/docs/getting-started-basic-usage.md +87 -0
  27. package/docs/getting-started-basic-usage.zh.md +87 -0
  28. package/docs/getting-started-installation.md +60 -0
  29. package/docs/getting-started-installation.zh.md +60 -0
  30. package/docs/getting-started.md +16 -0
  31. package/docs/getting-started.zh.md +16 -0
  32. package/docs/how-to-guides-delegate-permissions.md +167 -0
  33. package/docs/how-to-guides-delegate-permissions.zh.md +167 -0
  34. package/docs/how-to-guides-manage-accounts.md +73 -0
  35. package/docs/how-to-guides-manage-accounts.zh.md +73 -0
  36. package/docs/how-to-guides-manage-assets.md +255 -0
  37. package/docs/how-to-guides-manage-assets.zh.md +255 -0
  38. package/docs/how-to-guides-manage-tokens.md +179 -0
  39. package/docs/how-to-guides-manage-tokens.zh.md +179 -0
  40. package/docs/how-to-guides-stake-tokens-and-assets.md +205 -0
  41. package/docs/how-to-guides-stake-tokens-and-assets.zh.md +205 -0
  42. package/docs/how-to-guides-transfer-tokens-and-nfts.md +179 -0
  43. package/docs/how-to-guides-transfer-tokens-and-nfts.zh.md +179 -0
  44. package/docs/how-to-guides.md +27 -0
  45. package/docs/how-to-guides.zh.md +27 -0
  46. package/docs/overview.md +70 -0
  47. package/docs/overview.zh.md +70 -0
  48. package/package.json +14 -14
@@ -0,0 +1,73 @@
1
+ # 管理账户
2
+
3
+ 在由 OCAP 驱动的区块链上,账户本质上是与一个地址关联的状态,由一对加密密钥(公钥和私钥)控制。一个需要理解的关键概念是,账户是隐式创建的。一旦账户收到第一笔入账交易(例如代币或资产转移),它就正式在链上存在了。没有显式的 `createAccount` 交易。
4
+
5
+ 本指南专注于一个关键的账户管理操作:将账户迁移到一组新的密钥。这是一项重要的安全功能,允许您在不更改账户地址、余额或资产的情况下,更改账户的所有权。
6
+
7
+ ## 将账户迁移至新密钥
8
+
9
+ `migrateAccount` 函数允许您将一个现有的账户地址与一个新的公钥关联起来。这在以下几种场景中非常有用:
10
+
11
+ - **密钥泄露**:如果您怀疑自己的私钥已被泄露,可以将账户迁移到一个新的、安全的密钥对,以防止未经授权的访问。
12
+ - **升级安全性**:您可能希望从旧的密钥类型迁移到更新、更安全的密钥类型。
13
+
14
+ 此操作有效地转移了账户的控制权,同时保留了其在区块链上的历史和状态。
15
+
16
+ ### 参数
17
+
18
+ <x-field-group>
19
+ <x-field data-name="from" data-type="WalletObject" data-required="true">
20
+ <x-field-desc markdown>代表正在被迁移的当前账户的钱包对象。该钱包必须签署交易以证明所有权。</x-field-desc>
21
+ </x-field>
22
+ <x-field data-name="to" data-type="WalletObject" data-required="true">
23
+ <x-field-desc markdown>控制权将转移至的新钱包对象。来自此钱包的公钥将成为该账户地址的新权限所有者。</x-field-desc>
24
+ </x-field>
25
+ </x-field-group>
26
+
27
+ ### 返回值
28
+
29
+ `migrateAccount` 方法返回一个 Promise,该 Promise 会解析为交易哈希字符串。
30
+
31
+ ### 示例
32
+
33
+ 以下是如何将账户从一个旧钱包迁移到一个新钱包的示例。
34
+
35
+ ```javascript Migrating an Account icon=logos:javascript
36
+ import Client from '@ocap/client';
37
+ import { fromRandom } from '@ocap/wallet';
38
+
39
+ const client = new Client('https://beta.abtnetwork.io/api');
40
+
41
+ // 假设 oldWallet 是当前账户授权的钱包
42
+ const oldWallet = fromRandom();
43
+
44
+ // 创建一个带有新密钥集的新钱包
45
+ const newWallet = fromRandom();
46
+
47
+ async function migrate() {
48
+ try {
49
+ // 在迁移之前,您通常需要为 oldWallet 的地址充值。
50
+ // 在本示例中,我们假设它有足够的资金来支付交易费用。
51
+ // 您可以从 https://faucet.abtnetwork.io/ 获取测试代币
52
+ console.log(`正在将账户从 ${oldWallet.address} 迁移到与 ${newWallet.address} 关联的新密钥`);
53
+
54
+ const hash = await client.migrateAccount({
55
+ from: oldWallet,
56
+ to: newWallet,
57
+ });
58
+
59
+ console.log('账户迁移交易已成功发送!');
60
+ console.log('交易哈希:', hash);
61
+ console.log(`在 https://beta.abtnetwork.io/explorer/txs/${hash} 查看交易`);
62
+
63
+ } catch (err) {
64
+ console.error('迁移账户时出错:', err);
65
+ }
66
+ }
67
+
68
+ migrate();
69
+ ```
70
+
71
+ ---
72
+
73
+ 现在您已经了解了如何通过迁移密钥来保护您的账户,您可能想学习如何在不共享主密钥的情况下,向其他账户授予有限的权限。更多详情请参阅[委托权限](./how-to-guides-delegate-permissions.md)指南。
@@ -0,0 +1,255 @@
1
+ # Manage Assets (NFTs)
2
+
3
+ This guide provides a comprehensive walkthrough of managing the entire lifecycle of Non-Fungible Tokens (NFTs), also known as assets, using the OCAP Client. You will learn how to create new assets from scratch, update their properties, establish an asset factory for standardized minting, and acquire new assets from that factory.
4
+
5
+ ## Create a New Asset
6
+
7
+ You can create a unique, standalone asset on the blockchain using the `createAsset` method. Each asset is assigned a unique on-chain address derived from its initial properties.
8
+
9
+ ```javascript icon=logos:javascript
10
+ const { wallet } = getWallet(); // User's wallet object
11
+
12
+ async function createNewAsset() {
13
+ try {
14
+ const [hash, address] = await client.createAsset({
15
+ moniker: 'My Unique Digital Artwork',
16
+ data: {
17
+ typeUrl: 'json',
18
+ value: {
19
+ description: 'A one-of-a-kind piece created by Artist X.',
20
+ imageUrl: 'https://example.com/path/to/image.png',
21
+ },
22
+ },
23
+ readonly: true,
24
+ transferrable: true,
25
+ wallet: wallet,
26
+ });
27
+
28
+ console.log(`Asset creation transaction sent: ${hash}`);
29
+ console.log(`New asset address: ${address}`);
30
+ return address;
31
+ } catch (error) {
32
+ console.error('Error creating asset:', error);
33
+ }
34
+ }
35
+
36
+ createNewAsset();
37
+ ```
38
+
39
+ ### Parameters
40
+
41
+ <x-field-group>
42
+ <x-field data-name="moniker" data-type="string" data-required="true" data-desc="The name of the asset."></x-field>
43
+ <x-field data-name="parent" data-type="string" data-default="''" data-required="false" data-desc="The address of a parent asset, if any."></x-field>
44
+ <x-field data-name="data" data-type="object" data-required="true" data-desc="The data payload for the asset, which must include a typeUrl and value."></x-field>
45
+ <x-field data-name="readonly" data-type="boolean" data-default="false" data-required="false" data-desc="If true, the asset cannot be updated after creation."></x-field>
46
+ <x-field data-name="transferrable" data-type="boolean" data-default="true" data-required="false" data-desc="If true, the asset can be transferred to another account."></x-field>
47
+ <x-field data-name="ttl" data-type="number" data-default="0" data-required="false" data-desc="Time-to-live in seconds after the asset's first consumption."></x-field>
48
+ <x-field data-name="display" data-type="object" data-required="false" data-desc="Object containing display information for the asset."></x-field>
49
+ <x-field data-name="endpoint" data-type="object" data-required="false" data-desc="Object containing endpoint details for the asset."></x-field>
50
+ <x-field data-name="tags" data-type="string[]" data-default="[]" data-required="false" data-desc="An array of strings to categorize the asset."></x-field>
51
+ <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="The wallet object of the asset's initial owner."></x-field>
52
+ <x-field data-name="delegator" data-type="string" data-default="''" data-required="false" data-desc="The address of the account that authorized this transaction via delegation."></x-field>
53
+ </x-field-group>
54
+
55
+ ### Returns
56
+
57
+ A `Promise` that resolves to an array containing the transaction hash and the new asset's on-chain address.
58
+
59
+ <x-field data-name="response" data-type="Promise<[string, string]>" data-desc="[transactionHash, assetAddress]"></x-field>
60
+
61
+ ---
62
+
63
+ ## Update an Existing Asset
64
+
65
+ If an asset was created with `readonly: false`, you can modify its `moniker` and `data` fields using the `updateAsset` method. The asset is identified by its unique address.
66
+
67
+ ```javascript icon=logos:javascript
68
+ const { wallet } = getWallet(); // User's wallet object
69
+ const assetAddress = 'z362...'; // Address of the asset to update
70
+
71
+ async function updateExistingAsset() {
72
+ try {
73
+ const hash = await client.updateAsset({
74
+ address: assetAddress,
75
+ moniker: 'My Updated Digital Artwork',
76
+ data: {
77
+ typeUrl: 'json',
78
+ value: {
79
+ description: 'An updated description for my unique piece.',
80
+ imageUrl: 'https://example.com/path/to/new_image.png',
81
+ },
82
+ },
83
+ wallet: wallet,
84
+ });
85
+
86
+ console.log(`Asset update transaction sent: ${hash}`);
87
+ } catch (error) {
88
+ console.error('Error updating asset:', error);
89
+ }
90
+ }
91
+
92
+ updateExistingAsset();
93
+ ```
94
+
95
+ ### Parameters
96
+
97
+ <x-field-group>
98
+ <x-field data-name="address" data-type="string" data-required="true" data-desc="The on-chain address of the asset to update."></x-field>
99
+ <x-field data-name="moniker" data-type="string" data-required="true" data-desc="The new name for the asset."></x-field>
100
+ <x-field data-name="data" data-type="object" data-required="true" data-desc="The updated data payload for the asset."></x-field>
101
+ <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="The wallet object of the current asset owner."></x-field>
102
+ </x-field-group>
103
+
104
+ ### Returns
105
+
106
+ A `Promise` that resolves to the transaction hash.
107
+
108
+ <x-field data-name="response" data-type="Promise<string>" data-desc="transactionHash"></x-field>
109
+
110
+ ---
111
+
112
+ ## Create an Asset Factory
113
+
114
+ An Asset Factory is a template for creating multiple, similar assets. It defines the structure, rules, and logic for minting new assets, which is more efficient than creating each one individually. This is ideal for use cases like issuing event tickets, certificates, or collectibles.
115
+
116
+ ```javascript icon=logos:javascript
117
+ const { wallet } = getWallet(); // Factory owner's wallet
118
+
119
+ const factoryDefinition = {
120
+ name: 'Conference Ticket Factory',
121
+ description: 'Mints tickets for the 2024 Tech Conference.',
122
+ limit: 1000, // Max 1000 tickets can be minted
123
+ input: {
124
+ // Defines what data is required to mint an asset
125
+ type: 'object',
126
+ properties: {
127
+ attendeeName: { type: 'string' },
128
+ ticketType: { type: 'string', enum: ['General', 'VIP'] },
129
+ },
130
+ },
131
+ output: {
132
+ // Defines the structure of the minted asset
133
+ moniker: 'Ticket for {{attendeeName}}',
134
+ description: '{{ticketType}} admission for the 2024 Tech Conference.',
135
+ transferrable: false, // Tickets are non-transferrable
136
+ },
137
+ hooks: [],
138
+ };
139
+
140
+ async function createFactory() {
141
+ try {
142
+ const [hash, factoryAddress] = await client.createAssetFactory({
143
+ factory: factoryDefinition,
144
+ wallet: wallet,
145
+ });
146
+
147
+ console.log(`Factory creation transaction sent: ${hash}`);
148
+ console.log(`New factory address: ${factoryAddress}`);
149
+ } catch (error) {
150
+ console.error('Error creating asset factory:', error);
151
+ }
152
+ }
153
+
154
+ createFactory();
155
+ ```
156
+
157
+ ### Parameters
158
+
159
+ <x-field-group>
160
+ <x-field data-name="factory" data-type="object" data-required="true" data-desc="An object defining the factory's properties and minting logic.">
161
+ <x-field data-name="name" data-type="string" data-required="true" data-desc="Name of the factory."></x-field>
162
+ <x-field data-name="description" data-type="string" data-required="true" data-desc="Description of the factory's purpose."></x-field>
163
+ <x-field data-name="limit" data-type="number" data-default="0" data-required="false" data-desc="The maximum number of assets that can be minted from this factory. 0 means unlimited."></x-field>
164
+ <x-field data-name="trustedIssuers" data-type="string[]" data-required="false" data-desc="A list of account addresses authorized to mint from this factory."></x-field>
165
+ <x-field data-name="input" data-type="object" data-required="true" data-desc="Defines the required input data for minting an asset."></x-field>
166
+ <x-field data-name="output" data-type="object" data-required="true" data-desc="Defines the structure and properties of the asset that will be minted."></x-field>
167
+ <x-field data-name="hooks" data-type="object[]" data-required="false" data-desc="A list of hooks to execute during the minting process."></x-field>
168
+ <x-field data-name="data" data-type="object" data-required="false" data-desc="Additional arbitrary data to store with the factory."></x-field>
169
+ </x-field>
170
+ <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="The wallet object of the factory's owner."></x-field>
171
+ </x-field-group>
172
+
173
+
174
+ ### Returns
175
+
176
+ A `Promise` that resolves to an array containing the transaction hash and the new factory's on-chain address.
177
+
178
+ <x-field data-name="response" data-type="Promise<[string, string]>" data-desc="[transactionHash, factoryAddress]"></x-field>
179
+
180
+ ---
181
+
182
+ ## Acquire an Asset from a Factory
183
+
184
+ Acquiring an asset from a factory is a two-step process. First, you prepare the minting data, which allows you to preview the asset that will be created. Second, you submit the transaction to the blockchain to officially acquire the asset.
185
+
186
+ This separation is useful because it allows an application to show a user what they are about to receive before they sign and submit the final transaction.
187
+
188
+ ### Step 1: Prepare the Asset Data
189
+
190
+ The `preMintAsset` method takes the factory address and user-provided inputs to generate the final asset data. This happens off-chain and does not require a transaction.
191
+
192
+ ```javascript icon=logos:javascript
193
+ const factoryAddress = 'z2...'; // Address of the factory created previously
194
+ const { wallet: issuerWallet } = getIssuerWallet(); // The factory owner or a trusted issuer
195
+ const { wallet: userWallet } = getUserWallet(); // The user who will own the new asset
196
+
197
+ async function prepareAssetForMinting() {
198
+ try {
199
+ const mintingData = await client.preMintAsset({
200
+ factory: factoryAddress,
201
+ inputs: {
202
+ attendeeName: 'John Doe',
203
+ ticketType: 'VIP',
204
+ },
205
+ owner: userWallet.address,
206
+ wallet: issuerWallet,
207
+ });
208
+
209
+ console.log('Prepared asset data for minting:', mintingData);
210
+ return mintingData;
211
+ } catch (error) {
212
+ console.error('Error preparing asset:', error);
213
+ }
214
+ }
215
+ ```
216
+
217
+ ### Step 2: Send the Transaction
218
+
219
+ Once the minting data is prepared, the user (the future asset owner) signs and sends the `acquireAsset` transaction. The `itx` object from `preMintAsset` is used as the payload.
220
+
221
+ ```javascript icon=logos:javascript
222
+ async function acquireNewAsset() {
223
+ // First, get the minting data from Step 1
224
+ const itx = await prepareAssetForMinting();
225
+ if (!itx) return;
226
+
227
+ try {
228
+ const hash = await client.acquireAsset({
229
+ itx: itx,
230
+ wallet: userWallet, // The user's wallet signs the transaction
231
+ });
232
+
233
+ console.log(`Asset acquisition transaction sent: ${hash}`);
234
+ console.log(`New asset will be available at address: ${itx.address}`);
235
+ } catch (error) {
236
+ console.error('Error acquiring asset:', error);
237
+ }
238
+ }
239
+
240
+ acquireNewAsset();
241
+ ```
242
+
243
+ ### Parameters for `acquireAsset`
244
+
245
+ <x-field-group>
246
+ <x-field data-name="itx" data-type="object" data-required="true" data-desc="The inner transaction object returned from the `preMintAsset` method."></x-field>
247
+ <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="The wallet of the user who is acquiring the asset."></x-field>
248
+ <x-field data-name="delegator" data-type="string" data-default="''" data-required="false" data-desc="The address of the account that authorized this transaction via delegation."></x-field>
249
+ </x-field-group>
250
+
251
+ ### Returns
252
+
253
+ A `Promise` that resolves to the transaction hash for the `acquireAsset` operation.
254
+
255
+ <x-field data-name="response" data-type="Promise<string>" data-desc="transactionHash"></x-field>
@@ -0,0 +1,255 @@
1
+ # 管理资产 (NFT)
2
+
3
+ 本指南将全面介绍如何使用 OCAP Client 管理非同质化代币(NFT),也称资产的整个生命周期。你将学习如何从头开始创建新资产、更新其属性、建立用于标准化铸造的资产工厂,以及从该工厂获取新资产。
4
+
5
+ ## 创建新资产
6
+
7
+ 你可以使用 `createAsset` 方法在区块链上创建一个独特的、独立的资产。每个资产都会被分配一个唯一的链上地址,该地址由其初始属性派生。
8
+
9
+ ```javascript icon=logos:javascript
10
+ const { wallet } = getWallet(); // 用户的钱包对象
11
+
12
+ async function createNewAsset() {
13
+ try {
14
+ const [hash, address] = await client.createAsset({
15
+ moniker: '我的独特数字艺术品',
16
+ data: {
17
+ typeUrl: 'json',
18
+ value: {
19
+ description: '由艺术家 X 创作的独一无二的作品。',
20
+ imageUrl: 'https://example.com/path/to/image.png',
21
+ },
22
+ },
23
+ readonly: true,
24
+ transferrable: true,
25
+ wallet: wallet,
26
+ });
27
+
28
+ console.log(`资产创建交易已发送:${hash}`);
29
+ console.log(`新资产地址:${address}`);
30
+ return address;
31
+ } catch (error) {
32
+ console.error('创建资产时出错:', error);
33
+ }
34
+ }
35
+
36
+ createNewAsset();
37
+ ```
38
+
39
+ ### 参数
40
+
41
+ <x-field-group>
42
+ <x-field data-name="moniker" data-type="string" data-required="true" data-desc="资产的名称。"></x-field>
43
+ <x-field data-name="parent" data-type="string" data-default="''" data-required="false" data-desc="父资产的地址(如有)。"></x-field>
44
+ <x-field data-name="data" data-type="object" data-required="true" data-desc="资产的数据负载,必须包含 typeUrl 和 value。"></x-field>
45
+ <x-field data-name="readonly" data-type="boolean" data-default="false" data-required="false" data-desc="若为 true,则资产在创建后无法更新。"></x-field>
46
+ <x-field data-name="transferrable" data-type="boolean" data-default="true" data-required="false" data-desc="若为 true,则资产可以转移到另一个账户。"></x-field>
47
+ <x-field data-name="ttl" data-type="number" data-default="0" data-required="false" data-desc="资产首次消费后的存活时间(秒)。"></x-field>
48
+ <x-field data-name="display" data-type="object" data-required="false" data-desc="包含资产显示信息的对象。"></x-field>
49
+ <x-field data-name="endpoint" data-type="object" data-required="false" data-desc="包含资产端点详细信息的对象。"></x-field>
50
+ <x-field data-name="tags" data-type="string[]" data-default="[]" data-required="false" data-desc="用于对资产进行分类的字符串数组。"></x-field>
51
+ <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="资产初始所有者的钱包对象。"></x-field>
52
+ <x-field data-name="delegator" data-type="string" data-default="''" data-required="false" data-desc="通过委托授权此交易的账户地址。"></x-field>
53
+ </x-field-group>
54
+
55
+ ### 返回值
56
+
57
+ 一个 `Promise`,它会解析为一个包含交易哈希和新资产链上地址的数组。
58
+
59
+ <x-field data-name="response" data-type="Promise<[string, string]>" data-desc="[交易哈希, 资产地址]"></x-field>
60
+
61
+ ---
62
+
63
+ ## 更新现有资产
64
+
65
+ 如果资产创建时设置了 `readonly: false`,你可以使用 `updateAsset` 方法修改其 `moniker` 和 `data` 字段。资产通过其唯一地址进行识别。
66
+
67
+ ```javascript icon=logos:javascript
68
+ const { wallet } = getWallet(); // 用户的钱包对象
69
+ const assetAddress = 'z362...'; // 要更新的资产地址
70
+
71
+ async function updateExistingAsset() {
72
+ try {
73
+ const hash = await client.updateAsset({
74
+ address: assetAddress,
75
+ moniker: '我更新后的数字艺术品',
76
+ data: {
77
+ typeUrl: 'json',
78
+ value: {
79
+ description: '我这件独特作品的更新描述。',
80
+ imageUrl: 'https://example.com/path/to/new_image.png',
81
+ },
82
+ },
83
+ wallet: wallet,
84
+ });
85
+
86
+ console.log(`资产更新交易已发送:${hash}`);
87
+ } catch (error) {
88
+ console.error('更新资产时出错:', error);
89
+ }
90
+ }
91
+
92
+ updateExistingAsset();
93
+ ```
94
+
95
+ ### 参数
96
+
97
+ <x-field-group>
98
+ <x-field data-name="address" data-type="string" data-required="true" data-desc="要更新资产的链上地址。"></x-field>
99
+ <x-field data-name="moniker" data-type="string" data-required="true" data-desc="资产的新名称。"></x-field>
100
+ <x-field data-name="data" data-type="object" data-required="true" data-desc="资产更新后的数据负载。"></x-field>
101
+ <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="当前资产所有者的钱包对象。"></x-field>
102
+ </x-field-group>
103
+
104
+ ### 返回值
105
+
106
+ 一个 `Promise`,它会解析为交易哈希。
107
+
108
+ <x-field data-name="response" data-type="Promise<string>" data-desc="交易哈希"></x-field>
109
+
110
+ ---
111
+
112
+ ## 创建资产工厂
113
+
114
+ 资产工厂是用于创建多个相似资产的模板。它定义了铸造新资产的结构、规则和逻辑,比单独创建每个资产更高效。这非常适合发行活动门票、证书或收藏品等用例。
115
+
116
+ ```javascript icon=logos:javascript
117
+ const { wallet } = getWallet(); // 工厂所有者的钱包
118
+
119
+ const factoryDefinition = {
120
+ name: '会议门票工厂',
121
+ description: '铸造 2024 年科技大会的门票。',
122
+ limit: 1000, // 最多可铸造 1000 张门票
123
+ input: {
124
+ // 定义铸造资产所需的数据
125
+ type: 'object',
126
+ properties: {
127
+ attendeeName: { type: 'string' },
128
+ ticketType: { type: 'string', enum: ['General', 'VIP'] },
129
+ },
130
+ },
131
+ output: {
132
+ // 定义铸造资产的结构
133
+ moniker: '{{attendeeName}} 的门票',
134
+ description: '2024 年科技大会的 {{ticketType}} 门票。',
135
+ transferrable: false, // 门票不可转让
136
+ },
137
+ hooks: [],
138
+ };
139
+
140
+ async function createFactory() {
141
+ try {
142
+ const [hash, factoryAddress] = await client.createAssetFactory({
143
+ factory: factoryDefinition,
144
+ wallet: wallet,
145
+ });
146
+
147
+ console.log(`工厂创建交易已发送:${hash}`);
148
+ console.log(`新工厂地址:${factoryAddress}`);
149
+ } catch (error) {
150
+ console.error('创建资产工厂时出错:', error);
151
+ }
152
+ }
153
+
154
+ createFactory();
155
+ ```
156
+
157
+ ### 参数
158
+
159
+ <x-field-group>
160
+ <x-field data-name="factory" data-type="object" data-required="true" data-desc="定义工厂属性和铸造逻辑的对象。">
161
+ <x-field data-name="name" data-type="string" data-required="true" data-desc="工厂的名称。"></x-field>
162
+ <x-field data-name="description" data-type="string" data-required="true" data-desc="工厂用途的描述。"></x-field>
163
+ <x-field data-name="limit" data-type="number" data-default="0" data-required="false" data-desc="可从此工厂铸造的最大资产数量。0 表示无限制。"></x-field>
164
+ <x-field data-name="trustedIssuers" data-type="string[]" data-required="false" data-desc="有权从此工厂铸造的账户地址列表。"></x-field>
165
+ <x-field data-name="input" data-type="object" data-required="true" data-desc="定义铸造资产所需的输入数据。"></x-field>
166
+ <x-field data-name="output" data-type="object" data-required="true" data-desc="定义将要铸造的资产的结构和属性。"></x-field>
167
+ <x-field data-name="hooks" data-type="object[]" data-required="false" data-desc="在铸造过程中执行的钩子列表。"></x-field>
168
+ <x-field data-name="data" data-type="object" data-required="false" data-desc="与工厂一起存储的额外任意数据。"></x-field>
169
+ </x-field>
170
+ <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="工厂所有者的钱包对象。"></x-field>
171
+ </x-field-group>
172
+
173
+
174
+ ### 返回值
175
+
176
+ 一个 `Promise`,它会解析为一个包含交易哈希和新工厂链上地址的数组。
177
+
178
+ <x-field data-name="response" data-type="Promise<[string, string]>" data-desc="[交易哈希, 工厂地址]"></x-field>
179
+
180
+ ---
181
+
182
+ ## 从工厂获取资产
183
+
184
+ 从工厂获取资产是一个两步过程。首先,准备铸造数据,这可以让你预览将要创建的资产。其次,将交易提交到区块链以正式获取资产。
185
+
186
+ 这种分离很有用,因为它允许应用程序在用户签署并提交最终交易之前,向用户展示他们将要收到的内容。
187
+
188
+ ### 步骤 1:准备资产数据
189
+
190
+ `preMintAsset` 方法接收工厂地址和用户提供的输入,以生成最终的资产数据。此过程在链下进行,无需交易。
191
+
192
+ ```javascript icon=logos:javascript
193
+ const factoryAddress = 'z2...'; // 先前创建的工厂地址
194
+ const { wallet: issuerWallet } = getIssuerWallet(); // 工厂所有者或受信任的发行者
195
+ const { wallet: userWallet } = getUserWallet(); // 将拥有新资产的用户
196
+
197
+ async function prepareAssetForMinting() {
198
+ try {
199
+ const mintingData = await client.preMintAsset({
200
+ factory: factoryAddress,
201
+ inputs: {
202
+ attendeeName: 'John Doe',
203
+ ticketType: 'VIP',
204
+ },
205
+ owner: userWallet.address,
206
+ wallet: issuerWallet,
207
+ });
208
+
209
+ console.log('准备好用于铸造的资产数据:', mintingData);
210
+ return mintingData;
211
+ } catch (error) {
212
+ console.error('准备资产时出错:', error);
213
+ }
214
+ }
215
+ ```
216
+
217
+ ### 步骤 2:发送交易
218
+
219
+ 铸造数据准备好后,用户(即未来的资产所有者)签署并发送 `acquireAsset` 交易。`preMintAsset` 返回的 `itx` 对象将用作有效载荷。
220
+
221
+ ```javascript icon=logos:javascript
222
+ async function acquireNewAsset() {
223
+ // 首先,从步骤 1 获取铸造数据
224
+ const itx = await prepareAssetForMinting();
225
+ if (!itx) return;
226
+
227
+ try {
228
+ const hash = await client.acquireAsset({
229
+ itx: itx,
230
+ wallet: userWallet, // 用户的钱包签署交易
231
+ });
232
+
233
+ console.log(`资产获取交易已发送:${hash}`);
234
+ console.log(`新资产将在以下地址可用:${itx.address}`);
235
+ } catch (error) {
236
+ console.error('获取资产时出错:', error);
237
+ }
238
+ }
239
+
240
+ acquireNewAsset();
241
+ ```
242
+
243
+ ### `acquireAsset` 的参数
244
+
245
+ <x-field-group>
246
+ <x-field data-name="itx" data-type="object" data-required="true" data-desc="从 `preMintAsset` 方法返回的内部交易对象。"></x-field>
247
+ <x-field data-name="wallet" data-type="WalletObject" data-required="true" data-desc="正在获取资产的用户的钱包。"></x-field>
248
+ <x-field data-name="delegator" data-type="string" data-default="''" data-required="false" data-desc="通过委托授权此交易的账户地址。"></x-field>
249
+ </x-field-group>
250
+
251
+ ### 返回值
252
+
253
+ 一个 `Promise`,它会解析为 `acquireAsset` 操作的交易哈希。
254
+
255
+ <x-field data-name="response" data-type="Promise<string>" data-desc="交易哈希"></x-field>