@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
@@ -0,0 +1 @@
1
+ {"version":3,"file":"utils.js","sourceRoot":"","sources":["../../src/utils/utils.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AACpC,OAAO,EAAE,IAAI,EAAE,MAAM,wBAAwB,CAAC;AAE9C;;;;;GAKG;AACH,MAAM,CAAC,MAAM,mBAAmB,GAAG,GAAG,CAAC;AAEvC;;;;GAIG;AACH,MAAM,UAAU,WAAW;IACzB,OAAO,IAAI,GAAG,CAAC,iBAAiB,CAAC,GAAG,EAAE,IAAI,EAAE,IAAI,CAAC,CAAC;AACpD,CAAC;AAQD;;;;;GAKG;AACH,MAAM,CAAC,KAAK,UAAU,SAAS,CAC7B,MAAkB,EAClB,OAAuC;IAEvC,MAAM,SAAS,GAAG,gBAAgB,CAAC,MAAM,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D,CAAC;AAED;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,IAAY,EAAE,KAAa;IACvD,IAAI,IAAI,GAAG,KAAK,EAAE,CAAC;QACjB,OAAO,CAAC,CAAC,CAAC;IACZ,CAAC;IAED,IAAI,IAAI,GAAG,KAAK,EAAE,CAAC;QACjB,OAAO,CAAC,CAAC;IACX,CAAC;IAED,OAAO,CAAC,CAAC;AACX,CAAC;AAED;;;;;;;;;;GAUG;AACH,MAAM,SAAS,CAAC,CAAC,MAAM,CAAuB,KAAkB;IAC9D,MAAM,GAAG,GAAG,IAAI,GAAG,EAAU,CAAC;IAC9B,KAAK,MAAM,CAAC,IAAI,KAAK,EAAE,CAAC;QACtB,MAAM,GAAG,GAAG,CAAC,CAAC,KAAK,EAAE,CAAC;QACtB,IAAI,CAAC,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,EAAE,CAAC;YAClB,GAAG,CAAC,GAAG,CAAC,GAAG,CAAC,CAAC;YACb,MAAM,CAAC,CAAC;QACV,CAAC;IACH,CAAC;AACH,CAAC"}
@@ -0,0 +1,19 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ /**
3
+ * Wraps `inner` into a client that checks the reads the SDK makes: every header it hands
4
+ * out against the header's own hash, a header read by number against the number asked
5
+ * for, a transaction read by hash against the hash of its body, and a cell against the
6
+ * body of its creating transaction. Other reads pass through unchecked; the cell
7
+ * beside `getCellWithHeader`'s header is one, since the SDK reads only its header.
8
+ *
9
+ * @remarks It wraps the client, not the SDK's readers, because CCC's own DAO valuation
10
+ * (`Cell.getNervosDaoInfo`, in the send) reads headers through the client it is given.
11
+ * Why each check exists: SDK README, Account state.
12
+ */
13
+ export declare function verifiedClient(inner: ccc.Client): ccc.Client;
14
+ /**
15
+ * The hash of a header as the chain computes it: blake2b over the header struct, each
16
+ * number little-endian, in declaration order, the nonce last.
17
+ */
18
+ export declare function headerHash(header: ccc.ClientBlockHeader): ccc.Hex;
19
+ //# sourceMappingURL=verified_client.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verified_client.d.ts","sourceRoot":"","sources":["../../src/utils/verified_client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC;;;;;;;;;;GAUG;AACH,wBAAgB,cAAc,CAAC,KAAK,EAAE,GAAG,CAAC,MAAM,GAAG,GAAG,CAAC,MAAM,CAE5D;AAqGD;;;GAGG;AACH,wBAAgB,UAAU,CAAC,MAAM,EAAE,GAAG,CAAC,iBAAiB,GAAG,GAAG,CAAC,GAAG,CAejE"}
@@ -0,0 +1,92 @@
1
+ import { ccc } from "@ckb-ccc/core";
2
+ /**
3
+ * Wraps `inner` into a client that checks the reads the SDK makes: every header it hands
4
+ * out against the header's own hash, a header read by number against the number asked
5
+ * for, a transaction read by hash against the hash of its body, and a cell against the
6
+ * body of its creating transaction. Other reads pass through unchecked; the cell
7
+ * beside `getCellWithHeader`'s header is one, since the SDK reads only its header.
8
+ *
9
+ * @remarks It wraps the client, not the SDK's readers, because CCC's own DAO valuation
10
+ * (`Cell.getNervosDaoInfo`, in the send) reads headers through the client it is given.
11
+ * Why each check exists: SDK README, Account state.
12
+ */
13
+ export function verifiedClient(inner) {
14
+ return new VerifiedClient(inner);
15
+ }
16
+ // @ts-expect-error TS2655: the abstract members are forwarded by the proxy at runtime, as in CCC's own connector.
17
+ class VerifiedClient extends ccc.Proxy.Base(ccc.Client) {
18
+ static [ccc.Proxy.localKeys] = ["bodies"];
19
+ /** Creating transactions `getCell` read, so concurrent reads of one share a request. */
20
+ bodies = new Map();
21
+ async getTipHeader() {
22
+ return verified(await this[ccc.Proxy.inner].getTipHeader());
23
+ }
24
+ async getHeaderByNumber(...args) {
25
+ const header = ccc.apply(verified, await this[ccc.Proxy.inner].getHeaderByNumber(...args));
26
+ const number = ccc.numFrom(args[0]);
27
+ if (header !== undefined && header.number !== number) {
28
+ throw new Error(`Header ${header.hash} answers for block ${number.toString()}`);
29
+ }
30
+ return header;
31
+ }
32
+ async getTransaction(...args) {
33
+ return verifiedBody(await this[ccc.Proxy.inner].getTransaction(...args), args[0]);
34
+ }
35
+ /**
36
+ * The output of the verified creating transaction, cache first: CCC keys cached
37
+ * transactions by their body's hash, and its own `getCell` would build the cell from a
38
+ * read this wrapper never sees, then cache it.
39
+ */
40
+ async getCell(...args) {
41
+ const outPoint = ccc.OutPoint.from(args[0]);
42
+ const { txHash } = outPoint;
43
+ let body = this.bodies.get(txHash);
44
+ if (body === undefined) {
45
+ body = this.readBody(txHash);
46
+ this.bodies.set(txHash, body);
47
+ }
48
+ const output = (await body)?.transaction.getOutput(outPoint.index);
49
+ return output === undefined
50
+ ? undefined
51
+ : ccc.Cell.from({
52
+ outPoint,
53
+ cellOutput: output.cellOutput,
54
+ outputData: output.outputData,
55
+ });
56
+ }
57
+ async readBody(txHash) {
58
+ // CCC's own cache keys bodies by their hash, but a consumer's cache need not.
59
+ return (verifiedBody(await this.cache.getTransactionResponse(txHash), txHash) ??
60
+ (await this.getTransaction(txHash)));
61
+ }
62
+ async getCellWithHeader(...args) {
63
+ const result = await this[ccc.Proxy.inner].getCellWithHeader(...args);
64
+ ccc.apply(verified, result?.header);
65
+ return result;
66
+ }
67
+ }
68
+ /** The response when its body hashes to the hash asked for, else throws. */
69
+ function verifiedBody(response, txHash) {
70
+ const asked = ccc.hexFrom(txHash);
71
+ if (response !== undefined && response.transaction.hash() !== asked) {
72
+ throw new Error(`Transaction ${asked} was answered with a body of another hash`);
73
+ }
74
+ return response;
75
+ }
76
+ /** The header when its fields hash to the hash it carries, else throws. */
77
+ function verified(header) {
78
+ const computed = headerHash(header);
79
+ if (computed !== header.hash) {
80
+ throw new Error(`Header ${header.hash} carries the fields of ${computed}`);
81
+ }
82
+ return header;
83
+ }
84
+ /**
85
+ * The hash of a header as the chain computes it: blake2b over the header struct, each
86
+ * number little-endian, in declaration order, the nonce last.
87
+ */
88
+ export function headerHash(header) {
89
+ const { dao } = header;
90
+ return ccc.hashCkb(ccc.numLeToBytes(header.version, 4), ccc.numLeToBytes(header.compactTarget, 4), ccc.numLeToBytes(header.timestamp, 8), ccc.numLeToBytes(header.number, 8), ccc.numLeToBytes(header.epoch.toNum(), 8), header.parentHash, header.transactionsRoot, header.proposalsHash, header.extraHash, ...[dao.c, dao.ar, dao.s, dao.u].map((field) => ccc.numLeToBytes(field, 8)), ccc.numLeToBytes(header.nonce, 16));
91
+ }
92
+ //# sourceMappingURL=verified_client.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"verified_client.js","sourceRoot":"","sources":["../../src/utils/verified_client.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,GAAG,EAAE,MAAM,eAAe,CAAC;AAEpC;;;;;;;;;;GAUG;AACH,MAAM,UAAU,cAAc,CAAC,KAAiB;IAC9C,OAAO,IAAI,cAAc,CAAC,KAAK,CAAC,CAAC;AACnC,CAAC;AAED,kHAAkH;AAClH,MAAM,cAAe,SAAQ,GAAG,CAAC,KAAK,CAAC,IAAI,CAAC,GAAG,CAAC,MAAM,CAAC;IAC9C,MAAM,CAAU,CAAC,GAAG,CAAC,KAAK,CAAC,SAAS,CAAC,GAAG,CAAC,QAAQ,CAAC,CAAC;IAE1D,wFAAwF;IACvE,MAAM,GAAG,IAAI,GAAG,EAG9B,CAAC;IAEY,KAAK,CAAC,YAAY;QAChC,OAAO,QAAQ,CAAC,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,YAAY,EAAE,CAAC,CAAC;IAC9D,CAAC;IAEe,KAAK,CAAC,iBAAiB,CACrC,GAAG,IAAiD;QAEpD,MAAM,MAAM,GAAG,GAAG,CAAC,KAAK,CACtB,QAAQ,EACR,MAAM,IAAI,CAAC,GAAG,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,iBAAiB,CAAC,GAAG,IAAI,CAAC,CACvD,CAAC;QACF,MAAM,MAAM,GAAG,GAAG,CAAC,OAAO,CAAC,IAAI,CAAC,CAAC,CAAC,CAA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A6B;IACtD,MAAM,EAAE,GAAG,EAAE,GAAG,MAAM,CAAC;IACvB,OAAO,GAAG,CAAC,OAAO,CAChB,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,OAAO,EAAE,CAAC,CAAC,EACnC,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,aAAa,EAAE,CAAC,CAAC,EACzC,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,SAAS,EAAE,CAAC,CAAC,EACrC,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,MAAM,EAAE,CAAC,CAAC,EAClC,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,KAAK,CAAC,KAAK,EAAE,EAAE,CAAC,CAAC,EACzC,MAAM,CAAC,UAAU,EACjB,MAAM,CAAC,gBAAgB,EACvB,MAAM,CAAC,aAAa,EACpB,MAAM,CAAC,SAAS,EAChB,GAAG,CAAC,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,EAAE,GAAG,CAAC,CAAC,EAAE,GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,GAAG,CAAC,YAAY,CAAC,KAAK,EAAE,CAAC,CAAC,CAAC,EAC3E,GAAG,CAAC,YAAY,CAAC,MAAM,CAAC,KAAK,EAAE,EAAE,CAAC,CACnC,CAAC;AACJ,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@ickb/sdk",
3
- "version": "1000.0.82",
3
+ "version": "9001.0.0",
4
4
  "description": "iCKB SDK built on top of CCC",
5
5
  "keywords": [
6
6
  "ickb",
@@ -10,52 +10,45 @@
10
10
  ],
11
11
  "author": "phroi",
12
12
  "license": "MIT",
13
- "homepage": "https://github.com/ickb/sdk",
13
+ "homepage": "https://ickb.org",
14
14
  "repository": {
15
15
  "type": "git",
16
- "url": "https://github.com/ickb/sdk"
16
+ "url": "git+https://github.com/ickb/stack.git"
17
17
  },
18
18
  "bugs": {
19
- "url": "https://github.com/ickb/sdk/issues"
19
+ "url": "https://github.com/ickb/stack/issues"
20
20
  },
21
21
  "sideEffects": false,
22
22
  "type": "module",
23
+ "engines": {
24
+ "node": ">=26"
25
+ },
23
26
  "main": "dist/index.js",
24
27
  "types": "dist/index.d.ts",
25
28
  "exports": {
26
29
  ".": {
27
- "import": "./dist/index.js",
28
- "types": "./dist/index.d.ts"
30
+ "types": "./dist/index.d.ts",
31
+ "import": "./dist/index.js"
29
32
  }
30
33
  },
31
- "engines": {
32
- "node": ">=18"
33
- },
34
34
  "files": [
35
35
  "dist",
36
36
  "src"
37
37
  ],
38
38
  "publishConfig": {
39
- "access": "public",
40
- "provenance": true
41
- },
42
- "devDependencies": {
43
- "@eslint/js": "^9.29.0",
44
- "eslint": "^9.29.0",
45
- "prettier": "^3.5.3",
46
- "typescript": "^5.8.3",
47
- "typescript-eslint": "^8.34.0"
39
+ "access": "public"
48
40
  },
49
41
  "dependencies": {
50
- "@ckb-ccc/core": "canary",
51
- "@ickb/core": "^1000.0.82",
52
- "@ickb/dao": "^1000.0.82",
53
- "@ickb/order": "^1000.0.82",
54
- "@ickb/utils": "^1000.0.82"
42
+ "@ckb-ccc/core": "^1.23.0"
43
+ },
44
+ "devDependencies": {
45
+ "@ickb/testkit": "9001.0.0",
46
+ "@typescript/native-preview": "7.0.0-dev.20260707.2"
55
47
  },
56
48
  "scripts": {
57
- "lint": "eslint ./src",
58
- "clean": "rm -fr pnpm-lock.yaml dist/*",
59
- "revamp": "pnpm clean; pnpm up && node dist/index.js && pnpm lint"
49
+ "test": "vitest",
50
+ "build": "rm -fr dist && tsgo -p tsconfig.build.json && node ../scripts/tooling/build/rewrite-dts-imports.ts dist",
51
+ "lint:api": "api-extractor run",
52
+ "lint:publish": "publint --strict --pack pnpm"
60
53
  }
61
54
  }
