@ckb-ccc/core 1.5.3 → 1.7.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 (161) hide show
  1. package/CHANGELOG.md +61 -7
  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 +125 -7
  5. package/dist/ckb/transaction.d.ts.map +1 -1
  6. package/dist/ckb/transaction.js +263 -73
  7. package/dist/client/cache/cache.d.ts +67 -11
  8. package/dist/client/cache/cache.d.ts.map +1 -1
  9. package/dist/client/cache/cache.js +94 -9
  10. package/dist/client/cache/memory.advanced.d.ts +2 -2
  11. package/dist/client/cache/memory.advanced.d.ts.map +1 -1
  12. package/dist/client/cache/memory.advanced.js +19 -7
  13. package/dist/client/cache/memory.d.ts +24 -7
  14. package/dist/client/cache/memory.d.ts.map +1 -1
  15. package/dist/client/cache/memory.js +76 -18
  16. package/dist/client/client.d.ts +30 -7
  17. package/dist/client/client.d.ts.map +1 -1
  18. package/dist/client/client.js +114 -22
  19. package/dist/client/clientPublicMainnet.advanced.d.ts +2 -1
  20. package/dist/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  21. package/dist/client/clientPublicMainnet.advanced.js +1 -1
  22. package/dist/client/clientPublicMainnet.d.ts +2 -1
  23. package/dist/client/clientPublicMainnet.d.ts.map +1 -1
  24. package/dist/client/clientPublicMainnet.js +1 -6
  25. package/dist/client/clientPublicTestnet.advanced.d.ts +2 -1
  26. package/dist/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  27. package/dist/client/clientPublicTestnet.advanced.js +1 -1
  28. package/dist/client/clientPublicTestnet.d.ts +2 -1
  29. package/dist/client/clientPublicTestnet.d.ts.map +1 -1
  30. package/dist/client/clientTypes.advanced.d.ts +3 -0
  31. package/dist/client/clientTypes.advanced.d.ts.map +1 -1
  32. package/dist/client/clientTypes.advanced.js +3 -0
  33. package/dist/client/clientTypes.d.ts +110 -38
  34. package/dist/client/clientTypes.d.ts.map +1 -1
  35. package/dist/client/clientTypes.js +118 -31
  36. package/dist/client/index.d.ts +1 -0
  37. package/dist/client/index.d.ts.map +1 -1
  38. package/dist/client/index.js +1 -0
  39. package/dist/client/jsonRpc/client.d.ts +4 -4
  40. package/dist/client/jsonRpc/client.d.ts.map +1 -1
  41. package/dist/client/jsonRpc/client.js +4 -4
  42. package/dist/client/jsonRpc/transformers.js +3 -3
  43. package/dist/client/knownScript.d.ts +26 -0
  44. package/dist/client/knownScript.d.ts.map +1 -0
  45. package/dist/client/knownScript.js +27 -0
  46. package/dist/jsonRpc/transports/webSocket.d.ts.map +1 -1
  47. package/dist/jsonRpc/transports/webSocket.js +7 -3
  48. package/dist/molecule/predefined.d.ts +10 -10
  49. package/dist/molecule/predefined.d.ts.map +1 -1
  50. package/dist/num/index.d.ts.map +1 -1
  51. package/dist/num/index.js +17 -1
  52. package/dist/signer/ckb/signerCkbPublicKey.d.ts.map +1 -1
  53. package/dist/signer/ckb/signerCkbPublicKey.js +1 -5
  54. package/dist/signer/nostr/index.d.ts +1 -0
  55. package/dist/signer/nostr/index.d.ts.map +1 -1
  56. package/dist/signer/nostr/index.js +1 -0
  57. package/dist/signer/nostr/signerNostr.d.ts +1 -1
  58. package/dist/signer/nostr/signerNostr.d.ts.map +1 -1
  59. package/dist/signer/nostr/signerNostr.js +8 -0
  60. package/dist/signer/nostr/signerNostrPrivateKey.d.ts +5 -7
  61. package/dist/signer/nostr/signerNostrPrivateKey.d.ts.map +1 -1
  62. package/dist/signer/nostr/signerNostrPrivateKey.js +6 -12
  63. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts +11 -0
  64. package/dist/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +1 -0
  65. package/dist/signer/nostr/signerNostrPublicKeyReadonly.js +22 -0
  66. package/dist/utils/index.d.ts +4 -0
  67. package/dist/utils/index.d.ts.map +1 -1
  68. package/dist/utils/index.js +13 -1
  69. package/dist.commonjs/advanced.js +17 -7
  70. package/dist.commonjs/ckb/script.d.ts +2 -1
  71. package/dist.commonjs/ckb/script.d.ts.map +1 -1
  72. package/dist.commonjs/ckb/transaction.d.ts +125 -7
  73. package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
  74. package/dist.commonjs/ckb/transaction.js +352 -160
  75. package/dist.commonjs/client/cache/cache.d.ts +67 -11
  76. package/dist.commonjs/client/cache/cache.d.ts.map +1 -1
  77. package/dist.commonjs/client/cache/cache.js +94 -9
  78. package/dist.commonjs/client/cache/memory.advanced.d.ts +2 -2
  79. package/dist.commonjs/client/cache/memory.advanced.d.ts.map +1 -1
  80. package/dist.commonjs/client/cache/memory.advanced.js +19 -7
  81. package/dist.commonjs/client/cache/memory.d.ts +24 -7
  82. package/dist.commonjs/client/cache/memory.d.ts.map +1 -1
  83. package/dist.commonjs/client/cache/memory.js +75 -17
  84. package/dist.commonjs/client/client.d.ts +30 -7
  85. package/dist.commonjs/client/client.d.ts.map +1 -1
  86. package/dist.commonjs/client/client.js +112 -20
  87. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts +2 -1
  88. package/dist.commonjs/client/clientPublicMainnet.advanced.d.ts.map +1 -1
  89. package/dist.commonjs/client/clientPublicMainnet.advanced.js +21 -21
  90. package/dist.commonjs/client/clientPublicMainnet.d.ts +2 -1
  91. package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
  92. package/dist.commonjs/client/clientPublicMainnet.js +1 -6
  93. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts +2 -1
  94. package/dist.commonjs/client/clientPublicTestnet.advanced.d.ts.map +1 -1
  95. package/dist.commonjs/client/clientPublicTestnet.advanced.js +21 -21
  96. package/dist.commonjs/client/clientPublicTestnet.d.ts +2 -1
  97. package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
  98. package/dist.commonjs/client/clientTypes.advanced.d.ts +3 -0
  99. package/dist.commonjs/client/clientTypes.advanced.d.ts.map +1 -1
  100. package/dist.commonjs/client/clientTypes.advanced.js +4 -0
  101. package/dist.commonjs/client/clientTypes.d.ts +110 -38
  102. package/dist.commonjs/client/clientTypes.d.ts.map +1 -1
  103. package/dist.commonjs/client/clientTypes.js +123 -31
  104. package/dist.commonjs/client/index.d.ts +1 -0
  105. package/dist.commonjs/client/index.d.ts.map +1 -1
  106. package/dist.commonjs/client/index.js +1 -0
  107. package/dist.commonjs/client/jsonRpc/client.d.ts +4 -4
  108. package/dist.commonjs/client/jsonRpc/client.d.ts.map +1 -1
  109. package/dist.commonjs/client/jsonRpc/client.js +4 -4
  110. package/dist.commonjs/client/jsonRpc/transformers.js +2 -2
  111. package/dist.commonjs/client/knownScript.d.ts +26 -0
  112. package/dist.commonjs/client/knownScript.d.ts.map +1 -0
  113. package/dist.commonjs/client/knownScript.js +30 -0
  114. package/dist.commonjs/index.js +17 -7
  115. package/dist.commonjs/jsonRpc/transports/webSocket.d.ts.map +1 -1
  116. package/dist.commonjs/jsonRpc/transports/webSocket.js +7 -3
  117. package/dist.commonjs/molecule/index.js +17 -7
  118. package/dist.commonjs/num/index.d.ts.map +1 -1
  119. package/dist.commonjs/num/index.js +17 -1
  120. package/dist.commonjs/signer/ckb/signerCkbPublicKey.d.ts.map +1 -1
  121. package/dist.commonjs/signer/ckb/signerCkbPublicKey.js +1 -5
  122. package/dist.commonjs/signer/nostr/index.d.ts +1 -0
  123. package/dist.commonjs/signer/nostr/index.d.ts.map +1 -1
  124. package/dist.commonjs/signer/nostr/index.js +1 -0
  125. package/dist.commonjs/signer/nostr/signerNostr.d.ts +1 -1
  126. package/dist.commonjs/signer/nostr/signerNostr.d.ts.map +1 -1
  127. package/dist.commonjs/signer/nostr/signerNostr.js +8 -0
  128. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts +5 -7
  129. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.d.ts.map +1 -1
  130. package/dist.commonjs/signer/nostr/signerNostrPrivateKey.js +6 -12
  131. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts +11 -0
  132. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.d.ts.map +1 -0
  133. package/dist.commonjs/signer/nostr/signerNostrPublicKeyReadonly.js +26 -0
  134. package/dist.commonjs/utils/index.d.ts +4 -0
  135. package/dist.commonjs/utils/index.d.ts.map +1 -1
  136. package/dist.commonjs/utils/index.js +13 -0
  137. package/package.json +1 -1
  138. package/src/ckb/script.ts +2 -1
  139. package/src/ckb/transaction.ts +376 -97
  140. package/src/client/cache/cache.ts +126 -13
  141. package/src/client/cache/memory.advanced.ts +24 -7
  142. package/src/client/cache/memory.ts +130 -30
  143. package/src/client/client.ts +172 -28
  144. package/src/client/clientPublicMainnet.advanced.ts +2 -1
  145. package/src/client/clientPublicMainnet.ts +3 -7
  146. package/src/client/clientPublicTestnet.advanced.ts +2 -1
  147. package/src/client/clientPublicTestnet.ts +2 -1
  148. package/src/client/clientTypes.advanced.ts +4 -0
  149. package/src/client/clientTypes.ts +224 -61
  150. package/src/client/index.ts +1 -0
  151. package/src/client/jsonRpc/client.ts +11 -9
  152. package/src/client/jsonRpc/transformers.ts +2 -2
  153. package/src/client/knownScript.ts +27 -0
  154. package/src/jsonRpc/transports/webSocket.ts +17 -13
  155. package/src/num/index.ts +21 -1
  156. package/src/signer/ckb/signerCkbPublicKey.ts +3 -5
  157. package/src/signer/nostr/index.ts +1 -0
  158. package/src/signer/nostr/signerNostr.ts +3 -1
  159. package/src/signer/nostr/signerNostrPrivateKey.ts +7 -14
  160. package/src/signer/nostr/signerNostrPublicKeyReadonly.ts +29 -0
  161. package/src/utils/index.ts +14 -1
