@ickb/sdk 1000.0.82 → 9000.0.1

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 (157) hide show
  1. package/README.md +81 -32
  2. package/dist/constants.d.ts +13 -22
  3. package/dist/constants.d.ts.map +1 -1
  4. package/dist/constants.js +80 -76
  5. package/dist/constants.js.map +1 -1
  6. package/dist/conversion/estimate.d.ts +39 -0
  7. package/dist/conversion/estimate.d.ts.map +1 -0
  8. package/dist/conversion/estimate.js +117 -0
  9. package/dist/conversion/estimate.js.map +1 -0
  10. package/dist/conversion/maturity.d.ts +15 -0
  11. package/dist/conversion/maturity.d.ts.map +1 -0
  12. package/dist/conversion/maturity.js +112 -0
  13. package/dist/conversion/maturity.js.map +1 -0
  14. package/dist/conversion/plans.d.ts +20 -0
  15. package/dist/conversion/plans.d.ts.map +1 -0
  16. package/dist/conversion/plans.js +106 -0
  17. package/dist/conversion/plans.js.map +1 -0
  18. package/dist/conversion/projection.d.ts +22 -0
  19. package/dist/conversion/projection.d.ts.map +1 -0
  20. package/dist/conversion/projection.js +91 -0
  21. package/dist/conversion/projection.js.map +1 -0
  22. package/dist/conversion/types.d.ts +252 -0
  23. package/dist/conversion/types.d.ts.map +1 -0
  24. package/dist/conversion/types.js +2 -0
  25. package/dist/conversion/types.js.map +1 -0
  26. package/dist/conversion/withdrawal_ring.d.ts +36 -0
  27. package/dist/conversion/withdrawal_ring.d.ts.map +1 -0
  28. package/dist/conversion/withdrawal_ring.js +87 -0
  29. package/dist/conversion/withdrawal_ring.js.map +1 -0
  30. package/dist/dao.d.ts +86 -0
  31. package/dist/dao.d.ts.map +1 -0
  32. package/dist/dao.js +113 -0
  33. package/dist/dao.js.map +1 -0
  34. package/dist/index.d.ts +51 -3
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +20 -3
  37. package/dist/index.js.map +1 -1
  38. package/dist/logic.d.ts +85 -0
  39. package/dist/logic.d.ts.map +1 -0
  40. package/dist/logic.js +184 -0
  41. package/dist/logic.js.map +1 -0
  42. package/dist/order/cells.d.ts +207 -0
  43. package/dist/order/cells.d.ts.map +1 -0
  44. package/dist/order/cells.js +427 -0
  45. package/dist/order/cells.js.map +1 -0
  46. package/dist/order/conversion.d.ts +43 -0
  47. package/dist/order/conversion.d.ts.map +1 -0
  48. package/dist/order/conversion.js +123 -0
  49. package/dist/order/conversion.js.map +1 -0
  50. package/dist/order/fee.d.ts +9 -0
  51. package/dist/order/fee.d.ts.map +1 -0
  52. package/dist/order/fee.js +24 -0
  53. package/dist/order/fee.js.map +1 -0
  54. package/dist/order/fill.d.ts +36 -0
  55. package/dist/order/fill.d.ts.map +1 -0
  56. package/dist/order/fill.js +53 -0
  57. package/dist/order/fill.js.map +1 -0
  58. package/dist/order/info.d.ts +62 -0
  59. package/dist/order/info.d.ts.map +1 -0
  60. package/dist/order/info.js +99 -0
  61. package/dist/order/info.js.map +1 -0
  62. package/dist/order/master.d.ts +49 -0
  63. package/dist/order/master.d.ts.map +1 -0
  64. package/dist/order/master.js +27 -0
  65. package/dist/order/master.js.map +1 -0
  66. package/dist/order/matcher.d.ts +43 -0
  67. package/dist/order/matcher.d.ts.map +1 -0
  68. package/dist/order/matcher.js +123 -0
  69. package/dist/order/matcher.js.map +1 -0
  70. package/dist/order/order.d.ts +59 -0
  71. package/dist/order/order.d.ts.map +1 -0
  72. package/dist/order/order.js +239 -0
  73. package/dist/order/order.js.map +1 -0
  74. package/dist/order/order_data.d.ts +51 -0
  75. package/dist/order/order_data.d.ts.map +1 -0
  76. package/dist/order/order_data.js +64 -0
  77. package/dist/order/order_data.js.map +1 -0
  78. package/dist/order/ratio.d.ts +42 -0
  79. package/dist/order/ratio.d.ts.map +1 -0
  80. package/dist/order/ratio.js +82 -0
  81. package/dist/order/ratio.js.map +1 -0
  82. package/dist/order/relative.d.ts +43 -0
  83. package/dist/order/relative.d.ts.map +1 -0
  84. package/dist/order/relative.js +54 -0
  85. package/dist/order/relative.js.map +1 -0
  86. package/dist/owned_owner.d.ts +113 -0
  87. package/dist/owned_owner.d.ts.map +1 -0
  88. package/dist/owned_owner.js +280 -0
  89. package/dist/owned_owner.js.map +1 -0
  90. package/dist/sdk.d.ts +71 -42
  91. package/dist/sdk.d.ts.map +1 -1
  92. package/dist/sdk.js +388 -209
  93. package/dist/sdk.js.map +1 -1
  94. package/dist/send/sign_and_send_transaction.d.ts +46 -0
  95. package/dist/send/sign_and_send_transaction.d.ts.map +1 -0
  96. package/dist/send/sign_and_send_transaction.js +101 -0
  97. package/dist/send/sign_and_send_transaction.js.map +1 -0
  98. package/dist/send/wait_transaction.d.ts +41 -0
  99. package/dist/send/wait_transaction.d.ts.map +1 -0
  100. package/dist/send/wait_transaction.js +204 -0
  101. package/dist/send/wait_transaction.js.map +1 -0
  102. package/dist/udt.d.ts +123 -0
  103. package/dist/udt.d.ts.map +1 -0
  104. package/dist/udt.js +244 -0
  105. package/dist/udt.js.map +1 -0
  106. package/dist/utils/chain.d.ts +38 -0
  107. package/dist/utils/chain.d.ts.map +1 -0
  108. package/dist/utils/chain.js +20 -0
  109. package/dist/utils/chain.js.map +1 -0
  110. package/dist/utils/codec.d.ts +19 -0
  111. package/dist/utils/codec.d.ts.map +1 -0
  112. package/dist/utils/codec.js +66 -0
  113. package/dist/utils/codec.js.map +1 -0
  114. package/dist/utils/transaction_header.d.ts +17 -0
  115. package/dist/utils/transaction_header.d.ts.map +1 -0
  116. package/dist/utils/transaction_header.js +44 -0
  117. package/dist/utils/transaction_header.js.map +1 -0
  118. package/dist/utils/utils.d.ts +126 -0
  119. package/dist/utils/utils.d.ts.map +1 -0
  120. package/dist/utils/utils.js +151 -0
  121. package/dist/utils/utils.js.map +1 -0
  122. package/package.json +19 -26
  123. package/src/constants.ts +84 -181
  124. package/src/conversion/estimate.ts +157 -0
  125. package/src/conversion/maturity.ts +159 -0
  126. package/src/conversion/plans.ts +144 -0
  127. package/src/conversion/projection.ts +128 -0
  128. package/src/conversion/types.ts +287 -0
  129. package/src/conversion/withdrawal_ring.ts +126 -0
  130. package/src/dao.ts +172 -0
  131. package/src/index.ts +89 -3
  132. package/src/logic.ts +295 -0
  133. package/src/order/cells.ts +520 -0
  134. package/src/order/conversion.ts +173 -0
  135. package/src/order/fee.ts +30 -0
  136. package/src/order/fill.ts +83 -0
  137. package/src/order/info.ts +178 -0
  138. package/src/order/master.ts +42 -0
  139. package/src/order/matcher.ts +191 -0
  140. package/src/order/order.ts +302 -0
  141. package/src/order/order_data.ts +128 -0
  142. package/src/order/ratio.ts +144 -0
  143. package/src/order/relative.ts +106 -0
  144. package/src/owned_owner.ts +394 -0
  145. package/src/sdk.ts +555 -440
  146. package/src/send/sign_and_send_transaction.ts +120 -0
  147. package/src/send/wait_transaction.ts +271 -0
  148. package/src/udt.ts +317 -0
  149. package/src/utils/chain.ts +43 -0
  150. package/src/utils/codec.ts +74 -0
  151. package/src/utils/transaction_header.ts +66 -0
  152. package/src/utils/utils.ts +237 -0
  153. package/dist/codec.d.ts +0 -3
  154. package/dist/codec.d.ts.map +0 -1
  155. package/dist/codec.js +0 -58
  156. package/dist/codec.js.map +0 -1
  157. package/src/codec.ts +0 -138