package/src/constants.ts CHANGED
@@ -1,205 +1,89 @@
1
1
  import { ccc } from "@ckb-ccc/core";
2
- import { IckbUdtManager, LogicManager, OwnedOwnerManager } from "@ickb/core";
3
- import { DaoManager } from "@ickb/dao";
4
- import { OrderManager } from "@ickb/order";
5
- import { CapacityManager, unique, type ScriptDeps } from "@ickb/utils";
2
+ import type { SdkManagers } from "./conversion/types.ts";
3
+ import { LogicManager } from "./logic.ts";
4
+ import { OrderManager } from "./order/order.ts";
5
+ import { OwnedOwnerManager } from "./owned_owner.ts";
6
+ import { IckbUdt } from "./udt.ts";
7
+ import type { SupportedChain } from "./utils/chain.ts";
6
8
 
7
- /**
8
- * Retrieves the configuration for the given deployment environment.
9
- *
10
- * Accepts either a string identifier ("mainnet" or "testnet") or a custom configuration
11
- * object containing script dependencies for a devnet.
12
- *
13
- * It sets up various managers (UDT, Logic, OwnedOwner, Order, Dao, Capacity) and also
14
- * aggregates a unique list of known bot scripts.
15
- *
16
- * @param d - Either a network identifier ("mainnet"/"testnet") or an object containing
17
- * explicit script dependencies for devnet.
18
- * @param bots - An optional array of bot script-like objects to augment the list of known bots.
19
- * @returns An object containing the instantiated managers and bots.
20
- */
21
- export function getConfig(
22
- d:
23
- | "mainnet"
24
- | "testnet"
25
- | {
26
- // For devnet configuration provide explicit script dependencies.
27
- udt: ScriptDeps;
28
- logic: ScriptDeps;
29
- ownedOwner: ScriptDeps;
30
- order: ScriptDeps;
31
- dao: ScriptDeps;
32
- },
33
- bots: ccc.ScriptLike[] = [],
34
- ): {
35
- managers: {
36
- capacity: CapacityManager;
37
- dao: DaoManager;
38
- ickbUdt: IckbUdtManager;
39
- logic: LogicManager;
40
- ownedOwner: OwnedOwnerManager;
41
- order: OrderManager;
42
- };
43
- bots: ccc.Script[];
44
- } {
45
- // If deps is provided as a network string, use the pre-defined constants.
46
- if (d === "mainnet" || d === "testnet") {
47
- bots = bots.concat(
48
- d === "mainnet" ? MAINNET_KNOWN_BOTS : TESTNET_KNOWN_BOTS,
49
- );
50
- const depGroup = d === "mainnet" ? MAINNET_DEP_GROUP : TESTNET_DEP_GROUP;
51
- d = {
52
- udt: from(UDT, depGroup),
53
- logic: from(ICKB_LOGIC, depGroup),
54
- ownedOwner: from(OWNED_OWNER, depGroup),
55
- order: from(ORDER, depGroup),
56
- dao: from(DAO, depGroup),
57
- };
58
- }
59
-
60
- const capacity = CapacityManager.withAnyData();
61
- const dao = new DaoManager(d.dao.script, d.dao.cellDeps);
62
- const ickbUdt = new IckbUdtManager(
63
- d.udt.script,
64
- d.udt.cellDeps,
65
- d.logic.script,
66
- dao,
67
- );
68
- const logic = new LogicManager(
69
- d.logic.script,
70
- d.logic.cellDeps,
71
- dao,
72
- ickbUdt,
73
- );
74
- const ownedOwner = new OwnedOwnerManager(
75
- d.ownedOwner.script,
76
- d.ownedOwner.cellDeps,
77
- dao,
78
- ickbUdt,
79
- );
80
- const order = new OrderManager(d.order.script, d.order.cellDeps, ickbUdt);
81
-
82
- return {
83
- managers: {
84
- capacity,
85
- dao,
86
- ickbUdt,
87
- logic,
88
- ownedOwner,
89
- order,
90
- },
91
- bots: [...unique(bots.map((b) => ccc.Script.from(b)))],
92
- };
93
- }
94
-
95
- /**
96
- * Wraps a script-like object into a ScriptDeps structure.
97
- *
98
- * @param script - The script or script-like object.
99
- * @param cellDeps - Additional cell dependencies that the script may require.
100
- * @returns An object containing the script and its associated cell dependencies.
101
- */
102
- function from(script: ccc.ScriptLike, ...cellDeps: ccc.CellDep[]): ScriptDeps {
103
- return {
104
- script: ccc.Script.from(script),
105
- cellDeps,
106
- };
107
- }
108
-
109
- /**
110
- * DAO (Decentralized Autonomous Organization) lock script information.
111
- */
112
- const DAO = {
113
- codeHash:
114
- "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
9
+ /** Nervos DAO type script. */
10
+ const DAO = ccc.Script.from({
11
+ codeHash: "0x82d76d1b75fe2fd9a27dfbaa65a039221a380d76c926f378d3f81cf3e7e13f2e",
115
12
  hashType: "type",
116
13
  args: "0x",
117
- };
14
+ });
118
15
 
