@ckb-ccc/core 1.2.3 → 1.3.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.
- package/CHANGELOG.md +16 -0
- package/dist/advancedBarrel.d.ts +1 -0
- package/dist/advancedBarrel.d.ts.map +1 -1
- package/dist/advancedBarrel.js +1 -0
- package/dist/barrel.d.ts +1 -0
- package/dist/barrel.d.ts.map +1 -1
- package/dist/barrel.js +1 -0
- package/dist/ckb/script.d.ts +1 -0
- package/dist/ckb/script.d.ts.map +1 -1
- package/dist/ckb/script.js +1 -0
- package/dist/ckb/transaction.d.ts +3 -1
- package/dist/ckb/transaction.d.ts.map +1 -1
- package/dist/ckb/transaction.js +25 -2
- package/dist/client/advanced.d.ts +0 -1
- package/dist/client/advanced.d.ts.map +1 -1
- package/dist/client/advanced.js +0 -1
- package/dist/client/clientPublicMainnet.d.ts +3 -9
- package/dist/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist/client/clientPublicTestnet.d.ts +3 -9
- package/dist/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist/client/jsonRpc/client.d.ts +160 -0
- package/dist/client/jsonRpc/client.d.ts.map +1 -0
- package/dist/client/jsonRpc/client.js +220 -0
- package/dist/client/jsonRpc/index.d.ts +1 -182
- package/dist/client/jsonRpc/index.d.ts.map +1 -1
- package/dist/client/jsonRpc/index.js +1 -293
- package/dist/jsonRpc/advanced.d.ts +2 -0
- package/dist/jsonRpc/advanced.d.ts.map +1 -0
- package/dist/jsonRpc/advanced.js +1 -0
- package/dist/jsonRpc/index.d.ts +2 -0
- package/dist/jsonRpc/index.d.ts.map +1 -0
- package/dist/jsonRpc/index.js +1 -0
- package/dist/jsonRpc/requestor.d.ts +48 -0
- package/dist/jsonRpc/requestor.d.ts.map +1 -0
- package/dist/jsonRpc/requestor.js +107 -0
- package/dist/jsonRpc/transports/advanced.d.ts +5 -0
- package/dist/jsonRpc/transports/advanced.d.ts.map +1 -0
- package/dist/jsonRpc/transports/advanced.js +4 -0
- package/dist/{client/transports/advanced.d.ts → jsonRpc/transports/factory.d.ts} +1 -4
- package/dist/jsonRpc/transports/factory.d.ts.map +1 -0
- package/dist/{client/transports/advanced.js → jsonRpc/transports/factory.js} +0 -3
- package/dist/jsonRpc/transports/http.d.ts.map +1 -0
- package/dist/jsonRpc/transports/transport.d.ts +18 -0
- package/dist/jsonRpc/transports/transport.d.ts.map +1 -0
- package/dist/jsonRpc/transports/webSocket.d.ts.map +1 -0
- package/dist/molecule/predefined.d.ts +12 -0
- package/dist/molecule/predefined.d.ts.map +1 -1
- package/dist/molecule/predefined.js +28 -0
- package/dist/num/index.d.ts.map +1 -1
- package/dist/num/index.js +5 -1
- package/dist.commonjs/advancedBarrel.d.ts +1 -0
- package/dist.commonjs/advancedBarrel.d.ts.map +1 -1
- package/dist.commonjs/advancedBarrel.js +1 -0
- package/dist.commonjs/barrel.d.ts +1 -0
- package/dist.commonjs/barrel.d.ts.map +1 -1
- package/dist.commonjs/barrel.js +1 -0
- package/dist.commonjs/ckb/script.d.ts +1 -0
- package/dist.commonjs/ckb/script.d.ts.map +1 -1
- package/dist.commonjs/ckb/script.js +2 -1
- package/dist.commonjs/ckb/transaction.d.ts +3 -1
- package/dist.commonjs/ckb/transaction.d.ts.map +1 -1
- package/dist.commonjs/ckb/transaction.js +25 -2
- package/dist.commonjs/client/advanced.d.ts +0 -1
- package/dist.commonjs/client/advanced.d.ts.map +1 -1
- package/dist.commonjs/client/advanced.js +0 -1
- package/dist.commonjs/client/clientPublicMainnet.d.ts +3 -9
- package/dist.commonjs/client/clientPublicMainnet.d.ts.map +1 -1
- package/dist.commonjs/client/clientPublicTestnet.d.ts +3 -9
- package/dist.commonjs/client/clientPublicTestnet.d.ts.map +1 -1
- package/dist.commonjs/client/jsonRpc/client.d.ts +160 -0
- package/dist.commonjs/client/jsonRpc/client.d.ts.map +1 -0
- package/dist.commonjs/client/jsonRpc/client.js +224 -0
- package/dist.commonjs/client/jsonRpc/index.d.ts +1 -182
- package/dist.commonjs/client/jsonRpc/index.d.ts.map +1 -1
- package/dist.commonjs/client/jsonRpc/index.js +15 -295
- package/dist.commonjs/jsonRpc/advanced.d.ts +2 -0
- package/dist.commonjs/jsonRpc/advanced.d.ts.map +1 -0
- package/dist.commonjs/jsonRpc/advanced.js +17 -0
- package/dist.commonjs/jsonRpc/index.d.ts +2 -0
- package/dist.commonjs/jsonRpc/index.d.ts.map +1 -0
- package/dist.commonjs/jsonRpc/index.js +17 -0
- package/dist.commonjs/jsonRpc/requestor.d.ts +48 -0
- package/dist.commonjs/jsonRpc/requestor.d.ts.map +1 -0
- package/dist.commonjs/jsonRpc/requestor.js +111 -0
- package/dist.commonjs/jsonRpc/transports/advanced.d.ts +5 -0
- package/dist.commonjs/jsonRpc/transports/advanced.d.ts.map +1 -0
- package/dist.commonjs/{client → jsonRpc}/transports/advanced.js +1 -9
- package/dist.commonjs/{client/transports/advanced.d.ts → jsonRpc/transports/factory.d.ts} +1 -4
- package/dist.commonjs/jsonRpc/transports/factory.d.ts.map +1 -0
- package/dist.commonjs/jsonRpc/transports/factory.js +11 -0
- package/dist.commonjs/jsonRpc/transports/http.d.ts.map +1 -0
- package/dist.commonjs/jsonRpc/transports/transport.d.ts +18 -0
- package/dist.commonjs/jsonRpc/transports/transport.d.ts.map +1 -0
- package/dist.commonjs/jsonRpc/transports/webSocket.d.ts.map +1 -0
- package/dist.commonjs/molecule/predefined.d.ts +12 -0
- package/dist.commonjs/molecule/predefined.d.ts.map +1 -1
- package/dist.commonjs/molecule/predefined.js +30 -1
- package/dist.commonjs/num/index.d.ts.map +1 -1
- package/dist.commonjs/num/index.js +5 -1
- package/package.json +1 -1
- package/src/advancedBarrel.ts +1 -0
- package/src/barrel.ts +1 -0
- package/src/ckb/script.ts +1 -0
- package/src/ckb/transaction.ts +52 -2
- package/src/client/advanced.ts +0 -1
- package/src/client/clientPublicMainnet.ts +2 -8
- package/src/client/clientPublicTestnet.ts +2 -8
- package/src/client/jsonRpc/client.ts +395 -0
- package/src/client/jsonRpc/index.ts +1 -490
- package/src/jsonRpc/advanced.ts +1 -0
- package/src/jsonRpc/index.ts +1 -0
- package/src/jsonRpc/requestor.ts +157 -0
- package/src/jsonRpc/transports/advanced.ts +4 -0
- package/src/{client/transports/advanced.ts → jsonRpc/transports/factory.ts} +0 -4
- package/src/jsonRpc/transports/transport.ts +18 -0
- package/src/molecule/predefined.ts +32 -0
- package/src/num/index.ts +5 -1
- package/dist/client/transports/advanced.d.ts.map +0 -1
- package/dist/client/transports/http.d.ts.map +0 -1
- package/dist/client/transports/transport.d.ts +0 -10
- package/dist/client/transports/transport.d.ts.map +0 -1
- package/dist/client/transports/webSocket.d.ts.map +0 -1
- package/dist.commonjs/client/transports/advanced.d.ts.map +0 -1
- package/dist.commonjs/client/transports/http.d.ts.map +0 -1
- package/dist.commonjs/client/transports/transport.d.ts +0 -10
- package/dist.commonjs/client/transports/transport.d.ts.map +0 -1
- package/dist.commonjs/client/transports/webSocket.d.ts.map +0 -1
- package/src/client/transports/transport.ts +0 -10
- /package/dist/{client → jsonRpc}/transports/http.d.ts +0 -0
- /package/dist/{client → jsonRpc}/transports/http.js +0 -0
- /package/dist/{client → jsonRpc}/transports/transport.js +0 -0
- /package/dist/{client → jsonRpc}/transports/webSocket.d.ts +0 -0
- /package/dist/{client → jsonRpc}/transports/webSocket.js +0 -0
- /package/dist.commonjs/{client → jsonRpc}/transports/http.d.ts +0 -0
- /package/dist.commonjs/{client → jsonRpc}/transports/http.js +0 -0
- /package/dist.commonjs/{client → jsonRpc}/transports/transport.js +0 -0
- /package/dist.commonjs/{client → jsonRpc}/transports/webSocket.d.ts +0 -0
- /package/dist.commonjs/{client → jsonRpc}/transports/webSocket.js +0 -0
- /package/src/{client → jsonRpc}/transports/http.ts +0 -0
- /package/src/{client → jsonRpc}/transports/webSocket.ts +0 -0
|
@@ -1,490 +1 @@
|
|
|
1
|
-
|
|
2
|
-
Cell,
|
|
3
|
-
OutPoint,
|
|
4
|
-
OutPointLike,
|
|
5
|
-
TransactionLike,
|
|
6
|
-
} from "../../ckb/index.js";
|
|
7
|
-
import { Hex, HexLike, hexFrom } from "../../hex/index.js";
|
|
8
|
-
import { Num, NumLike, numFrom, numToHex } from "../../num/index.js";
|
|
9
|
-
import { apply } from "../../utils/index.js";
|
|
10
|
-
import { ClientCache } from "../cache/index.js";
|
|
11
|
-
import { Client } from "../client.js";
|
|
12
|
-
import {
|
|
13
|
-
ClientFindCellsResponse,
|
|
14
|
-
ClientIndexerSearchKeyLike,
|
|
15
|
-
ClientTransactionResponse,
|
|
16
|
-
ErrorClientBase,
|
|
17
|
-
ErrorClientBaseLike,
|
|
18
|
-
ErrorClientDuplicatedTransaction,
|
|
19
|
-
ErrorClientRBFRejected,
|
|
20
|
-
ErrorClientResolveUnknown,
|
|
21
|
-
ErrorClientVerification,
|
|
22
|
-
OutputsValidator,
|
|
23
|
-
} from "../clientTypes.js";
|
|
24
|
-
import {
|
|
25
|
-
JsonRpcPayload,
|
|
26
|
-
Transport,
|
|
27
|
-
transportFromUri,
|
|
28
|
-
} from "../transports/advanced.js";
|
|
29
|
-
import {
|
|
30
|
-
JsonRpcBlock,
|
|
31
|
-
JsonRpcBlockHeader,
|
|
32
|
-
JsonRpcCellOutput,
|
|
33
|
-
JsonRpcTransformers,
|
|
34
|
-
} from "./advanced.js";
|
|
35
|
-
|
|
36
|
-
/**
|
|
37
|
-
* Applies a transformation function to a value if the transformer is provided.
|
|
38
|
-
*
|
|
39
|
-
* @param value - The value to be transformed.
|
|
40
|
-
* @param transformer - An optional transformation function.
|
|
41
|
-
* @returns The transformed value if a transformer is provided, otherwise the original value.
|
|
42
|
-
*
|
|
43
|
-
* @example
|
|
44
|
-
* ```typescript
|
|
45
|
-
* const result = transform(5, (x) => x * 2); // Outputs 10
|
|
46
|
-
* const resultWithoutTransformer = transform(5); // Outputs 5
|
|
47
|
-
* ```
|
|
48
|
-
*/
|
|
49
|
-
|
|
50
|
-
function transform(value: unknown, transformer?: (i: unknown) => unknown) {
|
|
51
|
-
if (transformer) {
|
|
52
|
-
return transformer(value);
|
|
53
|
-
}
|
|
54
|
-
return value;
|
|
55
|
-
}
|
|
56
|
-
|
|
57
|
-
const ERROR_PARSERS: [
|
|
58
|
-
string,
|
|
59
|
-
(error: ErrorClientBaseLike, match: RegExpMatchArray) => ErrorClientBase,
|
|
60
|
-
][] = [
|
|
61
|
-
[
|
|
62
|
-
"Resolve\\(Unknown\\(OutPoint\\((0x.*)\\)\\)\\)",
|
|
63
|
-
(error, match) =>
|
|
64
|
-
new ErrorClientResolveUnknown(error, OutPoint.fromBytes(match[1])),
|
|
65
|
-
],
|
|
66
|
-
[
|
|
67
|
-
"Verification\\(Error { kind: Script, inner: TransactionScriptError { source: (Inputs|Outputs)\\[([0-9]*)\\].(Lock|Type), cause: ValidationFailure: see error code (-?[0-9])* on page https://nervosnetwork\\.github\\.io/ckb-script-error-codes/by-(type|data)-hash/(.*)\\.html",
|
|
68
|
-
(error, match) =>
|
|
69
|
-
new ErrorClientVerification(
|
|
70
|
-
error,
|
|
71
|
-
match[3] === "Lock"
|
|
72
|
-
? "lock"
|
|
73
|
-
: match[1] === "Inputs"
|
|
74
|
-
? "inputType"
|
|
75
|
-
: "outputType",
|
|
76
|
-
match[2],
|
|
77
|
-
Number(match[4]),
|
|
78
|
-
match[5] === "data" ? "data" : "type",
|
|
79
|
-
match[6],
|
|
80
|
-
),
|
|
81
|
-
],
|
|
82
|
-
[
|
|
83
|
-
"Duplicated\\(Byte32\\((0x.*)\\)\\)",
|
|
84
|
-
(error, match) => new ErrorClientDuplicatedTransaction(error, match[1]),
|
|
85
|
-
],
|
|
86
|
-
[
|
|
87
|
-
'RBFRejected\\("Tx\'s current fee is ([0-9]*), expect it to >= ([0-9]*) to replace old txs"\\)',
|
|
88
|
-
(error, match) => new ErrorClientRBFRejected(error, match[1], match[2]),
|
|
89
|
-
],
|
|
90
|
-
];
|
|
91
|
-
|
|
92
|
-
/**
|
|
93
|
-
* An abstract class implementing JSON-RPC client functionality for a specific URL and timeout.
|
|
94
|
-
* Provides methods for sending transactions and building JSON-RPC payloads.
|
|
95
|
-
*/
|
|
96
|
-
|
|
97
|
-
export abstract class ClientJsonRpc extends Client {
|
|
98
|
-
private readonly maxConcurrent?: number;
|
|
99
|
-
private concurrent = 0;
|
|
100
|
-
private readonly pending: (() => void)[] = [];
|
|
101
|
-
|
|
102
|
-
private id = 0;
|
|
103
|
-
private readonly transport: Transport;
|
|
104
|
-
|
|
105
|
-
/**
|
|
106
|
-
* Creates an instance of ClientJsonRpc.
|
|
107
|
-
*
|
|
108
|
-
* @param url_ - The URL of the JSON-RPC server.
|
|
109
|
-
* @param timeout - The timeout for requests in milliseconds
|
|
110
|
-
*/
|
|
111
|
-
|
|
112
|
-
constructor(
|
|
113
|
-
private readonly url_: string,
|
|
114
|
-
config?: {
|
|
115
|
-
timeout?: number;
|
|
116
|
-
cache?: ClientCache;
|
|
117
|
-
maxConcurrent?: number;
|
|
118
|
-
transport?: Transport;
|
|
119
|
-
},
|
|
120
|
-
) {
|
|
121
|
-
super(config);
|
|
122
|
-
|
|
123
|
-
this.maxConcurrent = config?.maxConcurrent;
|
|
124
|
-
this.transport = config?.transport ?? transportFromUri(url_, config);
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
/**
|
|
128
|
-
* Returns the URL of the JSON-RPC server.
|
|
129
|
-
*
|
|
130
|
-
* @returns The URL of the JSON-RPC server.
|
|
131
|
-
*/
|
|
132
|
-
|
|
133
|
-
get url(): string {
|
|
134
|
-
return this.url_;
|
|
135
|
-
}
|
|
136
|
-
|
|
137
|
-
/**
|
|
138
|
-
* Get fee rate statistics
|
|
139
|
-
*
|
|
140
|
-
* @returns Fee rate statistics
|
|
141
|
-
*/
|
|
142
|
-
|
|
143
|
-
getFeeRateStatistics = this.buildSender(
|
|
144
|
-
"get_fee_rate_statistics",
|
|
145
|
-
[(n: NumLike) => apply(numFrom, n)],
|
|
146
|
-
({ mean, median }: { mean: NumLike; median: NumLike }) => ({
|
|
147
|
-
mean: numFrom(mean),
|
|
148
|
-
median: numFrom(median),
|
|
149
|
-
}),
|
|
150
|
-
) as Client["getFeeRateStatistics"];
|
|
151
|
-
|
|
152
|
-
/**
|
|
153
|
-
* Get tip block number
|
|
154
|
-
*
|
|
155
|
-
* @returns Tip block number
|
|
156
|
-
*/
|
|
157
|
-
|
|
158
|
-
getTip = this.buildSender(
|
|
159
|
-
"get_tip_block_number",
|
|
160
|
-
[],
|
|
161
|
-
numFrom,
|
|
162
|
-
) as () => Promise<Num>;
|
|
163
|
-
|
|
164
|
-
/**
|
|
165
|
-
* Get tip block header
|
|
166
|
-
*
|
|
167
|
-
* @param verbosity - result format which allows 0 and 1. (Optional, the default is 1.)
|
|
168
|
-
* @returns BlockHeader
|
|
169
|
-
*/
|
|
170
|
-
getTipHeader = this.buildSender(
|
|
171
|
-
"get_tip_header",
|
|
172
|
-
[],
|
|
173
|
-
(b: JsonRpcBlockHeader) => apply(JsonRpcTransformers.blockHeaderTo, b),
|
|
174
|
-
) as Client["getTipHeader"];
|
|
175
|
-
|
|
176
|
-
/**
|
|
177
|
-
* Get block by block number
|
|
178
|
-
*
|
|
179
|
-
* @param blockNumber - The block number.
|
|
180
|
-
* @param verbosity - result format which allows 0 and 2. (Optional, the default is 2.)
|
|
181
|
-
* @param withCycles - whether the return cycles of block transactions. (Optional, default false.)
|
|
182
|
-
* @returns Block
|
|
183
|
-
*/
|
|
184
|
-
getBlockByNumber = this.buildSender(
|
|
185
|
-
"get_block_by_number",
|
|
186
|
-
[(v: NumLike) => numToHex(numFrom(v))],
|
|
187
|
-
(b: JsonRpcBlock) => apply(JsonRpcTransformers.blockTo, b),
|
|
188
|
-
) as Client["getBlockByNumber"];
|
|
189
|
-
|
|
190
|
-
/**
|
|
191
|
-
* Get block by block hash
|
|
192
|
-
*
|
|
193
|
-
* @param blockHash - The block hash.
|
|
194
|
-
* @param verbosity - result format which allows 0 and 2. (Optional, the default is 2.)
|
|
195
|
-
* @param withCycles - whether the return cycles of block transactions. (Optional, default false.)
|
|
196
|
-
* @returns Block
|
|
197
|
-
*/
|
|
198
|
-
getBlockByHash = this.buildSender("get_block", [hexFrom], (b: JsonRpcBlock) =>
|
|
199
|
-
apply(JsonRpcTransformers.blockTo, b),
|
|
200
|
-
) as Client["getBlockByHash"];
|
|
201
|
-
|
|
202
|
-
/**
|
|
203
|
-
* Get header by block number
|
|
204
|
-
*
|
|
205
|
-
* @param blockNumber - The block number.
|
|
206
|
-
* @param verbosity - result format which allows 0 and 1. (Optional, the default is 1.)
|
|
207
|
-
* @returns BlockHeader
|
|
208
|
-
*/
|
|
209
|
-
getHeaderByNumber = this.buildSender(
|
|
210
|
-
"get_header_by_number",
|
|
211
|
-
[(v: NumLike) => numToHex(numFrom(v))],
|
|
212
|
-
(b: JsonRpcBlockHeader) => apply(JsonRpcTransformers.blockHeaderTo, b),
|
|
213
|
-
) as Client["getHeaderByNumber"];
|
|
214
|
-
|
|
215
|
-
/**
|
|
216
|
-
* Get header by block hash
|
|
217
|
-
*
|
|
218
|
-
* @param blockHash - The block hash.
|
|
219
|
-
* @param verbosity - result format which allows 0 and 1. (Optional, the default is 1.)
|
|
220
|
-
* @returns BlockHeader
|
|
221
|
-
*/
|
|
222
|
-
getHeaderByHash = this.buildSender(
|
|
223
|
-
"get_header",
|
|
224
|
-
[hexFrom],
|
|
225
|
-
(b: JsonRpcBlockHeader) => apply(JsonRpcTransformers.blockHeaderTo, b),
|
|
226
|
-
) as Client["getHeaderByHash"];
|
|
227
|
-
|
|
228
|
-
/**
|
|
229
|
-
* Estimate cycles of a transaction.
|
|
230
|
-
*
|
|
231
|
-
* @param transaction - The transaction to estimate.
|
|
232
|
-
* @returns Consumed cycles
|
|
233
|
-
*/
|
|
234
|
-
estimateCycles = this.buildSender(
|
|
235
|
-
"estimate_cycles",
|
|
236
|
-
[JsonRpcTransformers.transactionFrom],
|
|
237
|
-
({ cycles }: { cycles: NumLike }) => numFrom(cycles),
|
|
238
|
-
) as Client["estimateCycles"];
|
|
239
|
-
|
|
240
|
-
/**
|
|
241
|
-
* Test a transaction.
|
|
242
|
-
*
|
|
243
|
-
* @param transaction - The transaction to test.
|
|
244
|
-
* @param validator - "passthrough": Disable validation. "well_known_scripts_only": Only accept well known scripts in the transaction.
|
|
245
|
-
* @returns Consumed cycles
|
|
246
|
-
*/
|
|
247
|
-
|
|
248
|
-
sendTransactionDry = this.buildSender(
|
|
249
|
-
"test_tx_pool_accept",
|
|
250
|
-
[JsonRpcTransformers.transactionFrom],
|
|
251
|
-
({ cycles }: { cycles: NumLike }) => numFrom(cycles),
|
|
252
|
-
) as Client["sendTransactionDry"];
|
|
253
|
-
|
|
254
|
-
/**
|
|
255
|
-
* Send a transaction to node.
|
|
256
|
-
*
|
|
257
|
-
* @param transaction - The transaction to send.
|
|
258
|
-
* @param validator - "passthrough": Disable validation. "well_known_scripts_only": Only accept well known scripts in the transaction.
|
|
259
|
-
* @returns Transaction hash.
|
|
260
|
-
*/
|
|
261
|
-
|
|
262
|
-
sendTransactionNoCache = this.buildSender(
|
|
263
|
-
"send_transaction",
|
|
264
|
-
[JsonRpcTransformers.transactionFrom],
|
|
265
|
-
hexFrom,
|
|
266
|
-
) as (
|
|
267
|
-
transaction: TransactionLike,
|
|
268
|
-
validator?: OutputsValidator | undefined,
|
|
269
|
-
) => Promise<Hex>;
|
|
270
|
-
|
|
271
|
-
/**
|
|
272
|
-
* Get a transaction from node.
|
|
273
|
-
*
|
|
274
|
-
* @param txHash - The hash of the transaction.
|
|
275
|
-
* @returns The transaction with status.
|
|
276
|
-
*/
|
|
277
|
-
|
|
278
|
-
getTransactionNoCache = this.buildSender(
|
|
279
|
-
"get_transaction",
|
|
280
|
-
[hexFrom],
|
|
281
|
-
JsonRpcTransformers.transactionResponseTo,
|
|
282
|
-
) as (txHash: HexLike) => Promise<ClientTransactionResponse | undefined>;
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Get a live cell from node.
|
|
286
|
-
*
|
|
287
|
-
* @param outPoint - The out point of the cell.
|
|
288
|
-
* @param withData - Include data in the response.
|
|
289
|
-
* @param includeTxPool - Include cells in the tx pool.
|
|
290
|
-
* @returns The cell
|
|
291
|
-
*/
|
|
292
|
-
getCellLiveNoCache(
|
|
293
|
-
outPoint: OutPointLike,
|
|
294
|
-
withData?: boolean | null,
|
|
295
|
-
includeTxPool?: boolean | null,
|
|
296
|
-
) {
|
|
297
|
-
return this.buildSender(
|
|
298
|
-
"get_live_cell",
|
|
299
|
-
[JsonRpcTransformers.outPointFrom],
|
|
300
|
-
({
|
|
301
|
-
cell,
|
|
302
|
-
}: {
|
|
303
|
-
cell?: {
|
|
304
|
-
output: JsonRpcCellOutput;
|
|
305
|
-
data?: { content: HexLike; hash: HexLike };
|
|
306
|
-
};
|
|
307
|
-
}) =>
|
|
308
|
-
apply(
|
|
309
|
-
({
|
|
310
|
-
output,
|
|
311
|
-
data,
|
|
312
|
-
}: {
|
|
313
|
-
output: JsonRpcCellOutput;
|
|
314
|
-
data?: { content: HexLike; hash: HexLike };
|
|
315
|
-
}) =>
|
|
316
|
-
Cell.from({
|
|
317
|
-
cellOutput: JsonRpcTransformers.cellOutputTo(output),
|
|
318
|
-
outputData: data?.content ?? "0x",
|
|
319
|
-
outPoint,
|
|
320
|
-
}),
|
|
321
|
-
cell,
|
|
322
|
-
),
|
|
323
|
-
)(outPoint, withData ?? true, includeTxPool) as Promise<Cell | undefined>;
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* find cells from node.
|
|
328
|
-
*
|
|
329
|
-
* @param key - The search key of cells.
|
|
330
|
-
* @param order - The order of cells.
|
|
331
|
-
* @param limit - The max return size of cells.
|
|
332
|
-
* @param after - Pagination parameter.
|
|
333
|
-
* @returns The found cells.
|
|
334
|
-
*/
|
|
335
|
-
|
|
336
|
-
findCellsPagedNoCache = this.buildSender(
|
|
337
|
-
"get_cells",
|
|
338
|
-
[
|
|
339
|
-
JsonRpcTransformers.indexerSearchKeyFrom,
|
|
340
|
-
(order?: "asc" | "desc") => order ?? "asc",
|
|
341
|
-
(limit?: NumLike) => numToHex(limit ?? 10),
|
|
342
|
-
],
|
|
343
|
-
JsonRpcTransformers.findCellsResponseTo,
|
|
344
|
-
) as (
|
|
345
|
-
key: ClientIndexerSearchKeyLike,
|
|
346
|
-
order?: "asc" | "desc",
|
|
347
|
-
limit?: NumLike,
|
|
348
|
-
after?: string,
|
|
349
|
-
) => Promise<ClientFindCellsResponse>;
|
|
350
|
-
|
|
351
|
-
/**
|
|
352
|
-
* find transactions from node.
|
|
353
|
-
*
|
|
354
|
-
* @param key - The search key of transactions.
|
|
355
|
-
* @param order - The order of transactions.
|
|
356
|
-
* @param limit - The max return size of transactions.
|
|
357
|
-
* @param after - Pagination parameter.
|
|
358
|
-
* @returns The found transactions.
|
|
359
|
-
*/
|
|
360
|
-
|
|
361
|
-
findTransactionsPaged = this.buildSender(
|
|
362
|
-
"get_transactions",
|
|
363
|
-
[
|
|
364
|
-
JsonRpcTransformers.indexerSearchKeyTransactionFrom,
|
|
365
|
-
(order?: "asc" | "desc") => order ?? "asc",
|
|
366
|
-
(limit?: NumLike) => numToHex(limit ?? 10),
|
|
367
|
-
],
|
|
368
|
-
JsonRpcTransformers.findTransactionsResponseTo,
|
|
369
|
-
) as Client["findTransactionsPaged"];
|
|
370
|
-
|
|
371
|
-
/**
|
|
372
|
-
* get cells capacity from node.
|
|
373
|
-
*
|
|
374
|
-
* @param key - The search key of cells.
|
|
375
|
-
* @returns The sum of cells capacity.
|
|
376
|
-
*/
|
|
377
|
-
|
|
378
|
-
getCellsCapacity = this.buildSender(
|
|
379
|
-
"get_cells_capacity",
|
|
380
|
-
[JsonRpcTransformers.indexerSearchKeyFrom],
|
|
381
|
-
({ capacity }: { capacity: NumLike }) => numFrom(capacity),
|
|
382
|
-
) as (key: ClientIndexerSearchKeyLike) => Promise<Num>;
|
|
383
|
-
|
|
384
|
-
/**
|
|
385
|
-
* Builds a sender function for a JSON-RPC method.
|
|
386
|
-
*
|
|
387
|
-
* @param rpcMethod - The JSON-RPC method.
|
|
388
|
-
* @param inTransformers - An array of input transformers.
|
|
389
|
-
* @param outTransformer - An output transformer function.
|
|
390
|
-
* @returns A function that sends a JSON-RPC request with the given method and transformed parameters.
|
|
391
|
-
*/
|
|
392
|
-
|
|
393
|
-
buildSender(
|
|
394
|
-
rpcMethod: string,
|
|
395
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
396
|
-
inTransformers: (((_: any) => unknown) | undefined)[],
|
|
397
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
398
|
-
outTransformer?: (_: any) => unknown,
|
|
399
|
-
): (...req: unknown[]) => Promise<unknown> {
|
|
400
|
-
return async (...req: unknown[]) => {
|
|
401
|
-
const payload = this.buildPayload(
|
|
402
|
-
rpcMethod,
|
|
403
|
-
req
|
|
404
|
-
.concat(
|
|
405
|
-
Array.from(
|
|
406
|
-
new Array(Math.max(inTransformers.length - req.length, 0)),
|
|
407
|
-
),
|
|
408
|
-
)
|
|
409
|
-
.map((v, i) => transform(v, inTransformers[i])),
|
|
410
|
-
);
|
|
411
|
-
|
|
412
|
-
try {
|
|
413
|
-
return transform(await this.send(payload), outTransformer);
|
|
414
|
-
} catch (errAny: unknown) {
|
|
415
|
-
if (
|
|
416
|
-
typeof errAny !== "object" ||
|
|
417
|
-
errAny === null ||
|
|
418
|
-
!("data" in errAny) ||
|
|
419
|
-
typeof errAny.data !== "string"
|
|
420
|
-
) {
|
|
421
|
-
throw errAny;
|
|
422
|
-
}
|
|
423
|
-
const err = errAny as ErrorClientBaseLike;
|
|
424
|
-
|
|
425
|
-
for (const [regexp, builder] of ERROR_PARSERS) {
|
|
426
|
-
const match = err.data.match(regexp);
|
|
427
|
-
if (match) {
|
|
428
|
-
throw builder(err, match);
|
|
429
|
-
}
|
|
430
|
-
}
|
|
431
|
-
|
|
432
|
-
throw new ErrorClientBase(err);
|
|
433
|
-
}
|
|
434
|
-
};
|
|
435
|
-
}
|
|
436
|
-
|
|
437
|
-
/**
|
|
438
|
-
* Sends a JSON-RPC request to the server.
|
|
439
|
-
*
|
|
440
|
-
* @param payload - The JSON-RPC payload to send.
|
|
441
|
-
* @returns The result of the JSON-RPC request.
|
|
442
|
-
*
|
|
443
|
-
* @throws Will throw an error if the response ID does not match the request ID, or if the response contains an error.
|
|
444
|
-
*/
|
|
445
|
-
async send(payload: JsonRpcPayload): Promise<unknown> {
|
|
446
|
-
if (
|
|
447
|
-
this.maxConcurrent !== undefined &&
|
|
448
|
-
this.concurrent >= this.maxConcurrent
|
|
449
|
-
) {
|
|
450
|
-
const pending = new Promise<void>((resolve) =>
|
|
451
|
-
this.pending.push(resolve),
|
|
452
|
-
);
|
|
453
|
-
await pending;
|
|
454
|
-
}
|
|
455
|
-
|
|
456
|
-
this.concurrent += 1;
|
|
457
|
-
const res = (await this.transport.request(payload)) as {
|
|
458
|
-
id: number;
|
|
459
|
-
error: unknown;
|
|
460
|
-
result: unknown;
|
|
461
|
-
};
|
|
462
|
-
this.concurrent -= 1;
|
|
463
|
-
this.pending.shift()?.();
|
|
464
|
-
|
|
465
|
-
if (res.id !== payload.id) {
|
|
466
|
-
throw new Error(`Id mismatched, got ${res.id}, expected ${payload.id}`);
|
|
467
|
-
}
|
|
468
|
-
if (res.error) {
|
|
469
|
-
throw res.error;
|
|
470
|
-
}
|
|
471
|
-
return res.result;
|
|
472
|
-
}
|
|
473
|
-
|
|
474
|
-
/**
|
|
475
|
-
* Builds a JSON-RPC payload for the given method and parameters.
|
|
476
|
-
*
|
|
477
|
-
* @param method - The JSON-RPC method name.
|
|
478
|
-
* @param req - The parameters for the JSON-RPC method.
|
|
479
|
-
* @returns The JSON-RPC payload.
|
|
480
|
-
*/
|
|
481
|
-
|
|
482
|
-
buildPayload(method: string, req: unknown[]): JsonRpcPayload {
|
|
483
|
-
return {
|
|
484
|
-
id: this.id++,
|
|
485
|
-
method,
|
|
486
|
-
params: req,
|
|
487
|
-
jsonrpc: "2.0",
|
|
488
|
-
};
|
|
489
|
-
}
|
|
490
|
-
}
|
|
1
|
+
export * from "./client.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./transports/advanced.js";
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export * from "./requestor.js";
|
|
@@ -0,0 +1,157 @@
|
|
|
1
|
+
import {
|
|
2
|
+
JsonRpcPayload,
|
|
3
|
+
Transport,
|
|
4
|
+
transportFromUri,
|
|
5
|
+
} from "./transports/advanced.js";
|
|
6
|
+
|
|
7
|
+
/**
|
|
8
|
+
* Applies a transformation function to a value if the transformer is provided.
|
|
9
|
+
*
|
|
10
|
+
* @param value - The value to be transformed.
|
|
11
|
+
* @param transformer - An optional transformation function.
|
|
12
|
+
* @returns The transformed value if a transformer is provided, otherwise the original value.
|
|
13
|
+
*
|
|
14
|
+
* @example
|
|
15
|
+
* ```typescript
|
|
16
|
+
* const result = transform(5, (x) => x * 2); // Outputs 10
|
|
17
|
+
* const resultWithoutTransformer = transform(5); // Outputs 5
|
|
18
|
+
* ```
|
|
19
|
+
*/
|
|
20
|
+
function transform(value: unknown, transformer?: (i: unknown) => unknown) {
|
|
21
|
+
if (transformer) {
|
|
22
|
+
return transformer(value);
|
|
23
|
+
}
|
|
24
|
+
return value;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
export type RequestorJsonRpcConfig = {
|
|
28
|
+
timeout?: number;
|
|
29
|
+
maxConcurrent?: number;
|
|
30
|
+
transport?: Transport;
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
export class RequestorJsonRpc {
|
|
34
|
+
public readonly maxConcurrent?: number;
|
|
35
|
+
private concurrent = 0;
|
|
36
|
+
private readonly pending: (() => void)[] = [];
|
|
37
|
+
|
|
38
|
+
public readonly transport: Transport;
|
|
39
|
+
|
|
40
|
+
private id = 0;
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* Creates an instance of ClientJsonRpc.
|
|
44
|
+
*
|
|
45
|
+
* @param url_ - The URL of the JSON-RPC server.
|
|
46
|
+
* @param timeout - The timeout for requests in milliseconds
|
|
47
|
+
*/
|
|
48
|
+
constructor(
|
|
49
|
+
private readonly url_: string,
|
|
50
|
+
config?: RequestorJsonRpcConfig,
|
|
51
|
+
private readonly onError?: (err: unknown) => Promise<void> | void,
|
|
52
|
+
) {
|
|
53
|
+
this.maxConcurrent = config?.maxConcurrent;
|
|
54
|
+
this.transport = config?.transport ?? transportFromUri(url_, config);
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Returns the URL of the JSON-RPC server.
|
|
59
|
+
*
|
|
60
|
+
* @returns The URL of the JSON-RPC server.
|
|
61
|
+
*/
|
|
62
|
+
|
|
63
|
+
get url(): string {
|
|
64
|
+
return this.url_;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
/**
|
|
68
|
+
* request a JSON-RPC method.
|
|
69
|
+
*
|
|
70
|
+
* @param rpcMethod - The JSON-RPC method.
|
|
71
|
+
* @param params - Params for the method.
|
|
72
|
+
* @param inTransformers - An array of input transformers.
|
|
73
|
+
* @param outTransformer - An output transformer function.
|
|
74
|
+
* @returns Method response.
|
|
75
|
+
*/
|
|
76
|
+
async request(
|
|
77
|
+
rpcMethod: string,
|
|
78
|
+
params: unknown[],
|
|
79
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
80
|
+
inTransformers?: (((_: any) => unknown) | undefined)[],
|
|
81
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
82
|
+
outTransformer?: (_: any) => unknown,
|
|
83
|
+
): Promise<unknown> {
|
|
84
|
+
const payload = this.buildPayload(
|
|
85
|
+
rpcMethod,
|
|
86
|
+
inTransformers
|
|
87
|
+
? await Promise.all(
|
|
88
|
+
params
|
|
89
|
+
.concat(
|
|
90
|
+
Array.from(
|
|
91
|
+
new Array(Math.max(inTransformers.length - params.length, 0)),
|
|
92
|
+
),
|
|
93
|
+
)
|
|
94
|
+
.map((v, i) => transform(v, inTransformers[i])),
|
|
95
|
+
)
|
|
96
|
+
: params,
|
|
97
|
+
);
|
|
98
|
+
|
|
99
|
+
try {
|
|
100
|
+
return await transform(
|
|
101
|
+
await this.requestPayload(payload),
|
|
102
|
+
outTransformer,
|
|
103
|
+
);
|
|
104
|
+
} catch (err: unknown) {
|
|
105
|
+
if (!this.onError) {
|
|
106
|
+
throw err;
|
|
107
|
+
}
|
|
108
|
+
await this.onError(err);
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
async requestPayload(payload: JsonRpcPayload): Promise<unknown> {
|
|
113
|
+
if (
|
|
114
|
+
this.maxConcurrent !== undefined &&
|
|
115
|
+
this.concurrent >= this.maxConcurrent
|
|
116
|
+
) {
|
|
117
|
+
const pending = new Promise<void>((resolve) =>
|
|
118
|
+
this.pending.push(resolve),
|
|
119
|
+
);
|
|
120
|
+
await pending;
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
this.concurrent += 1;
|
|
124
|
+
const res = (await this.transport.request(payload)) as {
|
|
125
|
+
id: number;
|
|
126
|
+
error: unknown;
|
|
127
|
+
result: unknown;
|
|
128
|
+
};
|
|
129
|
+
this.concurrent -= 1;
|
|
130
|
+
this.pending.shift()?.();
|
|
131
|
+
|
|
132
|
+
if (res.id !== payload.id) {
|
|
133
|
+
throw new Error(`Id mismatched, got ${res.id}, expected ${payload.id}`);
|
|
134
|
+
}
|
|
135
|
+
if (res.error) {
|
|
136
|
+
throw res.error;
|
|
137
|
+
}
|
|
138
|
+
return res.result;
|
|
139
|
+
}
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* Builds a JSON-RPC payload for the given method and parameters.
|
|
143
|
+
*
|
|
144
|
+
* @param method - The JSON-RPC method name.
|
|
145
|
+
* @param req - The parameters for the JSON-RPC method.
|
|
146
|
+
* @returns The JSON-RPC payload.
|
|
147
|
+
*/
|
|
148
|
+
|
|
149
|
+
buildPayload(method: string, req: unknown[]): JsonRpcPayload {
|
|
150
|
+
return {
|
|
151
|
+
id: this.id++,
|
|
152
|
+
method,
|
|
153
|
+
params: req,
|
|
154
|
+
jsonrpc: "2.0",
|
|
155
|
+
};
|
|
156
|
+
}
|
|
157
|
+
}
|
|
@@ -1,10 +1,6 @@
|
|
|
1
1
|
import { TransportHttp } from "./http.js";
|
|
2
2
|
import { TransportWebSocket } from "./webSocket.js";
|
|
3
3
|
|
|
4
|
-
export * from "./http.js";
|
|
5
|
-
export * from "./transport.js";
|
|
6
|
-
export * from "./webSocket.js";
|
|
7
|
-
|
|
8
4
|
export function transportFromUri(uri: string, config?: { timeout?: number }) {
|
|
9
5
|
if (uri.startsWith("wss://") || uri.startsWith("ws://")) {
|
|
10
6
|
return new TransportWebSocket(uri, config?.timeout);
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
export type JsonRpcPayload = {
|
|
2
|
+
id: number;
|
|
3
|
+
jsonrpc: "2.0";
|
|
4
|
+
method: string;
|
|
5
|
+
params: unknown[] | Record<string, unknown>;
|
|
6
|
+
};
|
|
7
|
+
|
|
8
|
+
export interface Transport {
|
|
9
|
+
/**
|
|
10
|
+
* Sends a JSON-RPC request to the server.
|
|
11
|
+
*
|
|
12
|
+
* @param payload - The JSON-RPC payload to send.
|
|
13
|
+
* @returns The result of the JSON-RPC request.
|
|
14
|
+
*
|
|
15
|
+
* @throws Will throw an error if the response ID does not match the request ID, or if the response contains an error.
|
|
16
|
+
*/
|
|
17
|
+
request(data: JsonRpcPayload): Promise<unknown>;
|
|
18
|
+
}
|