@alephium/web3 0.5.1 → 0.5.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/dist/alephium-web3.min.js +1 -1
- package/dist/alephium-web3.min.js.map +1 -1
- package/dist/src/api/explorer-provider.d.ts +1 -1
- package/dist/src/api/explorer-provider.js +6 -4
- package/dist/src/api/index.d.ts +1 -0
- package/dist/src/api/index.js +1 -0
- package/dist/src/api/node-provider.d.ts +1 -1
- package/dist/src/api/node-provider.js +6 -4
- package/dist/src/api/utils.d.ts +6 -0
- package/dist/src/api/utils.js +32 -2
- package/dist/src/global.d.ts +2 -2
- package/dist/src/global.js +4 -4
- package/dist/src/utils/utils.d.ts +1 -1
- package/dist/src/utils/utils.js +3 -3
- package/package.json +2 -1
- package/src/api/explorer-provider.ts +7 -5
- package/src/api/index.ts +1 -0
- package/src/api/node-provider.ts +7 -5
- package/src/api/utils.ts +32 -1
- package/src/global.ts +14 -6
- package/src/utils/utils.ts +1 -1
|
@@ -9,7 +9,7 @@ export declare class ExplorerProvider {
|
|
|
9
9
|
readonly tokens: ExplorerApi<string>['tokens'];
|
|
10
10
|
readonly charts: ExplorerApi<string>['charts'];
|
|
11
11
|
readonly utils: ExplorerApi<string>['utils'];
|
|
12
|
-
constructor(baseUrl: string, apiKey?: string);
|
|
12
|
+
constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch);
|
|
13
13
|
constructor(provider: ExplorerProvider);
|
|
14
14
|
constructor(handler: ApiRequestHandler);
|
|
15
15
|
request: (args: ApiRequestArguments) => Promise<any>;
|
|
@@ -20,23 +20,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
20
20
|
exports.ExplorerProvider = void 0;
|
|
21
21
|
const types_1 = require("./types");
|
|
22
22
|
const api_explorer_1 = require("./api-explorer");
|
|
23
|
-
|
|
23
|
+
const utils_1 = require("./utils");
|
|
24
|
+
function initializeExplorerApi(baseUrl, apiKey, customFetch) {
|
|
24
25
|
const explorerApi = new api_explorer_1.Api({
|
|
25
26
|
baseUrl: baseUrl,
|
|
26
27
|
baseApiParams: { secure: true },
|
|
27
|
-
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
|
|
28
|
+
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
|
|
29
|
+
customFetch: customFetch ?? utils_1.DEFAULT_THROTTLE_FETCH
|
|
28
30
|
});
|
|
29
31
|
explorerApi.setSecurityData(apiKey ?? null);
|
|
30
32
|
return explorerApi;
|
|
31
33
|
}
|
|
32
34
|
class ExplorerProvider {
|
|
33
|
-
constructor(param0, apiKey) {
|
|
35
|
+
constructor(param0, apiKey, customFetch) {
|
|
34
36
|
this.request = (args) => {
|
|
35
37
|
return (0, types_1.request)(this, args);
|
|
36
38
|
};
|
|
37
39
|
let explorerApi;
|
|
38
40
|
if (typeof param0 === 'string') {
|
|
39
|
-
explorerApi = initializeExplorerApi(param0, apiKey);
|
|
41
|
+
explorerApi = initializeExplorerApi(param0, apiKey, customFetch);
|
|
40
42
|
}
|
|
41
43
|
else if (typeof param0 === 'function') {
|
|
42
44
|
explorerApi = new ExplorerProvider('https://1.2.3.4:0');
|
package/dist/src/api/index.d.ts
CHANGED
package/dist/src/api/index.js
CHANGED
|
@@ -12,7 +12,7 @@ export declare class NodeProvider {
|
|
|
12
12
|
readonly utils: NodeApi<string>['utils'];
|
|
13
13
|
readonly miners: NodeApi<string>['miners'];
|
|
14
14
|
readonly events: NodeApi<string>['events'];
|
|
15
|
-
constructor(baseUrl: string, apiKey?: string);
|
|
15
|
+
constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch);
|
|
16
16
|
constructor(provider: NodeProvider);
|
|
17
17
|
constructor(handler: ApiRequestHandler);
|
|
18
18
|
request: (args: ApiRequestArguments) => Promise<any>;
|
|
@@ -20,23 +20,25 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
20
20
|
exports.NodeProvider = void 0;
|
|
21
21
|
const types_1 = require("./types");
|
|
22
22
|
const api_alephium_1 = require("./api-alephium");
|
|
23
|
-
|
|
23
|
+
const utils_1 = require("./utils");
|
|
24
|
+
function initializeNodeApi(baseUrl, apiKey, customFetch) {
|
|
24
25
|
const nodeApi = new api_alephium_1.Api({
|
|
25
26
|
baseUrl: baseUrl,
|
|
26
27
|
baseApiParams: { secure: true },
|
|
27
|
-
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
|
|
28
|
+
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
|
|
29
|
+
customFetch: customFetch ?? utils_1.DEFAULT_THROTTLE_FETCH
|
|
28
30
|
});
|
|
29
31
|
nodeApi.setSecurityData(apiKey ?? null);
|
|
30
32
|
return nodeApi;
|
|
31
33
|
}
|
|
32
34
|
class NodeProvider {
|
|
33
|
-
constructor(param0, apiKey) {
|
|
35
|
+
constructor(param0, apiKey, customFetch) {
|
|
34
36
|
this.request = (args) => {
|
|
35
37
|
return (0, types_1.request)(this, args);
|
|
36
38
|
};
|
|
37
39
|
let nodeApi;
|
|
38
40
|
if (typeof param0 === 'string') {
|
|
39
|
-
nodeApi = initializeNodeApi(param0, apiKey);
|
|
41
|
+
nodeApi = initializeNodeApi(param0, apiKey, customFetch);
|
|
40
42
|
}
|
|
41
43
|
else if (typeof param0 === 'function') {
|
|
42
44
|
nodeApi = new NodeProvider('https://1.2.3.4:0');
|
package/dist/src/api/utils.d.ts
CHANGED
|
@@ -1,6 +1,12 @@
|
|
|
1
|
+
import 'cross-fetch/polyfill';
|
|
1
2
|
export declare function convertHttpResponse<T>(response: {
|
|
2
3
|
data: T;
|
|
3
4
|
error?: {
|
|
4
5
|
detail: string;
|
|
5
6
|
};
|
|
6
7
|
}): T;
|
|
8
|
+
export declare function retryFetch(...fetchParams: Parameters<typeof fetch>): ReturnType<typeof fetch>;
|
|
9
|
+
export declare function throttledFetch(ratePerSec: number): typeof fetch;
|
|
10
|
+
export declare const RETRY_LIMIT_WHEN_429 = 3;
|
|
11
|
+
export declare const DEFAULT_RATE_LIMIT = 3;
|
|
12
|
+
export declare const DEFAULT_THROTTLE_FETCH: typeof fetch;
|
package/dist/src/api/utils.js
CHANGED
|
@@ -17,13 +17,43 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
17
17
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
18
18
|
*/
|
|
19
19
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
20
|
-
exports.convertHttpResponse = void 0;
|
|
20
|
+
exports.DEFAULT_THROTTLE_FETCH = exports.DEFAULT_RATE_LIMIT = exports.RETRY_LIMIT_WHEN_429 = exports.throttledFetch = exports.retryFetch = exports.convertHttpResponse = void 0;
|
|
21
|
+
require("cross-fetch/polyfill");
|
|
22
|
+
const utils_1 = require("../utils");
|
|
23
|
+
const async_sema_1 = require("async-sema");
|
|
21
24
|
function convertHttpResponse(response) {
|
|
22
25
|
if (response.error) {
|
|
23
|
-
throw new Error(`[
|
|
26
|
+
throw new Error(`[API Error] - ${response.error.detail}`);
|
|
24
27
|
}
|
|
25
28
|
else {
|
|
26
29
|
return response.data;
|
|
27
30
|
}
|
|
28
31
|
}
|
|
29
32
|
exports.convertHttpResponse = convertHttpResponse;
|
|
33
|
+
async function retryFetch(...fetchParams) {
|
|
34
|
+
const retry = async (retryCount) => {
|
|
35
|
+
const response = await fetch(...fetchParams);
|
|
36
|
+
if (response.status === 429 && retryCount < exports.RETRY_LIMIT_WHEN_429) {
|
|
37
|
+
const nextCount = retryCount + 1;
|
|
38
|
+
await (0, utils_1.sleep)(nextCount * 500);
|
|
39
|
+
return await retry(nextCount);
|
|
40
|
+
}
|
|
41
|
+
else {
|
|
42
|
+
return response;
|
|
43
|
+
}
|
|
44
|
+
};
|
|
45
|
+
return retry(0);
|
|
46
|
+
}
|
|
47
|
+
exports.retryFetch = retryFetch;
|
|
48
|
+
function throttledFetch(ratePerSec) {
|
|
49
|
+
const limit = (0, async_sema_1.RateLimit)(ratePerSec);
|
|
50
|
+
const customFetch = async (...fetchParams) => {
|
|
51
|
+
await limit();
|
|
52
|
+
return retryFetch(...fetchParams);
|
|
53
|
+
};
|
|
54
|
+
return customFetch;
|
|
55
|
+
}
|
|
56
|
+
exports.throttledFetch = throttledFetch;
|
|
57
|
+
exports.RETRY_LIMIT_WHEN_429 = 3;
|
|
58
|
+
exports.DEFAULT_RATE_LIMIT = 3;
|
|
59
|
+
exports.DEFAULT_THROTTLE_FETCH = throttledFetch(exports.DEFAULT_RATE_LIMIT);
|
package/dist/src/global.d.ts
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import { ExplorerProvider, NodeProvider } from './api';
|
|
2
2
|
export declare function setCurrentNodeProvider(provider: NodeProvider): void;
|
|
3
|
-
export declare function setCurrentNodeProvider(baseUrl: string, apiKey?: string): void;
|
|
3
|
+
export declare function setCurrentNodeProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void;
|
|
4
4
|
export declare function getCurrentNodeProvider(): NodeProvider;
|
|
5
5
|
export declare function setCurrentExplorerProvider(provider: ExplorerProvider): void;
|
|
6
|
-
export declare function setCurrentExplorerProvider(baseUrl: string, apiKey?: string): void;
|
|
6
|
+
export declare function setCurrentExplorerProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void;
|
|
7
7
|
export declare function getCurrentExplorerProvider(): ExplorerProvider | undefined;
|
package/dist/src/global.js
CHANGED
|
@@ -20,9 +20,9 @@ Object.defineProperty(exports, "__esModule", { value: true });
|
|
|
20
20
|
exports.getCurrentExplorerProvider = exports.setCurrentExplorerProvider = exports.getCurrentNodeProvider = exports.setCurrentNodeProvider = void 0;
|
|
21
21
|
const api_1 = require("./api");
|
|
22
22
|
let _currentNodeProvider = undefined;
|
|
23
|
-
function setCurrentNodeProvider(provider, apiKey) {
|
|
23
|
+
function setCurrentNodeProvider(provider, apiKey, customFetch) {
|
|
24
24
|
if (typeof provider == 'string') {
|
|
25
|
-
_currentNodeProvider = new api_1.NodeProvider(provider, apiKey);
|
|
25
|
+
_currentNodeProvider = new api_1.NodeProvider(provider, apiKey, customFetch);
|
|
26
26
|
}
|
|
27
27
|
else {
|
|
28
28
|
_currentNodeProvider = provider;
|
|
@@ -37,9 +37,9 @@ function getCurrentNodeProvider() {
|
|
|
37
37
|
}
|
|
38
38
|
exports.getCurrentNodeProvider = getCurrentNodeProvider;
|
|
39
39
|
let _currentExplorerProvider = undefined;
|
|
40
|
-
function setCurrentExplorerProvider(provider, apiKey) {
|
|
40
|
+
function setCurrentExplorerProvider(provider, apiKey, customFetch) {
|
|
41
41
|
if (typeof provider == 'string') {
|
|
42
|
-
_currentExplorerProvider = new api_1.ExplorerProvider(provider, apiKey);
|
|
42
|
+
_currentExplorerProvider = new api_1.ExplorerProvider(provider, apiKey, customFetch);
|
|
43
43
|
}
|
|
44
44
|
else {
|
|
45
45
|
_currentExplorerProvider = provider;
|
|
@@ -22,7 +22,7 @@ export declare function contractIdFromTx(txId: string, outputIndex: number): str
|
|
|
22
22
|
export declare function subContractId(parentContractId: string, pathInHex: string, group: number): string;
|
|
23
23
|
export declare function stringToHex(str: string): string;
|
|
24
24
|
export declare function hexToString(str: string): string;
|
|
25
|
-
export declare function
|
|
25
|
+
export declare function sleep(ms: number): Promise<void>;
|
|
26
26
|
declare type _Eq<X, Y> = (<T>() => T extends X ? 1 : 2) extends <T>() => T extends Y ? 1 : 2 ? true : false;
|
|
27
27
|
export declare type Eq<X, Y> = _Eq<{
|
|
28
28
|
[P in keyof X]: X[P];
|
package/dist/src/utils/utils.js
CHANGED
|
@@ -20,7 +20,7 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
20
20
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
21
21
|
};
|
|
22
22
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
23
|
-
exports.assertType = exports.
|
|
23
|
+
exports.assertType = exports.sleep = exports.hexToString = exports.stringToHex = exports.subContractId = exports.contractIdFromTx = exports.addressFromContractId = exports.addressFromPublicKey = exports.publicKeyFromPrivateKey = exports.groupOfPrivateKey = exports.binToHex = exports.hexToBinUnsafe = exports.tokenIdFromAddress = exports.contractIdFromAddress = exports.groupOfAddress = exports.isHexString = exports.xorByte = exports.signatureDecode = exports.encodeHexSignature = exports.encodeSignature = void 0;
|
|
24
24
|
const elliptic_1 = require("elliptic");
|
|
25
25
|
const bn_js_1 = __importDefault(require("bn.js"));
|
|
26
26
|
const blakejs_1 = __importDefault(require("blakejs"));
|
|
@@ -222,10 +222,10 @@ function hexToString(str) {
|
|
|
222
222
|
return buffer_1.Buffer.from(str, 'hex').toString();
|
|
223
223
|
}
|
|
224
224
|
exports.hexToString = hexToString;
|
|
225
|
-
function
|
|
225
|
+
function sleep(ms) {
|
|
226
226
|
return new Promise((resolve) => setTimeout(resolve, ms));
|
|
227
227
|
}
|
|
228
|
-
exports.
|
|
228
|
+
exports.sleep = sleep;
|
|
229
229
|
// eslint-disable-next-line @typescript-eslint/no-empty-function, @typescript-eslint/no-unused-vars
|
|
230
230
|
function assertType() { }
|
|
231
231
|
exports.assertType = assertType;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@alephium/web3",
|
|
3
|
-
"version": "0.5.
|
|
3
|
+
"version": "0.5.2",
|
|
4
4
|
"description": "A JS/TS library to interact with the Alephium platform",
|
|
5
5
|
"license": "GPL",
|
|
6
6
|
"main": "dist/src/index.js",
|
|
@@ -41,6 +41,7 @@
|
|
|
41
41
|
"type": "commonjs",
|
|
42
42
|
"dependencies": {
|
|
43
43
|
"@noble/secp256k1": "1.7.1",
|
|
44
|
+
"async-sema": "^3.1.1",
|
|
44
45
|
"base-x": "4.0.0",
|
|
45
46
|
"blakejs": "1.2.1",
|
|
46
47
|
"buffer": "^6.0.3",
|
|
@@ -18,12 +18,14 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
|
|
19
19
|
import { ApiRequestArguments, ApiRequestHandler, forwardRequests, request } from './types'
|
|
20
20
|
import { Api as ExplorerApi } from './api-explorer'
|
|
21
|
+
import { DEFAULT_THROTTLE_FETCH } from './utils'
|
|
21
22
|
|
|
22
|
-
function initializeExplorerApi(baseUrl: string, apiKey?: string): ExplorerApi<string> {
|
|
23
|
+
function initializeExplorerApi(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): ExplorerApi<string> {
|
|
23
24
|
const explorerApi = new ExplorerApi<string>({
|
|
24
25
|
baseUrl: baseUrl,
|
|
25
26
|
baseApiParams: { secure: true },
|
|
26
|
-
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
|
|
27
|
+
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
|
|
28
|
+
customFetch: customFetch ?? DEFAULT_THROTTLE_FETCH
|
|
27
29
|
})
|
|
28
30
|
explorerApi.setSecurityData(apiKey ?? null)
|
|
29
31
|
return explorerApi
|
|
@@ -39,13 +41,13 @@ export class ExplorerProvider {
|
|
|
39
41
|
readonly charts: ExplorerApi<string>['charts']
|
|
40
42
|
readonly utils: ExplorerApi<string>['utils']
|
|
41
43
|
|
|
42
|
-
constructor(baseUrl: string, apiKey?: string)
|
|
44
|
+
constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch)
|
|
43
45
|
constructor(provider: ExplorerProvider)
|
|
44
46
|
constructor(handler: ApiRequestHandler)
|
|
45
|
-
constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string) {
|
|
47
|
+
constructor(param0: string | ExplorerProvider | ApiRequestHandler, apiKey?: string, customFetch?: typeof fetch) {
|
|
46
48
|
let explorerApi: ExplorerProvider
|
|
47
49
|
if (typeof param0 === 'string') {
|
|
48
|
-
explorerApi = initializeExplorerApi(param0, apiKey)
|
|
50
|
+
explorerApi = initializeExplorerApi(param0, apiKey, customFetch)
|
|
49
51
|
} else if (typeof param0 === 'function') {
|
|
50
52
|
explorerApi = new ExplorerProvider('https://1.2.3.4:0')
|
|
51
53
|
forwardRequests(explorerApi, param0 as ApiRequestHandler)
|
package/src/api/index.ts
CHANGED
package/src/api/node-provider.ts
CHANGED
|
@@ -18,12 +18,14 @@ along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
18
18
|
|
|
19
19
|
import { ApiRequestArguments, ApiRequestHandler, forwardRequests, request } from './types'
|
|
20
20
|
import { Api as NodeApi } from './api-alephium'
|
|
21
|
+
import { DEFAULT_THROTTLE_FETCH } from './utils'
|
|
21
22
|
|
|
22
|
-
function initializeNodeApi(baseUrl: string, apiKey?: string): NodeApi<string> {
|
|
23
|
+
function initializeNodeApi(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): NodeApi<string> {
|
|
23
24
|
const nodeApi = new NodeApi<string>({
|
|
24
25
|
baseUrl: baseUrl,
|
|
25
26
|
baseApiParams: { secure: true },
|
|
26
|
-
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {})
|
|
27
|
+
securityWorker: (accessToken) => (accessToken !== null ? { headers: { 'X-API-KEY': `${accessToken}` } } : {}),
|
|
28
|
+
customFetch: customFetch ?? DEFAULT_THROTTLE_FETCH
|
|
27
29
|
})
|
|
28
30
|
nodeApi.setSecurityData(apiKey ?? null)
|
|
29
31
|
return nodeApi
|
|
@@ -42,13 +44,13 @@ export class NodeProvider {
|
|
|
42
44
|
readonly miners: NodeApi<string>['miners']
|
|
43
45
|
readonly events: NodeApi<string>['events']
|
|
44
46
|
|
|
45
|
-
constructor(baseUrl: string, apiKey?: string)
|
|
47
|
+
constructor(baseUrl: string, apiKey?: string, customFetch?: typeof fetch)
|
|
46
48
|
constructor(provider: NodeProvider)
|
|
47
49
|
constructor(handler: ApiRequestHandler)
|
|
48
|
-
constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string) {
|
|
50
|
+
constructor(param0: string | NodeProvider | ApiRequestHandler, apiKey?: string, customFetch?: typeof fetch) {
|
|
49
51
|
let nodeApi: NodeProvider
|
|
50
52
|
if (typeof param0 === 'string') {
|
|
51
|
-
nodeApi = initializeNodeApi(param0, apiKey)
|
|
53
|
+
nodeApi = initializeNodeApi(param0, apiKey, customFetch)
|
|
52
54
|
} else if (typeof param0 === 'function') {
|
|
53
55
|
nodeApi = new NodeProvider('https://1.2.3.4:0')
|
|
54
56
|
forwardRequests(nodeApi, param0 as ApiRequestHandler)
|
package/src/api/utils.ts
CHANGED
|
@@ -16,10 +16,41 @@ You should have received a copy of the GNU Lesser General Public License
|
|
|
16
16
|
along with the library. If not, see <http://www.gnu.org/licenses/>.
|
|
17
17
|
*/
|
|
18
18
|
|
|
19
|
+
import 'cross-fetch/polyfill'
|
|
20
|
+
import { sleep } from '../utils'
|
|
21
|
+
import { RateLimit } from 'async-sema'
|
|
22
|
+
|
|
19
23
|
export function convertHttpResponse<T>(response: { data: T; error?: { detail: string } }): T {
|
|
20
24
|
if (response.error) {
|
|
21
|
-
throw new Error(`[
|
|
25
|
+
throw new Error(`[API Error] - ${response.error.detail}`)
|
|
22
26
|
} else {
|
|
23
27
|
return response.data
|
|
24
28
|
}
|
|
25
29
|
}
|
|
30
|
+
|
|
31
|
+
export async function retryFetch(...fetchParams: Parameters<typeof fetch>): ReturnType<typeof fetch> {
|
|
32
|
+
const retry = async (retryCount: number): ReturnType<typeof fetch> => {
|
|
33
|
+
const response = await fetch(...fetchParams)
|
|
34
|
+
if (response.status === 429 && retryCount < RETRY_LIMIT_WHEN_429) {
|
|
35
|
+
const nextCount = retryCount + 1
|
|
36
|
+
await sleep(nextCount * 500)
|
|
37
|
+
return await retry(nextCount)
|
|
38
|
+
} else {
|
|
39
|
+
return response
|
|
40
|
+
}
|
|
41
|
+
}
|
|
42
|
+
return retry(0)
|
|
43
|
+
}
|
|
44
|
+
|
|
45
|
+
export function throttledFetch(ratePerSec: number): typeof fetch {
|
|
46
|
+
const limit = RateLimit(ratePerSec)
|
|
47
|
+
const customFetch = async (...fetchParams: Parameters<typeof fetch>): ReturnType<typeof fetch> => {
|
|
48
|
+
await limit()
|
|
49
|
+
return retryFetch(...fetchParams)
|
|
50
|
+
}
|
|
51
|
+
return customFetch
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const RETRY_LIMIT_WHEN_429 = 3
|
|
55
|
+
export const DEFAULT_RATE_LIMIT = 3
|
|
56
|
+
export const DEFAULT_THROTTLE_FETCH = throttledFetch(DEFAULT_RATE_LIMIT)
|
package/src/global.ts
CHANGED
|
@@ -21,10 +21,14 @@ import { ExplorerProvider, NodeProvider } from './api'
|
|
|
21
21
|
let _currentNodeProvider: NodeProvider | undefined = undefined
|
|
22
22
|
|
|
23
23
|
export function setCurrentNodeProvider(provider: NodeProvider): void
|
|
24
|
-
export function setCurrentNodeProvider(baseUrl: string, apiKey?: string): void
|
|
25
|
-
export function setCurrentNodeProvider(
|
|
24
|
+
export function setCurrentNodeProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void
|
|
25
|
+
export function setCurrentNodeProvider(
|
|
26
|
+
provider: NodeProvider | string,
|
|
27
|
+
apiKey?: string,
|
|
28
|
+
customFetch?: typeof fetch
|
|
29
|
+
): void {
|
|
26
30
|
if (typeof provider == 'string') {
|
|
27
|
-
_currentNodeProvider = new NodeProvider(provider, apiKey)
|
|
31
|
+
_currentNodeProvider = new NodeProvider(provider, apiKey, customFetch)
|
|
28
32
|
} else {
|
|
29
33
|
_currentNodeProvider = provider
|
|
30
34
|
}
|
|
@@ -40,10 +44,14 @@ export function getCurrentNodeProvider(): NodeProvider {
|
|
|
40
44
|
let _currentExplorerProvider: ExplorerProvider | undefined = undefined
|
|
41
45
|
|
|
42
46
|
export function setCurrentExplorerProvider(provider: ExplorerProvider): void
|
|
43
|
-
export function setCurrentExplorerProvider(baseUrl: string, apiKey?: string): void
|
|
44
|
-
export function setCurrentExplorerProvider(
|
|
47
|
+
export function setCurrentExplorerProvider(baseUrl: string, apiKey?: string, customFetch?: typeof fetch): void
|
|
48
|
+
export function setCurrentExplorerProvider(
|
|
49
|
+
provider: ExplorerProvider | string,
|
|
50
|
+
apiKey?: string,
|
|
51
|
+
customFetch?: typeof fetch
|
|
52
|
+
): void {
|
|
45
53
|
if (typeof provider == 'string') {
|
|
46
|
-
_currentExplorerProvider = new ExplorerProvider(provider, apiKey)
|
|
54
|
+
_currentExplorerProvider = new ExplorerProvider(provider, apiKey, customFetch)
|
|
47
55
|
} else {
|
|
48
56
|
_currentExplorerProvider = provider
|
|
49
57
|
}
|
package/src/utils/utils.ts
CHANGED
|
@@ -227,7 +227,7 @@ export function hexToString(str: string): string {
|
|
|
227
227
|
return Buffer.from(str, 'hex').toString()
|
|
228
228
|
}
|
|
229
229
|
|
|
230
|
-
export function
|
|
230
|
+
export function sleep(ms: number): Promise<void> {
|
|
231
231
|
return new Promise((resolve) => setTimeout(resolve, ms))
|
|
232
232
|
}
|
|
233
233
|
|