119
- /**
120
- * UDT (User Defined Token) lock script information used for onchain validation.
121
- */
122
- const UDT = {
123
- codeHash:
124
- "0x50bd8d6680b8b9cf98b73f3c08faf8b2a21914311954118ad6609be6e78a1b95",
16
+ /** Raw xUDT type script; the iCKB args are computed by `IckbUdt.typeScriptFrom`. */
17
+ const UDT = ccc.Script.from({
18
+ codeHash: "0x50bd8d6680b8b9cf98b73f3c08faf8b2a21914311954118ad6609be6e78a1b95",
125
19
  hashType: "data1",
126
- args: "0xb73b6ab39d79390c6de90a09c96b290c331baf1798ed6f97aed02590929734e800000080",
127
- };
20
+ args: "0x",
21
+ });
128
22
 
129
- /**
130
- * Logic script information for the iCKB logic contract.
131
- */
132
- const ICKB_LOGIC = {
133
- codeHash:
134
- "0x2a8100ab5990fa055ab1b50891702e1e895c7bd1df6322cd725c1a6115873bd3",
23
+ /** iCKB Logic script. */
24
+ const ICKB_LOGIC = ccc.Script.from({
25
+ codeHash: "0x2a8100ab5990fa055ab1b50891702e1e895c7bd1df6322cd725c1a6115873bd3",
135
26
  hashType: "data1",
136
27
  args: "0x",
137
- };
28
+ });
138
29
 
