@psalomo/jsonrpc-client 1.0.3 → 1.2.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/README.md +69 -19
- package/dist/browser-standalone.js +804 -37
- package/dist/browser-standalone.min.js +1 -1
- package/dist/chunk-GCIU6ZLN.mjs +395 -0
- package/dist/client.d.ts.map +1 -1
- package/dist/convenience-DFvZCHOk.d.mts +212 -0
- package/dist/convenience-DFvZCHOk.d.ts +212 -0
- package/dist/convenience.d.ts +9 -1
- package/dist/convenience.d.ts.map +1 -1
- package/dist/generated-types.d.ts +1 -1
- package/dist/index.d.mts +23 -172
- package/dist/index.d.ts +3 -3
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +646 -51
- package/dist/index.mjs +684 -331
- package/dist/no-validation/index.d.mts +6 -0
- package/dist/no-validation/index.d.ts +9 -0
- package/dist/no-validation/index.d.ts.map +1 -0
- package/dist/no-validation/index.js +471 -0
- package/dist/no-validation/index.mjs +95 -0
- package/dist/validated/index.d.ts +54 -0
- package/dist/validated/index.d.ts.map +1 -0
- package/dist/validation.d.ts.map +1 -1
- package/package.json +7 -2
@@ -0,0 +1,395 @@
|
|
1
|
+
// src/client.ts
|
2
|
+
function camelToSnake(str) {
|
3
|
+
return str.replace(/[A-Z]/g, (letter) => `_${letter.toLowerCase()}`);
|
4
|
+
}
|
5
|
+
function snakeToCamel(str) {
|
6
|
+
return str.replace(/_([a-z])/g, (_, letter) => letter.toUpperCase());
|
7
|
+
}
|
8
|
+
function convertKeysToSnakeCase(obj) {
|
9
|
+
if (obj === null || typeof obj !== "object") {
|
10
|
+
return obj;
|
11
|
+
}
|
12
|
+
if (Array.isArray(obj)) {
|
13
|
+
return obj.map(convertKeysToSnakeCase);
|
14
|
+
}
|
15
|
+
const converted = {};
|
16
|
+
for (const [key, value] of Object.entries(obj)) {
|
17
|
+
const snakeKey = camelToSnake(key);
|
18
|
+
converted[snakeKey] = convertKeysToSnakeCase(value);
|
19
|
+
}
|
20
|
+
return converted;
|
21
|
+
}
|
22
|
+
function convertKeysToCamelCase(obj) {
|
23
|
+
if (obj === null || typeof obj !== "object") {
|
24
|
+
return obj;
|
25
|
+
}
|
26
|
+
if (Array.isArray(obj)) {
|
27
|
+
return obj.map(convertKeysToCamelCase);
|
28
|
+
}
|
29
|
+
const converted = {};
|
30
|
+
for (const [key, value] of Object.entries(obj)) {
|
31
|
+
const camelKey = snakeToCamel(key);
|
32
|
+
converted[camelKey] = convertKeysToCamelCase(value);
|
33
|
+
}
|
34
|
+
return converted;
|
35
|
+
}
|
36
|
+
var REQUEST_ID = "dontcare";
|
37
|
+
var JsonRpcClientError = class extends Error {
|
38
|
+
constructor(message, code, data) {
|
39
|
+
super(message);
|
40
|
+
this.code = code;
|
41
|
+
this.data = data;
|
42
|
+
this.name = "JsonRpcClientError";
|
43
|
+
}
|
44
|
+
};
|
45
|
+
var JsonRpcNetworkError = class extends Error {
|
46
|
+
constructor(message, originalError, responseBody) {
|
47
|
+
super(message);
|
48
|
+
this.originalError = originalError;
|
49
|
+
this.responseBody = responseBody;
|
50
|
+
this.name = "JsonRpcNetworkError";
|
51
|
+
}
|
52
|
+
};
|
53
|
+
var NearRpcClient = class _NearRpcClient {
|
54
|
+
endpoint;
|
55
|
+
headers;
|
56
|
+
timeout;
|
57
|
+
retries;
|
58
|
+
validation;
|
59
|
+
constructor(config) {
|
60
|
+
if (typeof config === "string") {
|
61
|
+
this.endpoint = config;
|
62
|
+
this.headers = {};
|
63
|
+
this.timeout = 3e4;
|
64
|
+
this.retries = 3;
|
65
|
+
} else {
|
66
|
+
this.endpoint = config.endpoint;
|
67
|
+
this.headers = config.headers || {};
|
68
|
+
this.timeout = config.timeout || 3e4;
|
69
|
+
this.retries = config.retries || 3;
|
70
|
+
if (config.validation) {
|
71
|
+
this.validation = config.validation;
|
72
|
+
}
|
73
|
+
}
|
74
|
+
}
|
75
|
+
/**
|
76
|
+
* Make a raw JSON-RPC request
|
77
|
+
* This is used internally by the standalone RPC functions
|
78
|
+
*/
|
79
|
+
async makeRequest(method, params) {
|
80
|
+
const requestForValidation = {
|
81
|
+
jsonrpc: "2.0",
|
82
|
+
id: REQUEST_ID,
|
83
|
+
method,
|
84
|
+
params: params !== void 0 ? params : null
|
85
|
+
};
|
86
|
+
if (this.validation) {
|
87
|
+
if ("validateMethodRequest" in this.validation) {
|
88
|
+
this.validation.validateMethodRequest(method, requestForValidation);
|
89
|
+
} else {
|
90
|
+
this.validation.validateRequest(requestForValidation);
|
91
|
+
}
|
92
|
+
}
|
93
|
+
const snakeCaseParams = params !== void 0 ? convertKeysToSnakeCase(params) : null;
|
94
|
+
const request = {
|
95
|
+
jsonrpc: "2.0",
|
96
|
+
id: REQUEST_ID,
|
97
|
+
method,
|
98
|
+
params: snakeCaseParams
|
99
|
+
};
|
100
|
+
let lastError = null;
|
101
|
+
for (let attempt = 0; attempt <= this.retries; attempt++) {
|
102
|
+
try {
|
103
|
+
const controller = new AbortController();
|
104
|
+
const timeoutId = setTimeout(() => controller.abort(), this.timeout);
|
105
|
+
const response = await fetch(this.endpoint, {
|
106
|
+
method: "POST",
|
107
|
+
headers: {
|
108
|
+
"Content-Type": "application/json",
|
109
|
+
...this.headers
|
110
|
+
},
|
111
|
+
body: JSON.stringify(request),
|
112
|
+
signal: controller.signal
|
113
|
+
});
|
114
|
+
clearTimeout(timeoutId);
|
115
|
+
let jsonResponse;
|
116
|
+
try {
|
117
|
+
jsonResponse = await response.json();
|
118
|
+
} catch (parseError) {
|
119
|
+
if (!response.ok) {
|
120
|
+
throw new JsonRpcNetworkError(
|
121
|
+
`HTTP error! status: ${response.status} - Failed to parse JSON response`,
|
122
|
+
parseError
|
123
|
+
);
|
124
|
+
}
|
125
|
+
throw new JsonRpcNetworkError(
|
126
|
+
"Failed to parse JSON response",
|
127
|
+
parseError
|
128
|
+
);
|
129
|
+
}
|
130
|
+
if (jsonResponse.error) {
|
131
|
+
throw new JsonRpcClientError(
|
132
|
+
jsonResponse.error.message,
|
133
|
+
jsonResponse.error.code,
|
134
|
+
jsonResponse.error.data
|
135
|
+
);
|
136
|
+
}
|
137
|
+
if (!response.ok) {
|
138
|
+
throw new JsonRpcNetworkError(
|
139
|
+
`HTTP error! status: ${response.status}`,
|
140
|
+
void 0,
|
141
|
+
jsonResponse
|
142
|
+
);
|
143
|
+
}
|
144
|
+
if (this.validation) {
|
145
|
+
this.validation.validateResponse(jsonResponse);
|
146
|
+
}
|
147
|
+
const camelCaseResult = jsonResponse.result ? convertKeysToCamelCase(jsonResponse.result) : jsonResponse.result;
|
148
|
+
if (camelCaseResult && typeof camelCaseResult === "object" && "error" in camelCaseResult) {
|
149
|
+
const errorMessage = camelCaseResult.error;
|
150
|
+
throw new JsonRpcClientError(
|
151
|
+
`RPC Error: ${errorMessage}`,
|
152
|
+
-32e3,
|
153
|
+
// Generic RPC error code
|
154
|
+
camelCaseResult
|
155
|
+
);
|
156
|
+
}
|
157
|
+
if (this.validation && "validateMethodResponse" in this.validation) {
|
158
|
+
const camelCaseResponse = {
|
159
|
+
...jsonResponse,
|
160
|
+
result: camelCaseResult
|
161
|
+
};
|
162
|
+
this.validation.validateMethodResponse(method, camelCaseResponse);
|
163
|
+
}
|
164
|
+
return camelCaseResult;
|
165
|
+
} catch (error) {
|
166
|
+
lastError = error;
|
167
|
+
if (error instanceof JsonRpcClientError) {
|
168
|
+
throw error;
|
169
|
+
}
|
170
|
+
if (attempt === this.retries) {
|
171
|
+
break;
|
172
|
+
}
|
173
|
+
await new Promise(
|
174
|
+
(resolve) => setTimeout(resolve, Math.pow(2, attempt) * 1e3)
|
175
|
+
);
|
176
|
+
}
|
177
|
+
}
|
178
|
+
throw new JsonRpcNetworkError(
|
179
|
+
lastError?.message || "Request failed after all retries",
|
180
|
+
lastError || void 0
|
181
|
+
);
|
182
|
+
}
|
183
|
+
/**
|
184
|
+
* Create a new client with modified configuration
|
185
|
+
*/
|
186
|
+
withConfig(config) {
|
187
|
+
return new _NearRpcClient({
|
188
|
+
endpoint: config.endpoint ?? this.endpoint,
|
189
|
+
headers: config.headers ?? this.headers,
|
190
|
+
timeout: config.timeout ?? this.timeout,
|
191
|
+
retries: config.retries ?? this.retries,
|
192
|
+
...config.validation !== void 0 ? { validation: config.validation } : this.validation !== void 0 ? { validation: this.validation } : {}
|
193
|
+
});
|
194
|
+
}
|
195
|
+
};
|
196
|
+
var defaultClient = new NearRpcClient({
|
197
|
+
endpoint: "https://rpc.mainnet.near.org"
|
198
|
+
});
|
199
|
+
|
200
|
+
// src/types.ts
|
201
|
+
var NearRpcError = class extends Error {
|
202
|
+
constructor(code, message, data) {
|
203
|
+
super(message);
|
204
|
+
this.code = code;
|
205
|
+
this.data = data;
|
206
|
+
this.name = "NearRpcError";
|
207
|
+
}
|
208
|
+
};
|
209
|
+
|
210
|
+
// src/generated-types.ts
|
211
|
+
async function experimentalChanges(client, params) {
|
212
|
+
return client.makeRequest("EXPERIMENTAL_changes", params);
|
213
|
+
}
|
214
|
+
async function experimentalChangesInBlock(client, params) {
|
215
|
+
return client.makeRequest("EXPERIMENTAL_changes_in_block", params);
|
216
|
+
}
|
217
|
+
async function experimentalCongestionLevel(client, params) {
|
218
|
+
return client.makeRequest("EXPERIMENTAL_congestion_level", params);
|
219
|
+
}
|
220
|
+
async function experimentalGenesisConfig(client, params) {
|
221
|
+
return client.makeRequest("EXPERIMENTAL_genesis_config", params);
|
222
|
+
}
|
223
|
+
async function experimentalLightClientBlockProof(client, params) {
|
224
|
+
return client.makeRequest("EXPERIMENTAL_light_client_block_proof", params);
|
225
|
+
}
|
226
|
+
async function experimentalLightClientProof(client, params) {
|
227
|
+
return client.makeRequest("EXPERIMENTAL_light_client_proof", params);
|
228
|
+
}
|
229
|
+
async function experimentalMaintenanceWindows(client, params) {
|
230
|
+
return client.makeRequest("EXPERIMENTAL_maintenance_windows", params);
|
231
|
+
}
|
232
|
+
async function experimentalProtocolConfig(client, params) {
|
233
|
+
return client.makeRequest("EXPERIMENTAL_protocol_config", params);
|
234
|
+
}
|
235
|
+
async function experimentalReceipt(client, params) {
|
236
|
+
return client.makeRequest("EXPERIMENTAL_receipt", params);
|
237
|
+
}
|
238
|
+
async function experimentalSplitStorageInfo(client, params) {
|
239
|
+
return client.makeRequest("EXPERIMENTAL_split_storage_info", params);
|
240
|
+
}
|
241
|
+
async function experimentalTxStatus(client, params) {
|
242
|
+
return client.makeRequest("EXPERIMENTAL_tx_status", params);
|
243
|
+
}
|
244
|
+
async function experimentalValidatorsOrdered(client, params) {
|
245
|
+
return client.makeRequest("EXPERIMENTAL_validators_ordered", params);
|
246
|
+
}
|
247
|
+
async function block(client, params) {
|
248
|
+
return client.makeRequest("block", params);
|
249
|
+
}
|
250
|
+
async function blockEffects(client, params) {
|
251
|
+
return client.makeRequest("block_effects", params);
|
252
|
+
}
|
253
|
+
async function broadcastTxAsync(client, params) {
|
254
|
+
return client.makeRequest("broadcast_tx_async", params);
|
255
|
+
}
|
256
|
+
async function broadcastTxCommit(client, params) {
|
257
|
+
return client.makeRequest("broadcast_tx_commit", params);
|
258
|
+
}
|
259
|
+
async function changes(client, params) {
|
260
|
+
return client.makeRequest("changes", params);
|
261
|
+
}
|
262
|
+
async function chunk(client, params) {
|
263
|
+
return client.makeRequest("chunk", params);
|
264
|
+
}
|
265
|
+
async function clientConfig(client, params) {
|
266
|
+
return client.makeRequest("client_config", params);
|
267
|
+
}
|
268
|
+
async function gasPrice(client, params) {
|
269
|
+
return client.makeRequest("gas_price", params);
|
270
|
+
}
|
271
|
+
async function genesisConfig(client, params) {
|
272
|
+
return client.makeRequest("genesis_config", params);
|
273
|
+
}
|
274
|
+
async function health(client, params) {
|
275
|
+
return client.makeRequest("health", params);
|
276
|
+
}
|
277
|
+
async function lightClientProof(client, params) {
|
278
|
+
return client.makeRequest("light_client_proof", params);
|
279
|
+
}
|
280
|
+
async function maintenanceWindows(client, params) {
|
281
|
+
return client.makeRequest("maintenance_windows", params);
|
282
|
+
}
|
283
|
+
async function networkInfo(client, params) {
|
284
|
+
return client.makeRequest("network_info", params);
|
285
|
+
}
|
286
|
+
async function nextLightClientBlock(client, params) {
|
287
|
+
return client.makeRequest("next_light_client_block", params);
|
288
|
+
}
|
289
|
+
async function query(client, params) {
|
290
|
+
return client.makeRequest("query", params);
|
291
|
+
}
|
292
|
+
async function sendTx(client, params) {
|
293
|
+
return client.makeRequest("send_tx", params);
|
294
|
+
}
|
295
|
+
async function status(client, params) {
|
296
|
+
return client.makeRequest("status", params);
|
297
|
+
}
|
298
|
+
async function tx(client, params) {
|
299
|
+
return client.makeRequest("tx", params);
|
300
|
+
}
|
301
|
+
async function validators(client, params) {
|
302
|
+
return client.makeRequest("validators", params);
|
303
|
+
}
|
304
|
+
|
305
|
+
// src/convenience.ts
|
306
|
+
async function viewAccount(client, params) {
|
307
|
+
const queryParams = params.blockId ? {
|
308
|
+
requestType: "view_account",
|
309
|
+
accountId: params.accountId,
|
310
|
+
blockId: params.blockId
|
311
|
+
} : {
|
312
|
+
requestType: "view_account",
|
313
|
+
accountId: params.accountId,
|
314
|
+
finality: params.finality || "final"
|
315
|
+
};
|
316
|
+
return query(client, queryParams);
|
317
|
+
}
|
318
|
+
async function viewFunction(client, params) {
|
319
|
+
const baseParams = {
|
320
|
+
requestType: "call_function",
|
321
|
+
accountId: params.accountId,
|
322
|
+
methodName: params.methodName,
|
323
|
+
argsBase64: params.argsBase64 ?? ""
|
324
|
+
// Default to empty string if no arguments
|
325
|
+
};
|
326
|
+
const queryParams = params.blockId ? { ...baseParams, blockId: params.blockId } : { ...baseParams, finality: params.finality || "final" };
|
327
|
+
return query(client, queryParams);
|
328
|
+
}
|
329
|
+
async function viewAccessKey(client, params) {
|
330
|
+
const queryParams = params.blockId ? {
|
331
|
+
requestType: "view_access_key",
|
332
|
+
accountId: params.accountId,
|
333
|
+
publicKey: params.publicKey,
|
334
|
+
blockId: params.blockId
|
335
|
+
} : {
|
336
|
+
requestType: "view_access_key",
|
337
|
+
accountId: params.accountId,
|
338
|
+
publicKey: params.publicKey,
|
339
|
+
finality: params.finality || "final"
|
340
|
+
};
|
341
|
+
return query(client, queryParams);
|
342
|
+
}
|
343
|
+
function parseCallResultToJson(callResult) {
|
344
|
+
const bytes = new Uint8Array(callResult.result);
|
345
|
+
const text = new TextDecoder().decode(bytes);
|
346
|
+
return JSON.parse(text);
|
347
|
+
}
|
348
|
+
async function viewFunctionAsJson(client, params) {
|
349
|
+
const result = await viewFunction(client, params);
|
350
|
+
return parseCallResultToJson(result);
|
351
|
+
}
|
352
|
+
|
353
|
+
export {
|
354
|
+
JsonRpcClientError,
|
355
|
+
JsonRpcNetworkError,
|
356
|
+
NearRpcClient,
|
357
|
+
defaultClient,
|
358
|
+
NearRpcError,
|
359
|
+
experimentalChanges,
|
360
|
+
experimentalChangesInBlock,
|
361
|
+
experimentalCongestionLevel,
|
362
|
+
experimentalGenesisConfig,
|
363
|
+
experimentalLightClientBlockProof,
|
364
|
+
experimentalLightClientProof,
|
365
|
+
experimentalMaintenanceWindows,
|
366
|
+
experimentalProtocolConfig,
|
367
|
+
experimentalReceipt,
|
368
|
+
experimentalSplitStorageInfo,
|
369
|
+
experimentalTxStatus,
|
370
|
+
experimentalValidatorsOrdered,
|
371
|
+
block,
|
372
|
+
blockEffects,
|
373
|
+
broadcastTxAsync,
|
374
|
+
broadcastTxCommit,
|
375
|
+
changes,
|
376
|
+
chunk,
|
377
|
+
clientConfig,
|
378
|
+
gasPrice,
|
379
|
+
genesisConfig,
|
380
|
+
health,
|
381
|
+
lightClientProof,
|
382
|
+
maintenanceWindows,
|
383
|
+
networkInfo,
|
384
|
+
nextLightClientBlock,
|
385
|
+
query,
|
386
|
+
sendTx,
|
387
|
+
status,
|
388
|
+
tx,
|
389
|
+
validators,
|
390
|
+
viewAccount,
|
391
|
+
viewFunction,
|
392
|
+
viewAccessKey,
|
393
|
+
parseCallResultToJson,
|
394
|
+
viewFunctionAsJson
|
395
|
+
};
|
package/dist/client.d.ts.map
CHANGED
@@ -1 +1 @@
|
|
1
|
-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,IAAI,EAAE,MAAM,CAAC;IACb,OAAO,EAAE,MAAM,CAAC;IAChB,IAAI,CAAC,EAAE,OAAO,CAAC;CAChB;AAGD,qBAAa,kBAAmB,SAAQ,KAAK;IAGlC,IAAI,CAAC,EAAE,MAAM;IACb,IAAI,CAAC,EAAE,OAAO;gBAFrB,OAAO,EAAE,MAAM,EACR,IAAI,CAAC,EAAE,MAAM,YAAA,EACb,IAAI,CAAC,EAAE,OAAO,YAAA;CAKxB;AAED,qBAAa,mBAAoB,SAAQ,KAAK;IAGnC,aAAa,CAAC,EAAE,KAAK;IACrB,YAAY,CAAC,EAAE,OAAO;gBAF7B,OAAO,EAAE,MAAM,EACR,aAAa,CAAC,EAAE,KAAK,YAAA,EACrB,YAAY,CAAC,EAAE,OAAO,YAAA;CAKhC;AAED;;;;GAIG;AACH,qBAAa,aAAa;IACxB,SAAgB,QAAQ,EAAE,MAAM,CAAC;IACjC,SAAgB,OAAO,EAAE,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC;IAChD,SAAgB,OAAO,EAAE,MAAM,CAAC;IAChC,SAAgB,OAAO,EAAE,MAAM,CAAC;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAA+B;gBAE9C,MAAM,EAAE,MAAM,GAAG,YAAY;IAiBzC;;;OAGG;IACG,WAAW,CAAC,OAAO,GAAG,OAAO,EAAE,OAAO,GAAG,OAAO,EACpD,MAAM,EAAE,MAAM,EACd,MAAM,CAAC,EAAE,OAAO,GACf,OAAO,CAAC,OAAO,CAAC;IAkJnB;;OAEG;IACH,UAAU,CAAC,MAAM,EAAE,OAAO,CAAC,YAAY,CAAC,GAAG,aAAa;CAazD;AAGD,eAAO,MAAM,aAAa,eAExB,CAAC"}
|
@@ -0,0 +1,212 @@
|
|
1
|
+
import { RpcStateChangesInBlockByTypeRequest, RpcStateChangesInBlockResponse, RpcStateChangesInBlockRequest, RpcStateChangesInBlockByTypeResponse, RpcCongestionLevelRequest, RpcCongestionLevelResponse, GenesisConfigRequest, GenesisConfig, RpcLightClientBlockProofRequest, RpcLightClientBlockProofResponse, RpcLightClientExecutionProofRequest, RpcLightClientExecutionProofResponse, RpcMaintenanceWindowsRequest, EXPERIMENTALMaintenanceWindowsResponse, RpcProtocolConfigRequest, RpcProtocolConfigResponse, RpcReceiptRequest, RpcReceiptResponse, RpcSplitStorageInfoRequest, RpcSplitStorageInfoResponse, RpcTransactionStatusRequest, RpcTransactionResponse, RpcValidatorsOrderedRequest, EXPERIMENTALValidatorsOrderedResponse, RpcBlockRequest, RpcBlockResponse, RpcSendTransactionRequest, CryptoHash, RpcChunkRequest, RpcChunkResponse, RpcClientConfigRequest, RpcClientConfigResponse, RpcGasPriceRequest, RpcGasPriceResponse, RpcHealthRequest, RpcHealthResponse, MaintenanceWindowsResponse, RpcNetworkInfoRequest, RpcNetworkInfoResponse, RpcLightClientNextBlockRequest, RpcLightClientNextBlockResponse, RpcQueryRequest, RpcQueryResponse, RpcStatusRequest, RpcStatusResponse, RpcValidatorRequest, RpcValidatorResponse, AccountView, CallResult, AccessKeyView } from '@psalomo/jsonrpc-types';
|
2
|
+
|
3
|
+
interface ValidationResult {
|
4
|
+
validateRequest: (request: JsonRpcRequest) => void;
|
5
|
+
validateResponse: (response: JsonRpcResponse) => void;
|
6
|
+
validateMethodRequest?: (method: string, request: JsonRpcRequest) => void;
|
7
|
+
validateMethodResponse?: (method: string, response: JsonRpcResponse) => void;
|
8
|
+
}
|
9
|
+
/**
|
10
|
+
* Enable validation for the client
|
11
|
+
* This function should only be called if you want to include schema validation
|
12
|
+
* Calling this function will include Zod schemas in your bundle
|
13
|
+
*/
|
14
|
+
declare function enableValidation(): ValidationResult;
|
15
|
+
|
16
|
+
interface ClientConfig {
|
17
|
+
endpoint: string;
|
18
|
+
headers?: Record<string, string>;
|
19
|
+
timeout?: number;
|
20
|
+
retries?: number;
|
21
|
+
validation?: ValidationResult;
|
22
|
+
}
|
23
|
+
interface JsonRpcRequest<T = unknown> {
|
24
|
+
jsonrpc: '2.0';
|
25
|
+
id: string;
|
26
|
+
method: string;
|
27
|
+
params?: T;
|
28
|
+
}
|
29
|
+
interface JsonRpcResponse<T = unknown> {
|
30
|
+
jsonrpc: '2.0';
|
31
|
+
id: string;
|
32
|
+
result?: T;
|
33
|
+
error?: JsonRpcError;
|
34
|
+
}
|
35
|
+
interface JsonRpcError {
|
36
|
+
code: number;
|
37
|
+
message: string;
|
38
|
+
data?: unknown;
|
39
|
+
}
|
40
|
+
declare class JsonRpcClientError extends Error {
|
41
|
+
code?: number | undefined;
|
42
|
+
data?: unknown | undefined;
|
43
|
+
constructor(message: string, code?: number | undefined, data?: unknown | undefined);
|
44
|
+
}
|
45
|
+
declare class JsonRpcNetworkError extends Error {
|
46
|
+
originalError?: Error | undefined;
|
47
|
+
responseBody?: unknown | undefined;
|
48
|
+
constructor(message: string, originalError?: Error | undefined, responseBody?: unknown | undefined);
|
49
|
+
}
|
50
|
+
/**
|
51
|
+
* NEAR RPC Client with static function architecture
|
52
|
+
* This client only holds configuration and provides a makeRequest method
|
53
|
+
* Individual RPC methods are provided as standalone functions that take this client as a parameter
|
54
|
+
*/
|
55
|
+
declare class NearRpcClient {
|
56
|
+
readonly endpoint: string;
|
57
|
+
readonly headers: Record<string, string>;
|
58
|
+
readonly timeout: number;
|
59
|
+
readonly retries: number;
|
60
|
+
private readonly validation;
|
61
|
+
constructor(config: string | ClientConfig);
|
62
|
+
/**
|
63
|
+
* Make a raw JSON-RPC request
|
64
|
+
* This is used internally by the standalone RPC functions
|
65
|
+
*/
|
66
|
+
makeRequest<TParams = unknown, TResult = unknown>(method: string, params?: TParams): Promise<TResult>;
|
67
|
+
/**
|
68
|
+
* Create a new client with modified configuration
|
69
|
+
*/
|
70
|
+
withConfig(config: Partial<ClientConfig>): NearRpcClient;
|
71
|
+
}
|
72
|
+
declare const defaultClient: NearRpcClient;
|
73
|
+
|
74
|
+
interface RpcRequest {
|
75
|
+
jsonrpc: '2.0';
|
76
|
+
id: string | number;
|
77
|
+
method: string;
|
78
|
+
params: unknown;
|
79
|
+
}
|
80
|
+
interface RpcResponse<T = unknown> {
|
81
|
+
jsonrpc: '2.0';
|
82
|
+
id: string | number;
|
83
|
+
result?: T;
|
84
|
+
error?: RpcError;
|
85
|
+
}
|
86
|
+
interface RpcError {
|
87
|
+
code: number;
|
88
|
+
message: string;
|
89
|
+
data?: unknown;
|
90
|
+
}
|
91
|
+
declare class NearRpcError extends Error {
|
92
|
+
code: number;
|
93
|
+
data?: unknown | undefined;
|
94
|
+
constructor(code: number, message: string, data?: unknown | undefined);
|
95
|
+
}
|
96
|
+
|
97
|
+
interface DynamicRpcMethods {
|
98
|
+
experimentalChanges(params?: RpcStateChangesInBlockByTypeRequest): Promise<RpcStateChangesInBlockResponse>;
|
99
|
+
experimentalChangesInBlock(params?: RpcStateChangesInBlockRequest): Promise<RpcStateChangesInBlockByTypeResponse>;
|
100
|
+
experimentalCongestionLevel(params?: RpcCongestionLevelRequest): Promise<RpcCongestionLevelResponse>;
|
101
|
+
experimentalGenesisConfig(params?: GenesisConfigRequest): Promise<GenesisConfig>;
|
102
|
+
experimentalLightClientBlockProof(params?: RpcLightClientBlockProofRequest): Promise<RpcLightClientBlockProofResponse>;
|
103
|
+
experimentalLightClientProof(params?: RpcLightClientExecutionProofRequest): Promise<RpcLightClientExecutionProofResponse>;
|
104
|
+
experimentalMaintenanceWindows(params?: RpcMaintenanceWindowsRequest): Promise<EXPERIMENTALMaintenanceWindowsResponse>;
|
105
|
+
experimentalProtocolConfig(params?: RpcProtocolConfigRequest): Promise<RpcProtocolConfigResponse>;
|
106
|
+
experimentalReceipt(params?: RpcReceiptRequest): Promise<RpcReceiptResponse>;
|
107
|
+
experimentalSplitStorageInfo(params?: RpcSplitStorageInfoRequest): Promise<RpcSplitStorageInfoResponse>;
|
108
|
+
experimentalTxStatus(params?: RpcTransactionStatusRequest): Promise<RpcTransactionResponse>;
|
109
|
+
experimentalValidatorsOrdered(params?: RpcValidatorsOrderedRequest): Promise<EXPERIMENTALValidatorsOrderedResponse>;
|
110
|
+
block(params?: RpcBlockRequest): Promise<RpcBlockResponse>;
|
111
|
+
blockEffects(params?: RpcStateChangesInBlockRequest): Promise<RpcStateChangesInBlockByTypeResponse>;
|
112
|
+
broadcastTxAsync(params?: RpcSendTransactionRequest): Promise<CryptoHash>;
|
113
|
+
broadcastTxCommit(params?: RpcSendTransactionRequest): Promise<RpcTransactionResponse>;
|
114
|
+
changes(params?: RpcStateChangesInBlockByTypeRequest): Promise<RpcStateChangesInBlockResponse>;
|
115
|
+
chunk(params?: RpcChunkRequest): Promise<RpcChunkResponse>;
|
116
|
+
clientConfig(params?: RpcClientConfigRequest): Promise<RpcClientConfigResponse>;
|
117
|
+
gasPrice(params?: RpcGasPriceRequest): Promise<RpcGasPriceResponse>;
|
118
|
+
genesisConfig(params?: GenesisConfigRequest): Promise<GenesisConfig>;
|
119
|
+
health(params?: RpcHealthRequest): Promise<RpcHealthResponse>;
|
120
|
+
lightClientProof(params?: RpcLightClientExecutionProofRequest): Promise<RpcLightClientExecutionProofResponse>;
|
121
|
+
maintenanceWindows(params?: RpcMaintenanceWindowsRequest): Promise<MaintenanceWindowsResponse>;
|
122
|
+
networkInfo(params?: RpcNetworkInfoRequest): Promise<RpcNetworkInfoResponse>;
|
123
|
+
nextLightClientBlock(params?: RpcLightClientNextBlockRequest): Promise<RpcLightClientNextBlockResponse>;
|
124
|
+
query(params?: RpcQueryRequest): Promise<RpcQueryResponse>;
|
125
|
+
sendTx(params?: RpcSendTransactionRequest): Promise<RpcTransactionResponse>;
|
126
|
+
status(params?: RpcStatusRequest): Promise<RpcStatusResponse>;
|
127
|
+
tx(params?: RpcTransactionStatusRequest): Promise<RpcTransactionResponse>;
|
128
|
+
validators(params?: RpcValidatorRequest): Promise<RpcValidatorResponse>;
|
129
|
+
}
|
130
|
+
interface ConvenienceMethods {
|
131
|
+
viewAccount(params: {
|
132
|
+
accountId: string;
|
133
|
+
finality?: 'final' | 'near-final' | 'optimistic';
|
134
|
+
blockId?: string | number;
|
135
|
+
}): Promise<AccountView>;
|
136
|
+
viewFunction(params: {
|
137
|
+
accountId: string;
|
138
|
+
methodName: string;
|
139
|
+
argsBase64?: string;
|
140
|
+
finality?: 'final' | 'near-final' | 'optimistic';
|
141
|
+
blockId?: string | number;
|
142
|
+
}): Promise<CallResult>;
|
143
|
+
viewAccessKey(params: {
|
144
|
+
accountId: string;
|
145
|
+
publicKey: string;
|
146
|
+
finality?: 'final' | 'near-final' | 'optimistic';
|
147
|
+
blockId?: string | number;
|
148
|
+
}): Promise<AccessKeyView>;
|
149
|
+
}
|
150
|
+
interface CompleteClientInterface extends DynamicRpcMethods, ConvenienceMethods {
|
151
|
+
call<TParams = unknown, TResult = unknown>(method: string, params?: TParams): Promise<TResult>;
|
152
|
+
}
|
153
|
+
declare function experimentalChanges(client: NearRpcClient, params?: RpcStateChangesInBlockByTypeRequest): Promise<RpcStateChangesInBlockResponse>;
|
154
|
+
declare function experimentalChangesInBlock(client: NearRpcClient, params?: RpcStateChangesInBlockRequest): Promise<RpcStateChangesInBlockByTypeResponse>;
|
155
|
+
declare function experimentalCongestionLevel(client: NearRpcClient, params?: RpcCongestionLevelRequest): Promise<RpcCongestionLevelResponse>;
|
156
|
+
declare function experimentalGenesisConfig(client: NearRpcClient, params?: GenesisConfigRequest): Promise<GenesisConfig>;
|
157
|
+
declare function experimentalLightClientBlockProof(client: NearRpcClient, params?: RpcLightClientBlockProofRequest): Promise<RpcLightClientBlockProofResponse>;
|
158
|
+
declare function experimentalLightClientProof(client: NearRpcClient, params?: RpcLightClientExecutionProofRequest): Promise<RpcLightClientExecutionProofResponse>;
|
159
|
+
declare function experimentalMaintenanceWindows(client: NearRpcClient, params?: RpcMaintenanceWindowsRequest): Promise<EXPERIMENTALMaintenanceWindowsResponse>;
|
160
|
+
declare function experimentalProtocolConfig(client: NearRpcClient, params?: RpcProtocolConfigRequest): Promise<RpcProtocolConfigResponse>;
|
161
|
+
declare function experimentalReceipt(client: NearRpcClient, params?: RpcReceiptRequest): Promise<RpcReceiptResponse>;
|
162
|
+
declare function experimentalSplitStorageInfo(client: NearRpcClient, params?: RpcSplitStorageInfoRequest): Promise<RpcSplitStorageInfoResponse>;
|
163
|
+
declare function experimentalTxStatus(client: NearRpcClient, params?: RpcTransactionStatusRequest): Promise<RpcTransactionResponse>;
|
164
|
+
declare function experimentalValidatorsOrdered(client: NearRpcClient, params?: RpcValidatorsOrderedRequest): Promise<EXPERIMENTALValidatorsOrderedResponse>;
|
165
|
+
declare function block(client: NearRpcClient, params?: RpcBlockRequest): Promise<RpcBlockResponse>;
|
166
|
+
declare function blockEffects(client: NearRpcClient, params?: RpcStateChangesInBlockRequest): Promise<RpcStateChangesInBlockByTypeResponse>;
|
167
|
+
declare function broadcastTxAsync(client: NearRpcClient, params?: RpcSendTransactionRequest): Promise<CryptoHash>;
|
168
|
+
declare function broadcastTxCommit(client: NearRpcClient, params?: RpcSendTransactionRequest): Promise<RpcTransactionResponse>;
|
169
|
+
declare function changes(client: NearRpcClient, params?: RpcStateChangesInBlockByTypeRequest): Promise<RpcStateChangesInBlockResponse>;
|
170
|
+
declare function chunk(client: NearRpcClient, params?: RpcChunkRequest): Promise<RpcChunkResponse>;
|
171
|
+
declare function clientConfig(client: NearRpcClient, params?: RpcClientConfigRequest): Promise<RpcClientConfigResponse>;
|
172
|
+
declare function gasPrice(client: NearRpcClient, params?: RpcGasPriceRequest): Promise<RpcGasPriceResponse>;
|
173
|
+
declare function genesisConfig(client: NearRpcClient, params?: GenesisConfigRequest): Promise<GenesisConfig>;
|
174
|
+
declare function health(client: NearRpcClient, params?: RpcHealthRequest): Promise<RpcHealthResponse>;
|
175
|
+
declare function lightClientProof(client: NearRpcClient, params?: RpcLightClientExecutionProofRequest): Promise<RpcLightClientExecutionProofResponse>;
|
176
|
+
declare function maintenanceWindows(client: NearRpcClient, params?: RpcMaintenanceWindowsRequest): Promise<MaintenanceWindowsResponse>;
|
177
|
+
declare function networkInfo(client: NearRpcClient, params?: RpcNetworkInfoRequest): Promise<RpcNetworkInfoResponse>;
|
178
|
+
declare function nextLightClientBlock(client: NearRpcClient, params?: RpcLightClientNextBlockRequest): Promise<RpcLightClientNextBlockResponse>;
|
179
|
+
declare function query(client: NearRpcClient, params?: RpcQueryRequest): Promise<RpcQueryResponse>;
|
180
|
+
declare function sendTx(client: NearRpcClient, params?: RpcSendTransactionRequest): Promise<RpcTransactionResponse>;
|
181
|
+
declare function status(client: NearRpcClient, params?: RpcStatusRequest): Promise<RpcStatusResponse>;
|
182
|
+
declare function tx(client: NearRpcClient, params?: RpcTransactionStatusRequest): Promise<RpcTransactionResponse>;
|
183
|
+
declare function validators(client: NearRpcClient, params?: RpcValidatorRequest): Promise<RpcValidatorResponse>;
|
184
|
+
|
185
|
+
declare function viewAccount(client: NearRpcClient, params: {
|
186
|
+
accountId: string;
|
187
|
+
finality?: 'final' | 'near-final' | 'optimistic';
|
188
|
+
blockId?: string | number;
|
189
|
+
}): Promise<AccountView>;
|
190
|
+
declare function viewFunction(client: NearRpcClient, params: {
|
191
|
+
accountId: string;
|
192
|
+
methodName: string;
|
193
|
+
argsBase64?: string;
|
194
|
+
finality?: 'final' | 'near-final' | 'optimistic';
|
195
|
+
blockId?: string | number;
|
196
|
+
}): Promise<CallResult>;
|
197
|
+
declare function viewAccessKey(client: NearRpcClient, params: {
|
198
|
+
accountId: string;
|
199
|
+
publicKey: string;
|
200
|
+
finality?: 'final' | 'near-final' | 'optimistic';
|
201
|
+
blockId?: string | number;
|
202
|
+
}): Promise<AccessKeyView>;
|
203
|
+
declare function parseCallResultToJson<T = unknown>(callResult: CallResult): T;
|
204
|
+
declare function viewFunctionAsJson<T = unknown>(client: NearRpcClient, params: {
|
205
|
+
accountId: string;
|
206
|
+
methodName: string;
|
207
|
+
argsBase64?: string;
|
208
|
+
finality?: 'final' | 'near-final' | 'optimistic';
|
209
|
+
blockId?: string | number;
|
210
|
+
}): Promise<T>;
|
211
|
+
|
212
|
+
export { blockEffects as A, broadcastTxAsync as B, type ConvenienceMethods as C, type DynamicRpcMethods as D, broadcastTxCommit as E, changes as F, chunk as G, clientConfig as H, gasPrice as I, type JsonRpcRequest as J, genesisConfig as K, health as L, lightClientProof as M, NearRpcClient as N, maintenanceWindows as O, networkInfo as P, nextLightClientBlock as Q, type RpcRequest as R, query as S, sendTx as T, status as U, tx as V, validators as W, viewAccount as X, viewFunction as Y, viewAccessKey as Z, type CompleteClientInterface as a, type ClientConfig as b, type JsonRpcResponse as c, defaultClient as d, enableValidation as e, type JsonRpcError as f, JsonRpcClientError as g, JsonRpcNetworkError as h, type RpcResponse as i, type RpcError as j, NearRpcError as k, experimentalChanges as l, experimentalChangesInBlock as m, experimentalCongestionLevel as n, experimentalGenesisConfig as o, parseCallResultToJson as p, experimentalLightClientBlockProof as q, experimentalLightClientProof as r, experimentalMaintenanceWindows as s, experimentalProtocolConfig as t, experimentalReceipt as u, viewFunctionAsJson as v, experimentalSplitStorageInfo as w, experimentalTxStatus as x, experimentalValidatorsOrdered as y, block as z };
|