@ckb-ccc/core 1.6.0 → 1.8.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (104) hide show
  1. package/CHANGELOG.md +19 -1
  2. package/dist/ckb/script.d.ts +2 -1
  3. package/dist/ckb/script.d.ts.map +1 -1
  4. package/dist/ckb/transaction.d.ts +70 -5
  5. package/dist/ckb/transaction.d.ts.map +1 -1
  6. package/dist/ckb/transaction.js +186 -53
  7. package/dist/client/client.d.ts +3 -1
  8. package/dist/client/client.d.ts.map +1 -1
  9. package/dist/client/client.js +4 -3
  10. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -1
  11. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  12. package/dist/client/clientPublicMainnet.advanced.js +1 -1
  13. package/dist/client/clientPublicMainnet.d.ts +2 -1
  14. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  15. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -1
  16. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  17. package/dist/client/clientPublicTestnet.advanced.js +1 -1
  18. package/dist/client/clientPublicTestnet.d.ts +2 -1
  19. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  20. package/dist/client/clientTypes.d.ts +0 -25
  21. package/dist/client/clientTypes.d.ts.map +1 -1
  22. package/dist/client/clientTypes.js +0 -27
  23. package/dist/client/index.d.ts +1 -0
  24. package/dist/client/index.d.ts.map +1 -1
  25. package/dist/client/index.js +1 -0
  26. package/dist/client/knownScript.d.ts +26 -0
  27. package/dist/client/knownScript.d.ts.map +1 -0
  28. package/dist/client/knownScript.js +27 -0
  29. package/dist/num/index.d.ts.map +1 -1
  30. package/dist/num/index.js +17 -1
  31. package/dist/signer/ckb/signerCkbPublicKey.d.ts.map +1 -1
  32. package/dist/signer/ckb/signerCkbPublicKey.js +1 -5
  33. package/dist/signer/nostr/index.d.ts +1 -0
  34. package/dist/signer/nostr/index.d.ts.map +1 -1
  35. package/dist/signer/nostr/index.js +1 -0
  36. package/dist/signer/nostr/signerNostr.d.ts +1 -1
  37. package/dist/signer/nostr/signerNostr.d.ts.map +1 -1
  38. package/dist/signer/nostr/signerNostr.js +8 -0
  39. package/dist/signer/nostr/signerNostrPrivateKey.d.ts +5 -7
  40. package/dist/signer/nostr/signerNostrPrivateKey.d.ts.map +1 -1
  41. package/dist/signer/nostr/signerNostrPrivateKey.js +6 -12
  42. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts +11 -0
  43. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +1 -0
  44. package/dist/signer/nostr/signerNostrPublicKeyReadonly.js +22 -0
  45. package/dist.commonjs/ckb/script.d.ts +2 -1
  46. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  47. package/dist.commonjs/ckb/transaction.d.ts +70 -5
  48. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  49. package/dist.commonjs/ckb/transaction.js +282 -147
  50. package/dist.commonjs/client/client.d.ts +3 -1
  51. package/dist.commonjs/client/client.d.ts.map +1 -1
  52. package/dist.commonjs/client/client.js +4 -3
  53. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +2 -1
  54. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  55. package/dist.commonjs/client/clientPublicMainnet.advanced.js +21 -21
  56. package/dist.commonjs/client/clientPublicMainnet.d.ts +2 -1
  57. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  58. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +2 -1
  59. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  60. package/dist.commonjs/client/clientPublicTestnet.advanced.js +21 -21
  61. package/dist.commonjs/client/clientPublicTestnet.d.ts +2 -1
  62. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  63. package/dist.commonjs/client/clientTypes.d.ts +0 -25
  64. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  65. package/dist.commonjs/client/clientTypes.js +1 -28
  66. package/dist.commonjs/client/index.d.ts +1 -0
  67. package/dist.commonjs/client/index.d.ts.map +1 -1
  68. package/dist.commonjs/client/index.js +1 -0
  69. package/dist.commonjs/client/knownScript.d.ts +26 -0
  70. package/dist.commonjs/client/knownScript.d.ts.map +1 -0
  71. package/dist.commonjs/client/knownScript.js +30 -0
  72. package/dist.commonjs/num/index.d.ts.map +1 -1
  73. package/dist.commonjs/num/index.js +17 -1
  74. package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts.map +1 -1
  75. package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +1 -5
  76. package/dist.commonjs/signer/nostr/index.d.ts +1 -0
  77. package/dist.commonjs/signer/nostr/index.d.ts.map +1 -1
  78. package/dist.commonjs/signer/nostr/index.js +1 -0
  79. package/dist.commonjs/signer/nostr/signerNostr.d.ts +1 -1
  80. package/dist.commonjs/signer/nostr/signerNostr.d.ts.map +1 -1
  81. package/dist.commonjs/signer/nostr/signerNostr.js +8 -0
  82. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts +5 -7
  83. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts.map +1 -1
  84. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.js +6 -12
  85. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts +11 -0
  86. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +1 -0
  87. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.js +26 -0
  88. package/package.json +2 -2
  89. package/src/ckb/script.ts +2 -1
  90. package/src/ckb/transaction.ts +270 -62
  91. package/src/client/client.ts +5 -4
  92. package/src/client/clientPublicMainnet.advanced.ts +2 -1
  93. package/src/client/clientPublicMainnet.ts +2 -1
  94. package/src/client/clientPublicTestnet.advanced.ts +2 -1
  95. package/src/client/clientPublicTestnet.ts +2 -1
  96. package/src/client/clientTypes.ts +0 -28
  97. package/src/client/index.ts +1 -0
  98. package/src/client/knownScript.ts +27 -0
  99. package/src/num/index.ts +21 -1
  100. package/src/signer/ckb/signerCkbPublicKey.ts +3 -5
  101. package/src/signer/nostr/index.ts +1 -0
  102. package/src/signer/nostr/signerNostr.ts +3 -1
  103. package/src/signer/nostr/signerNostrPrivateKey.ts +7 -14
  104. package/src/signer/nostr/signerNostrPublicKeyReadonly.ts +29 -0
@@ -15,16 +15,20 @@ exports.epochFrom = epochFrom;
15
15
  exports.epochFromHex = epochFromHex;
16
16
  exports.epochToHex = epochToHex;
17
17
  exports.udtBalanceFrom = udtBalanceFrom;
18
+ exports.calcDaoProfit = calcDaoProfit;
19
+ exports.calcDaoClaimEpoch = calcDaoClaimEpoch;
18
20
  const index_js_1 = require("../bytes/index.js");