@@ -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(cell.outPoint), 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
  *
@@ -380,16 +460,31 @@ let CellInput = CellInput_1 = class CellInput extends index_js_5.mol.Entity.Base
380
460
  if (cellInput instanceof CellInput_1) {
381
461
  return cellInput;
382
462
  }
383
- return new CellInput_1(OutPoint.from(cellInput.previousOutput), 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));
463
+ return new CellInput_1(OutPoint.from("previousOutput" in cellInput
464
+ ? cellInput.previousOutput
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
+ });
384
477
  }
385
478
  /**
386
- * 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
387
482
  * The instance will be modified.
388
483
  *
389
484
  * @returns true if succeed.
390
485
  * @example
391
486
  * ```typescript
392
- * await cellInput.completeExtraInfos();
487
+ * await cellInput.completeExtraInfos(client);
393
488
  * ```
394
489
  */
395
490
  async completeExtraInfos(client) {
@@ -403,24 +498,35 @@ let CellInput = CellInput_1 = class CellInput extends index_js_5.mol.Entity.Base
403
498
  this.cellOutput = cell.cellOutput;
404
499
  this.outputData = cell.outputData;
405
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
+ }
509
+ clone() {
510
+ const cloned = super.clone();
511
+ cloned.cellOutput = this.cellOutput;
512
+ cloned.outputData = this.outputData;
513
+ return cloned;
514
+ }
406
515
  };