139
- /**
140
- * OwnedOwner lock script information.
141
- */
142
- const OWNED_OWNER = {
143
- codeHash:
144
- "0xacc79e07d107831feef4c70c9e683dac5644d5993b9cb106dca6e74baa381bd0",
30
+ /** Owned Owner lock script. */
31
+ const OWNED_OWNER = ccc.Script.from({
32
+ codeHash: "0xacc79e07d107831feef4c70c9e683dac5644d5993b9cb106dca6e74baa381bd0",
145
33
  hashType: "data1",
146
34
  args: "0x",
147
- };
35
+ });
148
36
 
149
- /**
150
- * Order lock script information.
151
- */
152
- const ORDER = {
153
- codeHash:
154
- "0x49dfb6afee5cc8ac4225aeea8cb8928b150caf3cd92fea33750683c74b13254a",
37
+ /** Order lock script. */
38
+ const ORDER = ccc.Script.from({
39
+ codeHash: "0x49dfb6afee5cc8ac4225aeea8cb8928b150caf3cd92fea33750683c74b13254a",
155
40
  hashType: "data1",
156
41
  args: "0x",
157
- };
158
-
159
- /**
160
- * Mainnet dependency group cell dep.
161
- */
162
- const MAINNET_DEP_GROUP = ccc.CellDep.from({
163
- outPoint: {
164
- txHash:
165
- "0x621a6f38de3b9f453016780edac3b26bfcbfa3e2ecb47c2da275471a5d3ed165",
166
- index: "0x0",
167
- },
168
- depType: "depGroup",
169
42
  });
170
43
 
171
44
  /**
172
- * Testnet dependency group cell dep.
45
+ * Each network's iCKB dep group: it carries every iCKB script's code cell, xUDT's and
46
+ * iCKB Logic's included, so one group dep resolves them all.
173
47
  */
174
- const TESTNET_DEP_GROUP = ccc.CellDep.from({
175
- outPoint: {
176
- txHash:
177
- "0xf7ece4fb33d8378344cab11fcd6a4c6f382fd4207ac921cf5821f30712dcd311",
178
- index: "0x0",
48
+ const DEPLOYMENTS = {
49
+ mainnet: {
50
+ depGroup: {
51
+ txHash: "0x621a6f38de3b9f453016780edac3b26bfcbfa3e2ecb47c2da275471a5d3ed165",
52
+ index: 0,
53
+ },
179
54
  },
180
- depType: "depGroup",
181
- });
182
-
183
- /**
184
- * Array of known bot scripts on the mainnet.
185
- */
186
- const MAINNET_KNOWN_BOTS = [
187
- {
188
- codeHash:
189
- "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
190
- hashType: "type",
191
- args: "0xd096cb29e2f68a85a46bd6bf6cbee6327959ba64",
55
+ testnet: {
56
+ depGroup: {
57
+ txHash: "0xf7ece4fb33d8378344cab11fcd6a4c6f382fd4207ac921cf5821f30712dcd311",
58
+ index: 0,
59
+ },
192
60
  },
193
- ];
61
+ } as const;
194
62
 
195
63
  /**
196
- * Array of known bot scripts on the testnet.
64
+ * The managers of one network's iCKB deployment.
65
+ *
66
+ * @remarks Builders still return partial transactions. `IckbSdk` owns the shared iCKB
67
+ * completion path as `sdk.completeTransaction(...)`, which callers invoke before send.
197
68
  */
198
- const TESTNET_KNOWN_BOTS = [
199
- {
200
- codeHash:
201
- "0x9bd7e06f3ecf4be0f2fcd2188b23f1b9fcc88e5d4b65a8637b17723bbda3cce8",
202
- hashType: "type",
203
- args: "0xb4380110f7679ac31cefe4925485645d82bf619f",
204
- },
205
- ];
69
+ export function getConfig(chain: SupportedChain): SdkManagers {
70
+ // The runtime check stands for callers outside TypeScript.
71
+ if (!Object.hasOwn(DEPLOYMENTS, chain)) {
72
+ throw new TypeError("unsupported iCKB network");
73
+ }
74
+ const { depGroup } = DEPLOYMENTS[chain];
75
+ const cellDeps = [ccc.CellDep.from({ outPoint: depGroup, depType: "depGroup" })];
76
+ const dao = { script: DAO, cellDeps };
77
+ const ickbUdt = new IckbUdt({
78
+ script: IckbUdt.typeScriptFrom(UDT, ICKB_LOGIC),
79
+ cellDeps,
80
+ logicScript: ICKB_LOGIC,
81
+ daoScript: DAO,
82
+ });
83
+ return {
84
+ ickbUdt,
85
+ ownedOwner: new OwnedOwnerManager(OWNED_OWNER, cellDeps, dao),
86
+ ickbLogic: new LogicManager(ICKB_LOGIC, cellDeps, dao),
87
+ order: new OrderManager(ORDER, cellDeps, ickbUdt.script),
88
+ };
89
+ }
@@ -0,0 +1,155 @@
1
+ import type { IckbDepositCell } from "../logic.ts";
2
+ import {
3
+ OrderConversionRepresentabilityError,
4
+ quoteConversion,
5
+ } from "../order/conversion.ts";
6
+ import { ceilDiv, type ValueComponents } from "../utils/utils.ts";
7
+ import { maturity } from "./maturity.ts";
8
+ import type { ConversionOrderEstimate, SystemState } from "./types.ts";
9
+
10
+ /**
11
+ * Default order fee: 0.01%. At roughly 2% annual DAO yield this buys about two days of
12
+ * price headroom for a buy, versus hours at 0.001%; actual fillability also depends on
13
+ * mining fees. It leaves time to notice a stalled bot without charging the much larger 0.1%.
14
+ */
15
+ export const DEFAULT_ORDER_FEE = 10n;
16
+
17
+ /** Default order-fee denominator used by Stack conversion quotes and plans. */
18
+ export const DEFAULT_ORDER_FEE_BASE = 100000n;
19
+
20
+ /** The quote and its maturity estimate, or `undefined` when no order ratio represents it. */
21
+ export function estimateConversionOrder(
22
+ isCkb2Udt: boolean,
23
+ amounts: ValueComponents,
24
+ system: SystemState,
25
+ { fee, feeBase }: { fee: bigint; feeBase: bigint },
26
+ takenDeposits: readonly IckbDepositCell[] = [],
27
+ ): ConversionOrderEstimate | undefined {
28
+ let quote: ReturnType<typeof quoteConversion>;
29
+ try {
30
+ quote = quoteConversion(isCkb2Udt, system.exchangeRatio, amounts, { fee, feeBase });
31
+ } catch (error) {
32
+ if (error instanceof OrderConversionRepresentabilityError) {
33
+ return undefined;
34
+ }
35
+ throw error;
36
+ }
37
+ const estimatedMaturity =
38
+ quote.ckbFee >= estimateMaturityFeeThreshold(system)
39
+ ? maturity({ info: quote.info, amounts }, system, takenDeposits)
40
+ : undefined;
41
+ return { ...quote, maturity: estimatedMaturity };
42
+ }
43
+
44
+ /**
45
+ * Returns the CKB fee threshold above which order maturity is worth estimating.
46
+ * This conservative proxy stays separate from partialOrderFee: the quote has no built
47
+ * order whose serialized size could supply that fee yet.
48
+ */
49
+ function estimateMaturityFeeThreshold(system: Pick<SystemState, "feeRate">): bigint {
50
+ return 10n * system.feeRate;
51
+ }
52
+
53
+ /**
54
+ * The smallest request one direction accepts as an order at the current fee rate. A
55
+ * CKB-to-iCKB remainder pays the default order fee, which must cover the maturity threshold
56
+ * of ten mining fees; an iCKB-to-CKB remainder can give its whole CKB value as the dust
57
+ * fee, so its value must exceed the threshold. The extra unit covers the quote's rounding,
58
+ * as the dust search's boundary shows; callers round up further for display.
59
+ */
60
+ export function minimumOrderAmount(isCkb2Udt: boolean, system: SystemState): bigint {
61
+ const threshold = estimateMaturityFeeThreshold(system) + 1n;
62
+ if (isCkb2Udt) {
63
+ return ceilDiv(threshold * DEFAULT_ORDER_FEE_BASE, DEFAULT_ORDER_FEE);
64
+ }
65
+ const { ckbScale, udtScale } = system.exchangeRatio;
66
+ return ceilDiv(threshold * ckbScale, udtScale);
67
+ }
68
+
69
+ /**
70
+ * The order leg of an iCKB-to-CKB conversion: at the default fee when that fee clears the
71
+ * maturity threshold, else the smallest fee that does (a dust order, noticed). Either way
72
+ * it is dated: a sell order at par is always covered by the pool (see `maturity`).
73
+ */
74
+ export function estimateIckbToCkbOrder(
75
+ amounts: ValueComponents,
76
+ system: SystemState,
77
+ takenDeposits: readonly IckbDepositCell[],
78
+ ): (ConversionOrderEstimate & { maturity: bigint }) | undefined {
79
+ const base = estimateConversionOrder(
80
+ false,
81
+ amounts,
82
+ system,
83
+ { fee: DEFAULT_ORDER_FEE, feeBase: DEFAULT_ORDER_FEE_BASE },
84
+ takenDeposits,
85
+ );
86
+ if (base?.maturity !== undefined) {
87
+ return { ...base, maturity: base.maturity };
88
+ }
89
+
90
+ const dust = estimateDustIckbToCkbOrder(amounts, system);
91
+ if (dust === undefined) {
92
+ return undefined;
93
+ }
94
+ return {
95
+ ...dust,
96
+ maturity: maturity({ info: dust.info, amounts }, system, takenDeposits),
97
+ notice: {
98
+ inputIckb: amounts.udtValue,
99
+ outputCkb: dust.convertedAmount,
100
+ incentiveCkb: positiveFee(dust.ckbFee),
101
+ },
102
+ };
103
+ }
104
+
105
+ function estimateDustIckbToCkbOrder(
106
+ amounts: ValueComponents,
107
+ system: SystemState,
108
+ ): ConversionOrderEstimate | undefined {
109
+ const baseEstimate = estimateConversionOrder(false, amounts, system, {
110
+ fee: 0n,
111
+ feeBase: DEFAULT_ORDER_FEE_BASE,
112
+ });
113
+ if (baseEstimate === undefined) {
114
+ return undefined;
115
+ }
116
+ const targetFee = estimateMaturityFeeThreshold(system);
117
+ const feeBase = baseEstimate.convertedAmount + 1n;
118
+ if (targetFee <= 0n || feeBase <= 1n) {
119
+ return baseEstimate;
120
+ }
121
+ const estimateWithFee = (fee: bigint): ConversionOrderEstimate | undefined =>
122
+ estimateConversionOrder(false, amounts, system, { fee, feeBase });
123
+ return lowestFeeEstimateAtThreshold(estimateWithFee, feeBase - 1n, targetFee);
124
+ }
125
+
126
+ function lowestFeeEstimateAtThreshold(
127
+ estimateWithFee: (fee: bigint) => ConversionOrderEstimate | undefined,
128
+ highestFee: bigint,
129
+ targetFee: bigint,
130
+ ): ConversionOrderEstimate | undefined {
131
+ const highestDiscount = estimateWithFee(highestFee);
132
+ if (highestDiscount === undefined || highestDiscount.ckbFee < targetFee) {
133
+ return undefined;
134
+ }
135
+
136
+ let low = 0n;
137
+ let high = highestFee;
138
+ while (low < high) {
139
+ const mid = (low + high) / 2n;
140
+ const estimateAtMid = estimateWithFee(mid);
141
+ if (estimateAtMid === undefined) {
142
+ return undefined;
143
+ }
144
+ if (estimateAtMid.ckbFee >= targetFee) {
145
+ high = mid;
146
+ } else {
147
+ low = mid + 1n;
148
+ }
149
+ }
150
+ return estimateWithFee(low);
151
+ }
152
+
153
+ function positiveFee(fee: bigint): bigint {
154
+ return fee > 0n ? fee : 0n;
155
+ }