@fleet-sdk/blockchain-providers 0.6.0 → 0.6.2
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 +17 -0
- package/dist/index.d.mts +44 -12
- package/dist/index.d.ts +44 -12
- package/dist/index.js +176 -107
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +172 -109
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
- package/src/ergo-graphql/ergoGraphQLProvider.ts +149 -137
- package/src/index.ts +3 -0
- package/src/utils/graphql.ts +1 -5
- package/src/utils/networking.ts +0 -1
package/CHANGELOG.md
CHANGED
@@ -1,5 +1,22 @@
|
|
1
1
|
# @fleet-sdk/blockchain-providers
|
2
2
|
|
3
|
+
## 0.6.2
|
4
|
+
|
5
|
+
### Patch Changes
|
6
|
+
|
7
|
+
- 401268b: Support `take` and `skip` params in `ErgoGraphQLProvider#stream*()` methods
|
8
|
+
|
9
|
+
## 0.6.1
|
10
|
+
|
11
|
+
### Patch Changes
|
12
|
+
|
13
|
+
- 208ac8e: Fix `Header#votes` encoding
|
14
|
+
- a3d2a62: Export `BlockChainProviders` types
|
15
|
+
- 506dfc4: [`ErgoGraphQLProvider`] Query Addresses/ErgoTrees by chunks of 20
|
16
|
+
- 6245bc9: Export `request` and `createGqlOperation` utility functions
|
17
|
+
- Updated dependencies [ece573c]
|
18
|
+
- @fleet-sdk/core@0.6.1
|
19
|
+
|
3
20
|
## 0.6.0
|
4
21
|
|
5
22
|
### Minor Changes
|
package/dist/index.d.mts
CHANGED
@@ -302,10 +302,27 @@ type URLLike = string | Route;
|
|
302
302
|
type FallbackRetryOptions = {
|
303
303
|
fallbacks?: URLLike[];
|
304
304
|
} & RetryOptions;
|
305
|
+
type FetchOptions = {
|
306
|
+
parser?: ParserLike;
|
307
|
+
base?: string;
|
308
|
+
query?: Record<string, unknown>;
|
309
|
+
retry?: FallbackRetryOptions;
|
310
|
+
httpOptions?: RequestInit;
|
311
|
+
};
|
312
|
+
declare function request<T>(path: string, opt?: Partial<FetchOptions>): Promise<T>;
|
305
313
|
type RetryOptions = {
|
306
314
|
attempts: number;
|
307
315
|
delay: number;
|
308
316
|
};
|
317
|
+
/**
|
318
|
+
* Retries an asynchronous operation a specified number of times with a delay
|
319
|
+
* growing exponentially between each attempt.
|
320
|
+
* @param operation - The asynchronous operation to retry.
|
321
|
+
* @param options - The retry options.
|
322
|
+
* @returns A promise that resolves to the result of the operation, or undefined
|
323
|
+
* if all attempts fail.
|
324
|
+
*/
|
325
|
+
declare function exponentialRetry<T>(operation: (remainingAttempts: number) => Promise<T>, { attempts, delay }: RetryOptions): Promise<T>;
|
309
326
|
|
310
327
|
type GraphQLVariables = Record<string, unknown> | null;
|
311
328
|
interface GraphQLError {
|
@@ -321,6 +338,7 @@ interface GraphQLErrorResponse {
|
|
321
338
|
}
|
322
339
|
type GraphQLResponse<T = unknown> = GraphQLSuccessResponse<T> | GraphQLErrorResponse;
|
323
340
|
type GraphQLOperation<R extends GraphQLResponse, V extends GraphQLVariables> = (variables?: V, url?: string) => Promise<R>;
|
341
|
+
type GraphQLRequiredUrlOperation<R extends GraphQLResponse, V extends GraphQLVariables> = (variables: V | undefined, url: string) => Promise<R>;
|
324
342
|
interface GraphQLRequestOptions {
|
325
343
|
url?: string;
|
326
344
|
parser?: ParserLike;
|
@@ -328,39 +346,52 @@ interface GraphQLRequestOptions {
|
|
328
346
|
throwOnNonNetworkErrors?: boolean;
|
329
347
|
httpOptions?: Omit<RequestInit, "body" | "method">;
|
330
348
|
}
|
331
|
-
|
332
|
-
|
349
|
+
declare function createGqlOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options: GraphQLRequestOptions & {
|
350
|
+
throwOnNonNetworkErrors: true;
|
351
|
+
}): GraphQLOperation<GraphQLSuccessResponse<R>, V>;
|
352
|
+
declare function createGqlOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options?: GraphQLRequestOptions & {
|
353
|
+
url: undefined;
|
354
|
+
}): GraphQLRequiredUrlOperation<GraphQLResponse<R>, V>;
|
355
|
+
declare function createGqlOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options: GraphQLRequestOptions & {
|
356
|
+
url: undefined;
|
357
|
+
throwOnNonNetworkErrors: true;
|
358
|
+
}): GraphQLRequiredUrlOperation<GraphQLSuccessResponse<R>, V>;
|
359
|
+
declare function createGqlOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options: GraphQLRequestOptions): GraphQLOperation<GraphQLResponse<R>, V>;
|
360
|
+
declare function gql(query: TemplateStringsArray): string;
|
361
|
+
declare function getOpName(query: string): string | undefined;
|
362
|
+
|
363
|
+
type SkipAndTake = {
|
364
|
+
skip?: number;
|
365
|
+
take?: number;
|
366
|
+
};
|
333
367
|
type GraphQLBoxWhere = BoxWhere & {
|
334
|
-
/** Base16-encoded BoxIds */
|
335
|
-
boxIds?: HexString[];
|
336
368
|
/** Base16-encoded ErgoTrees */
|
337
369
|
ergoTrees?: HexString[];
|
338
370
|
/** Base58-encoded addresses or `ErgoAddress` objects */
|
339
371
|
addresses?: (Base58String | ErgoAddress)[];
|
340
372
|
};
|
341
373
|
type GraphQLConfirmedTransactionWhere = ConfirmedTransactionWhere & {
|
342
|
-
transactionIds?: HexString[];
|
343
374
|
addresses?: (Base58String | ErgoAddress)[];
|
344
375
|
ergoTrees?: HexString[];
|
345
376
|
};
|
346
377
|
type GraphQLUnconfirmedTransactionWhere = UnconfirmedTransactionWhere & {
|
347
|
-
transactionIds?: HexString[];
|
348
378
|
addresses?: (Base58String | ErgoAddress)[];
|
349
379
|
ergoTrees?: HexString[];
|
350
380
|
};
|
351
381
|
type GraphQLBoxQuery = BoxQuery<GraphQLBoxWhere>;
|
352
|
-
type ErgoGraphQLRequestOptions = Omit<GraphQLRequestOptions, "
|
382
|
+
type ErgoGraphQLRequestOptions = Omit<GraphQLRequestOptions, "throwOnNonNetworkErrors">;
|
383
|
+
type BiMapper<T> = (value: string) => T;
|
353
384
|
declare class ErgoGraphQLProvider<I = bigint> implements IBlockchainProvider<I> {
|
354
385
|
#private;
|
355
386
|
constructor(url: string);
|
356
|
-
constructor(
|
387
|
+
constructor(options: ErgoGraphQLRequestOptions);
|
357
388
|
setUrl(url: string): ErgoGraphQLProvider<I>;
|
358
389
|
setBigIntMapper<M>(mapper: BiMapper<M>): ErgoGraphQLProvider<M>;
|
359
|
-
streamBoxes(query: GraphQLBoxQuery): AsyncGenerator<ChainProviderBox<I>[]>;
|
390
|
+
streamBoxes(query: GraphQLBoxQuery & SkipAndTake): AsyncGenerator<ChainProviderBox<I>[]>;
|
360
391
|
getBoxes(query: GraphQLBoxQuery): Promise<ChainProviderBox<I>[]>;
|
361
|
-
streamUnconfirmedTransactions(query: TransactionQuery<GraphQLUnconfirmedTransactionWhere>): AsyncIterable<ChainProviderUnconfirmedTransaction<I>[]>;
|
392
|
+
streamUnconfirmedTransactions(query: TransactionQuery<GraphQLUnconfirmedTransactionWhere> & SkipAndTake): AsyncIterable<ChainProviderUnconfirmedTransaction<I>[]>;
|
362
393
|
getUnconfirmedTransactions(query: TransactionQuery<GraphQLUnconfirmedTransactionWhere>): Promise<ChainProviderUnconfirmedTransaction<I>[]>;
|
363
|
-
streamConfirmedTransactions(query: TransactionQuery<GraphQLConfirmedTransactionWhere>): AsyncIterable<ChainProviderConfirmedTransaction<I>[]>;
|
394
|
+
streamConfirmedTransactions(query: TransactionQuery<GraphQLConfirmedTransactionWhere> & SkipAndTake): AsyncIterable<ChainProviderConfirmedTransaction<I>[]>;
|
364
395
|
getConfirmedTransactions(query: TransactionQuery<GraphQLConfirmedTransactionWhere>): Promise<ChainProviderConfirmedTransaction<I>[]>;
|
365
396
|
getHeaders(query: HeaderQuery): Promise<BlockHeader[]>;
|
366
397
|
createOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options?: Partial<ErgoGraphQLRequestOptions>): GraphQLOperation<GraphQLSuccessResponse<R>, V>;
|
@@ -368,5 +399,6 @@ declare class ErgoGraphQLProvider<I = bigint> implements IBlockchainProvider<I>
|
|
368
399
|
submitTransaction(signedTransaction: SignedTransaction): Promise<TransactionEvaluationResult>;
|
369
400
|
reduceTransaction(): Promise<TransactionReductionResult>;
|
370
401
|
}
|
402
|
+
declare function isRequestParam(obj: unknown): obj is ErgoGraphQLRequestOptions;
|
371
403
|
|
372
|
-
export { ErgoGraphQLProvider, type ErgoGraphQLRequestOptions, type GraphQLBoxQuery, type GraphQLBoxWhere, type GraphQLConfirmedTransactionWhere, type GraphQLUnconfirmedTransactionWhere };
|
404
|
+
export { type BoxQuery, type BoxSource, type BoxWhere, type ChainProviderBox, type ChainProviderConfirmedTransaction, type ChainProviderUnconfirmedTransaction, type ConfirmedTransactionWhere, ErgoGraphQLProvider, type ErgoGraphQLRequestOptions, type FallbackRetryOptions, type FetchOptions, type GraphQLBoxQuery, type GraphQLBoxWhere, type GraphQLConfirmedTransactionWhere, type GraphQLError, type GraphQLErrorResponse, type GraphQLOperation, type GraphQLRequestOptions, type GraphQLRequiredUrlOperation, type GraphQLResponse, type GraphQLSuccessResponse, type GraphQLUnconfirmedTransactionWhere, type GraphQLVariables, type HeaderQuery, type IBlockchainProvider, type ParserLike, type RetryOptions, type Route, type TransactionEvaluationError, type TransactionEvaluationResult, type TransactionEvaluationSuccess, type TransactionQuery, type TransactionReductionResult, type TransactionReductionSuccess, type URLLike, type UnconfirmedTransactionWhere, createGqlOperation, exponentialRetry, getOpName, gql, isRequestParam, request };
|
package/dist/index.d.ts
CHANGED
@@ -302,10 +302,27 @@ type URLLike = string | Route;
|
|
302
302
|
type FallbackRetryOptions = {
|
303
303
|
fallbacks?: URLLike[];
|
304
304
|
} & RetryOptions;
|
305
|
+
type FetchOptions = {
|
306
|
+
parser?: ParserLike;
|
307
|
+
base?: string;
|
308
|
+
query?: Record<string, unknown>;
|
309
|
+
retry?: FallbackRetryOptions;
|
310
|
+
httpOptions?: RequestInit;
|
311
|
+
};
|
312
|
+
declare function request<T>(path: string, opt?: Partial<FetchOptions>): Promise<T>;
|
305
313
|
type RetryOptions = {
|
306
314
|
attempts: number;
|
307
315
|
delay: number;
|
308
316
|
};
|
317
|
+
/**
|
318
|
+
* Retries an asynchronous operation a specified number of times with a delay
|
319
|
+
* growing exponentially between each attempt.
|
320
|
+
* @param operation - The asynchronous operation to retry.
|
321
|
+
* @param options - The retry options.
|
322
|
+
* @returns A promise that resolves to the result of the operation, or undefined
|
323
|
+
* if all attempts fail.
|
324
|
+
*/
|
325
|
+
declare function exponentialRetry<T>(operation: (remainingAttempts: number) => Promise<T>, { attempts, delay }: RetryOptions): Promise<T>;
|
309
326
|
|
310
327
|
type GraphQLVariables = Record<string, unknown> | null;
|
311
328
|
interface GraphQLError {
|
@@ -321,6 +338,7 @@ interface GraphQLErrorResponse {
|
|
321
338
|
}
|
322
339
|
type GraphQLResponse<T = unknown> = GraphQLSuccessResponse<T> | GraphQLErrorResponse;
|
323
340
|
type GraphQLOperation<R extends GraphQLResponse, V extends GraphQLVariables> = (variables?: V, url?: string) => Promise<R>;
|
341
|
+
type GraphQLRequiredUrlOperation<R extends GraphQLResponse, V extends GraphQLVariables> = (variables: V | undefined, url: string) => Promise<R>;
|
324
342
|
interface GraphQLRequestOptions {
|
325
343
|
url?: string;
|
326
344
|
parser?: ParserLike;
|
@@ -328,39 +346,52 @@ interface GraphQLRequestOptions {
|
|
328
346
|
throwOnNonNetworkErrors?: boolean;
|
329
347
|
httpOptions?: Omit<RequestInit, "body" | "method">;
|
330
348
|
}
|
331
|
-
|
332
|
-
|
349
|
+
declare function createGqlOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options: GraphQLRequestOptions & {
|
350
|
+
throwOnNonNetworkErrors: true;
|
351
|
+
}): GraphQLOperation<GraphQLSuccessResponse<R>, V>;
|
352
|
+
declare function createGqlOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options?: GraphQLRequestOptions & {
|
353
|
+
url: undefined;
|
354
|
+
}): GraphQLRequiredUrlOperation<GraphQLResponse<R>, V>;
|
355
|
+
declare function createGqlOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options: GraphQLRequestOptions & {
|
356
|
+
url: undefined;
|
357
|
+
throwOnNonNetworkErrors: true;
|
358
|
+
}): GraphQLRequiredUrlOperation<GraphQLSuccessResponse<R>, V>;
|
359
|
+
declare function createGqlOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options: GraphQLRequestOptions): GraphQLOperation<GraphQLResponse<R>, V>;
|
360
|
+
declare function gql(query: TemplateStringsArray): string;
|
361
|
+
declare function getOpName(query: string): string | undefined;
|
362
|
+
|
363
|
+
type SkipAndTake = {
|
364
|
+
skip?: number;
|
365
|
+
take?: number;
|
366
|
+
};
|
333
367
|
type GraphQLBoxWhere = BoxWhere & {
|
334
|
-
/** Base16-encoded BoxIds */
|
335
|
-
boxIds?: HexString[];
|
336
368
|
/** Base16-encoded ErgoTrees */
|
337
369
|
ergoTrees?: HexString[];
|
338
370
|
/** Base58-encoded addresses or `ErgoAddress` objects */
|
339
371
|
addresses?: (Base58String | ErgoAddress)[];
|
340
372
|
};
|
341
373
|
type GraphQLConfirmedTransactionWhere = ConfirmedTransactionWhere & {
|
342
|
-
transactionIds?: HexString[];
|
343
374
|
addresses?: (Base58String | ErgoAddress)[];
|
344
375
|
ergoTrees?: HexString[];
|
345
376
|
};
|
346
377
|
type GraphQLUnconfirmedTransactionWhere = UnconfirmedTransactionWhere & {
|
347
|
-
transactionIds?: HexString[];
|
348
378
|
addresses?: (Base58String | ErgoAddress)[];
|
349
379
|
ergoTrees?: HexString[];
|
350
380
|
};
|
351
381
|
type GraphQLBoxQuery = BoxQuery<GraphQLBoxWhere>;
|
352
|
-
type ErgoGraphQLRequestOptions = Omit<GraphQLRequestOptions, "
|
382
|
+
type ErgoGraphQLRequestOptions = Omit<GraphQLRequestOptions, "throwOnNonNetworkErrors">;
|
383
|
+
type BiMapper<T> = (value: string) => T;
|
353
384
|
declare class ErgoGraphQLProvider<I = bigint> implements IBlockchainProvider<I> {
|
354
385
|
#private;
|
355
386
|
constructor(url: string);
|
356
|
-
constructor(
|
387
|
+
constructor(options: ErgoGraphQLRequestOptions);
|
357
388
|
setUrl(url: string): ErgoGraphQLProvider<I>;
|
358
389
|
setBigIntMapper<M>(mapper: BiMapper<M>): ErgoGraphQLProvider<M>;
|
359
|
-
streamBoxes(query: GraphQLBoxQuery): AsyncGenerator<ChainProviderBox<I>[]>;
|
390
|
+
streamBoxes(query: GraphQLBoxQuery & SkipAndTake): AsyncGenerator<ChainProviderBox<I>[]>;
|
360
391
|
getBoxes(query: GraphQLBoxQuery): Promise<ChainProviderBox<I>[]>;
|
361
|
-
streamUnconfirmedTransactions(query: TransactionQuery<GraphQLUnconfirmedTransactionWhere>): AsyncIterable<ChainProviderUnconfirmedTransaction<I>[]>;
|
392
|
+
streamUnconfirmedTransactions(query: TransactionQuery<GraphQLUnconfirmedTransactionWhere> & SkipAndTake): AsyncIterable<ChainProviderUnconfirmedTransaction<I>[]>;
|
362
393
|
getUnconfirmedTransactions(query: TransactionQuery<GraphQLUnconfirmedTransactionWhere>): Promise<ChainProviderUnconfirmedTransaction<I>[]>;
|
363
|
-
streamConfirmedTransactions(query: TransactionQuery<GraphQLConfirmedTransactionWhere>): AsyncIterable<ChainProviderConfirmedTransaction<I>[]>;
|
394
|
+
streamConfirmedTransactions(query: TransactionQuery<GraphQLConfirmedTransactionWhere> & SkipAndTake): AsyncIterable<ChainProviderConfirmedTransaction<I>[]>;
|
364
395
|
getConfirmedTransactions(query: TransactionQuery<GraphQLConfirmedTransactionWhere>): Promise<ChainProviderConfirmedTransaction<I>[]>;
|
365
396
|
getHeaders(query: HeaderQuery): Promise<BlockHeader[]>;
|
366
397
|
createOperation<R, V extends GraphQLVariables = GraphQLVariables>(query: string, options?: Partial<ErgoGraphQLRequestOptions>): GraphQLOperation<GraphQLSuccessResponse<R>, V>;
|
@@ -368,5 +399,6 @@ declare class ErgoGraphQLProvider<I = bigint> implements IBlockchainProvider<I>
|
|
368
399
|
submitTransaction(signedTransaction: SignedTransaction): Promise<TransactionEvaluationResult>;
|
369
400
|
reduceTransaction(): Promise<TransactionReductionResult>;
|
370
401
|
}
|
402
|
+
declare function isRequestParam(obj: unknown): obj is ErgoGraphQLRequestOptions;
|
371
403
|
|
372
|
-
export { ErgoGraphQLProvider, type ErgoGraphQLRequestOptions, type GraphQLBoxQuery, type GraphQLBoxWhere, type GraphQLConfirmedTransactionWhere, type GraphQLUnconfirmedTransactionWhere };
|
404
|
+
export { type BoxQuery, type BoxSource, type BoxWhere, type ChainProviderBox, type ChainProviderConfirmedTransaction, type ChainProviderUnconfirmedTransaction, type ConfirmedTransactionWhere, ErgoGraphQLProvider, type ErgoGraphQLRequestOptions, type FallbackRetryOptions, type FetchOptions, type GraphQLBoxQuery, type GraphQLBoxWhere, type GraphQLConfirmedTransactionWhere, type GraphQLError, type GraphQLErrorResponse, type GraphQLOperation, type GraphQLRequestOptions, type GraphQLRequiredUrlOperation, type GraphQLResponse, type GraphQLSuccessResponse, type GraphQLUnconfirmedTransactionWhere, type GraphQLVariables, type HeaderQuery, type IBlockchainProvider, type ParserLike, type RetryOptions, type Route, type TransactionEvaluationError, type TransactionEvaluationResult, type TransactionEvaluationSuccess, type TransactionQuery, type TransactionReductionResult, type TransactionReductionSuccess, type URLLike, type UnconfirmedTransactionWhere, createGqlOperation, exponentialRetry, getOpName, gql, isRequestParam, request };
|
package/dist/index.js
CHANGED
@@ -4,6 +4,52 @@ var common = require('@fleet-sdk/common');
|
|
4
4
|
var core = require('@fleet-sdk/core');
|
5
5
|
|
6
6
|
// src/ergo-graphql/ergoGraphQLProvider.ts
|
7
|
+
new Uint8Array(new Uint32Array([287454020]).buffer)[0] === 68;
|
8
|
+
var HEXES = Array.from({ length: 256 }, (_, i) => i.toString(16).padStart(2, "0"));
|
9
|
+
var HexChar = {
|
10
|
+
ZERO: 48,
|
11
|
+
// 0
|
12
|
+
NINE: 57,
|
13
|
+
// 9
|
14
|
+
A_UP: 65,
|
15
|
+
// A
|
16
|
+
F_UP: 70,
|
17
|
+
// F
|
18
|
+
A_LO: 97,
|
19
|
+
// a
|
20
|
+
F_LO: 102
|
21
|
+
// f
|
22
|
+
};
|
23
|
+
function bytesToHex(bytes2) {
|
24
|
+
common.assertInstanceOf(bytes2, Uint8Array);
|
25
|
+
let hex3 = "";
|
26
|
+
for (let i = 0, len = bytes2.length; i < len; i++) {
|
27
|
+
hex3 += HEXES[bytes2[i]];
|
28
|
+
}
|
29
|
+
return hex3;
|
30
|
+
}
|
31
|
+
function hexToBytes(hex3) {
|
32
|
+
common.assertTypeOf(hex3, "string");
|
33
|
+
common.assert(hex3.length % 2 === 0, "Invalid hex padding.");
|
34
|
+
const len = hex3.length / 2;
|
35
|
+
const bytes2 = new Uint8Array(len);
|
36
|
+
for (let i = 0, j = 0; i < len; i++) {
|
37
|
+
const n1 = charCodeToBase16(hex3.charCodeAt(j++));
|
38
|
+
const n2 = charCodeToBase16(hex3.charCodeAt(j++));
|
39
|
+
bytes2[i] = n1 * 16 + n2;
|
40
|
+
}
|
41
|
+
return bytes2;
|
42
|
+
}
|
43
|
+
function charCodeToBase16(char) {
|
44
|
+
if (char >= HexChar.ZERO && char <= HexChar.NINE) return char - HexChar.ZERO;
|
45
|
+
if (char >= HexChar.A_UP && char <= HexChar.F_UP) return char - (HexChar.A_UP - 10);
|
46
|
+
if (char >= HexChar.A_LO && char <= HexChar.F_LO) return char - (HexChar.A_LO - 10);
|
47
|
+
throw new Error("Invalid byte sequence.");
|
48
|
+
}
|
49
|
+
var hex2 = {
|
50
|
+
encode: bytesToHex,
|
51
|
+
decode: hexToBytes
|
52
|
+
};
|
7
53
|
async function request(path, opt) {
|
8
54
|
const url = buildURL(path, opt?.query, opt?.base);
|
9
55
|
let response;
|
@@ -73,12 +119,12 @@ function createGqlOperation(query, options) {
|
|
73
119
|
return response;
|
74
120
|
};
|
75
121
|
}
|
122
|
+
function gql(query) {
|
123
|
+
return query[0];
|
124
|
+
}
|
76
125
|
function getOpName(query) {
|
77
126
|
return OP_NAME_REGEX.exec(query)?.at(2);
|
78
127
|
}
|
79
|
-
function isRequestParam(obj) {
|
80
|
-
return typeof obj === "object" && obj.url !== void 0;
|
81
|
-
}
|
82
128
|
|
83
129
|
// src/ergo-graphql/queries.ts
|
84
130
|
var B = [
|
@@ -102,6 +148,7 @@ var UNCONF_TX_QUERY = `query unconfirmedTransactions(${T[0]}) { mempool { transa
|
|
102
148
|
|
103
149
|
// src/ergo-graphql/ergoGraphQLProvider.ts
|
104
150
|
var PAGE_SIZE = 50;
|
151
|
+
var MAX_ARGS = 20;
|
105
152
|
var ErgoGraphQLProvider = class {
|
106
153
|
#options;
|
107
154
|
#biMapper;
|
@@ -114,11 +161,11 @@ var ErgoGraphQLProvider = class {
|
|
114
161
|
#sendTransaction;
|
115
162
|
#getHeaders;
|
116
163
|
constructor(optOrUrl) {
|
164
|
+
this.#biMapper = (value) => BigInt(value);
|
117
165
|
this.#options = {
|
118
166
|
...isRequestParam(optOrUrl) ? optOrUrl : { url: optOrUrl },
|
119
167
|
throwOnNonNetworkErrors: true
|
120
168
|
};
|
121
|
-
this.#biMapper = (value) => BigInt(value);
|
122
169
|
this.#getConfirmedBoxes = this.createOperation(CONF_BOXES_QUERY);
|
123
170
|
this.#getUnconfirmedBoxes = this.createOperation(UNCONF_BOXES_QUERY);
|
124
171
|
this.#getAllBoxes = this.createOperation(ALL_BOXES_QUERY);
|
@@ -129,7 +176,7 @@ var ErgoGraphQLProvider = class {
|
|
129
176
|
this.#getHeaders = this.createOperation(HEADERS_QUERY);
|
130
177
|
}
|
131
178
|
#fetchBoxes(args, inclConf, inclUnconf) {
|
132
|
-
return inclConf && inclUnconf ? this.#getAllBoxes(args
|
179
|
+
return inclConf && inclUnconf ? this.#getAllBoxes(args) : inclUnconf ? this.#getUnconfirmedBoxes(args) : this.#getConfirmedBoxes(args);
|
133
180
|
}
|
134
181
|
setUrl(url) {
|
135
182
|
this.#options.url = url;
|
@@ -145,96 +192,105 @@ var ErgoGraphQLProvider = class {
|
|
145
192
|
}
|
146
193
|
const notBeingSpent = (box) => !box.beingSpent;
|
147
194
|
const returnedBoxIds = /* @__PURE__ */ new Set();
|
148
|
-
const {
|
149
|
-
const
|
150
|
-
|
151
|
-
|
152
|
-
const
|
153
|
-
|
154
|
-
|
155
|
-
|
156
|
-
|
157
|
-
|
158
|
-
|
159
|
-
|
195
|
+
const { from, take } = query;
|
196
|
+
const pageSize = take ?? PAGE_SIZE;
|
197
|
+
const queries = buildGqlBoxQueries(query);
|
198
|
+
const isMempoolAware = from !== "blockchain";
|
199
|
+
for (const query2 of queries) {
|
200
|
+
let inclChain = from !== "mempool";
|
201
|
+
let inclPool = from !== "blockchain";
|
202
|
+
while (inclChain || inclPool) {
|
203
|
+
const { data } = await this.#fetchBoxes(query2, inclChain, inclPool);
|
204
|
+
let boxes = [];
|
205
|
+
if (inclChain && hasConfirmed(data)) {
|
206
|
+
if (common.some(data.boxes)) {
|
207
|
+
const confirmedBoxes = (isMempoolAware ? data.boxes.filter(notBeingSpent) : data.boxes).map((b) => mapConfirmedBox(b, this.#biMapper));
|
208
|
+
boxes = boxes.concat(confirmedBoxes);
|
209
|
+
}
|
210
|
+
inclChain = data.boxes.length === pageSize;
|
160
211
|
}
|
161
|
-
|
162
|
-
|
163
|
-
|
164
|
-
|
165
|
-
|
166
|
-
|
167
|
-
}
|
168
|
-
inclPool = data.mempool.boxes.length === PAGE_SIZE;
|
169
|
-
}
|
170
|
-
if (common.some(boxes)) {
|
171
|
-
if (boxes.some((box) => returnedBoxIds.has(box.boxId))) {
|
172
|
-
boxes = boxes.filter((b) => !returnedBoxIds.has(b.boxId));
|
212
|
+
if (isMempoolAware && hasMempool(data)) {
|
213
|
+
if (common.some(data.mempool.boxes)) {
|
214
|
+
const mempoolBoxes = data.mempool.boxes.filter(notBeingSpent).map((b) => mapUnconfirmedBox(b, this.#biMapper));
|
215
|
+
boxes = boxes.concat(mempoolBoxes);
|
216
|
+
}
|
217
|
+
inclPool = data.mempool.boxes.length === pageSize;
|
173
218
|
}
|
174
219
|
if (common.some(boxes)) {
|
175
|
-
|
176
|
-
|
177
|
-
|
220
|
+
if (boxes.some((box) => returnedBoxIds.has(box.boxId))) {
|
221
|
+
boxes = boxes.filter((b) => !returnedBoxIds.has(b.boxId));
|
222
|
+
}
|
223
|
+
if (common.some(boxes)) {
|
224
|
+
boxes = common.uniqBy(boxes, (box) => box.boxId);
|
225
|
+
for (const box of boxes) returnedBoxIds.add(box.boxId);
|
226
|
+
yield boxes;
|
227
|
+
}
|
178
228
|
}
|
229
|
+
if (inclChain || inclPool) query2.skip += pageSize;
|
179
230
|
}
|
180
|
-
|
181
|
-
} while (inclChain || inclPool);
|
231
|
+
}
|
182
232
|
}
|
183
233
|
async getBoxes(query) {
|
184
234
|
const boxes = [];
|
185
|
-
for await (const
|
235
|
+
for await (const chunk2 of this.streamBoxes(query)) boxes.push(chunk2);
|
186
236
|
return common.orderBy(boxes.flat(), (box) => box.creationHeight);
|
187
237
|
}
|
188
238
|
async *streamUnconfirmedTransactions(query) {
|
189
|
-
const
|
190
|
-
|
191
|
-
|
192
|
-
|
193
|
-
|
194
|
-
|
195
|
-
|
196
|
-
|
239
|
+
const pageSize = query.take ?? PAGE_SIZE;
|
240
|
+
const queries = buildGqlUnconfirmedTxQueries(query);
|
241
|
+
for (const query2 of queries) {
|
242
|
+
let keepFetching = true;
|
243
|
+
while (keepFetching) {
|
244
|
+
const response = await this.#getUnconfirmedTransactions(query2);
|
245
|
+
if (common.some(response.data?.mempool?.transactions)) {
|
246
|
+
yield response.data.mempool.transactions.map(
|
247
|
+
(t) => mapUnconfirmedTransaction(t, this.#biMapper)
|
248
|
+
);
|
249
|
+
}
|
250
|
+
keepFetching = response.data?.mempool?.transactions?.length === pageSize;
|
251
|
+
if (keepFetching) query2.skip += pageSize;
|
197
252
|
}
|
198
|
-
keepFetching = response.data?.mempool?.transactions?.length === PAGE_SIZE;
|
199
|
-
if (keepFetching) args.skip += PAGE_SIZE;
|
200
253
|
}
|
201
254
|
}
|
202
255
|
async getUnconfirmedTransactions(query) {
|
203
256
|
const transactions = [];
|
204
|
-
for await (const
|
205
|
-
transactions.push(
|
257
|
+
for await (const chunk2 of this.streamUnconfirmedTransactions(query)) {
|
258
|
+
transactions.push(chunk2);
|
206
259
|
}
|
207
260
|
return transactions.flat();
|
208
261
|
}
|
209
262
|
async *streamConfirmedTransactions(query) {
|
210
|
-
const
|
211
|
-
|
212
|
-
|
213
|
-
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
263
|
+
const pageSize = query.take ?? PAGE_SIZE;
|
264
|
+
const queries = buildGqlConfirmedTxQueries(query);
|
265
|
+
for (const query2 of queries) {
|
266
|
+
let keepFetching = true;
|
267
|
+
while (keepFetching) {
|
268
|
+
const response = await this.#getConfirmedTransactions(query2);
|
269
|
+
if (common.some(response.data?.transactions)) {
|
270
|
+
yield response.data.transactions.map(
|
271
|
+
(t) => mapConfirmedTransaction(t, this.#biMapper)
|
272
|
+
);
|
273
|
+
}
|
274
|
+
keepFetching = response.data?.transactions?.length === pageSize;
|
275
|
+
if (keepFetching) query2.skip += pageSize;
|
218
276
|
}
|
219
|
-
keepFetching = response.data?.transactions?.length === PAGE_SIZE;
|
220
|
-
if (keepFetching) args.skip += PAGE_SIZE;
|
221
277
|
}
|
222
278
|
}
|
223
279
|
async getConfirmedTransactions(query) {
|
224
280
|
const transactions = [];
|
225
|
-
for await (const
|
226
|
-
transactions.push(
|
281
|
+
for await (const chunk2 of this.streamConfirmedTransactions(query)) {
|
282
|
+
transactions.push(chunk2);
|
227
283
|
}
|
228
284
|
return transactions.flat();
|
229
285
|
}
|
230
286
|
async getHeaders(query) {
|
231
|
-
const response = await this.#getHeaders(query
|
232
|
-
return response.data?.blockHeaders.map((
|
233
|
-
...
|
234
|
-
id:
|
235
|
-
timestamp: Number(
|
236
|
-
nBits: Number(
|
237
|
-
votes:
|
287
|
+
const response = await this.#getHeaders(query);
|
288
|
+
return response.data?.blockHeaders.map((h) => ({
|
289
|
+
...h,
|
290
|
+
id: h.headerId,
|
291
|
+
timestamp: Number(h.timestamp),
|
292
|
+
nBits: Number(h.nBits),
|
293
|
+
votes: hex2.encode(Uint8Array.from(h.votes))
|
238
294
|
})) ?? [];
|
239
295
|
}
|
240
296
|
createOperation(query, options) {
|
@@ -244,10 +300,7 @@ var ErgoGraphQLProvider = class {
|
|
244
300
|
}
|
245
301
|
async checkTransaction(signedTransaction) {
|
246
302
|
try {
|
247
|
-
const response = await this.#checkTransaction(
|
248
|
-
{ signedTransaction },
|
249
|
-
this.#options.url
|
250
|
-
);
|
303
|
+
const response = await this.#checkTransaction({ signedTransaction });
|
251
304
|
return { success: true, transactionId: response.data.checkTransaction };
|
252
305
|
} catch (e) {
|
253
306
|
return { success: false, message: e.message };
|
@@ -255,10 +308,7 @@ var ErgoGraphQLProvider = class {
|
|
255
308
|
}
|
256
309
|
async submitTransaction(signedTransaction) {
|
257
310
|
try {
|
258
|
-
const response = await this.#sendTransaction(
|
259
|
-
{ signedTransaction },
|
260
|
-
this.#options.url
|
261
|
-
);
|
311
|
+
const response = await this.#sendTransaction({ signedTransaction });
|
262
312
|
return { success: true, transactionId: response.data.submitTransaction };
|
263
313
|
} catch (e) {
|
264
314
|
return { success: false, message: e.message };
|
@@ -268,50 +318,52 @@ var ErgoGraphQLProvider = class {
|
|
268
318
|
throw new common.NotSupportedError("Transaction reducing is not supported by ergo-graphql.");
|
269
319
|
}
|
270
320
|
};
|
271
|
-
function
|
272
|
-
const
|
321
|
+
function buildGqlBoxQueries(query) {
|
322
|
+
const ergoTrees = common.uniq(
|
323
|
+
[
|
324
|
+
merge(query.where.ergoTrees, query.where.ergoTree) ?? [],
|
325
|
+
merge(query.where.addresses, query.where.address)?.map(
|
326
|
+
(a) => typeof a === "string" ? core.ErgoAddress.decode(a).ergoTree : a.ergoTree
|
327
|
+
) ?? []
|
328
|
+
].flat()
|
329
|
+
);
|
330
|
+
return common.chunk(ergoTrees, MAX_ARGS).map((chunk2) => ({
|
273
331
|
spent: false,
|
274
|
-
boxIds:
|
275
|
-
ergoTrees:
|
276
|
-
ergoTreeTemplateHash: where.templateHash,
|
277
|
-
tokenId: where.tokenId,
|
278
|
-
skip: 0,
|
279
|
-
take: PAGE_SIZE
|
280
|
-
};
|
281
|
-
const addresses = merge(where.addresses, where.address);
|
282
|
-
if (common.some(addresses)) {
|
283
|
-
const trees = addresses.map(
|
284
|
-
(address) => typeof address === "string" ? core.ErgoAddress.decode(address).ergoTree : address.ergoTree
|
285
|
-
);
|
286
|
-
args.ergoTrees = common.uniq(common.some(args.ergoTrees) ? args.ergoTrees.concat(trees) : trees);
|
287
|
-
}
|
288
|
-
return args;
|
332
|
+
boxIds: query.where.boxId ? [query.where.boxId] : void 0,
|
333
|
+
ergoTrees: chunk2,
|
334
|
+
ergoTreeTemplateHash: query.where.templateHash,
|
335
|
+
tokenId: query.where.tokenId,
|
336
|
+
skip: query.skip ?? 0,
|
337
|
+
take: query.take ?? PAGE_SIZE
|
338
|
+
}));
|
289
339
|
}
|
290
|
-
function
|
340
|
+
function buildGqlUnconfirmedTxQueries(query) {
|
291
341
|
const addresses = common.uniq(
|
292
342
|
[
|
293
|
-
merge(where.addresses, where.address)?.map(
|
343
|
+
merge(query.where.addresses, query.where.address)?.map(
|
294
344
|
(address) => typeof address === "string" ? address : address.encode()
|
295
345
|
) ?? [],
|
296
|
-
merge(where.ergoTrees, where.ergoTree)?.map(
|
346
|
+
merge(query.where.ergoTrees, query.where.ergoTree)?.map(
|
297
347
|
(tree) => core.ErgoAddress.fromErgoTree(tree).encode()
|
298
348
|
) ?? []
|
299
349
|
].flat()
|
300
350
|
);
|
301
|
-
return {
|
302
|
-
addresses:
|
303
|
-
transactionIds:
|
304
|
-
skip: 0,
|
305
|
-
take: PAGE_SIZE
|
306
|
-
};
|
351
|
+
return common.chunk(addresses, MAX_ARGS).map((chunk2) => ({
|
352
|
+
addresses: chunk2.length ? chunk2 : void 0,
|
353
|
+
transactionIds: query.where.transactionId ? [query.where.transactionId] : void 0,
|
354
|
+
skip: query.skip ?? 0,
|
355
|
+
take: query.take ?? PAGE_SIZE
|
356
|
+
}));
|
307
357
|
}
|
308
|
-
function
|
309
|
-
return
|
310
|
-
|
311
|
-
|
312
|
-
|
313
|
-
|
314
|
-
|
358
|
+
function buildGqlConfirmedTxQueries(query) {
|
359
|
+
return buildGqlUnconfirmedTxQueries(
|
360
|
+
query
|
361
|
+
).map((q) => ({
|
362
|
+
...q,
|
363
|
+
headerId: query.where.headerId,
|
364
|
+
minHeight: query.where.minHeight,
|
365
|
+
onlyRelevantOutputs: query.where.onlyRelevantOutputs
|
366
|
+
}));
|
315
367
|
}
|
316
368
|
function merge(array, el) {
|
317
369
|
if (common.isEmpty(array) && common.isUndefined(el)) return;
|
@@ -388,7 +440,24 @@ function mapConfirmedTransaction(tx, mapper) {
|
|
388
440
|
confirmed: true
|
389
441
|
};
|
390
442
|
}
|
443
|
+
function isRequestParam(obj) {
|
444
|
+
return typeof obj === "object" && obj.url !== void 0;
|
445
|
+
}
|
446
|
+
/*! Bundled license information:
|
447
|
+
|
448
|
+
@noble/hashes/esm/utils.js:
|
449
|
+
(*! noble-hashes - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
450
|
+
|
451
|
+
@scure/base/lib/esm/index.js:
|
452
|
+
(*! scure-base - MIT License (c) 2022 Paul Miller (paulmillr.com) *)
|
453
|
+
*/
|
391
454
|
|
392
455
|
exports.ErgoGraphQLProvider = ErgoGraphQLProvider;
|
456
|
+
exports.createGqlOperation = createGqlOperation;
|
457
|
+
exports.exponentialRetry = exponentialRetry;
|
458
|
+
exports.getOpName = getOpName;
|
459
|
+
exports.gql = gql;
|
460
|
+
exports.isRequestParam = isRequestParam;
|
461
|
+
exports.request = request;
|
393
462
|
//# sourceMappingURL=index.js.map
|
394
463
|
//# sourceMappingURL=index.js.map
|