@ickb/sdk 1000.0.82 → 9001.0.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 (157) hide show
  1. package/README.md +84 -32
  2. package/dist/constants.d.ts +9 -22
  3. package/dist/constants.d.ts.map +1 -1
  4. package/dist/constants.js +62 -76
  5. package/dist/constants.js.map +1 -1
  6. package/dist/conversion/estimate.d.ts +33 -0
  7. package/dist/conversion/estimate.d.ts.map +1 -0
  8. package/dist/conversion/estimate.js +117 -0
  9. package/dist/conversion/estimate.js.map +1 -0
  10. package/dist/conversion/maturity.d.ts +15 -0
  11. package/dist/conversion/maturity.d.ts.map +1 -0
  12. package/dist/conversion/maturity.js +113 -0
  13. package/dist/conversion/maturity.js.map +1 -0
  14. package/dist/conversion/plans.d.ts +20 -0
  15. package/dist/conversion/plans.d.ts.map +1 -0
  16. package/dist/conversion/plans.js +106 -0
  17. package/dist/conversion/plans.js.map +1 -0
  18. package/dist/conversion/projection.d.ts +21 -0
  19. package/dist/conversion/projection.d.ts.map +1 -0
  20. package/dist/conversion/projection.js +94 -0
  21. package/dist/conversion/projection.js.map +1 -0
  22. package/dist/conversion/types.d.ts +264 -0
  23. package/dist/conversion/types.d.ts.map +1 -0
  24. package/dist/conversion/types.js +2 -0
  25. package/dist/conversion/types.js.map +1 -0
  26. package/dist/conversion/withdrawal_ring.d.ts +34 -0
  27. package/dist/conversion/withdrawal_ring.d.ts.map +1 -0
  28. package/dist/conversion/withdrawal_ring.js +87 -0
  29. package/dist/conversion/withdrawal_ring.js.map +1 -0
  30. package/dist/dao.d.ts +100 -0
  31. package/dist/dao.d.ts.map +1 -0
  32. package/dist/dao.js +125 -0
  33. package/dist/dao.js.map +1 -0
  34. package/dist/index.d.ts +49 -3
  35. package/dist/index.d.ts.map +1 -1
  36. package/dist/index.js +20 -3
  37. package/dist/index.js.map +1 -1
  38. package/dist/logic.d.ts +81 -0
  39. package/dist/logic.d.ts.map +1 -0
  40. package/dist/logic.js +173 -0
  41. package/dist/logic.js.map +1 -0
  42. package/dist/order/cells.d.ts +168 -0
  43. package/dist/order/cells.d.ts.map +1 -0
  44. package/dist/order/cells.js +309 -0
  45. package/dist/order/cells.js.map +1 -0
  46. package/dist/order/conversion.d.ts +37 -0
  47. package/dist/order/conversion.d.ts.map +1 -0
  48. package/dist/order/conversion.js +120 -0
  49. package/dist/order/conversion.js.map +1 -0
  50. package/dist/order/fee.d.ts +9 -0
  51. package/dist/order/fee.d.ts.map +1 -0
  52. package/dist/order/fee.js +24 -0
  53. package/dist/order/fee.js.map +1 -0
  54. package/dist/order/fill.d.ts +36 -0
  55. package/dist/order/fill.d.ts.map +1 -0
  56. package/dist/order/fill.js +53 -0
  57. package/dist/order/fill.js.map +1 -0
  58. package/dist/order/info.d.ts +62 -0
  59. package/dist/order/info.d.ts.map +1 -0
  60. package/dist/order/info.js +102 -0
  61. package/dist/order/info.js.map +1 -0
  62. package/dist/order/master.d.ts +49 -0
  63. package/dist/order/master.d.ts.map +1 -0
  64. package/dist/order/master.js +27 -0
  65. package/dist/order/master.js.map +1 -0
  66. package/dist/order/matcher.d.ts +43 -0
  67. package/dist/order/matcher.d.ts.map +1 -0
  68. package/dist/order/matcher.js +124 -0
  69. package/dist/order/matcher.js.map +1 -0
  70. package/dist/order/order.d.ts +63 -0
  71. package/dist/order/order.d.ts.map +1 -0
  72. package/dist/order/order.js +234 -0
  73. package/dist/order/order.js.map +1 -0
  74. package/dist/order/order_data.d.ts +51 -0
  75. package/dist/order/order_data.d.ts.map +1 -0
  76. package/dist/order/order_data.js +67 -0
  77. package/dist/order/order_data.js.map +1 -0
  78. package/dist/order/ratio.d.ts +40 -0
  79. package/dist/order/ratio.d.ts.map +1 -0
  80. package/dist/order/ratio.js +74 -0
  81. package/dist/order/ratio.js.map +1 -0
  82. package/dist/order/relative.d.ts +43 -0
  83. package/dist/order/relative.d.ts.map +1 -0
  84. package/dist/order/relative.js +57 -0
  85. package/dist/order/relative.js.map +1 -0
  86. package/dist/owned_owner.d.ts +108 -0
  87. package/dist/owned_owner.d.ts.map +1 -0
  88. package/dist/owned_owner.js +260 -0
  89. package/dist/owned_owner.js.map +1 -0
  90. package/dist/sdk.d.ts +113 -41
  91. package/dist/sdk.d.ts.map +1 -1
  92. package/dist/sdk.js +410 -209
  93. package/dist/sdk.js.map +1 -1
  94. package/dist/send/sign_and_send_transaction.d.ts +61 -0
  95. package/dist/send/sign_and_send_transaction.d.ts.map +1 -0
  96. package/dist/send/sign_and_send_transaction.js +201 -0
  97. package/dist/send/sign_and_send_transaction.js.map +1 -0
  98. package/dist/send/wait_transaction.d.ts +42 -0
  99. package/dist/send/wait_transaction.d.ts.map +1 -0
  100. package/dist/send/wait_transaction.js +172 -0
  101. package/dist/send/wait_transaction.js.map +1 -0
  102. package/dist/udt.d.ts +108 -0
  103. package/dist/udt.d.ts.map +1 -0
  104. package/dist/udt.js +180 -0
  105. package/dist/udt.js.map +1 -0
  106. package/dist/utils/chain.d.ts +3 -0
  107. package/dist/utils/chain.d.ts.map +1 -0
  108. package/dist/utils/chain.js +2 -0
  109. package/dist/utils/chain.js.map +1 -0
  110. package/dist/utils/codec.d.ts +19 -0
  111. package/dist/utils/codec.d.ts.map +1 -0
  112. package/dist/utils/codec.js +69 -0
  113. package/dist/utils/codec.js.map +1 -0
  114. package/dist/utils/utils.d.ts +99 -0
  115. package/dist/utils/utils.d.ts.map +1 -0
  116. package/dist/utils/utils.js +130 -0
  117. package/dist/utils/utils.js.map +1 -0
  118. package/dist/utils/verified_client.d.ts +19 -0
  119. package/dist/utils/verified_client.d.ts.map +1 -0
  120. package/dist/utils/verified_client.js +92 -0
  121. package/dist/utils/verified_client.js.map +1 -0
  122. package/package.json +19 -26
  123. package/src/constants.ts +65 -181
  124. package/src/conversion/estimate.ts +155 -0
  125. package/src/conversion/maturity.ts +158 -0
  126. package/src/conversion/plans.ts +144 -0
  127. package/src/conversion/projection.ts +136 -0
  128. package/src/conversion/types.ts +302 -0
  129. package/src/conversion/withdrawal_ring.ts +122 -0
  130. package/src/dao.ts +196 -0
  131. package/src/index.ts +89 -3
  132. package/src/logic.ts +283 -0
  133. package/src/order/cells.ts +383 -0
  134. package/src/order/conversion.ts +162 -0
  135. package/src/order/fee.ts +30 -0
  136. package/src/order/fill.ts +83 -0
  137. package/src/order/info.ts +181 -0
  138. package/src/order/master.ts +42 -0
  139. package/src/order/matcher.ts +191 -0
  140. package/src/order/order.ts +288 -0
  141. package/src/order/order_data.ts +131 -0
  142. package/src/order/ratio.ts +127 -0
  143. package/src/order/relative.ts +109 -0
  144. package/src/owned_owner.ts +364 -0
  145. package/src/sdk.ts +542 -440
  146. package/src/send/sign_and_send_transaction.ts +256 -0
  147. package/src/send/wait_transaction.ts +227 -0
  148. package/src/udt.ts +249 -0
  149. package/src/utils/chain.ts +2 -0
  150. package/src/utils/codec.ts +78 -0
  151. package/src/utils/utils.ts +208 -0
  152. package/src/utils/verified_client.ts +136 -0
  153. package/dist/codec.d.ts +0 -3
  154. package/dist/codec.d.ts.map +0 -1
  155. package/dist/codec.js +0 -58
  156. package/dist/codec.js.map +0 -1
  157. package/src/codec.ts +0 -138
