@mysten/payment-kit 0.1.10 → 0.1.11

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.
@@ -0,0 +1,529 @@
1
+ # Registry Management
2
+
3
+ > Create, configure, and manage payment registries and accepted coin types on Sui.
4
+
5
+ > **Note:** This package is in active development and should be used with caution. APIs are experimental and
6
+ > subject to breaking changes without notice. We recommend thoroughly testing any implementation
7
+ > before using in production environments.
8
+
9
+ Payment registries are the core of Payment Kit's duplicate prevention and payment tracking system.
10
+ This guide covers creating, configuring, and managing payment registries.
11
+
12
+ ## Understanding payment registries
13
+
14
+ A `PaymentRegistry` is an onchain object that:
15
+
16
+ - Stores `PaymentRecord` dynamic fields for each unique payment
17
+ - Manages configuration settings (expiration, fund management)
18
+ - Can be owned and administered by a specific account
19
+ - Provides namespaced payment tracking
20
+
21
+ ### Default registry
22
+
23
+ Payment Kit provides a default registry that's ready to use:
24
+
25
+ ```ts
26
+ // Use the default registry
27
+ const tx = client.paymentKit.tx.processRegistryPayment({
28
+ nonce: crypto.randomUUID(),
29
+ coinType: '0x2::sui::SUI',
30
+ amount: 1000000000,
31
+ receiver,
32
+ sender: senderAddress,
33
+ });
34
+ ```
35
+
36
+ ### When to create a custom registry
37
+
38
+ Consider creating your own registry when:
39
+
40
+ - You want isolated payment tracking for your application
41
+ - You need custom expiration policies
42
+ - You want to manage funds centrally
43
+ - You need better indexing of your payments
44
+ - You want to avoid potential congestion on the default registry
45
+
46
+ ## Creating a registry
47
+
48
+ ### Basic registry creation
49
+
50
+ Creating a registry is straightforward. You only need to provide a unique name:
51
+
52
+ ```ts
53
+ // Create a new registry
54
+ const tx = client.paymentKit.tx.createRegistry({
55
+ registryName: 'my-app-payments',
56
+ });
57
+
58
+ const result = await client.signAndExecuteTransaction({
59
+ transaction: tx,
60
+ signer: keypair,
61
+ options: {
62
+ showEffects: true,
63
+ showObjectChanges: true,
64
+ },
65
+ });
66
+
67
+ // Check transaction status
68
+ if (result.$kind === 'FailedTransaction') {
69
+ throw new Error(`Registry creation failed: ${result.FailedTransaction.status.error?.message}`);
70
+ }
71
+
72
+ console.log('Registry created!');
73
+ console.log('Transaction:', result.Transaction.digest);
74
+ ```
75
+
76
+ ### What gets created
77
+
78
+ When you create a registry, two objects are created:
79
+
80
+ 1. **PaymentRegistry**: The registry object that stores payment records
81
+ 2. **RegistryAdminCap**: A capability object that grants admin permissions
82
+
83
+ ### Extracting the admin capability
84
+
85
+ The `RegistryAdminCap` is crucial for configuring your registry:
86
+
87
+ ```ts
88
+ // Find the admin cap in the transaction result
89
+ const adminCapObject = result.Transaction.objectChanges?.find(
90
+ (change) => change.type === 'created' && change.objectType.includes('RegistryAdminCap'),
91
+ );
92
+
93
+ if (adminCapObject && 'objectId' in adminCapObject) {
94
+ const adminCapId = adminCapObject.objectId;
95
+
96
+ console.log('Admin Capability ID:', adminCapId);
97
+
98
+ // Store this ID - you'll need it to configure the registry
99
+ await database.saveAdminCap({
100
+ registryName: 'my-app-payments',
101
+ adminCapId: adminCapId,
102
+ owner: senderAddress,
103
+ });
104
+ }
105
+ ```
106
+
107
+ ### Deriving the registry ID
108
+
109
+ Payment Kit derives registry IDs deterministically from the registry name:
110
+
111
+ ```ts
112
+ // You can compute the registry ID without querying the chain
113
+ const registryName = 'random-registry-name';
114
+ const registryId = client.paymentKit.getRegistryIdFromName(registryName);
115
+
116
+ console.log('Registry ID:', registryId);
117
+ ```
118
+
119
+ This allows you to reference registries by name throughout your application.
120
+
121
+ ## Registry configuration
122
+
123
+ Registries have two main configuration options:
124
+
125
+ ### Step 1: Epoch expiration duration
126
+
127
+ Controls how long payment records persist before they can be deleted.
128
+
129
+ **Default**: 30 epochs (~30 days on Mainnet)
130
+
131
+ ```ts
132
+ // Set expiration to 60 epochs
133
+ const tx = client.paymentKit.tx.setConfigEpochExpirationDuration({
134
+ registryName: 'my-app-payments',
135
+ epochExpirationDuration: 60,
136
+ adminCapId: adminCapId,
137
+ });
138
+
139
+ const result = await client.signAndExecuteTransaction({
140
+ transaction: tx,
141
+ signer: keypair,
142
+ });
143
+
144
+ // Check transaction status
145
+ if (result.$kind === 'FailedTransaction') {
146
+ throw new Error(`Configuration update failed: ${result.FailedTransaction.status.error?.message}`);
147
+ }
148
+
149
+ console.log('Expiration set to 60 epochs');
150
+ ```
151
+
152
+ **How It Works:**
153
+
154
+ When a payment is recorded, it stores the current epoch. After the expiration duration has passed,
155
+ anyone can delete the record to reclaim storage fees:
156
+
157
+ ```ts
158
+ // Current epoch: 1000
159
+ // Payment recorded at epoch: 1000
160
+ // Expiration duration: 30
161
+ // Can be deleted after epoch: 1030
162
+ ```
163
+
164
+ **Use Cases:**
165
+
166
+ - **Short expiration (7-14 epochs)**: High-volume, time-sensitive payments (subscriptions, tickets)
167
+ - **Medium expiration (30-60 epochs)**: Standard e-commerce transactions
168
+ - **Long expiration (180+ epochs)**: Important financial records requiring long-term verification
169
+
170
+ ### Step 2: Registry managed funds
171
+
172
+ Controls whether payments must be sent to the registry itself for later withdrawal.
173
+
174
+ **Default**: Disabled (funds go directly to receivers)
175
+
176
+ ```ts
177
+ // Enable registry-managed funds
178
+ const tx = client.paymentKit.tx.setConfigRegistryManagedFunds({
179
+ registryName: 'my-app-payments',
180
+ registryManagedFunds: true,
181
+ adminCapId: adminCapId,
182
+ });
183
+
184
+ await client.signAndExecuteTransaction({
185
+ transaction: tx,
186
+ signer: keypair,
187
+ });
188
+
189
+ console.log('Registry now manages funds');
190
+ ```
191
+
192
+ **How it works:**
193
+
194
+ When enabled:
195
+
196
+ - All payments must specify the registry as the receiver
197
+ - Coins are transferred to the registry object
198
+ - Registry admin can withdraw accumulated funds later
199
+ - Simplifies coin merging for high-throughput scenarios
200
+
201
+ ```ts
202
+ // With registry-managed funds enabled
203
+ const registryId = getRegistryIdFromName('my-app-payments', namespaceId);
204
+
205
+ const tx = client.paymentKit.tx.processRegistryPayment({
206
+ nonce: crypto.randomUUID(),
207
+ coinType: '0x2::sui::SUI',
208
+ amount: 1000000000,
209
+ receiver: registryId, // Must be the registry itself
210
+ sender: senderAddress,
211
+ registryName: 'my-app-payments',
212
+ });
213
+ ```
214
+
215
+ **Use Cases:**
216
+
217
+ - Platforms collecting payments on behalf of sellers
218
+ - Applications that need to batch process payouts
219
+ - Scenarios with very high transaction volumes
220
+ - Simplified accounting and reconciliation
221
+
222
+ ### Configuring both settings
223
+
224
+ You can configure both settings independently:
225
+
226
+ ```ts
227
+
228
+ const tx = new Transaction();
229
+
230
+ // Set expiration duration
231
+ tx.add(
232
+ client.paymentKit.calls.setConfigEpochExpirationDuration({
233
+ registryName: 'my-app-payments',
234
+ epochExpirationDuration: 90,
235
+ adminCapId: adminCapId,
236
+ }),
237
+ );
238
+
239
+ // Enable managed funds
240
+ tx.add(
241
+ client.paymentKit.calls.setConfigRegistryManagedFunds({
242
+ registryName: 'my-app-payments',
243
+ registryManagedFunds: true,
244
+ adminCapId: adminCapId,
245
+ }),
246
+ );
247
+
248
+ // Apply both configurations in one transaction
249
+ await client.signAndExecuteTransaction({
250
+ transaction: tx,
251
+ signer: keypair,
252
+ });
253
+ ```
254
+
255
+ ## Withdrawing funds from a registry
256
+
257
+ If you've enabled registry-managed funds, you can withdraw accumulated coins:
258
+
259
+ ```ts
260
+ // Withdraw all SUI from the registry
261
+ const tx = client.paymentKit.tx.withdrawFromRegistry({
262
+ coinType: '0x2::sui::SUI',
263
+ registryName: 'my-app-payments',
264
+ adminCapId: adminCapId,
265
+ });
266
+
267
+ const result = await client.signAndExecuteTransaction({
268
+ transaction: tx,
269
+ signer: keypair,
270
+ options: {
271
+ showEffects: true,
272
+ },
273
+ });
274
+
275
+ console.log('Funds withdrawn!');
276
+ ```
277
+
278
+ **Important notes:**
279
+
280
+ - Only the admin cap owner can withdraw funds
281
+ - You must specify the coin type to withdraw
282
+ - All coins of that type are withdrawn in one operation
283
+ - The withdrawn coins are sent to the transaction sender
284
+
285
+ ### Withdrawing multiple coin types
286
+
287
+ If your registry accumulates different coin types:
288
+
289
+ ```ts
290
+ const tx = new Transaction();
291
+
292
+ // Withdraw SUI
293
+ tx.add(
294
+ client.paymentKit.calls.withdrawFromRegistry({
295
+ coinType: '0x2::sui::SUI',
296
+ registryName: 'my-app-payments',
297
+ adminCapId: adminCapId,
298
+ }),
299
+ );
300
+
301
+ // Withdraw custom token
302
+ tx.add(
303
+ client.paymentKit.calls.withdrawFromRegistry({
304
+ coinType: '0xabc123::my_token::MY_TOKEN',
305
+ registryName: 'my-app-payments',
306
+ adminCapId: adminCapId,
307
+ }),
308
+ );
309
+
310
+ await client.signAndExecuteTransaction({
311
+ transaction: tx,
312
+ signer: keypair,
313
+ });
314
+ ```
315
+
316
+ ## Deleting expired payment records
317
+
318
+ After the expiration period, payment records can be deleted to reclaim storage fees:
319
+
320
+ ```ts
321
+ // Delete an expired payment record
322
+ const tx = client.paymentKit.tx.deletePaymentRecord({
323
+ nonce: crypto.randomUUID(),
324
+ coinType: '0x2::sui::SUI',
325
+ amount: 1000000000,
326
+ receiver,
327
+ registryName: 'my-app-payments',
328
+ });
329
+
330
+ const result = await client.signAndExecuteTransaction({
331
+ transaction: tx,
332
+ signer: keypair,
333
+ });
334
+
335
+ console.log('Record deleted, storage rebate received');
336
+ ```
337
+
338
+ **Key points:**
339
+
340
+ - Records can only be deleted after expiration
341
+ - Anyone can delete expired records (permissionless cleanup)
342
+ - The deleter receives a small storage rebate
343
+ - This incentivizes automatic cleanup of old records
344
+
345
+ ### Checking if a record can be deleted
346
+
347
+ ```ts
348
+ // Get the payment record
349
+ const record = await client.paymentKit.getPaymentRecord({
350
+ nonce: crypto.randomUUID(),
351
+ coinType: '0x2::sui::SUI',
352
+ amount: 1000000000,
353
+ receiver,
354
+ registryName: 'my-app-payments',
355
+ });
356
+
357
+ if (record) {
358
+ const recordEpoch = parseInt(record.epochAtTimeOfRecord);
359
+ const currentEpoch = await client.core
360
+ .getLatestSuiSystemState()
361
+ .then((state) => Number(state.epoch));
362
+
363
+ const expirationDuration = 30; // Your registry's setting
364
+ const canDelete = currentEpoch >= recordEpoch + expirationDuration;
365
+
366
+ console.log('Record epoch:', recordEpoch);
367
+ console.log('Current epoch:', currentEpoch);
368
+ console.log('Can delete:', canDelete);
369
+ }
370
+ ```
371
+
372
+ ## Using registry ID directly
373
+
374
+ If you know the registry ID, you can use it instead of the name:
375
+
376
+ ```ts
377
+ const registryId = '0x123abc...';
378
+
379
+ // Process payment using registry ID
380
+ const tx = client.paymentKit.tx.processRegistryPayment({
381
+ nonce: crypto.randomUUID(),
382
+ coinType: '0x2::sui::SUI',
383
+ amount: 1000000000,
384
+ receiver,
385
+ sender: senderAddress,
386
+ registryId: registryId, // Use ID instead of name
387
+ });
388
+
389
+ // Query using registry ID
390
+ const record = await client.paymentKit.getPaymentRecord({
391
+ nonce: crypto.randomUUID(),
392
+ coinType: '0x2::sui::SUI',
393
+ amount: 1000000000,
394
+ receiver,
395
+ registryId: registryId,
396
+ });
397
+ ```
398
+
399
+ ## Complete registry setup example
400
+
401
+ Here's a complete workflow for setting up a custom registry:
402
+
403
+ ```ts
404
+
405
+ const client = new SuiGrpcClient({
406
+ network: 'testnet',
407
+ baseUrl: 'https://fullnode.testnet.sui.io:443',
408
+ }).$extend(paymentKit());
409
+
410
+ const keypair = Ed25519Keypair.generate();
411
+ const registryName = 'my-marketplace-registry';
412
+
413
+ // Step 1: Create the registry
414
+ console.log('Creating registry...');
415
+ const createTx = client.paymentKit.tx.createRegistry({
416
+ registryName: registryName,
417
+ });
418
+
419
+ const createResult = await client.signAndExecuteTransaction({
420
+ transaction: createTx,
421
+ signer: keypair,
422
+ options: {
423
+ showEffects: true,
424
+ showObjectChanges: true,
425
+ },
426
+ });
427
+
428
+ // Check transaction status
429
+ if (createResult.$kind === 'FailedTransaction') {
430
+ throw new Error(
431
+ `Registry creation failed: ${createResult.FailedTransaction.status.error?.message}`,
432
+ );
433
+ }
434
+
435
+ // Step 2: Extract the admin cap
436
+ const adminCapObject = createResult.Transaction.objectChanges?.find(
437
+ (change) => change.type === 'created' && change.objectType.includes('RegistryAdminCap'),
438
+ );
439
+
440
+ const adminCapId = adminCapObject && 'objectId' in adminCapObject ? adminCapObject.objectId : '';
441
+
442
+ console.log('Registry created!');
443
+ console.log('Admin Cap ID:', adminCapId);
444
+
445
+ // Step 3: Configure the registry
446
+ console.log('Configuring registry...');
447
+ const configTx = new Transaction();
448
+
449
+ // Set 60-epoch expiration
450
+ configTx.add(
451
+ client.paymentKit.calls.setConfigEpochExpirationDuration({
452
+ registryName: registryName,
453
+ epochExpirationDuration: 60,
454
+ adminCapId: adminCapId,
455
+ }),
456
+ );
457
+
458
+ // Enable managed funds
459
+ configTx.add(
460
+ client.paymentKit.calls.setConfigRegistryManagedFunds({
461
+ registryName: registryName,
462
+ registryManagedFunds: true,
463
+ adminCapId: adminCapId,
464
+ }),
465
+ );
466
+
467
+ const configResult = await client.signAndExecuteTransaction({
468
+ transaction: configTx,
469
+ signer: keypair,
470
+ });
471
+
472
+ // Check transaction status
473
+ if (configResult.$kind === 'FailedTransaction') {
474
+ throw new Error(`Configuration failed: ${configResult.FailedTransaction.status.error?.message}`);
475
+ }
476
+
477
+ console.log('Registry configured!');
478
+ console.log('Ready to process payments');
479
+
480
+ // Step 4: Process a payment
481
+ const registryId = getRegistryIdFromName(registryName, namespaceId);
482
+
483
+ const paymentTx = client.paymentKit.tx.processRegistryPayment({
484
+ nonce: crypto.randomUUID(),
485
+ coinType: '0x2::sui::SUI',
486
+ amount: 1000000000,
487
+ receiver: registryId, // Funds go to registry
488
+ sender: keypair.getPublicKey().toSuiAddress(),
489
+ registryName: registryName,
490
+ });
491
+
492
+ const paymentResult = await client.signAndExecuteTransaction({
493
+ transaction: paymentTx,
494
+ signer: keypair,
495
+ });
496
+
497
+ // Check transaction status
498
+ if (paymentResult.$kind === 'FailedTransaction') {
499
+ throw new Error(`Payment failed: ${paymentResult.FailedTransaction.status.error?.message}`);
500
+ }
501
+
502
+ console.log('First payment processed:', paymentResult.Transaction.digest);
503
+ ```
504
+
505
+ ## Best practices
506
+
507
+ 1. **Store admin caps securely**: Treat your admin capability like private keys - they control your
508
+ registry
509
+
510
+ 2. **Use descriptive names**: Choose registry names that clearly identify their purpose (for
511
+ example, `acme-subscriptions`, `store-123-payments`)
512
+
513
+ 3. **Set appropriate expiration**: Balance storage costs with verification needs
514
+ - Short expiration = lower storage costs
515
+ - Long expiration = longer verification period
516
+
517
+ 4. **Monitor registry growth**: Track the number of payment records to anticipate storage costs
518
+
519
+ 5. **Plan fund management**: Decide upfront whether to use registry-managed funds based on your
520
+ withdrawal patterns
521
+
522
+ 6. **Document your configuration**: Keep records of your registry settings for operational
523
+ consistency
524
+
525
+ 7. **Test thoroughly**: Always test registry operations on Testnet before Mainnet deployment
526
+
527
+ ## Next steps
528
+
529
+ - [SDK API Reference](./payment-kit-sdk) - Complete SDK API documentation
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mysten/payment-kit",
3
- "version": "0.1.10",
3
+ "version": "0.1.11",
4
4
  "private": false,
5
5
  "description": "Sui Payment Kit",
6
6
  "license": "Apache-2.0",
@@ -32,10 +32,10 @@
32
32
  "vitest": "^4.0.17"
33
33
  },
34
34
  "dependencies": {
35
- "@mysten/bcs": "^2.0.4"
35
+ "@mysten/bcs": "^2.0.5"
36
36
  },
37
37
  "peerDependencies": {
38
- "@mysten/sui": "^2.16.1"
38
+ "@mysten/sui": "^2.16.2"
39
39
  },
40
40
  "scripts": {
41
41
  "clean": "rm -rf tsconfig.tsbuildinfo ./dist",