19
- const index_js_2 = require("../fixedPoint/index.js");
20
- const index_js_3 = require("../hasher/index.js");
21
- const index_js_4 = require("../hex/index.js");
22
- const index_js_5 = require("../molecule/index.js");
23
- const index_js_6 = require("../num/index.js");
24
- const index_js_7 = require("../utils/index.js");
21
+ const index_js_2 = require("../client/index.js");
22
+ const knownScript_js_1 = require("../client/knownScript.js");
23
+ const index_js_3 = require("../fixedPoint/index.js");
24
+ const index_js_4 = require("../hasher/index.js");
25
+ const index_js_5 = require("../hex/index.js");
26
+ const index_js_6 = require("../molecule/index.js");
27
+ const index_js_7 = require("../num/index.js");
28
+ const index_js_8 = require("../utils/index.js");
25
29
  const script_js_1 = require("./script.js");
26
30
  const transaction_advanced_js_1 = require("./transaction.advanced.js");
27
- exports.DepTypeCodec = index_js_5.mol.Codec.from({
31
+ exports.DepTypeCodec = index_js_6.mol.Codec.from({
28
32
  byteLength: 1,
29
33
  encode: depTypeToBytes,
30
34
  decode: depTypeFromBytes,
@@ -94,7 +98,7 @@ function depTypeFromBytes(bytes) {
94
98
  /**
95
99
  * @public
96
100
  */
97
- let OutPoint = OutPoint_1 = class OutPoint extends index_js_5.mol.Entity.Base() {
101
+ let OutPoint = OutPoint_1 = class OutPoint extends index_js_6.mol.Entity.Base() {
98
102
  /**
99
103
  * Creates an instance of OutPoint.
100
104
  *
@@ -121,20 +125,20 @@ let OutPoint = OutPoint_1 = class OutPoint extends index_js_5.mol.Entity.Base()
121
125
  if (outPoint instanceof OutPoint_1) {
122
126
  return outPoint;
123
127
  }
124
- return new OutPoint_1((0, index_js_4.hexFrom)(outPoint.txHash), (0, index_js_6.numFrom)(outPoint.index));
128
+ return new OutPoint_1((0, index_js_5.hexFrom)(outPoint.txHash), (0, index_js_7.numFrom)(outPoint.index));
125
129
  }
126
130
  };
127
131
  exports.OutPoint = OutPoint;
128
132
  exports.OutPoint = OutPoint = OutPoint_1 = __decorate([
129
- index_js_5.mol.codec(index_js_5.mol.struct({
130
- txHash: index_js_5.mol.Byte32,
131
- index: index_js_5.mol.Uint32,
133
+ index_js_6.mol.codec(index_js_6.mol.struct({
134
+ txHash: index_js_6.mol.Byte32,
135
+ index: index_js_6.mol.Uint32,
132
136
  }))
133
137
  ], OutPoint);
134
138
  /**
135
139
  * @public
136
140
  */
137
- let CellOutput = CellOutput_1 = class CellOutput extends index_js_5.mol.Entity.Base() {
141
+ let CellOutput = CellOutput_1 = class CellOutput extends index_js_6.mol.Entity.Base() {
138
142
  /**
139
143
  * Creates an instance of CellOutput.
140
144
  *
@@ -170,18 +174,18 @@ let CellOutput = CellOutput_1 = class CellOutput extends index_js_5.mol.Entity.B
170
174
  if (cellOutput instanceof CellOutput_1) {
171
175
  return cellOutput;
172
176
  }
173
- return new CellOutput_1((0, index_js_6.numFrom)(cellOutput.capacity), script_js_1.Script.from(cellOutput.lock), (0, index_js_7.apply)(script_js_1.Script.from, cellOutput.type));
177
+ return new CellOutput_1((0, index_js_7.numFrom)(cellOutput.capacity), script_js_1.Script.from(cellOutput.lock), (0, index_js_8.apply)(script_js_1.Script.from, cellOutput.type));
174
178
  }
175
179
  };
176
180
  exports.CellOutput = CellOutput;
177
181
  exports.CellOutput = CellOutput = CellOutput_1 = __decorate([
178
- index_js_5.mol.codec(index_js_5.mol.table({
179
- capacity: index_js_5.mol.Uint64,
182
+ index_js_6.mol.codec(index_js_6.mol.table({
183
+ capacity: index_js_6.mol.Uint64,
180
184
  lock: script_js_1.Script,
181
185
  type: script_js_1.ScriptOpt,
182
186
  }))
183
187
  ], CellOutput);
184
- exports.CellOutputVec = index_js_5.mol.vector(CellOutput);
188
+ exports.CellOutputVec = index_js_6.mol.vector(CellOutput);
185
189
  /**
186
190
  * @public
187
191
  */
@@ -208,7 +212,83 @@ class Cell {
208
212
  if (cell instanceof Cell) {
209
213
  return cell;
210
214
  }
211
- return new Cell(OutPoint.from("outPoint" in cell ? cell.outPoint : cell.previousOutput), CellOutput.from(cell.cellOutput), (0, index_js_4.hexFrom)(cell.outputData));
215
+ return new Cell(OutPoint.from("outPoint" in cell ? cell.outPoint : cell.previousOutput), CellOutput.from(cell.cellOutput), (0, index_js_5.hexFrom)(cell.outputData));
216
+ }
217
+ get capacityFree() {
218
+ const occupiedSize = (0, index_js_3.fixedPointFrom)(this.cellOutput.occupiedSize + (0, index_js_1.bytesFrom)(this.outputData).length);
219
+ return this.cellOutput.capacity - occupiedSize;
220
+ }
221
+ /**
222
+ * Occupied bytes of a cell on chain
223
+ * It's CellOutput.occupiedSize + bytesFrom(outputData).byteLength
224
+ */
225
+ get occupiedSize() {
226
+ return this.cellOutput.occupiedSize + (0, index_js_1.bytesFrom)(this.outputData).byteLength;
227
+ }
228
+ /**
229
+ * Gets confirmed Nervos DAO profit of a Cell
230
+ * It returns non-zero value only when the cell is in withdrawal phase 2
231
+ * See https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md
232
+ *
233
+ * @param client - A client for searching DAO related headers
234
+ * @returns Profit
235
+ *
236
+ * @example
237
+ * ```typescript
238
+ * const profit = await cell.getDaoProfit(client);
239
+ * ```
240
+ */
241
+ async getDaoProfit(client) {
242
+ if (!(await this.isNervosDao(client, "withdrew"))) {
243
+ return index_js_3.Zero;
244
+ }
245
+ const { depositHeader, withdrawHeader } = await this.getNervosDaoInfo(client);
246
+ if (!withdrawHeader || !depositHeader) {
247
+ throw new Error(`Unable to get headers of a Nervos DAO cell ${this.outPoint.txHash}:${this.outPoint.index.toString()}`);
248
+ }
249
+ return calcDaoProfit(this.capacityFree, depositHeader, withdrawHeader);
250
+ }
251
+ async isNervosDao(client, phase) {
252
+ const { type } = this.cellOutput;
253
+ const daoType = await client.getKnownScript(knownScript_js_1.KnownScript.NervosDao);
254
+ if (!type ||
255
+ type.codeHash !== daoType.codeHash ||
256
+ type.hashType !== daoType.hashType) {
257
+ // Non Nervos DAO cell
258
+ return false;
259
+ }
260
+ const hasWithdrew = (0, index_js_7.numFrom)(this.outputData) !== index_js_3.Zero;
261
+ return (!phase ||
262
+ (phase === "deposited" && !hasWithdrew) ||
263
+ (phase === "withdrew" && hasWithdrew));
264
+ }
265
+ async getNervosDaoInfo(client) {
266
+ if (!(await this.isNervosDao(client))) {
267
+ // Non Nervos DAO cell
268
+ return {};
269
+ }
270
+ if ((0, index_js_7.numFrom)(this.outputData) === index_js_3.Zero) {
271
+ // Deposited Nervos DAO cell
272
+ const depositRes = await client.getCellWithHeader(this.outPoint);
273
+ if (!depositRes?.header) {
274
+ throw new Error(`Unable to get header of a Nervos DAO deposited cell ${this.outPoint.txHash}:${this.outPoint.index.toString()}`);
275
+ }
276
+ return {
277
+ depositHeader: depositRes.header,
278
+ };
279
+ }
280
+ // Withdrew Nervos DAO cell
281
+ const [depositHeader, withdrawRes] = await Promise.all([
282
+ client.getHeaderByNumber((0, index_js_7.numFromBytes)(this.outputData)),
283
+ client.getCellWithHeader(this.outPoint),
284
+ ]);
285
+ if (!withdrawRes?.header || !depositHeader) {
286
+ throw new Error(`Unable to get headers of a Nervos DAO withdrew cell ${this.outPoint.txHash}:${this.outPoint.index.toString()}`);
287
+ }
288
+ return {
289
+ depositHeader,
290
+ withdrawHeader: withdrawRes.header,
291
+ };
212
292
  }
213
293
  /**
214
294
  * Clone a Cell
@@ -229,17 +309,17 @@ exports.Cell = Cell;
229
309
  * @public
230
310
  */
231
311
  function epochFrom(epochLike) {
232
- return [(0, index_js_6.numFrom)(epochLike[0]), (0, index_js_6.numFrom)(epochLike[1]), (0, index_js_6.numFrom)(epochLike[2])];
312
+ return [(0, index_js_7.numFrom)(epochLike[0]), (0, index_js_7.numFrom)(epochLike[1]), (0, index_js_7.numFrom)(epochLike[2])];
233
313
  }
234
314
  /**
235
315
  * @public
236
316
  */
237
317
  function epochFromHex(hex) {
238
- const num = (0, index_js_6.numFrom)((0, index_js_4.hexFrom)(hex));
318
+ const num = (0, index_js_7.numFrom)((0, index_js_5.hexFrom)(hex));
239
319
  return [
240
- num & (0, index_js_6.numFrom)("0xffffff"),
241
- (num >> (0, index_js_6.numFrom)(24)) & (0, index_js_6.numFrom)("0xffff"),
242
- (num >> (0, index_js_6.numFrom)(40)) & (0, index_js_6.numFrom)("0xffff"),
320
+ num & (0, index_js_7.numFrom)("0xffffff"),
321
+ (num >> (0, index_js_7.numFrom)(24)) & (0, index_js_7.numFrom)("0xffff"),
322
+ (num >> (0, index_js_7.numFrom)(40)) & (0, index_js_7.numFrom)("0xffff"),
243
323
  ];
244
324
  }
245
325
  /**
@@ -247,14 +327,14 @@ function epochFromHex(hex) {
247
327
  */
248
328
  function epochToHex(epochLike) {
249
329
  const epoch = epochFrom(epochLike);
250
- return (0, index_js_6.numToHex)((0, index_js_6.numFrom)(epoch[0]) +
251
- ((0, index_js_6.numFrom)(epoch[1]) << (0, index_js_6.numFrom)(24)) +
252
- ((0, index_js_6.numFrom)(epoch[2]) << (0, index_js_6.numFrom)(40)));
330
+ return (0, index_js_7.numToHex)((0, index_js_7.numFrom)(epoch[0]) +
331
+ ((0, index_js_7.numFrom)(epoch[1]) << (0, index_js_7.numFrom)(24)) +
332
+ ((0, index_js_7.numFrom)(epoch[2]) << (0, index_js_7.numFrom)(40)));
253
333
  }
254
334
  /**
255
335
  * @public
256
336
  */
257
- let Since = Since_1 = class Since extends index_js_5.mol.Entity.Base() {
337
+ let Since = Since_1 = class Since extends index_js_6.mol.Entity.Base() {
258
338
  /**
259
339
  * Creates an instance of Since.
260
340
  *
@@ -297,7 +377,7 @@ let Since = Since_1 = class Since extends index_js_5.mol.Entity.Base() {
297
377
  return since;
298
378
  }
299
379
  if (typeof since === "object" && "relative" in since) {
300
- return new Since_1(since.relative, since.metric, (0, index_js_6.numFrom)(since.value));
380
+ return new Since_1(since.relative, since.metric, (0, index_js_7.numFrom)(since.value));
301
381
  }
302
382
  return Since_1.fromNum(since);
303
383
  }
@@ -313,11 +393,11 @@ let Since = Since_1 = class Since extends index_js_5.mol.Entity.Base() {
313
393
  */
314
394
  toNum() {
315
395
  return (this.value |
316
- (this.relative === "absolute" ? index_js_2.Zero : (0, index_js_6.numFrom)("0x8000000000000000")) |
396
+ (this.relative === "absolute" ? index_js_3.Zero : (0, index_js_7.numFrom)("0x8000000000000000")) |
317
397
  {
318
- blockNumber: (0, index_js_6.numFrom)("0x0000000000000000"),
319
- epoch: (0, index_js_6.numFrom)("0x2000000000000000"),
320
- timestamp: (0, index_js_6.numFrom)("0x4000000000000000"),
398
+ blockNumber: (0, index_js_7.numFrom)("0x0000000000000000"),
399
+ epoch: (0, index_js_7.numFrom)("0x2000000000000000"),
400
+ timestamp: (0, index_js_7.numFrom)("0x4000000000000000"),
321
401
  }[this.metric]);
322
402
  }
323
403
  /**
@@ -332,21 +412,21 @@ let Since = Since_1 = class Since extends index_js_5.mol.Entity.Base() {
332
412
  * ```
333
413
  */
334
414
  static fromNum(numLike) {
335
- const num = (0, index_js_6.numFrom)(numLike);
336
- const relative = num >> (0, index_js_6.numFrom)(63) === index_js_2.Zero ? "absolute" : "relative";
337
- const metric = ["blockNumber", "epoch", "timestamp"][Number((num >> (0, index_js_6.numFrom)(61)) & (0, index_js_6.numFrom)(3))];
338
- const value = num & (0, index_js_6.numFrom)("0x00ffffffffffffff");
415
+ const num = (0, index_js_7.numFrom)(numLike);
416
+ const relative = num >> (0, index_js_7.numFrom)(63) === index_js_3.Zero ? "absolute" : "relative";
417
+ const metric = ["blockNumber", "epoch", "timestamp"][Number((num >> (0, index_js_7.numFrom)(61)) & (0, index_js_7.numFrom)(3))];
418
+ const value = num & (0, index_js_7.numFrom)("0x00ffffffffffffff");
339
419
  return new Since_1(relative, metric, value);
340
420
  }
341
421
  };
342
422
  exports.Since = Since;
343
423
  exports.Since = Since = Since_1 = __decorate([
344
- index_js_5.mol.codec(index_js_5.mol.Uint64.mapIn((encodable) => Since.from(encodable).toNum()))
424
+ index_js_6.mol.codec(index_js_6.mol.Uint64.mapIn((encodable) => Since.from(encodable).toNum()))
345
425
  ], Since);
346
426
  /**
347
427
  * @public
348
428
  */
349
- let CellInput = CellInput_1 = class CellInput extends index_js_5.mol.Entity.Base() {
429
+ let CellInput = CellInput_1 = class CellInput extends index_js_6.mol.Entity.Base() {
350
430
  /**
351
431
  * Creates an instance of CellInput.
352
432
  *
@@ -382,16 +462,29 @@ let CellInput = CellInput_1 = class CellInput extends index_js_5.mol.Entity.Base
382
462
  }
383
463
  return new CellInput_1(OutPoint.from("previousOutput" in cellInput
384
464
  ? cellInput.previousOutput
385
- : cellInput.outPoint), Since.from(cellInput.since ?? 0).toNum(), (0, index_js_7.apply)(CellOutput.from, cellInput.cellOutput), (0, index_js_7.apply)(index_js_4.hexFrom, cellInput.outputData));
465
+ : cellInput.outPoint), Since.from(cellInput.since ?? 0).toNum(), (0, index_js_8.apply)(CellOutput.from, cellInput.cellOutput), (0, index_js_8.apply)(index_js_5.hexFrom, cellInput.outputData));
466
+ }
467
+ async getCell(client) {
468
+ await this.completeExtraInfos(client);
469
+ if (!this.cellOutput || !this.outputData) {
470
+ throw new Error("Unable to complete input");
471
+ }
472
+ return Cell.from({
473
+ outPoint: this.previousOutput,
474
+ cellOutput: this.cellOutput,
475
+ outputData: this.outputData,
476
+ });
386
477
  }
387
478
  /**
388
- * Complete extra infos in the input. Like the output of the out point.
479
+ * Complete extra infos in the input. Including
480
+ * - Previous cell output
481
+ * - Previous cell data
389
482
  * The instance will be modified.
390
483
  *
391
484
  * @returns true if succeed.
392
485
  * @example
393
486
  * ```typescript
394
- * await cellInput.completeExtraInfos();
487
+ * await cellInput.completeExtraInfos(client);
395
488
  * ```
396
489
  */
397
490
  async completeExtraInfos(client) {
@@ -405,6 +498,14 @@ let CellInput = CellInput_1 = class CellInput extends index_js_5.mol.Entity.Base
405
498
  this.cellOutput = cell.cellOutput;
406
499
  this.outputData = cell.outputData;
407
500
  }
501
+ /**
502
+ * The extra capacity created when consume this input.
503
+ * This is usually NervosDAO interest, see https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md.
504
+ * And it can also be miners' income. (But this is not implemented yet)
505
+ */
506
+ async getExtraCapacity(client) {
507
+ return (await this.getCell(client)).getDaoProfit(client);
508
+ }
408
509
  clone() {
409
510
  const cloned = super.clone();
410
511
  cloned.cellOutput = this.cellOutput;
@@ -414,18 +515,18 @@ let CellInput = CellInput_1 = class CellInput extends index_js_5.mol.Entity.Base
414
515
  };
415
516
  exports.CellInput = CellInput;
416
517
  exports.CellInput = CellInput = CellInput_1 = __decorate([
417
- index_js_5.mol.codec(index_js_5.mol
518
+ index_js_6.mol.codec(index_js_6.mol
418
519
  .struct({
419
520
  since: Since,
420
521
  previousOutput: OutPoint,
421
522
  })
422
523
  .mapIn((encodable) => CellInput.from(encodable)))
423
524
  ], CellInput);
424
- exports.CellInputVec = index_js_5.mol.vector(CellInput);
525
+ exports.CellInputVec = index_js_6.mol.vector(CellInput);
425
526
  /**
426
527
  * @public
427
528
  */
428
- let CellDep = CellDep_1 = class CellDep extends index_js_5.mol.Entity.Base() {
529
+ let CellDep = CellDep_1 = class CellDep extends index_js_6.mol.Entity.Base() {
429
530
  /**
430
531
  * Creates an instance of CellDep.
431
532
  *
@@ -473,16 +574,16 @@ let CellDep = CellDep_1 = class CellDep extends index_js_5.mol.Entity.Base() {
473
574
  };
474
575
  exports.CellDep = CellDep;
475
576
  exports.CellDep = CellDep = CellDep_1 = __decorate([
476
- index_js_5.mol.codec(index_js_5.mol.struct({
577
+ index_js_6.mol.codec(index_js_6.mol.struct({
477
578
  outPoint: OutPoint,
478
579
  depType: exports.DepTypeCodec,
479
580
  }))
480
581
  ], CellDep);
481
- exports.CellDepVec = index_js_5.mol.vector(CellDep);
582
+ exports.CellDepVec = index_js_6.mol.vector(CellDep);
482
583
  /**
483
584
  * @public
484
585
  */
485
- let WitnessArgs = WitnessArgs_1 = class WitnessArgs extends index_js_5.mol.Entity.Base() {
586
+ let WitnessArgs = WitnessArgs_1 = class WitnessArgs extends index_js_6.mol.Entity.Base() {
486
587
  /**
487
588
  * Creates an instance of WitnessArgs.
488
589
  *
@@ -515,36 +616,38 @@ let WitnessArgs = WitnessArgs_1 = class WitnessArgs extends index_js_5.mol.Entit
515
616
  if (witnessArgs instanceof WitnessArgs_1) {
516
617
  return witnessArgs;
517
618
  }
518
- return new WitnessArgs_1((0, index_js_7.apply)(index_js_4.hexFrom, witnessArgs.lock), (0, index_js_7.apply)(index_js_4.hexFrom, witnessArgs.inputType), (0, index_js_7.apply)(index_js_4.hexFrom, witnessArgs.outputType));
619
+ return new WitnessArgs_1((0, index_js_8.apply)(index_js_5.hexFrom, witnessArgs.lock), (0, index_js_8.apply)(index_js_5.hexFrom, witnessArgs.inputType), (0, index_js_8.apply)(index_js_5.hexFrom, witnessArgs.outputType));
519
620
  }
520
621
  };
521
622
  exports.WitnessArgs = WitnessArgs;
522
623
  exports.WitnessArgs = WitnessArgs = WitnessArgs_1 = __decorate([
523
- index_js_5.mol.codec(index_js_5.mol.table({
524
- lock: index_js_5.mol.BytesOpt,
525
- inputType: index_js_5.mol.BytesOpt,
526
- outputType: index_js_5.mol.BytesOpt,
624
+ index_js_6.mol.codec(index_js_6.mol.table({
625
+ lock: index_js_6.mol.BytesOpt,
626
+ inputType: index_js_6.mol.BytesOpt,
627
+ outputType: index_js_6.mol.BytesOpt,
527
628
  }))
528
629
  ], WitnessArgs);
529
630
  /**
631
+ * Convert a bytes to a num.
632
+ *
530
633
  * @public
531
634
  */
532
635
  function udtBalanceFrom(dataLike) {
533
636
  const data = (0, index_js_1.bytesFrom)(dataLike).slice(0, 16);
534
- return (0, index_js_6.numFromBytes)(data);
637
+ return data.length === 0 ? index_js_3.Zero : (0, index_js_7.numFromBytes)(data);
535
638
  }
536
- exports.RawTransaction = index_js_5.mol.table({
537
- version: index_js_5.mol.Uint32,
639
+ exports.RawTransaction = index_js_6.mol.table({
640
+ version: index_js_6.mol.Uint32,
538
641
  cellDeps: exports.CellDepVec,
539
- headerDeps: index_js_5.mol.Byte32Vec,
642
+ headerDeps: index_js_6.mol.Byte32Vec,
540
643
  inputs: exports.CellInputVec,
541
644
  outputs: exports.CellOutputVec,
542
- outputsData: index_js_5.mol.BytesVec,
645
+ outputsData: index_js_6.mol.BytesVec,
543
646
  });
544
647
  /**
545
648
  * @public
546
649
  */
547
- let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entity.Base() {
650
+ let Transaction = Transaction_1 = class Transaction extends index_js_6.mol.Entity.Base() {
548
651
  /**
549
652
  * Creates an instance of Transaction.
550
653
  *
@@ -625,17 +728,17 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
625
728
  ...output,
626
729
  capacity: output.capacity ?? 0,
627
730
  });
628
- if (o.capacity === index_js_2.Zero) {
629
- o.capacity = (0, index_js_2.fixedPointFrom)(o.occupiedSize +
630
- ((0, index_js_7.apply)(index_js_1.bytesFrom, tx.outputsData?.[i])?.length ?? 0));
731
+ if (o.capacity === index_js_3.Zero) {
732
+ o.capacity = (0, index_js_3.fixedPointFrom)(o.occupiedSize +
733
+ ((0, index_js_8.apply)(index_js_1.bytesFrom, tx.outputsData?.[i])?.length ?? 0));
631
734
  }
632
735
  return o;
633
736
  }) ?? [];
634
- const outputsData = outputs.map((_, i) => (0, index_js_4.hexFrom)(tx.outputsData?.[i] ?? "0x"));
737
+ const outputsData = outputs.map((_, i) => (0, index_js_5.hexFrom)(tx.outputsData?.[i] ?? "0x"));
635
738
  if (tx.outputsData != null && outputsData.length < tx.outputsData.length) {
636
- outputsData.push(...tx.outputsData.slice(outputsData.length).map((d) => (0, index_js_4.hexFrom)(d)));
739
+ outputsData.push(...tx.outputsData.slice(outputsData.length).map((d) => (0, index_js_5.hexFrom)(d)));
637
740
  }
638
- return new Transaction_1((0, index_js_6.numFrom)(tx.version ?? 0), tx.cellDeps?.map((cellDep) => CellDep.from(cellDep)) ?? [], tx.headerDeps?.map(index_js_4.hexFrom) ?? [], tx.inputs?.map((input) => CellInput.from(input)) ?? [], outputs, outputsData, tx.witnesses?.map(index_js_4.hexFrom) ?? []);
741
+ return new Transaction_1((0, index_js_7.numFrom)(tx.version ?? 0), tx.cellDeps?.map((cellDep) => CellDep.from(cellDep)) ?? [], tx.headerDeps?.map(index_js_5.hexFrom) ?? [], tx.inputs?.map((input) => CellInput.from(input)) ?? [], outputs, outputsData, tx.witnesses?.map(index_js_5.hexFrom) ?? []);
639
742
  }
640
743
  /**
641
744
  * Creates a Transaction instance from a Lumos skeleton.
@@ -679,7 +782,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
679
782
  stringify() {
680
783
  return JSON.stringify(this, (_, value) => {
681
784
  if (typeof value === "bigint") {
682
- return (0, index_js_6.numToHex)(value);
785
+ return (0, index_js_7.numToHex)(value);
683
786
  }
684
787
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
685
788
  return value;
@@ -710,7 +813,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
710
813
  * ```
711
814
  */
712
815
  hash() {
713
- return (0, index_js_3.hashCkb)(this.rawToBytes());
816
+ return (0, index_js_4.hashCkb)(this.rawToBytes());
714
817
  }
715
818
  /**
716
819
  * Calculates the hash of the transaction with witnesses.
@@ -723,7 +826,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
723
826
  * ```
724
827
  */
725
828
  hashFull() {
726
- return (0, index_js_3.hashCkb)(this.toBytes());
829
+ return (0, index_js_4.hashCkb)(this.toBytes());
727
830
  }
728
831
  /**
729
832
  * Hashes a witness and updates the hasher.
@@ -737,8 +840,8 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
737
840
  * ```
738
841
  */
739
842
  static hashWitnessToHasher(witness, hasher) {
740
- const raw = (0, index_js_1.bytesFrom)((0, index_js_4.hexFrom)(witness));
741
- hasher.update((0, index_js_6.numToBytes)(raw.length, 8));
843
+ const raw = (0, index_js_1.bytesFrom)((0, index_js_5.hexFrom)(witness));
844
+ hasher.update((0, index_js_7.numToBytes)(raw.length, 8));
742
845
  hasher.update(raw);
743
846
  }
744
847
  /**
@@ -758,18 +861,15 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
758
861
  * }
759
862
  * ```
760
863
  */
761
- async getSignHashInfo(scriptLike, client, hasher = new index_js_3.HasherCkb()) {
864
+ async getSignHashInfo(scriptLike, client, hasher = new index_js_4.HasherCkb()) {
762
865
  const script = script_js_1.Script.from(scriptLike);
763
866
  let position = -1;
764
867
  hasher.update(this.hash());
765
868
  for (let i = 0; i < this.witnesses.length; i += 1) {
766
869
  const input = this.inputs[i];
767
870
  if (input) {
768
- await input.completeExtraInfos(client);
769
- if (!input.cellOutput) {
770
- throw new Error("Unable to complete input");
771
- }
772
- if (!script.eq(input.cellOutput.lock)) {
871
+ const { cellOutput } = await input.getCell(client);
872
+ if (!script.eq(cellOutput.lock)) {
773
873
  continue;
774
874
  }
775
875
  if (position === -1) {
@@ -804,13 +904,9 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
804
904
  async findInputIndexByLockId(scriptIdLike, client) {
805
905
  const script = script_js_1.Script.from({ ...scriptIdLike, args: "0x" });
806
906
  for (let i = 0; i < this.inputs.length; i += 1) {
807
- const input = this.inputs[i];
808
- await input.completeExtraInfos(client);
809
- if (!input.cellOutput) {
810
- throw new Error("Unable to complete input");
811
- }
812
- if (script.codeHash === input.cellOutput.lock.codeHash &&
813
- script.hashType === input.cellOutput.lock.hashType) {
907
+ const { cellOutput } = await this.inputs[i].getCell(client);
908
+ if (script.codeHash === cellOutput.lock.codeHash &&
909
+ script.hashType === cellOutput.lock.hashType) {
814
910
  return i;
815
911
  }
816
912
  }
@@ -830,12 +926,8 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
830
926
  async findInputIndexByLock(scriptLike, client) {
831
927
  const script = script_js_1.Script.from(scriptLike);
832
928
  for (let i = 0; i < this.inputs.length; i += 1) {
833
- const input = this.inputs[i];
834
- await input.completeExtraInfos(client);
835
- if (!input.cellOutput) {
836
- throw new Error("Unable to complete input");
837
- }
838
- if (script.eq(input.cellOutput.lock)) {
929
+ const { cellOutput } = await this.inputs[i].getCell(client);
930
+ if (script.eq(cellOutput.lock)) {
839
931
  return i;
840
932
  }
841
933
  }
@@ -855,12 +947,8 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
855
947
  async findLastInputIndexByLock(scriptLike, client) {
856
948
  const script = script_js_1.Script.from(scriptLike);
857
949
  for (let i = this.inputs.length - 1; i >= 0; i -= 1) {
858
- const input = this.inputs[i];
859
- await input.completeExtraInfos(client);
860
- if (!input.cellOutput) {
861
- throw new Error("Unable to complete input");
862
- }
863
- if (script.eq(input.cellOutput.lock)) {
950
+ const { cellOutput } = await this.inputs[i].getCell(client);
951
+ if (script.eq(cellOutput.lock)) {
864
952
  return i;
865
953
  }
866
954
  }
@@ -948,7 +1036,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
948
1036
  if (this.outputsData.length < index) {
949
1037
  this.outputsData.push(...Array.from(new Array(index - this.outputsData.length), () => "0x"));
950
1038
  }
951
- this.outputsData[index] = (0, index_js_4.hexFrom)(witness);
1039
+ this.outputsData[index] = (0, index_js_5.hexFrom)(witness);
952
1040
  }
953
1041
  /**
954
1042
  * get input
@@ -961,7 +1049,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
961
1049
  * ```
962
1050
  */
963
1051
  getInput(index) {
964
- return this.inputs[Number((0, index_js_6.numFrom)(index))];
1052
+ return this.inputs[Number((0, index_js_7.numFrom)(index))];
965
1053
  }
966
1054
  /**
967
1055
  * add input
@@ -974,6 +1062,9 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
974
1062
  * ```
975
1063
  */
976
1064
  addInput(inputLike) {
1065
+ if (this.witnesses.length > this.inputs.length) {
1066
+ this.witnesses.splice(this.inputs.length, 0, "0x");
1067
+ }
977
1068
  return this.inputs.push(CellInput.from(inputLike));
978
1069
  }
979
1070
  /**
@@ -987,7 +1078,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
987
1078
  * ```
988
1079
  */
989
1080
  getOutput(index) {
990
- const i = Number((0, index_js_6.numFrom)(index));
1081
+ const i = Number((0, index_js_7.numFrom)(index));
991
1082
  if (i >= this.outputs.length) {
992
1083
  return;
993
1084
  }
@@ -1012,8 +1103,8 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1012
1103
  ...outputLike,
1013
1104
  capacity: outputLike.capacity ?? 0,
1014
1105
  });
1015
- if (output.capacity === index_js_2.Zero) {
1016
- output.capacity = (0, index_js_2.fixedPointFrom)(output.occupiedSize + (0, index_js_1.bytesFrom)(outputData).length);
1106
+ if (output.capacity === index_js_3.Zero) {
1107
+ output.capacity = (0, index_js_3.fixedPointFrom)(output.occupiedSize + (0, index_js_1.bytesFrom)(outputData).length);
1017
1108
  }
1018
1109
  const len = this.outputs.push(output);
1019
1110
  this.setOutputDataAt(len - 1, outputData);
@@ -1048,10 +1139,24 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1048
1139
  * ```
1049
1140
  */
1050
1141
  setWitnessArgsAt(index, witness) {
1142
+ this.setWitnessAt(index, witness.toBytes());
1143
+ }
1144
+ /**
1145
+ * Set witness at index
1146
+ *
1147
+ * @param index - The index of the witness.
1148
+ * @param witness - The witness to set.
1149
+ *
1150
+ * @example
1151
+ * ```typescript
1152
+ * await tx.setWitnessAt(0, witness);
1153
+ * ```
1154
+ */
1155
+ setWitnessAt(index, witness) {
1051
1156
  if (this.witnesses.length < index) {
1052
1157
  this.witnesses.push(...Array.from(new Array(index - this.witnesses.length), () => "0x"));
1053
1158
  }
1054
- this.witnesses[index] = (0, index_js_4.hexFrom)(witness.toBytes());
1159
+ this.witnesses[index] = (0, index_js_5.hexFrom)(witness);
1055
1160
  }
1056
1161
  /**
1057
1162
  * Prepare dummy witness for sighash all method
@@ -1072,32 +1177,30 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1072
1177
  return;
1073
1178
  }
1074
1179
  const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
1075
- witness.lock = (0, index_js_4.hexFrom)(Array.from(new Array(lockLen), () => 0));
1180
+ witness.lock = (0, index_js_5.hexFrom)(Array.from(new Array(lockLen), () => 0));
1076
1181
  this.setWitnessArgsAt(position, witness);
1077
1182
  }
1183
+ async getInputsCapacityExtra(client) {
1184
+ return (0, index_js_8.reduceAsync)(this.inputs, async (acc, input) => acc + (await input.getExtraCapacity(client)), (0, index_js_7.numFrom)(0));
1185
+ }
1186
+ // This also includes extra amount
1078
1187
  async getInputsCapacity(client) {
1079
- return (0, index_js_7.reduceAsync)(this.inputs, async (acc, input) => {
1080
- await input.completeExtraInfos(client);
1081
- if (!input.cellOutput) {
1082
- throw new Error("Unable to complete input");
1083
- }
1084
- return acc + input.cellOutput.capacity;
1085
- }, (0, index_js_6.numFrom)(0));
1188
+ return ((await (0, index_js_8.reduceAsync)(this.inputs, async (acc, input) => {
1189
+ const { cellOutput: { capacity }, } = await input.getCell(client);
1190
+ return acc + capacity;
1191
+ }, (0, index_js_7.numFrom)(0))) + (await this.getInputsCapacityExtra(client)));
1086
1192
  }
1087
1193
  getOutputsCapacity() {
1088
- return this.outputs.reduce((acc, { capacity }) => acc + capacity, (0, index_js_6.numFrom)(0));
1194
+ return this.outputs.reduce((acc, { capacity }) => acc + capacity, (0, index_js_7.numFrom)(0));
1089
1195
  }
1090
1196
  async getInputsUdtBalance(client, type) {
1091
- return (0, index_js_7.reduceAsync)(this.inputs, async (acc, input) => {
1092
- await input.completeExtraInfos(client);
1093
- if (!input.cellOutput || !input.outputData) {
1094
- throw new Error("Unable to complete input");
1095
- }
1096
- if (!input.cellOutput.type?.eq(type)) {
1197
+ return (0, index_js_8.reduceAsync)(this.inputs, async (acc, input) => {
1198
+ const { cellOutput, outputData } = await input.getCell(client);
1199
+ if (!cellOutput.type?.eq(type)) {
1097
1200
  return;
1098
1201
  }
1099
- return acc + udtBalanceFrom(input.outputData);
1100
- }, (0, index_js_6.numFrom)(0));
1202
+ return acc + udtBalanceFrom(outputData);
1203
+ }, (0, index_js_7.numFrom)(0));
1101
1204
  }
1102
1205
  getOutputsUdtBalance(type) {
1103
1206
  return this.outputs.reduce((acc, output, i) => {
@@ -1105,7 +1208,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1105
1208
  return acc;
1106
1209
  }
1107
1210
  return acc + udtBalanceFrom(this.outputsData[i]);
1108
- }, (0, index_js_6.numFrom)(0));
1211
+ }, (0, index_js_7.numFrom)(0));
1109
1212
  }
1110
1213
  async completeInputs(from, filter, accumulator, init) {
1111
1214
  const collectedCells = [];
@@ -1135,7 +1238,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1135
1238
  };
1136
1239
  }
1137
1240
  async completeInputsByCapacity(from, capacityTweak, filter) {
1138
- const exceptedCapacity = this.getOutputsCapacity() + (0, index_js_6.numFrom)(capacityTweak ?? 0);
1241
+ const exceptedCapacity = this.getOutputsCapacity() + (0, index_js_7.numFrom)(capacityTweak ?? 0);
1139
1242
  const inputsCapacity = await this.getInputsCapacity(from.client);
1140
1243
  if (inputsCapacity >= exceptedCapacity) {
1141
1244
  return 0;
@@ -1150,24 +1253,24 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1150
1253
  if (accumulated === undefined) {
1151
1254
  return addedCount;
1152
1255
  }
1153
- throw new Error(`Insufficient CKB, need ${(0, index_js_2.fixedPointToString)(exceptedCapacity - accumulated)} extra CKB`);
1256
+ throw new Error(`Insufficient CKB, need ${(0, index_js_3.fixedPointToString)(exceptedCapacity - accumulated)} extra CKB`);
1154
1257
  }
1155
1258
  async completeInputsAll(from, filter) {
1156
1259
  const { addedCount } = await this.completeInputs(from, filter ?? {
1157
1260
  scriptLenRange: [0, 1],
1158
1261
  outputDataLenRange: [0, 1],
1159
- }, (acc, { cellOutput: { capacity } }) => acc + capacity, index_js_2.Zero);
1262
+ }, (acc, { cellOutput: { capacity } }) => acc + capacity, index_js_3.Zero);
1160
1263
  return addedCount;
1161
1264
  }
1162
1265
  async completeInputsByUdt(from, type, balanceTweak) {
1163
- const exceptedBalance = this.getOutputsUdtBalance(type) + (0, index_js_6.numFrom)(balanceTweak ?? 0);
1266
+ const exceptedBalance = this.getOutputsUdtBalance(type) + (0, index_js_7.numFrom)(balanceTweak ?? 0);
1164
1267
  const inputsBalance = await this.getInputsUdtBalance(from.client, type);
1165
1268
  if (inputsBalance >= exceptedBalance) {
1166
1269
  return 0;
1167
1270
  }
1168
1271
  const { addedCount, accumulated } = await this.completeInputs(from, {
1169
1272
  script: type,
1170
- outputDataLenRange: [16, (0, index_js_6.numFrom)("0xffffffff")],
1273
+ outputDataLenRange: [16, (0, index_js_7.numFrom)("0xffffffff")],
1171
1274
  }, (acc, { outputData }) => {
1172
1275
  const balance = udtBalanceFrom(outputData);
1173
1276
  const sum = acc + balance;
@@ -1179,15 +1282,12 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1179
1282
  throw new Error(`Insufficient coin, need ${exceptedBalance - accumulated} extra coin`);
1180
1283
  }
1181
1284
  async completeInputsAddOne(from, filter) {
1182
- for await (const cell of from.findCells(filter ?? {
1285
+ const { addedCount, accumulated } = await this.completeInputs(from, filter ?? {
1183
1286
  scriptLenRange: [0, 1],
1184
1287
  outputDataLenRange: [0, 1],
1185
- }, true, undefined, 1)) {
1186
- if (this.inputs.some(({ previousOutput }) => previousOutput.eq(cell.outPoint))) {
1187
- continue;
1188
- }
1189
- this.addInput(cell);
1190
- return 1;
1288
+ }, () => undefined, true);
1289
+ if (accumulated === undefined) {
1290
+ return addedCount;
1191
1291
  }
1192
1292
  throw new Error(`Insufficient CKB, need at least one new cell`);
1193
1293
  }
@@ -1197,25 +1297,25 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1197
1297
  }
1198
1298
  return this.completeInputsAddOne(from, filter);
1199
1299
  }
1200
- async fee(client) {
1300
+ async getFee(client) {
1201
1301
  return (await this.getInputsCapacity(client)) - this.getOutputsCapacity();
1202
1302
  }
1203
- async feeRate(client) {
1204
- return (((await this.fee(client)) * (0, index_js_6.numFrom)(1000)) /
1205
- (0, index_js_6.numFrom)(this.toBytes().length + 4));
1303
+ async getFeeRate(client) {
1304
+ return (((await this.getFee(client)) * (0, index_js_7.numFrom)(1000)) /
1305
+ (0, index_js_7.numFrom)(this.toBytes().length + 4));
1206
1306
  }
1207
1307
  estimateFee(feeRate) {
1208
1308
  const txSize = this.toBytes().length + 4;
1209
1309
  // + 999 then / 1000 to ceil the calculated fee
1210
- return ((0, index_js_6.numFrom)(txSize) * (0, index_js_6.numFrom)(feeRate) + (0, index_js_6.numFrom)(999)) / (0, index_js_6.numFrom)(1000);
1310
+ return ((0, index_js_7.numFrom)(txSize) * (0, index_js_7.numFrom)(feeRate) + (0, index_js_7.numFrom)(999)) / (0, index_js_7.numFrom)(1000);
1211
1311
  }
1212
1312
  async completeFee(from, change, expectedFeeRate, filter, options) {
1213
1313
  const feeRate = expectedFeeRate ??
1214
1314
  (await from.client.getFeeRate(options?.feeRateBlockRange, options));
1215
1315
  // Complete all inputs extra infos for cache
1216
1316
  await this.getInputsCapacity(from.client);
1217
- let leastFee = index_js_2.Zero;
1218
- let leastExtraCapacity = index_js_2.Zero;
1317
+ let leastFee = index_js_3.Zero;
1318
+ let leastExtraCapacity = index_js_3.Zero;
1219
1319
  while (true) {
1220
1320
  const tx = this.clone();
1221
1321
  const collected = await (async () => {
@@ -1223,30 +1323,30 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1223
1323
  return await tx.completeInputsByCapacity(from, leastFee + leastExtraCapacity, filter);
1224
1324
  }
1225
1325
  catch (err) {
1226
- if (leastExtraCapacity !== index_js_2.Zero) {
1326
+ if (leastExtraCapacity !== index_js_3.Zero) {
1227
1327
  throw new Error("Not enough capacity for the change cell");
1228
1328
  }
1229
1329
  throw err;
1230
1330
  }
1231
1331
  })();
1232
1332
  await from.prepareTransaction(tx);
1233
- if (leastFee === index_js_2.Zero) {
1333
+ if (leastFee === index_js_3.Zero) {
1234
1334
  // The initial fee is calculated based on prepared transaction
1235
1335
  leastFee = tx.estimateFee(feeRate);
1236
1336
  }
1237
- const fee = await tx.fee(from.client);
1337
+ const fee = await tx.getFee(from.client);
1238
1338
  // The extra capacity paid the fee without a change
1239
1339
  if (fee === leastFee) {
1240
1340
  this.copy(tx);
1241
1341
  return [collected, false];
1242
1342
  }
1243
- const needed = (0, index_js_6.numFrom)(await Promise.resolve(change(tx, fee - leastFee)));
1343
+ const needed = (0, index_js_7.numFrom)(await Promise.resolve(change(tx, fee - leastFee)));
1244
1344
  // No enough extra capacity to create new cells for change
1245
- if (needed > index_js_2.Zero) {
1345
+ if (needed > index_js_3.Zero) {
1246
1346
  leastExtraCapacity = needed;
1247
1347
  continue;
1248
1348
  }
1249
- if ((await tx.fee(from.client)) !== leastFee) {
1349
+ if ((await tx.getFee(from.client)) !== leastFee) {
1250
1350
  throw new Error("The change function doesn't use all available capacity");
1251
1351
  }
1252
1352
  // New change cells created, update the fee
@@ -1268,7 +1368,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1268
1368
  const script = script_js_1.Script.from(change);
1269
1369
  return this.completeFee(from, (tx, capacity) => {
1270
1370
  const changeCell = CellOutput.from({ capacity: 0, lock: script });
1271
- const occupiedCapacity = (0, index_js_2.fixedPointFrom)(changeCell.occupiedSize);
1371
+ const occupiedCapacity = (0, index_js_3.fixedPointFrom)(changeCell.occupiedSize);
1272
1372
  if (capacity < occupiedCapacity) {
1273
1373
  return occupiedCapacity;
1274
1374
  }
@@ -1282,7 +1382,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1282
1382
  return this.completeFeeChangeToLock(from, script, feeRate, filter);
1283
1383
  }
1284
1384
  completeFeeChangeToOutput(from, index, feeRate, filter) {
1285
- const change = Number((0, index_js_6.numFrom)(index));
1385
+ const change = Number((0, index_js_7.numFrom)(index));
1286
1386
  if (!this.outputs[change]) {
1287
1387
  throw new Error("Non-existed output to change");
1288
1388
  }
@@ -1294,10 +1394,10 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1294
1394
  };
1295
1395
  exports.Transaction = Transaction;
1296
1396
  exports.Transaction = Transaction = Transaction_1 = __decorate([
1297
- index_js_5.mol.codec(index_js_5.mol
1397
+ index_js_6.mol.codec(index_js_6.mol
1298
1398
  .table({
1299
1399
  raw: exports.RawTransaction,
1300
- witnesses: index_js_5.mol.BytesVec,
1400
+ witnesses: index_js_6.mol.BytesVec,
1301
1401
  })
1302
1402
  .mapIn((txLike) => {
1303
1403
  const tx = Transaction.from(txLike);
@@ -1308,3 +1408,38 @@ exports.Transaction = Transaction = Transaction_1 = __decorate([
1308
1408
  })
1309
1409
  .mapOut((tx) => Transaction.from({ ...tx.raw, witnesses: tx.witnesses })))
1310
1410
  ], Transaction);
1411
+ /**
1412
+ * Calculate Nervos DAO profit between two blocks
1413
+ */
1414
+ function calcDaoProfit(profitableCapacity, depositHeaderLike, withdrawHeaderLike) {
1415
+ const depositHeader = index_js_2.ClientBlockHeader.from(depositHeaderLike);
1416
+ const withdrawHeader = index_js_2.ClientBlockHeader.from(withdrawHeaderLike);
1417
+ const profitableSize = (0, index_js_7.numFrom)(profitableCapacity);
1418
+ return ((profitableSize * withdrawHeader.dao.ar) / depositHeader.dao.ar -
1419
+ profitableSize);
1420
+ }
1421
+ /**
1422
+ * Calculate claimable epoch for Nervos DAO withdrawal
1423
+ * See https://github.com/nervosnetwork/rfcs/blob/master/rfcs/0023-dao-deposit-withdraw/0023-dao-deposit-withdraw.md
1424
+ */
1425
+ function calcDaoClaimEpoch(depositHeader, withdrawHeader) {
1426
+ const depositEpoch = index_js_2.ClientBlockHeader.from(depositHeader).epoch;
1427
+ const withdrawEpoch = index_js_2.ClientBlockHeader.from(withdrawHeader).epoch;
1428
+ const intDiff = withdrawEpoch[0] - depositEpoch[0];
1429
+ // deposit[1] withdraw[1]
1430
+ // ---------- <= -----------
1431
+ // deposit[2] withdraw[2]
1432
+ if (intDiff % (0, index_js_7.numFrom)(180) !== (0, index_js_7.numFrom)(0) ||
1433
+ depositEpoch[1] * withdrawEpoch[2] <= depositEpoch[2] * withdrawEpoch[1]) {
1434
+ return [
1435
+ depositEpoch[0] + (intDiff / (0, index_js_7.numFrom)(180) + (0, index_js_7.numFrom)(1)) * (0, index_js_7.numFrom)(180),
1436
+ depositEpoch[1],
1437
+ depositEpoch[2],
1438
+ ];
1439
+ }
1440
+ return [
1441
+ depositEpoch[0] + (intDiff / (0, index_js_7.numFrom)(180)) * (0, index_js_7.numFrom)(180),
1442
+ depositEpoch[1],
1443
+ depositEpoch[2],
1444
+ ];
1445
+ }