package/dist/udt.d.ts ADDED
@@ -0,0 +1,108 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import type { ExchangeRatio } from "./utils/utils.js";
3
+ export declare const receiptDataBytes = 12;
4
+ /** The receipt payload for one or more identical iCKB deposits. */
5
+ export interface ReceiptData {
6
+ /** Number of identical deposits represented by this receipt. */
7
+ depositQuantity: ccc.Num;
8
+ /** Free CKB capacity of each represented deposit before iCKB conversion. */
9
+ depositAmount: ccc.FixedPoint;
10
+ }
11
+ /** Encodes a receipt payload. */
12
+ export declare function encodeReceiptData(data: ReceiptData): ccc.Bytes;
13
+ /**
14
+ * Decodes the fixed receipt-data prefix and ignores trailing cell payload bytes.
15
+ *
16
+ * @remarks The receipt data prefix is 12 bytes after the `0x` marker: 4 bytes for deposit
17
+ * quantity and 8 bytes for deposit amount. Later payload bytes belong to other protocol
18
+ * data and are intentionally tolerated here.
19
+ */
20
+ export declare function decodeReceiptData(outputData: ccc.Hex): ReceiptData;
21
+ /**
22
+ * Soft per-deposit iCKB value cap used before applying the excess discount.
23
+ */
24
+ export declare const ICKB_DEPOSIT_CAP: bigint;
25
+ /**
26
+ * The iCKB xUDT token: its scripts, code cells, and the iCKB accounting of a
27
+ * transaction's inputs. Inputs carry iCKB as xUDT balances, as receipt value,
28
+ * and negatively as the first-phase deposits a transaction re-mints.
29
+ */
30
+ export declare class IckbUdt {
31
+ /** The iCKB xUDT type script. */
32
+ readonly script: ccc.Script;
33
+ /** The iCKB dep group, which also carries the xUDT and iCKB Logic code cells. */
34
+ readonly cellDeps: ccc.CellDep[];
35
+ /** Logic script whose hash is embedded in this iCKB xUDT type script. */
36
+ readonly logicScript: ccc.Script;
37
+ /** The Nervos DAO script, to recognize the first-phase deposits a transaction re-mints. */
38
+ readonly daoScript: ccc.Script;
39
+ /** Creates an instance of IckbUdt from its code and script references. */
40
+ constructor({ script, cellDeps, logicScript, daoScript, }: {
41
+ script: ccc.ScriptLike;
42
+ cellDeps: ccc.CellDepLike[];
43
+ logicScript: ccc.ScriptLike;
44
+ daoScript: ccc.ScriptLike;
45
+ });
46
+ /**
47
+ * Computes the iCKB UDT type script from raw UDT and Logic scripts.
48
+ *
49
+ * Concatenates the iCKB logic script hash with the fixed 4-byte little-endian
50
+ * xUDT owner-mode flags postfix ("00000080") to form the UDT type script args.
51
+ *
52
+ * @param udtScript - The raw xUDT script (codeHash and hashType reused).
53
+ * @param ickbLogic - The iCKB logic script (hash used for args).
54
+ * @returns A new Script with the computed args.
55
+ */
56
+ static typeScriptFrom(udtScript: ccc.Script, ickbLogic: ccc.Script): ccc.Script;
57
+ /**
58
+ * Minimum capacity for an iCKB xUDT cell locked by the supplied lock script.
59
+ */
60
+ static minimumXudtCellCapacity(lock: ccc.Script): ccc.FixedPoint;
61
+ /**
62
+ * Returns true when a cell carries this iCKB xUDT type and enough UDT data.
63
+ */
64
+ isUdt(cellLike: ccc.CellAnyLike): boolean;
65
+ /**
66
+ * iCKB carried by the transaction's inputs: xUDT balances plus receipt value, minus the
67
+ * first-phase deposits it re-mints. Receipts and deposits take the value the state read
68
+ * gave them at their deposit headers; a protocol input missing from `protocolCells` is
69
+ * a builder bug and throws, since iCKB Logic would refuse whatever it was valued at.
70
+ */
71
+ inputBalance(tx: ccc.Transaction, client: ccc.Client, protocolCells: readonly ProtocolCell[]): Promise<ccc.Num>;
72
+ /** 1 for a receipt, -1 for a first-phase iCKB deposit, 0 for any other cell. */
73
+ private protocolSign;
74
+ /** Adds the iCKB change output when the surplus is positive; zero needs no output. */
75
+ addChange(tx: ccc.Transaction, lock: ccc.ScriptLike, balance: ccc.Num): void;
76
+ }
77
+ /**
78
+ * Calculates iCKB value for unoccupied CKB capacity at a deposit header.
79
+ *
80
+ * @remarks
81
+ * Values above {@link ICKB_DEPOSIT_CAP} receive a 10% discount only on the
82
+ * excess amount.
83
+ */
84
+ export declare function ickbValue(ckbUnoccupiedCapacity: ccc.FixedPoint, header: ccc.ClientBlockHeader): ccc.FixedPoint;
85
+ /** The iCKB worth `amount` CKB at `ratio`, rounded down. */
86
+ export declare function udtFromCkb(amount: ccc.FixedPoint, ratio: ExchangeRatio): ccc.FixedPoint;
87
+ /** The CKB worth `amount` iCKB at `ratio`, rounded down. */
88
+ export declare function ckbFromUdt(amount: ccc.FixedPoint, ratio: ExchangeRatio): ccc.FixedPoint;
89
+ /**
90
+ * Calculates the free-capacity accounting ratio at a block header.
91
+ *
92
+ * @param header - The block header whose DAO accumulated rate is used.
93
+ * @returns An object containing the CKB and UDT scales.
94
+ */
95
+ export declare function ickbAccountingRatio(header: ccc.ClientBlockHeader): ExchangeRatio;
96
+ /**
97
+ * Calculates the gross iCKB exchange ratio for a recoverable standard deposit.
98
+ *
99
+ * @remarks Includes the standard deposit's 82 CKB occupied capacity spread
100
+ * over the 100,000 iCKB reference amount.
101
+ */
102
+ export declare function ickbExchangeRatio(header: ccc.ClientBlockHeader): ExchangeRatio;
103
+ /** A receipt or iCKB deposit with the iCKB value the state read gave it. */
104
+ export interface ProtocolCell {
105
+ cell: ccc.Cell;
106
+ udtValue: ccc.Num;
107
+ }
108
+ //# sourceMappingURL=udt.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"udt.d.ts","sourceRoot":"","sources":["../src/udt.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAO,MAAM,eAAe,CAAC;AAGzC,OAAO,KAAK,EAAE,aAAa,EAAE,MAAM,kBAAkB,CAAC;AAUtD,eAAO,MAAM,gBAAgB,KAAK,CAAC;AAEnC,mEAAmE;AACnE,MAAM,WAAW,WAAW;IAC1B,gEAAgE;IAChE,eAAe,EAAE,GAAG,CAAC,GAAG,CAAC;IACzB,4EAA4E;IAC5E,aAAa,EAAE,GAAG,CAAC,UAAU,CAAC;CAC/B;AAOD,iCAAiC;AACjC,wBAAgB,iBAAiB,CAAC,IAAI,EAAE,WAAW,GAAG,GAAG,CAAC,KAAK,CAE9D;AAED;;;;;;GAMG;AACH,wBAAgB,iBAAiB,CAAC,UAAU,EAAE,GAAG,CAAC,GAAG,GAAG,WAAW,CAElE;AAED;;GAEG;AACH,eAAO,MAAM,gBAAgB,QAA6B,CAAC;AAE3D;;;;GAIG;AACH,qBAAa,OAAO;IAClB,iCAAiC;IACjC,SAAgB,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC;IAEnC,iFAAiF;IACjF,SAAgB,QAAQ,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC;IAExC,yEAAyE;IACzE,SAAgB,WAAW,EAAE,GAAG,CAAC,MAAM,CAAC;IAExC,2FAA2F;IAC3F,SAAgB,SAAS,EAAE,GAAG,CAAC,MAAM,CAAC;IAEtC,0EAA0E;IAC1E,YAAY,EACV,MAAM,EACN,QAAQ,EACR,WAAW,EACX,SAAS,GACV,EAAE;QACD,MAAM,EAAE,GAAG,CAAC,UAAU,CAAC;QACvB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U,CAGvF;AAQD;;;;;GAKG;AACH,wBAAgB,mBAAmB,CAAC,MAAM,EAAE,GAAG,CAAC,iBAAiB,GAAG,aAAa,CAKhF;AAED;;;;;GAKG;AACH,wBAAgB,iBAAiB,CAAC,MAAM,EAAE,GAAG,CAAC,iBAAiB,GAAG,aAAa,CAK9E;AAED,4EAA4E;AAC5E,MAAM,WAAW,YAAY;IAC3B,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC;IACf,QAAQ,EAAE,GAAG,CAAC,GAAG,CAAC;CACnB"}
package/dist/udt.js ADDED
@@ -0,0 +1,180 @@
1
+ import { ccc, mol } from "@ckb-ccc/core";
2
+ import { isDaoDeposit } from "./dao.js";
3
+ import { CheckedUint128LE, CheckedUint32LE, CheckedUint64LE } from "./utils/codec.js";
4
+ const ickbXudtTypeOccupiedSize = 69;
5
+ const udtDataSize = 16;
6
+ const AR_0 = 10000000000000000n; // Base scale for CKB
7
+ const depositUsedCapacity = ccc.fixedPointFrom(82); // 82n CKB
8
+ const depositCapacityDelta = (depositUsedCapacity * AR_0) / ccc.fixedPointFrom(100000);
9
+ const xudtOwnerMode = 0x80000000n;
10
+ // A receipt's fixed prefix: 4 bytes of deposit quantity and 8 of deposit amount.
11
+ export const receiptDataBytes = 12;
12
+ const ReceiptDataCodec = mol.struct({
13
+ depositQuantity: CheckedUint32LE,
14
+ depositAmount: CheckedUint64LE,
15
+ });
16
+ /** Encodes a receipt payload. */
17
+ export function encodeReceiptData(data) {
18
+ return ReceiptDataCodec.encode(data);
19
+ }
20
+ /**
21
+ * Decodes the fixed receipt-data prefix and ignores trailing cell payload bytes.
22
+ *
23
+ * @remarks The receipt data prefix is 12 bytes after the `0x` marker: 4 bytes for deposit
24
+ * quantity and 8 bytes for deposit amount. Later payload bytes belong to other protocol
25
+ * data and are intentionally tolerated here.
26
+ */
27
+ export function decodeReceiptData(outputData) {
28
+ return ReceiptDataCodec.decode(outputData.slice(0, 2 + 2 * receiptDataBytes));
29
+ }
30
+ /**
31
+ * Soft per-deposit iCKB value cap used before applying the excess discount.
32
+ */
33
+ export const ICKB_DEPOSIT_CAP = ccc.fixedPointFrom(100000); // 100,000 iCKB
34
+ /**
35
+ * The iCKB xUDT token: its scripts, code cells, and the iCKB accounting of a
36
+ * transaction's inputs. Inputs carry iCKB as xUDT balances, as receipt value,
37
+ * and negatively as the first-phase deposits a transaction re-mints.
38
+ */
39
+ export class IckbUdt {
40
+ /** The iCKB xUDT type script. */
41
+ script;
42
+ /** The iCKB dep group, which also carries the xUDT and iCKB Logic code cells. */
43
+ cellDeps;
44
+ /** Logic script whose hash is embedded in this iCKB xUDT type script. */
45
+ logicScript;
46
+ /** The Nervos DAO script, to recognize the first-phase deposits a transaction re-mints. */
47
+ daoScript;
48
+ /** Creates an instance of IckbUdt from its code and script references. */
49
+ constructor({ script, cellDeps, logicScript, daoScript, }) {
50
+ this.script = ccc.Script.from(script);
51
+ this.cellDeps = cellDeps.map((cellDep) => ccc.CellDep.from(cellDep));
52
+ this.logicScript = ccc.Script.from(logicScript);
53
+ this.daoScript = ccc.Script.from(daoScript);
54
+ }
55
+ /**
56
+ * Computes the iCKB UDT type script from raw UDT and Logic scripts.
57
+ *
58
+ * Concatenates the iCKB logic script hash with the fixed 4-byte little-endian
59
+ * xUDT owner-mode flags postfix ("00000080") to form the UDT type script args.
60
+ *
61
+ * @param udtScript - The raw xUDT script (codeHash and hashType reused).
62
+ * @param ickbLogic - The iCKB logic script (hash used for args).
63
+ * @returns A new Script with the computed args.
64
+ */
65
+ static typeScriptFrom(udtScript, ickbLogic) {
66
+ const { codeHash, hashType } = udtScript;
67
+ return new ccc.Script(codeHash, hashType, ccc.hexFrom(ccc.bytesConcat(ickbLogic.hash(), CheckedUint32LE.encode(xudtOwnerMode))));
68
+ }
69
+ /**
70
+ * Minimum capacity for an iCKB xUDT cell locked by the supplied lock script.
71
+ */
72
+ static minimumXudtCellCapacity(lock) {
73
+ return (BigInt(8 + lock.occupiedSize + ickbXudtTypeOccupiedSize + udtDataSize) * ccc.One);
74
+ }
75
+ /**
76
+ * Returns true when a cell carries this iCKB xUDT type and enough UDT data.
77
+ */
78
+ isUdt(cellLike) {
79
+ const cell = ccc.CellAny.from(cellLike);
80
+ return (cell.cellOutput.type?.eq(this.script) === true &&
81
+ ccc.bytesFrom(cell.outputData).length >= udtDataSize);
82
+ }
83
+ /**
84
+ * iCKB carried by the transaction's inputs: xUDT balances plus receipt value, minus the
85
+ * first-phase deposits it re-mints. Receipts and deposits take the value the state read
86
+ * gave them at their deposit headers; a protocol input missing from `protocolCells` is
87
+ * a builder bug and throws, since iCKB Logic would refuse whatever it was valued at.
88
+ */
89
+ async inputBalance(tx, client, protocolCells) {
90
+ const values = new Map(protocolCells.map(({ cell, udtValue }) => [cell.outPoint.toHex(), udtValue]));
91
+ let balance = ccc.Zero;
92
+ for (const input of tx.inputs) {
93
+ const cell = await input.getCell(client);
94
+ if (this.isUdt(cell)) {
95
+ balance += ccc.udtBalanceFrom(cell.outputData);
96
+ continue;
97
+ }
98
+ const sign = this.protocolSign(cell);
99
+ if (sign === 0n) {
100
+ continue;
101
+ }
102
+ const value = values.get(cell.outPoint.toHex());
103
+ if (value === undefined) {
104
+ throw new Error(`iCKB input ${cell.outPoint.toHex()} has no value from the state read`);
105
+ }
106
+ balance += sign * value;
107
+ }
108
+ return balance;
109
+ }
110
+ /** 1 for a receipt, -1 for a first-phase iCKB deposit, 0 for any other cell. */
111
+ protocolSign(cell) {
112
+ const { type, lock } = cell.cellOutput;
113
+ if (type !== undefined && this.logicScript.eq(type)) {
114
+ return 1n;
115
+ }
116
+ return this.logicScript.eq(lock) && isDaoDeposit(cell, this.daoScript) ? -1n : 0n;
117
+ }
118
+ /** Adds the iCKB change output when the surplus is positive; zero needs no output. */
119
+ addChange(tx, lock, balance) {
120
+ if (balance <= ccc.Zero) {
121
+ return;
122
+ }
123
+ tx.addOutput({ lock, type: this.script }, CheckedUint128LE.encode(balance));
124
+ }
125
+ }
126
+ /**
127
+ * Calculates iCKB value for unoccupied CKB capacity at a deposit header.
128
+ *
129
+ * @remarks
130
+ * Values above {@link ICKB_DEPOSIT_CAP} receive a 10% discount only on the
131
+ * excess amount.
132
+ */
133
+ export function ickbValue(ckbUnoccupiedCapacity, header) {
134
+ let ickbAmount = udtFromCkb(ckbUnoccupiedCapacity, ickbAccountingRatio(header));
135
+ if (ICKB_DEPOSIT_CAP < ickbAmount) {
136
+ // Apply a 10% discount for the amount exceeding the soft iCKB cap per deposit.
137
+ ickbAmount -= (ickbAmount - ICKB_DEPOSIT_CAP) / 10n;
138
+ }
139
+ return ickbAmount;
140
+ }
141
+ /** The iCKB worth `amount` CKB at `ratio`, rounded down. */
142
+ export function udtFromCkb(amount, ratio) {
143
+ assertPositive(ratio);
144
+ return (amount * ratio.ckbScale) / ratio.udtScale;
145
+ }
146
+ /** The CKB worth `amount` iCKB at `ratio`, rounded down. */
147
+ export function ckbFromUdt(amount, ratio) {
148
+ assertPositive(ratio);
149
+ return (amount * ratio.udtScale) / ratio.ckbScale;
150
+ }
151
+ function assertPositive({ ckbScale, udtScale }) {
152
+ if (ckbScale <= 0n || udtScale <= 0n) {
153
+ throw new Error("Exchange ratio scales must be positive");
154
+ }
155
+ }
156
+ /**
157
+ * Calculates the free-capacity accounting ratio at a block header.
158
+ *
159
+ * @param header - The block header whose DAO accumulated rate is used.
160
+ * @returns An object containing the CKB and UDT scales.
161
+ */
162
+ export function ickbAccountingRatio(header) {
163
+ return {
164
+ ckbScale: AR_0,
165
+ udtScale: header.dao.ar,
166
+ };
167
+ }
168
+ /**
169
+ * Calculates the gross iCKB exchange ratio for a recoverable standard deposit.
170
+ *
171
+ * @remarks Includes the standard deposit's 82 CKB occupied capacity spread
172
+ * over the 100,000 iCKB reference amount.
173
+ */
174
+ export function ickbExchangeRatio(header) {
175
+ return {
176
+ ckbScale: AR_0,
177
+ udtScale: header.dao.ar + depositCapacityDelta,
178
+ };
179
+ }
180
+ //# sourceMappingURL=udt.js.map
@@ -0,0 +1 @@
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EAAE,KAAoB;IACrE,cAAc,CAAC,KAAK,CAAC,CAAC;IACtB,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC;AACpD,CAAC;AAED,4DAA4D;AAC5D,MAAM,UAAU,UAAU,CAAC,MAAsB,EAAE,KAAoB;IACrE,cAAc,CAAC,KAAK,CAAC,CAAC;IACtB,OAAO,CAAC,MAAM,GAAG,KAAK,CAAC,QAAQ,CAAC,GAAG,KAAK,CAAC,QAAQ,CAAC;AACpD,CAAC;AAED,SAAS,cAAc,CAAC,EAAE,QAAQ,EAAE,QAAQ,EAAiB;IAC3D,IAAI,QAAQ,IAAI,EAAE,IAAI,QAAQ,IAAI,EAAE,EAAE,CAAC;QACrC,MAAM,IAAI,KAAK,CAAC,wCAAwC,CAAC,CAAC;IAC5D,CAAC;AACH,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAA6B;IAC/D,OAAO;QACL,QAAQ,EAAE,IAAI;QACd,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE;KACxB,CAAC;AACJ,CAAC;AAED;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,MAA6B;IAC7D,OAAO;QACL,QAAQ,EAAE,IAAI;QACd,QAAQ,EAAE,MAAM,CAAC,GAAG,CAAC,EAAE,GAAG,oBAAoB;KAC/C,CAAC;AACJ,CAAC"}
@@ -0,0 +1,3 @@
1
+ /** Public CKB networks supported by Stack consumers. */
2
+ export type SupportedChain = "mainnet" | "testnet";
3
+ //# sourceMappingURL=chain.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chain.d.ts","sourceRoot":"","sources":["../../src/utils/chain.ts"],"names":[],"mappings":"AAAA,wDAAwD;AACxD,MAAM,MAAM,cAAc,GAAG,SAAS,GAAG,SAAS,CAAC"}
@@ -0,0 +1,2 @@
1
+ export {};
2
+ //# sourceMappingURL=chain.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"chain.js","sourceRoot":"","sources":["../../src/utils/chain.ts"],"names":[],"mappings":""}
@@ -0,0 +1,19 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ /**
3
+ * Codec for little-endian 32-bit signed integers.
4
+ *
5
+ * @remarks
6
+ * The encoder rejects numeric values outside the signed 32-bit range before
7
+ * writing bytes. Decoding reads a signed little-endian integer from the bytes
8
+ * after CCC byte normalization.
9
+ */
10
+ export declare const CheckedInt32LE: ccc.Codec<ccc.NumLike, number, any>;
11
+ /** Checked codec for 8-bit unsigned integers. */
12
+ export declare const CheckedUint8: ccc.Codec<ccc.NumLike, bigint, any>;
13
+ /** Checked codec for little-endian 32-bit unsigned integers. */
14
+ export declare const CheckedUint32LE: ccc.Codec<ccc.NumLike, bigint, any>;
15
+ /** Checked codec for little-endian 64-bit unsigned integers. */
16
+ export declare const CheckedUint64LE: ccc.Codec<ccc.NumLike, bigint, any>;
17
+ /** Checked codec for little-endian 128-bit unsigned integers. */
18
+ export declare const CheckedUint128LE: ccc.Codec<ccc.NumLike, bigint, any>;
19
+ //# sourceMappingURL=codec.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.d.ts","sourceRoot":"","sources":["../../src/utils/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC;;;;;;;GAOG;AACH,eAAO,MAAM,cAAc,qCAmBzB,CAAC;AAMH,iDAAiD;AACjD,eAAO,MAAM,YAAY,qCAAmB,CAAC;AAE7C,gEAAgE;AAChE,eAAO,MAAM,eAAe,qCAAmB,CAAC;AAEhD,gEAAgE;AAChE,eAAO,MAAM,eAAe,qCAAmB,CAAC;AAEhD,iEAAiE;AACjE,eAAO,MAAM,gBAAgB,qCAAoB,CAAC"}
@@ -0,0 +1,69 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ /**
3
+ * Codec for little-endian 32-bit signed integers.
4
+ *
5
+ * @remarks
6
+ * The encoder rejects numeric values outside the signed 32-bit range before
7
+ * writing bytes. Decoding reads a signed little-endian integer from the bytes
8
+ * after CCC byte normalization.
9
+ */
10
+ export const CheckedInt32LE = ccc.Codec.from({
11
+ byteLength: 4,
12
+ encode: (numLike) => {
13
+ const normalized = normalizeInteger(numLike);
14
+ if (normalized < -2147483648n || normalized > 2147483647n) {
15
+ throw new Error("NumLike out of int32 bounds");
16
+ }
17
+ const num = Number(normalized);
18
+ const encoded = new Uint8Array(4);
19
+ new DataView(encoded.buffer).setInt32(0, num, true);
20
+ return encoded;
21
+ },
22
+ decode: (bytesLike) => {
23
+ const bytes = ccc.bytesFrom(bytesLike);
24
+ return new DataView(bytes.buffer, bytes.byteOffset, bytes.byteLength).getInt32(0, true);
25
+ },
26
+ });
27
+ // CCC's unsigned codecs throw on overflow but encode a negative as its two's complement
28
+ // without a word, and `Ratio.toBytes()` is public and skips `validate`, so these checked
29
+ // codecs keep "never encode a negative" in one place; CCC has no signed Int32 codec at all.
30
+ /** Checked codec for 8-bit unsigned integers. */
31
+ export const CheckedUint8 = checkedUintLE(1);
32
+ /** Checked codec for little-endian 32-bit unsigned integers. */
33
+ export const CheckedUint32LE = checkedUintLE(4);
34
+ /** Checked codec for little-endian 64-bit unsigned integers. */
35
+ export const CheckedUint64LE = checkedUintLE(8);
36
+ /** Checked codec for little-endian 128-bit unsigned integers. */
37
+ export const CheckedUint128LE = checkedUintLE(16);
38
+ function checkedUintLE(byteLength) {
39
+ const bitLength = BigInt(byteLength * 8);
40
+ const max = (1n << bitLength) - 1n;
41
+ return ccc.Codec.from({
42
+ byteLength,
43
+ encode: (numLike) => {
44
+ const num = normalizeInteger(numLike);
45
+ if (num < 0n || num > max) {
46
+ throw new Error(`NumLike out of uint${String(bitLength)} bounds`);
47
+ }
48
+ return ccc.numLeToBytes(num, byteLength);
49
+ },
50
+ decode: ccc.numFromBytes,
51
+ });
52
+ }
53
+ function normalizeInteger(numLike) {
54
+ if (typeof numLike === "number") {
55
+ if (!Number.isFinite(numLike) || !Number.isInteger(numLike)) {
56
+ throw new TypeError("NumLike must be a finite integer");
57
+ }
58
+ if (!Number.isSafeInteger(numLike)) {
59
+ throw new TypeError("NumLike number must be a safe integer");
60
+ }
61
+ }
62
+ try {
63
+ return ccc.numFrom(numLike);
64
+ }
65
+ catch {
66
+ throw new TypeError("NumLike must be a finite integer");
67
+ }
68
+ }
69
+ //# sourceMappingURL=codec.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"codec.js","sourceRoot":"","sources":["../../src/utils/codec.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC;;;;;;;GAOG;AACH,MAAM,CAAC,MAAM,cAAc,GAAG,GAAG,CAAC,KAAK,CAAC,IAAI,CAAsB;IAChE,UAAU,EAAE,CAAC;IACb,MAAM,EAAE,CAAC,OAAO,EAAE,EAAE;QAClB,MAAM,UAAU,GAAG,gBAAgB,CAAC,OAAO,CAAC,CAAC;QAC7C,IAAI,UAAU,GAAG,CAAC,WAAW,IAAI,UAAU,GAAG,WAAW,EAAE,CAAC;YAC1D,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;QACjD,CAAC;QACD,MAAM,GAAG,GAAG,MAAM,CAAC,UAAU,CAAC,CAAC;QAC/B,MAAM,OAAO,GAAG,IAAI,UAAU,CAAC,CAAC,CAAC,CAAC;QAClC,IAAI,QAAQ,CAAC,OAAO,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,CAAC,EAAE,GAAG,EAAE,IAAI,CAAC,CAAC;QACpD,OAAO,OAAO,CAAC;IACjB,CAAC;IACD,MAAM,EAAE,CAAC,SAAS,EAAE,EAAE;QACpB,MAAM,KAAK,GAAG,GAAG,CAAC,SAAS,CAAC,SAAS,CAAC,CAAC;QACvC,OAAO,IAAI,QAAQ,CAAC,KAAK,CAAC,MAAM,EAAE,KAAK,CAAC,UAAU,EAAE,KAAK,CAAC,UAAU,CAAC,CAAC,QAAQ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C,SAAS,CAAC,OAAO,CAAC,EAAE,CAAC;YAC5D,MAAM,IAAI,SAAS,CAAC,kCAAkC,CAAC,CAAC;QAC1D,CAAC;QACD,IAAI,CAAC,MAAM,CAAC,aAAa,CAAC,OAAO,CAAC,EAAE,CAAC;YACnC,MAAM,IAAI,SAAS,CAAC,uCAAuC,CAAC,CAAC;QAC/D,CAAC;IACH,CAAC;IACD,IAAI,CAAC;QACH,OAAO,GAAG,CAAC,OAAO,CAAC,OAAO,CAAC,CAAC;IAC9B,CAAC;IAAC,MAAM,CAAC;QACP,MAAM,IAAI,SAAS,CAAC,kCAAkC,CAAC,CAAC;IAC1D,CAAC;AACH,CAAC"}
@@ -0,0 +1,99 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ /**
3
+ * The default page size used when querying cells from the chain.
4
+ *
5
+ * This page size is aligned with Nervos CKB's pull request #4576
6
+ * (https://github.com/nervosnetwork/ckb/pull/4576) to avoid excessive paging.
7
+ */
8
+ export declare const defaultCellPageSize = 400;
9
+ /**
10
+ * A CCC client cache sized for a whole pool: 4096 headers and 4096 transaction bodies,
11
+ * where CCC's defaults keep 128 and 256 (SDK README, Account state). An LRU fills only as
12
+ * far as the book and pool need.
13
+ */
14
+ export declare function clientCache(): ccc.ClientCacheMemory;
15
+ /** A committed cell and the block the node names for its creating transaction. */
16
+ export interface ScannedCell {
17
+ cell: ccc.Cell;
18
+ blockNumber: ccc.Num;
19
+ }
20
+ /**
21
+ * Reads every committed cell matching the key with the block the node names for it, page
22
+ * by page through the raw requestor (CCC's typed scan drops the block), bypassing CCC's
23
+ * cell cache. A full page whose cursor does not advance throws. Why: SDK README, Account
24
+ * state.
25
+ */
26
+ export declare function findCells(client: ccc.Client, keyLike: ccc.ClientIndexerSearchKeyLike): Promise<ScannedCell[]>;
27
+ /** The headers of a batch of block numbers, each read once; every block must exist. */
28
+ export declare function headersByNumber(client: ccc.Client, blockNumbers: Iterable<ccc.Num>): Promise<Map<ccc.Num, ccc.ClientBlockHeader>>;
29
+ /**
30
+ * CKB and UDT amounts carried by a cell, order, or planned value.
31
+ */
32
+ export interface ValueComponents {
33
+ /** CKB-side amount as a `ccc.FixedPoint`. */
34
+ ckbValue: ccc.FixedPoint;
35
+ /** UDT-side amount as a `ccc.FixedPoint`. */
36
+ udtValue: ccc.FixedPoint;
37
+ }
38
+ /**
39
+ * Integer scale pair for comparing or converting CKB-side and UDT-side values.
40
+ *
41
+ * @remarks
42
+ * CKB-to-UDT conversions multiply by `ckbScale` and divide by `udtScale`.
43
+ * UDT-to-CKB conversions swap the scales. Callers choose the rounding policy.
44
+ */
45
+ export interface ExchangeRatio {
46
+ /** Numerator scale for CKB-side values. */
47
+ ckbScale: ccc.Num;
48
+ /** Numerator scale for UDT-side values. */
49
+ udtScale: ccc.Num;
50
+ }
51
+ /**
52
+ * Script plus cell dependencies needed to build transactions that use it.
53
+ */
54
+ export interface ScriptDeps {
55
+ /**
56
+ * The lock or type script.
57
+ */
58
+ script: ccc.Script;
59
+ /**
60
+ * Cell dependencies required to resolve the script code.
61
+ */
62
+ cellDeps: ccc.CellDep[];
63
+ }
64
+ /**
65
+ * True when a cell has no type script and no data payload.
66
+ *
67
+ * @remarks
68
+ * This is a structural filter for plain capacity cells. Spendability still
69
+ * depends on the lock script, live cell state, and transaction context.
70
+ */
71
+ export declare function isPlainCapacityCell(cell: ccc.Cell): boolean;
72
+ /**
73
+ * The client's JSON-RPC requestor, which the SDK's raw reads need: scans with block
74
+ * numbers, status polls and transaction proofs. The connector hands the interface a
75
+ * composition proxy over the public client (`ClientWithFeeRate`, a `ccc.Client` but not a
76
+ * `ccc.ClientJsonRpc`), so `instanceof` misses it; the proxy forwards the property.
77
+ */
78
+ export declare function jsonRpcRequestor(client: ccc.Client): ccc.RequestorJsonRpc;
79
+ /** `numerator / denominator` rounded up; the denominator is positive. */
80
+ export declare function ceilDiv(numerator: bigint, denominator: bigint): bigint;
81
+ /**
82
+ * Compares two bigint values using sort-compatible ordering.
83
+ *
84
+ * @returns `-1` when `left` is smaller, `1` when `left` is larger, and `0` when equal.
85
+ */
86
+ export declare function compareBigInt(left: bigint, right: bigint): number;
87
+ /**
88
+ * Yields unique items from the given iterable based on their hex representation.
89
+ *
90
+ * The function uses a Set to track the hex-string keys of items that have already been yielded.
91
+ * Only the first occurrence of each unique key is yielded.
92
+ *
93
+ * @typeParam T - A type that extends ccc.Entity.
94
+ * @param items - An iterable collection of items of type T.
95
+ * @returns A generator that yields items from the iterable, ensuring that each item's
96
+ * hex representation (via toHex()) is unique.
97
+ */
98
+ export declare function unique<T extends ccc.Entity>(items: Iterable<T>): Generator<T>;
99
+ //# sourceMappingURL=utils.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.d.ts","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAGpC;;;;;GAKG;AACH,eAAO,MAAM,mBAAmB,MAAM,CAAC;AAEvC;;;;GAIG;AACH,wBAAgB,WAAW,IAAI,GAAG,CAAC,iBAAiB,CAEnD;AAED,kFAAkF;AAClF,MAAM,WAAW,WAAW;IAC1B,IAAI,EAAE,GAAG,CAAC,IAAI,CAAC;IACf,WAAW,EAAE,GAAG,CAAC,GAAG,CAAC;CACtB;AAED;;;;;GAKG;AACH,wBAAsB,SAAS,CAC7B,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,OAAO,EAAE,GAAG,CAAC,0BAA0B,GACtC,OAAO,CAAC,WAAW,EAAE,CAAC,CA+BxB;AAED,uFAAuF;AACvF,wBAAsB,eAAe,CACnC,MAAM,EAAE,GAAG,CAAC,MAAM,EAClB,YAAY,EAAE,QAAQ,CAAC,GAAG,CAAC,GAAG,CAAC,GAC9B,OAAO,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,EAAE,GAAG,CAAC,iBAAiB,CAAC,CAAC,CAc9C;AAaD;;GAEG;AACH,MAAM,WAAW,eAAe;IAC9B,6CAA6C;IAC7C,QAAQ,EAAE,GAAG,CAAC,UAAU,CAAC;IAEzB,6CAA6C;IAC7C,QAAQ,EAAE,GAAG,CAAC,UAAU,CAAC;CAC1B;AAED;;;;;;GAMG;AACH,MAAM,WAAW,aAAa;IAC5B,2CAA2C;IAC3C,QAAQ,EAAE,GAAG,CAAC,GAAG,CAAC;IAElB,2CAA2C;IAC3C,QAAQ,EAAE,GAAG,CAAC,GAAG,CAAC;CACnB;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB;;OAEG;IACH,MAAM,EAAE,GAAG,CAAC,MAAM,CAAC;IAEnB;;OAEG;IACH,QAAQ,EAAE,GAAG,CAAC,OAAO,EAAE,CAAC;CACzB;AAED;;;;;;GAMG;AACH,wBAAgB,mBAAmB,CAAC,IAAI,EAAE,GAAG,CAAC,IAAI,GAAG,OAAO,CAE3D;AAED;;;;;GAKG;AACH,wBAAgB,gBAAgB,CAAC,MAAM,EAAE,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,gBAAgB,CAMzE;AAED,yEAAyE;AACzE,wBAAgB,OAAO,CAAC,SAAS,EAAE,MAAM,EAAE,WAAW,EAAE,MAAM,GAAG,MAAM,CAEtE;AAED;;;;GAIG;AACH,wBAAgB,aAAa,CAAC,IAAI,EAAE,MAAM,EAAE,KAAK,EAAE,MAAM,GAAG,MAAM,CAUjE;AAED;;;;;;;;;;GAUG;AACH,wBAAiB,MAAM,CAAC,CAAC,SAAS,GAAG,CAAC,MAAM,EAAE,KAAK,EAAE,QAAQ,CAAC,CAAC,CAAC,GAAG,SAAS,CAAC,CAAC,CAAC,CAS9E"}
@@ -0,0 +1,130 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ import { cccA } from "@ckb-ccc/core/advanced";
3
+ /**
4
+ * The default page size used when querying cells from the chain.
5
+ *
6
+ * This page size is aligned with Nervos CKB's pull request #4576
7
+ * (https://github.com/nervosnetwork/ckb/pull/4576) to avoid excessive paging.
8
+ */
9
+ export const defaultCellPageSize = 400;
10
+ /**
11
+ * A CCC client cache sized for a whole pool: 4096 headers and 4096 transaction bodies,
12
+ * where CCC's defaults keep 128 and 256 (SDK README, Account state). An LRU fills only as
13
+ * far as the book and pool need.
14
+ */
15
+ export function clientCache() {
16
+ return new ccc.ClientCacheMemory(512, 4096, 4096);
17
+ }
18
+ /**
19
+ * Reads every committed cell matching the key with the block the node names for it, page
20
+ * by page through the raw requestor (CCC's typed scan drops the block), bypassing CCC's
21
+ * cell cache. A full page whose cursor does not advance throws. Why: SDK README, Account
22
+ * state.
23
+ */
24
+ export async function findCells(client, keyLike) {
25
+ const requestor = jsonRpcRequestor(client);
26
+ const key = cccA.JsonRpcTransformers.indexerSearchKeyFrom(keyLike);
27
+ const cells = [];
28
+ let after;
29
+ for (;;) {
30
+ // eslint-disable-next-line @typescript-eslint/no-unsafe-type-assertion, no-restricted-syntax -- the node's indexer answer, parsed field by field below as CCC's own transformer does.
31
+ const page = (await requestor.request("get_cells", [
32
+ key,
33
+ "asc",
34
+ ccc.numToHex(defaultCellPageSize),
35
+ after,
36
+ ]));
37
+ for (const object of page.objects) {
38
+ cells.push({
39
+ cell: ccc.Cell.from({
40
+ outPoint: cccA.JsonRpcTransformers.outPointTo(object.out_point),
41
+ cellOutput: cccA.JsonRpcTransformers.cellOutputTo(object.output),
42
+ outputData: object.output_data ?? "0x",
43
+ }),
44
+ blockNumber: ccc.numFrom(object.block_number),
45
+ });
46
+ }
47
+ if (page.objects.length < defaultCellPageSize) {
48
+ return cells;
49
+ }
50
+ if (page.last_cursor === after) {
51
+ throw new Error("Cell scan cursor did not advance");
52
+ }
53
+ after = page.last_cursor;
54
+ }
55
+ }
56
+ /** The headers of a batch of block numbers, each read once; every block must exist. */
57
+ export async function headersByNumber(client, blockNumbers) {
58
+ const distinct = [...new Set(blockNumbers)];
59
+ const headers = await Promise.all(distinct.map(async (blockNumber) => client.getHeaderByNumber(blockNumber)));
60
+ return new Map(distinct.map((blockNumber, index) => {
61
+ const header = headers[index];
62
+ if (header === undefined) {
63
+ throw new Error(`Header not found for block number ${String(blockNumber)}`);
64
+ }
65
+ return [blockNumber, header];
66
+ }));
67
+ }
68
+ /**
69
+ * True when a cell has no type script and no data payload.
70
+ *
71
+ * @remarks
72
+ * This is a structural filter for plain capacity cells. Spendability still
73
+ * depends on the lock script, live cell state, and transaction context.
74
+ */
75
+ export function isPlainCapacityCell(cell) {
76
+ return cell.cellOutput.type === undefined && cell.outputData === "0x";
77
+ }
78
+ /**
79
+ * The client's JSON-RPC requestor, which the SDK's raw reads need: scans with block
80
+ * numbers, status polls and transaction proofs. The connector hands the interface a
81
+ * composition proxy over the public client (`ClientWithFeeRate`, a `ccc.Client` but not a
82
+ * `ccc.ClientJsonRpc`), so `instanceof` misses it; the proxy forwards the property.
83
+ */
84
+ export function jsonRpcRequestor(client) {
85
+ const requestor = "requestor" in client ? client.requestor : undefined;
86
+ if (!(requestor instanceof ccc.RequestorJsonRpc)) {
87
+ throw new TypeError("The SDK needs a JSON-RPC client");
88
+ }
89
+ return requestor;
90
+ }
91
+ /** `numerator / denominator` rounded up; the denominator is positive. */
92
+ export function ceilDiv(numerator, denominator) {
93
+ return (numerator + denominator - 1n) / denominator;
94
+ }
95
+ /**
96
+ * Compares two bigint values using sort-compatible ordering.
97
+ *
98
+ * @returns `-1` when `left` is smaller, `1` when `left` is larger, and `0` when equal.
99
+ */
100
+ export function compareBigInt(left, right) {
101
+ if (left < right) {
102
+ return -1;
103
+ }
104
+ if (left > right) {
105
+ return 1;
106
+ }
107
+ return 0;
108
+ }
109
+ /**
110
+ * Yields unique items from the given iterable based on their hex representation.
111
+ *
112
+ * The function uses a Set to track the hex-string keys of items that have already been yielded.
113
+ * Only the first occurrence of each unique key is yielded.
114
+ *
115
+ * @typeParam T - A type that extends ccc.Entity.
116
+ * @param items - An iterable collection of items of type T.
117
+ * @returns A generator that yields items from the iterable, ensuring that each item's
118
+ * hex representation (via toHex()) is unique.
119
+ */
120
+ export function* unique(items) {
121
+ const set = new Set();
122
+ for (const i of items) {
123
+ const key = i.toHex();
124
+ if (!set.has(key)) {
125
+ set.add(key);
126
+ yield i;
127
+ }
128
+ }
129
+ }
130
+ //# sourceMappingURL=utils.js.map