package/dist/sdk.js CHANGED
@@ -1,242 +1,421 @@
1
1
  import { ccc } from "@ckb-ccc/core";
2
- import { collect, CapacityManager, SmartTransaction, binarySearch, hexFrom, getHeader, Epoch, } from "@ickb/utils";
3
- import { convert, ICKB_DEPOSIT_CAP, ickbExchangeRatio, } from "@ickb/core";
4
- import { Info, OrderManager, Ratio, } from "@ickb/order";
5
2
  import { getConfig } from "./constants.js";
6
- import { PoolSnapshot } from "./codec.js";
7
- export class IckbSdk {
8
- constructor(ownedOwner, ickbLogic, order, capacity, bots) {
9
- Object.defineProperty(this, "ownedOwner", {
10
- enumerable: true,
11
- configurable: true,
12
- writable: true,
13
- value: ownedOwner
14
- });
15
- Object.defineProperty(this, "ickbLogic", {
16
- enumerable: true,
17
- configurable: true,
18
- writable: true,
19
- value: ickbLogic
20
- });
21
- Object.defineProperty(this, "order", {
22
- enumerable: true,
23
- configurable: true,
24
- writable: true,
25
- value: order
26
- });
27
- Object.defineProperty(this, "capacity", {
28
- enumerable: true,
29
- configurable: true,
30
- writable: true,
31
- value: capacity
32
- });
33
- Object.defineProperty(this, "bots", {
34
- enumerable: true,
35
- configurable: true,
36
- writable: true,
37
- value: bots
38
- });
3
+ import { minimumOrderAmount } from "./conversion/estimate.js";
4
+ import { ckbToIckbConversionPlans, ickbToCkbConversionPlans, } from "./conversion/plans.js";
5
+ import { assertDaoOutputLimit, broadcastDeadline, DaoHeaderIndexError, DaoOutputLimitError, } from "./dao.js";
6
+ import { Ratio } from "./order/ratio.js";
7
+ import { ickbExchangeRatio } from "./udt.js";
8
+ import { compareBigInt, findCells, isPlainCapacityCell, unique } from "./utils/utils.js";
9
+ /**
10
+ * Input count up to which one own transaction sweeps liquid cells.
11
+ *
12
+ * @remarks A compaction target, not a validity bound: required funding and the fee may go
13
+ * past it. Counted rather than measured in bytes because a same-lock input costs a fixed
14
+ * 44 bytes (52 with an extra witness slot), so 1,000 inputs stay near 50 KB, far under the
15
+ * node's 512,000-byte limit, and near the one measured point (1,000 inputs: 44 KB, about
16
+ * 180 ms of local completion). A miner paying the bot adds about forty cellbase cells per
17
+ * turn, which this outpaces; a larger backlog takes several transactions.
18
+ */
19
+ export const SWEEP_INPUT_LIMIT = 1000;
20
+ // eslint-disable-next-line @typescript-eslint/no-shadow -- Preserve the runtime constructor name.
21
+ const IckbSdkImplementation = class IckbSdk {
22
+ ickbUdt;
23
+ ownedOwner;
24
+ ickbLogic;
25
+ order;
26
+ constructor({ ickbUdt, ownedOwner, ickbLogic, order }) {
27
+ this.ickbUdt = ickbUdt;
28
+ this.ownedOwner = ownedOwner;
29
+ this.ickbLogic = ickbLogic;
30
+ this.order = order;
39
31
  }
40
- static from(...args) {
41
- const { managers: { ownedOwner, logic, order, capacity }, bots, } = getConfig(...args);
42
- return new IckbSdk(ownedOwner, logic, order, capacity, bots);
32
+ /** Creates the SDK for one chain's deployment. */
33
+ static fromChain(chain) {
34
+ return new IckbSdk(getConfig(chain));
43
35
  }
44
- static estimate(isCkb2Udt, amounts, system, options) {
45
- options = {
46
- fee: 1n,
47
- feeBase: 100000n,
48
- ...options,
36
+ // ---- State reads -------------------------------------------------------------------
37
+ /**
38
+ * Reads system, user-order, and account state against one sampled tip, under the
39
+ * caller's withdrawal timing rules (`BOT_LOCK_UP` or `WALLET_LOCK_UP`).
40
+ *
41
+ * @remarks Every read is complete and uncapped: a large book or pool costs a slower
42
+ * read, never a partial or failed one. Each account lock is enumerated by one uncached
43
+ * unfiltered exact-lock scan and classified client-side.
44
+ */
45
+ async getL1AccountState(client, locks, lockUp) {
46
+ const tip = await client.getTipHeader();
47
+ const exchangeRatio = Ratio.from(ickbExchangeRatio(tip));
48
+ const [poolDeposits, orders, feeRate, accounts] = await Promise.all([
49
+ this.ickbLogic.findDeposits(client, tip),
50
+ this.order.findOrders(client),
51
+ getFeeRate(client),
52
+ Promise.all([...unique(locks)].map(async (lock) => this.readLockCells(client, lock, tip))),
53
+ ]);
54
+ return {
55
+ system: {
56
+ feeRate,
57
+ tip,
58
+ exchangeRatio,
59
+ // One book, two filters: the market side is every order
60
+ // past par, the wallet's own included, since the bot fills by price, not by owner.
61
+ orderPool: orders.filter((group) => isPastPar(group, exchangeRatio)),
62
+ poolDeposits,
63
+ lockUp,
64
+ },
65
+ user: { orders: orders.filter((group) => group.isOwner(...locks)) },
66
+ account: {
67
+ capacityCells: accounts.flatMap((account) => account.capacityCells),
68
+ nativeUdtCells: accounts.flatMap((account) => account.nativeUdtCells),
69
+ receipts: accounts.flatMap((account) => account.receipts),
70
+ withdrawalGroups: accounts.flatMap((account) => account.withdrawalGroups),
71
+ },
49
72
  };
50
- const { convertedAmount, ckbFee, info } = OrderManager.convert(isCkb2Udt, system.exchangeRatio, amounts, options);
51
- const maturity = ckbFee >= 10n * system.feeRate
52
- ? IckbSdk.maturity({ info, amounts }, system)
53
- : undefined;
54
- return { convertedAmount, ckbFee, info, maturity };
55
- }
56
- static maturity(o, system) {
57
- const info = "info" in o ? o.info : o.data.info;
58
- const amounts = "amounts" in o
59
- ? o.amounts
60
- : { ckbValue: o.ckbUnoccupied, udtValue: o.udtValue };
61
- if (info.isDualRatio()) {
62
- return;
73
+ }
74
+ /** Every Stack cell one lock owns, classified from a single exact-lock scan. */
75
+ async readLockCells(client, lock, tip) {
76
+ const cells = await findCells(client, {
77
+ script: lock,
78
+ scriptType: "lock",
79
+ scriptSearchMode: "exact",
80
+ withData: true,
81
+ });
82
+ const [receipts, withdrawalGroups] = await Promise.all([
83
+ this.ickbLogic.receiptsFrom(client, cells),
84
+ this.ownedOwner.withdrawalGroupsFrom(client, cells, tip),
85
+ ]);
86
+ return {
87
+ capacityCells: cells.filter(isPlainCapacityCell),
88
+ nativeUdtCells: cells.filter((cell) => this.ickbUdt.isUdt(cell)),
89
+ receipts,
90
+ withdrawalGroups,
91
+ };
92
+ }
93
+ // ---- Conversion planning -----------------------------------------------------------
94
+ /**
95
+ * Builds and completes one conversion transaction from a conversion context.
96
+ *
97
+ * @remarks
98
+ * Every branch returns a transaction funded from the signer's committed cells: the
99
+ * candidate plans (deposit counts, or prefixes of the greedy withdrawal selection with
100
+ * their rebuilt remainder order) are completed most direct first and the first fundable one
101
+ * wins, so a wallet short of CKB degrades to fewer direct actions plus a larger standing
102
+ * order rather than failing. Failure results are
103
+ * expected planning outcomes; when no plan can be funded the last completion error throws.
104
+ */
105
+ async buildConversionTransaction(txLike, options) {
106
+ const { amount, context, direction } = options;
107
+ if (amount < 0n) {
108
+ return conversionFailure("amount-negative", context.estimatedMaturity);
63
109
  }
64
- const isCkb2Udt = info.isCkb2Udt();
65
- const amount = isCkb2Udt ? amounts.ckbValue : amounts.udtValue;
66
- const ratio = isCkb2Udt ? info.ckbToUdt : info.udtToCkb;
67
- if (amount === 0n) {
68
- return 0n;
69
- }
70
- const { tip, exchangeRatio, orderPool, ckbAvailable, ckbMaturing } = system;
71
- const b = new Info(ratio, ratio, 1);
72
- let ckb = isCkb2Udt ? amount : 0n;
73
- let udt = isCkb2Udt ? 0n : amount;
74
- for (const o of orderPool) {
75
- const a = o.data.info;
76
- if (a.isCkb2Udt()) {
77
- if (!isCkb2Udt || a.ckb2UdtCompare(b) < 0) {
78
- ckb += o.ckbUnoccupied;
79
- }
80
- }
81
- else {
82
- if (isCkb2Udt || a.udt2CkbCompare(b) < 0) {
83
- udt += o.udtValue;
84
- }
85
- }
110
+ if (direction === "ckb-to-ickb" && amount > context.ckbAvailable) {
111
+ return conversionFailure("insufficient-ckb", context.estimatedMaturity);
86
112
  }
87
- ckb -= convert(false, udt, exchangeRatio);
88
- let maturity = 10n * 60n * 1000n;
89
- if (isCkb2Udt) {
90
- if (ckb > 0n) {
91
- maturity *= 1n + ckb / ccc.fixedPointFrom("200000");
92
- }
93
- return maturity + ("info" in o ? BigInt(Date.now()) : tip.timestamp);
113
+ if (direction === "ickb-to-ckb" && amount > context.ickbAvailable) {
114
+ return conversionFailure("insufficient-ickb", context.estimatedMaturity);
94
115
  }
95
- ckb += ckbAvailable;
96
- if (ckb >= 0) {
97
- return maturity + ("info" in o ? BigInt(Date.now()) : tip.timestamp);
116
+ const baseTx = ccc.Transaction.from(txLike);
117
+ const resolved = {
118
+ ...options,
119
+ lock: options.lock ?? (await options.signer.getRecommendedAddressObj()).script,
120
+ };
121
+ if (amount === 0n) {
122
+ return this.buildCollectOnlyConversion(baseTx, resolved);
98
123
  }
99
- const ckbNeeded = -ckb;
100
- const i = binarySearch(ckbMaturing.length, (n) => ckbMaturing[n].ckbCumulative >= ckbNeeded);
101
- return ckbMaturing[i]?.maturity;
102
- }
103
- async request(tx, user, info, amounts) {
104
- user =
105
- "codeHash" in user
106
- ? user
107
- : (await user.getRecommendedAddressObj()).script;
108
- this.order.mint(tx, user, info, amounts);
124
+ return direction === "ckb-to-ickb"
125
+ ? this.buildCkbToIckbConversion(baseTx, resolved)
126
+ : this.buildIckbToCkbConversion(baseTx, resolved);
109
127
  }
110
- collect(tx, groups, options) {
111
- this.order.melt(tx, groups, options);
128
+ async buildCollectOnlyConversion(baseTx, options) {
129
+ const { context, lock, signer } = options;
130
+ const tx = this.buildBaseTransaction(baseTx, context);
131
+ // An empty collection to a lock of the signer's own would only compact the account;
132
+ // to another lock it moves the liquid cells, which completion builds from the sweep.
133
+ if (!hasTransactionActivity(tx) &&
134
+ (context.cells.length === 0 || (await isOwnLock(signer, lock)))) {
135
+ return conversionFailure("nothing-to-do", context.estimatedMaturity);
136
+ }
137
+ const completed = await this.completeConversion(tx, options);
138
+ return {
139
+ ok: true,
140
+ tx: completed,
141
+ estimatedMaturity: context.estimatedMaturity,
142
+ conversion: { kind: "collect-only" },
143
+ isSweepComplete: sweepsAll(completed, context.cells),
144
+ };
112
145
  }
113
- async getL1State(client, locks) {
114
- const tip = await client.getTipHeader();
115
- const exchangeRatio = Ratio.from(ickbExchangeRatio(tip));
116
- const [{ ckbAvailable, ckbMaturing }, orders, feeRate] = await Promise.all([
117
- this.getCkb(client, tip),
118
- collect(this.order.findOrders(client)),
119
- client.getFeeRate(),
120
- ]);
121
- const midInfo = new Info(exchangeRatio, exchangeRatio, 1);
122
- const userOrders = [];
123
- const systemOrders = [];
124
- for (const group of orders) {
125
- if (group.isOwner(...locks)) {
126
- userOrders.push(group);
146
+ async buildCkbToIckbConversion(baseTx, options) {
147
+ const { context, lock } = options;
148
+ // A plan is dropped only for an unrepresentable remainder order.
149
+ const plans = ckbToIckbConversionPlans(options);
150
+ if (plans.length === 0) {
151
+ return conversionFailure("amount-too-small", context.estimatedMaturity, minimumOrderAmount(true, context.system));
152
+ }
153
+ const { candidate: plan, tx } = await completeFirstFundable(plans, (candidate) => {
154
+ let partial = this.buildBaseTransaction(baseTx.clone(), context);
155
+ if (candidate.depositCount > 0) {
156
+ partial = this.ickbLogic.deposit(partial, candidate.depositCount, candidate.depositCapacity, lock);
127
157
  }
128
- const { order } = group;
129
- const info = order.data.info;
130
- if ((order.isCkb2UdtMatchable() && info.ckb2UdtCompare(midInfo) < 0) ||
131
- (order.isUdt2CkbMatchable() && info.udt2CkbCompare(midInfo) < 0)) {
132
- systemOrders.push(order);
158
+ if (candidate.order !== undefined) {
159
+ partial = this.order.mint(partial, lock, candidate.order.estimate.info, candidate.order.amounts);
133
160
  }
134
- }
135
- const system = {
136
- feeRate,
137
- tip,
138
- exchangeRatio,
139
- orderPool: systemOrders,
140
- ckbAvailable,
141
- ckbMaturing,
161
+ return partial;
162
+ }, async (partial) => this.completeConversion(partial, options));
163
+ return {
164
+ ok: true,
165
+ tx,
166
+ estimatedMaturity: plan.estimatedMaturity,
167
+ conversion: {
168
+ kind: conversionKind(plan.depositCount > 0, plan.order !== undefined),
169
+ },
170
+ isSweepComplete: sweepsAll(tx, context.cells),
142
171
  };
143
- for (const { order } of userOrders) {
144
- order.maturity = IckbSdk.maturity(order, system);
172
+ }
173
+ async buildIckbToCkbConversion(baseTx, options) {
174
+ const { context, lock } = options;
175
+ const plans = ickbToCkbConversionPlans(options, context.system.poolDeposits);
176
+ if (plans.length === 0) {
177
+ return conversionFailure("amount-too-small", context.estimatedMaturity, minimumOrderAmount(false, context.system));
145
178
  }
179
+ const { candidate: plan, tx } = await completeFirstFundable(plans, (candidate) => {
180
+ let partial = this.buildBaseTransaction(baseTx.clone(), context, {
181
+ deposits: candidate.selectedDeposits,
182
+ lock,
183
+ });
184
+ if (candidate.order !== undefined) {
185
+ partial = this.order.mint(partial, lock, candidate.order.estimate.info, candidate.order.amounts);
186
+ }
187
+ return partial;
188
+ }, async (partial) => this.completeConversion(partial, options));
189
+ const notice = plan.order?.estimate.notice;
190
+ const broadcastBefore = broadcastDeadline(plan.selectedDeposits.map((deposit) => deposit.claimEpoch), context.system.lockUp);
146
191
  return {
147
- system,
148
- user: {
149
- orders: userOrders,
192
+ ok: true,
193
+ tx,
194
+ estimatedMaturity: plan.estimatedMaturity,
195
+ conversion: {
196
+ kind: conversionKind(plan.selectedDeposits.length > 0, plan.order !== undefined),
150
197
  },
198
+ ...(notice === undefined ? {} : { conversionNotice: notice }),
199
+ isSweepComplete: sweepsAll(tx, context.cells),
200
+ ...(broadcastBefore === undefined ? {} : { broadcastBefore }),
151
201
  };
152
202
  }
153
- async getCkb(client, tip) {
154
- const opts = {
155
- onChain: true,
156
- tip,
157
- };
158
- const promiseBotWithdrawals = collect(this.ownedOwner.findWithdrawalGroups(client, this.bots, opts));
159
- let poolSnapshotHex = "0x";
160
- let poolSnapshotEpoch = Epoch.from([0n, 0n, 1n]);
161
- const bot2Ckb = new Map();
162
- const reserved = -ccc.fixedPointFrom("2000");
163
- for await (const c of this.capacity.findCapacities(client, this.bots, opts)) {
164
- const key = hexFrom(c.cell.cellOutput.lock);
165
- const ckb = (bot2Ckb.get(key) ?? reserved) + c.ckbValue;
166
- bot2Ckb.set(key, ckb);
167
- const outputData = c.cell.outputData;
168
- if (outputData.length % 256 === 2) {
169
- const h = await getHeader(client, {
170
- type: "txHash",
171
- value: c.cell.outPoint.txHash,
172
- });
173
- const e = Epoch.from(h.epoch);
174
- if (poolSnapshotEpoch.compare(e) < 0) {
175
- poolSnapshotHex = outputData;
176
- poolSnapshotEpoch = e;
177
- }
203
+ async completeConversion(tx, options) {
204
+ return this.completeTransaction(tx, {
205
+ signer: options.signer,
206
+ lock: options.lock,
207
+ feeRate: options.context.system.feeRate,
208
+ cells: options.context.cells,
209
+ });
210
+ }
211
+ // ---- Transaction building ----------------------------------------------------------
212
+ /**
213
+ * Adds the context's collect steps to a partial transaction: the withdrawal requests when
214
+ * given, then the collectable orders, the receipts, and the ready withdrawals.
215
+ *
216
+ * @remarks
217
+ * The result is still partial. Callers should use `completeTransaction` before
218
+ * signing and sending. The withdrawals move their deposit headers to the front of
219
+ * `headerDeps`, so the given transaction must not reference a header by position.
220
+ */
221
+ buildBaseTransaction(txLike, steps, withdrawalRequest) {
222
+ let tx = ccc.Transaction.from(txLike);
223
+ if (withdrawalRequest !== undefined && withdrawalRequest.deposits.length > 0) {
224
+ tx = this.ownedOwner.requestWithdrawal(tx, withdrawalRequest.deposits, withdrawalRequest.lock);
225
+ }
226
+ if (steps.availableOrders.length > 0) {
227
+ tx = this.order.melt(tx, steps.availableOrders);
228
+ }
229
+ if (steps.receipts.length > 0) {
230
+ tx = this.ickbLogic.completeDeposit(tx, steps.receipts);
231
+ }
232
+ if (steps.readyWithdrawals.length > 0) {
233
+ tx = this.ownedOwner.withdraw(tx, steps.readyWithdrawals);
234
+ }
235
+ return tx;
236
+ }
237
+ /**
238
+ * Completes iCKB inputs, iCKB change, plain change, and the fee from the cells it is given.
239
+ *
240
+ * @remarks Completion never scans. `cells` are the signer's known liquid cells, plain
241
+ * CKB and iCKB, from the account state already read: the largest ones fund what the
242
+ * outputs need, the rest ride along as a sweep while the transaction has fewer than
243
+ * {@link SWEEP_INPUT_LIMIT} inputs, so every own transaction compacts the account.
244
+ * Ordinary change is always a plain cell; existing outputs
245
+ * are never reinterpreted or resized as fee change. This does not sign or send.
246
+ */
247
+ async completeTransaction(txLike, options) {
248
+ const { signer, feeRate } = options;
249
+ const tx = this.ickbUdt.addCellDeps(ccc.Transaction.from(txLike).clone());
250
+ const changeLock = options.lock ?? (await signer.getRecommendedAddressObj()).script;
251
+ // Inputs come from one state read, each cell once, and each action spends its own
252
+ // cell kind, so a repeated out point is a builder bug: this is the one place that
253
+ // names it, the node would refuse the transaction anyway.
254
+ const spent = new Set();
255
+ for (const { previousOutput } of tx.inputs) {
256
+ const key = previousOutput.toHex();
257
+ if (spent.has(key)) {
258
+ throw new Error(`Input ${key} is spent twice`);
178
259
  }
260
+ spent.add(key);
179
261
  }
180
- const ckbMaturing = new Array();
181
- for (const wr of await promiseBotWithdrawals) {
182
- if (wr.owned.isReady) {
183
- const key = hexFrom(wr.owner.cell.cellOutput.lock);
184
- const ckb = (bot2Ckb.get(key) ?? reserved) + wr.ckbValue;
185
- bot2Ckb.set(key, ckb);
186
- continue;
262
+ const unspent = options.cells.filter((cell) => !spent.has(cell.outPoint.toHex()));
263
+ const ickbCells = unspent
264
+ .filter((cell) => this.ickbUdt.isUdt(cell))
265
+ .toSorted((left, right) => compareBigInt(udtBalance(right), udtBalance(left)));
266
+ const plainCells = unspent
267
+ .filter(isPlainCapacityCell)
268
+ .toSorted((left, right) => compareBigInt(right.cellOutput.capacity, left.cellOutput.capacity));
269
+ await this.completeIckb(tx, signer.client, changeLock, ickbCells);
270
+ // Plain CKB: the sweep first, then whatever the fee still needs beyond the limit.
271
+ const swept = sweep(tx, plainCells);
272
+ await completeFee(tx, signer, changeLock, feeRate, plainCells.slice(swept));
273
+ assertDaoOutputLimit(tx, this.ickbLogic.dao.script);
274
+ return tx;
275
+ }
276
+ /** iCKB: what the outputs need first, then the sweep while the limit allows, then change. */
277
+ async completeIckb(tx, client, changeLock, ickbCells) {
278
+ const required = this.ickbUdt.outputBalance(tx);
279
+ let balance = await this.ickbUdt.inputBalance(tx, client);
280
+ for (const cell of ickbCells) {
281
+ if (balance >= required && tx.inputs.length >= SWEEP_INPUT_LIMIT) {
282
+ break;
187
283
  }
188
- ckbMaturing.push({
189
- ckbValue: wr.ckbValue,
190
- maturity: wr.owned.maturity.toUnix(tip),
284
+ tx.addInput(cell);
285
+ balance += udtBalance(cell);
286
+ }
287
+ if (balance < required) {
288
+ throw new IckbError(`Insufficient iCKB, need ${String(required - balance)} more`, {
289
+ code: "insufficient_ickb",
191
290
  });
192
291
  }
193
- let ckbAvailable = 0n;
194
- for (const ckb of bot2Ckb.values()) {
195
- if (ckb > 0n) {
196
- ckbAvailable += ckb;
197
- }
292
+ this.ickbUdt.addChange(tx, changeLock, balance - required);
293
+ }
294
+ };
295
+ /** Concrete iCKB SDK constructor and static estimators. */
296
+ // eslint-disable-next-line @typescript-eslint/no-redeclare -- The public type and runtime constructor intentionally share a name.
297
+ export const IckbSdk = IckbSdkImplementation;
298
+ /** Whether the transaction already carries any input or output. */
299
+ export function hasTransactionActivity(tx) {
300
+ return tx.inputs.length > 0 || tx.outputs.length > 0;
301
+ }
302
+ /** An order the market can fill at a gain: priced past the DAO ratio on its matchable side. */
303
+ function isPastPar(group, exchangeRatio) {
304
+ const { order } = group;
305
+ const { info } = order.data;
306
+ return ((order.isCkb2UdtMatchable() && info.ckbToUdt.compare(exchangeRatio) < 0) ||
307
+ (order.isUdt2CkbMatchable() && exchangeRatio.compare(info.udtToCkb) < 0));
308
+ }
309
+ function conversionFailure(reason, estimatedMaturity, minimum) {
310
+ return {
311
+ ok: false,
312
+ reason,
313
+ estimatedMaturity,
314
+ ...(minimum === undefined ? {} : { minimum }),
315
+ };
316
+ }
317
+ /** A plan always carries a deposit or an order, else it would be a collection. */
318
+ function conversionKind(hasDirect, hasOrder) {
319
+ if (hasDirect && hasOrder) {
320
+ return "direct-plus-order";
321
+ }
322
+ return hasDirect ? "direct" : "order";
323
+ }
324
+ /** Whether every liquid cell became an input; false when the input limit cut the sweep. */
325
+ function sweepsAll(tx, cells) {
326
+ const spent = new Set(tx.inputs.map((input) => input.previousOutput.toHex()));
327
+ return cells.every((cell) => spent.has(cell.outPoint.toHex()));
328
+ }
329
+ async function isOwnLock(signer, lock) {
330
+ return (await signer.getAddressObjs()).some(({ script }) => script.eq(lock));
331
+ }
332
+ async function getFeeRate(client) {
333
+ const feeRate = await client.getFeeRate();
334
+ if (feeRate < 0n) {
335
+ throw new Error("Client fee rate must be non-negative");
336
+ }
337
+ return feeRate;
338
+ }
339
+ function udtBalance(cell) {
340
+ return ccc.udtBalanceFrom(cell.outputData);
341
+ }
342
+ /** Adds cells while the input count stays under the limit; returns how many were added. */
343
+ function sweep(tx, cells) {
344
+ const added = Math.min(cells.length, Math.max(0, SWEEP_INPUT_LIMIT - tx.inputs.length));
345
+ for (const cell of cells.slice(0, added)) {
346
+ tx.addInput(cell);
347
+ }
348
+ return added;
349
+ }
350
+ /** Completes the fee, adding reserve cells one at a time while capacity is short. */
351
+ async function completeFee(tx, signer, changeLock, feeRate, reserve) {
352
+ const remaining = [...reserve];
353
+ for (;;) {
354
+ try {
355
+ await tx.completeFeeChangeToLock(signer, changeLock, feeRate, undefined, {
356
+ shouldAddInputs: false,
357
+ });
358
+ return;
198
359
  }
199
- const tipEpoch = Epoch.from(tip.epoch);
200
- const oneCycle = Epoch.from([180n, 0n, 1n]);
201
- if (poolSnapshotHex !== "0x") {
202
- const eNumber = tip.epoch[0];
203
- let start = Epoch.from([eNumber - (eNumber % 180n), 0n, 1n]);
204
- const step = Epoch.from([0n, 180n, 1024n]);
205
- const depositSize = convert(false, ICKB_DEPOSIT_CAP, tip);
206
- for (const binAmount of PoolSnapshot.decode(poolSnapshotHex)) {
207
- const end = start.add(step);
208
- if (binAmount > 0) {
209
- ckbMaturing.push({
210
- ckbValue: BigInt(binAmount) * depositSize,
211
- maturity: tipEpoch.compare(tipEpoch) < 0
212
- ?
213
- end.add(oneCycle).toUnix(tip)
214
- :
215
- end.toUnix(tip),
216
- });
217
- }
218
- start = end;
360
+ catch (error) {
361
+ if (!(error instanceof ccc.ErrorTransactionInsufficientCapacity)) {
362
+ throw error;
219
363
  }
220
- }
221
- else {
222
- for await (const d of this.ickbLogic.findDeposits(client, opts)) {
223
- ckbMaturing.push({
224
- ckbValue: d.ckbValue,
225
- maturity: d.maturity.toUnix(tip),
364
+ const cell = remaining.shift();
365
+ if (cell === undefined) {
366
+ throw new IckbError(error.message, {
367
+ code: "insufficient_capacity",
368
+ cause: error,
226
369
  });
227
370
  }
371
+ tx.addInput(cell);
228
372
  }
229
- ckbMaturing.sort((a, b) => Number(a.maturity - b.maturity));
230
- let cumulative = 0n;
231
- const ckbCumulativeMaturing = [];
232
- for (const { ckbValue, maturity } of ckbMaturing) {
233
- cumulative += ckbValue;
234
- ckbCumulativeMaturing.push({ ckbCumulative: cumulative, maturity });
373
+ }
374
+ }
375
+ /** Typed SDK failure with a stable machine-readable code. */
376
+ export class IckbError extends Error {
377
+ /** Stable failure code for callers and observability. */
378
+ code;
379
+ /** Creates a typed SDK failure while preserving its cause. */
380
+ constructor(message, options) {
381
+ super(message, options);
382
+ this.name = "IckbError";
383
+ this.code = options.code;
384
+ }
385
+ }
386
+ /**
387
+ * Completes the first candidate that the real completer can fund.
388
+ *
389
+ * @remarks
390
+ * Only completion knows what a transaction costs once markers, remainder orders, change, and
391
+ * fee are in, so no count is computed up front: each candidate is built and completed in turn.
392
+ * Capacity, DAO output-limit and DAO header-index failures advance
393
+ * the walk; transport, scan, signer, and malformed-transaction errors propagate. Exhausting
394
+ * the candidates throws the last advancing failure.
395
+ */
396
+ export async function completeFirstFundable(candidates, build, complete) {
397
+ let error;
398
+ for (const candidate of candidates) {
399
+ try {
400
+ return { candidate, tx: await complete(build(candidate)) };
401
+ }
402
+ catch (failure) {
403
+ if (!isFundabilityFailure(failure)) {
404
+ throw failure;
405
+ }
406
+ error = failure;
235
407
  }
236
- return {
237
- ckbAvailable,
238
- ckbMaturing: ckbCumulativeMaturing,
239
- };
240
408
  }
409
+ throw error ?? new Error("No candidate could be completed");
410
+ }
411
+ /**
412
+ * A failure the walk advances past: the candidate costs more than the account funds, or
413
+ * needs more DAO outputs or deposit-header slots than one transaction has.
414
+ */
415
+ export function isFundabilityFailure(error) {
416
+ return (error instanceof IckbError ||
417
+ error instanceof ccc.ErrorTransactionInsufficientCapacity ||
418
+ error instanceof DaoOutputLimitError ||
419
+ error instanceof DaoHeaderIndexError);
241
420
  }
242
421
  //# sourceMappingURL=sdk.js.map