@ickb/sdk 1000.0.52 → 1000.0.81

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/src/sdk.ts ADDED
@@ -0,0 +1,513 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import {
3
+ collect,
4
+ CapacityManager,
5
+ SmartTransaction,
6
+ binarySearch,
7
+ type ValueComponents,
8
+ hexFrom,
9
+ getHeader,
10
+ Epoch,
11
+ } from "@ickb/utils";
12
+ import {
13
+ convert,
14
+ ICKB_DEPOSIT_CAP,
15
+ ickbExchangeRatio,
16
+ type LogicManager,
17
+ type OwnedOwnerManager,
18
+ } from "@ickb/core";
19
+ import {
20
+ Info,
21
+ OrderManager,
22
+ Ratio,
23
+ type OrderCell,
24
+ type OrderGroup,
25
+ } from "@ickb/order";
26
+ import { getConfig } from "./constants.js";
27
+ import { PoolSnapshot } from "./codec.js";
28
+
29
+ /**
30
+ * SDK for managing iCKB operations.
31
+ */
32
+ export class IckbSdk {
33
+ /**
34
+ * Creates an instance of IckbSdk.
35
+ *
36
+ * @param ownedOwner - The manager for owned owner operations.
37
+ * @param ickbLogic - The manager for iCKB logic operations.
38
+ * @param order - The manager for order operations.
39
+ * @param capacity - The capacity manager instance.
40
+ * @param bots - An array of bot lock scripts.
41
+ */
42
+ constructor(
43
+ private readonly ownedOwner: OwnedOwnerManager,
44
+ private readonly ickbLogic: LogicManager,
45
+ private readonly order: OrderManager,
46
+ private readonly capacity: CapacityManager,
47
+ private readonly bots: ccc.Script[],
48
+ ) {}
49
+
50
+ /**
51
+ * Creates an instance of IckbSdk from script dependencies.
52
+ *
53
+ * @param args - Parameters matching those of getConfig.
54
+ * @returns A new instance of IckbSdk.
55
+ */
56
+ static from(...args: Parameters<typeof getConfig>): IckbSdk {
57
+ const {
58
+ managers: { ownedOwner, logic, order, capacity },
59
+ bots,
60
+ } = getConfig(...args);
61
+
62
+ return new IckbSdk(ownedOwner, logic, order, capacity, bots);
63
+ }
64
+
65
+ /**
66
+ * Previews the conversion between CKB and UDT.
67
+ *
68
+ * This method calculates a conversion preview using an exchange ratio midpoint.
69
+ * Optionally, a fee may be applied that influences the effective conversion rate,
70
+ * scaling the converted amount by (feeBase - fee) / feeBase.
71
+ *
72
+ * @param isCkb2Udt - Indicates the conversion direction:
73
+ * - true: Convert CKB to UDT.
74
+ * - false: Convert UDT to CKB.
75
+ * @param amounts - An object containing value components (amounts for CKB and UDT).
76
+ * @param system - The current system state containing exchange ratio, fee rate, tip, and order-related information.
77
+ * @param options - Optional parameters for fee and matching:
78
+ * - fee: The fee to apply in integer terms (defaults to 1n for 0.001% fee).
79
+ * - feeBase: The base used for fee scaling (defaults to 100000n).
80
+ *
81
+ * @returns An object with:
82
+ * - convertedAmount: The estimated converted amount as a FixedPoint.
83
+ * - ckbFee: The fee (or gain) in CKB, as a FixedPoint.
84
+ * - info: Additional conversion metadata.
85
+ * - maturity: Optional maturity information (as ccc.Num) if meets criteria.
86
+ */
87
+ static estimate(
88
+ isCkb2Udt: boolean,
89
+ amounts: ValueComponents,
90
+ system: SystemState,
91
+ options?: {
92
+ fee?: ccc.Num;
93
+ feeBase?: ccc.Num;
94
+ },
95
+ ): {
96
+ convertedAmount: ccc.FixedPoint;
97
+ ckbFee: ccc.FixedPoint;
98
+ info: Info;
99
+ maturity: ccc.Num | undefined;
100
+ } {
101
+ // Apply a 0.001% default fee if none provided.
102
+ options = {
103
+ fee: 1n,
104
+ feeBase: 100000n,
105
+ ...options,
106
+ };
107
+ const { convertedAmount, ckbFee, info } = OrderManager.convert(
108
+ isCkb2Udt,
109
+ system.exchangeRatio,
110
+ amounts,
111
+ options,
112
+ );
113
+
114
+ // If the fee meets a threshold, calculate the order maturity; otherwise, maturity is undefined.
115
+ const maturity =
116
+ ckbFee >= 10n * system.feeRate
117
+ ? IckbSdk.maturity({ info, amounts }, system)
118
+ : undefined;
119
+
120
+ return { convertedAmount, ckbFee, info, maturity };
121
+ }
122
+
123
+ /**
124
+ * Estimates the maturity for an order formatted as a Unix timestamp.
125
+ *
126
+ * Depending on the order type and amount remaining, the method calculates an estimated timestamp
127
+ * when the order (or part thereof) might be fulfilled.
128
+ *
129
+ * @param o - Either an OrderCell or an object containing order Info and value components.
130
+ * @param system - The current system state.
131
+ * @returns The Unix timestamp of estimated maturity as a bigint (in milliseconds) or undefined if not applicable.
132
+ */
133
+ static maturity(
134
+ o:
135
+ | OrderCell
136
+ | {
137
+ info: Info;
138
+ amounts: ValueComponents;
139
+ },
140
+ system: SystemState,
141
+ ): bigint | undefined {
142
+ const info = "info" in o ? o.info : o.data.info;
143
+ const amounts =
144
+ "amounts" in o
145
+ ? o.amounts
146
+ : { ckbValue: o.ckbUnoccupied, udtValue: o.udtValue };
147
+
148
+ // Dual-ratio orders have no fixed maturity.
149
+ if (info.isDualRatio()) {
150
+ return;
151
+ }
152
+
153
+ const isCkb2Udt = info.isCkb2Udt();
154
+ const amount = isCkb2Udt ? amounts.ckbValue : amounts.udtValue;
155
+ const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
156
+
157
+ // If order is already fulfilled.
158
+ if (amount === 0n) {
159
+ return 0n;
160
+ }
161
+
162
+ const { tip, exchangeRatio, orderPool, ckbAvailable, ckbMaturing } = system;
163
+
164
+ // Create a reference ratio instance for comparison.
165
+ const b = new Info(ratio, ratio, 1);
166
+ let ckb = isCkb2Udt ? amount : 0n;
167
+ let udt = isCkb2Udt ? 0n : amount;
168
+ for (const o of orderPool) {
169
+ const a = o.data.info;
170
+ if (a.isCkb2Udt()) {
171
+ // If not isCkb2Udt or a worse ratio, add available CKB.
172
+ if (!isCkb2Udt || a.ckb2UdtCompare(b) < 0) {
173
+ ckb += o.ckbUnoccupied;
174
+ }
175
+ } else {
176
+ // Conversely for UDT to CKB orders.
177
+ if (isCkb2Udt || a.udt2CkbCompare(b) < 0) {
178
+ udt += o.udtValue;
179
+ }
180
+ }
181
+ }
182
+ // Adjust ckb by the converted UDT amount.
183
+ ckb -= convert(false, udt, exchangeRatio);
184
+
185
+ // Minimum maturity of 10 minutes (in milliseconds).
186
+ let maturity = 10n * 60n * 1000n;
187
+ if (isCkb2Udt) {
188
+ // For CKB to UDT orders, extend maturity based on the CKB amount.
189
+ if (ckb > 0n) {
190
+ maturity *= 1n + ckb / ccc.fixedPointFrom("200000");
191
+ }
192
+ return maturity + ("info" in o ? BigInt(Date.now()) : tip.timestamp);
193
+ }
194
+
195
+ // For UDT to CKB orders, add available CKB.
196
+ ckb += ckbAvailable;
197
+ if (ckb >= 0) {
198
+ return maturity + ("info" in o ? BigInt(Date.now()) : tip.timestamp);
199
+ }
200
+
201
+ // Find the earliest maturity in the ckbMaturing array that satisfies the required CKB.
202
+ const ckbNeeded = -ckb;
203
+ const i = binarySearch(
204
+ ckbMaturing.length,
205
+ // eslint-disable-next-line @typescript-eslint/no-non-null-assertion
206
+ (n) => ckbMaturing[n]!.ckbCumulative >= ckbNeeded,
207
+ );
208
+
209
+ return ckbMaturing[i]?.maturity;
210
+ }
211
+
212
+ /**
213
+ * Mints a new order cell and appends it to the transaction.
214
+ *
215
+ * The method performs the following operations:
216
+ * - Creates order cell data using provided amounts and order information.
217
+ * - Adds required cell dependencies and UDT handlers to the transaction.
218
+ * - Appends the order cell to the transaction outputs.
219
+ *
220
+ * @param tx - The smart transaction to which the order cell is added.
221
+ * @param user - The user, represented either as a Signer or a Script.
222
+ * @param info - The order information meta data (usually computed via OrderManager.convert).
223
+ * @param amounts - The value components for the order, including:
224
+ * - ckbValue: The CKB amount (may include an internal surplus).
225
+ * - udtValue: The UDT amount.
226
+ *
227
+ * @returns A Promise resolving to void.
228
+ */
229
+ async request(
230
+ tx: SmartTransaction,
231
+ user: ccc.Signer | ccc.Script,
232
+ info: Info,
233
+ amounts: ValueComponents,
234
+ ): Promise<void> {
235
+ // If the user is provided as a Signer, extract the recommended lock script.
236
+ user =
237
+ "codeHash" in user
238
+ ? user
239
+ : (await user.getRecommendedAddressObj()).script;
240
+
241
+ this.order.mint(tx, user, info, amounts);
242
+ }
243
+
244
+ /**
245
+ * Melts (cancels) the specified order groups.
246
+ *
247
+ * For each order group, if the option is set to process fulfilled orders only,
248
+ * it filters accordingly. Then, for every valid group, the master and order cells are added
249
+ * as inputs to the transaction.
250
+ *
251
+ * @param tx - The smart transaction to which the inputs are added.
252
+ * @param groups - An array of order groups to be melted.
253
+ * @param options - Optional parameters:
254
+ * - isFulfilledOnly: If true, only order groups with fully or partially fulfilled orders are processed.
255
+ *
256
+ * @returns void
257
+ */
258
+ collect(
259
+ tx: SmartTransaction,
260
+ groups: OrderGroup[],
261
+ options?: {
262
+ isFulfilledOnly?: boolean;
263
+ },
264
+ ): void {
265
+ this.order.melt(tx, groups, options);
266
+ }
267
+
268
+ /**
269
+ * Retrieves the L1 state from the blockchain.
270
+ *
271
+ * The method performs the following:
272
+ * - Obtains the current block tip and calculates the exchange ratio.
273
+ * - Fetches available CKB and the maturing CKB based on bot capacities and deposit snapshots.
274
+ * - Filters orders into user-owned and system orders based on the provided locks.
275
+ * - Estimates user-owned orders maturity.
276
+ *
277
+ * @param client - The blockchain client interface.
278
+ * @param locks - An array of lock scripts used to filter user cells.
279
+ * @returns A promise that resolves to an object containing:
280
+ * - system: The system state (fee rate, tip header, exchange ratio, order pool, etc.).
281
+ * - user: The user's orders grouped as an array of OrderGroup.
282
+ */
283
+ async getL1State(
284
+ client: ccc.Client,
285
+ locks: ccc.Script[],
286
+ ): Promise<{ system: SystemState; user: { orders: OrderGroup[] } }> {
287
+ const tip = await client.getTipHeader();
288
+ const exchangeRatio = Ratio.from(ickbExchangeRatio(tip));
289
+
290
+ // Parallel fetching of system components.
291
+ const [{ ckbAvailable, ckbMaturing }, orders, feeRate] = await Promise.all([
292
+ this.getCkb(client, tip),
293
+ collect(this.order.findOrders(client)),
294
+ client.getFeeRate(),
295
+ ]);
296
+
297
+ const midInfo = new Info(exchangeRatio, exchangeRatio, 1);
298
+ const userOrders: OrderGroup[] = [];
299
+ const systemOrders: OrderCell[] = [];
300
+ for (const group of orders) {
301
+ if (group.isOwner(...locks)) {
302
+ userOrders.push(group);
303
+ }
304
+
305
+ const { order } = group;
306
+ const info = order.data.info;
307
+ if (
308
+ (order.isCkb2UdtMatchable() && info.ckb2UdtCompare(midInfo) < 0) ||
309
+ (order.isUdt2CkbMatchable() && info.udt2CkbCompare(midInfo) < 0)
310
+ ) {
311
+ systemOrders.push(order);
312
+ }
313
+ }
314
+
315
+ const system = {
316
+ feeRate,
317
+ tip,
318
+ exchangeRatio,
319
+ orderPool: systemOrders,
320
+ ckbAvailable,
321
+ ckbMaturing,
322
+ };
323
+
324
+ // Estimates user orders maturity.
325
+ for (const { order } of userOrders) {
326
+ order.maturity = IckbSdk.maturity(order, system);
327
+ }
328
+
329
+ return {
330
+ system,
331
+ user: {
332
+ orders: userOrders,
333
+ },
334
+ };
335
+ }
336
+
337
+ /**
338
+ * Retrieves available CKB and maturing CKB values from the blockchain.
339
+ *
340
+ * This method:
341
+ * - Fetches bot withdrawal requests and deposit snapshots.
342
+ * - Aggregates available CKB balances from bot capacities.
343
+ * - Calculates maturing CKB values (with their expected maturity timestamps)
344
+ * based on deposit pool snapshots or via direct deposit cell lookups.
345
+ * - Sorts and cumulatively sums the maturing values for later lookup.
346
+ *
347
+ * @param client - The blockchain client used for fetching data.
348
+ * @param tip - The current block tip header.
349
+ * @returns A Promise that resolves with:
350
+ * - ckbAvailable: The total available CKB (as a FixedPoint).
351
+ * - ckbMaturing: An array of maturing CKB objects containing the cumulative CKB
352
+ * and the associated maturity timestamp.
353
+ */
354
+ private async getCkb(
355
+ client: ccc.Client,
356
+ tip: ccc.ClientBlockHeader,
357
+ ): Promise<{
358
+ ckbAvailable: ccc.FixedPoint;
359
+ ckbMaturing: CkbCumulative[];
360
+ }> {
361
+ const opts = {
362
+ onChain: true,
363
+ tip,
364
+ };
365
+
366
+ // Start fetching bot iCKB withdrawal requests.
367
+ const promiseBotWithdrawals = collect(
368
+ this.ownedOwner.findWithdrawalGroups(client, this.bots, opts),
369
+ );
370
+
371
+ // Initialize deposit pool snapshot.
372
+ let poolSnapshotHex: ccc.Hex = "0x";
373
+ let poolSnapshotEpoch = Epoch.from([0n, 0n, 1n]);
374
+ // Map to track each bot's available CKB (minus a reserved amount for internal operations).
375
+ const bot2Ckb = new Map<string, ccc.FixedPoint>();
376
+ const reserved = -ccc.fixedPointFrom("2000");
377
+ for await (const c of this.capacity.findCapacities(
378
+ client,
379
+ this.bots,
380
+ opts,
381
+ )) {
382
+ const key = hexFrom(c.cell.cellOutput.lock);
383
+ const ckb = (bot2Ckb.get(key) ?? reserved) + c.ckbValue;
384
+ bot2Ckb.set(key, ckb);
385
+
386
+ // Find the most recent deposit pool snapshot from bot cell output data.
387
+ const outputData = c.cell.outputData;
388
+ if (outputData.length % 256 === 2) {
389
+ const h = await getHeader(client, {
390
+ type: "txHash",
391
+ value: c.cell.outPoint.txHash,
392
+ });
393
+ const e = Epoch.from(h.epoch);
394
+ if (poolSnapshotEpoch.compare(e) < 0) {
395
+ poolSnapshotHex = outputData;
396
+ poolSnapshotEpoch = e;
397
+ }
398
+ }
399
+ }
400
+
401
+ const ckbMaturing = new Array<{
402
+ ckbValue: ccc.FixedPoint;
403
+ maturity: ccc.Num;
404
+ }>();
405
+ for (const wr of await promiseBotWithdrawals) {
406
+ if (wr.owned.isReady) {
407
+ // Update the bot's CKB based on withdrawal if the bot is ready.
408
+ const key = hexFrom(wr.owner.cell.cellOutput.lock);
409
+ const ckb = (bot2Ckb.get(key) ?? reserved) + wr.ckbValue;
410
+ bot2Ckb.set(key, ckb);
411
+ continue;
412
+ }
413
+
414
+ // Otherwise, add to maturing amounts.
415
+ ckbMaturing.push({
416
+ ckbValue: wr.ckbValue,
417
+ maturity: wr.owned.maturity.toUnix(tip),
418
+ });
419
+ }
420
+
421
+ // Sum available CKB across all bot lock scripts.
422
+ let ckbAvailable = 0n;
423
+ for (const ckb of bot2Ckb.values()) {
424
+ if (ckb > 0n) {
425
+ ckbAvailable += ckb;
426
+ }
427
+ }
428
+
429
+ // Estimate available CKB from deposit pool snapshot.
430
+ const tipEpoch = Epoch.from(tip.epoch);
431
+ const oneCycle = Epoch.from([180n, 0n, 1n]);
432
+ if (poolSnapshotHex !== "0x") {
433
+ const eNumber = tip.epoch[0];
434
+ let start = Epoch.from([eNumber - (eNumber % 180n), 0n, 1n]);
435
+ const step = Epoch.from([0n, 180n, 1024n]);
436
+ const depositSize = convert(false, ICKB_DEPOSIT_CAP, tip);
437
+ for (const binAmount of PoolSnapshot.decode(poolSnapshotHex)) {
438
+ const end = start.add(step);
439
+
440
+ if (binAmount > 0) {
441
+ ckbMaturing.push({
442
+ ckbValue: BigInt(binAmount) * depositSize,
443
+ maturity:
444
+ tipEpoch.compare(tipEpoch) < 0
445
+ ? // If the bin has already started, assume worst-case timing.
446
+ end.add(oneCycle).toUnix(tip)
447
+ : // Otherwise, use the bin end as the maturity.
448
+ end.toUnix(tip),
449
+ });
450
+ }
451
+
452
+ start = end;
453
+ }
454
+ } else {
455
+ // Without snapshot data, fetch deposits directly.
456
+ for await (const d of this.ickbLogic.findDeposits(client, opts)) {
457
+ ckbMaturing.push({
458
+ ckbValue: d.ckbValue,
459
+ maturity: d.maturity.toUnix(tip),
460
+ });
461
+ }
462
+ }
463
+
464
+ // Sort maturing CKB entries by their maturity timestamp.
465
+ ckbMaturing.sort((a, b) => Number(a.maturity - b.maturity));
466
+
467
+ // Calculate cumulative maturing CKB values.
468
+ let cumulative = 0n;
469
+ const ckbCumulativeMaturing: CkbCumulative[] = [];
470
+ for (const { ckbValue, maturity } of ckbMaturing) {
471
+ cumulative += ckbValue;
472
+ ckbCumulativeMaturing.push({ ckbCumulative: cumulative, maturity });
473
+ }
474
+
475
+ return {
476
+ ckbAvailable,
477
+ ckbMaturing: ckbCumulativeMaturing,
478
+ };
479
+ }
480
+ }
481
+
482
+ /**
483
+ * Represents the state of the system.
484
+ */
485
+ export interface SystemState {
486
+ /** The fee rate for transactions. */
487
+ feeRate: ccc.Num;
488
+
489
+ /** The tip for the current block header. */
490
+ tip: ccc.ClientBlockHeader;
491
+
492
+ /** The exchange ratio between CKB and UDT. */
493
+ exchangeRatio: Ratio;
494
+
495
+ /** The order pool containing order cells matching system criteria. */
496
+ orderPool: OrderCell[];
497
+
498
+ /** The total available CKB (as FixedPoint). */
499
+ ckbAvailable: ccc.FixedPoint;
500
+
501
+ /** Array of CKB maturing entries with cumulative amounts and maturity timestamps. */
502
+ ckbMaturing: CkbCumulative[];
503
+ }
504
+
505
+ /**
506
+ * Represents a cumulative CKB maturing entry.
507
+ */
508
+ export interface CkbCumulative {
509
+ /** The cumulative CKB value (as FixedPoint) up to this maturity. */
510
+ ckbCumulative: ccc.FixedPoint;
511
+ /** The maturity timestamp (as ccc.Num). */
512
+ maturity: ccc.Num;
513
+ }
@@ -1,190 +0,0 @@
1
- import { ccc } from "@ckb-ccc/core";
2
- import { SmartTransaction, type ScriptDeps, type UdtCell, type CapacityCell, type ExchangeRatio } from "@ickb/utils";
3
- import { IckbUdtManager, LogicManager, OwnedOwnerManager, type IckbDepositCell, type ReceiptCell, type WithdrawalGroup } from "@ickb/core";
4
- import { DaoManager, type DaoCell } from "@ickb/dao";
5
- import { OrderManager, type OrderCell, type OrderGroup } from "@ickb/order";
6
- /**
7
- * Controller for managing iCKB operations.
8
- */
9
- export declare class IckbController {
10
- readonly ickbUdtManager: IckbUdtManager;
11
- readonly logicManager: LogicManager;
12
- readonly ownedOwnerManager: OwnedOwnerManager;
13
- readonly orderManager: OrderManager;
14
- readonly daoManager: DaoManager;
15
- readonly bots: ccc.Script[];
16
- /**
17
- * Creates an instance of IckbController.
18
- * @param ickbUdtManager - The manager for iCKB UDT operations.
19
- * @param logicManager - The logic manager for handling business logic.
20
- * @param ownedOwnerManager - The manager for owned owner operations.
21
- * @param orderManager - The manager for order operations.
22
- * @param daoManager - The manager for DAO operations.
23
- */
24
- constructor(ickbUdtManager: IckbUdtManager, logicManager: LogicManager, ownedOwnerManager: OwnedOwnerManager, orderManager: OrderManager, daoManager: DaoManager, bots: ccc.Script[]);
25
- /**
26
- * Creates an instance of IckbController from script dependencies.
27
- * @param deps - The script dependencies.
28
- * @param deps.udt - The script dependencies for UDT.
29
- * @param deps.dao - The script dependencies for DAO.
30
- * @param deps.ickbLogic - The script dependencies for iCKB logic.
31
- * @param deps.ownedOwner - The script dependencies for owned owner.
32
- * @param deps.order - The script dependencies for order.
33
- * @returns An instance of IckbController.
34
- */
35
- static fromDeps(deps: {
36
- udt: ScriptDeps;
37
- dao: ScriptDeps;
38
- ickbLogic: ScriptDeps;
39
- ownedOwner: ScriptDeps;
40
- order: ScriptDeps;
41
- }, bots: ccc.Script[]): IckbController;
42
- /**
43
- * Previews the conversion between CKB and UDT.
44
- * @param conversion - The conversion parameters.
45
- * @param conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
46
- * @param conversion.amount - The amount to convert.
47
- * @param conversion.rate - The exchange rate for conversion.
48
- * @param conversion.fee - The fee for the conversion.
49
- * @returns The rate and converted amount.
50
- *
51
- * Fee meaning examples:
52
- * - 100000n => 0.001%
53
- * - 10000n => 0.01%
54
- * - 1000n => 0.1%
55
- * - 100n => 1%
56
- * - 10n => 10%
57
- * - 1n => 100%
58
- * - less than 1n => 0%
59
- */
60
- static previewConversion(conversion: {
61
- isCkb2Udt: boolean;
62
- amount: ccc.FixedPoint;
63
- rate: ExchangeRatio | ccc.ClientBlockHeader;
64
- fee: ccc.Num;
65
- }): {
66
- rate: ExchangeRatio;
67
- convertedAmount: ccc.FixedPoint;
68
- };
69
- /**
70
- * Converts between CKB and UDT based on the provided options.
71
- * @param options - The conversion options.
72
- * @param options.conversion - The conversion parameters.
73
- * @param options.conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
74
- * @param options.conversion.amount - The amount to convert.
75
- * @param options.conversion.rate - The exchange rate for conversion.
76
- * @param options.conversion.ckbMinMatchLog - Minimum CKB match log (optional).
77
- * @param options.conversion.ckbMinFee - Minimum CKB fee (optional).
78
- * @param options.signer - The signer for the transaction.
79
- * @param options.userCells - The user's cells.
80
- * @param options.system - The current system state.
81
- * @param options.filters - The filters for the conversion.
82
- * @param options.filters.isReadyOnly - Indicates if only ready conversions should be considered.
83
- * @param options.filters.isFulfilledOnly - Indicates if only fulfilled conversions should be considered.
84
- * @returns A promise that resolves to the conversion result or undefined.
85
- */
86
- convert(options: {
87
- conversion: Parameters<typeof IckbController.previewConversion>[0] & {
88
- ckbMinMatchLog?: number;
89
- ckbMinFee?: ccc.Num;
90
- };
91
- signer: ccc.Signer;
92
- userCells: UserCells;
93
- system: SystemState;
94
- filters: {
95
- isReadyOnly: boolean;
96
- isFulfilledOnly: boolean;
97
- };
98
- }): Promise<Conversion | undefined>;
99
- /**
100
- * Attempts to perform a conversion based on the provided parameters.
101
- * @param n - The size of deposit to make or withdraw from.
102
- * @param conversion - The conversion parameters.
103
- * @param conversion.isCkb2Udt - Indicates if the conversion is from CKB to UDT.
104
- * @param conversion.amount - The amount to convert.
105
- * @param conversion.rate - The exchange rate for conversion.
106
- * @param conversion.info - Additional information for the conversion.
107
- * @param conversion.ckbMinFee - Minimum CKB fee.
108
- * @param conversion.lock - The script lock for the transaction.
109
- * @param options - Additional options for the conversion.
110
- * @returns A promise that resolves to the conversion result or undefined.
111
- */
112
- private attempt;
113
- /**
114
- * Retrieves the L1 state based on the provided parameters.
115
- * @param client - The client to interact with the blockchain.
116
- * @param filter - The set of filters to apply.
117
- * @param locks - The script locks to filter the user cells.
118
- * @param options - Optional parameters for the state retrieval.
119
- * @param options.minLockUp - The minimum lock-up period (optional).
120
- * @param options.maxLockUp - The maximum lock-up period (optional).
121
- * @returns A promise that resolves to an object containing the system state and user cells.
122
- */
123
- getL1State(client: ccc.Client, filter: Set<keyof UserCells | "depositPool" | "ckbBots">, locks: ccc.Script[], options?: {
124
- minLockUp?: ccc.Epoch;
125
- maxLockUp?: ccc.Epoch;
126
- }): Promise<{
127
- system: SystemState;
128
- user: UserCells;
129
- }>;
130
- }
131
- /**
132
- * Represents the state of the system.
133
- */
134
- export interface SystemState {
135
- /** The fee rate for transactions. */
136
- feeRate: ccc.Num;
137
- /** The tip for the current block header. */
138
- tip: ccc.ClientBlockHeader;
139
- /** The exchange ratio between CKB and UDT. */
140
- exchangeRatio: ExchangeRatio;
141
- /** The deposit cap expressed in CKB. */
142
- ckbDepositCap: ccc.FixedPoint;
143
- /** The deposit cap expressed in iCKB. */
144
- udtDepositCap: ccc.FixedPoint;
145
- /**
146
- * The deposit pool containing iCKB deposit cells with cumulative iCKB amounts.
147
- */
148
- depositPool: (IckbDepositCell & {
149
- udtCumulative: bigint;
150
- })[];
151
- /**
152
- * The order pool containing non-user order cells.
153
- */
154
- orderPool: OrderCell[];
155
- /**
156
- * The ckb held by bots ready to match incoming iCKB to CKB conversions.
157
- */
158
- ckbBots: ccc.FixedPoint;
159
- }
160
- /**
161
- * Represents the user cells in the system.
162
- */
163
- export interface UserCells {
164
- /** The capacities owned by the user. */
165
- capacities: CapacityCell[];
166
- /** The UDTs owned by the user. */
167
- udts: UdtCell[];
168
- /** The receipts associated with the user's transactions. */
169
- receipts: ReceiptCell[];
170
- /** The withdrawal groups for the user. */
171
- withdrawalGroups: WithdrawalGroup[];
172
- /** The orders placed by the user. */
173
- orders: OrderGroup[];
174
- /** The classical/non-iCKB deposits made by the user. */
175
- deposits: DaoCell[];
176
- /** The classical/non-iCKB withdrawal requests made by the user. */
177
- withdrawalRequests: DaoCell[];
178
- }
179
- /**
180
- * Represents a conversion transaction.
181
- */
182
- export interface Conversion {
183
- /** The smart transaction associated with the conversion. */
184
- tx: SmartTransaction;
185
- /** The fee in CKB for the order created by the conversion. */
186
- ckbFee: ccc.FixedPoint;
187
- /** The estimated maturity epoch for conversion. */
188
- maturity: ccc.Epoch;
189
- }
190
- //# sourceMappingURL=index.d.ts.map