407
516
  exports.CellInput = CellInput;
408
517
  exports.CellInput = CellInput = CellInput_1 = __decorate([
409
- index_js_5.mol.codec(index_js_5.mol
518
+ index_js_6.mol.codec(index_js_6.mol
410
519
  .struct({
411
520
  since: Since,
412
521
  previousOutput: OutPoint,
413
522
  })
414
- .mapIn((encodable) => ({
415
- ...encodable,
416
- since: encodable.since ?? 0,
417
- })))
523
+ .mapIn((encodable) => CellInput.from(encodable)))
418
524
  ], CellInput);
419
- exports.CellInputVec = index_js_5.mol.vector(CellInput);
525
+ exports.CellInputVec = index_js_6.mol.vector(CellInput);
420
526
  /**
421
527
  * @public
422
528
  */
423
- 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() {
424
530
  /**
425
531
  * Creates an instance of CellDep.
426
532
  *
@@ -468,16 +574,16 @@ let CellDep = CellDep_1 = class CellDep extends index_js_5.mol.Entity.Base() {
468
574
  };
469
575
  exports.CellDep = CellDep;
470
576
  exports.CellDep = CellDep = CellDep_1 = __decorate([
471
- index_js_5.mol.codec(index_js_5.mol.struct({
577
+ index_js_6.mol.codec(index_js_6.mol.struct({
472
578
  outPoint: OutPoint,
473
579
  depType: exports.DepTypeCodec,
474
580
  }))
475
581
  ], CellDep);
476
- exports.CellDepVec = index_js_5.mol.vector(CellDep);
582
+ exports.CellDepVec = index_js_6.mol.vector(CellDep);
477
583
  /**
478
584
  * @public
479
585
  */
480
- 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() {
481
587
  /**
482
588
  * Creates an instance of WitnessArgs.
483
589
  *
@@ -510,36 +616,38 @@ let WitnessArgs = WitnessArgs_1 = class WitnessArgs extends index_js_5.mol.Entit
510
616
  if (witnessArgs instanceof WitnessArgs_1) {
511
617
  return witnessArgs;
512
618
  }
513
- 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));
514
620
  }
515
621
  };
516
622
  exports.WitnessArgs = WitnessArgs;
517
623
  exports.WitnessArgs = WitnessArgs = WitnessArgs_1 = __decorate([
518
- index_js_5.mol.codec(index_js_5.mol.table({
519
- lock: index_js_5.mol.BytesOpt,
520
- inputType: index_js_5.mol.BytesOpt,
521
- 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,
522
628
  }))
523
629
  ], WitnessArgs);
524
630
  /**
631
+ * Convert a bytes to a num.
632
+ *
525
633
  * @public
526
634
  */
527
635
  function udtBalanceFrom(dataLike) {
528
636
  const data = (0, index_js_1.bytesFrom)(dataLike).slice(0, 16);
529
- return (0, index_js_6.numFromBytes)(data);
637
+ return data.length === 0 ? index_js_3.Zero : (0, index_js_7.numFromBytes)(data);
530
638
  }
531
- exports.RawTransaction = index_js_5.mol.table({
532
- version: index_js_5.mol.Uint32,
639
+ exports.RawTransaction = index_js_6.mol.table({
640
+ version: index_js_6.mol.Uint32,
533
641
  cellDeps: exports.CellDepVec,
534
- headerDeps: index_js_5.mol.Byte32Vec,
642
+ headerDeps: index_js_6.mol.Byte32Vec,
535
643
  inputs: exports.CellInputVec,
536
644
  outputs: exports.CellOutputVec,
537
- outputsData: index_js_5.mol.BytesVec,
645
+ outputsData: index_js_6.mol.BytesVec,
538
646
  });
539
647
  /**
540
648
  * @public
541
649
  */
542
- 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() {
543
651
  /**
544
652
  * Creates an instance of Transaction.
545
653
  *
@@ -620,17 +728,17 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
620
728
  ...output,
621
729
  capacity: output.capacity ?? 0,
622
730
  });
623
- if (o.capacity === index_js_2.Zero) {
624
- o.capacity = (0, index_js_2.fixedPointFrom)(o.occupiedSize +
625
- ((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));
626
734
  }
627
735
  return o;
628
736
  }) ?? [];
629
- 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"));
630
738
  if (tx.outputsData != null && outputsData.length < tx.outputsData.length) {
631
- 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)));
632
740
  }
633
- 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) ?? []);
634
742
  }
635
743
  /**
636
744
  * Creates a Transaction instance from a Lumos skeleton.
@@ -666,10 +774,15 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
666
774
  witnesses: skeleton.witnesses.toArray(),
667
775
  });
668
776
  }
777
+ /**
778
+ * @deprecated
779
+ * Use ccc.stringify instead.
780
+ * stringify the tx to JSON string.
781
+ */
669
782
  stringify() {
670
783
  return JSON.stringify(this, (_, value) => {
671
784
  if (typeof value === "bigint") {
672
- return (0, index_js_6.numToHex)(value);
785
+ return (0, index_js_7.numToHex)(value);
673
786
  }
674
787
  // eslint-disable-next-line @typescript-eslint/no-unsafe-return
675
788
  return value;
@@ -700,7 +813,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
700
813
  * ```
701
814
  */
702
815
  hash() {
703
- return (0, index_js_3.hashCkb)(this.rawToBytes());
816
+ return (0, index_js_4.hashCkb)(this.rawToBytes());
704
817
  }
705
818
  /**
706
819
  * Calculates the hash of the transaction with witnesses.
@@ -713,7 +826,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
713
826
  * ```
714
827
  */
715
828
  hashFull() {
716
- return (0, index_js_3.hashCkb)(this.toBytes());
829
+ return (0, index_js_4.hashCkb)(this.toBytes());
717
830
  }
718
831
  /**
719
832
  * Hashes a witness and updates the hasher.
@@ -727,8 +840,8 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
727
840
  * ```
728
841
  */
729
842
  static hashWitnessToHasher(witness, hasher) {
730
- const raw = (0, index_js_1.bytesFrom)((0, index_js_4.hexFrom)(witness));
731
- 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));
732
845
  hasher.update(raw);
733
846
  }
734
847
  /**
@@ -748,18 +861,15 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
748
861
  * }
749
862
  * ```
750
863
  */
751
- async getSignHashInfo(scriptLike, client, hasher = new index_js_3.HasherCkb()) {
864
+ async getSignHashInfo(scriptLike, client, hasher = new index_js_4.HasherCkb()) {
752
865
  const script = script_js_1.Script.from(scriptLike);
753
866
  let position = -1;
754
867
  hasher.update(this.hash());
755
868
  for (let i = 0; i < this.witnesses.length; i += 1) {
756
869
  const input = this.inputs[i];
757
870
  if (input) {
758
- await input.completeExtraInfos(client);
759
- if (!input.cellOutput) {
760
- throw new Error("Unable to complete input");
761
- }
762
- if (!script.eq(input.cellOutput.lock)) {
871
+ const { cellOutput } = await input.getCell(client);
872
+ if (!script.eq(cellOutput.lock)) {
763
873
  continue;
764
874
  }
765
875
  if (position === -1) {
@@ -794,13 +904,9 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
794
904
  async findInputIndexByLockId(scriptIdLike, client) {
795
905
  const script = script_js_1.Script.from({ ...scriptIdLike, args: "0x" });
796
906
  for (let i = 0; i < this.inputs.length; i += 1) {
797
- const input = this.inputs[i];
798
- await input.completeExtraInfos(client);
799
- if (!input.cellOutput) {
800
- throw new Error("Unable to complete input");
801
- }
802
- if (script.codeHash === input.cellOutput.lock.codeHash &&
803
- 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) {
804
910
  return i;
805
911
  }
806
912
  }
@@ -820,12 +926,8 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
820
926
  async findInputIndexByLock(scriptLike, client) {
821
927
  const script = script_js_1.Script.from(scriptLike);
822
928
  for (let i = 0; i < this.inputs.length; i += 1) {
823
- const input = this.inputs[i];
824
- await input.completeExtraInfos(client);
825
- if (!input.cellOutput) {
826
- throw new Error("Unable to complete input");
827
- }
828
- if (script.eq(input.cellOutput.lock)) {
929
+ const { cellOutput } = await this.inputs[i].getCell(client);
930
+ if (script.eq(cellOutput.lock)) {
829
931
  return i;
830
932
  }
831
933
  }
@@ -845,12 +947,8 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
845
947
  async findLastInputIndexByLock(scriptLike, client) {
846
948
  const script = script_js_1.Script.from(scriptLike);
847
949
  for (let i = this.inputs.length - 1; i >= 0; i -= 1) {
848
- const input = this.inputs[i];
849
- await input.completeExtraInfos(client);
850
- if (!input.cellOutput) {
851
- throw new Error("Unable to complete input");
852
- }
853
- if (script.eq(input.cellOutput.lock)) {
950
+ const { cellOutput } = await this.inputs[i].getCell(client);
951
+ if (script.eq(cellOutput.lock)) {
854
952
  return i;
855
953
  }
856
954
  }
@@ -938,7 +1036,56 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
938
1036
  if (this.outputsData.length < index) {
939
1037
  this.outputsData.push(...Array.from(new Array(index - this.outputsData.length), () => "0x"));
940
1038
  }
941
- this.outputsData[index] = (0, index_js_4.hexFrom)(witness);
1039
+ this.outputsData[index] = (0, index_js_5.hexFrom)(witness);
1040
+ }
1041
+ /**
1042
+ * get input
1043
+ *
1044
+ * @param index - The cell input index
1045
+ *
1046
+ * @example
1047
+ * ```typescript
1048
+ * await tx.getInput(0);
1049
+ * ```
1050
+ */
1051
+ getInput(index) {
1052
+ return this.inputs[Number((0, index_js_7.numFrom)(index))];
1053
+ }
1054
+ /**
1055
+ * add input
1056
+ *
1057
+ * @param inputLike - The cell input.
1058
+ *
1059
+ * @example
1060
+ * ```typescript
1061
+ * await tx.addInput({ });
1062
+ * ```
1063
+ */
1064
+ addInput(inputLike) {
1065
+ if (this.witnesses.length > this.inputs.length) {
1066
+ this.witnesses.splice(this.inputs.length, 0, "0x");
1067
+ }
1068
+ return this.inputs.push(CellInput.from(inputLike));
1069
+ }
1070
+ /**
1071
+ * get output
1072
+ *
1073
+ * @param index - The cell output index
1074
+ *
1075
+ * @example
1076
+ * ```typescript
1077
+ * await tx.getOutput(0);
1078
+ * ```
1079
+ */
1080
+ getOutput(index) {
1081
+ const i = Number((0, index_js_7.numFrom)(index));
1082
+ if (i >= this.outputs.length) {
1083
+ return;
1084
+ }
1085
+ return {
1086
+ cellOutput: this.outputs[i],
1087
+ outputData: this.outputsData[i] ?? "0x",
1088
+ };
942
1089
  }
943
1090
  /**
944
1091
  * Add output
@@ -956,11 +1103,12 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
956
1103
  ...outputLike,
957
1104
  capacity: outputLike.capacity ?? 0,
958
1105
  });
959
- if (output.capacity === index_js_2.Zero) {
960
- 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);
961
1108
  }
962
- const i = this.outputs.push(output) - 1;
963
- this.setOutputDataAt(i, outputData);
1109
+ const len = this.outputs.push(output);
1110
+ this.setOutputDataAt(len - 1, outputData);
1111
+ return len;
964
1112
  }
965
1113
  /**
966
1114
  * Get witness at index as WitnessArgs
@@ -991,10 +1139,24 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
991
1139
  * ```
992
1140
  */
993
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) {
994
1156
  if (this.witnesses.length < index) {
995
1157
  this.witnesses.push(...Array.from(new Array(index - this.witnesses.length), () => "0x"));
996
1158
  }
997
- this.witnesses[index] = (0, index_js_4.hexFrom)(witness.toBytes());
1159
+ this.witnesses[index] = (0, index_js_5.hexFrom)(witness);
998
1160
  }
999
1161
  /**
1000
1162
  * Prepare dummy witness for sighash all method
@@ -1015,32 +1177,30 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1015
1177
  return;
1016
1178
  }
1017
1179
  const witness = this.getWitnessArgsAt(position) ?? WitnessArgs.from({});
1018
- 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));
1019
1181
  this.setWitnessArgsAt(position, witness);
1020
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
1021
1187
  async getInputsCapacity(client) {
1022
- return (0, index_js_7.reduceAsync)(this.inputs, async (acc, input) => {
1023
- await input.completeExtraInfos(client);
1024
- if (!input.cellOutput) {
1025
- throw new Error("Unable to complete input");
1026
- }
1027
- return acc + input.cellOutput.capacity;
1028
- }, (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)));
1029
1192
  }
1030
1193
  getOutputsCapacity() {
1031
- 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));
1032
1195
  }
1033
1196
  async getInputsUdtBalance(client, type) {
1034
- return (0, index_js_7.reduceAsync)(this.inputs, async (acc, input) => {
1035
- await input.completeExtraInfos(client);
1036
- if (!input.cellOutput || !input.outputData) {
1037
- throw new Error("Unable to complete input");
1038
- }
1039
- 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)) {
1040
1200
  return;
1041
1201
  }
1042
- return acc + udtBalanceFrom(input.outputData);
1043
- }, (0, index_js_6.numFrom)(0));
1202
+ return acc + udtBalanceFrom(outputData);
1203
+ }, (0, index_js_7.numFrom)(0));
1044
1204
  }
1045
1205
  getOutputsUdtBalance(type) {
1046
1206
  return this.outputs.reduce((acc, output, i) => {
@@ -1048,7 +1208,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1048
1208
  return acc;
1049
1209
  }
1050
1210
  return acc + udtBalanceFrom(this.outputsData[i]);
1051
- }, (0, index_js_6.numFrom)(0));
1211
+ }, (0, index_js_7.numFrom)(0));
1052
1212
  }
1053
1213
  async completeInputs(from, filter, accumulator, init) {
1054
1214
  const collectedCells = [];
@@ -1066,11 +1226,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1066
1226
  }
1067
1227
  acc = next;
1068
1228
  }
1069
- this.inputs.push(...collectedCells.map(({ outPoint, outputData, cellOutput }) => CellInput.from({
1070
- previousOutput: outPoint,
1071
- outputData,
1072
- cellOutput,
1073
- })));
1229
+ collectedCells.forEach((cell) => this.addInput(cell));
1074
1230
  if (fulfilled) {
1075
1231
  return {
1076
1232
  addedCount: collectedCells.length,
@@ -1082,7 +1238,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1082
1238
  };
1083
1239
  }
1084
1240
  async completeInputsByCapacity(from, capacityTweak, filter) {
1085
- const exceptedCapacity = this.getOutputsCapacity() + (0, index_js_6.numFrom)(capacityTweak ?? 0);
1241
+ const exceptedCapacity = this.getOutputsCapacity() + (0, index_js_7.numFrom)(capacityTweak ?? 0);
1086
1242
  const inputsCapacity = await this.getInputsCapacity(from.client);
1087
1243
  if (inputsCapacity >= exceptedCapacity) {
1088
1244
  return 0;
@@ -1097,24 +1253,24 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1097
1253
  if (accumulated === undefined) {
1098
1254
  return addedCount;
1099
1255
  }
1100
- 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`);
1101
1257
  }
1102
1258
  async completeInputsAll(from, filter) {
1103
1259
  const { addedCount } = await this.completeInputs(from, filter ?? {
1104
1260
  scriptLenRange: [0, 1],
1105
1261
  outputDataLenRange: [0, 1],
1106
- }, (acc, { cellOutput: { capacity } }) => acc + capacity, index_js_2.Zero);
1262
+ }, (acc, { cellOutput: { capacity } }) => acc + capacity, index_js_3.Zero);
1107
1263
  return addedCount;
1108
1264
  }
1109
1265
  async completeInputsByUdt(from, type, balanceTweak) {
1110
- 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);
1111
1267
  const inputsBalance = await this.getInputsUdtBalance(from.client, type);
1112
1268
  if (inputsBalance >= exceptedBalance) {
1113
1269
  return 0;
1114
1270
  }
1115
1271
  const { addedCount, accumulated } = await this.completeInputs(from, {
1116
1272
  script: type,
1117
- outputDataLenRange: [16, (0, index_js_6.numFrom)("0xffffffff")],
1273
+ outputDataLenRange: [16, (0, index_js_7.numFrom)("0xffffffff")],
1118
1274
  }, (acc, { outputData }) => {
1119
1275
  const balance = udtBalanceFrom(outputData);
1120
1276
  const sum = acc + balance;
@@ -1126,19 +1282,12 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1126
1282
  throw new Error(`Insufficient coin, need ${exceptedBalance - accumulated} extra coin`);
1127
1283
  }
1128
1284
  async completeInputsAddOne(from, filter) {
1129
- for await (const cell of from.findCells(filter ?? {
1285
+ const { addedCount, accumulated } = await this.completeInputs(from, filter ?? {
1130
1286
  scriptLenRange: [0, 1],
1131
1287
  outputDataLenRange: [0, 1],
1132
- }, true, undefined, 1)) {
1133
- if (this.inputs.some(({ previousOutput }) => previousOutput.eq(cell.outPoint))) {
1134
- continue;
1135
- }
1136
- this.inputs.push(CellInput.from({
1137
- previousOutput: cell.outPoint,
1138
- outputData: cell.outputData,
1139
- cellOutput: cell.cellOutput,
1140
- }));
1141
- return 1;
1288
+ }, () => undefined, true);
1289
+ if (accumulated === undefined) {
1290
+ return addedCount;
1142
1291
  }
1143
1292
  throw new Error(`Insufficient CKB, need at least one new cell`);
1144
1293
  }
@@ -1148,16 +1297,25 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1148
1297
  }
1149
1298
  return this.completeInputsAddOne(from, filter);
1150
1299
  }
1300
+ async getFee(client) {
1301
+ return (await this.getInputsCapacity(client)) - this.getOutputsCapacity();
1302
+ }
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));
1306
+ }
1151
1307
  estimateFee(feeRate) {
1152
1308
  const txSize = this.toBytes().length + 4;
1153
- return ((0, index_js_6.numFrom)(txSize) * (0, index_js_6.numFrom)(feeRate) + (0, index_js_6.numFrom)(1000)) / (0, index_js_6.numFrom)(1000);
1309
+ // + 999 then / 1000 to ceil the calculated fee
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);
1154
1311
  }
1155
- async completeFee(from, change, expectedFeeRate, filter) {
1156
- const feeRate = expectedFeeRate ?? (await from.client.getFeeRate());
1312
+ async completeFee(from, change, expectedFeeRate, filter, options) {
1313
+ const feeRate = expectedFeeRate ??
1314
+ (await from.client.getFeeRate(options?.feeRateBlockRange, options));
1157
1315
  // Complete all inputs extra infos for cache
1158
1316
  await this.getInputsCapacity(from.client);
1159
- let leastFee = index_js_2.Zero;
1160
- let leastExtraCapacity = index_js_2.Zero;
1317
+ let leastFee = index_js_3.Zero;
1318
+ let leastExtraCapacity = index_js_3.Zero;
1161
1319
  while (true) {
1162
1320
  const tx = this.clone();
1163
1321
  const collected = await (async () => {
@@ -1165,31 +1323,30 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1165
1323
  return await tx.completeInputsByCapacity(from, leastFee + leastExtraCapacity, filter);
1166
1324
  }
1167
1325
  catch (err) {
1168
- if (leastExtraCapacity !== index_js_2.Zero) {
1326
+ if (leastExtraCapacity !== index_js_3.Zero) {
1169
1327
  throw new Error("Not enough capacity for the change cell");
1170
1328
  }
1171
1329
  throw err;
1172
1330
  }
1173
1331
  })();
1174
1332
  await from.prepareTransaction(tx);
1175
- if (leastFee === index_js_2.Zero) {
1333
+ if (leastFee === index_js_3.Zero) {
1176
1334
  // The initial fee is calculated based on prepared transaction
1177
1335
  leastFee = tx.estimateFee(feeRate);
1178
1336
  }
1179
- const extraCapacity = (await tx.getInputsCapacity(from.client)) - tx.getOutputsCapacity();
1337
+ const fee = await tx.getFee(from.client);
1180
1338
  // The extra capacity paid the fee without a change
1181
- if (extraCapacity === leastFee) {
1339
+ if (fee === leastFee) {
1182
1340
  this.copy(tx);
1183
1341
  return [collected, false];
1184
1342
  }
1185
- const needed = (0, index_js_6.numFrom)(await Promise.resolve(change(tx, extraCapacity - leastFee)));
1343
+ const needed = (0, index_js_7.numFrom)(await Promise.resolve(change(tx, fee - leastFee)));
1186
1344
  // No enough extra capacity to create new cells for change
1187
- if (needed > index_js_2.Zero) {
1345
+ if (needed > index_js_3.Zero) {
1188
1346
  leastExtraCapacity = needed;
1189
1347
  continue;
1190
1348
  }
1191
- if ((await tx.getInputsCapacity(from.client)) - tx.getOutputsCapacity() !==
1192
- leastFee) {
1349
+ if ((await tx.getFee(from.client)) !== leastFee) {
1193
1350
  throw new Error("The change function doesn't use all available capacity");
1194
1351
  }
1195
1352
  // New change cells created, update the fee
@@ -1211,7 +1368,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1211
1368
  const script = script_js_1.Script.from(change);
1212
1369
  return this.completeFee(from, (tx, capacity) => {
1213
1370
  const changeCell = CellOutput.from({ capacity: 0, lock: script });
1214
- const occupiedCapacity = (0, index_js_2.fixedPointFrom)(changeCell.occupiedSize);
1371
+ const occupiedCapacity = (0, index_js_3.fixedPointFrom)(changeCell.occupiedSize);
1215
1372
  if (capacity < occupiedCapacity) {
1216
1373
  return occupiedCapacity;
1217
1374
  }
@@ -1225,7 +1382,7 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1225
1382
  return this.completeFeeChangeToLock(from, script, feeRate, filter);
1226
1383
  }
1227
1384
  completeFeeChangeToOutput(from, index, feeRate, filter) {
1228
- const change = Number((0, index_js_6.numFrom)(index));
1385
+ const change = Number((0, index_js_7.numFrom)(index));
1229
1386
  if (!this.outputs[change]) {
1230
1387
  throw new Error("Non-existed output to change");
1231
1388
  }
@@ -1237,10 +1394,10 @@ let Transaction = Transaction_1 = class Transaction extends index_js_5.mol.Entit
1237
1394
  };
1238
1395
  exports.Transaction = Transaction;
1239
1396
  exports.Transaction = Transaction = Transaction_1 = __decorate([
1240
- index_js_5.mol.codec(index_js_5.mol
1397
+ index_js_6.mol.codec(index_js_6.mol
1241
1398
  .table({
1242
1399
  raw: exports.RawTransaction,
1243
- witnesses: index_js_5.mol.BytesVec,
1400
+ witnesses: index_js_6.mol.BytesVec,
1244
1401
  })
1245
1402
  .mapIn((txLike) => {
1246
1403
  const tx = Transaction.from(txLike);
@@ -1251,3 +1408,38 @@ exports.Transaction = Transaction = Transaction_1 = __decorate([
1251
1408
  })
1252
1409
  .mapOut((tx) => Transaction.from({ ...tx.raw, witnesses: tx.witnesses })))
1253
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
+ }